001    //$HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/tags/2.1/src/org/deegree/graphics/displayelements/RasterDisplayElement.java $
002    /*----------------    FILE HEADER  ------------------------------------------
003    
004     This file is part of deegree.
005     Copyright (C) 2001-2007 by:
006     EXSE, Department of Geography, University of Bonn
007     http://www.giub.uni-bonn.de/deegree/
008     lat/lon GmbH
009     http://www.lat-lon.de
010    
011     This library is free software; you can redistribute it and/or
012     modify it under the terms of the GNU Lesser General Public
013     License as published by the Free Software Foundation; either
014     version 2.1 of the License, or (at your option) any later version.
015    
016     This library is distributed in the hope that it will be useful,
017     but WITHOUT ANY WARRANTY; without even the implied warranty of
018     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
019     Lesser General Public License for more details.
020    
021     You should have received a copy of the GNU Lesser General Public
022     License along with this library; if not, write to the Free Software
023     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
024    
025     Contact:
026    
027     Andreas Poth
028     lat/lon GmbH
029     Aennchenstr. 19
030     53115 Bonn
031     Germany
032     E-Mail: poth@lat-lon.de
033    
034     Prof. Dr. Klaus Greve
035     Department of Geography
036     University of Bonn
037     Meckenheimer Allee 166
038     53115 Bonn
039     Germany
040     E-Mail: greve@giub.uni-bonn.de
041    
042     
043     ---------------------------------------------------------------------------*/
044    package org.deegree.graphics.displayelements;
045    
046    import java.awt.Graphics;
047    import java.awt.Graphics2D;
048    import java.awt.image.BufferedImage;
049    import java.io.Serializable;
050    
051    import org.deegree.framework.log.ILogger;
052    import org.deegree.framework.log.LoggerFactory;
053    import org.deegree.graphics.sld.Categorize;
054    import org.deegree.graphics.sld.Interpolate;
055    import org.deegree.graphics.sld.RasterSymbolizer;
056    import org.deegree.graphics.transformation.GeoTransform;
057    import org.deegree.model.coverage.grid.GridCoverage;
058    import org.deegree.model.coverage.grid.ImageGridCoverage;
059    import org.deegree.processing.raster.converter.Image2RawData;
060    import org.opengis.pt.PT_Envelope;
061    
062    /**
063     * 
064     * 
065     * 
066     * @version $Revision: 7824 $
067     * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
068     * @author last edited by: $Author: aschmitz $
069     * 
070     * @version 1.0. $Revision: 7824 $, $Date: 2007-07-24 11:02:18 +0200 (Di, 24 Jul 2007) $
071     * 
072     * @since 2.0
073     */
074    public class RasterDisplayElement extends AbstractDisplayElement implements Serializable {
075    
076        private static final long serialVersionUID = -5195730721807600940L;
077    
078        private static ILogger LOG = LoggerFactory.getLogger( RasterDisplayElement.class );
079    
080        private RasterSymbolizer symbolizer = null;
081    
082        private GridCoverage gc = null;
083    
084        /**
085         * Creates a new RasterDisplayElement_Impl object.
086         * 
087         * @param gc
088         *            raster
089         */
090        RasterDisplayElement( GridCoverage gc ) {
091            setRaster( gc );
092            symbolizer = new RasterSymbolizer();
093        }
094    
095        /**
096         * Creates a new RasterDisplayElement_Impl object.
097         * 
098         * @param gc
099         *            raster
100         * @param symbolizer
101         */
102        RasterDisplayElement( GridCoverage gc, RasterSymbolizer symbolizer ) {
103            setRaster( gc );
104            this.symbolizer = symbolizer;
105        }
106    
107        private void paintCoverage( Graphics2D g, float[][] data, int minx, int miny, int maxx, int maxy ) {
108            Categorize categorize = symbolizer.getCategorize();
109            Interpolate interpolate = symbolizer.getInterpolate();
110    
111            int opac = (int) ( 255 * symbolizer.getOpacity() ) << 24;
112    
113            int width = maxx - minx;
114            int height = maxy - miny;
115    
116            BufferedImage img = new BufferedImage( data[0].length, data.length, BufferedImage.TYPE_INT_ARGB );
117    
118            for ( int x = 0; x < data[0].length; ++x ) {
119                for ( int y = 0; y < data.length; ++y ) {
120                    float d = data[y][x];
121                    int val = 0;
122    
123                    if ( categorize != null ) {
124                        val = categorize.categorize( d, opac );
125                    }
126                    if ( interpolate != null ) {
127                        val = interpolate.interpolate( d, opac );
128                    }
129    
130                    img.setRGB( x, y, val );
131                }
132            }
133    
134            g.drawImage( img, minx, miny, width, height, null );
135        }
136    
137        /**
138         * renders the DisplayElement to the submitted graphic context
139         * 
140         */
141        public void paint( Graphics g, GeoTransform projection, double scale ) {
142            try {
143                if ( doesScaleConstraintApply( scale ) ) {
144                    PT_Envelope env = gc.getEnvelope();
145                    int minx = (int) ( projection.getDestX( env.minCP.ord[0] ) - 0.5 );
146                    int maxy = (int) ( projection.getDestY( env.minCP.ord[1] ) - 0.5 );
147                    int maxx = (int) ( projection.getDestX( env.maxCP.ord[0] ) + 0.5 );
148                    int miny = (int) ( projection.getDestY( env.maxCP.ord[1] ) + 0.5 );
149    
150                    if ( gc instanceof ImageGridCoverage ) {
151                        ImageGridCoverage igc = (ImageGridCoverage) gc;
152                        Graphics2D g2 = (Graphics2D) g;
153                        BufferedImage image = igc.getAsImage( -1, -1 );
154    
155                        if ( symbolizer.isDefault() ) {
156                            g2.drawImage( image, minx, miny, maxx - minx, maxy - miny, null );
157                        } else {
158                            if ( symbolizer.scaleValid( scale ) ) {
159                                paintCoverage( g2, new Image2RawData( image ).parse(), minx, miny, maxx, maxy );
160                            }
161                        }
162                    } else {
163                        // TODO
164                        // handle other grid coverages
165                    }
166                }
167            } catch ( Exception e ) {
168                LOG.logError( e.getMessage(), e );
169                throw new RuntimeException( e.getMessage(), e );
170            }
171    
172        }
173    
174        /**
175         * returns the content of the <code>RasterDisplayElement</code>
176         * 
177         * @return gird coverage
178         */
179        public GridCoverage getRaster() {
180            return gc;
181        }
182    
183        /**
184         * sets the grid coverage that represents the content of the <code>RasterDisplayElement</code>
185         * 
186         * @param gc
187         * 
188         */
189        public void setRaster( GridCoverage gc ) {
190            this.gc = gc;
191        }
192    
193        @Override
194        public boolean doesScaleConstraintApply( double scale ) {
195            return true;
196        }
197    
198    }