Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libjni-gdal / src / rasterband_interfaz.c @ 829

History | View | Annotate | Download (13.7 KB)

1
/**********************************************************************
2
 * $Id: rasterband_interfaz.c 829 2005-01-04 12:30:57Z igbrotru $
3
 *
4
 * Name:     rasterband_interfaz.c
5
 * Project:  JGDAL. Interface java to gdal (Frank Warmerdam).
6
 * Purpose:  Raster band's Basic Funcions.
7
 * Author:   Nacho Brodin, brodin_ign@gva.es
8
 *
9
 **********************************************************************/
10
/*Copyright (C) 2004  Nacho Brodin <brodin_ign@gva.es>
11

12
 This program is free software; you can redistribute it and/or
13
 modify it under the terms of the GNU General Public License
14
 as published by the Free Software Foundation; either version 2
15
 of the License, or (at your option) any later version.
16

17
 This program is distributed in the hope that it will be useful,
18
 but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 GNU General Public License for more details.
21

22
 You should have received a copy of the GNU General Public License
23
 along with this program; if not, write to the Free Software
24
 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
25
 */
26

    
27

    
28

    
29
#include <jni.h>
30
#include "es_gva_cit_jgdal_GdalRasterBand.h"
31
#include "es_gva_cit_jgdal_JNIBase.h"
32
#include "gdal.h"
33
#include "cpl_string.h"
34

    
35

    
36

    
37

    
38
  
39
/******************************************************************************/
40
//                                                        GetRasterBandXSize
41
/******************************************************************************/
42

    
43

    
44
JNIEXPORT jint JNICALL Java_es_gva_cit_jgdal_JNIBase_getRasterBandXSizeNat
45
  (JNIEnv *env, jobject obj, jlong cPtr){
46
          
47
          GDALRasterBandH *rb  = (GDALRasterBandH *) 0 ;
48
          int res = -1;
49
                    
50
    rb = *(GDALRasterBandH **)&cPtr;
51
    
52
    if(rb!=NULL)
53
            res = GDALGetRasterBandXSize(rb);        
54

    
55

    
56
    return res;
57
  }
58
  
59
  
60
/******************************************************************************/ 
61
//                                                        GetRasterBandYSize
62
/******************************************************************************/
63

    
64

    
65
JNIEXPORT jint JNICALL Java_es_gva_cit_jgdal_JNIBase_getRasterBandYSizeNat
66
  (JNIEnv *env, jobject obj, jlong cPtr){
67
          
68
          GDALRasterBandH *rb  = (GDALRasterBandH *) 0 ;
69
          int res = -1;
70
                    
71
    rb = *(GDALRasterBandH **)&cPtr;
72
    
73
    if(rb!=NULL)
74
            res = GDALGetRasterBandYSize(rb);        
75

    
76
    return res;
77
          
78
  }
79
  
80
 /****************************************************************************/
81
 //                                                         GetOverviewCount
82
 /******************************************************************************/
83
  
84
  
85
  JNIEXPORT jint JNICALL Java_es_gva_cit_jgdal_JNIBase_getOverviewCountNat
86
  (JNIEnv *env, jobject obj, jlong cPtr){
87
          
88
          GDALRasterBandH *rb  = (GDALRasterBandH *) 0 ;
89
          int res = -1;
90
                    
91
    rb = *(GDALRasterBandH **)&cPtr;
92
    
93
    if(rb!=NULL)
94
                res = GDALGetOverviewCount(rb);        
95

    
96
    return res;
97
    
98
  }
99
  
100
/******************************************************************************/   
101
//                                                                GetOverview
102
/******************************************************************************/
103

    
104

    
105
JNIEXPORT jlong JNICALL Java_es_gva_cit_jgdal_GdalRasterBand_getOverviewNat
106
  (JNIEnv *env, jobject obj, jlong cPtr, jint noverview){
107
          
108
        GDALRasterBandH *rb  = (GDALRasterBandH *) 0 ;
109
        GDALRasterBandH *res = NULL;
110
        jlong jresult = 0;
111
          
112
        rb = *(GDALRasterBandH **)&cPtr;
113
    
114
    if(rb!=NULL)
115
            res = GDALGetOverview(rb,(int)noverview);
116
    
117
    *(GDALDatasetH **)&jresult = res;
118
        
119
    if(res==NULL)return -1;
120
    else return jresult;
121
                  
122
  }
