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.
34 #include "windirent.h"
43 #define _stricmp strcasecmp
44 #define _strnicmp strncasecmp
47 #include "opj_config.h"
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 void decode_help_display(void) {
78 fprintf(stdout,"HELP for j2k_dump\n----\n\n");
79 fprintf(stdout,"- the -h option displays this help information on screen\n\n");
82 fprintf(stdout,"List of parameters for the JPEG 2000 "
90 fprintf(stdout," -ImgDir \n");
91 fprintf(stdout," Image file Directory path \n");
92 fprintf(stdout," -i <compressed file>\n");
93 fprintf(stdout," REQUIRED only if an Input image directory not specified\n");
94 fprintf(stdout," Currently accepts J2K-files, JP2-files and JPT-files. The file type\n");
95 fprintf(stdout," is identified based on its suffix.\n");
96 fprintf(stdout," -o <output file>\n");
97 fprintf(stdout," OPTIONAL\n");
98 fprintf(stdout," Output file where file info will be dump.\n");
99 fprintf(stdout," By default it will be in the stdout.\n");
100 fprintf(stdout,"\n");
103 /* -------------------------------------------------------------------------- */
104 static void j2k_dump_image(FILE *fd, opj_image_header_t * img);
105 static void j2k_dump_cp(FILE *fd, opj_image_t * img, opj_cp_v2_t * cp);
107 int get_num_images(char *imgdirpath){
109 struct dirent* content;
112 /*Reading the input images from given input directory*/
114 dir= opendir(imgdirpath);
116 fprintf(stderr,"Could not open Folder %s\n",imgdirpath);
120 while((content=readdir(dir))!=NULL){
121 if(strcmp(".",content->d_name)==0 || strcmp("..",content->d_name)==0 )
128 int load_images(dircnt_t *dirptr, char *imgdirpath){
130 struct dirent* content;
133 /*Reading the input images from given input directory*/
135 dir= opendir(imgdirpath);
137 fprintf(stderr,"Could not open Folder %s\n",imgdirpath);
140 fprintf(stderr,"Folder opened successfully\n");
143 while((content=readdir(dir))!=NULL){
144 if(strcmp(".",content->d_name)==0 || strcmp("..",content->d_name)==0 )
147 strcpy(dirptr->filename[i],content->d_name);
153 int get_file_format(char *filename) {
155 static const char *extension[] = {"pgx", "pnm", "pgm", "ppm", "bmp","tif", "raw", "tga", "png", "j2k", "jp2", "jpt", "j2c", "jpc" };
156 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 };
157 char * ext = strrchr(filename, '.');
162 for(i = 0; i < sizeof(format)/sizeof(*format); i++) {
163 if(_strnicmp(ext, extension[i], 3) == 0) {
172 char get_next_file(int imageno,dircnt_t *dirptr,img_fol_t *img_fol, opj_dparameters_t *parameters){
173 char image_filename[OPJ_PATH_LEN], infilename[OPJ_PATH_LEN],outfilename[OPJ_PATH_LEN],temp_ofname[OPJ_PATH_LEN];
174 char *temp_p, temp1[OPJ_PATH_LEN]="";
176 strcpy(image_filename,dirptr->filename[imageno]);
177 fprintf(stderr,"File Number %d \"%s\"\n",imageno,image_filename);
178 parameters->decod_format = get_file_format(image_filename);
179 if (parameters->decod_format == -1)
181 sprintf(infilename,"%s/%s",img_fol->imgdirpath,image_filename);
182 strncpy(parameters->infile, infilename, sizeof(infilename));
184 /* Set output file */
185 strcpy(temp_ofname,strtok(image_filename,"."));
186 while((temp_p = strtok(NULL,".")) != NULL){
187 strcat(temp_ofname,temp1);
188 sprintf(temp1,".%s",temp_p);
190 if(img_fol->set_out_format==1){
191 sprintf(outfilename,"%s/%s.%s",img_fol->imgdirpath,temp_ofname,img_fol->out_format);
192 strncpy(parameters->outfile, outfilename, sizeof(outfilename));
197 /* -------------------------------------------------------------------------- */
198 int parse_cmdline_decoder(int argc, char **argv, opj_dparameters_t *parameters,img_fol_t *img_fol) {
199 /* parse the command line */
201 opj_option_t long_option[]={
202 {"ImgDir",REQ_ARG, NULL ,'y'},
204 const char optlist[] = "i:o:d:h";
206 totlen=sizeof(long_option);
207 img_fol->set_out_format = 0;
209 c = opj_getopt_long(argc, argv,optlist,long_option,totlen);
213 case 'i': /* input file */
215 char *infile = opj_optarg;
216 parameters->decod_format = get_file_format(infile);
217 switch(parameters->decod_format) {
226 "!! Unrecognized format for infile : %s [accept only *.j2k, *.jp2, *.jpc or *.jpt] !!\n\n",
230 strncpy(parameters->infile, infile, sizeof(parameters->infile)-1);
234 /* ------------------------------------------------------ */
236 case 'o': /* output file */
238 char *outfile = opj_optarg;
239 strncpy(parameters->outfile, outfile, sizeof(parameters->outfile)-1);
243 /* ----------------------------------------------------- */
245 case 'h': /* display an help description */
246 decode_help_display();
249 /* ------------------------------------------------------ */
251 case 'y': /* Image Directory path */
253 img_fol->imgdirpath = (char*)malloc(strlen(opj_optarg) + 1);
254 strcpy(img_fol->imgdirpath,opj_optarg);
255 img_fol->set_imgdir=1;
259 /* ----------------------------------------------------- */
260 case 'd': /* Input decode ROI */
262 int size_optarg = (int)strlen(optarg) + 1;
263 char *ROI_values = (char*) malloc(size_optarg);
264 ROI_values[0] = '\0';
265 strncpy(ROI_values, optarg, strlen(optarg));
266 ROI_values[strlen(optarg)] = '\0';
267 printf("ROI_values = %s [%d / %d]\n", ROI_values, strlen(ROI_values), size_optarg );
268 parse_ROI_values( ROI_values, ¶meters->ROI_x0, ¶meters->ROI_y0, ¶meters->ROI_x1, ¶meters->ROI_y1);
272 /* ----------------------------------------------------- */
274 fprintf(stderr,"WARNING -> this option is not valid \"-%c %s\"\n",c, opj_optarg);
279 /* check for possible errors */
280 if(img_fol->set_imgdir==1){
281 if(!(parameters->infile[0]==0)){
282 fprintf(stderr, "Error: options -ImgDir and -i cannot be used together !!\n");
285 if(img_fol->set_out_format == 0){
286 fprintf(stderr, "Error: When -ImgDir is used, -OutFor <FORMAT> must be used !!\n");
287 fprintf(stderr, "Only one format allowed! Valid format PGM, PPM, PNM, PGX, BMP, TIF, RAW and TGA!!\n");
290 if(!((parameters->outfile[0] == 0))){
291 fprintf(stderr, "Error: options -ImgDir and -o cannot be used together !!\n");
295 if((parameters->infile[0] == 0) ) {
296 fprintf(stderr, "Example: %s -i image.j2k\n",argv[0]);
297 fprintf(stderr, " Try: %s -h\n",argv[0]);
305 /*******************************************************************************
306 * Parse ROI input values
308 *******************************************************************************/
309 int parse_ROI_values( char* inArg, unsigned int *ROI_x0, unsigned int *ROI_y0, unsigned int *ROI_x1, unsigned int *ROI_y1)
315 result = strtok( inArg, delims );
317 while( (result != NULL) && (it < 4 ) ) {
318 values[it] = atoi(result);
319 result = strtok( NULL, delims );
327 *ROI_x0 = values[0]; *ROI_y0 = values[1];
328 *ROI_x1 = values[2]; *ROI_y1 = values[3];
333 /* -------------------------------------------------------------------------- */
336 sample error callback expecting a FILE* client object
338 void error_callback(const char *msg, void *client_data) {
339 FILE *stream = (FILE*)client_data;
340 fprintf(stream, "[ERROR] %s", msg);
343 sample warning callback expecting a FILE* client object
345 void warning_callback(const char *msg, void *client_data) {
346 FILE *stream = (FILE*)client_data;
347 fprintf(stream, "[WARNING] %s", msg);
350 sample debug callback expecting no client object
352 void info_callback(const char *msg, void *client_data) {
354 fprintf(stdout, "[INFO] %s", msg);
357 /* -------------------------------------------------------------------------- */
359 int main(int argc, char *argv[])
362 opj_dparameters_t parameters; /* decompression parameters */
364 opj_event_mgr_t event_mgr; /* event manager */
365 opj_image_header_t* image = NULL;
366 opj_file_info_t file_info;
367 FILE *fsrc = NULL, *fout = NULL;
370 dircnt_t *dirptr = NULL;
371 opj_codec_t* dinfo = NULL; /* handle to a decompressor */
372 opj_stream_t *cio = NULL;
373 opj_codestream_info_t* cstr_info =NULL; /* Codestream information structure */
374 /* OPJ_INT32 l_tile_x0,l_tile_y0; */
375 /* OPJ_UINT32 l_tile_width,l_tile_height,l_nb_tiles_x,l_nb_tiles_y; */
378 /* FIXME configure the event callbacks (not required) */
379 memset(&event_mgr, 0, sizeof(opj_event_mgr_t));
380 event_mgr.error_handler = error_callback;
381 event_mgr.warning_handler = warning_callback;
382 event_mgr.info_handler = info_callback;
383 event_mgr.client_data = stderr;
385 /* set decoding parameters to default values */
386 opj_set_default_decoder_parameters(¶meters);
388 /* Initialize img_fol */
389 memset(&img_fol,0,sizeof(img_fol_t));
391 /* parse input and get user encoding parameters */
392 if(parse_cmdline_decoder(argc, argv, ¶meters,&img_fol) == 1) {
396 /* Initialize reading of directory */
397 if(img_fol.set_imgdir==1){
398 num_images=get_num_images(img_fol.imgdirpath);
400 dirptr=(dircnt_t*)malloc(sizeof(dircnt_t));
402 dirptr->filename_buf = (char*)malloc(num_images*OPJ_PATH_LEN*sizeof(char)); /* Stores at max 10 image file names */
403 dirptr->filename = (char**) malloc(num_images*sizeof(char*));
405 if(!dirptr->filename_buf){
409 for(i=0;i<num_images;i++){
410 dirptr->filename[i] = dirptr->filename_buf + i*OPJ_PATH_LEN;
413 if(load_images(dirptr,img_fol.imgdirpath)==1){
418 fprintf(stdout,"Folder is empty\n");
425 /* Try to open for writing the output file if necessary */
426 if (parameters.outfile[0] != 0){
427 fout = fopen(parameters.outfile,"w");
429 fprintf(stderr, "ERROR -> failed to open %s for writing\n", parameters.outfile);
436 /* Read the header of each image one by one */
437 for(imageno = 0; imageno < num_images ; imageno++){
439 fprintf(stderr,"\n");
441 if(img_fol.set_imgdir==1){
442 if (get_next_file(imageno, dirptr,&img_fol, ¶meters)) {
443 fprintf(stderr,"skipping file...\n");
448 /* read the input file and put it in memory */
449 /* ---------------------------------------- */
450 fsrc = fopen(parameters.infile, "rb");
452 fprintf(stderr, "ERROR -> failed to open %s for reading\n", parameters.infile);
456 cio = opj_stream_create_default_file_stream(fsrc,1);
458 fprintf(stderr, "ERROR -> failed to create the stream from the file\n");
462 /* decode the code-stream */
463 /* ---------------------- */
465 switch(parameters.decod_format) {
467 { /* JPEG-2000 codestream */
469 /* get a decoder handle */
470 dinfo = opj_create_decompress_v2(CODEC_J2K);
474 { /* JPEG 2000 compressed image data */
476 /* get a decoder handle */
477 dinfo = opj_create_decompress_v2(CODEC_JP2);
481 { /* JPEG 2000, JPIP */
483 /* get a decoder handle */
484 dinfo = opj_create_decompress_v2(CODEC_JPT);
488 fprintf(stderr, "skipping file..\n");
489 opj_stream_destroy(cio);
493 /* setup the decoder decoding parameters using user parameters */
494 opj_setup_decoder_v2(dinfo, ¶meters, &event_mgr);
496 if(! opj_read_header(cio, dinfo, &file_info, OPJ_IMG_INFO | OPJ_J2K_INFO)){
497 fprintf(stderr, "ERROR -> j2k_dump: failed to read the header\n");
498 opj_stream_destroy(cio);
500 opj_destroy_codec(dinfo);
504 printf("Setting decoding area to %d,%d,%d,%d\n",
505 parameters.ROI_x0, parameters.ROI_y0, parameters.ROI_x1, parameters.ROI_x1);
506 opj_set_decode_area(dinfo,
507 parameters.ROI_x0, parameters.ROI_y0,
508 parameters.ROI_x1, parameters.ROI_x1);
510 /* Dump file informations from header */
511 dump_file_info(fout, &file_info);
513 /* close the byte stream */
514 opj_stream_destroy(cio);
517 /* free remaining structures */
519 opj_destroy_codec(dinfo);
523 opj_image_header_destroy(image);
524 //FIXME opj_file_info_destroy(file_info);
529 /* Close the output file */
536 static void j2k_dump_image(FILE *fd, opj_image_header_t * img) {
538 fprintf(fd, "image {\n");
539 fprintf(fd, " x0=%d, y0=%d, x1=%d, y1=%d\n", img->x0, img->y0, img->x1, img->y1);
540 fprintf(fd, " numcomps=%d\n", img->numcomps);
541 for (compno = 0; compno < img->numcomps; compno++) {
542 opj_image_comp_header_t *comp = &img->comps[compno];
543 fprintf(fd, " comp %d {\n", compno);
544 fprintf(fd, " dx=%d, dy=%d\n", comp->dx, comp->dy);
545 fprintf(fd, " prec=%d\n", comp->prec);
546 /* fprintf(fd, " bpp=%d\n", comp->bpp); */
547 fprintf(fd, " sgnd=%d\n", comp->sgnd);
550 //fprintf(fd, " XTOsiz=%d, YTOsiz=%d, XTsiz=%d, YTsiz=%d\n", img->tile_x0, img->tile_y0, img->tile_width, img->tile_height);
551 //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);
555 static void j2k_dump_cp(FILE *fd, opj_image_t * img, opj_cp_v2_t * cp) {
556 int tileno, compno, layno, bandno, resno, numbands;
557 fprintf(fd, "coding parameters {\n");
558 fprintf(fd, " tx0=%d, ty0=%d\n", cp->tx0, cp->ty0);
559 fprintf(fd, " tdx=%d, tdy=%d\n", cp->tdx, cp->tdy);
560 fprintf(fd, " tw=%d, th=%d\n", cp->tw, cp->th);
561 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
562 opj_tcp_v2_t *tcp = &cp->tcps[tileno];
563 fprintf(fd, " tile %d {\n", tileno);
564 fprintf(fd, " csty=%x\n", tcp->csty);
565 fprintf(fd, " prg=%d\n", tcp->prg);
566 fprintf(fd, " numlayers=%d\n", tcp->numlayers);
567 fprintf(fd, " mct=%d\n", tcp->mct);
568 fprintf(fd, " rates=");
569 for (layno = 0; layno < tcp->numlayers; layno++) {
570 fprintf(fd, "%.1f ", tcp->rates[layno]);
573 for (compno = 0; compno < img->numcomps; compno++) {
574 opj_tccp_t *tccp = &tcp->tccps[compno];
575 fprintf(fd, " comp %d {\n", compno);
576 fprintf(fd, " csty=%x\n", tccp->csty);
577 fprintf(fd, " numresolutions=%d\n", tccp->numresolutions);
578 fprintf(fd, " cblkw=%d\n", tccp->cblkw);
579 fprintf(fd, " cblkh=%d\n", tccp->cblkh);
580 fprintf(fd, " cblksty=%x\n", tccp->cblksty);
581 fprintf(fd, " qmfbid=%d\n", tccp->qmfbid);
582 fprintf(fd, " qntsty=%d\n", tccp->qntsty);
583 fprintf(fd, " numgbits=%d\n", tccp->numgbits);
584 fprintf(fd, " roishift=%d\n", tccp->roishift);
585 fprintf(fd, " stepsizes=");
586 numbands = tccp->qntsty == J2K_CCP_QNTSTY_SIQNT ? 1 : tccp->numresolutions * 3 - 2;
587 for (bandno = 0; bandno < numbands; bandno++) {
588 fprintf(fd, "(%d,%d) ", tccp->stepsizes[bandno].mant,
589 tccp->stepsizes[bandno].expn);
593 if (tccp->csty & J2K_CCP_CSTY_PRT) {
594 fprintf(fd, " prcw=");
595 for (resno = 0; resno < tccp->numresolutions; resno++) {
596 fprintf(fd, "%d ", tccp->prcw[resno]);
599 fprintf(fd, " prch=");
600 for (resno = 0; resno < tccp->numresolutions; resno++) {
601 fprintf(fd, "%d ", tccp->prch[resno]);
606 } /*end of component*/