001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2003-2006, Geotools Project Management Committee (PMC)
005: * (C) 2001, Institut de Recherche pour le Développement
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: */
017: package org.geotools.coverage;
018:
019: // J2SE dependencies
020: import java.awt.color.ColorSpace;
021:
022: // Geotools dependencies
023: import org.geotools.resources.Utilities;
024:
025: /**
026: * Color space for raster backed by floating point numbers ranging between two arbitrary values.
027: *
028: * <strong>NOTE:</strong>
029: * Current implementation is a copy of {@code org.geotools.image.io.ScaledColorSpace}.
030: * Future implementation will be differents (interpolate in a color table instead of
031: * computing grayscales).
032: *
033: * @since 2.1
034: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/coverage/src/main/java/org/geotools/coverage/ScaledColorSpace.java $
035: * @version $Id: ScaledColorSpace.java 22817 2006-11-17 17:24:55Z desruisseaux $
036: * @author Martin Desruisseaux
037: *
038: * @todo Consider extending {@link javax.media.jai.ColorSpaceJAI}.
039: */
040: final class ScaledColorSpace extends ColorSpace {
041: /**
042: * Minimal normalized RGB value.
043: */
044: private static final float MIN_VALUE = 0f;
045:
046: /**
047: * Maximal normalized RGB value.
048: */
049: private static final float MAX_VALUE = 1f;
050:
051: /**
052: * The band to make visible (usually 0).
053: */
054: private final int band;
055:
056: /**
057: * Facteur par lequel multiplier les pixels.
058: */
059: private final float scale;
060:
061: /**
062: * Nombre à aditionner aux pixels après
063: * les avoir multiplier par {@link #scale}.
064: */
065: private final float offset;
066:
067: /**
068: * Construit un modèle de couleurs.
069: *
070: * @param band La bande à rendre visible (habituellement 0).
071: * @param numComponents Nombre de composante (seule la première sera prise en compte).
072: * @param minimum La valeur géophysique minimale.
073: * @param maximum La valeur géophysique maximale.
074: */
075: public ScaledColorSpace(final int band, final int numComponents,
076: final double minimum, final double maximum) {
077: super (TYPE_GRAY, numComponents);
078: this .band = band;
079: final double scale = (maximum - minimum)
080: / (MAX_VALUE - MIN_VALUE);
081: final double offset = minimum - MIN_VALUE * scale;
082: this .scale = (float) scale;
083: this .offset = (float) offset;
084: }
085:
086: /**
087: * Retourne une couleur RGB en tons de
088: * gris pour le nombre réel spécifié.
089: */
090: public float[] toRGB(final float[] values) {
091: float value = (values[band] - offset) / scale;
092: if (Float.isNaN(value))
093: value = MIN_VALUE;
094: return new float[] { value, value, value };
095: }
096:
097: /**
098: * Retourne une valeur réelle pour
099: * le ton de gris spécifié.
100: */
101: public float[] fromRGB(final float[] RGB) {
102: final float[] values = new float[getNumComponents()];
103: values[band] = (RGB[0] + RGB[1] + RGB[2]) / 3 * scale + offset;
104: return values;
105: }
106:
107: /**
108: * Convertit les valeurs en couleurs dans l'espace CIEXYZ.
109: */
110: public float[] toCIEXYZ(final float[] values) {
111: float value = (values[band] - offset) / scale;
112: if (Float.isNaN(value))
113: value = MIN_VALUE;
114: return new float[] { value * 0.9642f, value * 1.0000f,
115: value * 0.8249f };
116: }
117:
118: /**
119: * Convertit les couleurs de l'espace CIEXYZ en valeurs.
120: */
121: public float[] fromCIEXYZ(final float[] RGB) {
122: final float[] values = new float[getNumComponents()];
123: values[band] = (RGB[0] / 0.9642f + RGB[1] + RGB[2] / 0.8249f)
124: / 3 * scale + offset;
125: return values;
126: }
127:
128: /**
129: * Retourne la valeur minimale autorisée.
130: */
131: public float getMinValue(final int component) {
132: return MIN_VALUE * scale + offset;
133: }
134:
135: /**
136: * Retourne la valeur maximale autorisée.
137: */
138: public float getMaxValue(final int component) {
139: return MAX_VALUE * scale + offset;
140: }
141:
142: /**
143: * Returns a string representation of this color model.
144: */
145: public String toString() {
146: return Utilities.getShortClassName(this ) + '['
147: + getMinValue(band) + ", " + getMaxValue(band) + ']';
148: }
149: }
|