123
  
124
  
125
/******************************************************************************/     
126
//                                                                ReadRaster
127
/******************************************************************************/  
128
  
129
  
130
JNIEXPORT jobject JNICALL Java_es_gva_cit_jgdal_GdalRasterBand_readRasterNat
131
  (JNIEnv *env, jobject obj, jlong cPtr, jint nXOff, jint nYOff, jint nXSize, jint nYSize, jint BufXSize, jint BufYSize, jint eBufType){
132

    
133
                  jclass class_gdalbuffer;
134
                  jmethodID metodo;
135
                  jobject obj_gdalbuffer = NULL;
136
                  jfieldID id_campo;
137
                  
138
                  GDALRasterBandH *rb  = (GDALRasterBandH *) 0 ;
139
                  
140
                  jbyteArray bytearray;
141
                  jshortArray shortarray;
142
                  jintArray intarray;
143
                  jfloatArray floatarray;
144
                  jdoubleArray doublearray;
145
                  
146
                  unsigned char *pabyScanLineBuf;
147
                  int *buffInt=NULL;
148
                  unsigned int *buffUInt=NULL;
149
                  long *buffLong=NULL;
150
                  unsigned long *buffULong=NULL;
151
                  float *buffFloat=NULL;
152
                  double *buffDouble=NULL;
153
                  
154
                  int tipo=0;
155

    
156
                  
157
                  
158
            rb = *(GDALRasterBandH **)&cPtr;
159
                  
160
                  if(rb!=NULL){
161
                          
162
                  //Creamos el objeto java que contendr? la l?nea
163
                  
164
                          class_gdalbuffer = (*env)->FindClass (env, "es/gva/cit/jgdal/GdalBuffer");
165
                          
166
                          if(eBufType==1){
167
                                  metodo = (*env)->GetMethodID(env, class_gdalbuffer, "reservaByte", "(I)V");
168
                                  obj_gdalbuffer = (*env)->NewObject (env,class_gdalbuffer,metodo,(int)(nXSize*nYSize));
169
                                  id_campo = (*env)->GetFieldID(env, class_gdalbuffer, "buffByte", "[B");
170
                                            
171
                           //Reservamos memoria para el buffer y lo cargamos llamando a GDALRasterIO
172
                                            
173
                                  pabyScanLineBuf = (unsigned char*) CPLMalloc(sizeof(unsigned char) *(int)nXSize * (int)nYSize);
174
                                  GDALRasterIO(rb, GF_Read,  (int)nXOff, (int)nYOff, (int)nXSize, (int)nYSize, pabyScanLineBuf, (int)BufXSize, (int)BufYSize, GDT_Byte, 0, 0);
175
                                  
176
                                  bytearray = (*env)->NewByteArray(env,(int)(nXSize*nYSize));
177
                            if(bytearray!=NULL){
178
                                          (*env)->SetByteArrayRegion(env, bytearray, 0, (int)(nXSize*nYSize),(jbyte *)pabyScanLineBuf); 
179
                                          (*env)->SetObjectField(env, obj_gdalbuffer, id_campo, bytearray);
180
                                }
181
                                if(pabyScanLineBuf != NULL)CPLFree(pabyScanLineBuf);
182
                          }else
183
                          
184
                          //------------------------------------------------------------------
185
                          
186
                          if(eBufType==2 || eBufType==3 || eBufType==8){
187
                                  metodo = (*env)->GetMethodID(env, class_gdalbuffer, "reservaShort", "(I)V");
188
                                  obj_gdalbuffer = (*env)->NewObject (env,class_gdalbuffer,metodo,(int)(nXSize*nYSize));
189
                                  id_campo = (*env)->GetFieldID(env, class_gdalbuffer, "buffShort", "[S");
190
                                            
191
                           //Reservamos memoria para el buffer y lo cargamos llamando a GDALRasterIO
192
                                                                              
193
                                  if(eBufType==2){
194
                                          buffUInt = (unsigned int*) CPLMalloc(sizeof(unsigned int) *(int)nXSize * (int)nYSize);
195
                                          GDALRasterIO(rb, GF_Read,  (int)nXOff, (int)nYOff, (int)nXSize, (int)nYSize, buffUInt, (int)BufXSize, (int)BufYSize, GDT_UInt16, 0, 0);
196
                                            shortarray = (*env)->NewShortArray(env,(int)(nXSize*nYSize));
197
                                    if(shortarray!=NULL){
198
                                                  (*env)->SetShortArrayRegion(env, shortarray, 0, (int)(nXSize*nYSize),(jshort *)buffUInt); 
199
                                                  (*env)->SetObjectField(env, obj_gdalbuffer, id_campo, shortarray);
200
                                        }
201
                                        if(buffUInt != NULL)CPLFree(buffUInt);
202
                                        return obj_gdalbuffer;                
203
                                  }
204
                                  
205
                                  buffInt = (int*) CPLMalloc(sizeof(int) *(int)nXSize * (int)nYSize);
206
                                  GDALRasterIO(rb, GF_Read,  (int)nXOff, (int)nYOff, (int)nXSize, (int)nYSize, buffInt, (int)BufXSize, (int)BufYSize, (int)eBufType, 0, 0);
207
                                  shortarray = (*env)->NewShortArray(env,(int)(nXSize*nYSize));
208
                            if(shortarray!=NULL){
209
                                          (*env)->SetShortArrayRegion(env, shortarray, 0, (int)(nXSize*nYSize),(jshort *)buffInt); 
210
                                         (*env)->SetObjectField(env, obj_gdalbuffer, id_campo, shortarray);
211
                                }
212

    
213
                                if(buffInt != NULL)CPLFree(buffInt);
214
                                
215
                          }else
216
                          
217
                          //------------------------------------------------------------------
218
                          
219
                          if(eBufType==4 || eBufType==5 || eBufType==9){
220
                                  metodo = (*env)->GetMethodID(env, class_gdalbuffer, "reservaInt", "(I)V");
221
                                  obj_gdalbuffer = (*env)->NewObject (env,class_gdalbuffer,metodo,(int)(nXSize*nYSize));
222
                                  id_campo = (*env)->GetFieldID(env, class_gdalbuffer, "buffInt", "[I");
223
                                            
224
                           //Reservamos memoria para el buffer y lo cargamos llamando a GDALRasterIO
225
                                                                              
226
                                  if(eBufType==4){
227
                                          buffULong = (unsigned long*) CPLMalloc(sizeof(unsigned long) *(int)nXSize * (int)nYSize);
228
                                          GDALRasterIO(rb, GF_Read,  (int)nXOff, (int)nYOff, (int)nXSize, (int)nYSize, buffULong, (int)BufXSize, (int)BufYSize, GDT_UInt32, 0, 0);
229
                                          intarray = (*env)->NewIntArray(env,(int)(nXSize*nYSize));
230
                                    if(intarray!=NULL){
231
                                                  (*env)->SetIntArrayRegion(env, intarray, 0, (int)(nXSize*nYSize),(jint *)buffULong); 
232
                                                  (*env)->SetObjectField(env, obj_gdalbuffer, id_campo, intarray);
233
                                        }
234
                                        if(buffULong != NULL)CPLFree(buffULong);
235
                                        return obj_gdalbuffer;
236
                                  }
237
                                  
238
                                  buffLong = (long*) CPLMalloc(sizeof(long) *(int)nXSize * (int)nYSize);
239
                                  GDALRasterIO(rb, GF_Read,  (int)nXOff, (int)nYOff, (int)nXSize, (int)nYSize, buffLong, (int)BufXSize, (int)BufYSize, (int)eBufType, 0, 0);
240
                                  intarray = (*env)->NewIntArray(env,(int)(nXSize*nYSize));
241
                            if(intarray!=NULL){
242
                                          (*env)->SetIntArrayRegion(env, intarray, 0, (int)(nXSize*nYSize),(jint *)buffLong); 
243
                                         (*env)->SetObjectField(env, obj_gdalbuffer, id_campo, intarray);
244
                                }
245

    
246
                                if(buffLong != NULL)CPLFree(buffLong);
247
                                
248
                          }else
249
                          
250
                          //------------------------------------------------------------------
251
                          
252
                          if(eBufType==6 || eBufType==10){
253
                                  metodo = (*env)->GetMethodID(env, class_gdalbuffer, "reservaFloat", "(I)V");
254
                                  obj_gdalbuffer = (*env)->NewObject (env,class_gdalbuffer,metodo,(int)(nXSize*nYSize));
255
                                  id_campo = (*env)->GetFieldID(env, class_gdalbuffer, "buffFloat", "[F");
256
                                            
257
                                   //Reservamos memoria para el buffer y lo cargamos llamando a GDALRasterIO
258
                           
259
                                   buffFloat = (float*) CPLMalloc(sizeof(float) *(int)nXSize * (int)nYSize);
260
                                  GDALRasterIO(rb, GF_Read,  (int)nXOff, (int)nYOff, (int)nXSize, (int)nYSize, buffFloat, (int)BufXSize, (int)BufYSize, (int)eBufType, 0, 0);
261
                                  floatarray = (*env)->NewFloatArray(env,(int)(nXSize*nYSize));
262
                            if(floatarray!=NULL){
263
                                          (*env)->SetFloatArrayRegion(env, floatarray, 0, (int)(nXSize*nYSize),(jfloat *)buffFloat); 
264
                                         (*env)->SetObjectField(env, obj_gdalbuffer, id_campo, floatarray);
265
                                }
266

    
267
                                if(buffFloat != NULL)CPLFree(buffFloat);
268
                          }else
269
                          
270
                          //------------------------------------------------------------------
271
                          
272
                          if(eBufType==7 || eBufType==11){
273
                                  metodo = (*env)->GetMethodID(env, class_gdalbuffer, "reservaDouble", "(I)V");
274
                                  obj_gdalbuffer = (*env)->NewObject (env,class_gdalbuffer,metodo,(int)(nXSize*nYSize));
275
                                  id_campo = (*env)->GetFieldID(env, class_gdalbuffer, "buffDouble", "[D");
276
                                            
277
                                   //Reservamos memoria para el buffer y lo cargamos llamando a GDALRasterIO
278
                           
279
                                   buffDouble = (double*) CPLMalloc(sizeof(double) *(int)nXSize * (int)nYSize);
280
                                  GDALRasterIO(rb, GF_Read,  (int)nXOff, (int)nYOff, (int)nXSize, (int)nYSize, buffDouble, (int)BufXSize, (int)BufYSize, (int)eBufType, 0, 0);
281
                                  doublearray = (*env)->NewDoubleArray(env,(int)(nXSize*nYSize));
282
                            if(doublearray!=NULL){
283
                                          (*env)->SetDoubleArrayRegion(env, doublearray, 0, (int)(nXSize*nYSize),(jdouble *)buffDouble); 
284
                                         (*env)->SetObjectField(env, obj_gdalbuffer, id_campo, doublearray);
285
                                }
286

    
287
                                if(buffDouble != NULL)CPLFree(buffDouble);
288
                          }
289
                          
290
                  }//if(rb!=NULL)
291
                                    
292
                  return obj_gdalbuffer;
293
                  
294
  } 
