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 34 public class ReplicateScaleFilter extends ImageFilter { 35 36 39 protected int srcWidth; 40 41 44 protected int srcHeight; 45 46 49 protected int destWidth; 50 51 54 protected int destHeight; 55 56 60 protected int srcrows[]; 61 62 66 protected int srccols[]; 67 68 73 protected Object outpixbuf; 74 75 83 public ReplicateScaleFilter(int width, int height) { 84 if (width == 0 || height == 0) { 85 throw new IllegalArgumentException ("Width ("+width+ 86 ") and height ("+height+ 87 ") must be non-zero"); 88 } 89 destWidth = width; 90 destHeight = height; 91 } 92 93 106 public void setProperties(Hashtable <?,?> props) { 107 Hashtable <Object ,Object > p = (Hashtable <Object ,Object >)props.clone(); 108 String key = "rescale"; 109 String val = destWidth + "x" + destHeight; 110 Object o = p.get(key); 111 if (o != null && o instanceof String ) { 112 val = ((String ) o) + ", " + val; 113 } 114 p.put(key, val); 115 super.setProperties(p); 116 } 117 118 130 public void setDimensions(int w, int h) { 131 srcWidth = w; 132 srcHeight = h; 133 if (destWidth < 0) { 134 if (destHeight < 0) { 135 destWidth = srcWidth; 136 destHeight = srcHeight; 137 } else { 138 destWidth = srcWidth * destHeight / srcHeight; 139 } 140 } else if (destHeight < 0) { 141 destHeight = srcHeight * destWidth / srcWidth; 142 } 143 consumer.setDimensions(destWidth, destHeight); 144 } 145 146 private void calculateMaps() { 147 srcrows = new int[destHeight + 1]; 148 for (int y = 0; y <= destHeight; y++) { 149 srcrows[y] = (2 * y * srcHeight + srcHeight) / (2 * destHeight); 150 } 151 srccols = new int[destWidth + 1]; 152 for (int x = 0; x <= destWidth; x++) { 153 srccols[x] = (2 * x * srcWidth + srcWidth) / (2 * destWidth); 154 } 155 } 156 157 169 public void setPixels(int x, int y, int w, int h, 170 ColorModel model, byte pixels[], int off, 171 int scansize) { 172 if (srcrows == null || srccols == null) { 173 calculateMaps(); 174 } 175 int sx, sy; 176 int dx1 = (2 * x * destWidth + srcWidth - 1) / (2 * srcWidth); 177 int dy1 = (2 * y * destHeight + srcHeight - 1) / (2 * srcHeight); 178 byte outpix[]; 179 if (outpixbuf != null && outpixbuf instanceof byte[]) { 180 outpix = (byte[]) outpixbuf; 181 } else { 182 outpix = new byte[destWidth]; 183 outpixbuf = outpix; 184 } 185 for (int dy = dy1; (sy = srcrows[dy]) < y + h; dy++) { 186 int srcoff = off + scansize * (sy - y); 187 int dx; 188 for (dx = dx1; (sx = srccols[dx]) < x + w; dx++) { 189 outpix[dx] = pixels[srcoff + sx - x]; 190 } 191 if (dx > dx1) { 192 consumer.setPixels(dx1, dy, dx - dx1, 1, 193 model, outpix, dx1, destWidth); 194 } 195 } 196 } 197 198 210 public void setPixels(int x, int y, int w, int h, 211 ColorModel model, int pixels[], int off, 212 int scansize) { 213 if (srcrows == null || srccols == null) { 214 calculateMaps(); 215 } 216 int sx, sy; 217 int dx1 = (2 * x * destWidth + srcWidth - 1) / (2 * srcWidth); 218 int dy1 = (2 * y * destHeight + srcHeight - 1) / (2 * srcHeight); 219 int outpix[]; 220 if (outpixbuf != null && outpixbuf instanceof int[]) { 221 outpix = (int[]) outpixbuf; 222 } else { 223 outpix = new int[destWidth]; 224 outpixbuf = outpix; 225 } 226 for (int dy = dy1; (sy = srcrows[dy]) < y + h; dy++) { 227 int srcoff = off + scansize * (sy - y); 228 int dx; 229 for (dx = dx1; (sx = srccols[dx]) < x + w; dx++) { 230 outpix[dx] = pixels[srcoff + sx - x]; 231 } 232 if (dx > dx1) { 233 consumer.setPixels(dx1, dy, dx - dx1, 1, 234 model, outpix, dx1, destWidth); 235 } 236 } 237 } 238 } 239 | Popular Tags |