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
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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.
35 #define _CRTDBG_MAP_ALLOC
37 #include <stdlib.h> // Must be included first
50 #ifndef DONT_HAVE_GETOPT
53 #include "compat/getopt.h"
59 fprintf(stdout,"HELP\n----\n\n");
60 fprintf(stdout,"- the -h option displays this help information on screen\n\n");
63 fprintf(stdout,"List of parameters for the coder JPEG 2000:\n");
65 fprintf(stdout,"REMARKS:\n");
66 fprintf(stdout,"---------\n");
69 (stdout,"The markers written to the main_header are : SOC SIZ COD QCD COM.\n");
71 (stdout,"COD and QCD never appear in the tile_header.\n");
74 (stdout,"- This coder can encode a mega image, a test was made on a 24000x24000 pixels \n");
76 (stdout,"color image. You need enough disk space memory (twice the original) to encode \n");
78 (stdout,"the image,i.e. for a 1.5 GB image you need a minimum of 3GB of disk memory)\n");
80 fprintf(stdout,"By default:\n");
81 fprintf(stdout,"------------\n");
83 fprintf(stdout," * Lossless\n");
84 fprintf(stdout," * 1 tile\n");
85 fprintf(stdout," * Size of precinct : 2^15 x 2^15 (means 1 precinct)\n");
86 fprintf(stdout," * Size of code-block : 64 x 64\n");
87 fprintf(stdout," * Number of resolutions: 6\n");
88 fprintf(stdout," * No SOP marker in the codestream\n");
89 fprintf(stdout," * No EPH marker in the codestream\n");
90 fprintf(stdout," * No sub-sampling in x or y direction\n");
91 fprintf(stdout," * No mode switch activated\n");
92 fprintf(stdout," * Progression order: LRCP\n");
93 fprintf(stdout," * No index file\n");
94 fprintf(stdout," * No ROI upshifted\n");
95 fprintf(stdout," * No offset of the origin of the image\n");
96 fprintf(stdout," * No offset of the origin of the tiles\n");
97 fprintf(stdout," * Reversible DWT 5-3\n");
99 fprintf(stdout,"Parameters:\n");
100 fprintf(stdout,"------------\n");
101 fprintf(stdout,"\n");
103 (stdout,"Required Parameters (except with -h):\n");
104 fprintf(stdout,"\n");
106 (stdout,"-i : source file (-i source.pnm also *.pgm, *.ppm) \n");
107 fprintf(stdout,"\n");
109 (stdout,"-o : destination file (-o dest.j2k or .jp2) \n");
110 fprintf(stdout,"\n");
112 (stdout,"Optional Parameters:\n");
113 fprintf(stdout,"\n");
114 fprintf(stdout,"-h : display the help information \n ");
115 fprintf(stdout,"\n");
116 fprintf(stdout,"-r : different compression ratios for successive layers (-r 20,10,5)\n ");
117 fprintf(stdout," - The rate specified for each quality level is the desired \n");
118 fprintf(stdout," compression factor.\n");
119 fprintf(stdout," Example: -r 20,10,1 means quality 1: compress 20x, \n");
120 fprintf(stdout," quality 2: compress 10x and quality 3: compress lossless\n");
121 fprintf(stdout,"\n");
122 fprintf(stdout," (options -r and -q cannot be used together)\n ");
123 fprintf(stdout,"\n");
125 fprintf(stdout,"-q : different psnr for successive layers (-q 30,40,50) \n ");
127 fprintf(stdout," (options -r and -q cannot be used together)\n ");
129 fprintf(stdout,"\n");
130 fprintf(stdout,"-n : number of resolutions (-n 3) \n");
131 fprintf(stdout,"\n");
132 fprintf(stdout,"-b : size of code block (-b 32,32) \n");
133 fprintf(stdout,"\n");
134 fprintf(stdout,"-c : size of precinct (-c 128,128) \n");
135 fprintf(stdout,"\n");
136 fprintf(stdout,"-t : size of tile (-t 512,512) \n");
137 fprintf(stdout,"\n");
139 (stdout,"-p : progression order (-p LRCP) [LRCP, RLCP, RPCL, PCRL, CPRL] \n");
140 fprintf(stdout,"\n");
142 (stdout,"-s : subsampling factor (-s 2,2) [-s X,Y] \n");
143 fprintf(stdout," Remark: subsampling bigger than 2 can produce error\n");
144 fprintf(stdout,"\n");
146 (stdout,"-SOP : write SOP marker before each packet \n");
147 fprintf(stdout,"\n");
149 (stdout,"-EPH : write EPH marker after each header packet \n");
150 fprintf(stdout,"\n");
152 (stdout,"-M : mode switch (-M 3) [1=BYPASS(LAZY) 2=RESET 4=RESTART(TERMALL)\n");
154 (stdout," 8=VSC 16=ERTERM(SEGTERM) 32=SEGMARK(SEGSYM)] \n");
156 (stdout," Indicate multiple modes by adding their values. \n");
158 (stdout," ex: RESTART(4) + RESET(2) + SEGMARK(32) = -M 38\n");
159 fprintf(stdout,"\n");
161 (stdout,"-x : create an index file *.Idx (-x index_name.Idx) \n");
162 fprintf(stdout,"\n");
164 (stdout,"-ROI : c=%%d,U=%%d : quantization indices upshifted \n");
166 (stdout," for component c=%%d [%%d = 0,1,2]\n");
168 (stdout," with a value of U=%%d [0 <= %%d <= 37] (i.e. -ROI:c=0,U=25) \n");
169 fprintf(stdout,"\n");
171 (stdout,"-d : offset of the origin of the image (-d 150,300) \n");
172 fprintf(stdout,"\n");
174 (stdout,"-T : offset of the origin of the tiles (-T 100,75) \n");
175 fprintf(stdout,"\n");
176 fprintf(stdout,"-I : use the irreversible DWT 9-7 (-I) \n");
177 fprintf(stdout,"\n");
178 fprintf(stdout,"IMPORTANT:\n");
179 fprintf(stdout,"-----------\n");
180 fprintf(stdout,"\n");
181 fprintf(stdout,"The index file has the structure below:\n");
182 fprintf(stdout,"---------------------------------------\n");
183 fprintf(stdout,"\n");
184 fprintf(stdout,"Image_height Image_width\n");
185 fprintf(stdout,"progression order\n");
186 fprintf(stdout,"Tiles_size_X Tiles_size_Y\n");
187 fprintf(stdout,"Components_nb\n");
188 fprintf(stdout,"Layers_nb\n");
189 fprintf(stdout,"decomposition_levels\n");
190 fprintf(stdout,"[Precincts_size_X_res_Nr Precincts_size_Y_res_Nr]...\n");
191 fprintf(stdout," [Precincts_size_X_res_0 Precincts_size_Y_res_0]\n");
192 fprintf(stdout,"Main_header_end_position\n");
193 fprintf(stdout,"Codestream_size\n");
194 fprintf(stdout,"Tile_0 start_pos end_Theader end_pos TotalDisto NumPix MaxMSE\n");
195 fprintf(stdout,"Tile_1 '' '' '' '' '' ''\n");
196 fprintf(stdout,"...\n");
197 fprintf(stdout,"Tile_Nt '' '' '' '' '' ''\n");
198 fprintf(stdout,"Tpacket_0 Tile layer res. comp. prec. start_pos end_pos disto\n");
199 fprintf(stdout,"...\n");
200 fprintf(stdout,"Tpacket_Np '' '' '' '' '' '' '' ''\n");
202 fprintf(stdout,"MaxDisto\n");
204 fprintf(stdout,"TotalDisto\n\n");
207 int give_progression(char progression[4])
209 if (progression[0] == 'L' && progression[1] == 'R'
210 && progression[2] == 'C' && progression[3] == 'P') {
213 if (progression[0] == 'R' && progression[1] == 'L'
214 && progression[2] == 'C' && progression[3] == 'P') {
217 if (progression[0] == 'R' && progression[1] == 'P'
218 && progression[2] == 'C' && progression[3] == 'L') {
221 if (progression[0] == 'P' && progression[1] == 'C'
222 && progression[2] == 'R' && progression[3] == 'L') {
225 if (progression[0] == 'C' && progression[1] == 'P'
226 && progression[2] == 'R' && progression[3] == 'L') {
237 double dwt_norms_97[4][10] = {
238 {1.000, 1.965, 4.177, 8.403, 16.90, 33.84, 67.69, 135.3, 270.6, 540.9},
239 {2.022, 3.989, 8.355, 17.04, 34.27, 68.63, 137.3, 274.6, 549.0},
240 {2.022, 3.989, 8.355, 17.04, 34.27, 68.63, 137.3, 274.6, 549.0},
241 {2.080, 3.865, 8.307, 17.18, 34.71, 69.59, 139.3, 278.6, 557.2}
247 for (l = 0; a > 1; l++) {
253 void encode_stepsize(int stepsize, int numbps, int *expn, int *mant)
256 p = floorlog2(stepsize) - 13;
257 n = 11 - floorlog2(stepsize);
258 *mant = (n < 0 ? stepsize >> -n : stepsize << n) & 0x7ff;
262 void calc_explicit_stepsizes(j2k_tccp_t * tccp, int prec)
264 int numbands, bandno;
265 numbands = 3 * tccp->numresolutions - 2;
266 for (bandno = 0; bandno < numbands; bandno++) {
269 int resno, level, orient, gain;
270 resno = bandno == 0 ? 0 : (bandno - 1) / 3 + 1;
271 orient = bandno == 0 ? 0 : (bandno - 1) % 3 + 1;
272 level = tccp->numresolutions - 1 - resno;
274 tccp->qmfbid == 0 ? 0 : (orient ==
276 || orient == 2 ? 1 : 2));
277 if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
280 double norm = dwt_norms_97[orient][level];
281 stepsize = (1 << (gain + 1)) / norm;
283 encode_stepsize((int) floor(stepsize * 8192.0), prec + gain,
284 &tccp->stepsizes[bandno].expn,
285 &tccp->stepsizes[bandno].mant);
289 int main(int argc, char **argv)
292 int NumResolution, numD_min; /* NumResolution : number of resolution */
293 int Tile_arg; /* Tile_arg = 0 (not in argument) ou = 1 (in argument) */
294 int CSty; /* CSty : coding style */
295 int Prog_order; /* progression order (default LRCP) */
297 int numpocs, numpocs_tile; /* Number of progression order change (POC) default 0 */
298 int prcw_init[J2K_MAXRLVLS]; /* Initialisation Precinct width */
299 int prch_init[J2K_MAXRLVLS]; /* Initialisation Precinct height */
300 //int prcw_init, prch_init; /* Initialisation precincts' size */
301 int cblockw_init, cblockh_init; /* Initialisation codeblocks' size */
302 int mode, value; /* Mode switch (cblk_style) */
303 int subsampling_dx, subsampling_dy; /* subsampling value for dx and dy */
304 int ROI_compno, ROI_shift; /* region of interrest */
305 int Dim[2]; /* portion of the image coded */
306 int TX0, TY0; /* tile off-set */
308 j2k_cp_t cp, cp_init; /* cp_init is used to initialise in multiple tiles */
309 j2k_tcp_t *tcp, *tcp_init; /* tcp_init is used to initialise in multiple tile */
310 j2k_poc_t POC[32]; /* POC : used in case of Progression order change */
319 int res_spec = 0; /* For various precinct sizes specification */
321 char *j2k_codestream;
322 char *jp2_codestream;
340 ROI_compno = -1; /* no ROI */
346 cp.comment = "Created by OpenJPEG version 0.9";
349 cp.fixed_quality = 0; //add fixed_quality
353 cp_init.tcps = (j2k_tcp_t *) malloc(sizeof(j2k_tcp_t)); /* initialisation if only one tile */
354 tcp_init = &cp_init.tcps[0];
355 tcp_init->numlayers = 0;
357 cp.intermed_file = 1;
360 int c = getopt(argc, argv,
361 "i:o:r:q:f:t:n:c:b:x:p:s:d:h:P:S:E:M:R:T:C:I");
365 case 'i': /* IN fill */
378 if ((S1 == 'p' && S2 == 'g' && S3 == 'x')
379 || (S1 == 'P' && S2 == 'G' && S3 == 'X')) {
384 if ((S1 == 'p' && S2 == 'n' && S3 == 'm')
385 || (S1 == 'P' && S2 == 'N' && S3 == 'M')
386 || (S1 == 'p' && S2 == 'g' && S3 == 'm') || (S1 == 'P'
389 || (S1 == 'P' && S2 == 'P' && S3 == 'M') || (S1 == 'p'
396 if ((S1 == 'b' && S2 == 'm' && S3 == 'p')
397 || (S1 == 'B' && S2 == 'M' && S3 == 'P')) {
402 "!! Unrecognized format for infile : %c%c%c [accept only *.pnm, *.pgm, *.ppm, *.pgx or *.bmp] !!\n\n",
406 /* ----------------------------------------------------- */
407 case 'o': /* OUT fill */
421 if ((S1 == 'j' && S2 == '2' && S3 == 'k')
422 || (S1 == 'J' && S2 == '2' && S3 == 'K'))
423 cp.JPEG2000_format = 0;
424 else if ((S1 == 'j' && S2 == 'p' && S3 == '2')
425 || (S1 == 'J' && S2 == 'P' && S3 == '2'))
426 cp.JPEG2000_format = 1;
429 "Unknown output format image *.%c%c%c [only *.j2k, *.jp2]!! \n",
437 /* ----------------------------------------------------- */
438 case 'r': /* rates rates/distorsion */
440 while (sscanf(s, "%d", &tcp_init->rates[tcp_init->numlayers]) == 1) {
441 tcp_init->numlayers++;
442 while (*s && *s != ',') {
452 /* ----------------------------------------------------- */
453 case 'q': /* add fixed_quality */
455 while (sscanf(s, "%f", &tcp_init->distoratio[tcp_init->numlayers])
457 tcp_init->numlayers++;
458 while (*s && *s != ',') {
465 cp.fixed_quality = 1;
469 /* ----------------------------------------------------- */
470 case 'f': /* mod fixed_quality (before : -q) */
472 sscanf(s, "%d", &tcp_init->numlayers);
474 if (tcp_init->numlayers > 9)
477 (int *) malloc(tcp_init->numlayers * NumResolution * 3 *
480 for (i = 0; i < tcp_init->numlayers; i++) {
481 tcp_init->rates[i] = 1;
482 sscanf(s, "%d,", &cp.matrice[i * NumResolution * 3]);
484 if (cp.matrice[i * NumResolution * 3] > 9)
486 cp.matrice[i * NumResolution * 3 + 1] = 0;
487 cp.matrice[i * NumResolution * 3 + 2] = 0;
488 for (j = 1; j < NumResolution; j++) {
489 sscanf(s, "%d,%d,%d",
490 &cp.matrice[i * NumResolution * 3 + j * 3 + 0],
491 &cp.matrice[i * NumResolution * 3 + j * 3 + 1],
492 &cp.matrice[i * NumResolution * 3 + j * 3 + 2]);
494 if (cp.matrice[i * NumResolution * 3 + j * 3] > 9)
496 if (cp.matrice[i * NumResolution * 3 + j * 3 + 1] > 9)
498 if (cp.matrice[i * NumResolution * 3 + j * 3 + 2] > 9)
501 if (i < tcp_init->numlayers - 1)
506 /* ----------------------------------------------------- */
507 case 't': /* tiles */
508 sscanf(optarg, "%d,%d", &cp.tdx, &cp.tdy);
511 /* ----------------------------------------------------- */
512 case 'n': /* resolution */
513 sscanf(optarg, "%d", &NumResolution);
515 /* ----------------------------------------------------- */
516 case 'c': /* precinct dimension */
520 sscanf(s, "[%d,%d]%c", &prcw_init[res_spec],
521 &prch_init[res_spec], &sep);
524 s = strpbrk(s, "]") + 2;
528 /* ----------------------------------------------------- */
529 case 'b': /* code-block dimension */
530 sscanf(optarg, "%d,%d", &cblockw_init, &cblockh_init);
531 if (cblockw_init * cblockh_init > 4096 || cblockw_init > 1024
532 || cblockw_init < 4 || cblockh_init > 1024 || cblockh_init < 4) {
534 "!! Size of code_block error (option -b) !!\n\nRestriction :\n * width*height<=4096\n * 4<=width,height<= 1024\n\n");
538 /* ----------------------------------------------------- */
539 case 'x': /* creation of index file */
543 /* ----------------------------------------------------- */
544 case 'p': /* progression order */
546 for (i = 0; i < 4; i++) {
550 Prog_order = give_progression(progression);
552 if (Prog_order == -1) {
554 "Unrecognized progression order [LRCP, RLCP, RPCL, PCRL, CPRL] !!\n");
558 /* ----------------------------------------------------- */
559 case 's': /* subsampling factor */
560 if (sscanf(optarg, "%d,%d", &subsampling_dx, &subsampling_dy) != 2) {
562 "'-s' sub-sampling argument error ! [-s dx,dy]\n");
566 /* ----------------------------------------------------- */
567 case 'd': /* coordonnate of the reference grid */
568 if (sscanf(optarg, "%d,%d", &Dim[0], &Dim[1]) != 2) {
570 "-d 'coordonnate of the reference grid' argument error !! [-d x0,y0]\n");
574 /* ----------------------------------------------------- */
575 case 'h': /* Display an help description */
579 /* ----------------------------------------------------- */
581 fprintf(stderr, "/----------------------------------\\\n");
582 fprintf(stderr, "| POC option not fully tested !! |\n");
583 fprintf(stderr, "\\----------------------------------/\n");
586 while (sscanf(s, "T%d=%d,%d,%d,%d,%d,%s", &POC[numpocs].tile,
587 &POC[numpocs].resno0, &POC[numpocs].compno0,
588 &POC[numpocs].layno1, &POC[numpocs].resno1,
589 &POC[numpocs].compno1, POC[numpocs].progorder) == 7) {
590 POC[numpocs].prg = give_progression(POC[numpocs].progorder);
591 /* POC[numpocs].tile; */
593 while (*s && *s != '/') {
601 /* ------------------------------------------------------ */
602 case 'S': /* SOP marker */
605 /* ------------------------------------------------------ */
606 case 'E': /* EPH marker */
609 /* ------------------------------------------------------ */
610 case 'M': /* Mode switch pas tous au point !! */
611 if (sscanf(optarg, "%d", &value) == 1) {
612 for (i = 0; i <= 5; i++) {
613 int cache = value & (1 << i);
619 /* ------------------------------------------------------ */
621 if (sscanf(optarg, "OI:c=%d,U=%d", &ROI_compno, &ROI_shift) != 2) {
622 fprintf(stderr, "ROI error !! [-ROI:c='compno',U='shift']\n");
626 /* ------------------------------------------------------ */
627 case 'T': /* Tile offset */
628 if (sscanf(optarg, "%d,%d", &TX0, &TY0) != 2) {
629 fprintf(stderr, "-T 'tile offset' argument error !! [-T X0,Y0]");
633 /* ------------------------------------------------------ */
634 case 'C': /* Add a comment */
637 /* ------------------------------------------------------ */
638 case 'I': /* reversible or not */
641 /* ------------------------------------------------------ */
652 if (!infile || !outfile) {
654 "usage: image_to_j2k -i image-file -o j2k/jp2-file (+ options)\n");
658 if ((cp.disto_alloc || cp.fixed_alloc || cp.fixed_quality)
659 && (!(cp.disto_alloc ^ cp.fixed_alloc ^ cp.fixed_quality))) {
661 "Error: options -r -q and -f can not be used together !!\n");
663 } // mod fixed_quality
665 /* if no rate entered, lossless by default */
666 if (tcp_init->numlayers == 0) {
667 tcp_init->rates[tcp_init->numlayers] = 0; //MOD antonin : losslessbug
668 tcp_init->numlayers++;
672 if (TX0 > Dim[0] || TY0 > Dim[1]) {
674 "Error: Tile offset dimension is unnappropriate --> TX0(%d)<=IMG_X0(%d) TYO(%d)<=IMG_Y0(%d) \n",
675 TX0, Dim[0], TY0, Dim[1]);
679 for (i = 0; i < numpocs; i++) {
680 if (POC[i].prg == -1) {
682 "Unrecognized progression order in option -P (POC n %d) [LRCP, RLCP, RPCL, PCRL, CPRL] !!\n",
687 switch (cp.image_type) {
691 (infile, &img, cp.tdy, subsampling_dx, subsampling_dy, Dim, cp))
693 fprintf(stderr, "not a pgx file\n");
698 (infile, &img, -1, subsampling_dx, subsampling_dy, Dim, cp)) {
699 fprintf(stderr, " not a pgx file\n");
706 if (!pnmtoimage(infile, &img, subsampling_dx, subsampling_dy, Dim)) {
707 fprintf(stderr, " not a pnm file\n");
713 if (!bmptoimage(infile, &img, subsampling_dx, subsampling_dy, Dim)) {
714 fprintf(stderr, " not a bmp file\n");
719 /* to respect profile - 0 */
720 /* ---------------------- */
722 /* 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++;
723 if ((numD_min+1)>NumResolution)
725 fprintf(stderr,"\n********************************************************************************\n\n");
726 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);
727 fprintf(stderr, "********************************************************************************\n\n");
728 NumResolution=numD_min+1;
732 cp.tw = int_ceildiv(img.x1 - cp.tx0, cp.tdx);
733 cp.th = int_ceildiv(img.y1 - cp.ty0, cp.tdy);
735 cp.tdx = img.x1 - cp.tx0;
736 cp.tdy = img.y1 - cp.ty0;
739 /* Initialization for PPM marker */
745 /* Init the mutiple tiles */
746 /* ---------------------- */
747 cp.tcps = (j2k_tcp_t *) malloc(cp.tw * cp.th * sizeof(j2k_tcp_t));
749 for (tileno = 0; tileno < cp.tw * cp.th; tileno++) {
750 tcp = &cp.tcps[tileno];
751 tcp->numlayers = tcp_init->numlayers;
752 for (j = 0; j < tcp->numlayers; j++) {
753 if (cp.fixed_quality) // add fixed_quality
754 tcp->distoratio[j] = tcp_init->distoratio[j];
756 tcp->rates[j] = tcp_init->rates[j];
759 tcp->prg = Prog_order;
760 tcp->mct = img.numcomps == 3 ? 1 : 0;
762 tcp->ppt_data = NULL;
768 /* intialisation of POC */
770 for (i = 0; i < numpocs; i++) {
771 if (tileno == POC[i].tile - 1 || POC[i].tile == -1) {
772 tcp_poc = &tcp->pocs[numpocs_tile];
773 tcp_poc->resno0 = POC[numpocs_tile].resno0;
774 tcp_poc->compno0 = POC[numpocs_tile].compno0;
775 tcp_poc->layno1 = POC[numpocs_tile].layno1;
776 tcp_poc->resno1 = POC[numpocs_tile].resno1;
777 tcp_poc->compno1 = POC[numpocs_tile].compno1;
778 tcp_poc->prg = POC[numpocs_tile].prg;
779 tcp_poc->tile = POC[numpocs_tile].tile;
784 tcp->numpocs = numpocs_tile;
785 tcp->tccps = (j2k_tccp_t *) malloc(img.numcomps * sizeof(j2k_tccp_t));
787 for (i = 0; i < img.numcomps; i++) {
788 tccp = &tcp->tccps[i];
789 tccp->csty = CSty & 0x01; /* 0 => one precinct || 1 => custom precinct */
790 tccp->numresolutions = NumResolution;
791 tccp->cblkw = int_floorlog2(cblockw_init);
792 tccp->cblkh = int_floorlog2(cblockh_init);
793 tccp->cblksty = mode;
794 tccp->qmfbid = ir ? 0 : 1;
795 tccp->qntsty = ir ? J2K_CCP_QNTSTY_SEQNT : J2K_CCP_QNTSTY_NOQNT;
798 tccp->roishift = ROI_shift;
801 if (CSty & J2K_CCP_CSTY_PRT) {
803 for (j = tccp->numresolutions - 1; j >= 0; j--) {
805 if (prcw_init[p] < 1)
808 tccp->prcw[j] = int_floorlog2(prcw_init[p]);
810 if (prch_init[p] < 1)
813 tccp->prch[j] = int_floorlog2(prch_init[p]);
815 int size_prcw, size_prch;
816 size_prcw = prcw_init[res_spec - 1] >> (p - (res_spec - 1));
817 size_prch = prch_init[res_spec - 1] >> (p - (res_spec - 1));
821 tccp->prcw[j] = int_floorlog2(size_prcw);
825 tccp->prch[j] = int_floorlog2(size_prch);
828 /*printf("\nsize precinct pour level %d : %d,%d\n", j,
829 tccp->prcw[j], tccp->prch[j]); */
832 for (j = 0; j < tccp->numresolutions; j++) {
837 calc_explicit_stepsizes(tccp, img.comps[i].prec);
843 if (cp.JPEG2000_format == 0) { /* J2K format output */
844 if (cp.intermed_file == 1) { /* After the encoding of each tile, j2k_encode
845 stores the data in the file */
846 len = j2k_encode(&img, &cp, outfile, cp.tdx * cp.tdy * 2, index);
848 fprintf(stderr, "failed to encode image\n");
852 j2k_codestream = (char *) malloc(cp.tdx * cp.tdy * cp.tw * cp.th * 2);
853 /* Allocate memory for all tiles */
854 cio_init(j2k_codestream, cp.tdx * cp.tdy * cp.tw * cp.th * 2);
856 j2k_encode(&img, &cp, j2k_codestream,
857 cp.tdx * cp.tdy * cp.tw * cp.th * 2, index);
859 fprintf(stderr, "failed to encode image\n");
862 f = fopen(outfile, "wb");
864 fprintf(stderr, "failed to open %s for writing\n", outfile);
867 fwrite(j2k_codestream, 1, len, f);
868 free(j2k_codestream);
871 } else { /* JP2 format output */
873 jp2_struct_t *jp2_struct;
874 jp2_struct = (jp2_struct_t *) malloc(sizeof(jp2_struct_t));
875 jp2_struct->image = &img;
877 /* Initialising the standard JP2 box content */
878 /* If you wish to modify those boxes, you have to modify
879 the jp2_struct content */
880 if (jp2_init_stdjp2(jp2_struct, &img)) {
881 fprintf(stderr, "Error with jp2 initialization");
885 if (cp.intermed_file == 1) {
886 /*For the moment, JP2 format does not use intermediary
887 files for each tile */
888 cp.intermed_file = 0;
890 j2k_codestream = (char *) malloc(cp.tdx * cp.tdy * cp.tw * cp.th * 2);
891 jp2_codestream = (char *) malloc(cp.tdx * cp.tdy * cp.tw * cp.th * 2);
893 cio_init(j2k_codestream, cp.tdx * cp.tdy * cp.tw * cp.th * 2);
894 len = j2k_encode(&img, &cp, j2k_codestream,
895 cp.tdx * cp.tdy * cp.tw * cp.th * 2, index);
897 fprintf(stderr, "failed to encode image\n");
900 jp2_struct->j2k_codestream_len = len;
902 cio_init(jp2_codestream, cp.tdx * cp.tdy * cp.tw * cp.th * 2);
903 len = jp2_wrap_j2k(jp2_struct, j2k_codestream, jp2_codestream);
904 f = fopen(outfile, "wb");
906 fprintf(stderr, "failed to open %s for writing\n", outfile);
909 fwrite(jp2_codestream, 1, len, f);
910 free(jp2_codestream);
911 free(j2k_codestream);
915 /* Remove the temporary files */
916 /* -------------------------- */
917 if (cp.image_type) { /* PNM PGM PPM */
918 for (i = 0; i < img.numcomps; i++) {
920 sprintf(&tmp, "Compo%d", i);
921 if (remove(&tmp) == -1) {
922 fprintf(stderr, "failed to kill %s file !\n", &tmp);
926 for (i = 0; i < cp.th; i++) {
928 sprintf(&tmp, "bandtile%d", i + 1);
930 if (remove(&tmp) == -1) {
931 fprintf(stderr, "failed to kill %s file !\n", &tmp);
939 if (tcp_init->numlayers > 9)
941 for (tileno = 0; tileno < cp.tw * cp.th; tileno++)
942 free(cp.tcps[tileno].tccps);
953 _CrtDumpMemoryLeaks();