|
| 1 | +/* |
| 2 | + * Copyright (c) 1996, 2004, 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 |
| 23 | + * questions. |
| 24 | + */ |
| 25 | + |
| 26 | +package java.awt.image; |
| 27 | + |
| 28 | +import java.util.Hashtable; |
| 29 | + |
| 30 | +/** |
| 31 | + * An ImageFilter class for scaling images using the simplest algorithm. |
| 32 | + * This class extends the basic ImageFilter Class to scale an existing |
| 33 | + * image and provide a source for a new image containing the resampled |
| 34 | + * image. The pixels in the source image are sampled to produce pixels |
| 35 | + * for an image of the specified size by replicating rows and columns of |
| 36 | + * pixels to scale up or omitting rows and columns of pixels to scale |
| 37 | + * down. |
| 38 | + * <p>It is meant to be used in conjunction with a FilteredImageSource |
| 39 | + * object to produce scaled versions of existing images. Due to |
| 40 | + * implementation dependencies, there may be differences in pixel values |
| 41 | + * of an image filtered on different platforms. |
| 42 | + * |
| 43 | + * @see FilteredImageSource |
| 44 | + * @see ImageFilter |
| 45 | + * |
| 46 | + * @author Jim Graham |
| 47 | + */ |
| 48 | +public class ReplicateScaleFilter extends ImageFilter { |
| 49 | + |
| 50 | + /** |
| 51 | + * The width of the source image. |
| 52 | + */ |
| 53 | + protected int srcWidth; |
| 54 | + |
| 55 | + /** |
| 56 | + * The height of the source image. |
| 57 | + */ |
| 58 | + protected int srcHeight; |
| 59 | + |
| 60 | + /** |
| 61 | + * The target width to scale the image. |
| 62 | + */ |
| 63 | + protected int destWidth; |
| 64 | + |
| 65 | + /** |
| 66 | + * The target height to scale the image. |
| 67 | + */ |
| 68 | + protected int destHeight; |
| 69 | + |
| 70 | + /** |
| 71 | + * An <code>int</code> array containing information about a |
| 72 | + * row of pixels. |
| 73 | + */ |
| 74 | + protected int srcrows[]; |
| 75 | + |
| 76 | + /** |
| 77 | + * An <code>int</code> array containing information about a |
| 78 | + * column of pixels. |
| 79 | + */ |
| 80 | + protected int srccols[]; |
| 81 | + |
| 82 | + /** |
| 83 | + * A <code>byte</code> array initialized with a size of |
| 84 | + * {@link #destWidth} and used to deliver a row of pixel |
| 85 | + * data to the {@link ImageConsumer}. |
| 86 | + */ |
| 87 | + protected Object outpixbuf; |
| 88 | + |
| 89 | + /** |
| 90 | + * Constructs a ReplicateScaleFilter that scales the pixels from |
| 91 | + * its source Image as specified by the width and height parameters. |
| 92 | + * @param width the target width to scale the image |
| 93 | + * @param height the target height to scale the image |
| 94 | + * @throws IllegalArgumentException if <code>width</code> equals |
| 95 | + * zero or <code>height</code> equals zero |
| 96 | + */ |
| 97 | + public ReplicateScaleFilter(int width, int height) { |
| 98 | + if (width == 0 || height == 0) { |
| 99 | + throw new IllegalArgumentException("Width ("+width+ |
| 100 | + ") and height ("+height+ |
| 101 | + ") must be non-zero"); |
| 102 | + } |
| 103 | + destWidth = width; |
| 104 | + destHeight = height; |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Passes along the properties from the source object after adding a |
| 109 | + * property indicating the scale applied. |
| 110 | + * This method invokes <code>super.setProperties</code>, |
| 111 | + * which might result in additional properties being added. |
| 112 | + * <p> |
| 113 | + * Note: This method is intended to be called by the |
| 114 | + * <code>ImageProducer</code> of the <code>Image</code> whose pixels |
| 115 | + * are being filtered. Developers using |
| 116 | + * this class to filter pixels from an image should avoid calling |
| 117 | + * this method directly since that operation could interfere |
| 118 | + * with the filtering operation. |
| 119 | + */ |
| 120 | + @Override |
| 121 | + public void setProperties(Hashtable<?,?> props) { |
| 122 | + Hashtable<Object,Object> p = (Hashtable<Object,Object>)props.clone(); |
| 123 | + String key = "rescale"; |
| 124 | + String val = destWidth + "x" + destHeight; |
| 125 | + Object o = p.get(key); |
| 126 | + if (o != null && o instanceof String) { |
| 127 | + val = ((String) o) + ", " + val; |
| 128 | + } |
| 129 | + p.put(key, val); |
| 130 | + super.setProperties(p); |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * Override the dimensions of the source image and pass the dimensions |
| 135 | + * of the new scaled size to the ImageConsumer. |
| 136 | + * <p> |
| 137 | + * Note: This method is intended to be called by the |
| 138 | + * <code>ImageProducer</code> of the <code>Image</code> whose pixels |
| 139 | + * are being filtered. Developers using |
| 140 | + * this class to filter pixels from an image should avoid calling |
| 141 | + * this method directly since that operation could interfere |
| 142 | + * with the filtering operation. |
| 143 | + * @see ImageConsumer |
| 144 | + */ |
| 145 | + @Override |
| 146 | + public void setDimensions(int w, int h) { |
| 147 | + srcWidth = w; |
| 148 | + srcHeight = h; |
| 149 | + if (destWidth < 0) { |
| 150 | + if (destHeight < 0) { |
| 151 | + destWidth = srcWidth; |
| 152 | + destHeight = srcHeight; |
| 153 | + } else { |
| 154 | + destWidth = srcWidth * destHeight / srcHeight; |
| 155 | + } |
| 156 | + } else if (destHeight < 0) { |
| 157 | + destHeight = srcHeight * destWidth / srcWidth; |
| 158 | + } |
| 159 | + consumer.setDimensions(destWidth, destHeight); |
| 160 | + } |
| 161 | + |
| 162 | + private void calculateMaps() { |
| 163 | + srcrows = new int[destHeight + 1]; |
| 164 | + for (int y = 0; y <= destHeight; y++) { |
| 165 | + srcrows[y] = (2 * y * srcHeight + srcHeight) / (2 * destHeight); |
| 166 | + } |
| 167 | + srccols = new int[destWidth + 1]; |
| 168 | + for (int x = 0; x <= destWidth; x++) { |
| 169 | + srccols[x] = (2 * x * srcWidth + srcWidth) / (2 * destWidth); |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + /** |
| 174 | + * Choose which rows and columns of the delivered byte pixels are |
| 175 | + * needed for the destination scaled image and pass through just |
| 176 | + * those rows and columns that are needed, replicated as necessary. |
| 177 | + * <p> |
| 178 | + * Note: This method is intended to be called by the |
| 179 | + * <code>ImageProducer</code> of the <code>Image</code> whose pixels |
| 180 | + * are being filtered. Developers using |
| 181 | + * this class to filter pixels from an image should avoid calling |
| 182 | + * this method directly since that operation could interfere |
| 183 | + * with the filtering operation. |
| 184 | + */ |
| 185 | + @Override |
| 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 | + @Override |
| 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 | +} |
0 commit comments