/Users/lyon/j4p/src/j2d/imageproc/ImageProcessor.java
|
1 // Glenn Josefiak
2 // Fairfield University
3 // SW513
4 // Spring 2003
5
6 package j2d.imageproc;
7
8 import java.awt.*;
9 import java.awt.image.BufferedImage;
10 import java.awt.image.ImageObserver;
11 import java.awt.image.PixelGrabber;
12 import java.io.File;
13
14 /**
15 * This class will be the basis of many image processing algorithms.
16 * All image processing algorithms operate on an image and require
17 * the image's height and width. This class encapsulates that
18 * functionality. Also, all image processing operations will require
19 * a performAlgorithm() method. This method is left as abstract.
20 */
21 public abstract class ImageProcessor
22 implements ImageObserver {
23 protected Image imgBaseImage;
24 protected Image imgProcessedImage;
25 protected int baseImageHeight = 0;
26 protected int baseImageWidth = 0;
27
28 /**
29 * Construct a new ImageProcessor.
30 */
31 public ImageProcessor(){
32 imgBaseImage = null;
33 }
34
35 /**
36 * Set the base image reference to an image object read from
37 * the specified file.
38 */
39 public void openImage(File file){
40 String fileName = file.getAbsolutePath();
41 imgBaseImage =
42 Toolkit.getDefaultToolkit().getImage(fileName);
43 imgProcessedImage = imgBaseImage;
44 baseImageWidth = imgBaseImage.getWidth(this);
45 baseImageHeight = imgBaseImage.getHeight(this);
46 }
47
48 /**
49 * Set the base image reference.
50 */
51 public void setBaseImage(Image newImage){
52 imgBaseImage = newImage;
53 imgProcessedImage = imgBaseImage;
54 baseImageWidth = imgBaseImage.getWidth(this);
55 baseImageHeight = imgBaseImage.getHeight(this);
56 }
57
58 /**
59 * Return a handle to the base image.
60 */
61 public Image getBaseImage(){
62 return imgBaseImage;
63 }
64
65 /**
66 * Obtain the width of the base image.
67 */
68 public int getBaseImageWidth(){
69 return baseImageWidth;
70 }
71
72 /**
73 * Obtain the height of the base image.
74 */
75 public int getBaseImageHeight(){
76 return baseImageHeight;
77 }
78
79 /**
80 * Return a handle to the processed image.
81 */
82 public Image getProcessedImage(){
83 return imgProcessedImage;
84 }
85
86 /**
87 * Return imgProcessedImage converted to an array of pixels.
88 */
89 public int[] getPixels(){
90 int pixels[] = new int[baseImageWidth*baseImageHeight];
91
92 try{
93 PixelGrabber pg = new PixelGrabber(
94 imgBaseImage, 0, 0, baseImageWidth, baseImageHeight,
95 pixels, 0, baseImageWidth);
96 pg.grabPixels();
97 }catch(InterruptedException ie){
98 // do nothing for now
99 }
100 return pixels;
101 }
102
103 /**
104 * Store the array of pixels into imgProcessedImage
105 */
106 public void setPixels(int pixels[]){
107 imgProcessedImage = new BufferedImage(baseImageWidth,baseImageHeight,
108 BufferedImage.TYPE_INT_RGB);
109 ((BufferedImage)imgProcessedImage).setRGB(0,0,baseImageWidth,
110 baseImageHeight,pixels,0,baseImageWidth);
111 }
112
113 /**
114 * Implementation of ImageObserver. This is used in case the
115 * image takes some time to load and the width and height are
116 * not immediately available.
117 */
118 public boolean imageUpdate(Image img, int infoflags, int x, int y,
119 int width, int height){
120
121 if ((infoflags & ALLBITS) != 1) {
122 baseImageWidth = width;
123 baseImageHeight = height;
124 return true;
125 } else {
126 return false;
127 }
128 }
129
130 /**
131 * This abstract function must be overridden by any actual
132 * ImageProcessor implementation. This is where the real
133 * image processing happens. If the implementation generates
134 * an output image, performAlgorithm is responsible for
135 * updating imgProcessedImage.
136 */
137 public abstract void performAlgorithm() throws Exception;
138 }