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