001: //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/trunk/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 08:39:07 -0800 (Thu, 27 Dec 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(
116: dataFile.toURL().openStream());
117: TIFFImage tiff = new TIFFImage(fss, new TIFFDecodeParam(),
118: 0);
119: image = new BufferedImage(tiff.getColorModel(),
120: (WritableRaster) tiff.getData(), false, null);
121: width = tiff.getWidth();
122: height = tiff.getHeight();
123: } catch (Exception e) {
124: e.printStackTrace();
125: }
126: this .scale = scale;
127: this .offset = offset;
128: }
129:
130: /**
131: *
132: * @param data
133: * image containing raw data instead color information
134: */
135: public Image2RawData(BufferedImage data) {
136: this (data, 1, 0);
137: }
138:
139: /**
140: *
141: * @param data
142: * image containing raw data instead color information
143: * @param scale
144: * scale factor; newHeight[i][j] = height[i][j] * scale
145: */
146: public Image2RawData(BufferedImage data, float scale) {
147: this (data, scale, 0);
148: }
149:
150: /**
151: *
152: * @param data
153: * image containing raw data instead color information
154: * @param scale
155: * scale factor; newHeight[i][j] = height[i][j] * scale
156: * @param offset
157: * height offset; newHeight[i][j] = height[i][j] + offset
158: */
159: public Image2RawData(BufferedImage data, float scale, float offset) {
160: image = data;
161: this .scale = scale;
162: this .offset = offset;
163: width = data.getWidth();
164: height = data.getHeight();
165: }
166:
167: /**
168: * returns the DEM heights as float matrix
169: *
170: * @return the DEM heights as float matrix
171: */
172: public float[][] parse() {
173:
174: float[][] terrain = new float[height][width];
175:
176: DataBuffer db = image.getData().getDataBuffer();
177: int ps = image.getColorModel().getPixelSize();
178: if (ps == 8) {
179: for (int j = 0; j < height; j++) {
180: for (int i = 0; i < width; i++) {
181: terrain[j][i] = db.getElem(width * j + i) * scale
182: + offset;
183: }
184: }
185: } else if (ps == 16) {
186: for (int j = 0; j < height; j++) {
187: for (int i = 0; i < width; i++) {
188: terrain[j][i] = db.getElemFloat(width * j + i)
189: * scale + offset;
190: }
191: }
192: } else if (ps == 24 || ps == 32) {
193: for (int j = 0; j < height; j++) {
194: for (int i = 0; i < width; i++) {
195: int v = image.getRGB(i, j);
196: terrain[j][i] = Float.intBitsToFloat(v) * scale
197: + offset;
198: }
199: }
200: }
201:
202: return terrain;
203:
204: }
205:
206: /**
207: * @param x
208: * index
209: * @param y
210: * index
211: * @return the appropriate value
212: */
213: public float get(int x, int y) {
214:
215: int ps = image.getColorModel().getPixelSize();
216:
217: if (ps == 8) {
218: if (db == null) {
219: db = image.getData().getDataBuffer();
220: }
221: return db.getElem(width * y + x) * scale + offset;
222: } else if (ps == 16) {
223: if (db == null) {
224: db = image.getData().getDataBuffer();
225: }
226: return db.getElemFloat(width * y + x) * scale + offset;
227: }
228:
229: return Float.intBitsToFloat(image.getRGB(x, y)) * scale
230: + offset;
231:
232: }
233:
234: /**
235: * @param crs
236: * of the points
237: * @return the DEM heights as pointlist matrix
238: */
239: public List<Point> parseAsPointList(CoordinateSystem crs) {
240:
241: // float[][] terrain = new float[height][width];
242: List<Point> terrain = new ArrayList<Point>(height * width);
243:
244: DataBuffer db = image.getData().getDataBuffer();
245: int ps = image.getColorModel().getPixelSize();
246: if (ps == 8) {
247: for (int y = 0; ++y < height;) {
248: for (int x = 0; ++x < width;) {
249: terrain.add(GeometryFactory.createPoint(x, y, db
250: .getElem(width * y + x)
251: * scale + offset, crs));
252: }
253: }
254: } else if (ps == 16) {
255: for (int y = 0; ++y < height;) {
256: for (int x = 0; ++x < width;) {
257: terrain.add(GeometryFactory.createPoint(x, y, db
258: .getElemFloat(width * y + x)
259: * scale + offset, crs));
260: }
261: }
262: } else if (ps == 24 || ps == 32) {
263: for (int y = 0; ++y < height;) {
264: for (int x = 0; ++x < width;) {
265: int v = image.getRGB(x, y);
266: terrain.add(GeometryFactory.createPoint(x, y, Float
267: .intBitsToFloat(v)
268: * scale + offset, crs));
269: }
270: }
271: }
272:
273: return terrain;
274:
275: }
276:
277: }
|