author | tvaleev |
Thu, 04 Oct 2018 12:40:55 -0700 | |
changeset 52248 | 2e330da7cbf4 |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
52248
2e330da7cbf4
8211300: Convert C-style array declarations in JDK client code
tvaleev
parents:
47216
diff
changeset
|
2 |
* Copyright (c) 1996, 2018, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 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 |
/** |
|
35667 | 74 |
* An {@code int} array containing information about a |
2 | 75 |
* row of pixels. |
76 |
*/ |
|
52248
2e330da7cbf4
8211300: Convert C-style array declarations in JDK client code
tvaleev
parents:
47216
diff
changeset
|
77 |
protected int[] srcrows; |
2 | 78 |
|
79 |
/** |
|
35667 | 80 |
* An {@code int} array containing information about a |
2 | 81 |
* column of pixels. |
82 |
*/ |
|
52248
2e330da7cbf4
8211300: Convert C-style array declarations in JDK client code
tvaleev
parents:
47216
diff
changeset
|
83 |
protected int[] srccols; |
2 | 84 |
|
85 |
/** |
|
35667 | 86 |
* A {@code byte} array initialized with a size of |
2 | 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 |
|
35667 | 97 |
* @throws IllegalArgumentException if {@code width} equals |
98 |
* zero or {@code height} equals zero |
|
2 | 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. |
|
35667 | 113 |
* This method invokes {@code super.setProperties}, |
2 | 114 |
* which might result in additional properties being added. |
115 |
* <p> |
|
116 |
* Note: This method is intended to be called by the |
|
35667 | 117 |
* {@code ImageProducer} of the {@code Image} whose pixels |
2 | 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) { |
|
24549
147a5c8b7793
8039109: Fix unchecked and raw lint warnings in java.awt
darcy
parents:
5506
diff
changeset
|
124 |
@SuppressWarnings("unchecked") |
2 | 125 |
Hashtable<Object,Object> p = (Hashtable<Object,Object>)props.clone(); |
126 |
String key = "rescale"; |
|
127 |
String val = destWidth + "x" + destHeight; |
|
128 |
Object o = p.get(key); |
|
129 |
if (o != null && o instanceof String) { |
|
130 |
val = ((String) o) + ", " + val; |
|
131 |
} |
|
132 |
p.put(key, val); |
|
133 |
super.setProperties(p); |
|
134 |
} |
|
135 |
||
136 |
/** |
|
137 |
* Override the dimensions of the source image and pass the dimensions |
|
138 |
* of the new scaled size to the ImageConsumer. |
|
139 |
* <p> |
|
140 |
* Note: This method is intended to be called by the |
|
35667 | 141 |
* {@code ImageProducer} of the {@code Image} whose pixels |
2 | 142 |
* are being filtered. Developers using |
143 |
* this class to filter pixels from an image should avoid calling |
|
144 |
* this method directly since that operation could interfere |
|
145 |
* with the filtering operation. |
|
146 |
* @see ImageConsumer |
|
147 |
*/ |
|
148 |
public void setDimensions(int w, int h) { |
|
149 |
srcWidth = w; |
|
150 |
srcHeight = h; |
|
151 |
if (destWidth < 0) { |
|
152 |
if (destHeight < 0) { |
|
153 |
destWidth = srcWidth; |
|
154 |
destHeight = srcHeight; |
|
155 |
} else { |
|
156 |
destWidth = srcWidth * destHeight / srcHeight; |
|
157 |
} |
|
158 |
} else if (destHeight < 0) { |
|
159 |
destHeight = srcHeight * destWidth / srcWidth; |
|
160 |
} |
|
161 |
consumer.setDimensions(destWidth, destHeight); |
|
162 |
} |
|
163 |
||
164 |
private void calculateMaps() { |
|
165 |
srcrows = new int[destHeight + 1]; |
|
166 |
for (int y = 0; y <= destHeight; y++) { |
|
167 |
srcrows[y] = (2 * y * srcHeight + srcHeight) / (2 * destHeight); |
|
168 |
} |
|
169 |
srccols = new int[destWidth + 1]; |
|
170 |
for (int x = 0; x <= destWidth; x++) { |
|
171 |
srccols[x] = (2 * x * srcWidth + srcWidth) / (2 * destWidth); |
|
172 |
} |
|
173 |
} |
|
174 |
||
175 |
/** |
|
176 |
* Choose which rows and columns of the delivered byte pixels are |
|
177 |
* needed for the destination scaled image and pass through just |
|
178 |
* those rows and columns that are needed, replicated as necessary. |
|
179 |
* <p> |
|
180 |
* Note: This method is intended to be called by the |
|
35667 | 181 |
* {@code ImageProducer} of the {@code Image} whose pixels |
2 | 182 |
* are being filtered. Developers using |
183 |
* this class to filter pixels from an image should avoid calling |
|
184 |
* this method directly since that operation could interfere |
|
185 |
* with the filtering operation. |
|
186 |
*/ |
|
187 |
public void setPixels(int x, int y, int w, int h, |
|
52248
2e330da7cbf4
8211300: Convert C-style array declarations in JDK client code
tvaleev
parents:
47216
diff
changeset
|
188 |
ColorModel model, byte[] pixels, int off, |
2 | 189 |
int scansize) { |
190 |
if (srcrows == null || srccols == null) { |
|
191 |
calculateMaps(); |
|
192 |
} |
|
193 |
int sx, sy; |
|
194 |
int dx1 = (2 * x * destWidth + srcWidth - 1) / (2 * srcWidth); |
|
195 |
int dy1 = (2 * y * destHeight + srcHeight - 1) / (2 * srcHeight); |
|
52248
2e330da7cbf4
8211300: Convert C-style array declarations in JDK client code
tvaleev
parents:
47216
diff
changeset
|
196 |
byte[] outpix; |
2 | 197 |
if (outpixbuf != null && outpixbuf instanceof byte[]) { |
198 |
outpix = (byte[]) outpixbuf; |
|
199 |
} else { |
|
200 |
outpix = new byte[destWidth]; |
|
201 |
outpixbuf = outpix; |
|
202 |
} |
|
203 |
for (int dy = dy1; (sy = srcrows[dy]) < y + h; dy++) { |
|
204 |
int srcoff = off + scansize * (sy - y); |
|
205 |
int dx; |
|
206 |
for (dx = dx1; (sx = srccols[dx]) < x + w; dx++) { |
|
207 |
outpix[dx] = pixels[srcoff + sx - x]; |
|
208 |
} |
|
209 |
if (dx > dx1) { |
|
210 |
consumer.setPixels(dx1, dy, dx - dx1, 1, |
|
211 |
model, outpix, dx1, destWidth); |
|
212 |
} |
|
213 |
} |
|
214 |
} |
|
215 |
||
216 |
/** |
|
217 |
* Choose which rows and columns of the delivered int pixels are |
|
218 |
* needed for the destination scaled image and pass through just |
|
219 |
* those rows and columns that are needed, replicated as necessary. |
|
220 |
* <p> |
|
221 |
* Note: This method is intended to be called by the |
|
35667 | 222 |
* {@code ImageProducer} of the {@code Image} whose pixels |
2 | 223 |
* are being filtered. Developers using |
224 |
* this class to filter pixels from an image should avoid calling |
|
225 |
* this method directly since that operation could interfere |
|
226 |
* with the filtering operation. |
|
227 |
*/ |
|
228 |
public void setPixels(int x, int y, int w, int h, |
|
52248
2e330da7cbf4
8211300: Convert C-style array declarations in JDK client code
tvaleev
parents:
47216
diff
changeset
|
229 |
ColorModel model, int[] pixels, int off, |
2 | 230 |
int scansize) { |
231 |
if (srcrows == null || srccols == null) { |
|
232 |
calculateMaps(); |
|
233 |
} |
|
234 |
int sx, sy; |
|
235 |
int dx1 = (2 * x * destWidth + srcWidth - 1) / (2 * srcWidth); |
|
236 |
int dy1 = (2 * y * destHeight + srcHeight - 1) / (2 * srcHeight); |
|
52248
2e330da7cbf4
8211300: Convert C-style array declarations in JDK client code
tvaleev
parents:
47216
diff
changeset
|
237 |
int[] outpix; |
2 | 238 |
if (outpixbuf != null && outpixbuf instanceof int[]) { |
239 |
outpix = (int[]) outpixbuf; |
|
240 |
} else { |
|
241 |
outpix = new int[destWidth]; |
|
242 |
outpixbuf = outpix; |
|
243 |
} |
|
244 |
for (int dy = dy1; (sy = srcrows[dy]) < y + h; dy++) { |
|
245 |
int srcoff = off + scansize * (sy - y); |
|
246 |
int dx; |
|
247 |
for (dx = dx1; (sx = srccols[dx]) < x + w; dx++) { |
|
248 |
outpix[dx] = pixels[srcoff + sx - x]; |
|
249 |
} |
|
250 |
if (dx > dx1) { |
|
251 |
consumer.setPixels(dx1, dy, dx - dx1, 1, |
|
252 |
model, outpix, dx1, destWidth); |
|
253 |
} |
|
254 |
} |
|
255 |
} |
|
256 |
} |