001 //$HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/branches/2.2_testing/src/org/deegree/processing/raster/converter/Image2RawData.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 53177 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 package org.deegree.processing.raster.converter;
044
045 import java.awt.image.BufferedImage;
046 import java.awt.image.DataBuffer;
047 import java.awt.image.WritableRaster;
048 import java.io.File;
049 import java.util.ArrayList;
050 import java.util.List;
051
052 import org.apache.batik.ext.awt.image.codec.FileCacheSeekableStream;
053 import org.apache.batik.ext.awt.image.codec.tiff.TIFFDecodeParam;
054 import org.apache.batik.ext.awt.image.codec.tiff.TIFFImage;
055 import org.deegree.model.crs.CoordinateSystem;
056 import org.deegree.model.spatialschema.GeometryFactory;
057 import org.deegree.model.spatialschema.Point;
058
059 /**
060 * Parses 4 channel (32Bit) tiff images as DEM and returns a float matrix containing the DEM heights
061 *
062 * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
063 * @author last edited by: $Author: apoth $
064 *
065 * @version $Revision: 9346 $, $Date: 2007-12-27 17:39:07 +0100 (Do, 27 Dez 2007) $
066 */
067 public class Image2RawData {
068
069 private BufferedImage image;
070
071 private DataBuffer db;
072
073 private float scale = 0;
074
075 private float offset = 0;
076
077 private int width = 0;
078
079 private int height = 0;
080
081 /**
082 *
083 * @param dataFile
084 * image containing raw data instead color information. Data file must be a TIFF
085 * image
086 */
087 public Image2RawData( File dataFile ) {
088 this( dataFile, 1, 0 );
089 }
090
091 /**
092 *
093 * @param dataFile
094 * image containing raw data instead color information. Data file must be a TIFF
095 * image
096 * @param scale
097 * scale factor; newHeight[i][j] = height[i][j] * scale
098 */
099 public Image2RawData( File dataFile, float scale ) {
100 this( dataFile, scale, 0 );
101 }
102
103 /**
104 *
105 * @param dataFile
106 * image containing raw data instead color information. Data file must be a TIFF
107 * image
108 * @param scale
109 * scale factor; newHeight[i][j] = height[i][j] * scale
110 * @param offset
111 * height offset; newHeight[i][j] = height[i][j] + offset
112 */
113 public Image2RawData( File dataFile, float scale, float offset ) {
114 try {
115 FileCacheSeekableStream fss = new FileCacheSeekableStream( dataFile.toURL().openStream() );
116 TIFFImage tiff = new TIFFImage( fss, new TIFFDecodeParam(), 0 );
117 image = new BufferedImage( tiff.getColorModel(), (WritableRaster) tiff.getData(), false, null );
118 width = tiff.getWidth();
119 height = tiff.getHeight();
120 } catch ( Exception e ) {
121 e.printStackTrace();
122 }
123 this.scale = scale;
124 this.offset = offset;
125 }
126
127 /**
128 *
129 * @param data
130 * image containing raw data instead color information
131 */
132 public Image2RawData( BufferedImage data ) {
133 this( data, 1, 0 );
134 }
135
136 /**
137 *
138 * @param data
139 * image containing raw data instead color information
140 * @param scale
141 * scale factor; newHeight[i][j] = height[i][j] * scale
142 */
143 public Image2RawData( BufferedImage data, float scale ) {
144 this( data, scale, 0 );
145 }
146
147 /**
148 *
149 * @param data
150 * image containing raw data instead color information
151 * @param scale
152 * scale factor; newHeight[i][j] = height[i][j] * scale
153 * @param offset
154 * height offset; newHeight[i][j] = height[i][j] + offset
155 */
156 public Image2RawData( BufferedImage data, float scale, float offset ) {
157 image = data;
158 this.scale = scale;
159 this.offset = offset;
160 width = data.getWidth();
161 height = data.getHeight();
162 }
163
164 /**
165 * returns the DEM heights as float matrix
166 *
167 * @return the DEM heights as float matrix
168 */
169 public float[][] parse() {
170
171 float[][] terrain = new float[height][width];
172
173 DataBuffer db = image.getData().getDataBuffer();
174 int ps = image.getColorModel().getPixelSize();
175 if ( ps == 8 ) {
176 for ( int j = 0; j < height; j++ ) {
177 for ( int i = 0; i < width; i++ ) {
178 terrain[j][i] = db.getElem( width * j + i ) * scale + offset;
179 }
180 }
181 } else if ( ps == 16 ) {
182 for ( int j = 0; j < height; j++ ) {
183 for ( int i = 0; i < width; i++ ) {
184 terrain[j][i] = db.getElemFloat( width * j + i ) * scale + offset;
185 }
186 }
187 } else if ( ps == 24 || ps == 32 ) {
188 for ( int j = 0; j < height; j++ ) {
189 for ( int i = 0; i < width; i++ ) {
190 int v = image.getRGB( i, j );
191 terrain[j][i] = Float.intBitsToFloat( v ) * scale + offset;
192 }
193 }
194 }
195
196 return terrain;
197
198 }
199
200 /**
201 * @param x
202 * index
203 * @param y
204 * index
205 * @return the appropriate value
206 */
207 public float get( int x, int y ) {
208
209 int ps = image.getColorModel().getPixelSize();
210
211
212 if ( ps == 8 ) {
213 if ( db == null ) {
214 db = image.getData().getDataBuffer();
215 }
216 return db.getElem( width * y + x ) * scale + offset;
217 } else if ( ps == 16 ) {
218 if ( db == null ) {
219 db = image.getData().getDataBuffer();
220 }
221 return db.getElemFloat( width * y + x ) * scale + offset;
222 }
223
224 return Float.intBitsToFloat( image.getRGB( x, y ) ) * scale + offset;
225
226 }
227
228 /**
229 * @param crs
230 * of the points
231 * @return the DEM heights as pointlist matrix
232 */
233 public List<Point> parseAsPointList( CoordinateSystem crs ) {
234
235 // float[][] terrain = new float[height][width];
236 List<Point> terrain = new ArrayList<Point>( height * width );
237
238 DataBuffer db = image.getData().getDataBuffer();
239 int ps = image.getColorModel().getPixelSize();
240 if ( ps == 8 ) {
241 for ( int y = 0; ++y < height; ) {
242 for ( int x = 0; ++x < width; ) {
243 terrain.add( GeometryFactory.createPoint( x, y, db.getElem( width * y + x ) * scale + offset, crs ) );
244 }
245 }
246 } else if ( ps == 16 ) {
247 for ( int y = 0; ++y < height; ) {
248 for ( int x = 0; ++x < width; ) {
249 terrain.add( GeometryFactory.createPoint( x, y, db.getElemFloat( width * y + x ) * scale + offset,
250 crs ) );
251 }
252 }
253 } else if ( ps == 24 || ps == 32 ) {
254 for ( int y = 0; ++y < height; ) {
255 for ( int x = 0; ++x < width; ) {
256 int v = image.getRGB( x, y );
257 terrain.add( GeometryFactory.createPoint( x, y, Float.intBitsToFloat( v ) * scale + offset, crs ) );
258 }
259 }
260 }
261
262 return terrain;
263
264 }
265
266 }