001 /* RGBImageFilter.java -- Java class for filtering Pixels by RGB values
002 Copyright (C) 1999, 2005 Free Software Foundation, Inc.
003
004 This file is part of GNU Classpath.
005
006 GNU Classpath is free software; you can redistribute it and/or modify
007 it under the terms of the GNU General Public License as published by
008 the Free Software Foundation; either version 2, or (at your option)
009 any later version.
010
011 GNU Classpath is distributed in the hope that it will be useful, but
012 WITHOUT ANY WARRANTY; without even the implied warranty of
013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014 General Public License for more details.
015
016 You should have received a copy of the GNU General Public License
017 along with GNU Classpath; see the file COPYING. If not, write to the
018 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
019 02110-1301 USA.
020
021 Linking this library statically or dynamically with other modules is
022 making a combined work based on this library. Thus, the terms and
023 conditions of the GNU General Public License cover the whole
024 combination.
025
026 As a special exception, the copyright holders of this library give you
027 permission to link this library with independent modules to produce an
028 executable, regardless of the license terms of these independent
029 modules, and to copy and distribute the resulting executable under
030 terms of your choice, provided that you also meet, for each linked
031 independent module, the terms and conditions of the license of that
032 module. An independent module is a module which is not derived from
033 or based on this library. If you modify this library, you may extend
034 this exception to your version of the library, but you are not
035 obligated to do so. If you do not wish to do so, delete this
036 exception statement from your version. */
037
038
039 package java.awt.image;
040
041 /**
042 * A filter designed to filter images in the default RGBColorModel regardless of
043 * the ImageProducer's ColorModel.
044 *
045 * @author Mark Benvenuto (mcb54@columbia.edu)
046 */
047 public abstract class RGBImageFilter extends ImageFilter
048 {
049 protected ColorModel origmodel;
050
051 protected ColorModel newmodel;
052
053 /**
054 * Specifies whether to apply the filter to the index entries of the
055 * IndexColorModel. Subclasses should set this to true if the filter
056 * does not depend on the pixel's coordinate.
057 */
058 protected boolean canFilterIndexColorModel = false;
059
060 /**
061 * Construct new RGBImageFilter.
062 */
063 public RGBImageFilter()
064 {
065 }
066
067 /**
068 * Sets the ColorModel used to filter with. If the specified ColorModel is
069 * IndexColorModel and canFilterIndexColorModel is true, we subsitute the
070 * ColorModel for a filtered one here and in setPixels whenever the original
071 * one appears. Otherwise overrides the default ColorModel of ImageProducer
072 * and specifies the default RGBColorModel
073 *
074 * @param model the color model to be used most often by setPixels
075 *
076 * @see ColorModel
077 */
078 public void setColorModel(ColorModel model)
079 {
080 if ((model instanceof IndexColorModel) && canFilterIndexColorModel)
081 {
082 ColorModel newCM = filterIndexColorModel((IndexColorModel) model);
083 substituteColorModel(model, newCM);
084 consumer.setColorModel(newmodel);
085 }
086 else
087 {
088 consumer.setColorModel(ColorModel.getRGBdefault());
089 }
090 }
091
092 /**
093 * Registers a new ColorModel to subsitute for the old ColorModel when
094 * setPixels encounters the a pixel with the old ColorModel. The pixel
095 * remains unchanged except for a new ColorModel.
096 *
097 * @param oldcm the old ColorModel
098 * @param newcm the new ColorModel
099 */
100 public void substituteColorModel(ColorModel oldcm, ColorModel newcm)
101 {
102 origmodel = oldcm;
103 newmodel = newcm;
104 }
105
106 /**
107 * Filters an IndexColorModel through the filterRGB function. Uses
108 * coordinates of -1 to indicate its filtering an index and not a pixel.
109 *
110 * @param icm an IndexColorModel to filter
111 */
112 public IndexColorModel filterIndexColorModel(IndexColorModel icm)
113 {
114 int len = icm.getMapSize();
115 byte[] reds = new byte[len];
116 byte[] greens = new byte[len];
117 byte[] blues = new byte[len];
118 byte[] alphas = new byte[len];
119
120 icm.getAlphas( alphas );
121 icm.getReds( reds );
122 icm.getGreens( greens );
123 icm.getBlues( blues );
124
125 int transparent = icm.getTransparentPixel();
126 boolean needAlpha = false;
127 for( int i = 0; i < len; i++ )
128 {
129 int rgb = filterRGB(-1, -1, icm.getRGB(i));
130 alphas[i] = (byte) (rgb >> 24);
131 if (alphas[i] != ((byte) 0xff) && i != transparent)
132 needAlpha = true;
133 reds[i] = (byte) (rgb >> 16);
134 greens[i] = (byte) (rgb >> 8);
135 blues[i] = (byte) (rgb);
136 }
137 IndexColorModel newIcm;
138 if (needAlpha)
139 newIcm = new IndexColorModel(icm.getPixelSize(), len, reds, greens,
140 blues, alphas);
141 else
142 newIcm = new IndexColorModel(icm.getPixelSize(), len, reds, greens,
143 blues, transparent);
144 return newIcm;
145 }
146
147 /**
148 * This functions filters a set of RGB pixels through filterRGB.
149 *
150 * @param x the x coordinate of the rectangle
151 * @param y the y coordinate of the rectangle
152 * @param w the width of the rectangle
153 * @param h the height of the rectangle
154 * @param pixels the array of pixel values
155 * @param offset the index of the first pixels in the
156 * <code>pixels</code> array
157 * @param scansize the width to use in extracting pixels from the
158 * <code>pixels</code> array
159 */
160 public void filterRGBPixels(int x, int y, int w, int h, int[] pixels,
161 int offset, int scansize)
162 {
163 int index = offset;
164 for (int yp = 0; yp < h; yp++)
165 {
166 for (int xp = 0; xp < w; xp++)
167 {
168 pixels[index] = filterRGB(xp + x, yp + y, pixels[index]);
169 index++;
170 }
171 index += scansize - w;
172 }
173 consumer.setPixels(x, y, w, h, ColorModel.getRGBdefault(), pixels, offset,
174 scansize);
175 }
176
177 /**
178 * If the ColorModel is the same ColorModel which as already converted
179 * then it converts it the converted ColorModel. Otherwise it passes the
180 * array of pixels through filterRGBpixels.
181 *
182 * @param x the x coordinate of the rectangle
183 * @param y the y coordinate of the rectangle
184 * @param w the width of the rectangle
185 * @param h the height of the rectangle
186 * @param model the <code>ColorModel</code> used to translate the pixels
187 * @param pixels the array of pixel values
188 * @param offset the index of the first pixels in the <code>pixels</code>
189 * array
190 * @param scansize the width to use in extracting pixels from the
191 * <code>pixels</code> array
192 */
193 public void setPixels(int x, int y, int w, int h, ColorModel model,
194 byte[] pixels, int offset, int scansize)
195 {
196 if (model == origmodel)
197 {
198 consumer.setPixels(x, y, w, h, newmodel, pixels, offset, scansize);
199 }
200 else
201 {
202 int[] filtered = new int[w];
203 int index = offset;
204 for (int yp = 0; yp < h; yp++)
205 {
206 for (int xp = 0; xp < w; xp++)
207 {
208 filtered[xp] = model.getRGB((pixels[index] & 0xff));
209 index++;
210 }
211 index += scansize - w;
212 filterRGBPixels(x, y + yp, w, 1, filtered, 0, w);
213 }
214 }
215 }
216
217 /**
218 * This function delivers a rectangle of pixels where any
219 * pixel(m,n) is stored in the array as an <code>int</code> at
220 * index (n * scansize + m + offset).
221 *
222 * @param x the x coordinate of the rectangle
223 * @param y the y coordinate of the rectangle
224 * @param w the width of the rectangle
225 * @param h the height of the rectangle
226 * @param model the <code>ColorModel</code> used to translate the pixels
227 * @param pixels the array of pixel values
228 * @param offset the index of the first pixels in the <code>pixels</code>
229 * array
230 * @param scansize the width to use in extracting pixels from the
231 * <code>pixels</code> array
232 */
233 public void setPixels(int x, int y, int w, int h, ColorModel model,
234 int[] pixels, int offset, int scansize)
235 {
236 if (model == origmodel)
237 {
238 consumer.setPixels(x, y, w, h, newmodel, pixels, offset, scansize);
239 }
240 else
241 {
242 int[] filtered = new int[w];
243 int index = offset;
244 for (int yp = 0; yp < h; yp++)
245 {
246 for (int xp = 0; xp < w; xp++)
247 {
248 filtered[xp] = model.getRGB((pixels[index]));
249 index++;
250 }
251 index += scansize - w;
252 filterRGBPixels(x, y + yp, w, 1, filtered, 0, w);
253 }
254 }
255 }
256
257 /**
258 * Filters a single pixel from the default ColorModel.
259 *
260 * @param x x-coordinate
261 * @param y y-coordinate
262 * @param rgb color
263 */
264 public abstract int filterRGB(int x, int y, int rgb);
265 }