summaryrefslogtreecommitdiff
path: root/java/ImageProcessing/framework/BenchmarkApplet.java
blob: ac125a1d9de259c614bf0e79d013d1a9db1e2702 (plain)
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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
package imaging.framework;

import JACE.Timers.ProfileTimer;
import imaging.filters.*;
import java.awt.*;
import java.awt.image.*;
import java.applet.*;
import java.net.*;
import java.util.*;
import java.io.*;
import gjt.*;

public class BenchmarkApplet extends Applet implements Runnable
{
  public static final Font GLOBAL_FONT = new Font("Dialog", Font.BOLD, 10);
  private static final String NEW_LINE = System.getProperty("line.separator");
  private static final String WELCOME = "Welcome to MedJava Image Processing Benchmarking Tool";
  private static final String COPYRIGHT = "(c) 1997 Distributed Object Computing Group, Washington Univesity";
  private static final String ABORT = "Image Processing Tests Aborted";
  private static final String STOP = "Stop";
  private static final String START= "Start";
  private static final String CLEAR= "Clear";
  private static final String RANDOM= "Create Random Image";
  private static final int INTERNAL_ITERATIONS = 10;
  private static final int TRIALS = 1;
  
  private List images_, filters_;
  private TextArea console_;
  private TextField new_image_;
  private Button start_button_;
  private Button stop_button_;
  private Button clear_button_;
  private Button random_button_;
  private ButtonPanel button_panel_ = new ButtonPanel();
  private Hashtable filter_table_ = new Hashtable();
  private Hashtable image_table_ = new Hashtable();
  private Thread benchmarker_;
  private long elapsed_time_;
  private SpatialFilter filter_;
  private Object monitor_ = new Object();
  
  public void init()
    {
      Panel control_panel = new Panel();
      Panel sub_panel1 = new Panel();
      Panel filter_panel = new Panel();
      Panel image_panel = new Panel();
      Label image_panel_label = new Label("Images");
      Label filter_panel_label = new Label("Filters");

      new_image_ = new TextField();
      console_ = new TextArea();
      images_ = new List();
      filters_ = new List();

      console_.setEditable(false);
      images_.setMultipleSelections(true);
      filters_.setMultipleSelections(true);
      console_.setFont(GLOBAL_FONT);
      images_.setFont(GLOBAL_FONT);
      filters_.setFont(GLOBAL_FONT);
      image_panel_label.setFont(GLOBAL_FONT);
      filter_panel_label.setFont(GLOBAL_FONT);
      
      console_.appendText(WELCOME + NEW_LINE);
      console_.appendText(COPYRIGHT + NEW_LINE);
      
      image_panel.setLayout(new BorderLayout());
      image_panel.add("North", image_panel_label);
      image_panel.add("Center", images_);
      image_panel.add("South", new_image_);

      filter_panel.setLayout(new BorderLayout());
      filter_panel.add("North", filter_panel_label);
      filter_panel.add("Center", filters_);

      sub_panel1.setLayout(new GridLayout(1, 2, 5, 5));
      sub_panel1.add(filter_panel);
      sub_panel1.add(image_panel);

      control_panel.setLayout(new GridLayout(2, 1, 5, 5));
      control_panel.add(sub_panel1);
      control_panel.add(console_);
      
      setLayout(new BorderLayout());

      stop_button_ = button_panel_.add(STOP);
      start_button_ = button_panel_.add(START);
      clear_button_ = button_panel_.add(CLEAR);
      random_button_ = button_panel_.add(RANDOM);

      stop_button_.disable();
      add("Center", control_panel);
      add("South", button_panel_);

      getFilters();
      getImages();      
    }
  
