1/*
2 *  Licensed to the Apache Software Foundation (ASF) under one or more
3 *  contributor license agreements.  See the NOTICE file distributed with
4 *  this work for additional information regarding copyright ownership.
5 *  The ASF licenses this file to You under the Apache License, Version 2.0
6 *  (the "License"); you may not use this file except in compliance with
7 *  the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  See the License for the specific language governing permissions and
15 *  limitations under the License.
16 */
17/**
18 * @author Igor V. Stolyarov
19 * @version $Revision$
20 */
21/*
22 * Created on 02.11.2004
23 *
24 */
25package org.apache.harmony.awt.gl.color;
26
27import java.awt.color.ColorSpace;
28
29public class LUTColorConverter {
30
31    private static byte from8lRGBtosRGB_LUT[];
32
33    private static byte from16lRGBtosRGB_LUT[];
34
35    private static byte fromsRGBto8lRGB_LUT[];
36
37    private static short fromsRGBto16lRGB_LUT[];
38
39    private static byte fromsRGBto8sRGB_LUTs[][];
40
41    public static ColorSpace LINEAR_RGB_CS;
42
43    public static ColorSpace LINEAR_GRAY_CS;
44
45    public static ColorSpace sRGB_CS;
46
47    public LUTColorConverter() {
48    }
49
50    /*
51     * This class prepared and returned lookup tables for conversion color
52     * values from Linear RGB Color Space to sRGB and vice versa.
53     * Conversion is producing according to sRGB Color Space definition.
54     * "A Standard Default Color Space for the Internet - sRGB",
55     *  Michael Stokes (Hewlett-Packard), Matthew Anderson (Microsoft),
56     * Srinivasan Chandrasekar (Microsoft), Ricardo Motta (Hewlett-Packard)
57     * Version 1.10, November 5, 1996
58     * This document is available: http://www.w3.org/Graphics/Color/sRGB
59     */
60    public static byte[] getFrom8lRGBtosRGB_LUT() {
61        if (from8lRGBtosRGB_LUT == null) {
62            from8lRGBtosRGB_LUT = new byte[256];
63            float v;
64            for (int i = 0; i < 256; i++) {
65                v = (float)i / 255;
66                v = (v <= 0.04045f) ? v / 12.92f :
67                    (float) Math.pow((v + 0.055) / 1.055, 2.4);
68                from8lRGBtosRGB_LUT[i] = (byte) Math.round(v * 255.0f);
69            }
70        }
71        return from8lRGBtosRGB_LUT;
72    }
73
74    public static byte[] getFrom16lRGBtosRGB_LUT() {
75        if (from16lRGBtosRGB_LUT == null) {
76            from16lRGBtosRGB_LUT = new byte[65536];
77            float v;
78            for (int i = 0; i < 65536; i++) {
79                v = (float) i / 65535;
80                v = (v <= 0.04045f) ? v / 12.92f :
81                    (float) Math.pow((v + 0.055) / 1.055, 2.4);
82                from16lRGBtosRGB_LUT[i] = (byte) Math.round(v * 255.0f);
83            }
84        }
85        return from16lRGBtosRGB_LUT;
86    }
87
88    public static byte[] getFromsRGBto8lRGB_LUT() {
89        if (fromsRGBto8lRGB_LUT == null) {
90            fromsRGBto8lRGB_LUT = new byte[256];
91            float v;
92            for (int i = 0; i < 256; i++) {
93                v = (float) i / 255;
94                v = (v <= 0.0031308f) ? v * 12.92f :
95                    ((float) Math.pow(v, 1.0 / 2.4)) * 1.055f - 0.055f;
96                fromsRGBto8lRGB_LUT[i] = (byte) Math.round(v * 255.0f);
97            }
98        }
99        return fromsRGBto8lRGB_LUT;
100    }
101
102    public static short[] getFromsRGBto16lRGB_LUT() {
103        if (fromsRGBto16lRGB_LUT == null) {
104            fromsRGBto16lRGB_LUT = new short[256];
105            float v;
106            for (int i = 0; i < 256; i++) {
107                v = (float) i / 255;
108                v = (v <= 0.0031308f) ? v * 12.92f :
109                    ((float) Math.pow(v, 1.0 / 2.4)) * 1.055f - 0.055f;
110                fromsRGBto16lRGB_LUT[i] = (short) Math.round(v * 65535.0f);
111            }
112        }
113        return fromsRGBto16lRGB_LUT;
114    }
115
116    public static byte[] getsRGBLUT(int bits) {
117        if (bits < 1) return null;
118        int idx = bits -1;
119        if(fromsRGBto8sRGB_LUTs == null) fromsRGBto8sRGB_LUTs = new byte[16][];
120
121        if(fromsRGBto8sRGB_LUTs[idx] == null){
122            fromsRGBto8sRGB_LUTs[idx] = createLUT(bits);
123        }
124        return fromsRGBto8sRGB_LUTs[idx];
125    }
126
127    private static byte[] createLUT(int bits) {
128        int lutSize = (1 << bits);
129        byte lut[] = new byte[lutSize];
130        for (int i = 0; i < lutSize; i++) {
131            lut[i] = (byte) (255.0f / (lutSize - 1) + 0.5f);
132        }
133        return lut;
134    }
135
136    public static boolean is_LINEAR_RGB_CS(ColorSpace cs) {
137        return (cs == LINEAR_RGB_CS);
138    }
139
140    public static boolean is_LINEAR_GRAY_CS(ColorSpace cs) {
141        return (cs == LINEAR_GRAY_CS);
142    }
143
144    public static boolean is_sRGB_CS(ColorSpace cs) {
145        return (cs == sRGB_CS);
146    }
147
148}