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