Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libjni-ecw / src / main / native / jecw / ecw_jni.c @ 20171

History | View | Annotate | Download (26.6 KB)

1 20171 maquerol
/**********************************************************
2
** Copyright 1998 Earth Resource Mapping Ltd.
3
** This document contains proprietary source code of
4
** Earth Resource Mapping Ltd, and can only be used under
5
** one of the three licenses as described in the
6
** license.txt file supplied with this distribution.
7
** See separate license.txt file for license details
8
** and conditions.
9
**
10
** This software is covered by US patent #6,442,298,
11
** #6,102,897 and #6,633,688.  Rights to use these patents
12
** is included in the license agreements.
13
**
14
** FILE:           ecw_jni.c
15
** CREATED:        4 Jan 2000
16
** AUTHOR:         Mark Sheridan
17
** PURPOSE:        Java wrappers for JNI usage of the ECW library in Java.
18
** EDITS:
19
**
20
** [01] 01Nov00 mjs Rewrote to be JVM 1.1 compliant.
21
** [02] 08Mar04 mjs Removed old 1.1 spec stuff, now needs a 1.2 compatible virtual machine.
22
** [03] 01Oct05 nbt ECWOpenArray function to support name parameter in char array
23
**
24
*******************************************************/
25
26
#include "NCSErrors.h"
27
#include "NCSECWClient.h"
28
#include "NCSMalloc.h"
29
#include "NCSUtil.h"
30
#include "NCSBuildNumber.h"
31
#include "JNCSFile.h"
32
#include "NCSFile.h"
33
#include "NCSDynamicLib.h"
34
35
//#ifndef JNI_VERSION_1_2
36
//#error You must compile this class against a 1.2 virtual machine specification
37
//#endif
38
39
static JavaVM *pJavaVirtualMachineInst = NULL;
40
struct NCSJNIFieldIDs *pGlobalJNCSFieldIDs = NULL;
41
42
typedef struct NCSJNIFieldIDs {
43
        jfieldID jIDNativeDataPointer;
44
        jfieldID jIDWidth;
45
        jfieldID jIDHeight;
46
        jfieldID jIDNumberOfBands;
47
        jfieldID jIDCompressionRate;
48
        jfieldID jIDCellIncrementX;
49
        jfieldID jIDCellIncrementY;
50
        jfieldID jIDCellSizeUnits;
51
        jfieldID jIDOriginX;
52
        jfieldID jIDOriginY;
53
        jfieldID jIDDatum;
54
        jfieldID jIDProjection;
55
        jfieldID jIDFilename;
56
        jfieldID jIDIsOpen;
57
        jmethodID jIDRefreshUpdateMethod;
58
        jfieldID jIDFileType;
59
        jfieldID jIDFileMimeType;
60
} NCSJNIFieldIDs;
61
62
// This is the object specific data structure, its cached.
63
typedef struct NCSJNIInfo {
64
        //jobject  ECWFile;
65
        NCSFileView *pFileView;
66
} NCSJNIInfo;
67
68
void NCSJNIThrowException(JNIEnv *pEnv, const char *pSignature, const char *pMessage)
69
{
70
        jclass jExceptionClass = NULL;
71
72
        if ((pEnv)->ExceptionCheck()) {
73
                (pEnv)->ExceptionDescribe();
74
                (pEnv)->ExceptionClear();
75
        }
76
77
        jExceptionClass = (pEnv)->FindClass(pSignature);
78
79
        if (jExceptionClass != NULL) {
80
                (pEnv)->ThrowNew(jExceptionClass, pMessage);
81
        }
82
        (pEnv)->DeleteLocalRef(jExceptionClass);
83
}
84
85
NCSError NCSCreateJNIInfoStruct(JNIEnv *pEnv, jobject ECWFile, NCSFileView *pNCSFileView, NCSJNIInfo **pReturn)
86
{
87
        NCSJNIInfo *pJNIInfo;
88
        //char *pErrorString = NULL;
89
90
        pJNIInfo = (NCSJNIInfo *)NCSMalloc(sizeof(NCSJNIInfo), TRUE);
91
        if (pJNIInfo != NULL)
92
        {
93
                pJNIInfo->pFileView = pNCSFileView;
94
                //pJNIInfo->ECWFile = (void *)(*pEnv)->NewGlobalRef(pEnv, ECWFile);
95
                *pReturn = pJNIInfo;
96
        }
97
        else
98
        {
99
                return NCS_COULDNT_ALLOC_MEMORY;
100
        }
101
        return NCS_SUCCESS;
102
}
103
104
/*
105
 * Class:     None
106
 * Method:    JNI_OnLoad
107
 * Signature: ()Z
108
 */
109
110
JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *pJVM, void *reserved)
111
{
112
        JNIEnv *pEnv;
113
114
        if ((pJVM)->GetEnv((void **)&pEnv, JNI_VERSION_1_2)) {
115
                NCSJNIThrowException(pEnv, "java/lang/Exception", "JNCS classes require a version 1.2 or higher virtual machine.");
116
                return JNI_ERR;
117
        }
118
        else {
119
                pJavaVirtualMachineInst = pJVM;
120
#ifndef WIN32
121
                NCSecwInit();
122
#endif
123
        }
124
        return JNI_VERSION_1_2;
125
}
126
127
128
/*
129
 * Class:     None
130
 * Method:    JNI_OnUnload
131
 * Signature: ()Z
132
 */
