/*
 *    GeoTools - OpenSource mapping toolkit
 *    http://geotools.org
 *    (C) 2003-2006, Geotools Project Managment Committee (PMC)
 *    (C) 2001, Institut de Recherche pour le Développement
 *
 *    This library is free software; you can redistribute it and/or
 *    modify it under the terms of the GNU Lesser General Public
 *    License as published by the Free Software Foundation; either
 *    version 2.1 of the License, or (at your option) any later version.
 *
 *    This library is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *    Lesser General Public License for more details.
 */
package org.geotools.image.io;

import java.awt.color.ColorSpace;
import org.geotools.resources.Utilities;


/**
 * Color space for images storing pixels as real numbers. The color model can have an
 * arbitrary number of bands, but in current implementation only one band is used.
 *
 * @author Martin Desruisseaux
 * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/unsupported/coverageio/src/main/java/org/geotools/image/io/ScaledColorSpace.java $
 * @version $Id: ScaledColorSpace.java 26695 2007-08-23 18:58:56Z desruisseaux $
 */
final class ScaledColorSpace extends ColorSpace {
    /**
     * Minimal normalized RGB value.
     */
    private static final float MIN_VALUE = 0f;

    /**
     * Maximal normalized RGB value.
     */
    private static final float MAX_VALUE = 1f;

    /**
     * The scaling factor for pixel values.
     */
    private final float scale;

    /**
     * The offset to apply after the {@linkplain #scale} on pixel values.
     */
    private final float offset;

    /**
     * Index of the band to display.
     */
    private final int visibleBand;

    /**
     * Creates a color model.
     *
     * @param numComponents The number of components.
     * @param visibleBand The band to use for computing colors.
     * @param minimum The minimal sample value expected.
     * @param maximum The maximal sample value expected.
     */
    public ScaledColorSpace(final int numComponents, final int visibleBand,
                            final float minimum, final float maximum)
    {
        super(TYPE_GRAY, numComponents);
        this.visibleBand = visibleBand;
        scale  = (maximum - minimum) / (MAX_VALUE - MIN_VALUE);
        offset = minimum - MIN_VALUE*scale;
    }

    /**
     * Returns a RGB color for a gray scale value.
     */
    public float[] toRGB(final float[] values) {
        float value = (values[visibleBand] - offset) / scale;
        if (Float.isNaN(value)) {
            value = MIN_VALUE;
        }
        return new float[] {value, value, value};
    }

    /**
     * Returns a real value for the specified RGB color.
     * The RGB color is assumed to be a gray scale value.
     */
    public float[] fromRGB(final float[] RGB) {
        final float[] values = new float[getNumComponents()];
        values[visibleBand] = (RGB[0] + RGB[1] + RGB[2]) / 3 * scale + offset;
        return values;
    }

    /**
     * Convert a color to the CIEXYZ color space.
     */
    public float[] toCIEXYZ(final float[] values) {
        float value = (values[visibleBand] - offset) / scale;
        if (Float.isNaN(value)) {
            value = MIN_VALUE;
        }
        return new float[] {
            value * 0.9642f,
            value * 1.0000f,
            value * 0.8249f
        };
    }

    /**
     * Convert a color from the CIEXYZ color space.
     */
    public float[] fromCIEXYZ(final float[] RGB) {
        final float[] values = new float[getNumComponents()];
        values[visibleBand] = (RGB[0] / 0.9642f + RGB[1] + RGB[2] / 0.8249f) / 3 * scale + offset;
        return values;
    }

    /**
     * Returns the minimum value for the specified RGB component.
     */
    //@Override
    public float getMinValue(final int component) {
        return MIN_VALUE * scale + offset;
    }

    /**
     * Returns the maximum value for the specified RGB component.
     */
    //@Override
    public float getMaxValue(final int component) {
        return MAX_VALUE * scale + offset;
    }

    /**
     * Returns a string representation of this color model.
     */
    //@Override
    public String toString() {
        return Utilities.getShortClassName(this) + 
                '[' + getMinValue(visibleBand) + ", " + getMaxValue(visibleBand) + ']';
    }
}
