Merge from mainline (gomp-merge-2005-02-26).
[official-gcc.git] / libjava / java / awt / image / RGBImageFilter.java
blobca88d70e36053ca6190dad65bf7f79a11aba0367
1 /* RGBImageFilter.java -- Java class for filtering Pixels by RGB values
2 Copyright (C) 1999, 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package java.awt.image;
41 /**
42 * A filter designed to filter images in the default RGBColorModel regardless of
43 * the ImageProducer's ColorModel.
45 * @author Mark Benvenuto (mcb54@columbia.edu)
47 public abstract class RGBImageFilter extends ImageFilter
49 protected ColorModel origmodel;
51 protected ColorModel newmodel;
53 /**
54 Specifies whether to apply the filter to the index entries of the
55 IndexColorModel. Subclasses should set this to true if the filter
56 does not depend on the pixel's coordinate.
58 protected boolean canFilterIndexColorModel = false;
60 /**
61 Construct new RGBImageFilter.
63 public RGBImageFilter()
67 /**
68 * Sets the ColorModel used to filter with. If the specified ColorModel is IndexColorModel
69 * and canFilterIndexColorModel is true, we subsitute the ColorModel for a filtered one
70 * here and in setPixels whenever the original one appears. Otherwise overrides the default
71 * ColorModel of ImageProducer and specifies the default RGBColorModel
73 * @param model the color model to be used most often by setPixels
74 * @see ColorModel */
75 public void setColorModel(ColorModel model)
77 origmodel = model;
78 newmodel = model;
80 if( ( model instanceof IndexColorModel) && canFilterIndexColorModel ) {
81 newmodel = filterIndexColorModel( (IndexColorModel) model );
82 consumer.setColorModel(newmodel);
84 else {
85 consumer.setColorModel(ColorModel.getRGBdefault());
89 /**
90 Registers a new ColorModel to subsitute for the old ColorModel when
91 setPixels encounters the a pixel with the old ColorModel. The pixel
92 remains unchanged except for a new ColorModel.
94 @param oldcm the old ColorModel
95 @param newcm the new ColorModel
97 public void substituteColorModel(ColorModel oldcm,
98 ColorModel newcm)
100 origmodel = oldcm;
101 newmodel = newcm;
105 Filters an IndexColorModel through the filterRGB function. Uses
106 coordinates of -1 to indicate its filtering an index and not a pixel.
108 @param icm an IndexColorModel to filter
110 public IndexColorModel filterIndexColorModel(IndexColorModel icm)
112 int len = icm.getMapSize(), rgb;
113 byte reds[] = new byte[len], greens[] = new byte[len], blues[] = new byte[len], alphas[] = new byte[len];
115 icm.getAlphas( alphas );
116 icm.getReds( reds );
117 icm.getGreens( greens );
118 icm.getBlues( blues );
120 for( int i = 0; i < len; i++ )
122 rgb = filterRGB( -1, -1, makeColor ( alphas[i], reds[i], greens[i], blues[i] ) );
123 alphas[i] = (byte)(( 0xff000000 & rgb ) >> 24);
124 reds[i] = (byte)(( 0xff0000 & rgb ) >> 16);
125 greens[i] = (byte)(( 0xff00 & rgb ) >> 8);
126 blues[i] = (byte)(0xff & rgb);
128 return new IndexColorModel( icm.getPixelSize(), len, reds, greens, blues, alphas );
131 private int makeColor( byte a, byte r, byte g, byte b )
133 return ( 0xff000000 & (a << 24) | 0xff0000 & (r << 16) | 0xff00 & (g << 8) | 0xff & b );
137 This functions filters a set of RGB pixels through filterRGB.
139 @param x the x coordinate of the rectangle
140 @param y the y coordinate of the rectangle
141 @param w the width of the rectangle
142 @param h the height of the rectangle
143 @param pixels the array of pixel values
144 @param offset the index of the first pixels in the <code>pixels</code> array
145 @param scansize the width to use in extracting pixels from the <code>pixels</code> array
147 public void filterRGBPixels(int x, int y, int w, int h, int[] pixels,
148 int offset, int scansize)
150 for (int xp = x; xp < (x + w); xp++)
151 for (int yp = y; yp < (y + h); yp++)
153 pixels[offset] = filterRGB(xp, yp, pixels[offset]);
154 offset++;
160 * If the ColorModel is the same ColorModel which as already converted
161 * then it converts it the converted ColorModel. Otherwise it passes the
162 * array of pixels through filterRGBpixels.
164 * @param x the x coordinate of the rectangle
165 * @param y the y coordinate of the rectangle
166 * @param w the width of the rectangle
167 * @param h the height of the rectangle
168 * @param model the <code>ColorModel</code> used to translate the pixels
169 * @param pixels the array of pixel values
170 * @param offset the index of the first pixels in the <code>pixels</code> array
171 * @param scansize the width to use in extracting pixels from the <code>pixels</code> array
173 public void setPixels(int x, int y, int w, int h,
174 ColorModel model, byte[] pixels,
175 int offset, int scansize)
177 if(model == origmodel && (model instanceof IndexColorModel) && canFilterIndexColorModel)
179 consumer.setPixels(x, y, w, h, newmodel, pixels, offset, scansize);
181 else
183 int intPixels[] =
184 convertColorModelToDefault( x, y, w, h, model, pixels, offset, scansize );
185 filterRGBPixels( x, y, w, h, intPixels, offset, scansize );
186 consumer.setPixels(x, y, w, h, ColorModel.getRGBdefault(), intPixels, offset, scansize);
191 * This function delivers a rectangle of pixels where any
192 * pixel(m,n) is stored in the array as an <code>int</code> at
193 * index (n * scansize + m + offset).
195 * @param x the x coordinate of the rectangle
196 * @param y the y coordinate of the rectangle
197 * @param w the width of the rectangle
198 * @param h the height of the rectangle
199 * @param model the <code>ColorModel</code> used to translate the pixels
200 * @param pixels the array of pixel values
201 * @param offset the index of the first pixels in the <code>pixels</code> array
202 * @param scansize the width to use in extracting pixels from the <code>pixels</code> array
204 public void setPixels(int x, int y, int w, int h,
205 ColorModel model, int[] pixels,
206 int offset, int scansize)
208 if(model == origmodel && (model instanceof IndexColorModel) && canFilterIndexColorModel)
210 consumer.setPixels(x, y, w, h, newmodel, pixels, offset, scansize);
212 else
214 //FIXME: Store the filtered pixels in a separate temporary buffer?
215 convertColorModelToDefault( x, y, w, h, model, pixels, offset, scansize );
216 filterRGBPixels( x, y, w, h, pixels, offset, scansize );
217 consumer.setPixels(x, y, w, h, ColorModel.getRGBdefault(), pixels, offset, scansize);
221 private int[] convertColorModelToDefault(int x, int y, int w, int h,
222 ColorModel model, byte pixels[],
223 int offset, int scansize)
225 int intPixels[] = new int[pixels.length];
226 for (int i = 0; i < pixels.length; i++)
227 intPixels[i] = makeColorbyDefaultCM(model, pixels[i]);
228 return intPixels;
231 private void convertColorModelToDefault(int x, int y, int w, int h,
232 ColorModel model, int pixels[],
233 int offset, int scansize)
235 for (int i = 0; i < pixels.length; i++)
236 pixels[i] = makeColorbyDefaultCM(model, pixels[i]);
239 private int makeColorbyDefaultCM(ColorModel model, byte rgb)
241 return makeColor( model.getAlpha( rgb ) * 4, model.getRed( rgb ) * 4, model.getGreen( rgb ) * 4, model.getBlue( rgb ) * 4 );
244 private int makeColorbyDefaultCM(ColorModel model, int rgb)
246 return makeColor( model.getAlpha( rgb ), model.getRed( rgb ), model.getGreen( rgb ), model.getBlue( rgb ) );
249 private int makeColor( int a, int r, int g, int b )
251 return (int)( 0xff000000 & (a << 24) | 0xff0000 & (r << 16) | 0xff00 & (g << 8) | 0xff & b );
256 Filters a single pixel from the default ColorModel.
258 @param x x-coordinate
259 @param y y-coordinate
260 @param rgb color
262 public abstract int filterRGB(int x,
263 int y,
264 int rgb);