133
JNIEXPORT void JNICALL JNI_OnUnload(JavaVM *pJVM, void *reserved)
134
{
135
#ifndef WIN32
136
                NCSecwShutdown();
137
#endif
138
                NCSFree((void *)pGlobalJNCSFieldIDs);
139
        return;
140
}
141
142
/*
143
 * Class:     com_ermapper_ecw_JNCSFile
144
 * Method:    NCSJNIInit
145
 * Signature: ()I
146
 */
147
JNIEXPORT jint JNICALL Java_com_ermapper_ecw_JNCSFile_NCSJNIInit
148
  (JNIEnv *pEnv, jclass ECWFileClass)
149
{
150
        NCSJNIFieldIDs *pJNIInfo = NULL;
151
        char *pErrorString = NULL;
152
153
        if (!pGlobalJNCSFieldIDs) {
154
155
                pJNIInfo = (NCSJNIFieldIDs *)NCSMalloc(sizeof(NCSJNIFieldIDs), TRUE);
156
157
                // Get all the field ids of the ECWFile object
158
                pJNIInfo->jIDNativeDataPointer        = (pEnv)->GetFieldID(ECWFileClass, "nativeDataPointer", "J" );
159
                pJNIInfo->jIDWidth                                = (pEnv)->GetFieldID(ECWFileClass, "width", "I" );
160
                pJNIInfo->jIDHeight                                = (pEnv)->GetFieldID(ECWFileClass, "height", "I" );
161
                pJNIInfo->jIDNumberOfBands                = (pEnv)->GetFieldID(ECWFileClass, "numBands", "I" );
162
                pJNIInfo->jIDCompressionRate        = (pEnv)->GetFieldID(ECWFileClass, "compressionRate", "D" );
163
                pJNIInfo->jIDCellIncrementX                = (pEnv)->GetFieldID(ECWFileClass, "cellIncrementX", "D" );
164
                pJNIInfo->jIDCellIncrementY                = (pEnv)->GetFieldID(ECWFileClass, "cellIncrementY", "D" );
165
                pJNIInfo->jIDCellSizeUnits                = (pEnv)->GetFieldID(ECWFileClass, "cellSizeUnits", "I" );
166
                pJNIInfo->jIDOriginX                        = (pEnv)->GetFieldID(ECWFileClass, "originX", "D" );
167
                pJNIInfo->jIDOriginY                        = (pEnv)->GetFieldID(ECWFileClass, "originY", "D" );
168
                pJNIInfo->jIDDatum                                = (pEnv)->GetFieldID(ECWFileClass, "datum", "Ljava/lang/String;" );
169
                pJNIInfo->jIDProjection                        = (pEnv)->GetFieldID(ECWFileClass, "projection", "Ljava/lang/String;" );
170
                pJNIInfo->jIDFilename                        = (pEnv)->GetFieldID(ECWFileClass, "fileName", "Ljava/lang/String;" );
171
                pJNIInfo->jIDIsOpen                                = (pEnv)->GetFieldID(ECWFileClass, "bIsOpen", "Z" );
172
                pJNIInfo->jIDRefreshUpdateMethod= (pEnv)->GetMethodID(ECWFileClass, "refreshUpdate", "(IIDDDD)V");
173
174
                //pJNIInfo->jIDFileType                        = (pEnv)->GetFieldID(ECWFileClass, "fileType", "I" );
175
                //pJNIInfo->jIDFileMimeType                = (pEnv)->GetFieldID(ECWFileClass, "mimeType", "Ljava/lang/String;" );
176
177
178
                // Do some error checking
179
                if (!pJNIInfo->jIDNativeDataPointer)
180
                        pErrorString = "Could not determine fieldID for 'nativeDataPointer' in ECWFile object.";
181
                if (!pJNIInfo->jIDWidth)
182
                        pErrorString = "Could not determine fieldID for 'width' in ECWFile object.";
183
                if (!pJNIInfo->jIDHeight)
184
                        pErrorString = "Could not determine fieldID for 'height' in ECWFile object.";
185
                if (!pJNIInfo->jIDNumberOfBands)
186
                        pErrorString = "Could not determine fieldID for 'numBands' in ECWFile object.";
187
                if (!pJNIInfo->jIDCompressionRate)
188
                        pErrorString = "Could not determine fieldID for 'compressionRate' in ECWFile object.";
189
                if (!pJNIInfo->jIDCellIncrementX)
190
                        pErrorString = "Could not determine fieldID for 'cellIncrementX' in ECWFile object.";
191
                if (!pJNIInfo->jIDCellIncrementY)
192
                        pErrorString = "Could not determine fieldID for 'cellIncrementY' in ECWFile object.";
193
                if (!pJNIInfo->jIDCellSizeUnits)
194
                        pErrorString = "Could not determine fieldID for 'cellSizeUnits' in ECWFile object.";
195
                if (!pJNIInfo->jIDOriginX)
196
                        pErrorString = "Could not determine fieldID for 'originX' in ECWFile object.";
197
                if (!pJNIInfo->jIDOriginY)
198
                        pErrorString = "Could not determine fieldID for 'originY' in ECWFile object.";
199
                if (!pJNIInfo->jIDDatum)
200
                        pErrorString = "Could not determine fieldID for 'datum' in ECWFile object.";
201
                if (!pJNIInfo->jIDProjection)
202
                        pErrorString = "Could not determine fieldID for 'projection' in ECWFile object.";
203
                if (!pJNIInfo->jIDFilename)
204
                        pErrorString = "Could not determine fieldID for 'fileName' in ECWFile object.";
205
                if (!pJNIInfo->jIDIsOpen)
206
                        pErrorString = "Could not determine fieldID for 'bIsOpen' in ECWFile object.";
207
                /*if (!pJNIInfo->jIDFileType)
208
                        pErrorString = "Could not determine fieldID for 'fileType' in ECWFile object.";
209
                if (!pJNIInfo->jIDFileMimeType)
210
                        pErrorString = "Could not determine fieldID for 'mimeType' in ECWFile object.";*/
211
212
                if (pErrorString) {
213
#ifdef WIN32
214
                        MessageBox(NULL, OS_STRING(pErrorString), TEXT("JNCSClass Library (JNI)"), MB_OK);
215
#else
216
                        fprintf(stderr, "JNCSClass Library (JNI) : %s\n", pErrorString);
217
#endif
218
                        NCSFormatErrorText(NCS_JNI_ERROR, pErrorString);
219
                        return NCS_JNI_ERROR;
220
                }
221
                else {
222
                        pGlobalJNCSFieldIDs = pJNIInfo;
223
                        return NCS_SUCCESS;
224
                }
225
        }
226
        else {
227
                return NCS_SUCCESS;
228
        }
229
}
230
231
/*
232
 * There is so much that can go wrong in this call, that is why there is so much error checking.
233
 *
234
 * Class:     None
235
 * Method:    NCSJNIRefreshCallback
236
 * Signature:
237
 */
