Revision 2308 org.gvsig.raster/branches/org.gvsig.raster_dataaccess_refactoring/org.gvsig.raster.lib/org.gvsig.raster.lib.impl/src/main/java/org/gvsig/raster/impl/store/properties/DataStoreTransparency.java

View differences:

DataStoreTransparency.java
27 27
import org.gvsig.fmap.dal.coverage.dataset.Buffer;
28 28
import org.gvsig.fmap.dal.coverage.datastruct.NoData;
29 29
import org.gvsig.fmap.dal.coverage.datastruct.TransparencyRange;
30
import org.gvsig.fmap.dal.coverage.store.props.ColorInterpretation;
30 31
import org.gvsig.fmap.dal.coverage.store.props.Transparency;
31 32
import org.gvsig.fmap.dal.coverage.util.PropertyEvent;
32 33
import org.gvsig.fmap.dal.coverage.util.PropertyListener;
33
import org.gvsig.raster.impl.DefaultRasterManager;
34 34
import org.gvsig.tools.ToolsLocator;
35 35
import org.gvsig.tools.dynobject.DynStruct;
36 36
import org.gvsig.tools.persistence.PersistenceManager;
......
71 71
 * </UL>
72 72
 * </p>
73 73
 *
74
 * @version 07/06/2007
75 74
 * @author Nacho Brodin (nachobrodin@gmail.com)
76 75
 */
