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;
64 fprintf(stdout, "image {\n");
65 fprintf(stdout, " tw=%d, th=%d x0=%d x1=%d y0=%d y1=%d\n", img->tw,
66 img->th, tcd_img->x0, tcd_img->x1, tcd_img->y0, tcd_img->y1);
67 for (tileno = 0; tileno < img->th * img->tw; tileno++) {
68 tcd_tile_t *tile = &tcd_image.tiles[tileno];
69 fprintf(stdout, " tile {\n");
70 fprintf(stdout, " x0=%d, y0=%d, x1=%d, y1=%d, numcomps=%d\n",
71 tile->x0, tile->y0, tile->x1, tile->y1, tile->numcomps);
72 for (compno = 0; compno < tile->numcomps; compno++) {
73 tcd_tilecomp_t *tilec = &tile->comps[compno];
74 fprintf(stdout, " tilec {\n");
76 " x0=%d, y0=%d, x1=%d, y1=%d, numresolutions=%d\n",
77 tilec->x0, tilec->y0, tilec->x1, tilec->y1,
78 tilec->numresolutions);
79 for (resno = 0; resno < tilec->numresolutions; resno++) {
80 tcd_resolution_t *res = &tilec->resolutions[resno];
81 fprintf(stdout, "\n res {\n");
83 " x0=%d, y0=%d, x1=%d, y1=%d, pw=%d, ph=%d, numbands=%d\n",
84 res->x0, res->y0, res->x1, res->y1, res->pw, res->ph,
86 for (bandno = 0; bandno < res->numbands; bandno++) {
87 tcd_band_t *band = &res->bands[bandno];
88 fprintf(stdout, " band {\n");
90 " x0=%d, y0=%d, x1=%d, y1=%d, stepsize=%f, numbps=%d\n",
91 band->x0, band->y0, band->x1, band->y1,
92 band->stepsize, band->numbps);
93 for (precno = 0; precno < res->pw * res->ph; precno++) {
94 tcd_precinct_t *prec = &band->precincts[precno];
95 fprintf(stdout, " prec {\n");
97 " x0=%d, y0=%d, x1=%d, y1=%d, cw=%d, ch=%d\n",
98 prec->x0, prec->y0, prec->x1, prec->y1,
100 for (cblkno = 0; cblkno < prec->cw * prec->ch; cblkno++) {
101 tcd_cblk_t *cblk = &prec->cblks[cblkno];
102 fprintf(stdout, " cblk {\n");
104 " x0=%d, y0=%d, x1=%d, y1=%d\n",
105 cblk->x0, cblk->y0, cblk->x1, cblk->y1);
106 fprintf(stdout, " }\n");
108 fprintf(stdout, " }\n");
110 fprintf(stdout, " }\n");
112 fprintf(stdout, " }\n");
114 fprintf(stdout, " }\n");
116 fprintf(stdout, " }\n");
118 fprintf(stdout, "}\n");
121 void tcd_malloc_encode(j2k_image_t * img, j2k_cp_t * cp, int curtileno)
123 int tileno, compno, resno, bandno, precno, cblkno;
126 tcd_image.tw = cp->tw;
127 tcd_image.th = cp->th;
128 tcd_image.tiles = (tcd_tile_t *) malloc(sizeof(tcd_tile_t));
130 for (tileno = 0; tileno < 1; tileno++) {
131 j2k_tcp_t *tcp = &cp->tcps[curtileno];
133 /* cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
134 int p = curtileno % cp->tw; /* si numerotation matricielle .. */
135 int q = curtileno / cp->tw; /* .. coordonnees de la tile (q,p) q pour ligne et p pour colonne */
136 /* tcd_tile_t *tile=&tcd_image.tiles[tileno]; */
137 tile = tcd_image.tiles;
138 /* 4 borders of the tile rescale on the image if necessary */
139 tile->x0 = int_max(cp->tx0 + p * cp->tdx, img->x0);
140 tile->y0 = int_max(cp->ty0 + q * cp->tdy, img->y0);
141 tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, img->x1);
142 tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, img->y1);
143 tile->numcomps = img->numcomps;
144 /* tile->PPT=img->PPT; */
145 /* Modification of the RATE >> */
146 for (j = 0; j < tcp->numlayers; j++) {
147 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
149 if (j && tcp->rates[j] < tcp->rates[j - 1] + 10) {
150 tcp->rates[j] = tcp->rates[j - 1] + 20;
152 if (!j && tcp->rates[j] < 30)
157 /* << Modification of the RATE */
160 (tcd_tilecomp_t *) malloc(img->numcomps * sizeof(tcd_tilecomp_t));
161 for (compno = 0; compno < tile->numcomps; compno++) {
162 j2k_tccp_t *tccp = &tcp->tccps[compno];
163 /* tcd_tilecomp_t *tilec=&tile->comps[compno]; */
164 tilec = &tile->comps[compno];
165 /* border of each tile component (global) */
166 tilec->x0 = int_ceildiv(tile->x0, img->comps[compno].dx);
168 tilec->y0 = int_ceildiv(tile->y0, img->comps[compno].dy);
169 tilec->x1 = int_ceildiv(tile->x1, img->comps[compno].dx);
170 tilec->y1 = int_ceildiv(tile->y1, img->comps[compno].dy);
173 (int *) malloc((tilec->x1 - tilec->x0) *
174 (tilec->y1 - tilec->y0) * sizeof(int));
175 tilec->numresolutions = tccp->numresolutions;
178 (tcd_resolution_t *) malloc(tilec->numresolutions *
179 sizeof(tcd_resolution_t));
181 for (resno = 0; resno < tilec->numresolutions; resno++) {
183 int levelno = tilec->numresolutions - 1 - resno;
184 int tlprcxstart, tlprcystart, brprcxend, brprcyend;
185 int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend;
186 int cbgwidthexpn, cbgheightexpn;
187 int cblkwidthexpn, cblkheightexpn;
188 /* tcd_resolution_t *res=&tilec->resolutions[resno]; */
190 res = &tilec->resolutions[resno];
192 /* border for each resolution level (global) */
193 res->x0 = int_ceildivpow2(tilec->x0, levelno);
194 res->y0 = int_ceildivpow2(tilec->y0, levelno);
195 res->x1 = int_ceildivpow2(tilec->x1, levelno);
196 res->y1 = int_ceildivpow2(tilec->y1, levelno);
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];
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;
213 res->pw = (brprcxend - tlprcxstart) >> pdx;
214 res->ph = (brprcyend - tlprcystart) >> pdy;
217 tlcbgxstart = tlprcxstart;
218 tlcbgystart = tlprcystart;
219 brcbgxend = brprcxend;
220 brcbgyend = brprcyend;
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;
232 cblkwidthexpn = int_min(tccp->cblkw, cbgwidthexpn);
233 cblkheightexpn = int_min(tccp->cblkh, cbgheightexpn);
235 for (bandno = 0; bandno < res->numbands; bandno++) {
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;
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);
251 /* band border (global) */
253 int_ceildivpow2(tilec->x0 -
254 (1 << levelno) * x0b, levelno + 1);
256 int_ceildivpow2(tilec->y0 -
257 (1 << levelno) * y0b, levelno + 1);
259 int_ceildivpow2(tilec->x1 -
260 (1 << levelno) * x0b, levelno + 1);
262 int_ceildivpow2(tilec->y1 -
263 (1 << levelno) * y0b, levelno + 1);
267 ss = &tccp->stepsizes[resno ==
268 0 ? 0 : 3 * (resno - 1) + bandno + 1];
271 0 ? dwt_getgain_real(band->bandno) : dwt_getgain(band->bandno);
272 numbps = img->comps[compno].prec + gain;
273 band->stepsize = (1.0 + ss->mant / 2048.0) * pow(2.0, numbps - ss->expn);
274 band->numbps = ss->expn + tccp->numgbits - 1; /* WHY -1 ? */
277 (tcd_precinct_t *) malloc(3 * res->pw * res->ph *
278 sizeof(tcd_precinct_t));
280 for (i = 0; i < res->pw * res->ph * 3; i++) {
281 band->precincts[i].imsbtree = NULL;
282 band->precincts[i].incltree = NULL;
285 for (precno = 0; precno < res->pw * res->ph; precno++) {
286 int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
288 tlcbgxstart + (precno % res->pw) * (1 << cbgwidthexpn);
290 tlcbgystart + (precno / res->pw) * (1 << cbgheightexpn);
291 int cbgxend = cbgxstart + (1 << cbgwidthexpn);
292 int cbgyend = cbgystart + (1 << cbgheightexpn);
293 /* tcd_precinct_t *prc=&band->precincts[precno]; */
294 prc = &band->precincts[precno];
295 /* precinct size (global) */
296 prc->x0 = int_max(cbgxstart, band->x0);
297 prc->y0 = int_max(cbgystart, band->y0);
298 prc->x1 = int_min(cbgxend, band->x1);
299 prc->y1 = int_min(cbgyend, band->y1);
302 int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn;
304 int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn;
306 int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn;
308 int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn;
309 prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;
310 prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn;
313 (tcd_cblk_t *) malloc((prc->cw * prc->ch) *
315 prc->incltree = tgt_create(prc->cw, prc->ch);
316 prc->imsbtree = tgt_create(prc->cw, prc->ch);
318 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
320 tlcblkxstart + (cblkno % prc->cw) * (1 << cblkwidthexpn);
322 tlcblkystart + (cblkno / prc->cw) * (1 << cblkheightexpn);
323 int cblkxend = cblkxstart + (1 << cblkwidthexpn);
324 int cblkyend = cblkystart + (1 << cblkheightexpn);
326 cblk = &prc->cblks[cblkno];
327 /* code-block size (global) */
328 cblk->x0 = int_max(cblkxstart, prc->x0);
329 cblk->y0 = int_max(cblkystart, prc->y0);
330 cblk->x1 = int_min(cblkxend, prc->x1);
331 cblk->y1 = int_min(cblkyend, prc->y1);
338 /* tcd_dump(&tcd_image,curtileno); */
341 void tcd_free_encode(j2k_image_t * img, j2k_cp_t * cp, int curtileno)
343 int tileno, compno, resno, bandno, precno;
347 tcd_image.tw = cp->tw;
348 tcd_image.th = cp->th;
349 for (tileno = 0; tileno < 1; tileno++) {
350 /* j2k_tcp_t *tcp=&cp->tcps[curtileno]; */
351 tile = tcd_image.tiles;
352 for (compno = 0; compno < tile->numcomps; compno++) {
353 tilec = &tile->comps[compno];
354 for (resno = 0; resno < tilec->numresolutions; resno++) {
355 res = &tilec->resolutions[resno];
356 for (bandno = 0; bandno < res->numbands; bandno++) {
357 band = &res->bands[bandno];
358 for (precno = 0; precno < res->pw * res->ph; precno++) {
359 prc = &band->precincts[precno];
361 if (prc->incltree != NULL)
362 tgt_destroy(prc->incltree);
363 if (prc->imsbtree != NULL)
364 tgt_destroy(prc->imsbtree);
367 free(band->precincts);
370 free(tilec->resolutions);
374 free(tcd_image.tiles);
377 void tcd_init_encode(j2k_image_t * img, j2k_cp_t * cp, int curtileno)
379 int tileno, compno, resno, bandno, precno, cblkno;
381 for (tileno = 0; tileno < 1; tileno++) {
382 j2k_tcp_t *tcp = &cp->tcps[curtileno];
384 // int previous_x0, previous_x1, previous_y0, previous_y1;
385 /* cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
386 int p = curtileno % cp->tw;
387 int q = curtileno / cp->tw;
388 tile = tcd_image.tiles;
390 /* 4 borders of the tile rescale on the image if necessary */
391 tile->x0 = int_max(cp->tx0 + p * cp->tdx, img->x0);
392 tile->y0 = int_max(cp->ty0 + q * cp->tdy, img->y0);
393 tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, img->x1);
394 tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, img->y1);
396 tile->numcomps = img->numcomps;
397 /* tile->PPT=img->PPT; */
398 /* Modification of the RATE >> */
399 for (j = 0; j < tcp->numlayers; j++) {
400 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
402 if (j && tcp->rates[j] < tcp->rates[j - 1] + 10) {
403 tcp->rates[j] = tcp->rates[j - 1] + 20;
405 if (!j && tcp->rates[j] < 30)
410 /* << Modification of the RATE */
411 /* tile->comps=(tcd_tilecomp_t*)realloc(tile->comps,img->numcomps*sizeof(tcd_tilecomp_t)); */
412 for (compno = 0; compno < tile->numcomps; compno++) {
413 j2k_tccp_t *tccp = &tcp->tccps[compno];
414 /* int realloc_op; */
416 tilec = &tile->comps[compno];
417 /* border of each tile component (global) */
418 tilec->x0 = int_ceildiv(tile->x0, img->comps[compno].dx);
419 tilec->y0 = int_ceildiv(tile->y0, img->comps[compno].dy);
420 tilec->x1 = int_ceildiv(tile->x1, img->comps[compno].dx);
421 tilec->y1 = int_ceildiv(tile->y1, img->comps[compno].dy);
424 (int *) malloc((tilec->x1 - tilec->x0) *
425 (tilec->y1 - tilec->y0) * sizeof(int));
426 tilec->numresolutions = tccp->numresolutions;
427 /* tilec->resolutions=(tcd_resolution_t*)realloc(tilec->resolutions,tilec->numresolutions*sizeof(tcd_resolution_t)); */
428 for (resno = 0; resno < tilec->numresolutions; resno++) {
430 int levelno = tilec->numresolutions - 1 - resno;
431 int tlprcxstart, tlprcystart, brprcxend, brprcyend;
432 int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend;
433 int cbgwidthexpn, cbgheightexpn;
434 int cblkwidthexpn, cblkheightexpn;
436 res = &tilec->resolutions[resno];
437 /* border for each resolution level (global) */
438 res->x0 = int_ceildivpow2(tilec->x0, levelno);
439 res->y0 = int_ceildivpow2(tilec->y0, levelno);
440 res->x1 = int_ceildivpow2(tilec->x1, levelno);
441 res->y1 = int_ceildivpow2(tilec->y1, levelno);
443 res->numbands = resno == 0 ? 1 : 3;
444 /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */
445 if (tccp->csty & J2K_CCP_CSTY_PRT) {
446 pdx = tccp->prcw[resno];
447 pdy = tccp->prch[resno];
452 /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
453 tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx;
454 tlprcystart = int_floordivpow2(res->y0, pdy) << pdy;
455 brprcxend = int_ceildivpow2(res->x1, pdx) << pdx;
456 brprcyend = int_ceildivpow2(res->y1, pdy) << pdy;
458 res->pw = (brprcxend - tlprcxstart) >> pdx;
459 res->ph = (brprcyend - tlprcystart) >> pdy;
462 tlcbgxstart = tlprcxstart;
463 tlcbgystart = tlprcystart;
464 brcbgxend = brprcxend;
465 brcbgyend = brprcyend;
469 tlcbgxstart = int_ceildivpow2(tlprcxstart, 1);
470 tlcbgystart = int_ceildivpow2(tlprcystart, 1);
471 brcbgxend = int_ceildivpow2(brprcxend, 1);
472 brcbgyend = int_ceildivpow2(brprcyend, 1);
473 cbgwidthexpn = pdx - 1;
474 cbgheightexpn = pdy - 1;
477 cblkwidthexpn = int_min(tccp->cblkw, cbgwidthexpn);
478 cblkheightexpn = int_min(tccp->cblkh, cbgheightexpn);
480 for (bandno = 0; bandno < res->numbands; bandno++) {
484 band = &res->bands[bandno];
485 band->bandno = resno == 0 ? 0 : bandno + 1;
486 x0b = (band->bandno == 1) || (band->bandno == 3) ? 1 : 0;
487 y0b = (band->bandno == 2) || (band->bandno == 3) ? 1 : 0;
489 if (band->bandno == 0) {
491 band->x0 = int_ceildivpow2(tilec->x0, levelno);
492 band->y0 = int_ceildivpow2(tilec->y0, levelno);
493 band->x1 = int_ceildivpow2(tilec->x1, levelno);
494 band->y1 = int_ceildivpow2(tilec->y1, levelno);
497 int_ceildivpow2(tilec->x0 -
498 (1 << levelno) * x0b, levelno + 1);
500 int_ceildivpow2(tilec->y0 -
501 (1 << levelno) * y0b, levelno + 1);
503 int_ceildivpow2(tilec->x1 -
504 (1 << levelno) * x0b, levelno + 1);
506 int_ceildivpow2(tilec->y1 -
507 (1 << levelno) * y0b, levelno + 1);
510 ss = &tccp->stepsizes[resno ==
511 0 ? 0 : 3 * (resno - 1) + bandno + 1];
514 0 ? dwt_getgain_real(band->bandno) : dwt_getgain(band->bandno);
515 numbps = img->comps[compno].prec + gain;
516 band->stepsize = (1.0 + ss->mant / 2048.0) * pow(2.0, numbps - ss->expn);
517 band->numbps = ss->expn + tccp->numgbits - 1; /* WHY -1 ? */
519 for (precno = 0; precno < res->pw * res->ph; precno++) {
520 int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
522 tlcbgxstart + (precno % res->pw) * (1 << cbgwidthexpn);
524 tlcbgystart + (precno / res->pw) * (1 << cbgheightexpn);
525 int cbgxend = cbgxstart + (1 << cbgwidthexpn);
526 int cbgyend = cbgystart + (1 << cbgheightexpn);
528 prc = &band->precincts[precno];
529 /* precinct size (global) */
530 prc->x0 = int_max(cbgxstart, band->x0);
531 prc->y0 = int_max(cbgystart, band->y0);
532 prc->x1 = int_min(cbgxend, band->x1);
533 prc->y1 = int_min(cbgyend, band->y1);
536 int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn;
538 int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn;
540 int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn;
542 int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn;
543 prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;
544 prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn;
548 (tcd_cblk_t *) malloc(prc->cw * prc->ch *
551 if (prc->incltree != NULL)
552 tgt_destroy(prc->incltree);
553 if (prc->imsbtree != NULL)
554 tgt_destroy(prc->imsbtree);
556 prc->incltree = tgt_create(prc->cw, prc->ch);
557 prc->imsbtree = tgt_create(prc->cw, prc->ch);
559 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
561 tlcblkxstart + (cblkno % prc->cw) * (1 << cblkwidthexpn);
563 tlcblkystart + (cblkno / prc->cw) * (1 << cblkheightexpn);
564 int cblkxend = cblkxstart + (1 << cblkwidthexpn);
565 int cblkyend = cblkystart + (1 << cblkheightexpn);
566 cblk = &prc->cblks[cblkno];
568 /* code-block size (global) */
569 cblk->x0 = int_max(cblkxstart, prc->x0);
570 cblk->y0 = int_max(cblkystart, prc->y0);
571 cblk->x1 = int_min(cblkxend, prc->x1);
572 cblk->y1 = int_min(cblkyend, prc->y1);
580 /* tcd_dump(&tcd_image,0); */
583 void tcd_init(j2k_image_t * img, j2k_cp_t * cp)
585 int tileno, compno, resno, bandno, precno, cblkno, i, j, p, q;
586 unsigned int x0 = 0, y0 = 0, x1 = 0, y1 = 0, w, h;
589 tcd_image.tw = cp->tw;
590 tcd_image.th = cp->th;
592 (tcd_tile_t *) malloc(cp->tw * cp->th * sizeof(tcd_tile_t));
594 /*for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
595 j2k_tcp_t *tcp = &cp->tcps[tileno];
596 tcd_tile_t *tile = &tcd_image.tiles[tileno]; */
598 for (i = 0; i < cp->tileno_size; i++) {
599 j2k_tcp_t *tcp = &cp->tcps[cp->tileno[i]];
600 tcd_tile_t *tile = &tcd_image.tiles[cp->tileno[i]];
601 tileno = cp->tileno[i];
604 // int previous_x0, previous_x1, previous_y0, previous_y1;
605 /* cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
606 p = tileno % cp->tw; /* si numerotation matricielle .. */
607 q = tileno / cp->tw; /* .. coordonnees de la tile (q,p) q pour ligne et p pour colonne */
609 /* 4 borders of the tile rescale on the image if necessary */
610 tile->x0 = int_max(cp->tx0 + p * cp->tdx, img->x0);
611 tile->y0 = int_max(cp->ty0 + q * cp->tdy, img->y0);
612 tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, img->x1);
613 tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, img->y1);
615 tile->numcomps = img->numcomps;
617 (tcd_tilecomp_t *) malloc(img->numcomps * sizeof(tcd_tilecomp_t));
618 for (compno = 0; compno < tile->numcomps; compno++) {
619 j2k_tccp_t *tccp = &tcp->tccps[compno];
620 tcd_tilecomp_t *tilec = &tile->comps[compno];
621 /* border of each tile component (global) */
622 tilec->x0 = int_ceildiv(tile->x0, img->comps[compno].dx);
623 tilec->y0 = int_ceildiv(tile->y0, img->comps[compno].dy);
624 tilec->x1 = int_ceildiv(tile->x1, img->comps[compno].dx);
625 tilec->y1 = int_ceildiv(tile->y1, img->comps[compno].dy);
628 (int *) malloc((tilec->x1 - tilec->x0) *
629 (tilec->y1 - tilec->y0) * sizeof(int));
630 tilec->numresolutions = tccp->numresolutions;
632 (tcd_resolution_t *) malloc(tilec->numresolutions *
633 sizeof(tcd_resolution_t));
634 for (resno = 0; resno < tilec->numresolutions; resno++) {
636 int levelno = tilec->numresolutions - 1 - resno;
637 int tlprcxstart, tlprcystart, brprcxend, brprcyend;
638 int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend;
639 int cbgwidthexpn, cbgheightexpn;
640 int cblkwidthexpn, cblkheightexpn;
641 tcd_resolution_t *res = &tilec->resolutions[resno];
643 /* border for each resolution level (global) */
644 res->x0 = int_ceildivpow2(tilec->x0, levelno);
645 res->y0 = int_ceildivpow2(tilec->y0, levelno);
646 res->x1 = int_ceildivpow2(tilec->x1, levelno);
647 res->y1 = int_ceildivpow2(tilec->y1, levelno);
649 res->numbands = resno == 0 ? 1 : 3;
650 /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */
651 if (tccp->csty & J2K_CCP_CSTY_PRT) {
652 pdx = tccp->prcw[resno];
653 pdy = tccp->prch[resno];
658 /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
659 tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx;
660 tlprcystart = int_floordivpow2(res->y0, pdy) << pdy;
661 brprcxend = int_ceildivpow2(res->x1, pdx) << pdx;
662 brprcyend = int_ceildivpow2(res->y1, pdy) << pdy;
663 res->pw = (res->x0 == res->x1) ? 0 : ((brprcxend - tlprcxstart) >> pdx); // Mod Antonin : sizebug1
664 res->ph = (res->y0 == res->y1) ? 0 : ((brprcyend - tlprcystart) >> pdy); // Mod Antonin : sizebug1
667 tlcbgxstart = tlprcxstart;
668 tlcbgystart = tlprcystart;
669 brcbgxend = brprcxend;
670 brcbgyend = brprcyend;
674 tlcbgxstart = int_ceildivpow2(tlprcxstart, 1);
675 tlcbgystart = int_ceildivpow2(tlprcystart, 1);
676 brcbgxend = int_ceildivpow2(brprcxend, 1);
677 brcbgyend = int_ceildivpow2(brprcyend, 1);
678 cbgwidthexpn = pdx - 1;
679 cbgheightexpn = pdy - 1;
682 cblkwidthexpn = int_min(tccp->cblkw, cbgwidthexpn);
683 cblkheightexpn = int_min(tccp->cblkh, cbgheightexpn);
685 for (bandno = 0; bandno < res->numbands; bandno++) {
689 tcd_band_t *band = &res->bands[bandno];
690 band->bandno = resno == 0 ? 0 : bandno + 1;
691 x0b = (band->bandno == 1) || (band->bandno == 3) ? 1 : 0;
692 y0b = (band->bandno == 2) || (band->bandno == 3) ? 1 : 0;
694 if (band->bandno == 0) {
695 /* band border (global) */
696 band->x0 = int_ceildivpow2(tilec->x0, levelno);
697 band->y0 = int_ceildivpow2(tilec->y0, levelno);
698 band->x1 = int_ceildivpow2(tilec->x1, levelno);
699 band->y1 = int_ceildivpow2(tilec->y1, levelno);
701 /* band border (global) */
703 int_ceildivpow2(tilec->x0 -
704 (1 << levelno) * x0b, levelno + 1);
706 int_ceildivpow2(tilec->y0 -
707 (1 << levelno) * y0b, levelno + 1);
709 int_ceildivpow2(tilec->x1 -
710 (1 << levelno) * x0b, levelno + 1);
712 int_ceildivpow2(tilec->y1 -
713 (1 << levelno) * y0b, levelno + 1);
716 ss = &tccp->stepsizes[resno ==
717 0 ? 0 : 3 * (resno - 1) + bandno + 1];
720 0 ? dwt_getgain_real(band->bandno) : dwt_getgain(band->bandno);
721 numbps = img->comps[compno].prec + gain;
722 band->stepsize = (1.0 + ss->mant / 2048.0) * pow(2.0, numbps - ss->expn);
723 band->numbps = ss->expn + tccp->numgbits - 1; /* WHY -1 ? */
726 (tcd_precinct_t *) malloc(res->pw * res->ph *
727 sizeof(tcd_precinct_t));
729 for (precno = 0; precno < res->pw * res->ph; precno++) {
730 int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
732 tlcbgxstart + (precno % res->pw) * (1 << cbgwidthexpn);
734 tlcbgystart + (precno / res->pw) * (1 << cbgheightexpn);
735 int cbgxend = cbgxstart + (1 << cbgwidthexpn);
736 int cbgyend = cbgystart + (1 << cbgheightexpn);
737 tcd_precinct_t *prc = &band->precincts[precno];
738 /* precinct size (global) */
739 prc->x0 = int_max(cbgxstart, band->x0);
740 prc->y0 = int_max(cbgystart, band->y0);
741 prc->x1 = int_min(cbgxend, band->x1);
742 prc->y1 = int_min(cbgyend, band->y1);
745 int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn;
747 int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn;
749 int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn;
751 int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn;
752 prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;
753 prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn;
756 (tcd_cblk_t *) malloc(prc->cw * prc->ch *
759 prc->incltree = tgt_create(prc->cw, prc->ch);
760 prc->imsbtree = tgt_create(prc->cw, prc->ch);
762 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
764 tlcblkxstart + (cblkno % prc->cw) * (1 << cblkwidthexpn);
766 tlcblkystart + (cblkno / prc->cw) * (1 << cblkheightexpn);
767 int cblkxend = cblkxstart + (1 << cblkwidthexpn);
768 int cblkyend = cblkystart + (1 << cblkheightexpn);
769 tcd_cblk_t *cblk = &prc->cblks[cblkno];
770 /* code-block size (global) */
771 cblk->x0 = int_max(cblkxstart, prc->x0);
772 cblk->y0 = int_max(cblkystart, prc->y0);
773 cblk->x1 = int_min(cblkxend, prc->x1);
774 cblk->y1 = int_min(cblkyend, prc->y1);
781 //tcd_dump(&tcd_image,0);
784 /* Allocate place to store the data decoded = final image */
785 /* Place limited by the tile really present in the codestream */
788 for (i = 0; i < img->numcomps; i++) {
789 for (j = 0; j < cp->tileno_size; j++) {
790 tileno = cp->tileno[j];
791 x0 = j == 0 ? tcd_image.tiles[tileno].comps[i].x0 : int_min(x0,
799 j == 0 ? tcd_image.tiles[tileno].comps[i].y0 : int_min(y0,
807 j == 0 ? tcd_image.tiles[tileno].comps[i].x1 : int_max(x1,
815 j == 0 ? tcd_image.tiles[tileno].comps[i].y1 : int_max(y1,
827 img->comps[i].data = (int *) calloc(w * h, sizeof(int));
830 img->comps[i].x0 = x0;
831 img->comps[i].y0 = y0;
835 void tcd_makelayer_fixed(int layno, int final)
837 int compno, resno, bandno, precno, cblkno;
838 int value; //, matrice[tcd_tcp->numlayers][tcd_tile->comps[0].numresolutions][3];
839 int matrice[10][10][3];
842 /*matrice=(int*)malloc(tcd_tcp->numlayers*tcd_tile->comps[0].numresolutions*3*sizeof(int)); */
844 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
845 tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
846 for (i = 0; i < tcd_tcp->numlayers; i++) {
847 for (j = 0; j < tilec->numresolutions; j++) {
848 for (k = 0; k < 3; k++) {
851 matrice[i * tilec->numresolutions * 3 +
854 (float) (tcd_img->comps[compno].prec / 16.0));
857 for (resno = 0; resno < tilec->numresolutions; resno++) {
858 tcd_resolution_t *res = &tilec->resolutions[resno];
859 for (bandno = 0; bandno < res->numbands; bandno++) {
860 tcd_band_t *band = &res->bands[bandno];
861 for (precno = 0; precno < res->pw * res->ph; precno++) {
862 tcd_precinct_t *prc = &band->precincts[precno];
863 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
864 tcd_cblk_t *cblk = &prc->cblks[cblkno];
865 tcd_layer_t *layer = &cblk->layers[layno];
867 int imsb = tcd_img->comps[compno].prec - cblk->numbps; /* number of bit-plan equal to zero */
868 /* Correction of the matrix of coefficient to include the IMSB information */
871 value = matrice[layno][resno][bandno];
878 matrice[layno][resno][bandno] -
879 matrice[layno - 1][resno][bandno];
880 if (imsb >= matrice[layno - 1][resno][bandno]) {
881 value -= (imsb - matrice[layno - 1][resno][bandno]);
888 cblk->numpassesinlayers = 0;
890 n = cblk->numpassesinlayers;
891 if (cblk->numpassesinlayers == 0) {
893 n = 3 * value - 2 + cblk->numpassesinlayers;
895 n = cblk->numpassesinlayers;
897 n = 3 * value + cblk->numpassesinlayers;
899 layer->numpasses = n - cblk->numpassesinlayers;
901 if (!layer->numpasses)
904 if (cblk->numpassesinlayers == 0) {
905 layer->len = cblk->passes[n - 1].rate;
906 layer->data = cblk->data;
909 cblk->passes[n - 1].rate -
910 cblk->passes[cblk->numpassesinlayers - 1].rate;
913 cblk->passes[cblk->numpassesinlayers - 1].rate;
916 cblk->numpassesinlayers = n;
924 void tcd_rateallocate_fixed()
928 for (layno = 0; layno < tcd_tcp->numlayers; layno++) {
929 tcd_makelayer_fixed(layno, 1);
933 void tcd_makelayer(int layno, double thresh, int final)
935 int compno, resno, bandno, precno, cblkno, passno;
937 tcd_tile->distolayer[layno] = 0; //add fixed_quality
939 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
940 tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
941 for (resno = 0; resno < tilec->numresolutions; resno++) {
942 tcd_resolution_t *res = &tilec->resolutions[resno];
943 for (bandno = 0; bandno < res->numbands; bandno++) {
944 tcd_band_t *band = &res->bands[bandno];
945 for (precno = 0; precno < res->pw * res->ph; precno++) {
946 tcd_precinct_t *prc = &band->precincts[precno];
947 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
948 tcd_cblk_t *cblk = &prc->cblks[cblkno];
949 tcd_layer_t *layer = &cblk->layers[layno];
953 cblk->numpassesinlayers = 0;
955 n = cblk->numpassesinlayers;
956 for (passno = cblk->numpassesinlayers;
957 passno < cblk->totalpasses; passno++) {
960 tcd_pass_t *pass = &cblk->passes[passno];
963 dd = pass->distortiondec;
965 dr = pass->rate - cblk->passes[n - 1].rate;
966 dd = pass->distortiondec - cblk->passes[n -
975 if (dd / dr >= thresh)
978 layer->numpasses = n - cblk->numpassesinlayers;
980 if (!layer->numpasses) {
985 if (cblk->numpassesinlayers == 0) {
986 layer->len = cblk->passes[n - 1].rate;
987 layer->data = cblk->data;
988 layer->disto = cblk->passes[n - 1].distortiondec;
990 layer->len = cblk->passes[n - 1].rate -
991 cblk->passes[cblk->numpassesinlayers - 1].rate;
994 cblk->passes[cblk->numpassesinlayers - 1].rate;
996 cblk->passes[n - 1].distortiondec -
997 cblk->passes[cblk->numpassesinlayers - 1].distortiondec;
1000 tcd_tile->distolayer[layno] += layer->disto; //add fixed_quality
1003 cblk->numpassesinlayers = n;
1011 void tcd_rateallocate(unsigned char *dest, int len, info_image * info_IM)
1013 int compno, resno, bandno, precno, cblkno, passno, layno;
1015 double cumdisto[100]; //add fixed_quality
1016 const double K = 1; // 1.1; //add fixed_quality
1022 tcd_tile->nbpix = 0; //add fixed_quality
1024 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
1025 tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
1028 for (resno = 0; resno < tilec->numresolutions; resno++) {
1029 tcd_resolution_t *res = &tilec->resolutions[resno];
1030 for (bandno = 0; bandno < res->numbands; bandno++) {
1031 tcd_band_t *band = &res->bands[bandno];
1032 for (precno = 0; precno < res->pw * res->ph; precno++) {
1033 tcd_precinct_t *prc = &band->precincts[precno];
1034 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
1035 tcd_cblk_t *cblk = &prc->cblks[cblkno];
1036 for (passno = 0; passno < cblk->totalpasses; passno++) {
1037 tcd_pass_t *pass = &cblk->passes[passno];
1042 dd = pass->distortiondec;
1044 dr = pass->rate - cblk->passes[passno - 1].rate;
1045 dd = pass->distortiondec -
1046 cblk->passes[passno - 1].distortiondec;
1054 if (rdslope < min) {
1057 if (rdslope > max) {
1062 tcd_tile->nbpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0)); //add fixed_quality
1064 tilec->nbpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0)); //add fixed_quality
1070 maxSE += (((double)(1 << tcd_img->comps[compno].prec) - 1.0) * ((double)(1 << tcd_img->comps[compno].prec) -1.0)) * ((double)(tilec->nbpix));
1073 /* add antonin index */
1074 if (info_IM->index_on) {
1075 info_tile *info_TL = &info_IM->tile[tcd_tileno];
1076 info_TL->nbpix = tcd_tile->nbpix;
1077 info_TL->distotile = tcd_tile->distotile;
1079 (double *) malloc(tcd_tcp->numlayers * sizeof(double));
1083 for (layno = 0; layno < tcd_tcp->numlayers; layno++) {
1084 volatile double lo = min;
1085 volatile double hi = max;
1086 volatile int success = 0;
1087 volatile int maxlen = tcd_tcp->rates[layno] ? int_min(tcd_tcp->rates[layno], len) : len; //Mod antonin losslessbug
1088 volatile double goodthresh;
1090 double distotarget; //add fixed_quality
1092 distotarget = tcd_tile->distotile - ((K * maxSE) / pow(10, tcd_tcp->distoratio[layno] / 10)); // add fixed_quality
1094 if ((tcd_tcp->rates[layno]) || (tcd_cp->disto_alloc==0)) {
1095 for (i = 0; i < 32; i++) {
1096 volatile double thresh = (lo + hi) / 2;
1098 double distoachieved = 0; // add fixed_quality
1100 tcd_makelayer(layno, thresh, 0);
1102 if (tcd_cp->fixed_quality) { // add fixed_quality
1105 0 ? tcd_tile->distolayer[0] : cumdisto[layno - 1] +
1106 tcd_tile->distolayer[layno];
1107 if (distoachieved < distotarget) {
1114 t2_encode_packets(tcd_img, tcd_cp, tcd_tileno, tcd_tile,
1115 layno + 1, dest, maxlen, info_IM);
1116 /* fprintf(stderr, "rate alloc: len=%d, max=%d\n", l, maxlen); */
1125 goodthresh = thresh;
1133 longjmp(j2k_error, 1);
1136 if (info_IM->index_on) { /* Threshold for Marcela Index */
1137 info_IM->tile[tcd_tileno].thresh[layno] = goodthresh;
1139 tcd_makelayer(layno, goodthresh, 1);
1141 cumdisto[layno] = layno == 0 ? tcd_tile->distolayer[0] : cumdisto[layno - 1] + tcd_tile->distolayer[layno]; // add fixed_quality
1146 tcd_encode_tile_pxm(int tileno, unsigned char *dest, int len,
1147 info_image * info_IM)
1153 j2k_tcp_t *tcp = &tcd_cp->tcps[0];
1154 j2k_tccp_t *tccp = &tcp->tccps[0];
1156 tcd_tileno = tileno;
1157 tcd_tile = tcd_image.tiles;
1158 tcd_tcp = &tcd_cp->tcps[tileno];
1160 /* INDEX >> "Precinct_nb_X et Precinct_nb_Y" */
1161 if (info_IM->index_on) {
1162 tcd_tilecomp_t *tilec_idx = &tile->comps[0]; //Based on Component 0
1164 for (i = 0; i < tilec_idx->numresolutions; i++) {
1166 tcd_resolution_t *res_idx = &tilec_idx->resolutions[i];
1168 info_IM->tile[tileno].pw[i] = res_idx->pw;
1169 info_IM->tile[tileno].ph[i] = res_idx->ph;
1171 npck+=res_idx->pw * res_idx->ph;
1173 info_IM->tile[tileno].pdx[i] = tccp->prcw[i];
1174 info_IM->tile[tileno].pdy[i] = tccp->prch[i];
1177 info_IM->tile[tileno].packet = (info_packet *) calloc(info_IM->Comp * info_IM->Layer * npck, sizeof(info_packet));
1181 /*---------------TILE-------------------*/
1185 for (compno = 0; compno < tile->numcomps; compno++) {
1192 tcd_tilecomp_t *tilec = &tile->comps[compno];
1194 tcd_img->comps[compno].sgnd ? 0 : 1 << (tcd_img->comps[compno].
1196 int offset_x, offset_y;
1198 offset_x = int_ceildiv(tcd_img->x0, tcd_img->comps[compno].dx);
1199 offset_y = int_ceildiv(tcd_img->y0, tcd_img->comps[compno].dy);
1200 tw = tilec->x1 - tilec->x0;
1201 w = int_ceildiv(tcd_img->x1 - tcd_img->x0, tcd_img->comps[compno].dx);
1202 sprintf(tmp, "Compo%d", compno); /* component file */
1203 src = fopen(tmp, "rb");
1205 fprintf(stderr, "failed to open %s for reading\n", tmp);
1209 /* read the Compo file to extract data of the tile */
1211 fseek(src, (tilec->x0 - offset_x) + (tilec->y0 - offset_y) * w,
1213 k = (tilec->x0 - offset_x) + (tilec->y0 - offset_y) * w;
1214 for (j = tilec->y0; j < tilec->y1; j++) {
1215 for (i = tilec->x0; i < tilec->x1; i++) {
1216 if (tcd_tcp->tccps[compno].qmfbid == 1) {
1218 tilec->data[i - tilec->x0 + (j - tilec->y0) * tw] =
1221 } else if (tcd_tcp->tccps[compno].qmfbid == 0) {
1223 tilec->data[i - tilec->x0 + (j - tilec->y0) * tw] =
1224 (elmt - adjust) << 13;
1228 fseek(src, (tilec->x0 - offset_x) + (j + 1 - offset_y) * w - k,
1230 k = tilec->x0 - offset_x + (j + 1 - offset_y) * w;
1236 /*----------------MCT-------------------*/
1239 if (tcd_tcp->tccps[0].qmfbid == 0) {
1240 mct_encode_real(tile->comps[0].data, tile->comps[1].data,
1241 tile->comps[2].data,
1242 (tile->comps[0].x1 -
1243 tile->comps[0].x0) * (tile->comps[0].y1 -
1244 tile->comps[0].y0));
1246 mct_encode(tile->comps[0].data, tile->comps[1].data,
1247 tile->comps[2].data,
1248 (tile->comps[0].x1 -
1249 tile->comps[0].x0) * (tile->comps[0].y1 -
1250 tile->comps[0].y0));
1253 /*----------------DWT---------------------*/
1256 for (compno = 0; compno < tile->numcomps; compno++) {
1257 tcd_tilecomp_t *tilec = &tile->comps[compno];
1258 if (tcd_tcp->tccps[compno].qmfbid == 1) {
1260 } else if (tcd_tcp->tccps[compno].qmfbid == 0) {
1261 dwt_encode_real(tilec);
1265 /*------------------TIER1-----------------*/
1268 t1_encode_cblks(tile, tcd_tcp);
1270 /*-----------RATE-ALLOCATE------------------*/
1271 info_IM->index_write = 0; /* INDEX */
1273 if (tcd_cp->disto_alloc || tcd_cp->fixed_quality) // mod fixed_quality
1274 /* Normal Rate/distortion allocation */
1275 tcd_rateallocate(dest, len, info_IM);
1277 /* Fixed layer allocation */
1278 tcd_rateallocate_fixed();
1280 /*--------------TIER2------------------*/
1281 info_IM->index_write = 1; /* INDEX */
1282 l = t2_encode_packets(tcd_img, tcd_cp, tileno, tile,
1283 tcd_tcp->numlayers, dest, len, info_IM);
1284 /*---------------CLEAN-------------------*/
1286 time7 = clock() - time7;
1287 fprintf(stdout,"total: %ld.%.3ld s\n", time7 / CLOCKS_PER_SEC,
1288 (time7 % CLOCKS_PER_SEC) * 1000 / CLOCKS_PER_SEC);
1290 /* cleaning memory */
1291 for (compno = 0; compno < tile->numcomps; compno++) {
1292 tilec = &tile->comps[compno];
1300 tcd_encode_tile_pgx(int tileno, unsigned char *dest, int len,
1301 info_image * info_IM)
1307 j2k_tcp_t *tcp = &tcd_cp->tcps[0];
1308 j2k_tccp_t *tccp = &tcp->tccps[0];
1310 tcd_tileno = tileno;
1311 tcd_tile = tcd_image.tiles;
1312 tcd_tcp = &tcd_cp->tcps[tileno];
1314 /* INDEX >> "Precinct_nb_X et Precinct_nb_Y" */
1315 if (info_IM->index_on) {
1316 tcd_tilecomp_t *tilec_idx = &tile->comps[0]; //Based on Component 0
1318 for (i = 0; i < tilec_idx->numresolutions; i++) {
1320 tcd_resolution_t *res_idx = &tilec_idx->resolutions[i];
1322 info_IM->tile[tileno].pw[i] = res_idx->pw;
1323 info_IM->tile[tileno].ph[i] = res_idx->ph;
1325 npck+=res_idx->pw * res_idx->ph;
1327 info_IM->tile[tileno].pdx[i] = tccp->prcw[i];
1328 info_IM->tile[tileno].pdy[i] = tccp->prch[i];
1331 info_IM->tile[tileno].packet = (info_packet *) calloc(info_IM->Comp * info_IM->Layer * npck, sizeof(info_packet));
1334 /*---------------TILE-------------------*/
1337 for (compno = 0; compno < tile->numcomps; compno++) {
1344 tcd_tilecomp_t *tilec = &tile->comps[compno];
1346 tcd_img->comps[compno].sgnd ? 0 : 1 << (tcd_img->comps[compno].
1348 int offset_x, offset_y;
1350 offset_x = int_ceildiv(tcd_img->x0, tcd_img->comps[compno].dx);
1351 offset_y = int_ceildiv(tcd_img->y0, tcd_img->comps[compno].dy);
1352 tw = tilec->x1 - tilec->x0;
1353 w = int_ceildiv(tcd_img->x1 - tcd_img->x0, tcd_img->comps[compno].dx);
1354 sprintf(tmp, "bandtile%d", tileno / tcd_cp->tw + 1); /* bandtile file opening */
1355 src = fopen(tmp, "rb");
1357 fprintf(stderr, "failed to open %s for reading\n", tmp);
1360 /* Extract data from bandtile file limited to the current tile */
1362 while (k < tilec->x0 - offset_x) {
1364 fscanf(src, "%d", &elmt);
1367 for (j = 0; j < tilec->y1 - tilec->y0; j++) {
1368 for (i = tilec->x0; i < tilec->x1; i++) {
1369 if (tcd_tcp->tccps[compno].qmfbid == 1) {
1370 fscanf(src, "%d", &elmt);
1371 tilec->data[i - tilec->x0 + (j) * tw] = elmt - adjust;
1373 } else if (tcd_tcp->tccps[compno].qmfbid == 0) {
1374 fscanf(src, "%d", &elmt);
1375 tilec->data[i - tilec->x0 + (j) * tw] = (elmt - adjust) << 13;
1379 while (k < tilec->x0 - offset_x + (j + 1) * w) {
1381 fscanf(src, "%d", &elmt);
1387 /*----------------MCT-------------------*/
1390 if (tcd_tcp->tccps[0].qmfbid == 0) {
1391 mct_encode_real(tile->comps[0].data, tile->comps[1].data,
1392 tile->comps[2].data,
1393 (tile->comps[0].x1 -
1394 tile->comps[0].x0) * (tile->comps[0].y1 -
1395 tile->comps[0].y0));
1397 mct_encode(tile->comps[0].data, tile->comps[1].data,
1398 tile->comps[2].data,
1399 (tile->comps[0].x1 -
1400 tile->comps[0].x0) * (tile->comps[0].y1 -
1401 tile->comps[0].y0));
1405 /*----------------DWT---------------------*/
1408 for (compno = 0; compno < tile->numcomps; compno++) {
1409 tcd_tilecomp_t *tilec = &tile->comps[compno];
1410 if (tcd_tcp->tccps[compno].qmfbid == 1) {
1412 } else if (tcd_tcp->tccps[compno].qmfbid == 0) {
1413 dwt_encode_real(tilec);
1418 /*------------------TIER1-----------------*/
1421 t1_encode_cblks(tile, tcd_tcp);
1423 /*-----------RATE-ALLOCATE------------------*/
1425 info_IM->index_write = 0; /* INDEX */
1427 if (tcd_cp->disto_alloc || tcd_cp->fixed_quality) // mod fixed_quality
1429 /* Normal Rate/distortion allocation */
1431 tcd_rateallocate(dest, len, info_IM);
1434 /* Fixed layer allocation */
1436 tcd_rateallocate_fixed();
1438 /*--------------TIER2------------------*/
1439 info_IM->index_write = 1; /* INDEX */
1441 l = t2_encode_packets(tcd_img, tcd_cp, tileno, tile,
1442 tcd_tcp->numlayers, dest, len, info_IM);
1444 /*---------------CLEAN-------------------*/
1445 time = clock() - time;
1446 fprintf(stdout,"total: %ld.%.3ld s\n", time / CLOCKS_PER_SEC,
1447 (time % CLOCKS_PER_SEC) * 1000 / CLOCKS_PER_SEC);
1449 for (compno = 0; compno < tile->numcomps; compno++) {
1450 tilec = &tile->comps[compno];
1458 int tcd_decode_tile(unsigned char *src, int len, int tileno)
1466 tcd_tileno = tileno;
1467 tcd_tile = &tcd_image.tiles[tileno];
1468 tcd_tcp = &tcd_cp->tcps[tileno];
1473 fprintf(stdout, "Tile %d of %d decoded in ", tileno + 1,
1474 tcd_cp->tw * tcd_cp->th);
1476 /*--------------TIER2------------------*/
1478 l = t2_decode_packets(src, len, tcd_img, tcd_cp, tileno, tile);
1482 fprintf(stderr, "tcd_decode: incomplete bistream\n");
1485 /*------------------TIER1-----------------*/
1487 t1_decode_cblks(tile, tcd_tcp);
1489 /*----------------DWT---------------------*/
1491 for (compno = 0; compno < tile->numcomps; compno++) {
1492 tcd_tilecomp_t *tilec = &tile->comps[compno];
1493 if (tcd_cp->reduce != 0) {
1494 tcd_img->comps[compno].resno_decoded =
1495 tile->comps[compno].numresolutions - tcd_cp->reduce - 1;
1500 if (tcd_tcp->tccps[compno].qmfbid == 1) {
1502 tilec->numresolutions - 1 -
1503 tcd_img->comps[compno].resno_decoded);
1505 dwt_decode_real(tilec,
1506 tilec->numresolutions - 1 -
1507 tcd_img->comps[compno].resno_decoded);
1511 if (tile->comps[compno].numresolutions > 0)
1512 tcd_img->comps[compno].factor =
1513 tile->comps[compno].numresolutions -
1514 (tcd_img->comps[compno].resno_decoded + 1);
1517 /*----------------MCT-------------------*/
1520 if (tcd_tcp->tccps[0].qmfbid == 1) {
1521 mct_decode(tile->comps[0].data, tile->comps[1].data,
1522 tile->comps[2].data,
1523 (tile->comps[0].x1 -
1524 tile->comps[0].x0) * (tile->comps[0].y1 -
1525 tile->comps[0].y0));
1527 mct_decode_real(tile->comps[0].data, tile->comps[1].data,
1528 tile->comps[2].data,
1529 (tile->comps[0].x1 -
1530 tile->comps[0].x0) * (tile->comps[0].y1 -
1531 tile->comps[0].y0));
1535 /*---------------TILE-------------------*/
1537 for (compno = 0; compno < tile->numcomps; compno++) {
1538 tcd_tilecomp_t *tilec = &tile->comps[compno];
1539 tcd_resolution_t *res =
1540 &tilec->resolutions[tcd_img->comps[compno].resno_decoded];
1542 tcd_img->comps[compno].sgnd ? 0 : 1 << (tcd_img->comps[compno].
1545 tcd_img->comps[compno].
1546 sgnd ? -(1 << (tcd_img->comps[compno].prec - 1)) : 0;
1548 tcd_img->comps[compno].
1549 sgnd ? (1 << (tcd_img->comps[compno].prec - 1)) -
1550 1 : (1 << tcd_img->comps[compno].prec) - 1;
1552 int tw = tilec->x1 - tilec->x0;
1553 int w = tcd_img->comps[compno].w;
1556 int offset_x = int_ceildivpow2(tcd_img->comps[compno].x0,
1557 tcd_img->comps[compno].factor);
1558 int offset_y = int_ceildivpow2(tcd_img->comps[compno].y0,
1559 tcd_img->comps[compno].factor);
1561 for (j = res->y0; j < res->y1; j++) {
1562 for (i = res->x0; i < res->x1; i++) {
1565 float tmp = (tilec->data[i - res->x0 + (j - res->y0) * tw])/8192.0;
1568 if (tcd_tcp->tccps[compno].qmfbid == 1) {
1569 v = tilec->data[i - res->x0 + (j - res->y0) * tw];
1571 tmp2=((int) (floor(fabs(tmp)))) + ((int) floor(fabs(tmp*2))%2);
1572 v = ((tmp<0)?-tmp2:tmp2);
1577 tcd_img->comps[compno].data[(i - offset_x) +
1578 (j - offset_y) * w] =
1579 int_clamp(v, min, max);
1584 time = clock() - time;
1585 fprintf(stdout, "%ld.%.3ld s\n", time / CLOCKS_PER_SEC,
1586 (time % CLOCKS_PER_SEC) * 1000 / CLOCKS_PER_SEC);
1590 for (compno = 0; compno < tile->numcomps; compno++) {
1591 free(tcd_image.tiles[tileno].comps[compno].data);
1595 longjmp(j2k_error, 1);
1603 void tcd_dec_release()
1607 int tileno,compno,resno,bandno,precno;
1609 for (tileno=0;tileno<tcd_image.tw*tcd_image.th;tileno++) {
1611 tcd_tile_t tile=tcd_image.tiles[tileno];
1613 for (compno=0;compno<tile.numcomps;compno++) {
1615 tcd_tilecomp_t tilec=tile.comps[compno];
1617 for (resno=0;resno<tilec.numresolutions;resno++) {
1619 tcd_resolution_t res=tilec.resolutions[resno];
1621 for (bandno=0;bandno<res.numbands;bandno++) {
1623 tcd_band_t band=res.bands[bandno];
1625 for (precno=0;precno<res.ph*res.pw;precno++) {
1627 tcd_precinct_t prec=band.precincts[precno];
1629 if (prec.cblks!=NULL) free(prec.cblks);
1631 if (prec.imsbtree!=NULL) free(prec.imsbtree);
1633 if (prec.incltree!=NULL) free(prec.incltree);
1637 if (band.precincts!=NULL) free(band.precincts);
1643 if (tilec.resolutions!=NULL) free(tilec.resolutions);
1647 if (tile.comps!=NULL) free(tile.comps);
1651 if (tcd_image.tiles!=NULL) free(tcd_image.tiles);