1 7 8 package java.awt.image; 9 10 import java.awt.image.ImageConsumer ; 11 import java.awt.image.ColorModel ; 12 13 50 public abstract class RGBImageFilter extends ImageFilter { 51 52 57 protected ColorModel origmodel; 58 59 64 protected ColorModel newmodel; 65 66 77 protected boolean canFilterIndexColorModel; 78 79 97 public void setColorModel(ColorModel model) { 98 if (canFilterIndexColorModel && (model instanceof IndexColorModel )) { 99 ColorModel newcm = filterIndexColorModel((IndexColorModel )model); 100 substituteColorModel(model, newcm); 101 consumer.setColorModel(newcm); 102 } else { 103 consumer.setColorModel(ColorModel.getRGBdefault()); 104 } 105 } 106 107 115 public void substituteColorModel(ColorModel oldcm, ColorModel newcm) { 116 origmodel = oldcm; 117 newmodel = newcm; 118 } 119 120 130 public IndexColorModel filterIndexColorModel(IndexColorModel icm) { 131 int mapsize = icm.getMapSize(); 132 byte r[] = new byte[mapsize]; 133 byte g[] = new byte[mapsize]; 134 byte b[] = new byte[mapsize]; 135 byte a[] = new byte[mapsize]; 136 icm.getReds(r); 137 icm.getGreens(g); 138 icm.getBlues(b); 139 icm.getAlphas(a); 140 int trans = icm.getTransparentPixel(); 141 boolean needalpha = false; 142 for (int i = 0; i < mapsize; i++) { 143 int rgb = filterRGB(-1, -1, icm.getRGB(i)); 144 a[i] = (byte) (rgb >> 24); 145 if (a[i] != ((byte)0xff) && i != trans) { 146 needalpha = true; 147 } 148 r[i] = (byte) (rgb >> 16); 149 g[i] = (byte) (rgb >> 8); 150 b[i] = (byte) (rgb >> 0); 151 } 152 if (needalpha) { 153 return new IndexColorModel (icm.getPixelSize(), mapsize, 154 r, g, b, a); 155 } else { 156 return new IndexColorModel (icm.getPixelSize(), mapsize, 157 r, g, b, trans); 158 } 159 } 160 161 175 public void filterRGBPixels(int x, int y, int w, int h, 176 int pixels[], int off, int scansize) { 177 int index = off; 178 for (int cy = 0; cy < h; cy++) { 179 for (int cx = 0; cx < w; cx++) { 180 pixels[index] = filterRGB(x + cx, y + cy, pixels[index]); 181 index++; 182 } 183 index += scansize - w; 184 } 185 consumer.setPixels(x, y, w, h, ColorModel.getRGBdefault(), 186 pixels, off, scansize); 187 } 188 189 205 public void setPixels(int x, int y, int w, int h, 206 ColorModel model, byte pixels[], int off, 207 int scansize) { 208 if (model == origmodel) { 209 consumer.setPixels(x, y, w, h, newmodel, pixels, off, scansize); 210 } else { 211 int filteredpixels[] = new int[w]; 212 int index = off; 213 for (int cy = 0; cy < h; cy++) { 214 for (int cx = 0; cx < w; cx++) { 215 filteredpixels[cx] = model.getRGB((pixels[index] & 0xff)); 216 index++; 217 } 218 index += scansize - w; 219 filterRGBPixels(x, y + cy, w, 1, filteredpixels, 0, w); 220 } 221 } 222 } 223 224 242 public void setPixels(int x, int y, int w, int h, 243 ColorModel model, int pixels[], int off, 244 int scansize) { 245 if (model == origmodel) { 246 consumer.setPixels(x, y, w, h, newmodel, pixels, off, scansize); 247 } else { 248 int filteredpixels[] = new int[w]; 249 int index = off; 250 for (int cy = 0; cy < h; cy++) { 251 for (int cx = 0; cx < w; cx++) { 252 filteredpixels[cx] = model.getRGB(pixels[index]); 253 index++; 254 } 255 index += scansize - w; 256 filterRGBPixels(x, y + cy, w, 1, filteredpixels, 0, w); 257 } 258 } 259 } 260 261 271 public abstract int filterRGB(int x, int y, int rgb); 272 } 273 | Popular Tags |