238
NCSEcwReadStatus NCSJNIRefreshCallback(NCSFileView *pNCSFileView)
239
{
240
        NCSFileViewSetInfo        *pViewInfo;
241
        NCSJNIInfo *pJNIInfo;
242
        jint nError;
243
        JNIEnv *pEnv;
244
        jclass EcwObjectClass = NULL;
245
        jobject pECWFile = NULL;
246
247
        NCScbmGetViewInfo(pNCSFileView, &pViewInfo);
248
249
        // Must attach to the vm thread before calling any java methods.
250
        nError = (pJavaVirtualMachineInst)->AttachCurrentThread((void **)&pEnv, NULL);
251
252
        if (nError != 0) {
253
                char Message[] = "The ECW JNI interface could not attach to the current thread in\n"
254
                                                 "the Java virtual machine. Please refer to the Image Web Server\n"
255
                                                 "Java SDK documentation for more information about JVM threads.\n\n"
256
                                                 "Progressive imagery will not be available.";
257
#ifdef WIN32
258
                MessageBox(GetActiveWindow(), OS_STRING(Message), TEXT("JNCSFile VM Error"), MB_OK);
259
#else
260
                fprintf(stderr, "JNCSFile VM Error : %s\n", Message);
261
#endif
262
                return NCSECW_READ_FAILED;
263
        }
264
265
        // Make sure we got a view info struct.
266
        if (!pViewInfo) {
267
                NCSJNIThrowException(pEnv, "java/lang/Exception", "ECW JNI component could not obtain the NCSViewInfo pointer from the NCSFileView. No refreshUpdate() will occur.");
268
                (pJavaVirtualMachineInst)->DetachCurrentThread();
269
                return NCSECW_READ_FAILED;
270
        }
271
272
        // The file is the global reference stashed in the client data pointer.
273
        pECWFile = (jobject)pViewInfo->pClientData;
274
275
        // Check to make sure that the ECW object has not been free'd or garbaged collected. This is only valid for JNI 1.2
276
        if ((pEnv)->IsSameObject(pECWFile/*pViewInfo->pClientData*/, NULL) == JNI_TRUE) {
277
                (pJavaVirtualMachineInst)->DetachCurrentThread();
278
                return NCSECW_READ_FAILED;
279
        }
280
281
        // Use the valid reference to the object, to obtain the data pointer.
282
        pJNIInfo = (NCSJNIInfo *)(pEnv)->GetLongField((jobject)pViewInfo->pClientData, pGlobalJNCSFieldIDs->jIDNativeDataPointer);
283
284
        if (!pJNIInfo) {
285
                NCSJNIThrowException(pEnv, "java/lang/Exception", "The ECW JNI component could not obtain the client data pointer from the NCSViewInfo struct. No refreshUpdate() will occur.");
286
                (pJavaVirtualMachineInst)->DetachCurrentThread();
287
                return NCSECW_READ_FAILED;
288
        }
289
290
        EcwObjectClass = (pEnv)->GetObjectClass(pECWFile/*pJNIInfo->ECWFile*/);
291
        if (EcwObjectClass == NULL) {
292
                NCSJNIThrowException(pEnv, "java/lang/Exception", "The ECW JNI component could not determine the class signature of the ECW object.");
293
                (pJavaVirtualMachineInst)->DetachCurrentThread();
294
                return NCSECW_READ_FAILED;
295
        }
296
297
        (pEnv)->DeleteLocalRef(EcwObjectClass);
298
299
        //Call the  "refreshUpdate" method on this object, if it implements the JNCSProgressiveUpdate interface.
300
        if (pGlobalJNCSFieldIDs->jIDRefreshUpdateMethod) {
301
                (pEnv)->CallVoidMethod(        pECWFile, \
302
                                                                pGlobalJNCSFieldIDs->jIDRefreshUpdateMethod, \
303
                                                                pViewInfo->nSizeX, pViewInfo->nSizeY, \
304
                                                                pViewInfo->fTopX, pViewInfo->fLeftY, \
305
                                                                pViewInfo->fBottomX, pViewInfo->fRightY);
306
        }
307
        else {
308
                NCSJNIThrowException(pEnv, "java/lang/ClassNotFoundException", "'refreshUpdate' method not found. Derived classes must implement the interface 'JNCSProgressiveUpdate' to use progressive imagery.");
309
                (pJavaVirtualMachineInst)->DetachCurrentThread();
310
                return NCSECW_READ_FAILED;
311
        }
312
313
        (pJavaVirtualMachineInst)->DetachCurrentThread();
314
315
        return NCSECW_READ_OK;
316
}
317
318
319
/**
320
 *
321
 */
