2 * Copyright (c) 20010, Mathieu Malaterre, GDCM
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
15 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
18 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24 * POSSIBILITY OF SUCH DAMAGE.
33 #include "windirent.h"
42 #define _stricmp strcasecmp
43 #define _strnicmp strncasecmp
46 #include "opj_config.h"
50 #include "opj_getopt.h"
54 #include "format_defs.h"
56 typedef struct dircnt{
57 /** Buffer for holding images read from Directory*/
59 /** Pointer to the buffer*/
64 typedef struct img_folder{
65 /** The directory path of the folder containing input images*/
68 const char *out_format;
71 /** Enable Cod Format for output*/
76 void decode_help_display(void) {
77 fprintf(stdout,"HELP for j2k_dump\n----\n\n");
78 fprintf(stdout,"- the -h option displays this help information on screen\n\n");
81 fprintf(stdout,"List of parameters for the JPEG 2000 "
89 fprintf(stdout," -ImgDir \n");
90 fprintf(stdout," Image file Directory path \n");
91 fprintf(stdout," -i <compressed file>\n");
92 fprintf(stdout," REQUIRED only if an Input image directory not specified\n");
93 fprintf(stdout," Currently accepts J2K-files, JP2-files and JPT-files. The file type\n");
94 fprintf(stdout," is identified based on its suffix.\n");
95 fprintf(stdout," -o <output file>\n");
96 fprintf(stdout," OPTIONAL\n");
97 fprintf(stdout," Output file where file info will be dump.\n");
98 fprintf(stdout," By default it will be in the stdout.\n");
102 /* -------------------------------------------------------------------------- */
103 static void j2k_dump_image(FILE *fd, opj_image_header_t * img);
104 static void j2k_dump_cp(FILE *fd, opj_image_t * img, opj_cp_v2_t * cp);
106 int get_num_images(char *imgdirpath){
108 struct dirent* content;
111 /*Reading the input images from given input directory*/
113 dir= opendir(imgdirpath);
115 fprintf(stderr,"Could not open Folder %s\n",imgdirpath);
119 while((content=readdir(dir))!=NULL){
120 if(strcmp(".",content->d_name)==0 || strcmp("..",content->d_name)==0 )
127 int load_images(dircnt_t *dirptr, 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);
139 fprintf(stderr,"Folder opened successfully\n");
142 while((content=readdir(dir))!=NULL){
143 if(strcmp(".",content->d_name)==0 || strcmp("..",content->d_name)==0 )
146 strcpy(dirptr->filename[i],content->d_name);
152 int get_file_format(char *filename) {
154 static const char *extension[] = {"pgx", "pnm", "pgm", "ppm", "bmp","tif", "raw", "tga", "png", "j2k", "jp2", "jpt", "j2c", "jpc" };
155 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 };
156 char * ext = strrchr(filename, '.');
161 for(i = 0; i < sizeof(format)/sizeof(*format); i++) {
162 if(_strnicmp(ext, extension[i], 3) == 0) {
171 char get_next_file(int imageno,dircnt_t *dirptr,img_fol_t *img_fol, opj_dparameters_t *parameters){
172 char image_filename[OPJ_PATH_LEN], infilename[OPJ_PATH_LEN],outfilename[OPJ_PATH_LEN],temp_ofname[OPJ_PATH_LEN];
173 char *temp_p, temp1[OPJ_PATH_LEN]="";
175 strcpy(image_filename,dirptr->filename[imageno]);
176 fprintf(stderr,"File Number %d \"%s\"\n",imageno,image_filename);
177 parameters->decod_format = get_file_format(image_filename);
178 if (parameters->decod_format == -1)
180 sprintf(infilename,"%s/%s",img_fol->imgdirpath,image_filename);
181 strncpy(parameters->infile, infilename, sizeof(infilename));
184 strcpy(temp_ofname,strtok(image_filename,"."));
185 while((temp_p = strtok(NULL,".")) != NULL){
186 strcat(temp_ofname,temp1);
187 sprintf(temp1,".%s",temp_p);
189 if(img_fol->set_out_format==1){
190 sprintf(outfilename,"%s/%s.%s",img_fol->imgdirpath,temp_ofname,img_fol->out_format);
191 strncpy(parameters->outfile, outfilename, sizeof(outfilename));
196 /* -------------------------------------------------------------------------- */
197 int parse_cmdline_decoder(int argc, char **argv, opj_dparameters_t *parameters,img_fol_t *img_fol) {
198 /* parse the command line */
200 opj_option_t long_option[]={
201 {"ImgDir",REQ_ARG, NULL ,'y'},
203 const char optlist[] = "i:o:h";
205 totlen=sizeof(long_option);
206 img_fol->set_out_format = 0;
208 c = opj_getopt_long(argc, argv,optlist,long_option,totlen);
212 case 'i': /* input file */
214 char *infile = opj_optarg;
215 parameters->decod_format = get_file_format(infile);
216 switch(parameters->decod_format) {
223 "!! Unrecognized format for infile : %s [accept only *.j2k, *.jp2, *.jpc or *.jpt] !!\n\n",
227 strncpy(parameters->infile, infile, sizeof(parameters->infile)-1);
231 /* ------------------------------------------------------ */
233 case 'o': /* output file */
235 char *outfile = opj_optarg;
236 strncpy(parameters->outfile, outfile, sizeof(parameters->outfile)-1);
240 /* ----------------------------------------------------- */
242 case 'h': /* display an help description */
243 decode_help_display();
246 /* ------------------------------------------------------ */
248 case 'y': /* Image Directory path */
250 img_fol->imgdirpath = (char*)malloc(strlen(opj_optarg) + 1);
251 strcpy(img_fol->imgdirpath,opj_optarg);
252 img_fol->set_imgdir=1;
256 /* ----------------------------------------------------- */
259 fprintf(stderr,"WARNING -> this option is not valid \"-%c %s\"\n",c, opj_optarg);
264 /* check for possible errors */
265 if(img_fol->set_imgdir==1){
266 if(!(parameters->infile[0]==0)){
267 fprintf(stderr, "Error: options -ImgDir and -i cannot be used together !!\n");
270 if(img_fol->set_out_format == 0){
271 fprintf(stderr, "Error: When -ImgDir is used, -OutFor <FORMAT> must be used !!\n");
272 fprintf(stderr, "Only one format allowed! Valid format PGM, PPM, PNM, PGX, BMP, TIF, RAW and TGA!!\n");
275 if(!((parameters->outfile[0] == 0))){
276 fprintf(stderr, "Error: options -ImgDir and -o cannot be used together !!\n");
280 if((parameters->infile[0] == 0) ) {
281 fprintf(stderr, "Example: %s -i image.j2k\n",argv[0]);
282 fprintf(stderr, " Try: %s -h\n",argv[0]);
290 /* -------------------------------------------------------------------------- */
293 sample error callback expecting a FILE* client object
295 void error_callback(const char *msg, void *client_data) {
296 FILE *stream = (FILE*)client_data;
297 fprintf(stream, "[ERROR] %s", msg);
300 sample warning callback expecting a FILE* client object
302 void warning_callback(const char *msg, void *client_data) {
303 FILE *stream = (FILE*)client_data;
304 fprintf(stream, "[WARNING] %s", msg);
307 sample debug callback expecting no client object
309 void info_callback(const char *msg, void *client_data) {
311 fprintf(stdout, "[INFO] %s", msg);
314 /* -------------------------------------------------------------------------- */
316 int main(int argc, char *argv[])
319 opj_dparameters_t parameters; /* decompression parameters */
321 opj_event_mgr_t event_mgr; /* event manager */
322 opj_image_header_t* image = NULL;
323 FILE *fsrc = NULL, *fout = NULL;
327 dircnt_t *dirptr = NULL;
328 opj_codec_t* dinfo = NULL; /* handle to a decompressor */
329 opj_stream_t *cio = NULL;
330 opj_codestream_info_t* cstr_info =NULL; /* Codestream information structure */
331 //OPJ_INT32 l_tile_x0,l_tile_y0;
332 //OPJ_UINT32 l_tile_width,l_tile_height,l_nb_tiles_x,l_nb_tiles_y;
335 /* FIXME configure the event callbacks (not required) */
336 memset(&event_mgr, 0, sizeof(opj_event_mgr_t));
337 event_mgr.error_handler = error_callback;
338 event_mgr.warning_handler = warning_callback;
339 event_mgr.info_handler = info_callback;
340 event_mgr.client_data = stderr;
342 /* set decoding parameters to default values */
343 opj_set_default_decoder_parameters(¶meters);
345 /* Initialize img_fol */
346 memset(&img_fol,0,sizeof(img_fol_t));
348 /* parse input and get user encoding parameters */
349 if(parse_cmdline_decoder(argc, argv, ¶meters,&img_fol) == 1) {
353 /* Initialize reading of directory */
354 if(img_fol.set_imgdir==1){
355 num_images=get_num_images(img_fol.imgdirpath);
357 dirptr=(dircnt_t*)malloc(sizeof(dircnt_t));
359 dirptr->filename_buf = (char*)malloc(num_images*OPJ_PATH_LEN*sizeof(char)); // Stores at max 10 image file names
360 dirptr->filename = (char**) malloc(num_images*sizeof(char*));
362 if(!dirptr->filename_buf){
366 for(i=0;i<num_images;i++){
367 dirptr->filename[i] = dirptr->filename_buf + i*OPJ_PATH_LEN;
370 if(load_images(dirptr,img_fol.imgdirpath)==1){
375 fprintf(stdout,"Folder is empty\n");
383 if (parameters.outfile[0] != 0){
384 fout = fopen(parameters.outfile,"w");
386 fprintf(stderr, "ERROR -> failed to open %s for writing\n", parameters.outfile);
393 /*Read the header of each image one by one*/
394 for(imageno = 0; imageno < num_images ; imageno++){
396 fprintf(stderr,"\n");
398 if(img_fol.set_imgdir==1){
399 if (get_next_file(imageno, dirptr,&img_fol, ¶meters)) {
400 fprintf(stderr,"skipping file...\n");
406 /* read the input file and put it in memory */
407 /* ---------------------------------------- */
408 fsrc = fopen(parameters.infile, "rb");
410 fprintf(stderr, "ERROR -> failed to open %s for reading\n", parameters.infile);
414 cio = opj_stream_create_default_file_stream(fsrc,1);
416 /* decode the code-stream */
417 /* ---------------------- */
419 switch(parameters.decod_format) {
421 { /* JPEG-2000 codestream */
423 /* get a decoder handle */
424 dinfo = opj_create_decompress_v2(CODEC_J2K);
428 { /* JPEG 2000 compressed image data */
430 /* get a decoder handle */
431 dinfo = opj_create_decompress_v2(CODEC_JP2);
435 { /* JPEG 2000, JPIP */
437 /* get a decoder handle */
438 dinfo = opj_create_decompress_v2(CODEC_JPT);
442 fprintf(stderr, "skipping file..\n");
443 opj_stream_destroy(cio);
447 /* setup the decoder decoding parameters using user parameters */
448 opj_setup_decoder_v2(dinfo, ¶meters, &event_mgr);
450 /* decode the stream and fill the image structure */
451 /* if (*indexfilename) // If need to extract codestream information
452 image = opj_decode_with_info(dinfo, cio, &cstr_info);
455 //opj_codestream_info_t *cstr_info = NULL;
457 /*bResult = opj_read_header( dinfo,
467 bResult = opj_read_header(cio, dinfo, &image, &cstr_info);
470 fprintf(stderr, "ERROR -> j2k_to_image: failed to read header\n");
474 if (parameters.decod_format == J2K_CFMT){
476 j2k_dump_image(fout, image);
478 //j2k_dump_cp(stdout, image, ((opj_codec_private_t)dinfo)->m_codec);
479 //j2k_dump_cp(fout, image, ((opj_j2k_t*)dinfo->j2k_handle)->cp);
481 else if (parameters.decod_format == JP2_CFMT){
483 if(image->icc_profile_buf){
484 free(image->icc_profile_buf);
485 image->icc_profile_buf = NULL;
487 j2k_dump_image(fout, image);
489 //j2k_dump_cp(stdout, image, dinfo->m_codec);
490 //j2k_dump_cp(fout, image, ((opj_jp2_t*)dinfo->jp2_handle)->j2k->cp);
493 /* close the byte stream */
494 opj_stream_destroy(cio);
497 /* Write the index to disk */
498 /*if (*indexfilename) {
500 bSuccess = write_index_file(&cstr_info, indexfilename);
502 fprintf(stderr, "Failed to output index file\n");
511 /* free remaining structures */
513 opj_destroy_codec(dinfo);
515 /* free codestream information structure */
516 /*if (*indexfilename)
517 FIXME A GARDER opj_destroy_cstr_info(&cstr_info);*/
518 /* free image data structure */
519 opj_image_header_destroy(image);
530 static void j2k_dump_image(FILE *fd, opj_image_header_t * img) {
532 fprintf(fd, "image {\n");
533 fprintf(fd, " x0=%d, y0=%d, x1=%d, y1=%d\n", img->x0, img->y0, img->x1, img->y1);
534 fprintf(fd, " numcomps=%d\n", img->numcomps);
535 for (compno = 0; compno < img->numcomps; compno++) {
536 opj_image_comp_header_t *comp = &img->comps[compno];
537 fprintf(fd, " comp %d {\n", compno);
538 fprintf(fd, " dx=%d, dy=%d\n", comp->dx, comp->dy);
539 fprintf(fd, " prec=%d\n", comp->prec);
540 //fprintf(fd, " bpp=%d\n", comp->bpp);
541 fprintf(fd, " sgnd=%d\n", comp->sgnd);
547 static void j2k_dump_cp(FILE *fd, opj_image_t * img, opj_cp_v2_t * cp) {
548 int tileno, compno, layno, bandno, resno, numbands;
549 fprintf(fd, "coding parameters {\n");
550 fprintf(fd, " tx0=%d, ty0=%d\n", cp->tx0, cp->ty0);
551 fprintf(fd, " tdx=%d, tdy=%d\n", cp->tdx, cp->tdy);
552 fprintf(fd, " tw=%d, th=%d\n", cp->tw, cp->th);
553 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
554 opj_tcp_v2_t *tcp = &cp->tcps[tileno];
555 fprintf(fd, " tile %d {\n", tileno);
556 fprintf(fd, " csty=%x\n", tcp->csty);
557 fprintf(fd, " prg=%d\n", tcp->prg);
558 fprintf(fd, " numlayers=%d\n", tcp->numlayers);
559 fprintf(fd, " mct=%d\n", tcp->mct);
560 fprintf(fd, " rates=");
561 for (layno = 0; layno < tcp->numlayers; layno++) {
562 fprintf(fd, "%.1f ", tcp->rates[layno]);
565 for (compno = 0; compno < img->numcomps; compno++) {
566 opj_tccp_t *tccp = &tcp->tccps[compno];
567 fprintf(fd, " comp %d {\n", compno);
568 fprintf(fd, " csty=%x\n", tccp->csty);
569 fprintf(fd, " numresolutions=%d\n", tccp->numresolutions);
570 fprintf(fd, " cblkw=%d\n", tccp->cblkw);
571 fprintf(fd, " cblkh=%d\n", tccp->cblkh);
572 fprintf(fd, " cblksty=%x\n", tccp->cblksty);
573 fprintf(fd, " qmfbid=%d\n", tccp->qmfbid);
574 fprintf(fd, " qntsty=%d\n", tccp->qntsty);
575 fprintf(fd, " numgbits=%d\n", tccp->numgbits);
576 fprintf(fd, " roishift=%d\n", tccp->roishift);
577 fprintf(fd, " stepsizes=");
578 numbands = tccp->qntsty == J2K_CCP_QNTSTY_SIQNT ? 1 : tccp->numresolutions * 3 - 2;
579 for (bandno = 0; bandno < numbands; bandno++) {
580 fprintf(fd, "(%d,%d) ", tccp->stepsizes[bandno].mant,
581 tccp->stepsizes[bandno].expn);
585 if (tccp->csty & J2K_CCP_CSTY_PRT) {
586 fprintf(fd, " prcw=");
587 for (resno = 0; resno < tccp->numresolutions; resno++) {
588 fprintf(fd, "%d ", tccp->prcw[resno]);
591 fprintf(fd, " prch=");
592 for (resno = 0; resno < tccp->numresolutions; resno++) {
593 fprintf(fd, "%d ", tccp->prch[resno]);
598 } /*end of component*/