Revision 30008 branches/v2_0_0_prep/libraries/libRaster/src/org/gvsig/raster/datastruct/Transparency.java

View differences:

Transparency.java
49 49
 * <p>
50 50
 * Una transparencia que se aplica a un buffer puede tener cuatro procedencias distintas:
51 51
 * <UL>
52
 * <LI>Mascara: Un buffer de NxN aplicado sobre la zona a renderizar como una m?scara de 
52
 * <LI>Mascara: Un buffer de NxN aplicado sobre la zona a renderizar como una m?scara de
53 53
 * transparencia. Las bandas alpha de las imagenes se comportan de esta forma.</LI>
54 54
 * <LI>Opacidad global: Un valor de opacidad se aplicado a cada pixel a renderizar. Este es
55 55
 * igual para todos los p?xeles.</LI>
......
78 78
	protected IBuffer     originalData       = null;
79 79
	/**
80 80
	 * Valor de dato transparente. Todos los p?xeles de originalData que correspondan con este
81
	 * valor se pondr?n 100% transparentes. 
81
	 * valor se pondr?n 100% transparentes.
82 82
	 */
83 83
	protected double      noData             = RasterLibrary.defaultNoDataValue;
84 84
	/**
......
93 93
	 * Grado de opacidad de todo el raster
94 94
	 */
95 95
	protected int         opacity            = 0xff;
96
	
96

  
97 97
	/**
98 98
	 * Array de listeners que ser?n informados cuando cambia la propiedad de transparencia
99 99
	 */
100 100
	private ArrayList     transparencyPropertyListener = new ArrayList();
101
	
101

  
102 102
	/**
103 103
	 * Constructor
104 104
	 */
......
118 118
		this.noDataActive = t.isNoDataActive();
119 119
		this.originalData = t.originalData;
120 120
	}
121
	
121

  
122 122
	/**
123 123
	 * Asigna un listener a la lista que ser? informado cuando cambie una
124
	 * propiedad visual en la renderizaci?n. 
124
	 * propiedad visual en la renderizaci?n.
125 125
	 * @param listener VisualPropertyListener
126 126
	 */
127 127
	public void addPropertyListener(PropertyListener listener) {
128 128
		transparencyPropertyListener.add(listener);
129 129
	}
130
	
130

  
131 131
	/**
132 132
	 * M?todo llamado cuando hay un cambio en una propiedad de transparencia
133 133
	 */
......
155 155
			mask = b;
156 156
			callPropertyChanged(this);
157 157
			return;
158
		} else {		
158
		} else {
159 159
			mask = b;
160 160
			if(b != null)
161 161
				callPropertyChanged(this);
162 162
		}
163 163
	}
164
	
164

  
165 165
	/**
166 166
	 * Obtiene la informaci?n de si existe o no banda de transparencia cuando este
167 167
	 * objeto va asociado a un dataset. Si tiene este tipo de banda en cada
......
173 173
		return (mask != null);
174 174
	}
175 175

  
176
	
176

  
177 177
	/**
178 178
	 * Obtiene el ?rea de datos
179 179
	 * @return M?scara de transparencia
......
198 198
	public boolean isNoDataActive() {
199 199
		return noDataActive;
200 200
	}
201
	
201

  
202 202
	/**
203
	 * Obtiene el valor noData 
203
	 * Obtiene el valor noData
204 204
	 * @return
205 205
	 */
206 206
	public double getNoData() {
207 207
		return noData;
208 208
	}
209
	
209

  
210 210
	/**
211 211
	 * Asigna el valor noData
212 212
	 * @param noData
......
217 217
			callPropertyChanged(this);
218 218
		this.noData = noData;
219 219
	}
220
	
220

  
221 221
	/**
222 222
	 * Activa o desactiva el uso de noData como transparencia
223 223
	 * @param active
......
285 285
	public void setTransparencyByPixelFromMetadata(DatasetMetadata metadata){
286 286
		if (metadata != null) {
287 287
			TransparencyRange[] noData = metadata.parserNodataInMetadata();
288
			if (noData != null) {
288
			if (noData != null)
289 289
				for (int i = 0; i < noData.length; i++)
290 290
					getTransparencyRange().add(noData[i]);
291
			}
292 291
			TransparencyRange noDataValue = metadata.parserNodataByBand();
293 292
			if (noData == null && noDataValue != null)
294 293
				getTransparencyRange().add(noDataValue);
......
302 301
	 * @param dst
303 302
	 */
304 303
	public void mergeBuffer(IBuffer buffer, IBuffer dst) {
305
		for (int y = 0; y < mask.getHeight(); y++) {
306
			for (int x = 0; x < mask.getWidth(); x++) {
304
		for (int y = 0; y < mask.getHeight(); y++)
305
			for (int x = 0; x < mask.getWidth(); x++)
307 306
				// ((a / 255) * (b / 255)) * 255
308 307
				// Es lo mismo que:
309 308
				// (a * b) / 255
310 309
				dst.setElem(y, x, 0,
311 310
					(byte) (((mask.getElemByte(y, x, 0) & 0xff) * (buffer.getElemByte(y, x, 0) & 0xff)) / 255D));
312
			}
313
		}
314 311
	}