322
int openFile(JNIEnv *pEnv, jobject *JNCSFile, jstring Filename, char *pFilename, jboolean bProgressive, NCSFileViewSetInfo *pNCSFileViewSetInfo, NCSFileView *pNCSFileView){
323
        NCSError nError;
324
325
        if (bProgressive) {
326
                nError = NCScbmOpenFileView((char *)pFilename, &pNCSFileView, NCSJNIRefreshCallback);
327
        }
328
        else {
329
                nError = NCScbmOpenFileView((char *)pFilename, &pNCSFileView, NULL);
330
        }
331
332
333
        if (NCS_FAILED(nError)) {
334
                // Return the short filename, since people dont like diplaying the full ecwp url
335
                char        *pProtocol, *pHost, *pECWFilename, *pShortFileName;
336
                int                nProtocolLength, nHostLength, nFilenameLength;
337
338
                NCSecwNetBreakdownUrl((char *)pFilename, &pProtocol, &nProtocolLength,
339
                                                                         &pHost, &nHostLength,
340
                                                                         &pECWFilename, &nFilenameLength);
341
342
                if (pECWFilename && nFilenameLength > 0 && (strstr(pECWFilename, "/") || strstr(pECWFilename, "\\")))
343
                {
344
                        int len = strlen(pECWFilename), index = 0;
345
                        for (index=len; index > 0; index--)
346
                        {
347
                                pShortFileName = &pECWFilename[index];
348
                                if (pECWFilename[index] == '\\' || pECWFilename[index] == '/')
349
                                {
350
                                        pShortFileName ++;
351
                                        break;
352
                                }
353
                        }
354
                }
355
                else
356
                {
357
                        pShortFileName = (char *)pFilename;
358
                }
359
360
                NCSFormatErrorText(NCS_FILE_OPEN_FAILED, pShortFileName, NCSGetLastErrorText(nError));
361
                return NCS_FILE_OPEN_FAILED;
362
        }
363
        else {
364
                NCSJNIInfo *pJNIInfo = NULL;
365
                char *pMimeType = NULL;
366
                NCSFileViewFileInfo        *pNCSFileInfo = NULL;
367
368
                nError = NCSCreateJNIInfoStruct(pEnv, (*JNCSFile), pNCSFileView, &pJNIInfo);
369
                NCScbmGetViewFileInfo(pNCSFileView, &pNCSFileInfo);
370
371
                // Set the properties in the actual Java object
372
                (pEnv)->SetIntField((*JNCSFile), pGlobalJNCSFieldIDs->jIDWidth, (jint)pNCSFileInfo->nSizeX        );
373
                (pEnv)->SetIntField((*JNCSFile), pGlobalJNCSFieldIDs->jIDHeight, (jint)pNCSFileInfo->nSizeY );
374
                (pEnv)->SetIntField((*JNCSFile), pGlobalJNCSFieldIDs->jIDNumberOfBands, (jint)pNCSFileInfo->nBands);
375
                (pEnv)->SetDoubleField((*JNCSFile), pGlobalJNCSFieldIDs->jIDCompressionRate, (jdouble)pNCSFileInfo->nCompressionRate );
376
                (pEnv)->SetDoubleField((*JNCSFile), pGlobalJNCSFieldIDs->jIDCellIncrementX, (jdouble)pNCSFileInfo->fCellIncrementX );
377
                (pEnv)->SetDoubleField((*JNCSFile), pGlobalJNCSFieldIDs->jIDCellIncrementY, (jdouble)pNCSFileInfo->fCellIncrementY );
378
                (pEnv)->SetIntField((*JNCSFile), pGlobalJNCSFieldIDs->jIDCellSizeUnits, (jint)pNCSFileInfo->eCellSizeUnits);
379
                (pEnv)->SetDoubleField((*JNCSFile), pGlobalJNCSFieldIDs->jIDOriginX, (jdouble)pNCSFileInfo->fOriginX );
380
                (pEnv)->SetDoubleField((*JNCSFile), pGlobalJNCSFieldIDs->jIDOriginY, (jdouble)pNCSFileInfo->fOriginY );
381
                (pEnv)->SetObjectField((*JNCSFile), pGlobalJNCSFieldIDs->jIDDatum, (pEnv)->NewStringUTF(pNCSFileInfo->szDatum));
382
                (pEnv)->SetObjectField((*JNCSFile), pGlobalJNCSFieldIDs->jIDProjection, (pEnv)->NewStringUTF(pNCSFileInfo->szProjection));
383
                (pEnv)->SetObjectField((*JNCSFile), pGlobalJNCSFieldIDs->jIDFilename, Filename);
384
                (pEnv)->SetBooleanField((*JNCSFile), pGlobalJNCSFieldIDs->jIDIsOpen, JNI_TRUE);
385
                (pEnv)->SetLongField((*JNCSFile), pGlobalJNCSFieldIDs->jIDNativeDataPointer, (jlong)pJNIInfo);
386
        }
387
        return NCS_SUCCESS;
388
}
389
390
/*
391
 * Class:     com_ermapper_ecw_JNCSFile
392
 * Method:    ECWOpen
393
 * Signature: (Ljava/lang/String;Z)I
394
 */
