root / trunk / libraries / libRaster / src / org / gvsig / raster / buffer / cache / PageBuffer.java @ 11424
History | View | Annotate | Download (14.6 KB)
1 |
|
---|---|
2 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
3 |
*
|
4 |
* Copyright (C) 2006 IVER T.I. and Generalitat Valenciana.
|
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 |
package org.gvsig.raster.buffer.cache; |
21 |
|
22 |
import java.io.IOException; |
23 |
|
24 |
import org.gvsig.raster.RasterLibrary; |
25 |
import org.gvsig.raster.buffer.IBand; |
26 |
import org.gvsig.raster.dataset.IBuffer; |
27 |
import org.gvsig.raster.util.Histogram; |
28 |
import org.gvsig.raster.util.HistogramException; |
29 |
import org.gvsig.raster.util.IHistogramable; |
30 |
|
31 |
/**
|
32 |
* Esta clase representa una p?gina de cache.Cada p?gina de cach? est? compuesta por
|
33 |
* varios objetos PageBandBuffer donde cada uno es una p?gina que corresponde a una
|
34 |
* banda de raster cacheada.
|
35 |
*
|
36 |
* @author Nacho Brodin (nachobrodin@gmail.com)
|
37 |
*
|
38 |
*/
|
39 |
public class PageBuffer implements IBuffer { |
40 |
public static final int CANCEL_HISTOGRAM = IHistogramable.CANCEL_HISTOGRAM; |
41 |
private boolean[] cancel = new boolean[1]; |
42 |
|
43 |
private PageBandBuffer[] pageBandBuffer = null; |
44 |
|
45 |
private int percent = 0; |
46 |
|
47 |
/**
|
48 |
* Constructor
|
49 |
* @param dataType
|
50 |
* @param width
|
51 |
* @param height
|
52 |
* @param bandNr
|
53 |
* @param malloc
|
54 |
*/
|
55 |
public PageBuffer(int dataType, int width, int height, int bandNr, boolean malloc, int nHddPags){ |
56 |
pageBandBuffer = new PageBandBuffer[bandNr];
|
57 |
for (int i = 0; i < pageBandBuffer.length; i++) |
58 |
pageBandBuffer[i] = new PageBandBuffer(dataType, width, height, 1, malloc, i); |
59 |
} |
60 |
|
61 |
/*
|
62 |
* (non-Javadoc)
|
63 |
* @see org.gvsig.raster.driver.IBuffer#isBandSwitchable()
|
64 |
*/
|
65 |
public boolean isBandSwitchable(){ |
66 |
return false; |
67 |
} |
68 |
|
69 |
/**
|
70 |
* Asigna la lista de paginas de disco
|
71 |
* @param hddList Lista de p?ginas de disco
|
72 |
*/
|
73 |
public void setHddPages(HddPage[] hddList){ |
74 |
for (int i = 0; i < pageBandBuffer.length; i++) |
75 |
pageBandBuffer[i].setHddPages(hddList); |
76 |
} |
77 |
|
78 |
/**
|
79 |
* Carga una p?gina especificada en el par?metro nPag con los datos necesarios.
|
80 |
* Para esto recorre todas las bandas de la p?gina llamando al load de cada una.
|
81 |
*
|
82 |
* @param nPag N?mero de p?gina a cargar
|
83 |
*/
|
84 |
public void loadPage(int nPag){ |
85 |
for (int i = 0; i < pageBandBuffer.length; i++) |
86 |
pageBandBuffer[i].loadPage(nPag); |
87 |
} |
88 |
|
89 |
/**
|
90 |
* Salva una p?gina especificada en el par?metro nPag a disco.
|
91 |
* Para esto recorre todas las bandas de la p?gina llamando al save de cada una.
|
92 |
*
|
93 |
* @param nPag N?mero de p?gina a salvar
|
94 |
* @throws IOException
|
95 |
*/
|
96 |
public void savePage(int nPag) throws IOException{ |
97 |
for (int i = 0; i < pageBandBuffer.length; i++) |
98 |
pageBandBuffer[i].savePage(nPag); |
99 |
} |
100 |
|
101 |
public void assign(int band, byte value) { |
102 |
pageBandBuffer[band].assign(0, value);
|
103 |
} |
104 |
|
105 |
public void assign(int band, short value) { |
106 |
pageBandBuffer[band].assign(0, value);
|
107 |
} |
108 |
|
109 |
public void assign(int band, int value) { |
110 |
pageBandBuffer[band].assign(0, value);
|
111 |
} |
112 |
|
113 |
public void assign(int band, float value) { |
114 |
pageBandBuffer[band].assign(0, value);
|
115 |
} |
116 |
|
117 |
public void assign(int band, double value) { |
118 |
pageBandBuffer[band].assign(0, value);
|
119 |
} |
120 |
|
121 |
public void assignBand(int nBand, IBand band) { |
122 |
} |
123 |
|
124 |
public void assignBandToNotValid(int iBand) { |
125 |
} |
126 |
|
127 |
public IBuffer cloneBuffer() {
|
128 |
return null; |
129 |
} |
130 |
|
131 |
public void copyBand(int nBand, IBand band) { |
132 |
} |
133 |
|
134 |
public IBand createBand(byte defaultValue) { |
135 |
return null; |
136 |
} |
137 |
|
138 |
public IBand getBand(int nBand) { |
139 |
return null; |
140 |
} |
141 |
|
142 |
public IBuffer getBandBuffer(int nBand) { |
143 |
return null; |
144 |
} |
145 |
|
146 |
public int getBandCount() { |
147 |
return pageBandBuffer[0].getBandCount(); |
148 |
} |
149 |
|
150 |
public int getDataType() { |
151 |
return pageBandBuffer[0].getDataType(); |
152 |
} |
153 |
|
154 |
public void setDataType(int dataType) { |
155 |
|
156 |
} |
157 |
|
158 |
public int getHeight() { |
159 |
return pageBandBuffer[0].getHeight(); |
160 |
} |
161 |
|
162 |
public int getWidth() { |
163 |
return pageBandBuffer[0].getWidth(); |
164 |
} |
165 |
|
166 |
|
167 |
|
168 |
public byte getByteNoDataValue() { |
169 |
return pageBandBuffer[0].getByteNoDataValue(); |
170 |
} |
171 |
|
172 |
public float getFloatNoDataValue() { |
173 |
return pageBandBuffer[0].getFloatNoDataValue(); |
174 |
} |
175 |
|
176 |
public int getIntNoDataValue() { |
177 |
return pageBandBuffer[0].getIntNoDataValue(); |
178 |
} |
179 |
|
180 |
public double getNoDataValue() { |
181 |
return pageBandBuffer[0].getNoDataValue(); |
182 |
} |
183 |
|
184 |
public double getNotValidValue() { |
185 |
return pageBandBuffer[0].getNotValidValue(); |
186 |
} |
187 |
|
188 |
public short getShortNoDataValue() { |
189 |
return pageBandBuffer[0].getShortNoDataValue(); |
190 |
} |
191 |
|
192 |
|
193 |
|
194 |
public byte getElemByte(int line, int col, int band) { |
195 |
return pageBandBuffer[band].getElemByte(line, col, 0); |
196 |
} |
197 |
|
198 |
public short getElemShort(int line, int col, int band) { |
199 |
return pageBandBuffer[band].getElemShort(line, col, 0); |
200 |
} |
201 |
|
202 |
public int getElemInt(int line, int col, int band) { |
203 |
return pageBandBuffer[band].getElemInt(line, col, 0); |
204 |
} |
205 |
|
206 |
public float getElemFloat(int line, int col, int band) { |
207 |
return pageBandBuffer[band].getElemFloat(line, col, 0); |
208 |
} |
209 |
|
210 |
public double getElemDouble(int line, int col, int band) { |
211 |
return pageBandBuffer[band].getElemDouble(line, col, 0); |
212 |
} |
213 |
|
214 |
|
215 |
|
216 |
|
217 |
public void getElemByte(int line, int col, byte[] data) { |
218 |
for (int i = 0; i < pageBandBuffer.length; i++) |
219 |
data[i] = pageBandBuffer[i].getElemByte(line, col, 0);
|
220 |
} |
221 |
|
222 |
public void getElemShort(int line, int col, short[] data) { |
223 |
for (int i = 0; i < pageBandBuffer.length; i++) |
224 |
data[i] = pageBandBuffer[i].getElemShort(line, col, 0);
|
225 |
} |
226 |
|
227 |
public void getElemInt(int line, int col, int[] data) { |
228 |
for (int i = 0; i < pageBandBuffer.length; i++) |
229 |
data[i] = pageBandBuffer[i].getElemInt(line, col, 0);
|
230 |
} |
231 |
|
232 |
public void getElemFloat(int line, int col, float[] data) { |
233 |
for (int i = 0; i < pageBandBuffer.length; i++) |
234 |
data[i] = pageBandBuffer[i].getElemFloat(line, col, 0);
|
235 |
} |
236 |
|
237 |
public void getElemDouble(int line, int col, double[] data) { |
238 |
for (int i = 0; i < pageBandBuffer.length; i++) |
239 |
data[i] = pageBandBuffer[i].getElemDouble(line, col, 0);
|
240 |
} |
241 |
|
242 |
|
243 |
public byte[][] getLineByte(int line) { |
244 |
byte[][] b = new byte[pageBandBuffer.length][]; |
245 |
for (int i = 0; i < pageBandBuffer.length; i++) |
246 |
b[i] = pageBandBuffer[i].getLineByte(line)[0];
|
247 |
return b;
|
248 |
} |
249 |
|
250 |
public double[][] getLineDouble(int line) { |
251 |
double[][] d = new double[pageBandBuffer.length][]; |
252 |
for (int i = 0; i < pageBandBuffer.length; i++) |
253 |
d[i] = pageBandBuffer[i].getLineDouble(line)[0];
|
254 |
return d;
|
255 |
} |
256 |
|
257 |
public float[][] getLineFloat(int line) { |
258 |
float[][] f = new float[pageBandBuffer.length][]; |
259 |
for (int i = 0; i < pageBandBuffer.length; i++) |
260 |
f[i] = pageBandBuffer[i].getLineFloat(line)[0];
|
261 |
return f;
|
262 |
} |
263 |
|
264 |
public int[][] getLineInt(int line) { |
265 |
int[][] in = new int[pageBandBuffer.length][]; |
266 |
for (int i = 0; i < pageBandBuffer.length; i++) |
267 |
in[i] = pageBandBuffer[i].getLineInt(line)[0];
|
268 |
return in;
|
269 |
} |
270 |
|
271 |
public short[][] getLineShort(int line) { |
272 |
short[][] s = new short[pageBandBuffer.length][]; |
273 |
for (int i = 0; i < pageBandBuffer.length; i++) |
274 |
s[i] = pageBandBuffer[i].getLineShort(line)[0];
|
275 |
return s;
|
276 |
} |
277 |
|
278 |
|
279 |
|
280 |
public byte[] getLineFromBandByte(int line, int band) { |
281 |
return pageBandBuffer[band].getLineFromBandByte(line, 0); |
282 |
} |
283 |
|
284 |
public double[] getLineFromBandDouble(int line, int band) { |
285 |
return pageBandBuffer[band].getLineFromBandDouble(line, 0); |
286 |
} |
287 |
|
288 |
public float[] getLineFromBandFloat(int line, int band) { |
289 |
return pageBandBuffer[band].getLineFromBandFloat(line, 0); |
290 |
} |
291 |
|
292 |
public int[] getLineFromBandInt(int line, int band) { |
293 |
return pageBandBuffer[band].getLineFromBandInt(line, 0); |
294 |
} |
295 |
|
296 |
public short[] getLineFromBandShort(int line, int band) { |
297 |
return pageBandBuffer[band].getLineFromBandShort(line, 0); |
298 |
} |
299 |
|
300 |
|
301 |
|
302 |
|
303 |
public void interchangeBands(int band1, int band2) { |
304 |
} |
305 |
|
306 |
public void mallocOneBand(int dataType, int width, int height, int band) { |
307 |
} |
308 |
|
309 |
public void replicateBand(int orig, int dest) { |
310 |
} |
311 |
|
312 |
|
313 |
|
314 |
public void setElem(int line, int col, int band, byte data) { |
315 |
pageBandBuffer[band].setElem(line, col, 0, data);
|
316 |
} |
317 |
|
318 |
public void setElem(int line, int col, int band, short data) { |
319 |
pageBandBuffer[band].setElem(line, col, 0, data);
|
320 |
} |
321 |
|
322 |
public void setElem(int line, int col, int band, int data) { |
323 |
pageBandBuffer[band].setElem(line, col, 0, data);
|
324 |
} |
325 |
|
326 |
public void setElem(int line, int col, int band, float data) { |
327 |
pageBandBuffer[band].setElem(line, col, 0, data);
|
328 |
} |
329 |
|
330 |
public void setElem(int line, int col, int band, double data) { |
331 |
pageBandBuffer[band].setElem(line, col, 0, data);
|
332 |
} |
333 |
|
334 |
|
335 |
|
336 |
public void setElemByte(int line, int col, byte[] data) { |
337 |
byte[] b = new byte[1]; |
338 |
for (int i = 0; i < pageBandBuffer.length; i++) { |
339 |
b[0] = data[i];
|
340 |
pageBandBuffer[i].setElemByte(line, col, b); |
341 |
} |
342 |
} |
343 |
|
344 |
public void setElemDouble(int line, int col, double[] data) { |
345 |
double[] b = new double[1]; |
346 |
for (int i = 0; i < pageBandBuffer.length; i++) { |
347 |
b[0] = data[i];
|
348 |
pageBandBuffer[i].setElemDouble(line, col, b); |
349 |
} |
350 |
} |
351 |
|
352 |
public void setElemFloat(int line, int col, float[] data) { |
353 |
float[] b = new float[1]; |
354 |
for (int i = 0; i < pageBandBuffer.length; i++) { |
355 |
b[0] = data[i];
|
356 |
pageBandBuffer[i].setElemFloat(line, col, b); |
357 |
} |
358 |
} |
359 |
|
360 |
public void setElemInt(int line, int col, int[] data) { |
361 |
int[] b = new int[1]; |
362 |
for (int i = 0; i < pageBandBuffer.length; i++) { |
363 |
b[0] = data[i];
|
364 |
pageBandBuffer[i].setElemInt(line, col, b); |
365 |
} |
366 |
} |
367 |
|
368 |
public void setElemShort(int line, int col, short[] data) { |
369 |
short[] b = new short[1]; |
370 |
for (int i = 0; i < pageBandBuffer.length; i++) { |
371 |
b[0] = data[i];
|
372 |
pageBandBuffer[i].setElemShort(line, col, b); |
373 |
} |
374 |
} |
375 |
|
376 |
|
377 |
|
378 |
public void setLineByte(byte[][] data, int line) { |
379 |
} |
380 |
|
381 |
public void setLineDouble(double[][] data, int line) { |
382 |
} |
383 |
|
384 |
public void setLineFloat(float[][] data, int line) { |
385 |
} |
386 |
|
387 |
public void setLineInBandByte(byte[] data, int line, int band) { |
388 |
} |
389 |
|
390 |
public void setLineInBandDouble(double[] data, int line, int band) { |
391 |
} |
392 |
|
393 |
public void setLineInBandFloat(float[] data, int line, int band) { |
394 |
} |
395 |
|
396 |
public void setLineInBandInt(int[] data, int line, int band) { |
397 |
} |
398 |
|
399 |
public void setLineInBandShort(short[] data, int line, int band) { |
400 |
} |
401 |
|
402 |
public void setLineInt(int[][] data, int line) { |
403 |
} |
404 |
|
405 |
public void setLineShort(short[][] data, int line) { |
406 |
} |
407 |
|
408 |
public void setNoDataValue(double nd) { |
409 |
} |
410 |
|
411 |
public void setNotValidValue(double value) { |
412 |
} |
413 |
|
414 |
public void switchBands(int[] bands) { |
415 |
} |
416 |
|
417 |
/**
|
418 |
* Calcula el m?nimo y el m?ximo del histograma previamente.
|
419 |
* @return double[] con el m?nimo y el m?ximo.
|
420 |
*/
|
421 |
private double[] getLimits() { |
422 |
double max = Double.NEGATIVE_INFINITY; |
423 |
double min = Double.MAX_VALUE; |
424 |
double value = 0; |
425 |
|
426 |
switch (getDataType()) {
|
427 |
case IBuffer.TYPE_BYTE:
|
428 |
for (int i = 0; i < getBandCount(); i++) |
429 |
for (int r = 0; r < getHeight(); r++) |
430 |
for (int c = 0; c < getWidth(); c++) { |
431 |
value = (double) (getElemByte(r, c, i) & 0xff); |
432 |
if (value > max) max = value;
|
433 |
if (value < min) min = value;
|
434 |
} |
435 |
break;
|
436 |
case IBuffer.TYPE_SHORT:
|
437 |
for (int i = 0; i < getBandCount(); i++) |
438 |
for (int r = 0; r < getHeight(); r++) |
439 |
for (int c = 0; c < getWidth(); c++) { |
440 |
value = (double) (getElemShort(r, c, i) & 0xffff); |
441 |
if (value > max) max = value;
|
442 |
if (value < min) min = value;
|
443 |
} |
444 |
break;
|
445 |
case IBuffer.TYPE_INT:
|
446 |
for (int i = 0; i < getBandCount(); i++) |
447 |
for (int r = 0; r < getHeight(); r++) |
448 |
for (int c = 0; c < getWidth(); c++) { |
449 |
value = (double) (getElemInt(r, c, i) & 0xffffffff); |
450 |
if (value > max) max = value;
|
451 |
if (value < min) min = value;
|
452 |
} |
453 |
break;
|
454 |
case IBuffer.TYPE_FLOAT:
|
455 |
for (int i = 0; i < getBandCount(); i++) |
456 |
for (int r = 0; r < getHeight(); r++) |
457 |
for (int c = 0; c < getWidth(); c++) { |
458 |
value = (double) getElemFloat(r, c, i);
|
459 |
if (value > max) max = value;
|
460 |
if (value < min) min = value;
|
461 |
} |
462 |
break;
|
463 |
case IBuffer.TYPE_DOUBLE:
|
464 |
for (int i = 0; i < getBandCount(); i++) |
465 |
for (int r = 0; r < getHeight(); r++) |
466 |
for (int c = 0; c < getWidth(); c++) { |
467 |
value = getElemDouble(r, c, i); |
468 |
if (value > max) max = value;
|
469 |
if (value < min) min = value;
|
470 |
} |
471 |
break;
|
472 |
} |
473 |
double[] values = new double[2]; |
474 |
values[0] = min;
|
475 |
values[1] = max;
|
476 |
return values;
|
477 |
} |
478 |
|
479 |
/*
|
480 |
* (non-Javadoc)
|
481 |
* @see org.gvsig.raster.driver.datasetproperties.IHistogramable#getHistogram()
|
482 |
*/
|
483 |
public Histogram getHistogram() throws HistogramException { |
484 |
percent = 0;
|
485 |
Histogram hist = null;
|
486 |
double[] limits = getLimits(); |
487 |
|
488 |
if (getDataType() == IBuffer.TYPE_BYTE)
|
489 |
hist = new Histogram(getBandCount(), 255, limits[0], limits[1]); |
490 |
else
|
491 |
hist = new Histogram(getBandCount(), RasterLibrary.defaultNumberOfClasses, limits[0], limits[1]); |
492 |
|
493 |
for (int iBand = 0; iBand < getBandCount(); iBand++) { |
494 |
for (int row = 0; row < getHeight(); row++) { |
495 |
switch(getDataType()) {
|
496 |
case IBuffer.TYPE_BYTE:
|
497 |
for (int col = 0; col < getWidth(); col++) |
498 |
hist.incrementPxValue(iBand, (double)(getElemByte(row, col, iBand) & 0xff)); |
499 |
break;
|
500 |
case IBuffer.TYPE_SHORT:
|
501 |
for (int col = 0; col < getWidth(); col++) |
502 |
hist.incrementPxValue(iBand, (double)(getElemShort(row, col, iBand) & 0xffff)); |
503 |
break;
|
504 |
case IBuffer.TYPE_INT:
|
505 |
for (int col = 0; col < getWidth(); col++) |
506 |
hist.incrementPxValue(iBand, (double)(getElemInt(row, col, iBand) & 0xffffffff)); |
507 |
break;
|
508 |
case IBuffer.TYPE_FLOAT:
|
509 |
for (int col = 0; col < getWidth(); col++) |
510 |
hist.incrementPxValue(iBand, (double)getElemFloat(row, col, iBand));
|
511 |
break;
|
512 |
case IBuffer.TYPE_DOUBLE:
|
513 |
for (int col = 0; col < getWidth(); col++) |
514 |
hist.incrementPxValue(iBand, getElemDouble(row, col, iBand)); |
515 |
break;
|
516 |
} |
517 |
|
518 |
if (isCanceled(CANCEL_HISTOGRAM))
|
519 |
return null; |
520 |
percent = ((iBand*getHeight() + row) * 100) /(getHeight() * getBandCount());
|
521 |
} |
522 |
} |
523 |
percent = 100;
|
524 |
return hist;
|
525 |
} |
526 |
|
527 |
/*
|
528 |
* (non-Javadoc)
|
529 |
* @see org.gvsig.raster.util.IHistogramable#resetPercent()
|
530 |
*/
|
531 |
public void resetPercent() { |
532 |
percent = 0;
|
533 |
} |
534 |
|
535 |
/*
|
536 |
* (non-Javadoc)
|
537 |
* @see org.gvsig.raster.util.IHistogramable#getPercent()
|
538 |
*/
|
539 |
public int getPercent() { |
540 |
return percent;
|
541 |
} |
542 |
|
543 |
/*
|
544 |
* (non-Javadoc)
|
545 |
* @see org.gvsig.raster.util.ICancellable#isCanceled()
|
546 |
*/
|
547 |
public boolean isCanceled(int process) { |
548 |
if(process == CANCEL_HISTOGRAM)
|
549 |
return cancel[0]; |
550 |
return false; |
551 |
} |
552 |
|
553 |
/*
|
554 |
* (non-Javadoc)
|
555 |
* @see org.gvsig.raster.util.ICancellable#setCanceled(boolean)
|
556 |
*/
|
557 |
public void setCanceled(boolean value, int process) { |
558 |
if(process == CANCEL_HISTOGRAM || process == 0) |
559 |
cancel[0] = value;
|
560 |
} |
561 |
} |