File: IndexColorModel.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 (253 lines) | stat: -rw-r--r-- 7,858 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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/* IndexColorModel.java -- Java class for interpreting Pixel objects
   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;

/**
 *
 * @author C. Brian Jones (cbj@gnu.org) 
 */
public class IndexColorModel extends ColorModel
{
    private int map_size;
    private boolean opaque;
    private int trans = -1;

    private int[] rgb;

    /**
     * Each array much contain <code>size</code> elements.  For each 
     * array, the i-th color is described by reds[i], greens[i], 
     * blues[i], alphas[i], unless alphas is not specified, then all the 
     * colors are opaque except for the transparent color. 
     *
     * @param bits the number of bits needed to represent <code>size</code> colors
     * @param size the number of colors in the color map
     * @param reds the red component of all colors
     * @param greens the green component of all colors
     * @param blues the blue component of all colors
     */
    public IndexColorModel(int bits, int size, byte[] reds, byte[] greens,
			   byte[] blues) {
	this(bits, size, reds, greens, blues, (byte[])null);
    }

    /**
     * Each array much contain <code>size</code> elements.  For each 
     * array, the i-th color is described by reds[i], greens[i], 
     * blues[i], alphas[i], unless alphas is not specified, then all the 
     * colors are opaque except for the transparent color. 
     *
     * @param bits the number of bits needed to represent <code>size</code> colors
     * @param size the number of colors in the color map
     * @param reds the red component of all colors
     * @param greens the green component of all colors
     * @param blues the blue component of all colors
     * @param trans the index of the transparent color
     */
    public IndexColorModel(int bits, int size, byte[] reds, byte[] greens,
			   byte[] blues, int trans) {
	this(bits, size, reds, greens, blues, (byte[])null);
	this.trans = trans;
    }

    /**
     * Each array much contain <code>size</code> elements.  For each 
     * array, the i-th color is described by reds[i], greens[i], 
     * blues[i], alphas[i], unless alphas is not specified, then all the 
     * colors are opaque except for the transparent color. 
     *
     * @param bits the number of bits needed to represent <code>size</code> colors
     * @param size the number of colors in the color map
     * @param reds the red component of all colors
     * @param greens the green component of all colors
     * @param blues the blue component of all colors
     * @param alphas the alpha component of all colors
     */
    public IndexColorModel(int bits, int size, byte[] reds, byte[] greens,
			   byte[] blues, byte[] alphas) {
	super(bits);
	map_size = size;
	opaque = (alphas == null);

	rgb = new int[size];
	if (alphas == null) {
	    for (int i = 0; i < size; i++) {
		rgb[i] = 0xff000000 | 
		         ((reds[i] & 0xff) << 16) |
		         ((greens[i] & 0xff) << 8) |
		          (blues[i] & 0xff);
	    }
	}
	else {
	    for (int i = 0; i < size; i++) {
		rgb[i] = ((alphas[i] & 0xff) << 24 | 
		         ((reds[i] & 0xff) << 16) |
		         ((greens[i] & 0xff) << 8) |
		          (blues[i] & 0xff));
	    }
	}
    }

    /**
     * Each array much contain <code>size</code> elements.  For each 
     * array, the i-th color is described by reds[i], greens[i], 
     * blues[i], alphas[i], unless alphas is not specified, then all the 
     * colors are opaque except for the transparent color. 
     *
     * @param bits the number of bits needed to represent <code>size</code> colors
     * @param size the number of colors in the color map
     * @param cmap packed color components
     * @param start the offset of the first color component in <code>cmap</code>
     * @param hasAlpha <code>cmap</code> has alpha values
     */
    public IndexColorModel(int bits, int size, byte[] cmap, int start, 
			   boolean hasAlpha) {
	this(bits, size, cmap, start, hasAlpha, -1);
    }

    /**
     * Each array much contain <code>size</code> elements.  For each 
     * array, the i-th color is described by reds[i], greens[i], 
     * blues[i], alphas[i], unless alphas is not specified, then all the 
     * colors are opaque except for the transparent color. 
     *
     * @param bits the number of bits needed to represent <code>size</code> colors
     * @param size the number of colors in the color map
     * @param cmap packed color components
     * @param start the offset of the first color component in <code>cmap</code>
     * @param hasAlpha <code>cmap</code> has alpha values
     * @param trans the index of the transparent color
     */
    public IndexColorModel(int bits, int size, byte[] cmap, int start, 
			   boolean hasAlpha, int trans) {
	super(bits);
	map_size = size;
        opaque = !hasAlpha;
	this.trans = trans;
    }

    public final int getMapSize() {
	return map_size;
    }

    /**
     * Get the index of the transparent color in this color model
     */
    public final int getTransparentPixel() {
	return trans;
    }

    /**
     * <br>
     */
    public final void getReds(byte[] r) {
	getComponents( r, 2 );
    }

    /**
     * <br>
     */
    public final void getGreens(byte[] g) {
	getComponents( g, 1 );
    }

    /**
     * <br>
     */
    public final void getBlues(byte[] b) {
	getComponents( b, 0 );
    }

    /**
     * <br>
     */
    public final void getAlphas(byte[] a) {
	getComponents( a, 3 );
    }

    private void getComponents( byte[] c, int ci )
    {
	int i, max = ( map_size < c.length ) ? map_size : c.length;
	for( i = 0; i < max; i++ )
	    c[i] = (byte)(( generateMask( ci )  & rgb[i]) >> ( ci * pixel_bits) );
    } 

    /**
     * Get the red component of the given pixel.
     * <br>
     */
    public final int getRed(int pixel) {
	if( pixel < map_size ) 
	    return  (int)(( generateMask( 2 )  & rgb[pixel]) >> (2 * pixel_bits ) );
	return 0;
    }

    /**
     * Get the green component of the given pixel.
     * <br>
     */
    public final int getGreen(int pixel) {
	if( pixel < map_size ) 
	    return (int)(( generateMask( 1 )  & rgb[pixel]) >> (1 * pixel_bits ) );
	return 0;
    }

    /**
     * Get the blue component of the given pixel.
     * <br>
     */
    public final int getBlue(int pixel) {
	if( pixel < map_size ) 
	    return  (int)( generateMask( 0 )  & rgb[pixel]);
	return 0;
    }

    /**
     * Get the alpha component of the given pixel.
     * <br>
     */
    public final int getAlpha(int pixel) {
	if( pixel < map_size ) 
	    return  (int)(( generateMask( 3 )  & rgb[pixel]) >> (3 * pixel_bits ) );
	return 0;
    }

    /**
     * Get the RGB color value of the given pixel using the default
     * RGB color model. 
     * <br>
     *
     * @param pixel a pixel value
     */
    public final int getRGB(int pixel) {
	if( pixel < map_size ) 
	    return rgb[pixel];
	return 0;
    }
    
    //pixel_bits is number of bits to be in generated mask
    private int generateMask( int offset )
    {
	return (  ( ( 2 << pixel_bits ) - 1 ) << ( pixel_bits * offset ) );
    }

}