Revision 11183 trunk/libraries/libRaster/src/org/gvsig/raster/buffer/RasterBuffer.java

View differences:

RasterBuffer.java
180 180
		this.dataType = dataType;
181 181
	}
182 182

  
183
    /**
184
     * Obtiene el tama?o del tipo de dato en bytes
185
     * @return Tipo de dato
186
     */
187
    public int getDataSize() {
188
        if (dataType == TYPE_BYTE) {
189
            return 1;
190
        } else if ((dataType == TYPE_SHORT) | (dataType == TYPE_USHORT)) {
191
            return 2;
192
        } else if (dataType == TYPE_INT) {
193
            return 4;
194
        }else if (dataType == TYPE_FLOAT) {
195
            return 8;
196
        }else if (dataType == TYPE_DOUBLE) {
197
            return 16;
198
        }
183
  /**
184
   * Obtiene el tama?o del tipo de dato en bytes
185
   * @return Tipo de dato
186
   */
187
  public int getDataSize() {
188
      if (dataType == TYPE_BYTE) {
189
          return 1;
190
      } else if ((dataType == TYPE_SHORT) | (dataType == TYPE_USHORT)) {
191
          return 2;
192
      } else if (dataType == TYPE_INT) {
193
          return 4;
194
      }else if (dataType == TYPE_FLOAT) {
195
          return 8;
196
      }else if (dataType == TYPE_DOUBLE) {
197
          return 16;
198
      }
199 199

  
200
        return 0;
201
    }
200
      return 0;
201
  }
202 202

  
203
    /**
204
     * Obtiene el tama?o del buffer
205
     * @return tama?o del buffer
206
     */
207
    public long sizeof() {
208
        return getDataSize() * width * height * nBands;
209
    }
210
    
211
    /**
212
     * Replica la banda de una posici?n sobre otra. Si la banda de destino no existe
213
     * se crea nueva. Si la posici?n de la banda de destino est? intercalada entre bandas 
214
     * que ya existen las otras se desplazan hacia abajo, NO se machacan los datos de ninguna.
215
     * Los datos se replican por referencia por lo que al modificar la banda original las
216
     * del resto quedar?n afectadas.
217
     * @param orig. Posici?n de la banda de origen. 
218
     * @param dest. Posici?n de la banda destino
219
     */   
220
    public abstract void replicateBand(int orig, int dest);
221
    
222
    /**
223
     * Cambia bandas de posici?n. Las posiciones deben existir como bandas del raster. 
224
     * Cada elemento del array representa una banda existente en el buffer (de longitud
225
     * rasterBuf.length) y el valor contenido dentro la banda que le corresponde. Por ejemplo
226
     * si pasamos un array {1, 0, 3, 2} significa que el buffer tiene cuatro bandas y que 
227
     * cambiamos la 0 por la 1 y la 2 por la 3. Un array {0, 1, 2, 3} en el mismo 
228
     * caso no producir?a nig?n cambio.
229
     * 
230
     * Si quisieramos asignar en un buffer monobanda su banda a la segunda posici?n habria
231
     * que insertar una vacia, por ejemplo con addBandFloat(0, null) se insertaria una 
232
     * banda nula en la posici?n 0 y la banda que estaba en la 0 pasar?a a la segunda.
233
     * 
234
     */
235
    public abstract void switchBands(int[] bandPosition);
236
    	
237
    /**
238
     * Convierte un tipo de dato a cadena
239
     * @param type Tipo de dato
240
     * @return cadena  que representa el tipo de dato
241
     */
