Various corrections to avoid "signed/unsigned mismatch" warnings during compilation
[openjpeg.git] / libopenjpeg / tcd.c
1 /*
2  * Copyright (c) 2001-2002, David Janssens
3  * Copyright (c) 2002-2004, Yannick Verschueren
4  * Copyright (c) 2002-2004, 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 "tcd.h"
30 #include "int.h"
31 #include "t1.h"
32 #include "t2.h"
33 #include "dwt.h"
34 #include "mct.h"
35 #include <setjmp.h>
36 #include <float.h>
37 #include <stdio.h>
38 #include <time.h>
39 #include <math.h>
40 #include <stdlib.h>
41 #include <string.h>
42
43 static tcd_image_t tcd_image;
44
45 static j2k_image_t *tcd_img;
46 static j2k_cp_t *tcd_cp;
47
48 static tcd_tile_t *tcd_tile;
49 static j2k_tcp_t *tcd_tcp;
50 static int tcd_tileno;
51
52 static tcd_tile_t *tile;
53 static tcd_tilecomp_t *tilec;
54 static tcd_resolution_t *res;
55 static tcd_band_t *band;
56 static tcd_precinct_t *prc;
57 static tcd_cblk_t *cblk;
58
59 extern jmp_buf j2k_error;
60
61 void tcd_dump(tcd_image_t * img, int curtileno)
62 {
63   int tileno, compno, resno, bandno, precno, cblkno;
64   /* fprintf(stderr, "image {\n"); */
65   fprintf(stderr, "  tw=%d, th=%d x0 %d x1 %d\n", img->tw, img->th,
66           tcd_img->x0, tcd_img->x1);
67   for (tileno = 0; tileno < 1; tileno++) {
68     tcd_tile_t *tile = &tcd_image.tiles[curtileno];
69     /* fprintf(stderr, "  tile {\n"); */
70     /* fprintf(stderr, "    x0=%d, y0=%d, x1=%d, y1=%d, numcomps=%d\n", tile->x0, tile->y0, tile->x1, tile->y1, tile->numcomps); */
71     for (compno = 0; compno < tile->numcomps; compno++) {
72       tcd_tilecomp_t *tilec = &tile->comps[compno];
73       /* fprintf(stderr, "    tilec {\n"); */
74       /* fprintf(stderr, "      x0=%d, y0=%d, x1=%d, y1=%d, numresolutions=%d\n", tilec->x0, tilec->y0, tilec->x1, tilec->y1, tilec->numresolutions); */
75       for (resno = 0; resno < tilec->numresolutions; resno++) {
76         tcd_resolution_t *res = &tilec->resolutions[resno];
77         /* fprintf(stderr, "\n   res {\n"); */
78         /* fprintf(stderr, "          x0=%d, y0=%d, x1=%d, y1=%d, pw=%d, ph=%d, numbands=%d\n", res->x0, res->y0, res->x1, res->y1, res->pw, res->ph, res->numbands); */
79         for (bandno = 0; bandno < res->numbands; bandno++) {
80           tcd_band_t *band = &res->bands[bandno];
81           /* fprintf(stderr, "        band {\n"); */
82           /* fprintf(stderr, "          x0=%d, y0=%d, x1=%d, y1=%d, stepsize=%d, numbps=%d\n", band->x0, band->y0, band->x1, band->y1, band->stepsize, band->numbps); */
83           for (precno = 0; precno < res->pw * res->ph; precno++) {
84             tcd_precinct_t *prec = &band->precincts[precno];
85             /* fprintf(stderr, "          prec {\n"); */
86             /* fprintf(stderr, "            x0=%d, y0=%d, x1=%d, y1=%d, cw=%d, ch=%d\n", prec->x0, prec->y0, prec->x1, prec->y1, prec->cw, prec->ch); */
87             for (cblkno = 0; cblkno < prec->cw * prec->ch; cblkno++) {
88               /* tcd_cblk_t *cblk=&prec->cblks[cblkno]; */
89               /* fprintf(stderr, "            cblk {\n"); */
90               /* fprintf(stderr, "              x0=%d, y0=%d, x1=%d, y1=%d\n", cblk->x0, cblk->y0, cblk->x1, cblk->y1); */
91               /* fprintf(stderr, "            }\n"); */
92             }
93             /* fprintf(stderr, "          }\n"); */
94           }
95           /* fprintf(stderr, "        }\n"); */
96         }
97         /* fprintf(stderr, "      }\n"); */
98       }
99       /* fprintf(stderr, "    }\n"); */
100     }
101     /* fprintf(stderr, "  }\n"); */
102   }
103   /* fprintf(stderr, "}\n"); */
104 }
105
106 void tcd_malloc_encode(j2k_image_t * img, j2k_cp_t * cp, int curtileno)
107 {
108   int tileno, compno, resno, bandno, precno, cblkno;
109   tcd_img = img;
110   tcd_cp = cp;
111   tcd_image.tw = cp->tw;
112   tcd_image.th = cp->th;
113   tcd_image.tiles = (tcd_tile_t *) malloc(sizeof(tcd_tile_t));
114
115   for (tileno = 0; tileno < 1; tileno++) {
116     j2k_tcp_t *tcp = &cp->tcps[curtileno];
117     int j;
118     /* cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
119     int p = curtileno % cp->tw; /* si numerotation matricielle .. */
120     int q = curtileno / cp->tw; /* .. coordonnees de la tile (q,p) q pour ligne et p pour colonne */
121     /* tcd_tile_t *tile=&tcd_image.tiles[tileno]; */
122     tile = tcd_image.tiles;
123     /* 4 borders of the tile rescale on the image if necessary */
124     tile->x0 = int_max(cp->tx0 + p * cp->tdx, img->x0);
125     tile->y0 = int_max(cp->ty0 + q * cp->tdy, img->y0);
126     tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, img->x1);
127     tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, img->y1);
128     tile->numcomps = img->numcomps;
129     /* tile->PPT=img->PPT;  */
130     /* Modification of the RATE >> */
131     for (j = 0; j < tcp->numlayers; j++) {
132       tcp->rates[j] =
133         int_ceildiv(tile->numcomps * (tile->x1 - tile->x0) *
134              (tile->y1 -
135               tile->y0) * img->comps[0].prec , (tcp->rates[j] * 8 *
136                                                 img->comps[0].dx *
137                                                 img->comps[0].dy));
138       if (j && tcp->rates[j] < tcp->rates[j - 1] + 10) {
139         tcp->rates[j] = tcp->rates[j - 1] + 20;
140       } else {
141         if (!j && tcp->rates[j] < 30)
142           tcp->rates[j] = 30;
143       }
144     }
145     /* << Modification of the RATE */
146
147     tile->comps =
148       (tcd_tilecomp_t *) malloc(img->numcomps * sizeof(tcd_tilecomp_t));
149     for (compno = 0; compno < tile->numcomps; compno++) {
150       j2k_tccp_t *tccp = &tcp->tccps[compno];
151       /* tcd_tilecomp_t *tilec=&tile->comps[compno]; */
152       tilec = &tile->comps[compno];
153       /* border of each tile component (global) */
154       tilec->x0 = int_ceildiv(tile->x0, img->comps[compno].dx);
155
156       tilec->y0 = int_ceildiv(tile->y0, img->comps[compno].dy);
157       tilec->x1 = int_ceildiv(tile->x1, img->comps[compno].dx);
158       tilec->y1 = int_ceildiv(tile->y1, img->comps[compno].dy);
159
160       tilec->data =
161         (int *) malloc((tilec->x1 - tilec->x0) *
162                        (tilec->y1 - tilec->y0) * sizeof(int));
163       tilec->numresolutions = tccp->numresolutions;
164
165       tilec->resolutions =
166         (tcd_resolution_t *) malloc(tilec->numresolutions *
167                                     sizeof(tcd_resolution_t));
168
169       for (resno = 0; resno < tilec->numresolutions; resno++) {
170         int pdx, pdy;
171         int levelno = tilec->numresolutions - 1 - resno;
172         int tlprcxstart, tlprcystart, brprcxend, brprcyend;
173         int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend;
174         int cbgwidthexpn, cbgheightexpn;
175         int cblkwidthexpn, cblkheightexpn;
176         /* tcd_resolution_t *res=&tilec->resolutions[resno]; */
177
178         res = &tilec->resolutions[resno];
179
180         /* border for each resolution level (global) */
181         res->x0 = int_ceildivpow2(tilec->x0, levelno);
182         res->y0 = int_ceildivpow2(tilec->y0, levelno);
183         res->x1 = int_ceildivpow2(tilec->x1, levelno);
184         res->y1 = int_ceildivpow2(tilec->y1, levelno);
185
186         res->numbands = resno == 0 ? 1 : 3;
187         /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */
188         if (tccp->csty & J2K_CCP_CSTY_PRT) {
189           pdx = tccp->prcw[resno];
190           pdy = tccp->prch[resno];
191         } else {
192           pdx = 15;
193           pdy = 15;
194         }
195         /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000)  */
196         tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx;
197         tlprcystart = int_floordivpow2(res->y0, pdy) << pdy;
198         brprcxend = int_ceildivpow2(res->x1, pdx) << pdx;
199         brprcyend = int_ceildivpow2(res->y1, pdy) << pdy;
200
201         res->pw = (brprcxend - tlprcxstart) >> pdx;
202         res->ph = (brprcyend - tlprcystart) >> pdy;
203
204         if (resno == 0) {
205           tlcbgxstart = tlprcxstart;
206           tlcbgystart = tlprcystart;
207           brcbgxend = brprcxend;
208           brcbgyend = brprcyend;
209           cbgwidthexpn = pdx;
210           cbgheightexpn = pdy;
211         } else {
212           tlcbgxstart = int_ceildivpow2(tlprcxstart, 1);
213           tlcbgystart = int_ceildivpow2(tlprcystart, 1);
214           brcbgxend = int_ceildivpow2(brprcxend, 1);
215           brcbgyend = int_ceildivpow2(brprcyend, 1);
216           cbgwidthexpn = pdx - 1;
217           cbgheightexpn = pdy - 1;
218         }
219
220         cblkwidthexpn = int_min(tccp->cblkw, cbgwidthexpn);
221         cblkheightexpn = int_min(tccp->cblkh, cbgheightexpn);
222
223         for (bandno = 0; bandno < res->numbands; bandno++) {
224           int x0b, y0b, i;
225           int gain, numbps;
226           j2k_stepsize_t *ss;
227           band = &res->bands[bandno];
228           band->bandno = resno == 0 ? 0 : bandno + 1;
229           x0b = (band->bandno == 1)
230             || (band->bandno == 3) ? 1 : 0;
231           y0b = (band->bandno == 2)
232             || (band->bandno == 3) ? 1 : 0;
233
234           if (band->bandno == 0) {
235             /* band border (global) */
236             band->x0 = int_ceildivpow2(tilec->x0, levelno);
237             band->y0 = int_ceildivpow2(tilec->y0, levelno);
238             band->x1 = int_ceildivpow2(tilec->x1, levelno);
239             band->y1 = int_ceildivpow2(tilec->y1, levelno);
240           } else {
241             /* band border (global) */
242             band->x0 =
243               int_ceildivpow2(tilec->x0 -
244                               (1 << levelno) * x0b, levelno + 1);
245             band->y0 =
246               int_ceildivpow2(tilec->y0 -
247                               (1 << levelno) * y0b, levelno + 1);
248             band->x1 =
249               int_ceildivpow2(tilec->x1 -
250                               (1 << levelno) * x0b, levelno + 1);
251             band->y1 =
252               int_ceildivpow2(tilec->y1 -
253                               (1 << levelno) * y0b, levelno + 1);
254
255           }
256
257           ss = &tccp->stepsizes[resno ==
258                                 0 ? 0 : 3 * (resno - 1) + bandno + 1];
259           gain =
260             tccp->qmfbid ==
261             0 ? dwt_getgain_real(band->bandno) : dwt_getgain(band->bandno);
262           numbps = img->comps[compno].prec + gain;
263           band->stepsize =
264             (int) floor((1.0 + ss->mant / 2048.0) *
265                         pow(2.0, numbps - ss->expn) * 8192.0);
266           band->numbps = ss->expn + tccp->numgbits - 1; /* WHY -1 ? */
267
268           band->precincts =
269             (tcd_precinct_t *) malloc(3 * res->pw * res->ph *
270                                       sizeof(tcd_precinct_t));
271
272           for (i = 0; i < res->pw * res->ph * 3; i++) {
273             band->precincts[i].imsbtree = NULL;
274             band->precincts[i].incltree = NULL;
275           }
276
277           for (precno = 0; precno < res->pw * res->ph; precno++) {
278             int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
279             int cbgxstart =
280               tlcbgxstart + (precno % res->pw) * (1 << cbgwidthexpn);
281             int cbgystart =
282               tlcbgystart + (precno / res->pw) * (1 << cbgheightexpn);
283             int cbgxend = cbgxstart + (1 << cbgwidthexpn);
284             int cbgyend = cbgystart + (1 << cbgheightexpn);
285             /* tcd_precinct_t *prc=&band->precincts[precno]; */
286             prc = &band->precincts[precno];
287             /* precinct size (global) */
288             prc->x0 = int_max(cbgxstart, band->x0);
289             prc->y0 = int_max(cbgystart, band->y0);
290             prc->x1 = int_min(cbgxend, band->x1);
291             prc->y1 = int_min(cbgyend, band->y1);
292
293             tlcblkxstart =
294               int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn;
295             tlcblkystart =
296               int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn;
297             brcblkxend =
298               int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn;
299             brcblkyend =
300               int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn;
301             prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;
302             prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn;
303
304             prc->cblks =
305               (tcd_cblk_t *) malloc((prc->cw * prc->ch) *
306                                     sizeof(tcd_cblk_t));
307             prc->incltree = tgt_create(prc->cw, prc->ch);
308             prc->imsbtree = tgt_create(prc->cw, prc->ch);
309
310             for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
311               int cblkxstart =
312                 tlcblkxstart + (cblkno % prc->cw) * (1 << cblkwidthexpn);
313               int cblkystart =
314                 tlcblkystart + (cblkno / prc->cw) * (1 << cblkheightexpn);
315               int cblkxend = cblkxstart + (1 << cblkwidthexpn);
316               int cblkyend = cblkystart + (1 << cblkheightexpn);
317
318               cblk = &prc->cblks[cblkno];
319               /* code-block size (global) */
320               cblk->x0 = int_max(cblkxstart, prc->x0);
321               cblk->y0 = int_max(cblkystart, prc->y0);
322               cblk->x1 = int_min(cblkxend, prc->x1);
323               cblk->y1 = int_min(cblkyend, prc->y1);
324             }
325           }
326         }
327       }
328     }
329   }
330   /* tcd_dump(&tcd_image,curtileno); */
331 }
332
333 void tcd_free_encode(j2k_image_t * img, j2k_cp_t * cp, int curtileno)
334 {
335   int tileno, compno, resno, bandno, precno;
336   tcd_img = img;
337   tcd_cp = cp;
338   tcd_image.tw = cp->tw;
339   tcd_image.th = cp->th;
340   for (tileno = 0; tileno < 1; tileno++) {
341     /* j2k_tcp_t *tcp=&cp->tcps[curtileno]; */
342     tile = tcd_image.tiles;
343     for (compno = 0; compno < tile->numcomps; compno++) {
344       tilec = &tile->comps[compno];
345       for (resno = 0; resno < tilec->numresolutions; resno++) {
346         res = &tilec->resolutions[resno];
347         for (bandno = 0; bandno < res->numbands; bandno++) {
348           band = &res->bands[bandno];
349           for (precno = 0; precno < res->pw * res->ph; precno++) {
350             prc = &band->precincts[precno];
351
352             if (prc->incltree != NULL)
353               tgt_destroy(prc->incltree);
354             if (prc->imsbtree != NULL)
355               tgt_destroy(prc->imsbtree);
356             free(prc->cblks);
357           }                     /* for (precno */
358           free(band->precincts);
359         }                       /* for (bandno */
360       }                         /* for (resno */
361       free(tilec->resolutions);
362     }                           /* for (compno */
363     free(tile->comps);
364   }                             /* for (tileno */
365   free(tcd_image.tiles);
366 }
367
368 void tcd_init_encode(j2k_image_t * img, j2k_cp_t * cp, int curtileno)
369 {
370   int tileno, compno, resno, bandno, precno, cblkno;
371
372   for (tileno = 0; tileno < 1; tileno++) {
373     j2k_tcp_t *tcp = &cp->tcps[curtileno];
374     int j;
375     //              int previous_x0, previous_x1, previous_y0, previous_y1;
376     /* cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
377     int p = curtileno % cp->tw;
378     int q = curtileno / cp->tw;
379     tile = tcd_image.tiles;
380
381     /* 4 borders of the tile rescale on the image if necessary */
382     tile->x0 = int_max(cp->tx0 + p * cp->tdx, img->x0);
383     tile->y0 = int_max(cp->ty0 + q * cp->tdy, img->y0);
384     tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, img->x1);
385     tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, img->y1);
386
387     tile->numcomps = img->numcomps;
388     /* tile->PPT=img->PPT; */
389     /* Modification of the RATE >> */
390     for (j = 0; j < tcp->numlayers; j++) {
391       tcp->rates[j] =
392         int_ceildiv(tile->numcomps * (tile->x1 - tile->x0) *
393              (tile->y1 -
394               tile->y0) * img->comps[0].prec , (tcp->rates[j] * 8 *
395                                                 img->comps[0].dx *
396                                                 img->comps[0].dy));
397       if (j && tcp->rates[j] < tcp->rates[j - 1] + 10) {
398         tcp->rates[j] = tcp->rates[j - 1] + 20;
399       } else {
400         if (!j && tcp->rates[j] < 30)
401           tcp->rates[j] = 30;
402       }
403     }
404     /* << Modification of the RATE */
405     /* tile->comps=(tcd_tilecomp_t*)realloc(tile->comps,img->numcomps*sizeof(tcd_tilecomp_t)); */
406     for (compno = 0; compno < tile->numcomps; compno++) {
407       j2k_tccp_t *tccp = &tcp->tccps[compno];
408       /* int realloc_op; */
409
410       tilec = &tile->comps[compno];
411       /* border of each tile component (global) */
412       tilec->x0 = int_ceildiv(tile->x0, img->comps[compno].dx);
413       tilec->y0 = int_ceildiv(tile->y0, img->comps[compno].dy);
414       tilec->x1 = int_ceildiv(tile->x1, img->comps[compno].dx);
415       tilec->y1 = int_ceildiv(tile->y1, img->comps[compno].dy);
416
417       tilec->data =
418         (int *) malloc((tilec->x1 - tilec->x0) *
419                        (tilec->y1 - tilec->y0) * sizeof(int));
420       tilec->numresolutions = tccp->numresolutions;
421       /* tilec->resolutions=(tcd_resolution_t*)realloc(tilec->resolutions,tilec->numresolutions*sizeof(tcd_resolution_t)); */
422       for (resno = 0; resno < tilec->numresolutions; resno++) {
423         int pdx, pdy;
424         int levelno = tilec->numresolutions - 1 - resno;
425         int tlprcxstart, tlprcystart, brprcxend, brprcyend;
426         int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend;
427         int cbgwidthexpn, cbgheightexpn;
428         int cblkwidthexpn, cblkheightexpn;
429
430         res = &tilec->resolutions[resno];
431         /* border for each resolution level (global) */
432         res->x0 = int_ceildivpow2(tilec->x0, levelno);
433         res->y0 = int_ceildivpow2(tilec->y0, levelno);
434         res->x1 = int_ceildivpow2(tilec->x1, levelno);
435         res->y1 = int_ceildivpow2(tilec->y1, levelno);
436
437         res->numbands = resno == 0 ? 1 : 3;
438         /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */
439         if (tccp->csty & J2K_CCP_CSTY_PRT) {
440           pdx = tccp->prcw[resno];
441           pdy = tccp->prch[resno];
442         } else {
443           pdx = 15;
444           pdy = 15;
445         }
446         /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000)  */
447         tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx;
448         tlprcystart = int_floordivpow2(res->y0, pdy) << pdy;
449         brprcxend = int_ceildivpow2(res->x1, pdx) << pdx;
450         brprcyend = int_ceildivpow2(res->y1, pdy) << pdy;
451
452         res->pw = (brprcxend - tlprcxstart) >> pdx;
453         res->ph = (brprcyend - tlprcystart) >> pdy;
454
455         if (resno == 0) {
456           tlcbgxstart = tlprcxstart;
457           tlcbgystart = tlprcystart;
458           brcbgxend = brprcxend;
459           brcbgyend = brprcyend;
460           cbgwidthexpn = pdx;
461           cbgheightexpn = pdy;
462         } else {
463           tlcbgxstart = int_ceildivpow2(tlprcxstart, 1);
464           tlcbgystart = int_ceildivpow2(tlprcystart, 1);
465           brcbgxend = int_ceildivpow2(brprcxend, 1);
466           brcbgyend = int_ceildivpow2(brprcyend, 1);
467           cbgwidthexpn = pdx - 1;
468           cbgheightexpn = pdy - 1;
469         }
470
471         cblkwidthexpn = int_min(tccp->cblkw, cbgwidthexpn);
472         cblkheightexpn = int_min(tccp->cblkh, cbgheightexpn);
473
474         for (bandno = 0; bandno < res->numbands; bandno++) {
475           int x0b, y0b;
476           int gain, numbps;
477           j2k_stepsize_t *ss;
478           band = &res->bands[bandno];
479           band->bandno = resno == 0 ? 0 : bandno + 1;
480           x0b = (band->bandno == 1)
481             || (band->bandno == 3) ? 1 : 0;
482           y0b = (band->bandno == 2)
483             || (band->bandno == 3) ? 1 : 0;
484
485           if (band->bandno == 0) {
486             /* band border */
487             band->x0 = int_ceildivpow2(tilec->x0, levelno);
488             band->y0 = int_ceildivpow2(tilec->y0, levelno);
489             band->x1 = int_ceildivpow2(tilec->x1, levelno);
490             band->y1 = int_ceildivpow2(tilec->y1, levelno);
491           } else {
492             band->x0 =
493               int_ceildivpow2(tilec->x0 -
494                               (1 << levelno) * x0b, levelno + 1);
495             band->y0 =
496               int_ceildivpow2(tilec->y0 -
497                               (1 << levelno) * y0b, levelno + 1);
498             band->x1 =
499               int_ceildivpow2(tilec->x1 -
500                               (1 << levelno) * x0b, levelno + 1);
501             band->y1 =
502               int_ceildivpow2(tilec->y1 -
503                               (1 << levelno) * y0b, levelno + 1);
504           }
505
506           ss = &tccp->stepsizes[resno ==
507                                 0 ? 0 : 3 * (resno - 1) + bandno + 1];
508           gain =
509             tccp->qmfbid ==
510             0 ? dwt_getgain_real(band->bandno) : dwt_getgain(band->bandno);
511           numbps = img->comps[compno].prec + gain;
512           band->stepsize =
513             (int) floor((1.0 + ss->mant / 2048.0) *
514                         pow(2.0, numbps - ss->expn) * 8192.0);
515           band->numbps = ss->expn + tccp->numgbits - 1; /* WHY -1 ? */
516
517           for (precno = 0; precno < res->pw * res->ph; precno++) {
518             int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
519             int cbgxstart =
520               tlcbgxstart + (precno % res->pw) * (1 << cbgwidthexpn);
521             int cbgystart =
522               tlcbgystart + (precno / res->pw) * (1 << cbgheightexpn);
523             int cbgxend = cbgxstart + (1 << cbgwidthexpn);
524             int cbgyend = cbgystart + (1 << cbgheightexpn);
525
526             prc = &band->precincts[precno];
527             /* precinct size (global) */
528             prc->x0 = int_max(cbgxstart, band->x0);
529             prc->y0 = int_max(cbgystart, band->y0);
530             prc->x1 = int_min(cbgxend, band->x1);
531             prc->y1 = int_min(cbgyend, band->y1);
532
533             tlcblkxstart =
534               int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn;
535             tlcblkystart =
536               int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn;
537             brcblkxend =
538               int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn;
539             brcblkyend =
540               int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn;
541             prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;
542             prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn;
543
544             free(prc->cblks);
545             prc->cblks =
546               (tcd_cblk_t *) malloc(prc->cw * prc->ch *
547                                     sizeof(tcd_cblk_t));
548
549             if (prc->incltree != NULL)
550               tgt_destroy(prc->incltree);
551             if (prc->imsbtree != NULL)
552               tgt_destroy(prc->imsbtree);
553
554             prc->incltree = tgt_create(prc->cw, prc->ch);
555             prc->imsbtree = tgt_create(prc->cw, prc->ch);
556
557             for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
558               int cblkxstart =
559                 tlcblkxstart + (cblkno % prc->cw) * (1 << cblkwidthexpn);
560               int cblkystart =
561                 tlcblkystart + (cblkno / prc->cw) * (1 << cblkheightexpn);
562               int cblkxend = cblkxstart + (1 << cblkwidthexpn);
563               int cblkyend = cblkystart + (1 << cblkheightexpn);
564               cblk = &prc->cblks[cblkno];
565
566               /* code-block size (global) */
567               cblk->x0 = int_max(cblkxstart, prc->x0);
568               cblk->y0 = int_max(cblkystart, prc->y0);
569               cblk->x1 = int_min(cblkxend, prc->x1);
570               cblk->y1 = int_min(cblkyend, prc->y1);
571
572             }
573           }
574         }
575       }
576     }
577   }
578   /* tcd_dump(&tcd_image,0); */
579 }
580
581 void tcd_init(j2k_image_t * img, j2k_cp_t * cp)
582 {
583   int tileno, compno, resno, bandno, precno, cblkno, i, j;
584   unsigned int x0 = 0, y0 = 0, x1 = 0, y1 = 0, w, h, p, q;
585   tcd_img = img;
586   tcd_cp = cp;
587   tcd_image.tw = cp->tw;
588   tcd_image.th = cp->th;
589   tcd_image.tiles =
590     (tcd_tile_t *) malloc(cp->tw * cp->th * sizeof(tcd_tile_t));
591
592   /*for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
593      j2k_tcp_t *tcp = &cp->tcps[tileno];
594      tcd_tile_t *tile = &tcd_image.tiles[tileno]; */
595
596   for (i = 0; i < cp->tileno_size; i++) {
597     j2k_tcp_t *tcp = &cp->tcps[cp->tileno[i]];
598     tcd_tile_t *tile = &tcd_image.tiles[cp->tileno[i]];
599     tileno = cp->tileno[i];
600
601
602     //              int previous_x0, previous_x1, previous_y0, previous_y1;
603     /* cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
604     p = tileno % cp->tw;        /* si numerotation matricielle .. */
605     q = tileno / cp->tw;        /* .. coordonnees de la tile (q,p) q pour ligne et p pour colonne */
606
607     /* 4 borders of the tile rescale on the image if necessary */
608     tile->x0 = int_max(cp->tx0 + p * cp->tdx, img->x0);
609     tile->y0 = int_max(cp->ty0 + q * cp->tdy, img->y0);
610     tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, img->x1);
611     tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, img->y1);
612
613     tile->numcomps = img->numcomps;
614     tile->comps =
615       (tcd_tilecomp_t *) malloc(img->numcomps * sizeof(tcd_tilecomp_t));
616     for (compno = 0; compno < tile->numcomps; compno++) {
617       j2k_tccp_t *tccp = &tcp->tccps[compno];
618       tcd_tilecomp_t *tilec = &tile->comps[compno];
619       /* border of each tile component (global) */
620       tilec->x0 = int_ceildiv(tile->x0, img->comps[compno].dx);
621       tilec->y0 = int_ceildiv(tile->y0, img->comps[compno].dy);
622       tilec->x1 = int_ceildiv(tile->x1, img->comps[compno].dx);
623       tilec->y1 = int_ceildiv(tile->y1, img->comps[compno].dy);
624
625       tilec->data =
626         (int *) malloc((tilec->x1 - tilec->x0) *
627                        (tilec->y1 - tilec->y0) * sizeof(int));
628       tilec->numresolutions = tccp->numresolutions;
629       tilec->resolutions =
630         (tcd_resolution_t *) malloc(tilec->numresolutions *
631                                     sizeof(tcd_resolution_t));
632       for (resno = 0; resno < tilec->numresolutions; resno++) {
633         int pdx, pdy;
634         int levelno = tilec->numresolutions - 1 - resno;
635         int tlprcxstart, tlprcystart, brprcxend, brprcyend;
636         int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend;
637         int cbgwidthexpn, cbgheightexpn;
638         int cblkwidthexpn, cblkheightexpn;
639         tcd_resolution_t *res = &tilec->resolutions[resno];
640
641         /* border for each resolution level (global) */
642         res->x0 = int_ceildivpow2(tilec->x0, levelno);
643         res->y0 = int_ceildivpow2(tilec->y0, levelno);
644         res->x1 = int_ceildivpow2(tilec->x1, levelno);
645         res->y1 = int_ceildivpow2(tilec->y1, levelno);
646
647         res->numbands = resno == 0 ? 1 : 3;
648         /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */
649         if (tccp->csty & J2K_CCP_CSTY_PRT) {
650           pdx = tccp->prcw[resno];
651           pdy = tccp->prch[resno];
652         } else {
653           pdx = 15;
654           pdy = 15;
655         }
656         /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000)  */
657         tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx;
658         tlprcystart = int_floordivpow2(res->y0, pdy) << pdy;
659         brprcxend = int_ceildivpow2(res->x1, pdx) << pdx;
660         brprcyend = int_ceildivpow2(res->y1, pdy) << pdy;
661         res->pw = (brprcxend - tlprcxstart) >> pdx;
662         res->ph = (brprcyend - tlprcystart) >> pdy;
663
664         if (resno == 0) {
665           tlcbgxstart = tlprcxstart;
666           tlcbgystart = tlprcystart;
667           brcbgxend = brprcxend;
668           brcbgyend = brprcyend;
669           cbgwidthexpn = pdx;
670           cbgheightexpn = pdy;
671         } else {
672           tlcbgxstart = int_ceildivpow2(tlprcxstart, 1);
673           tlcbgystart = int_ceildivpow2(tlprcystart, 1);
674           brcbgxend = int_ceildivpow2(brprcxend, 1);
675           brcbgyend = int_ceildivpow2(brprcyend, 1);
676           cbgwidthexpn = pdx - 1;
677           cbgheightexpn = pdy - 1;
678         }
679
680         cblkwidthexpn = int_min(tccp->cblkw, cbgwidthexpn);
681         cblkheightexpn = int_min(tccp->cblkh, cbgheightexpn);
682
683         for (bandno = 0; bandno < res->numbands; bandno++) {
684           int x0b, y0b;
685           int gain, numbps;
686           j2k_stepsize_t *ss;
687           tcd_band_t *band = &res->bands[bandno];
688           band->bandno = resno == 0 ? 0 : bandno + 1;
689           x0b = (band->bandno == 1)
690             || (band->bandno == 3) ? 1 : 0;
691           y0b = (band->bandno == 2)
692             || (band->bandno == 3) ? 1 : 0;
693
694           if (band->bandno == 0) {
695             /* band border (global) */
696             band->x0 = int_ceildivpow2(tilec->x0, levelno);
697             band->y0 = int_ceildivpow2(tilec->y0, levelno);
698             band->x1 = int_ceildivpow2(tilec->x1, levelno);
699             band->y1 = int_ceildivpow2(tilec->y1, levelno);
700           } else {
701             /* band border (global) */
702             band->x0 =
703               int_ceildivpow2(tilec->x0 -
704                               (1 << levelno) * x0b, levelno + 1);
705             band->y0 =
706               int_ceildivpow2(tilec->y0 -
707                               (1 << levelno) * y0b, levelno + 1);
708             band->x1 =
709               int_ceildivpow2(tilec->x1 -
710                               (1 << levelno) * x0b, levelno + 1);
711             band->y1 =
712               int_ceildivpow2(tilec->y1 -
713                               (1 << levelno) * y0b, levelno + 1);
714           }
715
716           ss = &tccp->stepsizes[resno ==
717                                 0 ? 0 : 3 * (resno - 1) + bandno + 1];
718           gain =
719             tccp->qmfbid ==
720             0 ? dwt_getgain_real(band->bandno) : dwt_getgain(band->bandno);
721           numbps = img->comps[compno].prec + gain;
722           band->stepsize =
723             (int) floor((1.0 + ss->mant / 2048.0) *
724                         pow(2.0, numbps - ss->expn) * 8192.0);
725           band->numbps = ss->expn + tccp->numgbits - 1; /* WHY -1 ? */
726
727           band->precincts =
728             (tcd_precinct_t *) malloc(res->pw * res->ph *
729                                       sizeof(tcd_precinct_t));
730
731           for (precno = 0; precno < res->pw * res->ph; precno++) {
732             int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
733             int cbgxstart =
734               tlcbgxstart + (precno % res->pw) * (1 << cbgwidthexpn);
735             int cbgystart =
736               tlcbgystart + (precno / res->pw) * (1 << cbgheightexpn);
737             int cbgxend = cbgxstart + (1 << cbgwidthexpn);
738             int cbgyend = cbgystart + (1 << cbgheightexpn);
739             tcd_precinct_t *prc = &band->precincts[precno];
740             /* precinct size (global) */
741             prc->x0 = int_max(cbgxstart, band->x0);
742             prc->y0 = int_max(cbgystart, band->y0);
743             prc->x1 = int_min(cbgxend, band->x1);
744             prc->y1 = int_min(cbgyend, band->y1);
745
746             tlcblkxstart =
747               int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn;
748             tlcblkystart =
749               int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn;
750             brcblkxend =
751               int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn;
752             brcblkyend =
753               int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn;
754             prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;
755             prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn;
756
757             prc->cblks =
758               (tcd_cblk_t *) malloc(prc->cw * prc->ch *
759                                     sizeof(tcd_cblk_t));
760
761             prc->incltree = tgt_create(prc->cw, prc->ch);
762             prc->imsbtree = tgt_create(prc->cw, prc->ch);
763
764             for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
765               int cblkxstart =
766                 tlcblkxstart + (cblkno % prc->cw) * (1 << cblkwidthexpn);
767               int cblkystart =
768                 tlcblkystart + (cblkno / prc->cw) * (1 << cblkheightexpn);
769               int cblkxend = cblkxstart + (1 << cblkwidthexpn);
770               int cblkyend = cblkystart + (1 << cblkheightexpn);
771               tcd_cblk_t *cblk = &prc->cblks[cblkno];
772               /* code-block size (global) */
773               cblk->x0 = int_max(cblkxstart, prc->x0);
774               cblk->y0 = int_max(cblkystart, prc->y0);
775               cblk->x1 = int_min(cblkxend, prc->x1);
776               cblk->y1 = int_min(cblkyend, prc->y1);
777             }
778           }
779         }
780       }
781     }
782   }
783   /* tcd_dump(&tcd_image,0); */
784
785
786   /* Allocate place to store the date decoded = fianl image */
787   /* Place limited by the tile really present in the codestream */
788   for (i = 0; i < img->numcomps; i++) {
789     for (j = 0; j < cp->tileno_size; j++) {
790       tileno = cp->tileno[j];
791       x0 = j == 0 ? tcd_image.tiles[tileno].x0 : int_min(x0,
792                                                          tcd_image.
793                                                          tiles[tileno].x0);
794       y0 = j == 0 ? tcd_image.tiles[tileno].y0 : int_min(y0,
795                                                          tcd_image.
796                                                          tiles[tileno].y0);
797       x1 = j == 0 ? tcd_image.tiles[tileno].x1 : int_max(x1,
798                                                          tcd_image.
799                                                          tiles[tileno].x1);
800       y1 = j == 0 ? tcd_image.tiles[tileno].y1 : int_max(y1,
801                                                          tcd_image.
802                                                          tiles[tileno].y1);
803     }
804     w = int_ceildiv(x1 - x0, img->comps[i].dx);
805     h = int_ceildiv(y1 - y0, img->comps[i].dy);
806     img->comps[i].data = (int *) calloc(w * h, sizeof(int));
807     img->comps[i].w = w;
808     img->comps[i].h = h;
809     img->comps[i].x0 = x0;
810     img->comps[i].y0 = y0;
811   }
812
813 }
814
815 void tcd_makelayer_fixed(int layno, int final)
816 {
817   int compno, resno, bandno, precno, cblkno;
818   int value;                    //, matrice[tcd_tcp->numlayers][tcd_tile->comps[0].numresolutions][3];
819   int matrice[10][10][3];
820   int i, j, k;
821
822   /*matrice=(int*)malloc(tcd_tcp->numlayers*tcd_tile->comps[0].numresolutions*3*sizeof(int)); */
823
824   for (compno = 0; compno < tcd_tile->numcomps; compno++) {
825     tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
826     for (i = 0; i < tcd_tcp->numlayers; i++) {
827       for (j = 0; j < tilec->numresolutions; j++) {
828         for (k = 0; k < 3; k++) {
829           matrice[i][j][k] =
830             (int) (tcd_cp->
831                    matrice[i * tilec->numresolutions * 3 +
832                            j * 3 +
833                            k] *
834                    (float) (tcd_img->comps[compno].prec / 16.0));
835     }}}
836
837     for (resno = 0; resno < tilec->numresolutions; resno++) {
838       tcd_resolution_t *res = &tilec->resolutions[resno];
839       for (bandno = 0; bandno < res->numbands; bandno++) {
840         tcd_band_t *band = &res->bands[bandno];
841         for (precno = 0; precno < res->pw * res->ph; precno++) {
842           tcd_precinct_t *prc = &band->precincts[precno];
843           for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
844             tcd_cblk_t *cblk = &prc->cblks[cblkno];
845             tcd_layer_t *layer = &cblk->layers[layno];
846             int n;
847             int imsb = tcd_img->comps[compno].prec - cblk->numbps;      /* number of bit-plan equal to zero */
848             /* Correction of the matrix of coefficient to include the IMSB information */
849
850             if (layno == 0) {
851               value = matrice[layno][resno][bandno];
852               if (imsb >= value)
853                 value = 0;
854               else
855                 value -= imsb;
856             } else {
857               value =
858                 matrice[layno][resno][bandno] -
859                 matrice[layno - 1][resno][bandno];
860               if (imsb >= matrice[layno - 1][resno][bandno]) {
861                 value -= (imsb - matrice[layno - 1][resno][bandno]);
862                 if (value < 0)
863                   value = 0;
864               }
865             }
866
867             if (layno == 0)
868               cblk->numpassesinlayers = 0;
869
870             n = cblk->numpassesinlayers;
871             if (cblk->numpassesinlayers == 0) {
872               if (value != 0)
873                 n = 3 * value - 2 + cblk->numpassesinlayers;
874               else
875                 n = cblk->numpassesinlayers;
876             } else
877               n = 3 * value + cblk->numpassesinlayers;
878
879             layer->numpasses = n - cblk->numpassesinlayers;
880
881             if (!layer->numpasses)
882               continue;
883
884             if (cblk->numpassesinlayers == 0) {
885               layer->len = cblk->passes[n - 1].rate;
886               layer->data = cblk->data;
887             } else {
888               layer->len =
889                 cblk->passes[n - 1].rate -
890                 cblk->passes[cblk->numpassesinlayers - 1].rate;
891               layer->data =
892                 cblk->data +
893                 cblk->passes[cblk->numpassesinlayers - 1].rate;
894             }
895             if (final)
896               cblk->numpassesinlayers = n;
897           }
898         }
899       }
900     }
901   }
902 }
903
904 void tcd_rateallocate_fixed()
905 {
906   int layno;
907
908   for (layno = 0; layno < tcd_tcp->numlayers; layno++) {
909     tcd_makelayer_fixed(layno, 1);
910   }
911 }
912
913 void tcd_makelayer(int layno, double thresh, int final)
914 {
915   int compno, resno, bandno, precno, cblkno, passno;
916   for (compno = 0; compno < tcd_tile->numcomps; compno++) {
917     tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
918     for (resno = 0; resno < tilec->numresolutions; resno++) {
919       tcd_resolution_t *res = &tilec->resolutions[resno];
920       for (bandno = 0; bandno < res->numbands; bandno++) {
921         tcd_band_t *band = &res->bands[bandno];
922         for (precno = 0; precno < res->pw * res->ph; precno++) {
923           tcd_precinct_t *prc = &band->precincts[precno];
924           for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
925             tcd_cblk_t *cblk = &prc->cblks[cblkno];
926             tcd_layer_t *layer = &cblk->layers[layno];
927             int n;
928
929             if (layno == 0) {
930               cblk->numpassesinlayers = 0;
931             }
932             n = cblk->numpassesinlayers;
933             for (passno = cblk->numpassesinlayers;
934                  passno < cblk->totalpasses; passno++) {
935               int dr;
936               double dd;
937               tcd_pass_t *pass = &cblk->passes[passno];
938               if (n == 0) {
939                 dr = pass->rate;
940                 dd = pass->distortiondec;
941               } else {
942                 dr = pass->rate - cblk->passes[n - 1].rate;
943                 dd = pass->distortiondec - cblk->passes[n -
944                                                         1].distortiondec;
945               }
946               if (dr == 0) {
947                 if (dd != 0)
948                   n = passno + 1;
949                 continue;
950               }
951               if (dd / dr > thresh)
952                 n = passno + 1;
953             }
954             layer->numpasses = n - cblk->numpassesinlayers;
955             if (!layer->numpasses)
956               continue;
957             if (cblk->numpassesinlayers == 0) {
958               layer->len = cblk->passes[n - 1].rate;
959               layer->data = cblk->data;
960               layer->disto = cblk->passes[n - 1].distortiondec;
961             } else {
962               layer->len = cblk->passes[n - 1].rate -
963                 cblk->passes[cblk->numpassesinlayers - 1].rate;
964               layer->data =
965                 cblk->data +
966                 cblk->passes[cblk->numpassesinlayers - 1].rate;
967               layer->disto =
968                 cblk->passes[n - 1].distortiondec -
969                 cblk->passes[cblk->numpassesinlayers - 1].distortiondec;
970             }
971
972             if (final)
973               cblk->numpassesinlayers = n;
974           }
975         }
976       }
977     }
978   }
979 }
980
981 void tcd_rateallocate(unsigned char *dest, int len, info_image * info_IM)
982 {
983   int compno, resno, bandno, precno, cblkno, passno, layno;
984   double min, max;
985   min = DBL_MAX;
986   max = 0;
987
988   for (compno = 0; compno < tcd_tile->numcomps; compno++) {
989     tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
990     for (resno = 0; resno < tilec->numresolutions; resno++) {
991       tcd_resolution_t *res = &tilec->resolutions[resno];
992       for (bandno = 0; bandno < res->numbands; bandno++) {
993         tcd_band_t *band = &res->bands[bandno];
994         for (precno = 0; precno < res->pw * res->ph; precno++) {
995           tcd_precinct_t *prc = &band->precincts[precno];
996           for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
997             tcd_cblk_t *cblk = &prc->cblks[cblkno];
998             for (passno = 0; passno < cblk->totalpasses; passno++) {
999               tcd_pass_t *pass = &cblk->passes[passno];
1000               int dr;
1001               double dd, rdslope;
1002               if (passno == 0) {
1003                 dr = pass->rate;
1004                 dd = pass->distortiondec;
1005               } else {
1006                 dr = pass->rate - cblk->passes[passno - 1].rate;
1007                 dd = pass->distortiondec -
1008                   cblk->passes[passno - 1].distortiondec;
1009               }
1010               if (dr == 0) {
1011                 continue;
1012               }
1013               rdslope = dd / dr;
1014               if (rdslope < min) {
1015                 min = rdslope;
1016               }
1017               if (rdslope > max) {
1018                 max = rdslope;
1019               }
1020             }                   /* passno */
1021           }                     /* cbklno */
1022         }                       /* precno */
1023       }                         /* bandno */
1024     }                           /* resno */
1025   }                             /* compno */
1026   if (info_IM->index_on) {      /* Threshold for Marcela Index */
1027     info_IM->tile[tcd_tileno].thresh =
1028       (double *) malloc(tcd_tcp->numlayers * sizeof(double));
1029   }
1030   for (layno = 0; layno < tcd_tcp->numlayers; layno++) {
1031     volatile double lo = min;
1032     volatile double hi = max;
1033     volatile int success = 0;
1034     volatile int maxlen = int_min(tcd_tcp->rates[layno], len);
1035     volatile double goodthresh;
1036     volatile int goodlen;
1037     volatile int i;
1038
1039     for (i = 0; i < 32; i++) {
1040       volatile double thresh = (lo + hi) / 2;
1041       int l;
1042
1043       tcd_makelayer(layno, thresh, 0);
1044
1045       l = t2_encode_packets(tcd_img, tcd_cp, tcd_tileno, tcd_tile,
1046                             layno + 1, dest, maxlen, info_IM);
1047       /* fprintf(stderr, "rate alloc: len=%d, max=%d\n", l, maxlen); */
1048       if (l == -999) {
1049         lo = thresh;
1050         continue;
1051       }
1052
1053       hi = thresh;
1054       success = 1;
1055       goodthresh = thresh;
1056       goodlen = l;
1057     }
1058
1059     if (!success) {
1060       longjmp(j2k_error, 1);
1061     }
1062
1063     if (info_IM->index_on) {    /* Threshold for Marcela Index */
1064       info_IM->tile[tcd_tileno].thresh[layno] = goodthresh;
1065     }
1066     tcd_makelayer(layno, goodthresh, 1);
1067   }
1068 }
1069
1070 int tcd_encode_tile_pxm(int tileno, unsigned char *dest, int len,
1071                         info_image * info_IM)
1072 {
1073   int compno;
1074   int l;
1075   clock_t time7;
1076   tcd_tile_t *tile;
1077   j2k_tcp_t *tcp = &tcd_cp->tcps[0];
1078   j2k_tccp_t *tccp = &tcp->tccps[0];
1079
1080   tcd_tileno = tileno;
1081   tcd_tile = tcd_image.tiles;
1082   tcd_tcp = &tcd_cp->tcps[tileno];
1083   tile = tcd_tile;
1084   /* INDEX >> "Precinct_nb_X et Precinct_nb_Y" */
1085   if (info_IM->index_on) {
1086     tcd_tilecomp_t *tilec_idx = &tile->comps[0];        /* old parser version */
1087     tcd_resolution_t *res_idx = &tilec_idx->resolutions[0];     /* old parser version */
1088
1089     info_IM->tile[tileno].pw = res_idx->pw;
1090     info_IM->tile[tileno].ph = res_idx->ph;
1091
1092     info_IM->pw = res_idx->pw;  /* old parser version */
1093     info_IM->ph = res_idx->ph;  /* old parser version */
1094     info_IM->pdx = 1 << tccp->prcw[tccp->numresolutions - 1];
1095     info_IM->pdy = 1 << tccp->prch[tccp->numresolutions - 1];
1096   }
1097   /* << INDEX */
1098
1099 /*---------------TILE-------------------*/
1100
1101   time7 = clock();
1102
1103   for (compno = 0; compno < tile->numcomps; compno++) {
1104     FILE *src;
1105     char tmp[256];
1106     int k;
1107     unsigned char elmt;
1108     int i, j;
1109     int tw, w;
1110     tcd_tilecomp_t *tilec = &tile->comps[compno];
1111     int adjust =
1112       tcd_img->comps[compno].sgnd ? 0 : 1 << (tcd_img->comps[compno].
1113                                               prec - 1);
1114     int offset_x, offset_y;
1115
1116     offset_x = int_ceildiv(tcd_img->x0, tcd_img->comps[compno].dx);
1117     offset_y = int_ceildiv(tcd_img->y0, tcd_img->comps[compno].dy);
1118     tw = tilec->x1 - tilec->x0;
1119     w = int_ceildiv(tcd_img->x1 - tcd_img->x0, tcd_img->comps[compno].dx);
1120     sprintf(tmp, "Compo%d", compno);    /* component file */
1121     src = fopen(tmp, "rb");
1122     if (!src) {
1123       fprintf(stderr, "failed to open %s for reading\n", tmp);
1124       return 1;
1125     }
1126
1127     /* read the Compo file to extract data of the tile */
1128     k = 0;
1129     fseek(src, (tilec->x0 - offset_x) + (tilec->y0 - offset_y) * w,
1130           SEEK_SET);
1131     k = (tilec->x0 - offset_x) + (tilec->y0 - offset_y) * w;
1132     for (j = tilec->y0; j < tilec->y1; j++) {
1133       for (i = tilec->x0; i < tilec->x1; i++) {
1134         if (tcd_tcp->tccps[compno].qmfbid == 1) {
1135           elmt = fgetc(src);
1136           tilec->data[i - tilec->x0 + (j - tilec->y0) * tw] =
1137             elmt - adjust;
1138           k++;
1139         } else if (tcd_tcp->tccps[compno].qmfbid == 0) {
1140           elmt = fgetc(src);
1141           tilec->data[i - tilec->x0 + (j - tilec->y0) * tw] =
1142             (elmt - adjust) << 13;
1143           k++;
1144         }
1145       }
1146       fseek(src, (tilec->x0 - offset_x) + (j + 1 - offset_y) * w - k,
1147             SEEK_CUR);
1148       k = tilec->x0 - offset_x + (j + 1 - offset_y) * w;
1149
1150     }
1151     fclose(src);
1152   }
1153
1154 /*----------------MCT-------------------*/
1155
1156   if (tcd_tcp->mct) {
1157     if (tcd_tcp->tccps[0].qmfbid == 0) {
1158       mct_encode_real(tile->comps[0].data, tile->comps[1].data,
1159                       tile->comps[2].data,
1160                       (tile->comps[0].x1 -
1161                        tile->comps[0].x0) * (tile->comps[0].y1 -
1162                                              tile->comps[0].y0));
1163     } else {
1164       mct_encode(tile->comps[0].data, tile->comps[1].data,
1165                  tile->comps[2].data,
1166                  (tile->comps[0].x1 -
1167                   tile->comps[0].x0) * (tile->comps[0].y1 -
1168                                         tile->comps[0].y0));
1169     }
1170   }
1171 /*----------------DWT---------------------*/
1172
1173   /* time3=clock(); */
1174   for (compno = 0; compno < tile->numcomps; compno++) {
1175     tcd_tilecomp_t *tilec = &tile->comps[compno];
1176     if (tcd_tcp->tccps[compno].qmfbid == 1) {
1177       dwt_encode(tilec->data, tilec->x1 - tilec->x0,
1178                  tilec->y1 - tilec->y0, tilec, tilec->numresolutions - 1);
1179     } else if (tcd_tcp->tccps[compno].qmfbid == 0) {
1180       dwt_encode_real(tilec->data, tilec->x1 - tilec->x0,
1181                       tilec->y1 - tilec->y0, tilec,
1182                       tilec->numresolutions - 1);
1183     }
1184   }
1185 /*------------------TIER1-----------------*/
1186
1187   t1_init_luts();
1188   t1_encode_cblks(tile, tcd_tcp);
1189
1190 /*-----------RATE-ALLOCATE------------------*/
1191   info_IM->index_write = 0;     /* INDEX     */
1192
1193   if (tcd_cp->disto_alloc)
1194     /* Normal Rate/distortion allocation */
1195     tcd_rateallocate(dest, len, info_IM);
1196   else
1197     /* Fixed layer allocation */
1198     tcd_rateallocate_fixed();
1199
1200 /*--------------TIER2------------------*/
1201   info_IM->index_write = 1;     /* INDEX     */
1202   l = t2_encode_packets(tcd_img, tcd_cp, tileno, tile,
1203                         tcd_tcp->numlayers, dest, len, info_IM);
1204 /*---------------CLEAN-------------------*/
1205
1206   time7 = clock() - time7;
1207   printf("total:     %ld.%.3ld s\n", time7 / CLOCKS_PER_SEC,
1208          (time7 % CLOCKS_PER_SEC) * 1000 / CLOCKS_PER_SEC);
1209
1210   /* cleaning memory */
1211   for (compno = 0; compno < tile->numcomps; compno++) {
1212     tilec = &tile->comps[compno];
1213     free(tilec->data);
1214   }
1215
1216   return l;
1217 }
1218
1219 int tcd_encode_tile_pgx(int tileno, unsigned char *dest, int len,
1220                         info_image * info_IM)
1221 {
1222   int compno;
1223   int l;
1224   clock_t time;
1225   tcd_tile_t *tile;
1226   j2k_tcp_t *tcp = &tcd_cp->tcps[0];
1227   j2k_tccp_t *tccp = &tcp->tccps[0];
1228
1229   tcd_tileno = tileno;
1230   tcd_tile = tcd_image.tiles;
1231   tcd_tcp = &tcd_cp->tcps[tileno];
1232   tile = tcd_tile;
1233   /* INDEX >> "Precinct_nb_X et Precinct_nb_Y" */
1234   if (info_IM->index_on) {
1235     tcd_tilecomp_t *tilec_idx = &tile->comps[0];
1236     tcd_resolution_t *res_idx = &tilec_idx->resolutions[0];
1237     info_IM->tile[tileno].pw = res_idx->pw;
1238     info_IM->tile[tileno].ph = res_idx->ph;
1239     info_IM->pw = res_idx->pw;  /* old parser version */
1240     info_IM->ph = res_idx->ph;  /* old parser version */
1241     info_IM->pdx = 1 << tccp->prcw[tccp->numresolutions - 1];
1242     info_IM->pdy = 1 << tccp->prch[tccp->numresolutions - 1];
1243   }
1244   /* << INDEX */
1245 /*---------------TILE-------------------*/
1246   time = clock();
1247
1248   for (compno = 0; compno < tile->numcomps; compno++) {
1249     FILE *src;
1250     char tmp[256];
1251     int k;
1252     int elmt;
1253     int i, j;
1254     int tw, w;
1255     tcd_tilecomp_t *tilec = &tile->comps[compno];
1256     int adjust =
1257       tcd_img->comps[compno].sgnd ? 0 : 1 << (tcd_img->comps[compno].
1258                                               prec - 1);
1259     int offset_x, offset_y;
1260
1261     offset_x = int_ceildiv(tcd_img->x0, tcd_img->comps[compno].dx);
1262     offset_y = int_ceildiv(tcd_img->y0, tcd_img->comps[compno].dy);
1263     tw = tilec->x1 - tilec->x0;
1264     w = int_ceildiv(tcd_img->x1 - tcd_img->x0, tcd_img->comps[compno].dx);
1265     sprintf(tmp, "bandtile%d", tileno / tcd_cp->tw + 1);        /* bandtile file opening */
1266     src = fopen(tmp, "rb");
1267     if (!src) {
1268       fprintf(stderr, "failed to open %s for reading\n", tmp);
1269       return 1;
1270     }
1271     /* Extract data from bandtile file limited to the current tile */
1272     k = 0;
1273     while (k < tilec->x0 - offset_x) {
1274       k++;
1275       fscanf(src, "%d", &elmt);
1276     }
1277
1278     for (j = 0; j < tilec->y1 - tilec->y0; j++) {
1279       for (i = tilec->x0; i < tilec->x1; i++) {
1280         if (tcd_tcp->tccps[compno].qmfbid == 1) {
1281           fscanf(src, "%d", &elmt);
1282           tilec->data[i - tilec->x0 + (j) * tw] = elmt - adjust;
1283           k++;
1284         } else if (tcd_tcp->tccps[compno].qmfbid == 0) {
1285           fscanf(src, "%d", &elmt);
1286           tilec->data[i - tilec->x0 + (j) * tw] = (elmt - adjust) << 13;
1287           k++;
1288         }
1289       }
1290       while (k < tilec->x0 - offset_x + (j + 1) * w) {
1291         k++;
1292         fscanf(src, "%d", &elmt);
1293       }
1294     }
1295     fclose(src);
1296   }
1297
1298 /*----------------MCT-------------------*/
1299
1300   if (tcd_tcp->mct) {
1301     if (tcd_tcp->tccps[0].qmfbid == 0) {
1302       mct_encode_real(tile->comps[0].data, tile->comps[1].data,
1303                       tile->comps[2].data,
1304                       (tile->comps[0].x1 -
1305                        tile->comps[0].x0) * (tile->comps[0].y1 -
1306                                              tile->comps[0].y0));
1307     } else {
1308       mct_encode(tile->comps[0].data, tile->comps[1].data,
1309                  tile->comps[2].data,
1310                  (tile->comps[0].x1 -
1311                   tile->comps[0].x0) * (tile->comps[0].y1 -
1312                                         tile->comps[0].y0));
1313     }
1314   }
1315
1316 /*----------------DWT---------------------*/
1317
1318   for (compno = 0; compno < tile->numcomps; compno++) {
1319     tcd_tilecomp_t *tilec = &tile->comps[compno];
1320     if (tcd_tcp->tccps[compno].qmfbid == 1) {
1321       dwt_encode(tilec->data, tilec->x1 - tilec->x0,
1322                  tilec->y1 - tilec->y0, tilec, tilec->numresolutions - 1);
1323     } else if (tcd_tcp->tccps[compno].qmfbid == 0) {
1324       dwt_encode_real(tilec->data, tilec->x1 - tilec->x0,
1325                       tilec->y1 - tilec->y0, tilec,
1326                       tilec->numresolutions - 1);
1327     }
1328   }
1329
1330 /*------------------TIER1-----------------*/
1331
1332   t1_init_luts();
1333   t1_encode_cblks(tile, tcd_tcp);
1334
1335 /*-----------RATE-ALLOCATE------------------*/
1336   info_IM->index_write = 0;     /* INDEX */
1337
1338   info_IM->index_write = 0;     /* INDEX */
1339
1340   if (tcd_cp->disto_alloc)
1341     /* Normal Rate/distortion allocation */
1342     tcd_rateallocate(dest, len, info_IM);
1343   else
1344     /* Fixed layer allocation */
1345     tcd_rateallocate_fixed();
1346
1347 /*--------------TIER2------------------*/
1348   info_IM->index_write = 1;     /* INDEX */
1349
1350   l = t2_encode_packets(tcd_img, tcd_cp, tileno, tile,
1351                         tcd_tcp->numlayers, dest, len, info_IM);
1352
1353  /*---------------CLEAN-------------------*/
1354   time = clock() - time;
1355   printf("total:     %ld.%.3ld s\n", time / CLOCKS_PER_SEC,
1356          (time % CLOCKS_PER_SEC) * 1000 / CLOCKS_PER_SEC);
1357
1358   for (compno = 0; compno < tile->numcomps; compno++) {
1359     tilec = &tile->comps[compno];
1360     free(tilec->data);
1361   }
1362
1363   return l;
1364 }
1365
1366
1367 int tcd_decode_tile(unsigned char *src, int len, int tileno)
1368 {
1369   int l;
1370   int compno;
1371   int eof = 0;
1372   clock_t time;
1373   tcd_tile_t *tile;
1374
1375   tcd_tileno = tileno;
1376   tcd_tile = &tcd_image.tiles[tileno];
1377   tcd_tcp = &tcd_cp->tcps[tileno];
1378   tile = tcd_tile;
1379
1380   time = clock();
1381
1382   fprintf(stderr, "tile decoding time %d/%d: ", tileno + 1,
1383           tcd_cp->tw * tcd_cp->th);
1384
1385         /*--------------TIER2------------------*/
1386
1387   l = t2_decode_packets(src, len, tcd_img, tcd_cp, tileno, tile);
1388
1389   if (l == -999) {
1390     eof = 1;
1391     fprintf(stderr, "tcd_decode: incomplete bistream\n");
1392   }
1393
1394         /*------------------TIER1-----------------*/
1395   t1_init_luts();
1396   t1_decode_cblks(tile, tcd_tcp);
1397
1398         /*----------------DWT---------------------*/
1399
1400   for (compno = 0; compno < tile->numcomps; compno++) {
1401     tcd_tilecomp_t *tilec = &tile->comps[compno];
1402     if (tcd_cp->reduce_on == 1) {
1403       tcd_img->comps[compno].resno_decoded =
1404         tile->comps[compno].numresolutions - tcd_cp->reduce_value - 1;
1405     }
1406
1407
1408     if (tcd_tcp->tccps[compno].qmfbid == 1) {
1409       dwt_decode(tilec->data, tilec->x1 - tilec->x0,
1410                  tilec->y1 - tilec->y0, tilec,
1411                  tilec->numresolutions - 1,
1412                  tilec->numresolutions - 1 -
1413                  tcd_img->comps[compno].resno_decoded);
1414     } else {
1415       dwt_decode_real(tilec->data, tilec->x1 - tilec->x0,
1416                       tilec->y1 - tilec->y0, tilec,
1417                       tilec->numresolutions - 1,
1418                       tilec->numresolutions - 1 -
1419                       tcd_img->comps[compno].resno_decoded);
1420     }
1421
1422     if (tile->comps[compno].numresolutions > 0)
1423       tcd_img->comps[compno].factor =
1424         tile->comps[compno].numresolutions -
1425         (tcd_img->comps[compno].resno_decoded + 1);
1426   }
1427
1428         /*----------------MCT-------------------*/
1429
1430   if (tcd_tcp->mct) {
1431     if (tcd_tcp->tccps[0].qmfbid == 1) {
1432       mct_decode(tile->comps[0].data, tile->comps[1].data,
1433                  tile->comps[2].data,
1434                  (tile->comps[0].x1 -
1435                   tile->comps[0].x0) * (tile->comps[0].y1 -
1436                                         tile->comps[0].y0));
1437     } else {
1438       mct_decode_real(tile->comps[0].data, tile->comps[1].data,
1439                       tile->comps[2].data,
1440                       (tile->comps[0].x1 -
1441                        tile->comps[0].x0) * (tile->comps[0].y1 -
1442                                              tile->comps[0].y0));
1443     }
1444   }
1445
1446         /*---------------TILE-------------------*/
1447
1448   for (compno = 0; compno < tile->numcomps; compno++) {
1449     tcd_tilecomp_t *tilec = &tile->comps[compno];
1450     tcd_resolution_t *res =
1451       &tilec->resolutions[tcd_img->comps[compno].resno_decoded];
1452     int adjust =
1453       tcd_img->comps[compno].sgnd ? 0 : 1 << (tcd_img->comps[compno].
1454                                               prec - 1);
1455     int min =
1456       tcd_img->comps[compno].
1457       sgnd ? -(1 << (tcd_img->comps[compno].prec - 1)) : 0;
1458     int max =
1459       tcd_img->comps[compno].
1460       sgnd ? (1 << (tcd_img->comps[compno].prec - 1)) -
1461       1 : (1 << tcd_img->comps[compno].prec) - 1;
1462
1463     int tw = tilec->x1 - tilec->x0;
1464     int w = tcd_img->comps[compno].w;
1465
1466     int i, j;
1467     int offset_x = int_ceildivpow2(tcd_img->comps[compno].x0,
1468                                    tcd_img->comps[compno].factor);
1469     int offset_y = int_ceildivpow2(tcd_img->comps[compno].y0,
1470                                    tcd_img->comps[compno].factor);
1471
1472     for (j = res->y0; j < res->y1; j++) {
1473       for (i = res->x0; i < res->x1; i++) {
1474
1475         int v;
1476         if (tcd_tcp->tccps[compno].qmfbid == 1) {
1477           v = tilec->data[i - res->x0 + (j - res->y0) * tw];
1478         } else {
1479           v = tilec->data[i - res->x0 + (j - res->y0) * tw] >> 13;
1480         }
1481         v += adjust;
1482
1483         tcd_img->comps[compno].data[(i - offset_x) +
1484                                     (j - offset_y) * w] =
1485           int_clamp(v, min, max);
1486       }
1487     }
1488   }
1489
1490   time = clock() - time;
1491   fprintf(stderr, "total:     %ld.%.3ld s\n", time / CLOCKS_PER_SEC,
1492           (time % CLOCKS_PER_SEC) * 1000 / CLOCKS_PER_SEC);
1493
1494   if (eof) {
1495     longjmp(j2k_error, 1);
1496   }
1497
1498   return l;
1499 }