1 /*
2 * Copyright (c) 1996, 2014, Oracle and/or its affiliates. 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. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
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} array containing information about a
75 * row of pixels.
76 */
77 protected int srcrows[];
78
79 /**
80 * An {@code int} array containing information about a
81 * column of pixels.
82 */
83 protected int srccols[];
84
85 /**
86 * A {@code byte} 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} equals
98 * zero or {@code height} 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+
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
181 * {@code ImageProducer} of the {@code Image} whose pixels
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,
188 ColorModel model, byte pixels[], int off,
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);
196 byte outpix[];
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
222 * {@code ImageProducer} of the {@code Image} whose pixels
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,
229 ColorModel model, int pixels[], int off,
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);
237 int outpix[];
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 }
|
1 /*
2 * Copyright (c) 1996, 2018, Oracle and/or its affiliates. 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. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
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} array containing information about a
75 * row of pixels.
76 */
77 protected int[] srcrows;
78
79 /**
80 * An {@code int} array containing information about a
81 * column of pixels.
82 */
83 protected int[] srccols;
84
85 /**
86 * A {@code byte} 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} equals
98 * zero or {@code height} 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+
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
181 * {@code ImageProducer} of the {@code Image} whose pixels
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,
188 ColorModel model, byte[] pixels, int off,
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);
196 byte[] outpix;
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
222 * {@code ImageProducer} of the {@code Image} whose pixels
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,
229 ColorModel model, int[] pixels, int off,
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);
237 int[] outpix;
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 }
|