001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2003-2006, Geotools Project Managment Committee (PMC)
005: * (C) 2002, 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.resources.image;
018:
019: // J2SE dependencies
020: import java.awt.color.ColorSpace;
021: import java.awt.image.ComponentColorModel;
022: import java.awt.image.ComponentSampleModel;
023: import java.awt.image.DataBuffer;
024: import java.awt.image.SampleModel;
025:
026: import javax.media.jai.ComponentSampleModelJAI;
027: import javax.media.jai.FloatDoubleColorModel;
028: import javax.media.jai.iterator.RectIter;
029:
030: /**
031: * A {@link ComponentColorModel} modified for interoperability with Java Advanced Imaging.
032: * JAI 1.1 was designed for use with J2SE 1.3 and is not aware of new features in J2SE 1.4.
033: * This leads to the following problems:
034: *
035: * <ul>
036: * <li>{@link ComponentColorModel} support {@code float} and {@code double}
037: * datatypes since J2SE 1.4 only. The workaround for J2SE 1.3 is to use the
038: * {@link FloatDoubleColorModel} provided with JAI 1.1.</li>
039: * <li>{@link FloatDoubleColorModel} ignores the new API in {@link ColorSpace}, especially
040: * the {@code getMinValue} and {@code getMaxValue} methods. Consequently,
041: * rendering of any image using our custom {@code ScaledColorSpace} is wrong.</li>
042: * <li>{@link ComponentColorModel} uses {@link java.awt.image.DataBufferFloat} and {@link
043: * java.awt.image.DataBufferDouble}, which are unknown to JAI 1.1. Consequently, trying
044: * to use {@link RectIter} with one of those will throw {@link ClassCastException}.</li>
045: * </ul>
046: *
047: * The work around it to use J2SE's {@link ComponentColorModel} (which work with our custom
048: * {@link ColorSpace}) and override its {@code createCompatibleSampleModel} in order to
049: * returns {@link ComponentSampleModelJAI} instead of {@link ComponentSampleModel} when
050: * {@code float} or {@code double} datatype is requested.
051: *
052: * @todo Remove this patch when JAI will recognize J2SE 1.4 classes.
053: *
054: * @since 2.0
055: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/coverage/src/main/java/org/geotools/resources/image/ComponentColorModelJAI.java $
056: * @version $Id: ComponentColorModelJAI.java 20970 2006-08-11 07:53:22Z jgarnett $
057: * @author Martin Desruisseaux
058: */
059: public class ComponentColorModelJAI extends ComponentColorModel {
060: /**
061: * Construct a new color model.
062: */
063: public ComponentColorModelJAI(final ColorSpace colorSpace,
064: final int[] bits, final boolean hasAlpha,
065: final boolean isAlphaPremultiplied, final int transparency,
066: final int transferType) {
067: super (colorSpace, bits, hasAlpha, isAlphaPremultiplied,
068: transparency, transferType);
069: }
070:
071: /**
072: * Construct a new color model.
073: */
074: public ComponentColorModelJAI(final ColorSpace colorSpace,
075: final boolean hasAlpha, final boolean isAlphaPremultiplied,
076: final int transparency, final int transferType) {
077: super (colorSpace, hasAlpha, isAlphaPremultiplied, transparency,
078: transferType);
079: }
080:
081: /**
082: * Returns a compatible sample model. This implementation is nearly identical
083: * to default J2SE's implementation, except that it construct a JAI color model
084: * instead of a J2SE one.
085: */
086: public SampleModel createCompatibleSampleModel(final int w,
087: final int h) {
088: switch (transferType) {
089: default: {
090: return super .createCompatibleSampleModel(w, h);
091: }
092: case DataBuffer.TYPE_FLOAT: // fall through
093: case DataBuffer.TYPE_DOUBLE: {
094: final int numComponents = getNumComponents();
095: final int[] bandOffsets = new int[numComponents];
096: for (int i = 0; i < numComponents; i++) {
097: bandOffsets[i] = i;
098: }
099: return new ComponentSampleModelJAI(transferType, w, h,
100: numComponents, w * numComponents, bandOffsets);
101: }
102: }
103: }
104:
105: /**
106: * Returns the {@code String} representation of the contents of
107: * this {@code ColorModel}object.
108: *
109: * @return a {@code String} representing the contents of this
110: * {@code ColorModel} object.
111: */
112: public String toString() {
113: return new String("ComponentColorModelJAI: #pixelBits = "
114: + pixel_bits + " numComponents = "
115: + super .getNumComponents() + " color space = "
116: + super .getColorSpace() + " transparency = "
117: + super .getTransparency() + " has alpha = "
118: + super .hasAlpha() + " isAlphaPre = "
119: + super.isAlphaPremultiplied());
120: }
121: }
|