summaryrefslogtreecommitdiff
path: root/java/ImageProcessing/framework/TestHandler.java
blob: 84af7809ae868e189a90e6539fa85a2706398432 (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
package imaging.framework;

import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.image.*;
import java.util.*;
import JACE.OS.*;
import JACE.Connection.*;
import JACE.Timers.*;

public class TestHandler extends SvcHandler
{
  public TestHandler (String imageList, ImageApp parent)
  {
    this.imageList_ = imageList;
    this.parent_ = parent;
    this.filterTable_ = this.parent_.filterTable();
  }

  public int open (Object obj)
  {
    // We got called by the Connector so assume connection was set up
    // fine and therfore do not use standard output
    stdOut = false;

    doTesting ();
    return 0;
  }

  public void doTesting ()
  {
    if (imageList_ != null)
      {
	StringTokenizer tokens = new StringTokenizer (imageList_);
	String image = null;
	
	// Now parse the string, picking up image names. 
	while (tokens.hasMoreTokens ())
	  {
	    // Get the next token
	    image = tokens.nextToken ();
	    this.process (image);
	  }
      }
  }

  private void process (String image)
  {
    try
      {
	if (stdOut)
	  System.out.println ("Image: " + image);
	else
	  this.peer ().send ("Image: " + image + "\n");
      }
    catch (IOException e)
      {
	ACE.ERROR (e);
      }

    this.loadImage (image);
    this.processImage (image);
    this.uploadImage (image);
  }

  private void loadImage (String image)
  {
    try
      {
	if (stdOut)
	  System.out.print ("\tLoading...");
	else
	  this.peer ().send ("\tLoading...");

	// Start the timer
	timer_.start ();

	// Load the image
	parent_.openURL (image);

	// Stop the timer
	timer_.stop ();
	long time = timer_.elapsedTime ();
	if (stdOut)
	  System.out.println ("done (" + ((double) time)/1000 + " seconds).");
	else
	  this.peer ().send ("done (" + ((double) time)/1000 + " seconds).\n");
      }
    catch (IOException e)
      {
	ACE.ERROR (e);
      }

  }

  private void processImage (String image)
  {
    try
      {
	if (stdOut)
	  System.out.println ("\tProcessing...");
	else
	  this.peer ().send ("\tProcessing...\n");

	for (Enumeration e = filterTable_.keys (); e.hasMoreElements (); )
	  {
	    String filterName = (String) e.nextElement ();
	    if (stdOut)
	      System.out.print ("\t\t" + filterName + "...");
	    else
	      this.peer ().send ("\t\t" + filterName + "...");

	    ImageFilter filter = (ImageFilter) filterTable_.get (filterName);

	    // Start the timer
	    timer_.start ();

	    this.parent_.apply (filter);

	    // Stop the timer
	    timer_.stop ();
	    long time = timer_.elapsedTime ();

	    if (stdOut)
	      System.out.println ("done (" + ((double) time)/1000 + " seconds).");
	    else
	      this.peer ().send ("done (" + ((double) time)/1000 + " seconds).\n");
	    this.parent_.resetImage ();
	  }
      }
    catch (IOException e)
      {
	ACE.ERROR (e);
      }
  }
   
  private void uploadImage (String image)
  {

  }

  private ImageApp parent_ = null;
  private ProfileTimer timer_ = new ProfileTimer ();
  private String imageList_ = null;
  private boolean stdOut = true;
  private Hashtable filterTable_ = null;

}