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