001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Igor V. Stolyarov
019: * @version $Revision$
020: */package org.apache.harmony.awt.gl.color;
021:
022: import java.awt.color.ColorSpace;
023:
024: public class LUTColorConverter {
025:
026: private static byte from8lRGBtosRGB_LUT[];
027:
028: private static byte from16lRGBtosRGB_LUT[];
029:
030: private static byte fromsRGBto8lRGB_LUT[];
031:
032: private static short fromsRGBto16lRGB_LUT[];
033:
034: private static byte fromsRGBto8sRGB_LUTs[][];
035:
036: public static ColorSpace LINEAR_RGB_CS;
037:
038: public static ColorSpace LINEAR_GRAY_CS;
039:
040: public static ColorSpace sRGB_CS;
041:
042: public LUTColorConverter() {
043: }
044:
045: /*
046: * This class prepared and returned lookup tables for conversion color
047: * values from Linear RGB Color Space to sRGB and vice versa.
048: * Conversion is producing according to sRGB Color Space definition.
049: * "A Standard Default Color Space for the Internet - sRGB",
050: * Michael Stokes (Hewlett-Packard), Matthew Anderson (Microsoft),
051: * Srinivasan Chandrasekar (Microsoft), Ricardo Motta (Hewlett-Packard)
052: * Version 1.10, November 5, 1996
053: * This document is available: http://www.w3.org/Graphics/Color/sRGB
054: */
055: public static byte[] getFrom8lRGBtosRGB_LUT() {
056: if (from8lRGBtosRGB_LUT == null) {
057: from8lRGBtosRGB_LUT = new byte[256];
058: float v;
059: for (int i = 0; i < 256; i++) {
060: v = (float) i / 255;
061: v = (v <= 0.04045f) ? v / 12.92f : (float) Math.pow(
062: (v + 0.055) / 1.055, 2.4);
063: from8lRGBtosRGB_LUT[i] = (byte) Math.round(v * 255.0f);
064: }
065: }
066: return from8lRGBtosRGB_LUT;
067: }
068:
069: public static byte[] getFrom16lRGBtosRGB_LUT() {
070: if (from16lRGBtosRGB_LUT == null) {
071: from16lRGBtosRGB_LUT = new byte[65536];
072: float v;
073: for (int i = 0; i < 65536; i++) {
074: v = (float) i / 65535;
075: v = (v <= 0.04045f) ? v / 12.92f : (float) Math.pow(
076: (v + 0.055) / 1.055, 2.4);
077: from16lRGBtosRGB_LUT[i] = (byte) Math.round(v * 255.0f);
078: }
079: }
080: return from16lRGBtosRGB_LUT;
081: }
082:
083: public static byte[] getFromsRGBto8lRGB_LUT() {
084: if (fromsRGBto8lRGB_LUT == null) {
085: fromsRGBto8lRGB_LUT = new byte[256];
086: float v;
087: for (int i = 0; i < 256; i++) {
088: v = (float) i / 255;
089: v = (v <= 0.0031308f) ? v * 12.92f : ((float) Math.pow(
090: v, 1.0 / 2.4)) * 1.055f - 0.055f;
091: fromsRGBto8lRGB_LUT[i] = (byte) Math.round(v * 255.0f);
092: }
093: }
094: return fromsRGBto8lRGB_LUT;
095: }
096:
097: public static short[] getFromsRGBto16lRGB_LUT() {
098: if (fromsRGBto16lRGB_LUT == null) {
099: fromsRGBto16lRGB_LUT = new short[256];
100: float v;
101: for (int i = 0; i < 256; i++) {
102: v = (float) i / 255;
103: v = (v <= 0.0031308f) ? v * 12.92f : ((float) Math.pow(
104: v, 1.0 / 2.4)) * 1.055f - 0.055f;
105: fromsRGBto16lRGB_LUT[i] = (short) Math
106: .round(v * 65535.0f);
107: }
108: }
109: return fromsRGBto16lRGB_LUT;
110: }
111:
112: public static byte[] getsRGBLUT(int bits) {
113: if (bits < 1)
114: return null;
115: int idx = bits - 1;
116: if (fromsRGBto8sRGB_LUTs == null)
117: fromsRGBto8sRGB_LUTs = new byte[16][];
118:
119: if (fromsRGBto8sRGB_LUTs[idx] == null) {
120: fromsRGBto8sRGB_LUTs[idx] = createLUT(bits);
121: }
122: return fromsRGBto8sRGB_LUTs[idx];
123: }
124:
125: private static byte[] createLUT(int bits) {
126: int lutSize = (1 << bits);
127: byte lut[] = new byte[lutSize];
128: for (int i = 0; i < lutSize; i++) {
129: lut[i] = (byte) (255.0f / (lutSize - 1) + 0.5f);
130: }
131: return lut;
132: }
133:
134: public static boolean is_LINEAR_RGB_CS(ColorSpace cs) {
135: return (cs == LINEAR_RGB_CS);
136: }
137:
138: public static boolean is_LINEAR_GRAY_CS(ColorSpace cs) {
139: return (cs == LINEAR_GRAY_CS);
140: }
141:
142: public static boolean is_sRGB_CS(ColorSpace cs) {
143: return (cs == sRGB_CS);
144: }
145:
146: }
|