File: RGBImageFilter.java

package info (click to toggle)
orp-classpath 1%3A0.02.1-3
  • links: PTS
  • area: contrib
  • in suites: woody
  • size: 15,212 kB
  • ctags: 16,077
  • sloc: java: 82,255; ansic: 12,779; sh: 6,321; makefile: 1,478; perl: 962; exp: 122; lisp: 115
file content (226 lines) | stat: -rw-r--r-- 7,911 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/* RGBImageFilter.java -- Java class for filtering Pixels by RGB values
   Copyright (C) 1999 Free Software Foundation, Inc.

This file is part of the non-peer AWT libraries of GNU Classpath.

This library is free software; you can redistribute it and/or modify
it under the terms of the GNU Library General Public License as published 
by the Free Software Foundation, either version 2 of the License, or
(at your option) any later verion.

This library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Library General Public License for more details.

You should have received a copy of the GNU Library General Public License
along with this library; if not, write to the Free Software Foundation
Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307 USA. */


package java.awt.image;

/**
 * A filter designed to filter images in the default RGBColorModel regardless of 
 * the ImageProducer's ColorModel.
 *
 * @author Mark Benvenuto <mcb54@columbia.edu>
 */
public abstract class RGBImageFilter extends ImageFilter
{
    protected ColorModel origmodel = ColorModel.getRGBdefault();

    protected ColorModel newmodel;
    
    /**
       Specifies whether to apply the filter to the index entries of the 
       IndexColorModel. Subclasses should set this to true if the filter 
       does not depend on the pixel's coordinate.
     */
    protected boolean canFilterIndexColorModel = false;

    /**
       Construct new RGBImageFilter.
     */
    public RGBImageFilter() 
    {
    }

    /**
     * Sets the ColorModel used to filter with. If the specified ColorModel is IndexColorModel 
     * and canFilterIndexColorModel is true, we subsitute the ColorModel for a filtered one
     * here and in setPixels whenever the original one appears. Otherwise overrides the default
     * ColorModel of ImageProducer and specifies the default RGBColorModel
     *
     * @param model the color model to be used most often by setPixels
     * @see ColorModel */
    public void setColorModel(ColorModel model) 
    {
	origmodel = model;
	newmodel = model;

	if( ( model instanceof IndexColorModel) && canFilterIndexColorModel  ) {
		newmodel = filterIndexColorModel( (IndexColorModel) model );
	    }
    }
    
    /**
       Registers a new ColorModel to subsitute for the old ColorModel when 
       setPixels encounters the a pixel with the old ColorModel. The pixel 
       remains unchanged except for a new ColorModel.
       
       @param oldcm the old ColorModel
       @param newcm the new ColorModel
     */
    public void substituteColorModel(ColorModel oldcm,
				     ColorModel newcm)
    {
	origmodel = oldcm;
	newmodel = newcm;
    }

    /**
       Filters an IndexColorModel through the filterRGB function. Uses
       coordinates of -1 to indicate its filtering an index and not a pixel.

       @param icm an IndexColorModel to filter
     */
    public IndexColorModel filterIndexColorModel(IndexColorModel icm) 
    {
	int len = icm.getMapSize(), rgb;
	byte reds[] = new byte[len], greens[] = new byte[len], blues[] = new byte[len], alphas[]  = new byte[len];
	
	icm.getAlphas( alphas );
	icm.getReds( reds );
	icm.getGreens( greens );
	icm.getBlues( blues );

	for( int i = 0; i < len; i++ )
	    {
		rgb = filterRGB( -1, -1, makeColor ( alphas[i], reds[i], greens[i], blues[i] ) );
		alphas[i] = (byte)(( 0xff000000 & rgb ) >> 24);
		reds[i] = (byte)(( 0xff0000 & rgb ) >> 16);
		greens[i] = (byte)(( 0xff00 & rgb ) >> 8);
		blues[i] = (byte)(0xff & rgb);
	    }
	return new IndexColorModel( icm.getPixelSize(), len, reds, greens, blues, alphas );
    }

    private int makeColor( byte a, byte r, byte g, byte b )
    {
	return ( 0xff000000 & (a << 24) | 0xff0000 & (r << 16) | 0xff00 & (b << 8) | 0xff & g ); 
    }

