2 * Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium
\r
3 * Copyright (c) 2002-2007, Professor Benoit Macq
\r
4 * Copyright (c) 2001-2003, David Janssens
\r
5 * Copyright (c) 2002-2003, Yannick Verschueren
\r
6 * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe
\r
7 * Copyright (c) 2005, Herve Drolon, FreeImage Team
\r
8 * Copyright (c) 2006-2007, Parvatha Elangovan
\r
9 * Copyright (c) 2007, Patrick Piscaglia (Telemis)
\r
10 * All rights reserved.
\r
12 * Redistribution and use in source and binary forms, with or without
\r
13 * modification, are permitted provided that the following conditions
\r
15 * 1. Redistributions of source code must retain the above copyright
\r
16 * notice, this list of conditions and the following disclaimer.
\r
17 * 2. Redistributions in binary form must reproduce the above copyright
\r
18 * notice, this list of conditions and the following disclaimer in the
\r
19 * documentation and/or other materials provided with the distribution.
\r
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
\r
22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
\r
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
\r
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
\r
25 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
\r
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
\r
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
\r
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
\r
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
\r
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
\r
31 * POSSIBILITY OF SUCH DAMAGE.
\r
39 #include "openjpeg.h"
\r
41 #include "convert.h"
\r
43 #include "org_openJpeg_OpenJPEGJavaDecoder.h"
\r
46 #define stricmp strcasecmp
\r
47 #define strnicmp strncasecmp
\r
50 #include "format_defs.h"
\r
52 typedef struct callback_variables {
\r
54 /** 'jclass' object used to call a Java method from the C */
\r
56 /** 'jclass' object used to call a Java method from the C */
\r
57 jmethodID message_mid;
\r
58 jmethodID error_mid;
\r
59 } callback_variables_t;
\r
61 typedef struct dircnt{
\r
62 /** Buffer for holding images read from Directory*/
\r
64 /** Pointer to the buffer*/
\r
69 typedef struct img_folder{
\r
70 /** The directory path of the folder containing input images*/
\r
76 /** Enable Cod Format for output*/
\r
77 char set_out_format;
\r
82 void decode_help_display() {
\r
83 fprintf(stdout,"HELP\n----\n\n");
\r
84 fprintf(stdout,"- the -h option displays this help information on screen\n\n");
\r
87 fprintf(stdout,"List of parameters for the JPEG 2000 "
\r
90 #endif /* USE_JPWL */
\r
93 fprintf(stdout,"\n");
\r
94 fprintf(stdout,"\n");
\r
95 fprintf(stdout," -ImgDir \n");
\r
96 fprintf(stdout," Image file Directory path \n");
\r
97 fprintf(stdout," -OutFor \n");
\r
98 fprintf(stdout," REQUIRED only if -ImgDir is used\n");
\r
99 fprintf(stdout," Need to specify only format without filename <BMP> \n");
\r
100 fprintf(stdout," Currently accepts PGM, PPM, PNM, PGX, BMP format\n");
\r
101 fprintf(stdout," -i <compressed file>\n");
\r
102 fprintf(stdout," REQUIRED only if an Input image directory not specified\n");
\r
103 fprintf(stdout," Currently accepts J2K-files, JP2-files and JPT-files. The file type\n");
\r
104 fprintf(stdout," is identified based on its suffix.\n");
\r
105 fprintf(stdout," -o <decompressed file>\n");
\r
106 fprintf(stdout," REQUIRED\n");
\r
107 fprintf(stdout," Currently accepts PGM-files, PPM-files, PNM-files, PGX-files and\n");
\r
108 fprintf(stdout," BMP-files. Binary data is written to the file (not ascii). If a PGX\n");
\r
109 fprintf(stdout," filename is given, there will be as many output files as there are\n");
\r
110 fprintf(stdout," components: an indice starting from 0 will then be appended to the\n");
\r
111 fprintf(stdout," output filename, just before the \"pgx\" extension. If a PGM filename\n");
\r
112 fprintf(stdout," is given and there are more than one component, only the first component\n");
\r
113 fprintf(stdout," will be written to the file.\n");
\r
114 fprintf(stdout," -r <reduce factor>\n");
\r
115 fprintf(stdout," Set the number of highest resolution levels to be discarded. The\n");
\r
116 fprintf(stdout," image resolution is effectively divided by 2 to the power of the\n");
\r
117 fprintf(stdout," number of discarded levels. The reduce factor is limited by the\n");
\r
118 fprintf(stdout," smallest total number of decomposition levels among tiles.\n");
\r
119 fprintf(stdout," -l <number of quality layers to decode>\n");
\r
120 fprintf(stdout," Set the maximum number of quality layers to decode. If there are\n");
\r
121 fprintf(stdout," less quality layers than the specified number, all the quality layers\n");
\r
122 fprintf(stdout," are decoded.\n");
\r
125 fprintf(stdout," -W <options>\n");
\r
126 fprintf(stdout," Activates the JPWL correction capability, if the codestream complies.\n");
\r
127 fprintf(stdout," Options can be a comma separated list of <param=val> tokens:\n");
\r
128 fprintf(stdout," c, c=numcomps\n");
\r
129 fprintf(stdout," numcomps is the number of expected components in the codestream\n");
\r
130 fprintf(stdout," (search of first EPB rely upon this, default is %d)\n", JPWL_EXPECTED_COMPONENTS);
\r
131 #endif /* USE_JPWL */
\r
133 fprintf(stdout,"\n");
\r
136 /* -------------------------------------------------------------------------- */
\r
138 int get_num_images(char *imgdirpath){
\r
140 struct dirent* content;
\r
141 int num_images = 0;
\r
143 /*Reading the input images from given input directory*/
\r
145 dir= opendir(imgdirpath);
\r
147 fprintf(stderr,"Could not open Folder %s\n",imgdirpath);
\r
151 while((content=readdir(dir))!=NULL){
\r
152 if(strcmp(".",content->d_name)==0 || strcmp("..",content->d_name)==0 )
\r
159 int load_images(dircnt_t *dirptr, char *imgdirpath){
\r
161 struct dirent* content;
\r
164 /*Reading the input images from given input directory*/
\r
166 dir= opendir(imgdirpath);
\r
168 fprintf(stderr,"Could not open Folder %s\n",imgdirpath);
\r
171 fprintf(stderr,"Folder opened successfully\n");
\r
174 while((content=readdir(dir))!=NULL){
\r
175 if(strcmp(".",content->d_name)==0 || strcmp("..",content->d_name)==0 )
\r
178 strcpy(dirptr->filename[i],content->d_name);
\r
184 int get_file_format(char *filename) {
\r
186 static const char *extension[] = {"pgx", "pnm", "pgm", "ppm", "bmp","tif", "raw", "tga", "j2k", "jp2", "jpt", "j2c" };
\r
187 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 };
\r
188 char * ext = strrchr(filename, '.');
\r
193 for(i = 0; i < sizeof(format)/sizeof(*format); i++) {
\r
194 if(strnicmp(ext, extension[i], 3) == 0) {
\r
204 /* -------------------------------------------------------------------------- */
\r
206 int parse_cmdline_decoder(int argc, char **argv, opj_dparameters_t *parameters,img_fol_t *img_fol) {
\r
207 /* parse the command line */
\r
209 option_t long_option[]={
\r
210 {"ImgDir",REQ_ARG, NULL ,'y'},
\r
211 {"OutFor",REQ_ARG, NULL ,'O'},
\r
215 const char optlist[] = "i:o:r:l:hx:"
\r
219 #endif /* USE_JPWL */
\r
221 /*for (i=0; i<argc; i++) {
\r
222 printf("[%s]",argv[i]);
\r
227 totlen=sizeof(long_option);
\r
228 img_fol->set_out_format = 0;
\r
229 reset_options_reading();
\r
232 int c = getopt_long(argc, argv,optlist,long_option,totlen);
\r
236 case 'i': /* input file */
\r
238 char *infile = optarg;
\r
239 parameters->decod_format = get_file_format(infile);
\r
240 switch(parameters->decod_format) {
\r
247 "!! Unrecognized format for infile : %s [accept only *.j2k, *.jp2, *.jpc or *.jpt] !!\n\n",
\r
251 strncpy(parameters->infile, infile, sizeof(parameters->infile)-1);
\r
255 /* ----------------------------------------------------- */
\r
257 case 'o': /* output file */
\r
259 char *outfile = optarg;
\r
260 parameters->cod_format = get_file_format(outfile);
\r
261 switch(parameters->cod_format) {
\r
270 fprintf(stderr, "Unknown output format image %s [only *.pnm, *.pgm, *.ppm, *.pgx, *.bmp, *.tif, *.raw or *.tga]!! \n", outfile);
\r
273 strncpy(parameters->outfile, outfile, sizeof(parameters->outfile)-1);
\r
277 /* ----------------------------------------------------- */
\r
279 case 'O': /* output format */
\r
281 char outformat[50];
\r
283 sprintf(outformat,".%s",of);
\r
284 img_fol->set_out_format = 1;
\r
285 parameters->cod_format = get_file_format(outformat);
\r
286 switch(parameters->cod_format) {
\r
288 img_fol->out_format = "pgx";
\r
291 img_fol->out_format = "ppm";
\r
294 img_fol->out_format = "bmp";
\r
297 img_fol->out_format = "tif";
\r
300 img_fol->out_format = "raw";
\r
303 img_fol->out_format = "raw";
\r
306 fprintf(stderr, "Unknown output format image %s [only *.pnm, *.pgm, *.ppm, *.pgx, *.bmp, *.tif, *.raw or *.tga]!! \n", outformat);
\r
313 /* ----------------------------------------------------- */
\r
316 case 'r': /* reduce option */
\r
318 sscanf(optarg, "%d", ¶meters->cp_reduce);
\r
322 /* ----------------------------------------------------- */
\r
325 case 'l': /* layering option */
\r
327 sscanf(optarg, "%d", ¶meters->cp_layer);
\r
331 /* ----------------------------------------------------- */
\r
333 case 'h': /* display an help description */
\r
334 decode_help_display();
\r
337 /* ------------------------------------------------------ */
\r
339 case 'y': /* Image Directory path */
\r
341 img_fol->imgdirpath = (char*)malloc(strlen(optarg) + 1);
\r
342 strcpy(img_fol->imgdirpath,optarg);
\r
343 img_fol->set_imgdir=1;
\r
346 /* ----------------------------------------------------- */
\r
350 case 'W': /* activate JPWL correction */
\r
352 char *token = NULL;
\r
354 token = strtok(optarg, ",");
\r
355 while(token != NULL) {
\r
357 /* search expected number of components */
\r
358 if (*token == 'c') {
\r
362 compno = JPWL_EXPECTED_COMPONENTS; /* predefined no. of components */
\r
364 if(sscanf(token, "c=%d", &compno) == 1) {
\r
366 if ((compno < 1) || (compno > 256)) {
\r
367 fprintf(stderr, "ERROR -> invalid number of components c = %d\n", compno);
\r
370 parameters->jpwl_exp_comps = compno;
\r
372 } else if (!strcmp(token, "c")) {
\r
374 parameters->jpwl_exp_comps = compno; /* auto for default size */
\r
377 fprintf(stderr, "ERROR -> invalid components specified = %s\n", token);
\r
382 /* search maximum number of tiles */
\r
383 if (*token == 't') {
\r
387 tileno = JPWL_MAXIMUM_TILES; /* maximum no. of tiles */
\r
389 if(sscanf(token, "t=%d", &tileno) == 1) {
\r
391 if ((tileno < 1) || (tileno > JPWL_MAXIMUM_TILES)) {
\r
392 fprintf(stderr, "ERROR -> invalid number of tiles t = %d\n", tileno);
\r
395 parameters->jpwl_max_tiles = tileno;
\r
397 } else if (!strcmp(token, "t")) {
\r
399 parameters->jpwl_max_tiles = tileno; /* auto for default size */
\r
402 fprintf(stderr, "ERROR -> invalid tiles specified = %s\n", token);
\r
407 /* next token or bust */
\r
408 token = strtok(NULL, ",");
\r
410 parameters->jpwl_correct = true;
\r
411 fprintf(stdout, "JPWL correction capability activated\n");
\r
412 fprintf(stdout, "- expecting %d components\n", parameters->jpwl_exp_comps);
\r
415 #endif /* USE_JPWL */
\r
418 /* ----------------------------------------------------- */
\r
421 fprintf(stderr,"WARNING -> this option is not valid \"-%c %s\"\n",c, optarg);
\r
426 /* No check for possible errors before the -i and -o options are of course not mandatory*/
\r
431 /* -------------------------------------------------------------------------- */
\r
434 error callback returning the message to Java andexpecting a callback_variables_t client object
\r
436 void error_callback(const char *msg, void *client_data) {
\r
437 callback_variables_t* vars = (callback_variables_t*) client_data;
\r
438 JNIEnv *env = vars->env;
\r
441 jbuffer = (*env)->NewStringUTF(env, msg);
\r
442 (*env)->ExceptionClear(env);
\r
443 (*env)->CallVoidMethod(env, *(vars->jobj), vars->error_mid, jbuffer);
\r
445 if ((*env)->ExceptionOccurred(env)) {
\r
446 fprintf(stderr,"C: Exception during call back method\n");
\r
447 (*env)->ExceptionDescribe(env);
\r
448 (*env)->ExceptionClear(env);
\r
450 (*env)->DeleteLocalRef(env, jbuffer);
\r
453 warning callback returning the message to Java andexpecting a callback_variables_t client object
\r
455 void warning_callback(const char *msg, void *client_data) {
\r
456 callback_variables_t* vars = (callback_variables_t*) client_data;
\r
457 JNIEnv *env = vars->env;
\r
460 jbuffer = (*env)->NewStringUTF(env, msg);
\r
461 (*env)->ExceptionClear(env);
\r
462 (*env)->CallVoidMethod(env, *(vars->jobj), vars->message_mid, jbuffer);
\r
464 if ((*env)->ExceptionOccurred(env)) {
\r
465 fprintf(stderr,"C: Exception during call back method\n");
\r
466 (*env)->ExceptionDescribe(env);
\r
467 (*env)->ExceptionClear(env);
\r
469 (*env)->DeleteLocalRef(env, jbuffer);
\r
472 information callback returning the message to Java andexpecting a callback_variables_t client object
\r
474 void info_callback(const char *msg, void *client_data) {
\r
475 callback_variables_t* vars = (callback_variables_t*) client_data;
\r
476 JNIEnv *env = vars->env;
\r
479 jbuffer = (*env)->NewStringUTF(env, msg);
\r
480 (*env)->ExceptionClear(env);
\r
481 (*env)->CallVoidMethod(env, *(vars->jobj), vars->message_mid, jbuffer);
\r
483 if ((*env)->ExceptionOccurred(env)) {
\r
484 fprintf(stderr,"C: Exception during call back method\n");
\r
485 (*env)->ExceptionDescribe(env);
\r
486 (*env)->ExceptionClear(env);
\r
488 (*env)->DeleteLocalRef(env, jbuffer);
\r
492 /* --------------------------------------------------------------------------
\r
493 -------------------- MAIN METHOD, CALLED BY JAVA -----------------------*/
\r
494 JNIEXPORT jint JNICALL Java_org_openJpeg_OpenJPEGJavaDecoder_internalDecodeJ2KtoImage(JNIEnv *env, jobject obj, jobjectArray javaParameters) {
\r
495 int argc; /* To simulate the command line parameters (taken from the javaParameters variable) and be able to re-use the */
\r
496 char **argv; /* 'parse_cmdline_decoder' method taken from the j2k_to_image project */
\r
497 opj_dparameters_t parameters; /* decompression parameters */
\r
499 opj_event_mgr_t event_mgr; /* event manager */
\r
500 opj_image_t *image = NULL;
\r
502 unsigned char *src = NULL;
\r
506 opj_dinfo_t* dinfo = NULL; /* handle to a decompressor */
\r
507 opj_cio_t *cio = NULL;
\r
509 long min_value, max_value;
\r
510 short tempS; unsigned char tempUC, tempUC1, tempUC2;
\r
511 // ==> Access variables to the Java member variables
\r
520 jbyte *jbBody, *ptrBBody;
\r
521 jshort *jsBody, *ptrSBody;
\r
522 jint *jiBody, *ptrIBody;
\r
523 callback_variables_t msgErrorCallback_vars;
\r
524 // <=== access variable to Java member variables */
\r
525 int *ptr, *ptr1, *ptr2; // <== To transfer the decoded image to Java
\r
527 /* configure the event callbacks */
\r
528 memset(&event_mgr, 0, sizeof(opj_event_mgr_t));
\r
529 event_mgr.error_handler = error_callback;
\r
530 event_mgr.warning_handler = warning_callback;
\r
531 event_mgr.info_handler = info_callback;
\r
533 // JNI reference to the calling class
\r
534 cls = (*env)->GetObjectClass(env, obj);
\r
536 // Pointers to be able to call a Java method for all the info and error messages
\r
537 msgErrorCallback_vars.env = env;
\r
538 msgErrorCallback_vars.jobj = &obj;
\r
539 msgErrorCallback_vars.message_mid = (*env)->GetMethodID(env, cls, "logMessage", "(Ljava/lang/String;)V");
\r
540 msgErrorCallback_vars.error_mid = (*env)->GetMethodID(env, cls, "logError", "(Ljava/lang/String;)V");
\r
542 // Get the String[] containing the parameters, and converts it into a char** to simulate command line arguments.
\r
543 arraySize = (*env)->GetArrayLength(env, javaParameters);
\r
544 argc = (int) arraySize +1;
\r
545 argv = malloc(argc*sizeof(char*));
\r
546 argv[0] = "ProgramName.exe"; // The program name: useless
\r
548 for (i=1; i<argc; i++) {
\r
549 object = (*env)->GetObjectArrayElement(env, javaParameters, i-1);
\r
550 argv[i] = (*env)->GetStringUTFChars(env, object, &isCopy);
\r
553 /*printf("C: decoder params = ");
\r
554 for (i=0; i<argc; i++) {
\r
555 printf("[%s]",argv[i]);
\r
559 /* set decoding parameters to default values */
\r
560 opj_set_default_decoder_parameters(¶meters);
\r
561 parameters.decod_format = J2K_CFMT;
\r
563 /* parse input and get user encoding parameters */
\r
564 if(parse_cmdline_decoder(argc, argv, ¶meters,&img_fol) == 1) {
\r
565 // Release the Java arguments array
\r
566 for (i=1; i<argc; i++)
\r
567 (*env)->ReleaseStringUTFChars(env, (*env)->GetObjectArrayElement(env, javaParameters, i-1), argv[i]);
\r
570 // Release the Java arguments array
\r
571 for (i=1; i<argc; i++)
\r
572 (*env)->ReleaseStringUTFChars(env, (*env)->GetObjectArrayElement(env, javaParameters, i-1), argv[i]);
\r
576 // Get additional information from the Java object variables
\r
577 fid = (*env)->GetFieldID(env, cls,"skippedResolutions", "I");
\r
578 parameters.cp_reduce = (short) (*env)->GetIntField(env, obj, fid);
\r
580 /*Decoding image one by one*/
\r
581 for(imageno = 0; imageno < num_images ; imageno++)
\r
584 fprintf(stderr,"\n");
\r
586 /* read the input file and put it in memory into the 'src' object, if the -i option is given in JavaParameters.
\r
587 Implemented for debug purpose. */
\r
588 /* -------------------------------------------------------------- */
\r
589 if (parameters.infile && parameters.infile[0]!='\0') {
\r
590 //printf("C: opening [%s]\n", parameters.infile);
\r
591 fsrc = fopen(parameters.infile, "rb");
\r
593 fprintf(stderr, "ERROR -> failed to open %s for reading\n", parameters.infile);
\r
596 fseek(fsrc, 0, SEEK_END);
\r
597 file_length = ftell(fsrc);
\r
598 fseek(fsrc, 0, SEEK_SET);
\r
599 src = (unsigned char *) malloc(file_length);
\r
600 fread(src, 1, file_length, fsrc);
\r
602 //printf("C: %d bytes read from file\n",file_length);
\r
604 // Preparing the transfer of the codestream from Java to C
\r
605 //printf("C: before transfering codestream\n");
\r
606 fid = (*env)->GetFieldID(env, cls,"compressedStream", "[B");
\r
607 jba = (*env)->GetObjectField(env, obj, fid);
\r
608 file_length = (*env)->GetArrayLength(env, jba);
\r
609 jbBody = (*env)->GetByteArrayElements(env, jba, &isCopy);
\r
610 src = (unsigned char*)jbBody;
\r
613 /* decode the code-stream */
\r
614 /* ---------------------- */
\r
616 switch(parameters.decod_format) {
\r
619 /* JPEG-2000 codestream */
\r
621 /* get a decoder handle */
\r
622 dinfo = opj_create_decompress(CODEC_J2K);
\r
624 /* catch events using our callbacks and give a local context */
\r
625 opj_set_event_mgr((opj_common_ptr)dinfo, &event_mgr, &msgErrorCallback_vars);
\r
627 /* setup the decoder decoding parameters using user parameters */
\r
628 opj_setup_decoder(dinfo, ¶meters);
\r
630 /* open a byte stream */
\r
631 cio = opj_cio_open((opj_common_ptr)dinfo, src, file_length);
\r
633 /* decode the stream and fill the image structure */
\r
634 image = opj_decode(dinfo, cio);
\r
636 fprintf(stderr, "ERROR -> j2k_to_image: failed to decode image!\n");
\r
637 opj_destroy_decompress(dinfo);
\r
638 opj_cio_close(cio);
\r
642 /* close the byte stream */
\r
643 opj_cio_close(cio);
\r
649 /* JPEG 2000 compressed image data */
\r
651 /* get a decoder handle */
\r
652 dinfo = opj_create_decompress(CODEC_JP2);
\r
654 /* catch events using our callbacks and give a local context */
\r
655 opj_set_event_mgr((opj_common_ptr)dinfo, &event_mgr, &msgErrorCallback_vars);
\r
657 /* setup the decoder decoding parameters using the current image and user parameters */
\r
658 opj_setup_decoder(dinfo, ¶meters);
\r
660 /* open a byte stream */
\r
661 cio = opj_cio_open((opj_common_ptr)dinfo, src, file_length);
\r
663 /* decode the stream and fill the image structure */
\r
664 image = opj_decode(dinfo, cio);
\r
666 fprintf(stderr, "ERROR -> j2k_to_image: failed to decode image!\n");
\r
667 opj_destroy_decompress(dinfo);
\r
668 opj_cio_close(cio);
\r
672 /* close the byte stream */
\r
673 opj_cio_close(cio);
\r
680 /* JPEG 2000, JPIP */
\r
682 /* get a decoder handle */
\r
683 dinfo = opj_create_decompress(CODEC_JPT);
\r
685 /* catch events using our callbacks and give a local context */
\r
686 opj_set_event_mgr((opj_common_ptr)dinfo, &event_mgr, &msgErrorCallback_vars);
\r
688 /* setup the decoder decoding parameters using user parameters */
\r
689 opj_setup_decoder(dinfo, ¶meters);
\r
691 /* open a byte stream */
\r
692 cio = opj_cio_open((opj_common_ptr)dinfo, src, file_length);
\r
694 /* decode the stream and fill the image structure */
\r
695 image = opj_decode(dinfo, cio);
\r
697 fprintf(stderr, "ERROR -> j2k_to_image: failed to decode image!\n");
\r
698 opj_destroy_decompress(dinfo);
\r
699 opj_cio_close(cio);
\r
703 /* close the byte stream */
\r
704 opj_cio_close(cio);
\r
709 fprintf(stderr, "skipping file..\n");
\r
713 /* free the memory containing the code-stream */
\r
714 if (parameters.infile && parameters.infile[0]!='\0') {
\r
717 (*env)->ReleaseByteArrayElements(env, jba, jbBody, 0);
\r
721 /* create output image.
\r
722 If the -o parameter is given in the JavaParameters, write the decoded version into a file.
\r
723 Implemented for debug purpose. */
\r
724 /* ---------------------------------- */
\r
725 switch (parameters.cod_format) {
\r
726 case PXM_DFMT: /* PNM PGM PPM */
\r
727 if (imagetopnm(image, parameters.outfile)) {
\r
728 fprintf(stdout,"Outfile %s not generated\n",parameters.outfile);
\r
731 fprintf(stdout,"Generated Outfile %s\n",parameters.outfile);
\r
735 case PGX_DFMT: /* PGX */
\r
736 if(imagetopgx(image, parameters.outfile)){
\r
737 fprintf(stdout,"Outfile %s not generated\n",parameters.outfile);
\r
740 fprintf(stdout,"Generated Outfile %s\n",parameters.outfile);
\r
744 case BMP_DFMT: /* BMP */
\r
745 if(imagetobmp(image, parameters.outfile)){
\r
746 fprintf(stdout,"Outfile %s not generated\n",parameters.outfile);
\r
749 fprintf(stdout,"Generated Outfile %s\n",parameters.outfile);
\r
755 // ========= Return the image to the Java structure ===============
\r
756 #ifdef CHECK_THRESHOLDS
\r
757 printf("C: checking thresholds\n");
\r
759 // First compute the real with and height, in function of the resolutions decoded.
\r
760 //wr = (image->comps[0].w + (1 << image->comps[0].factor) -1) >> image->comps[0].factor;
\r
761 //hr = (image->comps[0].h + (1 << image->comps[0].factor) -1) >> image->comps[0].factor;
\r
762 w = image->comps[0].w;
\r
763 h = image->comps[0].h;
\r
765 if (image->numcomps==3) { // 3 components color image
\r
766 ptr = image->comps[0].data;
\r
767 ptr1 = image->comps[1].data;
\r
768 ptr2 = image->comps[2].data;
\r
769 #ifdef CHECK_THRESHOLDS
\r
770 if (image->comps[0].sgnd) {
\r
778 // Get the pointer to the Java structure where the data must be copied
\r
779 fid = (*env)->GetFieldID(env, cls,"image24", "[I");
\r
780 jia = (*env)->GetObjectField(env, obj, fid);
\r
781 jiBody = (*env)->GetIntArrayElements(env, jia, 0);
\r
783 printf("C: transfering image24: %d int to Java pointer=%d\n",image->numcomps*w*h, ptrIBody);
\r
785 for (i=0; i<w*h; i++) {
\r
786 tempUC = (unsigned char)(ptr[i]);
\r
787 tempUC1 = (unsigned char)(ptr1[i]);
\r
788 tempUC2 = (unsigned char)(ptr2[i]);
\r
789 #ifdef CHECK_THRESHOLDS
\r
790 if (tempUC < min_value)
\r
792 else if (tempUC > max_value)
\r
794 if (tempUC1 < min_value)
\r
796 else if (tempUC1 > max_value)
\r
798 if (tempUC2 < min_value)
\r
800 else if (tempUC2 > max_value)
\r
803 *(ptrIBody++) = (int) ( (tempUC2<<16) + (tempUC1<<8) + tempUC );
\r
805 (*env)->ReleaseIntArrayElements(env, jia, jiBody, 0);
\r
807 } else { // 1 component 8 or 16 bpp image
\r
808 ptr = image->comps[0].data;
\r
809 printf("C: before transfering a %d bpp image to java (length = %d)\n",image->comps[0].prec ,w*h);
\r
810 if (image->comps[0].prec<=8) {
\r
811 fid = (*env)->GetFieldID(env, cls,"image8", "[B");
\r
812 jba = (*env)->GetObjectField(env, obj, fid);
\r
813 jbBody = (*env)->GetByteArrayElements(env, jba, 0);
\r
815 #ifdef CHECK_THRESHOLDS
\r
816 if (image->comps[0].sgnd) {
\r
824 //printf("C: transfering %d shorts to Java image8 pointer = %d\n", wr*hr,ptrSBody);
\r
825 for (i=0; i<w*h; i++) {
\r
826 tempUC = (unsigned char) (ptr[i]);
\r
827 #ifdef CHECK_THRESHOLDS
\r
828 if (tempUC<min_value)
\r
829 tempUC = min_value;
\r
830 else if (tempUC > max_value)
\r
831 tempUC = max_value;
\r
833 *(ptrBBody++) = tempUC;
\r
835 (*env)->ReleaseByteArrayElements(env, jba, jbBody, 0);
\r
836 printf("C: image8 transfered to Java\n");
\r
838 fid = (*env)->GetFieldID(env, cls,"image16", "[S");
\r
839 jsa = (*env)->GetObjectField(env, obj, fid);
\r
840 jsBody = (*env)->GetShortArrayElements(env, jsa, 0);
\r
842 #ifdef CHECK_THRESHOLDS
\r
843 if (image->comps[0].sgnd) {
\r
844 min_value = -32768;
\r
850 printf("C: minValue = %d, maxValue = %d\n", min_value, max_value);
\r
852 printf("C: transfering %d shorts to Java image16 pointer = %d\n", w*h,ptrSBody);
\r
853 for (i=0; i<w*h; i++) {
\r
854 tempS = (short) (ptr[i]);
\r
855 #ifdef CHECK_THRESHOLDS
\r
856 if (tempS<min_value) {
\r
857 printf("C: value %d truncated to %d\n", tempS, min_value);
\r
859 } else if (tempS > max_value) {
\r
860 printf("C: value %d truncated to %d\n", tempS, max_value);
\r
864 *(ptrSBody++) = tempS;
\r
866 (*env)->ReleaseShortArrayElements(env, jsa, jsBody, 0);
\r
867 printf("C: image16 completely filled\n");
\r
872 /* free remaining structures */
\r
874 opj_destroy_decompress(dinfo);
\r
876 /* free image data structure */
\r
877 opj_image_destroy(image);
\r