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.coverage.grid;
018:
019: // J2SE dependencies
020: import java.awt.image.BufferedImage;
021: import java.awt.image.DataBuffer;
022: import java.awt.image.DataBufferByte;
023: import java.awt.image.RenderedImage;
024: import java.util.Random;
025:
026: // JAI dependencies
027: import javax.media.jai.PlanarImage;
028: import javax.media.jai.RenderedImageAdapter;
029: import javax.media.jai.RenderedOp;
030:
031: // JUnit dependencies
032: import junit.framework.Test;
033: import junit.framework.TestCase;
034: import junit.framework.TestSuite;
035:
036: // OpenGIS dependencies
037: import org.opengis.referencing.operation.MathTransform;
038: import org.opengis.referencing.operation.TransformException;
039:
040: // Geotools dependencies
041: import org.geotools.coverage.Category;
042: import org.geotools.coverage.CategoryListTest;
043: import org.geotools.coverage.FactoryFinder;
044: import org.geotools.coverage.GridSampleDimension;
045: import org.geotools.coverage.grid.GridCoverageFactory;
046: import org.geotools.referencing.crs.DefaultGeographicCRS;
047: import org.geotools.referencing.operation.transform.IdentityTransform;
048:
049: /**
050: * Test the {@link SampleTranscoder} implementation. Image adapter depends
051: * heavily on {@link CategoryList}, so this one should be tested first.
052: *
053: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/coverage/src/test/java/org/geotools/coverage/grid/SampleTranscoderTest.java $
054: * @version $Id: SampleTranscoderTest.java 23211 2006-12-05 00:38:41Z desruisseaux $
055: * @author Martin Desruisseaux
056: */
057: public class SampleTranscoderTest extends TestCase {
058: /**
059: * Small value for comparaisons. Remind: transformed values are stored in a new image
060: * using the 'float' data type. So we can't expected as much precision than with a
061: * 'double' data type.
062: */
063: private static final double EPS = 1E-5;
064:
065: /**
066: * Random number generator for this test.
067: */
068: private static final Random random = new Random(
069: 6215962897884256696L);
070:
071: /**
072: * A sample dimension for a band.
073: */
074: private GridSampleDimension band1;
075:
076: /**
077: * Run the suite from the command line.
078: */
079: public static void main(final String[] args) {
080: junit.textui.TestRunner.run(suite());
081: }
082:
083: /**
084: * Returns the test suite.
085: */
086: public static Test suite() {
087: return new TestSuite(SampleTranscoderTest.class);
088: }
089:
090: /**
091: * Constructs a test case with the given name.
092: */
093: public SampleTranscoderTest(final String name) {
094: super (name);
095: }
096:
097: /**
098: * Set up common objects used for all tests.
099: */
100: protected void setUp() throws Exception {
101: super .setUp();
102: band1 = new GridSampleDimension("Temperature", new Category[] {
103: new Category("No data", null, 0),
104: new Category("Land", null, 1),
105: new Category("Clouds", null, 2),
106: new Category("Temperature", null, 3, 100, 0.1, 5),
107: new Category("Foo", null, 100, 160, -1, 3),
108: new Category("Tarzan", null, 160) }, null);
109: }
110:
111: /**
112: * Tests the transformation using a random raster with only one band.
113: */
114: public void testOneBand() throws TransformException {
115: assertTrue(testOneBand(1, 0) instanceof RenderedImageAdapter);
116: assertTrue(testOneBand(.8, 2) instanceof RenderedOp);
117: assertTrue(testOneBand(band1) instanceof RenderedOp);
118: }
119:
120: /**
121: * Tests the transformation using a random raster with only one band.
122: * A sample dimension with only one category will be used.
123: *
124: * @param scale The scale factor.
125: * @param offset The offset value.
126: * @return The transformed image.
127: */
128: private RenderedImage testOneBand(final double scale,
129: final double offset) throws TransformException {
130: final Category category = new Category("Values", null, 0, 256,
131: scale, offset);
132: return testOneBand(new GridSampleDimension("Measure",
133: new Category[] { category }, null));
134: }
135:
136: /**
137: * Tests the transformation using a random raster with only one band.
138: *
139: * @param band The sample dimension for the only band.
140: * @return The transformed image.
141: */
142: private RenderedImage testOneBand(final GridSampleDimension band)
143: throws TransformException {
144: final int SIZE = 64;
145: /*
146: * Constructs a 64x64 image with random values.
147: * Samples values are integer in the range 0..160 inclusive.
148: */
149: final BufferedImage source = new BufferedImage(SIZE, SIZE,
150: BufferedImage.TYPE_BYTE_INDEXED);
151: final DataBufferByte buffer = (DataBufferByte) source
152: .getRaster().getDataBuffer();
153: final byte[] array = buffer.getData(0);
154: for (int i = 0; i < array.length; i++) {
155: array[i] = (byte) random.nextInt(161);
156: }
157: final MathTransform identity = IdentityTransform.create(2);
158: final GridCoverageFactory factory = FactoryFinder
159: .getGridCoverageFactory(null);
160: GridCoverage2D coverage;
161: coverage = (GridCoverage2D) factory.create("Test", source,
162: DefaultGeographicCRS.WGS84, identity,
163: new GridSampleDimension[] { band }, null, null);
164: /*
165: * Apply the operation. The SampleTranscoder class is suppose to transform our
166: * integers into real-world values. Check if the result use floating-points.
167: */
168: final RenderedImage target = coverage.geophysics(true)
169: .getRenderedImage();
170: assertSame(target, PlanarImage.wrapRenderedImage(target));
171: assertEquals(DataBuffer.TYPE_BYTE, source.getSampleModel()
172: .getDataType());
173: if (coverage.getRenderedImage() != target) {
174: assertEquals(DataBuffer.TYPE_FLOAT, target.getSampleModel()
175: .getDataType());
176: }
177: /*
178: * Now, gets the data as an array and compare it with the expected values.
179: */
180: double[] sourceData = source.getData().getSamples(0, 0, SIZE,
181: SIZE, 0, (double[]) null);
182: double[] targetData = target.getData().getSamples(0, 0, SIZE,
183: SIZE, 0, (double[]) null);
184: band.getSampleToGeophysics().transform(sourceData, 0,
185: sourceData, 0, sourceData.length);
186: CategoryListTest.compare(sourceData, targetData, EPS);
187: /*
188: * Construct a new image with the resulting data, and apply an inverse transformation.
189: * Compare the resulting values with the original data.
190: */
191: RenderedImage back = PlanarImage.wrapRenderedImage(target)
192: .getAsBufferedImage();
193: coverage = (GridCoverage2D) factory.create("Test", back,
194: DefaultGeographicCRS.WGS84, identity,
195: new GridSampleDimension[] { band.geophysics(true) },
196: null, null);
197:
198: back = coverage.geophysics(false).getRenderedImage();
199: assertEquals(DataBuffer.TYPE_BYTE, back.getSampleModel()
200: .getDataType());
201: sourceData = source.getData().getSamples(0, 0, SIZE, SIZE, 0,
202: (double[]) null);
203: targetData = back.getData().getSamples(0, 0, SIZE, SIZE, 0,
204: (double[]) null);
205: CategoryListTest.compare(sourceData, targetData, 1 + EPS);
206: /*
207: * Returns the "geophysics view" of the image.
208: */
209: return target;
210: }
211: }
|