Revision 1533 branches/v02_desarrollo/libraries/libCq CMS for java.old/src/org/cresques/px/PxRaster.java

View differences:

PxRaster.java
12 12
import java.awt.image.BufferedImage;
13 13
import java.awt.image.DataBuffer;
14 14
import java.awt.image.ImageObserver;
15
import java.awt.image.Raster;
16 15
import java.util.Date;
17 16
import java.util.Vector;
18 17

  
......
22 21
import org.cresques.geo.ViewPortData;
23 22
import org.cresques.io.GdalFile;
24 23
import org.cresques.io.GeoRasterFile;
24
import org.cresques.io.RasterBuf;
25 25

  
26 26
public class PxRaster extends PxObj implements Projected {
27 27
	protected GeoRasterFile geoFile = null;
......
31 31
	protected GeoRasterFile [] colorBand = null;
32 32
	protected int rBand = 1, gBand = 2, bBand = 3;
33 33
	
34
	int [] minBandValue = {Integer.MAX_VALUE,Integer.MAX_VALUE,Integer.MAX_VALUE};
35
	int [] maxBandValue = {Integer.MIN_VALUE,Integer.MIN_VALUE,Integer.MIN_VALUE};
36

  
34 37
	int transparente = 0x10ffff80;
35 38

  
36 39
	String vName = null;
......
282 285
				(geoFile.getDataType() != DataBuffer.TYPE_BYTE)) {
283 286
				System.out.println("PxRaster: Has dado con un Raster de 16 bits");
284 287
				System.out.println("Dibujando PxRaster sz=("+wImg+","+hImg+"...");
285
				Raster raster = ((GdalFile) geoFile).getRaster(wImg, hImg, rp);
288
				RasterBuf raster = ((GdalFile) geoFile).getRaster(wImg, hImg, rp);
289
				t2 = new Date().getTime();
290
				System.out.println("Dibujando PxRaster: "+(t2-t1)/1000D+", secs. Obteniendo"); t1 = t2;
286 291
				geoImage = renderizeRaster(raster);
287 292
				t2 = new Date().getTime();
288
				System.out.println("Dibujando PxRaster: "+(t2-t1)/1000D+", secs. Obteniendo");
293
				System.out.println("Dibujando PxRaster: "+(t2-t1)/1000D+", secs. Filtrando/Renderizando");t1 = t2;
289 294

  
290 295
				g.drawImage(geoImage, (int) Math.round(pt.getX()), (int) Math.round(pt.getY()), component);
291
				t1 = new Date().getTime();
292
				System.out.println("Dibujando PxRaster: "+(t1-t2)/1000D+", secs. Renderizando");
296
				t2 = new Date().getTime();
297
				System.out.println("Dibujando PxRaster: "+(t2-t1)/1000D+", secs. Dibujando");
293 298
			} else if (colorBand != null) { // multiBands
294 299
				System.out.println("Dibujando PxRaster (Multifile) ...");
295 300
    			//if (doTransparency)
......
323 328
		}
324 329
	}
325 330
	
