diff options
| author | Mathieu Malaterre <mathieu.malaterre@gmail.com> | 2012-04-23 13:35:01 +0000 |
|---|---|---|
| committer | Mathieu Malaterre <mathieu.malaterre@gmail.com> | 2012-04-23 13:35:01 +0000 |
| commit | ce9d5083ef778a66a8acad7b8ae8d658a560b0d8 (patch) | |
| tree | ac779ab3df8589bdb8ee58a517002cd2d6a1c259 /applications/JavaOpenJPEG/JavaOpenJPEGDecoder.c | |
| parent | baa7e32f9cd4bb8a627278768931dcc01f1518c6 (diff) | |
[1.5] Integrate patch from winfried posted on the mailing list. See here for more info: https://groups.google.com/group/openjpeg/msg/6488859a0dce77
Diffstat (limited to 'applications/JavaOpenJPEG/JavaOpenJPEGDecoder.c')
| -rw-r--r-- | applications/JavaOpenJPEG/JavaOpenJPEGDecoder.c | 100 |
1 files changed, 61 insertions, 39 deletions
diff --git a/applications/JavaOpenJPEG/JavaOpenJPEGDecoder.c b/applications/JavaOpenJPEG/JavaOpenJPEGDecoder.c index a273028c..f9fe92ea 100644 --- a/applications/JavaOpenJPEG/JavaOpenJPEGDecoder.c +++ b/applications/JavaOpenJPEG/JavaOpenJPEGDecoder.c @@ -203,7 +203,7 @@ int get_file_format(char *filename) { /* -------------------------------------------------------------------------- */
-int parse_cmdline_decoder(int argc, char **argv, opj_dparameters_t *parameters,img_fol_t *img_fol) {
+int parse_cmdline_decoder(int argc, char * const argv[], opj_dparameters_t *parameters,img_fol_t *img_fol) {
/* parse the command line */
int totlen;
opj_option_t long_option[]={
@@ -339,6 +339,9 @@ int parse_cmdline_decoder(int argc, char **argv, opj_dparameters_t *parameters,i case 'y': /* Image Directory path */
{
img_fol->imgdirpath = (char*)malloc(strlen(opj_optarg) + 1);
+
+ if(img_fol->imgdirpath == NULL) return 1;
+
strcpy(img_fol->imgdirpath,opj_optarg);
img_fol->set_imgdir=1;
}
@@ -426,7 +429,7 @@ int parse_cmdline_decoder(int argc, char **argv, opj_dparameters_t *parameters,i /* No check for possible errors before the -i and -o options are of course not mandatory*/
return 0;
-}
+}/* parse_cmdline_decoder() */
/* -------------------------------------------------------------------------- */
@@ -488,12 +491,13 @@ void info_callback(const char *msg, void *client_data) { (*env)->DeleteLocalRef(env, jbuffer);
}
-
+#define FAILS -1
+#define OK 0
/* --------------------------------------------------------------------------
-------------------- MAIN METHOD, CALLED BY JAVA -----------------------*/
JNIEXPORT jint JNICALL Java_org_openJpeg_OpenJPEGJavaDecoder_internalDecodeJ2KtoImage(JNIEnv *env, jobject obj, jobjectArray javaParameters) {
int argc; /* To simulate the command line parameters (taken from the javaParameters variable) and be able to re-use the */
- char **argv; /* 'parse_cmdline_decoder' method taken from the j2k_to_image project */
+ const char **argv; /* 'parse_cmdline_decoder' method taken from the j2k_to_image project */
opj_dparameters_t parameters; /* decompression parameters */
img_fol_t img_fol;
opj_event_mgr_t event_mgr; /* event manager */
@@ -508,7 +512,7 @@ JNIEXPORT jint JNICALL Java_org_openJpeg_OpenJPEGJavaDecoder_internalDecodeJ2Kto int w,h;
long min_value, max_value;
short tempS; unsigned char tempUC, tempUC1, tempUC2;
- // ==> Access variables to the Java member variables
+/* ==> Access variables to the Java member variables */
jsize arraySize;
jclass cls;
jobject object;
@@ -521,8 +525,9 @@ JNIEXPORT jint JNICALL Java_org_openJpeg_OpenJPEGJavaDecoder_internalDecodeJ2Kto jshort *jsBody, *ptrSBody;
jint *jiBody, *ptrIBody;
callback_variables_t msgErrorCallback_vars;
- // <=== access variable to Java member variables */
- int *ptr, *ptr1, *ptr2; // <== To transfer the decoded image to Java
+/* <=== access variable to Java member variables */
+ int *ptr, *ptr1, *ptr2;/* <== To transfer the decoded image to Java */
+ jint retval = FAILS;
/* configure the event callbacks */
memset(&event_mgr, 0, sizeof(opj_event_mgr_t));
@@ -530,20 +535,26 @@ JNIEXPORT jint JNICALL Java_org_openJpeg_OpenJPEGJavaDecoder_internalDecodeJ2Kto event_mgr.warning_handler = warning_callback;
event_mgr.info_handler = info_callback;
- // JNI reference to the calling class
+/* JNI reference to the calling class */
cls = (*env)->GetObjectClass(env, obj);
- // Pointers to be able to call a Java method for all the info and error messages
+/* Pointers to be able to call a Java method
+ * for all the info and error messages
+*/
msgErrorCallback_vars.env = env;
msgErrorCallback_vars.jobj = &obj;
msgErrorCallback_vars.message_mid = (*env)->GetMethodID(env, cls, "logMessage", "(Ljava/lang/String;)V");
msgErrorCallback_vars.error_mid = (*env)->GetMethodID(env, cls, "logError", "(Ljava/lang/String;)V");
- // Get the String[] containing the parameters, and converts it into a char** to simulate command line arguments.
+/* Get the String[] containing the parameters */
+
arraySize = (*env)->GetArrayLength(env, javaParameters);
argc = (int) arraySize +1;
- argv = malloc(argc*sizeof(char*));
- argv[0] = "ProgramName.exe"; // The program name: useless
+ argv = (const char **)malloc(argc*sizeof(char*));
+
+ if(argv == NULL) return FAILS;
+
+ argv[0] = "ProgramName.exe";/* The program name: useless */
j=0;
for (i=1; i<argc; i++) {
object = (*env)->GetObjectArrayElement(env, javaParameters, i-1);
@@ -561,19 +572,18 @@ JNIEXPORT jint JNICALL Java_org_openJpeg_OpenJPEGJavaDecoder_internalDecodeJ2Kto parameters.decod_format = J2K_CFMT;
/* parse input and get user encoding parameters */
- if(parse_cmdline_decoder(argc, argv, ¶meters,&img_fol) == 1) {
- // Release the Java arguments array
- for (i=1; i<argc; i++)
- (*env)->ReleaseStringUTFChars(env, (*env)->GetObjectArrayElement(env, javaParameters, i-1), argv[i]);
- return -1;
- }
- // Release the Java arguments array
+ j = parse_cmdline_decoder(argc, (char * const*)argv, ¶meters,&img_fol);
+
+/* Release the Java arguments array */
for (i=1; i<argc; i++)
- (*env)->ReleaseStringUTFChars(env, (*env)->GetObjectArrayElement(env, javaParameters, i-1), argv[i]);
+ (*env)->ReleaseStringUTFChars(env,
+ (*env)->GetObjectArrayElement(env, javaParameters, i-1), argv[i]);
+
+ if(j == 1) goto fin; /* failure */
num_images=1;
- // Get additional information from the Java object variables
+/* Get additional information from the Java object variables */
fid = (*env)->GetFieldID(env, cls,"skippedResolutions", "I");
parameters.cp_reduce = (short) (*env)->GetIntField(env, obj, fid);
@@ -587,22 +597,25 @@ JNIEXPORT jint JNICALL Java_org_openJpeg_OpenJPEGJavaDecoder_internalDecodeJ2Kto Implemented for debug purpose. */
/* -------------------------------------------------------------- */
if (parameters.infile && parameters.infile[0]!='\0') {
- //printf("C: opening [%s]\n", parameters.infile);
+ /* printf("C: opening [%s]\n", parameters.infile); */
fsrc = fopen(parameters.infile, "rb");
if (!fsrc) {
fprintf(stderr, "ERROR -> failed to open %s for reading\n", parameters.infile);
- return 1;
+ goto fin;
}
fseek(fsrc, 0, SEEK_END);
file_length = ftell(fsrc);
fseek(fsrc, 0, SEEK_SET);
src = (unsigned char *) malloc(file_length);
+
+ if(src == NULL) goto fin;
+
fread(src, 1, file_length, fsrc);
fclose(fsrc);
- //printf("C: %d bytes read from file\n",file_length);
+ /* printf("C: %d bytes read from file\n",file_length); */
} else {
- // Preparing the transfer of the codestream from Java to C
- //printf("C: before transfering codestream\n");
+ /* Preparing the transfer of the codestream from Java to C */
+ /* printf("C: before transfering codestream\n"); */
fid = (*env)->GetFieldID(env, cls,"compressedStream", "[B");
jba = (*env)->GetObjectField(env, obj, fid);
file_length = (*env)->GetArrayLength(env, jba);
@@ -636,7 +649,7 @@ JNIEXPORT jint JNICALL Java_org_openJpeg_OpenJPEGJavaDecoder_internalDecodeJ2Kto fprintf(stderr, "ERROR -> j2k_to_image: failed to decode image!\n");
opj_destroy_decompress(dinfo);
opj_cio_close(cio);
- return 1;
+ goto fin;
}
/* close the byte stream */
@@ -666,7 +679,7 @@ JNIEXPORT jint JNICALL Java_org_openJpeg_OpenJPEGJavaDecoder_internalDecodeJ2Kto fprintf(stderr, "ERROR -> j2k_to_image: failed to decode image!\n");
opj_destroy_decompress(dinfo);
opj_cio_close(cio);
- return 1;
+ goto fin;
}
/* close the byte stream */
@@ -697,7 +710,7 @@ JNIEXPORT jint JNICALL Java_org_openJpeg_OpenJPEGJavaDecoder_internalDecodeJ2Kto fprintf(stderr, "ERROR -> j2k_to_image: failed to decode image!\n");
opj_destroy_decompress(dinfo);
opj_cio_close(cio);
- return 1;
+ goto fin;
}
/* close the byte stream */
@@ -752,17 +765,21 @@ JNIEXPORT jint JNICALL Java_org_openJpeg_OpenJPEGJavaDecoder_internalDecodeJ2Kto }
- // ========= Return the image to the Java structure ===============
+/* ========= Return the image to the Java structure =============== */
#ifdef CHECK_THRESHOLDS
printf("C: checking thresholds\n");
#endif
- // First compute the real with and height, in function of the resolutions decoded.
- //wr = (image->comps[0].w + (1 << image->comps[0].factor) -1) >> image->comps[0].factor;
- //hr = (image->comps[0].h + (1 << image->comps[0].factor) -1) >> image->comps[0].factor;
+/* First compute the real with and height,
+ * in function of the resolutions decoded.
+*/
+/*---
+ wr = (image->comps[0].w + (1 << image->comps[0].factor) -1) >> image->comps[0].factor;
+ hr = (image->comps[0].h + (1 << image->comps[0].factor) -1) >> image->comps[0].factor;
+---*/
w = image->comps[0].w;
h = image->comps[0].h;
- if (image->numcomps==3) { // 3 components color image
+ if (image->numcomps==3) { /* 3 components color image */
ptr = image->comps[0].data;
ptr1 = image->comps[1].data;
ptr2 = image->comps[2].data;
@@ -775,7 +792,7 @@ JNIEXPORT jint JNICALL Java_org_openJpeg_OpenJPEGJavaDecoder_internalDecodeJ2Kto max_value = 255;
}
#endif
- // Get the pointer to the Java structure where the data must be copied
+/* Get the pointer to the Java structure where the data must be copied */
fid = (*env)->GetFieldID(env, cls,"image24", "[I");
jia = (*env)->GetObjectField(env, obj, fid);
jiBody = (*env)->GetIntArrayElements(env, jia, 0);
@@ -804,7 +821,7 @@ JNIEXPORT jint JNICALL Java_org_openJpeg_OpenJPEGJavaDecoder_internalDecodeJ2Kto }
(*env)->ReleaseIntArrayElements(env, jia, jiBody, 0);
- } else { // 1 component 8 or 16 bpp image
+ } else { /* 1 component 8 or 16 bpp image */
ptr = image->comps[0].data;
printf("C: before transfering a %d bpp image to java (length = %d)\n",image->comps[0].prec ,w*h);
if (image->comps[0].prec<=8) {
@@ -821,7 +838,7 @@ JNIEXPORT jint JNICALL Java_org_openJpeg_OpenJPEGJavaDecoder_internalDecodeJ2Kto max_value = 255;
}
#endif
- //printf("C: transfering %d shorts to Java image8 pointer = %d\n", wr*hr,ptrSBody);
+/* printf("C: transfering %d shorts to Java image8 pointer = %d\n", wr*hr,ptrSBody); */
for (i=0; i<w*h; i++) {
tempUC = (unsigned char) (ptr[i]);
#ifdef CHECK_THRESHOLDS
@@ -877,7 +894,12 @@ JNIEXPORT jint JNICALL Java_org_openJpeg_OpenJPEGJavaDecoder_internalDecodeJ2Kto opj_image_destroy(image);
}
- return 1; /* OK */
+ retval = OK;
+
+fin:
+ free(argv);
+
+ return retval;
}
-//end main
+/* end main MAIN */
|
