001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Igor V. Stolyarov
019: * @version $Revision$
020: */package java.awt.image;
021:
022: public abstract class RGBImageFilter extends ImageFilter {
023:
024: protected ColorModel origmodel;
025:
026: protected ColorModel newmodel;
027:
028: protected boolean canFilterIndexColorModel;
029:
030: public RGBImageFilter() {
031: }
032:
033: public IndexColorModel filterIndexColorModel(IndexColorModel icm) {
034: int transferType = icm.getTransferType();
035: int bits = icm.getPixelSize();
036: int mapSize = icm.getMapSize();
037: int colorMap[] = new int[mapSize];
038: int filteredColorMap[] = new int[mapSize];
039: icm.getRGBs(colorMap);
040: int trans = -1;
041: boolean hasAlpha = false;
042: for (int i = 0; i < mapSize; i++) {
043: filteredColorMap[i] = filterRGB(-1, -1, colorMap[i]);
044: int alpha = filteredColorMap[i] >>> 24;
045: if (alpha != 0xff) {
046: if (!hasAlpha) {
047: hasAlpha = true;
048: }
049: if (alpha == 0 && trans < 0) {
050: trans = i;
051: }
052: }
053: }
054:
055: return new IndexColorModel(bits, mapSize, filteredColorMap, 0,
056: hasAlpha, trans, transferType);
057: }
058:
059: public void substituteColorModel(ColorModel oldcm, ColorModel newcm) {
060: origmodel = oldcm;
061: newmodel = newcm;
062: }
063:
064: @Override
065: public void setColorModel(ColorModel model) {
066: if (model instanceof IndexColorModel
067: && canFilterIndexColorModel) {
068: IndexColorModel icm = (IndexColorModel) model;
069: ColorModel filteredModel = filterIndexColorModel(icm);
070: substituteColorModel(model, filteredModel);
071: consumer.setColorModel(filteredModel);
072: } else {
073: consumer.setColorModel(ColorModel.getRGBdefault());
074: }
075: }
076:
077: @Override
078: public void setPixels(int x, int y, int w, int h, ColorModel model,
079: int[] pixels, int off, int scansize) {
080:
081: if (model == null || model == origmodel) {
082: consumer.setPixels(x, y, w, h, newmodel, pixels, off,
083: scansize);
084: } else {
085: int rgbPixels[] = new int[w];
086: for (int sy = y, pixelsOff = off; sy < y + h; sy++, pixelsOff += scansize) {
087:
088: for (int sx = x, idx = 0; sx < x + w; sx++, idx++) {
089: rgbPixels[idx] = model.getRGB(pixels[pixelsOff
090: + idx]);
091: }
092: filterRGBPixels(x, sy, w, 1, rgbPixels, 0, w);
093: }
094: }
095: }
096:
097: @Override
098: public void setPixels(int x, int y, int w, int h, ColorModel model,
099: byte[] pixels, int off, int scansize) {
100:
101: if (model == null || model == origmodel) {
102: consumer.setPixels(x, y, w, h, newmodel, pixels, off,
103: scansize);
104: } else {
105: int rgbPixels[] = new int[w];
106: for (int sy = y, pixelsOff = off; sy < y + h; sy++, pixelsOff += scansize) {
107:
108: for (int sx = x, idx = 0; sx < x + w; sx++, idx++) {
109: rgbPixels[idx] = model.getRGB(pixels[pixelsOff
110: + idx] & 0xff);
111: }
112: filterRGBPixels(x, sy, w, 1, rgbPixels, 0, w);
113: }
114: }
115: }
116:
117: public void filterRGBPixels(int x, int y, int w, int h,
118: int[] pixels, int off, int scansize) {
119:
120: for (int sy = y, lineOff = off; sy < y + h; sy++, lineOff += scansize) {
121: for (int sx = x, idx = 0; sx < x + w; sx++, idx++) {
122: pixels[lineOff + idx] = filterRGB(sx, sy,
123: pixels[lineOff + idx]);
124: }
125: }
126: consumer.setPixels(x, y, w, h, ColorModel.getRGBdefault(),
127: pixels, off, scansize);
128: }
129:
130: public abstract int filterRGB(int x, int y, int rgb);
131:
132: }
|