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(const char *filename);
80 char get_next_file(int imageno,dircnt_t *dirptr,img_fol_t *img_fol, opj_dparameters_t *parameters);
81 static int infile_format(const char *fname);
83 int parse_cmdline_decoder(int argc, char **argv, opj_dparameters_t *parameters,img_fol_t *img_fol);
84 int parse_DA_values( char* inArg, unsigned int *DA_x0, unsigned int *DA_y0, unsigned int *DA_x1, unsigned int *DA_y1);
86 /* -------------------------------------------------------------------------- */
87 static void decode_help_display(void) {
88 fprintf(stdout,"HELP for opj_dump\n----\n\n");
89 fprintf(stdout,"- the -h option displays this help information on screen\n\n");
92 fprintf(stdout,"List of parameters for the JPEG 2000 "
100 fprintf(stdout," -ImgDir \n");
101 fprintf(stdout," Image file Directory path \n");
102 fprintf(stdout," -i <compressed file>\n");
103 fprintf(stdout," REQUIRED only if an Input image directory not specified\n");
104 fprintf(stdout," Currently accepts J2K-files, JP2-files and JPT-files. The file type\n");
105 fprintf(stdout," is identified based on its suffix.\n");
106 fprintf(stdout," -o <output file>\n");
107 fprintf(stdout," OPTIONAL\n");
108 fprintf(stdout," Output file where file info will be dump.\n");
109 fprintf(stdout," By default it will be in the stdout.\n");
110 fprintf(stdout," -v "); /* FIXME WIP_MSD */
111 fprintf(stdout," OPTIONAL\n");
112 fprintf(stdout," Activate or not the verbose mode (display info and warning message)\n");
113 fprintf(stdout," By default verbose mode is off.\n");
114 fprintf(stdout,"\n");
117 /* -------------------------------------------------------------------------- */
118 int get_num_images(char *imgdirpath){
120 struct dirent* content;
123 /*Reading the input images from given input directory*/
125 dir= opendir(imgdirpath);
127 fprintf(stderr,"Could not open Folder %s\n",imgdirpath);
131 while((content=readdir(dir))!=NULL){
132 if(strcmp(".",content->d_name)==0 || strcmp("..",content->d_name)==0 )
139 /* -------------------------------------------------------------------------- */
140 int load_images(dircnt_t *dirptr, char *imgdirpath){
142 struct dirent* content;
145 /*Reading the input images from given input directory*/
147 dir= opendir(imgdirpath);
149 fprintf(stderr,"Could not open Folder %s\n",imgdirpath);
152 fprintf(stderr,"Folder opened successfully\n");
155 while((content=readdir(dir))!=NULL){
156 if(strcmp(".",content->d_name)==0 || strcmp("..",content->d_name)==0 )
159 strcpy(dirptr->filename[i],content->d_name);
165 /* -------------------------------------------------------------------------- */
166 int get_file_format(const char *filename) {
168 static const char *extension[] = {"pgx", "pnm", "pgm", "ppm", "bmp","tif", "raw", "tga", "png", "j2k", "jp2", "jpt", "j2c", "jpc" };
169 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 };
170 char * ext = strrchr(filename, '.');
175 for(i = 0; i < sizeof(format)/sizeof(*format); i++) {
176 if(_strnicmp(ext, extension[i], 3) == 0) {
185 /* -------------------------------------------------------------------------- */
186 char get_next_file(int imageno,dircnt_t *dirptr,img_fol_t *img_fol, opj_dparameters_t *parameters){
187 char image_filename[OPJ_PATH_LEN], infilename[OPJ_PATH_LEN],outfilename[OPJ_PATH_LEN],temp_ofname[OPJ_PATH_LEN];
188 char *temp_p, temp1[OPJ_PATH_LEN]="";
190 strcpy(image_filename,dirptr->filename[imageno]);
191 fprintf(stderr,"File Number %d \"%s\"\n",imageno,image_filename);
192 parameters->decod_format = get_file_format(image_filename);
193 if (parameters->decod_format == -1)
195 sprintf(infilename,"%s/%s",img_fol->imgdirpath,image_filename);
196 strncpy(parameters->infile, infilename, sizeof(infilename));
199 strcpy(temp_ofname,strtok(image_filename,"."));
200 while((temp_p = strtok(NULL,".")) != NULL){
201 strcat(temp_ofname,temp1);
202 sprintf(temp1,".%s",temp_p);
204 if(img_fol->set_out_format==1){
205 sprintf(outfilename,"%s/%s.%s",img_fol->imgdirpath,temp_ofname,img_fol->out_format);
206 strncpy(parameters->outfile, outfilename, sizeof(outfilename));
211 /* -------------------------------------------------------------------------- */
212 #define JP2_RFC3745_MAGIC "\x00\x00\x00\x0c\x6a\x50\x20\x20\x0d\x0a\x87\x0a"
213 #define JP2_MAGIC "\x0d\x0a\x87\x0a"
214 /* position 45: "\xff\x52" */
215 #define J2K_CODESTREAM_MAGIC "\xff\x4f\xff\x51"
217 static int infile_format(const char *fname)
220 const char *s, *magic_s;
221 int ext_format, magic_format;
222 unsigned char buf[12];
223 unsigned int l_nb_read;
225 reader = fopen(fname, "rb");
231 l_nb_read = fread(buf, 1, 12, reader);
238 ext_format = get_file_format(fname);
240 if (ext_format == JPT_CFMT)
243 if (memcmp(buf, JP2_RFC3745_MAGIC, 12) == 0 || memcmp(buf, JP2_MAGIC, 4) == 0) {
244 magic_format = JP2_CFMT;
247 else if (memcmp(buf, J2K_CODESTREAM_MAGIC, 4) == 0) {
248 magic_format = J2K_CFMT;
249 magic_s = ".j2k or .jpc or .j2c";
254 if (magic_format == ext_format)
257 s = fname + strlen(fname) - 4;
259 fputs("\n===========================================\n", stderr);
260 fprintf(stderr, "The extension of this file is incorrect.\n"
261 "FOUND %s. SHOULD BE %s\n", s, magic_s);
262 fputs("===========================================\n", stderr);
266 /* -------------------------------------------------------------------------- */
268 * Parse the command line
270 /* -------------------------------------------------------------------------- */
271 int parse_cmdline_decoder(int argc, char **argv, opj_dparameters_t *parameters,img_fol_t *img_fol) {
273 opj_option_t long_option[]={
274 {"ImgDir",REQ_ARG, NULL ,'y'},
276 const char optlist[] = "i:o:hv";
278 totlen=sizeof(long_option);
279 img_fol->set_out_format = 0;
281 c = opj_getopt_long(argc, argv,optlist,long_option,totlen);
285 case 'i': /* input file */
287 char *infile = opj_optarg;
288 parameters->decod_format = infile_format(infile);
289 switch(parameters->decod_format) {
298 "!! Unrecognized format for infile : %s [accept only *.j2k, *.jp2, *.jpc or *.jpt] !!\n\n",
302 strncpy(parameters->infile, infile, sizeof(parameters->infile)-1);
306 /* ------------------------------------------------------ */
308 case 'o': /* output file */
310 char *outfile = opj_optarg;
311 strncpy(parameters->outfile, outfile, sizeof(parameters->outfile)-1);
315 /* ----------------------------------------------------- */
317 case 'h': /* display an help description */
318 decode_help_display();
321 /* ------------------------------------------------------ */
323 case 'y': /* Image Directory path */
325 img_fol->imgdirpath = (char*)malloc(strlen(opj_optarg) + 1);
326 strcpy(img_fol->imgdirpath,opj_optarg);
327 img_fol->set_imgdir=1;
331 /* ----------------------------------------------------- */
333 case 'v': /* Verbose mode */
335 parameters->m_verbose = 1;
339 /* ----------------------------------------------------- */
341 fprintf(stderr,"WARNING -> this option is not valid \"-%c %s\"\n",c, opj_optarg);
346 /* check for possible errors */
347 if(img_fol->set_imgdir==1){
348 if(!(parameters->infile[0]==0)){
349 fprintf(stderr, "Error: options -ImgDir and -i cannot be used together !!\n");
352 if(img_fol->set_out_format == 0){
353 fprintf(stderr, "Error: When -ImgDir is used, -OutFor <FORMAT> must be used !!\n");
354 fprintf(stderr, "Only one format allowed! Valid format PGM, PPM, PNM, PGX, BMP, TIF, RAW and TGA!!\n");
357 if(!(parameters->outfile[0] == 0)){
358 fprintf(stderr, "Error: options -ImgDir and -o cannot be used together !!\n");
362 if(parameters->infile[0] == 0) {
363 fprintf(stderr, "Example: %s -i image.j2k\n",argv[0]);
364 fprintf(stderr, " Try: %s -h\n",argv[0]);
372 /* -------------------------------------------------------------------------- */
375 sample error debug callback expecting no client object
377 static void error_callback(const char *msg, void *client_data) {
379 fprintf(stdout, "[ERROR] %s", msg);
382 sample warning debug callback expecting no client object
384 static void warning_callback(const char *msg, void *client_data) {
386 fprintf(stdout, "[WARNING] %s", msg);
389 sample debug callback expecting no client object
391 static void info_callback(const char *msg, void *client_data) {
393 fprintf(stdout, "[INFO] %s", msg);
396 /* -------------------------------------------------------------------------- */
400 /* -------------------------------------------------------------------------- */
401 int main(int argc, char *argv[])
403 FILE *fsrc = NULL, *fout = NULL;
405 opj_dparameters_t parameters; /* Decompression parameters */
406 opj_image_t* image = NULL; /* Image structure */
407 opj_codec_t* l_codec = NULL; /* Handle to a decompressor */
408 opj_stream_t *l_stream = NULL; /* Stream */
409 opj_codestream_info_v2_t* cstr_info = NULL;
410 opj_codestream_index_t* cstr_index = NULL;
412 OPJ_INT32 num_images, imageno;
414 dircnt_t *dirptr = NULL;
417 OPJ_BOOL l_go_on = OPJ_TRUE;
418 OPJ_UINT32 l_max_data_size = 1000;
419 OPJ_BYTE * l_data = (OPJ_BYTE *) malloc(1000);
422 /* Set decoding parameters to default values */
423 opj_set_default_decoder_parameters(¶meters);
425 /* Initialize img_fol */
426 memset(&img_fol,0,sizeof(img_fol_t));
428 /* Parse input and get user encoding parameters */
429 if(parse_cmdline_decoder(argc, argv, ¶meters,&img_fol) == 1) {
433 /* Initialize reading of directory */
434 if(img_fol.set_imgdir==1){
436 num_images=get_num_images(img_fol.imgdirpath);
438 dirptr=(dircnt_t*)malloc(sizeof(dircnt_t));
440 dirptr->filename_buf = (char*)malloc(num_images*OPJ_PATH_LEN*sizeof(char)); /* Stores at max 10 image file names*/
441 dirptr->filename = (char**) malloc(num_images*sizeof(char*));
443 if(!dirptr->filename_buf){
447 for(it_image=0;it_image<num_images;it_image++){
448 dirptr->filename[it_image] = dirptr->filename_buf + it_image*OPJ_PATH_LEN;
451 if(load_images(dirptr,img_fol.imgdirpath)==1){
456 fprintf(stdout,"Folder is empty\n");
463 /* Try to open for writing the output file if necessary */
464 if (parameters.outfile[0] != 0){
465 fout = fopen(parameters.outfile,"w");
467 fprintf(stderr, "ERROR -> failed to open %s for writing\n", parameters.outfile);
474 /* Read the header of each image one by one */
475 for(imageno = 0; imageno < num_images ; imageno++){
477 fprintf(stderr,"\n");
479 if(img_fol.set_imgdir==1){
480 if (get_next_file(imageno, dirptr,&img_fol, ¶meters)) {
481 fprintf(stderr,"skipping file...\n");
486 /* Read the input file and put it in memory */
487 /* ---------------------------------------- */
488 fsrc = fopen(parameters.infile, "rb");
490 fprintf(stderr, "ERROR -> failed to open %s for reading\n", parameters.infile);
494 l_stream = opj_stream_create_default_file_stream(fsrc,1);
497 fprintf(stderr, "ERROR -> failed to create the stream from the file\n");
501 /* Read the JPEG2000 stream */
502 /* ------------------------ */
504 switch(parameters.decod_format) {
505 case J2K_CFMT: /* JPEG-2000 codestream */
507 /* Get a decoder handle */
508 l_codec = opj_create_decompress(OPJ_CODEC_J2K);
511 case JP2_CFMT: /* JPEG 2000 compressed image data */
513 /* Get a decoder handle */
514 l_codec = opj_create_decompress(OPJ_CODEC_JP2);
517 case JPT_CFMT: /* JPEG 2000, JPIP */
519 /* Get a decoder handle */
520 l_codec = opj_create_decompress(OPJ_CODEC_JPT);
524 fprintf(stderr, "skipping file..\n");
525 opj_stream_destroy(l_stream);
529 /* catch events using our callbacks and give a local context */
530 opj_set_info_handler(l_codec, info_callback,00);
531 opj_set_warning_handler(l_codec, warning_callback,00);
532 opj_set_error_handler(l_codec, error_callback,00);
534 /* Setup the decoder decoding parameters using user parameters */
535 if ( !opj_setup_decoder(l_codec, ¶meters) ){
536 fprintf(stderr, "ERROR -> opj_dump: failed to setup the decoder\n");
537 opj_stream_destroy(l_stream);
539 opj_destroy_codec(l_codec);
544 /* Read the main header of the codestream and if necessary the JP2 boxes*/
545 if(! opj_read_header(l_stream, l_codec, &image)){
546 fprintf(stderr, "ERROR -> opj_dump: failed to read the header\n");
547 opj_stream_destroy(l_stream);
549 opj_destroy_codec(l_codec);
550 opj_image_destroy(image);
555 opj_dump_codec(l_codec, OPJ_IMG_INFO | OPJ_J2K_MH_INFO | OPJ_J2K_MH_IND, fout );
557 cstr_info = opj_get_cstr_info(l_codec);
559 cstr_index = opj_get_cstr_index(l_codec);
561 /* close the byte stream */
562 opj_stream_destroy(l_stream);
565 /* free remaining structures */
567 opj_destroy_codec(l_codec);
570 /* destroy the image header */
571 opj_image_destroy(image);
573 /* destroy the codestream index */
574 opj_destroy_cstr_index(&cstr_index);
576 /* destroy the codestream info */
577 opj_destroy_cstr_info(&cstr_info);
581 /* Close the output file */