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 and JAI dependencies
020: import java.awt.Transparency;
021: import java.awt.image.BufferedImage;
022: import java.awt.image.ColorModel;
023: import java.awt.image.ComponentColorModel;
024: import java.awt.image.DataBuffer;
025: import java.awt.image.RenderedImage;
026: import java.awt.image.WritableRaster;
027: import java.util.Random;
028:
029: // JUnit dependencies
030: import junit.framework.Test;
031: import junit.framework.TestCase;
032: import junit.framework.TestSuite;
033:
034: // Geotools dependencies
035: import org.geotools.coverage.grid.Viewer;
036: import org.geotools.resources.XMath;
037:
038: /**
039: * Tests the {@link ScaledColorSpace} implementation.
040: * This is a visual test when run on the command line.
041: *
042: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/coverage/src/test/java/org/geotools/coverage/ScaledColorSpaceTest.java $
043: * @version $Id: ScaledColorSpaceTest.java 20970 2006-08-11 07:53:22Z jgarnett $
044: * @author Martin Desruisseaux
045: */
046: public class ScaledColorSpaceTest extends TestCase {
047: /**
048: * Random number generator for this test.
049: */
050: private static final Random random = new Random(5078987324568283L);
051:
052: /**
053: * The minimal and maximal values to renderer.
054: */
055: private double minimum, maximum;
056:
057: /**
058: * The scaled color space to test.
059: */
060: private ScaledColorSpace colors;
061:
062: /**
063: * The image to use for test.
064: */
065: private RenderedImage image;
066:
067: /**
068: * Returns the test suite.
069: */
070: public static Test suite() {
071: return new TestSuite(ScaledColorSpaceTest.class);
072: }
073:
074: /**
075: * Constructs a test case with the given name.
076: */
077: public ScaledColorSpaceTest(final String name) {
078: super (name);
079: }
080:
081: /**
082: * Set up common objects used for all tests.
083: */
084: protected void setUp() throws Exception {
085: super .setUp();
086: minimum = random.nextDouble() * 100;
087: maximum = random.nextDouble() * 200 + minimum + 10;
088: colors = new ScaledColorSpace(0, 1, minimum, maximum);
089:
090: final int transparency = Transparency.OPAQUE;
091: final int datatype = DataBuffer.TYPE_FLOAT;
092: final ColorModel model = new ComponentColorModel(colors, false,
093: false, transparency, datatype);
094: final WritableRaster data = model
095: .createCompatibleWritableRaster(200, 200);
096: final BufferedImage image = new BufferedImage(model, data,
097: false, null);
098: for (int x = data.getWidth(); --x >= 0;) {
099: for (int y = data.getHeight(); --y >= 0;) {
100: double v = XMath.hypot((double) x / data.getWidth()
101: - 0.5, (double) y / data.getWidth() - 0.5);
102: v = v * (maximum - minimum) + minimum;
103: data.setSample(x, y, 0, v);
104: }
105: }
106: this .image = image;
107: }
108:
109: /**
110: * Test the color space.
111: */
112: public void testColorSpace() {
113: assertEquals(minimum, colors.getMinValue(0), 1E-4);
114: assertEquals(maximum, colors.getMaxValue(0), 1E-4);
115:
116: final float[] array = new float[1];
117: final double step = (maximum - minimum) / 256;
118: for (double x = minimum; x < maximum; x += step) {
119: array[0] = (float) x;
120: assertEquals(x, colors.fromRGB(colors.toRGB(array))[0],
121: 1E-3);
122: }
123: }
124:
125: /**
126: * Run the visual test.
127: */
128: public static void main(final String[] args) throws Exception {
129: final ScaledColorSpaceTest test = new ScaledColorSpaceTest(null);
130: test.setUp();
131: test.testColorSpace();
132: Viewer.show(test.image);
133: }
134: }
|