root/dwt/graphics/PaletteData.d

Revision 246:fd9c62a2998e, 6.2 kB (checked in by Frank Benoit <benoit@tionex.de>, 6 months ago)

Updater SWT 3.4M7 to 3.4

Line 
1 /*******************************************************************************
2  * Copyright (c) 2000, 2008 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  * Port to the D programming language:
11  *     Frank Benoit <benoit@tionex.de>
12  *******************************************************************************/
13 module dwt.graphics.PaletteData;
14
15
16 import dwt.DWT;
17 import dwt.graphics.RGB;
18
19 /**
20  * Instances of this class describe the color data used by an image.
21  * <p>
22  * Depending on the depth of the image, the PaletteData can take one
23  * of two forms, indicated by the isDirect field:
24  * </p>
25  * <dl>
26  * <dt>
27  * <em>isDirect is false</em>
28  * </dt>
29  * <dd>
30  * If isDirect is <code>false</code>, this palette is an indexed
31  * palette which maps pixel values to RGBs. The actual RGB values
32  * may be retrieved by using the getRGBs() method.
33  * </dd>
34  * <dt>
35  * <em>isDirect is true</em>
36  * </dt>
37  * <dd>
38  * If isDirect is <code>true</code>, this palette is a direct color
39  * palette. Instead of containing RGB values, it contains red,
40  * green and blue mask and shift information which indicates how
41  * the color components may be extracted from a given pixel.
42  * This means that the RGB value is actually encoded in the pixel value.
43  * <p>
44  * In this case, the shift data is the number of bits required to shift
45  * the RGB value to the left in order to align the high bit of the
46  * corresponding mask with the high bit of the first byte. This number
47  * may be negative, so care must be taken when shifting. For example,
48  * with a red mask of 0xFF0000, the red shift would be -16. With a red
49  * mask of 0x1F, the red shift would be 3.
50  * </p>
51  * </dd>
52  * </dl>
53  *
54  * @see Image
55  * @see RGB
56  * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
57  */
58
59 public final class PaletteData {
60
61     /**
62      * true if the receiver is a direct palette,
63      * and false otherwise
64      */
65     public bool isDirect;
66
67     /**
68      * the RGB values for an indexed palette, where the
69      * indices of the array correspond to pixel values
70      */
71     public RGB[] colors;
72
73     /**
74      * the red mask for a direct palette
75      */
76     public int redMask;
77
78     /**
79      * the green mask for a direct palette
80      */
81     public int greenMask;
82
83     /**
84      * the blue mask for a direct palette
85      */
86     public int blueMask;
87
88     /**
89      * the red shift for a direct palette
90      */
91     public int redShift;
92
93     /**
94      * the green shift for a direct palette
95      */
96     public int greenShift;
97
98     /**
99      * the blue shift for a direct palette
100      */
101     public int blueShift;
102
103 /**
104  * Constructs a new indexed palette given an array of RGB values.
105  *
106  * @param colors the array of <code>RGB</code>s for the palette
107  *
108  * @exception IllegalArgumentException <ul>
109  *    <li>ERROR_NULL_ARGUMENT - if the argument is null</li>
110  * </ul>
111  */
112 public this(RGB[] colors) {
113     if (colors is null) DWT.error(DWT.ERROR_NULL_ARGUMENT);
114     this.colors = colors;
115     this.isDirect = false;
116 }
117
118 /**
119  * Constructs a new direct palette given the red, green and blue masks.
120  *
121  * @param redMask the red mask
122  * @param greenMask the green mask
123  * @param blueMask the blue mask
124  */
125 public this(int redMask, int greenMask, int blueMask) {
126     this.redMask = redMask;
127     this.greenMask = greenMask;
128     this.blueMask = blueMask;
129     this.isDirect = true;
130     this.redShift = shiftForMask(redMask);
131     this.greenShift = shiftForMask(greenMask);
132     this.blueShift = shiftForMask(blueMask);
133 }
134
135 /**
136  * Returns the pixel value corresponding to the given <code>RGB</code>.
137  *
138  * @param rgb the RGB to get the pixel value for
139  * @return the pixel value for the given RGB
140  *
141  * @exception IllegalArgumentException <ul>
142  *    <li>ERROR_NULL_ARGUMENT - if the argument is null</li>
143  *    <li>ERROR_INVALID_ARGUMENT - if the RGB is not found in the palette</li>
144  * </ul>
145  */
146 public int getPixel(RGB rgb) {
147     if (rgb is null) DWT.error(DWT.ERROR_NULL_ARGUMENT);
148     if (isDirect) {
149         int pixel = 0;
150         pixel |= (redShift < 0 ? rgb.red << -redShift : rgb.red >>> redShift) & redMask;
151         pixel |= (greenShift < 0 ? rgb.green << -greenShift : rgb.green >>> greenShift) & greenMask;
152         pixel |= (blueShift < 0 ? rgb.blue << -blueShift : rgb.blue >>> blueShift) & blueMask;
153         return pixel;
154     } else {
155         for (int i = 0; i < colors.length; i++) {
156             if (colors[i].opEquals(rgb)) return i;
157         }
158         /* The RGB did not exist in the palette */
159         DWT.error(DWT.ERROR_INVALID_ARGUMENT);
160         return 0;
161     }
162 }
163
164 /**
165  * Returns an <code>RGB</code> corresponding to the given pixel value.
166  *
167  * @param pixel the pixel to get the RGB value for
168  * @return the RGB value for the given pixel
169  *
170  * @exception IllegalArgumentException <ul>
171  *    <li>ERROR_NULL_ARGUMENT - if the argument is null</li>
172  *    <li>ERROR_INVALID_ARGUMENT - if the pixel does not exist in the palette</li>
173  * </ul>
174  */
175 public RGB getRGB(int pixel) {
176     if (isDirect) {
177         int r = pixel & redMask;
178         r = (redShift < 0) ? r >>> -redShift : r << redShift;
179         int g = pixel & greenMask;
180         g = (greenShift < 0) ? g >>> -greenShift : g << greenShift;
181         int b = pixel & blueMask;
182         b = (blueShift < 0) ? b >>> -blueShift : b << blueShift;
183         return new RGB(r, g, b);
184     } else {
185         if (pixel < 0 || pixel >= colors.length) {
186             DWT.error(DWT.ERROR_INVALID_ARGUMENT);
187         }
188         return colors[pixel];
189     }
190 }
191
192 /**
193  * Returns all the RGB values in the receiver if it is an
194  * indexed palette, or null if it is a direct palette.
195  *
196  * @return the <code>RGB</code>s for the receiver or null
197  */
198 public RGB[] getRGBs() {
199     return colors;
200 }
201
202 /**
203  * Computes the shift value for a given mask.
204  *
205  * @param mask the mask to compute the shift for
206  * @return the shift amount
207  *
208  * @see PaletteData
209  */
210 int shiftForMask(int mask) {
211     for (int i = 31; i >= 0; i--) {
212         if (((mask >> i) & 0x1) !is 0) return 7 - i;
213     }
214     return 32;
215 }
216
217 }
Note: See TracBrowser for help on using the browser.