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 /* -------------------------------------------------------------------------- */
80 static void j2k_dump_image(FILE *fd, opj_image_header_t * img);
81 static void j2k_dump_cp(FILE *fd, opj_image_t * img, opj_cp_v2_t * cp);
83 int get_num_images(char *imgdirpath);
84 int load_images(dircnt_t *dirptr, char *imgdirpath);
85 int get_file_format(char *filename);
86 char get_next_file(int imageno,dircnt_t *dirptr,img_fol_t *img_fol, opj_dparameters_t *parameters);
88 int parse_cmdline_decoder(int argc, char **argv, opj_dparameters_t *parameters,img_fol_t *img_fol);
89 int parse_DA_values( char* inArg, unsigned int *DA_x0, unsigned int *DA_y0, unsigned int *DA_x1, unsigned int *DA_y1);
91 /* -------------------------------------------------------------------------- */
92 void decode_help_display(void) {
93 fprintf(stdout,"HELP for j2k_dump\n----\n\n");
94 fprintf(stdout,"- the -h option displays this help information on screen\n\n");
97 fprintf(stdout,"List of parameters for the JPEG 2000 "
100 #endif /* USE_JPWL */
103 fprintf(stdout,"\n");
104 fprintf(stdout,"\n");
105 fprintf(stdout," -ImgDir \n");
106 fprintf(stdout," Image file Directory path \n");
107 fprintf(stdout," -i <compressed file>\n");
108 fprintf(stdout," REQUIRED only if an Input image directory not specified\n");
109 fprintf(stdout," Currently accepts J2K-files, JP2-files and JPT-files. The file type\n");
110 fprintf(stdout," is identified based on its suffix.\n");
111 fprintf(stdout," -o <output file>\n");
112 fprintf(stdout," OPTIONAL\n");
113 fprintf(stdout," Output file where file info will be dump.\n");
114 fprintf(stdout," By default it will be in the stdout.\n");
115 fprintf(stdout," -d <x0,x1,y0,y1>\n"); /* FIXME WIP_MSD */
116 fprintf(stdout," OPTIONAL\n");
117 fprintf(stdout," Decoding area\n");
118 fprintf(stdout," By default all tiles header are read.\n");
119 fprintf(stdout," -v "); /* FIXME WIP_MSD */
120 fprintf(stdout," OPTIONAL\n");
121 fprintf(stdout," Activate or not the verbose mode (display info and warning message)\n");
122 fprintf(stdout," By default verbose mode is off.\n");
123 fprintf(stdout,"\n");
126 /* -------------------------------------------------------------------------- */
127 int get_num_images(char *imgdirpath){
129 struct dirent* content;
132 /*Reading the input images from given input directory*/
134 dir= opendir(imgdirpath);
136 fprintf(stderr,"Could not open Folder %s\n",imgdirpath);
140 while((content=readdir(dir))!=NULL){
141 if(strcmp(".",content->d_name)==0 || strcmp("..",content->d_name)==0 )
148 /* -------------------------------------------------------------------------- */
149 int load_images(dircnt_t *dirptr, char *imgdirpath){
151 struct dirent* content;
154 /*Reading the input images from given input directory*/
156 dir= opendir(imgdirpath);
158 fprintf(stderr,"Could not open Folder %s\n",imgdirpath);
161 fprintf(stderr,"Folder opened successfully\n");
164 while((content=readdir(dir))!=NULL){
165 if(strcmp(".",content->d_name)==0 || strcmp("..",content->d_name)==0 )
168 strcpy(dirptr->filename[i],content->d_name);
174 /* -------------------------------------------------------------------------- */
175 int get_file_format(char *filename) {
177 static const char *extension[] = {"pgx", "pnm", "pgm", "ppm", "bmp","tif", "raw", "tga", "png", "j2k", "jp2", "jpt", "j2c", "jpc" };
178 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 };
179 char * ext = strrchr(filename, '.');
184 for(i = 0; i < sizeof(format)/sizeof(*format); i++) {
185 if(_strnicmp(ext, extension[i], 3) == 0) {
194 /* -------------------------------------------------------------------------- */
195 char get_next_file(int imageno,dircnt_t *dirptr,img_fol_t *img_fol, opj_dparameters_t *parameters){
196 char image_filename[OPJ_PATH_LEN], infilename[OPJ_PATH_LEN],outfilename[OPJ_PATH_LEN],temp_ofname[OPJ_PATH_LEN];
197 char *temp_p, temp1[OPJ_PATH_LEN]="";
199 strcpy(image_filename,dirptr->filename[imageno]);
200 fprintf(stderr,"File Number %d \"%s\"\n",imageno,image_filename);
201 parameters->decod_format = get_file_format(image_filename);
202 if (parameters->decod_format == -1)
204 sprintf(infilename,"%s/%s",img_fol->imgdirpath,image_filename);
205 strncpy(parameters->infile, infilename, sizeof(infilename));
207 /* Set output file */
208 strcpy(temp_ofname,strtok(image_filename,"."));
209 while((temp_p = strtok(NULL,".")) != NULL){
210 strcat(temp_ofname,temp1);
211 sprintf(temp1,".%s",temp_p);
213 if(img_fol->set_out_format==1){
214 sprintf(outfilename,"%s/%s.%s",img_fol->imgdirpath,temp_ofname,img_fol->out_format);
215 strncpy(parameters->outfile, outfilename, sizeof(outfilename));
220 /* -------------------------------------------------------------------------- */
222 * Parse the command line
224 /* -------------------------------------------------------------------------- */
225 int parse_cmdline_decoder(int argc, char **argv, opj_dparameters_t *parameters,img_fol_t *img_fol) {
227 opj_option_t long_option[]={
228 {"ImgDir",REQ_ARG, NULL ,'y'},
230 const char optlist[] = "i:o:d:hv";
232 totlen=sizeof(long_option);
233 img_fol->set_out_format = 0;
235 c = opj_getopt_long(argc, argv,optlist,long_option,totlen);
239 case 'i': /* input file */
241 char *infile = opj_optarg;
242 parameters->decod_format = get_file_format(infile);
243 switch(parameters->decod_format) {
252 "!! Unrecognized format for infile : %s [accept only *.j2k, *.jp2, *.jpc or *.jpt] !!\n\n",
256 strncpy(parameters->infile, infile, sizeof(parameters->infile)-1);
260 /* ------------------------------------------------------ */
262 case 'o': /* output file */
264 char *outfile = opj_optarg;
265 strncpy(parameters->outfile, outfile, sizeof(parameters->outfile)-1);
269 /* ----------------------------------------------------- */
271 case 'h': /* display an help description */
272 decode_help_display();
275 /* ------------------------------------------------------ */
277 case 'y': /* Image Directory path */
279 img_fol->imgdirpath = (char*)malloc(strlen(opj_optarg) + 1);
280 strcpy(img_fol->imgdirpath,opj_optarg);
281 img_fol->set_imgdir=1;
285 /* ----------------------------------------------------- */
287 case 'd': /* Input decode ROI */
289 int size_optarg = (int)strlen(opj_optarg) + 1;
290 char *ROI_values = (char*) malloc(size_optarg);
291 ROI_values[0] = '\0';
292 strncpy(ROI_values, opj_optarg, strlen(opj_optarg));
293 ROI_values[strlen(opj_optarg)] = '\0';
294 /*printf("ROI_values = %s [%d / %d]\n", ROI_values, strlen(ROI_values), size_optarg ); */
295 parse_DA_values( ROI_values, ¶meters->DA_x0, ¶meters->DA_y0, ¶meters->DA_x1, ¶meters->DA_y1);
298 /* ----------------------------------------------------- */
300 case 'v': /* Verbose mode */
302 parameters->m_verbose = 1;
306 /* ----------------------------------------------------- */
308 fprintf(stderr,"WARNING -> this option is not valid \"-%c %s\"\n",c, opj_optarg);
313 /* check for possible errors */
314 if(img_fol->set_imgdir==1){
315 if(!(parameters->infile[0]==0)){
316 fprintf(stderr, "Error: options -ImgDir and -i cannot be used together !!\n");
319 if(img_fol->set_out_format == 0){
320 fprintf(stderr, "Error: When -ImgDir is used, -OutFor <FORMAT> must be used !!\n");
321 fprintf(stderr, "Only one format allowed! Valid format PGM, PPM, PNM, PGX, BMP, TIF, RAW and TGA!!\n");
324 if(!((parameters->outfile[0] == 0))){
325 fprintf(stderr, "Error: options -ImgDir and -o cannot be used together !!\n");
329 if((parameters->infile[0] == 0) ) {
330 fprintf(stderr, "Example: %s -i image.j2k\n",argv[0]);
331 fprintf(stderr, " Try: %s -h\n",argv[0]);
339 /* -------------------------------------------------------------------------- */
341 * Parse decoding area input values
344 /* -------------------------------------------------------------------------- */
345 int parse_DA_values( char* inArg, unsigned int *DA_x0, unsigned int *DA_y0, unsigned int *DA_x1, unsigned int *DA_y1)
351 result = strtok( inArg, delims );
353 while( (result != NULL) && (it < 4 ) ) {
354 values[it] = atoi(result);
355 result = strtok( NULL, delims );
363 *DA_x0 = values[0]; *DA_y0 = values[1];
364 *DA_x1 = values[2]; *DA_y1 = values[3];
370 /* -------------------------------------------------------------------------- */
374 /* -------------------------------------------------------------------------- */
375 int main(int argc, char *argv[])
377 FILE *fsrc = NULL, *fout = NULL;
379 opj_dparameters_t parameters; /* Decompression parameters */
380 opj_event_mgr_t event_mgr; /* Event manager */
381 opj_file_info_t file_info; /* File info structure */
382 opj_codec_t* dinfo = NULL; /* Handle to a decompressor */
383 opj_stream_t *cio = NULL; /* Stream */
385 OPJ_INT32 num_images, imageno;
387 dircnt_t *dirptr = NULL;
389 opj_bool l_go_on = OPJ_TRUE;
390 OPJ_UINT32 l_max_data_size = 1000;
391 OPJ_BYTE * l_data = (OPJ_BYTE *) malloc(1000);
393 /* Set decoding parameters to default values */
394 opj_set_default_decoder_parameters(¶meters);
396 /* Initialize img_fol */
397 memset(&img_fol,0,sizeof(img_fol_t));
399 /* Parse input and get user encoding parameters */
400 if(parse_cmdline_decoder(argc, argv, ¶meters,&img_fol) == 1) {
404 /* Set default event mgr */
405 opj_set_default_event_handler(&event_mgr, parameters.m_verbose);
407 /* Initialize reading of directory */
408 if(img_fol.set_imgdir==1){
410 num_images=get_num_images(img_fol.imgdirpath);
412 dirptr=(dircnt_t*)malloc(sizeof(dircnt_t));
414 dirptr->filename_buf = (char*)malloc(num_images*OPJ_PATH_LEN*sizeof(char)); /* Stores at max 10 image file names */
415 dirptr->filename = (char**) malloc(num_images*sizeof(char*));
417 if(!dirptr->filename_buf){
421 for(it_image=0;it_image<num_images;it_image++){
422 dirptr->filename[it_image] = dirptr->filename_buf + it_image*OPJ_PATH_LEN;
425 if(load_images(dirptr,img_fol.imgdirpath)==1){
430 fprintf(stdout,"Folder is empty\n");
437 /* Try to open for writing the output file if necessary */
438 if (parameters.outfile[0] != 0){
439 fout = fopen(parameters.outfile,"w");
441 fprintf(stderr, "ERROR -> failed to open %s for writing\n", parameters.outfile);
448 /* Read the header of each image one by one */
449 for(imageno = 0; imageno < num_images ; imageno++){
451 fprintf(stderr,"\n");
453 if(img_fol.set_imgdir==1){
454 if (get_next_file(imageno, dirptr,&img_fol, ¶meters)) {
455 fprintf(stderr,"skipping file...\n");
460 /* Read the input file and put it in memory */
461 /* ---------------------------------------- */
462 fsrc = fopen(parameters.infile, "rb");
464 fprintf(stderr, "ERROR -> failed to open %s for reading\n", parameters.infile);
468 cio = opj_stream_create_default_file_stream(fsrc,1);
470 fprintf(stderr, "ERROR -> failed to create the stream from the file\n");
474 /* Read the JPEG2000 stream */
475 /* ------------------------ */
477 switch(parameters.decod_format) {
478 case J2K_CFMT: /* JPEG-2000 codestream */
480 /* Get a decoder handle */
481 dinfo = opj_create_decompress_v2(CODEC_J2K);
484 case JP2_CFMT: /* JPEG 2000 compressed image data */
486 /* Get a decoder handle */
487 dinfo = opj_create_decompress_v2(CODEC_JP2);
490 case JPT_CFMT: /* JPEG 2000, JPIP */
492 /* Get a decoder handle */
493 dinfo = opj_create_decompress_v2(CODEC_JPT);
497 fprintf(stderr, "skipping file..\n");
498 opj_stream_destroy(cio);
502 /* Setup the decoder decoding parameters using user parameters */
503 if ( !opj_setup_decoder_v2(dinfo, ¶meters, &event_mgr) ){
504 fprintf(stderr, "ERROR -> j2k_dump: failed to setup the decoder\n");
505 opj_stream_destroy(cio);
507 opj_destroy_codec(dinfo);
512 /* Read the main header of the codestream and if necessary the JP2 boxes*/
513 if(! opj_read_header(cio, dinfo, &file_info, OPJ_IMG_INFO | OPJ_J2K_INFO)){
514 fprintf(stderr, "ERROR -> j2k_dump: failed to read the header\n");
515 opj_stream_destroy(cio);
517 opj_destroy_codec(dinfo);
522 fprintf(stdout,"Setting decoding area to %d,%d,%d,%d\n",
523 parameters.DA_x0, parameters.DA_y0, parameters.DA_x1, parameters.DA_y1);
526 if (! opj_set_decode_area( dinfo,
527 parameters.DA_x0, parameters.DA_y0,
528 parameters.DA_x1, parameters.DA_y1)){
529 fprintf(stderr, "ERROR -> j2k_dump: failed to set the decoded area\n");
530 opj_stream_destroy(cio);
531 opj_destroy_codec(dinfo);
538 OPJ_INT32 l_current_tile_x0,l_current_tile_y0,l_current_tile_x1,l_current_tile_y1;
539 OPJ_UINT32 l_nb_comps, l_tile_index, l_data_size;
542 if (! opj_read_tile_header( dinfo,
553 fprintf(stderr, "ERROR -> j2k_dump: failed read the tile header\n");
554 opj_stream_destroy(cio);
556 opj_destroy_codec(dinfo);
562 if (l_data_size > l_max_data_size) {
564 l_data = (OPJ_BYTE *) realloc(l_data,l_data_size);
566 opj_stream_destroy(cio);
567 opj_destroy_codec(dinfo);
573 l_max_data_size = l_data_size;
576 if (! opj_decode_tile_data(dinfo,l_tile_index,l_data,l_data_size,cio))
579 opj_stream_destroy(cio);
580 opj_destroy_codec(dinfo);
585 /** now should inspect image to know the reduction factor and then how to behave with data */
590 /* Dump file informations from header */
591 dump_file_info(fout, &file_info);
593 /* close the byte stream */
594 opj_stream_destroy(cio);
597 /* free remaining structures */
599 opj_destroy_codec(dinfo);
604 /* Close the output file */
611 static void j2k_dump_image(FILE *fd, opj_image_header_t * img) {
613 fprintf(fd, "image {\n");
614 fprintf(fd, " x0=%d, y0=%d, x1=%d, y1=%d\n", img->x0, img->y0, img->x1, img->y1);
615 fprintf(fd, " numcomps=%d\n", img->numcomps);
616 for (compno = 0; compno < img->numcomps; compno++) {
617 opj_image_comp_header_t *comp = &img->comps[compno];
618 fprintf(fd, " comp %d {\n", compno);
619 fprintf(fd, " dx=%d, dy=%d\n", comp->dx, comp->dy);
620 fprintf(fd, " prec=%d\n", comp->prec);
621 /* fprintf(fd, " bpp=%d\n", comp->bpp); */
622 fprintf(fd, " sgnd=%d\n", comp->sgnd);
625 //fprintf(fd, " XTOsiz=%d, YTOsiz=%d, XTsiz=%d, YTsiz=%d\n", img->tile_x0, img->tile_y0, img->tile_width, img->tile_height);
626 //fprintf(fd, " Nb of tiles in x direction=%d, Nb of tiles in y direction=%d\n", img->nb_tiles_x, img->nb_tiles_y);
630 static void j2k_dump_cp(FILE *fd, opj_image_t * img, opj_cp_v2_t * cp) {
631 int tileno, compno, layno, bandno, resno, numbands;
632 fprintf(fd, "coding parameters {\n");
633 fprintf(fd, " tx0=%d, ty0=%d\n", cp->tx0, cp->ty0);
634 fprintf(fd, " tdx=%d, tdy=%d\n", cp->tdx, cp->tdy);
635 fprintf(fd, " tw=%d, th=%d\n", cp->tw, cp->th);
636 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
637 opj_tcp_v2_t *tcp = &cp->tcps[tileno];
638 fprintf(fd, " tile %d {\n", tileno);
639 fprintf(fd, " csty=%x\n", tcp->csty);
640 fprintf(fd, " prg=%d\n", tcp->prg);
641 fprintf(fd, " numlayers=%d\n", tcp->numlayers);
642 fprintf(fd, " mct=%d\n", tcp->mct);
643 fprintf(fd, " rates=");
644 for (layno = 0; layno < tcp->numlayers; layno++) {
645 fprintf(fd, "%.1f ", tcp->rates[layno]);
648 for (compno = 0; compno < img->numcomps; compno++) {
649 opj_tccp_t *tccp = &tcp->tccps[compno];
650 fprintf(fd, " comp %d {\n", compno);
651 fprintf(fd, " csty=%x\n", tccp->csty);
652 fprintf(fd, " numresolutions=%d\n", tccp->numresolutions);
653 fprintf(fd, " cblkw=%d\n", tccp->cblkw);
654 fprintf(fd, " cblkh=%d\n", tccp->cblkh);
655 fprintf(fd, " cblksty=%x\n", tccp->cblksty);
656 fprintf(fd, " qmfbid=%d\n", tccp->qmfbid);
657 fprintf(fd, " qntsty=%d\n", tccp->qntsty);
658 fprintf(fd, " numgbits=%d\n", tccp->numgbits);
659 fprintf(fd, " roishift=%d\n", tccp->roishift);
660 fprintf(fd, " stepsizes=");
661 numbands = tccp->qntsty == J2K_CCP_QNTSTY_SIQNT ? 1 : tccp->numresolutions * 3 - 2;
662 for (bandno = 0; bandno < numbands; bandno++) {
663 fprintf(fd, "(%d,%d) ", tccp->stepsizes[bandno].mant,
664 tccp->stepsizes[bandno].expn);
668 if (tccp->csty & J2K_CCP_CSTY_PRT) {
669 fprintf(fd, " prcw=");
670 for (resno = 0; resno < tccp->numresolutions; resno++) {
671 fprintf(fd, "%d ", tccp->prcw[resno]);
674 fprintf(fd, " prch=");
675 for (resno = 0; resno < tccp->numresolutions; resno++) {
676 fprintf(fd, "%d ", tccp->prch[resno]);
681 } /*end of component*/