242
    public static String typesToString(int type) {
243
        switch (type) {
244
        case RasterBuffer.TYPE_IMAGE:
245
            return new String("Image");
203
  /**
204
   * Obtiene el tama?o del buffer
205
   * @return tama?o del buffer
206
   */
207
  public long sizeof() {
208
      return getDataSize() * width * height * nBands;
209
  }
210
  
211
  /**
212
   * Replica la banda de una posici?n sobre otra. Si la banda de destino no existe
213
   * se crea nueva. Si la posici?n de la banda de destino est? intercalada entre bandas 
214
   * que ya existen las otras se desplazan hacia abajo, NO se machacan los datos de ninguna.
215
   * Los datos se replican por referencia por lo que al modificar la banda original las
216
   * del resto quedar?n afectadas.
217
   * @param orig. Posici?n de la banda de origen. 
218
   * @param dest. Posici?n de la banda destino
219
   */   
220
  public abstract void replicateBand(int orig, int dest);
221
  
222
  /**
223
   * Cambia bandas de posici?n. Las posiciones deben existir como bandas del raster. 
224
   * Cada elemento del array representa una banda existente en el buffer (de longitud
225
   * rasterBuf.length) y el valor contenido dentro la banda que le corresponde. Por ejemplo
226
   * si pasamos un array {1, 0, 3, 2} significa que el buffer tiene cuatro bandas y que 
227
   * cambiamos la 0 por la 1 y la 2 por la 3. Un array {0, 1, 2, 3} en el mismo 
228
   * caso no producir?a nig?n cambio.
229
   * 
230
   * Si quisieramos asignar en un buffer monobanda su banda a la segunda posici?n habria
231
   * que insertar una vacia, por ejemplo con addBandFloat(0, null) se insertaria una 
232
   * banda nula en la posici?n 0 y la banda que estaba en la 0 pasar?a a la segunda.
233
   * 
234
   */
235
  public abstract void switchBands(int[] bandPosition);
236
  	
237
  /**
238
   * Convierte un tipo de dato a cadena
239
   * @param type Tipo de dato
240
   * @return cadena  que representa el tipo de dato
241
   */
242
  public static String typesToString(int type) {
243
      switch (type) {
244
      case RasterBuffer.TYPE_IMAGE:
245
          return new String("Image");
246 246

  
247
        case RasterBuffer.TYPE_BYTE:
248
            return new String("Byte");
247
      case RasterBuffer.TYPE_BYTE:
248
          return new String("Byte");
249 249

  
250
        case RasterBuffer.TYPE_DOUBLE:
251
            return new String("Double");
250
      case RasterBuffer.TYPE_DOUBLE:
251
          return new String("Double");
252 252

  
253
        case RasterBuffer.TYPE_FLOAT:
254
            return new String("Float");
253
      case RasterBuffer.TYPE_FLOAT:
254
          return new String("Float");
255 255

  
256
        case RasterBuffer.TYPE_INT:
257
        	return new String("Integer");
258
        	
259
        case RasterBuffer.TYPE_USHORT:
260
        case RasterBuffer.TYPE_SHORT:
261
            return new String("Short");
262
        }
256
      case RasterBuffer.TYPE_INT:
257
      	return new String("Integer");
258
      	
259
      case RasterBuffer.TYPE_USHORT:
260
      case RasterBuffer.TYPE_SHORT:
261
          return new String("Short");
262
      }
263 263

  
264
        return null;
265
    }
266
    
267
    /**
268
     * Ajusta el raster al ancho y alto solicitado por el vecino m?s cercano. Promedia el valor de dos
269
     * pixeles contiguos.
270
     * @param w Nuevo ancho
271
     * @param h Nuevo alto
272
     */
273
    public abstract RasterBuffer adjustRasterNearestNeighbourInterpolation(int w, int h, int[] bands);
274
    
275
    /**
276
     * Ajusta el raster al ancho y alto solicitado ajustando con una interpolaci?n bilineal. Promedia
277
     * el valor de cuatro pixeles adyacentes.
278
     * @param w Nuevo ancho
279
     * @param h Nuevo alto
280
     */
281
    public abstract RasterBuffer adjustRasterBilinearInterpolation(int w, int h, int[] bands);
282
    
283
    /**
284
     * Ajusta el raster al ancho y alto solicitado ajustando con una interpolaci?n de distancia inversa.
285
     * Asigna el valor de un pixel en funci?n inversa de la distancia.
286
     * 
287
     * @param w Nuevo ancho
288
     * @param h Nuevo alto
289
     */
290
    public abstract RasterBuffer adjustRasterInverseDistanceInterpolation(int w, int h, int[] bands);
291
    
292
    /**
293
     * Ajusta el raster al ancho y alto solicitado ajustando con una interpolaci?n de spline bicubica.
294
     * @param w Nuevo ancho
295
     * @param h Nuevo alto
296
     */
297
    public abstract RasterBuffer adjustRasterBicubicSplineInterpolation(int w, int h, int[] bands);
298
    
299
    /**
300
     * Ajusta el raster al ancho y alto solicitado ajustando con una interpolaci?n BSpline.
301
     * @param w Nuevo ancho
302
     * @param h Nuevo alto
303
     */
304
    public abstract RasterBuffer adjustRasterBSplineInterpolation(int w, int h, int[] bands);
305
    
306
    /*
307
     *  (non-Javadoc)
308
     * @see org.gvsig.fmap.driver.IBuffer#getNoDataValue()
309
     */
310
    public double getNoDataValue(){
311
    	return noDataValue;
312
    }
313
    
314
    /*
315
     *  (non-Javadoc)
316
     * @see org.gvsig.fmap.driver.IBuffer#getByteNoDataValue()
317
     */
318
    public byte getByteNoDataValue(){
319
    	return (byte)noDataValue;
320
    }
321
    
322
    /*
323
     *  (non-Javadoc)
324
     * @see org.gvsig.fmap.driver.IBuffer#getShortNoDataValue()
325
     */
326
    public short getShortNoDataValue(){
327
    	return (short)noDataValue;
328
    }
329
    
330
    /*
331
     *  (non-Javadoc)
332
     * @see org.gvsig.fmap.driver.IBuffer#getIntNoDataValue()
333
     */
334
    public int getIntNoDataValue(){
335
    	return (int)noDataValue;
336
    }
337
    
338
    /*
339
     *  (non-Javadoc)
340
     * @see org.gvsig.fmap.driver.IBuffer#getFloatNoDataValue()
341
     */
342
    public float getFloatNoDataValue(){
343
    	return (float)noDataValue;
344
    }
345
    
346
    /*
347
     *  (non-Javadoc)
348
     * @see org.gvsig.fmap.driver.IBuffer#setNoDataValue(double)
349
     */
350
    public void setNoDataValue(double nd){
351
    	noDataValue = nd;
352
    }
353
    
354
    /*
355
     *  (non-Javadoc)
356
     * @see org.gvsig.fmap.driver.IBuffer#getNotValidValue()
357
     */
358
    public double getNotValidValue(){
359
    	return notValidValue;
360
    }
361
    
362
    /*
363
	 *  (non-Javadoc)
364
	 * @see org.gvsig.fmap.driver.IBuffer#setNotValidValue(java.lang.Object)
365
	 */
366
    public void setNotValidValue(double value){
367
    	this.notValidValue = value;
368
    }
369
    
370
    /*
371
     *  (non-Javadoc)
372
     * @see org.gvsig.fmap.driver.IBuffer#cloneBuffer()
373
     */
374
    public abstract IBuffer cloneBuffer();
375
    
376
    /*
377
     * (non-Javadoc)
378
     * @see org.gvsig.raster.driver.datasetproperties.IHistogramable#getHistogram()
379
     */
264
      return null;
265
  }
