001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2007, Geotools Project Managment Committee (PMC)
005: * (C) 2007, Geomatys
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.image.io;
018:
019: import java.awt.image.DataBuffer;
020: import java.awt.image.SampleModel;
021: import java.awt.image.ColorModel;
022: import java.awt.image.IndexColorModel;
023: import javax.imageio.ImageTypeSpecifier;
024: import java.io.IOException;
025:
026: import junit.framework.Test;
027: import junit.framework.TestCase;
028: import junit.framework.TestSuite;
029:
030: /**
031: * Tests {@link GeographicImageReader}.
032: *
033: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/unsupported/coverageio/src/test/java/org/geotools/image/io/GeographicImageReaderTest.java $
034: * @version $Id: GeographicImageReaderTest.java 27848 2007-11-12 13:10:32Z desruisseaux $
035: * @author Martin Desruisseaux
036: */
037: public class GeographicImageReaderTest extends TestCase {
038: /**
039: * Run the suite from the command line.
040: */
041: public static void main(final String[] args) {
042: org.geotools.util.logging.Logging.GEOTOOLS
043: .forceMonolineConsoleOutput();
044: junit.textui.TestRunner.run(suite());
045: }
046:
047: /**
048: * Returns the test suite.
049: */
050: public static Test suite() {
051: return new TestSuite(GeographicImageReaderTest.class);
052: }
053:
054: /**
055: * Constructs a test case with the given name.
056: */
057: public GeographicImageReaderTest(final String name) {
058: super (name);
059: }
060:
061: /**
062: * Tests {@link DataBuffer#TYPE_FLOAT}. The converter should be the identity one
063: * except for pad values which should be replaced by NaN.
064: */
065: public void testTypeFloat() throws IOException {
066: final float minimum = -2000;
067: final float maximum = 4000;
068: final float padValue = -9999;
069: final SampleConverter[] converters = new SampleConverter[1];
070: final GeographicImageReader reader = new NullImageReader(
071: DataBuffer.TYPE_FLOAT, minimum, maximum, padValue);
072: final ImageTypeSpecifier specifier = reader.getRawImageType(0,
073: null, converters);
074: final SampleConverter converter = converters[0];
075:
076: // Tests the converter
077: assertNotNull(converter);
078: assertEquals(0.0, converter.getOffset(), 0.0);
079: assertEquals(minimum, converter.convert(minimum), 0f);
080: assertEquals(maximum, converter.convert(maximum), 0f);
081: assertTrue(Float.isNaN(converter.convert(padValue)));
082:
083: // Tests the sample model
084: final SampleModel sm = specifier.getSampleModel();
085: assertNotNull(sm);
086: assertEquals(DataBuffer.TYPE_FLOAT, sm.getDataType());
087:
088: // Tests the color model
089: final ColorModel cm = specifier.getColorModel();
090: assertFalse(cm instanceof IndexColorModel);
091: }
092:
093: /**
094: * Tests {@link DataBuffer#TYPE_SHORT}. The converter should be the identity one,
095: * including pad values. The index color model is going to be quite large.
096: */
097: public void testTypeShort() throws IOException {
098: final int minimum = -2000;
099: final int maximum = 4000;
100: final int padValue = -9999;
101: final SampleConverter[] converters = new SampleConverter[1];
102: final GeographicImageReader reader = new NullImageReader(
103: DataBuffer.TYPE_SHORT, minimum, maximum, padValue);
104: final GeographicImageReadParam param = (GeographicImageReadParam) reader
105: .getDefaultReadParam();
106: param.setPaletteName("grayscale"); // Easier to test than "rainbow".
107: final ImageTypeSpecifier specifier = reader.getRawImageType(0,
108: param, converters);
109: final SampleConverter converter = converters[0];
110:
111: // Tests the converter
112: assertNotNull(converter);
113: assertEquals(0.0, converter.getOffset(), 0.0);
114: assertEquals(minimum, converter.convert(minimum));
115: assertEquals(maximum, converter.convert(maximum));
116: assertEquals(padValue, converter.convert(padValue));
117:
118: // Tests the sample model
119: final SampleModel sm = specifier.getSampleModel();
120: assertNotNull(sm);
121: assertEquals(DataBuffer.TYPE_USHORT, sm.getDataType());
122:
123: // Tests the color model
124: final ColorModel cm = specifier.getColorModel();
125: assertTrue(cm instanceof IndexColorModel);
126: final IndexColorModel indexed = (IndexColorModel) cm;
127: assertEquals(65536, indexed.getMapSize());
128: assertEquals(0xFF000000, indexed.getRGB(minimum & 0xFFFF)); // Opaque black
129: assertEquals(0xFFFFFFFF, indexed.getRGB(maximum & 0xFFFF)); // Opaque white
130: assertEquals(0x00000000, indexed.getRGB(padValue & 0xFFFF)); // Transparent
131: if (false) {
132: // Disabled for now, because current implementation sets an arbitrary
133: // transparent pixel. I don't think that this is a real problem...
134: assertEquals(padValue & 0xFFFF, indexed
135: .getTransparentPixel());
136: }
137: }
138:
139: /**
140: * Tests {@link DataBuffer#TYPE_USHORT}. The converter should pack the negative
141: * values in a smaller range.
142: */
143: public void testTypeUnsignedShort() throws IOException {
144: final int minimum = -2000;
145: final int maximum = 4000;
146: final int padValue = -9999;
147: final SampleConverter[] converters = new SampleConverter[1];
148: final GeographicImageReader reader = new NullImageReader(
149: DataBuffer.TYPE_USHORT, minimum, maximum, padValue);
150: final GeographicImageReadParam param = (GeographicImageReadParam) reader
151: .getDefaultReadParam();
152: param.setPaletteName("grayscale"); // Easier to test than "rainbow".
153: final ImageTypeSpecifier specifier = reader.getRawImageType(0,
154: param, converters);
155: final SampleConverter converter = converters[0];
156:
157: // Tests the converter
158: assertNotNull(converter);
159: assertEquals(1 - minimum, converter.getOffset(), 0.0);
160: assertEquals(1, converter.convert(minimum));
161: assertEquals(1 + maximum - minimum, converter.convert(maximum));
162: assertEquals(0, converter.convert(padValue));
163:
164: // Tests the sample model
165: final SampleModel sm = specifier.getSampleModel();
166: assertNotNull(sm);
167: assertEquals(DataBuffer.TYPE_USHORT, sm.getDataType());
168:
169: // Tests the color model
170: final ColorModel cm = specifier.getColorModel();
171: assertTrue(cm instanceof IndexColorModel);
172: final IndexColorModel indexed = (IndexColorModel) cm;
173: assertEquals(2 + maximum - minimum, indexed.getMapSize());
174: assertEquals(0xFF000000, indexed.getRGB(converter
175: .convert(minimum) & 0xFFFF)); // Opaque black
176: assertEquals(0xFFFFFFFF, indexed.getRGB(converter
177: .convert(maximum) & 0xFFFF)); // Opaque white
178: assertEquals(0x00000000, indexed.getRGB(converter
179: .convert(padValue) & 0xFFFF)); // Transparent
180: assertEquals(0, indexed.getTransparentPixel());
181: }
182:
183: /**
184: * Tests {@link DataBuffer#TYPE_BYTE}.
185: */
186: public void testTypeUnsignedByte() throws IOException {
187: final int minimum = 0;
188: final int maximum = 200;
189: final int padValue = 255;
190: final SampleConverter[] converters = new SampleConverter[1];
191: final GeographicImageReader reader = new NullImageReader(
192: DataBuffer.TYPE_BYTE, minimum, maximum, padValue);
193: final GeographicImageReadParam param = (GeographicImageReadParam) reader
194: .getDefaultReadParam();
195: param.setPaletteName("grayscale"); // Easier to test than "rainbow".
196: final ImageTypeSpecifier specifier = reader.getRawImageType(0,
197: param, converters);
198: final SampleConverter converter = converters[0];
199:
200: // Tests the converter
201: assertNotNull(converter);
202: assertEquals(0, converter.getOffset(), 0.0);
203: assertEquals(minimum, converter.convert(minimum));
204: assertEquals(maximum, converter.convert(maximum));
205: assertEquals(padValue, converter.convert(padValue));
206:
207: // Tests the sample model
208: final SampleModel sm = specifier.getSampleModel();
209: assertNotNull(sm);
210: assertEquals(DataBuffer.TYPE_BYTE, sm.getDataType());
211:
212: // Tests the color model
213: final ColorModel cm = specifier.getColorModel();
214: assertTrue(cm instanceof IndexColorModel);
215: final IndexColorModel indexed = (IndexColorModel) cm;
216: assertEquals(256, indexed.getMapSize());
217: assertEquals(0xFF000000, indexed.getRGB(minimum & 0xFFFF)); // Opaque black
218: assertEquals(0xFFFFFFFF, indexed.getRGB(maximum & 0xFFFF)); // Opaque white
219: assertEquals(0x00000000, indexed.getRGB(padValue & 0xFFFF)); // Transparent
220: }
221: }
|