2
|
1 |
/*
|
|
2 |
* Copyright 1996-2004 Sun Microsystems, Inc. All Rights Reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation. Sun designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Sun in the LICENSE file that accompanied this code.
|
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
23 |
* have any questions.
|
|
24 |
*/
|
|
25 |
|
|
26 |
package java.awt.image;
|
|
27 |
|
|
28 |
import java.awt.image.ImageConsumer;
|
|
29 |
import java.awt.image.ColorModel;
|
|
30 |
import java.util.Hashtable;
|
|
31 |
import java.awt.Rectangle;
|
|
32 |
|
|
33 |
/**
|
|
34 |
* An ImageFilter class for scaling images using the simplest algorithm.
|
|
35 |
* This class extends the basic ImageFilter Class to scale an existing
|
|
36 |
* image and provide a source for a new image containing the resampled
|
|
37 |
* image. The pixels in the source image are sampled to produce pixels
|
|
38 |
* for an image of the specified size by replicating rows and columns of
|
|
39 |
* pixels to scale up or omitting rows and columns of pixels to scale
|
|
40 |
* down.
|
|
41 |
* <p>It is meant to be used in conjunction with a FilteredImageSource
|
|
42 |
* object to produce scaled versions of existing images. Due to
|
|
43 |
* implementation dependencies, there may be differences in pixel values
|
|
44 |
* of an image filtered on different platforms.
|
|
45 |
*
|
|
46 |
* @see FilteredImageSource
|
|
47 |
* @see ImageFilter
|
|
48 |
*
|
|
49 |
* @author Jim Graham
|
|
50 |
*/
|
|
51 |
public class ReplicateScaleFilter extends ImageFilter {
|
|
52 |
|
|
53 |
/**
|
|
54 |
* The width of the source image.
|
|
55 |
*/
|
|
56 |
protected int srcWidth;
|
|
57 |
|
|
58 |
/**
|
|
59 |
* The height of the source image.
|
|
60 |
*/
|
|
61 |
protected int srcHeight;
|
|
62 |
|
|
63 |
/**
|
|
64 |
* The target width to scale the image.
|
|
65 |
*/
|
|
66 |
protected int destWidth;
|
|
67 |
|
|
68 |
/**
|
|
69 |
* The target height to scale the image.
|
|
70 |
*/
|
|
71 |
protected int destHeight;
|
|
72 |
|
|
73 |
/**
|
|
74 |
* An <code>int</code> array containing information about a
|
|
75 |
* row of pixels.
|
|
76 |
*/
|
|
77 |
protected int srcrows[];
|
|
78 |
|
|
79 |
/**
|
|
80 |
* An <code>int</code> array containing information about a
|
|
81 |
* column of pixels.
|
|
82 |
*/
|
|
83 |
protected int srccols[];
|
|
84 |
|
|
85 |
/**
|
|
86 |
* A <code>byte</code> array initialized with a size of
|
|
87 |
* {@link #destWidth} and used to deliver a row of pixel
|
|
88 |
* data to the {@link ImageConsumer}.
|
|
89 |
*/
|
|
90 |
protected Object outpixbuf;
|
|
91 |
|
|
92 |
/**
|
|
93 |
* Constructs a ReplicateScaleFilter that scales the pixels from
|
|
94 |
* its source Image as specified by the width and height parameters.
|
|
95 |
* @param width the target width to scale the image
|
|
96 |
* @param height the target height to scale the image
|
|
97 |
* @throws IllegalArgumentException if <code>width</code> equals
|
|
98 |
* zero or <code>height</code> equals zero
|
|
99 |
*/
|
|
100 |
public ReplicateScaleFilter(int width, int height) {
|
|
101 |
if (width == 0 || height == 0) {
|
|
102 |
throw new IllegalArgumentException("Width ("+width+
|
|
103 |
") and height ("+height+
|
|
104 |
") must be non-zero");
|
|
105 |
}
|
|
106 |
destWidth = width;
|
|
107 |
destHeight = height;
|
|
108 |
}
|
|
109 |
|
|
110 |
/**
|
|
111 |
* Passes along the properties from the source object after adding a
|
|
112 |
* property indicating the scale applied.
|
|
113 |
* This method invokes <code>super.setProperties</code>,
|
|
114 |
* which might result in additional properties being added.
|
|
115 |
* <p>
|
|
116 |
* Note: This method is intended to be called by the
|
|
117 |
* <code>ImageProducer</code> of the <code>Image</code> whose pixels
|
|
118 |
* are being filtered. Developers using
|
|
119 |
* this class to filter pixels from an image should avoid calling
|
|
120 |
* this method directly since that operation could interfere
|
|
121 |
* with the filtering operation.
|
|
122 |
*/
|
|
123 |
public void setProperties(Hashtable<?,?> props) {
|
|
124 |
Hashtable<Object,Object> p = (Hashtable<Object,Object>)props.clone();
|
|
125 |
String key = "rescale";
|
|
126 |
String val = destWidth + "x" + destHeight;
|
|
127 |
Object o = p.get(key);
|
|
128 |
if (o != null && o instanceof String) {
|
|
129 |
val = ((String) o) + ", " + val;
|
|
130 |
}
|
|
131 |
p.put(key, val);
|
|
132 |
super.setProperties(p);
|
|
133 |
}
|
|
134 |
|
|
135 |
/**
|
|
136 |
* Override the dimensions of the source image and pass the dimensions
|
|
137 |
* of the new scaled size to the ImageConsumer.
|
|
138 |
* <p>
|
|
139 |
* Note: This method is intended to be called by the
|
|
140 |
* <code>ImageProducer</code> of the <code>Image</code> whose pixels
|
|
141 |
* are being filtered. Developers using
|
|
142 |
* this class to filter pixels from an image should avoid calling
|
|
143 |
* this method directly since that operation could interfere
|
|
144 |
* with the filtering operation.
|
|
145 |
* @see ImageConsumer
|
|
146 |
*/
|
|
147 |
public void setDimensions(int w, int h) {
|
|
148 |
srcWidth = w;
|
|
149 |
srcHeight = h;
|
|
150 |
if (destWidth < 0) {
|
|
151 |
if (destHeight < 0) {
|
|
152 |
destWidth = srcWidth;
|
|
153 |
destHeight = srcHeight;
|
|
154 |
} else {
|
|
155 |
destWidth = srcWidth * destHeight / srcHeight;
|
|
156 |
}
|
|
157 |
} else if (destHeight < 0) {
|
|
158 |
destHeight = srcHeight * destWidth / srcWidth;
|
|
159 |
}
|
|
160 |
consumer.setDimensions(destWidth, destHeight);
|
|
161 |
}
|
|
162 |
|
|
163 |
private void calculateMaps() {
|
|
164 |
srcrows = new int[destHeight + 1];
|
|
165 |
for (int y = 0; y <= destHeight; y++) {
|
|
166 |
srcrows[y] = (2 * y * srcHeight + srcHeight) / (2 * destHeight);
|
|
167 |
}
|
|
168 |
srccols = new int[destWidth + 1];
|
|
169 |
for (int x = 0; x <= destWidth; x++) {
|
|
170 |
srccols[x] = (2 * x * srcWidth + srcWidth) / (2 * destWidth);
|
|
171 |
}
|
|
172 |
}
|
|
173 |
|
|
174 |
/**
|
|
175 |
* Choose which rows and columns of the delivered byte pixels are
|
|
176 |
* needed for the destination scaled image and pass through just
|
|
177 |
* those rows and columns that are needed, replicated as necessary.
|
|
178 |
* <p>
|
|
179 |
* Note: This method is intended to be called by the
|
|
180 |
* <code>ImageProducer</code> of the <code>Image</code> whose pixels
|
|
181 |
* are being filtered. Developers using
|
|
182 |
* this class to filter pixels from an image should avoid calling
|
|
183 |
* this method directly since that operation could interfere
|
|
184 |
* with the filtering operation.
|
|
185 |
*/
|
|
186 |
public void setPixels(int x, int y, int w, int h,
|
|
187 |
ColorModel model, byte pixels[], int off,
|
|
188 |
int scansize) {
|
|
189 |
if (srcrows == null || srccols == null) {
|
|
190 |
calculateMaps();
|
|
191 |
}
|
|
192 |
int sx, sy;
|
|
193 |
int dx1 = (2 * x * destWidth + srcWidth - 1) / (2 * srcWidth);
|
|
194 |
int dy1 = (2 * y * destHeight + srcHeight - 1) / (2 * srcHeight);
|
|
195 |
byte outpix[];
|
|
196 |
if (outpixbuf != null && outpixbuf instanceof byte[]) {
|
|
197 |
outpix = (byte[]) outpixbuf;
|
|
198 |
} else {
|
|
199 |
outpix = new byte[destWidth];
|
|
200 |
outpixbuf = outpix;
|
|
201 |
}
|
|
202 |
for (int dy = dy1; (sy = srcrows[dy]) < y + h; dy++) {
|
|
203 |
int srcoff = off + scansize * (sy - y);
|
|
204 |
int dx;
|
|
205 |
for (dx = dx1; (sx = srccols[dx]) < x + w; dx++) {
|
|
206 |
outpix[dx] = pixels[srcoff + sx - x];
|
|
207 |
}
|
|
208 |
if (dx > dx1) {
|
|
209 |
consumer.setPixels(dx1, dy, dx - dx1, 1,
|
|
210 |
model, outpix, dx1, destWidth);
|
|
211 |
}
|
|
212 |
}
|
|
213 |
}
|
|
214 |
|
|
215 |
/**
|
|
216 |
* Choose which rows and columns of the delivered int pixels are
|
|
217 |
* needed for the destination scaled image and pass through just
|
|
218 |
* those rows and columns that are needed, replicated as necessary.
|
|
219 |
* <p>
|
|
220 |
* Note: This method is intended to be called by the
|
|
221 |
* <code>ImageProducer</code> of the <code>Image</code> whose pixels
|
|
222 |
* are being filtered. Developers using
|
|
223 |
* this class to filter pixels from an image should avoid calling
|
|
224 |
* this method directly since that operation could interfere
|
|
225 |
* with the filtering operation.
|
|
226 |
*/
|
|
227 |
public void setPixels(int x, int y, int w, int h,
|
|
228 |
ColorModel model, int pixels[], int off,
|
|
229 |
int scansize) {
|
|
230 |
if (srcrows == null || srccols == null) {
|
|
231 |
calculateMaps();
|
|
232 |
}
|
|
233 |
int sx, sy;
|
|
234 |
int dx1 = (2 * x * destWidth + srcWidth - 1) / (2 * srcWidth);
|
|
235 |
int dy1 = (2 * y * destHeight + srcHeight - 1) / (2 * srcHeight);
|
|
236 |
int outpix[];
|
|
237 |
if (outpixbuf != null && outpixbuf instanceof int[]) {
|
|
238 |
outpix = (int[]) outpixbuf;
|
|
239 |
} else {
|
|
240 |
outpix = new int[destWidth];
|
|
241 |
outpixbuf = outpix;
|
|
242 |
}
|
|
243 |
for (int dy = dy1; (sy = srcrows[dy]) < y + h; dy++) {
|
|
244 |
int srcoff = off + scansize * (sy - y);
|
|
245 |
int dx;
|
|
246 |
for (dx = dx1; (sx = srccols[dx]) < x + w; dx++) {
|
|
247 |
outpix[dx] = pixels[srcoff + sx - x];
|
|
248 |
}
|
|
249 |
if (dx > dx1) {
|
|
250 |
consumer.setPixels(dx1, dy, dx - dx1, 1,
|
|
251 |
model, outpix, dx1, destWidth);
|
|
252 |
}
|
|
253 |
}
|
|
254 |
}
|
|
255 |
}
|