WIP: begin to test opj_decode_tile_data
[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
28 #include <stdio.h>
29 #include <string.h>
30 #include <stdlib.h>
31 #include <math.h>
32
33 #ifdef _WIN32
34 #include "windirent.h"
35 #else
36 #include <dirent.h>
37 #endif /* _WIN32 */
38
39 #ifdef _WIN32
40 #include <windows.h>
41 #else
42 #include <strings.h>
43 #define _stricmp strcasecmp
44 #define _strnicmp strncasecmp
45 #endif /* _WIN32 */
46
47 #include "opj_config.h"
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 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");
80
81 /* UniPG>> */
82         fprintf(stdout,"List of parameters for the JPEG 2000 "
83 #ifdef USE_JPWL
84                 "+ JPWL "
85 #endif /* USE_JPWL */
86                 "decoder:\n");
87 /* <<UniPG */
88         fprintf(stdout,"\n");
89         fprintf(stdout,"\n");
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");
101 }
102
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);
106
107 int get_num_images(char *imgdirpath){
108         DIR *dir;
109         struct dirent* content; 
110         int num_images = 0;
111
112         /*Reading the input images from given input directory*/
113
114         dir= opendir(imgdirpath);
115         if(!dir){
116                 fprintf(stderr,"Could not open Folder %s\n",imgdirpath);
117                 return 0;
118         }
119         
120         while((content=readdir(dir))!=NULL){
121                 if(strcmp(".",content->d_name)==0 || strcmp("..",content->d_name)==0 )
122                         continue;
123                 num_images++;
124         }
125         return num_images;
126 }
127
128 int load_images(dircnt_t *dirptr, char *imgdirpath){
129         DIR *dir;
130         struct dirent* content; 
131         int i = 0;
132
133         /*Reading the input images from given input directory*/
134
135         dir= opendir(imgdirpath);
136         if(!dir){
137                 fprintf(stderr,"Could not open Folder %s\n",imgdirpath);
138                 return 1;
139         }else   {
140                 fprintf(stderr,"Folder opened successfully\n");
141         }
142         
143         while((content=readdir(dir))!=NULL){
144                 if(strcmp(".",content->d_name)==0 || strcmp("..",content->d_name)==0 )
145                         continue;
146
147                 strcpy(dirptr->filename[i],content->d_name);
148                 i++;
149         }
150         return 0;       
151 }
152
153 int get_file_format(char *filename) {
154         unsigned int i;
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, '.');
158         if (ext == NULL)
159                 return -1;
160         ext++;
161         if(ext) {
162                 for(i = 0; i < sizeof(format)/sizeof(*format); i++) {
163                         if(_strnicmp(ext, extension[i], 3) == 0) {
164                                 return format[i];
165                         }
166                 }
167         }
168
169         return -1;
170 }
171
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]="";
175
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)
180                 return 1;
181         sprintf(infilename,"%s/%s",img_fol->imgdirpath,image_filename);
182         strncpy(parameters->infile, infilename, sizeof(infilename));
183
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);
189         }
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));
193         }
194         return 0;
195 }
196
197 /* -------------------------------------------------------------------------- */
198 /* parse the command line */
199 int parse_cmdline_decoder(int argc, char **argv, opj_dparameters_t *parameters,img_fol_t *img_fol) {
200         int totlen, c;
201         opj_option_t long_option[]={
202                 {"ImgDir",REQ_ARG, NULL ,'y'},
203         };
204         const char optlist[] = "i:o:d:h";
205
206         totlen=sizeof(long_option);
207         img_fol->set_out_format = 0;
208         do {
209                 c = opj_getopt_long(argc, argv,optlist,long_option,totlen);
210                 if (c == -1)
211                         break;
212                 switch (c) {
213                         case 'i':                       /* input file */
214                         {
215                                 char *infile = opj_optarg;
216                                 parameters->decod_format = get_file_format(infile);
217                                 switch(parameters->decod_format) {
218                                         case J2K_CFMT:
219                                                 break;
220                                         case JP2_CFMT:
221                                                 break;
222                                         case JPT_CFMT:
223                                                 break;
224                                         default:
225                                                 fprintf(stderr, 
226                                                         "!! Unrecognized format for infile : %s [accept only *.j2k, *.jp2, *.jpc or *.jpt] !!\n\n", 
227                                                         infile);
228                                                 return 1;
229                                 }
230                                 strncpy(parameters->infile, infile, sizeof(parameters->infile)-1);
231                         }
232                         break;
233
234                                 /* ------------------------------------------------------ */
235
236                         case 'o':     /* output file */
237                         {
238                           char *outfile = opj_optarg;
239                           strncpy(parameters->outfile, outfile, sizeof(parameters->outfile)-1);
240                         }
241                         break;
242                                 
243                                 /* ----------------------------------------------------- */
244
245                         case 'h':                       /* display an help description */
246                                 decode_help_display();
247                                 return 1;                               
248
249                                 /* ------------------------------------------------------ */
250
251                         case 'y':                       /* Image Directory path */
252                                 {
253                                         img_fol->imgdirpath = (char*)malloc(strlen(opj_optarg) + 1);
254                                         strcpy(img_fol->imgdirpath,opj_optarg);
255                                         img_fol->set_imgdir=1;
256                                 }
257                                 break;
258
259                                 /* ----------------------------------------------------- */
260
261                         case 'd':               /* Input decode ROI */
262                         {
263                                 int size_optarg = (int)strlen(optarg) + 1;
264                                 char *ROI_values = (char*) malloc(size_optarg);
265                                 ROI_values[0] = '\0';
266                                 strncpy(ROI_values, optarg, strlen(optarg));
267                                 ROI_values[strlen(optarg)] = '\0';
268                                 /*printf("ROI_values = %s [%d / %d]\n", ROI_values, strlen(ROI_values), size_optarg ); */
269                                 parse_ROI_values( ROI_values, &parameters->ROI_x0, &parameters->ROI_y0, &parameters->ROI_x1, &parameters->ROI_y1);
270                         }
271                         break;
272                         
273                                 /* ----------------------------------------------------- */
274                         default:
275                                 fprintf(stderr,"WARNING -> this option is not valid \"-%c %s\"\n",c, opj_optarg);
276                                 break;
277                 }
278         }while(c != -1);
279
280         /* check for possible errors */
281         if(img_fol->set_imgdir==1){
282                 if(!(parameters->infile[0]==0)){
283                         fprintf(stderr, "Error: options -ImgDir and -i cannot be used together !!\n");
284                         return 1;
285                 }
286                 if(img_fol->set_out_format == 0){
287                         fprintf(stderr, "Error: When -ImgDir is used, -OutFor <FORMAT> must be used !!\n");
288                         fprintf(stderr, "Only one format allowed! Valid format PGM, PPM, PNM, PGX, BMP, TIF, RAW and TGA!!\n");
289                         return 1;
290                 }
291                 if(!((parameters->outfile[0] == 0))){
292                         fprintf(stderr, "Error: options -ImgDir and -o cannot be used together !!\n");
293                         return 1;
294                 }
295         }else{
296                 if((parameters->infile[0] == 0) ) {
297                         fprintf(stderr, "Example: %s -i image.j2k\n",argv[0]);
298                         fprintf(stderr, "    Try: %s -h\n",argv[0]);
299                         return 1;
300                 }
301         }
302
303         return 0;
304 }
305
306 /*******************************************************************************
307  * Parse ROI input values
308  * separator = ","
309  *******************************************************************************/
310 int parse_ROI_values( char* inArg, unsigned int *ROI_x0, unsigned int *ROI_y0, unsigned int *ROI_x1, unsigned int *ROI_y1)
311 {
312         int it = 0;
313         int values[4];
314         char delims[] = ",";
315         char *result = NULL;
316         result = strtok( inArg, delims );
317
318         while( (result != NULL) && (it < 4 ) ) {
319                 values[it] = atoi(result);
320                 result = strtok( NULL, delims );
321                 it++;
322         }
323
324         if (it != 4) {
325                 return EXIT_FAILURE;
326         }
327         else{
328                 *ROI_x0 = values[0]; *ROI_y0 = values[1];
329                 *ROI_x1 = values[2]; *ROI_y1 = values[3];
330                 return EXIT_SUCCESS;
331         }
332 }
333
334 /* -------------------------------------------------------------------------- */
335
336 /**
337 sample error callback expecting a FILE* client object
338 */
339 void error_callback(const char *msg, void *client_data) {
340         FILE *stream = (FILE*)client_data;
341         fprintf(stream, "[ERROR] %s", msg);
342 }
343 /**
344 sample warning callback expecting a FILE* client object
345 */
346 void warning_callback(const char *msg, void *client_data) {
347         FILE *stream = (FILE*)client_data;
348         fprintf(stream, "[WARNING] %s", msg);
349 }
350 /**
351 sample debug callback expecting no client object
352 */
353 void info_callback(const char *msg, void *client_data) {
354         (void)client_data;
355         fprintf(stdout, "[INFO] %s", msg);
356 }
357
358 /* -------------------------------------------------------------------------- */
359
360 int main(int argc, char *argv[])
361 {
362         int ret;
363         opj_dparameters_t parameters;   /* decompression parameters */
364         img_fol_t img_fol;
365         opj_event_mgr_t event_mgr;              /* event manager */
366         opj_image_header_t* image = NULL;
367         opj_file_info_t file_info;
368         FILE *fsrc = NULL, *fout = NULL;
369         int num_images;
370         int i,imageno;
371         dircnt_t *dirptr = NULL;
372         opj_codec_t* dinfo = NULL;      /* handle to a decompressor */
373         opj_stream_t *cio = NULL;
374         opj_codestream_info_t* cstr_info =NULL;  /* Codestream information structure */
375         opj_bool l_go_on = OPJ_TRUE;
376
377         OPJ_UINT32 l_max_data_size = 1000;
378         OPJ_BYTE * l_data = (OPJ_BYTE *) malloc(1000);
379
380         /* FIXME configure the event callbacks (not required) */
381         memset(&event_mgr, 0, sizeof(opj_event_mgr_t));
382         event_mgr.error_handler = error_callback;
383         event_mgr.warning_handler = warning_callback;
384         event_mgr.info_handler = info_callback;
385         event_mgr.client_data = stderr;
386
387         /* set decoding parameters to default values */
388         opj_set_default_decoder_parameters(&parameters);
389
390         /* Initialize img_fol */
391         memset(&img_fol,0,sizeof(img_fol_t));
392
393         /* parse input and get user encoding parameters */
394         if(parse_cmdline_decoder(argc, argv, &parameters,&img_fol) == 1) {
395                 return EXIT_FAILURE;
396         }
397
398         /* Initialize reading of directory */
399         if(img_fol.set_imgdir==1){      
400                 num_images=get_num_images(img_fol.imgdirpath);
401
402                 dirptr=(dircnt_t*)malloc(sizeof(dircnt_t));
403                 if(dirptr){
404                         dirptr->filename_buf = (char*)malloc(num_images*OPJ_PATH_LEN*sizeof(char));     /* Stores at max 10 image file names */
405                         dirptr->filename = (char**) malloc(num_images*sizeof(char*));
406
407                         if(!dirptr->filename_buf){
408                                 return EXIT_FAILURE;
409                         }
410
411                         for(i=0;i<num_images;i++){
412                                 dirptr->filename[i] = dirptr->filename_buf + i*OPJ_PATH_LEN;
413                         }
414                 }
415                 if(load_images(dirptr,img_fol.imgdirpath)==1){
416                         return EXIT_FAILURE;
417                 }
418
419                 if (num_images==0){
420                         fprintf(stdout,"Folder is empty\n");
421                         return EXIT_FAILURE;
422                 }
423         }else{
424                 num_images=1;
425         }
426
427         /* Try to open for writing the output file if necessary */
428         if (parameters.outfile[0] != 0){
429                 fout = fopen(parameters.outfile,"w");
430                 if (!fout){
431                         fprintf(stderr, "ERROR -> failed to open %s for writing\n", parameters.outfile);
432                         return EXIT_FAILURE;
433                 }
434         }
435         else
436                 fout = stdout;
437
438         /* Read the header of each image one by one */
439         for(imageno = 0; imageno < num_images ; imageno++){
440
441                 fprintf(stderr,"\n");
442
443                 if(img_fol.set_imgdir==1){
444                         if (get_next_file(imageno, dirptr,&img_fol, &parameters)) {
445                                 fprintf(stderr,"skipping file...\n");
446                                 continue;
447                         }
448                 }
449
450                 /* read the input file and put it in memory */
451                 /* ---------------------------------------- */
452                 fsrc = fopen(parameters.infile, "rb");
453                 if (!fsrc) {
454                         fprintf(stderr, "ERROR -> failed to open %s for reading\n", parameters.infile);
455                         return EXIT_FAILURE;
456                 }
457
458                 cio = opj_stream_create_default_file_stream(fsrc,1);
459                 if (!cio){
460                         fprintf(stderr, "ERROR -> failed to create the stream from the file\n");
461                         return EXIT_FAILURE;
462                 }
463
464                 /* decode the code-stream */
465                 /* ---------------------- */
466
467                 switch(parameters.decod_format) {
468                         case J2K_CFMT:
469                         {       /* JPEG-2000 codestream */
470
471                                 /* get a decoder handle */
472                                 dinfo = opj_create_decompress_v2(CODEC_J2K);
473                                 break;
474                         }
475                         case JP2_CFMT:
476                         {       /* JPEG 2000 compressed image data */
477
478                                 /* get a decoder handle */
479                                 dinfo = opj_create_decompress_v2(CODEC_JP2);
480                                 break;
481                         }
482                         case JPT_CFMT:
483                         {       /* JPEG 2000, JPIP */
484
485                                 /* get a decoder handle */
486                                 dinfo = opj_create_decompress_v2(CODEC_JPT);
487                                 break;
488                         }
489                         default:
490                                 fprintf(stderr, "skipping file..\n");
491                                 opj_stream_destroy(cio);
492                                 continue;
493                 }
494
495                 /* setup the decoder decoding parameters using user parameters */
496                 opj_setup_decoder_v2(dinfo, &parameters, &event_mgr);
497
498                 if(! opj_read_header(cio, dinfo, &file_info, OPJ_IMG_INFO | OPJ_J2K_INFO)){
499                         fprintf(stderr, "ERROR -> j2k_dump: failed to read the header\n");
500                         opj_stream_destroy(cio);
501                         fclose(fsrc);
502                         opj_destroy_codec(dinfo);
503                         return EXIT_FAILURE;
504                 }
505
506                 printf("Setting decoding area to %d,%d,%d,%d\n",
507                                 parameters.ROI_x0, parameters.ROI_y0, parameters.ROI_x1, parameters.ROI_y1);
508
509                 if (! opj_set_decode_area(      dinfo,
510                                                                         parameters.ROI_x0, parameters.ROI_y0,
511                                                                         parameters.ROI_x1, parameters.ROI_y1)){
512                         fprintf(stderr, "ERROR -> j2k_dump: failed to set the decoded area\n");
513                         opj_stream_destroy(cio);
514                         fclose(fsrc);
515                         opj_destroy_codec(dinfo);
516                         return EXIT_FAILURE;
517                 }
518
519                 while (l_go_on) {
520                         OPJ_INT32 l_current_tile_x0,l_current_tile_y0,l_current_tile_x1,l_current_tile_y1;
521                         OPJ_UINT32 l_nb_comps, l_tile_index, l_data_size;
522
523
524                         if (! opj_read_tile_header(     dinfo,
525                                                                                 cio,
526                                                                                 &l_tile_index,
527                                                                                 &l_data_size,
528                                                                                 &l_current_tile_x0,
529                                                                                 &l_current_tile_y0,
530                                                                                 &l_current_tile_x1,
531                                                                                 &l_current_tile_y1,
532                                                                                 &l_nb_comps,
533                                                                                 &l_go_on
534                                                                                 )) {
535                                 fprintf(stderr, "ERROR -> j2k_dump: failed read the tile header\n");
536                                 opj_stream_destroy(cio);
537                                 fclose(fsrc);
538                                 opj_destroy_codec(dinfo);
539                                 return EXIT_FAILURE;
540                         }
541
542                         if (l_go_on) {
543
544                                 if (l_data_size > l_max_data_size) {
545
546                                         l_data = (OPJ_BYTE *) realloc(l_data,l_data_size);
547                                         if (! l_data) {
548                                                 opj_stream_destroy(cio);
549                                                 fclose(fsrc);
550                                                 opj_destroy_codec(dinfo);
551                                                 return 1;
552                                         }
553
554                                         l_max_data_size = l_data_size;
555                                 }
556
557                                 if (! opj_decode_tile_data(dinfo,l_tile_index,l_data,l_data_size,cio))
558                                 {
559                                         free(l_data);
560                                         opj_stream_destroy(cio);
561                                         fclose(fsrc);
562                                         opj_destroy_codec(dinfo);
563                                         return 1;
564                                 }
565                                 /** now should inspect image to know the reduction factor and then how to behave with data */
566                         }
567                 }
568
569                 /* Dump file informations from header */
570                 dump_file_info(fout, &file_info);
571
572                 /* close the byte stream */
573                 opj_stream_destroy(cio);
574                 fclose(fsrc);
575
576                 /* free remaining structures */
577                 if (dinfo) {
578                         opj_destroy_codec(dinfo);
579                 }
580
581
582                 opj_image_header_destroy(image);
583                 //FIXME opj_file_info_destroy(file_info);
584
585         }
586
587
588         /* Close the output file */
589         fclose(fout);
590
591   return EXIT_SUCCESS;
592 }
593
594
595 static void j2k_dump_image(FILE *fd, opj_image_header_t * img) {
596         int compno;
597         fprintf(fd, "image {\n");
598         fprintf(fd, "  x0=%d, y0=%d, x1=%d, y1=%d\n", img->x0, img->y0, img->x1, img->y1);
599         fprintf(fd, "  numcomps=%d\n", img->numcomps);
600         for (compno = 0; compno < img->numcomps; compno++) {
601                 opj_image_comp_header_t *comp = &img->comps[compno];
602                 fprintf(fd, "  comp %d {\n", compno);
603                 fprintf(fd, "    dx=%d, dy=%d\n", comp->dx, comp->dy);
604                 fprintf(fd, "    prec=%d\n", comp->prec);
605                 /* fprintf(fd, "    bpp=%d\n", comp->bpp); */
606                 fprintf(fd, "    sgnd=%d\n", comp->sgnd);
607                 fprintf(fd, "  }\n");
608         }
609         //fprintf(fd, "  XTOsiz=%d, YTOsiz=%d, XTsiz=%d, YTsiz=%d\n", img->tile_x0, img->tile_y0, img->tile_width, img->tile_height);
610         //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);
611         fprintf(fd, "}\n");
612 }
613
614 static void j2k_dump_cp(FILE *fd, opj_image_t * img, opj_cp_v2_t * cp) {
615         int tileno, compno, layno, bandno, resno, numbands;
616         fprintf(fd, "coding parameters {\n");
617         fprintf(fd, "  tx0=%d, ty0=%d\n", cp->tx0, cp->ty0);
618         fprintf(fd, "  tdx=%d, tdy=%d\n", cp->tdx, cp->tdy);
619         fprintf(fd, "  tw=%d, th=%d\n", cp->tw, cp->th);
620         for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
621                 opj_tcp_v2_t *tcp = &cp->tcps[tileno];
622                 fprintf(fd, "  tile %d {\n", tileno);
623                 fprintf(fd, "    csty=%x\n", tcp->csty);
624                 fprintf(fd, "    prg=%d\n", tcp->prg);
625                 fprintf(fd, "    numlayers=%d\n", tcp->numlayers);
626                 fprintf(fd, "    mct=%d\n", tcp->mct);
627                 fprintf(fd, "    rates=");
628                 for (layno = 0; layno < tcp->numlayers; layno++) {
629                         fprintf(fd, "%.1f ", tcp->rates[layno]);
630                 }
631                 fprintf(fd, "\n");
632                 for (compno = 0; compno < img->numcomps; compno++) {
633                         opj_tccp_t *tccp = &tcp->tccps[compno];
634                         fprintf(fd, "    comp %d {\n", compno);
635                         fprintf(fd, "      csty=%x\n", tccp->csty);
636                         fprintf(fd, "      numresolutions=%d\n", tccp->numresolutions);
637                         fprintf(fd, "      cblkw=%d\n", tccp->cblkw);
638                         fprintf(fd, "      cblkh=%d\n", tccp->cblkh);
639                         fprintf(fd, "      cblksty=%x\n", tccp->cblksty);
640                         fprintf(fd, "      qmfbid=%d\n", tccp->qmfbid);
641                         fprintf(fd, "      qntsty=%d\n", tccp->qntsty);
642                         fprintf(fd, "      numgbits=%d\n", tccp->numgbits);
643                         fprintf(fd, "      roishift=%d\n", tccp->roishift);
644                         fprintf(fd, "      stepsizes=");
645                         numbands = tccp->qntsty == J2K_CCP_QNTSTY_SIQNT ? 1 : tccp->numresolutions * 3 - 2;
646                         for (bandno = 0; bandno < numbands; bandno++) {
647                                 fprintf(fd, "(%d,%d) ", tccp->stepsizes[bandno].mant,
648                                         tccp->stepsizes[bandno].expn);
649                         }
650                         fprintf(fd, "\n");
651                         
652                         if (tccp->csty & J2K_CCP_CSTY_PRT) {
653                                 fprintf(fd, "      prcw=");
654                                 for (resno = 0; resno < tccp->numresolutions; resno++) {
655                                         fprintf(fd, "%d ", tccp->prcw[resno]);
656                                 }
657                                 fprintf(fd, "\n");
658                                 fprintf(fd, "      prch=");
659                                 for (resno = 0; resno < tccp->numresolutions; resno++) {
660                                         fprintf(fd, "%d ", tccp->prch[resno]);
661                                 }
662                                 fprintf(fd, "\n");
663                         }
664                         fprintf(fd, "    }\n");
665                 } /*end of component*/
666                 fprintf(fd, "  }\n");
667         } /*end of tile */
668         fprintf(fd, "}\n");
669 }
670