2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / libjava / java / awt / image / IndexColorModel.java
blob9ceb0bf0944e3422f455a577c25da665cae7adf6
1 /* IndexColorModel.java -- Java class for interpreting Pixel objects
2 Copyright (C) 1999 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 * @author C. Brian Jones (cbj@gnu.org)
44 public class IndexColorModel extends ColorModel
46 private int map_size;
47 private boolean opaque;
48 private int trans = -1;
49 private int[] rgb;
51 /**
52 * Each array much contain <code>size</code> elements. For each
53 * array, the i-th color is described by reds[i], greens[i],
54 * blues[i], alphas[i], unless alphas is not specified, then all the
55 * colors are opaque except for the transparent color.
57 * @param bits the number of bits needed to represent <code>size</code> colors
58 * @param size the number of colors in the color map
59 * @param reds the red component of all colors
60 * @param greens the green component of all colors
61 * @param blues the blue component of all colors
63 public IndexColorModel(int bits, int size, byte[] reds, byte[] greens,
64 byte[] blues)
66 this (bits, size, reds, greens, blues, (byte[]) null);
69 /**
70 * Each array much contain <code>size</code> elements. For each
71 * array, the i-th color is described by reds[i], greens[i],
72 * blues[i], alphas[i], unless alphas is not specified, then all the
73 * colors are opaque except for the transparent color.
75 * @param bits the number of bits needed to represent <code>size</code> colors
76 * @param size the number of colors in the color map
77 * @param reds the red component of all colors
78 * @param greens the green component of all colors
79 * @param blues the blue component of all colors
80 * @param trans the index of the transparent color
82 public IndexColorModel(int bits, int size, byte[] reds, byte[] greens,
83 byte[] blues, int trans)
85 this (bits, size, reds, greens, blues, (byte[]) null);
86 this.trans = trans;
89 /**
90 * Each array much contain <code>size</code> elements. For each
91 * array, the i-th color is described by reds[i], greens[i],
92 * blues[i], alphas[i], unless alphas is not specified, then all the
93 * colors are opaque except for the transparent color.
95 * @param bits the number of bits needed to represent <code>size</code> colors
96 * @param size the number of colors in the color map
97 * @param reds the red component of all colors
98 * @param greens the green component of all colors
99 * @param blues the blue component of all colors
100 * @param alphas the alpha component of all colors
102 public IndexColorModel(int bits, int size, byte[] reds, byte[] greens,
103 byte[] blues, byte[] alphas)
105 super (bits);
106 map_size = size;
107 opaque = (alphas == null);
109 rgb = new int[size];
110 if (alphas == null)
112 for (int i = 0; i < size; i++)
114 rgb[i] = (0xff000000
115 | ((reds[i] & 0xff) << 16)
116 | ((greens[i] & 0xff) << 8)
117 | (blues[i] & 0xff));
120 else
122 for (int i = 0; i < size; i++)
124 rgb[i] = ((alphas[i] & 0xff) << 24
125 | ((reds[i] & 0xff) << 16)
126 | ((greens[i] & 0xff) << 8)
127 | (blues[i] & 0xff));
133 * Each array much contain <code>size</code> elements. For each
134 * array, the i-th color is described by reds[i], greens[i],
135 * blues[i], alphas[i], unless alphas is not specified, then all the
136 * colors are opaque except for the transparent color.
138 * @param bits the number of bits needed to represent <code>size</code> colors
139 * @param size the number of colors in the color map
140 * @param cmap packed color components
141 * @param start the offset of the first color component in <code>cmap</code>
142 * @param hasAlpha <code>cmap</code> has alpha values
144 public IndexColorModel (int bits, int size, byte[] cmap, int start,
145 boolean hasAlpha)
147 this (bits, size, cmap, start, hasAlpha, -1);
151 * Each array much contain <code>size</code> elements. For each
152 * array, the i-th color is described by reds[i], greens[i],
153 * blues[i], alphas[i], unless alphas is not specified, then all the
154 * colors are opaque except for the transparent color.
156 * @param bits the number of bits needed to represent <code>size</code> colors
157 * @param size the number of colors in the color map
158 * @param cmap packed color components
159 * @param start the offset of the first color component in <code>cmap</code>
160 * @param hasAlpha <code>cmap</code> has alpha values
161 * @param trans the index of the transparent color
163 public IndexColorModel (int bits, int size, byte[] cmap, int start,
164 boolean hasAlpha, int trans)
166 super (bits);
167 map_size = size;
168 opaque = !hasAlpha;
169 this.trans = trans;
172 public final int getMapSize ()
174 return map_size;
178 * Get the index of the transparent color in this color model
180 public final int getTransparentPixel ()
182 return trans;
186 * <br>
188 public final void getReds (byte[] r)
190 getComponents (r, 2);
194 * <br>
196 public final void getGreens (byte[] g)
198 getComponents (g, 1);
202 * <br>
204 public final void getBlues (byte[] b)
206 getComponents (b, 0);
210 * <br>
212 public final void getAlphas (byte[] a)
214 getComponents (a, 3);
217 private void getComponents (byte[] c, int ci)
219 int i, max = (map_size < c.length) ? map_size : c.length;
220 for (i = 0; i < max; i++)
221 c[i] = (byte) ((generateMask (ci) & rgb[i]) >> (ci * pixel_bits));
225 * Get the red component of the given pixel.
227 public final int getRed (int pixel)
229 if (pixel < map_size)
230 return (int) ((generateMask (2) & rgb[pixel]) >> (2 * pixel_bits));
232 return 0;
236 * Get the green component of the given pixel.
238 public final int getGreen (int pixel)
240 if (pixel < map_size)
241 return (int) ((generateMask (1) & rgb[pixel]) >> (1 * pixel_bits));
243 return 0;
247 * Get the blue component of the given pixel.
249 public final int getBlue (int pixel)
251 if (pixel < map_size)
252 return (int) (generateMask (0) & rgb[pixel]);
254 return 0;
258 * Get the alpha component of the given pixel.
260 public final int getAlpha (int pixel)
262 if (pixel < map_size)
263 return (int) ((generateMask (3) & rgb[pixel]) >> (3 * pixel_bits));
265 return 0;
269 * Get the RGB color value of the given pixel using the default
270 * RGB color model.
272 * @param pixel a pixel value
274 public final int getRGB (int pixel)
276 if (pixel < map_size)
277 return rgb[pixel];
279 return 0;
282 //pixel_bits is number of bits to be in generated mask
283 private int generateMask (int offset)
285 return (((2 << pixel_bits ) - 1) << (pixel_bits * offset));