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;
018:
019: // J2SE dependencies
020: import java.awt.image.BufferedImage;
021: import java.util.Random;
022:
023: // JAI dependencies
024: import javax.media.jai.JAI;
025: import javax.media.jai.OperationRegistry;
026: import javax.media.jai.ParameterBlockJAI;
027: import javax.media.jai.ParameterListDescriptor;
028: import javax.media.jai.RenderedOp;
029:
030: // JUnit dependencies
031: import junit.framework.Test;
032: import junit.framework.TestCase;
033: import junit.framework.TestSuite;
034:
035: /**
036: * Test the {@link GridSampleDimension} implementation. Since {@code GridSampleDimension}
037: * rely on {@link CategoryList} for many of its work, many {@code GridSampleDimension}
038: * tests are actually {@code CategoryList} tests.
039: *
040: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/coverage/src/test/java/org/geotools/coverage/SampleDimensionTest.java $
041: * @version $Id: SampleDimensionTest.java 27848 2007-11-12 13:10:32Z desruisseaux $
042: * @author Martin Desruisseaux
043: */
044: public class SampleDimensionTest extends TestCase {
045: /**
046: * The categories making the sample dimension to test.
047: */
048: private static final String[] CATEGORIES = { "No data", "Clouds",
049: "Lands" };
050:
051: /**
052: * The "no data" values making the sample dimension to test.
053: * There is one for each category in {@link #CATEGORIES}.
054: */
055: private static final int[] NO_DATA = { 0, 1, 255 };
056:
057: /**
058: * The minimal value for the geophysics category, inclusive.
059: */
060: private static final int minimum = 10;
061:
062: /**
063: * The maximal value for the geophysics category, exclusive.
064: */
065: private static final int maximum = 200;
066:
067: /**
068: * The scale factor for the sample dimension to test.
069: */
070: private static final double scale = 0.1;
071:
072: /**
073: * The offset value for the sample dimension to test.
074: */
075: private static final double offset = 5.0;
076:
077: /**
078: * Small number for comparaison.
079: */
080: private static final double EPS = 1E-7;
081:
082: /**
083: * The sample dimension to test.
084: */
085: private GridSampleDimension test;
086:
087: /**
088: * Random number generator for this test.
089: */
090: private static final Random random = new Random(
091: 5134392769888592001L);
092:
093: /**
094: * Run the suit from the command line.
095: */
096: public static void main(final String[] args) {
097: org.geotools.util.logging.Logging.GEOTOOLS
098: .forceMonolineConsoleOutput();
099: junit.textui.TestRunner.run(suite());
100: }
101:
102: /**
103: * Returns the test suite.
104: */
105: public static Test suite() {
106: return new TestSuite(SampleDimensionTest.class);
107: }
108:
109: /**
110: * Constructs a test case with the given name.
111: */
112: public SampleDimensionTest(final String name) {
113: super (name);
114: }
115:
116: /**
117: * Set up common objects used for all tests.
118: */
119: protected void setUp() throws Exception {
120: super .setUp();
121:
122: assertEquals("setUp", CATEGORIES.length, NO_DATA.length);
123: final Category[] categories = new Category[CATEGORIES.length + 1];
124: for (int i = 0; i < CATEGORIES.length; i++) {
125: categories[i] = new Category(CATEGORIES[i], null,
126: NO_DATA[i]);
127: }
128: categories[CATEGORIES.length] = new Category("SST", null,
129: minimum, maximum, scale, offset);
130: test = new GridSampleDimension("Temperature", categories, null);
131: }
132:
133: /**
134: * Test the consistency of the sample dimension.
135: */
136: public void testSampleDimension() {
137: final double[] nodataValues = test.getNoDataValues();
138: assertEquals("nodataValues.length", CATEGORIES.length,
139: nodataValues.length);
140: for (int i = 0; i < CATEGORIES.length; i++) {
141: assertEquals("nodataValues[" + i + ']', NO_DATA[i],
142: nodataValues[i], 0);
143: }
144: assertTrue("identity", !test.getSampleToGeophysics()
145: .isIdentity());
146: assertEquals("scale", scale, test.getScale(), 0);
147: assertEquals("offset", offset, test.getOffset(), 0);
148: assertEquals("minimum", 0, test.getMinimumValue(), 0);
149: assertEquals("maximum", 255, test.getMaximumValue(), 0);
150:
151: final GridSampleDimension invt = test.geophysics(true);
152: assertTrue(test != invt);
153: assertTrue("identity", invt.getSampleToGeophysics()
154: .isIdentity());
155: assertEquals("scale", 1, invt.getScale(), 0);
156: assertEquals("offset", 0, invt.getOffset(), 0);
157: assertEquals("minimum", minimum * scale + offset, invt
158: .getMinimumValue(), 0);
159: assertEquals("maximum", (maximum - 1) * scale + offset, invt
160: .getMaximumValue(), EPS);
161: }
162:
163: /**
164: * Test the creation of an {@link SampleTranscoder} using the image operation registry.
165: * This allow to apply the operation in the same way than other JAI operations, without
166: * any need for a direct access to package-private method.
167: */
168: public void testSampleTranscoderCreation() {
169: final OperationRegistry registry = JAI.getDefaultInstance()
170: .getOperationRegistry();
171: assertNotNull(registry.getDescriptor("rendered",
172: SampleTranscoder.OPERATION_NAME));
173:
174: final BufferedImage dummy = new BufferedImage(10, 10,
175: BufferedImage.TYPE_BYTE_GRAY);
176: final ParameterBlockJAI param = new ParameterBlockJAI(
177: SampleTranscoder.OPERATION_NAME);
178: final ParameterListDescriptor d = param
179: .getParameterListDescriptor();
180: assertTrue(d.getParamClasses()[0]
181: .equals(GridSampleDimension[].class));
182:
183: try {
184: JAI.create(SampleTranscoder.OPERATION_NAME, param);
185: fail();
186: } catch (IllegalArgumentException expected) {
187: // This is the expected exception: source required.
188: }
189:
190: param.addSource(dummy);
191: try {
192: JAI.create(SampleTranscoder.OPERATION_NAME, param);
193: fail();
194: } catch (IllegalArgumentException expected) {
195: // This is the expected exception: parameter required.
196: }
197:
198: param.setParameter("sampleDimensions",
199: new GridSampleDimension[] { test });
200: final RenderedOp op = JAI.create(
201: SampleTranscoder.OPERATION_NAME, param);
202: assertTrue(op.getRendering() instanceof SampleTranscoder);
203: }
204:
205: /**
206: * Test the {@link GridSampleDimension#rescale} method.
207: */
208: public void testRescale() {
209: GridSampleDimension scaled;
210: scaled = test.geophysics(false).rescale(2, -6);
211: assertEquals("Incorrect scale", 2, scaled.getScale(), EPS);
212: assertEquals("Incorrect offset", -6, scaled.getOffset(), EPS);
213: assertEquals("Incorrect minimum", 0, scaled.getMinimumValue(),
214: EPS);
215: assertEquals("Incorrect maximum", 255,
216: scaled.getMaximumValue(), EPS);
217:
218: scaled = test.geophysics(true).rescale(2, -6).geophysics(false);
219: assertEquals("Incorrect scale", scale * 2, scaled.getScale(),
220: EPS);
221: assertEquals("Incorrect offset", offset * 2 - 6, scaled
222: .getOffset(), EPS);
223: assertEquals("Incorrect minimum", 0, scaled.getMinimumValue(),
224: EPS);
225: assertEquals("Incorrect maximum", 255,
226: scaled.getMaximumValue(), EPS);
227: }
228: }
|