2 * Copyright (c) 2010, Mathieu Malaterre, GDCM
3 * Copyright (c) 2011, Mickael Savinaud, Communications & Systemes <mickael.savinaud@c-s.fr>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 * POSSIBILITY OF SUCH DAMAGE.
27 #include "opj_config.h"
35 #include "windirent.h"
44 #define _stricmp strcasecmp
45 #define _strnicmp strncasecmp
51 #include "opj_getopt.h"
55 #include "format_defs.h"
57 typedef struct dircnt{
58 /** Buffer for holding images read from Directory*/
60 /** Pointer to the buffer*/
65 typedef struct img_folder{
66 /** The directory path of the folder containing input images*/
69 const char *out_format;
72 /** Enable Cod Format for output*/
77 /* -------------------------------------------------------------------------- */
79 int get_num_images(char *imgdirpath);
80 int load_images(dircnt_t *dirptr, char *imgdirpath);
81 int get_file_format(char *filename);
82 char get_next_file(int imageno,dircnt_t *dirptr,img_fol_t *img_fol, opj_dparameters_t *parameters);
84 int parse_cmdline_decoder(int argc, char **argv, opj_dparameters_t *parameters,img_fol_t *img_fol);
85 int parse_DA_values( char* inArg, unsigned int *DA_x0, unsigned int *DA_y0, unsigned int *DA_x1, unsigned int *DA_y1);
87 /* -------------------------------------------------------------------------- */
88 void decode_help_display(void) {
89 fprintf(stdout,"HELP for j2k_dump\n----\n\n");
90 fprintf(stdout,"- the -h option displays this help information on screen\n\n");
93 fprintf(stdout,"List of parameters for the JPEG 2000 "
100 fprintf(stdout,"\n");
101 fprintf(stdout," -ImgDir \n");
102 fprintf(stdout," Image file Directory path \n");
103 fprintf(stdout," -i <compressed file>\n");
104 fprintf(stdout," REQUIRED only if an Input image directory not specified\n");
105 fprintf(stdout," Currently accepts J2K-files, JP2-files and JPT-files. The file type\n");
106 fprintf(stdout," is identified based on its suffix.\n");
107 fprintf(stdout," -o <output file>\n");
108 fprintf(stdout," OPTIONAL\n");
109 fprintf(stdout," Output file where file info will be dump.\n");
110 fprintf(stdout," By default it will be in the stdout.\n");
111 fprintf(stdout," -d <x0,x1,y0,y1>\n"); /* FIXME WIP_MSD */
112 fprintf(stdout," OPTIONAL\n");
113 fprintf(stdout," Decoding area\n");
114 fprintf(stdout," By default all tiles header are read.\n");
115 fprintf(stdout," -v "); /* FIXME WIP_MSD */
116 fprintf(stdout," OPTIONAL\n");
117 fprintf(stdout," Activate or not the verbose mode (display info and warning message)\n");
118 fprintf(stdout," By default verbose mode is off.\n");
119 fprintf(stdout,"\n");
122 /* -------------------------------------------------------------------------- */
123 int get_num_images(char *imgdirpath){
125 struct dirent* content;
128 /*Reading the input images from given input directory*/
130 dir= opendir(imgdirpath);
132 fprintf(stderr,"Could not open Folder %s\n",imgdirpath);
136 while((content=readdir(dir))!=NULL){
137 if(strcmp(".",content->d_name)==0 || strcmp("..",content->d_name)==0 )
144 /* -------------------------------------------------------------------------- */
145 int load_images(dircnt_t *dirptr, char *imgdirpath){
147 struct dirent* content;
150 /*Reading the input images from given input directory*/
152 dir= opendir(imgdirpath);
154 fprintf(stderr,"Could not open Folder %s\n",imgdirpath);
157 fprintf(stderr,"Folder opened successfully\n");
160 while((content=readdir(dir))!=NULL){
161 if(strcmp(".",content->d_name)==0 || strcmp("..",content->d_name)==0 )
164 strcpy(dirptr->filename[i],content->d_name);
170 /* -------------------------------------------------------------------------- */
171 int get_file_format(char *filename) {
173 static const char *extension[] = {"pgx", "pnm", "pgm", "ppm", "bmp","tif", "raw", "tga", "png", "j2k", "jp2", "jpt", "j2c", "jpc" };
174 static const int format[] = { PGX_DFMT, PXM_DFMT, PXM_DFMT, PXM_DFMT, BMP_DFMT, TIF_DFMT, RAW_DFMT, TGA_DFMT, PNG_DFMT, J2K_CFMT, JP2_CFMT, JPT_CFMT, J2K_CFMT, J2K_CFMT };
175 char * ext = strrchr(filename, '.');
180 for(i = 0; i < sizeof(format)/sizeof(*format); i++) {
181 if(_strnicmp(ext, extension[i], 3) == 0) {
190 /* -------------------------------------------------------------------------- */
191 char get_next_file(int imageno,dircnt_t *dirptr,img_fol_t *img_fol, opj_dparameters_t *parameters){
192 char image_filename[OPJ_PATH_LEN], infilename[OPJ_PATH_LEN],outfilename[OPJ_PATH_LEN],temp_ofname[OPJ_PATH_LEN];
193 char *temp_p, temp1[OPJ_PATH_LEN]="";
195 strcpy(image_filename,dirptr->filename[imageno]);
196 fprintf(stderr,"File Number %d \"%s\"\n",imageno,image_filename);
197 parameters->decod_format = get_file_format(image_filename);
198 if (parameters->decod_format == -1)
200 sprintf(infilename,"%s/%s",img_fol->imgdirpath,image_filename);
201 strncpy(parameters->infile, infilename, sizeof(infilename));
203 /* Set output file */
204 strcpy(temp_ofname,strtok(image_filename,"."));
205 while((temp_p = strtok(NULL,".")) != NULL){
206 strcat(temp_ofname,temp1);
207 sprintf(temp1,".%s",temp_p);
209 if(img_fol->set_out_format==1){
210 sprintf(outfilename,"%s/%s.%s",img_fol->imgdirpath,temp_ofname,img_fol->out_format);
211 strncpy(parameters->outfile, outfilename, sizeof(outfilename));
216 /* -------------------------------------------------------------------------- */
218 * Parse the command line
220 /* -------------------------------------------------------------------------- */
221 int parse_cmdline_decoder(int argc, char **argv, opj_dparameters_t *parameters,img_fol_t *img_fol) {
223 opj_option_t long_option[]={
224 {"ImgDir",REQ_ARG, NULL ,'y'},
226 const char optlist[] = "i:o:d:hv";
228 totlen=sizeof(long_option);
229 img_fol->set_out_format = 0;
231 c = opj_getopt_long(argc, argv,optlist,long_option,totlen);
235 case 'i': /* input file */
237 char *infile = opj_optarg;
238 parameters->decod_format = get_file_format(infile);
239 switch(parameters->decod_format) {
248 "!! Unrecognized format for infile : %s [accept only *.j2k, *.jp2, *.jpc or *.jpt] !!\n\n",
252 strncpy(parameters->infile, infile, sizeof(parameters->infile)-1);
256 /* ------------------------------------------------------ */
258 case 'o': /* output file */
260 char *outfile = opj_optarg;
261 strncpy(parameters->outfile, outfile, sizeof(parameters->outfile)-1);
265 /* ----------------------------------------------------- */
267 case 'h': /* display an help description */
268 decode_help_display();
271 /* ------------------------------------------------------ */
273 case 'y': /* Image Directory path */
275 img_fol->imgdirpath = (char*)malloc(strlen(opj_optarg) + 1);
276 strcpy(img_fol->imgdirpath,opj_optarg);
277 img_fol->set_imgdir=1;
281 /* ----------------------------------------------------- */
283 case 'd': /* Input decode ROI */
285 int size_optarg = (int)strlen(opj_optarg) + 1;
286 char *ROI_values = (char*) malloc(size_optarg);
287 ROI_values[0] = '\0';
288 strncpy(ROI_values, opj_optarg, strlen(opj_optarg));
289 ROI_values[strlen(opj_optarg)] = '\0';
290 /*printf("ROI_values = %s [%d / %d]\n", ROI_values, strlen(ROI_values), size_optarg ); */
291 parse_DA_values( ROI_values, ¶meters->DA_x0, ¶meters->DA_y0, ¶meters->DA_x1, ¶meters->DA_y1);
294 /* ----------------------------------------------------- */
296 case 'v': /* Verbose mode */
298 parameters->m_verbose = 1;
302 /* ----------------------------------------------------- */
304 fprintf(stderr,"WARNING -> this option is not valid \"-%c %s\"\n",c, opj_optarg);
309 /* check for possible errors */
310 if(img_fol->set_imgdir==1){
311 if(!(parameters->infile[0]==0)){
312 fprintf(stderr, "Error: options -ImgDir and -i cannot be used together !!\n");
315 if(img_fol->set_out_format == 0){
316 fprintf(stderr, "Error: When -ImgDir is used, -OutFor <FORMAT> must be used !!\n");
317 fprintf(stderr, "Only one format allowed! Valid format PGM, PPM, PNM, PGX, BMP, TIF, RAW and TGA!!\n");
320 if(!((parameters->outfile[0] == 0))){
321 fprintf(stderr, "Error: options -ImgDir and -o cannot be used together !!\n");
325 if((parameters->infile[0] == 0) ) {
326 fprintf(stderr, "Example: %s -i image.j2k\n",argv[0]);
327 fprintf(stderr, " Try: %s -h\n",argv[0]);
335 /* -------------------------------------------------------------------------- */
337 * Parse decoding area input values
340 /* -------------------------------------------------------------------------- */
341 int parse_DA_values( char* inArg, unsigned int *DA_x0, unsigned int *DA_y0, unsigned int *DA_x1, unsigned int *DA_y1)
347 result = strtok( inArg, delims );
349 while( (result != NULL) && (it < 4 ) ) {
350 values[it] = atoi(result);
351 result = strtok( NULL, delims );
359 *DA_x0 = values[0]; *DA_y0 = values[1];
360 *DA_x1 = values[2]; *DA_y1 = values[3];
366 /* -------------------------------------------------------------------------- */
370 /* -------------------------------------------------------------------------- */
371 int main(int argc, char *argv[])
373 FILE *fsrc = NULL, *fout = NULL;
375 opj_dparameters_t parameters; /* Decompression parameters */
376 opj_event_mgr_t event_mgr; /* Event manager */
377 opj_image_header_t img_header; /* Image info structure */
378 opj_codec_t* dinfo = NULL; /* Handle to a decompressor */
379 opj_stream_t *cio = NULL; /* Stream */
380 opj_codestream_info_v2_t* cstr_info;
381 opj_codestream_index_t* cstr_index;
383 OPJ_INT32 num_images, imageno;
385 dircnt_t *dirptr = NULL;
387 opj_bool l_go_on = OPJ_TRUE;
388 OPJ_UINT32 l_max_data_size = 1000;
389 OPJ_BYTE * l_data = (OPJ_BYTE *) malloc(1000);
391 /* Set decoding parameters to default values */
392 opj_set_default_decoder_parameters(¶meters);
394 /* Initialize img_fol */
395 memset(&img_fol,0,sizeof(img_fol_t));
397 /* Parse input and get user encoding parameters */
398 if(parse_cmdline_decoder(argc, argv, ¶meters,&img_fol) == 1) {
402 /* Set default event mgr */
403 opj_set_default_event_handler(&event_mgr, parameters.m_verbose);
405 /* Initialize reading of directory */
406 if(img_fol.set_imgdir==1){
408 num_images=get_num_images(img_fol.imgdirpath);
410 dirptr=(dircnt_t*)malloc(sizeof(dircnt_t));
412 dirptr->filename_buf = (char*)malloc(num_images*OPJ_PATH_LEN*sizeof(char)); /* Stores at max 10 image file names */
413 dirptr->filename = (char**) malloc(num_images*sizeof(char*));
415 if(!dirptr->filename_buf){
419 for(it_image=0;it_image<num_images;it_image++){
420 dirptr->filename[it_image] = dirptr->filename_buf + it_image*OPJ_PATH_LEN;
423 if(load_images(dirptr,img_fol.imgdirpath)==1){
428 fprintf(stdout,"Folder is empty\n");
435 /* Try to open for writing the output file if necessary */
436 if (parameters.outfile[0] != 0){
437 fout = fopen(parameters.outfile,"w");
439 fprintf(stderr, "ERROR -> failed to open %s for writing\n", parameters.outfile);
446 /* Read the header of each image one by one */
447 for(imageno = 0; imageno < num_images ; imageno++){
449 fprintf(stderr,"\n");
451 if(img_fol.set_imgdir==1){
452 if (get_next_file(imageno, dirptr,&img_fol, ¶meters)) {
453 fprintf(stderr,"skipping file...\n");
458 /* Read the input file and put it in memory */
459 /* ---------------------------------------- */
460 fsrc = fopen(parameters.infile, "rb");
462 fprintf(stderr, "ERROR -> failed to open %s for reading\n", parameters.infile);
466 cio = opj_stream_create_default_file_stream(fsrc,1);
468 fprintf(stderr, "ERROR -> failed to create the stream from the file\n");
472 /* Read the JPEG2000 stream */
473 /* ------------------------ */
475 switch(parameters.decod_format) {
476 case J2K_CFMT: /* JPEG-2000 codestream */
478 /* Get a decoder handle */
479 dinfo = opj_create_decompress_v2(CODEC_J2K);
482 case JP2_CFMT: /* JPEG 2000 compressed image data */
484 /* Get a decoder handle */
485 dinfo = opj_create_decompress_v2(CODEC_JP2);
488 case JPT_CFMT: /* JPEG 2000, JPIP */
490 /* Get a decoder handle */
491 dinfo = opj_create_decompress_v2(CODEC_JPT);
495 fprintf(stderr, "skipping file..\n");
496 opj_stream_destroy(cio);
500 /* Setup the decoder decoding parameters using user parameters */
501 if ( !opj_setup_decoder_v2(dinfo, ¶meters, &event_mgr) ){
502 fprintf(stderr, "ERROR -> j2k_dump: failed to setup the decoder\n");
503 opj_stream_destroy(cio);
505 opj_destroy_codec(dinfo);
510 /* Read the main header of the codestream and if necessary the JP2 boxes*/
511 if(! opj_read_header(cio, dinfo, &img_header)){
512 fprintf(stderr, "ERROR -> j2k_dump: failed to read the header\n");
513 opj_stream_destroy(cio);
515 opj_destroy_codec(dinfo);
520 opj_dump_codec(dinfo, OPJ_IMG_INFO | OPJ_J2K_MH_INFO | OPJ_J2K_MH_IND, stdout );
522 cstr_info = opj_get_cstr_info(dinfo);
524 cstr_index = opj_get_cstr_index(dinfo);
526 fprintf(stdout,"Setting decoding area to %d,%d,%d,%d\n",
527 parameters.DA_x0, parameters.DA_y0, parameters.DA_x1, parameters.DA_y1);
531 if (! opj_set_decode_area( dinfo,
532 parameters.DA_x0, parameters.DA_y0,
533 parameters.DA_x1, parameters.DA_y1)){
534 fprintf(stderr, "ERROR -> j2k_dump: failed to set the decoded area\n");
535 opj_stream_destroy(cio);
536 opj_destroy_codec(dinfo);
543 OPJ_INT32 l_current_tile_x0,l_current_tile_y0,l_current_tile_x1,l_current_tile_y1;
544 OPJ_UINT32 l_nb_comps, l_tile_index, l_data_size;
547 if (! opj_read_tile_header( dinfo,
558 fprintf(stderr, "ERROR -> j2k_dump: failed read the tile header\n");
559 opj_stream_destroy(cio);
561 opj_destroy_codec(dinfo);
567 if (l_data_size > l_max_data_size) {
569 l_data = (OPJ_BYTE *) realloc(l_data,l_data_size);
571 opj_stream_destroy(cio);
572 opj_destroy_codec(dinfo);
578 l_max_data_size = l_data_size;
581 if (! opj_decode_tile_data(dinfo,l_tile_index,l_data,l_data_size,cio))
584 opj_stream_destroy(cio);
585 opj_destroy_codec(dinfo);
590 /** now should inspect image to know the reduction factor and then how to behave with data */
596 /* close the byte stream */
597 opj_stream_destroy(cio);
600 /* free remaining structures */
602 opj_destroy_codec(dinfo);
605 /* destroy the image header */
606 opj_image_header_destroy(&img_header);
610 /* Close the output file */