2 * The copyright in this software is being made available under the 2-clauses
3 * BSD License, included below. This software may be subject to other third
4 * party and contributor rights, including patent rights, and no such rights
5 * are granted under this license.
7 * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium
8 * Copyright (c) 2002-2014, Professor Benoit Macq
9 * Copyright (c) 2001-2003, David Janssens
10 * Copyright (c) 2002-2003, Yannick Verschueren
11 * Copyright (c) 2003-2007, Francois-Olivier Devaux
12 * Copyright (c) 2003-2014, Antonin Descampe
13 * Copyright (c) 2005, Herve Drolon, FreeImage Team
14 * Copyright (c) 2006-2007, Parvatha Elangovan
15 * Copyright (c) 2008, 2011-2012, Centre National d'Etudes Spatiales (CNES), FR
16 * Copyright (c) 2012, CS Systemes d'Information, France
17 * All rights reserved.
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following conditions
22 * 1. Redistributions of source code must retain the above copyright
23 * notice, this list of conditions and the following disclaimer.
24 * 2. Redistributions in binary form must reproduce the above copyright
25 * notice, this list of conditions and the following disclaimer in the
26 * documentation and/or other materials provided with the distribution.
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
29 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
32 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38 * POSSIBILITY OF SUCH DAMAGE.
40 #include "opj_apps_config.h"
48 #include "windirent.h"
55 #define strcasecmp _stricmp
56 #define strncasecmp _strnicmp
62 #include "opj_getopt.h"
66 #ifdef OPJ_HAVE_LIBLCMS2
69 #ifdef OPJ_HAVE_LIBLCMS1
74 #include "format_defs.h"
76 typedef struct dircnt{
77 /** Buffer for holding images read from Directory*/
79 /** Pointer to the buffer*/
84 typedef struct img_folder{
85 /** The directory path of the folder containing input images*/
88 const char *out_format;
91 /** Enable Cod Format for output*/
96 typedef enum opj_prec_mode
100 } opj_precision_mode;
102 typedef struct opj_prec
105 opj_precision_mode mode;
108 typedef struct opj_decompress_params
110 /** core library parameters */
111 opj_dparameters_t core;
113 /** input file name */
114 char infile[OPJ_PATH_LEN];
115 /** output file name */
116 char outfile[OPJ_PATH_LEN];
117 /** input file format 0: J2K, 1: JP2, 2: JPT */
119 /** output file format 0: PGX, 1: PxM, 2: BMP */
122 /** Decoding area left boundary */
124 /** Decoding area right boundary */
126 /** Decoding area up boundary */
128 /** Decoding area bottom boundary */
133 /** tile number ot the decoded tile*/
134 OPJ_UINT32 tile_index;
135 /** Nb of tile to decode */
136 OPJ_UINT32 nb_tile_to_decode;
138 opj_precision* precision;
139 OPJ_UINT32 nb_precision;
141 /* force output colorspace to RGB */
143 /* upsample components according to their dx/dy values */
145 }opj_decompress_parameters;
147 /* -------------------------------------------------------------------------- */
149 int get_num_images(char *imgdirpath);
150 int load_images(dircnt_t *dirptr, char *imgdirpath);
151 int get_file_format(const char *filename);
152 char get_next_file(int imageno,dircnt_t *dirptr,img_fol_t *img_fol, opj_decompress_parameters *parameters);
153 static int infile_format(const char *fname);
155 int parse_cmdline_decoder(int argc, char **argv, opj_decompress_parameters *parameters,img_fol_t *img_fol, char *indexfilename);
156 int parse_DA_values( char* inArg, unsigned int *DA_x0, unsigned int *DA_y0, unsigned int *DA_x1, unsigned int *DA_y1);
158 static opj_image_t* convert_gray_to_rgb(opj_image_t* original);
160 /* -------------------------------------------------------------------------- */
161 static void decode_help_display(void) {
162 fprintf(stdout,"\nThis is the opj_decompress utility from the OpenJPEG project.\n"
163 "It decompresses JPEG 2000 codestreams to various image formats.\n"
164 "It has been compiled against openjp2 library v%s.\n\n",opj_version());
166 fprintf(stdout,"Parameters:\n"
169 " -ImgDir <directory> \n"
170 " Image file Directory path \n"
171 " -OutFor <PBM|PGM|PPM|PNM|PAM|PGX|PNG|BMP|TIF|RAW|RAWL|TGA>\n"
172 " REQUIRED only if -ImgDir is used\n"
173 " Output format for decompressed images.\n"
174 " -i <compressed file>\n"
175 " REQUIRED only if an Input image directory is not specified\n"
176 " Currently accepts J2K-files, JP2-files and JPT-files. The file type\n"
177 " is identified based on its suffix.\n"
178 " -o <decompressed file>\n"
180 " Currently accepts formats specified above (see OutFor option)\n"
181 " Binary data is written to the file (not ascii). If a PGX\n"
182 " filename is given, there will be as many output files as there are\n"
183 " components: an indice starting from 0 will then be appended to the\n"
184 " output filename, just before the \"pgx\" extension. If a PGM filename\n"
185 " is given and there are more than one component, only the first component\n"
186 " will be written to the file.\n"
187 " -r <reduce factor>\n"
188 " Set the number of highest resolution levels to be discarded. The\n"
189 " image resolution is effectively divided by 2 to the power of the\n"
190 " number of discarded levels. The reduce factor is limited by the\n"
191 " smallest total number of decomposition levels among tiles.\n"
192 " -l <number of quality layers to decode>\n"
193 " Set the maximum number of quality layers to decode. If there are\n"
194 " less quality layers than the specified number, all the quality layers\n"
197 " Create an index file *.Idx (-x index_name.Idx) \n"
198 " -d <x0,y0,x1,y1>\n"
201 " By default all the image is decoded.\n"
202 " -t <tile_number>\n"
204 " Set the tile number of the decoded tile. Follow the JPEG2000 convention from left-up to bottom-up\n"
205 " By default all tiles are decoded.\n"
206 " -p <comp 0 precision>[C|S][,<comp 1 precision>[C|S][,...]]\n"
208 " Force the precision (bit depth) of components.\n"
209 " 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"
210 " If there are less values than components, the last value is used for remaining components.\n"
211 " If 'C' is specified (default), values are clipped.\n"
212 " If 'S' is specified, values are scaled.\n"
213 " A 0 value can be specified (meaning original bit depth).\n"
215 " Force output image colorspace to RGB\n"
217 " Downsampled components will be upsampled to image size\n"
221 fprintf(stdout," -W <options>\n"
222 " Activates the JPWL correction capability, if the codestream complies.\n"
223 " Options can be a comma separated list of <param=val> tokens:\n"
225 " numcomps is the number of expected components in the codestream\n"
226 " (search of first EPB rely upon this, default is %d)\n", JPWL_EXPECTED_COMPONENTS);
227 #endif /* USE_JPWL */
229 fprintf(stdout,"\n");
232 /* -------------------------------------------------------------------------- */
234 static OPJ_BOOL parse_precision(const char* option, opj_decompress_parameters* parameters)
236 const char* l_remaining = option;
237 OPJ_BOOL l_result = OPJ_TRUE;
240 if (parameters->precision) {
241 free(parameters->precision);
242 parameters->precision = NULL;
244 parameters->nb_precision = 0U;
253 count = sscanf(l_remaining, "%d%c%c", &prec, &mode, &comma);
258 if ((count == 2) || (mode==',')) {
267 fprintf(stderr,"Invalid precision %d in precision option %s\n", prec, option);
268 l_result = OPJ_FALSE;
271 if ((mode != 'C') && (mode != 'S')) {
272 fprintf(stderr,"Invalid precision mode %c in precision option %s\n", mode, option);
273 l_result = OPJ_FALSE;
277 fprintf(stderr,"Invalid character %c in precision option %s\n", comma, option);
278 l_result = OPJ_FALSE;
282 if (parameters->precision == NULL) {
284 parameters->precision = malloc(sizeof(opj_precision));
285 if (parameters->precision == NULL) {
286 fprintf(stderr,"Could not allocate memory for precision option\n");
287 l_result = OPJ_FALSE;
291 OPJ_UINT32 l_new_size = parameters->nb_precision + 1U;
292 opj_precision* l_new;
294 if (l_new_size == 0U) {
295 fprintf(stderr,"Could not allocate memory for precision option\n");
296 l_result = OPJ_FALSE;
300 l_new = realloc(parameters->precision, l_new_size * sizeof(opj_precision));
302 fprintf(stderr,"Could not allocate memory for precision option\n");
303 l_result = OPJ_FALSE;
306 parameters->precision = l_new;
309 parameters->precision[parameters->nb_precision].prec = prec;
312 parameters->precision[parameters->nb_precision].mode = OPJ_PREC_MODE_CLIP;
315 parameters->precision[parameters->nb_precision].mode = OPJ_PREC_MODE_SCALE;
320 parameters->nb_precision++;
322 l_remaining = strchr(l_remaining, ',');
323 if (l_remaining == NULL) {
328 fprintf(stderr,"Could not parse precision option %s\n", option);
329 l_result = OPJ_FALSE;
337 /* -------------------------------------------------------------------------- */
339 int get_num_images(char *imgdirpath){
341 struct dirent* content;
344 /*Reading the input images from given input directory*/
346 dir= opendir(imgdirpath);
348 fprintf(stderr,"Could not open Folder %s\n",imgdirpath);
352 while((content=readdir(dir))!=NULL){
353 if(strcmp(".",content->d_name)==0 || strcmp("..",content->d_name)==0 )
360 /* -------------------------------------------------------------------------- */
361 int load_images(dircnt_t *dirptr, char *imgdirpath){
363 struct dirent* content;
366 /*Reading the input images from given input directory*/
368 dir= opendir(imgdirpath);
370 fprintf(stderr,"Could not open Folder %s\n",imgdirpath);
373 fprintf(stderr,"Folder opened successfully\n");
376 while((content=readdir(dir))!=NULL){
377 if(strcmp(".",content->d_name)==0 || strcmp("..",content->d_name)==0 )
380 strcpy(dirptr->filename[i],content->d_name);
386 /* -------------------------------------------------------------------------- */
387 int get_file_format(const char *filename) {
389 static const char *extension[] = {"pgx", "pnm", "pgm", "ppm", "bmp","tif", "raw", "rawl", "tga", "png", "j2k", "jp2", "jpt", "j2c", "jpc" };
390 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 };
391 char * ext = strrchr(filename, '.');
396 for(i = 0; i < sizeof(format)/sizeof(*format); i++) {
397 if(strcasecmp(ext, extension[i]) == 0) {
406 /* -------------------------------------------------------------------------- */
407 char get_next_file(int imageno,dircnt_t *dirptr,img_fol_t *img_fol, opj_decompress_parameters *parameters){
408 char image_filename[OPJ_PATH_LEN], infilename[OPJ_PATH_LEN],outfilename[OPJ_PATH_LEN],temp_ofname[OPJ_PATH_LEN];
409 char *temp_p, temp1[OPJ_PATH_LEN]="";
411 strcpy(image_filename,dirptr->filename[imageno]);
412 fprintf(stderr,"File Number %d \"%s\"\n",imageno,image_filename);
413 parameters->decod_format = infile_format(image_filename);
414 if (parameters->decod_format == -1)
416 sprintf(infilename,"%s/%s",img_fol->imgdirpath,image_filename);
417 strncpy(parameters->infile, infilename, sizeof(infilename));
420 strcpy(temp_ofname,strtok(image_filename,"."));
421 while((temp_p = strtok(NULL,".")) != NULL){
422 strcat(temp_ofname,temp1);
423 sprintf(temp1,".%s",temp_p);
425 if(img_fol->set_out_format==1){
426 sprintf(outfilename,"%s/%s.%s",img_fol->imgdirpath,temp_ofname,img_fol->out_format);
427 strncpy(parameters->outfile, outfilename, sizeof(outfilename));
432 /* -------------------------------------------------------------------------- */
433 #define JP2_RFC3745_MAGIC "\x00\x00\x00\x0c\x6a\x50\x20\x20\x0d\x0a\x87\x0a"
434 #define JP2_MAGIC "\x0d\x0a\x87\x0a"
435 /* position 45: "\xff\x52" */
436 #define J2K_CODESTREAM_MAGIC "\xff\x4f\xff\x51"
438 static int infile_format(const char *fname)
441 const char *s, *magic_s;
442 int ext_format, magic_format;
443 unsigned char buf[12];
444 OPJ_SIZE_T l_nb_read;
446 reader = fopen(fname, "rb");
452 l_nb_read = fread(buf, 1, 12, reader);
459 ext_format = get_file_format(fname);
461 if (ext_format == JPT_CFMT)
464 if (memcmp(buf, JP2_RFC3745_MAGIC, 12) == 0 || memcmp(buf, JP2_MAGIC, 4) == 0) {
465 magic_format = JP2_CFMT;
468 else if (memcmp(buf, J2K_CODESTREAM_MAGIC, 4) == 0) {
469 magic_format = J2K_CFMT;
470 magic_s = ".j2k or .jpc or .j2c";
475 if (magic_format == ext_format)
478 s = fname + strlen(fname) - 4;
480 fputs("\n===========================================\n", stderr);
481 fprintf(stderr, "The extension of this file is incorrect.\n"
482 "FOUND %s. SHOULD BE %s\n", s, magic_s);
483 fputs("===========================================\n", stderr);
488 /* -------------------------------------------------------------------------- */
490 * Parse the command line
492 /* -------------------------------------------------------------------------- */
493 int parse_cmdline_decoder(int argc, char **argv, opj_decompress_parameters *parameters,img_fol_t *img_fol, char *indexfilename) {
494 /* parse the command line */
496 opj_option_t long_option[]={
497 {"ImgDir", REQ_ARG, NULL ,'y'},
498 {"OutFor", REQ_ARG, NULL ,'O'},
499 {"force-rgb", NO_ARG, &(parameters->force_rgb), 1},
500 {"upsample", NO_ARG, &(parameters->upsample), 1}
503 const char optlist[] = "i:o:r:l:x:d:t:p:"
508 #endif /* USE_JPWL */
511 totlen=sizeof(long_option);
512 opj_reset_options_reading();
513 img_fol->set_out_format = 0;
515 c = opj_getopt_long(argc, argv,optlist,long_option,totlen);
519 case 0: /* long opt with flag */
521 case 'i': /* input file */
523 char *infile = opj_optarg;
524 parameters->decod_format = infile_format(infile);
525 switch(parameters->decod_format) {
534 "!! infile cannot be read: %s !!\n\n",
539 "[ERROR] Unknown input file format: %s \n"
540 " Known file formats are *.j2k, *.jp2, *.jpc or *.jpt\n",
544 strncpy(parameters->infile, infile, sizeof(parameters->infile)-1);
548 /* ----------------------------------------------------- */
550 case 'o': /* output file */
552 char *outfile = opj_optarg;
553 parameters->cod_format = get_file_format(outfile);
554 switch(parameters->cod_format) {
572 fprintf(stderr, "Unknown output format image %s [only *.pnm, *.pgm, *.ppm, *.pgx, *.bmp, *.tif, *.raw or *.tga]!! \n", outfile);
575 strncpy(parameters->outfile, outfile, sizeof(parameters->outfile)-1);
579 /* ----------------------------------------------------- */
581 case 'O': /* output format */
584 char *of = opj_optarg;
585 sprintf(outformat,".%s",of);
586 img_fol->set_out_format = 1;
587 parameters->cod_format = get_file_format(outformat);
588 switch(parameters->cod_format) {
590 img_fol->out_format = "pgx";
593 img_fol->out_format = "ppm";
596 img_fol->out_format = "bmp";
599 img_fol->out_format = "tif";
602 img_fol->out_format = "raw";
605 img_fol->out_format = "rawl";
608 img_fol->out_format = "raw";
611 img_fol->out_format = "png";
614 fprintf(stderr, "Unknown output format image %s [only *.pnm, *.pgm, *.ppm, *.pgx, *.bmp, *.tif, *.raw or *.tga]!! \n", outformat);
621 /* ----------------------------------------------------- */
624 case 'r': /* reduce option */
626 sscanf(opj_optarg, "%ud", &(parameters->core.cp_reduce));
630 /* ----------------------------------------------------- */
633 case 'l': /* layering option */
635 sscanf(opj_optarg, "%ud", &(parameters->core.cp_layer));
639 /* ----------------------------------------------------- */
641 case 'h': /* display an help description */
642 decode_help_display();
645 /* ----------------------------------------------------- */
647 case 'y': /* Image Directory path */
649 img_fol->imgdirpath = (char*)malloc(strlen(opj_optarg) + 1);
650 strcpy(img_fol->imgdirpath,opj_optarg);
651 img_fol->set_imgdir=1;
655 /* ----------------------------------------------------- */
657 case 'd': /* Input decode ROI */
659 int size_optarg = (int)strlen(opj_optarg) + 1;
660 char *ROI_values = (char*) malloc((size_t)size_optarg);
661 ROI_values[0] = '\0';
662 strncpy(ROI_values, opj_optarg, strlen(opj_optarg));
663 ROI_values[strlen(opj_optarg)] = '\0';
664 /*printf("ROI_values = %s [%d / %d]\n", ROI_values, strlen(ROI_values), size_optarg ); */
665 parse_DA_values( ROI_values, ¶meters->DA_x0, ¶meters->DA_y0, ¶meters->DA_x1, ¶meters->DA_y1);
671 /* ----------------------------------------------------- */
673 case 't': /* Input tile index */
675 sscanf(opj_optarg, "%ud", ¶meters->tile_index);
676 parameters->nb_tile_to_decode = 1;
680 /* ----------------------------------------------------- */
682 case 'x': /* Creation of index file */
684 char *index = opj_optarg;
685 strncpy(indexfilename, index, OPJ_PATH_LEN);
689 /* ----------------------------------------------------- */
690 case 'p': /* Force precision */
692 if (!parse_precision(opj_optarg, parameters))
698 /* ----------------------------------------------------- */
703 case 'W': /* activate JPWL correction */
707 token = strtok(opj_optarg, ",");
708 while(token != NULL) {
710 /* search expected number of components */
715 compno = JPWL_EXPECTED_COMPONENTS; /* predefined no. of components */
717 if(sscanf(token, "c=%d", &compno) == 1) {
719 if ((compno < 1) || (compno > 256)) {
720 fprintf(stderr, "ERROR -> invalid number of components c = %d\n", compno);
723 parameters->jpwl_exp_comps = compno;
725 } else if (!strcmp(token, "c")) {
727 parameters->jpwl_exp_comps = compno; /* auto for default size */
730 fprintf(stderr, "ERROR -> invalid components specified = %s\n", token);
735 /* search maximum number of tiles */
740 tileno = JPWL_MAXIMUM_TILES; /* maximum no. of tiles */
742 if(sscanf(token, "t=%d", &tileno) == 1) {
744 if ((tileno < 1) || (tileno > JPWL_MAXIMUM_TILES)) {
745 fprintf(stderr, "ERROR -> invalid number of tiles t = %d\n", tileno);
748 parameters->jpwl_max_tiles = tileno;
750 } else if (!strcmp(token, "t")) {
752 parameters->jpwl_max_tiles = tileno; /* auto for default size */
755 fprintf(stderr, "ERROR -> invalid tiles specified = %s\n", token);
760 /* next token or bust */
761 token = strtok(NULL, ",");
763 parameters->jpwl_correct = OPJ_TRUE;
764 fprintf(stdout, "JPWL correction capability activated\n");
765 fprintf(stdout, "- expecting %d components\n", parameters->jpwl_exp_comps);
768 #endif /* USE_JPWL */
771 /* ----------------------------------------------------- */
774 fprintf(stderr, "[WARNING] An invalid option has been ignored.\n");
779 /* check for possible errors */
780 if(img_fol->set_imgdir==1){
781 if(!(parameters->infile[0]==0)){
782 fprintf(stderr, "[ERROR] options -ImgDir and -i cannot be used together.\n");
785 if(img_fol->set_out_format == 0){
786 fprintf(stderr, "[ERROR] When -ImgDir is used, -OutFor <FORMAT> must be used.\n");
787 fprintf(stderr, "Only one format allowed.\n"
788 "Valid format are PGM, PPM, PNM, PGX, BMP, TIF, RAW and TGA.\n");
791 if(!((parameters->outfile[0] == 0))){
792 fprintf(stderr, "[ERROR] options -ImgDir and -o cannot be used together.\n");
796 if((parameters->infile[0] == 0) || (parameters->outfile[0] == 0)) {
797 fprintf(stderr, "[ERROR] Required parameters are missing\n"
798 "Example: %s -i image.j2k -o image.pgm\n",argv[0]);
799 fprintf(stderr, " Help: %s -h\n",argv[0]);
807 /* -------------------------------------------------------------------------- */
809 * Parse decoding area input values
812 /* -------------------------------------------------------------------------- */
813 int parse_DA_values( char* inArg, unsigned int *DA_x0, unsigned int *DA_y0, unsigned int *DA_x1, unsigned int *DA_y1)
819 result = strtok( inArg, delims );
821 while( (result != NULL) && (it < 4 ) ) {
822 values[it] = atoi(result);
823 result = strtok( NULL, delims );
831 *DA_x0 = (OPJ_UINT32)values[0]; *DA_y0 = (OPJ_UINT32)values[1];
832 *DA_x1 = (OPJ_UINT32)values[2]; *DA_y1 = (OPJ_UINT32)values[3];
837 /* -------------------------------------------------------------------------- */
840 sample error callback expecting a FILE* client object
842 static void error_callback(const char *msg, void *client_data) {
844 fprintf(stdout, "[ERROR] %s", msg);
847 sample warning callback expecting a FILE* client object
849 static void warning_callback(const char *msg, void *client_data) {
851 fprintf(stdout, "[WARNING] %s", msg);
854 sample debug callback expecting no client object
856 static void info_callback(const char *msg, void *client_data) {
858 fprintf(stdout, "[INFO] %s", msg);
861 static void set_default_parameters(opj_decompress_parameters* parameters)
864 memset(parameters, 0, sizeof(opj_decompress_parameters));
866 /* default decoding parameters (command line specific) */
867 parameters->decod_format = -1;
868 parameters->cod_format = -1;
870 /* default decoding parameters (core) */
871 opj_set_default_decoder_parameters(&(parameters->core));
875 static void destroy_parameters(opj_decompress_parameters* parameters)
878 if (parameters->precision) {
879 free(parameters->precision);
880 parameters->precision = NULL;
885 /* -------------------------------------------------------------------------- */
887 static opj_image_t* convert_gray_to_rgb(opj_image_t* original)
890 opj_image_t* l_new_image = NULL;
891 opj_image_cmptparm_t* l_new_components = NULL;
893 l_new_components = (opj_image_cmptparm_t*)malloc((original->numcomps + 2U) * sizeof(opj_image_cmptparm_t));
894 if (l_new_components == NULL) {
895 fprintf(stderr, "ERROR -> opj_decompress: failed to allocate memory for RGB image!\n");
896 opj_image_destroy(original);
900 l_new_components[0].bpp = l_new_components[1].bpp = l_new_components[2].bpp = original->comps[0].bpp;
901 l_new_components[0].dx = l_new_components[1].dx = l_new_components[2].dx = original->comps[0].dx;
902 l_new_components[0].dy = l_new_components[1].dy = l_new_components[2].dy = original->comps[0].dy;
903 l_new_components[0].h = l_new_components[1].h = l_new_components[2].h = original->comps[0].h;
904 l_new_components[0].w = l_new_components[1].w = l_new_components[2].w = original->comps[0].w;
905 l_new_components[0].prec = l_new_components[1].prec = l_new_components[2].prec = original->comps[0].prec;
906 l_new_components[0].sgnd = l_new_components[1].sgnd = l_new_components[2].sgnd = original->comps[0].sgnd;
907 l_new_components[0].x0 = l_new_components[1].x0 = l_new_components[2].x0 = original->comps[0].x0;
908 l_new_components[0].y0 = l_new_components[1].y0 = l_new_components[2].y0 = original->comps[0].y0;
910 for(compno = 1U; compno < original->numcomps; ++compno) {
911 l_new_components[compno+2U].bpp = original->comps[compno].bpp;
912 l_new_components[compno+2U].dx = original->comps[compno].dx;
913 l_new_components[compno+2U].dy = original->comps[compno].dy;
914 l_new_components[compno+2U].h = original->comps[compno].h;
915 l_new_components[compno+2U].w = original->comps[compno].w;
916 l_new_components[compno+2U].prec = original->comps[compno].prec;
917 l_new_components[compno+2U].sgnd = original->comps[compno].sgnd;
918 l_new_components[compno+2U].x0 = original->comps[compno].x0;
919 l_new_components[compno+2U].y0 = original->comps[compno].y0;
922 l_new_image = opj_image_create(original->numcomps + 2U, l_new_components, OPJ_CLRSPC_SRGB);
923 free(l_new_components);
924 if (l_new_image == NULL) {
925 fprintf(stderr, "ERROR -> opj_decompress: failed to allocate memory for RGB image!\n");
926 opj_image_destroy(original);
930 l_new_image->x0 = original->x0;
931 l_new_image->x1 = original->x1;
932 l_new_image->y0 = original->y0;
933 l_new_image->y1 = original->y1;
935 l_new_image->comps[0].factor = l_new_image->comps[1].factor = l_new_image->comps[2].factor = original->comps[0].factor;
936 l_new_image->comps[0].alpha = l_new_image->comps[1].alpha = l_new_image->comps[2].alpha = original->comps[0].alpha;
937 l_new_image->comps[0].resno_decoded = l_new_image->comps[1].resno_decoded = l_new_image->comps[2].resno_decoded = original->comps[0].resno_decoded;
939 memcpy(l_new_image->comps[0].data, original->comps[0].data, original->comps[0].w * original->comps[0].h * sizeof(OPJ_INT32));
940 memcpy(l_new_image->comps[1].data, original->comps[0].data, original->comps[0].w * original->comps[0].h * sizeof(OPJ_INT32));
941 memcpy(l_new_image->comps[2].data, original->comps[0].data, original->comps[0].w * original->comps[0].h * sizeof(OPJ_INT32));
943 for(compno = 1U; compno < original->numcomps; ++compno) {
944 l_new_image->comps[compno+2U].factor = original->comps[compno].factor;
945 l_new_image->comps[compno+2U].alpha = original->comps[compno].alpha;
946 l_new_image->comps[compno+2U].resno_decoded = original->comps[compno].resno_decoded;
947 memcpy(l_new_image->comps[compno+2U].data, original->comps[compno].data, original->comps[compno].w * original->comps[compno].h * sizeof(OPJ_INT32));
949 opj_image_destroy(original);
953 /* -------------------------------------------------------------------------- */
955 static opj_image_t* upsample_image_components(opj_image_t* original)
957 opj_image_t* l_new_image = NULL;
958 opj_image_cmptparm_t* l_new_components = NULL;
959 OPJ_BOOL l_upsample_need = OPJ_FALSE;
962 for (compno = 0U; compno < original->numcomps; ++compno) {
963 if (original->comps[compno].factor > 0U) {
964 fprintf(stderr, "ERROR -> opj_decompress: -upsample not supported with reduction\n");
965 opj_image_destroy(original);
968 if ((original->comps[compno].dx > 1U) || (original->comps[compno].dy > 1U)) {
969 l_upsample_need = OPJ_TRUE;
973 if (!l_upsample_need) {
976 /* Upsample is needed */
977 l_new_components = (opj_image_cmptparm_t*)malloc(original->numcomps * sizeof(opj_image_cmptparm_t));
978 if (l_new_components == NULL) {
979 fprintf(stderr, "ERROR -> opj_decompress: failed to allocate memory for upsampled components!\n");
980 opj_image_destroy(original);
984 for (compno = 0U; compno < original->numcomps; ++compno) {
985 opj_image_cmptparm_t* l_new_cmp = &(l_new_components[compno]);
986 opj_image_comp_t* l_org_cmp = &(original->comps[compno]);
988 l_new_cmp->bpp = l_org_cmp->bpp;
989 l_new_cmp->prec = l_org_cmp->prec;
990 l_new_cmp->sgnd = l_org_cmp->sgnd;
991 l_new_cmp->x0 = original->x0;
992 l_new_cmp->y0 = original->y0;
995 l_new_cmp->w = l_org_cmp->w; /* should be original->x1 - original->x0 for dx==1 */
996 l_new_cmp->h = l_org_cmp->h; /* should be original->y1 - original->y0 for dy==0 */
998 if (l_org_cmp->dx > 1U) {
999 l_new_cmp->w = original->x1 - original->x0;
1002 if (l_org_cmp->dy > 1U) {
1003 l_new_cmp->h = original->y1 - original->y0;
1007 l_new_image = opj_image_create(original->numcomps, l_new_components, original->color_space);
1008 free(l_new_components);
1009 if (l_new_image == NULL) {
1010 fprintf(stderr, "ERROR -> opj_decompress: failed to allocate memory for upsampled components!\n");
1011 opj_image_destroy(original);
1015 l_new_image->x0 = original->x0;
1016 l_new_image->x1 = original->x1;
1017 l_new_image->y0 = original->y0;
1018 l_new_image->y1 = original->y1;
1020 for (compno = 0U; compno < original->numcomps; ++compno) {
1021 opj_image_comp_t* l_new_cmp = &(l_new_image->comps[compno]);
1022 opj_image_comp_t* l_org_cmp = &(original->comps[compno]);
1024 l_new_cmp->factor = l_org_cmp->factor;
1025 l_new_cmp->alpha = l_org_cmp->alpha;
1026 l_new_cmp->resno_decoded = l_org_cmp->resno_decoded;
1028 if ((l_org_cmp->dx > 1U) || (l_org_cmp->dy > 1U)) {
1029 const OPJ_INT32* l_src = l_org_cmp->data;
1030 OPJ_INT32* l_dst = l_new_cmp->data;
1032 OPJ_UINT32 xoff, yoff;
1034 /* need to take into account dx & dy */
1035 xoff = l_org_cmp->dx * l_org_cmp->x0 - original->x0;
1036 yoff = l_org_cmp->dy * l_org_cmp->y0 - original->y0;
1037 if ((xoff >= l_org_cmp->dx) || (yoff >= l_org_cmp->dy)) {
1038 fprintf(stderr, "ERROR -> opj_decompress: Invalid image/component parameters found when upsampling\n");
1039 opj_image_destroy(original);
1040 opj_image_destroy(l_new_image);
1044 for (y = 0U; y < yoff; ++y) {
1045 memset(l_dst, 0U, l_new_cmp->w * sizeof(OPJ_INT32));
1046 l_dst += l_new_cmp->w;
1049 if(l_new_cmp->h > (l_org_cmp->dy - 1U)) { /* check substraction overflow for really small images */
1050 for (; y < l_new_cmp->h - (l_org_cmp->dy - 1U); y += l_org_cmp->dy) {
1055 for (x = 0U; x < xoff; ++x) {
1058 if (l_new_cmp->w > (l_org_cmp->dx - 1U)) { /* check substraction overflow for really small images */
1059 for (; x < l_new_cmp->w - (l_org_cmp->dx - 1U); x += l_org_cmp->dx, ++xorg) {
1061 for (dx = 0U; dx < l_org_cmp->dx; ++dx) {
1062 l_dst[x + dx] = l_src[xorg];
1066 for (; x < l_new_cmp->w; ++x) {
1067 l_dst[x] = l_src[xorg];
1069 l_dst += l_new_cmp->w;
1071 for (dy = 1U; dy < l_org_cmp->dy; ++dy) {
1072 memcpy(l_dst, l_dst - l_new_cmp->w, l_new_cmp->w * sizeof(OPJ_INT32));
1073 l_dst += l_new_cmp->w;
1075 l_src += l_org_cmp->w;
1078 if (y < l_new_cmp->h) {
1083 for (x = 0U; x < xoff; ++x) {
1086 if (l_new_cmp->w > (l_org_cmp->dx - 1U)) { /* check substraction overflow for really small images */
1087 for (; x < l_new_cmp->w - (l_org_cmp->dx - 1U); x += l_org_cmp->dx, ++xorg) {
1089 for (dx = 0U; dx < l_org_cmp->dx; ++dx) {
1090 l_dst[x + dx] = l_src[xorg];
1094 for (; x < l_new_cmp->w; ++x) {
1095 l_dst[x] = l_src[xorg];
1097 l_dst += l_new_cmp->w;
1099 for (; y < l_new_cmp->h; ++y) {
1100 memcpy(l_dst, l_dst - l_new_cmp->w, l_new_cmp->w * sizeof(OPJ_INT32));
1101 l_dst += l_new_cmp->w;
1106 memcpy(l_new_cmp->data, l_org_cmp->data, l_org_cmp->w * l_org_cmp->h * sizeof(OPJ_INT32));
1109 opj_image_destroy(original);
1113 /* -------------------------------------------------------------------------- */
1115 * OPJ_DECOMPRESS MAIN
1117 /* -------------------------------------------------------------------------- */
1118 int main(int argc, char **argv)
1120 opj_decompress_parameters parameters; /* decompression parameters */
1121 opj_image_t* image = NULL;
1122 opj_stream_t *l_stream = NULL; /* Stream */
1123 opj_codec_t* l_codec = NULL; /* Handle to a decompressor */
1124 opj_codestream_index_t* cstr_index = NULL;
1126 char indexfilename[OPJ_PATH_LEN]; /* index file name */
1128 OPJ_INT32 num_images, imageno;
1130 dircnt_t *dirptr = NULL;
1133 /* set decoding parameters to default values */
1134 set_default_parameters(¶meters);
1136 /* FIXME Initialize indexfilename and img_fol */
1139 /* Initialize img_fol */
1140 memset(&img_fol,0,sizeof(img_fol_t));
1142 /* parse input and get user encoding parameters */
1143 if(parse_cmdline_decoder(argc, argv, ¶meters,&img_fol, indexfilename) == 1) {
1144 destroy_parameters(¶meters);
1145 return EXIT_FAILURE;
1148 /* Initialize reading of directory */
1149 if(img_fol.set_imgdir==1){
1151 num_images=get_num_images(img_fol.imgdirpath);
1153 dirptr=(dircnt_t*)malloc(sizeof(dircnt_t));
1155 dirptr->filename_buf = (char*)malloc((size_t)num_images*OPJ_PATH_LEN*sizeof(char)); /* Stores at max 10 image file names*/
1156 dirptr->filename = (char**) malloc((size_t)num_images*sizeof(char*));
1158 if(!dirptr->filename_buf){
1159 destroy_parameters(¶meters);
1160 return EXIT_FAILURE;
1162 for(it_image=0;it_image<num_images;it_image++){
1163 dirptr->filename[it_image] = dirptr->filename_buf + it_image*OPJ_PATH_LEN;
1166 if(load_images(dirptr,img_fol.imgdirpath)==1){
1167 destroy_parameters(¶meters);
1168 return EXIT_FAILURE;
1171 fprintf(stdout,"Folder is empty\n");
1172 destroy_parameters(¶meters);
1173 return EXIT_FAILURE;
1179 /*Decoding image one by one*/
1180 for(imageno = 0; imageno < num_images ; imageno++) {
1182 fprintf(stderr,"\n");
1184 if(img_fol.set_imgdir==1){
1185 if (get_next_file(imageno, dirptr,&img_fol, ¶meters)) {
1186 fprintf(stderr,"skipping file...\n");
1187 destroy_parameters(¶meters);
1192 /* read the input file and put it in memory */
1193 /* ---------------------------------------- */
1195 l_stream = opj_stream_create_default_file_stream(parameters.infile,1);
1197 fprintf(stderr, "ERROR -> failed to create the stream from the file %s\n", parameters.infile);
1198 destroy_parameters(¶meters);
1199 return EXIT_FAILURE;
1202 /* decode the JPEG2000 stream */
1203 /* ---------------------- */
1205 switch(parameters.decod_format) {
1206 case J2K_CFMT: /* JPEG-2000 codestream */
1208 /* Get a decoder handle */
1209 l_codec = opj_create_decompress(OPJ_CODEC_J2K);
1212 case JP2_CFMT: /* JPEG 2000 compressed image data */
1214 /* Get a decoder handle */
1215 l_codec = opj_create_decompress(OPJ_CODEC_JP2);
1218 case JPT_CFMT: /* JPEG 2000, JPIP */
1220 /* Get a decoder handle */
1221 l_codec = opj_create_decompress(OPJ_CODEC_JPT);
1225 fprintf(stderr, "skipping file..\n");
1226 destroy_parameters(¶meters);
1227 opj_stream_destroy(l_stream);
1231 /* catch events using our callbacks and give a local context */
1232 opj_set_info_handler(l_codec, info_callback,00);
1233 opj_set_warning_handler(l_codec, warning_callback,00);
1234 opj_set_error_handler(l_codec, error_callback,00);
1236 /* Setup the decoder decoding parameters using user parameters */
1237 if ( !opj_setup_decoder(l_codec, &(parameters.core)) ){
1238 fprintf(stderr, "ERROR -> opj_compress: failed to setup the decoder\n");
1239 destroy_parameters(¶meters);
1240 opj_stream_destroy(l_stream);
1241 opj_destroy_codec(l_codec);
1242 return EXIT_FAILURE;
1246 /* Read the main header of the codestream and if necessary the JP2 boxes*/
1247 if(! opj_read_header(l_stream, l_codec, &image)){
1248 fprintf(stderr, "ERROR -> opj_decompress: failed to read the header\n");
1249 destroy_parameters(¶meters);
1250 opj_stream_destroy(l_stream);
1251 opj_destroy_codec(l_codec);
1252 opj_image_destroy(image);
1253 return EXIT_FAILURE;
1256 if (!parameters.nb_tile_to_decode) {
1257 /* Optional if you want decode the entire image */
1258 if (!opj_set_decode_area(l_codec, image, (OPJ_INT32)parameters.DA_x0,
1259 (OPJ_INT32)parameters.DA_y0, (OPJ_INT32)parameters.DA_x1, (OPJ_INT32)parameters.DA_y1)){
1260 fprintf(stderr, "ERROR -> opj_decompress: failed to set the decoded area\n");
1261 destroy_parameters(¶meters);
1262 opj_stream_destroy(l_stream);
1263 opj_destroy_codec(l_codec);
1264 opj_image_destroy(image);
1265 return EXIT_FAILURE;
1268 /* Get the decoded image */
1269 if (!(opj_decode(l_codec, l_stream, image) && opj_end_decompress(l_codec, l_stream))) {
1270 fprintf(stderr,"ERROR -> opj_decompress: failed to decode image!\n");
1271 destroy_parameters(¶meters);
1272 opj_destroy_codec(l_codec);
1273 opj_stream_destroy(l_stream);
1274 opj_image_destroy(image);
1275 return EXIT_FAILURE;
1280 /* It is just here to illustrate how to use the resolution after set parameters */
1281 /*if (!opj_set_decoded_resolution_factor(l_codec, 5)) {
1282 fprintf(stderr, "ERROR -> opj_decompress: failed to set the resolution factor tile!\n");
1283 opj_destroy_codec(l_codec);
1284 opj_stream_destroy(l_stream);
1285 opj_image_destroy(image);
1286 return EXIT_FAILURE;
1289 if (!opj_get_decoded_tile(l_codec, l_stream, image, parameters.tile_index)) {
1290 fprintf(stderr, "ERROR -> opj_decompress: failed to decode tile!\n");
1291 destroy_parameters(¶meters);
1292 opj_destroy_codec(l_codec);
1293 opj_stream_destroy(l_stream);
1294 opj_image_destroy(image);
1295 return EXIT_FAILURE;
1297 fprintf(stdout, "tile %d is decoded!\n\n", parameters.tile_index);
1300 /* Close the byte stream */
1301 opj_stream_destroy(l_stream);
1303 if(image->color_space == OPJ_CLRSPC_SYCC){
1304 color_sycc_to_rgb(image); /* FIXME */
1307 if( image->color_space != OPJ_CLRSPC_SYCC
1308 && image->numcomps == 3 && image->comps[0].dx == image->comps[0].dy
1309 && image->comps[1].dx != 1 )
1310 image->color_space = OPJ_CLRSPC_SYCC;
1311 else if (image->numcomps <= 2)
1312 image->color_space = OPJ_CLRSPC_GRAY;
1314 if(image->icc_profile_buf) {
1315 #if defined(OPJ_HAVE_LIBLCMS1) || defined(OPJ_HAVE_LIBLCMS2)
1316 color_apply_icc_profile(image); /* FIXME */
1318 free(image->icc_profile_buf);
1319 image->icc_profile_buf = NULL; image->icc_profile_len = 0;
1322 /* Force output precision */
1323 /* ---------------------- */
1324 if (parameters.precision != NULL)
1327 for (compno = 0; compno < image->numcomps; ++compno)
1329 OPJ_UINT32 precno = compno;
1332 if (precno >= parameters.nb_precision) {
1333 precno = parameters.nb_precision - 1U;
1336 prec = parameters.precision[precno].prec;
1338 prec = image->comps[compno].prec;
1341 switch (parameters.precision[precno].mode) {
1342 case OPJ_PREC_MODE_CLIP:
1343 clip_component(&(image->comps[compno]), prec);
1345 case OPJ_PREC_MODE_SCALE:
1346 scale_component(&(image->comps[compno]), prec);
1355 /* Upsample components */
1356 /* ------------------- */
1357 if (parameters.upsample)
1359 image = upsample_image_components(image);
1360 if (image == NULL) {
1361 fprintf(stderr, "ERROR -> opj_decompress: failed to upsample image components!\n");
1362 destroy_parameters(¶meters);
1363 opj_destroy_codec(l_codec);
1364 return EXIT_FAILURE;
1368 /* Force RGB output */
1369 /* ---------------- */
1370 if (parameters.force_rgb)
1372 switch (image->color_space) {
1373 case OPJ_CLRSPC_SRGB:
1375 case OPJ_CLRSPC_GRAY:
1376 image = convert_gray_to_rgb(image);
1379 fprintf(stderr, "ERROR -> opj_decompress: don't know how to convert image to RGB colorspace!\n");
1380 opj_image_destroy(image);
1384 if (image == NULL) {
1385 fprintf(stderr, "ERROR -> opj_decompress: failed to convert to RGB image!\n");
1386 destroy_parameters(¶meters);
1387 opj_destroy_codec(l_codec);
1388 return EXIT_FAILURE;
1392 /* create output image */
1393 /* ------------------- */
1394 switch (parameters.cod_format) {
1395 case PXM_DFMT: /* PNM PGM PPM */
1396 if (imagetopnm(image, parameters.outfile)) {
1397 fprintf(stderr,"[ERROR] Outfile %s not generated\n",parameters.outfile);
1401 fprintf(stdout,"[INFO] Generated Outfile %s\n",parameters.outfile);
1405 case PGX_DFMT: /* PGX */
1406 if(imagetopgx(image, parameters.outfile)){
1407 fprintf(stderr,"[ERROR] Outfile %s not generated\n",parameters.outfile);
1411 fprintf(stdout,"[INFO] Generated Outfile %s\n",parameters.outfile);
1415 case BMP_DFMT: /* BMP */
1416 if(imagetobmp(image, parameters.outfile)){
1417 fprintf(stderr,"[ERROR] Outfile %s not generated\n",parameters.outfile);
1421 fprintf(stdout,"[INFO] Generated Outfile %s\n",parameters.outfile);
1424 #ifdef OPJ_HAVE_LIBTIFF
1425 case TIF_DFMT: /* TIFF */
1426 if(imagetotif(image, parameters.outfile)){
1427 fprintf(stderr,"[ERROR] Outfile %s not generated\n",parameters.outfile);
1431 fprintf(stdout,"[INFO] Generated Outfile %s\n",parameters.outfile);
1434 #endif /* OPJ_HAVE_LIBTIFF */
1435 case RAW_DFMT: /* RAW */
1436 if(imagetoraw(image, parameters.outfile)){
1437 fprintf(stderr,"[ERROR] Error generating raw file. Outfile %s not generated\n",parameters.outfile);
1441 fprintf(stdout,"[INFO] Generated Outfile %s\n",parameters.outfile);
1445 case RAWL_DFMT: /* RAWL */
1446 if(imagetorawl(image, parameters.outfile)){
1447 fprintf(stderr,"[ERROR] Error generating rawl file. Outfile %s not generated\n",parameters.outfile);
1451 fprintf(stdout,"[INFO] Generated Outfile %s\n",parameters.outfile);
1455 case TGA_DFMT: /* TGA */
1456 if(imagetotga(image, parameters.outfile)){
1457 fprintf(stderr,"[ERROR] Error generating tga file. Outfile %s not generated\n",parameters.outfile);
1461 fprintf(stdout,"[INFO] Generated Outfile %s\n",parameters.outfile);
1464 #ifdef OPJ_HAVE_LIBPNG
1465 case PNG_DFMT: /* PNG */
1466 if(imagetopng(image, parameters.outfile)){
1467 fprintf(stderr,"[ERROR] Error generating png file. Outfile %s not generated\n",parameters.outfile);
1471 fprintf(stdout,"[INFO] Generated Outfile %s\n",parameters.outfile);
1474 #endif /* OPJ_HAVE_LIBPNG */
1475 /* Can happen if output file is TIFF or PNG
1476 * and OPJ_HAVE_LIBTIF or OPJ_HAVE_LIBPNG is undefined
1479 fprintf(stderr,"[ERROR] Outfile %s not generated\n",parameters.outfile);
1483 /* free remaining structures */
1485 opj_destroy_codec(l_codec);
1489 /* free image data structure */
1490 opj_image_destroy(image);
1492 /* destroy the codestream index */
1493 opj_destroy_cstr_index(&cstr_index);
1495 if(failed) remove(parameters.outfile);
1497 destroy_parameters(¶meters);
1498 return failed ? EXIT_FAILURE : EXIT_SUCCESS;