77 76
public class DataStoreTransparency implements Transparency, Persistent {
78 77
	public static final String PERSISTENCE_NAME = "Transparency_Persistent";
79 78

  
80 79
	public static int     MAX_OPACITY           = 255;
81
	protected int         alphaBandNumber       = -1;
80

  
82 81
	/**
83
	 * Buffer con la banda alpha correspondiente a la zona a renderizar
82
	 * Alpha source
84 83
	 */
85
	private Buffer       mask                   = null;
84
	protected ColorInterpretation
85
	                      colorInterpretation   = null;
86 86
	/**
87 87
	 * Buffer con los datos originales (sin filtrar) correspondiente a la zona a renderizar.
88 88
	 * Esto es util para aplicar el valor NoData ya que hay que consultar el valor original del
......
118 118
	/**
119 119
	 * Constructor
120 120
	 */
121
	public DataStoreTransparency() {
121
	public DataStoreTransparency(ColorInterpretation colorInterpretation) {
122
		this.colorInterpretation = colorInterpretation;
122 123
	}
123

  
124
	/**
125
	 * Constructor de copia
126
	 */
127
	@SuppressWarnings("unchecked")
128
	public DataStoreTransparency(Transparency tParam) {
129
		DataStoreTransparency t = null;
130
		if(tParam instanceof DataStoreTransparency)
131
			t = (DataStoreTransparency)tParam;
132
		else 
133
			return;
134
		
135
		//TODO: FUNCIONALIDAD: Falta asignar lo necesario para la transparencia por selecci?n
136
		this.transparencyRanges = (List<TransparencyRange>) ((ArrayList<TransparencyRange>)t.getTransparencyRange()).clone();
137
		if(this.mask != null)
138
			this.mask = t.getAlphaBand();
139
		this.opacity = t.getOpacity();
140
		this.alphaBandNumber = t.alphaBandNumber;
141
		this.noData = (NoData)t.getNoData().clone();
142
		if(this.originalData != null)
143
			this.originalData = t.originalData;
144
		this.transparencyActive = t.transparencyActive;
124
	
125
	public static void mergeBuffers(Buffer src1, Buffer src2) {
126
		for (int y = 0; y < src1.getHeight(); y++)
127
			for (int x = 0; x < src1.getWidth(); x++)
128
				// ((a / 255) * (b / 255)) * 255
129
				// Es lo mismo que:
130
				// (a * b) / 255
131
				src1.setElem(y, x, 0,
132
						(byte) (((src1.getElemByte(y, x, 0) & 0xff) * (src2.getElemByte(y, x, 0) & 0xff)) / 255D));
145 133
	}
146 134

  
147
	/*
148
	 * (non-Javadoc)
149
	 * @see org.gvsig.fmap.dal.coverage.store.props.Transparency#addPropertyListener(PropertyListener)
150
	 */
151 135
	public void addPropertyListener(PropertyListener listener) {
152 136
		transparencyPropertyListener.add(listener);
153 137
	}
......
162 146
		}
163 147
	}
164 148

  
165
	/**
166
	 * Obtiene la m?scara asociada
167
	 * @return M?scara de transparencia
168
	 */
169
	public Buffer getAlphaBand() {
170
		return mask;
171
	}
172

  
173
	public void setAlphaBand(Buffer b) {
174
		if(b == null && mask != null) {
175
			mask = b;
176
			//callPropertyChanged(this);
177
			return;
178
		} else {
179
			mask = b;
180
			//if(b != null)
181
				//callPropertyChanged(this);
182
		}
183
	}
184

  
185 149
	public boolean existAlphaBand() {
186
		return (mask != null);
150
		return getAlphaBandNumber() >= 0;
187 151
	}
188 152

  
189

  
190 153
	/**
191 154
	 * Obtiene el ?rea de datos
192 155
	 * @return M?scara de transparencia
......
271 234
	}
272 235

  
273 236
	/**
274
	 * Mezcla el alpha actual con el que nos pasan por parametro y se asigna
275
	 * directamente a destino
276
	 * @param buffer
277
	 * @param dst
278
	 */
279
	public void mergeBuffer(Buffer buffer, Buffer dst) {
280
		for (int y = 0; y < mask.getHeight(); y++)
281
			for (int x = 0; x < mask.getWidth(); x++)
282
				// ((a / 255) * (b / 255)) * 255
283
				// Es lo mismo que:
284
				// (a * b) / 255
285
				dst.setElem(y, x, 0,
286
						(byte) (((mask.getElemByte(y, x, 0) & 0xff) * (buffer.getElemByte(y, x, 0) & 0xff)) / 255D));
287
	}
288

  
289
	/**
290
	 * Mezcla un objeto Transparency con el actual
291
	 * @param ts objeto TransparencyStatus
292
	 */
293
	public Transparency merge(Transparency tParam) {
294
		DataStoreTransparency transp = null;
295
		if(tParam instanceof DataStoreTransparency)
296
			transp = (DataStoreTransparency)tParam;
297
		else 
298
			return null;
299
		
300
		DataStoreTransparency t = new DataStoreTransparency();
301
		// Mezclamos la opacidad
302
		double op1 = (double) opacity / (double) MAX_OPACITY;
303
		double op2 = (double) transp.getOpacity() / (double) MAX_OPACITY;
304
		t.setOpacity((int) (op1 * op2 * MAX_OPACITY));
305

  
306
		// Mezclamos los rangos de transparencia
307
		List<TransparencyRange> tr = transp.getTransparencyRange();
308
		for (int i = 0; i < tr.size(); i++)
309
			transparencyRanges.add(tr.get(i));
310

  
311
		// TODO: FUNCIONALIDAD Mezclamos la m?scara
312
		if (mask != null && transp.getAlphaBand() != null) {
313
			Buffer newMask = DefaultRasterManager.getInstance().createBuffer(Buffer.TYPE_BYTE, mask.getWidth(), mask.getHeight(), 1, true);
314
			// Mezclamos alphaBand con el que nos pasan en transp y lo asignamos al nuevo buffer
315
			mergeBuffer(transp.getAlphaBand(), newMask);
316

  
317
			t.setAlphaBand(newMask);
318
		} else if (mask != null) {
319
			t.setAlphaBand(mask);
320
			t.alphaBandNumber = alphaBandNumber;
321
		} else {
322
			t.setAlphaBand(transp.getAlphaBand());
323
			t.alphaBandNumber = transp.alphaBandNumber;
324
		}
325

  
326
		// TODO: FUNCIONALIDAD Mezclamos las ?reas
327

  
328
		// TODO: FUNCIONALIDAD Mezclamos las mascaras
329
		return t;
330
	}
331

  
332
	/**
333 237
	 * Obtiene la banda de transpareci si existe o -1 si no existe.
334 238
	 * @return n?mero de banda de transparencia o -1 si no existe.
335 239
	 */
336 240
	public int getAlphaBandNumber() {
337
		return alphaBandNumber;
241
		return colorInterpretation.getAlphaBand();
338 242
	}
339 243

  
340 244
	/**
......
345 249
	 * @param true si existe banda de transparencia y false si no lo es.
346 250
	 */
347 251
	public void setTransparencyBand(int alphaBandNumber) {
348
		this.alphaBandNumber = alphaBandNumber;
252
		colorInterpretation.setColorInterpValue(alphaBandNumber, ColorInterpretation.ALPHA_BAND);
349 253
	}
350 254

  
351 255
	/**
......
413 317
		this.transparencyActive = transparencyActive;
414 318
	}
415 319
	
416
	public int processRGB(int r, int g, int b, int line, int col) {
320
	public int processRGB(int r, int g, int b, int line, int col, Buffer buf) {
417 321
		// Si el valor es noData se pone totalmente transparente y ya no se tiene en
418 322
		// cuenta nada m?s.
419 323
		if (originalData != null && (getNoData().isDefined() && getNoData().isNoDataTransparent())) {
......
439 343
		if (alphaRange != 255)
440 344
			a *= (alphaRange / 255D);
441 345

  
442
		if (existAlphaBand() && getAlphaBand() != null)
443
			a *= (getAlphaBand().getElemByte(line, col, 0) & 0xff) / 255D;
346
		if (existAlphaBand() && buf != null)
347
			a *= (buf.getElemByte(line, col, getAlphaBandNumber()) & 0xff) / 255D;
444 348

  
445 349
		// Quitada la multiplicacion para optimizar
446 350
		// a = (int)(a * 255D);
......
492 396
		return 255;
493 397
	}
494 398
	
399
	public void setColorInterpretation(ColorInterpretation colorInterpretation) {
400
		this.colorInterpretation = colorInterpretation;
401
	}
402
	
495 403
	@SuppressWarnings("unchecked")
496 404
	public Transparency cloneTransparency() {
497
		DataStoreTransparency t = new DataStoreTransparency();
405
		DataStoreTransparency t = new DataStoreTransparency(this.colorInterpretation.cloneColorInterpretation());
498 406
		
499 407
		//TODO: FUNCIONALIDAD: Falta asignar lo necesario para la transparencia por selecci?n
500 408
		t.transparencyRanges = (ArrayList<TransparencyRange>)((ArrayList<TransparencyRange>) getTransparencyRange()).clone();
501
		t.mask = getAlphaBand();
409
		//t.mask = getAlphaBand();
502 410
		t.opacity = getOpacity();
503
		t.alphaBandNumber = alphaBandNumber;
504 411
		t.noData = getNoData();
505 412
		t.originalData = originalData;
506
		t.transparencyActive = t.transparencyActive;
413
		t.transparencyActive = transparencyActive;
507 414
		t.activeTransparency();
508 415
		return t;
509 416
	}
......
519 426
		if(tr != null) {
520 427
			transparencyRanges = new ArrayList<TransparencyRange>(tr);	
521 428
			if (state.hasValue("bandnumber")) {
522
				alphaBandNumber = state.getInt("bandnumber");
429
				int alphaBandNumber = state.getInt("bandnumber");
430
				setTransparencyBand(alphaBandNumber);
523 431
			}
524 432
		}
525 433

  
......
560 468
	}
561 469
	
562 470
	public void dispose() {
563
		if (mask != null)
564
			mask.dispose();
565 471
		if (originalData != null)
566 472
			originalData.dispose();
567
		mask = null;
568 473
		originalData = null;
569 474
	}
570 475
	
476
	public ColorInterpretation getColorInterpretation() {
477
		return colorInterpretation;
478
	}
479
	
571 480
	protected void finalize() throws Throwable {
572 481
		noData           = null;
573
		mask             = null;
574 482
		if(transparencyPropertyListener != null) {
575 483
			transparencyPropertyListener.clear();
576 484
			transparencyPropertyListener = null;

Also available in: Unified diff