  synchronized public void run()
    {
      Image image;
      String image_name, filter_name;
      int image_height, image_width;
      String[] image_list = images_.getSelectedItems();
      String[] filter_list = filters_.getSelectedItems();
      ImageProducer image_source;
      FilteredImageSource filtered_image;
      //      BenchmarkFrame bframe = BenchmarkFrame.instance();
      
      double cumulative_time;
      double best_time;

      SpatialFilter.setTimer(INTERNAL_ITERATIONS);
      console_.appendText(NEW_LINE);
      console_.appendText("Benchmark tests:  [mem=" +
			  Runtime.getRuntime().freeMemory() +"/" +
			  Runtime.getRuntime().totalMemory() +"]"
			  + NEW_LINE);
      
      for (int i = 0; i < image_list.length; i++)
	{

	  image_name = image_list[i];
	  image = (Image)image_table_.get(image_name);
	  
	  image_width = image.getWidth(this);
	  image_height = image.getHeight(this);
	  
	  console_.appendText("Begining tests for image " + image_name);
	  console_.appendText(" (size " + image_width + "x" + image_height +
			      " = " +  image_height*image_width*4 + " bytes )");
	  console_.appendText(NEW_LINE);

	  console_.appendText(NEW_LINE + " ********** " + NEW_LINE);
	  //	  bframe.init("Begining tests for image " + image_name, image, image_width, image_height);

	  //	  if (! bframe.isShowing())
	  //	    bframe.show();
	  
	  for (int j = 0; j < filter_list.length; j++)
	    {
	      filter_name = filter_list[j];
	      filter_ = (SpatialFilter)filter_table_.get(filter_name);

	      if (filter_ == null)
		System.err.println(filter_name + " is null");
	      
	      best_time = Double.MAX_VALUE;
	      cumulative_time = 0;
	      image_source = image.getSource();
	      filtered_image = new FilteredImageSource(image.getSource(), filter_);

	      for (int l = 0; l < TRIALS; l++)
		{
		  elapsed_time_ = 0;
		  prepareImage(createImage(filtered_image), this);

		  try
		    {
		      //		      synchronized(monitor_)
		      //			{
			  while (elapsed_time_ == 0)
			    {
			      System.out.println("Waiting for image loading to complete.");
			      //			      monitor_.wait();
			      wait();
			    }

			  System.out.println("Image loading has completed.");
			  //			}
		    }
		  catch(Exception excp)
		    {
		      System.out.println(excp);
		      stop();
		    }
		  
		  cumulative_time = (double)elapsed_time_ / (double)INTERNAL_ITERATIONS;		      
		  System.out.println(image_name + " " + filter_name + " " +  elapsed_time_);

		  if (best_time > cumulative_time)
		    best_time = cumulative_time;
		}
	      
	      console_.appendText(filter_name + ": best time from " +
				  TRIALS + " trials of " + INTERNAL_ITERATIONS +
				  " iterations equals: " + best_time + " ms");
	      console_.appendText(NEW_LINE);
	      Thread.yield();
	    }
	  
	  console_.appendText(NEW_LINE);
	}

      console_.appendText("Tests concluded." + NEW_LINE);

      //      if (bframe.isShowing())
      //	bframe.hide();
      
      stop_button_.disable();
      start_button_.enable();
    }

  synchronized public boolean imageUpdate(Image img, int infoflags, int x, int y,
			     int width, int height)
    {
      System.out.println("Image Update called");
      
      boolean return_value = false;
      try
	{
	  if ((infoflags & ALLBITS) != 0 ||
	      (infoflags & SOMEBITS) != 0)
	    {
	      System.out.println("notify called" + width + " " + height);
	      elapsed_time_ = filter_.filterTime();
	      return_value = true;
	      //	      monitor_.notify();
	      notifyAll();
	    }
	  else if ((infoflags & ImageObserver.ABORT) != 0 ||
		   (infoflags & ERROR) != 0)
	    {
	      System.out.println("image error!");
	      //	      stop();
	      return_value = true;
	    }
	}
      catch(SecurityException excp)
	{
	  System.err.println(excp);
	}

      return return_value;
    }
  
  public void stop()
    {
      try
	{
	  if (benchmarker_.isAlive())
	    {
	      BenchmarkFrame bframe = BenchmarkFrame.instance();

	      if (bframe.isShowing())
		bframe.hide();
	      
	      System.out.println("Stopping tests...");
	      benchmarker_.stop();
	      console_.appendText(NEW_LINE + ABORT + NEW_LINE);	      
	    }
	  
	  start_button_.enable();
	  stop_button_.disable();
	}
      catch(SecurityException exception)
	{
	  console_.appendText(NEW_LINE + "Stop Failed." + NEW_LINE);
	}      
    }

  public boolean action(Event evt, Object what)
    {
      if (what.equals(START))
	{
	  boolean proceed = ! (benchmarker_ != null && benchmarker_.isAlive());
	  
	  stop_button_.enable();
	  
	  if (proceed &&
	      images_.getSelectedIndexes() != null &&
	      filters_.getSelectedIndexes() != null)
	    {
	      System.out.println("Starting tests...");
	      start_button_.disable();
	      benchmarker_ = new Thread(this);
	      benchmarker_.setPriority(Math.min(Thread.currentThread().getPriority() + 2,
						Thread.MAX_PRIORITY - 1));
	      benchmarker_.start();
	    }
	}
      else if (what.equals(STOP))
	{
	  stop();
	}
      else if (what.equals(CLEAR))
	{
	  console_.setText("");
	  console_.appendText(WELCOME + NEW_LINE);
	  console_.appendText(COPYRIGHT + NEW_LINE);	  
	}
      else if (what.equals(RANDOM))
	{
	  RandomImageDialog.instance(this).show();
	}	
      else if (evt.target == new_image_)
	{
	  getSingleImage(new_image_.getText());
	}
	

      return true;
    }

