001    //$HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/branches/2.2_testing/src/org/deegree/graphics/displayelements/RasterDisplayElement.java $
002    /*----------------    FILE HEADER  ------------------------------------------
003    
004     This file is part of deegree.
005     Copyright (C) 2001-2008 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: 12692 $
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: 12692 $, $Date: 2008-07-07 11:33:02 +0200 (Mo, 07 Jul 2008) $
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            synchronized ( symbolizer ) {
143                try {
144                    if ( doesScaleConstraintApply( scale ) ) {
145                        PT_Envelope env = gc.getEnvelope();
146                        int minx = (int) ( projection.getDestX( env.minCP.ord[0] ) );
147                        int maxy = (int) ( projection.getDestY( env.minCP.ord[1] ) );
148                        int maxx = (int) ( projection.getDestX( env.maxCP.ord[0] ) );
149                        int miny = (int) ( projection.getDestY( env.maxCP.ord[1] ) );
150    
151                        if ( gc instanceof ImageGridCoverage ) {
152                            ImageGridCoverage igc = (ImageGridCoverage) gc;
153                            Graphics2D g2 = (Graphics2D) g;
154                            BufferedImage image = igc.getAsImage( -1, -1 );
155    
156                            if ( symbolizer.isDefault() ) {
157                                g2.drawImage( image, minx, miny, maxx - minx, maxy - miny, null );
158                            } else {
159                                if ( symbolizer.scaleValid( scale ) ) {
160                                    paintCoverage( g2, new Image2RawData( image ).parse(), minx, miny, maxx, maxy );
161                                }
162                            }
163                        } else {
164                            // TODO
165                            // handle other grid coverages
166                        }
167                    }
168                } catch ( Exception e ) {
169                    LOG.logError( e.getMessage(), e );
170                    throw new RuntimeException( e.getMessage(), e );
171                }
172            }
173        }
174    
175        /**
176         * returns the content of the <code>RasterDisplayElement</code>
177         * 
178         * @return gird coverage
179         */
180        public GridCoverage getRaster() {
181            return gc;
182        }
183    
184        /**
185         * sets the grid coverage that represents the content of the <code>RasterDisplayElement</code>
186         * 
187         * @param gc
188         * 
189         */
190        public void setRaster( GridCoverage gc ) {
191            this.gc = gc;
192        }
193    
194        @Override
195        public boolean doesScaleConstraintApply( double scale ) {
196            return true;
197        }
198    
199    }