001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2005-2007, Geotools Project Managment Committee (PMC)
005: * (C) 2007, GeoSolutions S.A.S.
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.processing.operation;
018:
019: import java.awt.Shape;
020: import java.awt.image.RenderedImage;
021: import java.util.Collections;
022: import java.util.HashMap;
023: import java.util.Map;
024:
025: import javax.media.jai.JAI;
026: import javax.media.jai.RenderedOp;
027: import javax.media.jai.operator.HistogramDescriptor;
028:
029: import org.geotools.coverage.grid.GridCoverage2D;
030: import org.geotools.coverage.processing.AbstractStatisticsOperationJAI;
031: import org.opengis.coverage.processing.OperationNotFoundException;
032: import org.opengis.parameter.ParameterValueGroup;
033: import org.opengis.referencing.crs.CoordinateReferenceSystem;
034: import org.opengis.referencing.operation.MathTransform;
035: import org.opengis.util.InternationalString;
036:
037: /**
038: * * This operation simply wraps JAI Extrema operations described by
039: * {@link HistogramDescriptor} inside a GeoTools operation in order to make it
040: * spatial-aware.
041: *
042: * <p>
043: * For the moment this is a very simple wrap. Plans on the 2.4 and successive
044: * versions of this operation are to add the ability to to use spatial ROIs and
045: * to specific Spatial subsampling. As of now, ROI has to be a Java2D
046: * {@link Shape} subclass and the parameters to control x and y subsamplings got
047: * to be Integer, which means pixel-aware.
048: *
049: * <p>
050: * For more information on how the underlying {@link JAI} operators works you
051: * can have a look here: <a
052: * href="http://download.java.net/media/jai/javadoc/1.1.3/jai-apidocs/javax/media/jai/operator/HistogramDescriptor.html">HistogramDescriptor</a>
053: * <a
054: * href="http://download.java.net/media/jai/javadoc/1.1.3/jai-apidocs/javax/media/jai/Histogram.html>Histogram</a>
055: *
056: * <p>
057: * <strong>How to use this operation</strong> Here is a very simple example on
058: * how to use this operation in order to get the
059: * {@link javax.media.jai.Histogram} of the source coverage.
060: *
061: * <code>
062: * final OperationJAI op=new OperationJAI("Histogram");
063: * ParameterValueGroup params = op.getParameters();
064: * params.parameter("Source").setValue(coverage);
065: * coverage=(GridCoverage2D) op.doOperation(params,null);
066: * System.out.println(((double[])coverage.getProperty("histogram")));
067: * </code>
068: *
069: * @author Simone Giannecchini
070: * @since 2.4
071: * @see javax.media.jai.Histogram
072: *
073: */
074: public class Histogram extends AbstractStatisticsOperationJAI {
075:
076: /**
077: *
078: */
079: private static final long serialVersionUID = -4256576399698278701L;
080:
081: /**
082: * {@link String} key for getting the {@link javax.media.jai.Histogram}
083: * object.
084: */
085: public final static String GT_SYNTHETIC_PROPERTY_HISTOGRAM = "histogram";
086:
087: /**
088: * Default constructor for the {@link Histogram} operation.
089: *
090: * @throws OperationNotFoundException
091: */
092: public Histogram() throws OperationNotFoundException {
093: super (getOperationDescriptor("Histogram"));
094: }
095:
096: /**
097: * This operation MUST be performed on the geophysics data for this
098: * {@link GridCoverage2D}.
099: *
100: * @param parameters
101: * {@link ParameterValueGroup} that describes this operation
102: * @return always true.
103: */
104: protected boolean computeOnGeophysicsValues(
105: ParameterValueGroup parameters) {
106: return true;
107: }
108:
109: /**
110: * Prepare the {@link javax.media.jai.Histogram} property for this extream
111: * operation.
112: *
113: * <p>
114: * See <a
115: * href="http://download.java.net/media/jai/javadoc/1.1.3/jai-apidocs/javax/media/jai/operator/ExtremaDescriptor.html">ExtremaDescriptor</a>
116: * for more info.
117: *
118: * @see OperationJAI#getProperties(RenderedImage, CoordinateReferenceSystem,
119: * InternationalString, MathTransform, GridCoverage2D[],
120: * org.geotools.coverage.processing.OperationJAI.Parameters),
121: */
122: protected Map getProperties(RenderedImage data,
123: CoordinateReferenceSystem crs, InternationalString name,
124: MathTransform toCRS, GridCoverage2D[] sources,
125: Parameters parameters) {
126: // /////////////////////////////////////////////////////////////////////
127: //
128: // If and only if data is a RenderedOp we prepare the properties for
129: // minimum and maximum as the output of the extrema operation.
130: //
131: // /////////////////////////////////////////////////////////////////////
132: if (data instanceof RenderedOp) {
133: // XXX remove me with 1.5
134: final RenderedOp result = (RenderedOp) data;
135:
136: // get the properties
137: final javax.media.jai.Histogram hist = (javax.media.jai.Histogram) result
138: .getProperty(GT_SYNTHETIC_PROPERTY_HISTOGRAM);
139:
140: // return the map
141: final Map synthProp = new HashMap(2);
142: synthProp.put(GT_SYNTHETIC_PROPERTY_HISTOGRAM, hist);
143: return Collections.unmodifiableMap(synthProp);
144:
145: }
146: return super.getProperties(data, crs, name, toCRS, sources,
147: parameters);
148: }
149:
150: }
|