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