summaryrefslogtreecommitdiff
path: root/java/ImageProcessing/framework/ImageProcessor.java
blob: 422298d508fe2465fd3d679b724244e07bf64062 (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
package imaging.framework;

import java.awt.*;
import java.awt.image.*;
import java.util.Vector;

public class ImageProcessor
{
  private Vector pipeline_ = new Vector();

  
  public ImageProcessor()
  {
  }

  public void addFilter(String filter_name, ImageFilter filter)
  {
    pipeline_.addElement(new FilterNode(filter_name, filter));
  }

  public boolean removeFilter(String name)
  {
    FilterNode filter_node;
    
    for (int i = 0; i < pipeline_.size(); i++)
      {
	filter_node =  (FilterNode)pipeline_.elementAt(i);
	if (name.equals(filter_node.getName()))
	  {
	    pipeline_.removeElementAt(i);
	    pipeline_.trimToSize();
	    return true;
	  }
      }

    return false;
  }

  public Image processImage(Image image, ImageFilter filter, Component component)
  {
    Image old_image = image, new_image = old_image;
    MediaTracker tracker = new MediaTracker(component);

    try
      {
	new_image = component.createImage(new FilteredImageSource(old_image.getSource(), filter));
	tracker.addImage(new_image, 0);
	tracker.waitForID(0);
      }
    catch(InterruptedException excp)
      {
	System.out.println("ImageProcessor::processImage Image failed to load.");
	System.out.println(excp);
	return null;
      }

    return new_image;
  }
  
  public Image processImage(Image image, Component component)
  {
    Image old_image = image, new_image = null;
    ImageFilter image_filter;
    FilterNode filter_node;
    MediaTracker tracker = new MediaTracker(component);

    try
      {
	for (int i = 0; i < pipeline_.size(); i++)
	  {
	    filter_node = (FilterNode) pipeline_.elementAt(i);
	    image_filter = filter_node.getFilter();
	    new_image = component.createImage(new FilteredImageSource(old_image.getSource(), image_filter));
	    old_image = new_image;
	    tracker.addImage(new_image, 0);
	    tracker.waitForID(0);
	  }
      }
    catch(InterruptedException excp)
      {
	System.out.println("ImageProcessor::processImage Image failed to load.");
	System.out.println(excp);
	return null;
      }

    return new_image;
  }
}

  
class FilterNode
{
  private String name_;
  private ImageFilter filter_;

  FilterNode(String name, ImageFilter filter)
  {
    name_ = name;
    filter_ = filter;
  }

  String getName()
  {
    return name_;
  }

  ImageFilter getFilter()
  {
    return filter_;
  }
}