266
  
267
  /**
268
   * Ajusta el raster al ancho y alto solicitado por el vecino m?s cercano. Promedia el valor de dos
269
   * pixeles contiguos.
270
   * @param w Nuevo ancho
271
   * @param h Nuevo alto
272
   */
273
  public abstract RasterBuffer adjustRasterNearestNeighbourInterpolation(int w, int h, int[] bands);
274
  
275
  /**
276
   * Ajusta el raster al ancho y alto solicitado ajustando con una interpolaci?n bilineal. Promedia
277
   * el valor de cuatro pixeles adyacentes.
278
   * @param w Nuevo ancho
279
   * @param h Nuevo alto
280
   */
281
  public abstract RasterBuffer adjustRasterBilinearInterpolation(int w, int h, int[] bands);
282
  
283
  /**
284
   * Ajusta el raster al ancho y alto solicitado ajustando con una interpolaci?n de distancia inversa.
285
   * Asigna el valor de un pixel en funci?n inversa de la distancia.
286
   * 
287
   * @param w Nuevo ancho
288
   * @param h Nuevo alto
289
   */
290
  public abstract RasterBuffer adjustRasterInverseDistanceInterpolation(int w, int h, int[] bands);
291
  
292
  /**
293
   * Ajusta el raster al ancho y alto solicitado ajustando con una interpolaci?n de spline bicubica.
294
   * @param w Nuevo ancho
295
   * @param h Nuevo alto
296
   */
297
  public abstract RasterBuffer adjustRasterBicubicSplineInterpolation(int w, int h, int[] bands);
298
  
299
  /**
300
   * Ajusta el raster al ancho y alto solicitado ajustando con una interpolaci?n BSpline.
301
   * @param w Nuevo ancho
302
   * @param h Nuevo alto
303
   */
304
  public abstract RasterBuffer adjustRasterBSplineInterpolation(int w, int h, int[] bands);
305
  
306
  /*
307
   *  (non-Javadoc)
308
   * @see org.gvsig.fmap.driver.IBuffer#getNoDataValue()
309
   */
310
  public double getNoDataValue(){
311
  	return noDataValue;
312
  }
313
  
314
  /*
315
   *  (non-Javadoc)
316
   * @see org.gvsig.fmap.driver.IBuffer#getByteNoDataValue()
317
   */
318
  public byte getByteNoDataValue(){
319
  	return (byte)noDataValue;
320
  }
321
  
322
  /*
323
   *  (non-Javadoc)
324
   * @see org.gvsig.fmap.driver.IBuffer#getShortNoDataValue()
325
   */
326
  public short getShortNoDataValue(){
327
  	return (short)noDataValue;
328
  }
329
  
330
  /*
331
   *  (non-Javadoc)
332
   * @see org.gvsig.fmap.driver.IBuffer#getIntNoDataValue()
333
   */
334
  public int getIntNoDataValue(){
335
  	return (int)noDataValue;
336
  }
337
  