295
  
296
  
297
    
298
/******************************************************************************/
299
//                                                                GetBlockXSize
300
/******************************************************************************/
301

    
302

    
303
JNIEXPORT jint JNICALL Java_es_gva_cit_jgdal_JNIBase_getBlockXSizeNat
304
  (JNIEnv *env, jobject obj, jlong cPtr){
305
          
306
          GDALRasterBandH *rb  = (GDALRasterBandH *) 0 ;
307
          int pnXSize=-1,pnYSize=-1;
308
                    
309
    rb = *(GDALRasterBandH **)&cPtr;
310
    
311
    if(rb!=NULL)
312
            GDALGetBlockSize( rb,&pnXSize, &pnYSize );
313

    
314
    return pnXSize;
315
  }
316

    
317

    
318
/******************************************************************************/     
319
//                                                                WriteRaster
320
/******************************************************************************/  
321
  
322
JNIEXPORT void JNICALL Java_es_gva_cit_jgdal_GdalRasterBand_writeRasterNat
323
  (JNIEnv *env, jobject obj, jlong cPtr, jint nXOff, jint nYOff, jint nXSize, jint nYSize, jobject buffer, jint eBufType){
324
          
325
                  GDALRasterBandH *rb  = (GDALRasterBandH *) 0 ;
326
                  jclass clase;
327
                  jfieldID id_campo;
328
                  jmethodID metodo;
329
                  jbyteArray bytearray;
330
                  int tam;
331
                  unsigned char *abyRaster;
332
                  
333
                  
334
                  
335
                  clase = (*env)->GetObjectClass(env, buffer);
336
                  id_campo = (*env)->GetFieldID(env, clase, "pabyScanLineBuf", "[B");
337
                  metodo = (*env)->GetMethodID(env, clase, "getSize","()I");
338
                  tam = (*env)->CallIntMethod(env,buffer,metodo);
339
                  
340
                  bytearray =(jbyteArray)(*env)->GetObjectField(env, buffer, id_campo); 
341
                  
342
                  abyRaster=(unsigned char *)malloc(sizeof(unsigned char)*tam);
343
                  
344
                  if(bytearray!=NULL){
345
                      (*env)->GetByteArrayRegion(env,bytearray,0,tam,(jbyte *)abyRaster); 
346
                }
347
                  
348
                  rb = *(GDALRasterBandH **)&cPtr;
349
                  
350
                  if(rb!=NULL)
351
                          GDALRasterIO( rb, GF_Write, nXOff, nYOff, nXSize, nYSize, 
352
                  abyRaster, nXSize, nYSize, GDT_Byte, 0, 0 );
353
                  
354
        free(abyRaster);
355
  }
