|
| 1 | +/* |
| 2 | + * Copyright (c) 1996, 2002, 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 | +/** |
| 29 | + * An ImageFilter class for scaling images using a simple area averaging |
| 30 | + * algorithm that produces smoother results than the nearest neighbor |
| 31 | + * algorithm. |
| 32 | + * <p>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 blended to produce pixels |
| 35 | + * for an image of the specified size. The blending process is analogous |
| 36 | + * to scaling up the source image to a multiple of the destination size |
| 37 | + * using pixel replication and then scaling it back down to the destination |
| 38 | + * size by simply averaging all the pixels in the supersized image that |
| 39 | + * fall within a given pixel of the destination image. If the data from |
| 40 | + * the source is not delivered in TopDownLeftRight order then the filter |
| 41 | + * will back off to a simple pixel replication behavior and utilize the |
| 42 | + * requestTopDownLeftRightResend() method to refilter the pixels in a |
| 43 | + * better way at the end. |
| 44 | + * <p>It is meant to be used in conjunction with a FilteredImageSource |
| 45 | + * object to produce scaled versions of existing images. Due to |
| 46 | + * implementation dependencies, there may be differences in pixel values |
| 47 | + * of an image filtered on different platforms. |
| 48 | + * |
| 49 | + * @see FilteredImageSource |
| 50 | + * @see ReplicateScaleFilter |
| 51 | + * @see ImageFilter |
| 52 | + * |
| 53 | + * @author Jim Graham |
| 54 | + */ |
| 55 | +public class AreaAveragingScaleFilter extends ReplicateScaleFilter { |
| 56 | + private static final ColorModel rgbmodel = ColorModel.getRGBdefault(); |
| 57 | + private static final int neededHints = (TOPDOWNLEFTRIGHT |
| 58 | + | COMPLETESCANLINES); |
| 59 | + |
| 60 | + private boolean passthrough; |
| 61 | + private float reds[], greens[], blues[], alphas[]; |
| 62 | + private int savedy; |
| 63 | + private int savedyrem; |
| 64 | + |
| 65 | + /** |
| 66 | + * Constructs an AreaAveragingScaleFilter that scales the pixels from |
| 67 | + * its source Image as specified by the width and height parameters. |
| 68 | + * @param width the target width to scale the image |
| 69 | + * @param height the target height to scale the image |
| 70 | + */ |
| 71 | + public AreaAveragingScaleFilter(int width, int height) { |
| 72 | + super(width, height); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Detect if the data is being delivered with the necessary hints |
| 77 | + * to allow the averaging algorithm to do its work. |
| 78 | + * <p> |
| 79 | + * Note: This method is intended to be called by the |
| 80 | + * <code>ImageProducer</code> of the <code>Image</code> whose |
| 81 | + * pixels are being filtered. Developers using |
| 82 | + * this class to filter pixels from an image should avoid calling |
| 83 | + * this method directly since that operation could interfere |
| 84 | + * with the filtering operation. |
| 85 | + * @see ImageConsumer#setHints |
| 86 | + */ |
| 87 | + @Override |
| 88 | + public void setHints(int hints) { |
| 89 | + passthrough = ((hints & neededHints) != neededHints); |
| 90 | + super.setHints(hints); |
| 91 | + } |
| 92 | + |
| 93 | + private void makeAccumBuffers() { |
| 94 | + reds = new float[destWidth]; |
| 95 | + greens = new float[destWidth]; |
| 96 | + blues = new float[destWidth]; |
| 97 | + alphas = new float[destWidth]; |
| 98 | + } |
| 99 | + |
| 100 | + private int[] calcRow() { |
| 101 | + float origmult = ((float) srcWidth) * srcHeight; |
| 102 | + if (outpixbuf == null || !(outpixbuf instanceof int[])) { |
| 103 | + outpixbuf = new int[destWidth]; |
| 104 | + } |
| 105 | + int[] outpix = (int[]) outpixbuf; |
| 106 | + for (int x = 0; x < destWidth; x++) { |
| 107 | + float mult = origmult; |
| 108 | + int a = Math.round(alphas[x] / mult); |
| 109 | + if (a <= 0) { |
| 110 | + a = 0; |
| 111 | + } else if (a >= 255) { |
| 112 | + a = 255; |
| 113 | + } else { |
| 114 | + // un-premultiply the components (by modifying mult here, we |
| 115 | + // are effectively doing the divide by mult and divide by |
| 116 | + // alpha in the same step) |
| 117 | + mult = alphas[x] / 255; |
| 118 | + } |
| 119 | + int r = Math.round(reds[x] / mult); |
| 120 | + int g = Math.round(greens[x] / mult); |
| 121 | + int b = Math.round(blues[x] / mult); |
| 122 | + if (r < 0) {r = 0;} else if (r > 255) {r = 255;} |
| 123 | + if (g < 0) {g = 0;} else if (g > 255) {g = 255;} |
| 124 | + if (b < 0) {b = 0;} else if (b > 255) {b = 255;} |
| 125 | + outpix[x] = (a << 24 | r << 16 | g << 8 | b); |
| 126 | + } |
| 127 | + return outpix; |
| 128 | + } |
| 129 | + |
| 130 | + private void accumPixels(int x, int y, int w, int h, |
| 131 | + ColorModel model, Object pixels, int off, |
| 132 | + int scansize) { |
| 133 | + if (reds == null) { |
| 134 | + makeAccumBuffers(); |
| 135 | + } |
| 136 | + int sy = y; |
| 137 | + int syrem = destHeight; |
| 138 | + int dy, dyrem; |
| 139 | + if (sy == 0) { |
| 140 | + dy = 0; |
| 141 | + dyrem = 0; |
| 142 | + } else { |
| 143 | + dy = savedy; |
| 144 | + dyrem = savedyrem; |
| 145 | + } |
| 146 | + while (sy < y + h) { |
| 147 | + int amty; |
| 148 | + if (dyrem == 0) { |
| 149 | + for (int i = 0; i < destWidth; i++) { |
| 150 | + alphas[i] = reds[i] = greens[i] = blues[i] = 0f; |
| 151 | + } |
| 152 | + dyrem = srcHeight; |
| 153 | + } |
| 154 | + if (syrem < dyrem) { |
| 155 | + amty = syrem; |
| 156 | + } else { |
| 157 | + amty = dyrem; |
| 158 | + } |
| 159 | + int sx = 0; |
| 160 | + int dx = 0; |
| 161 | + int sxrem = 0; |
| 162 | + int dxrem = srcWidth; |
| 163 | + float a = 0f, r = 0f, g = 0f, b = 0f; |
| 164 | + while (sx < w) { |
| 165 | + if (sxrem == 0) { |
| 166 | + sxrem = destWidth; |
| 167 | + int rgb; |
| 168 | + if (pixels instanceof byte[]) { |
| 169 | + rgb = ((byte[]) pixels)[off + sx] & 0xff; |
| 170 | + } else { |
| 171 | + rgb = ((int[]) pixels)[off + sx]; |
| 172 | + } |
| 173 | + // getRGB() always returns non-premultiplied components |
| 174 | + rgb = model.getRGB(rgb); |
| 175 | + a = rgb >>> 24; |
| 176 | + r = (rgb >> 16) & 0xff; |
| 177 | + g = (rgb >> 8) & 0xff; |
| 178 | + b = rgb & 0xff; |
| 179 | + // premultiply the components if necessary |
| 180 | + if (a != 255.0f) { |
| 181 | + float ascale = a / 255.0f; |
| 182 | + r *= ascale; |
| 183 | + g *= ascale; |
| 184 | + b *= ascale; |
| 185 | + } |
| 186 | + } |
| 187 | + int amtx; |
| 188 | + if (sxrem < dxrem) { |
| 189 | + amtx = sxrem; |
| 190 | + } else { |
| 191 | + amtx = dxrem; |
| 192 | + } |
| 193 | + float mult = ((float) amtx) * amty; |
| 194 | + alphas[dx] += mult * a; |
| 195 | + reds[dx] += mult * r; |
| 196 | + greens[dx] += mult * g; |
| 197 | + blues[dx] += mult * b; |
| 198 | + if ((sxrem -= amtx) == 0) { |
| 199 | + sx++; |
| 200 | + } |
| 201 | + if ((dxrem -= amtx) == 0) { |
| 202 | + dx++; |
| 203 | + dxrem = srcWidth; |
| 204 | + } |
| 205 | + } |
| 206 | + if ((dyrem -= amty) == 0) { |
| 207 | + int outpix[] = calcRow(); |
| 208 | + do { |
| 209 | + consumer.setPixels(0, dy, destWidth, 1, |
| 210 | + rgbmodel, outpix, 0, destWidth); |
| 211 | + dy++; |
| 212 | + } while ((syrem -= amty) >= amty && amty == srcHeight); |
| 213 | + } else { |
| 214 | + syrem -= amty; |
| 215 | + } |
| 216 | + if (syrem == 0) { |
| 217 | + syrem = destHeight; |
| 218 | + sy++; |
| 219 | + off += scansize; |
| 220 | + } |
| 221 | + } |
| 222 | + savedyrem = dyrem; |
| 223 | + savedy = dy; |
| 224 | + } |
| 225 | + |
| 226 | + /** |
| 227 | + * Combine the components for the delivered byte pixels into the |
| 228 | + * accumulation arrays and send on any averaged data for rows of |
| 229 | + * pixels that are complete. If the correct hints were not |
| 230 | + * specified in the setHints call then relay the work to our |
| 231 | + * superclass which is capable of scaling pixels regardless of |
| 232 | + * the delivery hints. |
| 233 | + * <p> |
| 234 | + * Note: This method is intended to be called by the |
| 235 | + * <code>ImageProducer</code> of the <code>Image</code> |
| 236 | + * whose pixels are being filtered. Developers using |
| 237 | + * this class to filter pixels from an image should avoid calling |
| 238 | + * this method directly since that operation could interfere |
| 239 | + * with the filtering operation. |
| 240 | + * @see ReplicateScaleFilter |
| 241 | + */ |
| 242 | + @Override |
| 243 | + public void setPixels(int x, int y, int w, int h, |
| 244 | + ColorModel model, byte pixels[], int off, |
| 245 | + int scansize) { |
| 246 | + if (passthrough) { |
| 247 | + super.setPixels(x, y, w, h, model, pixels, off, scansize); |
| 248 | + } else { |
| 249 | + accumPixels(x, y, w, h, model, pixels, off, scansize); |
| 250 | + } |
| 251 | + } |
| 252 | + |
| 253 | + /** |
| 254 | + * Combine the components for the delivered int pixels into the |
| 255 | + * accumulation arrays and send on any averaged data for rows of |
| 256 | + * pixels that are complete. If the correct hints were not |
| 257 | + * specified in the setHints call then relay the work to our |
| 258 | + * superclass which is capable of scaling pixels regardless of |
| 259 | + * the delivery hints. |
| 260 | + * <p> |
| 261 | + * Note: This method is intended to be called by the |
| 262 | + * <code>ImageProducer</code> of the <code>Image</code> |
| 263 | + * whose pixels are being filtered. Developers using |
| 264 | + * this class to filter pixels from an image should avoid calling |
| 265 | + * this method directly since that operation could interfere |
| 266 | + * with the filtering operation. |
| 267 | + * @see ReplicateScaleFilter |
| 268 | + */ |
| 269 | + @Override |
| 270 | + public void setPixels(int x, int y, int w, int h, |
| 271 | + ColorModel model, int pixels[], int off, |
| 272 | + int scansize) { |
| 273 | + if (passthrough) { |
| 274 | + super.setPixels(x, y, w, h, model, pixels, off, scansize); |
| 275 | + } else { |
| 276 | + accumPixels(x, y, w, h, model, pixels, off, scansize); |
| 277 | + } |
| 278 | + } |
| 279 | +} |
0 commit comments