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 */
017package org.apache.commons.imaging.formats.tiff.photometricinterpreters;
018
019import java.io.IOException;
020
021import org.apache.commons.imaging.ImageReadException;
022import org.apache.commons.imaging.common.ImageBuilder;
023
024public class PhotometricInterpreterLogLuv extends PhotometricInterpreter {
025    // private final boolean yOnly;
026
027    public PhotometricInterpreterLogLuv(final int samplesPerPixel,
028            final int[] bitsPerSample, final int predictor, final int width, final int height) {
029        super(samplesPerPixel, bitsPerSample, predictor, width, height);
030
031        // this.yOnly = yonly;
032    }
033
034    private float cube(final float f) {
035        return f * f * f;
036    }
037
038    // private float function_f(float value, )
039
040    @Override
041    public void interpretPixel(final ImageBuilder imageBuilder, final int[] samples, final int x,
042            final int y) throws ImageReadException, IOException {
043        float X, Y, Z;
044
045        final int cieL = samples[0];
046        final int cieA = (byte) samples[1];
047        final int cieB = (byte) samples[2];
048
049        {
050
051            float var_Y = ((cieL * 100.0f / 255.0f) + 16.0f) / 116.0f;
052            float var_X = cieA / 500.0f + var_Y;
053            float var_Z = var_Y - cieB / 200.0f;
054
055            final float var_x_cube = cube(var_X);
056            final float var_y_cube = cube(var_Y);
057            final float var_z_cube = cube(var_Z);
058
059            if (var_y_cube > 0.008856f) {
060                var_Y = var_y_cube;
061            } else {
062                var_Y = (var_Y - 16 / 116.0f) / 7.787f;
063            }
064
065            if (var_x_cube > 0.008856f) {
066                var_X = var_x_cube;
067            } else {
068                var_X = (var_X - 16 / 116.0f) / 7.787f;
069            }
070
071            if (var_z_cube > 0.008856f) {
072                var_Z = var_z_cube;
073            } else {
074                var_Z = (var_Z - 16 / 116.0f) / 7.787f;
075            }
076
077            final float ref_X = 95.047f;
078            final float ref_Y = 100.000f;
079            final float ref_Z = 108.883f;
080
081            X = ref_X * var_X; // ref_X = 95.047 Observer= 2°, Illuminant= D65
082            Y = ref_Y * var_Y; // ref_Y = 100.000
083            Z = ref_Z * var_Z; // ref_Z = 108.883
084
085        }
086
087        // ref_X = 95.047 //Observer = 2°, Illuminant = D65
088        // ref_Y = 100.000
089        // ref_Z = 108.883
090
091        int R, G, B;
092        {
093            final float var_X = X / 100f; // X = From 0 to ref_X
094            final float var_Y = Y / 100f; // Y = From 0 to ref_Y
095            final float var_Z = Z / 100f; // Z = From 0 to ref_Y
096
097            float var_R = var_X * 3.2406f + var_Y * -1.5372f + var_Z * -0.4986f;
098            float var_G = var_X * -0.9689f + var_Y * 1.8758f + var_Z * 0.0415f;
099            float var_B = var_X * 0.0557f + var_Y * -0.2040f + var_Z * 1.0570f;
100
101            if (var_R > 0.0031308) {
102                var_R = 1.055f * (float) Math.pow(var_R, (1 / 2.4)) - 0.055f;
103            } else {
104                var_R = 12.92f * var_R;
105            }
106            if (var_G > 0.0031308) {
107                var_G = 1.055f * (float) Math.pow(var_G, (1 / 2.4)) - 0.055f;
108            } else {
109                var_G = 12.92f * var_G;
110            }
111
112            if (var_B > 0.0031308) {
113                var_B = 1.055f * (float) Math.pow(var_B, (1 / 2.4)) - 0.055f;
114            } else {
115                var_B = 12.92f * var_B;
116            }
117
118            // var_R = (((var_R-)))
119            // updateMaxMin(new float[]{
120            // var_R, var_G, var_B,
121            // }, maxVarRGB, minVarRGB);
122
123            // var_R = ((var_R + 0.16561039f) / (3.0152583f + 0.16561039f));
124            // var_G = ((var_G + 0.06561642f) / (3.0239854f + 0.06561642f));
125            // var_B = ((var_B + 0.19393992f) / (3.1043448f + 0.19393992f));
126
127            R = (int) (var_R * 255f);
128            G = (int) (var_G * 255f);
129            B = (int) (var_B * 255f);
130        }
131
132        // float R = 1.910f * X - 0.532f * Y - 0.288f * Z;
133        // float G = -0.985f * X + 1.999f * Y - 0.028f * Z;
134        // float B = 0.058f * X - 0.118f * Y + 0.898f * Z;
135
136        // updateMaxMin(new float[]{
137        // R, G, B,
138        // }, maxRGB, minRGB);
139
140        int red = R;
141        int green = G;
142        int blue = B;
143
144        red = Math.min(255, Math.max(0, red));
145        green = Math.min(255, Math.max(0, green));
146        blue = Math.min(255, Math.max(0, blue));
147        final int alpha = 0xff;
148        final int rgb = (alpha << 24) | (red << 16) | (green << 8) | (blue << 0);
149        imageBuilder.setRGB(x, y, rgb);
150
151    }
152}