338
  /*
339
   *  (non-Javadoc)
340
   * @see org.gvsig.fmap.driver.IBuffer#getFloatNoDataValue()
341
   */
342
  public float getFloatNoDataValue(){
343
  	return (float)noDataValue;
344
  }
345
  
346
  /*
347
   *  (non-Javadoc)
348
   * @see org.gvsig.fmap.driver.IBuffer#setNoDataValue(double)
349
   */
350
  public void setNoDataValue(double nd){
351
  	noDataValue = nd;
352
  }
353
  
354
  /*
355
   *  (non-Javadoc)
356
   * @see org.gvsig.fmap.driver.IBuffer#getNotValidValue()
357
   */
358
  public double getNotValidValue(){
359
  	return notValidValue;
360
  }
361
  
362
  /*
363
   *  (non-Javadoc)
364
   * @see org.gvsig.fmap.driver.IBuffer#setNotValidValue(java.lang.Object)
365
   */
366
  public void setNotValidValue(double value){
367
  	this.notValidValue = value;
368
  }
369
  
370
  /*
371
   *  (non-Javadoc)
372
   * @see org.gvsig.fmap.driver.IBuffer#cloneBuffer()
373
   */
374
  public abstract IBuffer cloneBuffer();
375
  
376
  /**
377
   * Calcula el m?nimo y el m?ximo del histograma previamente.
378
   * @return double[] con el m?nimo y el m?ximo.
379
   */
380
  private double[] getLimits() {
381
  	double max = Double.MIN_VALUE;
382
  	double min = Double.MAX_VALUE;
383
  	double value = 0;
384

  
385
		switch (getDataType()) {
386
			case IBuffer.TYPE_BYTE:
387
				for (int i = 0; i < getBandCount(); i++)
388
					for (int r = 0; r < getHeight(); r++)
389
						for (int c = 0; c < getWidth(); c++) {
390
							value = (double) (getElemByte(r, c, i) & 0xff);
391
							if (value > max) max = value;
392
							if (value < min) min = value;
393
						}
394
				break;
395
			case IBuffer.TYPE_SHORT:
396
				for (int i = 0; i < getBandCount(); i++)
397
					for (int r = 0; r < getHeight(); r++)
398
						for (int c = 0; c < getWidth(); c++) {
399
							value = (double) (getElemShort(r, c, i) & 0xffff);
400
							if (value > max) max = value;
401
							if (value < min) min = value;
402
						}
403
				break;
404
			case IBuffer.TYPE_INT:
405
				for (int i = 0; i < getBandCount(); i++)
406
					for (int r = 0; r < getHeight(); r++)
407
						for (int c = 0; c < getWidth(); c++) {
408
							value = (double) (getElemInt(r, c, i) & 0xffffffff);
409
							if (value > max) max = value;
410
							if (value < min) min = value;
411
						}
412
				break;
413
			case IBuffer.TYPE_FLOAT:
414
				for (int i = 0; i < getBandCount(); i++)
415
					for (int r = 0; r < getHeight(); r++)
416
						for (int c = 0; c < getWidth(); c++) {
417
							value =  (double) getElemFloat(r, c, i);
418
							if (value > max) max = value;
419
							if (value < min) min = value;
420
						}
421
				break;
422
			case IBuffer.TYPE_DOUBLE:
423
				for (int i = 0; i < getBandCount(); i++)
424
					for (int r = 0; r < getHeight(); r++)
425
						for (int c = 0; c < getWidth(); c++) {
426
							value = getElemDouble(r, c, i);
427
							if (value > max) max = value;
428
							if (value < min) min = value;
429
						}
430
				break;
431
		}
432
		double[] values = new double[2];
433
		values[0] = min;
434
		values[1] = max;
435
  	return values;
436
  }
437

  
438
  /*
439
   * (non-Javadoc)
440
   * @see org.gvsig.raster.driver.datasetproperties.IHistogramable#getHistogram()
441
   */
380 442
	public Histogram getHistogram() throws HistogramException {
381 443
		percent = 0;
382 444
		Histogram hist = null;
445
		double[] limits = getLimits();
446

  
383 447
		if (getDataType() == IBuffer.TYPE_BYTE)
384
			hist = new Histogram(getBandCount(), 255);
448
			hist = new Histogram(getBandCount(), 255, limits[0], limits[1]);
385 449
		else
386
			hist = new Histogram(getBandCount(), RasterLibrary.defaultNumberOfClasses);
450
			hist = new Histogram(getBandCount(), RasterLibrary.defaultNumberOfClasses, limits[0], limits[1]);
387 451
				
388 452
		for (int iBand = 0; iBand < getBandCount(); iBand++) {
389 453
			for (int row = 0; row < getHeight(); row++) {

Also available in: Unified diff