-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImageLoader.java
executable file
·467 lines (419 loc) · 18.2 KB
/
ImageLoader.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
// ImageLoader - this class handles the details of loading the browse images
// from the server.
//
//--------------------------------------------------------------------------
import java.awt.Image;
import java.awt.MediaTracker;
class ImageLoader implements Runnable, WorkMonitor
{
private ImagePane imagePane;
private imgViewer applet;
private Image[] mediatrackerList; // list of images currently added to the
// media tracker. Maintained so they can be
// removed.
private boolean[] isIDFree; // array to track the free mediatracker IDs
private Metadata[] scenes; // references to scenes currently loading
private final int maxImageFiles = 9; // max number of image files to
// load at once. Used to size the
// mediatrackerList and isIDFree arrays
private Thread loaderThread; // thread for loading images
private Object loadLock; // mutex for exclusive access
private boolean isLoading; // images are loading flag
private boolean isLoadCancelled;// cancel image loading flag
private boolean killThread; // flag to indicate the thread should
// be killed
private int numImagesToLoad; // total number of images currently loading
private int currImageLoading; // current image loading of total number
// private copies of the parameters passed to loadImages so they can
// be used by the loading thread
private Metadata[] zOrder; // snapshot of the displayed scene z-order
private int cellsToDisplay; // number of cells currently displayed
private int pixelSize; // current display resolution
private Sensor currSensor; // reference to the current sensor
private int filesToLoad; // number of image files to load at once
// create a buffer for storing a load that has been submitted by calling
// loadImages, but not yet started on by the actual load thread. This
// buffer eliminates the need to wait for the image loader thread to
// complete a cancel before submitting a new set of images to load (and
// therefore eliminates some GUI hangs.
private class PendingLoad
{
Metadata[] zOrder;
int cellsToDisplay;
int pixelSize;
Sensor currSensor;
int filesToLoad;
boolean pending;
}
private PendingLoad pendingLoad;
// constructor
//------------
public ImageLoader(imgViewer applet, ImagePane imagePane)
{
this.applet = applet;
this.imagePane = imagePane;
// list of images loaded in the media tracker, and associated IDs and
// scene references
mediatrackerList = new Image[maxImageFiles];
isIDFree = new boolean[maxImageFiles];
scenes = new Metadata[maxImageFiles];
// create objects for loading images in a separate thread
loadLock = new Object();
pendingLoad = new PendingLoad();
loaderThread = new Thread(this,"Image Loader Thread");
loaderThread.start();
}
// methods required for the WorkMonitor interface
//-----------------------------------------------
public String getWorkLabel() { return "Loading Images"; }
public boolean isWorking() { return isBusy(); }
public int getTotalWork() { return numImagesToLoad; }
public int getWorkComplete() { return currImageLoading; }
// method to wait for any of the images being tracked by the media
// tracker to arrive. When one arrives, the ID is freed and the
// method returns. This allows the loader thread to keep kicking off
// new image loads while still being somewhat responsive to the user
// cancelling a load. If an extremely long time goes by without
// receiving an image, some kind of error must have occurred, so
// return false to indicate an error. Normally true is returned.
//-------------------------------------------------------------------
private boolean waitForAny(MediaTracker mt)
{
boolean receivedOne = false;
int waitCount = 0; // count of number of times sleep has been called
if (applet.verboseOutput)
System.out.println("Waiting for any image");
// loop until an image is received
while (!receivedOne)
{
boolean waitingForOne = false;
// check if one of the images has been received
for (int i = 0; i < filesToLoad; i++)
{
// only check IDs that are not free
if (!isIDFree[i])
{
waitingForOne = true;
// ID is in use, so check to see if it is done
if (mt.checkID(i))
{
// FIXME - test code to allow simulating a slow
// network connection
if (applet.slowdown)
{
try {loaderThread.sleep(500);}
catch (InterruptedException e) {}
}
// it is done, so free the ID and break out of the
// loop
receivedOne = true;
scenes[i].image = mediatrackerList[i];
mt.removeImage(mediatrackerList[i]);
mediatrackerList[i] = null;
isIDFree[i] = true;
scenes[i] = null;
break;
}
}
}
// if no allocated IDs were found, issue a warning message and
// break out of the loop (should only happen if there is a
// software bug).
if (!waitingForOne)
{
System.out.println("Warning - nothing to wait for");
break;
}
// if none arrived, sleep for a little while
if (!receivedOne)
{
try {loaderThread.sleep(20);}
catch (InterruptedException e) {}
waitCount++;
// check if we have been waiting for an image to download
// for an extremely long time (9000 is 3 minutes without
// receiving a single image - this is an extremely long
// time even on a slow connection)
if (waitCount > 9000)
return false;
}
}
if (applet.verboseOutput)
System.out.println("received image");
return true;
}
// method to communicate the images to load to the loading thread
//---------------------------------------------------------------
public void loadImages
(
ZOrderList zOrder, // I: zOrder list for load order of images
int cellsToDisplay, // I: number of cells to display
int pixelSize, // I: pixel resolution in meters
Sensor currSensor // I: current sensor reference
)
{
// calculate a guess at the number of files to load at once. The
// goal is to get a good balance between the time it takes to cancel
// a load on a slow connection and the speed of the download for a fast
// connection. Assume that only 75,000 bytes should be loading at
// once.
int fileSize = currSensor.getImageFileSize(pixelSize);
int filesToLoad = 75000/fileSize;
if (filesToLoad < 2)
filesToLoad = 2;
else if (filesToLoad > maxImageFiles)
filesToLoad = maxImageFiles;
// get a snapshot of the zOrder since the load thread will need to
// manipulate it at the same time the GUI thread does
Metadata[] zo = zOrder.getSnapshot();
if (zo == null)
return;
// set the parameters in the pending load buffer. Lock the buffer
// while loading the parameters to protect against the load thread
// reading a half filled buffer.
synchronized (pendingLoad)
{
pendingLoad.zOrder = zo;
pendingLoad.cellsToDisplay = cellsToDisplay;
pendingLoad.pixelSize = pixelSize;
pendingLoad.currSensor = currSensor;
pendingLoad.filesToLoad = filesToLoad;
pendingLoad.pending = true;
// if the load thread is still running, make sure it is cancelled
if (isLoading)
cancelLoad();
// notify the load thread that data is ready to be loaded
pendingLoad.notify();
}
}
// method to cancel a load operation currently underway
//-----------------------------------------------------
public void cancelLoad()
{
// if currently loading, set the cancel flag and interrupt the thread
// in case it is waiting in the media tracker. Note: this should be
// called from the same thread as loadImages. The two methods could
// be synchronized, but it is unnecessary overhead.
if (isLoading)
{
if (applet.verboseOutput)
System.out.println("cancelled");
isLoadCancelled = true;
}
}
// method to stop the load thread when the applet is going out of scope
//---------------------------------------------------------------------
public void killThread()
{
cancelLoad();
synchronized(pendingLoad)
{
killThread = true;
pendingLoad.notify();
}
}
// method to wait until the image load is no longer busy. Note that this
// is hopefully a temporary thing. It is needed to allow preventing
// flushing of images that are currently being loaded (causes applet to
// hang) and changes to the mosaicCells array.
public void waitUntilDone()
{
if (applet.verboseOutput)
System.out.println("Waiting for image load to complete");
synchronized(loadLock)
{
if (applet.verboseOutput)
System.out.println("Image load completed");
}
if (applet.verboseOutput)
System.out.println("Waiting done");
}
// method to return whether the load thread is loading images
//-----------------------------------------------------------
public boolean isBusy()
{
return isLoading || pendingLoad.pending;
}
// main method for the loading thread
//-----------------------------------
public void run()
{
String imgName; // image file name
MediaTracker mt = new MediaTracker(imagePane);
// loop forever, loading images as instructed
while (true)
{
// if the loading flag isn't set, check for a pending load. Note
// that the wait call releases the lock.
while (!isLoading)
{
synchronized (pendingLoad)
{
// if no load pending and the thread hasn't been killed,
// wait for one of them
if (!pendingLoad.pending && !killThread)
{
try
{
if (applet.verboseOutput)
{
System.out.println(
"waiting for load command");
}
pendingLoad.wait();
}
catch (InterruptedException e){}
}
// if load pending now, move the data from the pending
// load to the real load
if (pendingLoad.pending)
{
this.zOrder = pendingLoad.zOrder;
pendingLoad.zOrder = null;
this.cellsToDisplay = pendingLoad.cellsToDisplay;
this.pixelSize = pendingLoad.pixelSize;
this.currSensor = pendingLoad.currSensor;
this.filesToLoad = pendingLoad.filesToLoad;
isLoading = true;
isLoadCancelled = false;
pendingLoad.pending = false;
}
// return from the routine if the killThread flag is set
if (killThread)
{
loaderThread = null;
return;
}
}
}
// load the images while holding the load lock (mainly so
// waitUntilDone can actually block until the load is complete)
synchronized (loadLock)
{
int scenesLoading = 0; // count image scenes being loaded
int zIndex = 0; // start loading scenes at the top
// of the z-order
// initialize all IDs as free
for (int i = 0; i < filesToLoad; i++)
isIDFree[i] = true;
// set up the progress monitoring variables
currImageLoading = 0;
numImagesToLoad = 0;
for (int i = 0; i < zOrder.length; i++)
{
// count scenes that aren't loaded or have the wrong pixel
// size loaded
Metadata scene = zOrder[i];
if ((scene.image == null) || (scene.imageRes != pixelSize))
numImagesToLoad++;
}
// loop through the scenes in the z-order, loading each of them
boolean firstTime = true;
while (zIndex < zOrder.length)
{
Metadata scene = zOrder[zIndex];
zIndex++;
Image img; // temporary image pointer
// exit the loop if loading has been cancelled
if (isLoadCancelled)
break;
// do not load a scene if it is not visible
if (!scene.visible)
continue;
// do not load scene if the correct resolution is already
// loaded
if ((scene.image != null) && (scene.imageRes == pixelSize))
continue;
// get a free media tracker id
int id;
for (id = 0; id < filesToLoad; id++)
{
if (isIDFree[id])
{
isIDFree[id] = false;
break;
}
}
// get the image name for the current scene
imgName = currSensor.makeImageName(scene,pixelSize);
// flush the scene currently referenced for the
// scene to work around a Netscape bug that results in
// image resources not being released automatically as
// part of garbage collection
img = scene.image;
if (img != null)
{
img.flush();
scene.image = null;
}
if (applet.verboseOutput)
System.out.println("Loading image " + imgName);
// start the image loading
img = applet.getImage(CodeBase.getGlovisURL(),imgName);
// save the scene and the resolution
scenes[id] = scene;
scene.imageRes = pixelSize;
// add the image to the tracker
mt.addImage(img,id);
mediatrackerList[id] = img;
// start the load for this image
mt.checkID(id,true);
scenesLoading++;
// if the size of the media tracker list is reached,
// load the contents of the list
if (scenesLoading >= filesToLoad)
{
if (waitForAny(mt))
{
scenesLoading--;
currImageLoading++;
}
else
{
// an error happened while waiting for an image,
// so break out of the loop
scenesLoading = 0;
System.out.println("Error loading images. Image"
+" load cancelled.");
break;
}
}
// only load the topmost scene when in single scene mode
if (firstTime)
{
if (cellsToDisplay == Sensor.SINGLE_SCENE)
break;
firstTime = false;
}
}
// clear reference to the z-order snapshot to allow
// garbage collection on the referenced scenes if needed
zOrder = null;
// wait for all the scenes to show up
while (scenesLoading > 0)
{
if (waitForAny(mt))
{
scenesLoading--;
currImageLoading++;
}
else
{
scenesLoading = 0;
System.out.println("Error loading images. Image"
+" load cancelled.");
}
}
if (isLoadCancelled)
{
if (applet.verboseOutput)
System.out.println("Detected cancel");
}
// clear the loading flag and send an event to notify the
// load is complete
isLoading = false;
imagePane.repaint();
}
}
}
}