315 312

  
316 313
	/**
317
	 * Mezcla dos buffers de transparencia en uno solo. 
314
	 * Mezcla dos buffers de transparencia en uno solo.
318 315
	 * @param dst Buffer destino de la mezcla
319 316
	 * @param buf Buffer a mezclar sobre el destino
320 317
	 * @return Buffer destino
......
327 324
		if(buf != null && dst != null) {
328 325
			if(dst.getWidth() != buf.getWidth() || dst.getHeight() != buf.getHeight())
329 326
				return null;
330
			for (int y = 0; y < buf.getHeight(); y++) {
331
				for (int x = 0; x < buf.getWidth(); x++) {
327
			for (int y = 0; y < buf.getHeight(); y++)
328
				for (int x = 0; x < buf.getWidth(); x++)
332 329
					dst.setElem(y, x, 0,
333 330
							(byte) (((dst.getElemByte(y, x, 0) & 0xff) * (buf.getElemByte(y, x, 0) & 0xff)) / 255D));
334
				}
335
			}
336 331
			return dst;
337 332
		}
338 333
		return null;
339 334
	}
340
	
335

  
341 336
	/**
342 337
	 * Mezcla un objeto Transparency con el actual
343 338
	 * @param ts objeto TransparencyStatus
......
361 356
			mergeBuffer(transp.getAlphaBand(), newMask);
362 357

  
363 358
			t.setAlphaBand(newMask);
359
		} else if (mask != null) {
360
			t.setAlphaBand(mask);
361
			t.alphaBandNumber = alphaBandNumber;
364 362
		} else {
365
			if (mask != null) {
366
				t.setAlphaBand(mask);
367
				t.alphaBandNumber = alphaBandNumber;
368
			} else {
369
				t.setAlphaBand(transp.getAlphaBand());
370
				t.alphaBandNumber = transp.alphaBandNumber;
371
			}
363
			t.setAlphaBand(transp.getAlphaBand());
364
			t.alphaBandNumber = transp.alphaBandNumber;
372 365
		}
373 366

  
374 367
		// TODO: FUNCIONALIDAD Mezclamos las ?reas
......
395 388
	public void setTransparencyBand(int alphaBandNumber) {
396 389
		this.alphaBandNumber = alphaBandNumber;
397 390
	}
398
	
391

  
399 392
	/**
400 393
	 * Consulta si el valor de la posici?n (line, col) del buffer es considerado
401 394
	 * NoData o no.
......
406 399
	protected boolean isNoData(int line, int col) {
407 400
		switch (originalData.getDataType()) {
408 401
		case IBuffer.TYPE_BYTE:
409
			if(((double)originalData.getElemByte(line, col, 0)) == noData)
402
			if((originalData.getElemByte(line, col, 0)) == noData)
410 403
				return true;
411 404
			break;
412 405
		case IBuffer.TYPE_SHORT:
413
			if(((double)originalData.getElemShort(line, col, 0)) == noData)
406
			if((originalData.getElemShort(line, col, 0)) == noData)
414 407
				return true;
415 408
			break;
416 409
		case IBuffer.TYPE_INT:
417
			if(((double)originalData.getElemInt(line, col, 0)) == noData)
410
			if((originalData.getElemInt(line, col, 0)) == noData)
418 411
				return true;
419 412
			break;
420 413
		case IBuffer.TYPE_FLOAT:
421
			if(((double)originalData.getElemFloat(line, col, 0)) == noData)
414
			if((originalData.getElemFloat(line, col, 0)) == noData)
422 415
				return true;
423 416
			break;
424 417
		case IBuffer.TYPE_DOUBLE:
425
			if(((double)originalData.getElemDouble(line, col, 0)) == noData)
418
			if((originalData.getElemDouble(line, col, 0)) == noData)
426 419
				return true;
427 420
			break;
428 421
		}
429 422
		return false;
430 423
	}
431
	
424

  
432 425
	/**
433 426
	 * Pone a null los buffers de datos y pasa el garbage collector para liberar la memoria.
434 427
	 * Despu?s de renderizar es conveniente hacer esto porque el objeto Transparency asociado a
435 428
	 * un dataset no se destruye hasta que no se cierra este. Esto hace que esta memoria este
436
	 * siempre ocupada. 
429
	 * siempre ocupada.
437 430
	 */
438 431
	public void free() {
432
		if (mask != null)
433
			mask.free();
434
		if (originalData != null)
435
			originalData.free();
439 436
		mask = null;
440 437
		originalData = null;
441
		System.gc();
442 438
	}
443 439
}

Also available in: Unified diff