    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.ImagingException;
    022import org.apache.commons.imaging.common.ImageBuilder;
    023
    024/**
    025 * Photometric interpretation Logluv support. Logluv is an encoding for storing data inside TIFF images.
    026 *
    027 * @see <a href="https://en.wikipedia.org/wiki/Logluv_TIFF">Logluv TIFF</a>
    028 */
    029public class PhotometricInterpreterLogLuv extends AbstractPhotometricInterpreter {
    030
    031    /**
    032     * Rgb values (reg-green-blue, as R-G-B, as in the RGB color model).
    033     */
    034    static class RgbValues {
    035        public int r;
    036        public int g;
    037        public int b;
    038    }
    039
    040    /**
    041     * Tristimulus color values (red-green-blue, as X-Y-Z, in the CIE XYZ color space).
    042     */
    043    static class TristimulusValues {
    044        public float x;
    045        public float y;
    046        public float z;
    047    }
    048
    049    public PhotometricInterpreterLogLuv(final int samplesPerPixel, final int[] bitsPerSample, final int predictor, final int width, final int height) {
    050        super(samplesPerPixel, bitsPerSample, predictor, width, height);
    051    }
    052
    053    /**
    054     * Receives a triplet tristimulus values (CIE XYZ) and then does a CIELAB-CIEXYZ conversion (consult Wikipedia link for formula), where the CIELAB values
    055     * are used to calculate the tristimulus values of the reference white point.
    056     *
    057     * @param tristimulusValues the XYZ tristimulus values
    058     * @return RGB values
    059     * @see <a href="https://en.wikipedia.org/wiki/CIELAB_color_space">CIELAB color space</a>
    060     */
    061    RgbValues getRgbValues(final TristimulusValues tristimulusValues) {
    062        final float varX = tristimulusValues.x / 100f; // X = From 0 to ref_X
    063        final float varY = tristimulusValues.y / 100f; // Y = From 0 to ref_Y
    064        final float varZ = tristimulusValues.z / 100f; // Z = From 0 to ref_Y
    065
    066        float varR = varX * 3.2406f + varY * -1.5372f + varZ * -0.4986f;
    067        float varG = varX * -0.9689f + varY * 1.8758f + varZ * 0.0415f;
    068        float varB = varX * 0.0557f + varY * -0.2040f + varZ * 1.0570f;
    069
    070        if (varR > 0.0031308) {
    071            varR = 1.055f * (float) Math.pow(varR, 1 / 2.4) - 0.055f;
    072        } else {
    073            varR = 12.92f * varR;
    074        }
    075        if (varG > 0.0031308) {
    076            varG = 1.055f * (float) Math.pow(varG, 1 / 2.4) - 0.055f;
    077        } else {
    078            varG = 12.92f * varG;
    079        }
    080
    081        if (varB > 0.0031308) {
    082            varB = 1.055f * (float) Math.pow(varB, 1 / 2.4) - 0.055f;
    083        } else {
    084            varB = 12.92f * varB;
    085        }
    086
    087        // var_R = ((var_R + 0.16561039f) / (3.0152583f + 0.16561039f));
    088        // var_G = ((var_G + 0.06561642f) / (3.0239854f + 0.06561642f));
    089        // var_B = ((var_B + 0.19393992f) / (3.1043448f + 0.19393992f));
    090
    091        final RgbValues values = new RgbValues();
    092        values.r = (int) (varR * 255f);
    093        values.g = (int) (varG * 255f);
    094        values.b = (int) (varB * 255f);
    095        return values;
    096    }
    097
    098    /**
    099     * 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}