Statistics
| Revision:

svn-gvsig-desktop / tags / v1_1_Build_1000 / libraries / libCq_CMS_praster / src / org / cresques / io / ErmapperWriter.java @ 11885

History | View | Annotate | Download (28 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
import org.cresques.io.GdalWriter.GdalSupportOptions;
31

    
32
import org.cresques.px.PxRaster;
33

    
34
import java.io.IOException;
35

    
36

    
37
/**
38
 * Driver para la compresi?n en formato Ecw.
39
 *
40
 * Puede exportar un fichero desde un GeoRasterFile en cualquier formato soportado
41
 * por los drivers de lectura a uno en formato Ecw.
42
 *
43
 * Puede salvar a disco en formato Ecw obteniendo los datos que van siendo servidos
44
 * desde el cliente. Este cliente debe implementar un IDataWriter o tener un objeto
45
 * que lo implemente. Inicialmente le pasar? los par?metros de la imagen de salida
46
 * y cuando el driver comience a escribir le ir? solicitando m?s a trav?s del m?todo
47
 * readData de IDataWriter. El cliente ser? el que lleve el control de lo que va
48
 * sirviendo y lo que le queda por servir.
49
 * @author Nacho Brodin (brodin_ign@gva.es)
50
 */
51
public class ErmapperWriter extends GeoRasterWriter {
52
    static {
53
            String os = System.getProperties().getProperty("os.version");
54
        if (os.startsWith("2.4")){
55
                GeoRasterWriter.registerWriterExtension("ecw", ErmapperWriter.class);
56
                typeList.put("ecw", "Ecw");
57
        }
58
        GeoRasterWriter.registerWriterExtension("jp2", ErmapperWriter.class);
59
        typeList.put("jp2", "Jpeg2000");
60
    }
61

    
62
    public final int                                 windowSizeX = 386;
63
    public final int                                 windowSizeY = 220;
64
    public final int                                 panelSizeX = 358;
65
    public final int                                 panelSizeY = 125;
66
    public final String                         panelLayout = "BorderLayout";
67
    private NCSEcwCompressClient         compressclient = null;
68
    private Reader                                         readerObj;
69
    private double                                         pixelSizeX;
70
    private double                                         pixelSizeY;
71
    private double                                         geoCoordOrigenX;
72
    private double                                         geoCoordOrigenY;
73
    private EcwSupportOptions                 support = null;
74
    private boolean                                 consulta = false;
75

    
76
    /**
77
     * Constructor para la obtenci?n de par?metros del driver.
78
     */
79
    public ErmapperWriter(String fileName) {
80
            ident = fileName.toLowerCase().substring(fileName.lastIndexOf(".") + 1);            
81
            driver = (String)typeList.get(ident);
82
            
83
        support = new EcwSupportOptions(driver);
84
        support.setBlockSize(blockSizeDefault);
85
        support.setCompressionDefault(10);
86
        support.setFormatDefault(4);
87
        support.setWriteGeoref(true);
88
        consulta = true;
89
    }
90

    
91
    /**
92
     * Constructor para salvar una sola imagen completa
93
     * @param raster        PxRaster de la imagen origen
94
     * @param outfilename        Fichero de salida
95
     * @param infilename        Fichero de entrada
96
     * @param compresion        Compresi?n
97
     */
98
    public ErmapperWriter(PxRaster raster, String outFileName, String inFileName,
99
                     int compresion) throws EcwException, IOException {
100
            ident = outFileName.toLowerCase().substring(outFileName.lastIndexOf(".") + 1); 
101
            driver = (String)typeList.get(ident);
102
        support = new EcwSupportOptions(driver);
103
        
104
        this.outFileName = outFileName;
105
        this.inFileName = inFileName;
106
        currentRaster = raster;
107

    
108
        support.setCompressionDefault(compresion);
109

    
110
        sizeWindowX = raster.getFWidth();
111
        sizeWindowY = raster.getFHeight();
112

    
113
        support.setBlockSize(currentRaster.getBlockSize());
114

    
115
        nBands = currentRaster.getBandCount();
116

    
117
        //Calculamos la georeferenciaci?n
118
        double maxX = currentRaster.getExtent().maxX();
119
        geoCoordOrigenX = currentRaster.getExtent().minX();
120
        geoCoordOrigenY = currentRaster.getExtent().maxY();
121

    
122
        double minY = currentRaster.getExtent().minY();
123
        double w = currentRaster.getFWidth();
124
        double h = currentRaster.getFHeight();
125
        pixelSizeX = (maxX - geoCoordOrigenX) / w;
126
        pixelSizeY = (minY - geoCoordOrigenY) / h;
127

    
128
        if ((sizeWindowX < 0) || (sizeWindowY < 0)) {
129
            throw new IOException("Tama?o del fichero de salida erroneo.");
130
        }
131

    
132
        if (nBands == 1) {
133
            this.support.setFormatDefault(CompressFormat.COMPRESS_UINT8);
134
        } else if (nBands == 3) {
135
            this.support.setFormatDefault(CompressFormat.COMPRESS_RGB);
136
        } else {
137
            this.support.setFormatDefault(CompressFormat.COMPRESS_MULTI);
138
        }
139

    
140
        init();
141
    }
142

    
143
    /**
144
     * Constructor para la lectura de datos desde el objeto cliente a partir
145
     * de un viewport dado.
146
     * @param dataWriter        Objeto que sirve datos para el escritor
147
     * @param vp        viewport de origen
148
     * @param outFileName        Fichero de salida
149
     * @param blockSize        Tama?o de bloque
150
     * @param nBands        N?mero de bandas
151
     * @param compresion        Compresi?n
152
     * @throws EcwException
153
     * @throws IOException
154
     */
155
    public ErmapperWriter(        IDataWriter dataWriter, 
156
                                     String outFileName, 
157
                                     Integer blockSize, 
158
                                     Integer nBands,
159
                                     ViewPortData vp,
160
                                     Integer compresion,
161
                                     Integer outSizeX,
162
                                     Integer outSizeY,
163
                                     Integer dataType) throws EcwException, IOException {
164
            ident = outFileName.toLowerCase().substring(outFileName.lastIndexOf(".") + 1); 
165
            driver = (String)typeList.get(ident);
166
        support = new EcwSupportOptions(driver);
167
        int comp = compresion.intValue();
168
        
169
        if (comp <= 0)
170
            comp = 1;
171

    
172
        if (nBands.intValue() <= 0) {
173
            throw new EcwException("N?mero de bandas erroneo.");
174
        }
175

    
176
        if ((vp.getWidth() <= 0) || (vp.getHeight() <= 0)) {
177
            throw new EcwException("Tama?o de la imagen de salida erroneo.");
178
        }
179

    
180
        this.outFileName = outFileName;
181
        this.dataWriter = dataWriter;
182
        this.support.setCompressionDefault(compresion.intValue());
183
        this.nBands = nBands.intValue();
184

    
185
        this.sizeWindowX = (int) vp.getWidth();
186
        this.sizeWindowY = (int) vp.getHeight();
187
        this.support.setBlockSize(blockSize.intValue());
188

    
189
        //Calculamos la georeferenciaci?n a partir del extend pasado por el cliente.
190
        double maxX = vp.getExtent().maxX();
191
        geoCoordOrigenX = vp.getExtent().minX();
192
        geoCoordOrigenY = vp.getExtent().maxY();
193

    
194
        double minY = vp.getExtent().minY();
195
        pixelSizeX = (maxX - geoCoordOrigenX) / vp.getWidth();
196
        pixelSizeY = (minY - geoCoordOrigenY) / vp.getHeight();
197

    
198
        if (pixelSizeX == 0) {
199
            pixelSizeX = 1.0;
200
        }
201

    
202
        if (pixelSizeY == 0) {
203
            pixelSizeY = 1.0;
204
        }
205

    
206
        init();
207
    }
208

    
209
    /**
210
     * Inicializaci?n de los par?metros del compresor que ser?n obtenidos
211
     * de PxRaster.
212
     * @throws EcwException
213
     */
214
    private void init() throws EcwException {
215
        if (this.support.getCompression() == 0) {
216
            this.support.setCompressionDefault(1);
217
        }
218

    
219
        if (compressclient == null) {
220
            compressclient = new NCSEcwCompressClient();
221
        }
222

    
223
        compressclient.setOutputFilename(outFileName);
224
        compressclient.setInputFilename(inFileName);
225
        compressclient.setTargetCompress(this.support.getCompression());
226
        compressclient.setInOutSizeX(sizeWindowX);
227
        compressclient.setInOutSizeY(sizeWindowY);
228
        compressclient.setInputBands(nBands);
229
        compressclient.setCompressFormat(this.support.getFormat());
230

    
231
        //System.out.println("Origen=>"+minX + "  "+maxY );
232
        //System.out.println("Pixel Size=>"+psX+"  "+psY);
233
        //compressclient.setDatum("UTM Zona 31N");
234
        //client.setProjection("WGS84");
235
        if (this.support.getGeoref()) {
236
            compressclient.setCellIncrementX(pixelSizeX);
237
            compressclient.setCellIncrementY(pixelSizeY);
238
            compressclient.setOriginX(geoCoordOrigenX);
239
            compressclient.setOriginY(geoCoordOrigenY);
240
        }
241

    
242
        compressclient.setCellSizeUnits(1);
243

    
244
        //System.out.println(outfilename+" "+infilename+" "+compresion+" "+sizeWindowX+" "+sizeWindowY+" "+currentRaster.getBandCount());
245
        //System.out.println(psX+" "+psY+" "+minX+" "+maxX);
246
        if (currentRaster != null) {
247
            readerObj = new Reader(currentRaster.getGeoFile(), compressclient,
248
                                   ulX, ulY, sizeWindowX, sizeWindowY,
249
                                   this.support.getBlockSize());
250
        } else if (dataWriter != null) {
251
            readerObj = new Reader(dataWriter, compressclient, sizeWindowX,
252
                                   sizeWindowY, this.support.getBlockSize(),
253
                                   this.nBands);
254
        }
255
    }
256

    
257
    /**
258
     * A partir de un elemento que contiene una propiedad y un valor
259
     * lo parsea y asigna el valor a su variable.
260
     * @param propValue        elemento con la forma propiedad=valor
261
     */
262
    private void readProperty(String propValue) {
263
        String prop = propValue.substring(0, propValue.indexOf("="));
264

    
265
        if (propValue.startsWith(prop)) {
266
            String value = propValue.substring(propValue.indexOf("=") + 1,
267
                                               propValue.length());
268

    
269
            if ((value != null) && !value.equals("")) {
270
                if (prop.equals("BLOCKSIZE")) {
271
                    this.support.setBlockSize(Integer.parseInt(value));
272
                }
273

    
274
                if (prop.equals("FORMAT")) {
275
                    int select = 0;
276

    
277
                    if (value.equals("UINT8")) {
278
                        select = 1;
279
                    }
280

    
281
                    if (value.equals("YUV")) {
282
                        select = 2;
283
                    }
284

    
285
                    if (value.equals("MULTI")) {
286
                        select = 3;
287
                    }
288

    
289
                    if (value.equals("RGB")) {
290
                        select = 4;
291
                    }
292

    
293
                    this.support.setFormatDefault(select);
294
                }
295

    
296
                if (prop.equals("GEOREF")) {
297
                    boolean georef = true;
298

    
299
                    if (value.equals("yes")) {
300
                        georef = true;
301
                    } else {
302
                        georef = false;
303
                    }
304

    
305
                    this.support.setWriteGeoref(georef);
306
                }
307

    
308
                if (prop.equals("COMPRESSION")) {
309
                    this.support.setCompressionDefault(Integer.parseInt(value));
310
                }
311
            }
312
        }
313
    }
314

    
315
    /**
316
     * Asigna propiedades al driver a partir de un vector de
317
     * strings donde cada elemento tiene la estructura de
318
     * propiedad=valor.
319
     * @param props        Propiedades
320
     */
321
    public void setProps(String[] props) {
322
        for (int iProps = 0; iProps < props.length; iProps++)
323
            readProperty(props[iProps]);
324

    
325
        try {
326
            if (!consulta) {
327
                init();
328
            }
329
        } catch (EcwException e) {
330
            e.printStackTrace();
331
        }
332
    }
333

    
334
    /**
335
     * Realiza la funci?n de compresi?n a partir de un GeoRasterFile.
336
     * @throws IOException
337
     */
338
    public void fileWrite() throws IOException {
339
        if (currentRaster == null) {
340
            throw new IOException("No se ha asignado un fichero de entrada.");
341
        }
342

    
343
        try {
344
            compressclient.NCSEcwCompressOpen(false);
345
            compressclient.NCSEcwCompress(readerObj);
346
        } catch (EcwException e) {
347
            e.printStackTrace();
348
        }
349
    }
350

    
351
    /**
352
     * Realiza la funci?n de compresi?n a partir de los datos pasados por el cliente.
353
     * @throws IOException
354
     */
355
    public void dataWrite() throws IOException {
356
        if (dataWriter == null) {
357
            throw new IOException("No se ha obtenido un objeto para la lectura valido.");
358
        }
359

    
360
        try {
361
            compressclient.NCSEcwCompressOpen(false);
362
            compressclient.NCSEcwCompress(readerObj);
363
        } catch (EcwException e) {
364
            e.printStackTrace();
365
        }
366
    }
367

    
368
    /**
369
     * Cierra el compresor ecw.
370
     */
371
    public void writeClose() {
372
        try {
373
            compressclient.NCSEcwCompressClose();
374
        } catch (EcwException e) {
375
            e.printStackTrace();
376
        }
377
    }
378
    
379
    /**
380
     * Cancela el compresor ecw.
381
     */
382
    public void writeCancel() {
383
        try {
384
                if(readerObj != null)
385
                        readerObj.setWrite(false);
386
            compressclient.NCSEcwCompressCancel();
387
        } catch (EcwException e) {
388
            e.printStackTrace();
389
        }
390
    }
391

    
392
    /**
393
     * Devuelve la configuraci?n de la ventana de dialogo
394
     * para las propiedades del driver de escritura de Ecw.
395
     * @return XML de configuraci?n del dialogo.
396
     */
397
    public String getXMLPropertiesDialog() {
398
        StringBuffer options = null;
399
        options = new StringBuffer();
400
        options.append("<window sizex=\"" + this.windowSizeX + "\" sizey=\"" +
401
                       this.windowSizeY + "\">");
402
        options.append("<panel sizex=\"" + this.panelSizeX + "\" sizey=\"" +
403
                       this.panelSizeY + "\" layout=\"" + this.panelLayout +
404
                       "\" border=\"yes\">");
405

    
406
        options.append("<panel layout=\"FlowLayout\" position=\"North\" align=\"left\">");
407
        options.append("<label>Tama?o bloque:</label>");
408
        options.append("<combo ident=\"BLOCKSIZE\" selected=\"" +
409
                       this.support.getBlockSize() + "\">");
410

    
411
        for (int i = 0; i < this.support.getBlockSizeList().length; i++)
412
            options.append("<elem>" + this.support.getBlockSizeList()[i] +
413
                           "</elem>");
414

    
415
        options.append("</combo>");
416
        /*options.append("<label>Formato:</label>");
417

418
        String sel = null;
419

420
        if (this.support.getGeoref()) {
421
            sel = new String("yes");
422
        } else {
423
            sel = new String("no");
424
        }
425

426
        options.append("<check ident=\"GEOREF\" selected=\"" + sel +
427
                       "\" text=\"Georef Si/No\">");
428
        options.append("</check>");*/
429
        options.append("</panel>");
430

    
431
        options.append("<panel layout=\"FlowLayout\" position=\"South\">");
432
        options.append("<slider ident=\"COMPRESSION\" name=\"Nivel de Compresi?n\" sizex=\"350\" sizey=\"20\">");
433
        options.append("<min>" + this.support.getCompressionList()[0] +
434
                       "</min>");
435
        options.append("<max>" + this.support.getCompressionList()[1] +
436
                       "</max>");
437
        options.append("<value>" + this.support.compresionDefault + "</value>");
438
        options.append("<minorspacing>" + this.support.getCompressionList()[3] +
439
                       "</minorspacing>");
440
        options.append("<majorspacing>" + this.support.getCompressionList()[4] +
441
                       "</majorspacing>");
442
        options.append("</slider>");
443
        options.append("</panel>");
444

    
445
        options.append("</panel>");
446
        options.append("</window>");
447

    
448
        return options.toString();
449
    }
450

    
451
    /**
452
     * @author Nacho Brodin <brodin_ign@gva.es>
453
     *
454
     * Opciones de escritura para el Ecw. Estas opciones son utilizadas para el
455
     * dialogo de propiedades de Salvar a Raster en Ecw.
456
     */
457
    class EcwSupportOptions extends WriterSupportOptions {
458
        private String[] compresionListValues = { "0", "20", "10", "1", "5" }; //min, max, valor defecto, intervalo peque?o, intervalo grande;
459
        private String[] formatList = { "NONE", "UINT8", "YUV", "MULTI", "RGB" };
460
        private int formatDefault = 4;
461
        private int compresionDefault = 10;
462

    
463
        EcwSupportOptions(String drv) {
464
            super(drv);
465
        }
466

    
467
        /**
468
         * Asigna la lista de valores de compresi?n
469
         * @param compresion        lista de valores
470
         */
471
        public void setCompressionList(String[] compresion) {
472
            this.compresionListValues = compresion;
473
        }
474

    
475
        /**
476
         * Valor de formato seleccionado en el driver.
477
         * 0-NONE, 1-UINT8, 2-YUV, 3-MULTI, 4-RGB
478
         * @param i        valor de formato
479
         */
480
        public void setFormatDefault(int i) {
481
            this.formatDefault = i;
482
        }
483

    
484
        /**
485
         * Asigna el nivel de compresi?npor defecto
486
         * @param compress        Nivel de compresi?n
487
         */
488
        public void setCompressionDefault(int compress) {
489
            this.compresionDefault = compress;
490
        }
491

    
492
        /**
493
         * Obtiene el tipo de imagen en un formato comprensible por el driver.
494
         * @return        Tipo de imagen
495
         */
496
        public String getStringFormat() {
497
            return "COMPRESS_" + formatList[formatDefault];
498
        }
499

    
500
        /**
501
         * Obtiene la lista de los tipos de formato disponible
502
         * @return Lista de tipos de formato
503
         */
504
        public String[] getFormatList() {
505
            return formatList;
506
        }
507

    
508
        /**
509
         * Obtiene el tipo de formato seleccionado en el driver
510
         * @return Tipo de formato seleccionado
511
         */
512
        public int getFormat() {
513
            return formatDefault;
514
        }
515

    
516
        /**
517
         * Obtiene la compresion seleccionada
518
         * @return Compresi?n seleccionada
519
         */
520
        public int getCompression() {
521
            return compresionDefault;
522
        }
523

    
524
        /**
525
         * Obtiene la lista de los valores para la generaci?n de la barra de
526
         * compresi?n. Son cinco valores m?nimo, m?ximo, seleccionado,
527
         * intervalo peque?o, intervalo grande.
528
         * @return lista de valores de compresi?n
529
         */
530
        public String[] getCompressionList() {
531
            return compresionListValues;
532
        }
533
    }
534
}
535

    
536

    
537
/**
538
 * Clase que se encarga de la lectura de datos que ser?n escritos. Hereda de JniObject
539
 * para asegurar  para asegurar que el interfaz de la libreria tiene acceso a variables
540
 * necesarias para la petici?n de datos cuando vacia el buffer. Implementa ReadCallBack
541
 * para obligar escribir el m?todo loadBuffer encargado de servir los datos cuando el
542
 * buffer se vacia.
543
 *
544
 */
545
class Reader extends JniObject implements ReadCallBack {
546
    private NCSEcwCompressClient        compressclient = null;
547
    private int                                         width;
548
    private int                                         height;
549
    private int                                         ulX;
550
    private int                                         ulY;
551
    private GeoRasterFile                         grf = null;
552
    private IDataWriter                         dataWriter = null;
553
    private GdalRasterBand                         rband = null;
554
    private int                                         blockSizeRead = 1; //Alto del bloque leido de la imagen origen 
555
    private int                                         countLine = 1; //Contador de l?neas procesadas en cada lectura de bloque
556
    byte[][]                                                 buf = null;
557
    byte[]                                                         bufband1 = null;
558
    byte[]                                                         bufband2 = null;
559
    byte[]                                                         bufband3 = null;
560
    byte[]                                                         bufband4 = null;
561
    byte[]                                                         bufband5 = null;
562
    byte[]                                                         bufband6 = null;
563
    int[]                                                         dataBuffer = null;
564
    private int                                         lineasBloqueFinal = 0; //N?mero de l?neas leidas del bloque final
565
    private int                                         nBlocks = 0; //N?mero de bloques completos
566
    private int                                         countBlock = 1; //Contador de bloques completos procesados
567
    private int                                         nBands = 0;
568
    private        boolean                                        write = true; //Cuando est? a true se puede escribir en la imagen de salida. Si est? a false el proceso es interrumpido
569

    
570
    /**
571
     * Constructor para la escritura de un fichero desde un GeoRasterFile
572
     * @param grf        GeorasterFile del fichero de origen
573
     * @param compressclient        Objeto que representa al compresor ecw
574
     * @param ulx        Coordenada X de la esquina superior izquierda
575
     * @param uly        Coordenada Y de la esquina superior izquierda
576
     * @param width        Ancho de la imagen
577
     * @param height        Alto de la imagen
578
     * @param blockSizeRead        Altura del bloque en la lectura
579
     */
580
    public Reader(GeoRasterFile grf, NCSEcwCompressClient compressclient,
581
                  int ulx, int uly, int width, int height, int blockSizeRead) {
582
        this.compressclient = compressclient;
583
        this.width = width;
584
        this.height = height;
585
        this.ulX = ulx;
586
        this.ulY = uly;
587
        this.grf = grf;
588
        this.nBands = grf.getBandCount();
589

    
590
        if (blockSizeRead != 0) {
591
            this.blockSizeRead = blockSizeRead;
592
        }
593

    
594
        nBlocks = (int) (height / this.blockSizeRead);
595
        lineasBloqueFinal = height - (nBlocks * this.blockSizeRead);
596

    
597
    }
598

    
599
    /**
600
     * Constructor para que los datos sean servidos desde el cliente a trav?s de un
601
     * IDataWriter.
602
     * @param dataWriter        Objeto servidor de datos del driver
603
     * @param compressclient        Objeto que representa al compresor ecw
604
     * @param width        Ancho de la imagen
605
     * @param height        Alto de la imagen
606
     * @param blockSizeRead        Altura del bloque en la lectura
607
     * @param nBands        N?mero de bandas
608
     */
609
    public Reader(IDataWriter dataWriter, NCSEcwCompressClient compressclient,
610
                  int width, int height, int blockSizeRead, int nBands) {
611
        this.compressclient = compressclient;
612
        this.width = width;
613
        this.height = height;
614
        this.dataWriter = dataWriter;
615
        this.nBands = nBands;
616

    
617
        if (blockSizeRead != 0) {
618
            this.blockSizeRead = blockSizeRead;
619
        }
620

    
621
        nBlocks = (int) (height / this.blockSizeRead);
622
        lineasBloqueFinal = height - (nBlocks * this.blockSizeRead);
623

    
624
    }
625

    
626
    /**
627
     * Lectura de bandas desde un GeoRasterFile
628
     * @param ulX        Coordenada X de la esquina superior izquierda
629
     * @param ulY        Coordenada Y de la esquina superior izquierda
630
     * @param width        Ancho de la imagen
631
     * @param lineasLeidas        N?mero de l?neas a leer del origen
632
     */
633
    private void readBandsGRFile(int ulX, int ulY, int width, int lineasLeidas) {
634
        if (nBands >= 1) {
635
            bufband1 = grf.getWindow(ulX, ulY, width, lineasLeidas, 1);
636
        }
637

    
638
        if (nBands >= 2) {
639
            bufband2 = grf.getWindow(ulX, ulY, width, lineasLeidas, 2);
640
        }
641

    
642
        if (nBands >= 3) {
643
            bufband3 = grf.getWindow(ulX, ulY, width, lineasLeidas, 3);
644
        }
645

    
646
        if (nBands >= 4) {
647
            bufband4 = grf.getWindow(ulX, ulY, width, lineasLeidas, 4);
648
        }
649

    
650
        if (nBands >= 5) {
651
            bufband5 = grf.getWindow(ulX, ulY, width, lineasLeidas, 5);
652
        }
653

    
654
        if (nBands >= 6) {
655
            bufband6 = grf.getWindow(ulX, ulY, width, lineasLeidas, 6);
656
        }
657
    }
658

    
659
    /**
660
     * Lectura de bandas llamando al objeto cliente para que nos cargue el buffer
661
     * @param width        Ancho de la imagen
662
     * @param height        Alto de la imagen
663
     */
664
    private void readBands(int width, int height) {
665
        dataBuffer = dataWriter.readARGBData(width, height, 0);
666
    }
667

    
668
    /**
669
     * M?todo obligado por el interfaz y que es llamado desde C cuando el
670
     * compresor necesita que le sirvan m?s datos.
671
     */
672
    public void loadBuffer() {
673
        int lineasLeidas = 0;
674

    
675
        //si se leen bloques completos lineasLeidas es == al tama?o de bloque sino
676
        //es que es el ?ltimo con lo que ser? del tama?o del bloque final
677
        if (countBlock <= nBlocks) {
678
            lineasLeidas = blockSizeRead;
679
        } else {
680
            lineasLeidas = lineasBloqueFinal;
681
        }
682

    
683
        //Leemos un bloque nuevo cuando se han procesado todas las l?neas del anterior
684
        if ((nNextLine % blockSizeRead) == 0) {
685
            if (grf != null) {
686
                readBandsGRFile(ulX, ulY + nNextLine, width, lineasLeidas);
687
            } else if (dataWriter != null) {
688
                readBands(width, lineasLeidas);
689
            }
690

    
691
            countLine = 0;
692
            countBlock++;
693
        }
694

    
695
        for (int iBand = 0; iBand < this.nBands; iBand++) {
696
            for (int pos = 0; pos < width; pos++) {
697
                if (grf != null && write) {
698
                    if (iBand == 0) {
699
                        compressclient.buffer[pos + (width * iBand)] = bufband1[pos +
700
                                                                       (width * countLine)];
701
                    }
702

    
703
                    if (iBand == 1) {
704
                        compressclient.buffer[pos + (width * iBand)] = bufband2[pos +
705
                                                                       (width * countLine)];
706
                    }
707

    
708
                    if (iBand == 2) {
709
                        compressclient.buffer[pos + (width * iBand)] = bufband3[pos +
710
                                                                       (width * countLine)];
711
                    }
712

    
713
                    if (iBand == 3) {
714
                        compressclient.buffer[pos + (width * iBand)] = bufband4[pos +
715
                                                                       (width * countLine)];
716
                    }
717

    
718
                    if (iBand == 4) {
719
                        compressclient.buffer[pos + (width * iBand)] = bufband5[pos +
720
                                                                       (width * countLine)];
721
                    }
722

    
723
                    if (iBand == 5) {
724
                        compressclient.buffer[pos + (width * iBand)] = bufband6[pos +
725
                                                                       (width * countLine)];
726
                    }
727
                } else {
728
                    if (iBand == 0) {
729
                        compressclient.buffer[pos + (width * iBand)] = (byte) ((dataBuffer[pos +
730
                                                                       (width * countLine)] &
731
                                                                       0xff0000) >> 16);
732
                    }
733

    
734
                    if (iBand == 1) {
735
                        compressclient.buffer[pos + (width * iBand)] = (byte) ((dataBuffer[pos +
736
                                                                       (width * countLine)] &
737
                                                                       0xff00) >> 8);
738
                    }
739

    
740
                    if (iBand == 2) {
741
                        compressclient.buffer[pos + (width * iBand)] = (byte) (dataBuffer[pos +
742
                                                                       (width * countLine)] &
743
                                                                       0xff);
744
                    }
745
                }
746

    
747
                if ((pos == (width - 1)) && (iBand == (this.nBands - 1))) {
748
                    countLine++;
749
                }
750
            }
751
        }
752
    }
753

    
754
    /**
755
     * M?todo obligado por el interfaz y que es llamado desde C cuando el
756
     * compresor actualiza el porcentaje de compresi?n
757
     */
758
    public void updatePercent() {
759
        System.out.println(compressclient.getPercent() + "%");
760
    }
761
    
762
    /**
763
     * Obtiene el valor a la variable write que estar? a true cuando se est? escribiendo
764
     *  o puede escribirse la imagen de salida. El cancelar la operaci?n de escritura
765
     * pondr? esta variable a false deteniendose la escritura y cerrandose el dataset
766
     * de salida. 
767
     * @return True si puede escribirse y false si no puede
768
     */
769
    public boolean isWrite() {
770
                return write;
771
        }
772

    
773
    /**
774
     * Asigna el valor a la variable write que estar? a true cuando se est? escribiendo
775
     *  o puede escribirse la imagen de salida. El cancelar la operaci?n de escritura
776
     * pondr? esta variable a false deteniendose la escritura y cerrandose el dataset
777
     * de salida. 
778
     * @param write Variable booleana. True si puede escribirse y false si no puede
779
     */
780
        public void setWrite(boolean write) {
781
                this.write = write;
782
        }
783
        
784
}