]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/gfx/PixelFormat.java
Implement ImageDescriptor.getImageData(int zoom)
[simantics/platform.git] / bundles / org.simantics.utils.ui / src / org / simantics / utils / ui / gfx / PixelFormat.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.utils.ui.gfx;
13
14 //import org.eclipse.ui.IMemento;
15
16 /**
17  * PixelFormat
18  * @author Toni Kalajainen
19  */
20 public final class PixelFormat {
21
22     public static PixelFormat RGB24 = new PixelFormat(0xff, 0xff00, 0xff0000, 0);
23     
24     /** red mask */
25     private final int redMask;
26     /** green mask */
27     private final int greenMask;
28     /** blue mask */
29     private final int blueMask;
30     /** alpha mask */
31     private final int alphaMask;
32     
33     private final int redHighBit;
34     private final int greenHighBit;
35     private final int blueHighBit;
36     private final int alphaHighBit;
37
38     private final int redLowBit;
39     private final int greenLowBit;
40     private final int blueLowBit;
41     private final int alphaLowBit;
42     
43     // Highest bit
44     private final int bitDepth;
45     
46     public PixelFormat(int redMask, int greenMask, int blueMask, int alphaMask) {
47         this.redMask = redMask;
48         this.greenMask = greenMask;
49         this.blueMask = blueMask;
50         this.alphaMask = alphaMask;
51
52         this.redHighBit = findHighestBit(redMask);
53         this.greenHighBit = findHighestBit(greenMask);
54         this.blueHighBit = findHighestBit(blueMask);
55         this.alphaHighBit = findHighestBit(alphaMask);
56
57         this.redLowBit = findLowestBit(redMask);
58         this.greenLowBit = findLowestBit(greenMask);
59         this.blueLowBit = findLowestBit(blueMask);
60         this.alphaLowBit = findLowestBit(alphaMask);
61         
62         this.bitDepth = findHighestBit(redMask|greenMask|blueMask|alphaMask)+1;        
63     }
64         
65     public boolean hasAlpha() {
66         return alphaMask != 0;
67     }
68     
69     /**
70      * Calculates mask's bit shift value
71      *
72      * @param mask color mask
73      */
74     static int calculateShiftFromMask(int mask) {
75         return 7-findHighestBit(mask);
76     }
77     
78     static int findHighestBit(int mask) {
79         // Scan bits starting from left. Find first bit
80         for (int i=31; i>=0; i--)
81             // Check if bit i is 1
82             if ((mask & (1<<i)) > 0) return i;
83         return 0;
84     }
85
86     static int findLowestBit(int mask) {
87         // Scan bits starting from left. Find first bit
88         for (int i=0; i<32; i++)
89             // Check if bit i is 1
90             if ((mask & (1<<i)) > 0) return i;
91         return 0;
92     }
93
94     public int getAlphaMask() {
95         return alphaMask;
96     }
97
98     public int getAlphaShift() {
99         return 7-alphaHighBit;
100     }
101
102     public int getAlphaDepth() {
103         return alphaHighBit-alphaLowBit+1;
104     }
105     
106     public int getAlphaHighBit() {
107         return alphaHighBit;
108     }
109     
110     public int getAlphaLowBit() {
111         return alphaLowBit;
112     }
113     
114     
115     public int getBlueMask() {
116         return blueMask;
117     }
118
119     public int getBlueShift() {
120         return 7- blueHighBit;
121     }
122
123     public int getBlueDepth() {
124         return blueHighBit-blueLowBit+1;
125     }
126     
127     public int getBlueHighBit() {
128         return blueHighBit;
129     }
130     
131     public int getBlueLowBit() {
132         return blueLowBit;
133     }
134
135     
136     public int getGreenMask() {
137         return greenMask;
138     }
139
140     public int getGreenShift() {
141         return 7-greenHighBit;
142     }
143
144     public int getGreenDepth() {
145         return greenHighBit-greenLowBit+1;
146     }
147     
148     public int getGreenHighBit() {
149         return greenHighBit;
150     }
151     
152     public int getGreenLowBit() {
153         return greenLowBit;
154     }
155
156     public int getRedMask() {
157         return redMask;
158     }
159     
160     public int getRedShift() {
161         return 7-redHighBit;
162     }
163
164     public int getRedDepth() {
165         return redHighBit-redLowBit+1;
166     }
167     
168     public int getRedHighBit() {
169         return redHighBit;
170     }
171     
172     public int getRedLowBit() {
173         return redLowBit;
174     }
175     
176     public int getBitDepth() {
177         return bitDepth; 
178     }
179     
180     public int getNumberOfColorComponents() {
181         int result = 0;
182         if (redMask>0) result++;
183         if (greenMask>0) result++;
184         if (blueMask>0) result++;
185         if (alphaMask>0) result++;
186         return result;
187     }
188     
189     
190     /**
191      * Return pixel size in bytes
192      * @return pixel size in bytes
193      */
194     public int getPixelSize() {
195         return (bitDepth+7)/8; 
196     }
197     
198     @Override
199     public int hashCode() {
200         return (redLowBit)|(redHighBit<<4)|(greenLowBit<<8)|(greenHighBit<<12)|(blueLowBit<<16)|(blueHighBit<<20)|(alphaLowBit<<24)|(alphaHighBit<<28); 
201     }
202     
203     @Override
204     public boolean equals(Object o2) {
205         if (!o2.getClass().equals(this.getClass())) return false;
206         PixelFormat pf2 = (PixelFormat) o2;
207         return (redMask == pf2.redMask) && (greenMask == pf2.greenMask) && (blueMask == pf2.blueMask) && (alphaMask == pf2.alphaMask);
208     }    
209     
210     @Override
211     public String toString() {
212         return bitDepth+" bits";
213     }
214     
215 //    public void saveState(IMemento memento)
216 //    {   
217 //        memento.putInteger("redMask", redMask);
218 //        memento.putInteger("greenMask", greenMask);
219 //        memento.putInteger("blueMask", blueMask);
220 //        memento.putInteger("alphaMask", alphaMask);
221 //    }
222 //    
223 //    public static PixelFormat restoreState(IMemento memento)
224 //    {
225 //        return new PixelFormat(
226 //                memento.getInteger("redMask"),
227 //                memento.getInteger("greenMask"),
228 //                memento.getInteger("blueMask"),
229 //                memento.getInteger("alphaMask")
230 //                );
231 //    }
232 }