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
49 #include "opj_getopt.h"
53 #include "format_defs.h"
55 typedef struct dircnt{
56 /** Buffer for holding images read from Directory*/
58 /** Pointer to the buffer*/
63 typedef struct img_folder{
64 /** The directory path of the folder containing input images*/
67 const char *out_format;
70 /** Enable Cod Format for output*/
75 /* -------------------------------------------------------------------------- */
77 int get_num_images(char *imgdirpath);
78 int load_images(dircnt_t *dirptr, char *imgdirpath);
79 int get_file_format(char *filename);
80 char get_next_file(int imageno,dircnt_t *dirptr,img_fol_t *img_fol, opj_dparameters_t *parameters);
82 int parse_cmdline_decoder(int argc, char **argv, opj_dparameters_t *parameters,img_fol_t *img_fol);
83 int parse_DA_values( char* inArg, unsigned int *DA_x0, unsigned int *DA_y0, unsigned int *DA_x1, unsigned int *DA_y1);
85 /* -------------------------------------------------------------------------- */
86 void decode_help_display(void) {
87 fprintf(stdout,"HELP for j2k_dump\n----\n\n");
88 fprintf(stdout,"- the -h option displays this help information on screen\n\n");
91 fprintf(stdout,"List of parameters for the JPEG 2000 "
99 fprintf(stdout," -ImgDir \n");
100 fprintf(stdout," Image file Directory path \n");
101 fprintf(stdout," -i <compressed file>\n");
102 fprintf(stdout," REQUIRED only if an Input image directory not specified\n");
103 fprintf(stdout," Currently accepts J2K-files, JP2-files and JPT-files. The file type\n");
104 fprintf(stdout," is identified based on its suffix.\n");
105 fprintf(stdout," -o <output file>\n");
106 fprintf(stdout," OPTIONAL\n");
107 fprintf(stdout," Output file where file info will be dump.\n");
108 fprintf(stdout," By default it will be in the stdout.\n");
109 fprintf(stdout," -d <x0,x1,y0,y1>\n"); /* FIXME WIP_MSD */
110 fprintf(stdout," OPTIONAL\n");
111 fprintf(stdout," Decoding area\n");
112 fprintf(stdout," By default all tiles header are read.\n");
113 fprintf(stdout," -v "); /* FIXME WIP_MSD */
114 fprintf(stdout," OPTIONAL\n");
115 fprintf(stdout," Activate or not the verbose mode (display info and warning message)\n");
116 fprintf(stdout," By default verbose mode is off.\n");
117 fprintf(stdout,"\n");
120 /* -------------------------------------------------------------------------- */
121 int get_num_images(char *imgdirpath){
123 struct dirent* content;
126 /*Reading the input images from given input directory*/
128 dir= opendir(imgdirpath);
130 fprintf(stderr,"Could not open Folder %s\n",imgdirpath);
134 while((content=readdir(dir))!=NULL){
135 if(strcmp(".",content->d_name)==0 || strcmp("..",content->d_name)==0 )
142 /* -------------------------------------------------------------------------- */
143 int load_images(dircnt_t *dirptr, char *imgdirpath){
145 struct dirent* content;
148 /*Reading the input images from given input directory*/
150 dir= opendir(imgdirpath);
152 fprintf(stderr,"Could not open Folder %s\n",imgdirpath);
155 fprintf(stderr,"Folder opened successfully\n");
158 while((content=readdir(dir))!=NULL){
159 if(strcmp(".",content->d_name)==0 || strcmp("..",content->d_name)==0 )
162 strcpy(dirptr->filename[i],content->d_name);
168 /* -------------------------------------------------------------------------- */
169 int get_file_format(char *filename) {
171 static const char *extension[] = {"pgx", "pnm", "pgm", "ppm", "bmp","tif", "raw", "tga", "png", "j2k", "jp2", "jpt", "j2c", "jpc" };
172 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 };
173 char * ext = strrchr(filename, '.');
178 for(i = 0; i < sizeof(format)/sizeof(*format); i++) {
179 if(_strnicmp(ext, extension[i], 3) == 0) {
188 /* -------------------------------------------------------------------------- */
189 char get_next_file(int imageno,dircnt_t *dirptr,img_fol_t *img_fol, opj_dparameters_t *parameters){
190 char image_filename[OPJ_PATH_LEN], infilename[OPJ_PATH_LEN],outfilename[OPJ_PATH_LEN],temp_ofname[OPJ_PATH_LEN];
191 char *temp_p, temp1[OPJ_PATH_LEN]="";
193 strcpy(image_filename,dirptr->filename[imageno]);
194 fprintf(stderr,"File Number %d \"%s\"\n",imageno,image_filename);
195 parameters->decod_format = get_file_format(image_filename);
196 if (parameters->decod_format == -1)
198 sprintf(infilename,"%s/%s",img_fol->imgdirpath,image_filename);
199 strncpy(parameters->infile, infilename, sizeof(infilename));
201 /* Set output file */
202 strcpy(temp_ofname,strtok(image_filename,"."));
203 while((temp_p = strtok(NULL,".")) != NULL){
204 strcat(temp_ofname,temp1);
205 sprintf(temp1,".%s",temp_p);
207 if(img_fol->set_out_format==1){
208 sprintf(outfilename,"%s/%s.%s",img_fol->imgdirpath,temp_ofname,img_fol->out_format);
209 strncpy(parameters->outfile, outfilename, sizeof(outfilename));
214 /* -------------------------------------------------------------------------- */
216 * Parse the command line
218 /* -------------------------------------------------------------------------- */
219 int parse_cmdline_decoder(int argc, char **argv, opj_dparameters_t *parameters,img_fol_t *img_fol) {
221 opj_option_t long_option[]={
222 {"ImgDir",REQ_ARG, NULL ,'y'},
224 const char optlist[] = "i:o:d:hv";
226 totlen=sizeof(long_option);
227 img_fol->set_out_format = 0;
229 c = opj_getopt_long(argc, argv,optlist,long_option,totlen);
233 case 'i': /* input file */
235 char *infile = opj_optarg;
236 parameters->decod_format = get_file_format(infile);
237 switch(parameters->decod_format) {
246 "!! Unrecognized format for infile : %s [accept only *.j2k, *.jp2, *.jpc or *.jpt] !!\n\n",
250 strncpy(parameters->infile, infile, sizeof(parameters->infile)-1);
254 /* ------------------------------------------------------ */
256 case 'o': /* output file */
258 char *outfile = opj_optarg;
259 strncpy(parameters->outfile, outfile, sizeof(parameters->outfile)-1);
263 /* ----------------------------------------------------- */
265 case 'h': /* display an help description */
266 decode_help_display();
269 /* ------------------------------------------------------ */
271 case 'y': /* Image Directory path */
273 img_fol->imgdirpath = (char*)malloc(strlen(opj_optarg) + 1);
274 strcpy(img_fol->imgdirpath,opj_optarg);
275 img_fol->set_imgdir=1;
279 /* ----------------------------------------------------- */
281 case 'd': /* Input decode ROI */
283 int size_optarg = (int)strlen(opj_optarg) + 1;
284 char *ROI_values = (char*) malloc(size_optarg);
285 ROI_values[0] = '\0';
286 strncpy(ROI_values, opj_optarg, strlen(opj_optarg));
287 ROI_values[strlen(opj_optarg)] = '\0';
288 /*printf("ROI_values = %s [%d / %d]\n", ROI_values, strlen(ROI_values), size_optarg ); */
289 parse_DA_values( ROI_values, ¶meters->DA_x0, ¶meters->DA_y0, ¶meters->DA_x1, ¶meters->DA_y1);
292 /* ----------------------------------------------------- */
294 case 'v': /* Verbose mode */
296 parameters->m_verbose = 1;
300 /* ----------------------------------------------------- */
302 fprintf(stderr,"WARNING -> this option is not valid \"-%c %s\"\n",c, opj_optarg);
307 /* check for possible errors */
308 if(img_fol->set_imgdir==1){
309 if(!(parameters->infile[0]==0)){
310 fprintf(stderr, "Error: options -ImgDir and -i cannot be used together !!\n");
313 if(img_fol->set_out_format == 0){
314 fprintf(stderr, "Error: When -ImgDir is used, -OutFor <FORMAT> must be used !!\n");
315 fprintf(stderr, "Only one format allowed! Valid format PGM, PPM, PNM, PGX, BMP, TIF, RAW and TGA!!\n");
318 if(!((parameters->outfile[0] == 0))){
319 fprintf(stderr, "Error: options -ImgDir and -o cannot be used together !!\n");
323 if((parameters->infile[0] == 0) ) {
324 fprintf(stderr, "Example: %s -i image.j2k\n",argv[0]);
325 fprintf(stderr, " Try: %s -h\n",argv[0]);
333 /* -------------------------------------------------------------------------- */
335 * Parse decoding area input values
338 /* -------------------------------------------------------------------------- */
339 int parse_DA_values( char* inArg, unsigned int *DA_x0, unsigned int *DA_y0, unsigned int *DA_x1, unsigned int *DA_y1)
345 result = strtok( inArg, delims );
347 while( (result != NULL) && (it < 4 ) ) {
348 values[it] = atoi(result);
349 result = strtok( NULL, delims );
357 *DA_x0 = values[0]; *DA_y0 = values[1];
358 *DA_x1 = values[2]; *DA_y1 = values[3];
364 /* -------------------------------------------------------------------------- */
368 /* -------------------------------------------------------------------------- */
369 int main(int argc, char *argv[])
371 FILE *fsrc = NULL, *fout = NULL;
373 opj_dparameters_t parameters; /* Decompression parameters */
374 opj_event_mgr_t event_mgr; /* Event manager */
375 opj_image_t* image = NULL; /* Image structure */
376 opj_codec_t* dinfo = NULL; /* Handle to a decompressor */
377 opj_stream_t *cio = NULL; /* Stream */
378 opj_codestream_info_v2_t* cstr_info = NULL;
379 opj_codestream_index_t* cstr_index = NULL;
381 OPJ_INT32 num_images, imageno;
383 dircnt_t *dirptr = NULL;
386 opj_bool l_go_on = OPJ_TRUE;
387 OPJ_UINT32 l_max_data_size = 1000;
388 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_initialize_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);
469 fprintf(stderr, "ERROR -> failed to create the stream from the file\n");
473 /* Read the JPEG2000 stream */
474 /* ------------------------ */
476 switch(parameters.decod_format) {
477 case J2K_CFMT: /* JPEG-2000 codestream */
479 /* Get a decoder handle */
480 dinfo = opj_create_decompress_v2(CODEC_J2K);
483 case JP2_CFMT: /* JPEG 2000 compressed image data */
485 /* Get a decoder handle */
486 dinfo = opj_create_decompress_v2(CODEC_JP2);
489 case JPT_CFMT: /* JPEG 2000, JPIP */
491 /* Get a decoder handle */
492 dinfo = opj_create_decompress_v2(CODEC_JPT);
496 fprintf(stderr, "skipping file..\n");
497 opj_stream_destroy(cio);
501 /* Setup the decoder decoding parameters using user parameters */
502 if ( !opj_setup_decoder_v2(dinfo, ¶meters, &event_mgr) ){
503 fprintf(stderr, "ERROR -> j2k_dump: failed to setup the decoder\n");
504 opj_stream_destroy(cio);
506 opj_destroy_codec(dinfo);
511 /* Read the main header of the codestream and if necessary the JP2 boxes*/
512 if(! opj_read_header(cio, dinfo, &image)){
513 fprintf(stderr, "ERROR -> j2k_dump: failed to read the header\n");
514 opj_stream_destroy(cio);
516 opj_destroy_codec(dinfo);
517 opj_image_destroy(image);
522 opj_dump_codec(dinfo, OPJ_IMG_INFO | OPJ_J2K_MH_INFO | OPJ_J2K_MH_IND, fout );
524 cstr_info = opj_get_cstr_info(dinfo);
526 cstr_index = opj_get_cstr_index(dinfo);
529 fprintf(stdout,"Setting decoding area to %d,%d,%d,%d\n",
530 parameters.DA_x0, parameters.DA_y0, parameters.DA_x1, parameters.DA_y1);
534 if (! opj_set_decode_area( dinfo,
535 parameters.DA_x0, parameters.DA_y0,
536 parameters.DA_x1, parameters.DA_y1)){
537 fprintf(stderr, "ERROR -> j2k_dump: failed to set the decoded area\n");
538 opj_stream_destroy(cio);
539 opj_destroy_codec(dinfo);
546 OPJ_INT32 l_current_tile_x0,l_current_tile_y0,l_current_tile_x1,l_current_tile_y1;
547 OPJ_UINT32 l_nb_comps, l_tile_index, l_data_size;
550 if (! opj_read_tile_header( dinfo,
561 fprintf(stderr, "ERROR -> j2k_dump: failed read the tile header\n");
562 opj_stream_destroy(cio);
564 opj_destroy_codec(dinfo);
570 if (l_data_size > l_max_data_size) {
572 l_data = (OPJ_BYTE *) realloc(l_data,l_data_size);
574 opj_stream_destroy(cio);
575 opj_destroy_codec(dinfo);
581 l_max_data_size = l_data_size;
584 if (! opj_decode_tile_data(dinfo,l_tile_index,l_data,l_data_size,cio))
587 opj_stream_destroy(cio);
588 opj_destroy_codec(dinfo);
593 /** now should inspect image to know the reduction factor and then how to behave with data */
599 /* close the byte stream */
600 opj_stream_destroy(cio);
603 /* free remaining structures */
605 opj_destroy_codec(dinfo);
608 /* destroy the image header */
609 opj_image_destroy(image);
611 /* destroy the codestream index */
612 opj_destroy_cstr_index(&cstr_index);
614 /* destroy the codestream info */
615 opj_destroy_cstr_info_v2(&cstr_info);
619 /* Close the output file */