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