Fixed bug for rate option
[openjpeg.git] / codec / image_to_j2k.c
1 /*
2  * Copyright (c) 2001-2003, David Janssens
3  * Copyright (c) 2002-2003, Yannick Verschueren
4  * Copyright (c) 2002-2003,  Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28
29 #include <openjpeg.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <math.h>
33 #include <string.h>
34 #ifndef DONT_HAVE_GETOPT
35 #include <getopt.h>
36 #else
37 #include "compat/getopt.h"
38 #endif
39 #include "convert.h"
40
41 void help_display()
42 {
43   printf("HELP\n----\n\n");
44   printf("- the option -help displays the readme.txt file on screen\n\n");
45
46
47   printf("List of parameters for the coder JPEG 2000 :\n");
48   printf("\n");
49   printf
50     ("- The markers COD and QCD are writed both of two in the main_header and never appear in the tile_header.  The markers in the main header are : SOC SIZ COD QCD COM.\n");
51   printf("\n");
52   printf
53     ("- This coder can encode mega image, a test was made on a 24000x24000 pixels color image.  You need enough disk space memory (twice the original) to encode the image. (i.e. for a 1.5 Gb image you need a minimum of 3Gb of disk memory)\n");
54   printf("\n");
55   printf("REMARKS :\n");
56   printf("---------\n");
57   printf("\n");
58   printf
59     ("* the value of rate enter in the code line is the compression factor !\n");
60   printf("exemple :\n");
61   printf("\n");
62   printf
63     ("-r 20,10,1 means quality 1 : compress 20x, quality 2 : compress 10x and quality 3 : compress 1x = lossless\n");
64   printf("\n");
65   printf("By default :\n");
66   printf("------------\n");
67   printf("\n");
68   printf(" * lossless\n");
69   printf(" * 1 tile\n");
70   printf(" * size of precinct 2^15 x 2^15 (means 1 precinct)\n");
71   printf(" * size of code-block 64 x 64\n");
72   printf(" * Number of resolution : 6\n");
73   printf(" * No SOP marker in the codestream\n");
74   printf(" * No EPH marker in the codestream\n");
75   printf(" * No sub-sampling in x and y direction\n");
76   printf(" * No mode switch activated\n");
77   printf(" * progression order : LRCP\n");
78   printf(" * No index file\n");
79   printf(" * No ROI upshifted\n");
80   printf(" * No offset of the origin of the image\n");
81   printf(" * No offset of the origin of the tiles\n");
82   printf(" * Reversible DWT 5-3\n");
83   printf("\n");
84   printf("Parameters :\n");
85   printf("------------\n");
86   printf("\n");
87   printf
88     ("-i             : source file  (-i source.pnm also *.pgm, *.ppm) required\n");
89   printf("\n");
90   printf("-o             : destination file (-o dest.j2k) required\n");
91   printf("\n");
92   printf("-help          : Display the help information optional\n ");
93   printf("\n");
94   printf("-r             : different rates (-r 20,10,5) optional\n ");
95   printf("\n");
96   printf("-n             : Number of resolution (-n 3) optional\n");
97   printf("\n");
98   printf("-b             : size of code block (-b 32,32) optional\n");
99   printf("\n");
100   printf("-c             : size of precinct (-c 128,128) optional\n");
101   printf("\n");
102   printf("-t             : size of tile (-t 512,512) optional\n");
103   printf("\n");
104   printf
105     ("-p             : progression order (-p LRCP) [LRCP, RLCP, RPCL, PCRL, CPRL] optional\n");
106   printf("\n");
107   printf
108     ("-s             : subsampling factor (-s 2,2) [-s X,Y] optional\n");
109   printf("\n");
110   printf
111     ("-SOP           : write SOP marker before each packet optional\n");
112   printf("\n");
113   printf
114     ("-EPH           : write EPH marker after each header packet optional\n");
115   printf("\n");
116   printf
117     ("-M             : mode switch (-M 3) [1=BYPASS(LAZY) 2=RESET 4=RESTART(TERMALL) 8=VSC 16=ERTERM(SEGTERM) 32=SEGMARK(SEGSYM)] optional\n");
118   printf
119     ("                    for several mode switch you have to add the value of each mode you want\n");
120   printf
121     ("                    ex : RESTART(4) + RESET(2) + SEGMARK(32) = -M 38\n");
122   printf("\n");
123   printf
124     ("-x             : Create an index file *.Idx (-x index_name.Idx) optional\n");
125   printf("\n");
126   printf
127     ("-ROI:c=%%d,U=%%d : quantization indices upshifted for component c=%%d [%%d = 0,1,2]\n");
128   printf
129     ("                 with a value of U=%%d [0 <= %%d <= 37] (i.e. -ROI:c=0,U=25) optional\n");
130   printf("\n");
131   printf
132     ("-d             : offset of the origin of the image (-d 150,300) optional\n");
133   printf("\n");
134   printf
135     ("-T             : offset of the origin of the tiles (-T 100,75) optional\n");
136   printf("\n");
137   printf("-I             : Use the irreversible DWT 9-7 (-I) optional\n");
138   printf("\n");
139   printf("IMPORTANT :\n");
140   printf("-----------\n");
141   printf("\n");
142   printf("* subsampling bigger than 2 can produce error\n");
143   printf("\n");
144   printf("The index file respect the structure below :\n");
145   printf("---------------------------------------------\n");
146   printf("\n");
147   printf("Image_height Image_width\n");
148   printf("progression order\n");
149   printf("Tiles_size_X Tiles_size_Y\n");
150   printf("Components_nb\n");
151   printf("Layers_nb\n");
152   printf("decomposition_levels\n");
153   printf("Precincts_size_X Precincts_size_Y\n");
154   printf("Main_header_end_position\n");
155   printf("Codestream_size\n");
156   printf("Tile0 start_pos end_Theader end_pos\n");
157   printf("Tile1  ''           ''        ''\n");
158   printf("...\n");
159   printf("TileN  ''           ''        ''\n");
160   printf("Tpacket_0 Tile layer res. comp. prec. start_pos end_pos\n");
161   printf("...\n");
162   printf("Tpacket_M  ''    ''   ''   ''    ''       ''       ''\n");
163 }
164
165 int give_progression(char progression[4])
166 {
167   if (progression[0] == 'L' && progression[1] == 'R'
168       && progression[2] == 'C' && progression[3] == 'P') {
169     return 0;
170   } else {
171     if (progression[0] == 'R' && progression[1] == 'L'
172         && progression[2] == 'C' && progression[3] == 'P') {
173       return 1;
174     } else {
175       if (progression[0] == 'R' && progression[1] == 'P'
176           && progression[2] == 'C' && progression[3] == 'L') {
177         return 2;
178       } else {
179         if (progression[0] == 'P' && progression[1] == 'C'
180             && progression[2] == 'R' && progression[3] == 'L') {
181           return 3;
182         } else {
183           if (progression[0] == 'C' && progression[1] == 'P'
184               && progression[2] == 'R' && progression[3] == 'L') {
185             return 4;
186           } else {
187             return -1;
188           }
189         }
190       }
191     }
192   }
193 }
194
195 double dwt_norms_97[4][10] = {
196   {1.000, 1.965, 4.177, 8.403, 16.90, 33.84, 67.69, 135.3, 270.6, 540.9},
197   {2.022, 3.989, 8.355, 17.04, 34.27, 68.63, 137.3, 274.6, 549.0},
198   {2.022, 3.989, 8.355, 17.04, 34.27, 68.63, 137.3, 274.6, 549.0},
199   {2.080, 3.865, 8.307, 17.18, 34.71, 69.59, 139.3, 278.6, 557.2}
200 };
201
202 int floorlog2(int a)
203 {
204   int l;
205   for (l = 0; a > 1; l++) {
206     a >>= 1;
207   }
208   return l;
209 }
210
211 void encode_stepsize(int stepsize, int numbps, int *expn, int *mant)
212 {
213   int p, n;
214   p = floorlog2(stepsize) - 13;
215   n = 11 - floorlog2(stepsize);
216   *mant = (n < 0 ? stepsize >> -n : stepsize << n) & 0x7ff;
217   *expn = numbps - p;
218 }
219
220 void calc_explicit_stepsizes(j2k_tccp_t * tccp, int prec)
221 {
222   int numbands, bandno;
223   numbands = 3 * tccp->numresolutions - 2;
224   for (bandno = 0; bandno < numbands; bandno++) {
225     double stepsize;
226
227     int resno, level, orient, gain;
228     resno = bandno == 0 ? 0 : (bandno - 1) / 3 + 1;
229     orient = bandno == 0 ? 0 : (bandno - 1) % 3 + 1;
230     level = tccp->numresolutions - 1 - resno;
231     gain =
232       tccp->qmfbid == 0 ? 0 : (orient ==
233                                0 ? 0 : (orient == 1
234                                         || orient == 2 ? 1 : 2));
235     if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
236       stepsize = 1.0;
237     } else {
238       double norm = dwt_norms_97[orient][level];
239       stepsize = (1 << (gain + 1)) / norm;
240     }
241     encode_stepsize((int) floor(stepsize * 8192.0), prec + gain,
242                     &tccp->stepsizes[bandno].expn,
243                     &tccp->stepsizes[bandno].mant);
244   }
245 }
246
247 int main(int argc, char **argv)
248 {
249   int len;
250   int NumResolution, numD_min;  /*   NumResolution : number of resolution                     */
251   int Tile_arg;                 /*   Tile_arg = 0 (not in argument) ou = 1 (in argument)      */
252   int CSty;                     /*   CSty : coding style                                      */
253   int Prog_order;               /*   progression order (default LRCP)                         */
254   char progression[4];
255   int numpocs, numpocs_tile;    /*   Number of progression order change (POC) default 0       */
256   int prcw_init[J2K_MAXRLVLS];  /*   Initialisation Precinct width                            */
257   int prch_init[J2K_MAXRLVLS];  /*   Initialisation Precinct height                           */
258   //int prcw_init, prch_init;                     /*   Initialisation precincts' size                           */
259   int cblockw_init, cblockh_init;       /*   Initialisation codeblocks' size                          */
260   int mode, value;              /*   Mode switch (cblk_style)                                 */
261   int subsampling_dx, subsampling_dy;   /* subsampling value for dx and dy                    */
262   int ROI_compno, ROI_shift;    /*   region of interrest                                      */
263   int Dim[2];                   /*   portion of the image coded                               */
264   int TX0, TY0;                 /*   tile off-set                                             */
265   j2k_image_t img;
266   j2k_cp_t cp, cp_init;         /*   cp_init is used to initialise in multiple tiles          */
267   j2k_tcp_t *tcp, *tcp_init;    /*   tcp_init is used to initialise in multiple tile          */
268   j2k_poc_t POC[32];            /*   POC : used in case of Progression order change           */
269   j2k_poc_t *tcp_poc;
270   j2k_tccp_t *tccp;
271   int i, tileno, j;
272   char *infile = 0;
273   char *outfile = 0;
274   char *index = 0;
275   char *s, S1, S2, S3;
276   int ir = 0;
277   int res_spec = 0;             /*   For various precinct sizes specification                 */
278   char sep;
279
280   /* default value */
281   /* ------------- */
282   NumResolution = 6;
283   CSty = 0;
284   cblockw_init = 64;
285   cblockh_init = 64;
286   cp.tw = 1;
287   cp.th = 1;
288   img.index_on = 0;
289   Prog_order = 0;
290   numpocs = 0;
291   mode = 0;
292   subsampling_dx = 1;
293   subsampling_dy = 1;
294   ROI_compno = -1;              /* no ROI */
295   ROI_shift = 0;
296   Dim[0] = 0;
297   Dim[1] = 0;
298   TX0 = 0;
299   TY0 = 0;
300   cp.comment = NULL;
301   cp.disto_alloc = 0;
302   cp.fixed_alloc = 0;
303   cp.fixed_quality = 0;         //add fixed_quality
304   /* img.PPT=0; */
305
306   Tile_arg = 0;
307   cp_init.tcps = (j2k_tcp_t *) malloc(sizeof(j2k_tcp_t));       /* initialisation if only one tile */
308   tcp_init = &cp_init.tcps[0];
309   tcp_init->numlayers = 0;
310
311   while (1) {
312     int c = getopt(argc, argv,
313                    "i:o:r:q:f:t:n:c:b:x:p:s:d:h:P:S:E:M:R:T:C:I");
314     if (c == -1)
315       break;
316     switch (c) {
317     case 'i':                   /* IN fill */
318       infile = optarg;
319       s = optarg;
320       while (*s) {
321         s++;
322       }
323       s--;
324       S3 = *s;
325       s--;
326       S2 = *s;
327       s--;
328       S1 = *s;
329
330       if ((S1 == 'p' && S2 == 'g' && S3 == 'x')
331           || (S1 == 'P' && S2 == 'G' && S3 == 'X')) {
332         cp.image_type = 0;
333         break;
334       }
335
336       if ((S1 == 'p' && S2 == 'n' && S3 == 'm')
337           || (S1 == 'P' && S2 == 'N' && S3 == 'M')
338           || (S1 == 'p' && S2 == 'g' && S3 == 'm') || (S1 == 'P'
339                                                        && S2 == 'G'
340                                                        && S3 == 'M')
341           || (S1 == 'P' && S2 == 'P' && S3 == 'M') || (S1 == 'p'
342                                                        && S2 == 'p'
343                                                        && S3 == 'm')) {
344         cp.image_type = 1;
345         break;
346       }
347
348       if ((S1 == 'b' && S2 == 'm' && S3 == 'p')
349           || (S1 == 'B' && S2 == 'M' && S3 == 'P')) {
350         cp.image_type = 2;
351         break;
352       }
353       fprintf(stderr,
354               "!! Unrecognized format for infile : %c%c%c [accept only *.pnm, *.pgm, *.ppm, *.pgx or *.bmp] !!\n\n",
355               S1, S2, S3);
356       return 1;
357       break;
358       /* ----------------------------------------------------- */
359     case 'o':                   /* OUT fill */
360       outfile = optarg;
361       break;
362       /* ----------------------------------------------------- */
363     case 'r':                   /* rates rates/distorsion */
364       s = optarg;
365       while (sscanf(s, "%d", &tcp_init->rates[tcp_init->numlayers])
366              == 1) {
367         tcp_init->numlayers++;
368         while (*s && *s != ',') {
369           s++;
370         }
371         if (!*s)
372           break;
373         s++;
374       }
375       cp.disto_alloc = 1;
376       cp.matrice = NULL;
377       break;
378       /* ----------------------------------------------------- */
379     case 'q':                   /* add fixed_quality */
380       s = optarg;
381       while (sscanf(s, "%f", &tcp_init->distoratio[tcp_init->numlayers]) ==
382              1) {
383         tcp_init->numlayers++;
384         while (*s && *s != ',') {
385           s++;
386         }
387         if (!*s)
388           break;
389         s++;
390       }
391       cp.fixed_quality = 1;
392       cp.matrice = NULL;
393       break;
394       /* dda */
395       /* ----------------------------------------------------- */
396     case 'f':                   /* mod fixed_quality (before : -q) */
397       s = optarg;
398       sscanf(s, "%d", &tcp_init->numlayers);
399       s++;
400       if (tcp_init->numlayers > 9)
401         s++;
402       cp.matrice =
403         (int *) malloc(tcp_init->numlayers * NumResolution * 3 *
404                        sizeof(int));
405       s = s + 2;
406       for (i = 0; i < tcp_init->numlayers; i++) {
407         tcp_init->rates[i] = 1;
408         sscanf(s, "%d,", &cp.matrice[i * NumResolution * 3]);
409         s += 2;
410         if (cp.matrice[i * NumResolution * 3] > 9)
411           s++;
412         cp.matrice[i * NumResolution * 3 + 1] = 0;
413         cp.matrice[i * NumResolution * 3 + 2] = 0;
414         for (j = 1; j < NumResolution; j++) {
415           sscanf(s, "%d,%d,%d",
416                  &cp.matrice[i * NumResolution * 3 + j * 3 + 0],
417                  &cp.matrice[i * NumResolution * 3 + j * 3 + 1],
418                  &cp.matrice[i * NumResolution * 3 + j * 3 + 2]);
419           s += 6;
420           if (cp.matrice[i * NumResolution * 3 + j * 3] > 9)
421             s++;
422           if (cp.matrice[i * NumResolution * 3 + j * 3 + 1] > 9)
423             s++;
424           if (cp.matrice[i * NumResolution * 3 + j * 3 + 2] > 9)
425             s++;
426         }
427         if (i < tcp_init->numlayers - 1)
428           s++;
429       }
430       cp.fixed_alloc = 1;
431       break;
432       /* ----------------------------------------------------- */
433     case 't':                   /* tiles */
434       sscanf(optarg, "%d,%d", &cp.tdx, &cp.tdy);
435       Tile_arg = 1;
436       break;
437       /* ----------------------------------------------------- */
438     case 'n':                   /* resolution */
439       sscanf(optarg, "%d", &NumResolution);
440       break;
441       /* ----------------------------------------------------- */
442     case 'c':                   /* precinct dimension */
443       s = optarg;
444       do {
445         sep = 0;
446         sscanf(s, "[%d,%d]%c", &prcw_init[res_spec],
447                &prch_init[res_spec], &sep);
448         CSty |= 0x01;
449         res_spec++;
450         s = strpbrk(s, "]") + 2;
451       } while (sep == ',');
452       break;
453       /* ----------------------------------------------------- */
454     case 'b':                   /* code-block dimension */
455       sscanf(optarg, "%d,%d", &cblockw_init, &cblockh_init);
456       if (cblockw_init * cblockh_init > 4096 || cblockw_init > 1024
457           || cblockw_init < 4 || cblockh_init > 1024 || cblockh_init < 4) {
458         fprintf(stderr,
459                 "!! Size of code_block error (option -b) !!\n\nRestriction :\n    * width*height<=4096\n    * 4<=width,height<= 1024\n\n");
460         return 1;
461       }
462       break;
463       /* ----------------------------------------------------- */
464     case 'x':                   /* creation of index file */
465       index = optarg;
466       img.index_on = 1;
467       break;
468       /* ----------------------------------------------------- */
469     case 'p':                   /* progression order */
470       s = optarg;
471       for (i = 0; i < 4; i++) {
472         progression[i] = *s;
473         s++;
474       }
475       Prog_order = give_progression(progression);
476
477       if (Prog_order == -1) {
478         fprintf(stderr,
479                 "Unrecognized progression order [LRCP, RLCP, RPCL, PCRL, CPRL] !!\n");
480         return 1;
481       }
482       break;
483       /* ----------------------------------------------------- */
484     case 's':                   /* subsampling factor */
485       if (sscanf(optarg, "%d,%d", &subsampling_dx, &subsampling_dy)
486           != 2) {
487         fprintf(stderr,
488                 "'-s' sub-sampling argument error !  [-s dx,dy]\n");
489         return 1;
490       }
491       break;
492       /* ----------------------------------------------------- */
493     case 'd':                   /* coordonnate of the reference grid */
494       if (sscanf(optarg, "%d,%d", &Dim[0], &Dim[1]) != 2) {
495         fprintf(stderr,
496                 "-d 'coordonnate of the reference grid' argument error !! [-d x0,y0]\n");
497         return 1;
498       }
499       break;
500       /* ----------------------------------------------------- */
501     case 'h':                   /* Display an help description */
502       help_display();
503       return 0;
504       break;
505       /* ----------------------------------------------------- */
506     case 'P':                   /* POC */
507       fprintf(stderr, "/----------------------------------\\\n");
508       fprintf(stderr, "|  POC option not fully tested !!  |\n");
509       fprintf(stderr, "\\----------------------------------/\n");
510
511       s = optarg;
512       while (sscanf(s, "T%d=%d,%d,%d,%d,%d,%s", &POC[numpocs].tile,
513                     &POC[numpocs].resno0, &POC[numpocs].compno0,
514                     &POC[numpocs].layno1, &POC[numpocs].resno1,
515                     &POC[numpocs].compno1, POC[numpocs].progorder) == 7) {
516         POC[numpocs].prg = give_progression(POC[numpocs].progorder);
517         /* POC[numpocs].tile; */
518         numpocs++;
519         while (*s && *s != '/') {
520           s++;
521         }
522         if (!*s)
523           break;
524         s++;
525       }
526       break;
527       /* ------------------------------------------------------ */
528     case 'S':                   /* SOP marker */
529       CSty |= 0x02;
530       break;
531       /* ------------------------------------------------------ */
532     case 'E':                   /* EPH marker */
533       CSty |= 0x04;
534       break;
535       /* ------------------------------------------------------ */
536     case 'M':                   /* Mode switch pas tous au point !! */
537       if (sscanf(optarg, "%d", &value) == 1) {
538         for (i = 0; i <= 5; i++) {
539           int cache = value & (1 << i);
540           if (cache)
541             mode |= (1 << i);
542         }
543       }
544       break;
545       /* ------------------------------------------------------ */
546     case 'R':                   /* ROI */
547       if (sscanf(optarg, "OI:c=%d,U=%d", &ROI_compno, &ROI_shift) != 2) {
548         fprintf(stderr, "ROI error !! [-ROI:c='compno',U='shift']\n");
549         return 1;
550       }
551       break;
552       /* ------------------------------------------------------ */
553     case 'T':                   /* Tile offset */
554       if (sscanf(optarg, "%d,%d", &TX0, &TY0) != 2) {
555         fprintf(stderr, "-T 'tile offset' argument error !! [-T X0,Y0]");
556         return 1;
557       }
558       break;
559       /* ------------------------------------------------------ */
560     case 'C':                   /* Add a comment */
561       cp.comment = optarg;
562       break;
563       /* ------------------------------------------------------ */
564     case 'I':                   /* reversible or not */
565       ir = 1;
566       break;
567       /* ------------------------------------------------------ */
568     default:
569       return 1;
570     }
571   }
572
573   cp.tx0 = TX0;
574   cp.ty0 = TY0;
575
576   /* Error messages */
577   /* -------------- */
578   if (!infile || !outfile) {
579     fprintf(stderr,
580             "usage: image_to_j2k -i image-file -o j2k-file (+ options)\n");
581     return 1;
582   }
583
584   if ((cp.disto_alloc || cp.fixed_alloc || cp.fixed_quality)&&(!(cp.disto_alloc ^ cp.fixed_alloc ^ cp.fixed_quality))) {
585     fprintf(stderr,
586             "Error: options -r -q and -f can not be used together !!\n");
587     return 1;
588   } // mod fixed_quality
589
590   /* if no rate entered, lossless by default */
591   if (tcp_init->numlayers == 0) {
592     tcp_init->rates[tcp_init->numlayers] = 1;
593     tcp_init->numlayers++;
594     cp.disto_alloc = 1;
595   }
596
597   if (TX0 > Dim[0] || TY0 > Dim[1]) {
598     fprintf(stderr,
599             "Error: Tile offset dimension is unnappropriate --> TX0(%d)<=IMG_X0(%d) TYO(%d)<=IMG_Y0(%d) \n",
600             TX0, Dim[0], TY0, Dim[1]);
601     return 1;
602   }
603
604   for (i = 0; i < numpocs; i++) {
605     if (POC[i].prg == -1) {
606       fprintf(stderr,
607               "Unrecognized progression order in option -P (POC n %d) [LRCP, RLCP, RPCL, PCRL, CPRL] !!\n",
608               i + 1);
609     }
610   }
611
612   switch (cp.image_type) {
613   case 0:
614     if (Tile_arg) {
615       if (!pgxtoimage
616           (infile, &img, cp.tdy, subsampling_dx, subsampling_dy, Dim,
617            cp)) {
618         fprintf(stderr, "not a pgx file\n");
619         return 1;
620       }
621     } else {
622       if (!pgxtoimage
623           (infile, &img, -1, subsampling_dx, subsampling_dy, Dim, cp)) {
624         fprintf(stderr, " not a pgx file\n");
625         return 1;
626       }
627     }
628     break;
629
630   case 1:
631     if (!pnmtoimage(infile, &img, subsampling_dx, subsampling_dy, Dim)) {
632       fprintf(stderr, " not a pnm file\n");
633       return 1;
634     }
635     break;
636
637   case 2:
638     if (!bmptoimage(infile, &img, subsampling_dx, subsampling_dy, Dim)) {
639       fprintf(stderr, " not a bmp file\n");
640       return 1;
641     }
642     break;
643   }
644   /* to respect profile - 0 */
645   /* ---------------------- */
646   numD_min = 0;
647   /*   while (int_ceildiv(img.x1,(1<<numD_min))-int_ceildiv(img.x0,(1<<numD_min))>120 || int_ceildiv(img.y1,(1<<numD_min))-int_ceildiv(img.y0,(1<<numD_min))>160) numD_min++;
648      if ((numD_min+1)>NumResolution)
649      {
650      fprintf(stderr,"\n********************************************************************************\n\n");
651      fprintf(stderr,  "In view to respect Profile-0, the number of resolution used is %d in place of %d\n\n",numD_min+1,NumResolution);
652      fprintf(stderr,  "********************************************************************************\n\n");
653      NumResolution=numD_min+1;
654      } */
655
656   if (Tile_arg == 1) {
657     cp.tw = int_ceildiv(img.x1 - cp.tx0, cp.tdx);
658     cp.th = int_ceildiv(img.y1 - cp.ty0, cp.tdy);
659   } else {
660     cp.tdx = img.x1 - cp.tx0;
661     cp.tdy = img.y1 - cp.ty0;
662   }
663
664   /* Initialization for PPM marker */
665   cp.ppm = 0;
666   cp.ppm_data = NULL;
667   cp.ppm_previous = 0;
668   cp.ppm_store = 0;
669
670   /* Init the mutiple tiles */
671   /* ---------------------- */
672   cp.tcps = (j2k_tcp_t *) malloc(cp.tw * cp.th * sizeof(j2k_tcp_t));
673
674   for (tileno = 0; tileno < cp.tw * cp.th; tileno++) {
675     tcp = &cp.tcps[tileno];
676     tcp->numlayers = tcp_init->numlayers;
677     for (j = 0; j < tcp->numlayers; j++) {
678       if (cp.fixed_quality)   // add fixed_quality
679         tcp->distoratio[j] = tcp_init->distoratio[j];
680       else
681         tcp->rates[j] = tcp_init->rates[j];
682     }
683     tcp->csty = CSty;
684     tcp->prg = Prog_order;
685     tcp->mct = img.numcomps == 3 ? 1 : 0;
686     tcp->ppt = 0;
687     tcp->ppt_data = NULL;
688     tcp->ppt_store = 0;
689
690     numpocs_tile = 0;
691     tcp->POC = 0;
692     if (numpocs) {
693       /* intialisation of POC */
694       tcp->POC = 1;
695       for (i = 0; i < numpocs; i++) {
696         if (tileno == POC[i].tile - 1 || POC[i].tile == -1) {
697           tcp_poc = &tcp->pocs[numpocs_tile];
698           tcp_poc->resno0 = POC[numpocs_tile].resno0;
699           tcp_poc->compno0 = POC[numpocs_tile].compno0;
700           tcp_poc->layno1 = POC[numpocs_tile].layno1;
701           tcp_poc->resno1 = POC[numpocs_tile].resno1;
702           tcp_poc->compno1 = POC[numpocs_tile].compno1;
703           tcp_poc->prg = POC[numpocs_tile].prg;
704           tcp_poc->tile = POC[numpocs_tile].tile;
705           numpocs_tile++;
706         }
707       }
708     }
709     tcp->numpocs = numpocs_tile;
710     tcp->tccps = (j2k_tccp_t *) malloc(img.numcomps * sizeof(j2k_tccp_t));
711
712     for (i = 0; i < img.numcomps; i++) {
713       tccp = &tcp->tccps[i];
714       tccp->csty = CSty & 0x01; /* 0 => one precinct || 1 => custom precinct  */
715       tccp->numresolutions = NumResolution;
716       tccp->cblkw = int_floorlog2(cblockw_init);
717       tccp->cblkh = int_floorlog2(cblockh_init);
718       tccp->cblksty = mode;
719       tccp->qmfbid = ir ? 0 : 1;
720       tccp->qntsty = ir ? J2K_CCP_QNTSTY_SEQNT : J2K_CCP_QNTSTY_NOQNT;
721       tccp->numgbits = 2;
722       if (i == ROI_compno)
723         tccp->roishift = ROI_shift;
724       else
725         tccp->roishift = 0;
726       if (CSty & J2K_CCP_CSTY_PRT) {
727         int p = 0;
728         for (j = tccp->numresolutions - 1; j >= 0; j--) {
729           if (p < res_spec) {
730             if (prcw_init[p] < 1)
731               tccp->prcw[j] = 1;
732             else
733               tccp->prcw[j] = int_floorlog2(prcw_init[p]);
734
735             if (prch_init[p] < 1)
736               tccp->prch[j] = 1;
737             else
738               tccp->prch[j] = int_floorlog2(prch_init[p]);
739           } else {
740             int size_prcw, size_prch;
741             size_prcw = prcw_init[res_spec - 1] >> (p - (res_spec - 1));
742             size_prch = prch_init[res_spec - 1] >> (p - (res_spec - 1));
743             if (size_prcw < 1)
744               tccp->prcw[j] = 1;
745             else
746               tccp->prcw[j] = int_floorlog2(size_prcw);
747             if (size_prch < 1)
748               tccp->prch[j] = 1;
749             else
750               tccp->prch[j] = int_floorlog2(size_prch);
751           }
752           p++;
753           /*printf("\nsize precinct pour level %d : %d,%d\n", j,
754                  tccp->prcw[j], tccp->prch[j]);*/
755         }
756       } else {
757         for (j = 0; j < tccp->numresolutions; j++) {
758           tccp->prcw[j] = 15;
759           tccp->prch[j] = 15;
760         }
761       }
762       calc_explicit_stepsizes(tccp, img.comps[i].prec);
763     }
764   }
765
766   len = j2k_encode(&img, &cp, outfile, cp.tdx * cp.tdy * 2, index);
767   if (len == 0) {
768     fprintf(stderr, "failed to encode image\n");
769     return 1;
770   }
771
772   /* Remove the temporary files */
773   /* -------------------------- */
774   if (cp.image_type) {          /* PNM PGM PPM */
775     for (i = 0; i < img.numcomps; i++) {
776       char tmp;
777       sprintf(&tmp, "Compo%d", i);
778       if (remove(&tmp) == -1) {
779         fprintf(stderr, "failed to kill %s file !\n", &tmp);
780       }
781     }
782   } else {                      /* PGX */
783     for (i = 0; i < cp.th; i++) {
784       char tmp;
785       sprintf(&tmp, "bandtile%d", i + 1);
786
787       if (remove(&tmp) == -1) {
788         fprintf(stderr, "failed to kill %s file !\n", &tmp);
789       }
790     }
791   }
792
793   system("pause");
794   return 0;
795 }