    001    //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/branches/2.4_testing/src/org/deegree/model/coverage/grid/ImageGridCoverage.java $
    002    /*----------------------------------------------------------------------------
    003     This file is part of deegree, http://deegree.org/
    004     Copyright (C) 2001-2009 by:
    005       Department of Geography, University of Bonn
    006     and
    007       lat/lon GmbH
    008
    009     This library is free software; you can redistribute it and/or modify it under
    010     the terms of the GNU Lesser General Public License as published by the Free
    011     Software Foundation; either version 2.1 of the License, or (at your option)
    012     any later version.
    013     This library is distributed in the hope that it will be useful, but WITHOUT
    014     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    015     FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
    016     details.
    017     You should have received a copy of the GNU Lesser General Public License
    018     along with this library; if not, write to the Free Software Foundation, Inc.,
    019     59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
    020
    021     Contact information:
    022
    023     lat/lon GmbH
    024     Aennchenstr. 19, 53177 Bonn
    025     Germany
    026     http://lat-lon.de/
    027
    028     Department of Geography, University of Bonn
    029     Prof. Dr. Klaus Greve
    030     Postfach 1147, 53001 Bonn
    031     Germany
    032     http://www.geographie.uni-bonn.de/deegree/
    033
    034     e-mail: info@deegree.org
    035    ----------------------------------------------------------------------------*/
    036    package org.deegree.model.coverage.grid;
    037
    038    import java.awt.Rectangle;
    039    import java.awt.image.BufferedImage;
    040    import java.awt.image.renderable.ParameterBlock;
    041    import java.awt.image.renderable.RenderableImage;
    042
    043    import javax.media.jai.Interpolation;
    044    import javax.media.jai.InterpolationNearest;
    045    import javax.media.jai.JAI;
    046    import javax.media.jai.RenderableGraphics;
    047    import javax.media.jai.RenderedOp;
    048
    049    import org.deegree.model.crs.CoordinateSystem;
    050    import org.deegree.model.spatialschema.Envelope;
    051    import org.deegree.ogcwebservices.wcs.describecoverage.CoverageOffering;
    052
    053    /**
    054     * GridCoverage implementation for holding grids stored in an <tt>BufferedImage</tt> or in a set of
    055     * <tt>ImageGridCoverage</tt>s
    056     *
    057     * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
    058     * @author last edited by: $Author: mschneider $
    059     *
    060     * @version $Revision: 18195 $, $Date: 2009-06-18 17:55:39 +0200 (Do, 18. Jun 2009) $
    061     */
    062    public class ImageGridCoverage extends AbstractGridCoverage {
    063
    064        private static final long serialVersionUID = -531939507044569726L;
    065
    066        private transient BufferedImage image = null;
    067
    068        /**
    069         *
    070         * @param coverageOffering
    071         * @param envelope
    072         * @param image
    073         */
    074        public ImageGridCoverage( CoverageOffering coverageOffering, Envelope envelope, BufferedImage image ) {
    075            this( coverageOffering, envelope, false, image );
    076        }
    077
    078        /**
    079         *
    080         * @param coverageOffering
    081         * @param envelope
    082         * @param isEditable
    083         * @param image
    084         */
    085        public ImageGridCoverage( CoverageOffering coverageOffering, Envelope envelope, boolean isEditable,
    086                                  BufferedImage image ) {
    087            super( coverageOffering, envelope, isEditable );
    088            this.image = image;
    089        }
    090
    091        /**
    092         *
    093         * @param coverageOffering
    094         * @param envelope
    095         * @param crs
    096         * @param isEditable
    097         * @param image
    098         */
    099        public ImageGridCoverage( CoverageOffering coverageOffering, Envelope envelope, CoordinateSystem crs,
    100                                  boolean isEditable, BufferedImage image ) {
    101            super( coverageOffering, envelope, crs, isEditable );
    102            this.image = image;
    103        }
    104
    105        /**
    106         *
    107         * @param coverageOffering
    108         * @param envelope
    109         * @param sources
    110         */
    111        public ImageGridCoverage( CoverageOffering coverageOffering, Envelope envelope, ImageGridCoverage[] sources ) {
    112            super( coverageOffering, envelope, sources );
    113        }
    114
    115        /**
    116         * The number of sample dimensions in the coverage. For grid coverages, a sample dimension is a band.
    117         *
    118         * @return The number of sample dimensions in the coverage.
    119         */
    120        public int getNumSampleDimensions() {
    121            if ( image != null ) {
    122                return image.getData().getNumBands();
    123            }
    124            return sources[0].getNumSampleDimensions();
    125        }
    126
    127        /**
    128         * Returns 2D view of this coverage as a renderable image. This optional operation allows interoperability with <A
    129         * HREF="http://java.sun.com/products/java-media/2D/">Java2D</A>. If this coverage is a
    130         * {@link "org.opengis.coverage.grid.GridCoverage"} backed by a {@link java.awt.image.RenderedImage}, the underlying
    131         * image can be obtained with:
    132         *
    133         * <code>getRenderableImage(0,1).{@linkplain RenderableImage#createDefaultRendering()
    134         * createDefaultRendering()}</code>
    135         *
    136         * @param xAxis
    137         *            Dimension to use for the <var>x</var> axis.
    138         * @param yAxis
    139         *            Dimension to use for the <var>y</var> axis.
    140         * @return A 2D view of this coverage as a renderable image.
    141         * @throws UnsupportedOperationException
    142         *             if this optional operation is not supported.
    143         * @throws IndexOutOfBoundsException
    144         *             if <code>xAxis</code> or <code>yAxis</code> is out of bounds.
    145         */
    146        @Override
    147        public RenderableImage getRenderableImage( int xAxis, int yAxis )
    148                                throws UnsupportedOperationException, IndexOutOfBoundsException {
    149            if ( image != null ) {
    150                if ( xAxis > 0 && yAxis > 0 ) {
    151                    Rectangle rect = new Rectangle( xAxis, yAxis );
    152                    RenderableGraphics rg = new RenderableGraphics( rect );
    153                    rg.drawImage( image, 0, 0, xAxis, yAxis, null );
    154                    return rg;
    155                }
    156                Rectangle rect = new Rectangle( image.getWidth(), image.getHeight() );
    157                RenderableGraphics rg = new RenderableGraphics( rect );
    158                rg.drawImage( image, 0, 0, null );
    159                return rg;
    160            }
    161            // TODO if multi images -> sources.length > 0
    162            return null;
    163        }
    164
    165        /**
    166         * this is a deegree convenience method which returns the source image of an <tt>ImageGridCoverage</tt>. In procipal
    167         * the same can be done with the getRenderableImage(int xAxis, int yAxis) method. but creating a
    168         * <tt>RenderableImage</tt> image is very slow. I xAxis or yAxis <= 0 then the size of the returned image will be
    169         * calculated from the source images of the coverage.
    170         *
    171         * @param xAxis
    172         *            Dimension to use for the <var>x</var> axis.
    173         * @param yAxis
    174         *            Dimension to use for the <var>y</var> axis.
    175         * @return the source image of an <tt>ImageGridCoverage</tt>.
    176         */
    177        @Override
    178        public BufferedImage getAsImage( int xAxis, int yAxis ) {
    179
    180            if ( xAxis <= 0 || yAxis <= 0 ) {
    181                // get default size if passed target size is <= 0
    182                Rectangle rect = calculateOriginalSize();
    183                xAxis = rect.width;
    184                yAxis = rect.height;
    185            }
    186
    187            BufferedImage bi = null;
    188            if ( image != null ) {
    189                if ( xAxis == image.getWidth() && yAxis == image.getHeight() ) {
    190                    bi = image;
    191                } else {
    192                    // it's a simple ImageGridCoverage just made up of one image
    193                    ParameterBlock pb = new ParameterBlock();
    194                    pb.addSource( image );
    195                    pb.add( xAxis / (float) image.getWidth() ); // The xScale
    196                    pb.add( yAxis / (float) image.getHeight() ); // The yScale
    197                    pb.add( 0.0F ); // The x translation
    198                    pb.add( 0.0F ); // The y translation
    199                    Interpolation interpolation = new InterpolationNearest();
    200                    pb.add( interpolation ); // The interpolation
    201                    // Create the scale operation
    202                    RenderedOp ro = JAI.create( "scale", pb, null );
    203                    bi = ro.getAsBufferedImage();
    204                }
    205            } else {
    206                String natFrm = coverageOffering.getSupportedFormats().getNativeFormat().getCode();
    207                if ( "jpg".equalsIgnoreCase( natFrm ) || "jpeg".equalsIgnoreCase( natFrm )
    208                     || "bmp".equalsIgnoreCase( natFrm ) ) {
    209                    bi = new BufferedImage( xAxis, yAxis, BufferedImage.TYPE_INT_RGB );
    210                } else {
    211                    bi = new BufferedImage( xAxis, yAxis, BufferedImage.TYPE_INT_ARGB );
    212                }
    213                // it's a complex ImageGridCoverage made up of different
    214                // source coverages
    215                if ( sources == null || sources.length == 0 ) {
    216                    return bi;
    217                }
    218
    219                for ( int i = 0; i < sources.length; i++ ) {
    220                    BufferedImage sourceImg = ( (ImageGridCoverage) sources[i] ).getAsImage( -1, -1 );
    221                    bi = paintImage( bi, getEnvelope(), sourceImg, sources[i].getEnvelope() );
    222                }
    223            }
    224
    225            return bi;
    226        }
    227
    228        /**
    229         * calculates the original size of a gridcoverage based on its resolution and the envelope(s) of its source(s).
    230         *
    231         */
    232        private Rectangle calculateOriginalSize() {
    233            if ( image != null ) {
    234                return new Rectangle( image.getWidth(), image.getHeight() );
    235            }
    236            BufferedImage bi = ( (ImageGridCoverage) sources[0] ).getAsImage( -1, -1 );
    237            Envelope env = sources[0].getEnvelope();
    238            double dx = env.getWidth() / bi.getWidth();
    239            double dy = env.getHeight() / bi.getHeight();
    240            env = this.getEnvelope();
    241            int w = (int) Math.round( env.getWidth() / dx );
    242            int h = (int) Math.round( env.getHeight() / dy );
    243            return new Rectangle( w, h );
    244        }
    245
    246    }