svn-gvsig-desktop / branches / CqCMSDvp / libraries / libCq CMS for java.old / src / org / cresques / io / GdalFile.java @ 2312
History | View | Annotate | Download (26.4 KB)
1 |
/*
|
---|---|
2 |
* Cresques Mapping Suite. Graphic Library for constructing mapping applications.
|
3 |
*
|
4 |
* Copyright (C) 2004-5.
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 2
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
19 |
*
|
20 |
* For more information, contact:
|
21 |
*
|
22 |
* cresques@gmail.com
|
23 |
*/
|
24 |
package org.cresques.io; |
25 |
|
26 |
import java.awt.Color; |
27 |
import java.awt.Image; |
28 |
import java.awt.Point; |
29 |
import java.awt.geom.Point2D; |
30 |
import java.awt.image.BufferedImage; |
31 |
import java.awt.image.DataBuffer; |
32 |
import java.io.IOException; |
33 |
import java.util.Vector; |
34 |
|
35 |
import org.cresques.cts.ICoordTrans; |
36 |
import org.cresques.cts.IProjection; |
37 |
import org.cresques.io.raster.RasterBuf; |
38 |
import org.cresques.px.Extent; |
39 |
|
40 |
import es.gva.cit.jgdal.Gdal; |
41 |
import es.gva.cit.jgdal.GdalBuffer; |
42 |
import es.gva.cit.jgdal.GdalException; |
43 |
import es.gva.cit.jgdal.GdalRasterBand; |
44 |
import es.gva.cit.jgdal.GeoTransform; |
45 |
/**
|
46 |
* Soporte 'nativo' para ficheros desde GDAL.
|
47 |
* Este conjunto de funcionalidades est? tomado de manera casi literal
|
48 |
* del soporte para ECW de ermapper.<br>
|
49 |
* Probablemente esto deber?a formar parte del JNI que recubre a la
|
50 |
* librer?a en C extraida de gdal.<br>
|
51 |
* Lo pongo aqu? a manera de ejemplo de como atacar un formato binario
|
52 |
* desde Java.<br><br>
|
53 |
* @author Luis W. Sevilla.
|
54 |
*/
|
55 |
|
56 |
class GdalNative extends Gdal { |
57 |
static boolean WITH_OVERVIEWS = true; |
58 |
// Polilinea con extent
|
59 |
class Contour extends Vector { |
60 |
public double minX = Double.MAX_VALUE, minY = Double.MAX_VALUE; |
61 |
public double maxX = -Double.MAX_VALUE, maxY = -Double.MAX_VALUE; |
62 |
public Contour() {
|
63 |
super();
|
64 |
} |
65 |
public void add(Point2D pt) { |
66 |
super.add(pt);
|
67 |
if (pt.getX() > maxX) maxX = pt.getX();
|
68 |
if (pt.getX() < minX) minX = pt.getX();
|
69 |
if (pt.getY() > maxY) maxY = pt.getY();
|
70 |
if (pt.getY() < minY) minY = pt.getY();
|
71 |
} |
72 |
} |
73 |
/**
|
74 |
* Contorno en coordenadas geogr?ficas. (y Extent del raster).
|
75 |
*/
|
76 |
public Contour esq = new Contour(); |
77 |
public int width = 0, height = 0; |
78 |
public double originX = 0D, originY = 0D; |
79 |
public String version = ""; |
80 |
|
81 |
private int alpha = 0; |
82 |
protected int rBandNr = 1, gBandNr = 2, bBandNr = 3; |
83 |
private int dataType = GDT_Byte; |
84 |
|
85 |
public GdalNative(String fName) throws GdalException, IOException { |
86 |
super();
|
87 |
init(fName); |
88 |
} |
89 |
|
90 |
private void init(String fName) throws GdalException, IOException { |
91 |
open(fName,GA_ReadOnly); |
92 |
String ext = fName.toLowerCase().substring(fName.lastIndexOf('.')+1); |
93 |
if (ext.compareTo("tif") == 0) |
94 |
WITH_OVERVIEWS = false;
|
95 |
width = getRasterXSize(); |
96 |
height = getRasterYSize(); |
97 |
if (true) { //ext.compareTo("sid") == 0) { |
98 |
String [] metadata = getMetadata(); |
99 |
double ox=0D, oy=0D, resx=0D, resy=0D; |
100 |
|
101 |
GeoTransform trans = getGeoTransform(); |
102 |
ox = trans.adfgeotransform[0];
|
103 |
oy = trans.adfgeotransform[3];
|
104 |
resx = trans.adfgeotransform[1];
|
105 |
resy = trans.adfgeotransform[5];
|
106 |
|
107 |
System.out.println("Origin = ("+ox+","+oy+")"); |
108 |
System.out.println("Pixel Size = ("+resx+","+resy+")"); |
109 |
esq.add(new Point2D.Double(ox, oy)); |
110 |
esq.add(new Point2D.Double(ox+resx*width, oy)); |
111 |
esq.add(new Point2D.Double(ox, oy+resy*height)); |
112 |
esq.add(new Point2D.Double(ox+resx*width, oy+resy*height)); |
113 |
} else { //version.startsWith("1")) { |
114 |
//double [] transParam = getGeoTransform();
|
115 |
esq.add(new Point2D.Double(0D,height)); |
116 |
esq.add(new Point2D.Double(0D,0D)); |
117 |
esq.add(new Point2D.Double(width,0D)); |
118 |
esq.add(new Point2D.Double(width,height)); |
119 |
} |
120 |
setDataType(this.getRasterBand(1).getRasterDataType()); |
121 |
} |
122 |
|
123 |
public void setAlpha(int a) { alpha = a; } |
124 |
|
125 |
public void setDataType(int dt) { dataType = dt; } |
126 |
public int getDataType() { return dataType; } |
127 |
|
128 |
double lastReadLine = -1; |
129 |
int currentFullWidth = -1; |
130 |
int currentFullHeight = -1; |
131 |
int currentViewWidth = -1; |
132 |
int currentViewHeight = -1; |
133 |
double currentViewX = 0D; |
134 |
double viewportScale = 0D; |
135 |
double step = 0D; |
136 |
int currentOverview = -1; |
137 |
|
138 |
protected GdalRasterBand bandR=null, bandG=null, bandB=null; |
139 |
|
140 |
|
141 |
/**
|
142 |
* Devuelve la banda actualmente en uso para el color especificado.
|
143 |
* @param color 0=Rojo, 1=Green, 2=Blue.
|
144 |
* @return
|
145 |
*/
|
146 |
public GdalRasterBand getCurrentBand(int color) { |
147 |
if (color == 0) return bandR; |
148 |
else if (color == 1) return bandG; |
149 |
return bandB;
|
150 |
} |
151 |
// Supone rasters no girados
|
152 |
public Point2D worldToRaster(Point2D pt) { |
153 |
double x = (((double) currentFullWidth)/(esq.maxX-esq.minX))*(pt.getX()-esq.minX); |
154 |
double y = (((double) currentFullHeight)/(esq.maxY-esq.minY))*(esq.maxY-pt.getY()); |
155 |
Point2D ptRes = new Point2D.Double(x, y); |
156 |
return ptRes;
|
157 |
} |
158 |
|
159 |
public int setView(double dWorldTLX, double dWorldTLY, |
160 |
double dWorldBRX, double dWorldBRY, |
161 |
int nWidth, int nHeight) { |
162 |
int err = 0; |
163 |
currentFullWidth = width; |
164 |
currentFullHeight = height; |
165 |
Point2D tl = worldToRaster(new Point2D.Double(dWorldTLX, dWorldTLY)); |
166 |
Point2D br = worldToRaster(new Point2D.Double(dWorldBRX, dWorldBRY)); |
167 |
// Calcula cual es la primera l?nea a leer;
|
168 |
currentViewWidth = nWidth; |
169 |
currentViewHeight = nHeight; |
170 |
|
171 |
currentViewX = tl.getX(); |
172 |
viewportScale = (double) currentViewWidth/(br.getX()-tl.getX());
|
173 |
step = 1D/viewportScale;
|
174 |
lastReadLine = tl.getY(); |
175 |
try {
|
176 |
// calcula el overview a usar
|
177 |
bandR = getRasterBand(1);
|
178 |
currentOverview = -1;
|
179 |
if (WITH_OVERVIEWS && bandR.getOverviewCount() > 0) { |
180 |
GdalRasterBand ovb = null;
|
181 |
for (int i=bandR.getOverviewCount()-1; i>0; i--) { |
182 |
ovb = bandR.getOverview(i); |
183 |
if (ovb.getRasterBandXSize()>getRasterXSize()*viewportScale) {
|
184 |
currentOverview = i; |
185 |
viewportScale *= ((double) width/(double) ovb.getRasterBandXSize()); |
186 |
step = 1D/viewportScale;
|
187 |
currentFullWidth = ovb.getRasterBandXSize(); |
188 |
currentFullHeight = ovb.getRasterBandYSize(); |
189 |
tl = worldToRaster(new Point2D.Double(dWorldTLX, dWorldTLY)); |
190 |
currentViewX = tl.getX(); |
191 |
lastReadLine = tl.getY(); |
192 |
break;
|
193 |
} |
194 |
} |
195 |
} |
196 |
|
197 |
// Selecciona las bandas y los overviews necesarios
|
198 |
bandR = getRasterBand(rBandNr); |
199 |
setDataType(bandR.getRasterDataType()); |
200 |
if (this.getRasterCount() > 1) { |
201 |
bandG = getRasterBand(gBandNr); |
202 |
bandB = getRasterBand(bBandNr); |
203 |
} |
204 |
if (currentOverview > 0) { |
205 |
bandR = bandR.getOverview(currentOverview); |
206 |
if (this.getRasterCount() > 1) { |
207 |
bandG = bandG.getOverview(currentOverview); |
208 |
bandB = bandB.getOverview(currentOverview); |
209 |
} |
210 |
} |
211 |
|
212 |
//System.out.println(band.)
|
213 |
} catch (GdalException e) {
|
214 |
// TODO Auto-generated catch block
|
215 |
e.printStackTrace(); |
216 |
} |
217 |
|
218 |
System.out.println("GdalFile: TL=("+dWorldTLX+","+dWorldTLY+ |
219 |
"); BR=("+dWorldBRX+","+dWorldBRY+")\n"+ |
220 |
"GdalFile: escala="+viewportScale+"; lastReadLine="+lastReadLine+"\n"+ |
221 |
"Actual Raster Size="+currentFullWidth+"x"+currentFullHeight+ |
222 |
"\nDataType="+dataType);
|
223 |
return err;
|
224 |
} |
225 |
|
226 |
int lastY = -1; |
227 |
|
228 |
public void readLine(int[][] line) throws GdalException { |
229 |
int err = 0; |
230 |
int nbands = getRasterCount();
|
231 |
GdalRasterBand band = null;
|
232 |
int w = (int)(((double)currentViewWidth)*step); |
233 |
int x = (int) currentViewX; |
234 |
int y = (int) lastReadLine; |
235 |
GdalBuffer r = null, g = null, b = null; |
236 |
//if (alpha > 0) a = alpha << 24;
|
237 |
if (x+w > bandR.getRasterBandXSize())
|
238 |
w = bandR.getRasterBandXSize()-x; |
239 |
r = bandR.readRaster(x, y, w, 1, w, 1, dataType); |
240 |
if (bandG != null) |
241 |
g = bandG.readRaster(x, y, w, 1, w, 1, dataType); |
242 |
if (bandB != null) |
243 |
b = bandB.readRaster(x, y, w, 1, w, 1, dataType); |
244 |
|
245 |
lastReadLine += step; |
246 |
|
247 |
int white = Color.BLUE.getRGB(), i2=0, i=0; |
248 |
float j =0F; |
249 |
|
250 |
if (dataType == GDT_CInt16 || dataType == GDT_Int16 || dataType == GDT_UInt16){
|
251 |
if (g == null){ // Sibgle Band (Typical DEM) |
252 |
for (int k=0; k<4; k++){ |
253 |
for (i=0, j=0F; i<currentViewWidth && j<r.getSize(); i++, j+=step) { |
254 |
if(k<3) |
255 |
line[i][k] = (r.buffShort[(int) j] & 0xffff); |
256 |
else
|
257 |
line[i][3] = 0xff; |
258 |
} |
259 |
} |
260 |
}else { // Multiband |
261 |
short px;
|
262 |
//System.err.println("readLine(): Raster 16bits multibanda");
|
263 |
GdalBuffer [] bands = {r,g,b};
|
264 |
for (int k=0; k<4; k++){ |
265 |
for (i=0, j=0F; i<currentViewWidth && j<r.getSize(); i++, j+=step){ |
266 |
if(k<3) |
267 |
line[i][k] = (bands[k].buffShort[(int) j] & 0xffff); |
268 |
else
|
269 |
line[i][3] = 0xff; |
270 |
} |
271 |
} |
272 |
} |
273 |
} |
274 |
return;
|
275 |
} |
276 |
|
277 |
public int readLineRGBA(int [] line) throws GdalException { |
278 |
int err = 0; |
279 |
int nbands = getRasterCount();
|
280 |
GdalRasterBand band = null;
|
281 |
int w = (int)(((double)currentViewWidth)*step); |
282 |
int x = (int) currentViewX; |
283 |
int y = (int) lastReadLine; |
284 |
GdalBuffer r = null, g = null, b = null; |
285 |
//if (alpha > 0) a = alpha << 24;
|
286 |
if (x+w > bandR.getRasterBandXSize())
|
287 |
w = bandR.getRasterBandXSize()-x; |
288 |
r = bandR.readRaster(x, y, w, 1, w, 1, dataType); |
289 |
if (bandG != null) |
290 |
g = bandG.readRaster(x, y, w, 1, w, 1, dataType); |
291 |
if (bandB != null) |
292 |
b = bandB.readRaster(x, y, w, 1, w, 1, dataType); |
293 |
|
294 |
lastReadLine += step; |
295 |
|
296 |
int white = Color.BLUE.getRGB(), i2=0, i=0; |
297 |
float j =0F; |
298 |
int alpha = (this.alpha & 0xff) << 24; |
299 |
if (dataType == GDT_Byte)
|
300 |
if (g != null) |
301 |
for (i=0; i<currentViewWidth && j<r.getSize(); i++, j+=step) { |
302 |
line[i] = alpha + ((r.buffByte[(int) j] & 0xff) << 16) + ((g.buffByte[(int) j] & 0xff) << 8) + (b.buffByte[(int) j] & 0xff); |
303 |
} |
304 |
else
|
305 |
for (i=0; i<currentViewWidth && j<r.getSize(); i++, j+=step) { |
306 |
line[i] = alpha + ((r.buffByte[(int) j] & 0xff) << 16) + |
307 |
((r.buffByte[(int) j] & 0xff) << 8) + (r.buffByte[(int) j] & 0xff); |
308 |
} |
309 |
else if (dataType == GDT_CInt16 || dataType == GDT_Int16 || dataType == GDT_UInt16) |
310 |
if (g == null) // Sibgle Band (Typical DEM) |
311 |
/*for (i=0, j=0F, i2 = 1; i<currentViewWidth && i2<r.length;
|
312 |
i++, j+=step, i2 = (((int) j)*2)+1) {
|
313 |
line[i] = a + ((r[i2-1]) << 8) + r[i2];
|
314 |
}*/
|
315 |
for (i=0, j=0F; i<currentViewWidth && j<r.getSize(); i++, j+=step) { |
316 |
line[i] = alpha + r.buffShort[(int) j];
|
317 |
} |
318 |
else { // Multiband |
319 |
// System.err.println("Raster 16bits multibanda");
|
320 |
for (i=0, j=0F; i<currentViewWidth && j<r.getSize(); i++, j+=step) { |
321 |
line[i] = alpha | (((r.buffShort[(int) j] & 0xfff0) << 12) & 0xff0000 ) | |
322 |
(((g.buffShort[(int) j] & 0xfff0) << 4 ) & 0xff00 ) | |
323 |
(((b.buffShort[(int) j] & 0xfff0) >> 4 ) & 0xff ); |
324 |
} |
325 |
} |
326 |
//for (int i=0; i<currentViewWidth; i++) line[i] = 128+128*256+128*256*256;
|
327 |
|
328 |
return err;
|
329 |
} |
330 |
|
331 |
/**
|
332 |
* Lee una franja de la imagen.
|
333 |
* @param bandH Altura de la franja
|
334 |
* @param bufH Altura del buffer
|
335 |
* @param buf Buffer con la franja (retorno)
|
336 |
* @return
|
337 |
* @throws GdalException
|
338 |
*/
|
339 |
public int readBandRGBA(int bandH, int bufH, int [] buf) throws GdalException { |
340 |
int err = 0; |
341 |
int nbands = getRasterCount();
|
342 |
GdalRasterBand band = null;
|
343 |
int w = (int)(((double)currentViewWidth)*step); |
344 |
int x = (int)(((double)currentViewX)*step); |
345 |
int y = (int) lastReadLine; |
346 |
int h = (int) (((double)bandH)*step); |
347 |
System.out.println("Leyendo "+y); |
348 |
|
349 |
if (x+w > bandR.getRasterBandXSize())
|
350 |
w = bandR.getRasterBandXSize()-x; |
351 |
GdalBuffer r = bandR.readRaster(x, y, w, h, w, h, GDT_Byte); |
352 |
GdalBuffer g = bandG.readRaster(x, y, w, h, w, h, GDT_Byte); |
353 |
GdalBuffer b = bandB.readRaster(x, y, w, h, w, h, GDT_Byte); |
354 |
|
355 |
lastReadLine += ((double)bandH)*step;
|
356 |
|
357 |
// TODO Acabar de implementarlo
|
358 |
float k=0F; |
359 |
int alpha = (this.alpha & 0xff) << 24; |
360 |
for (int j=0, t=0; j<bandH; j++) { |
361 |
k = j*w; t=j*currentViewWidth; |
362 |
for (int i=0; i<currentViewWidth && k<r.getSize(); i++, k+=step) { |
363 |
buf[t+i] = alpha + ((r.buffByte[(int) k]) << 16) + ((g.buffByte[(int) k]) << 8) + b.buffByte[(int) k]; |
364 |
} |
365 |
} |
366 |
//for (int i=0; i<currentViewWidth; i++) line[i] = 128+128*256+128*256*256;
|
367 |
|
368 |
return err;
|
369 |
|
370 |
} |
371 |
|
372 |
void pintaInfo() {
|
373 |
try {
|
374 |
//System.out.println("Origin = "+originX+","+originY);
|
375 |
//System.out.println("Origin = "+this.);
|
376 |
System.out.println("GeoTransform:"); |
377 |
GeoTransform trans = getGeoTransform(); |
378 |
for (int i=0; i<6; i++) |
379 |
System.out.println(" param["+i+"]="+trans.adfgeotransform[i]); |
380 |
System.out.println("Metadata:"); |
381 |
String [] metadata = getMetadata(); |
382 |
for (int i=0; i<metadata.length; i++) { |
383 |
System.out.println(metadata[i]);
|
384 |
} |
385 |
} catch (GdalException e) {
|
386 |
// TODO Auto-generated catch block
|
387 |
e.printStackTrace(); |
388 |
} |
389 |
|
390 |
} |
391 |
|
392 |
void pintaPaleta() {
|
393 |
} |
394 |
|
395 |
public int getBlockSize(){ |
396 |
return this.getBlockSize(); |
397 |
} |
398 |
} |
399 |
|
400 |
/**
|
401 |
* @author Luis W. Sevilla
|
402 |
*/
|
403 |
public class GdalFile extends GeoRasterFile { |
404 |
public final static int BAND_HEIGHT = 64; |
405 |
protected GdalNative file = null; |
406 |
|
407 |
private Extent v = null; |
408 |
|
409 |
static {
|
410 |
//GeoRasterFile.registerExtension("sid", GdalFile.class);
|
411 |
GeoRasterFile.registerExtension("img", GdalFile.class);
|
412 |
GeoRasterFile.registerExtension("tif", GdalFile.class);
|
413 |
GeoRasterFile.registerExtension("tiff", GdalFile.class);
|
414 |
GeoRasterFile.registerExtension("jpg", GdalFile.class);
|
415 |
GeoRasterFile.registerExtension("png", GdalFile.class);
|
416 |
} |
417 |
|
418 |
public GdalFile(IProjection proj, String fName) { |
419 |
super(proj, fName);
|
420 |
extent = new Extent();
|
421 |
try {
|
422 |
file = new GdalNative(fName);
|
423 |
showOnOpen(); |
424 |
load(); |
425 |
bandCount = file.getRasterCount(); |
426 |
if ( bandCount > 2) { |
427 |
setBand(RED_BAND, 0);
|
428 |
setBand(GREEN_BAND, 1);
|
429 |
setBand(BLUE_BAND, 2);
|
430 |
} else
|
431 |
setBand(RED_BAND|GREEN_BAND|BLUE_BAND, 0);
|
432 |
} catch(Exception e){ |
433 |
System.out.println("Error en GdalOpen"); |
434 |
e.printStackTrace(); |
435 |
file = null;
|
436 |
} |
437 |
int dataType = file.getDataType();
|
438 |
if (dataType == Gdal.GDT_Byte)
|
439 |
setDataType(DataBuffer.TYPE_BYTE);
|
440 |
else if (dataType == Gdal.GDT_CInt16 || dataType == Gdal.GDT_Int16) |
441 |
setDataType(DataBuffer.TYPE_SHORT);
|
442 |
else if(dataType == Gdal.GDT_UInt16) |
443 |
setDataType(DataBuffer.TYPE_USHORT);
|
444 |
} |
445 |
|
446 |
public GeoFile load() {
|
447 |
/*double minX, minY, maxX, maxY;
|
448 |
minX = minY = 0D;
|
449 |
maxX = (double) width;
|
450 |
maxY = (double) height;*/
|
451 |
extent = new Extent(file.esq.minX, file.esq.minY, file.esq.maxX, file.esq.maxY);
|
452 |
return this; |
453 |
} |
454 |
|
455 |
public void close() { |
456 |
try {
|
457 |
file.close(); |
458 |
} catch (GdalException e) {
|
459 |
// TODO Auto-generated catch block
|
460 |
e.printStackTrace(); |
461 |
} |
462 |
} |
463 |
|
464 |
public void setBand(int flag, int bandNr) { |
465 |
super.setBand(flag, bandNr);
|
466 |
if ((flag & GeoRasterFile.RED_BAND) == GeoRasterFile.RED_BAND) file.rBandNr = bandNr+1; |
467 |
if ((flag & GeoRasterFile.GREEN_BAND) == GeoRasterFile.GREEN_BAND) file.gBandNr = bandNr+1; |
468 |
if ((flag & GeoRasterFile.BLUE_BAND) == GeoRasterFile.BLUE_BAND) file.bBandNr = bandNr+1; |
469 |
} |
470 |
public void setView(Extent e) { v = new Extent(e); } |
471 |
public Extent getView() { return v; } |
472 |
|
473 |
public int getWidth() { return file.width; } |
474 |
public int getHeight() { return file.height;} |
475 |
|
476 |
/* (non-Javadoc)
|
477 |
* @see org.cresques.io.GeoRasterFile#reProject(org.cresques.cts.ICoordTrans)
|
478 |
*/
|
479 |
public void reProject(ICoordTrans rp) { |
480 |
// TODO Auto-generated method stub
|
481 |
|
482 |
} |
483 |
/* (non-Javadoc)
|
484 |
* @see org.cresques.io.GeoRasterFile#setTransparency(boolean)
|
485 |
* /
|
486 |
public void setTransparency(boolean t) {
|
487 |
// TODO Auto-generated method stub
|
488 |
}
|
489 |
public void setTransparency(int t) {
|
490 |
// TODO Auto-generated method stub
|
491 |
}*/
|
492 |
/* (non-Javadoc)
|
493 |
* @see org.cresques.io.GeoRasterFile#updateImage(int, int, org.cresques.cts.ICoordTrans)
|
494 |
*/
|
495 |
public Image updateImage(int width, int height, ICoordTrans rp) { |
496 |
double dFileAspect, dWindowAspect;
|
497 |
int line, pRGBArray[] = null; |
498 |
Image image = null; |
499 |
|
500 |
// Work out the correct aspect for the setView call.
|
501 |
dFileAspect = (double)v.width()/(double)v.height(); |
502 |
dWindowAspect = (double)width /(double)height; |
503 |
|
504 |
if (dFileAspect > dWindowAspect) {
|
505 |
height =(int)((double)width/dFileAspect); |
506 |
} else {
|
507 |
width = (int)((double)height*dFileAspect); |
508 |
} |
509 |
|
510 |
// Set the view
|
511 |
file.setView(v.minX(), v.maxY(), v.maxX(), v.minY(), |
512 |
width, height); |
513 |
|
514 |
if(width<=0)width=1; |
515 |
if(height<=0)height=1; |
516 |
|
517 |
image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); |
518 |
//image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
|
519 |
pRGBArray = new int[width/**BAND_HEIGHT*/]; |
520 |
try {
|
521 |
//int nLin = height % BAND_HEIGHT;
|
522 |
file.setAlpha(getAlpha()); |
523 |
setBand(RED_BAND, rBandNr); |
524 |
setBand(GREEN_BAND, gBandNr); |
525 |
setBand(BLUE_BAND, bBandNr); |
526 |
for (line=0; line < height; line++) { //+=BAND_HEIGHT) { |
527 |
//int bandH = Math.min(BAND_HEIGHT, height-line);
|
528 |
//file.readBandRGBA(bandH, BAND_HEIGHT, pRGBArray);
|
529 |
file.readLineRGBA(pRGBArray); |
530 |
setRGBLine((BufferedImage) image, 0, line, width, 1/*bandH*/, pRGBArray, 0, width); |
531 |
} |
532 |
} catch (Exception e) { |
533 |
// TODO Auto-generated catch block
|
534 |
e.printStackTrace(); |
535 |
} |
536 |
|
537 |
return image;
|
538 |
} |
539 |
|
540 |
public RasterBuf getRaster(int width, int height, ICoordTrans rp) { |
541 |
double dFileAspect, dWindowAspect;
|
542 |
int line, pRGBArray[][] = null; |
543 |
RasterBuf raster = null;
|
544 |
|
545 |
// Work out the correct aspect for the setView call.
|
546 |
dFileAspect = (double)v.width()/(double)v.height(); |
547 |
dWindowAspect = (double)width /(double)height; |
548 |
|
549 |
if (dFileAspect > dWindowAspect) {
|
550 |
height =(int)((double)width/dFileAspect); |
551 |
} else {
|
552 |
width = (int)((double)height*dFileAspect); |
553 |
} |
554 |
|
555 |
// Set the view
|
556 |
file.setView(v.minX(), v.maxY(), v.maxX(), v.minY(), |
557 |
width, height); |
558 |
|
559 |
raster = new RasterBuf(DataBuffer.TYPE_INT, width, height, 4, new Point(0,0)); |
560 |
try {
|
561 |
//int nLin = height % BAND_HEIGHT;
|
562 |
file.setAlpha(getAlpha()); |
563 |
setBand(RED_BAND, rBandNr); |
564 |
setBand(GREEN_BAND, gBandNr); |
565 |
setBand(BLUE_BAND, bBandNr); |
566 |
for (line=0; line < height; line++) { //+=BAND_HEIGHT) { |
567 |
file.readLine(raster.getLineInt(line)); |
568 |
} |
569 |
} catch (Exception e) { |
570 |
// TODO Auto-generated catch block
|
571 |
e.printStackTrace(); |
572 |
} |
573 |
|
574 |
return raster;
|
575 |
} |
576 |
|
577 |
/**
|
578 |
* Asigna al objeto Image los valores con los dato de la imagen contenidos en el
|
579 |
* vector de enteros.
|
580 |
* @param image imagen con los datos actuales
|
581 |
* @param startX inicio de la posici?n en X dentro de la imagen
|
582 |
* @param startY inicio de la posici?n en X dentro de la imagen
|
583 |
* @param w Ancho de la imagen
|
584 |
* @param h Alto de la imagen
|
585 |
* @param rgbArray vector que contiene la banda que se va a sustituir
|
586 |
* @param offset desplazamiento
|
587 |
* @param scansize tama?o de imagen recorrida por cada p
|
588 |
*/
|
589 |
protected void setRGBLine(BufferedImage image, int startX, int startY, int w, int h, int[] rgbArray, |
590 |
int offset, int scansize) { |
591 |
image.setRGB(startX, startY, w, h, rgbArray, offset, scansize); |
592 |
} |
593 |
|
594 |
/**
|
595 |
* Asigna al objeto Image la mezcla entre los valores que ya tiene y los valores
|
596 |
* con los dato de la imagen contenidos en el vector de enteros. De los valores RGB
|
597 |
* que ya contiene se mantienen las bandas que no coinciden con el valor de flags. La
|
598 |
* banda correspondiente a flags es sustituida por los datos del vector.
|
599 |
* @param image imagen con los datos actuales
|
600 |
* @param startX inicio de la posici?n en X dentro de la imagen
|
601 |
* @param startY inicio de la posici?n en X dentro de la imagen
|
602 |
* @param w Ancho de la imagen
|
603 |
* @param h Alto de la imagen
|
604 |
* @param rgbArray vector que contiene la banda que se va a sustituir
|
605 |
* @param offset desplazamiento
|
606 |
* @param scansize tama?o de imagen recorrida por cada paso
|
607 |
* @param flags banda que se va a sustituir (Ctes de GeoRasterFile)
|
608 |
*/
|
609 |
protected void setRGBLine(BufferedImage image, int startX, int startY, int w, int h, int[] rgbArray, |
610 |
int offset, int scansize, int flags) { |
611 |
int [] line = new int[rgbArray.length]; |
612 |
image.getRGB(startX, startY, w, h, line, offset, scansize); |
613 |
if (flags == GeoRasterFile.RED_BAND)
|
614 |
for (int i=0; i<line.length; i++) |
615 |
line[i] = (line[i] & 0x0000ffff) | (rgbArray[i] & 0xffff0000); |
616 |
else if (flags == GeoRasterFile.GREEN_BAND) |
617 |
for (int i=0; i<line.length; i++) |
618 |
line[i] = (line[i] & 0x00ff00ff) | (rgbArray[i] & 0xff00ff00); |
619 |
else if (flags == GeoRasterFile.BLUE_BAND) |
620 |
for (int i=0; i<line.length; i++) |
621 |
line[i] = (line[i] & 0x00ffff00) | (rgbArray[i] & 0xff0000ff); |
622 |
image.setRGB(startX, startY, w, h, line, offset, scansize); |
623 |
} |
624 |
|
625 |
/**
|
626 |
* Asigna al objeto Image la mezcla entre los valores que ya tiene y los valores
|
627 |
* con los dato de la imagen contenidos en el vector de enteros. De los valores RGB
|
628 |
* que ya contiene se mantienen las bandas que no coinciden con el valor de flags. La
|
629 |
* banda correspondiente a flags es sustituida por los datos del vector.
|
630 |
* @param image imagen con los datos actuales
|
631 |
* @param startX inicio de la posici?n en X dentro de la imagen
|
632 |
* @param startY inicio de la posici?n en X dentro de la imagen
|
633 |
* @param w Ancho de la imagen
|
634 |
* @param h Alto de la imagen
|
635 |
* @param rgbArray vector que contiene la banda que se va a sustituir
|
636 |
* @param offset desplazamiento
|
637 |
* @param scansize tama?o de imagen recorrida por cada paso
|
638 |
* @param origBand Banda origen del GeoRasterFile
|
639 |
* @param destBandFlag banda que se va a sustituir (Ctes de GeoRasterFile)
|
640 |
*/
|
641 |
protected void setRGBLine(BufferedImage image, int startX, int startY, int w, int h, int[] rgbArray, |
642 |
int offset, int scansize, int origBand, int destBandFlag) { |
643 |
int [] line = new int[rgbArray.length]; |
644 |
image.getRGB(startX, startY, w, h, line, offset, scansize); |
645 |
if (origBand == 0 && destBandFlag == GeoRasterFile.RED_BAND) |
646 |
for (int i=0; i<line.length; i++) |
647 |
line[i] = (line[i] & 0x0000ffff) | (rgbArray[i] & 0xffff0000); |
648 |
else if (origBand == 1 && destBandFlag == GeoRasterFile.GREEN_BAND) |
649 |
for (int i=0; i<line.length; i++) |
650 |
line[i] = (line[i] & 0x00ff00ff) | (rgbArray[i] & 0xff00ff00); |
651 |
else if (origBand == 2 && destBandFlag == GeoRasterFile.BLUE_BAND) |
652 |
for (int i=0; i<line.length; i++) |
653 |
line[i] = (line[i] & 0x00ffff00) | (rgbArray[i] & 0xff0000ff); |
654 |
|
655 |
else if (origBand == 0 && destBandFlag == GeoRasterFile.GREEN_BAND) |
656 |
for (int i=0; i<line.length; i++) |
657 |
line[i] = (line[i] & 0xffff00ff) | ((rgbArray[i] & 0x00ff0000) >> 8) ; |
658 |
else if (origBand == 0 && destBandFlag == GeoRasterFile.BLUE_BAND) |
659 |
for (int i=0; i<line.length; i++) |
660 |
line[i] = (line[i] & 0xffffff00) | ((rgbArray[i] & 0x00ff0000) >> 16); |
661 |
else if (origBand == 1 && destBandFlag == GeoRasterFile.RED_BAND) |
662 |
for (int i=0; i<line.length; i++) |
663 |
line[i] = (line[i] & 0xff00ffff) | ((rgbArray[i] & 0x0000ff00) << 8); |
664 |
|
665 |
else if (origBand == 1 && destBandFlag == GeoRasterFile.BLUE_BAND) |
666 |
for (int i=0; i<line.length; i++) |
667 |
line[i] = (line[i] & 0xffffff00) | ((rgbArray[i] & 0x0000ff00) >> 8); |
668 |
else if (origBand == 2 && destBandFlag == GeoRasterFile.RED_BAND) |
669 |
for (int i=0; i<line.length; i++) |
670 |
line[i] = (line[i] & 0xff00ffff) | ((rgbArray[i] & 0x000000ff) << 16); |
671 |
else if (origBand == 2 && destBandFlag == GeoRasterFile.GREEN_BAND) |
672 |
for (int i=0; i<line.length; i++) |
673 |
line[i] = (line[i] & 0xffff00ff) | ((rgbArray[i] & 0x000000ff) << 8); |
674 |
image.setRGB(startX, startY, w, h, line, offset, scansize); |
675 |
} |
676 |
|
677 |
private void showOnOpen() { |
678 |
// Report en la apertura (quitar)
|
679 |
System.out.println("Fichero GDAL '"+getName()+"' abierto."); |
680 |
System.out.println("Version = "+file.version); |
681 |
System.out.println(" Size = ("+file.width+","+file.height+")"); |
682 |
try {
|
683 |
System.out.println(" NumBands = ("+file.getRasterCount()+")"); |
684 |
} catch (GdalException e) {
|
685 |
// TODO Auto-generated catch block
|
686 |
e.printStackTrace(); |
687 |
} |
688 |
file.pintaInfo(); |
689 |
file.pintaPaleta(); |
690 |
|
691 |
} |
692 |
|
693 |
/* (non-Javadoc)
|
694 |
* @see org.cresques.io.GeoRasterFile#updateImage(int, int, org.cresques.cts.ICoordTrans, java.awt.Image, int, int)
|
695 |
*/
|
696 |
public Image updateImage(int width, int height, ICoordTrans rp, Image img, int origBand, int destBandFlag){ |
697 |
|
698 |
double dFileAspect, dWindowAspect;
|
699 |
int line, pRGBArray[] = null; |
700 |
|
701 |
// Work out the correct aspect for the setView call.
|
702 |
dFileAspect = (double)v.width()/(double)v.height(); |
703 |
dWindowAspect = (double)width /(double)height; |
704 |
|
705 |
if (dFileAspect > dWindowAspect) {
|
706 |
height =(int)((double)width/dFileAspect); |
707 |
} else {
|
708 |
width = (int)((double)height*dFileAspect); |
709 |
} |
710 |
|
711 |
// Set the view
|
712 |
file.setView(v.minX(), v.maxY(), v.maxX(), v.minY(), |
713 |
width, height); |
714 |
|
715 |
if(width<=0)width=1; |
716 |
if(height<=0)height=1; |
717 |
|
718 |
pRGBArray = new int[width/**BAND_HEIGHT*/]; |
719 |
try {
|
720 |
setBand(RED_BAND, rBandNr); |
721 |
setBand(GREEN_BAND, gBandNr); |
722 |
setBand(BLUE_BAND, bBandNr); |
723 |
file.setAlpha(getAlpha()); |
724 |
if(img!=null){ |
725 |
for (line=0; line < height; line++) { |
726 |
file.readLineRGBA(pRGBArray); |
727 |
setRGBLine((BufferedImage) img, 0, line, width, 1/*bandH*/, pRGBArray, 0, width, origBand, destBandFlag); |
728 |
} |
729 |
return img;
|
730 |
}else{
|
731 |
Image image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); |
732 |
for (line=0; line < height; line++) { |
733 |
file.readLineRGBA(pRGBArray); |
734 |
setRGBLine((BufferedImage) image, 0, line, width, 1/*bandH*/, pRGBArray, 0, width); |
735 |
} |
736 |
return image;
|
737 |
} |
738 |
} catch (Exception e) { |
739 |
// TODO Auto-generated catch block
|
740 |
e.printStackTrace(); |
741 |
} |
742 |
|
743 |
return img;
|
744 |
} |
745 |
|
746 |
/* (non-Javadoc)
|
747 |
* @see org.cresques.io.GeoRasterFile#getData(int, int, int)
|
748 |
*/
|
749 |
public Object getData(int x, int y, int band) { |
750 |
// TODO Auto-generated method stub
|
751 |
return null; |
752 |
} |
753 |
|
754 |
/**
|
755 |
* Devuelve los datos de una ventana solicitada
|
756 |
* @param ulX coordenada X superior izda.
|
757 |
* @param ulY coordenada Y superior derecha.
|
758 |
* @param sizeX tama?o en X de la ventana.
|
759 |
* @param sizeY tama?o en Y de la ventana.
|
760 |
* @param band Banda solicitada.
|
761 |
*/
|
762 |
public byte[] getWindow(int ulX, int ulY, int sizeX, int sizeY, int band){ |
763 |
|
764 |
return null; |
765 |
} |
766 |
|
767 |
/**
|
768 |
* Obtiene la zona (Norte / Sur)
|
769 |
* @return true si la zona es norte y false si es sur
|
770 |
*/
|
771 |
|
772 |
public boolean getZone(){ |
773 |
|
774 |
return false; |
775 |
} |
776 |
|
777 |
/**
|
778 |
*Devuelve el n?mero de zona UTM
|
779 |
*@return N?mero de zona
|
780 |
*/
|
781 |
|
782 |
public int getUTM(){ |
783 |
|
784 |
return 0; |
785 |
} |
786 |
|
787 |
/**
|
788 |
* Obtiene el sistema de coordenadas geograficas
|
789 |
* @return Sistema de coordenadas geogr?ficas
|
790 |
*/
|
791 |
public String getGeogCS(){ |
792 |
|
793 |
return new String(""); |
794 |
} |
795 |
/**
|
796 |
* Devuelve el tama?o de bloque
|
797 |
* @return Tama?o de bloque
|
798 |
*/
|
799 |
public int getBlockSize(){ |
800 |
//TODO Nacho: Implementar getBlockSize de EcwFile
|
801 |
return file.getBlockSize();
|
802 |
} |
803 |
} |
804 |
|
805 |
|