2 * Copyright (c) 2001-2002, David Janssens
3 * Copyright (c) 2002-2004, Yannick Verschueren
4 * Copyright (c) 2002-2004, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
43 static tcd_image_t tcd_image;
45 static j2k_image_t *tcd_img;
46 static j2k_cp_t *tcd_cp;
48 static tcd_tile_t *tcd_tile;
49 static j2k_tcp_t *tcd_tcp;
50 static int tcd_tileno;
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;
59 extern jmp_buf j2k_error;
62 tcd_dump (tcd_image_t * img, int curtileno)
64 int tileno, compno, resno, bandno, precno, cblkno;
65 fprintf (stderr, "image {\n");
66 fprintf (stderr, " tw=%d, th=%d x0=%d x1=%d y0=%d y1=%d\n", img->tw,
67 img->th, tcd_img->x0, tcd_img->x1, tcd_img->y0, tcd_img->y1);
68 for (tileno = 0; tileno < img->th * img->tw; tileno++)
70 tcd_tile_t *tile = &tcd_image.tiles[tileno];
71 fprintf (stderr, " tile {\n");
72 fprintf (stderr, " x0=%d, y0=%d, x1=%d, y1=%d, numcomps=%d\n",
73 tile->x0, tile->y0, tile->x1, tile->y1, tile->numcomps);
74 for (compno = 0; compno < tile->numcomps; compno++)
76 tcd_tilecomp_t *tilec = &tile->comps[compno];
77 fprintf (stderr, " tilec {\n");
79 " x0=%d, y0=%d, x1=%d, y1=%d, numresolutions=%d\n",
80 tilec->x0, tilec->y0, tilec->x1, tilec->y1,
81 tilec->numresolutions);
82 for (resno = 0; resno < tilec->numresolutions; resno++)
84 tcd_resolution_t *res = &tilec->resolutions[resno];
85 fprintf (stderr, "\n res {\n");
87 " x0=%d, y0=%d, x1=%d, y1=%d, pw=%d, ph=%d, numbands=%d\n",
88 res->x0, res->y0, res->x1, res->y1, res->pw, res->ph,
90 for (bandno = 0; bandno < res->numbands; bandno++)
92 tcd_band_t *band = &res->bands[bandno];
93 fprintf (stderr, " band {\n");
95 " x0=%d, y0=%d, x1=%d, y1=%d, stepsize=%d, numbps=%d\n",
96 band->x0, band->y0, band->x1, band->y1,
97 band->stepsize, band->numbps);
98 for (precno = 0; precno < res->pw * res->ph; precno++)
100 tcd_precinct_t *prec = &band->precincts[precno];
101 fprintf (stderr, " prec {\n");
103 " x0=%d, y0=%d, x1=%d, y1=%d, cw=%d, ch=%d\n",
104 prec->x0, prec->y0, prec->x1, prec->y1,
106 for (cblkno = 0; cblkno < prec->cw * prec->ch; cblkno++)
108 tcd_cblk_t *cblk = &prec->cblks[cblkno];
109 fprintf (stderr, " cblk {\n");
111 " x0=%d, y0=%d, x1=%d, y1=%d\n",
112 cblk->x0, cblk->y0, cblk->x1, cblk->y1);
113 fprintf (stderr, " }\n");
115 fprintf (stderr, " }\n");
117 fprintf (stderr, " }\n");
119 fprintf (stderr, " }\n");
121 fprintf (stderr, " }\n");
123 fprintf (stderr, " }\n");
125 fprintf (stderr, "}\n");
129 tcd_malloc_encode (j2k_image_t * img, j2k_cp_t * cp, int curtileno)
131 int tileno, compno, resno, bandno, precno, cblkno;
134 tcd_image.tw = cp->tw;
135 tcd_image.th = cp->th;
136 tcd_image.tiles = (tcd_tile_t *) malloc (sizeof (tcd_tile_t));
138 for (tileno = 0; tileno < 1; tileno++)
140 j2k_tcp_t *tcp = &cp->tcps[curtileno];
142 /* cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
143 int p = curtileno % cp->tw; /* si numerotation matricielle .. */
144 int q = curtileno / cp->tw; /* .. coordonnees de la tile (q,p) q pour ligne et p pour colonne */
145 /* tcd_tile_t *tile=&tcd_image.tiles[tileno]; */
146 tile = tcd_image.tiles;
147 /* 4 borders of the tile rescale on the image if necessary */
148 tile->x0 = int_max (cp->tx0 + p * cp->tdx, img->x0);
149 tile->y0 = int_max (cp->ty0 + q * cp->tdy, img->y0);
150 tile->x1 = int_min (cp->tx0 + (p + 1) * cp->tdx, img->x1);
151 tile->y1 = int_min (cp->ty0 + (q + 1) * cp->tdy, img->y1);
152 tile->numcomps = img->numcomps;
153 /* tile->PPT=img->PPT; */
154 /* Modification of the RATE >> */
155 for (j = 0; j < tcp->numlayers; j++)
157 tcp->rates[j] = tcp->rates[j] ? int_ceildiv (tile->numcomps * (tile->x1 - tile->x0) * (tile->y1 - tile->y0) * img->comps[0].prec, (tcp->rates[j] * 8 * img->comps[0].dx * img->comps[0].dy)) : 0; //Mod antonin losslessbug
160 if (j && tcp->rates[j] < tcp->rates[j - 1] + 10)
162 tcp->rates[j] = tcp->rates[j - 1] + 20;
166 if (!j && tcp->rates[j] < 30)
171 /* << Modification of the RATE */
174 (tcd_tilecomp_t *) malloc (img->numcomps * sizeof (tcd_tilecomp_t));
175 for (compno = 0; compno < tile->numcomps; compno++)
177 j2k_tccp_t *tccp = &tcp->tccps[compno];
178 /* tcd_tilecomp_t *tilec=&tile->comps[compno]; */
179 tilec = &tile->comps[compno];
180 /* border of each tile component (global) */
181 tilec->x0 = int_ceildiv (tile->x0, img->comps[compno].dx);
183 tilec->y0 = int_ceildiv (tile->y0, img->comps[compno].dy);
184 tilec->x1 = int_ceildiv (tile->x1, img->comps[compno].dx);
185 tilec->y1 = int_ceildiv (tile->y1, img->comps[compno].dy);
188 (int *) malloc ((tilec->x1 - tilec->x0) *
189 (tilec->y1 - tilec->y0) * sizeof (int));
190 tilec->numresolutions = tccp->numresolutions;
193 (tcd_resolution_t *) malloc (tilec->numresolutions *
194 sizeof (tcd_resolution_t));
196 for (resno = 0; resno < tilec->numresolutions; resno++)
199 int levelno = tilec->numresolutions - 1 - resno;
200 int tlprcxstart, tlprcystart, brprcxend, brprcyend;
201 int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend;
202 int cbgwidthexpn, cbgheightexpn;
203 int cblkwidthexpn, cblkheightexpn;
204 /* tcd_resolution_t *res=&tilec->resolutions[resno]; */
206 res = &tilec->resolutions[resno];
208 /* border for each resolution level (global) */
209 res->x0 = int_ceildivpow2 (tilec->x0, levelno);
210 res->y0 = int_ceildivpow2 (tilec->y0, levelno);
211 res->x1 = int_ceildivpow2 (tilec->x1, levelno);
212 res->y1 = int_ceildivpow2 (tilec->y1, levelno);
214 res->numbands = resno == 0 ? 1 : 3;
215 /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */
216 if (tccp->csty & J2K_CCP_CSTY_PRT)
218 pdx = tccp->prcw[resno];
219 pdy = tccp->prch[resno];
226 /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
227 tlprcxstart = int_floordivpow2 (res->x0, pdx) << pdx;
228 tlprcystart = int_floordivpow2 (res->y0, pdy) << pdy;
229 brprcxend = int_ceildivpow2 (res->x1, pdx) << pdx;
230 brprcyend = int_ceildivpow2 (res->y1, pdy) << pdy;
232 res->pw = (brprcxend - tlprcxstart) >> pdx;
233 res->ph = (brprcyend - tlprcystart) >> pdy;
237 tlcbgxstart = tlprcxstart;
238 tlcbgystart = tlprcystart;
239 brcbgxend = brprcxend;
240 brcbgyend = brprcyend;
246 tlcbgxstart = int_ceildivpow2 (tlprcxstart, 1);
247 tlcbgystart = int_ceildivpow2 (tlprcystart, 1);
248 brcbgxend = int_ceildivpow2 (brprcxend, 1);
249 brcbgyend = int_ceildivpow2 (brprcyend, 1);
250 cbgwidthexpn = pdx - 1;
251 cbgheightexpn = pdy - 1;
254 cblkwidthexpn = int_min (tccp->cblkw, cbgwidthexpn);
255 cblkheightexpn = int_min (tccp->cblkh, cbgheightexpn);
257 for (bandno = 0; bandno < res->numbands; bandno++)
262 band = &res->bands[bandno];
263 band->bandno = resno == 0 ? 0 : bandno + 1;
264 x0b = (band->bandno == 1) || (band->bandno == 3) ? 1 : 0;
265 y0b = (band->bandno == 2) || (band->bandno == 3) ? 1 : 0;
267 if (band->bandno == 0)
269 /* band border (global) */
270 band->x0 = int_ceildivpow2 (tilec->x0, levelno);
271 band->y0 = int_ceildivpow2 (tilec->y0, levelno);
272 band->x1 = int_ceildivpow2 (tilec->x1, levelno);
273 band->y1 = int_ceildivpow2 (tilec->y1, levelno);
277 /* band border (global) */
279 int_ceildivpow2 (tilec->x0 -
280 (1 << levelno) * x0b, levelno + 1);
282 int_ceildivpow2 (tilec->y0 -
283 (1 << levelno) * y0b, levelno + 1);
285 int_ceildivpow2 (tilec->x1 -
286 (1 << levelno) * x0b, levelno + 1);
288 int_ceildivpow2 (tilec->y1 -
289 (1 << levelno) * y0b, levelno + 1);
293 ss = &tccp->stepsizes[resno ==
294 0 ? 0 : 3 * (resno - 1) + bandno + 1];
297 0 ? dwt_getgain_real (band->bandno) : dwt_getgain (band->
299 numbps = img->comps[compno].prec + gain;
301 (int) floor ((1.0 + ss->mant / 2048.0) *
302 pow (2.0, numbps - ss->expn) * 8192.0);
303 band->numbps = ss->expn + tccp->numgbits - 1; /* WHY -1 ? */
306 (tcd_precinct_t *) malloc (3 * res->pw * res->ph *
307 sizeof (tcd_precinct_t));
309 for (i = 0; i < res->pw * res->ph * 3; i++)
311 band->precincts[i].imsbtree = NULL;
312 band->precincts[i].incltree = NULL;
315 for (precno = 0; precno < res->pw * res->ph; precno++)
317 int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
320 (precno % res->pw) * (1 << cbgwidthexpn);
323 (precno / res->pw) * (1 << cbgheightexpn);
324 int cbgxend = cbgxstart + (1 << cbgwidthexpn);
325 int cbgyend = cbgystart + (1 << cbgheightexpn);
326 /* tcd_precinct_t *prc=&band->precincts[precno]; */
327 prc = &band->precincts[precno];
328 /* precinct size (global) */
329 prc->x0 = int_max (cbgxstart, band->x0);
330 prc->y0 = int_max (cbgystart, band->y0);
331 prc->x1 = int_min (cbgxend, band->x1);
332 prc->y1 = int_min (cbgyend, band->y1);
335 int_floordivpow2 (prc->x0,
336 cblkwidthexpn) << cblkwidthexpn;
338 int_floordivpow2 (prc->y0,
339 cblkheightexpn) << cblkheightexpn;
341 int_ceildivpow2 (prc->x1,
342 cblkwidthexpn) << cblkwidthexpn;
344 int_ceildivpow2 (prc->y1,
345 cblkheightexpn) << cblkheightexpn;
346 prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;
347 prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn;
350 (tcd_cblk_t *) malloc ((prc->cw * prc->ch) *
351 sizeof (tcd_cblk_t));
352 prc->incltree = tgt_create (prc->cw, prc->ch);
353 prc->imsbtree = tgt_create (prc->cw, prc->ch);
355 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++)
359 (cblkno % prc->cw) * (1 << cblkwidthexpn);
362 (cblkno / prc->cw) * (1 << cblkheightexpn);
363 int cblkxend = cblkxstart + (1 << cblkwidthexpn);
364 int cblkyend = cblkystart + (1 << cblkheightexpn);
366 cblk = &prc->cblks[cblkno];
367 /* code-block size (global) */
368 cblk->x0 = int_max (cblkxstart, prc->x0);
369 cblk->y0 = int_max (cblkystart, prc->y0);
370 cblk->x1 = int_min (cblkxend, prc->x1);
371 cblk->y1 = int_min (cblkyend, prc->y1);
378 /* tcd_dump(&tcd_image,curtileno); */
382 tcd_free_encode (j2k_image_t * img, j2k_cp_t * cp, int curtileno)
384 int tileno, compno, resno, bandno, precno;
387 tcd_image.tw = cp->tw;
388 tcd_image.th = cp->th;
389 for (tileno = 0; tileno < 1; tileno++)
391 /* j2k_tcp_t *tcp=&cp->tcps[curtileno]; */
392 tile = tcd_image.tiles;
393 for (compno = 0; compno < tile->numcomps; compno++)
395 tilec = &tile->comps[compno];
396 for (resno = 0; resno < tilec->numresolutions; resno++)
398 res = &tilec->resolutions[resno];
399 for (bandno = 0; bandno < res->numbands; bandno++)
401 band = &res->bands[bandno];
402 for (precno = 0; precno < res->pw * res->ph; precno++)
404 prc = &band->precincts[precno];
406 if (prc->incltree != NULL)
407 tgt_destroy (prc->incltree);
408 if (prc->imsbtree != NULL)
409 tgt_destroy (prc->imsbtree);
412 free (band->precincts);
415 free (tilec->resolutions);
419 free (tcd_image.tiles);
423 tcd_init_encode (j2k_image_t * img, j2k_cp_t * cp, int curtileno)
425 int tileno, compno, resno, bandno, precno, cblkno;
427 for (tileno = 0; tileno < 1; tileno++)
429 j2k_tcp_t *tcp = &cp->tcps[curtileno];
431 // int previous_x0, previous_x1, previous_y0, previous_y1;
432 /* cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
433 int p = curtileno % cp->tw;
434 int q = curtileno / cp->tw;
435 tile = tcd_image.tiles;
437 /* 4 borders of the tile rescale on the image if necessary */
438 tile->x0 = int_max (cp->tx0 + p * cp->tdx, img->x0);
439 tile->y0 = int_max (cp->ty0 + q * cp->tdy, img->y0);
440 tile->x1 = int_min (cp->tx0 + (p + 1) * cp->tdx, img->x1);
441 tile->y1 = int_min (cp->ty0 + (q + 1) * cp->tdy, img->y1);
443 tile->numcomps = img->numcomps;
444 /* tile->PPT=img->PPT; */
445 /* Modification of the RATE >> */
446 for (j = 0; j < tcp->numlayers; j++)
448 tcp->rates[j] = tcp->rates[j] ? int_ceildiv (tile->numcomps * (tile->x1 - tile->x0) * (tile->y1 - tile->y0) * img->comps[0].prec, (tcp->rates[j] * 8 * img->comps[0].dx * img->comps[0].dy)) : 0; //Mod antonin losslessbug
451 if (j && tcp->rates[j] < tcp->rates[j - 1] + 10)
453 tcp->rates[j] = tcp->rates[j - 1] + 20;
457 if (!j && tcp->rates[j] < 30)
462 /* << Modification of the RATE */
463 /* tile->comps=(tcd_tilecomp_t*)realloc(tile->comps,img->numcomps*sizeof(tcd_tilecomp_t)); */
464 for (compno = 0; compno < tile->numcomps; compno++)
466 j2k_tccp_t *tccp = &tcp->tccps[compno];
467 /* int realloc_op; */
469 tilec = &tile->comps[compno];
470 /* border of each tile component (global) */
471 tilec->x0 = int_ceildiv (tile->x0, img->comps[compno].dx);
472 tilec->y0 = int_ceildiv (tile->y0, img->comps[compno].dy);
473 tilec->x1 = int_ceildiv (tile->x1, img->comps[compno].dx);
474 tilec->y1 = int_ceildiv (tile->y1, img->comps[compno].dy);
477 (int *) malloc ((tilec->x1 - tilec->x0) *
478 (tilec->y1 - tilec->y0) * sizeof (int));
479 tilec->numresolutions = tccp->numresolutions;
480 /* tilec->resolutions=(tcd_resolution_t*)realloc(tilec->resolutions,tilec->numresolutions*sizeof(tcd_resolution_t)); */
481 for (resno = 0; resno < tilec->numresolutions; resno++)
484 int levelno = tilec->numresolutions - 1 - resno;
485 int tlprcxstart, tlprcystart, brprcxend, brprcyend;
486 int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend;
487 int cbgwidthexpn, cbgheightexpn;
488 int cblkwidthexpn, cblkheightexpn;
490 res = &tilec->resolutions[resno];
491 /* border for each resolution level (global) */
492 res->x0 = int_ceildivpow2 (tilec->x0, levelno);
493 res->y0 = int_ceildivpow2 (tilec->y0, levelno);
494 res->x1 = int_ceildivpow2 (tilec->x1, levelno);
495 res->y1 = int_ceildivpow2 (tilec->y1, levelno);
497 res->numbands = resno == 0 ? 1 : 3;
498 /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */
499 if (tccp->csty & J2K_CCP_CSTY_PRT)
501 pdx = tccp->prcw[resno];
502 pdy = tccp->prch[resno];
509 /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
510 tlprcxstart = int_floordivpow2 (res->x0, pdx) << pdx;
511 tlprcystart = int_floordivpow2 (res->y0, pdy) << pdy;
512 brprcxend = int_ceildivpow2 (res->x1, pdx) << pdx;
513 brprcyend = int_ceildivpow2 (res->y1, pdy) << pdy;
515 res->pw = (brprcxend - tlprcxstart) >> pdx;
516 res->ph = (brprcyend - tlprcystart) >> pdy;
520 tlcbgxstart = tlprcxstart;
521 tlcbgystart = tlprcystart;
522 brcbgxend = brprcxend;
523 brcbgyend = brprcyend;
529 tlcbgxstart = int_ceildivpow2 (tlprcxstart, 1);
530 tlcbgystart = int_ceildivpow2 (tlprcystart, 1);
531 brcbgxend = int_ceildivpow2 (brprcxend, 1);
532 brcbgyend = int_ceildivpow2 (brprcyend, 1);
533 cbgwidthexpn = pdx - 1;
534 cbgheightexpn = pdy - 1;
537 cblkwidthexpn = int_min (tccp->cblkw, cbgwidthexpn);
538 cblkheightexpn = int_min (tccp->cblkh, cbgheightexpn);
540 for (bandno = 0; bandno < res->numbands; bandno++)
545 band = &res->bands[bandno];
546 band->bandno = resno == 0 ? 0 : bandno + 1;
547 x0b = (band->bandno == 1) || (band->bandno == 3) ? 1 : 0;
548 y0b = (band->bandno == 2) || (band->bandno == 3) ? 1 : 0;
550 if (band->bandno == 0)
553 band->x0 = int_ceildivpow2 (tilec->x0, levelno);
554 band->y0 = int_ceildivpow2 (tilec->y0, levelno);
555 band->x1 = int_ceildivpow2 (tilec->x1, levelno);
556 band->y1 = int_ceildivpow2 (tilec->y1, levelno);
561 int_ceildivpow2 (tilec->x0 -
562 (1 << levelno) * x0b, levelno + 1);
564 int_ceildivpow2 (tilec->y0 -
565 (1 << levelno) * y0b, levelno + 1);
567 int_ceildivpow2 (tilec->x1 -
568 (1 << levelno) * x0b, levelno + 1);
570 int_ceildivpow2 (tilec->y1 -
571 (1 << levelno) * y0b, levelno + 1);
574 ss = &tccp->stepsizes[resno ==
575 0 ? 0 : 3 * (resno - 1) + bandno + 1];
578 0 ? dwt_getgain_real (band->bandno) : dwt_getgain (band->
580 numbps = img->comps[compno].prec + gain;
582 (int) floor ((1.0 + ss->mant / 2048.0) *
583 pow (2.0, numbps - ss->expn) * 8192.0);
584 band->numbps = ss->expn + tccp->numgbits - 1; /* WHY -1 ? */
586 for (precno = 0; precno < res->pw * res->ph; precno++)
588 int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
591 (precno % res->pw) * (1 << cbgwidthexpn);
594 (precno / res->pw) * (1 << cbgheightexpn);
595 int cbgxend = cbgxstart + (1 << cbgwidthexpn);
596 int cbgyend = cbgystart + (1 << cbgheightexpn);
598 prc = &band->precincts[precno];
599 /* precinct size (global) */
600 prc->x0 = int_max (cbgxstart, band->x0);
601 prc->y0 = int_max (cbgystart, band->y0);
602 prc->x1 = int_min (cbgxend, band->x1);
603 prc->y1 = int_min (cbgyend, band->y1);
606 int_floordivpow2 (prc->x0,
607 cblkwidthexpn) << cblkwidthexpn;
609 int_floordivpow2 (prc->y0,
610 cblkheightexpn) << cblkheightexpn;
612 int_ceildivpow2 (prc->x1,
613 cblkwidthexpn) << cblkwidthexpn;
615 int_ceildivpow2 (prc->y1,
616 cblkheightexpn) << cblkheightexpn;
617 prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;
618 prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn;
622 (tcd_cblk_t *) malloc (prc->cw * prc->ch *
623 sizeof (tcd_cblk_t));
625 if (prc->incltree != NULL)
626 tgt_destroy (prc->incltree);
627 if (prc->imsbtree != NULL)
628 tgt_destroy (prc->imsbtree);
630 prc->incltree = tgt_create (prc->cw, prc->ch);
631 prc->imsbtree = tgt_create (prc->cw, prc->ch);
633 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++)
637 (cblkno % prc->cw) * (1 << cblkwidthexpn);
640 (cblkno / prc->cw) * (1 << cblkheightexpn);
641 int cblkxend = cblkxstart + (1 << cblkwidthexpn);
642 int cblkyend = cblkystart + (1 << cblkheightexpn);
643 cblk = &prc->cblks[cblkno];
645 /* code-block size (global) */
646 cblk->x0 = int_max (cblkxstart, prc->x0);
647 cblk->y0 = int_max (cblkystart, prc->y0);
648 cblk->x1 = int_min (cblkxend, prc->x1);
649 cblk->y1 = int_min (cblkyend, prc->y1);
657 /* tcd_dump(&tcd_image,0); */
661 tcd_init (j2k_image_t * img, j2k_cp_t * cp)
663 int tileno, compno, resno, bandno, precno, cblkno, i, j;
664 unsigned int x0 = 0, y0 = 0, x1 = 0, y1 = 0, w, h, p, q;
667 tcd_image.tw = cp->tw;
668 tcd_image.th = cp->th;
670 (tcd_tile_t *) malloc (cp->tw * cp->th * sizeof (tcd_tile_t));
672 /*for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
673 j2k_tcp_t *tcp = &cp->tcps[tileno];
674 tcd_tile_t *tile = &tcd_image.tiles[tileno]; */
676 for (i = 0; i < cp->tileno_size; i++)
678 j2k_tcp_t *tcp = &cp->tcps[cp->tileno[i]];
679 tcd_tile_t *tile = &tcd_image.tiles[cp->tileno[i]];
680 tileno = cp->tileno[i];
683 // int previous_x0, previous_x1, previous_y0, previous_y1;
684 /* cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
685 p = tileno % cp->tw; /* si numerotation matricielle .. */
686 q = tileno / cp->tw; /* .. coordonnees de la tile (q,p) q pour ligne et p pour colonne */
688 /* 4 borders of the tile rescale on the image if necessary */
689 tile->x0 = int_max (cp->tx0 + p * cp->tdx, img->x0);
690 tile->y0 = int_max (cp->ty0 + q * cp->tdy, img->y0);
691 tile->x1 = int_min (cp->tx0 + (p + 1) * cp->tdx, img->x1);
692 tile->y1 = int_min (cp->ty0 + (q + 1) * cp->tdy, img->y1);
694 tile->numcomps = img->numcomps;
696 (tcd_tilecomp_t *) malloc (img->numcomps * sizeof (tcd_tilecomp_t));
697 for (compno = 0; compno < tile->numcomps; compno++)
699 j2k_tccp_t *tccp = &tcp->tccps[compno];
700 tcd_tilecomp_t *tilec = &tile->comps[compno];
701 /* border of each tile component (global) */
702 tilec->x0 = int_ceildiv (tile->x0, img->comps[compno].dx);
703 tilec->y0 = int_ceildiv (tile->y0, img->comps[compno].dy);
704 tilec->x1 = int_ceildiv (tile->x1, img->comps[compno].dx);
705 tilec->y1 = int_ceildiv (tile->y1, img->comps[compno].dy);
708 (int *) malloc ((tilec->x1 - tilec->x0) *
709 (tilec->y1 - tilec->y0) * sizeof (int));
710 tilec->numresolutions = tccp->numresolutions;
712 (tcd_resolution_t *) malloc (tilec->numresolutions *
713 sizeof (tcd_resolution_t));
714 for (resno = 0; resno < tilec->numresolutions; resno++)
717 int levelno = tilec->numresolutions - 1 - resno;
718 int tlprcxstart, tlprcystart, brprcxend, brprcyend;
719 int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend;
720 int cbgwidthexpn, cbgheightexpn;
721 int cblkwidthexpn, cblkheightexpn;
722 tcd_resolution_t *res = &tilec->resolutions[resno];
724 /* border for each resolution level (global) */
725 res->x0 = int_ceildivpow2 (tilec->x0, levelno);
726 res->y0 = int_ceildivpow2 (tilec->y0, levelno);
727 res->x1 = int_ceildivpow2 (tilec->x1, levelno);
728 res->y1 = int_ceildivpow2 (tilec->y1, levelno);
730 res->numbands = resno == 0 ? 1 : 3;
731 /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */
732 if (tccp->csty & J2K_CCP_CSTY_PRT)
734 pdx = tccp->prcw[resno];
735 pdy = tccp->prch[resno];
742 /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
743 tlprcxstart = int_floordivpow2 (res->x0, pdx) << pdx;
744 tlprcystart = int_floordivpow2 (res->y0, pdy) << pdy;
745 brprcxend = int_ceildivpow2 (res->x1, pdx) << pdx;
746 brprcyend = int_ceildivpow2 (res->y1, pdy) << pdy;
747 res->pw = (res->x0 == res->x1) ? 0 : ((brprcxend - tlprcxstart) >> pdx); // Mod Antonin : sizebug1
748 res->ph = (res->y0 == res->y1) ? 0 : ((brprcyend - tlprcystart) >> pdy); // Mod Antonin : sizebug1
752 tlcbgxstart = tlprcxstart;
753 tlcbgystart = tlprcystart;
754 brcbgxend = brprcxend;
755 brcbgyend = brprcyend;
761 tlcbgxstart = int_ceildivpow2 (tlprcxstart, 1);
762 tlcbgystart = int_ceildivpow2 (tlprcystart, 1);
763 brcbgxend = int_ceildivpow2 (brprcxend, 1);
764 brcbgyend = int_ceildivpow2 (brprcyend, 1);
765 cbgwidthexpn = pdx - 1;
766 cbgheightexpn = pdy - 1;
769 cblkwidthexpn = int_min (tccp->cblkw, cbgwidthexpn);
770 cblkheightexpn = int_min (tccp->cblkh, cbgheightexpn);
772 for (bandno = 0; bandno < res->numbands; bandno++)
777 tcd_band_t *band = &res->bands[bandno];
778 band->bandno = resno == 0 ? 0 : bandno + 1;
779 x0b = (band->bandno == 1) || (band->bandno == 3) ? 1 : 0;
780 y0b = (band->bandno == 2) || (band->bandno == 3) ? 1 : 0;
782 if (band->bandno == 0)
784 /* band border (global) */
785 band->x0 = int_ceildivpow2 (tilec->x0, levelno);
786 band->y0 = int_ceildivpow2 (tilec->y0, levelno);
787 band->x1 = int_ceildivpow2 (tilec->x1, levelno);
788 band->y1 = int_ceildivpow2 (tilec->y1, levelno);
792 /* band border (global) */
794 int_ceildivpow2 (tilec->x0 -
795 (1 << levelno) * x0b, levelno + 1);
797 int_ceildivpow2 (tilec->y0 -
798 (1 << levelno) * y0b, levelno + 1);
800 int_ceildivpow2 (tilec->x1 -
801 (1 << levelno) * x0b, levelno + 1);
803 int_ceildivpow2 (tilec->y1 -
804 (1 << levelno) * y0b, levelno + 1);
807 ss = &tccp->stepsizes[resno ==
808 0 ? 0 : 3 * (resno - 1) + bandno + 1];
811 0 ? dwt_getgain_real (band->bandno) : dwt_getgain (band->
813 numbps = img->comps[compno].prec + gain;
815 (int) floor ((1.0 + ss->mant / 2048.0) *
816 pow (2.0, numbps - ss->expn) * 8192.0);
817 band->numbps = ss->expn + tccp->numgbits - 1; /* WHY -1 ? */
820 (tcd_precinct_t *) malloc (res->pw * res->ph *
821 sizeof (tcd_precinct_t));
823 for (precno = 0; precno < res->pw * res->ph; precno++)
825 int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
828 (precno % res->pw) * (1 << cbgwidthexpn);
831 (precno / res->pw) * (1 << cbgheightexpn);
832 int cbgxend = cbgxstart + (1 << cbgwidthexpn);
833 int cbgyend = cbgystart + (1 << cbgheightexpn);
834 tcd_precinct_t *prc = &band->precincts[precno];
835 /* precinct size (global) */
836 prc->x0 = int_max (cbgxstart, band->x0);
837 prc->y0 = int_max (cbgystart, band->y0);
838 prc->x1 = int_min (cbgxend, band->x1);
839 prc->y1 = int_min (cbgyend, band->y1);
842 int_floordivpow2 (prc->x0,
843 cblkwidthexpn) << cblkwidthexpn;
845 int_floordivpow2 (prc->y0,
846 cblkheightexpn) << cblkheightexpn;
848 int_ceildivpow2 (prc->x1,
849 cblkwidthexpn) << cblkwidthexpn;
851 int_ceildivpow2 (prc->y1,
852 cblkheightexpn) << cblkheightexpn;
853 prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;
854 prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn;
857 (tcd_cblk_t *) malloc (prc->cw * prc->ch *
858 sizeof (tcd_cblk_t));
860 prc->incltree = tgt_create (prc->cw, prc->ch);
861 prc->imsbtree = tgt_create (prc->cw, prc->ch);
863 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++)
867 (cblkno % prc->cw) * (1 << cblkwidthexpn);
870 (cblkno / prc->cw) * (1 << cblkheightexpn);
871 int cblkxend = cblkxstart + (1 << cblkwidthexpn);
872 int cblkyend = cblkystart + (1 << cblkheightexpn);
873 tcd_cblk_t *cblk = &prc->cblks[cblkno];
874 /* code-block size (global) */
875 cblk->x0 = int_max (cblkxstart, prc->x0);
876 cblk->y0 = int_max (cblkystart, prc->y0);
877 cblk->x1 = int_min (cblkxend, prc->x1);
878 cblk->y1 = int_min (cblkyend, prc->y1);
882 cblk->lastbp = 0; // Add Antonin : quantizbug1
889 //tcd_dump(&tcd_image,0);
892 /* Allocate place to store the data decoded = final image */
893 /* Place limited by the tile really present in the codestream */
896 for (i = 0; i < img->numcomps; i++)
898 for (j = 0; j < cp->tileno_size; j++)
900 tileno = cp->tileno[j];
901 x0 = j == 0 ? tcd_image.tiles[tileno].comps[i].x0 : int_min (x0,
909 j == 0 ? tcd_image.tiles[tileno].comps[i].y0 : int_min (y0,
916 j == 0 ? tcd_image.tiles[tileno].comps[i].x1 : int_max (x1,
923 j == 0 ? tcd_image.tiles[tileno].comps[i].y1 : int_max (y1,
930 //w = int_ceildiv(x1 - x0, img->comps[i].dx);
931 //h = int_ceildiv(y1 - y0, img->comps[i].dy);
936 img->comps[i].data = (int *) calloc (w * h, sizeof (int));
939 img->comps[i].x0 = x0;
940 img->comps[i].y0 = y0;
945 tcd_makelayer_fixed (int layno, int final)
947 int compno, resno, bandno, precno, cblkno;
948 int value; //, matrice[tcd_tcp->numlayers][tcd_tile->comps[0].numresolutions][3];
949 int matrice[10][10][3];
952 /*matrice=(int*)malloc(tcd_tcp->numlayers*tcd_tile->comps[0].numresolutions*3*sizeof(int)); */
954 for (compno = 0; compno < tcd_tile->numcomps; compno++)
956 tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
957 for (i = 0; i < tcd_tcp->numlayers; i++)
959 for (j = 0; j < tilec->numresolutions; j++)
961 for (k = 0; k < 3; k++)
965 matrice[i * tilec->numresolutions * 3 +
968 (float) (tcd_img->comps[compno].prec / 16.0));
971 for (resno = 0; resno < tilec->numresolutions; resno++)
973 tcd_resolution_t *res = &tilec->resolutions[resno];
974 for (bandno = 0; bandno < res->numbands; bandno++)
976 tcd_band_t *band = &res->bands[bandno];
977 for (precno = 0; precno < res->pw * res->ph; precno++)
979 tcd_precinct_t *prc = &band->precincts[precno];
980 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++)
982 tcd_cblk_t *cblk = &prc->cblks[cblkno];
983 tcd_layer_t *layer = &cblk->layers[layno];
985 int imsb = tcd_img->comps[compno].prec - cblk->numbps; /* number of bit-plan equal to zero */
986 /* Correction of the matrix of coefficient to include the IMSB information */
990 value = matrice[layno][resno][bandno];
999 matrice[layno][resno][bandno] -
1000 matrice[layno - 1][resno][bandno];
1001 if (imsb >= matrice[layno - 1][resno][bandno])
1004 (imsb - matrice[layno - 1][resno][bandno]);
1011 cblk->numpassesinlayers = 0;
1013 n = cblk->numpassesinlayers;
1014 if (cblk->numpassesinlayers == 0)
1017 n = 3 * value - 2 + cblk->numpassesinlayers;
1019 n = cblk->numpassesinlayers;
1022 n = 3 * value + cblk->numpassesinlayers;
1024 layer->numpasses = n - cblk->numpassesinlayers;
1026 if (!layer->numpasses)
1029 if (cblk->numpassesinlayers == 0)
1031 layer->len = cblk->passes[n - 1].rate;
1032 layer->data = cblk->data;
1037 cblk->passes[n - 1].rate -
1038 cblk->passes[cblk->numpassesinlayers - 1].rate;
1041 cblk->passes[cblk->numpassesinlayers - 1].rate;
1044 cblk->numpassesinlayers = n;
1053 tcd_rateallocate_fixed ()
1057 for (layno = 0; layno < tcd_tcp->numlayers; layno++)
1059 tcd_makelayer_fixed (layno, 1);
1064 tcd_makelayer (int layno, double thresh, int final)
1066 int compno, resno, bandno, precno, cblkno, passno;
1068 tcd_tile->distolayer[layno] = 0; //add fixed_quality
1070 for (compno = 0; compno < tcd_tile->numcomps; compno++)
1072 tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
1073 for (resno = 0; resno < tilec->numresolutions; resno++)
1075 tcd_resolution_t *res = &tilec->resolutions[resno];
1076 for (bandno = 0; bandno < res->numbands; bandno++)
1078 tcd_band_t *band = &res->bands[bandno];
1079 for (precno = 0; precno < res->pw * res->ph; precno++)
1081 tcd_precinct_t *prc = &band->precincts[precno];
1082 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++)
1084 tcd_cblk_t *cblk = &prc->cblks[cblkno];
1085 tcd_layer_t *layer = &cblk->layers[layno];
1090 cblk->numpassesinlayers = 0;
1092 n = cblk->numpassesinlayers;
1093 for (passno = cblk->numpassesinlayers;
1094 passno < cblk->totalpasses; passno++)
1098 tcd_pass_t *pass = &cblk->passes[passno];
1102 dd = pass->distortiondec;
1106 dr = pass->rate - cblk->passes[n - 1].rate;
1107 dd = pass->distortiondec - cblk->passes[n -
1118 if (dd / dr >= thresh)
1121 layer->numpasses = n - cblk->numpassesinlayers;
1123 if (!layer->numpasses)
1129 if (cblk->numpassesinlayers == 0)
1131 layer->len = cblk->passes[n - 1].rate;
1132 layer->data = cblk->data;
1133 layer->disto = cblk->passes[n - 1].distortiondec;
1137 layer->len = cblk->passes[n - 1].rate -
1138 cblk->passes[cblk->numpassesinlayers - 1].rate;
1141 cblk->passes[cblk->numpassesinlayers - 1].rate;
1143 cblk->passes[n - 1].distortiondec -
1144 cblk->passes[cblk->numpassesinlayers -
1148 tcd_tile->distolayer[layno] += layer->disto; //add fixed_quality
1151 cblk->numpassesinlayers = n;
1160 tcd_rateallocate (unsigned char *dest, int len, info_image * info_IM)
1162 int compno, resno, bandno, precno, cblkno, passno, layno;
1164 double cumdisto[100]; //add fixed_quality
1165 const double K = 1; // 1.1; //add fixed_quality
1171 tcd_tile->nbpix = 0; //add fixed_quality
1173 for (compno = 0; compno < tcd_tile->numcomps; compno++)
1175 tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
1178 for (resno = 0; resno < tilec->numresolutions; resno++)
1180 tcd_resolution_t *res = &tilec->resolutions[resno];
1181 for (bandno = 0; bandno < res->numbands; bandno++)
1183 tcd_band_t *band = &res->bands[bandno];
1184 for (precno = 0; precno < res->pw * res->ph; precno++)
1186 tcd_precinct_t *prc = &band->precincts[precno];
1187 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++)
1189 tcd_cblk_t *cblk = &prc->cblks[cblkno];
1190 for (passno = 0; passno < cblk->totalpasses; passno++)
1192 tcd_pass_t *pass = &cblk->passes[passno];
1198 dd = pass->distortiondec;
1202 dr = pass->rate - cblk->passes[passno - 1].rate;
1203 dd = pass->distortiondec -
1204 cblk->passes[passno - 1].distortiondec;
1223 tcd_tile->nbpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0)); //add fixed_quality
1225 tilec->nbpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0)); //add fixed_quality
1233 (double) (((1 << tcd_img->comps[compno].prec) -
1234 1) * ((1 << tcd_img->comps[compno].prec) -
1235 1)) * (tilec->nbpix);
1238 /* add antonin index */
1239 if (info_IM->index_on)
1241 info_tile *info_TL = &info_IM->tile[tcd_tileno];
1242 info_TL->nbpix = tcd_tile->nbpix;
1243 info_TL->distotile = tcd_tile->distotile;
1245 (double *) malloc (tcd_tcp->numlayers * sizeof (double));
1249 for (layno = 0; layno < tcd_tcp->numlayers; layno++)
1251 volatile double lo = min;
1252 volatile double hi = max;
1253 volatile int success = 0;
1254 volatile int maxlen = tcd_tcp->rates[layno] ? int_min (tcd_tcp->rates[layno], len) : len; //Mod antonin losslessbug
1255 volatile double goodthresh;
1257 double distotarget; //add fixed_quality
1259 distotarget = tcd_tile->distotile - ((K * maxSE) / pow (10, tcd_tcp->distoratio[layno] / 10)); // add fixed_quality
1261 if (tcd_tcp->rates[layno])
1263 for (i = 0; i < 32; i++)
1265 volatile double thresh = (lo + hi) / 2;
1267 double distoachieved = 0; // add fixed_quality
1269 tcd_makelayer (layno, thresh, 0);
1271 if (tcd_cp->fixed_quality)
1272 { // add fixed_quality
1275 0 ? tcd_tile->distolayer[0] : cumdisto[layno - 1] +
1276 tcd_tile->distolayer[layno];
1277 if (distoachieved < distotarget)
1287 t2_encode_packets (tcd_img, tcd_cp, tcd_tileno, tcd_tile,
1288 layno + 1, dest, maxlen, info_IM);
1289 /* fprintf(stderr, "rate alloc: len=%d, max=%d\n", l, maxlen); */
1299 goodthresh = thresh;
1310 longjmp (j2k_error, 1);
1313 if (info_IM->index_on)
1314 { /* Threshold for Marcela Index */
1315 info_IM->tile[tcd_tileno].thresh[layno] = goodthresh;
1317 tcd_makelayer (layno, goodthresh, 1);
1319 cumdisto[layno] = layno == 0 ? tcd_tile->distolayer[0] : cumdisto[layno - 1] + tcd_tile->distolayer[layno]; // add fixed_quality
1324 tcd_encode_tile_pxm (int tileno, unsigned char *dest, int len,
1325 info_image * info_IM)
1331 j2k_tcp_t *tcp = &tcd_cp->tcps[0];
1332 j2k_tccp_t *tccp = &tcp->tccps[0];
1334 tcd_tileno = tileno;
1335 tcd_tile = tcd_image.tiles;
1336 tcd_tcp = &tcd_cp->tcps[tileno];
1338 /* INDEX >> "Precinct_nb_X et Precinct_nb_Y" */
1339 if (info_IM->index_on)
1341 tcd_tilecomp_t *tilec_idx = &tile->comps[0]; //Based on Component 0
1343 for (i = 0; i < tilec_idx->numresolutions; i++)
1346 tcd_resolution_t *res_idx = &tilec_idx->resolutions[i];
1348 info_IM->tile[tileno].pw[i] = res_idx->pw;
1349 info_IM->tile[tileno].ph[i] = res_idx->ph;
1351 info_IM->tile[tileno].pdx[i] = tccp->prcw[i];
1352 info_IM->tile[tileno].pdy[i] = tccp->prch[i];
1358 /*---------------TILE-------------------*/
1362 for (compno = 0; compno < tile->numcomps; compno++)
1370 tcd_tilecomp_t *tilec = &tile->comps[compno];
1372 tcd_img->comps[compno].sgnd ? 0 : 1 << (tcd_img->comps[compno].
1374 int offset_x, offset_y;
1376 offset_x = int_ceildiv (tcd_img->x0, tcd_img->comps[compno].dx);
1377 offset_y = int_ceildiv (tcd_img->y0, tcd_img->comps[compno].dy);
1378 tw = tilec->x1 - tilec->x0;
1379 w = int_ceildiv (tcd_img->x1 - tcd_img->x0, tcd_img->comps[compno].dx);
1380 sprintf (tmp, "Compo%d", compno); /* component file */
1381 src = fopen (tmp, "rb");
1384 fprintf (stderr, "failed to open %s for reading\n", tmp);
1388 /* read the Compo file to extract data of the tile */
1390 fseek (src, (tilec->x0 - offset_x) + (tilec->y0 - offset_y) * w,
1392 k = (tilec->x0 - offset_x) + (tilec->y0 - offset_y) * w;
1393 for (j = tilec->y0; j < tilec->y1; j++)
1395 for (i = tilec->x0; i < tilec->x1; i++)
1397 if (tcd_tcp->tccps[compno].qmfbid == 1)
1400 tilec->data[i - tilec->x0 + (j - tilec->y0) * tw] =
1404 else if (tcd_tcp->tccps[compno].qmfbid == 0)
1407 tilec->data[i - tilec->x0 + (j - tilec->y0) * tw] =
1408 (elmt - adjust) << 13;
1412 fseek (src, (tilec->x0 - offset_x) + (j + 1 - offset_y) * w - k,
1414 k = tilec->x0 - offset_x + (j + 1 - offset_y) * w;
1420 /*----------------MCT-------------------*/
1424 if (tcd_tcp->tccps[0].qmfbid == 0)
1426 mct_encode_real (tile->comps[0].data, tile->comps[1].data,
1427 tile->comps[2].data,
1428 (tile->comps[0].x1 -
1429 tile->comps[0].x0) * (tile->comps[0].y1 -
1430 tile->comps[0].y0));
1434 mct_encode (tile->comps[0].data, tile->comps[1].data,
1435 tile->comps[2].data,
1436 (tile->comps[0].x1 -
1437 tile->comps[0].x0) * (tile->comps[0].y1 -
1438 tile->comps[0].y0));
1441 /*----------------DWT---------------------*/
1443 /* time3=clock(); */
1444 for (compno = 0; compno < tile->numcomps; compno++)
1446 tcd_tilecomp_t *tilec = &tile->comps[compno];
1447 if (tcd_tcp->tccps[compno].qmfbid == 1)
1449 dwt_encode (tilec->data, tilec->x1 - tilec->x0,
1450 tilec->y1 - tilec->y0, tilec,
1451 tilec->numresolutions - 1);
1453 else if (tcd_tcp->tccps[compno].qmfbid == 0)
1455 dwt_encode_real (tilec->data, tilec->x1 - tilec->x0,
1456 tilec->y1 - tilec->y0, tilec,
1457 tilec->numresolutions - 1);
1460 /*------------------TIER1-----------------*/
1463 t1_encode_cblks (tile, tcd_tcp);
1465 /*-----------RATE-ALLOCATE------------------*/
1466 info_IM->index_write = 0; /* INDEX */
1468 if (tcd_cp->disto_alloc || tcd_cp->fixed_quality) // mod fixed_quality
1469 /* Normal Rate/distortion allocation */
1470 tcd_rateallocate (dest, len, info_IM);
1472 /* Fixed layer allocation */
1473 tcd_rateallocate_fixed ();
1475 /*--------------TIER2------------------*/
1476 info_IM->index_write = 1; /* INDEX */
1477 l = t2_encode_packets (tcd_img, tcd_cp, tileno, tile,
1478 tcd_tcp->numlayers, dest, len, info_IM);
1479 /*---------------CLEAN-------------------*/
1481 time7 = clock () - time7;
1482 printf ("total: %ld.%.3ld s\n", time7 / CLOCKS_PER_SEC,
1483 (time7 % CLOCKS_PER_SEC) * 1000 / CLOCKS_PER_SEC);
1485 /* cleaning memory */
1486 for (compno = 0; compno < tile->numcomps; compno++)
1488 tilec = &tile->comps[compno];
1496 tcd_encode_tile_pgx (int tileno, unsigned char *dest, int len,
1497 info_image * info_IM)
1503 j2k_tcp_t *tcp = &tcd_cp->tcps[0];
1504 j2k_tccp_t *tccp = &tcp->tccps[0];
1506 tcd_tileno = tileno;
1507 tcd_tile = tcd_image.tiles;
1508 tcd_tcp = &tcd_cp->tcps[tileno];
1510 /* INDEX >> "Precinct_nb_X et Precinct_nb_Y" */
1512 if (info_IM->index_on)
1515 tcd_tilecomp_t *tilec_idx = &tile->comps[0]; //Based on Component 0
1517 for (i = 0; i < tilec_idx->numresolutions; i++)
1520 tcd_resolution_t *res_idx = &tilec_idx->resolutions[i];
1524 info_IM->tile[tileno].pw[i] = res_idx->pw;
1526 info_IM->tile[tileno].ph[i] = res_idx->ph;
1530 info_IM->tile[tileno].pdx[i] = tccp->prcw[i];
1532 info_IM->tile[tileno].pdy[i] = tccp->prch[i];
1539 /*---------------TILE-------------------*/
1542 for (compno = 0; compno < tile->numcomps; compno++)
1550 tcd_tilecomp_t *tilec = &tile->comps[compno];
1552 tcd_img->comps[compno].sgnd ? 0 : 1 << (tcd_img->comps[compno].
1554 int offset_x, offset_y;
1556 offset_x = int_ceildiv (tcd_img->x0, tcd_img->comps[compno].dx);
1557 offset_y = int_ceildiv (tcd_img->y0, tcd_img->comps[compno].dy);
1558 tw = tilec->x1 - tilec->x0;
1559 w = int_ceildiv (tcd_img->x1 - tcd_img->x0, tcd_img->comps[compno].dx);
1560 sprintf (tmp, "bandtile%d", tileno / tcd_cp->tw + 1); /* bandtile file opening */
1561 src = fopen (tmp, "rb");
1564 fprintf (stderr, "failed to open %s for reading\n", tmp);
1567 /* Extract data from bandtile file limited to the current tile */
1569 while (k < tilec->x0 - offset_x)
1572 fscanf (src, "%d", &elmt);
1575 for (j = 0; j < tilec->y1 - tilec->y0; j++)
1577 for (i = tilec->x0; i < tilec->x1; i++)
1579 if (tcd_tcp->tccps[compno].qmfbid == 1)
1581 fscanf (src, "%d", &elmt);
1582 tilec->data[i - tilec->x0 + (j) * tw] = elmt - adjust;
1585 else if (tcd_tcp->tccps[compno].qmfbid == 0)
1587 fscanf (src, "%d", &elmt);
1588 tilec->data[i - tilec->x0 + (j) * tw] =
1589 (elmt - adjust) << 13;
1593 while (k < tilec->x0 - offset_x + (j + 1) * w)
1596 fscanf (src, "%d", &elmt);
1602 /*----------------MCT-------------------*/
1606 if (tcd_tcp->tccps[0].qmfbid == 0)
1608 mct_encode_real (tile->comps[0].data, tile->comps[1].data,
1609 tile->comps[2].data,
1610 (tile->comps[0].x1 -
1611 tile->comps[0].x0) * (tile->comps[0].y1 -
1612 tile->comps[0].y0));
1616 mct_encode (tile->comps[0].data, tile->comps[1].data,
1617 tile->comps[2].data,
1618 (tile->comps[0].x1 -
1619 tile->comps[0].x0) * (tile->comps[0].y1 -
1620 tile->comps[0].y0));
1624 /*----------------DWT---------------------*/
1626 for (compno = 0; compno < tile->numcomps; compno++)
1628 tcd_tilecomp_t *tilec = &tile->comps[compno];
1629 if (tcd_tcp->tccps[compno].qmfbid == 1)
1631 dwt_encode (tilec->data, tilec->x1 - tilec->x0,
1632 tilec->y1 - tilec->y0, tilec,
1633 tilec->numresolutions - 1);
1635 else if (tcd_tcp->tccps[compno].qmfbid == 0)
1637 dwt_encode_real (tilec->data, tilec->x1 - tilec->x0,
1638 tilec->y1 - tilec->y0, tilec,
1639 tilec->numresolutions - 1);
1643 /*------------------TIER1-----------------*/
1646 t1_encode_cblks (tile, tcd_tcp);
1648 /*-----------RATE-ALLOCATE------------------*/
1650 info_IM->index_write = 0; /* INDEX */
1652 if (tcd_cp->disto_alloc || tcd_cp->fixed_quality) // mod fixed_quality
1654 /* Normal Rate/distortion allocation */
1656 tcd_rateallocate (dest, len, info_IM);
1659 /* Fixed layer allocation */
1661 tcd_rateallocate_fixed ();
1663 /*--------------TIER2------------------*/
1664 info_IM->index_write = 1; /* INDEX */
1666 l = t2_encode_packets (tcd_img, tcd_cp, tileno, tile,
1667 tcd_tcp->numlayers, dest, len, info_IM);
1669 /*---------------CLEAN-------------------*/
1670 time = clock () - time;
1671 printf ("total: %ld.%.3ld s\n", time / CLOCKS_PER_SEC,
1672 (time % CLOCKS_PER_SEC) * 1000 / CLOCKS_PER_SEC);
1674 for (compno = 0; compno < tile->numcomps; compno++)
1676 tilec = &tile->comps[compno];
1685 tcd_decode_tile (unsigned char *src, int len, int tileno)
1693 tcd_tileno = tileno;
1694 tcd_tile = &tcd_image.tiles[tileno];
1695 tcd_tcp = &tcd_cp->tcps[tileno];
1700 fprintf (stderr, "tile decoding time %d/%d: ", tileno + 1,
1701 tcd_cp->tw * tcd_cp->th);
1703 /*--------------TIER2------------------*/
1705 l = t2_decode_packets (src, len, tcd_img, tcd_cp, tileno, tile);
1710 fprintf (stderr, "tcd_decode: incomplete bistream\n");
1713 /*------------------TIER1-----------------*/
1715 t1_decode_cblks (tile, tcd_tcp);
1717 /*----------------DWT---------------------*/
1719 for (compno = 0; compno < tile->numcomps; compno++)
1721 tcd_tilecomp_t *tilec = &tile->comps[compno];
1722 if (tcd_cp->reduce_on == 1)
1724 tcd_img->comps[compno].resno_decoded =
1725 tile->comps[compno].numresolutions - tcd_cp->reduce_value - 1;
1729 if (tcd_tcp->tccps[compno].qmfbid == 1)
1731 dwt_decode (tilec->data, tilec->x1 - tilec->x0,
1732 tilec->y1 - tilec->y0, tilec,
1733 tilec->numresolutions - 1,
1734 tilec->numresolutions - 1 -
1735 tcd_img->comps[compno].resno_decoded);
1739 dwt_decode_real (tilec->data, tilec->x1 - tilec->x0,
1740 tilec->y1 - tilec->y0, tilec,
1741 tilec->numresolutions - 1,
1742 tilec->numresolutions - 1 -
1743 tcd_img->comps[compno].resno_decoded);
1746 if (tile->comps[compno].numresolutions > 0)
1747 tcd_img->comps[compno].factor =
1748 tile->comps[compno].numresolutions -
1749 (tcd_img->comps[compno].resno_decoded + 1);
1752 /*----------------MCT-------------------*/
1756 if (tcd_tcp->tccps[0].qmfbid == 1)
1758 mct_decode (tile->comps[0].data, tile->comps[1].data,
1759 tile->comps[2].data,
1760 (tile->comps[0].x1 -
1761 tile->comps[0].x0) * (tile->comps[0].y1 -
1762 tile->comps[0].y0));
1766 mct_decode_real (tile->comps[0].data, tile->comps[1].data,
1767 tile->comps[2].data,
1768 (tile->comps[0].x1 -
1769 tile->comps[0].x0) * (tile->comps[0].y1 -
1770 tile->comps[0].y0));
1774 /*---------------TILE-------------------*/
1776 for (compno = 0; compno < tile->numcomps; compno++)
1778 tcd_tilecomp_t *tilec = &tile->comps[compno];
1779 tcd_resolution_t *res =
1780 &tilec->resolutions[tcd_img->comps[compno].resno_decoded];
1782 tcd_img->comps[compno].sgnd ? 0 : 1 << (tcd_img->comps[compno].
1785 tcd_img->comps[compno].
1786 sgnd ? -(1 << (tcd_img->comps[compno].prec - 1)) : 0;
1788 tcd_img->comps[compno].
1789 sgnd ? (1 << (tcd_img->comps[compno].prec - 1)) -
1790 1 : (1 << tcd_img->comps[compno].prec) - 1;
1792 int tw = tilec->x1 - tilec->x0;
1793 int w = tcd_img->comps[compno].w;
1796 int offset_x = int_ceildivpow2 (tcd_img->comps[compno].x0,
1797 tcd_img->comps[compno].factor);
1798 int offset_y = int_ceildivpow2 (tcd_img->comps[compno].y0,
1799 tcd_img->comps[compno].factor);
1801 for (j = res->y0; j < res->y1; j++)
1803 for (i = res->x0; i < res->x1; i++)
1809 (double) tilec->data[i - res->x0 + (j - res->y0) * tw];
1810 if (tcd_tcp->tccps[compno].qmfbid == 1)
1817 //v = (int) tmp >> 13;
1819 //Mod antonin : multbug1
1821 (int) ((fabs (tmp / 8192.0) >=
1822 floor (fabs (tmp / 8192.0)) +
1823 0.5) ? fabs (tmp / 8192.0) +
1824 1.0 : fabs (tmp / 8192.0));
1826 v = (tmp < 0) ? -v : v;
1832 tcd_img->comps[compno].data[(i - offset_x) +
1833 (j - offset_y) * w] =
1834 int_clamp (v, min, max);
1839 time = clock () - time;
1840 fprintf (stderr, "total: %ld.%.3ld s\n", time / CLOCKS_PER_SEC,
1841 (time % CLOCKS_PER_SEC) * 1000 / CLOCKS_PER_SEC);
1843 for (compno = 0; compno < tile->numcomps; compno++)
1845 free (tcd_image.tiles[tileno].comps[compno].data);
1850 longjmp (j2k_error, 1);