svn-gvsig-desktop / tags / gvSIGv0_6_1RELEASE / libraries / libCq CMS for java.old / src / org / cresques / io / EcwWriter.java @ 5222
History | View | Annotate | Download (25.9 KB)
1 |
/*
|
---|---|
2 |
* Cresques Mapping Suite. Graphic Library for constructing mapping applications.
|
3 |
*
|
4 |
* Copyright (C) 2004-5.
|
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 |
* For more information, contact:
|
21 |
*
|
22 |
* cresques@gmail.com
|
23 |
*/
|
24 |
package org.cresques.io; |
25 |
|
26 |
import es.gva.cit.jecwcompress.*; |
27 |
import es.gva.cit.jgdal.GdalRasterBand; |
28 |
|
29 |
import org.cresques.geo.ViewPortData; |
30 |
|
31 |
import org.cresques.px.PxRaster; |
32 |
|
33 |
import java.io.IOException; |
34 |
|
35 |
|
36 |
/**
|
37 |
* Driver para la compresi?n en formato Ecw.
|
38 |
*
|
39 |
* Puede exportar un fichero desde un GeoRasterFile en cualquier formato soportado
|
40 |
* por los drivers de lectura a uno en formato Ecw.
|
41 |
*
|
42 |
* Puede salvar a disco en formato Ecw obteniendo los datos que van siendo servidos
|
43 |
* desde el cliente. Este cliente debe implementar un IDataWriter o tener un objeto
|
44 |
* que lo implemente. Inicialmente le pasar? los par?metros de la imagen de salida
|
45 |
* y cuando el driver comience a escribir le ir? solicitando m?s a trav?s del m?todo
|
46 |
* readData de IDataWriter. El cliente ser? el que lleve el control de lo que va
|
47 |
* sirviendo y lo que le queda por servir.
|
48 |
* @author Nacho Brodin (brodin_ign@gva.es)
|
49 |
*/
|
50 |
public class EcwWriter extends GeoRasterWriter { |
51 |
static {
|
52 |
GeoRasterWriter.registerWriterExtension("ecw", EcwWriter.class);
|
53 |
} |
54 |
|
55 |
public final int windowSizeX = 386; |
56 |
public final int windowSizeY = 215; |
57 |
public final int panelSizeX = 350; |
58 |
public final int panelSizeY = 125; |
59 |
public final String panelLayout = "BorderLayout"; |
60 |
private NCSEcwCompressClient compressclient = null; |
61 |
private Reader readerObj; |
62 |
private double pixelSizeX; |
63 |
private double pixelSizeY; |
64 |
private double geoCoordOrigenX; |
65 |
private double geoCoordOrigenY; |
66 |
private EcwSupportOptions support = null; |
67 |
private boolean consulta = false; |
68 |
|
69 |
/**
|
70 |
* Constructor para la obtenci?n de par?metros del driver.
|
71 |
*
|
72 |
*/
|
73 |
public EcwWriter() {
|
74 |
this.support = new EcwSupportOptions(); |
75 |
this.driver = "ecw"; |
76 |
this.support.setBlockSize(64); |
77 |
this.support.setCompressionDefault(10); |
78 |
this.support.setFormatDefault(4); |
79 |
this.support.setWriteGeoref(true); |
80 |
this.ident = "Ecw"; |
81 |
this.consulta = true; |
82 |
} |
83 |
|
84 |
/**
|
85 |
* Constructor para salvar una sola imagen completa
|
86 |
* @param raster PxRaster de la imagen origen
|
87 |
* @param outfilename Fichero de salida
|
88 |
* @param infilename Fichero de entrada
|
89 |
* @param compresion Compresi?n
|
90 |
*/
|
91 |
public EcwWriter(PxRaster raster, String outfilename, String infilename, |
92 |
int compresion) throws EcwException, IOException { |
93 |
this.support = new EcwSupportOptions(); |
94 |
this.ident = "Ecw"; |
95 |
|
96 |
this.outfilename = outfilename;
|
97 |
this.infilename = infilename;
|
98 |
this.currentRaster = raster;
|
99 |
|
100 |
this.support.setCompressionDefault(compresion);
|
101 |
|
102 |
this.sizeWindowX = raster.getFWidth();
|
103 |
this.sizeWindowY = raster.getFHeight();
|
104 |
|
105 |
this.support.setBlockSize(currentRaster.getBlockSize());
|
106 |
|
107 |
nBands = currentRaster.getBandCount(); |
108 |
|
109 |
//Calculamos la georeferenciaci?n
|
110 |
double maxX = currentRaster.getExtent().maxX();
|
111 |
geoCoordOrigenX = currentRaster.getExtent().minX(); |
112 |
geoCoordOrigenY = currentRaster.getExtent().maxY(); |
113 |
|
114 |
double minY = currentRaster.getExtent().minY();
|
115 |
double w = currentRaster.getFWidth();
|
116 |
double h = currentRaster.getFHeight();
|
117 |
pixelSizeX = (maxX - geoCoordOrigenX) / w; |
118 |
pixelSizeY = (minY - geoCoordOrigenY) / h; |
119 |
|
120 |
if ((sizeWindowX < 0) || (sizeWindowY < 0)) { |
121 |
throw new IOException("Tama?o del fichero de salida erroneo."); |
122 |
} |
123 |
|
124 |
if (nBands == 1) { |
125 |
this.support.setFormatDefault(CompressFormat.COMPRESS_UINT8);
|
126 |
} else if (nBands == 3) { |
127 |
this.support.setFormatDefault(CompressFormat.COMPRESS_RGB);
|
128 |
} else {
|
129 |
this.support.setFormatDefault(CompressFormat.COMPRESS_MULTI);
|
130 |
} |
131 |
|
132 |
init(); |
133 |
} |
134 |
|
135 |
/**
|
136 |
* Constructor para la lectura de datos desde el objeto cliente a partir
|
137 |
* de un viewport dado.
|
138 |
* @param dataWriter Objeto que sirve datos para el escritor
|
139 |
* @param vp viewport de origen
|
140 |
* @param outFileName Fichero de salida
|
141 |
* @param blockSize Tama?o de bloque
|
142 |
* @param nBands N?mero de bandas
|
143 |
* @param compresion Compresi?n
|
144 |
* @throws EcwException
|
145 |
* @throws IOException
|
146 |
*/
|
147 |
public EcwWriter(IDataWriter dataWriter, ViewPortData vp,
|
148 |
String outFileName, int blockSize, int nBands, |
149 |
int compresion) throws EcwException, IOException { |
150 |
this.support = new EcwSupportOptions(); |
151 |
this.ident = "Ecw"; |
152 |
|
153 |
if (compresion <= 0) { |
154 |
throw new EcwException("Tasa de compresi?n no valida."); |
155 |
} |
156 |
|
157 |
if (nBands <= 0) { |
158 |
throw new EcwException("N?mero de bandas erroneo."); |
159 |
} |
160 |
|
161 |
if ((vp.getWidth() <= 0) || (vp.getHeight() <= 0)) { |
162 |
throw new EcwException("Tama?o de la imagen de salida erroneo."); |
163 |
} |
164 |
|
165 |
this.outfilename = outFileName;
|
166 |
this.dataWriter = dataWriter;
|
167 |
this.support.setCompressionDefault(compresion);
|
168 |
this.nBands = nBands;
|
169 |
|
170 |
this.sizeWindowX = (int) vp.getWidth(); |
171 |
this.sizeWindowY = (int) vp.getHeight(); |
172 |
this.support.setBlockSize(blockSize);
|
173 |
|
174 |
//Calculamos la georeferenciaci?n a partir del extend pasado por el cliente.
|
175 |
double maxX = vp.getExtent().maxX();
|
176 |
geoCoordOrigenX = vp.getExtent().minX(); |
177 |
geoCoordOrigenY = vp.getExtent().maxY(); |
178 |
|
179 |
double minY = vp.getExtent().minY();
|
180 |
pixelSizeX = (maxX - geoCoordOrigenX) / vp.getWidth(); |
181 |
pixelSizeY = (minY - geoCoordOrigenY) / vp.getHeight(); |
182 |
|
183 |
if (pixelSizeX == 0) { |
184 |
pixelSizeX = 1.0;
|
185 |
} |
186 |
|
187 |
if (pixelSizeY == 0) { |
188 |
pixelSizeY = 1.0;
|
189 |
} |
190 |
|
191 |
init(); |
192 |
} |
193 |
|
194 |
/**
|
195 |
* Inicializaci?n de los par?metros del compresor que ser?n obtenidos
|
196 |
* de PxRaster.
|
197 |
* @throws EcwException
|
198 |
*/
|
199 |
private void init() throws EcwException { |
200 |
if (this.support.getCompression() == 0) { |
201 |
this.support.setCompressionDefault(1); |
202 |
} |
203 |
|
204 |
if (compressclient == null) { |
205 |
compressclient = new NCSEcwCompressClient();
|
206 |
} |
207 |
|
208 |
compressclient.setOutputFilename(outfilename); |
209 |
compressclient.setInputFilename(infilename); |
210 |
compressclient.setTargetCompress(this.support.getCompression());
|
211 |
compressclient.setInOutSizeX(sizeWindowX); |
212 |
compressclient.setInOutSizeY(sizeWindowY); |
213 |
compressclient.setInputBands(nBands); |
214 |
compressclient.setCompressFormat(this.support.getFormat());
|
215 |
|
216 |
//System.out.println("Origen=>"+minX + " "+maxY );
|
217 |
//System.out.println("Pixel Size=>"+psX+" "+psY);
|
218 |
//compressclient.setDatum("UTM Zona 31N");
|
219 |
//client.setProjection("WGS84");
|
220 |
if (this.support.getGeoref()) { |
221 |
compressclient.setCellIncrementX(pixelSizeX); |
222 |
compressclient.setCellIncrementY(pixelSizeY); |
223 |
compressclient.setOriginX(geoCoordOrigenX); |
224 |
compressclient.setOriginY(geoCoordOrigenY); |
225 |
} |
226 |
|
227 |
compressclient.setCellSizeUnits(1);
|
228 |
|
229 |
//System.out.println(outfilename+" "+infilename+" "+compresion+" "+sizeWindowX+" "+sizeWindowY+" "+currentRaster.getBandCount());
|
230 |
//System.out.println(psX+" "+psY+" "+minX+" "+maxX);
|
231 |
if (currentRaster != null) { |
232 |
readerObj = new Reader(currentRaster.getGeoFile(), compressclient, |
233 |
ulX, ulY, sizeWindowX, sizeWindowY, |
234 |
this.support.getBlockSize());
|
235 |
} else if (dataWriter != null) { |
236 |
readerObj = new Reader(dataWriter, compressclient, sizeWindowX, |
237 |
sizeWindowY, this.support.getBlockSize(),
|
238 |
this.nBands);
|
239 |
} |
240 |
} |
241 |
|
242 |
/**
|
243 |
* A partir de un elemento que contiene una propiedad y un valor
|
244 |
* lo parsea y asigna el valor a su variable.
|
245 |
* @param propValue elemento con la forma propiedad=valor
|
246 |
*/
|
247 |
private void readProperty(String propValue) { |
248 |
String prop = propValue.substring(0, propValue.indexOf("=")); |
249 |
|
250 |
if (propValue.startsWith(prop)) {
|
251 |
String value = propValue.substring(propValue.indexOf("=") + 1, |
252 |
propValue.length()); |
253 |
|
254 |
if ((value != null) && !value.equals("")) { |
255 |
if (prop.equals("BLOCKSIZE")) { |
256 |
this.support.setBlockSize(Integer.parseInt(value)); |
257 |
} |
258 |
|
259 |
if (prop.equals("FORMAT")) { |
260 |
int select = 0; |
261 |
|
262 |
if (value.equals("UINT8")) { |
263 |
select = 1;
|
264 |
} |
265 |
|
266 |
if (value.equals("YUV")) { |
267 |
select = 2;
|
268 |
} |
269 |
|
270 |
if (value.equals("MULTI")) { |
271 |
select = 3;
|
272 |
} |
273 |
|
274 |
if (value.equals("RGB")) { |
275 |
select = 4;
|
276 |
} |
277 |
|
278 |
this.support.setFormatDefault(select);
|
279 |
} |
280 |
|
281 |
if (prop.equals("GEOREF")) { |
282 |
boolean georef = true; |
283 |
|
284 |
if (value.equals("yes")) { |
285 |
georef = true;
|
286 |
} else {
|
287 |
georef = false;
|
288 |
} |
289 |
|
290 |
this.support.setWriteGeoref(georef);
|
291 |
} |
292 |
|
293 |
if (prop.equals("COMPRESSION")) { |
294 |
this.support.setCompressionDefault(Integer.parseInt(value)); |
295 |
} |
296 |
} |
297 |
} |
298 |
} |
299 |
|
300 |
/**
|
301 |
* Asigna propiedades al driver a partir de un vector de
|
302 |
* strings donde cada elemento tiene la estructura de
|
303 |
* propiedad=valor.
|
304 |
* @param props Propiedades
|
305 |
*/
|
306 |
public void setProps(String[] props) { |
307 |
for (int iProps = 0; iProps < props.length; iProps++) |
308 |
readProperty(props[iProps]); |
309 |
|
310 |
try {
|
311 |
if (!consulta) {
|
312 |
init(); |
313 |
} |
314 |
} catch (EcwException e) {
|
315 |
e.printStackTrace(); |
316 |
} |
317 |
} |
318 |
|
319 |
/**
|
320 |
* Realiza la funci?n de compresi?n a partir de un GeoRasterFile.
|
321 |
* @throws IOException
|
322 |
*/
|
323 |
public void fileWrite() throws IOException { |
324 |
if (currentRaster == null) { |
325 |
throw new IOException("No se ha asignado un fichero de entrada."); |
326 |
} |
327 |
|
328 |
try {
|
329 |
compressclient.NCSEcwCompressOpen(false);
|
330 |
compressclient.NCSEcwCompress(readerObj); |
331 |
} catch (EcwException e) {
|
332 |
e.printStackTrace(); |
333 |
} |
334 |
} |
335 |
|
336 |
/**
|
337 |
* Realiza la funci?n de compresi?n a partir de los datos pasados por el cliente.
|
338 |
* @throws IOException
|
339 |
*/
|
340 |
public void dataWrite() throws IOException { |
341 |
if (dataWriter == null) { |
342 |
throw new IOException("No se ha obtenido un objeto para la lectura valido."); |
343 |
} |
344 |
|
345 |
try {
|
346 |
compressclient.NCSEcwCompressOpen(false);
|
347 |
compressclient.NCSEcwCompress(readerObj); |
348 |
} catch (EcwException e) {
|
349 |
e.printStackTrace(); |
350 |
} |
351 |
} |
352 |
|
353 |
/**
|
354 |
* Cierra el compresor ecw.
|
355 |
*/
|
356 |
public void writeClose() { |
357 |
try {
|
358 |
compressclient.NCSEcwCompressClose(); |
359 |
} catch (EcwException e) {
|
360 |
e.printStackTrace(); |
361 |
} |
362 |
} |
363 |
|
364 |
/**
|
365 |
* Devuelve la configuraci?n de la ventana de dialogo
|
366 |
* para las propiedades del driver de escritura de Ecw.
|
367 |
* @return XML de configuraci?n del dialogo.
|
368 |
*/
|
369 |
public String getXMLPropertiesDialog() { |
370 |
StringBuffer options = null; |
371 |
options = new StringBuffer(); |
372 |
options.append("<window sizex=\"" + this.windowSizeX + "\" sizey=\"" + |
373 |
this.windowSizeY + "\">"); |
374 |
options.append("<panel sizex=\"" + this.panelSizeX + "\" sizey=\"" + |
375 |
this.panelSizeY + "\" layout=\"" + this.panelLayout + |
376 |
"\" border=\"yes\">");
|
377 |
|
378 |
options.append("<panel layout=\"FlowLayout\" position=\"North\" align=\"left\">");
|
379 |
options.append("<label>Tama?o bloque:</label>");
|
380 |
options.append("<combo ident=\"BLOCKSIZE\" selected=\"" +
|
381 |
this.support.getBlockSize() + "\">"); |
382 |
|
383 |
for (int i = 0; i < this.support.getBlockSizeList().length; i++) |
384 |
options.append("<elem>" + this.support.getBlockSizeList()[i] + |
385 |
"</elem>");
|
386 |
|
387 |
options.append("</combo>");
|
388 |
/*options.append("<label>Formato:</label>");
|
389 |
|
390 |
String sel = null;
|
391 |
|
392 |
if (this.support.getGeoref()) {
|
393 |
sel = new String("yes");
|
394 |
} else {
|
395 |
sel = new String("no");
|
396 |
}
|
397 |
|
398 |
options.append("<check ident=\"GEOREF\" selected=\"" + sel +
|
399 |
"\" text=\"Georef Si/No\">");
|
400 |
options.append("</check>");*/
|
401 |
options.append("</panel>");
|
402 |
|
403 |
options.append("<panel layout=\"FlowLayout\" position=\"South\">");
|
404 |
options.append("<slider ident=\"COMPRESSION\" name=\"Nivel de Compresi?n\" sizex=\"350\" sizey=\"20\">");
|
405 |
options.append("<min>" + this.support.getCompressionList()[0] + |
406 |
"</min>");
|
407 |
options.append("<max>" + this.support.getCompressionList()[1] + |
408 |
"</max>");
|
409 |
options.append("<value>" + this.support.compresionDefault + "</value>"); |
410 |
options.append("<minorspacing>" + this.support.getCompressionList()[3] + |
411 |
"</minorspacing>");
|
412 |
options.append("<majorspacing>" + this.support.getCompressionList()[4] + |
413 |
"</majorspacing>");
|
414 |
options.append("</slider>");
|
415 |
options.append("</panel>");
|
416 |
|
417 |
options.append("</panel>");
|
418 |
options.append("</window>");
|
419 |
|
420 |
return options.toString();
|
421 |
} |
422 |
|
423 |
/**
|
424 |
* @author Nacho Brodin <brodin_ign@gva.es>
|
425 |
*
|
426 |
* Opciones de escritura para el Ecw. Estas opciones son utilizadas para el
|
427 |
* dialogo de propiedades de Salvar a Raster en Ecw.
|
428 |
*/
|
429 |
class EcwSupportOptions extends WriterSupportOptions { |
430 |
private String[] compresionListValues = { "0", "20", "10", "1", "5" }; //min, max, valor defecto, intervalo peque?o, intervalo grande; |
431 |
private String[] formatList = { "NONE", "UINT8", "YUV", "MULTI", "RGB" }; |
432 |
private int formatDefault = 4; |
433 |
private int compresionDefault = 10; |
434 |
|
435 |
EcwSupportOptions() { |
436 |
super("Ecw"); |
437 |
} |
438 |
|
439 |
/**
|
440 |
* Asigna la lista de valores de compresi?n
|
441 |
* @param compresion lista de valores
|
442 |
*/
|
443 |
public void setCompressionList(String[] compresion) { |
444 |
this.compresionListValues = compresion;
|
445 |
} |
446 |
|
447 |
/**
|
448 |
* Valor de formato seleccionado en el driver.
|
449 |
* 0-NONE, 1-UINT8, 2-YUV, 3-MULTI, 4-RGB
|
450 |
* @param i valor de formato
|
451 |
*/
|
452 |
public void setFormatDefault(int i) { |
453 |
this.formatDefault = i;
|
454 |
} |
455 |
|
456 |
/**
|
457 |
* Asigna el nivel de compresi?npor defecto
|
458 |
* @param compress Nivel de compresi?n
|
459 |
*/
|
460 |
public void setCompressionDefault(int compress) { |
461 |
this.compresionDefault = compress;
|
462 |
} |
463 |
|
464 |
/**
|
465 |
* Obtiene el tipo de imagen en un formato comprensible por el driver.
|
466 |
* @return Tipo de imagen
|
467 |
*/
|
468 |
public String getStringFormat() { |
469 |
return "COMPRESS_" + formatList[formatDefault]; |
470 |
} |
471 |
|
472 |
/**
|
473 |
* Obtiene la lista de los tipos de formato disponible
|
474 |
* @return Lista de tipos de formato
|
475 |
*/
|
476 |
public String[] getFormatList() { |
477 |
return formatList;
|
478 |
} |
479 |
|
480 |
/**
|
481 |
* Obtiene el tipo de formato seleccionado en el driver
|
482 |
* @return Tipo de formato seleccionado
|
483 |
*/
|
484 |
public int getFormat() { |
485 |
return formatDefault;
|
486 |
} |
487 |
|
488 |
/**
|
489 |
* Obtiene la compresion seleccionada
|
490 |
* @return Compresi?n seleccionada
|
491 |
*/
|
492 |
public int getCompression() { |
493 |
return compresionDefault;
|
494 |
} |
495 |
|
496 |
/**
|
497 |
* Obtiene la lista de los valores para la generaci?n de la barra de
|
498 |
* compresi?n. Son cinco valores m?nimo, m?ximo, seleccionado,
|
499 |
* intervalo peque?o, intervalo grande.
|
500 |
* @return lista de valores de compresi?n
|
501 |
*/
|
502 |
public String[] getCompressionList() { |
503 |
return compresionListValues;
|
504 |
} |
505 |
} |
506 |
} |
507 |
|
508 |
|
509 |
/**
|
510 |
* Clase que se encarga de la lectura de datos que ser?n escritos. Hereda de JniObject
|
511 |
* para asegurar para asegurar que el interfaz de la libreria tiene acceso a variables
|
512 |
* necesarias para la petici?n de datos cuando vacia el buffer. Implementa ReadCallBack
|
513 |
* para obligar escribir el m?todo loadBuffer encargado de servir los datos cuando el
|
514 |
* buffer se vacia.
|
515 |
*
|
516 |
*/
|
517 |
class Reader extends JniObject implements ReadCallBack { |
518 |
private NCSEcwCompressClient compressclient = null; |
519 |
private int width; |
520 |
private int height; |
521 |
private int ulX; |
522 |
private int ulY; |
523 |
private GeoRasterFile grf = null; |
524 |
private IDataWriter dataWriter = null; |
525 |
private GdalRasterBand rband = null; |
526 |
private int blockSizeRead = 1; //Alto del bloque leido de la imagen origen |
527 |
private int countLine = 1; //Contador de l?neas procesadas en cada lectura de bloque |
528 |
byte[][] buf = null; |
529 |
byte[] bufband1 = null; |
530 |
byte[] bufband2 = null; |
531 |
byte[] bufband3 = null; |
532 |
byte[] bufband4 = null; |
533 |
byte[] bufband5 = null; |
534 |
byte[] bufband6 = null; |
535 |
int[] dataBuffer = null; |
536 |
private int lineasBloqueFinal = 0; //N?mero de l?neas leidas del bloque final |
537 |
private int nBlocks = 0; //N?mero de bloques completos |
538 |
private int countBlock = 1; //Contador de bloques completos procesados |
539 |
private int nBands = 0; |
540 |
|
541 |
/**
|
542 |
* Constructor para la escritura de un fichero desde un GeoRasterFile
|
543 |
* @param grf GeorasterFile del fichero de origen
|
544 |
* @param compressclient Objeto que representa al compresor ecw
|
545 |
* @param ulx Coordenada X de la esquina superior izquierda
|
546 |
* @param uly Coordenada Y de la esquina superior izquierda
|
547 |
* @param width Ancho de la imagen
|
548 |
* @param height Alto de la imagen
|
549 |
* @param blockSizeRead Altura del bloque en la lectura
|
550 |
*/
|
551 |
public Reader(GeoRasterFile grf, NCSEcwCompressClient compressclient, |
552 |
int ulx, int uly, int width, int height, int blockSizeRead) { |
553 |
this.compressclient = compressclient;
|
554 |
this.width = width;
|
555 |
this.height = height;
|
556 |
this.ulX = ulx;
|
557 |
this.ulY = uly;
|
558 |
this.grf = grf;
|
559 |
this.nBands = grf.bandCount;
|
560 |
|
561 |
if (blockSizeRead != 0) { |
562 |
this.blockSizeRead = blockSizeRead;
|
563 |
} |
564 |
|
565 |
nBlocks = (int) (height / this.blockSizeRead); |
566 |
lineasBloqueFinal = height - (nBlocks * this.blockSizeRead);
|
567 |
|
568 |
if (blockSizeRead > 64) { |
569 |
this.blockSizeRead = 64; |
570 |
} |
571 |
} |
572 |
|
573 |
/**
|
574 |
* Constructor para que los datos sean servidos desde el cliente a trav?s de un
|
575 |
* IDataWriter.
|
576 |
* @param dataWriter Objeto servidor de datos del driver
|
577 |
* @param compressclient Objeto que representa al compresor ecw
|
578 |
* @param width Ancho de la imagen
|
579 |
* @param height Alto de la imagen
|
580 |
* @param blockSizeRead Altura del bloque en la lectura
|
581 |
* @param nBands N?mero de bandas
|
582 |
*/
|
583 |
public Reader(IDataWriter dataWriter, NCSEcwCompressClient compressclient, |
584 |
int width, int height, int blockSizeRead, int nBands) { |
585 |
this.compressclient = compressclient;
|
586 |
this.width = width;
|
587 |
this.height = height;
|
588 |
this.dataWriter = dataWriter;
|
589 |
this.nBands = nBands;
|
590 |
|
591 |
if (blockSizeRead != 0) { |
592 |
this.blockSizeRead = blockSizeRead;
|
593 |
} |
594 |
|
595 |
nBlocks = (int) (height / this.blockSizeRead); |
596 |
lineasBloqueFinal = height - (nBlocks * this.blockSizeRead);
|
597 |
|
598 |
//System.out.println("NBLOQUES="+nBlocks+" lineas ultimo="+lineasBloqueFinal);
|
599 |
if (blockSizeRead > 64) { |
600 |
this.blockSizeRead = 64; |
601 |
} |
602 |
} |
603 |
|
604 |
/**
|
605 |
* Lectura de bandas desde un GeoRasterFile
|
606 |
* @param ulX Coordenada X de la esquina superior izquierda
|
607 |
* @param ulY Coordenada Y de la esquina superior izquierda
|
608 |
* @param width Ancho de la imagen
|
609 |
* @param lineasLeidas N?mero de l?neas a leer del origen
|
610 |
*/
|
611 |
private void readBandsGRFile(int ulX, int ulY, int width, int lineasLeidas) { |
612 |
if (nBands >= 1) { |
613 |
bufband1 = grf.getWindow(ulX, ulY, width, lineasLeidas, 1);
|
614 |
} |
615 |
|
616 |
if (nBands >= 2) { |
617 |
bufband2 = grf.getWindow(ulX, ulY, width, lineasLeidas, 2);
|
618 |
} |
619 |
|
620 |
if (nBands >= 3) { |
621 |
bufband3 = grf.getWindow(ulX, ulY, width, lineasLeidas, 3);
|
622 |
} |
623 |
|
624 |
if (nBands >= 4) { |
625 |
bufband4 = grf.getWindow(ulX, ulY, width, lineasLeidas, 4);
|
626 |
} |
627 |
|
628 |
if (nBands >= 5) { |
629 |
bufband5 = grf.getWindow(ulX, ulY, width, lineasLeidas, 5);
|
630 |
} |
631 |
|
632 |
if (nBands >= 6) { |
633 |
bufband6 = grf.getWindow(ulX, ulY, width, lineasLeidas, 6);
|
634 |
} |
635 |
} |
636 |
|
637 |
/**
|
638 |
* Lectura de bandas llamando al objeto cliente para que nos cargue el buffer
|
639 |
* @param width Ancho de la imagen
|
640 |
* @param height Alto de la imagen
|
641 |
*/
|
642 |
private void readBands(int width, int height) { |
643 |
dataBuffer = dataWriter.readData(width, height, 0);
|
644 |
} |
645 |
|
646 |
/**
|
647 |
* M?todo obligado por el interfaz y que es llamado desde C cuando el
|
648 |
* compresor necesita que le sirvan m?s datos.
|
649 |
*/
|
650 |
public void loadBuffer() { |
651 |
int lineasLeidas = 0; |
652 |
|
653 |
//si se leen bloques completos lineasLeidas es == al tama?o de bloque sino
|
654 |
//es que es el ?ltimo con lo que ser? del tama?o del bloque final
|
655 |
if (countBlock <= nBlocks) {
|
656 |
lineasLeidas = blockSizeRead; |
657 |
} else {
|
658 |
lineasLeidas = lineasBloqueFinal; |
659 |
} |
660 |
|
661 |
//Leemos un bloque nuevo cuando se han procesado todas las l?neas del anterior
|
662 |
if ((nNextLine % blockSizeRead) == 0) { |
663 |
if (grf != null) { |
664 |
readBandsGRFile(ulX, ulY + nNextLine, width, lineasLeidas); |
665 |
} else if (dataWriter != null) { |
666 |
readBands(width, lineasLeidas); |
667 |
} |
668 |
|
669 |
countLine = 0;
|
670 |
countBlock++; |
671 |
} |
672 |
|
673 |
for (int iBand = 0; iBand < this.nBands; iBand++) { |
674 |
for (int pos = 0; pos < width; pos++) { |
675 |
if (grf != null) { |
676 |
if (iBand == 0) { |
677 |
compressclient.buffer[pos + (width * iBand)] = bufband1[pos + |
678 |
(width * countLine)]; |
679 |
} |
680 |
|
681 |
if (iBand == 1) { |
682 |
compressclient.buffer[pos + (width * iBand)] = bufband2[pos + |
683 |
(width * countLine)]; |
684 |
} |
685 |
|
686 |
if (iBand == 2) { |
687 |
compressclient.buffer[pos + (width * iBand)] = bufband3[pos + |
688 |
(width * countLine)]; |
689 |
} |
690 |
|
691 |
if (iBand == 3) { |
692 |
compressclient.buffer[pos + (width * iBand)] = bufband4[pos + |
693 |
(width * countLine)]; |
694 |
} |
695 |
|
696 |
if (iBand == 4) { |
697 |
compressclient.buffer[pos + (width * iBand)] = bufband5[pos + |
698 |
(width * countLine)]; |
699 |
} |
700 |
|
701 |
if (iBand == 5) { |
702 |
compressclient.buffer[pos + (width * iBand)] = bufband6[pos + |
703 |
(width * countLine)]; |
704 |
} |
705 |
} else {
|
706 |
if (iBand == 0) { |
707 |
compressclient.buffer[pos + (width * iBand)] = (byte) ((dataBuffer[pos +
|
708 |
(width * countLine)] & |
709 |
0xff0000) >> 16); |
710 |
} |
711 |
|
712 |
if (iBand == 1) { |
713 |
compressclient.buffer[pos + (width * iBand)] = (byte) ((dataBuffer[pos +
|
714 |
(width * countLine)] & |
715 |
0xff00) >> 8); |
716 |
} |
717 |
|
718 |
if (iBand == 2) { |
719 |
compressclient.buffer[pos + (width * iBand)] = (byte) (dataBuffer[pos +
|
720 |
(width * countLine)] & |
721 |
0xff);
|
722 |
} |
723 |
} |
724 |
|
725 |
if ((pos == (width - 1)) && (iBand == (this.nBands - 1))) { |
726 |
countLine++; |
727 |
} |
728 |
} |
729 |
} |
730 |
} |
731 |
|
732 |
/**
|
733 |
* M?todo obligado por el interfaz y que es llamado desde C cuando el
|
734 |
* compresor actualiza el porcentaje de compresi?n
|
735 |
*/
|
736 |
public void updatePercent() { |
737 |
System.out.println(compressclient.getPercent() + "%"); |
738 |
} |
739 |
} |