395
396
JNIEXPORT jint JNICALL Java_com_ermapper_ecw_JNCSFile_ECWOpen
397
  (JNIEnv *pEnv, jobject JNCSFile, jstring Filename, jboolean bProgressive){
398
        const char *pFilename = (pEnv)->GetStringUTFChars(Filename, (jboolean *)NULL);
399
        NCSFileView *pNCSFileView = NULL;
400
        NCSFileViewSetInfo *pNCSFileViewSetInfo = NULL;
401
        int ret;
402
403
        ret = openFile(pEnv, &JNCSFile, Filename, (char *)pFilename, bProgressive, pNCSFileViewSetInfo, pNCSFileView);
404
405
        (pEnv)->ReleaseStringUTFChars(Filename, (const char *)pFilename);
406
407
        NCScbmGetViewInfo(pNCSFileView, &pNCSFileViewSetInfo);
408
409
        pNCSFileViewSetInfo->pClientData = (void *)(pEnv)->NewGlobalRef(JNCSFile);
410
411
        return ret;
412
}
413
414
415
/*
416
 * Class:     com_ermapper_ecw_JNCSFile
417
 * Method:    ECWOpen
418
 * Signature: (Ljava/lang/String;Z)I
419
 */
420
421
JNIEXPORT jint JNICALL Java_com_ermapper_ecw_JNCSFile_ECWOpenArray
422
  (JNIEnv *pEnv, jobject JNCSFile, jstring Filename, jboolean bProgressive, jbyteArray pszF){
423
424
        NCSFileView *pNCSFileView = NULL;
425
        NCSFileViewSetInfo *pNCSFileViewSetInfo = NULL;
426
        int ret;
427
        jbyte *pFilename;
428
        int longitud = 0;
429
        NCSError nError;
430
431
          longitud = (pEnv)->GetArrayLength(pszF);
432
          pFilename = (pEnv)->GetByteArrayElements(pszF, 0);
433
          pFilename = (jbyte *)realloc(pFilename, longitud + 1);
434
          pFilename[longitud] = '\0';
435
436
          if (bProgressive) {
437
                nError = NCScbmOpenFileView((char *)pFilename, &pNCSFileView, NCSJNIRefreshCallback);
438
        }else {
439
                nError = NCScbmOpenFileView((char *)pFilename, &pNCSFileView, NULL);
440
        }
441
442
443
        if (NCS_FAILED(nError)) {
444
                // Return the short filename, since people dont like diplaying the full ecwp url
445
                char        *pProtocol, *pHost, *pECWFilename, *pShortFileName;
446
                int                nProtocolLength, nHostLength, nFilenameLength;
447
448
                NCSecwNetBreakdownUrl((char *)pFilename, &pProtocol, &nProtocolLength,
449
                                                                         &pHost, &nHostLength,
450
                                                                         &pECWFilename, &nFilenameLength);
451
452
                if (pECWFilename && nFilenameLength > 0 && (strstr(pECWFilename, "/") || strstr(pECWFilename, "\\")))
453
                {
454
                        int len = strlen(pECWFilename), index = 0;
455
                        for (index=len; index > 0; index--)
456
                        {
457
                                pShortFileName = &pECWFilename[index];
458
                                if (pECWFilename[index] == '\\' || pECWFilename[index] == '/')
459
                                {
460
                                        pShortFileName ++;
461
                                        break;
462
                                }
463
                        }
464
                }
465
                else
466
                {
467
                        pShortFileName = (char *)pFilename;
468
                }
469
470
                NCSFormatErrorText(NCS_FILE_OPEN_FAILED, pShortFileName, NCSGetLastErrorText(nError));
471
                return NCS_FILE_OPEN_FAILED;
472
        }
473
        else {
474
                NCSJNIInfo *pJNIInfo = NULL;
475
                char *pMimeType = NULL;
476
                NCSFileViewFileInfo        *pNCSFileInfo = NULL;
477
478
                nError = NCSCreateJNIInfoStruct(pEnv, JNCSFile, pNCSFileView, &pJNIInfo);
479
                NCScbmGetViewFileInfo(pNCSFileView, &pNCSFileInfo);
480
481
                // Set the properties in the actual Java object
482
                (pEnv)->SetIntField(JNCSFile, pGlobalJNCSFieldIDs->jIDWidth, (jint)pNCSFileInfo->nSizeX        );
483
                (pEnv)->SetIntField(JNCSFile, pGlobalJNCSFieldIDs->jIDHeight, (jint)pNCSFileInfo->nSizeY );
484
                (pEnv)->SetIntField(JNCSFile, pGlobalJNCSFieldIDs->jIDNumberOfBands, (jint)pNCSFileInfo->nBands);
485
                (pEnv)->SetDoubleField(JNCSFile, pGlobalJNCSFieldIDs->jIDCompressionRate, (jdouble)pNCSFileInfo->nCompressionRate );
486
                (pEnv)->SetDoubleField(JNCSFile, pGlobalJNCSFieldIDs->jIDCellIncrementX, (jdouble)pNCSFileInfo->fCellIncrementX );
487
                (pEnv)->SetDoubleField(JNCSFile, pGlobalJNCSFieldIDs->jIDCellIncrementY, (jdouble)pNCSFileInfo->fCellIncrementY );
488
                (pEnv)->SetIntField(JNCSFile, pGlobalJNCSFieldIDs->jIDCellSizeUnits, (jint)pNCSFileInfo->eCellSizeUnits);
489
                (pEnv)->SetDoubleField(JNCSFile, pGlobalJNCSFieldIDs->jIDOriginX, (jdouble)pNCSFileInfo->fOriginX );
490
                (pEnv)->SetDoubleField(JNCSFile, pGlobalJNCSFieldIDs->jIDOriginY, (jdouble)pNCSFileInfo->fOriginY );
491
                (pEnv)->SetObjectField(JNCSFile, pGlobalJNCSFieldIDs->jIDDatum, (pEnv)->NewStringUTF(pNCSFileInfo->szDatum));
492
                (pEnv)->SetObjectField(JNCSFile, pGlobalJNCSFieldIDs->jIDProjection, (pEnv)->NewStringUTF(pNCSFileInfo->szProjection));
493
                (pEnv)->SetObjectField(JNCSFile, pGlobalJNCSFieldIDs->jIDFilename, Filename);
494
                (pEnv)->SetBooleanField(JNCSFile, pGlobalJNCSFieldIDs->jIDIsOpen, JNI_TRUE);
495
                (pEnv)->SetLongField(JNCSFile, pGlobalJNCSFieldIDs->jIDNativeDataPointer, (jlong)pJNIInfo);
496
497
        }
498
499
500
        //ret = openFile(pEnv, &JNCSFile, Filename, (char *)pszFilename, bProgressive, pNCSFileViewSetInfo, pNCSFileView);
501
502
        (pEnv)->ReleaseByteArrayElements(pszF,(jbyte *)pFilename, 0);
503
504
        NCScbmGetViewInfo(pNCSFileView, &pNCSFileViewSetInfo);
505
506
        pNCSFileViewSetInfo->pClientData = (void *)(pEnv)->NewGlobalRef(JNCSFile);
507
508
        return NCS_SUCCESS;
509
}
510
511
512
/*
513
 * Class:     com_ermapper_ecw_JNCSFile
514
 * Method:    ECWClose
515
 * Signature: (Z)V
516
 */