356
    
357
/******************************************************************************/
358
//                                                                GetBlockYSize
359
/******************************************************************************/
360

    
361

    
362
JNIEXPORT jint JNICALL Java_es_gva_cit_jgdal_JNIBase_getBlockYSizeNat
363
  (JNIEnv *env, jobject obj, jlong cPtr){
364
          
365
          GDALRasterBandH *rb  = (GDALRasterBandH *) 0 ;
366
          int pnXSize=-1,pnYSize=-1;
367
                    
368
    rb = *(GDALRasterBandH **)&cPtr;
369
    
370
    if(rb!=NULL)
371
            GDALGetBlockSize( rb,&pnXSize, &pnYSize );
372

    
373
    return pnYSize;
374
  }
375
  
376
/******************************************************************************/
377
//                                                                GetRasterDataType
378
/******************************************************************************/  
379
  
380
  JNIEXPORT jint JNICALL Java_es_gva_cit_jgdal_JNIBase_getRasterDataTypeNat
381
  (JNIEnv *env, jobject obj, jlong cPtr){
382
          
383
          int datatype = -1;
384
          GDALRasterBandH *rb  = (GDALRasterBandH *) 0 ;
385
          
386
          rb = *(GDALRasterBandH **)&cPtr;
387
          
388
          if(rb!=NULL)
389
            datatype = GDALGetRasterDataType( rb );
390
 
391
    return datatype;
392
  }
393
  
394