Statistics
| Revision:

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

History | View | Annotate | Download (13.7 KB)

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

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

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

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

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

    
299

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

    
311
    return pnXSize;
312
  }
313

    
314

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

    
358

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

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