517
JNIEXPORT void JNICALL Java_com_ermapper_ecw_JNCSFile_ECWClose
518
  (JNIEnv *pEnv , jobject JNCSFile, jboolean bFreeCache)
519
{
520
        NCSError nError;
521
        NCSJNIInfo *pJNIInfo = NULL;
522
523
        pJNIInfo = (NCSJNIInfo *)(pEnv)->GetLongField(JNCSFile, pGlobalJNCSFieldIDs->jIDNativeDataPointer);
524
525
        if (pJNIInfo != NULL) {
526
                NCSFileViewSetInfo        *pViewInfo;
527
528
                NCScbmGetViewInfo(pJNIInfo->pFileView, &pViewInfo);
529
530
                // Clear the Java object members.
531
                (pEnv)->SetIntField   (JNCSFile, pGlobalJNCSFieldIDs->jIDWidth, (jint)0        );
532
                (pEnv)->SetIntField   (JNCSFile, pGlobalJNCSFieldIDs->jIDHeight, (jint)0 );
533
                (pEnv)->SetIntField   (JNCSFile, pGlobalJNCSFieldIDs->jIDNumberOfBands, (jint)0);
534
                (pEnv)->SetDoubleField(JNCSFile, pGlobalJNCSFieldIDs->jIDCompressionRate, (jdouble)0.0 );
535
                (pEnv)->SetDoubleField(JNCSFile, pGlobalJNCSFieldIDs->jIDCellIncrementX, (jdouble)0.0 );
536
                (pEnv)->SetDoubleField(JNCSFile, pGlobalJNCSFieldIDs->jIDCellIncrementY, (jdouble)0.0 );
537
                (pEnv)->SetIntField   (JNCSFile, pGlobalJNCSFieldIDs->jIDCellSizeUnits, (jint)0 );
538
                (pEnv)->SetDoubleField(JNCSFile, pGlobalJNCSFieldIDs->jIDOriginX, (jdouble)0.0 );
539
                (pEnv)->SetDoubleField(JNCSFile, pGlobalJNCSFieldIDs->jIDOriginY, (jdouble)0.0 );
540
                (pEnv)->SetObjectField(JNCSFile, pGlobalJNCSFieldIDs->jIDDatum, (pEnv)->NewStringUTF(""));
541
                (pEnv)->SetObjectField(JNCSFile, pGlobalJNCSFieldIDs->jIDProjection, (pEnv)->NewStringUTF(""));
542
                (pEnv)->SetObjectField(JNCSFile, pGlobalJNCSFieldIDs->jIDFilename, NULL);
543
                (pEnv)->SetBooleanField(JNCSFile, pGlobalJNCSFieldIDs->jIDIsOpen, JNI_FALSE);
544
                (pEnv)->SetLongField  (JNCSFile, pGlobalJNCSFieldIDs->jIDNativeDataPointer, (jlong)0);
545
546
                (pEnv)->SetIntField(JNCSFile, pGlobalJNCSFieldIDs->jIDFileType, (jint)0 );
547
                (pEnv)->SetObjectField(JNCSFile, pGlobalJNCSFieldIDs->jIDFileMimeType, NULL );
548
549
                // Clean up any global refs
550
                //(pEnv)->DeleteGlobalRef(pJNIInfo->ECWFile);
551
                (pEnv)->DeleteGlobalRef((jobject)pViewInfo->pClientData);
552
553
                nError = NCScbmCloseFileViewEx(pJNIInfo->pFileView, bFreeCache);
554
555
                pJNIInfo->pFileView = NULL;
556
                NCSFree(pJNIInfo);
557
        }
558
        return;
559
}
560
561
/*
562
 * Class:     com_ermapper_ecw_JNCSFile
563
 * Method:    ECWSetView
564
 * Signature: (I[IIIIIDDDDII)I
565
 */
