1 7 8 package java.awt.image; 9 10 import java.awt.image.ImageConsumer ; 11 import java.awt.image.ColorModel ; 12 import java.util.Hashtable ; 13 import java.awt.Rectangle ; 14 15 43 public class AreaAveragingScaleFilter extends ReplicateScaleFilter { 44 private static final ColorModel rgbmodel = ColorModel.getRGBdefault(); 45 private static final int neededHints = (TOPDOWNLEFTRIGHT 46 | COMPLETESCANLINES); 47 48 private boolean passthrough; 49 private float reds[], greens[], blues[], alphas[]; 50 private int savedy; 51 private int savedyrem; 52 53 59 public AreaAveragingScaleFilter(int width, int height) { 60 super(width, height); 61 } 62 63 75 public void setHints(int hints) { 76 passthrough = ((hints & neededHints) != neededHints); 77 super.setHints(hints); 78 } 79 80 private void makeAccumBuffers() { 81 reds = new float[destWidth]; 82 greens = new float[destWidth]; 83 blues = new float[destWidth]; 84 alphas = new float[destWidth]; 85 } 86 87 private int[] calcRow() { 88 float origmult = ((float) srcWidth) * srcHeight; 89 if (outpixbuf == null || !(outpixbuf instanceof int[])) { 90 outpixbuf = new int[destWidth]; 91 } 92 int[] outpix = (int[]) outpixbuf; 93 for (int x = 0; x < destWidth; x++) { 94 float mult = origmult; 95 int a = Math.round(alphas[x] / mult); 96 if (a <= 0) { 97 a = 0; 98 } else if (a >= 255) { 99 a = 255; 100 } else { 101 mult = alphas[x] / 255; 105 } 106 int r = Math.round(reds[x] / mult); 107 int g = Math.round(greens[x] / mult); 108 int b = Math.round(blues[x] / mult); 109 if (r < 0) {r = 0;} else if (r > 255) {r = 255;} 110 if (g < 0) {g = 0;} else if (g > 255) {g = 255;} 111 if (b < 0) {b = 0;} else if (b > 255) {b = 255;} 112 outpix[x] = (a << 24 | r << 16 | g << 8 | b); 113 } 114 return outpix; 115 } 116 117 private void accumPixels(int x, int y, int w, int h, 118 ColorModel model, Object pixels, int off, 119 int scansize) { 120 if (reds == null) { 121 makeAccumBuffers(); 122 } 123 int sy = y; 124 int syrem = destHeight; 125 int dy, dyrem; 126 if (sy == 0) { 127 dy = 0; 128 dyrem = 0; 129 } else { 130 dy = savedy; 131 dyrem = savedyrem; 132 } 133 while (sy < y + h) { 134 int amty; 135 if (dyrem == 0) { 136 for (int i = 0; i < destWidth; i++) { 137 alphas[i] = reds[i] = greens[i] = blues[i] = 0f; 138 } 139 dyrem = srcHeight; 140 } 141 if (syrem < dyrem) { 142 amty = syrem; 143 } else { 144 amty = dyrem; 145 } 146 int sx = 0; 147 int dx = 0; 148 int sxrem = 0; 149 int dxrem = srcWidth; 150 float a = 0f, r = 0f, g = 0f, b = 0f; 151 while (sx < w) { 152 if (sxrem == 0) { 153 sxrem = destWidth; 154 int rgb; 155 if (pixels instanceof byte[]) { 156 rgb = ((byte[]) pixels)[off + sx] & 0xff; 157 } else { 158 rgb = ((int[]) pixels)[off + sx]; 159 } 160 rgb = model.getRGB(rgb); 162 a = rgb >>> 24; 163 r = (rgb >> 16) & 0xff; 164 g = (rgb >> 8) & 0xff; 165 b = rgb & 0xff; 166 if (a != 255.0f) { 168 float ascale = a / 255.0f; 169 r *= ascale; 170 g *= ascale; 171 b *= ascale; 172 } 173 } 174 int amtx; 175 if (sxrem < dxrem) { 176 amtx = sxrem; 177 } else { 178 amtx = dxrem; 179 } 180 float mult = ((float) amtx) * amty; 181 alphas[dx] += mult * a; 182 reds[dx] += mult * r; 183 greens[dx] += mult * g; 184 blues[dx] += mult * b; 185 if ((sxrem -= amtx) == 0) { 186 sx++; 187 } 188 if ((dxrem -= amtx) == 0) { 189 dx++; 190 dxrem = srcWidth; 191 } 192 } 193 if ((dyrem -= amty) == 0) { 194 int outpix[] = calcRow(); 195 do { 196 consumer.setPixels(0, dy, destWidth, 1, 197 rgbmodel, outpix, 0, destWidth); 198 dy++; 199 } while ((syrem -= amty) >= amty && amty == srcHeight); 200 } else { 201 syrem -= amty; 202 } 203 if (syrem == 0) { 204 syrem = destHeight; 205 sy++; 206 off += scansize; 207 } 208 } 209 savedyrem = dyrem; 210 savedy = dy; 211 } 212 213 229 public void setPixels(int x, int y, int w, int h, 230 ColorModel model, byte pixels[], int off, 231 int scansize) { 232 if (passthrough) { 233 super.setPixels(x, y, w, h, model, pixels, off, scansize); 234 } else { 235 accumPixels(x, y, w, h, model, pixels, off, scansize); 236 } 237 } 238 239 255 public void setPixels(int x, int y, int w, int h, 256 ColorModel model, int pixels[], int off, 257 int scansize) { 258 if (passthrough) { 259 super.setPixels(x, y, w, h, model, pixels, off, scansize); 260 } else { 261 accumPixels(x, y, w, h, model, pixels, off, scansize); 262 } 263 } 264 } 265 | Popular Tags |