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.Dimension;
021: import java.awt.Graphics;
022: import java.awt.Graphics2D;
023: import java.awt.geom.AffineTransform;
024: import java.awt.image.ColorModel;
025: import java.awt.image.IndexColorModel;
026: import java.awt.image.RenderedImage;
027: import java.io.IOException;
028: import java.io.PrintWriter;
029: import java.util.Locale;
030: import javax.swing.JComponent;
031: import javax.swing.JFrame;
032: import javax.swing.JPanel;
033:
034: // JAI dependencies
035: import javax.media.jai.GraphicsJAI;
036: import javax.media.jai.PlanarImage;
037:
038: // OpenGIS dependencies
039: import org.opengis.parameter.ParameterValueGroup;
040:
041: // Geotools dependencies
042: import org.geotools.coverage.GridSampleDimension;
043: import org.geotools.coverage.processing.AbstractProcessor;
044: import org.geotools.resources.Arguments;
045: import org.geotools.resources.Utilities;
046:
047: /**
048: * A very simple viewer for {@link GridCoverage2D}. This viewer provides no zoom
049: * capability, no user interaction and ignores the coordinate system. It is just
050: * for quick test of grid coverage.
051: *
052: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/coverage/src/test/java/org/geotools/coverage/grid/Viewer.java $
053: * @version $Id: Viewer.java 25591 2007-05-20 10:43:59Z desruisseaux $
054: * @author Martin Desruisseaux
055: */
056: public class Viewer extends JPanel {
057: /**
058: * The image to display.
059: */
060: private final RenderedImage image;
061:
062: /**
063: * The main sample dimension, or {@code null} if none.
064: * Used by {@link #printPalette} for printing categories.
065: */
066: private GridSampleDimension categories;
067:
068: /**
069: * The transform from grid to coordinate system.
070: * Usually an identity transform for this simple viewer.
071: */
072: private final AffineTransform gridToCoordinateSystem = new AffineTransform();
073:
074: /**
075: * The location for the next frame window.
076: */
077: private static int location;
078:
079: /**
080: * Constructs a viewer for the specified image.
081: *
082: * @param coverage The image to display.
083: */
084: public Viewer(RenderedImage image) {
085: image = this .image = PlanarImage.wrapRenderedImage(image);
086: gridToCoordinateSystem.translate(-image.getMinX(), -image
087: .getMinY());
088: setPreferredSize(new Dimension(image.getWidth(), image
089: .getHeight()));
090: }
091:
092: /**
093: * Constructs a viewer for the specified grid coverage.
094: *
095: * @param coverage The coverage to display.
096: */
097: public Viewer(final GridCoverage2D coverage) {
098: this (coverage.getRenderedImage());
099: categories = (GridSampleDimension) coverage
100: .getSampleDimension(0);
101: }
102:
103: /**
104: * Paint this component.
105: */
106: public void paintComponent(final Graphics graphics) {
107: super .paintComponent(graphics);
108: final GraphicsJAI g = GraphicsJAI.createGraphicsJAI(
109: (Graphics2D) graphics, this );
110: g.drawRenderedImage(image, gridToCoordinateSystem);
111: }
112:
113: /**
114: * A convenience method showing an image. The application
115: * will be terminated when the user close the frame.
116: *
117: * @param coverage The coverage to display.
118: * @return The viewer, for information.
119: */
120: public static Viewer show(final RenderedImage image) {
121: return show(new Viewer(image), null);
122: }
123:
124: /**
125: * A convenience method showing a grid coverage. The application
126: * will be terminated when the user close the frame.
127: *
128: * @param coverage The coverage to display.
129: * @return The viewer, for information.
130: */
131: public static Viewer show(final GridCoverage2D coverage) {
132: return show(coverage, null);
133: }
134:
135: /**
136: * A convenience method showing a grid coverage. The application
137: * will be terminated when the user close the frame.
138: *
139: * @param coverage The coverage to display.
140: * @param title The window title.
141: * @return The viewer, for information.
142: */
143: public static Viewer show(final GridCoverage2D coverage,
144: final String title) {
145: final StringBuffer buffer = new StringBuffer();
146: if (title != null) {
147: buffer.append(title);
148: buffer.append(" - ");
149: }
150: buffer.append(coverage.getName().toString(
151: JComponent.getDefaultLocale()));
152: if (coverage != coverage.geophysics(true)) {
153: buffer.append(" (packed)");
154: } else if (coverage != coverage.geophysics(false)) {
155: buffer.append(" (geophysics)");
156: }
157: return show(new Viewer(coverage), buffer.toString());
158: }
159:
160: /**
161: * A convenience method showing a grid coverage. The application
162: * will be terminated when the user close the frame.
163: *
164: * @param viewer The viewer to display.
165: * @param title The frame title, or {@code null} if none.
166: * @return The viewer, for convenience.
167: */
168: private static Viewer show(final Viewer viewer, final String title) {
169: final JFrame frame = new JFrame(title);
170: frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
171: frame.setLocation(location, location);
172: frame.getContentPane().add(viewer);
173: frame.pack();
174: frame.setVisible(true);
175: location += 64;
176: return viewer;
177: }
178:
179: /**
180: * Prints the color palette to the specified output stream. First, the color model
181: * name is displayed. Next, if the color model is an {@link IndexColorModel}, then the
182: * RGB codes are written for all samples values. Category names or geophysics values,
183: * if any are written after each sample values.
184: *
185: * @param out The writer where to print the palette.
186: */
187: public void printPalette(final PrintWriter out) {
188: final Locale locale = getLocale();
189: final ColorModel model = image.getColorModel();
190: out.print(Utilities.getShortClassName(model));
191: out.println(':');
192: if (model instanceof IndexColorModel) {
193: out.println();
194: out
195: .println("Sample Colors Category or geophysics value");
196: out
197: .println("------ ---------------- ----------------------------");
198: final IndexColorModel palette = (IndexColorModel) model;
199: final int size = palette.getMapSize();
200: final byte[] R = new byte[size];
201: final byte[] G = new byte[size];
202: final byte[] B = new byte[size];
203: palette.getReds(R);
204: palette.getGreens(G);
205: palette.getBlues(B);
206: for (int i = 0; i < size; i++) {
207: format(out, i);
208: out.print(": RGB[");
209: format(out, R[i]);
210: out.print(',');
211: format(out, G[i]);
212: out.print(',');
213: format(out, R[i]);
214: out.print(']');
215: if (categories != null) {
216: final String label = categories.getLabel(i, locale);
217: if (label != null) {
218: out.print(" ");
219: out.print(label);
220: }
221: }
222: out.println();
223: }
224: } else {
225: out.println(model.getColorSpace());
226: }
227: }
228:
229: /**
230: * Format a unsigned byte to the specified output stream.
231: * The number will be right-justified in a cell of 3 spaces width.
232: *
233: * @param The writer where to print the number.
234: * @param value The number to format.
235: */
236: private static void format(final PrintWriter out, final byte value) {
237: format(out, ((int) value) & 0xFF);
238: }
239:
240: /**
241: * Format an integer to the specified output stream.
242: * The number will be right-justified in a cell of 3 spaces width.
243: *
244: * @param The writer where to print the number.
245: * @param value The number to format.
246: */
247: private static void format(final PrintWriter out, final int value) {
248: final String str = String.valueOf(value);
249: out.print(Utilities.spaces(3 - str.length()));
250: out.print(str);
251: }
252:
253: /**
254: * Load and display one of examples grid coverages.
255: */
256: public static void main(String[] args) throws IOException {
257: final Arguments arguments = new Arguments(args);
258: final PrintWriter out = arguments.out;
259: final Locale locale = arguments.locale;
260: final String operation = arguments
261: .getOptionalString("-operation");
262: final Boolean geophysics = arguments
263: .getOptionalBoolean("-geophysics");
264: final boolean palette = arguments.getFlag("-palette");
265: args = arguments.getRemainingArguments(1);
266: if (args.length == 0) {
267: out.println("Usage: Viewer [options] example");
268: out.println();
269: out
270: .print("Where \"example\" is the number of the requested example (0 to ");
271: out.print(GridCoverageExamples.getNumExamples() - 1);
272: out.println(" inclusive)");
273: out.println("and [options] includes:");
274: out.println();
275: out
276: .println(" -operation=[s] An operation name to apply (e.g. \"GradientMagniture\").");
277: out
278: .println(" For a list of available operations, run the following:");
279: out
280: .println(" java org.geotools.coverage.processing.DefaultProcessor");
281: out
282: .println(" -geophysics=[b] Set to 'true' or 'false' for requesting a \"geophysics\"");
283: out
284: .println(" version of data or an indexed version, respectively.");
285: out
286: .println(" -palette Dumps RGB codes to standard output.");
287: out.flush();
288: return;
289: }
290: GridCoverage2D coverage = GridCoverageExamples
291: .getExample(Integer.parseInt(args[0]));
292: if (geophysics != null) {
293: coverage = coverage.geophysics(geophysics.booleanValue());
294: }
295: if (operation != null) {
296: final AbstractProcessor processor = AbstractProcessor
297: .getInstance();
298: final ParameterValueGroup param = processor.getOperation(
299: operation).getParameters();
300: param.parameter("Source").setValue(coverage);
301: coverage = (GridCoverage2D) processor.doOperation(param);
302: }
303: Viewer viewer = new Viewer(coverage);
304: viewer.setLocale(locale);
305: viewer = show(viewer, coverage.getName().toString(locale));
306: if (palette) {
307: viewer.printPalette(out);
308: }
309: out.flush();
310: }
311: }
|