001    //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/branches/2.3_testing/src/org/deegree/model/coverage/grid/TiffDEMParser.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.image.BufferedImage;
039    import java.awt.image.DataBuffer;
040    import java.io.File;
041    
042    import org.apache.batik.ext.awt.image.codec.FileCacheSeekableStream;
043    import org.apache.batik.ext.awt.image.codec.tiff.TIFFDecodeParam;
044    import org.apache.batik.ext.awt.image.codec.tiff.TIFFImage;
045    
046    /**
047     * Parses 4 channel (32Bit) tiff images as DEM and returns a float matrix containing the DEM heights
048     *
049     *
050     * @version $Revision: 18195 $
051     * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
052     * @author last edited by: $Author: mschneider $
053     *
054     * @version 1.0. $Revision: 18195 $, $Date: 2009-06-18 17:55:39 +0200 (Do, 18. Jun 2009) $
055     *
056     * @since 2.0
057     */
058    public class TiffDEMParser {
059    
060        private DataBuffer db;
061    
062        private float scale = 0;
063    
064        private float offset = 0;
065    
066        private int width = 0;
067    
068        private int height = 0;
069    
070        /**
071         *
072         * @param dataFile
073         *            image containing DEM data instead color information
074         */
075        public TiffDEMParser( File dataFile ) {
076            this( dataFile, 1, 0 );
077        }
078    
079        /**
080         *
081         * @param dataFile
082         *            image containing DEM data instead color information
083         * @param scale
084         *            scale factor; newHeight[i][j] = height[i][j] * scale
085         */
086        public TiffDEMParser( File dataFile, float scale ) {
087            this( dataFile, scale, 0 );
088        }
089    
090        /**
091         *
092         * @param dataFile
093         *            image containing DEM data instead color information
094         * @param scale
095         *            scale factor; newHeight[i][j] = height[i][j] * scale
096         * @param offset
097         *            height offset; newHeight[i][j] = height[i][j] + offset
098         */
099        public TiffDEMParser( File dataFile, float scale, float offset ) {
100            try {
101                FileCacheSeekableStream fss = new FileCacheSeekableStream( dataFile.toURL().openStream() );
102                TIFFImage tiff = new TIFFImage( fss, new TIFFDecodeParam(), 0 );
103                db = tiff.getData().getDataBuffer();
104                width = tiff.getWidth();
105                height = tiff.getHeight();
106            } catch ( Exception e ) {
107                e.printStackTrace();
108            }
109            this.scale = scale;
110            this.offset = offset;
111        }
112    
113        /**
114         *
115         * @param data
116         *            image containing DEM data instead color information
117         */
118        public TiffDEMParser( BufferedImage data ) {
119            this( data, 1, 0 );
120        }
121    
122        /**
123         *
124         * @param data
125         *            image containing DEM data instead color information
126         * @param scale
127         *            scale factor; newHeight[i][j] = height[i][j] * scale
128         */
129        public TiffDEMParser( BufferedImage data, float scale ) {
130            this( data, scale, 0 );
131        }
132    
133        /**
134         *
135         * @param data
136         * @param scale
137         *            scale factor; newHeight[i][j] = height[i][j] * scale
138         * @param offset
139         *            height offset; newHeight[i][j] = height[i][j] + offset
140         */
141        public TiffDEMParser( BufferedImage data, float scale, float offset ) {
142            this.db = data.getRaster().getDataBuffer();
143            this.scale = scale;
144            this.offset = offset;
145            width = data.getWidth();
146            height = data.getHeight();
147        }
148    
149        /**
150         * returns the DEM heights as float matrix
151         *
152         * @return the DEM heights as float matrix
153         */
154        public float[][] parse() {
155    
156            float[][] terrain = new float[height][width];
157    
158            for ( int j = 0; j < height; j++ ) {
159                for ( int i = 0; i < width; i++ ) {
160                    terrain[j][i] = db.getElemFloat( width * j + i ) * scale + offset;
161                }
162            }
163    
164            return terrain;
165    
166        }
167    
168    }