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"
49 #include "windirent.h"
56 #define strcasecmp _stricmp
57 #define strncasecmp _strnicmp
61 #include <sys/resource.h>
62 #include <sys/times.h>
66 #include "opj_getopt.h"
70 #ifdef OPJ_HAVE_LIBLCMS2
73 #ifdef OPJ_HAVE_LIBLCMS1
78 #include "format_defs.h"
79 #include "opj_string.h"
81 typedef struct dircnt {
82 /** Buffer for holding images read from Directory*/
84 /** Pointer to the buffer*/
89 typedef struct img_folder {
90 /** The directory path of the folder containing input images*/
93 const char *out_format;
96 /** Enable Cod Format for output*/
101 typedef enum opj_prec_mode {
104 } opj_precision_mode;
106 typedef struct opj_prec {
108 opj_precision_mode mode;
111 typedef struct opj_decompress_params {
112 /** core library parameters */
113 opj_dparameters_t core;
115 /** input file name */
116 char infile[OPJ_PATH_LEN];
117 /** output file name */
118 char outfile[OPJ_PATH_LEN];
119 /** input file format 0: J2K, 1: JP2, 2: JPT */
121 /** output file format 0: PGX, 1: PxM, 2: BMP */
123 /** index file name */
124 char indexfilename[OPJ_PATH_LEN];
126 /** Decoding area left boundary */
128 /** Decoding area right boundary */
130 /** Decoding area up boundary */
132 /** Decoding area bottom boundary */
137 /** tile number ot the decoded tile*/
138 OPJ_UINT32 tile_index;
139 /** Nb of tile to decode */
140 OPJ_UINT32 nb_tile_to_decode;
142 opj_precision* precision;
143 OPJ_UINT32 nb_precision;
145 /* force output colorspace to RGB */
147 /* upsample components according to their dx/dy values */
149 /* split output components to different files */
151 /** number of threads */
155 } opj_decompress_parameters;
157 /* -------------------------------------------------------------------------- */
159 int get_num_images(char *imgdirpath);
160 int load_images(dircnt_t *dirptr, char *imgdirpath);
161 int get_file_format(const char *filename);
162 char get_next_file(int imageno, dircnt_t *dirptr, img_fol_t *img_fol,
163 opj_decompress_parameters *parameters);
164 static int infile_format(const char *fname);
166 int parse_cmdline_decoder(int argc, char **argv,
167 opj_decompress_parameters *parameters, img_fol_t *img_fol);
168 int parse_DA_values(char* inArg, unsigned int *DA_x0, unsigned int *DA_y0,
169 unsigned int *DA_x1, unsigned int *DA_y1);
171 static opj_image_t* convert_gray_to_rgb(opj_image_t* original);
173 /* -------------------------------------------------------------------------- */
174 static void decode_help_display(void)
177 "\nThis is the opj_decompress utility from the OpenJPEG project.\n"
178 "It decompresses JPEG 2000 codestreams to various image formats.\n"
179 "It has been compiled against openjp2 library v%s.\n\n", opj_version());
181 fprintf(stdout, "Parameters:\n"
184 " -ImgDir <directory> \n"
185 " Image file Directory path \n"
186 " -OutFor <PBM|PGM|PPM|PNM|PAM|PGX|PNG|BMP|TIF|RAW|RAWL|TGA>\n"
187 " REQUIRED only if -ImgDir is used\n"
188 " Output format for decompressed images.\n");
189 fprintf(stdout, " -i <compressed file>\n"
190 " REQUIRED only if an Input image directory is not specified\n"
191 " Currently accepts J2K-files, JP2-files and JPT-files. The file type\n"
192 " is identified based on its suffix.\n");
193 fprintf(stdout, " -o <decompressed file>\n"
195 " Currently accepts formats specified above (see OutFor option)\n"
196 " Binary data is written to the file (not ascii). If a PGX\n"
197 " filename is given, there will be as many output files as there are\n"
198 " components: an indice starting from 0 will then be appended to the\n"
199 " output filename, just before the \"pgx\" extension. If a PGM filename\n"
200 " is given and there are more than one component, only the first component\n"
201 " will be written to the file.\n");
202 fprintf(stdout, " -r <reduce factor>\n"
203 " Set the number of highest resolution levels to be discarded. The\n"
204 " image resolution is effectively divided by 2 to the power of the\n"
205 " number of discarded levels. The reduce factor is limited by the\n"
206 " smallest total number of decomposition levels among tiles.\n"
207 " -l <number of quality layers to decode>\n"
208 " Set the maximum number of quality layers to decode. If there are\n"
209 " less quality layers than the specified number, all the quality layers\n"
211 fprintf(stdout, " -x \n"
212 " Create an index file *.Idx (-x index_name.Idx) \n"
213 " -d <x0,y0,x1,y1>\n"
216 " By default all the image is decoded.\n"
217 " -t <tile_number>\n"
219 " Set the tile number of the decoded tile. Follow the JPEG2000 convention from left-up to bottom-up\n"
220 " By default all tiles are decoded.\n");
221 fprintf(stdout, " -p <comp 0 precision>[C|S][,<comp 1 precision>[C|S][,...]]\n"
223 " Force the precision (bit depth) of components.\n");
225 " 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"
226 " If there are less values than components, the last value is used for remaining components.\n"
227 " If 'C' is specified (default), values are clipped.\n"
228 " If 'S' is specified, values are scaled.\n"
229 " A 0 value can be specified (meaning original bit depth).\n");
230 fprintf(stdout, " -force-rgb\n"
231 " Force output image colorspace to RGB\n"
233 " Downsampled components will be upsampled to image size\n"
235 " Split output components to different files when writing to PNM\n");
236 if (opj_has_thread_support()) {
237 fprintf(stdout, " -threads <num_threads>\n"
238 " Number of threads to use for decoding.\n");
240 fprintf(stdout, " -quiet\n"
241 " Disable output from the library and other output.\n");
244 fprintf(stdout, " -W <options>\n"
245 " Activates the JPWL correction capability, if the codestream complies.\n"
246 " Options can be a comma separated list of <param=val> tokens:\n"
248 " numcomps is the number of expected components in the codestream\n"
249 " (search of first EPB rely upon this, default is %d)\n",
250 JPWL_EXPECTED_COMPONENTS);
251 #endif /* USE_JPWL */
253 fprintf(stdout, "\n");
256 /* -------------------------------------------------------------------------- */
258 static OPJ_BOOL parse_precision(const char* option,
259 opj_decompress_parameters* parameters)
261 const char* l_remaining = option;
262 OPJ_BOOL l_result = OPJ_TRUE;
265 if (parameters->precision) {
266 free(parameters->precision);
267 parameters->precision = NULL;
269 parameters->nb_precision = 0U;
277 count = sscanf(l_remaining, "%d%c%c", &prec, &mode, &comma);
282 if ((count == 2) || (mode == ',')) {
290 if ((prec < 1) || (prec > 32)) {
291 fprintf(stderr, "Invalid precision %d in precision option %s\n", prec, option);
292 l_result = OPJ_FALSE;
295 if ((mode != 'C') && (mode != 'S')) {
296 fprintf(stderr, "Invalid precision mode %c in precision option %s\n", mode,
298 l_result = OPJ_FALSE;
302 fprintf(stderr, "Invalid character %c in precision option %s\n", comma, option);
303 l_result = OPJ_FALSE;
307 if (parameters->precision == NULL) {
309 parameters->precision = (opj_precision *)malloc(sizeof(opj_precision));
310 if (parameters->precision == NULL) {
311 fprintf(stderr, "Could not allocate memory for precision option\n");
312 l_result = OPJ_FALSE;
316 OPJ_UINT32 l_new_size = parameters->nb_precision + 1U;
317 opj_precision* l_new;
319 if (l_new_size == 0U) {
320 fprintf(stderr, "Could not allocate memory for precision option\n");
321 l_result = OPJ_FALSE;
325 l_new = (opj_precision *)realloc(parameters->precision,
326 l_new_size * sizeof(opj_precision));
328 fprintf(stderr, "Could not allocate memory for precision option\n");
329 l_result = OPJ_FALSE;
332 parameters->precision = l_new;
335 parameters->precision[parameters->nb_precision].prec = (OPJ_UINT32)prec;
338 parameters->precision[parameters->nb_precision].mode = OPJ_PREC_MODE_CLIP;
341 parameters->precision[parameters->nb_precision].mode = OPJ_PREC_MODE_SCALE;
346 parameters->nb_precision++;
348 l_remaining = strchr(l_remaining, ',');
349 if (l_remaining == NULL) {
354 fprintf(stderr, "Could not parse precision option %s\n", option);
355 l_result = OPJ_FALSE;
363 /* -------------------------------------------------------------------------- */
365 int get_num_images(char *imgdirpath)
368 struct dirent* content;
371 /*Reading the input images from given input directory*/
373 dir = opendir(imgdirpath);
375 fprintf(stderr, "Could not open Folder %s\n", imgdirpath);
379 while ((content = readdir(dir)) != NULL) {
380 if (strcmp(".", content->d_name) == 0 || strcmp("..", content->d_name) == 0) {
389 /* -------------------------------------------------------------------------- */
390 int load_images(dircnt_t *dirptr, char *imgdirpath)
393 struct dirent* content;
396 /*Reading the input images from given input directory*/
398 dir = opendir(imgdirpath);
400 fprintf(stderr, "Could not open Folder %s\n", imgdirpath);
403 fprintf(stderr, "Folder opened successfully\n");
406 while ((content = readdir(dir)) != NULL) {
407 if (strcmp(".", content->d_name) == 0 || strcmp("..", content->d_name) == 0) {
411 strcpy(dirptr->filename[i], content->d_name);
418 /* -------------------------------------------------------------------------- */
419 int get_file_format(const char *filename)
422 static const char *extension[] = {"pgx", "pnm", "pgm", "ppm", "bmp", "tif", "raw", "rawl", "tga", "png", "j2k", "jp2", "jpt", "j2c", "jpc" };
423 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 };
424 const char * ext = strrchr(filename, '.');
430 for (i = 0; i < sizeof(format) / sizeof(*format); i++) {
431 if (strcasecmp(ext, extension[i]) == 0) {
441 const char* path_separator = "\\";
443 const char* path_separator = "/";
446 /* -------------------------------------------------------------------------- */
447 char get_next_file(int imageno, dircnt_t *dirptr, img_fol_t *img_fol,
448 opj_decompress_parameters *parameters)
450 char image_filename[OPJ_PATH_LEN], infilename[OPJ_PATH_LEN],
451 outfilename[OPJ_PATH_LEN], temp_ofname[OPJ_PATH_LEN];
452 char *temp_p, temp1[OPJ_PATH_LEN] = "";
454 strcpy(image_filename, dirptr->filename[imageno]);
455 fprintf(stderr, "File Number %d \"%s\"\n", imageno, image_filename);
456 sprintf(infilename, "%s%s%s", img_fol->imgdirpath, path_separator,
458 parameters->decod_format = infile_format(infilename);
459 if (parameters->decod_format == -1) {
462 if (opj_strcpy_s(parameters->infile, sizeof(parameters->infile),
468 strcpy(temp_ofname, strtok(image_filename, "."));
469 while ((temp_p = strtok(NULL, ".")) != NULL) {
470 strcat(temp_ofname, temp1);
471 sprintf(temp1, ".%s", temp_p);
473 if (img_fol->set_out_format == 1) {
474 sprintf(outfilename, "%s/%s.%s", img_fol->imgdirpath, temp_ofname,
475 img_fol->out_format);
476 if (opj_strcpy_s(parameters->outfile, sizeof(parameters->outfile),
484 /* -------------------------------------------------------------------------- */
485 #define JP2_RFC3745_MAGIC "\x00\x00\x00\x0c\x6a\x50\x20\x20\x0d\x0a\x87\x0a"
486 #define JP2_MAGIC "\x0d\x0a\x87\x0a"
487 /* position 45: "\xff\x52" */
488 #define J2K_CODESTREAM_MAGIC "\xff\x4f\xff\x51"
490 static int infile_format(const char *fname)
493 const char *s, *magic_s;
494 int ext_format, magic_format;
495 unsigned char buf[12];
496 OPJ_SIZE_T l_nb_read;
498 reader = fopen(fname, "rb");
500 if (reader == NULL) {
505 l_nb_read = fread(buf, 1, 12, reader);
507 if (l_nb_read != 12) {
513 ext_format = get_file_format(fname);
515 if (ext_format == JPT_CFMT) {
519 if (memcmp(buf, JP2_RFC3745_MAGIC, 12) == 0 || memcmp(buf, JP2_MAGIC, 4) == 0) {
520 magic_format = JP2_CFMT;
522 } else if (memcmp(buf, J2K_CODESTREAM_MAGIC, 4) == 0) {
523 magic_format = J2K_CFMT;
524 magic_s = ".j2k or .jpc or .j2c";
529 if (magic_format == ext_format) {
533 s = fname + strlen(fname) - 4;
535 fputs("\n===========================================\n", stderr);
536 fprintf(stderr, "The extension of this file is incorrect.\n"
537 "FOUND %s. SHOULD BE %s\n", s, magic_s);
538 fputs("===========================================\n", stderr);
543 /* -------------------------------------------------------------------------- */
545 * Parse the command line
547 /* -------------------------------------------------------------------------- */
548 int parse_cmdline_decoder(int argc, char **argv,
549 opj_decompress_parameters *parameters, img_fol_t *img_fol)
551 /* parse the command line */
553 opj_option_t long_option[] = {
554 {"ImgDir", REQ_ARG, NULL, 'y'},
555 {"OutFor", REQ_ARG, NULL, 'O'},
556 {"force-rgb", NO_ARG, NULL, 1},
557 {"upsample", NO_ARG, NULL, 1},
558 {"split-pnm", NO_ARG, NULL, 1},
559 {"threads", REQ_ARG, NULL, 'T'},
560 {"quiet", NO_ARG, NULL, 1},
563 const char optlist[] = "i:o:r:l:x:d:t:p:"
568 #endif /* USE_JPWL */
572 long_option[2].flag = &(parameters->force_rgb);
573 long_option[3].flag = &(parameters->upsample);
574 long_option[4].flag = &(parameters->split_pnm);
575 long_option[6].flag = &(parameters->quiet);
576 totlen = sizeof(long_option);
577 opj_reset_options_reading();
578 img_fol->set_out_format = 0;
580 c = opj_getopt_long(argc, argv, optlist, long_option, totlen);
585 case 0: /* long opt with flag */
587 case 'i': { /* input file */
588 char *infile = opj_optarg;
589 parameters->decod_format = infile_format(infile);
590 switch (parameters->decod_format) {
599 "!! infile cannot be read: %s !!\n\n",
604 "[ERROR] Unknown input file format: %s \n"
605 " Known file formats are *.j2k, *.jp2, *.jpc or *.jpt\n",
609 if (opj_strcpy_s(parameters->infile, sizeof(parameters->infile), infile) != 0) {
610 fprintf(stderr, "[ERROR] Path is too long\n");
616 /* ----------------------------------------------------- */
618 case 'o': { /* output file */
619 char *outfile = opj_optarg;
620 parameters->cod_format = get_file_format(outfile);
621 switch (parameters->cod_format) {
640 "Unknown output format image %s [only *.png, *.pnm, *.pgm, *.ppm, *.pgx, *.bmp, *.tif, *.raw or *.tga]!!\n",
644 if (opj_strcpy_s(parameters->outfile, sizeof(parameters->outfile),
646 fprintf(stderr, "[ERROR] Path is too long\n");
652 /* ----------------------------------------------------- */
654 case 'O': { /* output format */
656 char *of = opj_optarg;
657 sprintf(outformat, ".%s", of);
658 img_fol->set_out_format = 1;
659 parameters->cod_format = get_file_format(outformat);
660 switch (parameters->cod_format) {
662 img_fol->out_format = "pgx";
665 img_fol->out_format = "ppm";
668 img_fol->out_format = "bmp";
671 img_fol->out_format = "tif";
674 img_fol->out_format = "raw";
677 img_fol->out_format = "rawl";
680 img_fol->out_format = "raw";
683 img_fol->out_format = "png";
687 "Unknown output format image %s [only *.png, *.pnm, *.pgm, *.ppm, *.pgx, *.bmp, *.tif, *.raw or *.tga]!!\n",
695 /* ----------------------------------------------------- */
698 case 'r': { /* reduce option */
699 sscanf(opj_optarg, "%u", &(parameters->core.cp_reduce));
703 /* ----------------------------------------------------- */
706 case 'l': { /* layering option */
707 sscanf(opj_optarg, "%u", &(parameters->core.cp_layer));
711 /* ----------------------------------------------------- */
713 case 'h': /* display an help description */
714 decode_help_display();
717 /* ----------------------------------------------------- */
719 case 'y': { /* Image Directory path */
720 img_fol->imgdirpath = (char*)malloc(strlen(opj_optarg) + 1);
721 if (img_fol->imgdirpath == NULL) {
724 strcpy(img_fol->imgdirpath, opj_optarg);
725 img_fol->set_imgdir = 1;
729 /* ----------------------------------------------------- */
731 case 'd': { /* Input decode ROI */
732 size_t size_optarg = (size_t)strlen(opj_optarg) + 1U;
733 char *ROI_values = (char*) malloc(size_optarg);
734 if (ROI_values == NULL) {
735 fprintf(stderr, "[ERROR] Couldn't allocate memory\n");
738 ROI_values[0] = '\0';
739 memcpy(ROI_values, opj_optarg, size_optarg);
740 /*printf("ROI_values = %s [%d / %d]\n", ROI_values, strlen(ROI_values), size_optarg ); */
741 parse_DA_values(ROI_values, ¶meters->DA_x0, ¶meters->DA_y0,
742 ¶meters->DA_x1, ¶meters->DA_y1);
748 /* ----------------------------------------------------- */
750 case 't': { /* Input tile index */
751 sscanf(opj_optarg, "%u", ¶meters->tile_index);
752 parameters->nb_tile_to_decode = 1;
756 /* ----------------------------------------------------- */
758 case 'x': { /* Creation of index file */
759 if (opj_strcpy_s(parameters->indexfilename, sizeof(parameters->indexfilename),
761 fprintf(stderr, "[ERROR] Path is too long\n");
767 /* ----------------------------------------------------- */
768 case 'p': { /* Force precision */
769 if (!parse_precision(opj_optarg, parameters)) {
774 /* ----------------------------------------------------- */
779 case 'W': { /* activate JPWL correction */
782 token = strtok(opj_optarg, ",");
783 while (token != NULL) {
785 /* search expected number of components */
790 compno = JPWL_EXPECTED_COMPONENTS; /* predefined no. of components */
792 if (sscanf(token, "c=%d", &compno) == 1) {
794 if ((compno < 1) || (compno > 256)) {
795 fprintf(stderr, "ERROR -> invalid number of components c = %d\n", compno);
798 parameters->jpwl_exp_comps = compno;
800 } else if (!strcmp(token, "c")) {
802 parameters->jpwl_exp_comps = compno; /* auto for default size */
805 fprintf(stderr, "ERROR -> invalid components specified = %s\n", token);
810 /* search maximum number of tiles */
815 tileno = JPWL_MAXIMUM_TILES; /* maximum no. of tiles */
817 if (sscanf(token, "t=%d", &tileno) == 1) {
819 if ((tileno < 1) || (tileno > JPWL_MAXIMUM_TILES)) {
820 fprintf(stderr, "ERROR -> invalid number of tiles t = %d\n", tileno);
823 parameters->jpwl_max_tiles = tileno;
825 } else if (!strcmp(token, "t")) {
827 parameters->jpwl_max_tiles = tileno; /* auto for default size */
830 fprintf(stderr, "ERROR -> invalid tiles specified = %s\n", token);
835 /* next token or bust */
836 token = strtok(NULL, ",");
838 parameters->jpwl_correct = OPJ_TRUE;
839 if (!(parameter->quiet)) {
840 fprintf(stdout, "JPWL correction capability activated\n");
841 fprintf(stdout, "- expecting %d components\n", parameters->jpwl_exp_comps);
845 #endif /* USE_JPWL */
848 /* ----------------------------------------------------- */
849 case 'T': { /* Number of threads */
850 if (strcmp(opj_optarg, "ALL_CPUS") == 0) {
851 parameters->num_threads = opj_get_num_cpus();
852 if (parameters->num_threads == 1) {
853 parameters->num_threads = 0;
856 sscanf(opj_optarg, "%d", ¶meters->num_threads);
861 /* ----------------------------------------------------- */
864 fprintf(stderr, "[WARNING] An invalid option has been ignored.\n");
869 /* check for possible errors */
870 if (img_fol->set_imgdir == 1) {
871 if (!(parameters->infile[0] == 0)) {
872 fprintf(stderr, "[ERROR] options -ImgDir and -i cannot be used together.\n");
875 if (img_fol->set_out_format == 0) {
877 "[ERROR] When -ImgDir is used, -OutFor <FORMAT> must be used.\n");
878 fprintf(stderr, "Only one format allowed.\n"
879 "Valid format are PGM, PPM, PNM, PGX, BMP, TIF, RAW and TGA.\n");
882 if (!((parameters->outfile[0] == 0))) {
883 fprintf(stderr, "[ERROR] options -ImgDir and -o cannot be used together.\n");
887 if ((parameters->infile[0] == 0) || (parameters->outfile[0] == 0)) {
888 fprintf(stderr, "[ERROR] Required parameters are missing\n"
889 "Example: %s -i image.j2k -o image.pgm\n", argv[0]);
890 fprintf(stderr, " Help: %s -h\n", argv[0]);
898 /* -------------------------------------------------------------------------- */
900 * Parse decoding area input values
903 /* -------------------------------------------------------------------------- */
904 int parse_DA_values(char* inArg, unsigned int *DA_x0, unsigned int *DA_y0,
905 unsigned int *DA_x1, unsigned int *DA_y1)
911 result = strtok(inArg, delims);
913 while ((result != NULL) && (it < 4)) {
914 values[it] = atoi(result);
915 result = strtok(NULL, delims);
922 *DA_x0 = (OPJ_UINT32)values[0];
923 *DA_y0 = (OPJ_UINT32)values[1];
924 *DA_x1 = (OPJ_UINT32)values[2];
925 *DA_y1 = (OPJ_UINT32)values[3];
930 OPJ_FLOAT64 opj_clock(void)
933 /* _WIN32: use QueryPerformance (very accurate) */
934 LARGE_INTEGER freq, t ;
935 /* freq is the clock speed of the CPU */
936 QueryPerformanceFrequency(&freq) ;
937 /* cout << "freq = " << ((double) freq.QuadPart) << endl; */
938 /* t is the high resolution performance counter (see MSDN) */
939 QueryPerformanceCounter(& t) ;
940 return freq.QuadPart ? ((OPJ_FLOAT64)t.QuadPart / (OPJ_FLOAT64)freq.QuadPart) :
942 #elif defined(__linux)
944 clock_gettime(CLOCK_REALTIME, &ts);
945 return ((OPJ_FLOAT64)ts.tv_sec + (OPJ_FLOAT64)ts.tv_nsec * 1e-9);
947 /* Unix : use resource usage */
948 /* FIXME: this counts the total CPU time, instead of the user perceived time */
950 OPJ_FLOAT64 procTime;
951 /* (1) Get the rusage data structure at this moment (man getrusage) */
953 /* (2) What is the elapsed time ? - CPU time = User time + System time */
954 /* (2a) Get the seconds */
955 procTime = (OPJ_FLOAT64)(t.ru_utime.tv_sec + t.ru_stime.tv_sec);
956 /* (2b) More precisely! Get the microseconds part ! */
957 return (procTime + (OPJ_FLOAT64)(t.ru_utime.tv_usec + t.ru_stime.tv_usec) *
962 /* -------------------------------------------------------------------------- */
965 sample error callback expecting a FILE* client object
967 static void error_callback(const char *msg, void *client_data)
970 fprintf(stdout, "[ERROR] %s", msg);
973 sample warning callback expecting a FILE* client object
975 static void warning_callback(const char *msg, void *client_data)
978 fprintf(stdout, "[WARNING] %s", msg);
981 sample debug callback expecting no client object
983 static void info_callback(const char *msg, void *client_data)
986 fprintf(stdout, "[INFO] %s", msg);
989 sample quiet callback expecting no client object
991 static void quiet_callback(const char *msg, void *client_data)
997 static void set_default_parameters(opj_decompress_parameters* parameters)
1000 memset(parameters, 0, sizeof(opj_decompress_parameters));
1002 /* default decoding parameters (command line specific) */
1003 parameters->decod_format = -1;
1004 parameters->cod_format = -1;
1006 /* default decoding parameters (core) */
1007 opj_set_default_decoder_parameters(&(parameters->core));
1011 static void destroy_parameters(opj_decompress_parameters* parameters)
1014 if (parameters->precision) {
1015 free(parameters->precision);
1016 parameters->precision = NULL;
1021 /* -------------------------------------------------------------------------- */
1023 static opj_image_t* convert_gray_to_rgb(opj_image_t* original)
1026 opj_image_t* l_new_image = NULL;
1027 opj_image_cmptparm_t* l_new_components = NULL;
1029 l_new_components = (opj_image_cmptparm_t*)malloc((original->numcomps + 2U) *
1030 sizeof(opj_image_cmptparm_t));
1031 if (l_new_components == NULL) {
1033 "ERROR -> opj_decompress: failed to allocate memory for RGB image!\n");
1034 opj_image_destroy(original);
1038 l_new_components[0].bpp = l_new_components[1].bpp = l_new_components[2].bpp =
1039 original->comps[0].bpp;
1040 l_new_components[0].dx = l_new_components[1].dx = l_new_components[2].dx =
1041 original->comps[0].dx;
1042 l_new_components[0].dy = l_new_components[1].dy = l_new_components[2].dy =
1043 original->comps[0].dy;
1044 l_new_components[0].h = l_new_components[1].h = l_new_components[2].h =
1045 original->comps[0].h;
1046 l_new_components[0].w = l_new_components[1].w = l_new_components[2].w =
1047 original->comps[0].w;
1048 l_new_components[0].prec = l_new_components[1].prec = l_new_components[2].prec =
1049 original->comps[0].prec;
1050 l_new_components[0].sgnd = l_new_components[1].sgnd = l_new_components[2].sgnd =
1051 original->comps[0].sgnd;
1052 l_new_components[0].x0 = l_new_components[1].x0 = l_new_components[2].x0 =
1053 original->comps[0].x0;
1054 l_new_components[0].y0 = l_new_components[1].y0 = l_new_components[2].y0 =
1055 original->comps[0].y0;
1057 for (compno = 1U; compno < original->numcomps; ++compno) {
1058 l_new_components[compno + 2U].bpp = original->comps[compno].bpp;
1059 l_new_components[compno + 2U].dx = original->comps[compno].dx;
1060 l_new_components[compno + 2U].dy = original->comps[compno].dy;
1061 l_new_components[compno + 2U].h = original->comps[compno].h;
1062 l_new_components[compno + 2U].w = original->comps[compno].w;
1063 l_new_components[compno + 2U].prec = original->comps[compno].prec;
1064 l_new_components[compno + 2U].sgnd = original->comps[compno].sgnd;
1065 l_new_components[compno + 2U].x0 = original->comps[compno].x0;
1066 l_new_components[compno + 2U].y0 = original->comps[compno].y0;
1069 l_new_image = opj_image_create(original->numcomps + 2U, l_new_components,
1071 free(l_new_components);
1072 if (l_new_image == NULL) {
1074 "ERROR -> opj_decompress: failed to allocate memory for RGB image!\n");
1075 opj_image_destroy(original);
1079 l_new_image->x0 = original->x0;
1080 l_new_image->x1 = original->x1;
1081 l_new_image->y0 = original->y0;
1082 l_new_image->y1 = original->y1;
1084 l_new_image->comps[0].factor = l_new_image->comps[1].factor =
1085 l_new_image->comps[2].factor = original->comps[0].factor;
1086 l_new_image->comps[0].alpha = l_new_image->comps[1].alpha =
1087 l_new_image->comps[2].alpha = original->comps[0].alpha;
1088 l_new_image->comps[0].resno_decoded = l_new_image->comps[1].resno_decoded =
1089 l_new_image->comps[2].resno_decoded = original->comps[0].resno_decoded;
1091 memcpy(l_new_image->comps[0].data, original->comps[0].data,
1092 original->comps[0].w * original->comps[0].h * sizeof(OPJ_INT32));
1093 memcpy(l_new_image->comps[1].data, original->comps[0].data,
1094 original->comps[0].w * original->comps[0].h * sizeof(OPJ_INT32));
1095 memcpy(l_new_image->comps[2].data, original->comps[0].data,
1096 original->comps[0].w * original->comps[0].h * sizeof(OPJ_INT32));
1098 for (compno = 1U; compno < original->numcomps; ++compno) {
1099 l_new_image->comps[compno + 2U].factor = original->comps[compno].factor;
1100 l_new_image->comps[compno + 2U].alpha = original->comps[compno].alpha;
1101 l_new_image->comps[compno + 2U].resno_decoded =
1102 original->comps[compno].resno_decoded;
1103 memcpy(l_new_image->comps[compno + 2U].data, original->comps[compno].data,
1104 original->comps[compno].w * original->comps[compno].h * sizeof(OPJ_INT32));
1106 opj_image_destroy(original);
1110 /* -------------------------------------------------------------------------- */
1112 static opj_image_t* upsample_image_components(opj_image_t* original)
1114 opj_image_t* l_new_image = NULL;
1115 opj_image_cmptparm_t* l_new_components = NULL;
1116 OPJ_BOOL l_upsample_need = OPJ_FALSE;
1119 for (compno = 0U; compno < original->numcomps; ++compno) {
1120 if (original->comps[compno].factor > 0U) {
1122 "ERROR -> opj_decompress: -upsample not supported with reduction\n");
1123 opj_image_destroy(original);
1126 if ((original->comps[compno].dx > 1U) || (original->comps[compno].dy > 1U)) {
1127 l_upsample_need = OPJ_TRUE;
1131 if (!l_upsample_need) {
1134 /* Upsample is needed */
1135 l_new_components = (opj_image_cmptparm_t*)malloc(original->numcomps * sizeof(
1136 opj_image_cmptparm_t));
1137 if (l_new_components == NULL) {
1139 "ERROR -> opj_decompress: failed to allocate memory for upsampled components!\n");
1140 opj_image_destroy(original);
1144 for (compno = 0U; compno < original->numcomps; ++compno) {
1145 opj_image_cmptparm_t* l_new_cmp = &(l_new_components[compno]);
1146 opj_image_comp_t* l_org_cmp = &(original->comps[compno]);
1148 l_new_cmp->bpp = l_org_cmp->bpp;
1149 l_new_cmp->prec = l_org_cmp->prec;
1150 l_new_cmp->sgnd = l_org_cmp->sgnd;
1151 l_new_cmp->x0 = original->x0;
1152 l_new_cmp->y0 = original->y0;
1156 l_org_cmp->w; /* should be original->x1 - original->x0 for dx==1 */
1158 l_org_cmp->h; /* should be original->y1 - original->y0 for dy==0 */
1160 if (l_org_cmp->dx > 1U) {
1161 l_new_cmp->w = original->x1 - original->x0;
1164 if (l_org_cmp->dy > 1U) {
1165 l_new_cmp->h = original->y1 - original->y0;
1169 l_new_image = opj_image_create(original->numcomps, l_new_components,
1170 original->color_space);
1171 free(l_new_components);
1172 if (l_new_image == NULL) {
1174 "ERROR -> opj_decompress: failed to allocate memory for upsampled components!\n");
1175 opj_image_destroy(original);
1179 l_new_image->x0 = original->x0;
1180 l_new_image->x1 = original->x1;
1181 l_new_image->y0 = original->y0;
1182 l_new_image->y1 = original->y1;
1184 for (compno = 0U; compno < original->numcomps; ++compno) {
1185 opj_image_comp_t* l_new_cmp = &(l_new_image->comps[compno]);
1186 opj_image_comp_t* l_org_cmp = &(original->comps[compno]);
1188 l_new_cmp->factor = l_org_cmp->factor;
1189 l_new_cmp->alpha = l_org_cmp->alpha;
1190 l_new_cmp->resno_decoded = l_org_cmp->resno_decoded;
1192 if ((l_org_cmp->dx > 1U) || (l_org_cmp->dy > 1U)) {
1193 const OPJ_INT32* l_src = l_org_cmp->data;
1194 OPJ_INT32* l_dst = l_new_cmp->data;
1196 OPJ_UINT32 xoff, yoff;
1198 /* need to take into account dx & dy */
1199 xoff = l_org_cmp->dx * l_org_cmp->x0 - original->x0;
1200 yoff = l_org_cmp->dy * l_org_cmp->y0 - original->y0;
1201 if ((xoff >= l_org_cmp->dx) || (yoff >= l_org_cmp->dy)) {
1203 "ERROR -> opj_decompress: Invalid image/component parameters found when upsampling\n");
1204 opj_image_destroy(original);
1205 opj_image_destroy(l_new_image);
1209 for (y = 0U; y < yoff; ++y) {
1210 memset(l_dst, 0U, l_new_cmp->w * sizeof(OPJ_INT32));
1211 l_dst += l_new_cmp->w;
1214 if (l_new_cmp->h > (l_org_cmp->dy -
1215 1U)) { /* check subtraction overflow for really small images */
1216 for (; y < l_new_cmp->h - (l_org_cmp->dy - 1U); y += l_org_cmp->dy) {
1221 for (x = 0U; x < xoff; ++x) {
1224 if (l_new_cmp->w > (l_org_cmp->dx -
1225 1U)) { /* check subtraction overflow for really small images */
1226 for (; x < l_new_cmp->w - (l_org_cmp->dx - 1U); x += l_org_cmp->dx, ++xorg) {
1228 for (dx = 0U; dx < l_org_cmp->dx; ++dx) {
1229 l_dst[x + dx] = l_src[xorg];
1233 for (; x < l_new_cmp->w; ++x) {
1234 l_dst[x] = l_src[xorg];
1236 l_dst += l_new_cmp->w;
1238 for (dy = 1U; dy < l_org_cmp->dy; ++dy) {
1239 memcpy(l_dst, l_dst - l_new_cmp->w, l_new_cmp->w * sizeof(OPJ_INT32));
1240 l_dst += l_new_cmp->w;
1242 l_src += l_org_cmp->w;
1245 if (y < l_new_cmp->h) {
1250 for (x = 0U; x < xoff; ++x) {
1253 if (l_new_cmp->w > (l_org_cmp->dx -
1254 1U)) { /* check subtraction overflow for really small images */
1255 for (; x < l_new_cmp->w - (l_org_cmp->dx - 1U); x += l_org_cmp->dx, ++xorg) {
1257 for (dx = 0U; dx < l_org_cmp->dx; ++dx) {
1258 l_dst[x + dx] = l_src[xorg];
1262 for (; x < l_new_cmp->w; ++x) {
1263 l_dst[x] = l_src[xorg];
1265 l_dst += l_new_cmp->w;
1267 for (; y < l_new_cmp->h; ++y) {
1268 memcpy(l_dst, l_dst - l_new_cmp->w, l_new_cmp->w * sizeof(OPJ_INT32));
1269 l_dst += l_new_cmp->w;
1273 memcpy(l_new_cmp->data, l_org_cmp->data,
1274 l_org_cmp->w * l_org_cmp->h * sizeof(OPJ_INT32));
1277 opj_image_destroy(original);
1281 /* -------------------------------------------------------------------------- */
1283 * OPJ_DECOMPRESS MAIN
1285 /* -------------------------------------------------------------------------- */
1286 int main(int argc, char **argv)
1288 opj_decompress_parameters parameters; /* decompression parameters */
1289 opj_image_t* image = NULL;
1290 opj_stream_t *l_stream = NULL; /* Stream */
1291 opj_codec_t* l_codec = NULL; /* Handle to a decompressor */
1292 opj_codestream_index_t* cstr_index = NULL;
1294 OPJ_INT32 num_images, imageno;
1296 dircnt_t *dirptr = NULL;
1298 OPJ_FLOAT64 t, tCumulative = 0;
1299 OPJ_UINT32 numDecompressedImages = 0;
1301 /* set decoding parameters to default values */
1302 set_default_parameters(¶meters);
1304 /* Initialize img_fol */
1305 memset(&img_fol, 0, sizeof(img_fol_t));
1307 /* parse input and get user encoding parameters */
1308 if (parse_cmdline_decoder(argc, argv, ¶meters, &img_fol) == 1) {
1314 /* Initialize reading of directory */
1315 if (img_fol.set_imgdir == 1) {
1317 num_images = get_num_images(img_fol.imgdirpath);
1319 dirptr = (dircnt_t*)malloc(sizeof(dircnt_t));
1321 destroy_parameters(¶meters);
1322 return EXIT_FAILURE;
1324 dirptr->filename_buf = (char*)malloc((size_t)num_images * OPJ_PATH_LEN * sizeof(
1325 char)); /* Stores at max 10 image file names*/
1326 if (!dirptr->filename_buf) {
1331 dirptr->filename = (char**) malloc((size_t)num_images * sizeof(char*));
1333 if (!dirptr->filename) {
1337 for (it_image = 0; it_image < num_images; it_image++) {
1338 dirptr->filename[it_image] = dirptr->filename_buf + it_image * OPJ_PATH_LEN;
1341 if (load_images(dirptr, img_fol.imgdirpath) == 1) {
1345 if (num_images == 0) {
1346 fprintf(stderr, "Folder is empty\n");
1354 /*Decoding image one by one*/
1355 for (imageno = 0; imageno < num_images ; imageno++) {
1357 if (!parameters.quiet) {
1358 fprintf(stderr, "\n");
1361 if (img_fol.set_imgdir == 1) {
1362 if (get_next_file(imageno, dirptr, &img_fol, ¶meters)) {
1363 fprintf(stderr, "skipping file...\n");
1364 destroy_parameters(¶meters);
1369 /* read the input file and put it in memory */
1370 /* ---------------------------------------- */
1372 l_stream = opj_stream_create_default_file_stream(parameters.infile, 1);
1374 fprintf(stderr, "ERROR -> failed to create the stream from the file %s\n",
1380 /* decode the JPEG2000 stream */
1381 /* ---------------------- */
1383 switch (parameters.decod_format) {
1384 case J2K_CFMT: { /* JPEG-2000 codestream */
1385 /* Get a decoder handle */
1386 l_codec = opj_create_decompress(OPJ_CODEC_J2K);
1389 case JP2_CFMT: { /* JPEG 2000 compressed image data */
1390 /* Get a decoder handle */
1391 l_codec = opj_create_decompress(OPJ_CODEC_JP2);
1394 case JPT_CFMT: { /* JPEG 2000, JPIP */
1395 /* Get a decoder handle */
1396 l_codec = opj_create_decompress(OPJ_CODEC_JPT);
1400 fprintf(stderr, "skipping file..\n");
1401 destroy_parameters(¶meters);
1402 opj_stream_destroy(l_stream);
1406 if (parameters.quiet) {
1407 /* Set all callbacks to quiet */
1408 opj_set_info_handler(l_codec, quiet_callback, 00);
1409 opj_set_warning_handler(l_codec, quiet_callback, 00);
1410 opj_set_error_handler(l_codec, quiet_callback, 00);
1412 /* catch events using our callbacks and give a local context */
1413 opj_set_info_handler(l_codec, info_callback, 00);
1414 opj_set_warning_handler(l_codec, warning_callback, 00);
1415 opj_set_error_handler(l_codec, error_callback, 00);
1421 /* Setup the decoder decoding parameters using user parameters */
1422 if (!opj_setup_decoder(l_codec, &(parameters.core))) {
1423 fprintf(stderr, "ERROR -> opj_decompress: failed to setup the decoder\n");
1424 opj_stream_destroy(l_stream);
1425 opj_destroy_codec(l_codec);
1430 if (parameters.num_threads >= 1 &&
1431 !opj_codec_set_threads(l_codec, parameters.num_threads)) {
1432 fprintf(stderr, "ERROR -> opj_decompress: failed to set number of threads\n");
1433 opj_stream_destroy(l_stream);
1434 opj_destroy_codec(l_codec);
1439 /* Read the main header of the codestream and if necessary the JP2 boxes*/
1440 if (! opj_read_header(l_stream, l_codec, &image)) {
1441 fprintf(stderr, "ERROR -> opj_decompress: failed to read the header\n");
1442 opj_stream_destroy(l_stream);
1443 opj_destroy_codec(l_codec);
1444 opj_image_destroy(image);
1449 if (!parameters.nb_tile_to_decode) {
1450 /* Optional if you want decode the entire image */
1451 if (!opj_set_decode_area(l_codec, image, (OPJ_INT32)parameters.DA_x0,
1452 (OPJ_INT32)parameters.DA_y0, (OPJ_INT32)parameters.DA_x1,
1453 (OPJ_INT32)parameters.DA_y1)) {
1454 fprintf(stderr, "ERROR -> opj_decompress: failed to set the decoded area\n");
1455 opj_stream_destroy(l_stream);
1456 opj_destroy_codec(l_codec);
1457 opj_image_destroy(image);
1462 /* Get the decoded image */
1463 if (!(opj_decode(l_codec, l_stream, image) &&
1464 opj_end_decompress(l_codec, l_stream))) {
1465 fprintf(stderr, "ERROR -> opj_decompress: failed to decode image!\n");
1466 opj_destroy_codec(l_codec);
1467 opj_stream_destroy(l_stream);
1468 opj_image_destroy(image);
1474 /* It is just here to illustrate how to use the resolution after set parameters */
1475 /*if (!opj_set_decoded_resolution_factor(l_codec, 5)) {
1476 fprintf(stderr, "ERROR -> opj_decompress: failed to set the resolution factor tile!\n");
1477 opj_destroy_codec(l_codec);
1478 opj_stream_destroy(l_stream);
1479 opj_image_destroy(image);
1480 failed = 1; goto fin;
1483 if (!opj_get_decoded_tile(l_codec, l_stream, image, parameters.tile_index)) {
1484 fprintf(stderr, "ERROR -> opj_decompress: failed to decode tile!\n");
1485 opj_destroy_codec(l_codec);
1486 opj_stream_destroy(l_stream);
1487 opj_image_destroy(image);
1491 if (!(parameters.quiet)) {
1492 fprintf(stdout, "tile %d is decoded!\n\n", parameters.tile_index);
1496 /* FIXME? Shouldn't that situation be considered as an error of */
1497 /* opj_decode() / opj_get_decoded_tile() ? */
1498 if (image->comps[0].data == NULL) {
1499 fprintf(stderr, "ERROR -> opj_decompress: no image data!\n");
1500 opj_destroy_codec(l_codec);
1501 opj_stream_destroy(l_stream);
1502 opj_image_destroy(image);
1507 tCumulative += opj_clock() - t;
1508 numDecompressedImages++;
1510 /* Close the byte stream */
1511 opj_stream_destroy(l_stream);
1513 if (image->color_space != OPJ_CLRSPC_SYCC
1514 && image->numcomps == 3 && image->comps[0].dx == image->comps[0].dy
1515 && image->comps[1].dx != 1) {
1516 image->color_space = OPJ_CLRSPC_SYCC;
1517 } else if (image->numcomps <= 2) {
1518 image->color_space = OPJ_CLRSPC_GRAY;
1521 if (image->color_space == OPJ_CLRSPC_SYCC) {
1522 color_sycc_to_rgb(image);
1523 } else if ((image->color_space == OPJ_CLRSPC_CMYK) &&
1524 (parameters.cod_format != TIF_DFMT)) {
1525 color_cmyk_to_rgb(image);
1526 } else if (image->color_space == OPJ_CLRSPC_EYCC) {
1527 color_esycc_to_rgb(image);
1530 if (image->icc_profile_buf) {
1531 #if defined(OPJ_HAVE_LIBLCMS1) || defined(OPJ_HAVE_LIBLCMS2)
1532 if (image->icc_profile_len) {
1533 color_apply_icc_profile(image);
1535 color_cielab_to_rgb(image);
1538 free(image->icc_profile_buf);
1539 image->icc_profile_buf = NULL;
1540 image->icc_profile_len = 0;
1543 /* Force output precision */
1544 /* ---------------------- */
1545 if (parameters.precision != NULL) {
1547 for (compno = 0; compno < image->numcomps; ++compno) {
1548 OPJ_UINT32 precno = compno;
1551 if (precno >= parameters.nb_precision) {
1552 precno = parameters.nb_precision - 1U;
1555 prec = parameters.precision[precno].prec;
1557 prec = image->comps[compno].prec;
1560 switch (parameters.precision[precno].mode) {
1561 case OPJ_PREC_MODE_CLIP:
1562 clip_component(&(image->comps[compno]), prec);
1564 case OPJ_PREC_MODE_SCALE:
1565 scale_component(&(image->comps[compno]), prec);
1574 /* Upsample components */
1575 /* ------------------- */
1576 if (parameters.upsample) {
1577 image = upsample_image_components(image);
1578 if (image == NULL) {
1580 "ERROR -> opj_decompress: failed to upsample image components!\n");
1581 opj_destroy_codec(l_codec);
1587 /* Force RGB output */
1588 /* ---------------- */
1589 if (parameters.force_rgb) {
1590 switch (image->color_space) {
1591 case OPJ_CLRSPC_SRGB:
1593 case OPJ_CLRSPC_GRAY:
1594 image = convert_gray_to_rgb(image);
1598 "ERROR -> opj_decompress: don't know how to convert image to RGB colorspace!\n");
1599 opj_image_destroy(image);
1603 if (image == NULL) {
1604 fprintf(stderr, "ERROR -> opj_decompress: failed to convert to RGB image!\n");
1605 opj_destroy_codec(l_codec);
1611 /* create output image */
1612 /* ------------------- */
1613 switch (parameters.cod_format) {
1614 case PXM_DFMT: /* PNM PGM PPM */
1615 if (imagetopnm(image, parameters.outfile, parameters.split_pnm)) {
1616 fprintf(stderr, "[ERROR] Outfile %s not generated\n", parameters.outfile);
1618 } else if (!(parameters.quiet)) {
1619 fprintf(stdout, "[INFO] Generated Outfile %s\n", parameters.outfile);
1623 case PGX_DFMT: /* PGX */
1624 if (imagetopgx(image, parameters.outfile)) {
1625 fprintf(stderr, "[ERROR] Outfile %s not generated\n", parameters.outfile);
1627 } else if (!(parameters.quiet)) {
1628 fprintf(stdout, "[INFO] Generated Outfile %s\n", parameters.outfile);
1632 case BMP_DFMT: /* BMP */
1633 if (imagetobmp(image, parameters.outfile)) {
1634 fprintf(stderr, "[ERROR] Outfile %s not generated\n", parameters.outfile);
1636 } else if (!(parameters.quiet)) {
1637 fprintf(stdout, "[INFO] Generated Outfile %s\n", parameters.outfile);
1640 #ifdef OPJ_HAVE_LIBTIFF
1641 case TIF_DFMT: /* TIFF */
1642 if (imagetotif(image, parameters.outfile)) {
1643 fprintf(stderr, "[ERROR] Outfile %s not generated\n", parameters.outfile);
1645 } else if (!(parameters.quiet)) {
1646 fprintf(stdout, "[INFO] Generated Outfile %s\n", parameters.outfile);
1649 #endif /* OPJ_HAVE_LIBTIFF */
1650 case RAW_DFMT: /* RAW */
1651 if (imagetoraw(image, parameters.outfile)) {
1652 fprintf(stderr, "[ERROR] Error generating raw file. Outfile %s not generated\n",
1653 parameters.outfile);
1655 } else if (!(parameters.quiet)) {
1656 fprintf(stdout, "[INFO] Generated Outfile %s\n", parameters.outfile);
1660 case RAWL_DFMT: /* RAWL */
1661 if (imagetorawl(image, parameters.outfile)) {
1663 "[ERROR] Error generating rawl file. Outfile %s not generated\n",
1664 parameters.outfile);
1666 } else if (!(parameters.quiet)) {
1667 fprintf(stdout, "[INFO] Generated Outfile %s\n", parameters.outfile);
1671 case TGA_DFMT: /* TGA */
1672 if (imagetotga(image, parameters.outfile)) {
1673 fprintf(stderr, "[ERROR] Error generating tga file. Outfile %s not generated\n",
1674 parameters.outfile);
1676 } else if (!(parameters.quiet)) {
1677 fprintf(stdout, "[INFO] Generated Outfile %s\n", parameters.outfile);
1680 #ifdef OPJ_HAVE_LIBPNG
1681 case PNG_DFMT: /* PNG */
1682 if (imagetopng(image, parameters.outfile)) {
1683 fprintf(stderr, "[ERROR] Error generating png file. Outfile %s not generated\n",
1684 parameters.outfile);
1686 } else if (!(parameters.quiet)) {
1687 fprintf(stdout, "[INFO] Generated Outfile %s\n", parameters.outfile);
1690 #endif /* OPJ_HAVE_LIBPNG */
1691 /* Can happen if output file is TIFF or PNG
1692 * and OPJ_HAVE_LIBTIF or OPJ_HAVE_LIBPNG is undefined
1695 fprintf(stderr, "[ERROR] Outfile %s not generated\n", parameters.outfile);
1699 /* free remaining structures */
1701 opj_destroy_codec(l_codec);
1705 /* free image data structure */
1706 opj_image_destroy(image);
1708 /* destroy the codestream index */
1709 opj_destroy_cstr_index(&cstr_index);
1712 (void)remove(parameters.outfile); /* ignore return value */
1716 destroy_parameters(¶meters);
1717 if (failed && img_fol.imgdirpath) {
1718 free(img_fol.imgdirpath);
1721 if (dirptr->filename) {
1722 free(dirptr->filename);
1724 if (dirptr->filename_buf) {
1725 free(dirptr->filename_buf);
1729 if (numDecompressedImages && !failed && !(parameters.quiet)) {
1730 fprintf(stdout, "decode time: %d ms\n",
1731 (int)((tCumulative * 1000.0) / (OPJ_FLOAT64)numDecompressedImages));
1733 return failed ? EXIT_FAILURE : EXIT_SUCCESS;