/********************************************************************** * $Id$ * * Name: bsb_interfaz.c * Project: JGDAL. Interface java to gdal (Frank Warmerdam). * Purpose: Interface functions to manage bsb files. This include gdal * code but it doesn't use the gdal API. * Author: Nacho Brodin, brodin_ign@gva.es * **********************************************************************/ /* gvSIG. Sistema de Información Geográfica de la Generalitat Valenciana * * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana. * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA. * * For more information, contact: * * Generalitat Valenciana * Conselleria d'Infraestructures i Transport * Av. Blasco Ibáñez, 50 * 46010 VALENCIA * SPAIN * * +34 963862235 * gvsig@gva.es * www.gvsig.gva.es * * or * * IVER T.I. S.A * Salamanca 50 * 46005 Valencia * Spain * * +34 963163400 * dac@iver.es */ #include #include "es_gva_cit_jbsb_Bsb.h" #include "bsb_read.h" int tam; BSBInfo *g_psInfo; /****************************************************************/ /*Carga un objeto jobject a partir de una estructura BSBInfo*/ jobject cargarJobject(JNIEnv *env, BSBInfo *psInfo, jobject *ref){ jclass class_bsbinfo; jfieldID id_campo; jobject obj_bsbinfo; jmethodID metodo; jshortArray jvect_short, array_strings; jintArray jvect_int; jshort *vect_short; jint *vect_int; jclass class_string; int i; int nitems_header; //tama? de memoria reservada en papszHeader int cont_arrays; //Numero de arrays en papszHeader jlong jresult=0; //Obtenemos la direccion del BSBInfo *(BSBInfo **)&jresult = g_psInfo; //Calculamos el numero de bytes reservados del campo papszHeader cont_arrays=0; nitems_header=0; while(psInfo->papszHeader[cont_arrays]!=NULL){ //printf("%s %d\n",psInfo->papszHeader[cont_arrays],cont_arrays); nitems_header+=strlen(psInfo->papszHeader[cont_arrays]); cont_arrays++; } //printf("%d %d\n",cont_arrays,sizeof(nitems_header)); //obtenemos la clase BSBInfo y creamos un objeto de este tipo if(ref==NULL){ class_bsbinfo = (*env)->FindClass (env, "es/gva/cit/jbsb/BsbInfo"); metodo = (*env)->GetMethodID(env, class_bsbinfo, "reserva", "(III)V"); obj_bsbinfo = (*env)->NewObject (env,class_bsbinfo,metodo,psInfo->nPCTSize*3,cont_arrays/*sizeof(nitems_header)*/,psInfo->nYSize); if(obj_bsbinfo!=NULL){ id_campo = (*env)->GetFieldID(env, class_bsbinfo, "nXSize", "I"); (*env)->SetIntField(env, obj_bsbinfo, id_campo, psInfo->nXSize); id_campo = (*env)->GetFieldID(env, class_bsbinfo, "nYSize", "I"); (*env)->SetIntField(env, obj_bsbinfo, id_campo, psInfo->nYSize); id_campo = (*env)->GetFieldID(env, class_bsbinfo, "nPCTSize", "I"); (*env)->SetIntField(env, obj_bsbinfo, id_campo, psInfo->nPCTSize); id_campo = (*env)->GetFieldID(env, class_bsbinfo, "cPtr", "J"); (*env)->SetLongField(env, obj_bsbinfo, id_campo, jresult); //----------------------------------------------------------------------- //CARGAMOS EL CAMPO DE JOBJECT pabyPCT //----------------------------------------------------------------------- //printf("pabyPCT %d",psInfo->nPCTSize); vect_short = (jshort *) calloc(sizeof(jchar),(psInfo->nPCTSize*3)); for(i=0;i<(psInfo->nPCTSize*3);i++){ vect_short[i]=(jchar)(psInfo->pabyPCT[i]); } jvect_short = (*env)->NewShortArray(env, psInfo->nPCTSize*3); if(jvect_short!=NULL){ (*env)->SetShortArrayRegion(env, jvect_short, 0, psInfo->nPCTSize*3,(jshort *)vect_short); id_campo = (*env)->GetFieldID(env, class_bsbinfo, "pabyPCT", "[S"); (*env)->SetObjectField(env, obj_bsbinfo, id_campo, jvect_short); free(vect_short); }else{ free(vect_short); return NULL; } //printf("tama? papszHeader %d",nitems_header); id_campo = (*env)->GetFieldID(env, class_bsbinfo, "papszHeader", "[Ljava/lang/String;"); class_string = (*env)->FindClass (env, "java/lang/String"); array_strings=(*env)->NewObjectArray(env, cont_arrays, class_string, (*env)->NewStringUTF(env,"")); if(array_strings!=NULL){ for(i=0;iSetObjectArrayElement(env,array_strings,i,(*env)->NewStringUTF(env,psInfo->papszHeader[i])); (*env)->SetObjectField(env, obj_bsbinfo, id_campo, array_strings); }else return NULL; //printf("tama? panLineOffset %d",sizeof(*psInfo->panLineOffset)); vect_int = (jint *) calloc(sizeof(jint),psInfo->nYSize); for(i=0;i< psInfo->nYSize;i++){ vect_int[i]=(jint)(psInfo->panLineOffset[i]); } jvect_int = (*env)->NewIntArray(env, psInfo->nYSize); if(jvect_int!=NULL){ (*env)->SetIntArrayRegion(env, jvect_int, 0, psInfo->nYSize,vect_int); id_campo = (*env)->GetFieldID(env, class_bsbinfo, "panLineOffset", "[I"); (*env)->SetObjectField(env, obj_bsbinfo, id_campo, jvect_int); free(vect_int); }else{ free(vect_int); return NULL; } id_campo = (*env)->GetFieldID(env, class_bsbinfo, "nColorSize", "I"); (*env)->SetIntField(env, obj_bsbinfo, id_campo, psInfo->nColorSize); id_campo = (*env)->GetFieldID(env, class_bsbinfo, "nVersion", "I"); (*env)->SetIntField(env, obj_bsbinfo, id_campo, psInfo->nVersion); id_campo = (*env)->GetFieldID(env, class_bsbinfo, "bNO1", "I"); (*env)->SetIntField(env, obj_bsbinfo, id_campo, psInfo->bNO1); id_campo = (*env)->GetFieldID(env, class_bsbinfo, "bNewFile", "I"); (*env)->SetIntField(env, obj_bsbinfo, id_campo, psInfo->bNewFile); id_campo = (*env)->GetFieldID(env, class_bsbinfo, "nLastLineWritten", "I"); (*env)->SetIntField(env, obj_bsbinfo, id_campo, psInfo->nLastLineWritten); }else return NULL; return obj_bsbinfo; }else{ return NULL; } } /****************************************************************/ /*Llamada a la funci? BSBOpen de bsb_read.c. Convierte los par?tros que viene de java * , llama a la funci? y construye el objeto para devolver el resultado*/ JNIEXPORT jobject JNICALL Java_es_gva_cit_jbsb_Bsb_bsbOpenNat(JNIEnv *env, jobject obj, jstring pszF) { const char *pszFilename = (*env)->GetStringUTFChars(env, pszF, 0); g_psInfo = BSBOpen(pszFilename); (*env)->ReleaseStringUTFChars(env, pszF, pszFilename); return cargarJobject(env,g_psInfo,NULL); } /****************************************************************/ //Función que encapsula a BSBReadLine cd bsb_read.c. Convierte los parámetros que viene de // java y llama a la función cd C que lee una linea. JNIEXPORT jint JNICALL Java_es_gva_cit_jbsb_Bsb_bsbReadLineNat( JNIEnv *env, jobject obj, jlong cPtr, jobject buf, jint nScanline, jstring pszF) { int result; jfieldID id_campo; jclass class_buf; jbyteArray pabySLB; unsigned char *pabyScanLineBuf; g_psInfo = (BSBInfo *) 0 ; g_psInfo = *(BSBInfo **)&cPtr; pabyScanLineBuf = (unsigned char *) malloc(g_psInfo->nXSize); class_buf = (*env)->GetObjectClass(env, buf); //----------------------------------------------------------------------- //Llamamos a la función de libreria que lee una línea y la cargamos sobre //pabyScanLineBuf //----------------------------------------------------------------------- result = BSBReadScanline(g_psInfo,(int)nScanline,(unsigned char *)pabyScanLineBuf); //----------------------------------------------------------------------- //Obtenemos el valor de retorno //----------------------------------------------------------------------- id_campo = (*env)->GetFieldID(env, class_buf, "pabyScanLineBuf", "[B"); pabySLB = (*env)->NewByteArray(env,g_psInfo->nXSize); if(pabySLB!=NULL){ (*env)->SetByteArrayRegion(env, pabySLB, 0, g_psInfo->nXSize,(jbyte *)pabyScanLineBuf); (*env)->SetObjectField(env, buf, id_campo, pabySLB); } //----------------------------------------------------------------------- //Liberamos la memoria de la linea leída //----------------------------------------------------------------------- if(pabyScanLineBuf != NULL)free(pabyScanLineBuf); return result; } /****************************************************************/ //Libera recursos y memoria JNIEXPORT void JNICALL Java_es_gva_cit_jbsb_Bsb_bsbCloseNat (JNIEnv *env, jobject obj, jlong cPtr) { g_psInfo = (BSBInfo *) 0 ; g_psInfo = *(BSBInfo **)&cPtr; BSBClose(g_psInfo); }