326
	public Image renderizeRaster(Raster raster) {
327
		int [] px = new int[3];
328
		int rgb = 0;
329
		int [] minBandValue = {Integer.MAX_VALUE,Integer.MAX_VALUE,Integer.MAX_VALUE};
330
		int [] maxBandValue = {Integer.MIN_VALUE,Integer.MIN_VALUE,Integer.MIN_VALUE};
331
		for (int y=0; y<raster.getHeight(); y++)
332
			for (int x=0; x<raster.getWidth(); x++) {
333
				raster.getPixel(x, y, px);
334
				for (int i=0; i<3; i++) {
335
					minBandValue[i] = Math.min(minBandValue[i], px[i]);
336
					maxBandValue[i] = Math.max(maxBandValue[i], px[i]);
331
	/**
332
	 * Filtro para raster. Ancestro de todos los filtros.
333
	 * @author Luis W. Sevilla (sevilla_lui@gva.es)
334
	 */
335
	abstract class RasterFilter {
336
		protected RasterBuf raster = null;
337
		protected int [] px = new int[3];
338
		private int height = 0;
339
		private int width = 0;
340
		
341
		public RasterFilter(RasterBuf raster) {
342
			this.raster = raster;
343
			height = raster.getHeight();
344
			width = raster.getWidth();
345
		}
346
		
347
		public void execute() {
348
			for (int y=0; y<height; y++)
349
				for (int x=0; x<width; x++) {
350
					process(x, y);
337 351
				}
352
		}
353
		
354
		abstract public void process(int x, int y);
355
	}
356
	
357
	/**
358
	 * Calcula el m?ximo y el m?nimo de un raster.
359
	 * @author Luis W. Sevilla (sevilla_lui@gva.es)
360
	 */
361
	class ComputeMinMaxFilter extends RasterFilter {
362
		private int [] min = {Integer.MAX_VALUE,Integer.MAX_VALUE,Integer.MAX_VALUE};
363
		private int [] max = {Integer.MIN_VALUE,Integer.MIN_VALUE,Integer.MIN_VALUE};
364
		
365
		public ComputeMinMaxFilter(RasterBuf raster, int [] min, int [] max) {
366
			super(raster);
367
			this.raster = raster;
368
			this.min = new int[min.length];
369
			this.max = new int[max.length];
370
			for (int i=0; i<min.length; i++) {
371
				this.min[i] = min[i];
372
				this.max[i] = max[i];
338 373
			}
339
		double [] scale = new double[3];
340
		double [] offset = new double[3];
341
		for (int i=0; i<3; i++) {
342
			scale[i] = 255D/(maxBandValue[i]-minBandValue[i]);
343
			offset[i] = (255D*minBandValue[i])/(minBandValue[i]-maxBandValue[i]);
374
			execute();
344 375
		}
376
		
377
		public void process(int x, int y) {
378
			raster.getPixelShort(x, y, px);
379
			for (int i=0; i<3; i++) {
380
				min[i] = Math.min(min[i], px[i]);
381
				max[i] = Math.max(max[i], px[i]);
382
			}
383
		}
345 384

  
346
		BufferedImage image = new BufferedImage(raster.getWidth(), raster.getHeight(), BufferedImage.TYPE_INT_ARGB);
385
		public int getMin(int bandNr) { return min[bandNr]; }
347 386

  
348
		int alpha = (getAlpha() & 0xff) << 24;
349
		for (int y=0; y<raster.getHeight(); y++)
350
			for (int x=0; x<raster.getWidth(); x++) {
351
				raster.getPixel(x, y, px);
352
				/*rgb = alpha | (((px[0] & 0xffff) << 12) & 0xff0000 ) | 
353
				  (((px[1] & 0xffff) << 4 ) & 0xff00 ) |
354
				  (((px[2] & 0xffff) >> 4 ) & 0xff );*/
355
				rgb = alpha | (((int) (((double)px[0])*scale[0] + offset[0])) & 0xff) << 16 |
356
							  (((int) (((double)px[1])*scale[1] + offset[1])) & 0xff) << 8 |
357
							  (((int) (((double)px[2])*scale[2] + offset[2])) & 0xff);
358
				
359
				image.setRGB(x, y, rgb);
387
		public int getMax(int bandNr) { return max[bandNr]; }
388
	}
389
	
390
	/**
391
	 * Realzado Lineal (Amplitude Rescaling).
392
	 * @author Luis W. Sevilla (sevilla_lui@gva.es)
393
	 */
394
	class LinearEnhancementFilter extends RasterFilter {
395
		private double [] scale = new double[3];
396
		private double [] offset = new double[3];
397
		public LinearEnhancementFilter(RasterBuf raster, int [] min, int [] max) {
398
			super(raster);
399
			for (int i=0; i<3; i++) {
400
				scale[i] = 255D/(max[i]-min[i]);
401
				offset[i] = (255D*min[i])/(min[i]-max[i]);
360 402
			}
403
			execute();
404
		}
405

  
406
		public void process(int x, int y) {
407
			raster.getPixelShort(x, y, px);
408
			px[0] = (((int) (((double)px[0])*scale[0] + offset[0])) & 0xff);
409
			px[1] = (((int) (((double)px[1])*scale[1] + offset[1])) & 0xff);
410
			px[2] = (((int) (((double)px[2])*scale[2] + offset[2])) & 0xff);
411
			raster.setPixelShort(x, y, px);
412
		}
413
	}
414
	
415
	/**
416
	 * Renderiza el raster.
417
	 * @author Luis W. Sevilla (sevilla_lui@gva.es)
418
	 */
419
	
420
	class RasterToImageFilter extends RasterFilter {
421
		private BufferedImage image;
422
		private int alpha;
423
		private int rgb;
424
		public RasterToImageFilter(RasterBuf raster, int a) {
425
			super(raster);
426
			this.alpha = (a & 0xff) << 24;
427
			image = new BufferedImage(raster.getWidth(), raster.getHeight(), BufferedImage.TYPE_INT_ARGB);
428
			execute();
429
		}
361 430
		
431
		/* (non-Javadoc)
432
		 * @see org.cresques.px.PxRaster.RasterFilter#process(int, int)
433
		 */
434
		public void process(int x, int y) {
435
			raster.getPixelShort(x, y, px);
436
			rgb = alpha | ((px[0] & 0xff) << 16) |
437
					((px[1] & 0xff) << 8) | (px[2] & 0xff);
438
			
439
			image.setRGB(x, y, rgb);
440
		}
441
		
442
		public Image getImage() { return image; }
443
	}
444
	
445
	public Image renderizeRaster(RasterBuf raster) {
446
		ComputeMinMaxFilter filter = 
447
			new ComputeMinMaxFilter(raster, minBandValue, maxBandValue);
448
		for (int i=0; i<3; i++) {
449
			minBandValue[i] = filter.getMin(i);
450
			maxBandValue[i] = filter.getMax(i);
451
		}
452

  
453
		LinearEnhancementFilter lEFilter = 
454
			new LinearEnhancementFilter(raster, minBandValue, maxBandValue);
455

  
456
		Image image = new RasterToImageFilter(raster, getAlpha()).getImage();
457
		
362 458
		for (int i=0; i<3; i++)
363 459
			System.out.println("  Banda ["+i+"]: "+minBandValue[i]+"..."+maxBandValue[i]);
364 460
		return image;
365 461
	}
462
	
366 463
	public void drawMarco(Graphics2D g, ViewPortData vp) {
367 464
//		Color color = new Color(255,222,165,128), fillColor = new Color(255,214,132,128);
368 465
		Color color = new Color(128,128,128), fillColor = new Color(255,220,220,0x20);

Also available in: Unified diff