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;
61 void tcd_dump(tcd_image_t * img, int curtileno)
63 int tileno, compno, resno, bandno, precno, cblkno;
65 fprintf(stdout, "image {\n");
66 fprintf(stdout, " 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++) {
69 tcd_tile_t *tile = &tcd_image.tiles[tileno];
70 fprintf(stdout, " tile {\n");
71 fprintf(stdout, " x0=%d, y0=%d, x1=%d, y1=%d, numcomps=%d\n",
72 tile->x0, tile->y0, tile->x1, tile->y1, tile->numcomps);
73 for (compno = 0; compno < tile->numcomps; compno++) {
74 tcd_tilecomp_t *tilec = &tile->comps[compno];
75 fprintf(stdout, " tilec {\n");
77 " x0=%d, y0=%d, x1=%d, y1=%d, numresolutions=%d\n",
78 tilec->x0, tilec->y0, tilec->x1, tilec->y1,
79 tilec->numresolutions);
80 for (resno = 0; resno < tilec->numresolutions; resno++) {
81 tcd_resolution_t *res = &tilec->resolutions[resno];
82 fprintf(stdout, "\n res {\n");
84 " x0=%d, y0=%d, x1=%d, y1=%d, pw=%d, ph=%d, numbands=%d\n",
85 res->x0, res->y0, res->x1, res->y1, res->pw, res->ph,
87 for (bandno = 0; bandno < res->numbands; bandno++) {
88 tcd_band_t *band = &res->bands[bandno];
89 fprintf(stdout, " band {\n");
91 " x0=%d, y0=%d, x1=%d, y1=%d, stepsize=%f, numbps=%d\n",
92 band->x0, band->y0, band->x1, band->y1,
93 band->stepsize, band->numbps);
94 for (precno = 0; precno < res->pw * res->ph; precno++) {
95 tcd_precinct_t *prec = &band->precincts[precno];
96 fprintf(stdout, " prec {\n");
98 " x0=%d, y0=%d, x1=%d, y1=%d, cw=%d, ch=%d\n",
99 prec->x0, prec->y0, prec->x1, prec->y1,
101 for (cblkno = 0; cblkno < prec->cw * prec->ch; cblkno++) {
102 tcd_cblk_t *cblk = &prec->cblks[cblkno];
103 fprintf(stdout, " cblk {\n");
105 " x0=%d, y0=%d, x1=%d, y1=%d\n",
106 cblk->x0, cblk->y0, cblk->x1, cblk->y1);
107 fprintf(stdout, " }\n");
109 fprintf(stdout, " }\n");
111 fprintf(stdout, " }\n");
113 fprintf(stdout, " }\n");
115 fprintf(stdout, " }\n");
117 fprintf(stdout, " }\n");
119 fprintf(stdout, "}\n");
122 void tcd_malloc_encode(j2k_image_t * img, j2k_cp_t * cp, int curtileno)
124 int tileno, compno, resno, bandno, precno, cblkno;
127 tcd_image.tw = cp->tw;
128 tcd_image.th = cp->th;
129 tcd_image.tiles = (tcd_tile_t *) malloc(sizeof(tcd_tile_t));
131 for (tileno = 0; tileno < 1; tileno++) {
132 j2k_tcp_t *tcp = &cp->tcps[curtileno];
134 /* cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
135 int p = curtileno % cp->tw; /* si numerotation matricielle .. */
136 int q = curtileno / cp->tw; /* .. coordonnees de la tile (q,p) q pour ligne et p pour colonne */
137 /* tcd_tile_t *tile=&tcd_image.tiles[tileno]; */
138 tile = tcd_image.tiles;
139 /* 4 borders of the tile rescale on the image if necessary */
140 tile->x0 = int_max(cp->tx0 + p * cp->tdx, img->x0);
141 tile->y0 = int_max(cp->ty0 + q * cp->tdy, img->y0);
142 tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, img->x1);
143 tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, img->y1);
144 tile->numcomps = img->numcomps;
145 /* tile->PPT=img->PPT; */
146 /* Modification of the RATE >> */
147 for (j = 0; j < tcp->numlayers; j++) {
148 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*/
150 if (j && tcp->rates[j] < tcp->rates[j - 1] + 10) {
151 tcp->rates[j] = tcp->rates[j - 1] + 20;
153 if (!j && tcp->rates[j] < 30)
158 /* << Modification of the RATE */
161 (tcd_tilecomp_t *) malloc(img->numcomps * sizeof(tcd_tilecomp_t));
162 for (compno = 0; compno < tile->numcomps; compno++) {
163 j2k_tccp_t *tccp = &tcp->tccps[compno];
164 /* tcd_tilecomp_t *tilec=&tile->comps[compno]; */
165 tilec = &tile->comps[compno];
166 /* border of each tile component (global) */
167 tilec->x0 = int_ceildiv(tile->x0, img->comps[compno].dx);
169 tilec->y0 = int_ceildiv(tile->y0, img->comps[compno].dy);
170 tilec->x1 = int_ceildiv(tile->x1, img->comps[compno].dx);
171 tilec->y1 = int_ceildiv(tile->y1, img->comps[compno].dy);
174 (int *) malloc((tilec->x1 - tilec->x0) *
175 (tilec->y1 - tilec->y0) * sizeof(int));
176 tilec->numresolutions = tccp->numresolutions;
179 (tcd_resolution_t *) malloc(tilec->numresolutions *
180 sizeof(tcd_resolution_t));
182 for (resno = 0; resno < tilec->numresolutions; resno++) {
184 int levelno = tilec->numresolutions - 1 - resno;
185 int tlprcxstart, tlprcystart, brprcxend, brprcyend;
186 int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend;
187 int cbgwidthexpn, cbgheightexpn;
188 int cblkwidthexpn, cblkheightexpn;
189 /* tcd_resolution_t *res=&tilec->resolutions[resno]; */
191 res = &tilec->resolutions[resno];
193 /* border for each resolution level (global) */
194 res->x0 = int_ceildivpow2(tilec->x0, levelno);
195 res->y0 = int_ceildivpow2(tilec->y0, levelno);
196 res->x1 = int_ceildivpow2(tilec->x1, levelno);
197 res->y1 = int_ceildivpow2(tilec->y1, levelno);
199 res->numbands = resno == 0 ? 1 : 3;
200 /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */
201 if (tccp->csty & J2K_CCP_CSTY_PRT) {
202 pdx = tccp->prcw[resno];
203 pdy = tccp->prch[resno];
208 /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
209 tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx;
210 tlprcystart = int_floordivpow2(res->y0, pdy) << pdy;
211 brprcxend = int_ceildivpow2(res->x1, pdx) << pdx;
212 brprcyend = int_ceildivpow2(res->y1, pdy) << pdy;
214 res->pw = (brprcxend - tlprcxstart) >> pdx;
215 res->ph = (brprcyend - tlprcystart) >> pdy;
218 tlcbgxstart = tlprcxstart;
219 tlcbgystart = tlprcystart;
220 brcbgxend = brprcxend;
221 brcbgyend = brprcyend;
225 tlcbgxstart = int_ceildivpow2(tlprcxstart, 1);
226 tlcbgystart = int_ceildivpow2(tlprcystart, 1);
227 brcbgxend = int_ceildivpow2(brprcxend, 1);
228 brcbgyend = int_ceildivpow2(brprcyend, 1);
229 cbgwidthexpn = pdx - 1;
230 cbgheightexpn = pdy - 1;
233 cblkwidthexpn = int_min(tccp->cblkw, cbgwidthexpn);
234 cblkheightexpn = int_min(tccp->cblkh, cbgheightexpn);
236 for (bandno = 0; bandno < res->numbands; bandno++) {
240 band = &res->bands[bandno];
241 band->bandno = resno == 0 ? 0 : bandno + 1;
242 x0b = (band->bandno == 1) || (band->bandno == 3) ? 1 : 0;
243 y0b = (band->bandno == 2) || (band->bandno == 3) ? 1 : 0;
245 if (band->bandno == 0) {
246 /* band border (global) */
247 band->x0 = int_ceildivpow2(tilec->x0, levelno);
248 band->y0 = int_ceildivpow2(tilec->y0, levelno);
249 band->x1 = int_ceildivpow2(tilec->x1, levelno);
250 band->y1 = int_ceildivpow2(tilec->y1, levelno);
252 /* band border (global) */
254 int_ceildivpow2(tilec->x0 -
255 (1 << levelno) * x0b, levelno + 1);
257 int_ceildivpow2(tilec->y0 -
258 (1 << levelno) * y0b, levelno + 1);
260 int_ceildivpow2(tilec->x1 -
261 (1 << levelno) * x0b, levelno + 1);
263 int_ceildivpow2(tilec->y1 -
264 (1 << levelno) * y0b, levelno + 1);
268 ss = &tccp->stepsizes[resno ==
269 0 ? 0 : 3 * (resno - 1) + bandno + 1];
272 0 ? dwt_getgain_real(band->bandno) : dwt_getgain(band->bandno);
273 numbps = img->comps[compno].prec + gain;
274 band->stepsize = (float)((1.0 + ss->mant / 2048.0) * pow(2.0, numbps - ss->expn));
275 band->numbps = ss->expn + tccp->numgbits - 1; /* WHY -1 ? */
278 (tcd_precinct_t *) malloc(3 * res->pw * res->ph *
279 sizeof(tcd_precinct_t));
281 for (i = 0; i < res->pw * res->ph * 3; i++) {
282 band->precincts[i].imsbtree = NULL;
283 band->precincts[i].incltree = NULL;
286 for (precno = 0; precno < res->pw * res->ph; precno++) {
287 int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
289 tlcbgxstart + (precno % res->pw) * (1 << cbgwidthexpn);
291 tlcbgystart + (precno / res->pw) * (1 << cbgheightexpn);
292 int cbgxend = cbgxstart + (1 << cbgwidthexpn);
293 int cbgyend = cbgystart + (1 << cbgheightexpn);
294 /* tcd_precinct_t *prc=&band->precincts[precno]; */
295 prc = &band->precincts[precno];
296 /* precinct size (global) */
297 prc->x0 = int_max(cbgxstart, band->x0);
298 prc->y0 = int_max(cbgystart, band->y0);
299 prc->x1 = int_min(cbgxend, band->x1);
300 prc->y1 = int_min(cbgyend, band->y1);
303 int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn;
305 int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn;
307 int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn;
309 int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn;
310 prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;
311 prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn;
314 (tcd_cblk_t *) malloc((prc->cw * prc->ch) *
316 prc->incltree = tgt_create(prc->cw, prc->ch);
317 prc->imsbtree = tgt_create(prc->cw, prc->ch);
319 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
321 tlcblkxstart + (cblkno % prc->cw) * (1 << cblkwidthexpn);
323 tlcblkystart + (cblkno / prc->cw) * (1 << cblkheightexpn);
324 int cblkxend = cblkxstart + (1 << cblkwidthexpn);
325 int cblkyend = cblkystart + (1 << cblkheightexpn);
327 cblk = &prc->cblks[cblkno];
328 /* code-block size (global) */
329 cblk->x0 = int_max(cblkxstart, prc->x0);
330 cblk->y0 = int_max(cblkystart, prc->y0);
331 cblk->x1 = int_min(cblkxend, prc->x1);
332 cblk->y1 = int_min(cblkyend, prc->y1);
339 /* tcd_dump(&tcd_image,curtileno); */
342 void tcd_free_encode(j2k_image_t * img, j2k_cp_t * cp, int curtileno)
344 int tileno, compno, resno, bandno, precno;
348 tcd_image.tw = cp->tw;
349 tcd_image.th = cp->th;
350 for (tileno = 0; tileno < 1; tileno++) {
351 /* j2k_tcp_t *tcp=&cp->tcps[curtileno]; */
352 tile = tcd_image.tiles;
353 for (compno = 0; compno < tile->numcomps; compno++) {
354 tilec = &tile->comps[compno];
355 for (resno = 0; resno < tilec->numresolutions; resno++) {
356 res = &tilec->resolutions[resno];
357 for (bandno = 0; bandno < res->numbands; bandno++) {
358 band = &res->bands[bandno];
359 for (precno = 0; precno < res->pw * res->ph; precno++) {
360 prc = &band->precincts[precno];
362 if (prc->incltree != NULL)
363 tgt_destroy(prc->incltree);
364 if (prc->imsbtree != NULL)
365 tgt_destroy(prc->imsbtree);
368 free(band->precincts);
371 free(tilec->resolutions);
375 free(tcd_image.tiles);
378 void tcd_init_encode(j2k_image_t * img, j2k_cp_t * cp, int curtileno)
380 int tileno, compno, resno, bandno, precno, cblkno;
382 for (tileno = 0; tileno < 1; tileno++) {
383 j2k_tcp_t *tcp = &cp->tcps[curtileno];
385 /* int previous_x0, previous_x1, previous_y0, previous_y1;*/
386 /* cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
387 int p = curtileno % cp->tw;
388 int q = curtileno / cp->tw;
389 tile = tcd_image.tiles;
391 /* 4 borders of the tile rescale on the image if necessary */
392 tile->x0 = int_max(cp->tx0 + p * cp->tdx, img->x0);
393 tile->y0 = int_max(cp->ty0 + q * cp->tdy, img->y0);
394 tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, img->x1);
395 tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, img->y1);
397 tile->numcomps = img->numcomps;
398 /* tile->PPT=img->PPT; */
399 /* Modification of the RATE >> */
400 for (j = 0; j < tcp->numlayers; j++) {
401 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*/
403 if (j && tcp->rates[j] < tcp->rates[j - 1] + 10) {
404 tcp->rates[j] = tcp->rates[j - 1] + 20;
406 if (!j && tcp->rates[j] < 30)
411 /* << Modification of the RATE */
412 /* tile->comps=(tcd_tilecomp_t*)realloc(tile->comps,img->numcomps*sizeof(tcd_tilecomp_t)); */
413 for (compno = 0; compno < tile->numcomps; compno++) {
414 j2k_tccp_t *tccp = &tcp->tccps[compno];
415 /* int realloc_op; */
417 tilec = &tile->comps[compno];
418 /* border of each tile component (global) */
419 tilec->x0 = int_ceildiv(tile->x0, img->comps[compno].dx);
420 tilec->y0 = int_ceildiv(tile->y0, img->comps[compno].dy);
421 tilec->x1 = int_ceildiv(tile->x1, img->comps[compno].dx);
422 tilec->y1 = int_ceildiv(tile->y1, img->comps[compno].dy);
425 (int *) malloc((tilec->x1 - tilec->x0) *
426 (tilec->y1 - tilec->y0) * sizeof(int));
427 tilec->numresolutions = tccp->numresolutions;
428 /* tilec->resolutions=(tcd_resolution_t*)realloc(tilec->resolutions,tilec->numresolutions*sizeof(tcd_resolution_t)); */
429 for (resno = 0; resno < tilec->numresolutions; resno++) {
431 int levelno = tilec->numresolutions - 1 - resno;
432 int tlprcxstart, tlprcystart, brprcxend, brprcyend;
433 int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend;
434 int cbgwidthexpn, cbgheightexpn;
435 int cblkwidthexpn, cblkheightexpn;
437 res = &tilec->resolutions[resno];
438 /* border for each resolution level (global) */
439 res->x0 = int_ceildivpow2(tilec->x0, levelno);
440 res->y0 = int_ceildivpow2(tilec->y0, levelno);
441 res->x1 = int_ceildivpow2(tilec->x1, levelno);
442 res->y1 = int_ceildivpow2(tilec->y1, levelno);
444 res->numbands = resno == 0 ? 1 : 3;
445 /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */
446 if (tccp->csty & J2K_CCP_CSTY_PRT) {
447 pdx = tccp->prcw[resno];
448 pdy = tccp->prch[resno];
453 /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
454 tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx;
455 tlprcystart = int_floordivpow2(res->y0, pdy) << pdy;
456 brprcxend = int_ceildivpow2(res->x1, pdx) << pdx;
457 brprcyend = int_ceildivpow2(res->y1, pdy) << pdy;
459 res->pw = (brprcxend - tlprcxstart) >> pdx;
460 res->ph = (brprcyend - tlprcystart) >> pdy;
463 tlcbgxstart = tlprcxstart;
464 tlcbgystart = tlprcystart;
465 brcbgxend = brprcxend;
466 brcbgyend = brprcyend;
470 tlcbgxstart = int_ceildivpow2(tlprcxstart, 1);
471 tlcbgystart = int_ceildivpow2(tlprcystart, 1);
472 brcbgxend = int_ceildivpow2(brprcxend, 1);
473 brcbgyend = int_ceildivpow2(brprcyend, 1);
474 cbgwidthexpn = pdx - 1;
475 cbgheightexpn = pdy - 1;
478 cblkwidthexpn = int_min(tccp->cblkw, cbgwidthexpn);
479 cblkheightexpn = int_min(tccp->cblkh, cbgheightexpn);
481 for (bandno = 0; bandno < res->numbands; bandno++) {
485 band = &res->bands[bandno];
486 band->bandno = resno == 0 ? 0 : bandno + 1;
487 x0b = (band->bandno == 1) || (band->bandno == 3) ? 1 : 0;
488 y0b = (band->bandno == 2) || (band->bandno == 3) ? 1 : 0;
490 if (band->bandno == 0) {
492 band->x0 = int_ceildivpow2(tilec->x0, levelno);
493 band->y0 = int_ceildivpow2(tilec->y0, levelno);
494 band->x1 = int_ceildivpow2(tilec->x1, levelno);
495 band->y1 = int_ceildivpow2(tilec->y1, levelno);
498 int_ceildivpow2(tilec->x0 -
499 (1 << levelno) * x0b, levelno + 1);
501 int_ceildivpow2(tilec->y0 -
502 (1 << levelno) * y0b, levelno + 1);
504 int_ceildivpow2(tilec->x1 -
505 (1 << levelno) * x0b, levelno + 1);
507 int_ceildivpow2(tilec->y1 -
508 (1 << levelno) * y0b, levelno + 1);
511 ss = &tccp->stepsizes[resno ==
512 0 ? 0 : 3 * (resno - 1) + bandno + 1];
515 0 ? dwt_getgain_real(band->bandno) : dwt_getgain(band->bandno);
516 numbps = img->comps[compno].prec + gain;
517 band->stepsize = (float)((1.0 + ss->mant / 2048.0) * pow(2.0, numbps - ss->expn));
518 band->numbps = ss->expn + tccp->numgbits - 1; /* WHY -1 ? */
520 for (precno = 0; precno < res->pw * res->ph; precno++) {
521 int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
523 tlcbgxstart + (precno % res->pw) * (1 << cbgwidthexpn);
525 tlcbgystart + (precno / res->pw) * (1 << cbgheightexpn);
526 int cbgxend = cbgxstart + (1 << cbgwidthexpn);
527 int cbgyend = cbgystart + (1 << cbgheightexpn);
529 prc = &band->precincts[precno];
530 /* precinct size (global) */
531 prc->x0 = int_max(cbgxstart, band->x0);
532 prc->y0 = int_max(cbgystart, band->y0);
533 prc->x1 = int_min(cbgxend, band->x1);
534 prc->y1 = int_min(cbgyend, band->y1);
537 int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn;
539 int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn;
541 int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn;
543 int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn;
544 prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;
545 prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn;
549 (tcd_cblk_t *) malloc(prc->cw * prc->ch *
552 if (prc->incltree != NULL)
553 tgt_destroy(prc->incltree);
554 if (prc->imsbtree != NULL)
555 tgt_destroy(prc->imsbtree);
557 prc->incltree = tgt_create(prc->cw, prc->ch);
558 prc->imsbtree = tgt_create(prc->cw, prc->ch);
560 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
562 tlcblkxstart + (cblkno % prc->cw) * (1 << cblkwidthexpn);
564 tlcblkystart + (cblkno / prc->cw) * (1 << cblkheightexpn);
565 int cblkxend = cblkxstart + (1 << cblkwidthexpn);
566 int cblkyend = cblkystart + (1 << cblkheightexpn);
567 cblk = &prc->cblks[cblkno];
569 /* code-block size (global) */
570 cblk->x0 = int_max(cblkxstart, prc->x0);
571 cblk->y0 = int_max(cblkystart, prc->y0);
572 cblk->x1 = int_min(cblkxend, prc->x1);
573 cblk->y1 = int_min(cblkyend, prc->y1);
581 /* tcd_dump(&tcd_image,0); */
584 void tcd_init(j2k_image_t * img, j2k_cp_t * cp)
586 int tileno, compno, resno, bandno, precno, cblkno, i, j, p, q;
587 unsigned int x0 = 0, y0 = 0, x1 = 0, y1 = 0, w, h;
590 tcd_image.tw = cp->tw;
591 tcd_image.th = cp->th;
593 (tcd_tile_t *) malloc(cp->tw * cp->th * sizeof(tcd_tile_t));
595 /*for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
596 j2k_tcp_t *tcp = &cp->tcps[tileno];
597 tcd_tile_t *tile = &tcd_image.tiles[tileno]; */
599 for (i = 0; i < cp->tileno_size; i++) {
600 j2k_tcp_t *tcp = &cp->tcps[cp->tileno[i]];
601 tcd_tile_t *tile = &tcd_image.tiles[cp->tileno[i]];
602 tileno = cp->tileno[i];
605 /* int previous_x0, previous_x1, previous_y0, previous_y1;*/
606 /* cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
607 p = tileno % cp->tw; /* si numerotation matricielle .. */
608 q = tileno / cp->tw; /* .. coordonnees de la tile (q,p) q pour ligne et p pour colonne */
610 /* 4 borders of the tile rescale on the image if necessary */
611 tile->x0 = int_max(cp->tx0 + p * cp->tdx, img->x0);
612 tile->y0 = int_max(cp->ty0 + q * cp->tdy, img->y0);
613 tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, img->x1);
614 tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, img->y1);
616 tile->numcomps = img->numcomps;
618 (tcd_tilecomp_t *) malloc(img->numcomps * sizeof(tcd_tilecomp_t));
619 for (compno = 0; compno < tile->numcomps; compno++) {
620 j2k_tccp_t *tccp = &tcp->tccps[compno];
621 tcd_tilecomp_t *tilec = &tile->comps[compno];
622 /* border of each tile component (global) */
623 tilec->x0 = int_ceildiv(tile->x0, img->comps[compno].dx);
624 tilec->y0 = int_ceildiv(tile->y0, img->comps[compno].dy);
625 tilec->x1 = int_ceildiv(tile->x1, img->comps[compno].dx);
626 tilec->y1 = int_ceildiv(tile->y1, img->comps[compno].dy);
629 (int *) malloc((tilec->x1 - tilec->x0) *
630 (tilec->y1 - tilec->y0) * sizeof(int));
631 tilec->numresolutions = tccp->numresolutions;
633 (tcd_resolution_t *) malloc(tilec->numresolutions *
634 sizeof(tcd_resolution_t));
635 for (resno = 0; resno < tilec->numresolutions; resno++) {
637 int levelno = tilec->numresolutions - 1 - resno;
638 int tlprcxstart, tlprcystart, brprcxend, brprcyend;
639 int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend;
640 int cbgwidthexpn, cbgheightexpn;
641 int cblkwidthexpn, cblkheightexpn;
642 tcd_resolution_t *res = &tilec->resolutions[resno];
644 /* border for each resolution level (global) */
645 res->x0 = int_ceildivpow2(tilec->x0, levelno);
646 res->y0 = int_ceildivpow2(tilec->y0, levelno);
647 res->x1 = int_ceildivpow2(tilec->x1, levelno);
648 res->y1 = int_ceildivpow2(tilec->y1, levelno);
650 res->numbands = resno == 0 ? 1 : 3;
651 /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */
652 if (tccp->csty & J2K_CCP_CSTY_PRT) {
653 pdx = tccp->prcw[resno];
654 pdy = tccp->prch[resno];
659 /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
660 tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx;
661 tlprcystart = int_floordivpow2(res->y0, pdy) << pdy;
662 brprcxend = int_ceildivpow2(res->x1, pdx) << pdx;
663 brprcyend = int_ceildivpow2(res->y1, pdy) << pdy;
664 res->pw = (res->x0 == res->x1) ? 0 : ((brprcxend - tlprcxstart) >> pdx); /* Mod Antonin : sizebug1*/
665 res->ph = (res->y0 == res->y1) ? 0 : ((brprcyend - tlprcystart) >> pdy); /* Mod Antonin : sizebug1*/
668 tlcbgxstart = tlprcxstart;
669 tlcbgystart = tlprcystart;
670 brcbgxend = brprcxend;
671 brcbgyend = brprcyend;
675 tlcbgxstart = int_ceildivpow2(tlprcxstart, 1);
676 tlcbgystart = int_ceildivpow2(tlprcystart, 1);
677 brcbgxend = int_ceildivpow2(brprcxend, 1);
678 brcbgyend = int_ceildivpow2(brprcyend, 1);
679 cbgwidthexpn = pdx - 1;
680 cbgheightexpn = pdy - 1;
683 cblkwidthexpn = int_min(tccp->cblkw, cbgwidthexpn);
684 cblkheightexpn = int_min(tccp->cblkh, cbgheightexpn);
686 for (bandno = 0; bandno < res->numbands; bandno++) {
690 tcd_band_t *band = &res->bands[bandno];
691 band->bandno = resno == 0 ? 0 : bandno + 1;
692 x0b = (band->bandno == 1) || (band->bandno == 3) ? 1 : 0;
693 y0b = (band->bandno == 2) || (band->bandno == 3) ? 1 : 0;
695 if (band->bandno == 0) {
696 /* band border (global) */
697 band->x0 = int_ceildivpow2(tilec->x0, levelno);
698 band->y0 = int_ceildivpow2(tilec->y0, levelno);
699 band->x1 = int_ceildivpow2(tilec->x1, levelno);
700 band->y1 = int_ceildivpow2(tilec->y1, levelno);
702 /* band border (global) */
704 int_ceildivpow2(tilec->x0 -
705 (1 << levelno) * x0b, levelno + 1);
707 int_ceildivpow2(tilec->y0 -
708 (1 << levelno) * y0b, levelno + 1);
710 int_ceildivpow2(tilec->x1 -
711 (1 << levelno) * x0b, levelno + 1);
713 int_ceildivpow2(tilec->y1 -
714 (1 << levelno) * y0b, levelno + 1);
717 ss = &tccp->stepsizes[resno ==
718 0 ? 0 : 3 * (resno - 1) + bandno + 1];
721 0 ? dwt_getgain_real(band->bandno) : dwt_getgain(band->bandno);
722 numbps = img->comps[compno].prec + gain;
723 band->stepsize = (float)((1.0 + ss->mant / 2048.0) * pow(2.0, numbps - ss->expn));
724 band->numbps = ss->expn + tccp->numgbits - 1; /* WHY -1 ? */
727 (tcd_precinct_t *) malloc(res->pw * res->ph *
728 sizeof(tcd_precinct_t));
730 for (precno = 0; precno < res->pw * res->ph; precno++) {
731 int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
733 tlcbgxstart + (precno % res->pw) * (1 << cbgwidthexpn);
735 tlcbgystart + (precno / res->pw) * (1 << cbgheightexpn);
736 int cbgxend = cbgxstart + (1 << cbgwidthexpn);
737 int cbgyend = cbgystart + (1 << cbgheightexpn);
738 tcd_precinct_t *prc = &band->precincts[precno];
739 /* precinct size (global) */
740 prc->x0 = int_max(cbgxstart, band->x0);
741 prc->y0 = int_max(cbgystart, band->y0);
742 prc->x1 = int_min(cbgxend, band->x1);
743 prc->y1 = int_min(cbgyend, band->y1);
746 int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn;
748 int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn;
750 int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn;
752 int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn;
753 prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;
754 prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn;
757 (tcd_cblk_t *) malloc(prc->cw * prc->ch *
760 prc->incltree = tgt_create(prc->cw, prc->ch);
761 prc->imsbtree = tgt_create(prc->cw, prc->ch);
763 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
765 tlcblkxstart + (cblkno % prc->cw) * (1 << cblkwidthexpn);
767 tlcblkystart + (cblkno / prc->cw) * (1 << cblkheightexpn);
768 int cblkxend = cblkxstart + (1 << cblkwidthexpn);
769 int cblkyend = cblkystart + (1 << cblkheightexpn);
770 tcd_cblk_t *cblk = &prc->cblks[cblkno];
771 /* code-block size (global) */
772 cblk->x0 = int_max(cblkxstart, prc->x0);
773 cblk->y0 = int_max(cblkystart, prc->y0);
774 cblk->x1 = int_min(cblkxend, prc->x1);
775 cblk->y1 = int_min(cblkyend, prc->y1);
782 /*tcd_dump(&tcd_image,0);*/
785 /* Allocate place to store the data decoded = final image */
786 /* Place limited by the tile really present in the codestream */
789 for (i = 0; i < img->numcomps; i++) {
790 for (j = 0; j < cp->tileno_size; j++) {
791 tileno = cp->tileno[j];
792 x0 = j == 0 ? tcd_image.tiles[tileno].comps[i].x0 : int_min(x0,
800 j == 0 ? tcd_image.tiles[tileno].comps[i].y0 : int_min(y0,
808 j == 0 ? tcd_image.tiles[tileno].comps[i].x1 : int_max(x1,
816 j == 0 ? tcd_image.tiles[tileno].comps[i].y1 : int_max(y1,
828 img->comps[i].data = (int *) calloc(w * h, sizeof(int));
831 img->comps[i].x0 = x0;
832 img->comps[i].y0 = y0;
836 void tcd_makelayer_fixed(int layno, int final)
838 int compno, resno, bandno, precno, cblkno;
839 int value; /*, matrice[tcd_tcp->numlayers][tcd_tile->comps[0].numresolutions][3];*/
840 int matrice[10][10][3];
843 /*matrice=(int*)malloc(tcd_tcp->numlayers*tcd_tile->comps[0].numresolutions*3*sizeof(int)); */
845 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
846 tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
847 for (i = 0; i < tcd_tcp->numlayers; i++) {
848 for (j = 0; j < tilec->numresolutions; j++) {
849 for (k = 0; k < 3; k++) {
852 matrice[i * tilec->numresolutions * 3 +
855 (float) (tcd_img->comps[compno].prec / 16.0));
860 for (resno = 0; resno < tilec->numresolutions; resno++) {
861 tcd_resolution_t *res = &tilec->resolutions[resno];
862 for (bandno = 0; bandno < res->numbands; bandno++) {
863 tcd_band_t *band = &res->bands[bandno];
864 for (precno = 0; precno < res->pw * res->ph; precno++) {
865 tcd_precinct_t *prc = &band->precincts[precno];
866 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
867 tcd_cblk_t *cblk = &prc->cblks[cblkno];
868 tcd_layer_t *layer = &cblk->layers[layno];
870 int imsb = tcd_img->comps[compno].prec - cblk->numbps; /* number of bit-plan equal to zero */
871 /* Correction of the matrix of coefficient to include the IMSB information */
874 value = matrice[layno][resno][bandno];
881 matrice[layno][resno][bandno] -
882 matrice[layno - 1][resno][bandno];
883 if (imsb >= matrice[layno - 1][resno][bandno]) {
884 value -= (imsb - matrice[layno - 1][resno][bandno]);
891 cblk->numpassesinlayers = 0;
893 n = cblk->numpassesinlayers;
894 if (cblk->numpassesinlayers == 0) {
896 n = 3 * value - 2 + cblk->numpassesinlayers;
898 n = cblk->numpassesinlayers;
900 n = 3 * value + cblk->numpassesinlayers;
902 layer->numpasses = n - cblk->numpassesinlayers;
904 if (!layer->numpasses)
907 if (cblk->numpassesinlayers == 0) {
908 layer->len = cblk->passes[n - 1].rate;
909 layer->data = cblk->data;
912 cblk->passes[n - 1].rate -
913 cblk->passes[cblk->numpassesinlayers - 1].rate;
916 cblk->passes[cblk->numpassesinlayers - 1].rate;
919 cblk->numpassesinlayers = n;
927 void tcd_rateallocate_fixed()
931 for (layno = 0; layno < tcd_tcp->numlayers; layno++) {
932 tcd_makelayer_fixed(layno, 1);
936 void tcd_makelayer(int layno, double thresh, int final)
938 int compno, resno, bandno, precno, cblkno, passno;
940 tcd_tile->distolayer[layno] = 0; /*add fixed_quality*/
942 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
943 tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
944 for (resno = 0; resno < tilec->numresolutions; resno++) {
945 tcd_resolution_t *res = &tilec->resolutions[resno];
946 for (bandno = 0; bandno < res->numbands; bandno++) {
947 tcd_band_t *band = &res->bands[bandno];
948 for (precno = 0; precno < res->pw * res->ph; precno++) {
949 tcd_precinct_t *prc = &band->precincts[precno];
950 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
951 tcd_cblk_t *cblk = &prc->cblks[cblkno];
952 tcd_layer_t *layer = &cblk->layers[layno];
956 cblk->numpassesinlayers = 0;
958 n = cblk->numpassesinlayers;
959 for (passno = cblk->numpassesinlayers;
960 passno < cblk->totalpasses; passno++) {
963 tcd_pass_t *pass = &cblk->passes[passno];
966 dd = pass->distortiondec;
968 dr = pass->rate - cblk->passes[n - 1].rate;
969 dd = pass->distortiondec - cblk->passes[n -
978 if (dd / dr >= thresh)
981 layer->numpasses = n - cblk->numpassesinlayers;
983 if (!layer->numpasses) {
988 if (cblk->numpassesinlayers == 0) {
989 layer->len = cblk->passes[n - 1].rate;
990 layer->data = cblk->data;
991 layer->disto = cblk->passes[n - 1].distortiondec;
993 layer->len = cblk->passes[n - 1].rate -
994 cblk->passes[cblk->numpassesinlayers - 1].rate;
997 cblk->passes[cblk->numpassesinlayers - 1].rate;
999 cblk->passes[n - 1].distortiondec -
1000 cblk->passes[cblk->numpassesinlayers - 1].distortiondec;
1003 tcd_tile->distolayer[layno] += layer->disto; /*add fixed_quality*/
1006 cblk->numpassesinlayers = n;
1014 void tcd_rateallocate(unsigned char *dest, int len, info_image * info_IM)
1016 int compno, resno, bandno, precno, cblkno, passno, layno;
1018 double cumdisto[100]; /*add fixed_quality*/
1019 const double K = 1; /* 1.1; //add fixed_quality*/
1025 tcd_tile->nbpix = 0; /*add fixed_quality*/
1027 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
1028 tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
1031 for (resno = 0; resno < tilec->numresolutions; resno++) {
1032 tcd_resolution_t *res = &tilec->resolutions[resno];
1033 for (bandno = 0; bandno < res->numbands; bandno++) {
1034 tcd_band_t *band = &res->bands[bandno];
1035 for (precno = 0; precno < res->pw * res->ph; precno++) {
1036 tcd_precinct_t *prc = &band->precincts[precno];
1037 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
1038 tcd_cblk_t *cblk = &prc->cblks[cblkno];
1039 for (passno = 0; passno < cblk->totalpasses; passno++) {
1040 tcd_pass_t *pass = &cblk->passes[passno];
1045 dd = pass->distortiondec;
1047 dr = pass->rate - cblk->passes[passno - 1].rate;
1048 dd = pass->distortiondec -
1049 cblk->passes[passno - 1].distortiondec;
1057 if (rdslope < min) {
1060 if (rdslope > max) {
1065 tcd_tile->nbpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0)); /*add fixed_quality*/
1067 tilec->nbpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0)); /*add fixed_quality*/
1073 maxSE += (((double)(1 << tcd_img->comps[compno].prec) - 1.0) * ((double)(1 << tcd_img->comps[compno].prec) -1.0)) * ((double)(tilec->nbpix));
1076 /* add antonin index */
1077 if (info_IM->index_on) {
1078 info_tile *info_TL = &info_IM->tile[tcd_tileno];
1079 info_TL->nbpix = tcd_tile->nbpix;
1080 info_TL->distotile = tcd_tile->distotile;
1082 (double *) malloc(tcd_tcp->numlayers * sizeof(double));
1086 for (layno = 0; layno < tcd_tcp->numlayers; layno++) {
1087 volatile double lo = min;
1088 volatile double hi = max;
1089 volatile int success = 0;
1090 volatile int maxlen = tcd_tcp->rates[layno] ? int_min(tcd_tcp->rates[layno], len) : len; /*Mod antonin losslessbug*/
1091 volatile double goodthresh;
1093 double distotarget; /*add fixed_quality*/
1095 distotarget = tcd_tile->distotile - ((K * maxSE) / pow(10, tcd_tcp->distoratio[layno] / 10)); /* add fixed_quality*/
1097 if ((tcd_tcp->rates[layno]) || (tcd_cp->disto_alloc==0)) {
1098 for (i = 0; i < 32; i++) {
1099 volatile double thresh = (lo + hi) / 2;
1101 double distoachieved = 0; /* add fixed_quality*/
1103 tcd_makelayer(layno, thresh, 0);
1105 if (tcd_cp->fixed_quality) { /* add fixed_quality*/
1108 0 ? tcd_tile->distolayer[0] : cumdisto[layno - 1] +
1109 tcd_tile->distolayer[layno];
1110 if (distoachieved < distotarget) {
1117 t2_encode_packets(tcd_img, tcd_cp, tcd_tileno, tcd_tile,
1118 layno + 1, dest, maxlen, info_IM);
1119 /* fprintf(stderr, "rate alloc: len=%d, max=%d\n", l, maxlen); */
1128 goodthresh = thresh;
1136 longjmp(j2k_error, 1);
1139 if (info_IM->index_on) { /* Threshold for Marcela Index */
1140 info_IM->tile[tcd_tileno].thresh[layno] = goodthresh;
1142 tcd_makelayer(layno, goodthresh, 1);
1144 cumdisto[layno] = layno == 0 ? tcd_tile->distolayer[0] : cumdisto[layno - 1] + tcd_tile->distolayer[layno]; /* add fixed_quality*/
1149 tcd_encode_tile_pxm(int tileno, unsigned char *dest, int len,
1150 info_image * info_IM)
1156 j2k_tcp_t *tcp = &tcd_cp->tcps[0];
1157 j2k_tccp_t *tccp = &tcp->tccps[0];
1159 tcd_tileno = tileno;
1160 tcd_tile = tcd_image.tiles;
1161 tcd_tcp = &tcd_cp->tcps[tileno];
1163 /* INDEX >> "Precinct_nb_X et Precinct_nb_Y" */
1164 if (info_IM->index_on) {
1165 tcd_tilecomp_t *tilec_idx = &tile->comps[0]; /*Based on Component 0*/
1167 for (i = 0; i < tilec_idx->numresolutions; i++) {
1169 tcd_resolution_t *res_idx = &tilec_idx->resolutions[i];
1171 info_IM->tile[tileno].pw[i] = res_idx->pw;
1172 info_IM->tile[tileno].ph[i] = res_idx->ph;
1174 npck+=res_idx->pw * res_idx->ph;
1176 info_IM->tile[tileno].pdx[i] = tccp->prcw[i];
1177 info_IM->tile[tileno].pdy[i] = tccp->prch[i];
1180 info_IM->tile[tileno].packet = (info_packet *) calloc(info_IM->Comp * info_IM->Layer * npck, sizeof(info_packet));
1184 /*---------------TILE-------------------*/
1188 for (compno = 0; compno < tile->numcomps; compno++) {
1195 tcd_tilecomp_t *tilec = &tile->comps[compno];
1197 tcd_img->comps[compno].sgnd ? 0 : 1 << (tcd_img->comps[compno].
1199 int offset_x, offset_y;
1201 offset_x = int_ceildiv(tcd_img->x0, tcd_img->comps[compno].dx);
1202 offset_y = int_ceildiv(tcd_img->y0, tcd_img->comps[compno].dy);
1203 tw = tilec->x1 - tilec->x0;
1204 w = int_ceildiv(tcd_img->x1 - tcd_img->x0, tcd_img->comps[compno].dx);
1205 sprintf(tmp, "Compo%d", compno); /* component file */
1206 src = fopen(tmp, "rb");
1208 fprintf(stderr, "failed to open %s for reading\n", tmp);
1212 /* read the Compo file to extract data of the tile */
1214 fseek(src, (tilec->x0 - offset_x) + (tilec->y0 - offset_y) * w,
1216 k = (tilec->x0 - offset_x) + (tilec->y0 - offset_y) * w;
1217 for (j = tilec->y0; j < tilec->y1; j++) {
1218 for (i = tilec->x0; i < tilec->x1; i++) {
1219 if (tcd_tcp->tccps[compno].qmfbid == 1) {
1221 tilec->data[i - tilec->x0 + (j - tilec->y0) * tw] =
1224 } else if (tcd_tcp->tccps[compno].qmfbid == 0) {
1226 tilec->data[i - tilec->x0 + (j - tilec->y0) * tw] =
1227 (elmt - adjust) << 13;
1231 fseek(src, (tilec->x0 - offset_x) + (j + 1 - offset_y) * w - k,
1233 k = tilec->x0 - offset_x + (j + 1 - offset_y) * w;
1239 /*----------------MCT-------------------*/
1242 if (tcd_tcp->tccps[0].qmfbid == 0) {
1243 mct_encode_real(tile->comps[0].data, tile->comps[1].data,
1244 tile->comps[2].data,
1245 (tile->comps[0].x1 -
1246 tile->comps[0].x0) * (tile->comps[0].y1 -
1247 tile->comps[0].y0));
1249 mct_encode(tile->comps[0].data, tile->comps[1].data,
1250 tile->comps[2].data,
1251 (tile->comps[0].x1 -
1252 tile->comps[0].x0) * (tile->comps[0].y1 -
1253 tile->comps[0].y0));
1256 /*----------------DWT---------------------*/
1259 for (compno = 0; compno < tile->numcomps; compno++) {
1260 tcd_tilecomp_t *tilec = &tile->comps[compno];
1261 if (tcd_tcp->tccps[compno].qmfbid == 1) {
1263 } else if (tcd_tcp->tccps[compno].qmfbid == 0) {
1264 dwt_encode_real(tilec);
1268 /*------------------TIER1-----------------*/
1271 t1_encode_cblks(tile, tcd_tcp);
1273 /*-----------RATE-ALLOCATE------------------*/
1274 info_IM->index_write = 0; /* INDEX */
1276 if (tcd_cp->disto_alloc || tcd_cp->fixed_quality) /* mod fixed_quality*/
1277 /* Normal Rate/distortion allocation */
1278 tcd_rateallocate(dest, len, info_IM);
1280 /* Fixed layer allocation */
1281 tcd_rateallocate_fixed();
1283 /*--------------TIER2------------------*/
1284 info_IM->index_write = 1; /* INDEX */
1285 l = t2_encode_packets(tcd_img, tcd_cp, tileno, tile,
1286 tcd_tcp->numlayers, dest, len, info_IM);
1287 /*---------------CLEAN-------------------*/
1289 time7 = clock() - time7;
1290 fprintf(stdout,"total: %ld.%.3ld s\n", time7 / CLOCKS_PER_SEC,
1291 (time7 % (int)CLOCKS_PER_SEC) * 1000 / CLOCKS_PER_SEC);
1293 /* cleaning memory */
1294 for (compno = 0; compno < tile->numcomps; compno++) {
1295 tilec = &tile->comps[compno];
1303 tcd_encode_tile_pgx(int tileno, unsigned char *dest, int len,
1304 info_image * info_IM)
1310 j2k_tcp_t *tcp = &tcd_cp->tcps[0];
1311 j2k_tccp_t *tccp = &tcp->tccps[0];
1313 tcd_tileno = tileno;
1314 tcd_tile = tcd_image.tiles;
1315 tcd_tcp = &tcd_cp->tcps[tileno];
1317 /* INDEX >> "Precinct_nb_X et Precinct_nb_Y" */
1318 if (info_IM->index_on) {
1319 tcd_tilecomp_t *tilec_idx = &tile->comps[0]; /*Based on Component 0*/
1321 for (i = 0; i < tilec_idx->numresolutions; i++) {
1323 tcd_resolution_t *res_idx = &tilec_idx->resolutions[i];
1325 info_IM->tile[tileno].pw[i] = res_idx->pw;
1326 info_IM->tile[tileno].ph[i] = res_idx->ph;
1328 npck+=res_idx->pw * res_idx->ph;
1330 info_IM->tile[tileno].pdx[i] = tccp->prcw[i];
1331 info_IM->tile[tileno].pdy[i] = tccp->prch[i];
1334 info_IM->tile[tileno].packet = (info_packet *) calloc(info_IM->Comp * info_IM->Layer * npck, sizeof(info_packet));
1337 /*---------------TILE-------------------*/
1340 for (compno = 0; compno < tile->numcomps; compno++) {
1347 tcd_tilecomp_t *tilec = &tile->comps[compno];
1349 tcd_img->comps[compno].sgnd ? 0 : 1 << (tcd_img->comps[compno].
1351 int offset_x, offset_y;
1353 offset_x = int_ceildiv(tcd_img->x0, tcd_img->comps[compno].dx);
1354 offset_y = int_ceildiv(tcd_img->y0, tcd_img->comps[compno].dy);
1355 tw = tilec->x1 - tilec->x0;
1356 w = int_ceildiv(tcd_img->x1 - tcd_img->x0, tcd_img->comps[compno].dx);
1357 sprintf(tmp, "bandtile%d", tileno / tcd_cp->tw + 1); /* bandtile file opening */
1358 src = fopen(tmp, "rb");
1360 fprintf(stderr, "failed to open %s for reading\n", tmp);
1363 /* Extract data from bandtile file limited to the current tile */
1365 while (k < tilec->x0 - offset_x) {
1367 fscanf(src, "%d", &elmt);
1370 for (j = 0; j < tilec->y1 - tilec->y0; j++) {
1371 for (i = tilec->x0; i < tilec->x1; i++) {
1372 if (tcd_tcp->tccps[compno].qmfbid == 1) {
1373 fscanf(src, "%d", &elmt);
1374 tilec->data[i - tilec->x0 + (j) * tw] = elmt - adjust;
1376 } else if (tcd_tcp->tccps[compno].qmfbid == 0) {
1377 fscanf(src, "%d", &elmt);
1378 tilec->data[i - tilec->x0 + (j) * tw] = (elmt - adjust) << 13;
1382 while (k < tilec->x0 - offset_x + (j + 1) * w) {
1384 fscanf(src, "%d", &elmt);
1390 /*----------------MCT-------------------*/
1393 if (tcd_tcp->tccps[0].qmfbid == 0) {
1394 mct_encode_real(tile->comps[0].data, tile->comps[1].data,
1395 tile->comps[2].data,
1396 (tile->comps[0].x1 -
1397 tile->comps[0].x0) * (tile->comps[0].y1 -
1398 tile->comps[0].y0));
1400 mct_encode(tile->comps[0].data, tile->comps[1].data,
1401 tile->comps[2].data,
1402 (tile->comps[0].x1 -
1403 tile->comps[0].x0) * (tile->comps[0].y1 -
1404 tile->comps[0].y0));
1408 /*----------------DWT---------------------*/
1411 for (compno = 0; compno < tile->numcomps; compno++) {
1412 tcd_tilecomp_t *tilec = &tile->comps[compno];
1413 if (tcd_tcp->tccps[compno].qmfbid == 1) {
1415 } else if (tcd_tcp->tccps[compno].qmfbid == 0) {
1416 dwt_encode_real(tilec);
1421 /*------------------TIER1-----------------*/
1424 t1_encode_cblks(tile, tcd_tcp);
1426 /*-----------RATE-ALLOCATE------------------*/
1428 info_IM->index_write = 0; /* INDEX */
1430 if (tcd_cp->disto_alloc || tcd_cp->fixed_quality) /* mod fixed_quality*/
1432 /* Normal Rate/distortion allocation */
1434 tcd_rateallocate(dest, len, info_IM);
1437 /* Fixed layer allocation */
1439 tcd_rateallocate_fixed();
1441 /*--------------TIER2------------------*/
1442 info_IM->index_write = 1; /* INDEX */
1444 l = t2_encode_packets(tcd_img, tcd_cp, tileno, tile,
1445 tcd_tcp->numlayers, dest, len, info_IM);
1447 /*---------------CLEAN-------------------*/
1448 time = clock() - time;
1449 fprintf(stdout,"total: %ld.%.3ld s\n", time / CLOCKS_PER_SEC,
1450 (time % (int)CLOCKS_PER_SEC) * 1000 / CLOCKS_PER_SEC);
1452 for (compno = 0; compno < tile->numcomps; compno++) {
1453 tilec = &tile->comps[compno];
1461 int tcd_decode_tile(unsigned char *src, int len, int tileno)
1469 tcd_tileno = tileno;
1470 tcd_tile = &tcd_image.tiles[tileno];
1471 tcd_tcp = &tcd_cp->tcps[tileno];
1476 fprintf(stdout, "Tile %d of %d decoded in ", tileno + 1,
1477 tcd_cp->tw * tcd_cp->th);
1479 /*--------------TIER2------------------*/
1481 l = t2_decode_packets(src, len, tcd_img, tcd_cp, tileno, tile);
1485 fprintf(stderr, "tcd_decode: incomplete bistream\n");
1488 /*------------------TIER1-----------------*/
1490 t1_decode_cblks(tile, tcd_tcp);
1492 /*----------------DWT---------------------*/
1494 for (compno = 0; compno < tile->numcomps; compno++) {
1495 tcd_tilecomp_t *tilec = &tile->comps[compno];
1496 if (tcd_cp->reduce != 0) {
1497 tcd_img->comps[compno].resno_decoded =
1498 tile->comps[compno].numresolutions - tcd_cp->reduce - 1;
1501 if (tcd_tcp->tccps[compno].qmfbid == 1) {
1503 tilec->numresolutions - 1 -
1504 tcd_img->comps[compno].resno_decoded);
1506 dwt_decode_real(tilec,
1507 tilec->numresolutions - 1 -
1508 tcd_img->comps[compno].resno_decoded);
1512 if (tile->comps[compno].numresolutions > 0)
1513 tcd_img->comps[compno].factor =
1514 tile->comps[compno].numresolutions -
1515 (tcd_img->comps[compno].resno_decoded + 1);
1518 /*----------------MCT-------------------*/
1521 if (tcd_tcp->tccps[0].qmfbid == 1) {
1522 mct_decode(tile->comps[0].data, tile->comps[1].data,
1523 tile->comps[2].data,
1524 (tile->comps[0].x1 -
1525 tile->comps[0].x0) * (tile->comps[0].y1 -
1526 tile->comps[0].y0));
1528 mct_decode_real(tile->comps[0].data, tile->comps[1].data,
1529 tile->comps[2].data,
1530 (tile->comps[0].x1 -
1531 tile->comps[0].x0) * (tile->comps[0].y1 -
1532 tile->comps[0].y0));
1536 /*---------------TILE-------------------*/
1538 for (compno = 0; compno < tile->numcomps; compno++) {
1539 tcd_tilecomp_t *tilec = &tile->comps[compno];
1540 tcd_resolution_t *res =
1541 &tilec->resolutions[tcd_img->comps[compno].resno_decoded];
1543 tcd_img->comps[compno].sgnd ? 0 : 1 << (tcd_img->comps[compno].
1546 tcd_img->comps[compno].
1547 sgnd ? -(1 << (tcd_img->comps[compno].prec - 1)) : 0;
1549 tcd_img->comps[compno].
1550 sgnd ? (1 << (tcd_img->comps[compno].prec - 1)) -
1551 1 : (1 << tcd_img->comps[compno].prec) - 1;
1553 int tw = tilec->x1 - tilec->x0;
1554 int w = tcd_img->comps[compno].w;
1557 int offset_x = int_ceildivpow2(tcd_img->comps[compno].x0,
1558 tcd_img->comps[compno].factor);
1559 int offset_y = int_ceildivpow2(tcd_img->comps[compno].y0,
1560 tcd_img->comps[compno].factor);
1562 for (j = res->y0; j < res->y1; j++) {
1563 for (i = res->x0; i < res->x1; i++) {
1566 double tmp = (tilec->data[i - res->x0 + (j - res->y0) * tw])/8192.0;
1569 if (tcd_tcp->tccps[compno].qmfbid == 1) {
1570 v = tilec->data[i - res->x0 + (j - res->y0) * tw];
1572 tmp2=((int) (floor(fabs(tmp)))) + ((int) floor(fabs(tmp*2))%2);
1573 v = ((tmp<0)?-tmp2:tmp2);
1578 tcd_img->comps[compno].data[(i - offset_x) +
1579 (j - offset_y) * w] =
1580 int_clamp(v, min, max);
1585 time = clock() - time;
1586 fprintf(stdout, "%ld.%.3ld s\n", time / CLOCKS_PER_SEC,
1587 (time % (int)CLOCKS_PER_SEC) * 1000 / CLOCKS_PER_SEC);
1589 for (compno = 0; compno < tile->numcomps; compno++) {
1590 free(tcd_image.tiles[tileno].comps[compno].data);
1594 longjmp(j2k_error, 1);
1602 void tcd_dec_release()
1605 int tileno,compno,resno,bandno,precno;
1606 for (tileno=0;tileno<tcd_image.tw*tcd_image.th;tileno++) {
1607 tcd_tile_t tile=tcd_image.tiles[tileno];
1608 for (compno=0;compno<tile.numcomps;compno++) {
1609 tcd_tilecomp_t tilec=tile.comps[compno];
1610 for (resno=0;resno<tilec.numresolutions;resno++) {
1611 tcd_resolution_t res=tilec.resolutions[resno];
1612 for (bandno=0;bandno<res.numbands;bandno++) {
1613 tcd_band_t band=res.bands[bandno];
1614 for (precno=0;precno<res.ph*res.pw;precno++) {
1615 tcd_precinct_t prec=band.precincts[precno];
1616 if (prec.cblks!=NULL) free(prec.cblks);
1617 if (prec.imsbtree!=NULL) free(prec.imsbtree);
1618 if (prec.incltree!=NULL) free(prec.incltree);
1620 if (band.precincts!=NULL) free(band.precincts);
1623 if (tilec.resolutions!=NULL) free(tilec.resolutions);
1625 if (tile.comps!=NULL) free(tile.comps);
1627 if (tcd_image.tiles!=NULL) free(tcd_image.tiles);