KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > awt > image > ReplicateScaleFilter


1 /*
2  * @(#)ReplicateScaleFilter.java 1.20 04/07/16
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package java.awt.image;
9
10 import java.awt.image.ImageConsumer JavaDoc;
11 import java.awt.image.ColorModel JavaDoc;
12 import java.util.Hashtable JavaDoc;
13 import java.awt.Rectangle JavaDoc;
14
15 /**
16  * An ImageFilter class for scaling images using the simplest algorithm.
17  * This class extends the basic ImageFilter Class to scale an existing
18  * image and provide a source for a new image containing the resampled
19  * image. The pixels in the source image are sampled to produce pixels
20  * for an image of the specified size by replicating rows and columns of
21  * pixels to scale up or omitting rows and columns of pixels to scale
22  * down.
23  * <p>It is meant to be used in conjunction with a FilteredImageSource
24  * object to produce scaled versions of existing images. Due to
25  * implementation dependencies, there may be differences in pixel values
26  * of an image filtered on different platforms.
27  *
28  * @see FilteredImageSource
29  * @see ImageFilter
30  *
31  * @version 1.20 07/16/04
32  * @author Jim Graham
33  */

34 public class ReplicateScaleFilter extends ImageFilter JavaDoc {
35
36     /**
37      * The width of the source image.
38      */

39     protected int srcWidth;
40
41     /**
42      * The height of the source image.
43      */

44     protected int srcHeight;
45
46     /**
47      * The target width to scale the image.
48      */

49     protected int destWidth;
50
51     /**
52      * The target height to scale the image.
53      */

54     protected int destHeight;
55
56     /**
57      * An <code>int</code> array containing information about a
58      * row of pixels.
59      */

60     protected int srcrows[];
61
62     /**
63      * An <code>int</code> array containing information about a
64      * column of pixels.
65      */

66     protected int srccols[];
67
68     /**
69      * A <code>byte</code> array initialized with a size of
70      * {@link #destWidth} and used to deliver a row of pixel
71      * data to the {@link ImageConsumer}.
72      */

73     protected Object JavaDoc outpixbuf;
74
75     /**
76      * Constructs a ReplicateScaleFilter that scales the pixels from
77      * its source Image as specified by the width and height parameters.
78      * @param width the target width to scale the image
79      * @param height the target height to scale the image
80      * @throws IllegalArgumentException if <code>width</code> equals
81      * zero or <code>height</code> equals zero
82      */

83     public ReplicateScaleFilter(int width, int height) {
84         if (width == 0 || height == 0) {
85             throw new IllegalArgumentException JavaDoc("Width ("+width+
86                                                 ") and height ("+height+
87                                                 ") must be non-zero");
88         }
89     destWidth = width;
90     destHeight = height;
91     }
92
93     /**
94      * Passes along the properties from the source object after adding a
95      * property indicating the scale applied.
96      * This method invokes <code>super.setProperties</code>,
97      * which might result in additional properties being added.
98      * <p>
99      * Note: This method is intended to be called by the
100      * <code>ImageProducer</code> of the <code>Image</code> whose pixels
101      * are being filtered. Developers using
102      * this class to filter pixels from an image should avoid calling
103      * this method directly since that operation could interfere
104      * with the filtering operation.
105      */

106     public void setProperties(Hashtable JavaDoc<?,?> props) {
107     Hashtable JavaDoc<Object JavaDoc,Object JavaDoc> p = (Hashtable JavaDoc<Object JavaDoc,Object JavaDoc>)props.clone();
108     String JavaDoc key = "rescale";
109     String JavaDoc val = destWidth + "x" + destHeight;
110     Object JavaDoc o = p.get(key);
111     if (o != null && o instanceof String JavaDoc) {
112         val = ((String JavaDoc) o) + ", " + val;
113     }
114     p.put(key, val);
115     super.setProperties(p);
116     }
117
118     /**
119      * Override the dimensions of the source image and pass the dimensions
120      * of the new scaled size to the ImageConsumer.
121      * <p>
122      * Note: This method is intended to be called by the
123      * <code>ImageProducer</code> of the <code>Image</code> whose pixels
124      * are being filtered. Developers using
125      * this class to filter pixels from an image should avoid calling
126      * this method directly since that operation could interfere
127      * with the filtering operation.
128      * @see ImageConsumer
129      */

130     public void setDimensions(int w, int h) {
131     srcWidth = w;
132     srcHeight = h;
133     if (destWidth < 0) {
134         if (destHeight < 0) {
135         destWidth = srcWidth;
136         destHeight = srcHeight;
137         } else {
138         destWidth = srcWidth * destHeight / srcHeight;
139         }
140     } else if (destHeight < 0) {
141         destHeight = srcHeight * destWidth / srcWidth;
142     }
143     consumer.setDimensions(destWidth, destHeight);
144     }
145
146     private void calculateMaps() {
147     srcrows = new int[destHeight + 1];
148     for (int y = 0; y <= destHeight; y++) {
149         srcrows[y] = (2 * y * srcHeight + srcHeight) / (2 * destHeight);
150     }
151     srccols = new int[destWidth + 1];
152     for (int x = 0; x <= destWidth; x++) {
153         srccols[x] = (2 * x * srcWidth + srcWidth) / (2 * destWidth);
154     }
155     }
156    
157     /**
158      * Choose which rows and columns of the delivered byte pixels are
159      * needed for the destination scaled image and pass through just
160      * those rows and columns that are needed, replicated as necessary.
161      * <p>
162      * Note: This method is intended to be called by the
163      * <code>ImageProducer</code> of the <code>Image</code> whose pixels
164      * are being filtered. Developers using
165      * this class to filter pixels from an image should avoid calling
166      * this method directly since that operation could interfere
167      * with the filtering operation.
168      */

169     public void setPixels(int x, int y, int w, int h,
170               ColorModel JavaDoc model, byte pixels[], int off,
171               int scansize) {
172     if (srcrows == null || srccols == null) {
173         calculateMaps();
174     }
175     int sx, sy;
176     int dx1 = (2 * x * destWidth + srcWidth - 1) / (2 * srcWidth);
177     int dy1 = (2 * y * destHeight + srcHeight - 1) / (2 * srcHeight);
178     byte outpix[];
179     if (outpixbuf != null && outpixbuf instanceof byte[]) {
180         outpix = (byte[]) outpixbuf;
181     } else {
182         outpix = new byte[destWidth];
183         outpixbuf = outpix;
184     }
185     for (int dy = dy1; (sy = srcrows[dy]) < y + h; dy++) {
186         int srcoff = off + scansize * (sy - y);
187         int dx;
188         for (dx = dx1; (sx = srccols[dx]) < x + w; dx++) {
189         outpix[dx] = pixels[srcoff + sx - x];
190         }
191         if (dx > dx1) {
192         consumer.setPixels(dx1, dy, dx - dx1, 1,
193                    model, outpix, dx1, destWidth);
194         }
195     }
196     }
197
198     /**
199      * Choose which rows and columns of the delivered int pixels are
200      * needed for the destination scaled image and pass through just
201      * those rows and columns that are needed, replicated as necessary.
202      * <p>
203      * Note: This method is intended to be called by the
204      * <code>ImageProducer</code> of the <code>Image</code> whose pixels
205      * are being filtered. Developers using
206      * this class to filter pixels from an image should avoid calling
207      * this method directly since that operation could interfere
208      * with the filtering operation.
209      */

210     public void setPixels(int x, int y, int w, int h,
211               ColorModel JavaDoc model, int pixels[], int off,
212               int scansize) {
213     if (srcrows == null || srccols == null) {
214         calculateMaps();
215     }
216     int sx, sy;
217     int dx1 = (2 * x * destWidth + srcWidth - 1) / (2 * srcWidth);
218     int dy1 = (2 * y * destHeight + srcHeight - 1) / (2 * srcHeight);
219     int outpix[];
220     if (outpixbuf != null && outpixbuf instanceof int[]) {
221         outpix = (int[]) outpixbuf;
222     } else {
223         outpix = new int[destWidth];
224         outpixbuf = outpix;
225     }
226     for (int dy = dy1; (sy = srcrows[dy]) < y + h; dy++) {
227         int srcoff = off + scansize * (sy - y);
228         int dx;
229         for (dx = dx1; (sx = srccols[dx]) < x + w; dx++) {
230         outpix[dx] = pixels[srcoff + sx - x];
231         }
232         if (dx > dx1) {
233         consumer.setPixels(dx1, dy, dx - dx1, 1,
234                    model, outpix, dx1, destWidth);
235         }
236     }
237     }
238 }
239
Popular Tags