566
JNIEXPORT jint JNICALL Java_com_ermapper_ecw_JNCSFile_ECWSetView
567
  (JNIEnv *pEnv, jobject ECWFile, jint nBands, jintArray nBandList, jint nDatasetTLX, jint nDatasetTLY, jint nDatasetBRX, jint nDatasetBRY, jdouble dWorldTLX, jdouble dWorldTLY, jdouble dWorldBRX, jdouble dWorldBRY, jint nWidth, jint nHeight)
568
{
569
        NCSError nError = NCS_SUCCESS;
570
        NCSJNIInfo *pJNIInfo = NULL;
571
572
        // Sanity check
573
        if ((pEnv)->IsInstanceOf(ECWFile, (pEnv)->FindClass("com/ermapper/ecw/JNCSFile")) == JNI_FALSE)
574
        {
575
#ifdef WIN32
576
                MessageBox(NULL, OS_STRING("ECWSetView() error : object is not a JNCSFile instance"), TEXT("JNCSClass Library (JNI)"), MB_OK);
577
#endif //WIN32
578
                return NCS_JNI_ERROR;
579
        }
580
581
        pJNIInfo = (NCSJNIInfo *)(pEnv)->GetLongField(ECWFile, pGlobalJNCSFieldIDs->jIDNativeDataPointer);
582
583
        if (pJNIInfo) {
584
585
                jint *pBandBuffer = (jint *)NCSMalloc(sizeof(UINT32)*nBands+1, TRUE);
586
                (pEnv)->GetIntArrayRegion(nBandList, 0, nBands, pBandBuffer);
587
588
                nError = NCScbmSetFileViewEx(   pJNIInfo->pFileView,
589
                                                                                nBands,
590
                                                                                (UINT32*)pBandBuffer,
591
                                                                                nDatasetTLX, nDatasetTLY, nDatasetBRX, nDatasetBRY,
592
                                                                                nWidth,
593
                                                                                nHeight,
594
                                                                                dWorldTLX,
595
                                                                                dWorldTLY,
596
                                                                                dWorldBRX,
597
                                                                                dWorldBRY);
598
                NCSFree(pBandBuffer);
599
600
        } else {
601
                NCSFormatErrorText(NCS_JNI_ERROR, "method SetView() could not get native data from JNCSFile object.");
602
                nError = NCS_JNI_ERROR;
603
        }
604
605
        return nError;
606
}
607
608
609
/*
610
 * Class:     com_ermapper_ecw_JNCSFile
611
 * Method:    ECWReadImageRGBA
612
 * Signature: ([I)I
613
 */
614
JNIEXPORT jint JNICALL Java_com_ermapper_ecw_JNCSFile_ECWReadImageRGBA
615
  (JNIEnv *pEnv, jobject JNCSFile, jintArray pRGBArray, jint width, jint height)
616
{
617
        jboolean bIsCopy;
618
        NCSEcwReadStatus eStatus;
619
        NCSJNIInfo *pJNIInfo = NULL;
620
        NCSError nError = NCS_SUCCESS;
621
        jint *pRGBAPixels;
622
        jint *pRGBLineArrayPtr = NULL;
623
        int nIndex;
624
625
        pJNIInfo = (NCSJNIInfo *)(pEnv)->GetLongField(JNCSFile, pGlobalJNCSFieldIDs->jIDNativeDataPointer);
626
627
        if (!pJNIInfo) {
628
                NCSFormatErrorText(NCS_JNI_ERROR, "method readLineRGB() could not get native data from JNCSFile object.");
629
                nError = NCS_JNI_ERROR;
630
        }
631
        else {
632
                // Lock the primitive array and get a pointer to the memory.
633
                pRGBAPixels = (jint *)(pEnv)->GetPrimitiveArrayCritical(pRGBArray, &bIsCopy);
634
635
                if (pRGBAPixels) {
636
637
                        pRGBLineArrayPtr = pRGBAPixels;
638
                        for (nIndex = 0; nIndex < height; nIndex ++) {
639
#if defined(NCSBO_LSBFIRST)
640
                                eStatus = NCScbmReadViewLineBGRA( pJNIInfo->pFileView, (UINT32 *)pRGBLineArrayPtr);
641
#elif defined(NCSBO_MSBFIRST)
642
                                eStatus = NCScbmReadViewLineBGRA( pJNIInfo->pFileView, (UINT32 *)pRGBLineArrayPtr);
643
                                //eStatus = NCScbmReadViewLineRGBA( pJNIInfo->pFileView, (UINT32 *)pRGBLineArrayPtr);
644
#endif
645
                                pRGBLineArrayPtr += width;
646
647
                                if (eStatus == NCSECW_READ_CANCELLED) {
648
                                        nError = NCS_SUCCESS;
649
                                        break;
650
                                }
651
                                if (eStatus == NCSECW_READ_FAILED) {
652
                                        NCSFormatErrorText(NCS_JNI_ERROR, "method readLineRGB() failed (internal error).");
653
                                        nError = NCS_JNI_ERROR;
654
                                        break;
655
                                }
656
#ifndef WIN32
657
                                NCSThreadYield();
658
#endif
659
                        }
660
                }
661
                else {
662
                        NCSFormatErrorText(NCS_JNI_ERROR, "method readLineRGB() could not allocate memory for RGB Array.");
663
                        nError = NCS_JNI_ERROR;
664
                }
665
                // Copy the array back to the object and free the memory
666
                (pEnv)->ReleasePrimitiveArrayCritical(pRGBArray, pRGBAPixels, 0);
667
        }
668
        return nError;
669
}
670
671
672
/*
673
 * Class:     com_ermapper_ecw_JNCSFile
674
 * Method:    ECWReadLineRGB
675
 * Signature: ([I)I
676
 */
