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)
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., 51 Franklin Street, Fifth Floor, Boston, MA
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
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
;
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
;
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;
61 * Construct new RGBImageFilter.
63 public RGBImageFilter()
68 * Sets the ColorModel used to filter with. If the specified ColorModel is
69 * IndexColorModel and canFilterIndexColorModel is true, we subsitute the
70 * ColorModel for a filtered one here and in setPixels whenever the original
71 * one appears. Otherwise overrides the default ColorModel of ImageProducer
72 * and specifies the default RGBColorModel
74 * @param model the color model to be used most often by setPixels
78 public void setColorModel(ColorModel model
)
80 if ((model
instanceof IndexColorModel
) && canFilterIndexColorModel
)
82 ColorModel newCM
= filterIndexColorModel((IndexColorModel
) model
);
83 substituteColorModel(model
, newCM
);
84 consumer
.setColorModel(newmodel
);
88 consumer
.setColorModel(ColorModel
.getRGBdefault());
93 * Registers a new ColorModel to subsitute for the old ColorModel when
94 * setPixels encounters the a pixel with the old ColorModel. The pixel
95 * remains unchanged except for a new ColorModel.
97 * @param oldcm the old ColorModel
98 * @param newcm the new ColorModel
100 public void substituteColorModel(ColorModel oldcm
, ColorModel newcm
)
107 * Filters an IndexColorModel through the filterRGB function. Uses
108 * coordinates of -1 to indicate its filtering an index and not a pixel.
110 * @param icm an IndexColorModel to filter
112 public IndexColorModel
filterIndexColorModel(IndexColorModel icm
)
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
];
120 icm
.getAlphas( alphas
);
122 icm
.getGreens( greens
);
123 icm
.getBlues( blues
);
125 int transparent
= icm
.getTransparentPixel();
126 boolean needAlpha
= false;
127 for( int i
= 0; i
< len
; i
++ )
129 int rgb
= filterRGB(-1, -1, icm
.getRGB(i
));
130 alphas
[i
] = (byte) (rgb
>> 24);
131 if (alphas
[i
] != ((byte) 0xff) && i
!= transparent
)
133 reds
[i
] = (byte) (rgb
>> 16);
134 greens
[i
] = (byte) (rgb
>> 8);
135 blues
[i
] = (byte) (rgb
);
137 IndexColorModel newIcm
;
139 newIcm
= new IndexColorModel(icm
.getPixelSize(), len
, reds
, greens
,
142 newIcm
= new IndexColorModel(icm
.getPixelSize(), len
, reds
, greens
,
148 * This functions filters a set of RGB pixels through filterRGB.
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
160 public void filterRGBPixels(int x
, int y
, int w
, int h
, int[] pixels
,
161 int offset
, int scansize
)
164 for (int yp
= 0; yp
< h
; yp
++)
166 for (int xp
= 0; xp
< w
; xp
++)
168 pixels
[index
] = filterRGB(xp
+ x
, yp
+ y
, pixels
[index
]);
171 index
+= scansize
- w
;
173 consumer
.setPixels(x
, y
, w
, h
, ColorModel
.getRGBdefault(), pixels
, offset
,
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.
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>
190 * @param scansize the width to use in extracting pixels from the
191 * <code>pixels</code> array
193 public void setPixels(int x
, int y
, int w
, int h
, ColorModel model
,
194 byte[] pixels
, int offset
, int scansize
)
196 if (model
== origmodel
)
198 consumer
.setPixels(x
, y
, w
, h
, newmodel
, pixels
, offset
, scansize
);
202 int[] filtered
= new int[w
];
204 for (int yp
= 0; yp
< h
; yp
++)
206 for (int xp
= 0; xp
< w
; xp
++)
208 filtered
[xp
] = model
.getRGB((pixels
[index
] & 0xff));
211 index
+= scansize
- w
;
212 filterRGBPixels(x
, y
+ yp
, w
, 1, filtered
, 0, w
);
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).
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>
230 * @param scansize the width to use in extracting pixels from the
231 * <code>pixels</code> array
233 public void setPixels(int x
, int y
, int w
, int h
, ColorModel model
,
234 int[] pixels
, int offset
, int scansize
)
236 if (model
== origmodel
)
238 consumer
.setPixels(x
, y
, w
, h
, newmodel
, pixels
, offset
, scansize
);
242 int[] filtered
= new int[w
];
244 for (int yp
= 0; yp
< h
; yp
++)
246 for (int xp
= 0; xp
< w
; xp
++)
248 filtered
[xp
] = model
.getRGB((pixels
[index
]));
251 index
+= scansize
- w
;
252 filterRGBPixels(x
, y
+ yp
, w
, 1, filtered
, 0, w
);
258 * Filters a single pixel from the default ColorModel.
260 * @param x x-coordinate
261 * @param y y-coordinate
264 public abstract int filterRGB(int x
, int y
, int rgb
);