643240cff64909c371f2440e5fb525c0a0030ce5
[openjpeg.git] / applications / codec / j2k_dump.c
1 /*
2  * Copyright (c) 2010, Mathieu Malaterre, GDCM
3  * Copyright (c) 2011, Mickael Savinaud, Communications & Systemes <mickael.savinaud@c-s.fr>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
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.
14  *
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.
26  */
27 #include "opj_config.h"
28
29 #include <stdio.h>
30 #include <string.h>
31 #include <stdlib.h>
32 #include <math.h>
33
34 #ifdef _WIN32
35 #include "windirent.h"
36 #else
37 #include <dirent.h>
38 #endif /* _WIN32 */
39
40 #ifdef _WIN32
41 #include <windows.h>
42 #else
43 #include <strings.h>
44 #define _stricmp strcasecmp
45 #define _strnicmp strncasecmp
46 #endif /* _WIN32 */
47
48 #include "openjpeg.h"
49 #include "j2k.h"
50 #include "jp2.h"
51 #include "opj_getopt.h"
52 #include "convert.h"
53 #include "index.h"
54
55 #include "format_defs.h"
56
57 typedef struct dircnt{
58         /** Buffer for holding images read from Directory*/
59         char *filename_buf;
60         /** Pointer to the buffer*/
61         char **filename;
62 }dircnt_t;
63
64
65 typedef struct img_folder{
66         /** The directory path of the folder containing input images*/
67         char *imgdirpath;
68         /** Output format*/
69         const char *out_format;
70         /** Enable option*/
71         char set_imgdir;
72         /** Enable Cod Format for output*/
73         char set_out_format;
74
75 }img_fol_t;
76
77 /* -------------------------------------------------------------------------- */
78 /* Declarations                                                               */
79
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);
82
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);
87
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);
90
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");
95
96 /* UniPG>> */
97         fprintf(stdout,"List of parameters for the JPEG 2000 "
98 #ifdef USE_JPWL
99                 "+ JPWL "
100 #endif /* USE_JPWL */
101                 "decoder:\n");
102 /* <<UniPG */
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");
124 }
125
126 /* -------------------------------------------------------------------------- */
127 int get_num_images(char *imgdirpath){
128         DIR *dir;
129         struct dirent* content; 
130         int num_images = 0;
131
132         /*Reading the input images from given input directory*/
133
134         dir= opendir(imgdirpath);
135         if(!dir){
136                 fprintf(stderr,"Could not open Folder %s\n",imgdirpath);
137                 return 0;
138         }
139         
140         while((content=readdir(dir))!=NULL){
141                 if(strcmp(".",content->d_name)==0 || strcmp("..",content->d_name)==0 )
142                         continue;
143                 num_images++;
144         }
145         return num_images;
146 }
147
148 /* -------------------------------------------------------------------------- */
149 int load_images(dircnt_t *dirptr, char *imgdirpath){
150         DIR *dir;
151         struct dirent* content; 
152         int i = 0;
153
154         /*Reading the input images from given input directory*/
155
156         dir= opendir(imgdirpath);
157         if(!dir){
158                 fprintf(stderr,"Could not open Folder %s\n",imgdirpath);
159                 return 1;
160         }else   {
161                 fprintf(stderr,"Folder opened successfully\n");
162         }
163         
164         while((content=readdir(dir))!=NULL){
165                 if(strcmp(".",content->d_name)==0 || strcmp("..",content->d_name)==0 )
166                         continue;
167
168                 strcpy(dirptr->filename[i],content->d_name);
169                 i++;
170         }
171         return 0;       
172 }
173
174 /* -------------------------------------------------------------------------- */
175 int get_file_format(char *filename) {
176         unsigned int i;
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, '.');
180         if (ext == NULL)
181                 return -1;
182         ext++;
183         if(ext) {
184                 for(i = 0; i < sizeof(format)/sizeof(*format); i++) {
185                         if(_strnicmp(ext, extension[i], 3) == 0) {
186                                 return format[i];
187                         }
188                 }
189         }
190
191         return -1;
192 }
193
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]="";
198
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)
203                 return 1;
204         sprintf(infilename,"%s/%s",img_fol->imgdirpath,image_filename);
205         strncpy(parameters->infile, infilename, sizeof(infilename));
206
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);
212         }
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));
216         }
217         return 0;
218 }
219
220 /* -------------------------------------------------------------------------- */
221 /**
222  * Parse the command line
223  */
224 /* -------------------------------------------------------------------------- */
225 int parse_cmdline_decoder(int argc, char **argv, opj_dparameters_t *parameters,img_fol_t *img_fol) {
226         int totlen, c;
227         opj_option_t long_option[]={
228                 {"ImgDir",REQ_ARG, NULL ,'y'},
229         };
230         const char optlist[] = "i:o:d:hv";
231
232         totlen=sizeof(long_option);
233         img_fol->set_out_format = 0;
234         do {
235                 c = opj_getopt_long(argc, argv,optlist,long_option,totlen);
236                 if (c == -1)
237                         break;
238                 switch (c) {
239                         case 'i':                       /* input file */
240                         {
241                                 char *infile = opj_optarg;
242                                 parameters->decod_format = get_file_format(infile);
243                                 switch(parameters->decod_format) {
244                                         case J2K_CFMT:
245                                                 break;
246                                         case JP2_CFMT:
247                                                 break;
248                                         case JPT_CFMT:
249                                                 break;
250                                         default:
251                                                 fprintf(stderr, 
252                                                         "!! Unrecognized format for infile : %s [accept only *.j2k, *.jp2, *.jpc or *.jpt] !!\n\n", 
253                                                         infile);
254                                                 return 1;
255                                 }
256                                 strncpy(parameters->infile, infile, sizeof(parameters->infile)-1);
257                         }
258                         break;
259
260                                 /* ------------------------------------------------------ */
261
262                         case 'o':     /* output file */
263                         {
264                           char *outfile = opj_optarg;
265                           strncpy(parameters->outfile, outfile, sizeof(parameters->outfile)-1);
266                         }
267                         break;
268                                 
269                                 /* ----------------------------------------------------- */
270
271                         case 'h':                       /* display an help description */
272                                 decode_help_display();
273                                 return 1;                               
274
275                                 /* ------------------------------------------------------ */
276
277                         case 'y':                       /* Image Directory path */
278                         {
279                                 img_fol->imgdirpath = (char*)malloc(strlen(opj_optarg) + 1);
280                                 strcpy(img_fol->imgdirpath,opj_optarg);
281                                 img_fol->set_imgdir=1;
282                         }
283                         break;
284
285                                 /* ----------------------------------------------------- */
286
287                         case 'd':               /* Input decode ROI */
288                         {
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, &parameters->DA_x0, &parameters->DA_y0, &parameters->DA_x1, &parameters->DA_y1);
296                         }
297                         break;
298                         /* ----------------------------------------------------- */
299
300                         case 'v':               /* Verbose mode */
301                         {
302                                 parameters->m_verbose = 1;
303                         }
304                         break;
305                         
306                                 /* ----------------------------------------------------- */
307                         default:
308                                 fprintf(stderr,"WARNING -> this option is not valid \"-%c %s\"\n",c, opj_optarg);
309                                 break;
310                 }
311         }while(c != -1);
312
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");
317                         return 1;
318                 }
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");
322                         return 1;
323                 }
324                 if(!((parameters->outfile[0] == 0))){
325                         fprintf(stderr, "Error: options -ImgDir and -o cannot be used together !!\n");
326                         return 1;
327                 }
328         }else{
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]);
332                         return 1;
333                 }
334         }
335
336         return 0;
337 }
338
339 /* -------------------------------------------------------------------------- */
340 /**
341  * Parse decoding area input values
342  * separator = ","
343  */
344 /* -------------------------------------------------------------------------- */
345 int parse_DA_values( char* inArg, unsigned int *DA_x0, unsigned int *DA_y0, unsigned int *DA_x1, unsigned int *DA_y1)
346 {
347         int it = 0;
348         int values[4];
349         char delims[] = ",";
350         char *result = NULL;
351         result = strtok( inArg, delims );
352
353         while( (result != NULL) && (it < 4 ) ) {
354                 values[it] = atoi(result);
355                 result = strtok( NULL, delims );
356                 it++;
357         }
358
359         if (it != 4) {
360                 return EXIT_FAILURE;
361         }
362         else{
363                 *DA_x0 = values[0]; *DA_y0 = values[1];
364                 *DA_x1 = values[2]; *DA_y1 = values[3];
365                 return EXIT_SUCCESS;
366         }
367 }
368
369
370 /* -------------------------------------------------------------------------- */
371 /**
372  * J2K_DUMP MAIN
373  */
374 /* -------------------------------------------------------------------------- */
375 int main(int argc, char *argv[])
376 {
377         FILE *fsrc = NULL, *fout = NULL;
378
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 */
384
385         OPJ_INT32 num_images, imageno;
386         img_fol_t img_fol;
387         dircnt_t *dirptr = NULL;
388
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);
392
393         /* Set decoding parameters to default values */
394         opj_set_default_decoder_parameters(&parameters);
395
396         /* Initialize img_fol */
397         memset(&img_fol,0,sizeof(img_fol_t));
398
399         /* Parse input and get user encoding parameters */
400         if(parse_cmdline_decoder(argc, argv, &parameters,&img_fol) == 1) {
401                 return EXIT_FAILURE;
402         }
403
404         /* Set default event mgr */
405         opj_set_default_event_handler(&event_mgr, parameters.m_verbose);
406
407         /* Initialize reading of directory */
408         if(img_fol.set_imgdir==1){      
409                 int it_image;
410                 num_images=get_num_images(img_fol.imgdirpath);
411
412                 dirptr=(dircnt_t*)malloc(sizeof(dircnt_t));
413                 if(dirptr){
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*));
416
417                         if(!dirptr->filename_buf){
418                                 return EXIT_FAILURE;
419                         }
420
421                         for(it_image=0;it_image<num_images;it_image++){
422                                 dirptr->filename[it_image] = dirptr->filename_buf + it_image*OPJ_PATH_LEN;
423                         }
424                 }
425                 if(load_images(dirptr,img_fol.imgdirpath)==1){
426                         return EXIT_FAILURE;
427                 }
428
429                 if (num_images==0){
430                         fprintf(stdout,"Folder is empty\n");
431                         return EXIT_FAILURE;
432                 }
433         }else{
434                 num_images=1;
435         }
436
437         /* Try to open for writing the output file if necessary */
438         if (parameters.outfile[0] != 0){
439                 fout = fopen(parameters.outfile,"w");
440                 if (!fout){
441                         fprintf(stderr, "ERROR -> failed to open %s for writing\n", parameters.outfile);
442                         return EXIT_FAILURE;
443                 }
444         }
445         else
446                 fout = stdout;
447
448         /* Read the header of each image one by one */
449         for(imageno = 0; imageno < num_images ; imageno++){
450
451                 fprintf(stderr,"\n");
452
453                 if(img_fol.set_imgdir==1){
454                         if (get_next_file(imageno, dirptr,&img_fol, &parameters)) {
455                                 fprintf(stderr,"skipping file...\n");
456                                 continue;
457                         }
458                 }
459
460                 /* Read the input file and put it in memory */
461                 /* ---------------------------------------- */
462                 fsrc = fopen(parameters.infile, "rb");
463                 if (!fsrc) {
464                         fprintf(stderr, "ERROR -> failed to open %s for reading\n", parameters.infile);
465                         return EXIT_FAILURE;
466                 }
467
468                 cio = opj_stream_create_default_file_stream(fsrc,1);
469                 if (!cio){
470                         fprintf(stderr, "ERROR -> failed to create the stream from the file\n");
471                         return EXIT_FAILURE;
472                 }
473
474                 /* Read the JPEG2000 stream */
475                 /* ------------------------ */
476
477                 switch(parameters.decod_format) {
478                         case J2K_CFMT:  /* JPEG-2000 codestream */
479                         {
480                                 /* Get a decoder handle */
481                                 dinfo = opj_create_decompress_v2(CODEC_J2K);
482                                 break;
483                         }
484                         case JP2_CFMT:  /* JPEG 2000 compressed image data */
485                         {
486                                 /* Get a decoder handle */
487                                 dinfo = opj_create_decompress_v2(CODEC_JP2);
488                                 break;
489                         }
490                         case JPT_CFMT:  /* JPEG 2000, JPIP */
491                         {
492                                 /* Get a decoder handle */
493                                 dinfo = opj_create_decompress_v2(CODEC_JPT);
494                                 break;
495                         }
496                         default:
497                                 fprintf(stderr, "skipping file..\n");
498                                 opj_stream_destroy(cio);
499                                 continue;
500                 }
501
502                 /* Setup the decoder decoding parameters using user parameters */
503                 if ( !opj_setup_decoder_v2(dinfo, &parameters, &event_mgr) ){
504                         fprintf(stderr, "ERROR -> j2k_dump: failed to setup the decoder\n");
505                         opj_stream_destroy(cio);
506                         fclose(fsrc);
507                         opj_destroy_codec(dinfo);
508                         fclose(fout);
509                         return EXIT_FAILURE;
510                 }
511
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);
516                         fclose(fsrc);
517                         opj_destroy_codec(dinfo);
518                         fclose(fout);
519                         return EXIT_FAILURE;
520                 }
521
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);
524
525                 /* FIXME WIP_MSD <*/
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);
532                         fclose(fsrc);
533                         fclose(fout);
534                         return EXIT_FAILURE;
535                 }
536
537                 while (l_go_on) {
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;
540
541
542                         if (! opj_read_tile_header(     dinfo,
543                                                                                 cio,
544                                                                                 &l_tile_index,
545                                                                                 &l_data_size,
546                                                                                 &l_current_tile_x0,
547                                                                                 &l_current_tile_y0,
548                                                                                 &l_current_tile_x1,
549                                                                                 &l_current_tile_y1,
550                                                                                 &l_nb_comps,
551                                                                                 &l_go_on
552                                                                                 )) {
553                                 fprintf(stderr, "ERROR -> j2k_dump: failed read the tile header\n");
554                                 opj_stream_destroy(cio);
555                                 fclose(fsrc);
556                                 opj_destroy_codec(dinfo);
557                                 return EXIT_FAILURE;
558                         }
559
560                         if (l_go_on) {
561
562                                 if (l_data_size > l_max_data_size) {
563
564                                         l_data = (OPJ_BYTE *) realloc(l_data,l_data_size);
565                                         if (! l_data) {
566                                                 opj_stream_destroy(cio);
567                                                 opj_destroy_codec(dinfo);
568                                                 fclose(fsrc);
569                                                 fclose(fout);
570                                                 return EXIT_FAILURE;
571                                         }
572
573                                         l_max_data_size = l_data_size;
574                                 }
575
576                                 if (! opj_decode_tile_data(dinfo,l_tile_index,l_data,l_data_size,cio))
577                                 {
578                                         free(l_data);
579                                         opj_stream_destroy(cio);
580                                         opj_destroy_codec(dinfo);
581                                         fclose(fsrc);
582                                         fclose(fout);
583                                         return EXIT_FAILURE;
584                                 }
585                                 /** now should inspect image to know the reduction factor and then how to behave with data */
586                         }
587                 }
588                 /* FIXME WIP_MSD >*/
589
590                 /* Dump file informations from header */
591                 dump_file_info(fout, &file_info);
592
593                 /* close the byte stream */
594                 opj_stream_destroy(cio);
595                 fclose(fsrc);
596
597                 /* free remaining structures */
598                 if (dinfo) {
599                         opj_destroy_codec(dinfo);
600                 }
601
602         }
603
604         /* Close the output file */
605         fclose(fout);
606
607   return EXIT_SUCCESS;
608 }
609
610
611 static void j2k_dump_image(FILE *fd, opj_image_header_t * img) {
612         int compno;
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);
623                 fprintf(fd, "  }\n");
624         }
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);
627         fprintf(fd, "}\n");
628 }
629
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]);
646                 }
647                 fprintf(fd, "\n");
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);
665                         }
666                         fprintf(fd, "\n");
667                         
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]);
672                                 }
673                                 fprintf(fd, "\n");
674                                 fprintf(fd, "      prch=");
675                                 for (resno = 0; resno < tccp->numresolutions; resno++) {
676                                         fprintf(fd, "%d ", tccp->prch[resno]);
677                                 }
678                                 fprintf(fd, "\n");
679                         }
680                         fprintf(fd, "    }\n");
681                 } /*end of component*/
682                 fprintf(fd, "  }\n");
683         } /*end of tile */
684         fprintf(fd, "}\n");
685 }
686