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=%d, 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;
274 (int) floor((1.0 + ss->mant / 2048.0) *
275 pow(2.0, numbps - ss->expn) * 8192.0);
276 band->numbps = ss->expn + tccp->numgbits - 1; /* WHY -1 ? */
279 (tcd_precinct_t *) malloc(3 * res->pw * res->ph *
280 sizeof(tcd_precinct_t));
282 for (i = 0; i < res->pw * res->ph * 3; i++) {
283 band->precincts[i].imsbtree = NULL;
284 band->precincts[i].incltree = NULL;
287 for (precno = 0; precno < res->pw * res->ph; precno++) {
288 int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
290 tlcbgxstart + (precno % res->pw) * (1 << cbgwidthexpn);
292 tlcbgystart + (precno / res->pw) * (1 << cbgheightexpn);
293 int cbgxend = cbgxstart + (1 << cbgwidthexpn);
294 int cbgyend = cbgystart + (1 << cbgheightexpn);
295 /* tcd_precinct_t *prc=&band->precincts[precno]; */
296 prc = &band->precincts[precno];
297 /* precinct size (global) */
298 prc->x0 = int_max(cbgxstart, band->x0);
299 prc->y0 = int_max(cbgystart, band->y0);
300 prc->x1 = int_min(cbgxend, band->x1);
301 prc->y1 = int_min(cbgyend, band->y1);
304 int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn;
306 int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn;
308 int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn;
310 int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn;
311 prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;
312 prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn;
315 (tcd_cblk_t *) malloc((prc->cw * prc->ch) *
317 prc->incltree = tgt_create(prc->cw, prc->ch);
318 prc->imsbtree = tgt_create(prc->cw, prc->ch);
320 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
322 tlcblkxstart + (cblkno % prc->cw) * (1 << cblkwidthexpn);
324 tlcblkystart + (cblkno / prc->cw) * (1 << cblkheightexpn);
325 int cblkxend = cblkxstart + (1 << cblkwidthexpn);
326 int cblkyend = cblkystart + (1 << cblkheightexpn);
328 cblk = &prc->cblks[cblkno];
329 /* code-block size (global) */
330 cblk->x0 = int_max(cblkxstart, prc->x0);
331 cblk->y0 = int_max(cblkystart, prc->y0);
332 cblk->x1 = int_min(cblkxend, prc->x1);
333 cblk->y1 = int_min(cblkyend, prc->y1);
340 /* tcd_dump(&tcd_image,curtileno); */
343 void tcd_free_encode(j2k_image_t * img, j2k_cp_t * cp, int curtileno)
345 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;
518 (int) floor((1.0 + ss->mant / 2048.0) *
519 pow(2.0, numbps - ss->expn) * 8192.0);
520 band->numbps = ss->expn + tccp->numgbits - 1; /* WHY -1 ? */
522 for (precno = 0; precno < res->pw * res->ph; precno++) {
523 int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
525 tlcbgxstart + (precno % res->pw) * (1 << cbgwidthexpn);
527 tlcbgystart + (precno / res->pw) * (1 << cbgheightexpn);
528 int cbgxend = cbgxstart + (1 << cbgwidthexpn);
529 int cbgyend = cbgystart + (1 << cbgheightexpn);
531 prc = &band->precincts[precno];
532 /* precinct size (global) */
533 prc->x0 = int_max(cbgxstart, band->x0);
534 prc->y0 = int_max(cbgystart, band->y0);
535 prc->x1 = int_min(cbgxend, band->x1);
536 prc->y1 = int_min(cbgyend, band->y1);
539 int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn;
541 int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn;
543 int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn;
545 int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn;
546 prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;
547 prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn;
551 (tcd_cblk_t *) malloc(prc->cw * prc->ch *
554 if (prc->incltree != NULL)
555 tgt_destroy(prc->incltree);
556 if (prc->imsbtree != NULL)
557 tgt_destroy(prc->imsbtree);
559 prc->incltree = tgt_create(prc->cw, prc->ch);
560 prc->imsbtree = tgt_create(prc->cw, prc->ch);
562 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
564 tlcblkxstart + (cblkno % prc->cw) * (1 << cblkwidthexpn);
566 tlcblkystart + (cblkno / prc->cw) * (1 << cblkheightexpn);
567 int cblkxend = cblkxstart + (1 << cblkwidthexpn);
568 int cblkyend = cblkystart + (1 << cblkheightexpn);
569 cblk = &prc->cblks[cblkno];
571 /* code-block size (global) */
572 cblk->x0 = int_max(cblkxstart, prc->x0);
573 cblk->y0 = int_max(cblkystart, prc->y0);
574 cblk->x1 = int_min(cblkxend, prc->x1);
575 cblk->y1 = int_min(cblkyend, prc->y1);
583 /* tcd_dump(&tcd_image,0); */
586 void tcd_init(j2k_image_t * img, j2k_cp_t * cp)
588 int tileno, compno, resno, bandno, precno, cblkno, i, j;
589 unsigned int x0 = 0, y0 = 0, x1 = 0, y1 = 0, w, h, p, q;
592 tcd_image.tw = cp->tw;
593 tcd_image.th = cp->th;
595 (tcd_tile_t *) malloc(cp->tw * cp->th * sizeof(tcd_tile_t));
597 /*for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
598 j2k_tcp_t *tcp = &cp->tcps[tileno];
599 tcd_tile_t *tile = &tcd_image.tiles[tileno]; */
601 for (i = 0; i < cp->tileno_size; i++) {
602 j2k_tcp_t *tcp = &cp->tcps[cp->tileno[i]];
603 tcd_tile_t *tile = &tcd_image.tiles[cp->tileno[i]];
604 tileno = cp->tileno[i];
607 // int previous_x0, previous_x1, previous_y0, previous_y1;
608 /* cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
609 p = tileno % cp->tw; /* si numerotation matricielle .. */
610 q = tileno / cp->tw; /* .. coordonnees de la tile (q,p) q pour ligne et p pour colonne */
612 /* 4 borders of the tile rescale on the image if necessary */
613 tile->x0 = int_max(cp->tx0 + p * cp->tdx, img->x0);
614 tile->y0 = int_max(cp->ty0 + q * cp->tdy, img->y0);
615 tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, img->x1);
616 tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, img->y1);
618 tile->numcomps = img->numcomps;
620 (tcd_tilecomp_t *) malloc(img->numcomps * sizeof(tcd_tilecomp_t));
621 for (compno = 0; compno < tile->numcomps; compno++) {
622 j2k_tccp_t *tccp = &tcp->tccps[compno];
623 tcd_tilecomp_t *tilec = &tile->comps[compno];
624 /* border of each tile component (global) */
625 tilec->x0 = int_ceildiv(tile->x0, img->comps[compno].dx);
626 tilec->y0 = int_ceildiv(tile->y0, img->comps[compno].dy);
627 tilec->x1 = int_ceildiv(tile->x1, img->comps[compno].dx);
628 tilec->y1 = int_ceildiv(tile->y1, img->comps[compno].dy);
631 (int *) malloc((tilec->x1 - tilec->x0) *
632 (tilec->y1 - tilec->y0) * sizeof(int));
633 tilec->numresolutions = tccp->numresolutions;
635 (tcd_resolution_t *) malloc(tilec->numresolutions *
636 sizeof(tcd_resolution_t));
637 for (resno = 0; resno < tilec->numresolutions; resno++) {
639 int levelno = tilec->numresolutions - 1 - resno;
640 int tlprcxstart, tlprcystart, brprcxend, brprcyend;
641 int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend;
642 int cbgwidthexpn, cbgheightexpn;
643 int cblkwidthexpn, cblkheightexpn;
644 tcd_resolution_t *res = &tilec->resolutions[resno];
646 /* border for each resolution level (global) */
647 res->x0 = int_ceildivpow2(tilec->x0, levelno);
648 res->y0 = int_ceildivpow2(tilec->y0, levelno);
649 res->x1 = int_ceildivpow2(tilec->x1, levelno);
650 res->y1 = int_ceildivpow2(tilec->y1, levelno);
652 res->numbands = resno == 0 ? 1 : 3;
653 /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */
654 if (tccp->csty & J2K_CCP_CSTY_PRT) {
655 pdx = tccp->prcw[resno];
656 pdy = tccp->prch[resno];
661 /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
662 tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx;
663 tlprcystart = int_floordivpow2(res->y0, pdy) << pdy;
664 brprcxend = int_ceildivpow2(res->x1, pdx) << pdx;
665 brprcyend = int_ceildivpow2(res->y1, pdy) << pdy;
666 res->pw = (res->x0 == res->x1) ? 0 : ((brprcxend - tlprcxstart) >> pdx); // Mod Antonin : sizebug1
667 res->ph = (res->y0 == res->y1) ? 0 : ((brprcyend - tlprcystart) >> pdy); // Mod Antonin : sizebug1
670 tlcbgxstart = tlprcxstart;
671 tlcbgystart = tlprcystart;
672 brcbgxend = brprcxend;
673 brcbgyend = brprcyend;
677 tlcbgxstart = int_ceildivpow2(tlprcxstart, 1);
678 tlcbgystart = int_ceildivpow2(tlprcystart, 1);
679 brcbgxend = int_ceildivpow2(brprcxend, 1);
680 brcbgyend = int_ceildivpow2(brprcyend, 1);
681 cbgwidthexpn = pdx - 1;
682 cbgheightexpn = pdy - 1;
685 cblkwidthexpn = int_min(tccp->cblkw, cbgwidthexpn);
686 cblkheightexpn = int_min(tccp->cblkh, cbgheightexpn);
688 for (bandno = 0; bandno < res->numbands; bandno++) {
692 tcd_band_t *band = &res->bands[bandno];
693 band->bandno = resno == 0 ? 0 : bandno + 1;
694 x0b = (band->bandno == 1) || (band->bandno == 3) ? 1 : 0;
695 y0b = (band->bandno == 2) || (band->bandno == 3) ? 1 : 0;
697 if (band->bandno == 0) {
698 /* band border (global) */
699 band->x0 = int_ceildivpow2(tilec->x0, levelno);
700 band->y0 = int_ceildivpow2(tilec->y0, levelno);
701 band->x1 = int_ceildivpow2(tilec->x1, levelno);
702 band->y1 = int_ceildivpow2(tilec->y1, levelno);
704 /* band border (global) */
706 int_ceildivpow2(tilec->x0 -
707 (1 << levelno) * x0b, levelno + 1);
709 int_ceildivpow2(tilec->y0 -
710 (1 << levelno) * y0b, levelno + 1);
712 int_ceildivpow2(tilec->x1 -
713 (1 << levelno) * x0b, levelno + 1);
715 int_ceildivpow2(tilec->y1 -
716 (1 << levelno) * y0b, levelno + 1);
719 ss = &tccp->stepsizes[resno ==
720 0 ? 0 : 3 * (resno - 1) + bandno + 1];
723 0 ? dwt_getgain_real(band->bandno) : dwt_getgain(band->bandno);
724 numbps = img->comps[compno].prec + gain;
726 (int) floor((1.0 + ss->mant / 2048.0) *
727 pow(2.0, numbps - ss->expn) * 8192.0);
728 band->numbps = ss->expn + tccp->numgbits - 1; /* WHY -1 ? */
731 (tcd_precinct_t *) malloc(res->pw * res->ph *
732 sizeof(tcd_precinct_t));
734 for (precno = 0; precno < res->pw * res->ph; precno++) {
735 int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
737 tlcbgxstart + (precno % res->pw) * (1 << cbgwidthexpn);
739 tlcbgystart + (precno / res->pw) * (1 << cbgheightexpn);
740 int cbgxend = cbgxstart + (1 << cbgwidthexpn);
741 int cbgyend = cbgystart + (1 << cbgheightexpn);
742 tcd_precinct_t *prc = &band->precincts[precno];
743 /* precinct size (global) */
744 prc->x0 = int_max(cbgxstart, band->x0);
745 prc->y0 = int_max(cbgystart, band->y0);
746 prc->x1 = int_min(cbgxend, band->x1);
747 prc->y1 = int_min(cbgyend, band->y1);
750 int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn;
752 int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn;
754 int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn;
756 int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn;
757 prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;
758 prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn;
761 (tcd_cblk_t *) malloc(prc->cw * prc->ch *
764 prc->incltree = tgt_create(prc->cw, prc->ch);
765 prc->imsbtree = tgt_create(prc->cw, prc->ch);
767 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
769 tlcblkxstart + (cblkno % prc->cw) * (1 << cblkwidthexpn);
771 tlcblkystart + (cblkno / prc->cw) * (1 << cblkheightexpn);
772 int cblkxend = cblkxstart + (1 << cblkwidthexpn);
773 int cblkyend = cblkystart + (1 << cblkheightexpn);
774 tcd_cblk_t *cblk = &prc->cblks[cblkno];
775 /* code-block size (global) */
776 cblk->x0 = int_max(cblkxstart, prc->x0);
777 cblk->y0 = int_max(cblkystart, prc->y0);
778 cblk->x1 = int_min(cblkxend, prc->x1);
779 cblk->y1 = int_min(cblkyend, prc->y1);
783 cblk->lastbp = 0; // Add Antonin : quantizbug1
790 //tcd_dump(&tcd_image,0);
793 /* Allocate place to store the data decoded = final image */
794 /* Place limited by the tile really present in the codestream */
797 for (i = 0; i < img->numcomps; i++) {
798 for (j = 0; j < cp->tileno_size; j++) {
799 tileno = cp->tileno[j];
800 x0 = j == 0 ? tcd_image.tiles[tileno].comps[i].x0 : int_min(x0,
807 j == 0 ? tcd_image.tiles[tileno].comps[i].y0 : int_min(y0,
814 j == 0 ? tcd_image.tiles[tileno].comps[i].x1 : int_max(x1,
821 j == 0 ? tcd_image.tiles[tileno].comps[i].y1 : int_max(y1,
828 //w = int_ceildiv(x1 - x0, img->comps[i].dx);
829 //h = int_ceildiv(y1 - y0, img->comps[i].dy);
834 img->comps[i].data = (int *) calloc(w * h, sizeof(int));
837 img->comps[i].x0 = x0;
838 img->comps[i].y0 = y0;
842 void tcd_makelayer_fixed(int layno, int final)
844 int compno, resno, bandno, precno, cblkno;
845 int value; //, matrice[tcd_tcp->numlayers][tcd_tile->comps[0].numresolutions][3];
846 int matrice[10][10][3];
849 /*matrice=(int*)malloc(tcd_tcp->numlayers*tcd_tile->comps[0].numresolutions*3*sizeof(int)); */
851 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
852 tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
853 for (i = 0; i < tcd_tcp->numlayers; i++) {
854 for (j = 0; j < tilec->numresolutions; j++) {
855 for (k = 0; k < 3; k++) {
858 matrice[i * tilec->numresolutions * 3 +
861 (float) (tcd_img->comps[compno].prec / 16.0));
864 for (resno = 0; resno < tilec->numresolutions; resno++) {
865 tcd_resolution_t *res = &tilec->resolutions[resno];
866 for (bandno = 0; bandno < res->numbands; bandno++) {
867 tcd_band_t *band = &res->bands[bandno];
868 for (precno = 0; precno < res->pw * res->ph; precno++) {
869 tcd_precinct_t *prc = &band->precincts[precno];
870 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
871 tcd_cblk_t *cblk = &prc->cblks[cblkno];
872 tcd_layer_t *layer = &cblk->layers[layno];
874 int imsb = tcd_img->comps[compno].prec - cblk->numbps; /* number of bit-plan equal to zero */
875 /* Correction of the matrix of coefficient to include the IMSB information */
878 value = matrice[layno][resno][bandno];
885 matrice[layno][resno][bandno] -
886 matrice[layno - 1][resno][bandno];
887 if (imsb >= matrice[layno - 1][resno][bandno]) {
888 value -= (imsb - matrice[layno - 1][resno][bandno]);
895 cblk->numpassesinlayers = 0;
897 n = cblk->numpassesinlayers;
898 if (cblk->numpassesinlayers == 0) {
900 n = 3 * value - 2 + cblk->numpassesinlayers;
902 n = cblk->numpassesinlayers;
904 n = 3 * value + cblk->numpassesinlayers;
906 layer->numpasses = n - cblk->numpassesinlayers;
908 if (!layer->numpasses)
911 if (cblk->numpassesinlayers == 0) {
912 layer->len = cblk->passes[n - 1].rate;
913 layer->data = cblk->data;
916 cblk->passes[n - 1].rate -
917 cblk->passes[cblk->numpassesinlayers - 1].rate;
920 cblk->passes[cblk->numpassesinlayers - 1].rate;
923 cblk->numpassesinlayers = n;
931 void tcd_rateallocate_fixed()
935 for (layno = 0; layno < tcd_tcp->numlayers; layno++) {
936 tcd_makelayer_fixed(layno, 1);
940 void tcd_makelayer(int layno, double thresh, int final)
942 int compno, resno, bandno, precno, cblkno, passno;
944 tcd_tile->distolayer[layno] = 0; //add fixed_quality
946 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
947 tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
948 for (resno = 0; resno < tilec->numresolutions; resno++) {
949 tcd_resolution_t *res = &tilec->resolutions[resno];
950 for (bandno = 0; bandno < res->numbands; bandno++) {
951 tcd_band_t *band = &res->bands[bandno];
952 for (precno = 0; precno < res->pw * res->ph; precno++) {
953 tcd_precinct_t *prc = &band->precincts[precno];
954 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
955 tcd_cblk_t *cblk = &prc->cblks[cblkno];
956 tcd_layer_t *layer = &cblk->layers[layno];
960 cblk->numpassesinlayers = 0;
962 n = cblk->numpassesinlayers;
963 for (passno = cblk->numpassesinlayers;
964 passno < cblk->totalpasses; passno++) {
967 tcd_pass_t *pass = &cblk->passes[passno];
970 dd = pass->distortiondec;
972 dr = pass->rate - cblk->passes[n - 1].rate;
973 dd = pass->distortiondec - cblk->passes[n -
982 if (dd / dr >= thresh)
985 layer->numpasses = n - cblk->numpassesinlayers;
987 if (!layer->numpasses) {
992 if (cblk->numpassesinlayers == 0) {
993 layer->len = cblk->passes[n - 1].rate;
994 layer->data = cblk->data;
995 layer->disto = cblk->passes[n - 1].distortiondec;
997 layer->len = cblk->passes[n - 1].rate -
998 cblk->passes[cblk->numpassesinlayers - 1].rate;
1001 cblk->passes[cblk->numpassesinlayers - 1].rate;
1003 cblk->passes[n - 1].distortiondec -
1004 cblk->passes[cblk->numpassesinlayers - 1].distortiondec;
1007 tcd_tile->distolayer[layno] += layer->disto; //add fixed_quality
1010 cblk->numpassesinlayers = n;
1018 void tcd_rateallocate(unsigned char *dest, int len, info_image * info_IM)
1020 int compno, resno, bandno, precno, cblkno, passno, layno;
1022 double cumdisto[100]; //add fixed_quality
1023 const double K = 1; // 1.1; //add fixed_quality
1029 tcd_tile->nbpix = 0; //add fixed_quality
1031 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
1032 tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
1035 for (resno = 0; resno < tilec->numresolutions; resno++) {
1036 tcd_resolution_t *res = &tilec->resolutions[resno];
1037 for (bandno = 0; bandno < res->numbands; bandno++) {
1038 tcd_band_t *band = &res->bands[bandno];
1039 for (precno = 0; precno < res->pw * res->ph; precno++) {
1040 tcd_precinct_t *prc = &band->precincts[precno];
1041 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
1042 tcd_cblk_t *cblk = &prc->cblks[cblkno];
1043 for (passno = 0; passno < cblk->totalpasses; passno++) {
1044 tcd_pass_t *pass = &cblk->passes[passno];
1049 dd = pass->distortiondec;
1051 dr = pass->rate - cblk->passes[passno - 1].rate;
1052 dd = pass->distortiondec -
1053 cblk->passes[passno - 1].distortiondec;
1061 if (rdslope < min) {
1064 if (rdslope > max) {
1069 tcd_tile->nbpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0)); //add fixed_quality
1071 tilec->nbpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0)); //add fixed_quality
1079 (double) (((1 << tcd_img->comps[compno].prec) -
1080 1) * ((1 << tcd_img->comps[compno].prec) -
1081 1)) * (tilec->nbpix);
1084 /* add antonin index */
1085 if (info_IM->index_on) {
1086 info_tile *info_TL = &info_IM->tile[tcd_tileno];
1087 info_TL->nbpix = tcd_tile->nbpix;
1088 info_TL->distotile = tcd_tile->distotile;
1090 (double *) malloc(tcd_tcp->numlayers * sizeof(double));
1094 for (layno = 0; layno < tcd_tcp->numlayers; layno++) {
1095 volatile double lo = min;
1096 volatile double hi = max;
1097 volatile int success = 0;
1098 volatile int maxlen = tcd_tcp->rates[layno] ? int_min(tcd_tcp->rates[layno], len) : len; //Mod antonin losslessbug
1099 volatile double goodthresh;
1101 double distotarget; //add fixed_quality
1103 distotarget = tcd_tile->distotile - ((K * maxSE) / pow(10, tcd_tcp->distoratio[layno] / 10)); // add fixed_quality
1105 if (tcd_tcp->rates[layno]) {
1106 for (i = 0; i < 32; i++) {
1107 volatile double thresh = (lo + hi) / 2;
1109 double distoachieved = 0; // add fixed_quality
1111 tcd_makelayer(layno, thresh, 0);
1113 if (tcd_cp->fixed_quality) { // add fixed_quality
1116 0 ? tcd_tile->distolayer[0] : cumdisto[layno - 1] +
1117 tcd_tile->distolayer[layno];
1118 if (distoachieved < distotarget) {
1125 t2_encode_packets(tcd_img, tcd_cp, tcd_tileno, tcd_tile,
1126 layno + 1, dest, maxlen, info_IM);
1127 /* fprintf(stderr, "rate alloc: len=%d, max=%d\n", l, maxlen); */
1136 goodthresh = thresh;
1144 longjmp(j2k_error, 1);
1147 if (info_IM->index_on) { /* Threshold for Marcela Index */
1148 info_IM->tile[tcd_tileno].thresh[layno] = goodthresh;
1150 tcd_makelayer(layno, goodthresh, 1);
1152 cumdisto[layno] = layno == 0 ? tcd_tile->distolayer[0] : cumdisto[layno - 1] + tcd_tile->distolayer[layno]; // add fixed_quality
1157 tcd_encode_tile_pxm(int tileno, unsigned char *dest, int len,
1158 info_image * info_IM)
1164 j2k_tcp_t *tcp = &tcd_cp->tcps[0];
1165 j2k_tccp_t *tccp = &tcp->tccps[0];
1167 tcd_tileno = tileno;
1168 tcd_tile = tcd_image.tiles;
1169 tcd_tcp = &tcd_cp->tcps[tileno];
1171 /* INDEX >> "Precinct_nb_X et Precinct_nb_Y" */
1172 if (info_IM->index_on) {
1173 tcd_tilecomp_t *tilec_idx = &tile->comps[0]; //Based on Component 0
1175 for (i = 0; i < tilec_idx->numresolutions; i++) {
1177 tcd_resolution_t *res_idx = &tilec_idx->resolutions[i];
1179 info_IM->tile[tileno].pw[i] = res_idx->pw;
1180 info_IM->tile[tileno].ph[i] = res_idx->ph;
1182 info_IM->tile[tileno].pdx[i] = tccp->prcw[i];
1183 info_IM->tile[tileno].pdy[i] = tccp->prch[i];
1189 /*---------------TILE-------------------*/
1193 for (compno = 0; compno < tile->numcomps; compno++) {
1200 tcd_tilecomp_t *tilec = &tile->comps[compno];
1202 tcd_img->comps[compno].sgnd ? 0 : 1 << (tcd_img->comps[compno].
1204 int offset_x, offset_y;
1206 offset_x = int_ceildiv(tcd_img->x0, tcd_img->comps[compno].dx);
1207 offset_y = int_ceildiv(tcd_img->y0, tcd_img->comps[compno].dy);
1208 tw = tilec->x1 - tilec->x0;
1209 w = int_ceildiv(tcd_img->x1 - tcd_img->x0, tcd_img->comps[compno].dx);
1210 sprintf(tmp, "Compo%d", compno); /* component file */
1211 src = fopen(tmp, "rb");
1213 fprintf(stderr, "failed to open %s for reading\n", tmp);
1217 /* read the Compo file to extract data of the tile */
1219 fseek(src, (tilec->x0 - offset_x) + (tilec->y0 - offset_y) * w,
1221 k = (tilec->x0 - offset_x) + (tilec->y0 - offset_y) * w;
1222 for (j = tilec->y0; j < tilec->y1; j++) {
1223 for (i = tilec->x0; i < tilec->x1; i++) {
1224 if (tcd_tcp->tccps[compno].qmfbid == 1) {
1226 tilec->data[i - tilec->x0 + (j - tilec->y0) * tw] =
1229 } else if (tcd_tcp->tccps[compno].qmfbid == 0) {
1231 tilec->data[i - tilec->x0 + (j - tilec->y0) * tw] =
1232 (elmt - adjust) << 13;
1236 fseek(src, (tilec->x0 - offset_x) + (j + 1 - offset_y) * w - k,
1238 k = tilec->x0 - offset_x + (j + 1 - offset_y) * w;
1244 /*----------------MCT-------------------*/
1247 if (tcd_tcp->tccps[0].qmfbid == 0) {
1248 mct_encode_real(tile->comps[0].data, tile->comps[1].data,
1249 tile->comps[2].data,
1250 (tile->comps[0].x1 -
1251 tile->comps[0].x0) * (tile->comps[0].y1 -
1252 tile->comps[0].y0));
1254 mct_encode(tile->comps[0].data, tile->comps[1].data,
1255 tile->comps[2].data,
1256 (tile->comps[0].x1 -
1257 tile->comps[0].x0) * (tile->comps[0].y1 -
1258 tile->comps[0].y0));
1261 /*----------------DWT---------------------*/
1263 /* time3=clock(); */
1264 for (compno = 0; compno < tile->numcomps; compno++) {
1265 tcd_tilecomp_t *tilec = &tile->comps[compno];
1266 if (tcd_tcp->tccps[compno].qmfbid == 1) {
1267 dwt_encode(tilec->data, tilec->x1 - tilec->x0,
1268 tilec->y1 - tilec->y0, tilec, tilec->numresolutions - 1);
1269 } else if (tcd_tcp->tccps[compno].qmfbid == 0) {
1270 dwt_encode_real(tilec->data, tilec->x1 - tilec->x0,
1271 tilec->y1 - tilec->y0, tilec,
1272 tilec->numresolutions - 1);
1275 /*------------------TIER1-----------------*/
1278 t1_encode_cblks(tile, tcd_tcp);
1280 /*-----------RATE-ALLOCATE------------------*/
1281 info_IM->index_write = 0; /* INDEX */
1283 if (tcd_cp->disto_alloc || tcd_cp->fixed_quality) // mod fixed_quality
1284 /* Normal Rate/distortion allocation */
1285 tcd_rateallocate(dest, len, info_IM);
1287 /* Fixed layer allocation */
1288 tcd_rateallocate_fixed();
1290 /*--------------TIER2------------------*/
1291 info_IM->index_write = 1; /* INDEX */
1292 l = t2_encode_packets(tcd_img, tcd_cp, tileno, tile,
1293 tcd_tcp->numlayers, dest, len, info_IM);
1294 /*---------------CLEAN-------------------*/
1296 time7 = clock() - time7;
1297 fprintf(stdout,"total: %ld.%.3ld s\n", time7 / CLOCKS_PER_SEC,
1298 (time7 % CLOCKS_PER_SEC) * 1000 / CLOCKS_PER_SEC);
1300 /* cleaning memory */
1301 for (compno = 0; compno < tile->numcomps; compno++) {
1302 tilec = &tile->comps[compno];
1310 tcd_encode_tile_pgx(int tileno, unsigned char *dest, int len,
1311 info_image * info_IM)
1317 j2k_tcp_t *tcp = &tcd_cp->tcps[0];
1318 j2k_tccp_t *tccp = &tcp->tccps[0];
1320 tcd_tileno = tileno;
1321 tcd_tile = tcd_image.tiles;
1322 tcd_tcp = &tcd_cp->tcps[tileno];
1324 /* INDEX >> "Precinct_nb_X et Precinct_nb_Y" */
1326 if (info_IM->index_on) {
1328 tcd_tilecomp_t *tilec_idx = &tile->comps[0]; //Based on Component 0
1330 for (i = 0; i < tilec_idx->numresolutions; i++) {
1332 tcd_resolution_t *res_idx = &tilec_idx->resolutions[i];
1336 info_IM->tile[tileno].pw[i] = res_idx->pw;
1338 info_IM->tile[tileno].ph[i] = res_idx->ph;
1342 info_IM->tile[tileno].pdx[i] = tccp->prcw[i];
1344 info_IM->tile[tileno].pdy[i] = tccp->prch[i];
1351 /*---------------TILE-------------------*/
1354 for (compno = 0; compno < tile->numcomps; compno++) {
1361 tcd_tilecomp_t *tilec = &tile->comps[compno];
1363 tcd_img->comps[compno].sgnd ? 0 : 1 << (tcd_img->comps[compno].
1365 int offset_x, offset_y;
1367 offset_x = int_ceildiv(tcd_img->x0, tcd_img->comps[compno].dx);
1368 offset_y = int_ceildiv(tcd_img->y0, tcd_img->comps[compno].dy);
1369 tw = tilec->x1 - tilec->x0;
1370 w = int_ceildiv(tcd_img->x1 - tcd_img->x0, tcd_img->comps[compno].dx);
1371 sprintf(tmp, "bandtile%d", tileno / tcd_cp->tw + 1); /* bandtile file opening */
1372 src = fopen(tmp, "rb");
1374 fprintf(stderr, "failed to open %s for reading\n", tmp);
1377 /* Extract data from bandtile file limited to the current tile */
1379 while (k < tilec->x0 - offset_x) {
1381 fscanf(src, "%d", &elmt);
1384 for (j = 0; j < tilec->y1 - tilec->y0; j++) {
1385 for (i = tilec->x0; i < tilec->x1; i++) {
1386 if (tcd_tcp->tccps[compno].qmfbid == 1) {
1387 fscanf(src, "%d", &elmt);
1388 tilec->data[i - tilec->x0 + (j) * tw] = elmt - adjust;
1390 } else if (tcd_tcp->tccps[compno].qmfbid == 0) {
1391 fscanf(src, "%d", &elmt);
1392 tilec->data[i - tilec->x0 + (j) * tw] = (elmt - adjust) << 13;
1396 while (k < tilec->x0 - offset_x + (j + 1) * w) {
1398 fscanf(src, "%d", &elmt);
1404 /*----------------MCT-------------------*/
1407 if (tcd_tcp->tccps[0].qmfbid == 0) {
1408 mct_encode_real(tile->comps[0].data, tile->comps[1].data,
1409 tile->comps[2].data,
1410 (tile->comps[0].x1 -
1411 tile->comps[0].x0) * (tile->comps[0].y1 -
1412 tile->comps[0].y0));
1414 mct_encode(tile->comps[0].data, tile->comps[1].data,
1415 tile->comps[2].data,
1416 (tile->comps[0].x1 -
1417 tile->comps[0].x0) * (tile->comps[0].y1 -
1418 tile->comps[0].y0));
1422 /*----------------DWT---------------------*/
1424 for (compno = 0; compno < tile->numcomps; compno++) {
1425 tcd_tilecomp_t *tilec = &tile->comps[compno];
1426 if (tcd_tcp->tccps[compno].qmfbid == 1) {
1427 dwt_encode(tilec->data, tilec->x1 - tilec->x0,
1428 tilec->y1 - tilec->y0, tilec, tilec->numresolutions - 1);
1429 } else if (tcd_tcp->tccps[compno].qmfbid == 0) {
1430 dwt_encode_real(tilec->data, tilec->x1 - tilec->x0,
1431 tilec->y1 - tilec->y0, tilec,
1432 tilec->numresolutions - 1);
1436 /*------------------TIER1-----------------*/
1439 t1_encode_cblks(tile, tcd_tcp);
1441 /*-----------RATE-ALLOCATE------------------*/
1443 info_IM->index_write = 0; /* INDEX */
1445 if (tcd_cp->disto_alloc || tcd_cp->fixed_quality) // mod fixed_quality
1447 /* Normal Rate/distortion allocation */
1449 tcd_rateallocate(dest, len, info_IM);
1452 /* Fixed layer allocation */
1454 tcd_rateallocate_fixed();
1456 /*--------------TIER2------------------*/
1457 info_IM->index_write = 1; /* INDEX */
1459 l = t2_encode_packets(tcd_img, tcd_cp, tileno, tile,
1460 tcd_tcp->numlayers, dest, len, info_IM);
1462 /*---------------CLEAN-------------------*/
1463 time = clock() - time;
1464 fprintf(stdout,"total: %ld.%.3ld s\n", time / CLOCKS_PER_SEC,
1465 (time % CLOCKS_PER_SEC) * 1000 / CLOCKS_PER_SEC);
1467 for (compno = 0; compno < tile->numcomps; compno++) {
1468 tilec = &tile->comps[compno];
1476 int tcd_decode_tile(unsigned char *src, int len, int tileno)
1484 tcd_tileno = tileno;
1485 tcd_tile = &tcd_image.tiles[tileno];
1486 tcd_tcp = &tcd_cp->tcps[tileno];
1491 fprintf(stdout, "tile decoding time %d/%d: ", tileno + 1,
1492 tcd_cp->tw * tcd_cp->th);
1494 /*--------------TIER2------------------*/
1496 l = t2_decode_packets(src, len, tcd_img, tcd_cp, tileno, tile);
1500 fprintf(stderr, "tcd_decode: incomplete bistream\n");
1503 /*------------------TIER1-----------------*/
1505 t1_decode_cblks(tile, tcd_tcp);
1507 /*----------------DWT---------------------*/
1509 for (compno = 0; compno < tile->numcomps; compno++) {
1510 tcd_tilecomp_t *tilec = &tile->comps[compno];
1511 if (tcd_cp->reduce_on == 1) {
1512 tcd_img->comps[compno].resno_decoded =
1513 tile->comps[compno].numresolutions - tcd_cp->reduce_value - 1;
1517 if (tcd_tcp->tccps[compno].qmfbid == 1) {
1518 dwt_decode(tilec->data, tilec->x1 - tilec->x0,
1519 tilec->y1 - tilec->y0, tilec,
1520 tilec->numresolutions - 1,
1521 tilec->numresolutions - 1 -
1522 tcd_img->comps[compno].resno_decoded);
1524 dwt_decode_real(tilec->data, tilec->x1 - tilec->x0,
1525 tilec->y1 - tilec->y0, tilec,
1526 tilec->numresolutions - 1,
1527 tilec->numresolutions - 1 -
1528 tcd_img->comps[compno].resno_decoded);
1531 if (tile->comps[compno].numresolutions > 0)
1532 tcd_img->comps[compno].factor =
1533 tile->comps[compno].numresolutions -
1534 (tcd_img->comps[compno].resno_decoded + 1);
1537 /*----------------MCT-------------------*/
1540 if (tcd_tcp->tccps[0].qmfbid == 1) {
1541 mct_decode(tile->comps[0].data, tile->comps[1].data,
1542 tile->comps[2].data,
1543 (tile->comps[0].x1 -
1544 tile->comps[0].x0) * (tile->comps[0].y1 -
1545 tile->comps[0].y0));
1547 mct_decode_real(tile->comps[0].data, tile->comps[1].data,
1548 tile->comps[2].data,
1549 (tile->comps[0].x1 -
1550 tile->comps[0].x0) * (tile->comps[0].y1 -
1551 tile->comps[0].y0));
1555 /*---------------TILE-------------------*/
1557 for (compno = 0; compno < tile->numcomps; compno++) {
1558 tcd_tilecomp_t *tilec = &tile->comps[compno];
1559 tcd_resolution_t *res =
1560 &tilec->resolutions[tcd_img->comps[compno].resno_decoded];
1562 tcd_img->comps[compno].sgnd ? 0 : 1 << (tcd_img->comps[compno].
1565 tcd_img->comps[compno].
1566 sgnd ? -(1 << (tcd_img->comps[compno].prec - 1)) : 0;
1568 tcd_img->comps[compno].
1569 sgnd ? (1 << (tcd_img->comps[compno].prec - 1)) -
1570 1 : (1 << tcd_img->comps[compno].prec) - 1;
1572 int tw = tilec->x1 - tilec->x0;
1573 int w = tcd_img->comps[compno].w;
1576 int offset_x = int_ceildivpow2(tcd_img->comps[compno].x0,
1577 tcd_img->comps[compno].factor);
1578 int offset_y = int_ceildivpow2(tcd_img->comps[compno].y0,
1579 tcd_img->comps[compno].factor);
1581 for (j = res->y0; j < res->y1; j++) {
1582 for (i = res->x0; i < res->x1; i++) {
1587 (double) tilec->data[i - res->x0 + (j - res->y0) * tw];
1588 if (tcd_tcp->tccps[compno].qmfbid == 1) {
1592 //v = (int) tmp >> 13;
1594 //Mod antonin : multbug1
1596 (int) ((fabs(tmp / 8192.0) >=
1597 floor(fabs(tmp / 8192.0)) +
1598 0.5) ? fabs(tmp / 8192.0) + 1.0 : fabs(tmp / 8192.0));
1600 v = (tmp < 0) ? -v : v;
1606 tcd_img->comps[compno].data[(i - offset_x) +
1607 (j - offset_y) * w] =
1608 int_clamp(v, min, max);
1613 time = clock() - time;
1614 fprintf(stdout, "total: %ld.%.3ld s\n", time / CLOCKS_PER_SEC,
1615 (time % CLOCKS_PER_SEC) * 1000 / CLOCKS_PER_SEC);
1619 for (compno = 0; compno < tile->numcomps; compno++) {
1620 free(tcd_image.tiles[tileno].comps[compno].data);
1624 longjmp(j2k_error, 1);
1632 void tcd_dec_release()
1636 int tileno,compno,resno,bandno,precno;
1638 for (tileno=0;tileno<tcd_image.tw*tcd_image.th;tileno++) {
1640 tcd_tile_t tile=tcd_image.tiles[tileno];
1642 for (compno=0;compno<tile.numcomps;compno++) {
1644 tcd_tilecomp_t tilec=tile.comps[compno];
1646 for (resno=0;resno<tilec.numresolutions;resno++) {
1648 tcd_resolution_t res=tilec.resolutions[resno];
1650 for (bandno=0;bandno<res.numbands;bandno++) {
1652 tcd_band_t band=res.bands[bandno];
1654 for (precno=0;precno<res.ph*res.pw;precno++) {
1656 tcd_precinct_t prec=band.precincts[precno];
1658 if (prec.cblks!=NULL) free(prec.cblks);
1660 if (prec.imsbtree!=NULL) free(prec.imsbtree);
1662 if (prec.incltree!=NULL) free(prec.incltree);
1666 if (band.precincts!=NULL) free(band.precincts);
1672 if (tilec.resolutions!=NULL) free(tilec.resolutions);
1676 if (tile.comps!=NULL) free(tile.comps);
1680 if (tcd_image.tiles!=NULL) free(tcd_image.tiles);