(no commit message)
[openjpeg.git] / codec / j2k_to_image.c
1 /*
2  * Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium
3  * Copyright (c) 2002-2007, 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 and Antonin Descampe
7  * Copyright (c) 2005, Herve Drolon, FreeImage Team
8  * Copyright (c) 2006-2007, Parvatha Elangovan
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 #include <stdio.h>
33 #include <string.h>
34 #include <stdlib.h>
35
36 #include "openjpeg.h"
37 #include "compat/getopt.h"
38 #include "convert.h"
39 #include "dirent.h"
40
41 #ifndef WIN32
42 #define stricmp strcasecmp
43 #define strnicmp strncasecmp
44 #endif
45
46 /* ----------------------------------------------------------------------- */
47
48 #define J2K_CFMT 0
49 #define JP2_CFMT 1
50 #define JPT_CFMT 2
51
52 #define PXM_DFMT 10
53 #define PGX_DFMT 11
54 #define BMP_DFMT 12
55 #define YUV_DFMT 13
56 #define TIF_DFMT 14
57
58 /* ----------------------------------------------------------------------- */
59
60 typedef struct dircnt{
61         /** Buffer for holding images read from Directory*/
62         char *filename_buf;
63         /** Pointer to the buffer*/
64         char **filename;
65 }dircnt_t;
66
67
68 typedef struct img_folder{
69         /** The directory path of the folder containing input images*/
70         char *imgdirpath;
71         /** Output format*/
72         char *out_format;
73         /** Enable option*/
74         char set_imgdir;
75         /** Enable Cod Format for output*/
76         char set_out_format;
77
78 }img_fol_t;
79
80 void decode_help_display() {
81         fprintf(stdout,"HELP\n----\n\n");
82         fprintf(stdout,"- the -h option displays this help information on screen\n\n");
83
84 /* UniPG>> */
85         fprintf(stdout,"List of parameters for the JPEG 2000 "
86 #ifdef USE_JPWL
87                 "+ JPWL "
88 #endif /* USE_JPWL */
89                 "decoder:\n");
90 /* <<UniPG */
91         fprintf(stdout,"\n");
92         fprintf(stdout,"\n");
93         fprintf(stdout,"  -ImgDir \n");
94         fprintf(stdout,"        Image file Directory path \n");
95         fprintf(stdout,"  -OutFor \n");
96         fprintf(stdout,"    REQUIRED only if -ImgDir is used\n");
97         fprintf(stdout,"          Need to specify only format without filename <BMP>  \n");
98         fprintf(stdout,"    Currently accepts PGM, PPM, PNM, PGX, BMP format\n");
99         fprintf(stdout,"  -i <compressed file>\n");
100         fprintf(stdout,"    REQUIRED only if an Input image directory not specified\n");
101         fprintf(stdout,"    Currently accepts J2K-files, JP2-files and JPT-files. The file type\n");
102         fprintf(stdout,"    is identified based on its suffix.\n");
103         fprintf(stdout,"  -o <decompressed file>\n");
104         fprintf(stdout,"    REQUIRED\n");
105         fprintf(stdout,"    Currently accepts PGM-files, PPM-files, PNM-files, PGX-files and\n");
106         fprintf(stdout,"    BMP-files. Binary data is written to the file (not ascii). If a PGX\n");
107         fprintf(stdout,"    filename is given, there will be as many output files as there are\n");
108         fprintf(stdout,"    components: an indice starting from 0 will then be appended to the\n");
109         fprintf(stdout,"    output filename, just before the \"pgx\" extension. If a PGM filename\n");
110         fprintf(stdout,"    is given and there are more than one component, only the first component\n");
111         fprintf(stdout,"    will be written to the file.\n");
112         fprintf(stdout,"  -r <reduce factor>\n");
113         fprintf(stdout,"    Set the number of highest resolution levels to be discarded. The\n");
114         fprintf(stdout,"    image resolution is effectively divided by 2 to the power of the\n");
115         fprintf(stdout,"    number of discarded levels. The reduce factor is limited by the\n");
116         fprintf(stdout,"    smallest total number of decomposition levels among tiles.\n");
117         fprintf(stdout,"  -l <number of quality layers to decode>\n");
118         fprintf(stdout,"    Set the maximum number of quality layers to decode. If there are\n");
119         fprintf(stdout,"    less quality layers than the specified number, all the quality layers\n");
120         fprintf(stdout,"    are decoded.\n");
121 /* UniPG>> */
122 #ifdef USE_JPWL
123         fprintf(stdout,"  -W <options>\n");
124         fprintf(stdout,"    Activates the JPWL correction capability, if the codestream complies.\n");
125         fprintf(stdout,"    Options can be a comma separated list of <param=val> tokens:\n");
126         fprintf(stdout,"    c, c=numcomps\n");
127         fprintf(stdout,"       numcomps is the number of expected components in the codestream\n");
128         fprintf(stdout,"       (search of first EPB rely upon this, default is %d)\n", JPWL_EXPECTED_COMPONENTS);
129 #endif /* USE_JPWL */
130 /* <<UniPG */
131         fprintf(stdout,"\n");
132 }
133
134 /* -------------------------------------------------------------------------- */
135
136 int get_num_images(char *imgdirpath){
137         DIR *dir;
138         struct dirent* content; 
139         int num_images = 0;
140
141         /*Reading the input images from given input directory*/
142
143         dir= opendir(imgdirpath);
144         if(!dir){
145                 fprintf(stderr,"Could not open Folder %s\n",imgdirpath);
146                 return 0;
147         }
148         
149         while((content=readdir(dir))!=NULL){
150                 if(strcmp(".",content->d_name)==0 || strcmp("..",content->d_name)==0 )
151                         continue;
152                 num_images++;
153         }
154         return num_images;
155 }
156
157 int load_images(dircnt_t *dirptr, char *imgdirpath){
158         DIR *dir;
159         struct dirent* content; 
160         int i = 0;
161
162         /*Reading the input images from given input directory*/
163
164         dir= opendir(imgdirpath);
165         if(!dir){
166                 fprintf(stderr,"Could not open Folder %s\n",imgdirpath);
167                 return 1;
168         }else   {
169                 fprintf(stderr,"Folder opened successfully\n");
170         }
171         
172         while((content=readdir(dir))!=NULL){
173                 if(strcmp(".",content->d_name)==0 || strcmp("..",content->d_name)==0 )
174                         continue;
175
176                 strcpy(dirptr->filename[i],content->d_name);
177                 i++;
178         }
179         return 0;       
180 }
181
182 int get_file_format(char *filename) {
183         unsigned int i;
184         static const char *extension[] = {"pgx", "pnm", "pgm", "ppm", "bmp","tif", "j2k", "jp2", "jpt", "j2c" };
185         static const int format[] = { PGX_DFMT, PXM_DFMT, PXM_DFMT, PXM_DFMT, BMP_DFMT, TIF_DFMT, J2K_CFMT, JP2_CFMT, JPT_CFMT, J2K_CFMT };
186         char * ext = strrchr(filename, '.');
187         if (ext == NULL)
188                 return -1;
189         ext++;
190         if(ext) {
191                 for(i = 0; i < sizeof(format)/sizeof(*format); i++) {
192                         if(strnicmp(ext, extension[i], 3) == 0) {
193                                 return format[i];
194                         }
195                 }
196         }
197
198         return -1;
199 }
200
201 char get_next_file(int imageno,dircnt_t *dirptr,img_fol_t *img_fol, opj_dparameters_t *parameters){
202         char image_filename[OPJ_PATH_LEN], infilename[OPJ_PATH_LEN],outfilename[OPJ_PATH_LEN],temp_ofname[OPJ_PATH_LEN];
203
204         strcpy(image_filename,dirptr->filename[imageno]);
205         fprintf(stderr,"File Number %d \"%s\"\n",imageno,image_filename);
206         parameters->decod_format = get_file_format(image_filename);
207         if (parameters->decod_format == -1)
208                 return 1;
209         sprintf(infilename,"%s/%s",img_fol->imgdirpath,image_filename);
210         strncpy(parameters->infile, infilename, sizeof(infilename));
211
212         //Set output file
213         strcpy(temp_ofname,strtok(image_filename,"."));
214         if(img_fol->set_out_format==1){
215                 sprintf(outfilename,"%s/%s.%s",img_fol->imgdirpath,temp_ofname,img_fol->out_format);
216                 strncpy(parameters->outfile, outfilename, sizeof(outfilename));
217         }
218         return 0;
219 }
220
221
222 /* -------------------------------------------------------------------------- */
223
224 int parse_cmdline_decoder(int argc, char **argv, opj_dparameters_t *parameters,img_fol_t *img_fol) {
225         /* parse the command line */
226         int totlen;
227         option_t long_option[]={
228                 {"ImgDir",REQ_ARG, NULL ,'y'},
229                 {"OutFor",REQ_ARG, NULL ,'O'},
230         };
231
232 /* UniPG>> */
233         const char optlist[] = "i:o:r:l:h"
234
235 #ifdef USE_JPWL
236                                         "W:"
237 #endif /* USE_JPWL */
238                                         ;
239 /* <<UniPG */
240         totlen=sizeof(long_option);
241         img_fol->set_out_format = 0;
242         while (1) {
243                 int c = getopt_long(argc, argv,optlist,long_option,totlen);
244                 if (c == -1)
245                         break;
246                 switch (c) {
247                         case 'i':                       /* input file */
248                         {
249                                 char *infile = optarg;
250                                 parameters->decod_format = get_file_format(infile);
251                                 switch(parameters->decod_format) {
252                                         case J2K_CFMT:
253                                         case JP2_CFMT:
254                                         case JPT_CFMT:
255                                                 break;
256                                         default:
257                                                 fprintf(stderr, 
258                                                         "!! Unrecognized format for infile : %s [accept only *.j2k, *.jp2, *.jpc or *.jpt] !!\n\n", 
259                                                         infile);
260                                                 return 1;
261                                 }
262                                 strncpy(parameters->infile, infile, sizeof(parameters->infile)-1);
263                         }
264                         break;
265                                 
266                                 /* ----------------------------------------------------- */
267
268                         case 'o':                       /* output file */
269                         {
270                                 char *outfile = optarg;
271                                 parameters->cod_format = get_file_format(outfile);
272                                 switch(parameters->cod_format) {
273                                         case PGX_DFMT:
274                                         case PXM_DFMT:
275                                         case BMP_DFMT:
276                                         case TIF_DFMT:
277                                                 break;
278                                         default:
279                                                 fprintf(stderr, "Unknown output format image %s [only *.pnm, *.pgm, *.ppm, *.pgx or *.bmp]!! \n", outfile);
280                                                 return 1;
281                                 }
282                                 strncpy(parameters->outfile, outfile, sizeof(parameters->outfile)-1);
283                         }
284                         break;
285                         
286                                 /* ----------------------------------------------------- */
287
288                         case 'O':                       /* output format */
289                         {
290                                 char outformat[50];
291                                 char *of = optarg;
292                                 sprintf(outformat,".%s",of);
293                                 img_fol->set_out_format = 1;
294                                 parameters->cod_format = get_file_format(outformat);
295                                 switch(parameters->cod_format) {
296                                         case PGX_DFMT:
297                                                 img_fol->out_format = "pgx";
298                                                 break;
299                                         case PXM_DFMT:
300                                                 img_fol->out_format = "ppm";
301                                                 break;
302                                         case BMP_DFMT:
303                                                 img_fol->out_format = "bmp";
304                                                 break;
305                                         case TIF_DFMT:
306                                                 img_fol->out_format = "tif";
307                                                 break;
308                                         default:
309                                                 fprintf(stderr, "Unknown output format image %s [only *.pnm, *.pgm, *.ppm, *.pgx or *.bmp]!! \n");
310                                                 return 1;
311                                                 break;
312                                 }
313                         }
314                         break;
315
316                                 /* ----------------------------------------------------- */
317
318
319                         case 'r':               /* reduce option */
320                         {
321                                 sscanf(optarg, "%d", &parameters->cp_reduce);
322                         }
323                         break;
324                         
325                                 /* ----------------------------------------------------- */
326       
327
328                         case 'l':               /* layering option */
329                         {
330                                 sscanf(optarg, "%d", &parameters->cp_layer);
331                         }
332                         break;
333                         
334                                 /* ----------------------------------------------------- */
335
336                         case 'h':                       /* display an help description */
337                                 decode_help_display();
338                                 return 1;                               
339
340                                 /* ------------------------------------------------------ */
341
342                         case 'y':                       /* Image Directory path */
343                                 {
344                                         img_fol->imgdirpath = (char*)malloc(strlen(optarg) + 1);
345                                         strcpy(img_fol->imgdirpath,optarg);
346                                         img_fol->set_imgdir=1;
347                                 }
348                                 break;
349                                 /* ----------------------------------------------------- */
350 /* UniPG>> */
351 #ifdef USE_JPWL
352                         
353                         case 'W':                       /* activate JPWL correction */
354                         {
355                                 char *token = NULL;
356
357                                 token = strtok(optarg, ",");
358                                 while(token != NULL) {
359
360                                         /* search expected number of components */
361                                         if (*token == 'c') {
362
363                                                 static int compno;
364
365                                                 compno = JPWL_EXPECTED_COMPONENTS; /* predefined no. of components */
366
367                                                 if(sscanf(token, "c=%d", &compno) == 1) {
368                                                         /* Specified */
369                                                         if ((compno < 1) || (compno > 256)) {
370                                                                 fprintf(stderr, "ERROR -> invalid number of components c = %d\n", compno);
371                                                                 return 1;
372                                                         }
373                                                         parameters->jpwl_exp_comps = compno;
374
375                                                 } else if (!strcmp(token, "c")) {
376                                                         /* default */
377                                                         parameters->jpwl_exp_comps = compno; /* auto for default size */
378
379                                                 } else {
380                                                         fprintf(stderr, "ERROR -> invalid components specified = %s\n", token);
381                                                         return 1;
382                                                 };
383                                         }
384
385                                         /* search maximum number of tiles */
386                                         if (*token == 't') {
387
388                                                 static int tileno;
389
390                                                 tileno = JPWL_MAXIMUM_TILES; /* maximum no. of tiles */
391
392                                                 if(sscanf(token, "t=%d", &tileno) == 1) {
393                                                         /* Specified */
394                                                         if ((tileno < 1) || (tileno > JPWL_MAXIMUM_TILES)) {
395                                                                 fprintf(stderr, "ERROR -> invalid number of tiles t = %d\n", tileno);
396                                                                 return 1;
397                                                         }
398                                                         parameters->jpwl_max_tiles = tileno;
399
400                                                 } else if (!strcmp(token, "t")) {
401                                                         /* default */
402                                                         parameters->jpwl_max_tiles = tileno; /* auto for default size */
403
404                                                 } else {
405                                                         fprintf(stderr, "ERROR -> invalid tiles specified = %s\n", token);
406                                                         return 1;
407                                                 };
408                                         }
409
410                                         /* next token or bust */
411                                         token = strtok(NULL, ",");
412                                 };
413                                 parameters->jpwl_correct = true;
414                                 fprintf(stdout, "JPWL correction capability activated\n");
415                                 fprintf(stdout, "- expecting %d components\n", parameters->jpwl_exp_comps);
416                         }
417                         break;  
418 #endif /* USE_JPWL */
419 /* <<UniPG */            
420
421                                 /* ----------------------------------------------------- */
422                         
423                         default:
424                                 fprintf(stderr,"WARNING -> this option is not valid \"-%c %s\"\n",c, optarg);
425                                 break;
426                 }
427         }
428
429         /* check for possible errors */
430         if(img_fol->set_imgdir==1){
431                 if(!(parameters->infile[0]==0)){
432                         fprintf(stderr, "Error: options -ImgDir and -i cannot be used together !!\n");
433                         return 1;
434                 }
435                 if(img_fol->set_out_format == 0){
436                         fprintf(stderr, "Error: When -ImgDir is used, -OutFor <FORMAT> must be used !!\n");
437                         fprintf(stderr, "Only one format allowed! Valid format PGM, PPM, PNM, PGX,BMP!!\n");
438                         return 1;
439                 }
440                 if(!((parameters->outfile[0] == 0))){
441                         fprintf(stderr, "Error: options -ImgDir and -o cannot be used together !!\n");
442                         return 1;
443                 }
444         }else{
445                 if((parameters->infile[0] == 0) || (parameters->outfile[0] == 0)) {
446                         fprintf(stderr, "Error: One of option; -i or -ImgDir must be specified\n");
447                         fprintf(stderr, "Error: When using -i; -o must be used\n");
448                         fprintf(stderr, "usage: image_to_j2k -i *.j2k/jp2 -o *.pgm/ppm/pnm/pgx/bmp(+ options)\n");
449                         return 1;
450                 }
451         }
452
453         return 0;
454 }
455
456 /* -------------------------------------------------------------------------- */
457
458 /**
459 sample error callback expecting a FILE* client object
460 */
461 void error_callback(const char *msg, void *client_data) {
462         FILE *stream = (FILE*)client_data;
463         fprintf(stream, "[ERROR] %s", msg);
464 }
465 /**
466 sample warning callback expecting a FILE* client object
467 */
468 void warning_callback(const char *msg, void *client_data) {
469         FILE *stream = (FILE*)client_data;
470         fprintf(stream, "[WARNING] %s", msg);
471 }
472 /**
473 sample debug callback expecting no client object
474 */
475 void info_callback(const char *msg, void *client_data) {
476         (void)client_data;
477         fprintf(stdout, "[INFO] %s", msg);
478 }
479
480 /* -------------------------------------------------------------------------- */
481
482 int main(int argc, char **argv) {
483         opj_dparameters_t parameters;   /* decompression parameters */
484         img_fol_t img_fol;
485         opj_event_mgr_t event_mgr;              /* event manager */
486         opj_image_t *image = NULL;
487         FILE *fsrc = NULL;
488         unsigned char *src = NULL;
489         int file_length;
490         int num_images;
491         int i,imageno;
492         dircnt_t *dirptr;
493         opj_dinfo_t* dinfo = NULL;      /* handle to a decompressor */
494         opj_cio_t *cio = NULL;
495
496         /* configure the event callbacks (not required) */
497         memset(&event_mgr, 0, sizeof(opj_event_mgr_t));
498         event_mgr.error_handler = error_callback;
499         event_mgr.warning_handler = warning_callback;
500         event_mgr.info_handler = info_callback;
501
502         /* set decoding parameters to default values */
503         opj_set_default_decoder_parameters(&parameters);
504
505
506         /* parse input and get user encoding parameters */
507         if(parse_cmdline_decoder(argc, argv, &parameters,&img_fol) == 1) {
508                 return 0;
509         }
510
511         if(img_fol.set_imgdir==1){      
512                 num_images=get_num_images(img_fol.imgdirpath);
513
514                 dirptr=(dircnt_t*)malloc(sizeof(dircnt_t));
515                 if(dirptr){
516                         dirptr->filename_buf = (char*)malloc(num_images*OPJ_PATH_LEN*sizeof(char));     // Stores at max 10 image file names
517                         dirptr->filename = (char**) malloc(num_images*sizeof(char*));
518
519                         if(!dirptr->filename_buf){
520                                 return 0;
521                         }
522                         for(i=0;i<num_images;i++){
523                                 dirptr->filename[i] = dirptr->filename_buf + i*OPJ_PATH_LEN;
524                         }
525                 }
526                 if(load_images(dirptr,img_fol.imgdirpath)==1){
527                         return 0;
528                 }
529                 if (num_images==0){
530                         fprintf(stdout,"Folder is empty\n");
531                         return 0;
532                 }
533         }else{
534                 num_images=1;
535         }
536
537         /*Encoding image one by one*/
538         for(imageno = 0; imageno < num_images ; imageno++)
539         {
540
541                 image = NULL;
542                 fprintf(stderr,"\n");
543
544                 if(img_fol.set_imgdir==1){
545                         if (get_next_file(imageno, dirptr,&img_fol, &parameters)) {
546                                 fprintf(stderr,"skipping file...\n");
547                                 continue;
548                         }
549
550                 }
551
552
553                 /* read the input file and put it in memory */
554                 /* ---------------------------------------- */
555                 fsrc = fopen(parameters.infile, "rb");
556                 if (!fsrc) {
557                         fprintf(stderr, "ERROR -> failed to open %s for reading\n", parameters.infile);
558                         return 1;
559                 }
560                 fseek(fsrc, 0, SEEK_END);
561                 file_length = ftell(fsrc);
562                 fseek(fsrc, 0, SEEK_SET);
563                 src = (unsigned char *) malloc(file_length);
564                 fread(src, 1, file_length, fsrc);
565                 fclose(fsrc);
566
567
568
569                 /* decode the code-stream */
570                 /* ---------------------- */
571
572                 switch(parameters.decod_format) {
573                 case J2K_CFMT:
574                 {
575                         /* JPEG-2000 codestream */
576
577                         /* get a decoder handle */
578                         dinfo = opj_create_decompress(CODEC_J2K);
579
580                         /* catch events using our callbacks and give a local context */
581                         opj_set_event_mgr((opj_common_ptr)dinfo, &event_mgr, stderr);
582
583                         /* setup the decoder decoding parameters using user parameters */
584                         opj_setup_decoder(dinfo, &parameters);
585
586                         /* open a byte stream */
587                         cio = opj_cio_open((opj_common_ptr)dinfo, src, file_length);
588
589                         /* decode the stream and fill the image structure */
590                         image = opj_decode(dinfo, cio);
591                         if(!image) {
592                                 fprintf(stderr, "ERROR -> j2k_to_image: failed to decode image!\n");
593                                 opj_destroy_decompress(dinfo);
594                                 opj_cio_close(cio);
595                                 return 1;
596                         }
597
598                         /* close the byte stream */
599                         opj_cio_close(cio);
600                 }
601                 break;
602
603                 case JP2_CFMT:
604                 {
605                         /* JPEG 2000 compressed image data */
606
607                         /* get a decoder handle */
608                         dinfo = opj_create_decompress(CODEC_JP2);
609
610                         /* catch events using our callbacks and give a local context */
611                         opj_set_event_mgr((opj_common_ptr)dinfo, &event_mgr, stderr);
612
613                         /* setup the decoder decoding parameters using the current image and user parameters */
614                         opj_setup_decoder(dinfo, &parameters);
615
616                         /* open a byte stream */
617                         cio = opj_cio_open((opj_common_ptr)dinfo, src, file_length);
618
619                         /* decode the stream and fill the image structure */
620                         image = opj_decode(dinfo, cio);
621                         if(!image) {
622                                 fprintf(stderr, "ERROR -> j2k_to_image: failed to decode image!\n");
623                                 opj_destroy_decompress(dinfo);
624                                 opj_cio_close(cio);
625                                 return 1;
626                         }
627
628                         /* close the byte stream */
629                         opj_cio_close(cio);
630
631                 }
632                 break;
633
634                 case JPT_CFMT:
635                 {
636                         /* JPEG 2000, JPIP */
637
638                         /* get a decoder handle */
639                         dinfo = opj_create_decompress(CODEC_JPT);
640
641                         /* catch events using our callbacks and give a local context */
642                         opj_set_event_mgr((opj_common_ptr)dinfo, &event_mgr, stderr);
643
644                         /* setup the decoder decoding parameters using user parameters */
645                         opj_setup_decoder(dinfo, &parameters);
646
647                         /* open a byte stream */
648                         cio = opj_cio_open((opj_common_ptr)dinfo, src, file_length);
649
650                         /* decode the stream and fill the image structure */
651                         image = opj_decode(dinfo, cio);
652                         if(!image) {
653                                 fprintf(stderr, "ERROR -> j2k_to_image: failed to decode image!\n");
654                                 opj_destroy_decompress(dinfo);
655                                 opj_cio_close(cio);
656                                 return 1;
657                         }
658
659                         /* close the byte stream */
660                         opj_cio_close(cio);
661                 }
662                 break;
663
664                 default:
665                         fprintf(stderr, "skipping file..\n");
666                         continue;
667         }
668
669                 /* free the memory containing the code-stream */
670                 free(src);
671                 src = NULL;
672
673                 /* create output image */
674                 /* ------------------- */
675                 switch (parameters.cod_format) {
676                 case PXM_DFMT:                  /* PNM PGM PPM */
677                         if (imagetopnm(image, parameters.outfile)) {
678                                 fprintf(stdout,"Outfile %s not generated\n",parameters.outfile);
679                         }
680                         else {
681                                 fprintf(stdout,"Generated Outfile %s\n",parameters.outfile);
682                         }
683                         break;
684
685                 case PGX_DFMT:                  /* PGX */
686                         if(imagetopgx(image, parameters.outfile)){
687                                 fprintf(stdout,"Outfile %s not generated\n",parameters.outfile);
688                         }
689                         else {
690                                 fprintf(stdout,"Generated Outfile %s\n",parameters.outfile);
691                         }
692                         break;
693
694                 case BMP_DFMT:                  /* BMP */
695                         if(imagetobmp(image, parameters.outfile)){
696                                 fprintf(stdout,"Outfile %s not generated\n",parameters.outfile);
697                         }
698                         else {
699                                 fprintf(stdout,"Generated Outfile %s\n",parameters.outfile);
700                         }
701                         break;
702
703                 case TIF_DFMT:                  /* TIFF */
704                         if(imagetotif(image, parameters.outfile)){
705                                 fprintf(stdout,"Outfile %s not generated\n",parameters.outfile);
706                         }
707                         else {
708                                 fprintf(stdout,"Generated Outfile %s\n",parameters.outfile);
709                         }
710                         break;
711                 }
712
713                 /* free remaining structures */
714                 if(dinfo) {
715                         opj_destroy_decompress(dinfo);
716                 }
717                 /* free image data structure */
718                 opj_image_destroy(image);
719
720         }
721         return 0;
722 }
723 //end main
724