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