KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > swt > graphics > PaletteData


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.swt.graphics;
12
13
14 import org.eclipse.swt.*;
15
16 /**
17  * Instances of this class describe the color data used by an image.
18  * <p>
19  * Depending on the depth of the image, the PaletteData can take one
20  * of two forms, indicated by the isDirect field:
21  * </p>
22  * <dl>
23  * <dt>
24  * <em>isDirect is false</em>
25  * </dt>
26  * <dd>
27  * If isDirect is <code>false</code>, this palette is an indexed
28  * palette which maps pixel values to RGBs. The actual RGB values
29  * may be retrieved by using the getRGBs() method.
30  * </dd>
31  * <dt>
32  * <em>isDirect is true</em>
33  * </dt>
34  * <dd>
35  * If isDirect is <code>true</code>, this palette is a direct color
36  * palette. Instead of containing RGB values, it contains red,
37  * green and blue mask and shift information which indicates how
38  * the color components may be extracted from a given pixel.
39  * This means that the RGB value is actually encoded in the pixel value.
40  * <p>
41  * In this case, the shift data is the number of bits required to shift
42  * the RGB value to the left in order to align the high bit of the
43  * corresponding mask with the high bit of the first byte. This number
44  * may be negative, so care must be taken when shifting. For example,
45  * with a red mask of 0xFF0000, the red shift would be -16. With a red
46  * mask of 0x1F, the red shift would be 3.
47  * </p>
48  * </dd>
49  * </dl>
50  *
51  * @see Image
52  * @see RGB
53  */

54  
55 public final class PaletteData {
56     
57     /**
58      * true if the receiver is a direct palette,
59      * and false otherwise
60      */

61     public boolean isDirect;
62     
63     /**
64      * the RGB values for an indexed palette, where the
65      * indices of the array correspond to pixel values
66      */

67     public RGB[] colors;
68     
69     /**
70      * the red mask for a direct palette
71      */

72     public int redMask;
73     
74     /**
75      * the green mask for a direct palette
76      */

77     public int greenMask;
78     
79     /**
80      * the blue mask for a direct palette
81      */

82     public int blueMask;
83     
84     /**
85      * the red shift for a direct palette
86      */

87     public int redShift;
88     
89     /**
90      * the green shift for a direct palette
91      */

92     public int greenShift;
93     
94     /**
95      * the blue shift for a direct palette
96      */

97     public int blueShift;
98
99 /**
100  * Constructs a new indexed palette given an array of RGB values.
101  *
102  * @param colors the array of <code>RGB</code>s for the palette
103  *
104  * @exception IllegalArgumentException <ul>
105  * <li>ERROR_NULL_ARGUMENT - if the argument is null</li>
106  * </ul>
107  */

108 public PaletteData(RGB[] colors) {
109     if (colors == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
110     this.colors = colors;
111     this.isDirect = false;
112 }
113
114 /**
115  * Constructs a new direct palette given the red, green and blue masks.
116  *
117  * @param redMask the red mask
118  * @param greenMask the green mask
119  * @param blueMask the blue mask
120  */

121 public PaletteData(int redMask, int greenMask, int blueMask) {
122     this.redMask = redMask;
123     this.greenMask = greenMask;
124     this.blueMask = blueMask;
125     this.isDirect = true;
126     this.redShift = shiftForMask(redMask);
127     this.greenShift = shiftForMask(greenMask);
128     this.blueShift = shiftForMask(blueMask);
129 }
130
131 /**
132  * Returns the pixel value corresponding to the given <code>RGB</code>.
133  *
134  * @param rgb the RGB to get the pixel value for
135  * @return the pixel value for the given RGB
136  *
137  * @exception IllegalArgumentException <ul>
138  * <li>ERROR_NULL_ARGUMENT - if the argument is null</li>
139  * <li>ERROR_INVALID_ARGUMENT - if the RGB is not found in the palette</li>
140  * </ul>
141  */

142 public int getPixel(RGB rgb) {
143     if (rgb == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
144     if (isDirect) {
145         int pixel = 0;
146         pixel |= (redShift < 0 ? rgb.red << -redShift : rgb.red >>> redShift) & redMask;
147         pixel |= (greenShift < 0 ? rgb.green << -greenShift : rgb.green >>> greenShift) & greenMask;
148         pixel |= (blueShift < 0 ? rgb.blue << -blueShift : rgb.blue >>> blueShift) & blueMask;
149         return pixel;
150     } else {
151         for (int i = 0; i < colors.length; i++) {
152             if (colors[i].equals(rgb)) return i;
153         }
154         /* The RGB did not exist in the palette */
155         SWT.error(SWT.ERROR_INVALID_ARGUMENT);
156         return 0;
157     }
158 }
159
160 /**
161  * Returns an <code>RGB</code> corresponding to the given pixel value.
162  *
163  * @param pixel the pixel to get the RGB value for
164  * @return the RGB value for the given pixel
165  *
166  * @exception IllegalArgumentException <ul>
167  * <li>ERROR_NULL_ARGUMENT - if the argument is null</li>
168  * <li>ERROR_INVALID_ARGUMENT - if the pixel does not exist in the palette</li>
169  * </ul>
170  */

171 public RGB getRGB(int pixel) {
172     if (isDirect) {
173         int r = pixel & redMask;
174         r = (redShift < 0) ? r >>> -redShift : r << redShift;
175         int g = pixel & greenMask;
176         g = (greenShift < 0) ? g >>> -greenShift : g << greenShift;
177         int b = pixel & blueMask;
178         b = (blueShift < 0) ? b >>> -blueShift : b << blueShift;
179         return new RGB(r, g, b);
180     } else {
181         if (pixel < 0 || pixel >= colors.length) {
182             SWT.error(SWT.ERROR_INVALID_ARGUMENT);
183         }
184         return colors[pixel];
185     }
186 }
187
188 /**
189  * Returns all the RGB values in the receiver if it is an
190  * indexed palette, or null if it is a direct palette.
191  *
192  * @return the <code>RGB</code>s for the receiver or null
193  */

194 public RGB[] getRGBs() {
195     return colors;
196 }
197
198 /**
199  * Computes the shift value for a given mask.
200  *
201  * @param mask the mask to compute the shift for
202  * @return the shift amount
203  *
204  * @see PaletteData
205  */

206 int shiftForMask(int mask) {
207     for (int i = 31; i >= 0; i--) {
208         if (((mask >> i) & 0x1) != 0) return 7 - i;
209     }
210     return 32;
211 }
212
213 }
214
Popular Tags