Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libjni-ecwcompress / src-test / TestCompressEcw.java @ 20701

History | View | Annotate | Download (4.77 KB)

1
import es.gva.cit.jecwcompress.*;
2
import es.gva.cit.jgdal.*;
3

    
4
public class TestCompressEcw{
5
        
6
        public static void Uso(){
7
                System.out.println("Uso 1: Comprime una ventana de la imagen.");
8
                System.out.println("        java TifToEcw file-src file-dst compresi?n ulX ulY sizeX sizeY");
9
                System.out.println("Uso 2: Comprime a tama?o completo de la im?gen.");
10
                System.out.println("        java TifToEcw file-src file-dst compresi?n");
11
        }
12
        
13
        public static void main(String[] args){
14
 
15
          Gdal migdal=new Gdal();
16
          NCSEcwCompressClient client;
17
          Read lectura=null;
18
         
19
          if(args.length != 3 && args.length != 7){
20
                  TestCompressEcw.Uso();
21
                  return;
22
          }
23
          
24
          try{
25
                    migdal.open(args[0],Gdal.GA_ReadOnly);
26
          }catch(Exception ge){
27
                        ge.printStackTrace();
28
                        //...........
29
          }
30
          
31
          GdalRasterBand mirasterband=null;  
32
          String st;
33
          int nxsize=0;
34
          int nysize=0;
35
          int ulX=0;
36
          int ulY=0;
37
          int widthcompress = 0;
38
          int heightcompress = 0;
39
          int rastercount=0;
40
          String rasterband; 
41
          GeoTransform gt=null;          
42
                    
43
          try{
44
                  rastercount=migdal.getRasterCount();
45
            nxsize = migdal.getRasterXSize();
46
            nysize = migdal.getRasterYSize();
47
            System.out.println("N BANDAS="+rastercount+" SIZEX="+nxsize+" SIZEY="+nysize);
48
            System.out.println(migdal.getProjectionRef());        
49
          }catch(GdalException ge){
50
                                                          ge.printStackTrace();
51
                                                          //...........
52
                                                     }
53
          
54
          if(args.length == 7){
55
                  ulX = Integer.parseInt(args[3]);
56
                  ulY = Integer.parseInt(args[4]);
57
                  widthcompress = Integer.parseInt(args[5]);
58
                heightcompress = Integer.parseInt(args[6]);
59
                if(ulX<0 || ulY<0 || (ulX+widthcompress)>nxsize || (ulY+heightcompress)>nysize){
60
                        System.out.println("Error en los l?mites de la ventana solicitada");
61
                        return;
62
                }
63
                
64
          }else{
65
                  widthcompress = nxsize;
66
                heightcompress = nysize;
67
          }
68
          
69
          
70
          try{
71
                  gt=migdal.getGeoTransform();
72
                  System.out.println("Origins = ("+gt.adfgeotransform[0]+","+gt.adfgeotransform[3]+")");
73
                  System.out.println("Pixel Size = ("+gt.adfgeotransform[1]+","+gt.adfgeotransform[5]+")");
74
          }catch(GdalException e){
75
                          System.out.println("No puedo obtener el array geoTransform.");
76
                        e.printStackTrace();
77
                        //...........
78
          }
79
          
80
          try{
81
                  client = new NCSEcwCompressClient();
82
                  
83
                  client.setOutputFilename(args[1]);          
84
                  client.setInputFilename(args[0]);
85
            client.setTargetCompress(Double.parseDouble(args[2]));
86
            client.setInOutSizeX(widthcompress);
87
            client.setInOutSizeY(heightcompress);
88
            client.setInputBands(rastercount);
89
            
90
                if(rastercount == 1) {
91
                        client.setCompressFormat(CompressFormat.COMPRESS_UINT8);
92
                }
93
                else if(rastercount == 3) {
94
                        client.setCompressFormat(CompressFormat.COMPRESS_RGB);
95
                }
96
                else {
97
                        client.setCompressFormat(CompressFormat.COMPRESS_MULTI);
98
                }
99
                //client.setDatum("UTM Zona 31N");
100
                client.setProjection("WGS84");
101
                client.setCellIncrementX(gt.adfgeotransform[1]);
102
                client.setCellIncrementY(gt.adfgeotransform[5]);
103
                client.setOriginX(gt.adfgeotransform[0]);
104
                client.setOriginY(gt.adfgeotransform[3]);
105
                client.setCellSizeUnits(1);
106
                
107
                System.out.println(Double.parseDouble(args[2]));
108
        
109
                lectura = new Read(migdal, client, ulX, ulY, widthcompress, heightcompress);
110
                client.NCSEcwCompressOpen(false);
111
                client.NCSEcwCompress(lectura);
112
                client.NCSEcwCompressClose();
113
                                  
114
          }catch(EcwException e){
115
                e.printStackTrace();
116
                //...........
117
          }
118
          
119
                             
120
          try{                                  
121
            migdal.close();
122
          }catch(GdalException e){
123
                                                                  e.printStackTrace();
124
                                                           }
125
          
126
          
127
        }  
128
}
129

    
130
/**
131
 *
132
 * Para la lectura hay que hacer una clase que extienda de JniObject e implemente ReadCallBack
133
 * . Esto obliga a crear un m?todo loadBuffer. En el hay que meter la funcionalidad para que
134
 * llene el buffer. El buffer esta en la clase cliente y tendr? una longitud de 
135
 * ancho de una l?nea * n?mero de bandas
136
 */
137

    
138
class Read extends JniObject implements ReadCallBack {
139
        
140
        private GdalBuffer buf=null;
141
        private GdalRasterBand mirasterband=null;
142
        private Gdal migdal=null;
143
        private NCSEcwCompressClient client=null;
144
        private int width, height;
145
        private int ulX, ulY;
146
        
147
        public Read(Gdal g, NCSEcwCompressClient client,int ulx, int uly, int width, int height){
148
                migdal = g;
149
                this.client = client;
150
                this.width = width;
151
                this.height = height;
152
                this.ulX = ulx;
153
                this.ulY = uly;
154
        }
155
        
156
        public void loadBuffer(){
157
        
158
                try{
159
                        for(int iBand=0;iBand<migdal.getRasterCount();iBand++){
160
                                mirasterband=migdal.getRasterBand(iBand+1);
161
                                  buf = mirasterband.readRaster(ulX, ulY+nNextLine, width, 1, width, 1, 1);
162
                                  for(int pos=0; pos<width; pos++){
163
                                          client.buffer[pos+(width*iBand)]=buf.buffByte[pos];
164
                                  }
165
                        }
166
                  }catch(GdalException e){ 
167
                          System.out.println("Error en la lectura de lineas con Gdal.");
168
                          System.exit(1);
169
                  }
170
                  
171
                   
172
                                  
173
        }
174
        
175
        public void updatePercent(){
176
                System.out.println(client.getPercent()+"%");
177
        }
178
}