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 /** number of components to decode */
157 /** indices of components to decode */
158 OPJ_UINT32* comps_indices;
159 } opj_decompress_parameters;
161 /* -------------------------------------------------------------------------- */
163 int get_num_images(char *imgdirpath);
164 int load_images(dircnt_t *dirptr, char *imgdirpath);
165 int get_file_format(const char *filename);
166 char get_next_file(int imageno, dircnt_t *dirptr, img_fol_t *img_fol,
167 opj_decompress_parameters *parameters);
168 static int infile_format(const char *fname);
170 int parse_cmdline_decoder(int argc, char **argv,
171 opj_decompress_parameters *parameters, img_fol_t *img_fol);
172 int parse_DA_values(char* inArg, unsigned int *DA_x0, unsigned int *DA_y0,
173 unsigned int *DA_x1, unsigned int *DA_y1);
175 static opj_image_t* convert_gray_to_rgb(opj_image_t* original);
177 /* -------------------------------------------------------------------------- */
178 static void decode_help_display(void)
181 "\nThis is the opj_decompress utility from the OpenJPEG project.\n"
182 "It decompresses JPEG 2000 codestreams to various image formats.\n"
183 "It has been compiled against openjp2 library v%s.\n\n", opj_version());
185 fprintf(stdout, "Parameters:\n"
188 " -ImgDir <directory> \n"
189 " Image file Directory path \n"
190 " -OutFor <PBM|PGM|PPM|PNM|PAM|PGX|PNG|BMP|TIF|RAW|RAWL|TGA>\n"
191 " REQUIRED only if -ImgDir is used\n"
192 " Output format for decompressed images.\n");
193 fprintf(stdout, " -i <compressed file>\n"
194 " REQUIRED only if an Input image directory is not specified\n"
195 " Currently accepts J2K-files, JP2-files and JPT-files. The file type\n"
196 " is identified based on its suffix.\n");
197 fprintf(stdout, " -o <decompressed file>\n"
199 " Currently accepts formats specified above (see OutFor option)\n"
200 " Binary data is written to the file (not ascii). If a PGX\n"
201 " filename is given, there will be as many output files as there are\n"
202 " components: an indice starting from 0 will then be appended to the\n"
203 " output filename, just before the \"pgx\" extension. If a PGM filename\n"
204 " is given and there are more than one component, only the first component\n"
205 " will be written to the file.\n");
206 fprintf(stdout, " -r <reduce factor>\n"
207 " Set the number of highest resolution levels to be discarded. The\n"
208 " image resolution is effectively divided by 2 to the power of the\n"
209 " number of discarded levels. The reduce factor is limited by the\n"
210 " smallest total number of decomposition levels among tiles.\n"
211 " -l <number of quality layers to decode>\n"
212 " Set the maximum number of quality layers to decode. If there are\n"
213 " less quality layers than the specified number, all the quality layers\n"
215 fprintf(stdout, " -x \n"
216 " Create an index file *.Idx (-x index_name.Idx) \n"
217 " -d <x0,y0,x1,y1>\n"
220 " By default all the image is decoded.\n"
221 " -t <tile_number>\n"
223 " Set the tile number of the decoded tile. Follow the JPEG2000 convention from left-up to bottom-up\n"
224 " By default all tiles are decoded.\n");
225 fprintf(stdout, " -p <comp 0 precision>[C|S][,<comp 1 precision>[C|S][,...]]\n"
227 " Force the precision (bit depth) of components.\n");
229 " 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"
230 " If there are less values than components, the last value is used for remaining components.\n"
231 " If 'C' is specified (default), values are clipped.\n"
232 " If 'S' is specified, values are scaled.\n"
233 " A 0 value can be specified (meaning original bit depth).\n");
234 fprintf(stdout, " -c first_comp_index[,second_comp_index][,...]\n"
236 " To limit the number of components to decoded.\n"
237 " Component indices are numbered starting at 0.\n");
238 fprintf(stdout, " -force-rgb\n"
239 " Force output image colorspace to RGB\n"
241 " Downsampled components will be upsampled to image size\n"
243 " Split output components to different files when writing to PNM\n");
244 if (opj_has_thread_support()) {
245 fprintf(stdout, " -threads <num_threads|ALL_CPUS>\n"
246 " Number of threads to use for decoding or ALL_CPUS for all available cores.\n");
248 fprintf(stdout, " -quiet\n"
249 " Disable output from the library and other output.\n");
252 fprintf(stdout, " -W <options>\n"
253 " Activates the JPWL correction capability, if the codestream complies.\n"
254 " Options can be a comma separated list of <param=val> tokens:\n"
256 " numcomps is the number of expected components in the codestream\n"
257 " (search of first EPB rely upon this, default is %d)\n",
258 JPWL_EXPECTED_COMPONENTS);
259 #endif /* USE_JPWL */
261 fprintf(stdout, "\n");
264 /* -------------------------------------------------------------------------- */
266 static OPJ_BOOL parse_precision(const char* option,
267 opj_decompress_parameters* parameters)
269 const char* l_remaining = option;
270 OPJ_BOOL l_result = OPJ_TRUE;
273 if (parameters->precision) {
274 free(parameters->precision);
275 parameters->precision = NULL;
277 parameters->nb_precision = 0U;
285 count = sscanf(l_remaining, "%d%c%c", &prec, &mode, &comma);
290 if ((count == 2) || (mode == ',')) {
298 if ((prec < 1) || (prec > 32)) {
299 fprintf(stderr, "Invalid precision %d in precision option %s\n", prec, option);
300 l_result = OPJ_FALSE;
303 if ((mode != 'C') && (mode != 'S')) {
304 fprintf(stderr, "Invalid precision mode %c in precision option %s\n", mode,
306 l_result = OPJ_FALSE;
310 fprintf(stderr, "Invalid character %c in precision option %s\n", comma, option);
311 l_result = OPJ_FALSE;
315 if (parameters->precision == NULL) {
317 parameters->precision = (opj_precision *)malloc(sizeof(opj_precision));
318 if (parameters->precision == NULL) {
319 fprintf(stderr, "Could not allocate memory for precision option\n");
320 l_result = OPJ_FALSE;
324 OPJ_UINT32 l_new_size = parameters->nb_precision + 1U;
325 opj_precision* l_new;
327 if (l_new_size == 0U) {
328 fprintf(stderr, "Could not allocate memory for precision option\n");
329 l_result = OPJ_FALSE;
333 l_new = (opj_precision *)realloc(parameters->precision,
334 l_new_size * sizeof(opj_precision));
336 fprintf(stderr, "Could not allocate memory for precision option\n");
337 l_result = OPJ_FALSE;
340 parameters->precision = l_new;
343 parameters->precision[parameters->nb_precision].prec = (OPJ_UINT32)prec;
346 parameters->precision[parameters->nb_precision].mode = OPJ_PREC_MODE_CLIP;
349 parameters->precision[parameters->nb_precision].mode = OPJ_PREC_MODE_SCALE;
354 parameters->nb_precision++;
356 l_remaining = strchr(l_remaining, ',');
357 if (l_remaining == NULL) {
362 fprintf(stderr, "Could not parse precision option %s\n", option);
363 l_result = OPJ_FALSE;
371 /* -------------------------------------------------------------------------- */
373 int get_num_images(char *imgdirpath)
376 struct dirent* content;
379 /*Reading the input images from given input directory*/
381 dir = opendir(imgdirpath);
383 fprintf(stderr, "Could not open Folder %s\n", imgdirpath);
387 while ((content = readdir(dir)) != NULL) {
388 if (strcmp(".", content->d_name) == 0 || strcmp("..", content->d_name) == 0) {
397 /* -------------------------------------------------------------------------- */
398 int load_images(dircnt_t *dirptr, char *imgdirpath)
401 struct dirent* content;
404 /*Reading the input images from given input directory*/
406 dir = opendir(imgdirpath);
408 fprintf(stderr, "Could not open Folder %s\n", imgdirpath);
411 fprintf(stderr, "Folder opened successfully\n");
414 while ((content = readdir(dir)) != NULL) {
415 if (strcmp(".", content->d_name) == 0 || strcmp("..", content->d_name) == 0) {
419 strcpy(dirptr->filename[i], content->d_name);
426 /* -------------------------------------------------------------------------- */
427 int get_file_format(const char *filename)
430 static const char *extension[] = {"pgx", "pnm", "pgm", "ppm", "bmp", "tif", "raw", "rawl", "tga", "png", "j2k", "jp2", "jpt", "j2c", "jpc" };
431 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 };
432 const char * ext = strrchr(filename, '.');
438 for (i = 0; i < sizeof(format) / sizeof(*format); i++) {
439 if (strcasecmp(ext, extension[i]) == 0) {
449 const char* path_separator = "\\";
451 const char* path_separator = "/";
454 /* -------------------------------------------------------------------------- */
455 char get_next_file(int imageno, dircnt_t *dirptr, img_fol_t *img_fol,
456 opj_decompress_parameters *parameters)
458 char image_filename[OPJ_PATH_LEN], infilename[OPJ_PATH_LEN],
459 outfilename[OPJ_PATH_LEN], temp_ofname[OPJ_PATH_LEN];
460 char *temp_p, temp1[OPJ_PATH_LEN] = "";
462 strcpy(image_filename, dirptr->filename[imageno]);
463 fprintf(stderr, "File Number %d \"%s\"\n", imageno, image_filename);
464 sprintf(infilename, "%s%s%s", img_fol->imgdirpath, path_separator,
466 parameters->decod_format = infile_format(infilename);
467 if (parameters->decod_format == -1) {
470 if (opj_strcpy_s(parameters->infile, sizeof(parameters->infile),
476 strcpy(temp_ofname, strtok(image_filename, "."));
477 while ((temp_p = strtok(NULL, ".")) != NULL) {
478 strcat(temp_ofname, temp1);
479 sprintf(temp1, ".%s", temp_p);
481 if (img_fol->set_out_format == 1) {
482 sprintf(outfilename, "%s/%s.%s", img_fol->imgdirpath, temp_ofname,
483 img_fol->out_format);
484 if (opj_strcpy_s(parameters->outfile, sizeof(parameters->outfile),
492 /* -------------------------------------------------------------------------- */
493 #define JP2_RFC3745_MAGIC "\x00\x00\x00\x0c\x6a\x50\x20\x20\x0d\x0a\x87\x0a"
494 #define JP2_MAGIC "\x0d\x0a\x87\x0a"
495 /* position 45: "\xff\x52" */
496 #define J2K_CODESTREAM_MAGIC "\xff\x4f\xff\x51"
498 static int infile_format(const char *fname)
501 const char *s, *magic_s;
502 int ext_format, magic_format;
503 unsigned char buf[12];
504 OPJ_SIZE_T l_nb_read;
506 reader = fopen(fname, "rb");
508 if (reader == NULL) {
513 l_nb_read = fread(buf, 1, 12, reader);
515 if (l_nb_read != 12) {
521 ext_format = get_file_format(fname);
523 if (ext_format == JPT_CFMT) {
527 if (memcmp(buf, JP2_RFC3745_MAGIC, 12) == 0 || memcmp(buf, JP2_MAGIC, 4) == 0) {
528 magic_format = JP2_CFMT;
530 } else if (memcmp(buf, J2K_CODESTREAM_MAGIC, 4) == 0) {
531 magic_format = J2K_CFMT;
532 magic_s = ".j2k or .jpc or .j2c";
537 if (magic_format == ext_format) {
541 s = fname + strlen(fname) - 4;
543 fputs("\n===========================================\n", stderr);
544 fprintf(stderr, "The extension of this file is incorrect.\n"
545 "FOUND %s. SHOULD BE %s\n", s, magic_s);
546 fputs("===========================================\n", stderr);
551 /* -------------------------------------------------------------------------- */
553 * Parse the command line
555 /* -------------------------------------------------------------------------- */
556 int parse_cmdline_decoder(int argc, char **argv,
557 opj_decompress_parameters *parameters, img_fol_t *img_fol)
559 /* parse the command line */
561 opj_option_t long_option[] = {
562 {"ImgDir", REQ_ARG, NULL, 'y'},
563 {"OutFor", REQ_ARG, NULL, 'O'},
564 {"force-rgb", NO_ARG, NULL, 1},
565 {"upsample", NO_ARG, NULL, 1},
566 {"split-pnm", NO_ARG, NULL, 1},
567 {"threads", REQ_ARG, NULL, 'T'},
568 {"quiet", NO_ARG, NULL, 1},
571 const char optlist[] = "i:o:r:l:x:d:t:p:c:"
576 #endif /* USE_JPWL */
580 long_option[2].flag = &(parameters->force_rgb);
581 long_option[3].flag = &(parameters->upsample);
582 long_option[4].flag = &(parameters->split_pnm);
583 long_option[6].flag = &(parameters->quiet);
584 totlen = sizeof(long_option);
585 opj_reset_options_reading();
586 img_fol->set_out_format = 0;
588 c = opj_getopt_long(argc, argv, optlist, long_option, totlen);
593 case 0: /* long opt with flag */
595 case 'i': { /* input file */
596 char *infile = opj_optarg;
597 parameters->decod_format = infile_format(infile);
598 switch (parameters->decod_format) {
607 "!! infile cannot be read: %s !!\n\n",
612 "[ERROR] Unknown input file format: %s \n"
613 " Known file formats are *.j2k, *.jp2, *.jpc or *.jpt\n",
617 if (opj_strcpy_s(parameters->infile, sizeof(parameters->infile), infile) != 0) {
618 fprintf(stderr, "[ERROR] Path is too long\n");
624 /* ----------------------------------------------------- */
626 case 'o': { /* output file */
627 char *outfile = opj_optarg;
628 parameters->cod_format = get_file_format(outfile);
629 switch (parameters->cod_format) {
648 "Unknown output format image %s [only *.png, *.pnm, *.pgm, *.ppm, *.pgx, *.bmp, *.tif, *.raw or *.tga]!!\n",
652 if (opj_strcpy_s(parameters->outfile, sizeof(parameters->outfile),
654 fprintf(stderr, "[ERROR] Path is too long\n");
660 /* ----------------------------------------------------- */
662 case 'O': { /* output format */
664 char *of = opj_optarg;
665 sprintf(outformat, ".%s", of);
666 img_fol->set_out_format = 1;
667 parameters->cod_format = get_file_format(outformat);
668 switch (parameters->cod_format) {
670 img_fol->out_format = "pgx";
673 img_fol->out_format = "ppm";
676 img_fol->out_format = "bmp";
679 img_fol->out_format = "tif";
682 img_fol->out_format = "raw";
685 img_fol->out_format = "rawl";
688 img_fol->out_format = "raw";
691 img_fol->out_format = "png";
695 "Unknown output format image %s [only *.png, *.pnm, *.pgm, *.ppm, *.pgx, *.bmp, *.tif, *.raw or *.tga]!!\n",
703 /* ----------------------------------------------------- */
706 case 'r': { /* reduce option */
707 sscanf(opj_optarg, "%u", &(parameters->core.cp_reduce));
711 /* ----------------------------------------------------- */
714 case 'l': { /* layering option */
715 sscanf(opj_optarg, "%u", &(parameters->core.cp_layer));
719 /* ----------------------------------------------------- */
721 case 'h': /* display an help description */
722 decode_help_display();
725 /* ----------------------------------------------------- */
727 case 'y': { /* Image Directory path */
728 img_fol->imgdirpath = (char*)malloc(strlen(opj_optarg) + 1);
729 if (img_fol->imgdirpath == NULL) {
732 strcpy(img_fol->imgdirpath, opj_optarg);
733 img_fol->set_imgdir = 1;
737 /* ----------------------------------------------------- */
739 case 'd': { /* Input decode ROI */
740 size_t size_optarg = (size_t)strlen(opj_optarg) + 1U;
741 char *ROI_values = (char*) malloc(size_optarg);
742 if (ROI_values == NULL) {
743 fprintf(stderr, "[ERROR] Couldn't allocate memory\n");
746 ROI_values[0] = '\0';
747 memcpy(ROI_values, opj_optarg, size_optarg);
748 /*printf("ROI_values = %s [%d / %d]\n", ROI_values, strlen(ROI_values), size_optarg ); */
749 parse_DA_values(ROI_values, ¶meters->DA_x0, ¶meters->DA_y0,
750 ¶meters->DA_x1, ¶meters->DA_y1);
756 /* ----------------------------------------------------- */
758 case 't': { /* Input tile index */
759 sscanf(opj_optarg, "%u", ¶meters->tile_index);
760 parameters->nb_tile_to_decode = 1;
764 /* ----------------------------------------------------- */
766 case 'x': { /* Creation of index file */
767 if (opj_strcpy_s(parameters->indexfilename, sizeof(parameters->indexfilename),
769 fprintf(stderr, "[ERROR] Path is too long\n");
775 /* ----------------------------------------------------- */
776 case 'p': { /* Force precision */
777 if (!parse_precision(opj_optarg, parameters)) {
783 /* ----------------------------------------------------- */
784 case 'c': { /* Componenets */
785 const char* iter = opj_optarg;
787 parameters->numcomps ++;
788 parameters->comps_indices = (OPJ_UINT32*) realloc(
789 parameters->comps_indices,
790 parameters->numcomps * sizeof(OPJ_UINT32));
791 parameters->comps_indices[parameters->numcomps - 1] =
792 (OPJ_UINT32) atoi(iter);
793 iter = strchr(iter, ',');
801 /* ----------------------------------------------------- */
806 case 'W': { /* activate JPWL correction */
809 token = strtok(opj_optarg, ",");
810 while (token != NULL) {
812 /* search expected number of components */
817 compno = JPWL_EXPECTED_COMPONENTS; /* predefined no. of components */
819 if (sscanf(token, "c=%d", &compno) == 1) {
821 if ((compno < 1) || (compno > 256)) {
822 fprintf(stderr, "ERROR -> invalid number of components c = %d\n", compno);
825 parameters->jpwl_exp_comps = compno;
827 } else if (!strcmp(token, "c")) {
829 parameters->jpwl_exp_comps = compno; /* auto for default size */
832 fprintf(stderr, "ERROR -> invalid components specified = %s\n", token);
837 /* search maximum number of tiles */
842 tileno = JPWL_MAXIMUM_TILES; /* maximum no. of tiles */
844 if (sscanf(token, "t=%d", &tileno) == 1) {
846 if ((tileno < 1) || (tileno > JPWL_MAXIMUM_TILES)) {
847 fprintf(stderr, "ERROR -> invalid number of tiles t = %d\n", tileno);
850 parameters->jpwl_max_tiles = tileno;
852 } else if (!strcmp(token, "t")) {
854 parameters->jpwl_max_tiles = tileno; /* auto for default size */
857 fprintf(stderr, "ERROR -> invalid tiles specified = %s\n", token);
862 /* next token or bust */
863 token = strtok(NULL, ",");
865 parameters->jpwl_correct = OPJ_TRUE;
866 if (!(parameter->quiet)) {
867 fprintf(stdout, "JPWL correction capability activated\n");
868 fprintf(stdout, "- expecting %d components\n", parameters->jpwl_exp_comps);
872 #endif /* USE_JPWL */
875 /* ----------------------------------------------------- */
876 case 'T': { /* Number of threads */
877 if (strcmp(opj_optarg, "ALL_CPUS") == 0) {
878 parameters->num_threads = opj_get_num_cpus();
879 if (parameters->num_threads == 1) {
880 parameters->num_threads = 0;
883 sscanf(opj_optarg, "%d", ¶meters->num_threads);
888 /* ----------------------------------------------------- */
891 fprintf(stderr, "[WARNING] An invalid option has been ignored.\n");
896 /* check for possible errors */
897 if (img_fol->set_imgdir == 1) {
898 if (!(parameters->infile[0] == 0)) {
899 fprintf(stderr, "[ERROR] options -ImgDir and -i cannot be used together.\n");
902 if (img_fol->set_out_format == 0) {
904 "[ERROR] When -ImgDir is used, -OutFor <FORMAT> must be used.\n");
905 fprintf(stderr, "Only one format allowed.\n"
906 "Valid format are PGM, PPM, PNM, PGX, BMP, TIF, RAW and TGA.\n");
909 if (!((parameters->outfile[0] == 0))) {
910 fprintf(stderr, "[ERROR] options -ImgDir and -o cannot be used together.\n");
914 if ((parameters->infile[0] == 0) || (parameters->outfile[0] == 0)) {
915 fprintf(stderr, "[ERROR] Required parameters are missing\n"
916 "Example: %s -i image.j2k -o image.pgm\n", argv[0]);
917 fprintf(stderr, " Help: %s -h\n", argv[0]);
925 /* -------------------------------------------------------------------------- */
927 * Parse decoding area input values
930 /* -------------------------------------------------------------------------- */
931 int parse_DA_values(char* inArg, unsigned int *DA_x0, unsigned int *DA_y0,
932 unsigned int *DA_x1, unsigned int *DA_y1)
938 result = strtok(inArg, delims);
940 while ((result != NULL) && (it < 4)) {
941 values[it] = atoi(result);
942 result = strtok(NULL, delims);
949 *DA_x0 = (OPJ_UINT32)values[0];
950 *DA_y0 = (OPJ_UINT32)values[1];
951 *DA_x1 = (OPJ_UINT32)values[2];
952 *DA_y1 = (OPJ_UINT32)values[3];
957 OPJ_FLOAT64 opj_clock(void)
960 /* _WIN32: use QueryPerformance (very accurate) */
961 LARGE_INTEGER freq, t ;
962 /* freq is the clock speed of the CPU */
963 QueryPerformanceFrequency(&freq) ;
964 /* cout << "freq = " << ((double) freq.QuadPart) << endl; */
965 /* t is the high resolution performance counter (see MSDN) */
966 QueryPerformanceCounter(& t) ;
967 return freq.QuadPart ? ((OPJ_FLOAT64)t.QuadPart / (OPJ_FLOAT64)freq.QuadPart) :
969 #elif defined(__linux)
971 clock_gettime(CLOCK_REALTIME, &ts);
972 return ((OPJ_FLOAT64)ts.tv_sec + (OPJ_FLOAT64)ts.tv_nsec * 1e-9);
974 /* Unix : use resource usage */
975 /* FIXME: this counts the total CPU time, instead of the user perceived time */
977 OPJ_FLOAT64 procTime;
978 /* (1) Get the rusage data structure at this moment (man getrusage) */
980 /* (2) What is the elapsed time ? - CPU time = User time + System time */
981 /* (2a) Get the seconds */
982 procTime = (OPJ_FLOAT64)(t.ru_utime.tv_sec + t.ru_stime.tv_sec);
983 /* (2b) More precisely! Get the microseconds part ! */
984 return (procTime + (OPJ_FLOAT64)(t.ru_utime.tv_usec + t.ru_stime.tv_usec) *
989 /* -------------------------------------------------------------------------- */
992 sample error callback expecting a FILE* client object
994 static void error_callback(const char *msg, void *client_data)
997 fprintf(stdout, "[ERROR] %s", msg);
1000 sample warning callback expecting a FILE* client object
1002 static void warning_callback(const char *msg, void *client_data)
1005 fprintf(stdout, "[WARNING] %s", msg);
1008 sample debug callback expecting no client object
1010 static void info_callback(const char *msg, void *client_data)
1013 fprintf(stdout, "[INFO] %s", msg);
1016 sample quiet callback expecting no client object
1018 static void quiet_callback(const char *msg, void *client_data)
1024 static void set_default_parameters(opj_decompress_parameters* parameters)
1027 memset(parameters, 0, sizeof(opj_decompress_parameters));
1029 /* default decoding parameters (command line specific) */
1030 parameters->decod_format = -1;
1031 parameters->cod_format = -1;
1033 /* default decoding parameters (core) */
1034 opj_set_default_decoder_parameters(&(parameters->core));
1038 static void destroy_parameters(opj_decompress_parameters* parameters)
1041 if (parameters->precision) {
1042 free(parameters->precision);
1043 parameters->precision = NULL;
1046 free(parameters->comps_indices);
1047 parameters->comps_indices = NULL;
1051 /* -------------------------------------------------------------------------- */
1053 static opj_image_t* convert_gray_to_rgb(opj_image_t* original)
1056 opj_image_t* l_new_image = NULL;
1057 opj_image_cmptparm_t* l_new_components = NULL;
1059 l_new_components = (opj_image_cmptparm_t*)malloc((original->numcomps + 2U) *
1060 sizeof(opj_image_cmptparm_t));
1061 if (l_new_components == NULL) {
1063 "ERROR -> opj_decompress: failed to allocate memory for RGB image!\n");
1064 opj_image_destroy(original);
1068 l_new_components[0].bpp = l_new_components[1].bpp = l_new_components[2].bpp =
1069 original->comps[0].bpp;
1070 l_new_components[0].dx = l_new_components[1].dx = l_new_components[2].dx =
1071 original->comps[0].dx;
1072 l_new_components[0].dy = l_new_components[1].dy = l_new_components[2].dy =
1073 original->comps[0].dy;
1074 l_new_components[0].h = l_new_components[1].h = l_new_components[2].h =
1075 original->comps[0].h;
1076 l_new_components[0].w = l_new_components[1].w = l_new_components[2].w =
1077 original->comps[0].w;
1078 l_new_components[0].prec = l_new_components[1].prec = l_new_components[2].prec =
1079 original->comps[0].prec;
1080 l_new_components[0].sgnd = l_new_components[1].sgnd = l_new_components[2].sgnd =
1081 original->comps[0].sgnd;
1082 l_new_components[0].x0 = l_new_components[1].x0 = l_new_components[2].x0 =
1083 original->comps[0].x0;
1084 l_new_components[0].y0 = l_new_components[1].y0 = l_new_components[2].y0 =
1085 original->comps[0].y0;
1087 for (compno = 1U; compno < original->numcomps; ++compno) {
1088 l_new_components[compno + 2U].bpp = original->comps[compno].bpp;
1089 l_new_components[compno + 2U].dx = original->comps[compno].dx;
1090 l_new_components[compno + 2U].dy = original->comps[compno].dy;
1091 l_new_components[compno + 2U].h = original->comps[compno].h;
1092 l_new_components[compno + 2U].w = original->comps[compno].w;
1093 l_new_components[compno + 2U].prec = original->comps[compno].prec;
1094 l_new_components[compno + 2U].sgnd = original->comps[compno].sgnd;
1095 l_new_components[compno + 2U].x0 = original->comps[compno].x0;
1096 l_new_components[compno + 2U].y0 = original->comps[compno].y0;
1099 l_new_image = opj_image_create(original->numcomps + 2U, l_new_components,
1101 free(l_new_components);
1102 if (l_new_image == NULL) {
1104 "ERROR -> opj_decompress: failed to allocate memory for RGB image!\n");
1105 opj_image_destroy(original);
1109 l_new_image->x0 = original->x0;
1110 l_new_image->x1 = original->x1;
1111 l_new_image->y0 = original->y0;
1112 l_new_image->y1 = original->y1;
1114 l_new_image->comps[0].factor = l_new_image->comps[1].factor =
1115 l_new_image->comps[2].factor = original->comps[0].factor;
1116 l_new_image->comps[0].alpha = l_new_image->comps[1].alpha =
1117 l_new_image->comps[2].alpha = original->comps[0].alpha;
1118 l_new_image->comps[0].resno_decoded = l_new_image->comps[1].resno_decoded =
1119 l_new_image->comps[2].resno_decoded = original->comps[0].resno_decoded;
1121 memcpy(l_new_image->comps[0].data, original->comps[0].data,
1122 original->comps[0].w * original->comps[0].h * sizeof(OPJ_INT32));
1123 memcpy(l_new_image->comps[1].data, original->comps[0].data,
1124 original->comps[0].w * original->comps[0].h * sizeof(OPJ_INT32));
1125 memcpy(l_new_image->comps[2].data, original->comps[0].data,
1126 original->comps[0].w * original->comps[0].h * sizeof(OPJ_INT32));
1128 for (compno = 1U; compno < original->numcomps; ++compno) {
1129 l_new_image->comps[compno + 2U].factor = original->comps[compno].factor;
1130 l_new_image->comps[compno + 2U].alpha = original->comps[compno].alpha;
1131 l_new_image->comps[compno + 2U].resno_decoded =
1132 original->comps[compno].resno_decoded;
1133 memcpy(l_new_image->comps[compno + 2U].data, original->comps[compno].data,
1134 original->comps[compno].w * original->comps[compno].h * sizeof(OPJ_INT32));
1136 opj_image_destroy(original);
1140 /* -------------------------------------------------------------------------- */
1142 static opj_image_t* upsample_image_components(opj_image_t* original)
1144 opj_image_t* l_new_image = NULL;
1145 opj_image_cmptparm_t* l_new_components = NULL;
1146 OPJ_BOOL l_upsample_need = OPJ_FALSE;
1149 for (compno = 0U; compno < original->numcomps; ++compno) {
1150 if (original->comps[compno].factor > 0U) {
1152 "ERROR -> opj_decompress: -upsample not supported with reduction\n");
1153 opj_image_destroy(original);
1156 if ((original->comps[compno].dx > 1U) || (original->comps[compno].dy > 1U)) {
1157 l_upsample_need = OPJ_TRUE;
1161 if (!l_upsample_need) {
1164 /* Upsample is needed */
1165 l_new_components = (opj_image_cmptparm_t*)malloc(original->numcomps * sizeof(
1166 opj_image_cmptparm_t));
1167 if (l_new_components == NULL) {
1169 "ERROR -> opj_decompress: failed to allocate memory for upsampled components!\n");
1170 opj_image_destroy(original);
1174 for (compno = 0U; compno < original->numcomps; ++compno) {
1175 opj_image_cmptparm_t* l_new_cmp = &(l_new_components[compno]);
1176 opj_image_comp_t* l_org_cmp = &(original->comps[compno]);
1178 l_new_cmp->bpp = l_org_cmp->bpp;
1179 l_new_cmp->prec = l_org_cmp->prec;
1180 l_new_cmp->sgnd = l_org_cmp->sgnd;
1181 l_new_cmp->x0 = original->x0;
1182 l_new_cmp->y0 = original->y0;
1186 l_org_cmp->w; /* should be original->x1 - original->x0 for dx==1 */
1188 l_org_cmp->h; /* should be original->y1 - original->y0 for dy==0 */
1190 if (l_org_cmp->dx > 1U) {
1191 l_new_cmp->w = original->x1 - original->x0;
1194 if (l_org_cmp->dy > 1U) {
1195 l_new_cmp->h = original->y1 - original->y0;
1199 l_new_image = opj_image_create(original->numcomps, l_new_components,
1200 original->color_space);
1201 free(l_new_components);
1202 if (l_new_image == NULL) {
1204 "ERROR -> opj_decompress: failed to allocate memory for upsampled components!\n");
1205 opj_image_destroy(original);
1209 l_new_image->x0 = original->x0;
1210 l_new_image->x1 = original->x1;
1211 l_new_image->y0 = original->y0;
1212 l_new_image->y1 = original->y1;
1214 for (compno = 0U; compno < original->numcomps; ++compno) {
1215 opj_image_comp_t* l_new_cmp = &(l_new_image->comps[compno]);
1216 opj_image_comp_t* l_org_cmp = &(original->comps[compno]);
1218 l_new_cmp->factor = l_org_cmp->factor;
1219 l_new_cmp->alpha = l_org_cmp->alpha;
1220 l_new_cmp->resno_decoded = l_org_cmp->resno_decoded;
1222 if ((l_org_cmp->dx > 1U) || (l_org_cmp->dy > 1U)) {
1223 const OPJ_INT32* l_src = l_org_cmp->data;
1224 OPJ_INT32* l_dst = l_new_cmp->data;
1226 OPJ_UINT32 xoff, yoff;
1228 /* need to take into account dx & dy */
1229 xoff = l_org_cmp->dx * l_org_cmp->x0 - original->x0;
1230 yoff = l_org_cmp->dy * l_org_cmp->y0 - original->y0;
1231 if ((xoff >= l_org_cmp->dx) || (yoff >= l_org_cmp->dy)) {
1233 "ERROR -> opj_decompress: Invalid image/component parameters found when upsampling\n");
1234 opj_image_destroy(original);
1235 opj_image_destroy(l_new_image);
1239 for (y = 0U; y < yoff; ++y) {
1240 memset(l_dst, 0U, l_new_cmp->w * sizeof(OPJ_INT32));
1241 l_dst += l_new_cmp->w;
1244 if (l_new_cmp->h > (l_org_cmp->dy -
1245 1U)) { /* check subtraction overflow for really small images */
1246 for (; y < l_new_cmp->h - (l_org_cmp->dy - 1U); y += l_org_cmp->dy) {
1251 for (x = 0U; x < xoff; ++x) {
1254 if (l_new_cmp->w > (l_org_cmp->dx -
1255 1U)) { /* check subtraction overflow for really small images */
1256 for (; x < l_new_cmp->w - (l_org_cmp->dx - 1U); x += l_org_cmp->dx, ++xorg) {
1258 for (dx = 0U; dx < l_org_cmp->dx; ++dx) {
1259 l_dst[x + dx] = l_src[xorg];
1263 for (; x < l_new_cmp->w; ++x) {
1264 l_dst[x] = l_src[xorg];
1266 l_dst += l_new_cmp->w;
1268 for (dy = 1U; dy < l_org_cmp->dy; ++dy) {
1269 memcpy(l_dst, l_dst - l_new_cmp->w, l_new_cmp->w * sizeof(OPJ_INT32));
1270 l_dst += l_new_cmp->w;
1272 l_src += l_org_cmp->w;
1275 if (y < l_new_cmp->h) {
1280 for (x = 0U; x < xoff; ++x) {
1283 if (l_new_cmp->w > (l_org_cmp->dx -
1284 1U)) { /* check subtraction overflow for really small images */
1285 for (; x < l_new_cmp->w - (l_org_cmp->dx - 1U); x += l_org_cmp->dx, ++xorg) {
1287 for (dx = 0U; dx < l_org_cmp->dx; ++dx) {
1288 l_dst[x + dx] = l_src[xorg];
1292 for (; x < l_new_cmp->w; ++x) {
1293 l_dst[x] = l_src[xorg];
1295 l_dst += l_new_cmp->w;
1297 for (; y < l_new_cmp->h; ++y) {
1298 memcpy(l_dst, l_dst - l_new_cmp->w, l_new_cmp->w * sizeof(OPJ_INT32));
1299 l_dst += l_new_cmp->w;
1303 memcpy(l_new_cmp->data, l_org_cmp->data,
1304 l_org_cmp->w * l_org_cmp->h * sizeof(OPJ_INT32));
1307 opj_image_destroy(original);
1311 /* -------------------------------------------------------------------------- */
1313 * OPJ_DECOMPRESS MAIN
1315 /* -------------------------------------------------------------------------- */
1316 int main(int argc, char **argv)
1318 opj_decompress_parameters parameters; /* decompression parameters */
1319 opj_image_t* image = NULL;
1320 opj_stream_t *l_stream = NULL; /* Stream */
1321 opj_codec_t* l_codec = NULL; /* Handle to a decompressor */
1322 opj_codestream_index_t* cstr_index = NULL;
1324 OPJ_INT32 num_images, imageno;
1326 dircnt_t *dirptr = NULL;
1328 OPJ_FLOAT64 t, tCumulative = 0;
1329 OPJ_UINT32 numDecompressedImages = 0;
1330 OPJ_UINT32 cp_reduce;
1332 /* set decoding parameters to default values */
1333 set_default_parameters(¶meters);
1335 /* Initialize img_fol */
1336 memset(&img_fol, 0, sizeof(img_fol_t));
1338 /* parse input and get user encoding parameters */
1339 if (parse_cmdline_decoder(argc, argv, ¶meters, &img_fol) == 1) {
1344 cp_reduce = parameters.core.cp_reduce;
1345 if (getenv("USE_OPJ_SET_DECODED_RESOLUTION_FACTOR") != NULL) {
1346 /* For debugging/testing purposes, do not set the cp_reduce member */
1347 /* if USE_OPJ_SET_DECODED_RESOLUTION_FACTOR is defined, but used */
1348 /* the opj_set_decoded_resolution_factor() API instead */
1349 parameters.core.cp_reduce = 0;
1353 /* Initialize reading of directory */
1354 if (img_fol.set_imgdir == 1) {
1356 num_images = get_num_images(img_fol.imgdirpath);
1358 dirptr = (dircnt_t*)malloc(sizeof(dircnt_t));
1360 destroy_parameters(¶meters);
1361 return EXIT_FAILURE;
1363 dirptr->filename_buf = (char*)malloc((size_t)num_images * OPJ_PATH_LEN * sizeof(
1364 char)); /* Stores at max 10 image file names*/
1365 if (!dirptr->filename_buf) {
1370 dirptr->filename = (char**) malloc((size_t)num_images * sizeof(char*));
1372 if (!dirptr->filename) {
1376 for (it_image = 0; it_image < num_images; it_image++) {
1377 dirptr->filename[it_image] = dirptr->filename_buf + it_image * OPJ_PATH_LEN;
1380 if (load_images(dirptr, img_fol.imgdirpath) == 1) {
1384 if (num_images == 0) {
1385 fprintf(stderr, "Folder is empty\n");
1393 /*Decoding image one by one*/
1394 for (imageno = 0; imageno < num_images ; imageno++) {
1396 if (!parameters.quiet) {
1397 fprintf(stderr, "\n");
1400 if (img_fol.set_imgdir == 1) {
1401 if (get_next_file(imageno, dirptr, &img_fol, ¶meters)) {
1402 fprintf(stderr, "skipping file...\n");
1403 destroy_parameters(¶meters);
1408 /* read the input file and put it in memory */
1409 /* ---------------------------------------- */
1411 l_stream = opj_stream_create_default_file_stream(parameters.infile, 1);
1413 fprintf(stderr, "ERROR -> failed to create the stream from the file %s\n",
1419 /* decode the JPEG2000 stream */
1420 /* ---------------------- */
1422 switch (parameters.decod_format) {
1423 case J2K_CFMT: { /* JPEG-2000 codestream */
1424 /* Get a decoder handle */
1425 l_codec = opj_create_decompress(OPJ_CODEC_J2K);
1428 case JP2_CFMT: { /* JPEG 2000 compressed image data */
1429 /* Get a decoder handle */
1430 l_codec = opj_create_decompress(OPJ_CODEC_JP2);
1433 case JPT_CFMT: { /* JPEG 2000, JPIP */
1434 /* Get a decoder handle */
1435 l_codec = opj_create_decompress(OPJ_CODEC_JPT);
1439 fprintf(stderr, "skipping file..\n");
1440 destroy_parameters(¶meters);
1441 opj_stream_destroy(l_stream);
1445 if (parameters.quiet) {
1446 /* Set all callbacks to quiet */
1447 opj_set_info_handler(l_codec, quiet_callback, 00);
1448 opj_set_warning_handler(l_codec, quiet_callback, 00);
1449 opj_set_error_handler(l_codec, quiet_callback, 00);
1451 /* catch events using our callbacks and give a local context */
1452 opj_set_info_handler(l_codec, info_callback, 00);
1453 opj_set_warning_handler(l_codec, warning_callback, 00);
1454 opj_set_error_handler(l_codec, error_callback, 00);
1460 /* Setup the decoder decoding parameters using user parameters */
1461 if (!opj_setup_decoder(l_codec, &(parameters.core))) {
1462 fprintf(stderr, "ERROR -> opj_decompress: failed to setup the decoder\n");
1463 opj_stream_destroy(l_stream);
1464 opj_destroy_codec(l_codec);
1469 if (parameters.num_threads >= 1 &&
1470 !opj_codec_set_threads(l_codec, parameters.num_threads)) {
1471 fprintf(stderr, "ERROR -> opj_decompress: failed to set number of threads\n");
1472 opj_stream_destroy(l_stream);
1473 opj_destroy_codec(l_codec);
1478 /* Read the main header of the codestream and if necessary the JP2 boxes*/
1479 if (! opj_read_header(l_stream, l_codec, &image)) {
1480 fprintf(stderr, "ERROR -> opj_decompress: failed to read the header\n");
1481 opj_stream_destroy(l_stream);
1482 opj_destroy_codec(l_codec);
1483 opj_image_destroy(image);
1488 if (parameters.numcomps) {
1489 if (! opj_set_decoded_components(l_codec,
1490 parameters.numcomps,
1491 parameters.comps_indices,
1494 "ERROR -> opj_decompress: failed to set the component indices!\n");
1495 opj_destroy_codec(l_codec);
1496 opj_stream_destroy(l_stream);
1497 opj_image_destroy(image);
1503 if (getenv("USE_OPJ_SET_DECODED_RESOLUTION_FACTOR") != NULL) {
1504 /* For debugging/testing purposes, and also an illustration on how to */
1505 /* use the alternative API opj_set_decoded_resolution_factor() instead */
1506 /* of setting parameters.cp_reduce */
1507 if (! opj_set_decoded_resolution_factor(l_codec, cp_reduce)) {
1509 "ERROR -> opj_decompress: failed to set the resolution factor tile!\n");
1510 opj_destroy_codec(l_codec);
1511 opj_stream_destroy(l_stream);
1512 opj_image_destroy(image);
1518 if (!parameters.nb_tile_to_decode) {
1519 if (getenv("SKIP_OPJ_SET_DECODE_AREA") != NULL &&
1520 parameters.DA_x0 == 0 &&
1521 parameters.DA_y0 == 0 &&
1522 parameters.DA_x1 == 0 &&
1523 parameters.DA_y1 == 0) {
1524 /* For debugging/testing purposes, */
1525 /* do nothing if SKIP_OPJ_SET_DECODE_AREA env variable */
1526 /* is defined and no decoded area has been set */
1528 /* Optional if you want decode the entire image */
1529 else if (!opj_set_decode_area(l_codec, image, (OPJ_INT32)parameters.DA_x0,
1530 (OPJ_INT32)parameters.DA_y0, (OPJ_INT32)parameters.DA_x1,
1531 (OPJ_INT32)parameters.DA_y1)) {
1532 fprintf(stderr, "ERROR -> opj_decompress: failed to set the decoded area\n");
1533 opj_stream_destroy(l_stream);
1534 opj_destroy_codec(l_codec);
1535 opj_image_destroy(image);
1540 /* Get the decoded image */
1541 if (!(opj_decode(l_codec, l_stream, image) &&
1542 opj_end_decompress(l_codec, l_stream))) {
1543 fprintf(stderr, "ERROR -> opj_decompress: failed to decode image!\n");
1544 opj_destroy_codec(l_codec);
1545 opj_stream_destroy(l_stream);
1546 opj_image_destroy(image);
1551 if (!(parameters.DA_x0 == 0 &&
1552 parameters.DA_y0 == 0 &&
1553 parameters.DA_x1 == 0 &&
1554 parameters.DA_y1 == 0)) {
1555 if (!(parameters.quiet)) {
1556 fprintf(stderr, "WARNING: -d option ignored when used together with -t\n");
1560 if (!opj_get_decoded_tile(l_codec, l_stream, image, parameters.tile_index)) {
1561 fprintf(stderr, "ERROR -> opj_decompress: failed to decode tile!\n");
1562 opj_destroy_codec(l_codec);
1563 opj_stream_destroy(l_stream);
1564 opj_image_destroy(image);
1568 if (!(parameters.quiet)) {
1569 fprintf(stdout, "tile %d is decoded!\n\n", parameters.tile_index);
1573 /* FIXME? Shouldn't that situation be considered as an error of */
1574 /* opj_decode() / opj_get_decoded_tile() ? */
1575 if (image->comps[0].data == NULL) {
1576 fprintf(stderr, "ERROR -> opj_decompress: no image data!\n");
1577 opj_destroy_codec(l_codec);
1578 opj_stream_destroy(l_stream);
1579 opj_image_destroy(image);
1584 tCumulative += opj_clock() - t;
1585 numDecompressedImages++;
1587 /* Close the byte stream */
1588 opj_stream_destroy(l_stream);
1590 if (image->color_space != OPJ_CLRSPC_SYCC
1591 && image->numcomps == 3 && image->comps[0].dx == image->comps[0].dy
1592 && image->comps[1].dx != 1) {
1593 image->color_space = OPJ_CLRSPC_SYCC;
1594 } else if (image->numcomps <= 2) {
1595 image->color_space = OPJ_CLRSPC_GRAY;
1598 if (image->color_space == OPJ_CLRSPC_SYCC) {
1599 color_sycc_to_rgb(image);
1600 } else if ((image->color_space == OPJ_CLRSPC_CMYK) &&
1601 (parameters.cod_format != TIF_DFMT)) {
1602 color_cmyk_to_rgb(image);
1603 } else if (image->color_space == OPJ_CLRSPC_EYCC) {
1604 color_esycc_to_rgb(image);
1607 if (image->icc_profile_buf) {
1608 #if defined(OPJ_HAVE_LIBLCMS1) || defined(OPJ_HAVE_LIBLCMS2)
1609 if (image->icc_profile_len) {
1610 color_apply_icc_profile(image);
1612 color_cielab_to_rgb(image);
1615 free(image->icc_profile_buf);
1616 image->icc_profile_buf = NULL;
1617 image->icc_profile_len = 0;
1620 /* Force output precision */
1621 /* ---------------------- */
1622 if (parameters.precision != NULL) {
1624 for (compno = 0; compno < image->numcomps; ++compno) {
1625 OPJ_UINT32 precno = compno;
1628 if (precno >= parameters.nb_precision) {
1629 precno = parameters.nb_precision - 1U;
1632 prec = parameters.precision[precno].prec;
1634 prec = image->comps[compno].prec;
1637 switch (parameters.precision[precno].mode) {
1638 case OPJ_PREC_MODE_CLIP:
1639 clip_component(&(image->comps[compno]), prec);
1641 case OPJ_PREC_MODE_SCALE:
1642 scale_component(&(image->comps[compno]), prec);
1651 /* Upsample components */
1652 /* ------------------- */
1653 if (parameters.upsample) {
1654 image = upsample_image_components(image);
1655 if (image == NULL) {
1657 "ERROR -> opj_decompress: failed to upsample image components!\n");
1658 opj_destroy_codec(l_codec);
1664 /* Force RGB output */
1665 /* ---------------- */
1666 if (parameters.force_rgb) {
1667 switch (image->color_space) {
1668 case OPJ_CLRSPC_SRGB:
1670 case OPJ_CLRSPC_GRAY:
1671 image = convert_gray_to_rgb(image);
1675 "ERROR -> opj_decompress: don't know how to convert image to RGB colorspace!\n");
1676 opj_image_destroy(image);
1680 if (image == NULL) {
1681 fprintf(stderr, "ERROR -> opj_decompress: failed to convert to RGB image!\n");
1682 opj_destroy_codec(l_codec);
1688 /* create output image */
1689 /* ------------------- */
1690 switch (parameters.cod_format) {
1691 case PXM_DFMT: /* PNM PGM PPM */
1692 if (imagetopnm(image, parameters.outfile, parameters.split_pnm)) {
1693 fprintf(stderr, "[ERROR] Outfile %s not generated\n", parameters.outfile);
1695 } else if (!(parameters.quiet)) {
1696 fprintf(stdout, "[INFO] Generated Outfile %s\n", parameters.outfile);
1700 case PGX_DFMT: /* PGX */
1701 if (imagetopgx(image, parameters.outfile)) {
1702 fprintf(stderr, "[ERROR] Outfile %s not generated\n", parameters.outfile);
1704 } else if (!(parameters.quiet)) {
1705 fprintf(stdout, "[INFO] Generated Outfile %s\n", parameters.outfile);
1709 case BMP_DFMT: /* BMP */
1710 if (imagetobmp(image, parameters.outfile)) {
1711 fprintf(stderr, "[ERROR] Outfile %s not generated\n", parameters.outfile);
1713 } else if (!(parameters.quiet)) {
1714 fprintf(stdout, "[INFO] Generated Outfile %s\n", parameters.outfile);
1717 #ifdef OPJ_HAVE_LIBTIFF
1718 case TIF_DFMT: /* TIFF */
1719 if (imagetotif(image, parameters.outfile)) {
1720 fprintf(stderr, "[ERROR] Outfile %s not generated\n", parameters.outfile);
1722 } else if (!(parameters.quiet)) {
1723 fprintf(stdout, "[INFO] Generated Outfile %s\n", parameters.outfile);
1726 #endif /* OPJ_HAVE_LIBTIFF */
1727 case RAW_DFMT: /* RAW */
1728 if (imagetoraw(image, parameters.outfile)) {
1729 fprintf(stderr, "[ERROR] Error generating raw file. Outfile %s not generated\n",
1730 parameters.outfile);
1732 } else if (!(parameters.quiet)) {
1733 fprintf(stdout, "[INFO] Generated Outfile %s\n", parameters.outfile);
1737 case RAWL_DFMT: /* RAWL */
1738 if (imagetorawl(image, parameters.outfile)) {
1740 "[ERROR] Error generating rawl file. Outfile %s not generated\n",
1741 parameters.outfile);
1743 } else if (!(parameters.quiet)) {
1744 fprintf(stdout, "[INFO] Generated Outfile %s\n", parameters.outfile);
1748 case TGA_DFMT: /* TGA */
1749 if (imagetotga(image, parameters.outfile)) {
1750 fprintf(stderr, "[ERROR] Error generating tga file. Outfile %s not generated\n",
1751 parameters.outfile);
1753 } else if (!(parameters.quiet)) {
1754 fprintf(stdout, "[INFO] Generated Outfile %s\n", parameters.outfile);
1757 #ifdef OPJ_HAVE_LIBPNG
1758 case PNG_DFMT: /* PNG */
1759 if (imagetopng(image, parameters.outfile)) {
1760 fprintf(stderr, "[ERROR] Error generating png file. Outfile %s not generated\n",
1761 parameters.outfile);
1763 } else if (!(parameters.quiet)) {
1764 fprintf(stdout, "[INFO] Generated Outfile %s\n", parameters.outfile);
1767 #endif /* OPJ_HAVE_LIBPNG */
1768 /* Can happen if output file is TIFF or PNG
1769 * and OPJ_HAVE_LIBTIF or OPJ_HAVE_LIBPNG is undefined
1772 fprintf(stderr, "[ERROR] Outfile %s not generated\n", parameters.outfile);
1776 /* free remaining structures */
1778 opj_destroy_codec(l_codec);
1782 /* free image data structure */
1783 opj_image_destroy(image);
1785 /* destroy the codestream index */
1786 opj_destroy_cstr_index(&cstr_index);
1789 (void)remove(parameters.outfile); /* ignore return value */
1793 destroy_parameters(¶meters);
1794 if (failed && img_fol.imgdirpath) {
1795 free(img_fol.imgdirpath);
1798 if (dirptr->filename) {
1799 free(dirptr->filename);
1801 if (dirptr->filename_buf) {
1802 free(dirptr->filename_buf);
1806 if (numDecompressedImages && !failed && !(parameters.quiet)) {
1807 fprintf(stdout, "decode time: %d ms\n",
1808 (int)((tCumulative * 1000.0) / (OPJ_FLOAT64)numDecompressedImages));
1810 return failed ? EXIT_FAILURE : EXIT_SUCCESS;