summaryrefslogtreecommitdiff
path: root/java/ImageProcessing/framework/ImageApp.java
blob: 1c6f3a8591223d8f6487d18eb6d556cbc0a51305 (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
package imaging.framework;

import java.util.*;
import java.awt.*;
import java.net.*;
import java.io.*;
import java.awt.image.*;
import java.applet.*;
import gjt.Separator;
import gjt.Util;

public class ImageApp extends Applet
{
  public void init ()
  {
    // Use BorderLayout for our applet frame
    this.setLayout (new BorderLayout ());

    // Now load all the filters specified in the config file
    //    this.loadFilters ();
    this.setupButtonPanel ();
    this.add ("Center", this.imageCanvas_);
    // Check if we are running in test mode
    String testFile = getParameter ("testFile");
    if (testFile != null)
      {
	this.tester_ = new Tester (testFile, this);
	this.tester_.initialize ();
      }
    // Initialize the DialogManager
    DialogManager.initialize (this);
  }

  private void setupButtonPanel ()
  {
    Panel southPanel = new Panel ();
    southPanel.setLayout (new BorderLayout ());
    
    Panel buttonPanel = new Panel ();
    buttonPanel.setLayout (new GridLayout (1, 5));

    this.statusDisplay_ = new StatusDisplay ();
    // Create a panel for all the buttons
    this.filePanel_ = new FilePanel (this);
    this.resetPanel_ = new ResetPanel (this);
    this.zoomPanel_ = new ZoomPanel (this);
    this.filterPanel_ = new FilterPanel (this);
    this.helpPanel_ = new HelpPanel (this);

    buttonPanel.add (this.filePanel_);
    buttonPanel.add (this.resetPanel_);
    buttonPanel.add (this.zoomPanel_);
    buttonPanel.add (this.filterPanel_);
    buttonPanel.add (this.helpPanel_);

    southPanel.add ("North", new Separator ());
    southPanel.add ("Center", buttonPanel);
    southPanel.add ("South", this.statusDisplay_);

    southPanel.resize (400, 400);
  
    // Now add all these components to the main frame
    this.add ("South", southPanel);
    this.add ("North", new Panel ()); // Empty panel (for aesthetics)
    //    this.add ("East", new Panel ()); // Empty panel (for aesthetics)
    //    this.add ("West", new Panel ()); // Empty panel (for aesthetics)
  }

  public void displayStatus (String s)
  {
    this.statusDisplay_.setText (s);
  }

  // Handle all action events
  public void zoomFactor (double zoomFactor)
  {
    this.imageCanvas_.zoomFactor (zoomFactor);
  }

  public void reloadFilters ()
  {
    this.filterPanel_.loadFilters ();
    repaint ();
  }
  
  public Hashtable filterTable ()
  {
    return this.filterTable_;
  }

  public ImageFilter getFilter (String s)
  {
    return (ImageFilter) this.filterTable_.get (s);
  }

  public void apply ()
  {
    ImageFilter filter = this.getFilter (this.filterPanel_.choice ().getSelectedItem ());
    this.apply (filter);
  }

  public void apply (ImageFilter filter)
  {
    if (filter != null)
      {
	Util.getFrame (this).setCursor (Frame.WAIT_CURSOR);
	this.imageCanvas_.applyFilter (filter);
	Util.getFrame (this).setCursor (Frame.DEFAULT_CURSOR);
      }
  }

  public void resetImage ()
  {
    this.imageCanvas_.applyFilter (null);
  }

  public int openURL (String url)
  {
    if (url == null)
      return -1;

    Image image = null;
    try
      {
	image = getImage (new URL (url));
      }
    catch (MalformedURLException e)
      {
	return -1;
      }

    if (image != null)
      {
	// Check if the image was actually loaded. Note that we have
	// to wait for the potential image to finish loading before we
	// know if it is a valid image.
	if (this.imageCanvas_.setImage (image) == -1)
	  return -1;
	else
	  this.filePanel_.enableSaveButton ();
      }
    else
      return -1;
    return 0;
  }

  // maintain compatibility
  public void saveFile (String url)
    {
      this.saveFile (url, null);
    }

  public void saveFile (String url, String authentication)
  {
    ImageSender imageSender = new ImageSender (this);
    imageSender.open (this.imageCanvas_.getImage (), url);
    int bytesSent = imageSender.send (authentication);
    if (bytesSent == -1)
      DialogManager.popDialog (DialogType.NOT_SUPPORTED,
			       "Server does not support uploading or URL not found");
  }

  public Choice getFilters () 
  {
    Choice choice = new Choice ();

    // Add the default choice first
    choice.addItem ("Filters:");

    // Now do the file processing -- to determine which filters need
    // to be loaded.

    // Check if the filename has been previously specified and
    // if not then check if the user has specified the name of the
    // config file
    if (this.configFile_ == null) {
      this.configFile_ = getParameter ("configFile");
      System.out.println ("Configuration File (1): " + this.configFile_);
    }
    if (this.configFile_ == null)
      this.configFile_ = "http://www.cs.wustl.edu/~pjain/java/ACE_wrappers/java/ImageProcessing/framework/filter.conf";

    URL url;
    String configInfo= null;
    try
      {
	System.out.println ("Configuration File: " + this.configFile_);
	// Create a new URL
	url = new URL (this.configFile_);

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

    if (configInfo != null)
      {
	try
	  {
	    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);
		
		// Add the filter to the Filter Repository
		this.filterTable_.put (filterName, 
				       (ImageFilter) c.newInstance ());
	    
		// Add filter name to the list of filter choices
		choice.addItem (filterName);
	      }
	  }
	catch (ClassNotFoundException e)
	  {
	    System.err.println ("Filter not found: " + e);
	    return null;
	  }
	catch (IllegalAccessException e)
	  {
	    System.err.println ("Filter not found: " + e);
	    return null;
	  }
	catch (InstantiationException e)
	  {
	    System.err.println ("Filter not found: " + e);
	    return null;
	  }
      }
    return choice;
  }

  // Extract the short filter name from the full filter name. For
  // example, this method returns "EmbossFilter" if it is given the
  // string "imaging/filters/EmbossFilter" 
  private String extractFilterName (String s)
  {
    String filterName = null;
    StringTokenizer tokens = new StringTokenizer (s, ".");
    while (tokens.hasMoreTokens ())
      filterName = tokens.nextToken ();
    return filterName;
  }

  private Panel centerPanel_ = new Panel ();
  private String configFile_ = null;
  private Choice choice_ = null;
  private ImageCanvas imageCanvas_ = new ImageCanvas ();
  private FilePanel filePanel_;
  private ResetPanel resetPanel_;
  private ZoomPanel zoomPanel_;
  private FilterPanel filterPanel_;
  private HelpPanel helpPanel_;

  private StatusDisplay statusDisplay_;
  private Hashtable filterTable_ = new Hashtable ();
  private Tester tester_;
  
}