    /**
       This functions filters a set of RGB pixels through filterRGB.

       @param x the x coordinate of the rectangle
       @param y the y coordinate of the rectangle
       @param w the width of the rectangle
       @param h the height of the rectangle
       @param model the <code>ColorModel</code> used to translate the pixels
       @param pixels the array of pixel values
       @param offset the index of the first pixels in the <code>pixels</code> array
       @param scansize the width to use in extracting pixels from the <code>pixels</code> array
    */
    public void filterRGBPixels(int x,
				int y,
				int w,
				int h,
				int[] pixels,
				int off,
				int scansize)
    {
	int xp, yp, i;

	for( xp = x; xp < ( x + w); xp++ )
	    for( yp = y; yp < (y + h); yp++ )
		 pixels[ off + yp * scansize + xp ] = filterRGB( xp, yp, pixels[ off + yp * scansize + xp ] );
    }


    /**
     * If the ColorModel is the same ColorModel which as already converted 
     * then it converts it the converted ColorModel. Otherwise it passes the 
     * array of pixels through filterRGBpixels.
     *
     * @param x the x coordinate of the rectangle
     * @param y the y coordinate of the rectangle
     * @param w the width of the rectangle
     * @param h the height of the rectangle
     * @param model the <code>ColorModel</code> used to translate the pixels
     * @param pixels the array of pixel values
     * @param offset the index of the first pixels in the <code>pixels</code> array
     * @param scansize the width to use in extracting pixels from the <code>pixels</code> array
     */
    public void setPixels(int x, int y, int w, int h, 
	   ColorModel model, byte[] pixels, int offset, int scansize)
    {
	if( model == origmodel ) {
	    consumer.setPixels(x, y, w, h, newmodel, pixels, offset, scansize);
	} else {
	    //FIXME
	    //convert to proper CM
	    int pixelsi[] = new int[ pixels.length / 4 ];
	    filterRGBPixels( x, y, w, h, pixelsi, offset, scansize );
	}
    }

    /**
     * This function delivers a rectangle of pixels where any
     * pixel(m,n) is stored in the array as an <code>int</code> at
     * index (n * scansize + m + offset).  
     *
     * @param x the x coordinate of the rectangle
     * @param y the y coordinate of the rectangle
     * @param w the width of the rectangle
     * @param h the height of the rectangle
     * @param model the <code>ColorModel</code> used to translate the pixels
     * @param pixels the array of pixel values
     * @param offset the index of the first pixels in the <code>pixels</code> array
     * @param scansize the width to use in extracting pixels from the <code>pixels</code> array
     */
    public void setPixels(int x, int y, int w, int h, 
           ColorModel model, int[] pixels, int offset, int scansize)
    {
	if( model == origmodel ) {
	    consumer.setPixels(x, y, w, h, newmodel, pixels, offset, scansize);
	} else {
	    convertColorModelToDefault( x, y, w, h, model, pixels, offset, scansize );
	    filterRGBPixels( x, y, w, h, pixels, offset, scansize );
	}
    }

    private void convertColorModelToDefault( int x, int y, int w, int h, 
	    ColorModel model, int pixels[], int offset, int scansize)
	{
	int xp, yp, i;

	for( xp = x; xp < ( x + w); xp++ )
	    for( yp = y; yp < (y + h); yp++ )
		 pixels[ offset + yp * scansize + xp ] =  makeColorbyDefaultCM( pixels[ offset + yp * scansize + xp ] );
	    
	}
    private int makeColorbyDefaultCM( int rgb ) 
	{
	    return makeColor( origmodel.getRed( rgb ), origmodel.getGreen( rgb ), origmodel.getGreen( rgb ), origmodel.getBlue( rgb ) );
	}


    private int makeColor( int a, int r, int g, int b )
    {
	return (int)( 0xff000000 & (a << 24) | 0xff0000 & (r << 16) | 0xff00 & (b << 8) | 0xff & g ); 
    }


    /**
       Filters a single pixel from the default ColorModel.

       @param x x-coordinate
       @param y y-coordinate
       @param rgb color
     */
    public abstract int filterRGB(int x,
				  int y,
				  int rgb);
}