2 * The copyright in this software is being made available under the 2-clauses
3 * BSD License, included below. This software may be subject to other third
4 * party and contributor rights, including patent rights, and no such rights
5 * are granted under this license.
7 * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium
8 * Copyright (c) 2002-2014, Professor Benoit Macq
9 * Copyright (c) 2001-2003, David Janssens
10 * Copyright (c) 2002-2003, Yannick Verschueren
11 * Copyright (c) 2003-2007, Francois-Olivier Devaux
12 * Copyright (c) 2003-2014, Antonin Descampe
13 * Copyright (c) 2005, Herve Drolon, FreeImage Team
14 * Copyright (c) 2006-2007, Parvatha Elangovan
15 * Copyright (c) 2008, 2011-2012, Centre National d'Etudes Spatiales (CNES), FR
16 * Copyright (c) 2012, CS Systemes d'Information, France
17 * All rights reserved.
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following conditions
22 * 1. Redistributions of source code must retain the above copyright
23 * notice, this list of conditions and the following disclaimer.
24 * 2. Redistributions in binary form must reproduce the above copyright
25 * notice, this list of conditions and the following disclaimer in the
26 * documentation and/or other materials provided with the distribution.
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
29 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
32 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38 * POSSIBILITY OF SUCH DAMAGE.
40 #include "opj_apps_config.h"
48 #include "windirent.h"
55 #define strcasecmp _stricmp
56 #define strncasecmp _strnicmp
60 #include <sys/resource.h>
61 #include <sys/times.h>
65 #include "opj_getopt.h"
69 #ifdef OPJ_HAVE_LIBLCMS2
72 #ifdef OPJ_HAVE_LIBLCMS1
77 #include "format_defs.h"
78 #include "opj_string.h"
80 typedef struct dircnt{
81 /** Buffer for holding images read from Directory*/
83 /** Pointer to the buffer*/
88 typedef struct img_folder{
89 /** The directory path of the folder containing input images*/
92 const char *out_format;
95 /** Enable Cod Format for output*/
100 typedef enum opj_prec_mode
104 } opj_precision_mode;
106 typedef struct opj_prec
109 opj_precision_mode mode;
112 typedef struct opj_decompress_params
114 /** core library parameters */
115 opj_dparameters_t core;
117 /** input file name */
118 char infile[OPJ_PATH_LEN];
119 /** output file name */
120 char outfile[OPJ_PATH_LEN];
121 /** input file format 0: J2K, 1: JP2, 2: JPT */
123 /** output file format 0: PGX, 1: PxM, 2: BMP */
125 /** index file name */
126 char indexfilename[OPJ_PATH_LEN];
128 /** Decoding area left boundary */
130 /** Decoding area right boundary */
132 /** Decoding area up boundary */
134 /** Decoding area bottom boundary */
139 /** tile number ot the decoded tile*/
140 OPJ_UINT32 tile_index;
141 /** Nb of tile to decode */
142 OPJ_UINT32 nb_tile_to_decode;
144 opj_precision* precision;
145 OPJ_UINT32 nb_precision;
147 /* force output colorspace to RGB */
149 /* upsample components according to their dx/dy values */
151 /* split output components to different files */
153 }opj_decompress_parameters;
155 /* -------------------------------------------------------------------------- */
157 int get_num_images(char *imgdirpath);
158 int load_images(dircnt_t *dirptr, char *imgdirpath);
159 int get_file_format(const char *filename);
160 char get_next_file(int imageno,dircnt_t *dirptr,img_fol_t *img_fol, opj_decompress_parameters *parameters);
161 static int infile_format(const char *fname);
163 int parse_cmdline_decoder(int argc, char **argv, opj_decompress_parameters *parameters,img_fol_t *img_fol);
164 int parse_DA_values( char* inArg, unsigned int *DA_x0, unsigned int *DA_y0, unsigned int *DA_x1, unsigned int *DA_y1);
166 static opj_image_t* convert_gray_to_rgb(opj_image_t* original);
168 /* -------------------------------------------------------------------------- */
169 static void decode_help_display(void) {
170 fprintf(stdout,"\nThis is the opj_decompress utility from the OpenJPEG project.\n"
171 "It decompresses JPEG 2000 codestreams to various image formats.\n"
172 "It has been compiled against openjp2 library v%s.\n\n",opj_version());
174 fprintf(stdout,"Parameters:\n"
177 " -ImgDir <directory> \n"
178 " Image file Directory path \n"
179 " -OutFor <PBM|PGM|PPM|PNM|PAM|PGX|PNG|BMP|TIF|RAW|RAWL|TGA>\n"
180 " REQUIRED only if -ImgDir is used\n"
181 " Output format for decompressed images.\n");
182 fprintf(stdout," -i <compressed file>\n"
183 " REQUIRED only if an Input image directory is not specified\n"
184 " Currently accepts J2K-files, JP2-files and JPT-files. The file type\n"
185 " is identified based on its suffix.\n");
186 fprintf(stdout," -o <decompressed file>\n"
188 " Currently accepts formats specified above (see OutFor option)\n"
189 " Binary data is written to the file (not ascii). If a PGX\n"
190 " filename is given, there will be as many output files as there are\n"
191 " components: an indice starting from 0 will then be appended to the\n"
192 " output filename, just before the \"pgx\" extension. If a PGM filename\n"
193 " is given and there are more than one component, only the first component\n"
194 " will be written to the file.\n");
195 fprintf(stdout," -r <reduce factor>\n"
196 " Set the number of highest resolution levels to be discarded. The\n"
197 " image resolution is effectively divided by 2 to the power of the\n"
198 " number of discarded levels. The reduce factor is limited by the\n"
199 " smallest total number of decomposition levels among tiles.\n"
200 " -l <number of quality layers to decode>\n"
201 " Set the maximum number of quality layers to decode. If there are\n"
202 " less quality layers than the specified number, all the quality layers\n"
204 fprintf(stdout," -x \n"
205 " Create an index file *.Idx (-x index_name.Idx) \n"
206 " -d <x0,y0,x1,y1>\n"
209 " By default all the image is decoded.\n"
210 " -t <tile_number>\n"
212 " Set the tile number of the decoded tile. Follow the JPEG2000 convention from left-up to bottom-up\n"
213 " By default all tiles are decoded.\n");
214 fprintf(stdout," -p <comp 0 precision>[C|S][,<comp 1 precision>[C|S][,...]]\n"
216 " Force the precision (bit depth) of components.\n");
217 fprintf(stdout," There shall be at least 1 value. Theres no limit on the number of values (comma separated, last values ignored if too much values).\n"
218 " If there are less values than components, the last value is used for remaining components.\n"
219 " If 'C' is specified (default), values are clipped.\n"
220 " If 'S' is specified, values are scaled.\n"
221 " A 0 value can be specified (meaning original bit depth).\n");
222 fprintf(stdout," -force-rgb\n"
223 " Force output image colorspace to RGB\n"
225 " Downsampled components will be upsampled to image size\n"
227 " Split output components to different files when writing to PNM\n"
231 fprintf(stdout," -W <options>\n"
232 " Activates the JPWL correction capability, if the codestream complies.\n"
233 " Options can be a comma separated list of <param=val> tokens:\n"
235 " numcomps is the number of expected components in the codestream\n"
236 " (search of first EPB rely upon this, default is %d)\n", JPWL_EXPECTED_COMPONENTS);
237 #endif /* USE_JPWL */
239 fprintf(stdout,"\n");
242 /* -------------------------------------------------------------------------- */
244 static OPJ_BOOL parse_precision(const char* option, opj_decompress_parameters* parameters)
246 const char* l_remaining = option;
247 OPJ_BOOL l_result = OPJ_TRUE;
250 if (parameters->precision) {
251 free(parameters->precision);
252 parameters->precision = NULL;
254 parameters->nb_precision = 0U;
263 count = sscanf(l_remaining, "%d%c%c", &prec, &mode, &comma);
268 if ((count == 2) || (mode==',')) {
276 if ((prec < 1) || (prec > 32)) {
277 fprintf(stderr,"Invalid precision %d in precision option %s\n", prec, option);
278 l_result = OPJ_FALSE;
281 if ((mode != 'C') && (mode != 'S')) {
282 fprintf(stderr,"Invalid precision mode %c in precision option %s\n", mode, option);
283 l_result = OPJ_FALSE;
287 fprintf(stderr,"Invalid character %c in precision option %s\n", comma, option);
288 l_result = OPJ_FALSE;
292 if (parameters->precision == NULL) {
294 parameters->precision = (opj_precision *)malloc(sizeof(opj_precision));
295 if (parameters->precision == NULL) {
296 fprintf(stderr,"Could not allocate memory for precision option\n");
297 l_result = OPJ_FALSE;
301 OPJ_UINT32 l_new_size = parameters->nb_precision + 1U;
302 opj_precision* l_new;
304 if (l_new_size == 0U) {
305 fprintf(stderr,"Could not allocate memory for precision option\n");
306 l_result = OPJ_FALSE;
310 l_new = (opj_precision *)realloc(parameters->precision, l_new_size * sizeof(opj_precision));
312 fprintf(stderr,"Could not allocate memory for precision option\n");
313 l_result = OPJ_FALSE;
316 parameters->precision = l_new;
319 parameters->precision[parameters->nb_precision].prec = (OPJ_UINT32)prec;
322 parameters->precision[parameters->nb_precision].mode = OPJ_PREC_MODE_CLIP;
325 parameters->precision[parameters->nb_precision].mode = OPJ_PREC_MODE_SCALE;
330 parameters->nb_precision++;
332 l_remaining = strchr(l_remaining, ',');
333 if (l_remaining == NULL) {
338 fprintf(stderr,"Could not parse precision option %s\n", option);
339 l_result = OPJ_FALSE;
347 /* -------------------------------------------------------------------------- */
349 int get_num_images(char *imgdirpath){
351 struct dirent* content;
354 /*Reading the input images from given input directory*/
356 dir= opendir(imgdirpath);
358 fprintf(stderr,"Could not open Folder %s\n",imgdirpath);
362 while((content=readdir(dir))!=NULL){
363 if(strcmp(".",content->d_name)==0 || strcmp("..",content->d_name)==0 )
371 /* -------------------------------------------------------------------------- */
372 int load_images(dircnt_t *dirptr, char *imgdirpath){
374 struct dirent* content;
377 /*Reading the input images from given input directory*/
379 dir= opendir(imgdirpath);
381 fprintf(stderr,"Could not open Folder %s\n",imgdirpath);
384 fprintf(stderr,"Folder opened successfully\n");
387 while((content=readdir(dir))!=NULL){
388 if(strcmp(".",content->d_name)==0 || strcmp("..",content->d_name)==0 )
391 strcpy(dirptr->filename[i],content->d_name);
398 /* -------------------------------------------------------------------------- */
399 int get_file_format(const char *filename) {
401 static const char *extension[] = {"pgx", "pnm", "pgm", "ppm", "bmp","tif", "raw", "rawl", "tga", "png", "j2k", "jp2", "jpt", "j2c", "jpc" };
402 static const int format[] = { PGX_DFMT, PXM_DFMT, PXM_DFMT, PXM_DFMT, BMP_DFMT, TIF_DFMT, RAW_DFMT, RAWL_DFMT, TGA_DFMT, PNG_DFMT, J2K_CFMT, JP2_CFMT, JPT_CFMT, J2K_CFMT, J2K_CFMT };
403 const char * ext = strrchr(filename, '.');
408 for(i = 0; i < sizeof(format)/sizeof(*format); i++) {
409 if(strcasecmp(ext, extension[i]) == 0) {
419 const char* path_separator = "\\";
421 const char* path_separator = "/";
424 /* -------------------------------------------------------------------------- */
425 char get_next_file(int imageno,dircnt_t *dirptr,img_fol_t *img_fol, opj_decompress_parameters *parameters){
426 char image_filename[OPJ_PATH_LEN], infilename[OPJ_PATH_LEN],outfilename[OPJ_PATH_LEN],temp_ofname[OPJ_PATH_LEN];
427 char *temp_p, temp1[OPJ_PATH_LEN]="";
429 strcpy(image_filename,dirptr->filename[imageno]);
430 fprintf(stderr,"File Number %d \"%s\"\n",imageno,image_filename);
431 sprintf(infilename, "%s%s%s", img_fol->imgdirpath, path_separator, image_filename);
432 parameters->decod_format = infile_format(infilename);
433 if (parameters->decod_format == -1)
435 if (opj_strcpy_s(parameters->infile, sizeof(parameters->infile), infilename) != 0) {
440 strcpy(temp_ofname,strtok(image_filename,"."));
441 while((temp_p = strtok(NULL,".")) != NULL){
442 strcat(temp_ofname,temp1);
443 sprintf(temp1,".%s",temp_p);
445 if(img_fol->set_out_format==1){
446 sprintf(outfilename,"%s/%s.%s",img_fol->imgdirpath,temp_ofname,img_fol->out_format);
447 if (opj_strcpy_s(parameters->outfile, sizeof(parameters->outfile), outfilename) != 0) {
454 /* -------------------------------------------------------------------------- */
455 #define JP2_RFC3745_MAGIC "\x00\x00\x00\x0c\x6a\x50\x20\x20\x0d\x0a\x87\x0a"
456 #define JP2_MAGIC "\x0d\x0a\x87\x0a"
457 /* position 45: "\xff\x52" */
458 #define J2K_CODESTREAM_MAGIC "\xff\x4f\xff\x51"
460 static int infile_format(const char *fname)
463 const char *s, *magic_s;
464 int ext_format, magic_format;
465 unsigned char buf[12];
466 OPJ_SIZE_T l_nb_read;
468 reader = fopen(fname, "rb");
474 l_nb_read = fread(buf, 1, 12, reader);
481 ext_format = get_file_format(fname);
483 if (ext_format == JPT_CFMT)
486 if (memcmp(buf, JP2_RFC3745_MAGIC, 12) == 0 || memcmp(buf, JP2_MAGIC, 4) == 0) {
487 magic_format = JP2_CFMT;
490 else if (memcmp(buf, J2K_CODESTREAM_MAGIC, 4) == 0) {
491 magic_format = J2K_CFMT;
492 magic_s = ".j2k or .jpc or .j2c";
497 if (magic_format == ext_format)
500 s = fname + strlen(fname) - 4;
502 fputs("\n===========================================\n", stderr);
503 fprintf(stderr, "The extension of this file is incorrect.\n"
504 "FOUND %s. SHOULD BE %s\n", s, magic_s);
505 fputs("===========================================\n", stderr);
510 /* -------------------------------------------------------------------------- */
512 * Parse the command line
514 /* -------------------------------------------------------------------------- */
515 int parse_cmdline_decoder(int argc, char **argv, opj_decompress_parameters *parameters,img_fol_t *img_fol) {
516 /* parse the command line */
518 opj_option_t long_option[]={
519 {"ImgDir", REQ_ARG, NULL,'y'},
520 {"OutFor", REQ_ARG, NULL,'O'},
521 {"force-rgb", NO_ARG, NULL, 1},
522 {"upsample", NO_ARG, NULL, 1},
523 {"split-pnm", NO_ARG, NULL, 1}
526 const char optlist[] = "i:o:r:l:x:d:t:p:"
531 #endif /* USE_JPWL */
535 long_option[2].flag = &(parameters->force_rgb);
536 long_option[3].flag = &(parameters->upsample);
537 long_option[4].flag = &(parameters->split_pnm);
538 totlen=sizeof(long_option);
539 opj_reset_options_reading();
540 img_fol->set_out_format = 0;
542 c = opj_getopt_long(argc, argv,optlist,long_option,totlen);
546 case 0: /* long opt with flag */
548 case 'i': /* input file */
550 char *infile = opj_optarg;
551 parameters->decod_format = infile_format(infile);
552 switch(parameters->decod_format) {
561 "!! infile cannot be read: %s !!\n\n",
566 "[ERROR] Unknown input file format: %s \n"
567 " Known file formats are *.j2k, *.jp2, *.jpc or *.jpt\n",
571 if (opj_strcpy_s(parameters->infile, sizeof(parameters->infile), infile) != 0) {
572 fprintf(stderr, "[ERROR] Path is too long\n");
578 /* ----------------------------------------------------- */
580 case 'o': /* output file */
582 char *outfile = opj_optarg;
583 parameters->cod_format = get_file_format(outfile);
584 switch(parameters->cod_format) {
602 fprintf(stderr, "Unknown output format image %s [only *.png, *.pnm, *.pgm, *.ppm, *.pgx, *.bmp, *.tif, *.raw or *.tga]!!\n", outfile);
605 if (opj_strcpy_s(parameters->outfile, sizeof(parameters->outfile), outfile) != 0) {
606 fprintf(stderr, "[ERROR] Path is too long\n");
612 /* ----------------------------------------------------- */
614 case 'O': /* output format */
617 char *of = opj_optarg;
618 sprintf(outformat,".%s",of);
619 img_fol->set_out_format = 1;
620 parameters->cod_format = get_file_format(outformat);
621 switch(parameters->cod_format) {
623 img_fol->out_format = "pgx";
626 img_fol->out_format = "ppm";
629 img_fol->out_format = "bmp";
632 img_fol->out_format = "tif";
635 img_fol->out_format = "raw";
638 img_fol->out_format = "rawl";
641 img_fol->out_format = "raw";
644 img_fol->out_format = "png";
647 fprintf(stderr, "Unknown output format image %s [only *.png, *.pnm, *.pgm, *.ppm, *.pgx, *.bmp, *.tif, *.raw or *.tga]!!\n", outformat);
654 /* ----------------------------------------------------- */
657 case 'r': /* reduce option */
659 sscanf(opj_optarg, "%u", &(parameters->core.cp_reduce));
663 /* ----------------------------------------------------- */
666 case 'l': /* layering option */
668 sscanf(opj_optarg, "%u", &(parameters->core.cp_layer));
672 /* ----------------------------------------------------- */
674 case 'h': /* display an help description */
675 decode_help_display();
678 /* ----------------------------------------------------- */
680 case 'y': /* Image Directory path */
682 img_fol->imgdirpath = (char*)malloc(strlen(opj_optarg) + 1);
683 if(img_fol->imgdirpath == NULL){
686 strcpy(img_fol->imgdirpath,opj_optarg);
687 img_fol->set_imgdir=1;
691 /* ----------------------------------------------------- */
693 case 'd': /* Input decode ROI */
695 size_t size_optarg = (size_t)strlen(opj_optarg) + 1U;
696 char *ROI_values = (char*) malloc(size_optarg);
697 if (ROI_values == NULL) {
698 fprintf(stderr, "[ERROR] Couldn't allocate memory\n");
701 ROI_values[0] = '\0';
702 memcpy(ROI_values, opj_optarg, size_optarg);
703 /*printf("ROI_values = %s [%d / %d]\n", ROI_values, strlen(ROI_values), size_optarg ); */
704 parse_DA_values( ROI_values, ¶meters->DA_x0, ¶meters->DA_y0, ¶meters->DA_x1, ¶meters->DA_y1);
710 /* ----------------------------------------------------- */
712 case 't': /* Input tile index */
714 sscanf(opj_optarg, "%u", ¶meters->tile_index);
715 parameters->nb_tile_to_decode = 1;
719 /* ----------------------------------------------------- */
721 case 'x': /* Creation of index file */
723 if (opj_strcpy_s(parameters->indexfilename, sizeof(parameters->indexfilename), opj_optarg) != 0) {
724 fprintf(stderr, "[ERROR] Path is too long\n");
730 /* ----------------------------------------------------- */
731 case 'p': /* Force precision */
733 if (!parse_precision(opj_optarg, parameters))
739 /* ----------------------------------------------------- */
744 case 'W': /* activate JPWL correction */
748 token = strtok(opj_optarg, ",");
749 while(token != NULL) {
751 /* search expected number of components */
756 compno = JPWL_EXPECTED_COMPONENTS; /* predefined no. of components */
758 if(sscanf(token, "c=%d", &compno) == 1) {
760 if ((compno < 1) || (compno > 256)) {
761 fprintf(stderr, "ERROR -> invalid number of components c = %d\n", compno);
764 parameters->jpwl_exp_comps = compno;
766 } else if (!strcmp(token, "c")) {
768 parameters->jpwl_exp_comps = compno; /* auto for default size */
771 fprintf(stderr, "ERROR -> invalid components specified = %s\n", token);
776 /* search maximum number of tiles */
781 tileno = JPWL_MAXIMUM_TILES; /* maximum no. of tiles */
783 if(sscanf(token, "t=%d", &tileno) == 1) {
785 if ((tileno < 1) || (tileno > JPWL_MAXIMUM_TILES)) {
786 fprintf(stderr, "ERROR -> invalid number of tiles t = %d\n", tileno);
789 parameters->jpwl_max_tiles = tileno;
791 } else if (!strcmp(token, "t")) {
793 parameters->jpwl_max_tiles = tileno; /* auto for default size */
796 fprintf(stderr, "ERROR -> invalid tiles specified = %s\n", token);
801 /* next token or bust */
802 token = strtok(NULL, ",");
804 parameters->jpwl_correct = OPJ_TRUE;
805 fprintf(stdout, "JPWL correction capability activated\n");
806 fprintf(stdout, "- expecting %d components\n", parameters->jpwl_exp_comps);
809 #endif /* USE_JPWL */
812 /* ----------------------------------------------------- */
815 fprintf(stderr, "[WARNING] An invalid option has been ignored.\n");
820 /* check for possible errors */
821 if(img_fol->set_imgdir==1){
822 if(!(parameters->infile[0]==0)){
823 fprintf(stderr, "[ERROR] options -ImgDir and -i cannot be used together.\n");
826 if(img_fol->set_out_format == 0){
827 fprintf(stderr, "[ERROR] When -ImgDir is used, -OutFor <FORMAT> must be used.\n");
828 fprintf(stderr, "Only one format allowed.\n"
829 "Valid format are PGM, PPM, PNM, PGX, BMP, TIF, RAW and TGA.\n");
832 if(!((parameters->outfile[0] == 0))){
833 fprintf(stderr, "[ERROR] options -ImgDir and -o cannot be used together.\n");
837 if((parameters->infile[0] == 0) || (parameters->outfile[0] == 0)) {
838 fprintf(stderr, "[ERROR] Required parameters are missing\n"
839 "Example: %s -i image.j2k -o image.pgm\n",argv[0]);
840 fprintf(stderr, " Help: %s -h\n",argv[0]);
848 /* -------------------------------------------------------------------------- */
850 * Parse decoding area input values
853 /* -------------------------------------------------------------------------- */
854 int parse_DA_values( char* inArg, unsigned int *DA_x0, unsigned int *DA_y0, unsigned int *DA_x1, unsigned int *DA_y1)
860 result = strtok( inArg, delims );
862 while( (result != NULL) && (it < 4 ) ) {
863 values[it] = atoi(result);
864 result = strtok( NULL, delims );
872 *DA_x0 = (OPJ_UINT32)values[0]; *DA_y0 = (OPJ_UINT32)values[1];
873 *DA_x1 = (OPJ_UINT32)values[2]; *DA_y1 = (OPJ_UINT32)values[3];
878 OPJ_FLOAT64 opj_clock(void) {
880 /* _WIN32: use QueryPerformance (very accurate) */
881 LARGE_INTEGER freq , t ;
882 /* freq is the clock speed of the CPU */
883 QueryPerformanceFrequency(&freq) ;
884 /* cout << "freq = " << ((double) freq.QuadPart) << endl; */
885 /* t is the high resolution performance counter (see MSDN) */
886 QueryPerformanceCounter ( & t ) ;
887 return freq.QuadPart ? (t.QuadPart / (OPJ_FLOAT64)freq.QuadPart) : 0;
889 /* Unix or Linux: use resource usage */
891 OPJ_FLOAT64 procTime;
892 /* (1) Get the rusage data structure at this moment (man getrusage) */
894 /* (2) What is the elapsed time ? - CPU time = User time + System time */
895 /* (2a) Get the seconds */
896 procTime = (OPJ_FLOAT64)(t.ru_utime.tv_sec + t.ru_stime.tv_sec);
897 /* (2b) More precisely! Get the microseconds part ! */
898 return ( procTime + (OPJ_FLOAT64)(t.ru_utime.tv_usec + t.ru_stime.tv_usec) * 1e-6 ) ;
902 /* -------------------------------------------------------------------------- */
905 sample error callback expecting a FILE* client object
907 static void error_callback(const char *msg, void *client_data) {
909 fprintf(stdout, "[ERROR] %s", msg);
912 sample warning callback expecting a FILE* client object
914 static void warning_callback(const char *msg, void *client_data) {
916 fprintf(stdout, "[WARNING] %s", msg);
919 sample debug callback expecting no client object
921 static void info_callback(const char *msg, void *client_data) {
923 fprintf(stdout, "[INFO] %s", msg);
926 static void set_default_parameters(opj_decompress_parameters* parameters)
929 memset(parameters, 0, sizeof(opj_decompress_parameters));
931 /* default decoding parameters (command line specific) */
932 parameters->decod_format = -1;
933 parameters->cod_format = -1;
935 /* default decoding parameters (core) */
936 opj_set_default_decoder_parameters(&(parameters->core));
940 static void destroy_parameters(opj_decompress_parameters* parameters)
943 if (parameters->precision) {
944 free(parameters->precision);
945 parameters->precision = NULL;
950 /* -------------------------------------------------------------------------- */
952 static opj_image_t* convert_gray_to_rgb(opj_image_t* original)
955 opj_image_t* l_new_image = NULL;
956 opj_image_cmptparm_t* l_new_components = NULL;
958 l_new_components = (opj_image_cmptparm_t*)malloc((original->numcomps + 2U) * sizeof(opj_image_cmptparm_t));
959 if (l_new_components == NULL) {
960 fprintf(stderr, "ERROR -> opj_decompress: failed to allocate memory for RGB image!\n");
961 opj_image_destroy(original);
965 l_new_components[0].bpp = l_new_components[1].bpp = l_new_components[2].bpp = original->comps[0].bpp;
966 l_new_components[0].dx = l_new_components[1].dx = l_new_components[2].dx = original->comps[0].dx;
967 l_new_components[0].dy = l_new_components[1].dy = l_new_components[2].dy = original->comps[0].dy;
968 l_new_components[0].h = l_new_components[1].h = l_new_components[2].h = original->comps[0].h;
969 l_new_components[0].w = l_new_components[1].w = l_new_components[2].w = original->comps[0].w;
970 l_new_components[0].prec = l_new_components[1].prec = l_new_components[2].prec = original->comps[0].prec;
971 l_new_components[0].sgnd = l_new_components[1].sgnd = l_new_components[2].sgnd = original->comps[0].sgnd;
972 l_new_components[0].x0 = l_new_components[1].x0 = l_new_components[2].x0 = original->comps[0].x0;
973 l_new_components[0].y0 = l_new_components[1].y0 = l_new_components[2].y0 = original->comps[0].y0;
975 for(compno = 1U; compno < original->numcomps; ++compno) {
976 l_new_components[compno+2U].bpp = original->comps[compno].bpp;
977 l_new_components[compno+2U].dx = original->comps[compno].dx;
978 l_new_components[compno+2U].dy = original->comps[compno].dy;
979 l_new_components[compno+2U].h = original->comps[compno].h;
980 l_new_components[compno+2U].w = original->comps[compno].w;
981 l_new_components[compno+2U].prec = original->comps[compno].prec;
982 l_new_components[compno+2U].sgnd = original->comps[compno].sgnd;
983 l_new_components[compno+2U].x0 = original->comps[compno].x0;
984 l_new_components[compno+2U].y0 = original->comps[compno].y0;
987 l_new_image = opj_image_create(original->numcomps + 2U, l_new_components, OPJ_CLRSPC_SRGB);
988 free(l_new_components);
989 if (l_new_image == NULL) {
990 fprintf(stderr, "ERROR -> opj_decompress: failed to allocate memory for RGB image!\n");
991 opj_image_destroy(original);
995 l_new_image->x0 = original->x0;
996 l_new_image->x1 = original->x1;
997 l_new_image->y0 = original->y0;
998 l_new_image->y1 = original->y1;
1000 l_new_image->comps[0].factor = l_new_image->comps[1].factor = l_new_image->comps[2].factor = original->comps[0].factor;
1001 l_new_image->comps[0].alpha = l_new_image->comps[1].alpha = l_new_image->comps[2].alpha = original->comps[0].alpha;
1002 l_new_image->comps[0].resno_decoded = l_new_image->comps[1].resno_decoded = l_new_image->comps[2].resno_decoded = original->comps[0].resno_decoded;
1004 memcpy(l_new_image->comps[0].data, original->comps[0].data, original->comps[0].w * original->comps[0].h * sizeof(OPJ_INT32));
1005 memcpy(l_new_image->comps[1].data, original->comps[0].data, original->comps[0].w * original->comps[0].h * sizeof(OPJ_INT32));
1006 memcpy(l_new_image->comps[2].data, original->comps[0].data, original->comps[0].w * original->comps[0].h * sizeof(OPJ_INT32));
1008 for(compno = 1U; compno < original->numcomps; ++compno) {
1009 l_new_image->comps[compno+2U].factor = original->comps[compno].factor;
1010 l_new_image->comps[compno+2U].alpha = original->comps[compno].alpha;
1011 l_new_image->comps[compno+2U].resno_decoded = original->comps[compno].resno_decoded;
1012 memcpy(l_new_image->comps[compno+2U].data, original->comps[compno].data, original->comps[compno].w * original->comps[compno].h * sizeof(OPJ_INT32));
1014 opj_image_destroy(original);
1018 /* -------------------------------------------------------------------------- */
1020 static opj_image_t* upsample_image_components(opj_image_t* original)
1022 opj_image_t* l_new_image = NULL;
1023 opj_image_cmptparm_t* l_new_components = NULL;
1024 OPJ_BOOL l_upsample_need = OPJ_FALSE;
1027 for (compno = 0U; compno < original->numcomps; ++compno) {
1028 if (original->comps[compno].factor > 0U) {
1029 fprintf(stderr, "ERROR -> opj_decompress: -upsample not supported with reduction\n");
1030 opj_image_destroy(original);
1033 if ((original->comps[compno].dx > 1U) || (original->comps[compno].dy > 1U)) {
1034 l_upsample_need = OPJ_TRUE;
1038 if (!l_upsample_need) {
1041 /* Upsample is needed */
1042 l_new_components = (opj_image_cmptparm_t*)malloc(original->numcomps * sizeof(opj_image_cmptparm_t));
1043 if (l_new_components == NULL) {
1044 fprintf(stderr, "ERROR -> opj_decompress: failed to allocate memory for upsampled components!\n");
1045 opj_image_destroy(original);
1049 for (compno = 0U; compno < original->numcomps; ++compno) {
1050 opj_image_cmptparm_t* l_new_cmp = &(l_new_components[compno]);
1051 opj_image_comp_t* l_org_cmp = &(original->comps[compno]);
1053 l_new_cmp->bpp = l_org_cmp->bpp;
1054 l_new_cmp->prec = l_org_cmp->prec;
1055 l_new_cmp->sgnd = l_org_cmp->sgnd;
1056 l_new_cmp->x0 = original->x0;
1057 l_new_cmp->y0 = original->y0;
1060 l_new_cmp->w = l_org_cmp->w; /* should be original->x1 - original->x0 for dx==1 */
1061 l_new_cmp->h = l_org_cmp->h; /* should be original->y1 - original->y0 for dy==0 */
1063 if (l_org_cmp->dx > 1U) {
1064 l_new_cmp->w = original->x1 - original->x0;
1067 if (l_org_cmp->dy > 1U) {
1068 l_new_cmp->h = original->y1 - original->y0;
1072 l_new_image = opj_image_create(original->numcomps, l_new_components, original->color_space);
1073 free(l_new_components);
1074 if (l_new_image == NULL) {
1075 fprintf(stderr, "ERROR -> opj_decompress: failed to allocate memory for upsampled components!\n");
1076 opj_image_destroy(original);
1080 l_new_image->x0 = original->x0;
1081 l_new_image->x1 = original->x1;
1082 l_new_image->y0 = original->y0;
1083 l_new_image->y1 = original->y1;
1085 for (compno = 0U; compno < original->numcomps; ++compno) {
1086 opj_image_comp_t* l_new_cmp = &(l_new_image->comps[compno]);
1087 opj_image_comp_t* l_org_cmp = &(original->comps[compno]);
1089 l_new_cmp->factor = l_org_cmp->factor;
1090 l_new_cmp->alpha = l_org_cmp->alpha;
1091 l_new_cmp->resno_decoded = l_org_cmp->resno_decoded;
1093 if ((l_org_cmp->dx > 1U) || (l_org_cmp->dy > 1U)) {
1094 const OPJ_INT32* l_src = l_org_cmp->data;
1095 OPJ_INT32* l_dst = l_new_cmp->data;
1097 OPJ_UINT32 xoff, yoff;
1099 /* need to take into account dx & dy */
1100 xoff = l_org_cmp->dx * l_org_cmp->x0 - original->x0;
1101 yoff = l_org_cmp->dy * l_org_cmp->y0 - original->y0;
1102 if ((xoff >= l_org_cmp->dx) || (yoff >= l_org_cmp->dy)) {
1103 fprintf(stderr, "ERROR -> opj_decompress: Invalid image/component parameters found when upsampling\n");
1104 opj_image_destroy(original);
1105 opj_image_destroy(l_new_image);
1109 for (y = 0U; y < yoff; ++y) {
1110 memset(l_dst, 0U, l_new_cmp->w * sizeof(OPJ_INT32));
1111 l_dst += l_new_cmp->w;
1114 if(l_new_cmp->h > (l_org_cmp->dy - 1U)) { /* check subtraction overflow for really small images */
1115 for (; y < l_new_cmp->h - (l_org_cmp->dy - 1U); y += l_org_cmp->dy) {
1120 for (x = 0U; x < xoff; ++x) {
1123 if (l_new_cmp->w > (l_org_cmp->dx - 1U)) { /* check subtraction overflow for really small images */
1124 for (; x < l_new_cmp->w - (l_org_cmp->dx - 1U); x += l_org_cmp->dx, ++xorg) {
1126 for (dx = 0U; dx < l_org_cmp->dx; ++dx) {
1127 l_dst[x + dx] = l_src[xorg];
1131 for (; x < l_new_cmp->w; ++x) {
1132 l_dst[x] = l_src[xorg];
1134 l_dst += l_new_cmp->w;
1136 for (dy = 1U; dy < l_org_cmp->dy; ++dy) {
1137 memcpy(l_dst, l_dst - l_new_cmp->w, l_new_cmp->w * sizeof(OPJ_INT32));
1138 l_dst += l_new_cmp->w;
1140 l_src += l_org_cmp->w;
1143 if (y < l_new_cmp->h) {
1148 for (x = 0U; x < xoff; ++x) {
1151 if (l_new_cmp->w > (l_org_cmp->dx - 1U)) { /* check subtraction overflow for really small images */
1152 for (; x < l_new_cmp->w - (l_org_cmp->dx - 1U); x += l_org_cmp->dx, ++xorg) {
1154 for (dx = 0U; dx < l_org_cmp->dx; ++dx) {
1155 l_dst[x + dx] = l_src[xorg];
1159 for (; x < l_new_cmp->w; ++x) {
1160 l_dst[x] = l_src[xorg];
1162 l_dst += l_new_cmp->w;
1164 for (; y < l_new_cmp->h; ++y) {
1165 memcpy(l_dst, l_dst - l_new_cmp->w, l_new_cmp->w * sizeof(OPJ_INT32));
1166 l_dst += l_new_cmp->w;
1171 memcpy(l_new_cmp->data, l_org_cmp->data, l_org_cmp->w * l_org_cmp->h * sizeof(OPJ_INT32));
1174 opj_image_destroy(original);
1178 /* -------------------------------------------------------------------------- */
1180 * OPJ_DECOMPRESS MAIN
1182 /* -------------------------------------------------------------------------- */
1183 int main(int argc, char **argv)
1185 opj_decompress_parameters parameters; /* decompression parameters */
1186 opj_image_t* image = NULL;
1187 opj_stream_t *l_stream = NULL; /* Stream */
1188 opj_codec_t* l_codec = NULL; /* Handle to a decompressor */
1189 opj_codestream_index_t* cstr_index = NULL;
1191 OPJ_INT32 num_images, imageno;
1193 dircnt_t *dirptr = NULL;
1195 OPJ_FLOAT64 t, tCumulative = 0;
1196 OPJ_UINT32 numDecompressedImages = 0;
1198 /* set decoding parameters to default values */
1199 set_default_parameters(¶meters);
1201 /* Initialize img_fol */
1202 memset(&img_fol,0,sizeof(img_fol_t));
1204 /* parse input and get user encoding parameters */
1205 if(parse_cmdline_decoder(argc, argv, ¶meters,&img_fol) == 1) {
1206 failed = 1; goto fin;
1209 /* Initialize reading of directory */
1210 if(img_fol.set_imgdir==1){
1212 num_images=get_num_images(img_fol.imgdirpath);
1214 dirptr=(dircnt_t*)malloc(sizeof(dircnt_t));
1216 destroy_parameters(¶meters);
1217 return EXIT_FAILURE;
1219 dirptr->filename_buf = (char*)malloc((size_t)num_images*OPJ_PATH_LEN*sizeof(char)); /* Stores at max 10 image file names*/
1220 if(!dirptr->filename_buf){
1221 failed = 1; goto fin;
1224 dirptr->filename = (char**) malloc((size_t)num_images*sizeof(char*));
1226 if(!dirptr->filename){
1227 failed = 1; goto fin;
1229 for(it_image=0;it_image<num_images;it_image++){
1230 dirptr->filename[it_image] = dirptr->filename_buf + it_image*OPJ_PATH_LEN;
1233 if(load_images(dirptr,img_fol.imgdirpath)==1){
1234 failed = 1; goto fin;
1237 fprintf(stdout,"Folder is empty\n");
1238 failed = 1; goto fin;
1244 /*Decoding image one by one*/
1245 for(imageno = 0; imageno < num_images ; imageno++) {
1247 fprintf(stderr,"\n");
1249 if(img_fol.set_imgdir==1){
1250 if (get_next_file(imageno, dirptr,&img_fol, ¶meters)) {
1251 fprintf(stderr,"skipping file...\n");
1252 destroy_parameters(¶meters);
1257 /* read the input file and put it in memory */
1258 /* ---------------------------------------- */
1260 l_stream = opj_stream_create_default_file_stream(parameters.infile,1);
1262 fprintf(stderr, "ERROR -> failed to create the stream from the file %s\n", parameters.infile);
1263 failed = 1; goto fin;
1266 /* decode the JPEG2000 stream */
1267 /* ---------------------- */
1269 switch(parameters.decod_format) {
1270 case J2K_CFMT: /* JPEG-2000 codestream */
1272 /* Get a decoder handle */
1273 l_codec = opj_create_decompress(OPJ_CODEC_J2K);
1276 case JP2_CFMT: /* JPEG 2000 compressed image data */
1278 /* Get a decoder handle */
1279 l_codec = opj_create_decompress(OPJ_CODEC_JP2);
1282 case JPT_CFMT: /* JPEG 2000, JPIP */
1284 /* Get a decoder handle */
1285 l_codec = opj_create_decompress(OPJ_CODEC_JPT);
1289 fprintf(stderr, "skipping file..\n");
1290 destroy_parameters(¶meters);
1291 opj_stream_destroy(l_stream);
1295 /* catch events using our callbacks and give a local context */
1296 opj_set_info_handler(l_codec, info_callback,00);
1297 opj_set_warning_handler(l_codec, warning_callback,00);
1298 opj_set_error_handler(l_codec, error_callback,00);
1302 /* Setup the decoder decoding parameters using user parameters */
1303 if ( !opj_setup_decoder(l_codec, &(parameters.core)) ){
1304 fprintf(stderr, "ERROR -> opj_decompress: failed to setup the decoder\n");
1305 opj_stream_destroy(l_stream);
1306 opj_destroy_codec(l_codec);
1307 failed = 1; goto fin;
1311 /* Read the main header of the codestream and if necessary the JP2 boxes*/
1312 if(! opj_read_header(l_stream, l_codec, &image)){
1313 fprintf(stderr, "ERROR -> opj_decompress: failed to read the header\n");
1314 opj_stream_destroy(l_stream);
1315 opj_destroy_codec(l_codec);
1316 opj_image_destroy(image);
1317 failed = 1; goto fin;
1320 if (!parameters.nb_tile_to_decode) {
1321 /* Optional if you want decode the entire image */
1322 if (!opj_set_decode_area(l_codec, image, (OPJ_INT32)parameters.DA_x0,
1323 (OPJ_INT32)parameters.DA_y0, (OPJ_INT32)parameters.DA_x1, (OPJ_INT32)parameters.DA_y1)){
1324 fprintf(stderr, "ERROR -> opj_decompress: failed to set the decoded area\n");
1325 opj_stream_destroy(l_stream);
1326 opj_destroy_codec(l_codec);
1327 opj_image_destroy(image);
1328 failed = 1; goto fin;
1331 /* Get the decoded image */
1332 if (!(opj_decode(l_codec, l_stream, image) && opj_end_decompress(l_codec, l_stream))) {
1333 fprintf(stderr,"ERROR -> opj_decompress: failed to decode image!\n");
1334 opj_destroy_codec(l_codec);
1335 opj_stream_destroy(l_stream);
1336 opj_image_destroy(image);
1337 failed = 1; goto fin;
1342 /* It is just here to illustrate how to use the resolution after set parameters */
1343 /*if (!opj_set_decoded_resolution_factor(l_codec, 5)) {
1344 fprintf(stderr, "ERROR -> opj_decompress: failed to set the resolution factor tile!\n");
1345 opj_destroy_codec(l_codec);
1346 opj_stream_destroy(l_stream);
1347 opj_image_destroy(image);
1348 failed = 1; goto fin;
1351 if (!opj_get_decoded_tile(l_codec, l_stream, image, parameters.tile_index)) {
1352 fprintf(stderr, "ERROR -> opj_decompress: failed to decode tile!\n");
1353 opj_destroy_codec(l_codec);
1354 opj_stream_destroy(l_stream);
1355 opj_image_destroy(image);
1356 failed = 1; goto fin;
1358 fprintf(stdout, "tile %d is decoded!\n\n", parameters.tile_index);
1361 tCumulative += opj_clock() - t;
1362 numDecompressedImages++;
1364 /* Close the byte stream */
1365 opj_stream_destroy(l_stream);
1367 if( image->color_space != OPJ_CLRSPC_SYCC
1368 && image->numcomps == 3 && image->comps[0].dx == image->comps[0].dy
1369 && image->comps[1].dx != 1 )
1370 image->color_space = OPJ_CLRSPC_SYCC;
1371 else if (image->numcomps <= 2)
1372 image->color_space = OPJ_CLRSPC_GRAY;
1374 if(image->color_space == OPJ_CLRSPC_SYCC){
1375 color_sycc_to_rgb(image);
1377 else if((image->color_space == OPJ_CLRSPC_CMYK) && (parameters.cod_format != TIF_DFMT)){
1378 color_cmyk_to_rgb(image);
1380 else if(image->color_space == OPJ_CLRSPC_EYCC){
1381 color_esycc_to_rgb(image);
1384 if(image->icc_profile_buf) {
1385 #if defined(OPJ_HAVE_LIBLCMS1) || defined(OPJ_HAVE_LIBLCMS2)
1386 if(image->icc_profile_len)
1387 color_apply_icc_profile(image);
1389 color_cielab_to_rgb(image);
1391 free(image->icc_profile_buf);
1392 image->icc_profile_buf = NULL; image->icc_profile_len = 0;
1395 /* Force output precision */
1396 /* ---------------------- */
1397 if (parameters.precision != NULL)
1400 for (compno = 0; compno < image->numcomps; ++compno)
1402 OPJ_UINT32 precno = compno;
1405 if (precno >= parameters.nb_precision) {
1406 precno = parameters.nb_precision - 1U;
1409 prec = parameters.precision[precno].prec;
1411 prec = image->comps[compno].prec;
1414 switch (parameters.precision[precno].mode) {
1415 case OPJ_PREC_MODE_CLIP:
1416 clip_component(&(image->comps[compno]), prec);
1418 case OPJ_PREC_MODE_SCALE:
1419 scale_component(&(image->comps[compno]), prec);
1428 /* Upsample components */
1429 /* ------------------- */
1430 if (parameters.upsample)
1432 image = upsample_image_components(image);
1433 if (image == NULL) {
1434 fprintf(stderr, "ERROR -> opj_decompress: failed to upsample image components!\n");
1435 opj_destroy_codec(l_codec);
1436 failed = 1; goto fin;
1440 /* Force RGB output */
1441 /* ---------------- */
1442 if (parameters.force_rgb)
1444 switch (image->color_space) {
1445 case OPJ_CLRSPC_SRGB:
1447 case OPJ_CLRSPC_GRAY:
1448 image = convert_gray_to_rgb(image);
1451 fprintf(stderr, "ERROR -> opj_decompress: don't know how to convert image to RGB colorspace!\n");
1452 opj_image_destroy(image);
1456 if (image == NULL) {
1457 fprintf(stderr, "ERROR -> opj_decompress: failed to convert to RGB image!\n");
1458 opj_destroy_codec(l_codec);
1459 failed = 1; goto fin;
1463 /* create output image */
1464 /* ------------------- */
1465 switch (parameters.cod_format) {
1466 case PXM_DFMT: /* PNM PGM PPM */
1467 if (imagetopnm(image, parameters.outfile, parameters.split_pnm)) {
1468 fprintf(stderr,"[ERROR] Outfile %s not generated\n",parameters.outfile);
1472 fprintf(stdout,"[INFO] Generated Outfile %s\n",parameters.outfile);
1476 case PGX_DFMT: /* PGX */
1477 if(imagetopgx(image, parameters.outfile)){
1478 fprintf(stderr,"[ERROR] Outfile %s not generated\n",parameters.outfile);
1482 fprintf(stdout,"[INFO] Generated Outfile %s\n",parameters.outfile);
1486 case BMP_DFMT: /* BMP */
1487 if(imagetobmp(image, parameters.outfile)){
1488 fprintf(stderr,"[ERROR] Outfile %s not generated\n",parameters.outfile);
1492 fprintf(stdout,"[INFO] Generated Outfile %s\n",parameters.outfile);
1495 #ifdef OPJ_HAVE_LIBTIFF
1496 case TIF_DFMT: /* TIFF */
1497 if(imagetotif(image, parameters.outfile)){
1498 fprintf(stderr,"[ERROR] Outfile %s not generated\n",parameters.outfile);
1502 fprintf(stdout,"[INFO] Generated Outfile %s\n",parameters.outfile);
1505 #endif /* OPJ_HAVE_LIBTIFF */
1506 case RAW_DFMT: /* RAW */
1507 if(imagetoraw(image, parameters.outfile)){
1508 fprintf(stderr,"[ERROR] Error generating raw file. Outfile %s not generated\n",parameters.outfile);
1512 fprintf(stdout,"[INFO] Generated Outfile %s\n",parameters.outfile);
1516 case RAWL_DFMT: /* RAWL */
1517 if(imagetorawl(image, parameters.outfile)){
1518 fprintf(stderr,"[ERROR] Error generating rawl file. Outfile %s not generated\n",parameters.outfile);
1522 fprintf(stdout,"[INFO] Generated Outfile %s\n",parameters.outfile);
1526 case TGA_DFMT: /* TGA */
1527 if(imagetotga(image, parameters.outfile)){
1528 fprintf(stderr,"[ERROR] Error generating tga file. Outfile %s not generated\n",parameters.outfile);
1532 fprintf(stdout,"[INFO] Generated Outfile %s\n",parameters.outfile);
1535 #ifdef OPJ_HAVE_LIBPNG
1536 case PNG_DFMT: /* PNG */
1537 if(imagetopng(image, parameters.outfile)){
1538 fprintf(stderr,"[ERROR] Error generating png file. Outfile %s not generated\n",parameters.outfile);
1542 fprintf(stdout,"[INFO] Generated Outfile %s\n",parameters.outfile);
1545 #endif /* OPJ_HAVE_LIBPNG */
1546 /* Can happen if output file is TIFF or PNG
1547 * and OPJ_HAVE_LIBTIF or OPJ_HAVE_LIBPNG is undefined
1550 fprintf(stderr,"[ERROR] Outfile %s not generated\n",parameters.outfile);
1554 /* free remaining structures */
1556 opj_destroy_codec(l_codec);
1560 /* free image data structure */
1561 opj_image_destroy(image);
1563 /* destroy the codestream index */
1564 opj_destroy_cstr_index(&cstr_index);
1566 if(failed) (void)remove(parameters.outfile); /* ignore return value */
1569 destroy_parameters(¶meters);
1570 if(failed && img_fol.imgdirpath) free(img_fol.imgdirpath);
1572 if(dirptr->filename) free(dirptr->filename);
1573 if(dirptr->filename_buf) free(dirptr->filename_buf);
1576 if (numDecompressedImages) {
1577 fprintf(stdout, "decode time: %d ms\n", (int)( (tCumulative * 1000.0) / (OPJ_FLOAT64)numDecompressedImages));
1579 return failed ? EXIT_FAILURE : EXIT_SUCCESS;