2 * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium
3 * Copyright (c) 2002-2014, Professor Benoit Macq
4 * Copyright (c) 2001-2003, David Janssens
5 * Copyright (c) 2002-2003, Yannick Verschueren
6 * Copyright (c) 2003-2007, Francois-Olivier Devaux
7 * Copyright (c) 2003-2014, Antonin Descampe
8 * Copyright (c) 2005, Herve Drolon, FreeImage Team
9 * Copyright (c) 2006-2007, Parvatha Elangovan
10 * Copyright (c) 2007, Patrick Piscaglia (Telemis)
11 * All rights reserved.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
23 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
41 #include "opj_includes.h"
42 #include "opj_getopt.h"
45 #include "org_openJpeg_OpenJPEGJavaDecoder.h"
48 #define stricmp strcasecmp
49 #define strnicmp strncasecmp
52 #include "format_defs.h"
54 typedef struct callback_variables {
56 /** 'jclass' object used to call a Java method from the C */
58 /** 'jclass' object used to call a Java method from the C */
59 jmethodID message_mid;
61 } callback_variables_t;
63 typedef struct dircnt{
64 /** Buffer for holding images read from Directory*/
66 /** Pointer to the buffer*/
71 typedef struct img_folder{
72 /** The directory path of the folder containing input images*/
78 /** Enable Cod Format for output*/
84 void decode_help_display() {
85 fprintf(stdout,"HELP\n----\n\n");
86 fprintf(stdout,"- the -h option displays this help information on screen\n\n");
89 fprintf(stdout,"List of parameters for the JPEG 2000 "
97 fprintf(stdout," -ImgDir \n");
98 fprintf(stdout," Image file Directory path \n");
99 fprintf(stdout," -OutFor \n");
100 fprintf(stdout," REQUIRED only if -ImgDir is used\n");
101 fprintf(stdout," Need to specify only format without filename <BMP> \n");
102 fprintf(stdout," Currently accepts PGM, PPM, PNM, PGX, BMP format\n");
103 fprintf(stdout," -i <compressed file>\n");
104 fprintf(stdout," REQUIRED only if an Input image directory not specified\n");
105 fprintf(stdout," Currently accepts J2K-files, JP2-files and JPT-files. The file type\n");
106 fprintf(stdout," is identified based on its suffix.\n");
107 fprintf(stdout," -o <decompressed file>\n");
108 fprintf(stdout," REQUIRED\n");
109 fprintf(stdout," Currently accepts PGM-files, PPM-files, PNM-files, PGX-files and\n");
110 fprintf(stdout," BMP-files. Binary data is written to the file (not ascii). If a PGX\n");
111 fprintf(stdout," filename is given, there will be as many output files as there are\n");
112 fprintf(stdout," components: an indice starting from 0 will then be appended to the\n");
113 fprintf(stdout," output filename, just before the \"pgx\" extension. If a PGM filename\n");
114 fprintf(stdout," is given and there are more than one component, only the first component\n");
115 fprintf(stdout," will be written to the file.\n");
116 fprintf(stdout," -r <reduce factor>\n");
117 fprintf(stdout," Set the number of highest resolution levels to be discarded. The\n");
118 fprintf(stdout," image resolution is effectively divided by 2 to the power of the\n");
119 fprintf(stdout," number of discarded levels. The reduce factor is limited by the\n");
120 fprintf(stdout," smallest total number of decomposition levels among tiles.\n");
121 fprintf(stdout," -l <number of quality layers to decode>\n");
122 fprintf(stdout," Set the maximum number of quality layers to decode. If there are\n");
123 fprintf(stdout," less quality layers than the specified number, all the quality layers\n");
124 fprintf(stdout," are decoded.\n");
127 fprintf(stdout," -W <options>\n");
128 fprintf(stdout," Activates the JPWL correction capability, if the codestream complies.\n");
129 fprintf(stdout," Options can be a comma separated list of <param=val> tokens:\n");
130 fprintf(stdout," c, c=numcomps\n");
131 fprintf(stdout," numcomps is the number of expected components in the codestream\n");
132 fprintf(stdout," (search of first EPB rely upon this, default is %d)\n", JPWL_EXPECTED_COMPONENTS);
133 #endif /* USE_JPWL */
135 fprintf(stdout,"\n");
138 /* -------------------------------------------------------------------------- */
140 int get_num_images(char *imgdirpath){
142 struct dirent* content;
145 /*Reading the input images from given input directory*/
147 dir= opendir(imgdirpath);
149 fprintf(stderr,"Could not open Folder %s\n",imgdirpath);
153 while((content=readdir(dir))!=NULL){
154 if(strcmp(".",content->d_name)==0 || strcmp("..",content->d_name)==0 )
161 int load_images(dircnt_t *dirptr, char *imgdirpath){
163 struct dirent* content;
166 /*Reading the input images from given input directory*/
168 dir= opendir(imgdirpath);
170 fprintf(stderr,"Could not open Folder %s\n",imgdirpath);
173 fprintf(stderr,"Folder opened successfully\n");
176 while((content=readdir(dir))!=NULL){
177 if(strcmp(".",content->d_name)==0 || strcmp("..",content->d_name)==0 )
180 strcpy(dirptr->filename[i],content->d_name);
186 int get_file_format(char *filename) {
188 static const char *extension[] = {"pgx", "pnm", "pgm", "ppm", "bmp","tif", "raw", "tga", "j2k", "jp2", "jpt", "j2c" };
189 static const int format[] = { PGX_DFMT, PXM_DFMT, PXM_DFMT, PXM_DFMT, BMP_DFMT, TIF_DFMT, RAW_DFMT, TGA_DFMT, J2K_CFMT, JP2_CFMT, JPT_CFMT, J2K_CFMT };
190 char * ext = strrchr(filename, '.');
195 for(i = 0; i < sizeof(format)/sizeof(*format); i++) {
196 if(strnicmp(ext, extension[i], 3) == 0) {
206 /* -------------------------------------------------------------------------- */
208 int parse_cmdline_decoder(int argc, char **argv, opj_dparameters_t *parameters,img_fol_t *img_fol) {
209 /* parse the command line */
211 opj_option_t long_option[]={
212 {"ImgDir",REQ_ARG, NULL ,'y'},
213 {"OutFor",REQ_ARG, NULL ,'O'},
217 const char optlist[] = "i:o:r:l:hx:"
221 #endif /* USE_JPWL */
223 /*for (i=0; i<argc; i++) {
224 printf("[%s]",argv[i]);
229 totlen=sizeof(long_option);
230 img_fol->set_out_format = 0;
231 reset_options_reading();
234 int c = opj_getopt_long(argc, argv,optlist,long_option,totlen);
238 case 'i': /* input file */
240 char *infile = opj_optarg;
241 parameters->decod_format = get_file_format(infile);
242 switch(parameters->decod_format) {
249 "!! Unrecognized format for infile : %s [accept only *.j2k, *.jp2, *.jpc or *.jpt] !!\n\n",
253 strncpy(parameters->infile, infile, sizeof(parameters->infile)-1);
257 /* ----------------------------------------------------- */
259 case 'o': /* output file */
261 char *outfile = opj_optarg;
262 parameters->cod_format = get_file_format(outfile);
263 switch(parameters->cod_format) {
272 fprintf(stderr, "Unknown output format image %s [only *.pnm, *.pgm, *.ppm, *.pgx, *.bmp, *.tif, *.raw or *.tga]!! \n", outfile);
275 strncpy(parameters->outfile, outfile, sizeof(parameters->outfile)-1);
279 /* ----------------------------------------------------- */
281 case 'O': /* output format */
284 char *of = opj_optarg;
285 sprintf(outformat,".%s",of);
286 img_fol->set_out_format = 1;
287 parameters->cod_format = get_file_format(outformat);
288 switch(parameters->cod_format) {
290 img_fol->out_format = "pgx";
293 img_fol->out_format = "ppm";
296 img_fol->out_format = "bmp";
299 img_fol->out_format = "tif";
302 img_fol->out_format = "raw";
305 img_fol->out_format = "raw";
308 fprintf(stderr, "Unknown output format image %s [only *.pnm, *.pgm, *.ppm, *.pgx, *.bmp, *.tif, *.raw or *.tga]!! \n", outformat);
315 /* ----------------------------------------------------- */
318 case 'r': /* reduce option */
320 sscanf(opj_optarg, "%d", ¶meters->cp_reduce);
324 /* ----------------------------------------------------- */
327 case 'l': /* layering option */
329 sscanf(opj_optarg, "%d", ¶meters->cp_layer);
333 /* ----------------------------------------------------- */
335 case 'h': /* display an help description */
336 decode_help_display();
339 /* ------------------------------------------------------ */
341 case 'y': /* Image Directory path */
343 img_fol->imgdirpath = (char*)opj_malloc(strlen(opj_optarg) + 1);
344 strcpy(img_fol->imgdirpath,opj_optarg);
345 img_fol->set_imgdir=1;
348 /* ----------------------------------------------------- */
352 case 'W': /* activate JPWL correction */
356 token = strtok(opj_optarg, ",");
357 while(token != NULL) {
359 /* search expected number of components */
364 compno = JPWL_EXPECTED_COMPONENTS; /* predefined no. of components */
366 if(sscanf(token, "c=%d", &compno) == 1) {
368 if ((compno < 1) || (compno > 256)) {
369 fprintf(stderr, "ERROR -> invalid number of components c = %d\n", compno);
372 parameters->jpwl_exp_comps = compno;
374 } else if (!strcmp(token, "c")) {
376 parameters->jpwl_exp_comps = compno; /* auto for default size */
379 fprintf(stderr, "ERROR -> invalid components specified = %s\n", token);
384 /* search maximum number of tiles */
389 tileno = JPWL_MAXIMUM_TILES; /* maximum no. of tiles */
391 if(sscanf(token, "t=%d", &tileno) == 1) {
393 if ((tileno < 1) || (tileno > JPWL_MAXIMUM_TILES)) {
394 fprintf(stderr, "ERROR -> invalid number of tiles t = %d\n", tileno);
397 parameters->jpwl_max_tiles = tileno;
399 } else if (!strcmp(token, "t")) {
401 parameters->jpwl_max_tiles = tileno; /* auto for default size */
404 fprintf(stderr, "ERROR -> invalid tiles specified = %s\n", token);
409 /* next token or bust */
410 token = strtok(NULL, ",");
412 parameters->jpwl_correct = true;
413 fprintf(stdout, "JPWL correction capability activated\n");
414 fprintf(stdout, "- expecting %d components\n", parameters->jpwl_exp_comps);
417 #endif /* USE_JPWL */
420 /* ----------------------------------------------------- */
423 fprintf(stderr,"WARNING -> this option is not valid \"-%c %s\"\n",c, opj_optarg);
428 /* No check for possible errors before the -i and -o options are of course not mandatory*/
433 /* -------------------------------------------------------------------------- */
436 error callback returning the message to Java andexpecting a callback_variables_t client object
438 void error_callback(const char *msg, void *client_data) {
439 callback_variables_t* vars = (callback_variables_t*) client_data;
440 JNIEnv *env = vars->env;
443 jbuffer = (*env)->NewStringUTF(env, msg);
444 (*env)->ExceptionClear(env);
445 (*env)->CallVoidMethod(env, *(vars->jobj), vars->error_mid, jbuffer);
447 if ((*env)->ExceptionOccurred(env)) {
448 fprintf(stderr,"C: Exception during call back method\n");
449 (*env)->ExceptionDescribe(env);
450 (*env)->ExceptionClear(env);
452 (*env)->DeleteLocalRef(env, jbuffer);
455 warning callback returning the message to Java andexpecting a callback_variables_t client object
457 void warning_callback(const char *msg, void *client_data) {
458 callback_variables_t* vars = (callback_variables_t*) client_data;
459 JNIEnv *env = vars->env;
462 jbuffer = (*env)->NewStringUTF(env, msg);
463 (*env)->ExceptionClear(env);
464 (*env)->CallVoidMethod(env, *(vars->jobj), vars->message_mid, jbuffer);
466 if ((*env)->ExceptionOccurred(env)) {
467 fprintf(stderr,"C: Exception during call back method\n");
468 (*env)->ExceptionDescribe(env);
469 (*env)->ExceptionClear(env);
471 (*env)->DeleteLocalRef(env, jbuffer);
474 information callback returning the message to Java andexpecting a callback_variables_t client object
476 void info_callback(const char *msg, void *client_data) {
477 callback_variables_t* vars = (callback_variables_t*) client_data;
478 JNIEnv *env = vars->env;
481 jbuffer = (*env)->NewStringUTF(env, msg);
482 (*env)->ExceptionClear(env);
483 (*env)->CallVoidMethod(env, *(vars->jobj), vars->message_mid, jbuffer);
485 if ((*env)->ExceptionOccurred(env)) {
486 fprintf(stderr,"C: Exception during call back method\n");
487 (*env)->ExceptionDescribe(env);
488 (*env)->ExceptionClear(env);
490 (*env)->DeleteLocalRef(env, jbuffer);
494 /* --------------------------------------------------------------------------
495 -------------------- MAIN METHOD, CALLED BY JAVA -----------------------*/
496 JNIEXPORT jint JNICALL Java_org_openJpeg_OpenJPEGJavaDecoder_internalDecodeJ2KtoImage(JNIEnv *env, jobject obj, jobjectArray javaParameters) {
497 int argc; /* To simulate the command line parameters (taken from the javaParameters variable) and be able to re-use the */
498 char **argv; /* 'parse_cmdline_decoder' method taken from the j2k_to_image project */
499 opj_dparameters_t parameters; /* decompression parameters */
501 opj_event_mgr_t event_mgr; /* event manager */
502 opj_image_t *image = NULL;
504 unsigned char *src = NULL;
508 opj_dinfo_t* dinfo = NULL; /* handle to a decompressor */
509 opj_cio_t *cio = NULL;
511 long min_value, max_value;
512 short tempS; unsigned char tempUC, tempUC1, tempUC2;
513 /* ==> Access variables to the Java member variables*/
522 jbyte *jbBody, *ptrBBody;
523 jshort *jsBody, *ptrSBody;
524 jint *jiBody, *ptrIBody;
525 callback_variables_t msgErrorCallback_vars;
526 /* <=== access variable to Java member variables */
527 int *ptr, *ptr1, *ptr2; /* <== To transfer the decoded image to Java*/
529 /* configure the event callbacks */
530 memset(&event_mgr, 0, sizeof(opj_event_mgr_t));
531 event_mgr.error_handler = error_callback;
532 event_mgr.warning_handler = warning_callback;
533 event_mgr.info_handler = info_callback;
535 /* JNI reference to the calling class*/
536 cls = (*env)->GetObjectClass(env, obj);
538 /* Pointers to be able to call a Java method for all the info and error messages*/
539 msgErrorCallback_vars.env = env;
540 msgErrorCallback_vars.jobj = &obj;
541 msgErrorCallback_vars.message_mid = (*env)->GetMethodID(env, cls, "logMessage", "(Ljava/lang/String;)V");
542 msgErrorCallback_vars.error_mid = (*env)->GetMethodID(env, cls, "logError", "(Ljava/lang/String;)V");
544 /* Get the String[] containing the parameters, and converts it into a char** to simulate command line arguments.*/
545 arraySize = (*env)->GetArrayLength(env, javaParameters);
546 argc = (int) arraySize +1;
547 argv = opj_malloc(argc*sizeof(char*));
548 argv[0] = "ProgramName.exe"; /* The program name: useless*/
550 for (i=1; i<argc; i++) {
551 object = (*env)->GetObjectArrayElement(env, javaParameters, i-1);
552 argv[i] = (char*)(*env)->GetStringUTFChars(env, object, &isCopy);
555 /*printf("C: decoder params = ");
556 for (i=0; i<argc; i++) {
557 printf("[%s]",argv[i]);
561 /* set decoding parameters to default values */
562 opj_set_default_decoder_parameters(¶meters);
563 parameters.decod_format = J2K_CFMT;
565 /* parse input and get user encoding parameters */
566 if(parse_cmdline_decoder(argc, argv, ¶meters,&img_fol) == 1) {
567 /* Release the Java arguments array*/
568 for (i=1; i<argc; i++)
569 (*env)->ReleaseStringUTFChars(env, (*env)->GetObjectArrayElement(env, javaParameters, i-1), argv[i]);
572 /* Release the Java arguments array*/
573 for (i=1; i<argc; i++)
574 (*env)->ReleaseStringUTFChars(env, (*env)->GetObjectArrayElement(env, javaParameters, i-1), argv[i]);
578 /* Get additional information from the Java object variables*/
579 fid = (*env)->GetFieldID(env, cls,"skippedResolutions", "I");
580 parameters.cp_reduce = (short) (*env)->GetIntField(env, obj, fid);
582 /*Decoding image one by one*/
583 for(imageno = 0; imageno < num_images ; imageno++)
586 fprintf(stderr,"\n");
588 /* read the input file and put it in memory into the 'src' object, if the -i option is given in JavaParameters.
589 Implemented for debug purpose. */
590 /* -------------------------------------------------------------- */
591 if (parameters.infile && parameters.infile[0]!='\0') {
592 /*printf("C: opening [%s]\n", parameters.infile);*/
593 fsrc = fopen(parameters.infile, "rb");
595 fprintf(stderr, "ERROR -> failed to open %s for reading\n", parameters.infile);
598 fseek(fsrc, 0, SEEK_END);
599 file_length = ftell(fsrc);
600 fseek(fsrc, 0, SEEK_SET);
601 src = (unsigned char *) opj_malloc(file_length);
602 fread(src, 1, file_length, fsrc);
604 /*printf("C: %d bytes read from file\n",file_length);*/
606 /* Preparing the transfer of the codestream from Java to C*/
607 /*printf("C: before transferring codestream\n");*/
608 fid = (*env)->GetFieldID(env, cls,"compressedStream", "[B");
609 jba = (*env)->GetObjectField(env, obj, fid);
610 file_length = (*env)->GetArrayLength(env, jba);
611 jbBody = (*env)->GetByteArrayElements(env, jba, &isCopy);
612 src = (unsigned char*)jbBody;
615 /* decode the code-stream */
616 /* ---------------------- */
618 switch(parameters.decod_format) {
621 /* JPEG-2000 codestream */
623 /* get a decoder handle */
624 dinfo = opj_create_decompress(CODEC_J2K);
626 /* catch events using our callbacks and give a local context */
627 opj_set_event_mgr((opj_common_ptr)dinfo, &event_mgr, &msgErrorCallback_vars);
629 /* setup the decoder decoding parameters using user parameters */
630 opj_setup_decoder(dinfo, ¶meters);
632 /* open a byte stream */
633 cio = opj_cio_open((opj_common_ptr)dinfo, src, file_length);
635 /* decode the stream and fill the image structure */
636 image = opj_decode(dinfo, cio);
638 fprintf(stderr, "ERROR -> j2k_to_image: failed to decode image!\n");
639 opj_destroy_decompress(dinfo);
644 /* close the byte stream */
651 /* JPEG 2000 compressed image data */
653 /* get a decoder handle */
654 dinfo = opj_create_decompress(CODEC_JP2);
656 /* catch events using our callbacks and give a local context */
657 opj_set_event_mgr((opj_common_ptr)dinfo, &event_mgr, &msgErrorCallback_vars);
659 /* setup the decoder decoding parameters using the current image and user parameters */
660 opj_setup_decoder(dinfo, ¶meters);
662 /* open a byte stream */
663 cio = opj_cio_open((opj_common_ptr)dinfo, src, file_length);
665 /* decode the stream and fill the image structure */
666 image = opj_decode(dinfo, cio);
668 fprintf(stderr, "ERROR -> j2k_to_image: failed to decode image!\n");
669 opj_destroy_decompress(dinfo);
674 /* close the byte stream */
682 /* JPEG 2000, JPIP */
684 /* get a decoder handle */
685 dinfo = opj_create_decompress(CODEC_JPT);
687 /* catch events using our callbacks and give a local context */
688 opj_set_event_mgr((opj_common_ptr)dinfo, &event_mgr, &msgErrorCallback_vars);
690 /* setup the decoder decoding parameters using user parameters */
691 opj_setup_decoder(dinfo, ¶meters);
693 /* open a byte stream */
694 cio = opj_cio_open((opj_common_ptr)dinfo, src, file_length);
696 /* decode the stream and fill the image structure */
697 image = opj_decode(dinfo, cio);
699 fprintf(stderr, "ERROR -> j2k_to_image: failed to decode image!\n");
700 opj_destroy_decompress(dinfo);
705 /* close the byte stream */
711 fprintf(stderr, "skipping file..\n");
715 /* free the memory containing the code-stream */
716 if (parameters.infile && parameters.infile[0]!='\0') {
719 (*env)->ReleaseByteArrayElements(env, jba, jbBody, 0);
723 /* create output image.
724 If the -o parameter is given in the JavaParameters, write the decoded version into a file.
725 Implemented for debug purpose. */
726 /* ---------------------------------- */
727 switch (parameters.cod_format) {
728 case PXM_DFMT: /* PNM PGM PPM */
729 if (imagetopnm(image, parameters.outfile)) {
730 fprintf(stdout,"Outfile %s not generated\n",parameters.outfile);
733 fprintf(stdout,"Generated Outfile %s\n",parameters.outfile);
737 case PGX_DFMT: /* PGX */
738 if(imagetopgx(image, parameters.outfile)){
739 fprintf(stdout,"Outfile %s not generated\n",parameters.outfile);
742 fprintf(stdout,"Generated Outfile %s\n",parameters.outfile);
746 case BMP_DFMT: /* BMP */
747 if(imagetobmp(image, parameters.outfile)){
748 fprintf(stdout,"Outfile %s not generated\n",parameters.outfile);
751 fprintf(stdout,"Generated Outfile %s\n",parameters.outfile);
757 /* ========= Return the image to the Java structure ===============*/
758 #ifdef CHECK_THRESHOLDS
759 printf("C: checking thresholds\n");
761 /* First compute the real with and height, in function of the resolutions decoded.*/
762 /*wr = (image->comps[0].w + (1 << image->comps[0].factor) -1) >> image->comps[0].factor;*/
763 /*hr = (image->comps[0].h + (1 << image->comps[0].factor) -1) >> image->comps[0].factor;*/
764 w = image->comps[0].w;
765 h = image->comps[0].h;
767 if (image->numcomps==3) { /* 3 components color image*/
768 ptr = image->comps[0].data;
769 ptr1 = image->comps[1].data;
770 ptr2 = image->comps[2].data;
771 #ifdef CHECK_THRESHOLDS
772 if (image->comps[0].sgnd) {
780 /* Get the pointer to the Java structure where the data must be copied*/
781 fid = (*env)->GetFieldID(env, cls,"image24", "[I");
782 jia = (*env)->GetObjectField(env, obj, fid);
783 jiBody = (*env)->GetIntArrayElements(env, jia, 0);
785 printf("C: transferring image24: %d int to Java pointer=%d\n",image->numcomps*w*h, ptrIBody);
787 for (i=0; i<w*h; i++) {
788 tempUC = (unsigned char)(ptr[i]);
789 tempUC1 = (unsigned char)(ptr1[i]);
790 tempUC2 = (unsigned char)(ptr2[i]);
791 #ifdef CHECK_THRESHOLDS
792 if (tempUC < min_value)
794 else if (tempUC > max_value)
796 if (tempUC1 < min_value)
798 else if (tempUC1 > max_value)
800 if (tempUC2 < min_value)
802 else if (tempUC2 > max_value)
805 *(ptrIBody++) = (int) ( (tempUC2<<16) + (tempUC1<<8) + tempUC );
807 (*env)->ReleaseIntArrayElements(env, jia, jiBody, 0);
809 } else { /* 1 component 8 or 16 bpp image*/
810 ptr = image->comps[0].data;
811 printf("C: before transferring a %d bpp image to java (length = %d)\n",image->comps[0].prec ,w*h);
812 if (image->comps[0].prec<=8) {
813 fid = (*env)->GetFieldID(env, cls,"image8", "[B");
814 jba = (*env)->GetObjectField(env, obj, fid);
815 jbBody = (*env)->GetByteArrayElements(env, jba, 0);
817 #ifdef CHECK_THRESHOLDS
818 if (image->comps[0].sgnd) {
826 /*printf("C: transferring %d shorts to Java image8 pointer = %d\n", wr*hr,ptrSBody);*/
827 for (i=0; i<w*h; i++) {
828 tempUC = (unsigned char) (ptr[i]);
829 #ifdef CHECK_THRESHOLDS
830 if (tempUC<min_value)
832 else if (tempUC > max_value)
835 *(ptrBBody++) = tempUC;
837 (*env)->ReleaseByteArrayElements(env, jba, jbBody, 0);
838 printf("C: image8 transferred to Java\n");
840 fid = (*env)->GetFieldID(env, cls,"image16", "[S");
841 jsa = (*env)->GetObjectField(env, obj, fid);
842 jsBody = (*env)->GetShortArrayElements(env, jsa, 0);
844 #ifdef CHECK_THRESHOLDS
845 if (image->comps[0].sgnd) {
852 printf("C: minValue = %d, maxValue = %d\n", min_value, max_value);
854 printf("C: transferring %d shorts to Java image16 pointer = %d\n", w*h,ptrSBody);
855 for (i=0; i<w*h; i++) {
856 tempS = (short) (ptr[i]);
857 #ifdef CHECK_THRESHOLDS
858 if (tempS<min_value) {
859 printf("C: value %d truncated to %d\n", tempS, min_value);
861 } else if (tempS > max_value) {
862 printf("C: value %d truncated to %d\n", tempS, max_value);
866 *(ptrSBody++) = tempS;
868 (*env)->ReleaseShortArrayElements(env, jsa, jsBody, 0);
869 printf("C: image16 completely filled\n");
874 /* free remaining structures */
876 opj_destroy_decompress(dinfo);
878 /* free image data structure */
879 opj_image_destroy(image);