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 package org.apache.commons.imaging.formats.tiff.photometricinterpreters;
18
19 import java.io.IOException;
20
21 import org.apache.commons.imaging.ImagingException;
22 import org.apache.commons.imaging.common.ImageBuilder;
23
24 /**
25 * Photometric interpretation Logluv support. Logluv is an encoding for storing data inside TIFF images.
26 *
27 * @see <a href="https://en.wikipedia.org/wiki/Logluv_TIFF">Logluv TIFF</a>
28 */
29 public class PhotometricInterpreterLogLuv extends PhotometricInterpreter {
30
31 /**
32 * Rgb values (reg-green-blue, as R-G-B, as in the RGB color model).
33 */
34 static class RgbValues {
35 public int r;
36 public int g;
37 public int b;
38 }
39
40 /**
41 * Tristimulus color values (red-green-blue, as X-Y-Z, in the CIE XYZ color space).
42 */
43 static class TristimulusValues {
44 public float x;
45 public float y;
46 public float z;
47 }
48
49 public PhotometricInterpreterLogLuv(final int samplesPerPixel, final int[] bitsPerSample, final int predictor, final int width, final int height) {
50 super(samplesPerPixel, bitsPerSample, predictor, width, height);
51 }
52
53 /**
54 * Receives a triplet tristimulus values (CIE XYZ) and then does a CIELAB-CIEXYZ conversion (consult Wikipedia link for formula), where the CIELAB values
55 * are used to calculate the tristimulus values of the reference white point.
56 *
57 * @param tristimulusValues the XYZ tristimulus values
58 * @return RGB values
59 * @see <a href="https://en.wikipedia.org/wiki/CIELAB_color_space">CIELAB color space</a>
60 */
61 RgbValues getRgbValues(final TristimulusValues tristimulusValues) {
62 final float varX = tristimulusValues.x / 100f; // X = From 0 to ref_X
63 final float varY = tristimulusValues.y / 100f; // Y = From 0 to ref_Y
64 final float varZ = tristimulusValues.z / 100f; // Z = From 0 to ref_Y
65
66 float varR = varX * 3.2406f + varY * -1.5372f + varZ * -0.4986f;
67 float varG = varX * -0.9689f + varY * 1.8758f + varZ * 0.0415f;
68 float varB = varX * 0.0557f + varY * -0.2040f + varZ * 1.0570f;
69
70 if (varR > 0.0031308) {
71 varR = 1.055f * (float) Math.pow(varR, 1 / 2.4) - 0.055f;
72 } else {
73 varR = 12.92f * varR;
74 }
75 if (varG > 0.0031308) {
76 varG = 1.055f * (float) Math.pow(varG, 1 / 2.4) - 0.055f;
77 } else {
78 varG = 12.92f * varG;
79 }
80
81 if (varB > 0.0031308) {
82 varB = 1.055f * (float) Math.pow(varB, 1 / 2.4) - 0.055f;
83 } else {
84 varB = 12.92f * varB;
85 }
86
87 // var_R = ((var_R + 0.16561039f) / (3.0152583f + 0.16561039f));
88 // var_G = ((var_G + 0.06561642f) / (3.0239854f + 0.06561642f));
89 // var_B = ((var_B + 0.19393992f) / (3.1043448f + 0.19393992f));
90
91 final RgbValues values = new RgbValues();
92 values.r = (int) (varR * 255f);
93 values.g = (int) (varG * 255f);
94 values.b = (int) (varB * 255f);
95 return values;
96 }
97
98 /**
99 * Receives a triplet of CIELAB values, and calculates the tristimulus values. The reference white point used here is the equivalent to summer sun and sky.
100 *
101 * @param cieL lightness from black to white
102 * @param cieA lightness from green to red
103 * @param cieB lightness from blue to yellow
104 * @return tristimulus (X, Y, and Z) values
105 * @see <a href="https://en.wikipedia.org/wiki/CIELAB_color_space">CIELAB color space</a>
106 * @see <a href="https://en.wikipedia.org/wiki/White_point">White point</a>
107 */
108 TristimulusValues getTristimulusValues(final int cieL, final int cieA, final int cieB) {
109 float varY = (cieL * 100.0f / 255.0f + 16.0f) / 116.0f;
110 float varX = cieA / 500.0f + varY;
111 float varZ = varY - cieB / 200.0f;
112
113 final float varXCube = (float) Math.pow(varX, 3.0d);
114 final float varYCube = (float) Math.pow(varY, 3.0d);
115 final float varZCube = (float) Math.pow(varZ, 3.0d);
116
117 if (varYCube > 0.008856f) {
118 varY = varYCube;
119 } else {
120 varY = (varY - 16 / 116.0f) / 7.787f;
121 }
122
123 if (varXCube > 0.008856f) {
124 varX = varXCube;
125 } else {
126 varX = (varX - 16 / 116.0f) / 7.787f;
127 }
128
129 if (varZCube > 0.008856f) {
130 varZ = varZCube;
131 } else {
132 varZ = (varZ - 16 / 116.0f) / 7.787f;
133 }
134
135 // These reference values are the relative white points (XYZ) for commons scene types.
136 // The chosen values here reflect a scene with Summer Sun and Sky, temperature of 6504 K,
137 // X 95.047, Y 100.0, and Z 108.883.
138 // See Color Science by Wyszecki and Stiles for more
139 final float refX = 95.047f;
140 final float refY = 100.000f;
141 final float refZ = 108.883f;
142
143 final TristimulusValues values = new TristimulusValues();
144 values.x = refX * varX; // ref_X = 95.047 Observer= 2°, Illuminant= D65
145 values.y = refY * varY; // ref_Y = 100.000
146 values.z = refZ * varZ; // ref_Z = 108.883
147 return values;
148 }
149
150 @Override
151 public void interpretPixel(final ImageBuilder imageBuilder, final int[] samples, final int x, final int y) throws ImagingException, IOException {
152 if (samples == null || samples.length != 3) {
153 throw new ImagingException("Invalid length of bits per sample (expected 3).");
154 }
155
156 // CIE illuminants. An illuminant is a theorical source of visible light with a profile.
157 // CIE stands for Commission Internationale de l'Eclairage, or International
158 // Comission on Illumination.
159 final int cieL = samples[0];
160 final int cieA = (byte) samples[1];
161 final int cieB = (byte) samples[2];
162
163 final TristimulusValues tristimulusValues = getTristimulusValues(cieL, cieA, cieB);
164
165 // ref_X = 95.047 //Observer = 2°, Illuminant = D65
166 // ref_Y = 100.000
167 // ref_Z = 108.883
168
169 final RgbValues rgbValues = getRgbValues(tristimulusValues);
170
171 // float R = 1.910f * X - 0.532f * Y - 0.288f * Z;
172 // float G = -0.985f * X + 1.999f * Y - 0.028f * Z;
173 // float B = 0.058f * X - 0.118f * Y + 0.898f * Z;
174
175 final int red = Math.min(255, Math.max(0, rgbValues.r));
176 final int green = Math.min(255, Math.max(0, rgbValues.g));
177 final int blue = Math.min(255, Math.max(0, rgbValues.b));
178 final int alpha = 0xff;
179 final int rgb = alpha << 24 | red << 16 | green << 8 | blue << 0;
180 imageBuilder.setRgb(x, y, rgb);
181
182 }
183 }