  private void getSingleImage(String image_url)
    {
      Image image;
      MediaTracker tracker = new MediaTracker(this);

      try
	{
	  image = getImage(new URL(image_url));
	  tracker.addImage(image, 0);
	  tracker.waitForID(0);
	      
	  if (image != null)
	    {
	      int index = image_url.lastIndexOf('/');
	      
	      if (index != -1)
		image_url = image_url.substring(index + 1);
	      
	      image_table_.put(image_url, image);
	      images_.addItem(image_url);
	    }
	}
      catch(MalformedURLException e)
	{
	  System.err.println(e);
	}
      catch(InterruptedException excp)
	{
	  System.err.println(excp);
	}

    }
  
  private void getImages()
    {
      Image image;
      String image_list = getParameter("images"), image_url;
      StringTokenizer str_tok = new StringTokenizer(image_list, ",");

      while (str_tok.hasMoreTokens())
	{
	  image_url = str_tok.nextToken();
	  getSingleImage(image_url);
	}
    }
  
  private void getFilters()
    {
      URL url;
      Object download;
      String config_file;
      String configInfo= null;
      Choice choice = new Choice ();
      ImageFilter filter;
      
      config_file = getParameter ("configFile");
      
      if (config_file == null)
	config_file = "http://www.cs.wustl.edu/~pjain/java/ACE_wrappers/java/ImageProcessing/framework/filter.conf";
      
      try
	{
	  System.out.println ("Configuration File: " + config_file);
	  // Create a new URL
	  url = new URL (config_file);
	  
	  // Get the input stream and pipe it to a DataInputStream
	  DataInputStream iStream = new DataInputStream (url.openStream ());
	  
	  // Create a buffer to hold all the data we get
	  StringBuffer tempBuf = new StringBuffer ();
	  // Keep reading the data until we are done
	  String tempString = iStream.readLine ();
	  while (tempString != null)
	    {
	      tempBuf.append (tempString);
	      tempBuf.append (" ");
	      tempString = iStream.readLine ();
	    }
	  
	  configInfo = tempBuf.toString ();	  
	  System.out.println(configInfo);
	  
	  if (configInfo != null)
	    {
	      StringTokenizer tokens = new StringTokenizer (configInfo);
	      String fullFilterName = null;
	      String filterName = null;
	      // Now parse the string, picking up filter names. Use these
	      // names to load the actual filters as well add new choices to
	      // the filter choices.
	      while (tokens.hasMoreTokens ())
		{
		  // Get the next token
		  fullFilterName = tokens.nextToken ();
		  filterName = this.extractFilterName (fullFilterName);
		  
		  System.out.println ("Loading: " + fullFilterName);
		  // Load the filter class
		  Class c = Class.forName (fullFilterName);
		  //		Class c = this.filterRepository_.load (filter);

		  filter = (ImageFilter)c.newInstance();

		  if (filter instanceof SpatialFilter)
		    {
		      // Add the filter to the Filter Repository
		      filter_table_.put(filterName, filter);
		      
		      // Add filter name to the list of filter choices
		      filters_.addItem (filterName);
		    }
		}
	    }	  
	}
      catch (Exception e)
	{
	  System.err.println ("Filter not found: " + e);
	}      
  }

  private String extractFilterName (String s)
  {
    String filterName = null;
    StringTokenizer tokens = new StringTokenizer (s, ".");
    while (tokens.hasMoreTokens ())
      filterName = tokens.nextToken ();
    return filterName;
  }

  public void generateRandomImage(String name, int width, int height)
    {
      Image image = createImage(width, height);
      Graphics g = image.getGraphics();
      FilteredImageSource filtered_image;

      if (g != null)
	{
	  ImageFilter filter = new RandomizeFilter();
	  g.fillRect(0, 0, width, height);
	  filtered_image = new FilteredImageSource(image.getSource(), filter);
	  image_table_.put(name, createImage(filtered_image));
	  images_.addItem(name);
	}
    }
}

class RandomImageDialog extends Frame
{
  private static String GENERATE = "Generate";
  private static String CANCEL = "Cancel";
  
