summaryrefslogtreecommitdiff
path: root/java/ImageProcessing/framework/Tester.java
blob: 953bbe71e76dc2c59a6d485b96dcd8a41e5b25fb (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
package imaging.framework;

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

public class Tester implements Runnable
{
  public Tester (String testInFile, String testOutFile, ImageApp parent)
  {
    this.testInFile_ = testInFile;
    this.testOutFile_ = testOutFile;
    this.parent_ = parent;
    this.filterTable_ = this.parent_.filterTable();

    // Run in your own thread of control
    (new Thread (this)).start ();
  }

  public void run ()
  {
    URL inputURL;
    URL outputURL;
    String imageList= null;
    try
      {
	System.out.println ("Test Input File: " + this.testInFile_);
	System.out.println ("Test Output File: " + this.testOutFile_);

	// Create URLs
	inputURL = new URL (this.testInFile_);
	outputURL = new URL (this.testOutFile_);
	URLConnection connection = outputURL.openConnection ();

	// Get the input stream and pipe it to a DataInputStream
	DataInputStream iStream = new DataInputStream (inputURL.openStream ());

	// Get the output stream and pipe it to a PrintStream
	oStream_ = new PrintStream (connection.getOutputStream ());

	// 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 ();
	  }
	imageList = tempBuf.toString ();
      }
    catch (MalformedURLException e)
      {
	System.err.println (e);
      }
    catch (IOException e)
      {
	System.err.println (e);
      }

    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.doTesting (image);
	  }
      }
  }

  private void doTesting (String image)
  {
    if (oStream_ == null)
      System.out.println ("Image: " + image);
    else
      oStream_.println ("Image: " + image);

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

  private void loadImage (String image)
  {
    if (oStream_ == null)
      System.out.print ("\tLoading...");
    else
      oStream_.print ("\tLoading...");

    // Start the timer
    timer_.start ();

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

    // Stop the timer
    timer_.stop ();
    long time = timer_.elapsedTime ();
    if (oStream_ == null)
      System.out.println ("done (" + ((double) time)/1000 + " seconds).");
    else
      oStream_.println ("done (" + ((double) time)/1000 + " seconds).");
  }

  private void processImage (String image)
  {
    if (oStream_ == null)
      System.out.println ("\tProcessing...");
    else
      oStream_.println ("\tProcessing...");

    for (Enumeration e = filterTable_.keys (); e.hasMoreElements (); )
      {
	String filterName = (String) e.nextElement ();
	if (oStream_ == null)
	  System.out.print ("\t\t" + filterName + "...");
	else
	  oStream_.print ("\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 (oStream_ == null)
	  System.out.println ("done (" + ((double) time)/1000 + " seconds).");
	else
	  oStream_.println ("done (" + ((double) time)/1000 + " seconds).");
	this.parent_.resetImage ();
      }
  }
   
  private void uploadImage (String image)
  {

  }

  private String testInFile_ = null;
  private String testOutFile_ = null;
  private ImageApp parent_ = null;
  private ProfileTimer timer_ = new ProfileTimer ();
  private Hashtable filterTable_ = null;
  PrintStream oStream_ = null;
}