Update for version 0.8
[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(sizeof(int) * (tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0));
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(sizeof(int) * (tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0));
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;
520         tcd_img = img;
521         tcd_cp = cp;
522         tcd_image.tw = cp->tw;
523         tcd_image.th = cp->th;
524         tcd_image.tiles = (tcd_tile_t *) malloc(cp->tw * cp->th * sizeof(tcd_tile_t));
525         for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
526                 j2k_tcp_t *tcp = &cp->tcps[tileno];
527                 tcd_tile_t *tile = &tcd_image.tiles[tileno];
528                 //              int previous_x0, previous_x1, previous_y0, previous_y1;
529                 /* cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
530                 int p = tileno % cp->tw;                /* si numerotation matricielle .. */
531                 int q = tileno / cp->tw;                /* .. coordonnees de la tile (q,p) q pour ligne et p pour colonne */
532
533                 /* 4 borders of the tile rescale on the image if necessary */
534                 tile->x0 = int_max(cp->tx0 + p * cp->tdx, img->x0);
535                 tile->y0 = int_max(cp->ty0 + q * cp->tdy, img->y0);
536                 tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, img->x1);
537                 tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, img->y1);
538
539                 tile->numcomps = img->numcomps;
540                 tile->comps = (tcd_tilecomp_t *) malloc(img->numcomps * sizeof(tcd_tilecomp_t));
541                 for (compno = 0; compno < tile->numcomps; compno++) {
542                         j2k_tccp_t *tccp = &tcp->tccps[compno];
543                         tcd_tilecomp_t *tilec = &tile->comps[compno];
544                         /* border of each tile component (global) */
545                         tilec->x0 = int_ceildiv(tile->x0, img->comps[compno].dx);
546                         tilec->y0 = int_ceildiv(tile->y0, img->comps[compno].dy);
547                         tilec->x1 = int_ceildiv(tile->x1, img->comps[compno].dx);
548                         tilec->y1 = int_ceildiv(tile->y1, img->comps[compno].dy);
549
550                         tilec->data = (int *) malloc(sizeof(int) * (tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0));
551                         tilec->numresolutions = tccp->numresolutions;
552                         tilec->resolutions = (tcd_resolution_t *) malloc(tilec->numresolutions * sizeof(tcd_resolution_t));
553                         for (resno = 0; resno < tilec->numresolutions; resno++) {
554                                 int pdx, pdy;
555                                 int levelno = tilec->numresolutions - 1 - resno;
556                                 int tlprcxstart, tlprcystart, brprcxend, brprcyend;
557                                 int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend;
558                                 int cbgwidthexpn, cbgheightexpn;
559                                 int cblkwidthexpn, cblkheightexpn;
560                                 tcd_resolution_t *res = &tilec->resolutions[resno];
561
562                                 /* border for each resolution level (global) */
563                                 res->x0 = int_ceildivpow2(tilec->x0, levelno);
564                                 res->y0 = int_ceildivpow2(tilec->y0, levelno);
565                                 res->x1 = int_ceildivpow2(tilec->x1, levelno);
566                                 res->y1 = int_ceildivpow2(tilec->y1, levelno);
567
568                                 res->numbands = resno == 0 ? 1 : 3;
569                                 /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */
570                                 if (tccp->csty & J2K_CCP_CSTY_PRT) {
571                                         pdx = tccp->prcw[resno];
572                                         pdy = tccp->prch[resno];
573                                 } else {
574                                         pdx = 15;
575                                         pdy = 15;
576                                 }
577                                 /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000)  */
578                                 tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx;
579                                 tlprcystart = int_floordivpow2(res->y0, pdy) << pdy;
580                                 brprcxend = int_ceildivpow2(res->x1, pdx) << pdx;
581                                 brprcyend = int_ceildivpow2(res->y1, pdy) << pdy;
582                                 res->pw = (brprcxend - tlprcxstart) >> pdx;
583                                 res->ph = (brprcyend - tlprcystart) >> pdy;
584
585                                 if (resno == 0) {
586                                         tlcbgxstart = tlprcxstart;
587                                         tlcbgystart = tlprcystart;
588                                         brcbgxend = brprcxend;
589                                         brcbgyend = brprcyend;
590                                         cbgwidthexpn = pdx;
591                                         cbgheightexpn = pdy;
592                                 } else {
593                                         tlcbgxstart = int_ceildivpow2(tlprcxstart, 1);
594                                         tlcbgystart = int_ceildivpow2(tlprcystart, 1);
595                                         brcbgxend = int_ceildivpow2(brprcxend, 1);
596                                         brcbgyend = int_ceildivpow2(brprcyend, 1);
597                                         cbgwidthexpn = pdx - 1;
598                                         cbgheightexpn = pdy - 1;
599                                 }
600
601                                 cblkwidthexpn = int_min(tccp->cblkw, cbgwidthexpn);
602                                 cblkheightexpn = int_min(tccp->cblkh, cbgheightexpn);
603
604                                 for (bandno = 0; bandno < res->numbands; bandno++) {
605                                         int x0b, y0b;
606                                         int gain, numbps;
607                                         j2k_stepsize_t *ss;
608                                         tcd_band_t *band = &res->bands[bandno];
609                                         band->bandno = resno == 0 ? 0 : bandno + 1;
610                                         x0b = (band->bandno == 1) || (band->bandno == 3) ? 1 : 0;
611                                         y0b = (band->bandno == 2) || (band->bandno == 3) ? 1 : 0;
612
613                                         if (band->bandno == 0) {
614                                                 /* band border (global) */
615                                                 band->x0 = int_ceildivpow2(tilec->x0, levelno);
616                                                 band->y0 = int_ceildivpow2(tilec->y0, levelno);
617                                                 band->x1 = int_ceildivpow2(tilec->x1, levelno);
618                                                 band->y1 = int_ceildivpow2(tilec->y1, levelno);
619                                         } else {
620                                                 /* band border (global) */
621                                                 band->x0 = int_ceildivpow2(tilec->x0 - (1 << levelno) * x0b, levelno + 1);
622                                                 band->y0 = int_ceildivpow2(tilec->y0 - (1 << levelno) * y0b, levelno + 1);
623                                                 band->x1 = int_ceildivpow2(tilec->x1 - (1 << levelno) * x0b, levelno + 1);
624                                                 band->y1 = int_ceildivpow2(tilec->y1 - (1 << levelno) * y0b, levelno + 1);
625                                         }
626
627                                         ss = &tccp->stepsizes[resno == 0 ? 0 : 3 * (resno - 1) + bandno + 1];
628                                         gain = tccp->qmfbid == 0 ? dwt_getgain_real(band->bandno) : dwt_getgain(band->bandno);
629                                         numbps = img->comps[compno].prec + gain;
630                                         band->stepsize = (int) floor((1.0 + ss->mant / 2048.0) * pow(2.0, numbps - ss->expn) * 8192.0);
631                                         band->numbps = ss->expn + tccp->numgbits - 1;   /* WHY -1 ? */
632
633                                         band->precincts = (tcd_precinct_t *) malloc(res->pw * res->ph * sizeof(tcd_precinct_t));
634
635                                         for (precno = 0; precno < res->pw * res->ph; precno++) {
636                                                 int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
637                                                 int cbgxstart = tlcbgxstart + (precno % res->pw) * (1 << cbgwidthexpn);
638                                                 int cbgystart = tlcbgystart + (precno / res->pw) * (1 << cbgheightexpn);
639                                                 int cbgxend = cbgxstart + (1 << cbgwidthexpn);
640                                                 int cbgyend = cbgystart + (1 << cbgheightexpn);
641                                                 tcd_precinct_t *prc = &band->precincts[precno];
642                                                 /* precinct size (global) */
643                                                 prc->x0 = int_max(cbgxstart, band->x0);
644                                                 prc->y0 = int_max(cbgystart, band->y0);
645                                                 prc->x1 = int_min(cbgxend, band->x1);
646                                                 prc->y1 = int_min(cbgyend, band->y1);
647
648                                                 tlcblkxstart = int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn;
649                                                 tlcblkystart = int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn;
650                                                 brcblkxend = int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn;
651                                                 brcblkyend = int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn;
652                                                 prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;
653                                                 prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn;
654
655                                                 prc->cblks = (tcd_cblk_t *) malloc(prc->cw * prc->ch * sizeof(tcd_cblk_t));
656
657                                                 prc->incltree = tgt_create(prc->cw, prc->ch);
658                                                 prc->imsbtree = tgt_create(prc->cw, prc->ch);
659
660                                                 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
661                                                         int cblkxstart = tlcblkxstart + (cblkno % prc->cw) * (1 << cblkwidthexpn);
662                                                         int cblkystart = tlcblkystart + (cblkno / prc->cw) * (1 << cblkheightexpn);
663                                                         int cblkxend = cblkxstart + (1 << cblkwidthexpn);
664                                                         int cblkyend = cblkystart + (1 << cblkheightexpn);
665                                                         tcd_cblk_t *cblk = &prc->cblks[cblkno];
666                                                         /* code-block size (global) */
667                                                         cblk->x0 = int_max(cblkxstart, prc->x0);
668                                                         cblk->y0 = int_max(cblkystart, prc->y0);
669                                                         cblk->x1 = int_min(cblkxend, prc->x1);
670                                                         cblk->y1 = int_min(cblkyend, prc->y1);
671                                                 }
672                                         }
673                                 }
674                         }
675                 }
676         }
677         /* tcd_dump(&tcd_image,0); */
678 }
679
680 void tcd_makelayer_fixed(int layno, int final) {
681   int compno, resno, bandno, precno, cblkno;
682   int value; //, matrice[tcd_tcp->numlayers][tcd_tile->comps[0].numresolutions][3];
683   int matrice[10][10][3];
684   int i,j,k;
685
686   /*matrice=(int*)malloc(tcd_tcp->numlayers*tcd_tile->comps[0].numresolutions*3*sizeof(int));*/
687
688   for (compno=0; compno<tcd_tile->numcomps; compno++) 
689     {
690       tcd_tilecomp_t *tilec=&tcd_tile->comps[compno];
691       for (i=0;i<tcd_tcp->numlayers;i++) {
692         for (j=0;j<tilec->numresolutions;j++) {
693           for (k=0;k<3;k++) {
694             matrice[i][j][k]=(int)(tcd_cp->matrice[i*tilec->numresolutions*3+j*3+k]*(float)(tcd_img->comps[compno].prec/16.0));}}}
695       
696       for (resno=0; resno<tilec->numresolutions; resno++) {
697         tcd_resolution_t *res=&tilec->resolutions[resno];
698         for (bandno=0; bandno<res->numbands; bandno++) {
699           tcd_band_t *band=&res->bands[bandno];
700           for (precno=0; precno<res->pw*res->ph; precno++) {
701             tcd_precinct_t *prc=&band->precincts[precno];
702             for (cblkno=0; cblkno<prc->cw*prc->ch; cblkno++) {
703               tcd_cblk_t *cblk=&prc->cblks[cblkno];
704               tcd_layer_t *layer=&cblk->layers[layno];
705               int n;
706               int imsb=tcd_img->comps[compno].prec-cblk->numbps; /* number of bit-plan equal to zero */
707               /* Correction of the matrix of coefficient to include the IMSB information */
708               
709               if (layno==0)
710                 {
711                   value=matrice[layno][resno][bandno];
712                   if (imsb>=value)
713                     value=0;
714                   else
715                     value-=imsb;
716                 } else
717                   {
718                     value=matrice[layno][resno][bandno]-matrice[layno-1][resno][bandno];
719                     if (imsb>=matrice[layno-1][resno][bandno])
720                       {
721                         value-=(imsb-matrice[layno-1][resno][bandno]);
722                         if (value<0) value=0;   
723                       }
724                   }
725               
726               if (layno==0) 
727                 cblk->numpassesinlayers=0;
728               
729               n=cblk->numpassesinlayers;
730               if (cblk->numpassesinlayers==0)
731                 {
732                   if (value!=0)
733                     n=3*value-2+cblk->numpassesinlayers;
734                   else
735                     n=cblk->numpassesinlayers;
736                 } else
737                   n=3*value+cblk->numpassesinlayers;
738               
739               layer->numpasses=n-cblk->numpassesinlayers;
740               
741               if (!layer->numpasses) 
742                 continue;
743               
744               if (cblk->numpassesinlayers==0) 
745                 {
746                   layer->len=cblk->passes[n-1].rate;
747                   layer->data=cblk->data;
748                 } else 
749                   {
750                     layer->len=cblk->passes[n-1].rate-cblk->passes[cblk->numpassesinlayers-1].rate;
751                     layer->data=cblk->data+cblk->passes[cblk->numpassesinlayers-1].rate;
752                   }
753               if (final) 
754                 cblk->numpassesinlayers=n;
755             }
756           }
757         }
758       }
759     }
760 }
761
762 void tcd_rateallocate_fixed() {
763   int layno;
764
765   for (layno=0; layno<tcd_tcp->numlayers; layno++) 
766     {
767       tcd_makelayer_fixed(layno, 1);
768     }
769 }
770
771 void tcd_makelayer(int layno, double thresh, int final)
772 {
773         int compno, resno, bandno, precno, cblkno, passno;
774         for (compno = 0; compno < tcd_tile->numcomps; compno++) {
775                 tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
776                 for (resno = 0; resno < tilec->numresolutions; resno++) {
777                         tcd_resolution_t *res = &tilec->resolutions[resno];
778                         for (bandno = 0; bandno < res->numbands; bandno++) {
779                                 tcd_band_t *band = &res->bands[bandno];
780                                 for (precno = 0; precno < res->pw * res->ph; precno++) {
781                                         tcd_precinct_t *prc = &band->precincts[precno];
782                                         for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
783                                                 tcd_cblk_t *cblk = &prc->cblks[cblkno];
784                                                 tcd_layer_t *layer = &cblk->layers[layno];
785                                                 int n;
786
787                                                 if (layno == 0) {
788                                                         cblk->numpassesinlayers = 0;
789                                                 }
790                                                 n = cblk->numpassesinlayers;
791                                                 for (passno = cblk->numpassesinlayers;
792                                                                  passno < cblk->totalpasses; passno++) {
793                                                         int dr;
794                                                         double dd;
795                                                         tcd_pass_t *pass = &cblk->passes[passno];
796                                                         if (n == 0) {
797                                                                 dr = pass->rate;
798                                                                 dd = pass->distortiondec;
799                                                         } else {
800                                                                 dr = pass->rate - cblk->passes[n - 1].rate;
801                                                                 dd = pass->distortiondec - cblk->passes[n - 1].distortiondec;
802                                                         }
803                                                         if (dr == 0) {
804                                                                 if (dd != 0)
805                                                                         n = passno + 1;
806                                                                 continue;
807                                                         }
808                                                         if (dd / dr > thresh)
809                                                                 n = passno + 1;
810                                                 }
811                                                 layer->numpasses = n - cblk->numpassesinlayers;
812                                                 if (!layer->numpasses)
813                                                         continue;
814                                                 if (cblk->numpassesinlayers == 0) {
815                                                         layer->len = cblk->passes[n - 1].rate;
816                                                         layer->data = cblk->data;
817                                                         layer->disto = cblk->passes[n - 1].distortiondec;
818                                                 } else {
819                                                         layer->len = cblk->passes[n - 1].rate - 
820                                                           cblk->passes[cblk->numpassesinlayers - 1].rate;
821                                                         layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate;
822                                                         layer->disto = cblk->passes[n - 1].distortiondec - 
823                                                           cblk->passes[cblk->numpassesinlayers - 1].distortiondec;
824                                                 }
825
826                                                 if (final)
827                                                         cblk->numpassesinlayers = n;
828                                         }
829                                 }
830                         }
831                 }
832         }
833 }
834
835 void tcd_rateallocate(unsigned char *dest, int len, info_image * info_IM)
836 {
837         int compno, resno, bandno, precno, cblkno, passno, layno;
838         double min, max;
839         min = DBL_MAX;
840         max = 0;
841
842         for (compno = 0; compno < tcd_tile->numcomps; compno++) {
843                 tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
844                 for (resno = 0; resno < tilec->numresolutions; resno++) {
845                         tcd_resolution_t *res = &tilec->resolutions[resno];
846                         for (bandno = 0; bandno < res->numbands; bandno++) {
847                                 tcd_band_t *band = &res->bands[bandno];
848                                 for (precno = 0; precno < res->pw * res->ph; precno++) {
849                                         tcd_precinct_t *prc = &band->precincts[precno];
850                                         for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
851                                                 tcd_cblk_t *cblk = &prc->cblks[cblkno];
852                                                 for (passno = 0; passno < cblk->totalpasses; passno++) {
853                                                         tcd_pass_t *pass = &cblk->passes[passno];
854                                                         int dr;
855                                                         double dd, rdslope;
856                                                         if (passno == 0) {
857                                                                 dr = pass->rate;
858                                                                 dd = pass->distortiondec;
859                                                         } else {
860                                                                 dr = pass->rate - cblk->passes[passno - 1].rate;
861                                                                 dd = pass->distortiondec - cblk->passes[passno - 1].distortiondec;
862                                                         }
863                                                         if (dr == 0) {
864                                                                 continue;
865                                                         }
866                                                         rdslope = dd / dr;
867                                                         if (rdslope < min) {
868                                                                 min = rdslope;
869                                                         }
870                                                         if (rdslope > max) {
871                                                                 max = rdslope;
872                                                         }
873                                                 } /* passno */
874                                         } /* cbklno */
875                                 } /* precno */
876                         } /* bandno */
877                 } /* resno */
878         } /* compno */
879         if (info_IM->index_on) { /* Threshold for Marcela Index */
880           info_IM->tile[tcd_tileno].thresh=(double*)malloc(tcd_tcp->numlayers*sizeof(double));
881         }
882         for (layno = 0; layno < tcd_tcp->numlayers; layno++) {
883                 volatile double lo = min;
884                 volatile double hi = max;
885                 volatile int success = 0;
886                 volatile int maxlen = int_min(tcd_tcp->rates[layno], len);
887                 volatile double goodthresh;
888                 volatile int goodlen;
889                 volatile int i;
890
891                 for (i = 0; i < 32; i++) {
892                         volatile double thresh = (lo + hi) / 2;
893                         int l;
894
895                         tcd_makelayer(layno, thresh, 0);
896
897                         l = t2_encode_packets(tcd_img, tcd_cp, tcd_tileno, tcd_tile, layno + 1, dest, maxlen, info_IM);
898                         /* fprintf(stderr, "rate alloc: len=%d, max=%d\n", l, maxlen); */
899                         if (l == -999) {
900                                 lo = thresh;
901                                 continue;
902                         }
903
904                         hi = thresh;
905                         success = 1;
906                         goodthresh = thresh;
907                         goodlen = l;
908                 }
909
910                 if (!success) {
911                         longjmp(j2k_error, 1);
912                 }
913                 
914                 if (info_IM->index_on) { /* Threshold for Marcela Index */
915                   info_IM->tile[tcd_tileno].thresh[layno]=goodthresh;
916                 }
917                 tcd_makelayer(layno, goodthresh, 1);
918         }
919 }
920
921 int tcd_encode_tile_pxm(int tileno, unsigned char *dest, int len, info_image * info_IM)
922 {
923         int compno;
924         int l;
925         clock_t time7;
926         tcd_tile_t *tile;
927         j2k_tcp_t *tcp = &tcd_cp->tcps[0];
928         j2k_tccp_t *tccp = &tcp->tccps[0];
929
930         tcd_tileno = tileno;
931         tcd_tile = tcd_image.tiles;
932         tcd_tcp = &tcd_cp->tcps[tileno];
933         tile = tcd_tile;
934         /* INDEX >> "Precinct_nb_X et Precinct_nb_Y" */
935         if (info_IM->index_on) {
936                 tcd_tilecomp_t *tilec_idx = &tile->comps[0];    /* old parser version */
937                 tcd_resolution_t *res_idx = &tilec_idx->resolutions[0]; /* old parser version */
938
939                 info_IM->tile[tileno].pw = res_idx->pw;
940                 info_IM->tile[tileno].ph = res_idx->ph;
941
942                 info_IM->pw = res_idx->pw;      /* old parser version */
943                 info_IM->ph = res_idx->ph;      /* old parser version */
944                 info_IM->pdx = 1 << tccp->prcw[tccp->numresolutions - 1];
945                 info_IM->pdy = 1 << tccp->prch[tccp->numresolutions - 1];
946         }
947         /* << INDEX */
948
949 /*---------------TILE-------------------*/
950
951         time7 = clock();
952
953         for (compno = 0; compno < tile->numcomps; compno++) {
954                 FILE *src;
955                 char tmp[256];
956                 int k;
957                 unsigned char elmt;
958                 int i, j;
959                 int tw, w;
960                 tcd_tilecomp_t *tilec = &tile->comps[compno];
961                 int adjust = tcd_img->comps[compno].sgnd ? 0 : 1 << (tcd_img->comps[compno].prec - 1);
962                 int offset_x, offset_y;
963
964                 offset_x = int_ceildiv(tcd_img->x0, tcd_img->comps[compno].dx);
965                 offset_y = int_ceildiv(tcd_img->y0, tcd_img->comps[compno].dy);
966                 tw = tilec->x1 - tilec->x0;
967                 w = int_ceildiv(tcd_img->x1 - tcd_img->x0, tcd_img->comps[compno].dx);
968                 sprintf(tmp, "Compo%d", compno);        /* component file */
969                 src = fopen(tmp, "rb");
970                 if (!src) {
971                         fprintf(stderr, "failed to open %s for reading\n", tmp);
972                         return 1;
973                 }
974
975                 /* read the Compo file to extract data of the tile */
976                 k = 0;
977                 fseek(src, (tilec->x0 - offset_x) + (tilec->y0 - offset_y) * w, SEEK_SET);
978                 k = (tilec->x0 - offset_x) + (tilec->y0 - offset_y) * w;
979                 for (j = tilec->y0; j < tilec->y1; j++) {
980                         for (i = tilec->x0; i < tilec->x1; i++) {
981                                 if (tcd_tcp->tccps[compno].qmfbid == 1) {
982                                         elmt = fgetc(src);
983                                         tilec->data[i - tilec->x0 + (j - tilec->y0) * tw] = elmt - adjust;
984                                         k++;
985                                 } else if (tcd_tcp->tccps[compno].qmfbid == 0) {
986                                         elmt = fgetc(src);
987                                         tilec->data[i - tilec->x0 + (j - tilec->y0) * tw] = (elmt - adjust) << 13;
988                                         k++;
989                                 }
990                         }
991                         fseek(src, (tilec->x0 - offset_x) + (j + 1 - offset_y) * w - k, SEEK_CUR);
992                         k = tilec->x0 - offset_x + (j + 1 - offset_y) * w;
993
994                 }
995                 fclose(src);
996         }
997
998 /*----------------MCT-------------------*/
999
1000         if (tcd_tcp->mct) {
1001                 if (tcd_tcp->tccps[0].qmfbid == 0) {
1002                         mct_encode_real(tile->comps[0].data, tile->comps[1].data,tile->comps[2].data, 
1003                                         (tile->comps[0].x1 - tile->comps[0].x0) * (tile->comps[0].y1 - tile->comps[0].y0));
1004                 } else {
1005                         mct_encode(tile->comps[0].data, tile->comps[1].data, tile->comps[2].data, 
1006                                    (tile->comps[0].x1 - tile->comps[0].x0) * (tile->comps[0].y1 - tile->comps[0].y0));
1007                 }
1008         }
1009 /*----------------DWT---------------------*/
1010
1011         /* time3=clock(); */
1012         for (compno = 0; compno < tile->numcomps; compno++) {
1013                 tcd_tilecomp_t *tilec = &tile->comps[compno];
1014                 if (tcd_tcp->tccps[compno].qmfbid == 1) {
1015                         dwt_encode(tilec->data, tilec->x1 - tilec->x0, tilec->y1 - tilec->y0, tilec, tilec->numresolutions - 1);
1016                 } else if (tcd_tcp->tccps[compno].qmfbid == 0) {
1017                         dwt_encode_real(tilec->data, tilec->x1 - tilec->x0, tilec->y1 - tilec->y0, tilec, tilec->numresolutions - 1);
1018                 }
1019         }
1020 /*------------------TIER1-----------------*/
1021
1022         t1_init_luts();
1023         t1_encode_cblks(tile, tcd_tcp);
1024
1025 /*-----------RATE-ALLOCATE------------------*/
1026         info_IM->index_write = 0;                       /* INDEX     */
1027
1028         if (tcd_cp->disto_alloc)
1029           /* Normal Rate/distortion allocation */
1030           tcd_rateallocate(dest, len, info_IM);
1031         else
1032           /* Fixed layer allocation */
1033           tcd_rateallocate_fixed();
1034         
1035 /*--------------TIER2------------------*/
1036         info_IM->index_write = 1;                       /* INDEX     */
1037         l = t2_encode_packets(tcd_img, tcd_cp, tileno, tile, tcd_tcp->numlayers, dest, len, info_IM);
1038 /*---------------CLEAN-------------------*/
1039
1040         time7 = clock() - time7;
1041         printf("total:     %ld.%.3ld s\n", time7 / CLOCKS_PER_SEC, (time7 % CLOCKS_PER_SEC) * 1000 / CLOCKS_PER_SEC);
1042
1043         /* cleaning memory */
1044         for (compno = 0; compno < tile->numcomps; compno++) {
1045                 tilec = &tile->comps[compno];
1046                 free(tilec->data);
1047         }
1048
1049         return l;
1050 }
1051
1052 int tcd_encode_tile_pgx(int tileno, unsigned char *dest, int len, info_image * info_IM)
1053 {
1054         int compno;
1055         int l;
1056         clock_t time7;
1057         tcd_tile_t *tile;
1058         j2k_tcp_t *tcp = &tcd_cp->tcps[0];
1059         j2k_tccp_t *tccp = &tcp->tccps[0];
1060
1061         tcd_tileno = tileno;
1062         tcd_tile = tcd_image.tiles;
1063         tcd_tcp = &tcd_cp->tcps[tileno];
1064         tile = tcd_tile;
1065         /* INDEX >> "Precinct_nb_X et Precinct_nb_Y" */
1066         if (info_IM->index_on) {
1067                 tcd_tilecomp_t *tilec_idx = &tile->comps[0];
1068                 tcd_resolution_t *res_idx = &tilec_idx->resolutions[0];
1069                 info_IM->tile[tileno].pw = res_idx->pw;
1070                 info_IM->tile[tileno].ph = res_idx->ph;
1071                 info_IM->pw = res_idx->pw;      /* old parser version */
1072                 info_IM->ph = res_idx->ph;      /* old parser version */
1073                 info_IM->pdx = 1 << tccp->prcw[tccp->numresolutions - 1];
1074                 info_IM->pdy = 1 << tccp->prch[tccp->numresolutions - 1];
1075         }
1076         /* << INDEX */
1077 /*---------------TILE-------------------*/
1078         time7 = clock();
1079
1080         for (compno = 0; compno < tile->numcomps; compno++) {
1081                 FILE *src;
1082                 char tmp[256];
1083                 int k;
1084                 int elmt;
1085                 int i, j;
1086                 int tw, w;
1087                 tcd_tilecomp_t *tilec = &tile->comps[compno];
1088                 int adjust = tcd_img->comps[compno].sgnd ? 0 : 1 << (tcd_img->comps[compno].prec - 1);
1089                 int offset_x, offset_y;
1090
1091                 offset_x = int_ceildiv(tcd_img->x0, tcd_img->comps[compno].dx);
1092                 offset_y = int_ceildiv(tcd_img->y0, tcd_img->comps[compno].dy);
1093                 tw = tilec->x1 - tilec->x0;
1094                 w = int_ceildiv(tcd_img->x1 - tcd_img->x0, tcd_img->comps[compno].dx);
1095                 sprintf(tmp, "bandtile%d", tileno / tcd_cp->tw + 1);    /* bandtile file opening */
1096                 src = fopen(tmp, "rb");
1097                 if (!src) {
1098                         fprintf(stderr, "failed to open %s for reading\n", tmp);
1099                         return 1;
1100                 }
1101                 /* Extract data from bandtile file limited to the current tile */
1102                 k = 0;
1103                 while (k < tilec->x0 - offset_x) {
1104                         k++;
1105                         fscanf(src, "%d", &elmt);
1106                 }
1107
1108                 for (j = 0; j < tilec->y1 - tilec->y0; j++) {
1109                         for (i = tilec->x0; i < tilec->x1; i++) {
1110                                 if (tcd_tcp->tccps[compno].qmfbid == 1) {
1111                                         fscanf(src, "%d", &elmt);
1112                                         tilec->data[i - tilec->x0 + (j) * tw] = elmt - adjust;
1113                                         k++;
1114                                 } else if (tcd_tcp->tccps[compno].qmfbid == 0) {
1115                                         fscanf(src, "%d", &elmt);
1116                                         tilec->data[i - tilec->x0 + (j) * tw] = (elmt - adjust) << 13;
1117                                         k++;
1118                                 }
1119                         }
1120                         while (k < tilec->x0 - offset_x + (j + 1) * w) {
1121                                 k++;
1122                                 fscanf(src, "%d", &elmt);
1123                         }
1124                 }
1125                 fclose(src);
1126         }
1127
1128 /*----------------MCT-------------------*/
1129
1130         if (tcd_tcp->mct) {
1131                 if (tcd_tcp->tccps[0].qmfbid == 0) {
1132                         mct_encode_real(tile->comps[0].data, tile->comps[1].data, tile->comps[2].data, 
1133                                         (tile->comps[0].x1 - tile->comps[0].x0) * (tile->comps[0].y1 - tile->comps[0].y0));
1134                 } else {
1135                         mct_encode(tile->comps[0].data, tile->comps[1].data, tile->comps[2].data, 
1136                                    (tile->comps[0].x1 - tile->comps[0].x0) * (tile->comps[0].y1 - tile->comps[0].y0));
1137                 }
1138         }
1139
1140 /*----------------DWT---------------------*/
1141
1142         for (compno = 0; compno < tile->numcomps; compno++) {
1143                 tcd_tilecomp_t *tilec = &tile->comps[compno];
1144                 if (tcd_tcp->tccps[compno].qmfbid == 1) {
1145                         dwt_encode(tilec->data, tilec->x1 - tilec->x0, tilec->y1 - tilec->y0, tilec, tilec->numresolutions - 1);
1146                 } else if (tcd_tcp->tccps[compno].qmfbid == 0) {
1147                         dwt_encode_real(tilec->data, tilec->x1 - tilec->x0, tilec->y1 - tilec->y0, tilec, tilec->numresolutions - 1);
1148                 }
1149         }
1150
1151 /*------------------TIER1-----------------*/
1152         
1153         t1_init_luts();
1154         t1_encode_cblks(tile, tcd_tcp);
1155
1156 /*-----------RATE-ALLOCATE------------------*/
1157         info_IM->index_write = 0;                       /* INDEX */
1158
1159         info_IM->index_write=0; /* INDEX */
1160         
1161         if (tcd_cp->disto_alloc)
1162           /* Normal Rate/distortion allocation */
1163           tcd_rateallocate(dest, len, info_IM);
1164         else
1165           /* Fixed layer allocation */
1166           tcd_rateallocate_fixed();
1167
1168 /*--------------TIER2------------------*/
1169         info_IM->index_write = 1;                       /* INDEX */
1170
1171         l = t2_encode_packets(tcd_img, tcd_cp, tileno, tile, tcd_tcp->numlayers, dest, len, info_IM);
1172
1173  /*---------------CLEAN-------------------*/
1174         time7 = clock() - time7;
1175         printf("total:     %ld.%.3ld s\n", time7 / CLOCKS_PER_SEC, (time7 % CLOCKS_PER_SEC) * 1000 / CLOCKS_PER_SEC);
1176
1177         for (compno = 0; compno < tile->numcomps; compno++) {
1178                 tilec = &tile->comps[compno];
1179                 free(tilec->data);
1180         }
1181
1182         return l;
1183 }
1184
1185
1186 int tcd_decode_tile(unsigned char *src, int len, int tileno)
1187 {
1188         int l;
1189         int compno;
1190         int eof = 0;
1191         clock_t time1, time2, time3, time4, time5, time6;
1192         tcd_tile_t *tile;
1193
1194         tcd_tileno = tileno;
1195         tcd_tile = &tcd_image.tiles[tileno];
1196         tcd_tcp = &tcd_cp->tcps[tileno];
1197         tile = tcd_tile;
1198
1199         time6 = clock();
1200
1201         time1 = clock();
1202         printf("tile decoding time %d/%d:\n", tileno + 1, tcd_cp->tw * tcd_cp->th);
1203
1204         l = t2_decode_packets(src, len, tcd_img, tcd_cp, tileno, tile);
1205
1206         if (l == -999) {
1207                 eof = 1;
1208                 fprintf(stderr, "tcd_decode: incomplete bistream\n");
1209         }
1210         time1 = clock() - time1;
1211         
1212         /* printf("tier 2:    %ld.%.3ld s\n", time1/CLOCKS_PER_SEC, (time1%CLOCKS_PER_SEC)*1000/CLOCKS_PER_SEC);    */
1213
1214         time2 = clock();
1215         t1_init_luts();
1216         t1_decode_cblks(tile, tcd_tcp);
1217         time2 = clock() - time2;
1218         /* printf("tier 1:    %ld.%.3ld s\n", time2/CLOCKS_PER_SEC, (time2%CLOCKS_PER_SEC)*1000/CLOCKS_PER_SEC); */
1219
1220         time3 = clock();
1221         for (compno = 0; compno < tile->numcomps; compno++) 
1222           {
1223             tcd_tilecomp_t *tilec = &tile->comps[compno];
1224             if (tcd_tcp->tccps[compno].qmfbid == 1) 
1225               {
1226                 dwt_decode(tilec->data, tilec->x1 - tilec->x0,tilec->y1 - tilec->y0, tilec, tilec->numresolutions - 1);
1227               }  else 
1228                 { /*if (tcd_tcp->tccps[compno].qmfbid == 0) {*/
1229                   dwt_decode_real(tilec->data, tilec->x1 - tilec->x0, tilec->y1 - tilec->y0, tilec, tilec->numresolutions - 1);
1230                 } 
1231           }
1232
1233         time3 = clock() - time3;
1234         /* printf("dwt:       %ld.%.3ld s\n", time3/CLOCKS_PER_SEC, (time3%CLOCKS_PER_SEC)*1000/CLOCKS_PER_SEC); */
1235
1236         time4 = clock();
1237         if (tcd_tcp->mct) {
1238                 if (tcd_tcp->tccps[0].qmfbid == 0) {
1239                         mct_decode_real(tile->comps[0].data, tile->comps[1].data, tile->comps[2].data, 
1240                                         (tile->comps[0].x1 - tile->comps[0].x0) * (tile->comps[0].y1 - tile->comps[0].y0));
1241                 } else {
1242                         mct_decode(tile->comps[0].data, tile->comps[1].data, tile->comps[2].data, 
1243                                    (tile->comps[0].x1 - tile->comps[0].x0) * (tile->comps[0].y1 - tile->comps[0].y0));
1244                 }
1245         }
1246         time4 = clock() - time4;
1247         /* printf("mct:       %ld.%.3ld s\n", time4/CLOCKS_PER_SEC, (time4%CLOCKS_PER_SEC)*1000/CLOCKS_PER_SEC); */
1248
1249         time5 = clock();
1250         for (compno = 0; compno < tile->numcomps; compno++) {
1251                 tcd_tilecomp_t *tilec = &tile->comps[compno];
1252                 int adjust = tcd_img->comps[compno].sgnd ? 0 : 1 << (tcd_img->comps[compno].prec - 1);
1253                 int min = tcd_img->comps[compno].sgnd ? -(1 << (tcd_img->comps[compno].prec - 1)) : 0;
1254                 int max = tcd_img->comps[compno].sgnd ? (1 << (tcd_img->comps[compno].prec - 1)) - 1 : (1 << tcd_img->comps[compno].prec) - 1;
1255                 int tw = tilec->x1 - tilec->x0;
1256                 int w = int_ceildiv(tcd_img->x1 - tcd_img->x0, tcd_img->comps[compno].dx);
1257                 int i, j;
1258                 int offset_x = int_ceildiv(tcd_img->x0, tcd_img->comps[compno].dx);
1259                 int offset_y = int_ceildiv(tcd_img->y0, tcd_img->comps[compno].dy);
1260
1261                 for (j = tilec->y0; j < tilec->y1; j++) {
1262                         for (i = tilec->x0; i < tilec->x1; i++) {
1263                                 int v;
1264                                 if (tcd_tcp->tccps[compno].qmfbid == 1) {
1265                                         v = tilec->data[i - tilec->x0 + (j - tilec->y0) * tw];
1266                                 } else {  /* if (tcd_tcp->tccps[compno].qmfbid == 0) */
1267                                         v = tilec->data[i - tilec->x0 + (j - tilec->y0) * tw] >> 13;
1268                                 }
1269                                 v += adjust;
1270                                 
1271                                 /* tcd_img->comps[compno].data[i+j*w]=int_clamp(v, min, max); */
1272                                 tcd_img->comps[compno].data[(i - offset_x) + (j - offset_y) * w] = int_clamp(v, min, max);      /* change ! */
1273                         }
1274                 }
1275         }
1276         time5 = clock() - time5;
1277         /* printf("tile->img: %ld.%.3ld s\n", time5/CLOCKS_PER_SEC, (time5%CLOCKS_PER_SEC)*1000/CLOCKS_PER_SEC); */
1278
1279         time6 = clock() - time6;
1280         printf("total:     %ld.%.3ld s\n\n", time6 / CLOCKS_PER_SEC, (time6 % CLOCKS_PER_SEC) * 1000 / CLOCKS_PER_SEC);
1281
1282         if (eof) {
1283                 longjmp(j2k_error, 1);
1284         }
1285
1286         return l;
1287 }