  private TextField name_ = new TextField(30),
  width_ = new TextField(5),
    height_= new TextField(5);
  private BenchmarkApplet applet_;
  private Button generate_button_;
  private Button cancel_button_;
  private ButtonPanel button_panel_ = new ButtonPanel();
  
  private static RandomImageDialog instance_;  

  public static RandomImageDialog instance(BenchmarkApplet applet)
    {
      if (instance_ == null)
	instance_ = new RandomImageDialog(applet);
      
      return instance_;	  
    }

  protected RandomImageDialog(BenchmarkApplet applet)
    {
      super("Generate Random Image");
      applet_ = applet;

      Panel controls = new Panel();
      GridBagLayout gb = new GridBagLayout();
      GridBagConstraints gc = new GridBagConstraints();
      Label width = new Label("Width: "),
	height = new Label("Height: "),
	name = new Label("Name: ");

      reshape (0, 0, 300, 200);
      
      controls.setLayout(gb);
      gc.gridx = 0;
      gc.gridy = 0;
      gc.gridwidth = 1;
      gc.insets = new Insets(5,5,5,5);
      gc.anchor = GridBagConstraints.WEST;
      width.setFont(BenchmarkApplet.GLOBAL_FONT);
      gb.setConstraints(width, gc);
      controls.add(width);

      gc.gridx = 1;
      gc.anchor = GridBagConstraints.CENTER;
      gc.fill = GridBagConstraints.HORIZONTAL;
      gb.setConstraints(width_, gc);
      controls.add(width_);
      
      gc.gridx = 2;
      gc.anchor = GridBagConstraints.WEST;
      gc.fill = GridBagConstraints.NONE;
      height.setFont(BenchmarkApplet.GLOBAL_FONT);
      gb.setConstraints(height, gc);
      controls.add(height);

      gc.gridx = 3;
      gc.anchor = GridBagConstraints.CENTER;
      gc.fill = GridBagConstraints.HORIZONTAL;
      gb.setConstraints(height_, gc);
      controls.add(height_);

      gc.gridx = 0;
      gc.gridy = 1;
      gc.gridwidth = 1;
      gc.fill = GridBagConstraints.NONE;
      gc.anchor = GridBagConstraints.WEST;
      name.setFont(BenchmarkApplet.GLOBAL_FONT);
      gb.setConstraints(name, gc);
      controls.add(name);

      gc.gridx = 1;
      gc.gridwidth = GridBagConstraints.REMAINDER;
      gc.anchor = GridBagConstraints.CENTER;
      gc.fill = GridBagConstraints.HORIZONTAL;
      gb.setConstraints(name_, gc);
      controls.add(name_);
      
      setLayout(new BorderLayout());
      generate_button_ = button_panel_.add(GENERATE);
      cancel_button_ = button_panel_.add(CANCEL);
      add("Center", controls);
      add("South", button_panel_);

    }

  public boolean action(Event evt, Object what)
    {
      if (what.equals(GENERATE))
	{
	  String width = width_.getText(),
	    height = height_.getText(),
	    name = name_.getText();
	  
	  try
	    {
	      int width_num = Integer.parseInt(width),
		height_num = Integer.parseInt(height);

	      applet_.generateRandomImage(name, width_num, height_num);
	      dispose();		      	      
	    }
	  catch(NumberFormatException nfe)
	    {
	      width_.setText("");
	      height_.setText("");
	    }
	}
      else if (what.equals(CANCEL))
	dispose();

      return true;
    }  
}

class BenchmarkFrame extends Frame
{
  private String desc_;
  private Image image_;
  private int height_, width_;
  private static BenchmarkFrame instance_;
  private static final Font FONT = new Font("Dialog", Font.BOLD, 12);
  
  public static BenchmarkFrame instance()
    {
      if (instance_ == null)
	instance_ = new BenchmarkFrame();
      
      return instance_;	  
    }
  
  public void init(String desc, Image image, int width, int height)
    {
      reshape(0, 0, width, height + 50);
      setImage(desc, image);
    }
  
  public void setImage(String desc, Image image)
    {/*
      try
	{
	  MediaTracker tracker = new MediaTracker(this);
	  tracker.addImage(image, 0);
	  tracker.waitForID(0);
	}
      catch(InterruptedException excp)
	{
	}
	*/     
      desc_ = desc;
      image_ = image;
    }

  public void paint(Graphics g)
    {
      g.setColor(Color.black);
      g.setFont(FONT);
      g.drawImage(image_, 0, 50, this);      
      g.drawString(desc_, 5, 40);
    }
  
  protected BenchmarkFrame()
    {
      super("Benchmark Frame");
    }
}