677
678
JNIEXPORT jint JNICALL Java_com_ermapper_ecw_JNCSFile_ECWReadLineRGBA
679
  (JNIEnv *pEnv, jobject JNCSFile, jintArray pRGBArray)
680
{
681
        jboolean bIsCopy;
682
        NCSEcwReadStatus eStatus;
683
        NCSJNIInfo *pJNIInfo = NULL;
684
        NCSError nError = NCS_SUCCESS;
685
        jint *pRGBAPixels;
686
687
        pJNIInfo = (NCSJNIInfo *)(pEnv)->GetLongField(JNCSFile, pGlobalJNCSFieldIDs->jIDNativeDataPointer);
688
689
        if (!pJNIInfo) {
690
                NCSFormatErrorText(NCS_JNI_ERROR, "method readLineRGB() could not get native data from JNCSFile object.");
691
                nError = NCS_JNI_ERROR;
692
        }
693
        else {
694
                // Lock the primitive array and get a pointer to the memory.
695
                pRGBAPixels = (jint *)(pEnv)->GetPrimitiveArrayCritical(pRGBArray, &bIsCopy);
696
697
                if (pRGBAPixels) {
698
                        // Read into a RGBA Java scaneline (which is byte reversed, so go the BGRA here (???)
699
                        eStatus = NCScbmReadViewLineBGRA( pJNIInfo->pFileView, (UINT32 *)pRGBAPixels);
700
701
                        if (eStatus == NCSECW_READ_FAILED) {
702
                                NCSFormatErrorText(NCS_JNI_ERROR, "method readLineRGB() failed (internal error).");
703
                                nError = NCS_JNI_ERROR;
704
                        }
705
                }
706
                else {
707
                        NCSFormatErrorText(NCS_JNI_ERROR, "method readLineRGB() could not allocate memory for RGB Array.");
708
                        nError = NCS_JNI_ERROR;
709
                }
710
                // Copy the array back to the object and free the memory
711
                (pEnv)->ReleasePrimitiveArrayCritical(pRGBArray, pRGBAPixels, 0);
712
        }
713
        return nError;
714
}
715
716
/*
717
 * Class:     com_ermapper_ecw_JNCSFile
718
 * Method:    ECWGetErrorString
719
 * Signature: (I)Ljava/lang/String;
720
 */
721
JNIEXPORT jstring JNICALL Java_com_ermapper_ecw_JNCSFile_ECWGetErrorString
722
  (JNIEnv *pEnv, jobject JNCSFile, jint nErrorNumber)
723
{
724
        //return (*pEnv)->NewStringUTF(pEnv, NCSGetLastErrorText(nErrorNumber));
725
726
        return (pEnv)->NewStringUTF("....");
727
}
728
729
730
/*
731
 * Class:     com_ermapper_ecw_JNCSFile
732
 * Method:    ECWGetLibVersion
733
 * Signature: ()Ljava/lang/String;
734
 */
735
JNIEXPORT jstring JNICALL Java_com_ermapper_ecw_JNCSFile_ECWGetLibVersion
736
  (JNIEnv *pEnv, jclass clazz)
737
{
738
        return (pEnv)->NewStringUTF(NCS_VERSION_STRING_NO_NULL);
739
}
740
741
/*
742
 * Class:     com_ermapper_ecw_JNCSFile
743
 * Method:    ECWGetPercentComplete
744
 * Signature: ()I
745
 */
746
747
JNIEXPORT jshort JNICALL Java_com_ermapper_ecw_JNCSFile_ECWGetPercentComplete
748
  (JNIEnv *pEnv, jobject JNCSFile)
749
750
{
751
        NCSFileViewSetInfo *pViewInfo = NULL;
752
        NCSJNIInfo *pJNIInfo = NULL;
753
754
        pJNIInfo = (NCSJNIInfo *)(pEnv)->GetLongField(JNCSFile, pGlobalJNCSFieldIDs->jIDNativeDataPointer);
755
756
        if (!pJNIInfo)
757
                return 0;
758
759
        if (pJNIInfo->pFileView) {
760
                NCScbmGetViewInfo(pJNIInfo->pFileView, &pViewInfo);
761
                if (pViewInfo) {
762
                        return (jshort)((pViewInfo->nBlocksAvailable / (double)pViewInfo->nBlocksInView) * 100);
763
                }
764
        }
765
        else {
766
                return 0;
767
        }
768
769
        return 0;
770
}