    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
    024/**
    025 * Photometric interpretation Logluv support. Logluv is an encoding for storing
    026 * data inside TIFF images.
    027 *
    028 * @see <a href="https://en.wikipedia.org/wiki/Logluv_TIFF">Logluv TIFF</a>
    029 */
    030public class PhotometricInterpreterLogLuv extends PhotometricInterpreter {
    031
    032    /**
    033     * Tristimulus color values (red-green-blue, as X-Y-Z, in the CIE XYZ color space).
    034     */
    035    static class TristimulusValues {
    036        public float x;
    037        public float y;
    038        public float z;
    039    }
    040
    041    /**
    042     * Rgb values (reg-green-blue, as R-G-B, as in the RGB color model).
    043     */
    044    static class RgbValues {
    045        public int r;
    046        public int g;
    047        public int b;
    048    }
    049
    050    public PhotometricInterpreterLogLuv(final int samplesPerPixel,
    051            final int[] bitsPerSample, final int predictor, final int width, final int height) {
    052        super(samplesPerPixel, bitsPerSample, predictor, width, height);
    053    }
    054
    055    @Override
    056    public void interpretPixel(final ImageBuilder imageBuilder, final int[] samples, final int x,
    057            final int y) throws ImageReadException, IOException {
    058        if (samples == null || samples.length != 3) {
    059            throw new ImageReadException("Invalid length of bits per sample (expected 3).");
    060        }
    061
    062        // CIE illuminants. An illuminant is a theorical source of visible light with a profile.
    063        // CIE stands for Commission Internationale de l'Eclairage, or International
    064        // Comission on Illumination.
    065        final int cieL = samples[0];
    066        final int cieA = (byte) samples[1];
    067        final int cieB = (byte) samples[2];
    068
    069        final TristimulusValues tristimulusValues = getTristimulusValues(cieL, cieA, cieB);
    070
    071        // ref_X = 95.047 //Observer = 2°, Illuminant = D65
    072        // ref_Y = 100.000
    073        // ref_Z = 108.883
    074
    075        final RgbValues rgbValues = getRgbValues(tristimulusValues);
    076
    077        // float R = 1.910f * X - 0.532f * Y - 0.288f * Z;
    078        // float G = -0.985f * X + 1.999f * Y - 0.028f * Z;
    079        // float B = 0.058f * X - 0.118f * Y + 0.898f * Z;
    080
    081        final int red = Math.min(255, Math.max(0, rgbValues.r));
    082        final int green = Math.min(255, Math.max(0, rgbValues.g));
    083        final int blue = Math.min(255, Math.max(0, rgbValues.b));
    084        final int alpha = 0xff;
    085        final int rgb = (alpha << 24) | (red << 16) | (green << 8) | (blue << 0);
    086        imageBuilder.setRGB(x, y, rgb);
    087
    088    }
    089
    090    /**
    091     * Receives a triplet of CIELAB values, and calculates the tristimulus values.
    092     * The reference white point used here is the equivalent to summer sun and sky.
    093     *
    094     * @param cieL lightness from black to white
    095     * @param cieA lightness from green to red
    096     * @param cieB lightness from blue to yellow
    097     * @return tristimulus (X, Y, and Z) values
    098     * @see <a href="https://en.wikipedia.org/wiki/CIELAB_color_space">CIELAB color space</a>
    099     * @see <a href="https://en.wikipedia.org/wiki/White_point">White point</a>
    100     */
    101    TristimulusValues getTristimulusValues(final int cieL, final int cieA, final int cieB) {
    102        float var_Y = ((cieL * 100.0f / 255.0f) + 16.0f) / 116.0f;
    103        float var_X = cieA / 500.0f + var_Y;
    104        float var_Z = var_Y - cieB / 200.0f;
    105
    106        final float var_x_cube = (float) Math.pow(var_X, 3.0d);
    107        final float var_y_cube = (float) Math.pow(var_Y, 3.0d);
    108        final float var_z_cube = (float) Math.pow(var_Z, 3.0d);
    109
    110        if (var_y_cube > 0.008856f) {
    111            var_Y = var_y_cube;
    112        } else {
    113            var_Y = (var_Y - 16 / 116.0f) / 7.787f;
    114        }
    115
    116        if (var_x_cube > 0.008856f) {
    117            var_X = var_x_cube;
    118        } else {
    119            var_X = (var_X - 16 / 116.0f) / 7.787f;
    120        }
    121
    122        if (var_z_cube > 0.008856f) {
    123            var_Z = var_z_cube;
    124        } else {
    125            var_Z = (var_Z - 16 / 116.0f) / 7.787f;
    126        }
    127
    128        // These reference values are the relative white points (XYZ) for commons scene types.
    129        // The chosen values here reflect a scene with Summer Sun and Sky, temperature of 6504 K,
    130        // X 95.047, Y 100.0, and Z 108.883.
    131        // See Color Science by Wyszecki and Stiles for more
    132        final float ref_X = 95.047f;
    133        final float ref_Y = 100.000f;
    134        final float ref_Z = 108.883f;
    135
    136        final TristimulusValues values = new TristimulusValues();
    137        values.x = ref_X * var_X; // ref_X = 95.047 Observer= 2°, Illuminant= D65
    138        values.y = ref_Y * var_Y; // ref_Y = 100.000
    139        values.z = ref_Z * var_Z; // ref_Z = 108.883
    140        return values;
    141    }
    142
    143    /**
    144     * Receives a triplet tristimulus values (CIE XYZ) and then does a CIELAB-CIEXYZ
    145     * conversion (consult Wikipedia link for formula), where the CIELAB values are
    146     * used to calculate the tristimulus values of the reference white point.
    147     *
    148     * @param tristimulusValues the XYZ tristimulus values
    149     * @return RGB values
    150     * @see <a href="https://en.wikipedia.org/wiki/CIELAB_color_space">CIELAB color space</a>
    151     */
    152    RgbValues getRgbValues(final TristimulusValues tristimulusValues) {
    153        final float var_X = tristimulusValues.x / 100f; // X = From 0 to ref_X
    154        final float var_Y = tristimulusValues.y / 100f; // Y = From 0 to ref_Y
    155        final float var_Z = tristimulusValues.z / 100f; // Z = From 0 to ref_Y
    156
    157        float var_R = var_X * 3.2406f + var_Y * -1.5372f + var_Z * -0.4986f;
    158        float var_G = var_X * -0.9689f + var_Y * 1.8758f + var_Z * 0.0415f;
    159        float var_B = var_X * 0.0557f + var_Y * -0.2040f + var_Z * 1.0570f;
    160
    161        if (var_R > 0.0031308) {
    162            var_R = 1.055f * (float) Math.pow(var_R, (1 / 2.4)) - 0.055f;
    163        } else {
    164            var_R = 12.92f * var_R;
    165        }
    166        if (var_G > 0.0031308) {
    167            var_G = 1.055f * (float) Math.pow(var_G, (1 / 2.4)) - 0.055f;
    168        } else {
    169            var_G = 12.92f * var_G;
    170        }
    171
    172        if (var_B > 0.0031308) {
    173            var_B = 1.055f * (float) Math.pow(var_B, (1 / 2.4)) - 0.055f;
    174        } else {
    175            var_B = 12.92f * var_B;
    176        }
    177
    178        // var_R = ((var_R + 0.16561039f) / (3.0152583f + 0.16561039f));
    179        // var_G = ((var_G + 0.06561642f) / (3.0239854f + 0.06561642f));
    180        // var_B = ((var_B + 0.19393992f) / (3.1043448f + 0.19393992f));
    181
    182        final RgbValues values = new RgbValues();
    183        values.r = (int) (var_R * 255f);
    184        values.g = (int) (var_G * 255f);
    185        values.b = (int) (var_B * 255f);
    186        return values;
    187    }
    188}