1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.utils.ui.gfx;
14 //import org.eclipse.ui.IMemento;
18 * @author Toni Kalajainen
20 public final class PixelFormat {
22 public static PixelFormat RGB24 = new PixelFormat(0xff, 0xff00, 0xff0000, 0);
25 private final int redMask;
27 private final int greenMask;
29 private final int blueMask;
31 private final int alphaMask;
33 private final int redHighBit;
34 private final int greenHighBit;
35 private final int blueHighBit;
36 private final int alphaHighBit;
38 private final int redLowBit;
39 private final int greenLowBit;
40 private final int blueLowBit;
41 private final int alphaLowBit;
44 private final int bitDepth;
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;
52 this.redHighBit = findHighestBit(redMask);
53 this.greenHighBit = findHighestBit(greenMask);
54 this.blueHighBit = findHighestBit(blueMask);
55 this.alphaHighBit = findHighestBit(alphaMask);
57 this.redLowBit = findLowestBit(redMask);
58 this.greenLowBit = findLowestBit(greenMask);
59 this.blueLowBit = findLowestBit(blueMask);
60 this.alphaLowBit = findLowestBit(alphaMask);
62 this.bitDepth = findHighestBit(redMask|greenMask|blueMask|alphaMask)+1;
65 public boolean hasAlpha() {
66 return alphaMask != 0;
70 * Calculates mask's bit shift value
72 * @param mask color mask
74 static int calculateShiftFromMask(int mask) {
75 return 7-findHighestBit(mask);
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;
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;
94 public int getAlphaMask() {
98 public int getAlphaShift() {
99 return 7-alphaHighBit;
102 public int getAlphaDepth() {
103 return alphaHighBit-alphaLowBit+1;
106 public int getAlphaHighBit() {
110 public int getAlphaLowBit() {
115 public int getBlueMask() {
119 public int getBlueShift() {
120 return 7- blueHighBit;
123 public int getBlueDepth() {
124 return blueHighBit-blueLowBit+1;
127 public int getBlueHighBit() {
131 public int getBlueLowBit() {
136 public int getGreenMask() {
140 public int getGreenShift() {
141 return 7-greenHighBit;
144 public int getGreenDepth() {
145 return greenHighBit-greenLowBit+1;
148 public int getGreenHighBit() {
152 public int getGreenLowBit() {
156 public int getRedMask() {
160 public int getRedShift() {
164 public int getRedDepth() {
165 return redHighBit-redLowBit+1;
168 public int getRedHighBit() {
172 public int getRedLowBit() {
176 public int getBitDepth() {
180 public int getNumberOfColorComponents() {
182 if (redMask>0) result++;
183 if (greenMask>0) result++;
184 if (blueMask>0) result++;
185 if (alphaMask>0) result++;
191 * Return pixel size in bytes
192 * @return pixel size in bytes
194 public int getPixelSize() {
195 return (bitDepth+7)/8;
199 public int hashCode() {
200 return (redLowBit)|(redHighBit<<4)|(greenLowBit<<8)|(greenHighBit<<12)|(blueLowBit<<16)|(blueHighBit<<20)|(alphaLowBit<<24)|(alphaHighBit<<28);
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);
211 public String toString() {
212 return bitDepth+" bits";
215 // public void saveState(IMemento memento)
217 // memento.putInteger("redMask", redMask);
218 // memento.putInteger("greenMask", greenMask);
219 // memento.putInteger("blueMask", blueMask);
220 // memento.putInteger("alphaMask", alphaMask);
223 // public static PixelFormat restoreState(IMemento memento)
225 // return new PixelFormat(
226 // memento.getInteger("redMask"),
227 // memento.getInteger("greenMask"),
228 // memento.getInteger("blueMask"),
229 // memento.getInteger("alphaMask")