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;
346 tcd_image.tw = cp->tw;
347 tcd_image.th = cp->th;
348 for (tileno = 0; tileno < 1; tileno++) {
349 /* j2k_tcp_t *tcp=&cp->tcps[curtileno]; */
350 tile = tcd_image.tiles;
351 for (compno = 0; compno < tile->numcomps; compno++) {
352 tilec = &tile->comps[compno];
353 for (resno = 0; resno < tilec->numresolutions; resno++) {
354 res = &tilec->resolutions[resno];
355 for (bandno = 0; bandno < res->numbands; bandno++) {
356 band = &res->bands[bandno];
357 for (precno = 0; precno < res->pw * res->ph; precno++) {
358 prc = &band->precincts[precno];
360 if (prc->incltree != NULL)
361 tgt_destroy(prc->incltree);
362 if (prc->imsbtree != NULL)
363 tgt_destroy(prc->imsbtree);
366 free(band->precincts);
369 free(tilec->resolutions);
373 free(tcd_image.tiles);
376 void tcd_init_encode(j2k_image_t * img, j2k_cp_t * cp, int curtileno)
378 int tileno, compno, resno, bandno, precno, cblkno;
380 for (tileno = 0; tileno < 1; tileno++) {
381 j2k_tcp_t *tcp = &cp->tcps[curtileno];
383 // int previous_x0, previous_x1, previous_y0, previous_y1;
384 /* cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
385 int p = curtileno % cp->tw;
386 int q = curtileno / cp->tw;
387 tile = tcd_image.tiles;
389 /* 4 borders of the tile rescale on the image if necessary */
390 tile->x0 = int_max(cp->tx0 + p * cp->tdx, img->x0);
391 tile->y0 = int_max(cp->ty0 + q * cp->tdy, img->y0);
392 tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, img->x1);
393 tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, img->y1);
395 tile->numcomps = img->numcomps;
396 /* tile->PPT=img->PPT; */
397 /* Modification of the RATE >> */
398 for (j = 0; j < tcp->numlayers; j++) {
399 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
401 if (j && tcp->rates[j] < tcp->rates[j - 1] + 10) {
402 tcp->rates[j] = tcp->rates[j - 1] + 20;
404 if (!j && tcp->rates[j] < 30)
409 /* << Modification of the RATE */
410 /* tile->comps=(tcd_tilecomp_t*)realloc(tile->comps,img->numcomps*sizeof(tcd_tilecomp_t)); */
411 for (compno = 0; compno < tile->numcomps; compno++) {
412 j2k_tccp_t *tccp = &tcp->tccps[compno];
413 /* int realloc_op; */
415 tilec = &tile->comps[compno];
416 /* border of each tile component (global) */
417 tilec->x0 = int_ceildiv(tile->x0, img->comps[compno].dx);
418 tilec->y0 = int_ceildiv(tile->y0, img->comps[compno].dy);
419 tilec->x1 = int_ceildiv(tile->x1, img->comps[compno].dx);
420 tilec->y1 = int_ceildiv(tile->y1, img->comps[compno].dy);
423 (int *) malloc((tilec->x1 - tilec->x0) *
424 (tilec->y1 - tilec->y0) * sizeof(int));
425 tilec->numresolutions = tccp->numresolutions;
426 /* tilec->resolutions=(tcd_resolution_t*)realloc(tilec->resolutions,tilec->numresolutions*sizeof(tcd_resolution_t)); */
427 for (resno = 0; resno < tilec->numresolutions; resno++) {
429 int levelno = tilec->numresolutions - 1 - resno;
430 int tlprcxstart, tlprcystart, brprcxend, brprcyend;
431 int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend;
432 int cbgwidthexpn, cbgheightexpn;
433 int cblkwidthexpn, cblkheightexpn;
435 res = &tilec->resolutions[resno];
436 /* border for each resolution level (global) */
437 res->x0 = int_ceildivpow2(tilec->x0, levelno);
438 res->y0 = int_ceildivpow2(tilec->y0, levelno);
439 res->x1 = int_ceildivpow2(tilec->x1, levelno);
440 res->y1 = int_ceildivpow2(tilec->y1, levelno);
442 res->numbands = resno == 0 ? 1 : 3;
443 /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */
444 if (tccp->csty & J2K_CCP_CSTY_PRT) {
445 pdx = tccp->prcw[resno];
446 pdy = tccp->prch[resno];
451 /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
452 tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx;
453 tlprcystart = int_floordivpow2(res->y0, pdy) << pdy;
454 brprcxend = int_ceildivpow2(res->x1, pdx) << pdx;
455 brprcyend = int_ceildivpow2(res->y1, pdy) << pdy;
457 res->pw = (brprcxend - tlprcxstart) >> pdx;
458 res->ph = (brprcyend - tlprcystart) >> pdy;
461 tlcbgxstart = tlprcxstart;
462 tlcbgystart = tlprcystart;
463 brcbgxend = brprcxend;
464 brcbgyend = brprcyend;
468 tlcbgxstart = int_ceildivpow2(tlprcxstart, 1);
469 tlcbgystart = int_ceildivpow2(tlprcystart, 1);
470 brcbgxend = int_ceildivpow2(brprcxend, 1);
471 brcbgyend = int_ceildivpow2(brprcyend, 1);
472 cbgwidthexpn = pdx - 1;
473 cbgheightexpn = pdy - 1;
476 cblkwidthexpn = int_min(tccp->cblkw, cbgwidthexpn);
477 cblkheightexpn = int_min(tccp->cblkh, cbgheightexpn);
479 for (bandno = 0; bandno < res->numbands; bandno++) {
483 band = &res->bands[bandno];
484 band->bandno = resno == 0 ? 0 : bandno + 1;
485 x0b = (band->bandno == 1) || (band->bandno == 3) ? 1 : 0;
486 y0b = (band->bandno == 2) || (band->bandno == 3) ? 1 : 0;
488 if (band->bandno == 0) {
490 band->x0 = int_ceildivpow2(tilec->x0, levelno);
491 band->y0 = int_ceildivpow2(tilec->y0, levelno);
492 band->x1 = int_ceildivpow2(tilec->x1, levelno);
493 band->y1 = int_ceildivpow2(tilec->y1, levelno);
496 int_ceildivpow2(tilec->x0 -
497 (1 << levelno) * x0b, levelno + 1);
499 int_ceildivpow2(tilec->y0 -
500 (1 << levelno) * y0b, levelno + 1);
502 int_ceildivpow2(tilec->x1 -
503 (1 << levelno) * x0b, levelno + 1);
505 int_ceildivpow2(tilec->y1 -
506 (1 << levelno) * y0b, levelno + 1);
509 ss = &tccp->stepsizes[resno ==
510 0 ? 0 : 3 * (resno - 1) + bandno + 1];
513 0 ? dwt_getgain_real(band->bandno) : dwt_getgain(band->bandno);
514 numbps = img->comps[compno].prec + gain;
515 band->stepsize = (1.0 + ss->mant / 2048.0) * pow(2.0, numbps - ss->expn);
516 band->numbps = ss->expn + tccp->numgbits - 1; /* WHY -1 ? */
518 for (precno = 0; precno < res->pw * res->ph; precno++) {
519 int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
521 tlcbgxstart + (precno % res->pw) * (1 << cbgwidthexpn);
523 tlcbgystart + (precno / res->pw) * (1 << cbgheightexpn);
524 int cbgxend = cbgxstart + (1 << cbgwidthexpn);
525 int cbgyend = cbgystart + (1 << cbgheightexpn);
527 prc = &band->precincts[precno];
528 /* precinct size (global) */
529 prc->x0 = int_max(cbgxstart, band->x0);
530 prc->y0 = int_max(cbgystart, band->y0);
531 prc->x1 = int_min(cbgxend, band->x1);
532 prc->y1 = int_min(cbgyend, band->y1);
535 int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn;
537 int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn;
539 int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn;
541 int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn;
542 prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;
543 prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn;
547 (tcd_cblk_t *) malloc(prc->cw * prc->ch *
550 if (prc->incltree != NULL)
551 tgt_destroy(prc->incltree);
552 if (prc->imsbtree != NULL)
553 tgt_destroy(prc->imsbtree);
555 prc->incltree = tgt_create(prc->cw, prc->ch);
556 prc->imsbtree = tgt_create(prc->cw, prc->ch);
558 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
560 tlcblkxstart + (cblkno % prc->cw) * (1 << cblkwidthexpn);
562 tlcblkystart + (cblkno / prc->cw) * (1 << cblkheightexpn);
563 int cblkxend = cblkxstart + (1 << cblkwidthexpn);
564 int cblkyend = cblkystart + (1 << cblkheightexpn);
565 cblk = &prc->cblks[cblkno];
567 /* code-block size (global) */
568 cblk->x0 = int_max(cblkxstart, prc->x0);
569 cblk->y0 = int_max(cblkystart, prc->y0);
570 cblk->x1 = int_min(cblkxend, prc->x1);
571 cblk->y1 = int_min(cblkyend, prc->y1);
579 /* tcd_dump(&tcd_image,0); */
582 void tcd_init(j2k_image_t * img, j2k_cp_t * cp)
584 int tileno, compno, resno, bandno, precno, cblkno, i, j, p, q;
585 unsigned int x0 = 0, y0 = 0, x1 = 0, y1 = 0, w, h;
588 tcd_image.tw = cp->tw;
589 tcd_image.th = cp->th;
591 (tcd_tile_t *) malloc(cp->tw * cp->th * sizeof(tcd_tile_t));
593 /*for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
594 j2k_tcp_t *tcp = &cp->tcps[tileno];
595 tcd_tile_t *tile = &tcd_image.tiles[tileno]; */
597 for (i = 0; i < cp->tileno_size; i++) {
598 j2k_tcp_t *tcp = &cp->tcps[cp->tileno[i]];
599 tcd_tile_t *tile = &tcd_image.tiles[cp->tileno[i]];
600 tileno = cp->tileno[i];
603 // int previous_x0, previous_x1, previous_y0, previous_y1;
604 /* cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
605 p = tileno % cp->tw; /* si numerotation matricielle .. */
606 q = tileno / cp->tw; /* .. coordonnees de la tile (q,p) q pour ligne et p pour colonne */
608 /* 4 borders of the tile rescale on the image if necessary */
609 tile->x0 = int_max(cp->tx0 + p * cp->tdx, img->x0);
610 tile->y0 = int_max(cp->ty0 + q * cp->tdy, img->y0);
611 tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, img->x1);
612 tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, img->y1);
614 tile->numcomps = img->numcomps;
616 (tcd_tilecomp_t *) malloc(img->numcomps * sizeof(tcd_tilecomp_t));
617 for (compno = 0; compno < tile->numcomps; compno++) {
618 j2k_tccp_t *tccp = &tcp->tccps[compno];
619 tcd_tilecomp_t *tilec = &tile->comps[compno];
620 /* border of each tile component (global) */
621 tilec->x0 = int_ceildiv(tile->x0, img->comps[compno].dx);
622 tilec->y0 = int_ceildiv(tile->y0, img->comps[compno].dy);
623 tilec->x1 = int_ceildiv(tile->x1, img->comps[compno].dx);
624 tilec->y1 = int_ceildiv(tile->y1, img->comps[compno].dy);
627 (int *) malloc((tilec->x1 - tilec->x0) *
628 (tilec->y1 - tilec->y0) * sizeof(int));
629 tilec->numresolutions = tccp->numresolutions;
631 (tcd_resolution_t *) malloc(tilec->numresolutions *
632 sizeof(tcd_resolution_t));
633 for (resno = 0; resno < tilec->numresolutions; resno++) {
635 int levelno = tilec->numresolutions - 1 - resno;
636 int tlprcxstart, tlprcystart, brprcxend, brprcyend;
637 int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend;
638 int cbgwidthexpn, cbgheightexpn;
639 int cblkwidthexpn, cblkheightexpn;
640 tcd_resolution_t *res = &tilec->resolutions[resno];
642 /* border for each resolution level (global) */
643 res->x0 = int_ceildivpow2(tilec->x0, levelno);
644 res->y0 = int_ceildivpow2(tilec->y0, levelno);
645 res->x1 = int_ceildivpow2(tilec->x1, levelno);
646 res->y1 = int_ceildivpow2(tilec->y1, levelno);
648 res->numbands = resno == 0 ? 1 : 3;
649 /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */
650 if (tccp->csty & J2K_CCP_CSTY_PRT) {
651 pdx = tccp->prcw[resno];
652 pdy = tccp->prch[resno];
657 /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
658 tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx;
659 tlprcystart = int_floordivpow2(res->y0, pdy) << pdy;
660 brprcxend = int_ceildivpow2(res->x1, pdx) << pdx;
661 brprcyend = int_ceildivpow2(res->y1, pdy) << pdy;
662 res->pw = (res->x0 == res->x1) ? 0 : ((brprcxend - tlprcxstart) >> pdx); // Mod Antonin : sizebug1
663 res->ph = (res->y0 == res->y1) ? 0 : ((brprcyend - tlprcystart) >> pdy); // Mod Antonin : sizebug1
666 tlcbgxstart = tlprcxstart;
667 tlcbgystart = tlprcystart;
668 brcbgxend = brprcxend;
669 brcbgyend = brprcyend;
673 tlcbgxstart = int_ceildivpow2(tlprcxstart, 1);
674 tlcbgystart = int_ceildivpow2(tlprcystart, 1);
675 brcbgxend = int_ceildivpow2(brprcxend, 1);
676 brcbgyend = int_ceildivpow2(brprcyend, 1);
677 cbgwidthexpn = pdx - 1;
678 cbgheightexpn = pdy - 1;
681 cblkwidthexpn = int_min(tccp->cblkw, cbgwidthexpn);
682 cblkheightexpn = int_min(tccp->cblkh, cbgheightexpn);
684 for (bandno = 0; bandno < res->numbands; bandno++) {
688 tcd_band_t *band = &res->bands[bandno];
689 band->bandno = resno == 0 ? 0 : bandno + 1;
690 x0b = (band->bandno == 1) || (band->bandno == 3) ? 1 : 0;
691 y0b = (band->bandno == 2) || (band->bandno == 3) ? 1 : 0;
693 if (band->bandno == 0) {
694 /* band border (global) */
695 band->x0 = int_ceildivpow2(tilec->x0, levelno);
696 band->y0 = int_ceildivpow2(tilec->y0, levelno);
697 band->x1 = int_ceildivpow2(tilec->x1, levelno);
698 band->y1 = int_ceildivpow2(tilec->y1, levelno);
700 /* band border (global) */
702 int_ceildivpow2(tilec->x0 -
703 (1 << levelno) * x0b, levelno + 1);
705 int_ceildivpow2(tilec->y0 -
706 (1 << levelno) * y0b, levelno + 1);
708 int_ceildivpow2(tilec->x1 -
709 (1 << levelno) * x0b, levelno + 1);
711 int_ceildivpow2(tilec->y1 -
712 (1 << levelno) * y0b, levelno + 1);
715 ss = &tccp->stepsizes[resno ==
716 0 ? 0 : 3 * (resno - 1) + bandno + 1];
719 0 ? dwt_getgain_real(band->bandno) : dwt_getgain(band->bandno);
720 numbps = img->comps[compno].prec + gain;
721 band->stepsize = (1.0 + ss->mant / 2048.0) * pow(2.0, numbps - ss->expn);
722 band->numbps = ss->expn + tccp->numgbits - 1; /* WHY -1 ? */
725 (tcd_precinct_t *) malloc(res->pw * res->ph *
726 sizeof(tcd_precinct_t));
728 for (precno = 0; precno < res->pw * res->ph; precno++) {
729 int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
731 tlcbgxstart + (precno % res->pw) * (1 << cbgwidthexpn);
733 tlcbgystart + (precno / res->pw) * (1 << cbgheightexpn);
734 int cbgxend = cbgxstart + (1 << cbgwidthexpn);
735 int cbgyend = cbgystart + (1 << cbgheightexpn);
736 tcd_precinct_t *prc = &band->precincts[precno];
737 /* precinct size (global) */
738 prc->x0 = int_max(cbgxstart, band->x0);
739 prc->y0 = int_max(cbgystart, band->y0);
740 prc->x1 = int_min(cbgxend, band->x1);
741 prc->y1 = int_min(cbgyend, band->y1);
744 int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn;
746 int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn;
748 int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn;
750 int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn;
751 prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;
752 prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn;
755 (tcd_cblk_t *) malloc(prc->cw * prc->ch *
758 prc->incltree = tgt_create(prc->cw, prc->ch);
759 prc->imsbtree = tgt_create(prc->cw, prc->ch);
761 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
763 tlcblkxstart + (cblkno % prc->cw) * (1 << cblkwidthexpn);
765 tlcblkystart + (cblkno / prc->cw) * (1 << cblkheightexpn);
766 int cblkxend = cblkxstart + (1 << cblkwidthexpn);
767 int cblkyend = cblkystart + (1 << cblkheightexpn);
768 tcd_cblk_t *cblk = &prc->cblks[cblkno];
769 /* code-block size (global) */
770 cblk->x0 = int_max(cblkxstart, prc->x0);
771 cblk->y0 = int_max(cblkystart, prc->y0);
772 cblk->x1 = int_min(cblkxend, prc->x1);
773 cblk->y1 = int_min(cblkyend, prc->y1);
780 //tcd_dump(&tcd_image,0);
783 /* Allocate place to store the data decoded = final image */
784 /* Place limited by the tile really present in the codestream */
787 for (i = 0; i < img->numcomps; i++) {
788 for (j = 0; j < cp->tileno_size; j++) {
789 tileno = cp->tileno[j];
790 x0 = j == 0 ? tcd_image.tiles[tileno].comps[i].x0 : int_min(x0,
798 j == 0 ? tcd_image.tiles[tileno].comps[i].y0 : int_min(y0,
806 j == 0 ? tcd_image.tiles[tileno].comps[i].x1 : int_max(x1,
814 j == 0 ? tcd_image.tiles[tileno].comps[i].y1 : int_max(y1,
826 img->comps[i].data = (int *) calloc(w * h, sizeof(int));
829 img->comps[i].x0 = x0;
830 img->comps[i].y0 = y0;
834 void tcd_makelayer_fixed(int layno, int final)
836 int compno, resno, bandno, precno, cblkno;
837 int value; //, matrice[tcd_tcp->numlayers][tcd_tile->comps[0].numresolutions][3];
838 int matrice[10][10][3];
841 /*matrice=(int*)malloc(tcd_tcp->numlayers*tcd_tile->comps[0].numresolutions*3*sizeof(int)); */
843 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
844 tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
845 for (i = 0; i < tcd_tcp->numlayers; i++) {
846 for (j = 0; j < tilec->numresolutions; j++) {
847 for (k = 0; k < 3; k++) {
850 matrice[i * tilec->numresolutions * 3 +
853 (float) (tcd_img->comps[compno].prec / 16.0));
856 for (resno = 0; resno < tilec->numresolutions; resno++) {
857 tcd_resolution_t *res = &tilec->resolutions[resno];
858 for (bandno = 0; bandno < res->numbands; bandno++) {
859 tcd_band_t *band = &res->bands[bandno];
860 for (precno = 0; precno < res->pw * res->ph; precno++) {
861 tcd_precinct_t *prc = &band->precincts[precno];
862 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
863 tcd_cblk_t *cblk = &prc->cblks[cblkno];
864 tcd_layer_t *layer = &cblk->layers[layno];
866 int imsb = tcd_img->comps[compno].prec - cblk->numbps; /* number of bit-plan equal to zero */
867 /* Correction of the matrix of coefficient to include the IMSB information */
870 value = matrice[layno][resno][bandno];
877 matrice[layno][resno][bandno] -
878 matrice[layno - 1][resno][bandno];
879 if (imsb >= matrice[layno - 1][resno][bandno]) {
880 value -= (imsb - matrice[layno - 1][resno][bandno]);
887 cblk->numpassesinlayers = 0;
889 n = cblk->numpassesinlayers;
890 if (cblk->numpassesinlayers == 0) {
892 n = 3 * value - 2 + cblk->numpassesinlayers;
894 n = cblk->numpassesinlayers;
896 n = 3 * value + cblk->numpassesinlayers;
898 layer->numpasses = n - cblk->numpassesinlayers;
900 if (!layer->numpasses)
903 if (cblk->numpassesinlayers == 0) {
904 layer->len = cblk->passes[n - 1].rate;
905 layer->data = cblk->data;
908 cblk->passes[n - 1].rate -
909 cblk->passes[cblk->numpassesinlayers - 1].rate;
912 cblk->passes[cblk->numpassesinlayers - 1].rate;
915 cblk->numpassesinlayers = n;
923 void tcd_rateallocate_fixed()
927 for (layno = 0; layno < tcd_tcp->numlayers; layno++) {
928 tcd_makelayer_fixed(layno, 1);
932 void tcd_makelayer(int layno, double thresh, int final)
934 int compno, resno, bandno, precno, cblkno, passno;
936 tcd_tile->distolayer[layno] = 0; //add fixed_quality
938 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
939 tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
940 for (resno = 0; resno < tilec->numresolutions; resno++) {
941 tcd_resolution_t *res = &tilec->resolutions[resno];
942 for (bandno = 0; bandno < res->numbands; bandno++) {
943 tcd_band_t *band = &res->bands[bandno];
944 for (precno = 0; precno < res->pw * res->ph; precno++) {
945 tcd_precinct_t *prc = &band->precincts[precno];
946 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
947 tcd_cblk_t *cblk = &prc->cblks[cblkno];
948 tcd_layer_t *layer = &cblk->layers[layno];
952 cblk->numpassesinlayers = 0;
954 n = cblk->numpassesinlayers;
955 for (passno = cblk->numpassesinlayers;
956 passno < cblk->totalpasses; passno++) {
959 tcd_pass_t *pass = &cblk->passes[passno];
962 dd = pass->distortiondec;
964 dr = pass->rate - cblk->passes[n - 1].rate;
965 dd = pass->distortiondec - cblk->passes[n -
974 if (dd / dr >= thresh)
977 layer->numpasses = n - cblk->numpassesinlayers;
979 if (!layer->numpasses) {
984 if (cblk->numpassesinlayers == 0) {
985 layer->len = cblk->passes[n - 1].rate;
986 layer->data = cblk->data;
987 layer->disto = cblk->passes[n - 1].distortiondec;
989 layer->len = cblk->passes[n - 1].rate -
990 cblk->passes[cblk->numpassesinlayers - 1].rate;
993 cblk->passes[cblk->numpassesinlayers - 1].rate;
995 cblk->passes[n - 1].distortiondec -
996 cblk->passes[cblk->numpassesinlayers - 1].distortiondec;
999 tcd_tile->distolayer[layno] += layer->disto; //add fixed_quality
1002 cblk->numpassesinlayers = n;
1010 void tcd_rateallocate(unsigned char *dest, int len, info_image * info_IM)
1012 int compno, resno, bandno, precno, cblkno, passno, layno;
1014 double cumdisto[100]; //add fixed_quality
1015 const double K = 1; // 1.1; //add fixed_quality
1021 tcd_tile->nbpix = 0; //add fixed_quality
1023 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
1024 tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
1027 for (resno = 0; resno < tilec->numresolutions; resno++) {
1028 tcd_resolution_t *res = &tilec->resolutions[resno];
1029 for (bandno = 0; bandno < res->numbands; bandno++) {
1030 tcd_band_t *band = &res->bands[bandno];
1031 for (precno = 0; precno < res->pw * res->ph; precno++) {
1032 tcd_precinct_t *prc = &band->precincts[precno];
1033 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
1034 tcd_cblk_t *cblk = &prc->cblks[cblkno];
1035 for (passno = 0; passno < cblk->totalpasses; passno++) {
1036 tcd_pass_t *pass = &cblk->passes[passno];
1041 dd = pass->distortiondec;
1043 dr = pass->rate - cblk->passes[passno - 1].rate;
1044 dd = pass->distortiondec -
1045 cblk->passes[passno - 1].distortiondec;
1053 if (rdslope < min) {
1056 if (rdslope > max) {
1061 tcd_tile->nbpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0)); //add fixed_quality
1063 tilec->nbpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0)); //add fixed_quality
1069 maxSE += (((double)(1 << tcd_img->comps[compno].prec) - 1.0) * ((double)(1 << tcd_img->comps[compno].prec) -1.0)) * ((double)(tilec->nbpix));
1072 /* add antonin index */
1073 if (info_IM->index_on) {
1074 info_tile *info_TL = &info_IM->tile[tcd_tileno];
1075 info_TL->nbpix = tcd_tile->nbpix;
1076 info_TL->distotile = tcd_tile->distotile;
1078 (double *) malloc(tcd_tcp->numlayers * sizeof(double));
1082 for (layno = 0; layno < tcd_tcp->numlayers; layno++) {
1083 volatile double lo = min;
1084 volatile double hi = max;
1085 volatile int success = 0;
1086 volatile int maxlen = tcd_tcp->rates[layno] ? int_min(tcd_tcp->rates[layno], len) : len; //Mod antonin losslessbug
1087 volatile double goodthresh;
1089 double distotarget; //add fixed_quality
1091 distotarget = tcd_tile->distotile - ((K * maxSE) / pow(10, tcd_tcp->distoratio[layno] / 10)); // add fixed_quality
1093 if ((tcd_tcp->rates[layno]) || (tcd_cp->disto_alloc==0)) {
1094 for (i = 0; i < 32; i++) {
1095 volatile double thresh = (lo + hi) / 2;
1097 double distoachieved = 0; // add fixed_quality
1099 tcd_makelayer(layno, thresh, 0);
1101 if (tcd_cp->fixed_quality) { // add fixed_quality
1104 0 ? tcd_tile->distolayer[0] : cumdisto[layno - 1] +
1105 tcd_tile->distolayer[layno];
1106 if (distoachieved < distotarget) {
1113 t2_encode_packets(tcd_img, tcd_cp, tcd_tileno, tcd_tile,
1114 layno + 1, dest, maxlen, info_IM);
1115 /* fprintf(stderr, "rate alloc: len=%d, max=%d\n", l, maxlen); */
1124 goodthresh = thresh;
1132 longjmp(j2k_error, 1);
1135 if (info_IM->index_on) { /* Threshold for Marcela Index */
1136 info_IM->tile[tcd_tileno].thresh[layno] = goodthresh;
1138 tcd_makelayer(layno, goodthresh, 1);
1140 cumdisto[layno] = layno == 0 ? tcd_tile->distolayer[0] : cumdisto[layno - 1] + tcd_tile->distolayer[layno]; // add fixed_quality
1145 tcd_encode_tile_pxm(int tileno, unsigned char *dest, int len,
1146 info_image * info_IM)
1152 j2k_tcp_t *tcp = &tcd_cp->tcps[0];
1153 j2k_tccp_t *tccp = &tcp->tccps[0];
1155 tcd_tileno = tileno;
1156 tcd_tile = tcd_image.tiles;
1157 tcd_tcp = &tcd_cp->tcps[tileno];
1159 /* INDEX >> "Precinct_nb_X et Precinct_nb_Y" */
1160 if (info_IM->index_on) {
1161 tcd_tilecomp_t *tilec_idx = &tile->comps[0]; //Based on Component 0
1163 for (i = 0; i < tilec_idx->numresolutions; i++) {
1165 tcd_resolution_t *res_idx = &tilec_idx->resolutions[i];
1167 info_IM->tile[tileno].pw[i] = res_idx->pw;
1168 info_IM->tile[tileno].ph[i] = res_idx->ph;
1170 npck+=res_idx->pw * res_idx->ph;
1172 info_IM->tile[tileno].pdx[i] = tccp->prcw[i];
1173 info_IM->tile[tileno].pdy[i] = tccp->prch[i];
1176 info_IM->tile[tileno].packet = (info_packet *) calloc(info_IM->Comp * info_IM->Layer * npck, sizeof(info_packet));
1180 /*---------------TILE-------------------*/
1184 for (compno = 0; compno < tile->numcomps; compno++) {
1191 tcd_tilecomp_t *tilec = &tile->comps[compno];
1193 tcd_img->comps[compno].sgnd ? 0 : 1 << (tcd_img->comps[compno].
1195 int offset_x, offset_y;
1197 offset_x = int_ceildiv(tcd_img->x0, tcd_img->comps[compno].dx);
1198 offset_y = int_ceildiv(tcd_img->y0, tcd_img->comps[compno].dy);
1199 tw = tilec->x1 - tilec->x0;
1200 w = int_ceildiv(tcd_img->x1 - tcd_img->x0, tcd_img->comps[compno].dx);
1201 sprintf(tmp, "Compo%d", compno); /* component file */
1202 src = fopen(tmp, "rb");
1204 fprintf(stderr, "failed to open %s for reading\n", tmp);
1208 /* read the Compo file to extract data of the tile */
1210 fseek(src, (tilec->x0 - offset_x) + (tilec->y0 - offset_y) * w,
1212 k = (tilec->x0 - offset_x) + (tilec->y0 - offset_y) * w;
1213 for (j = tilec->y0; j < tilec->y1; j++) {
1214 for (i = tilec->x0; i < tilec->x1; i++) {
1215 if (tcd_tcp->tccps[compno].qmfbid == 1) {
1217 tilec->data[i - tilec->x0 + (j - tilec->y0) * tw] =
1220 } else if (tcd_tcp->tccps[compno].qmfbid == 0) {
1222 tilec->data[i - tilec->x0 + (j - tilec->y0) * tw] =
1223 (elmt - adjust) << 13;
1227 fseek(src, (tilec->x0 - offset_x) + (j + 1 - offset_y) * w - k,
1229 k = tilec->x0 - offset_x + (j + 1 - offset_y) * w;
1235 /*----------------MCT-------------------*/
1238 if (tcd_tcp->tccps[0].qmfbid == 0) {
1239 mct_encode_real(tile->comps[0].data, tile->comps[1].data,
1240 tile->comps[2].data,
1241 (tile->comps[0].x1 -
1242 tile->comps[0].x0) * (tile->comps[0].y1 -
1243 tile->comps[0].y0));
1245 mct_encode(tile->comps[0].data, tile->comps[1].data,
1246 tile->comps[2].data,
1247 (tile->comps[0].x1 -
1248 tile->comps[0].x0) * (tile->comps[0].y1 -
1249 tile->comps[0].y0));
1252 /*----------------DWT---------------------*/
1255 for (compno = 0; compno < tile->numcomps; compno++) {
1256 tcd_tilecomp_t *tilec = &tile->comps[compno];
1257 if (tcd_tcp->tccps[compno].qmfbid == 1) {
1259 } else if (tcd_tcp->tccps[compno].qmfbid == 0) {
1260 dwt_encode_real(tilec);
1264 /*------------------TIER1-----------------*/
1267 t1_encode_cblks(tile, tcd_tcp);
1269 /*-----------RATE-ALLOCATE------------------*/
1270 info_IM->index_write = 0; /* INDEX */
1272 if (tcd_cp->disto_alloc || tcd_cp->fixed_quality) // mod fixed_quality
1273 /* Normal Rate/distortion allocation */
1274 tcd_rateallocate(dest, len, info_IM);
1276 /* Fixed layer allocation */
1277 tcd_rateallocate_fixed();
1279 /*--------------TIER2------------------*/
1280 info_IM->index_write = 1; /* INDEX */
1281 l = t2_encode_packets(tcd_img, tcd_cp, tileno, tile,
1282 tcd_tcp->numlayers, dest, len, info_IM);
1283 /*---------------CLEAN-------------------*/
1285 time7 = clock() - time7;
1286 fprintf(stdout,"total: %ld.%.3ld s\n", time7 / CLOCKS_PER_SEC,
1287 (time7 % CLOCKS_PER_SEC) * 1000 / CLOCKS_PER_SEC);
1289 /* cleaning memory */
1290 for (compno = 0; compno < tile->numcomps; compno++) {
1291 tilec = &tile->comps[compno];
1299 tcd_encode_tile_pgx(int tileno, unsigned char *dest, int len,
1300 info_image * info_IM)
1306 j2k_tcp_t *tcp = &tcd_cp->tcps[0];
1307 j2k_tccp_t *tccp = &tcp->tccps[0];
1309 tcd_tileno = tileno;
1310 tcd_tile = tcd_image.tiles;
1311 tcd_tcp = &tcd_cp->tcps[tileno];
1313 /* INDEX >> "Precinct_nb_X et Precinct_nb_Y" */
1314 if (info_IM->index_on) {
1315 tcd_tilecomp_t *tilec_idx = &tile->comps[0]; //Based on Component 0
1317 for (i = 0; i < tilec_idx->numresolutions; i++) {
1319 tcd_resolution_t *res_idx = &tilec_idx->resolutions[i];
1321 info_IM->tile[tileno].pw[i] = res_idx->pw;
1322 info_IM->tile[tileno].ph[i] = res_idx->ph;
1324 npck+=res_idx->pw * res_idx->ph;
1326 info_IM->tile[tileno].pdx[i] = tccp->prcw[i];
1327 info_IM->tile[tileno].pdy[i] = tccp->prch[i];
1330 info_IM->tile[tileno].packet = (info_packet *) calloc(info_IM->Comp * info_IM->Layer * npck, sizeof(info_packet));
1333 /*---------------TILE-------------------*/
1336 for (compno = 0; compno < tile->numcomps; compno++) {
1343 tcd_tilecomp_t *tilec = &tile->comps[compno];
1345 tcd_img->comps[compno].sgnd ? 0 : 1 << (tcd_img->comps[compno].
1347 int offset_x, offset_y;
1349 offset_x = int_ceildiv(tcd_img->x0, tcd_img->comps[compno].dx);
1350 offset_y = int_ceildiv(tcd_img->y0, tcd_img->comps[compno].dy);
1351 tw = tilec->x1 - tilec->x0;
1352 w = int_ceildiv(tcd_img->x1 - tcd_img->x0, tcd_img->comps[compno].dx);
1353 sprintf(tmp, "bandtile%d", tileno / tcd_cp->tw + 1); /* bandtile file opening */
1354 src = fopen(tmp, "rb");
1356 fprintf(stderr, "failed to open %s for reading\n", tmp);
1359 /* Extract data from bandtile file limited to the current tile */
1361 while (k < tilec->x0 - offset_x) {
1363 fscanf(src, "%d", &elmt);
1366 for (j = 0; j < tilec->y1 - tilec->y0; j++) {
1367 for (i = tilec->x0; i < tilec->x1; i++) {
1368 if (tcd_tcp->tccps[compno].qmfbid == 1) {
1369 fscanf(src, "%d", &elmt);
1370 tilec->data[i - tilec->x0 + (j) * tw] = elmt - adjust;
1372 } else if (tcd_tcp->tccps[compno].qmfbid == 0) {
1373 fscanf(src, "%d", &elmt);
1374 tilec->data[i - tilec->x0 + (j) * tw] = (elmt - adjust) << 13;
1378 while (k < tilec->x0 - offset_x + (j + 1) * w) {
1380 fscanf(src, "%d", &elmt);
1386 /*----------------MCT-------------------*/
1389 if (tcd_tcp->tccps[0].qmfbid == 0) {
1390 mct_encode_real(tile->comps[0].data, tile->comps[1].data,
1391 tile->comps[2].data,
1392 (tile->comps[0].x1 -
1393 tile->comps[0].x0) * (tile->comps[0].y1 -
1394 tile->comps[0].y0));
1396 mct_encode(tile->comps[0].data, tile->comps[1].data,
1397 tile->comps[2].data,
1398 (tile->comps[0].x1 -
1399 tile->comps[0].x0) * (tile->comps[0].y1 -
1400 tile->comps[0].y0));
1404 /*----------------DWT---------------------*/
1407 for (compno = 0; compno < tile->numcomps; compno++) {
1408 tcd_tilecomp_t *tilec = &tile->comps[compno];
1409 if (tcd_tcp->tccps[compno].qmfbid == 1) {
1411 } else if (tcd_tcp->tccps[compno].qmfbid == 0) {
1412 dwt_encode_real(tilec);
1417 /*------------------TIER1-----------------*/
1420 t1_encode_cblks(tile, tcd_tcp);
1422 /*-----------RATE-ALLOCATE------------------*/
1424 info_IM->index_write = 0; /* INDEX */
1426 if (tcd_cp->disto_alloc || tcd_cp->fixed_quality) // mod fixed_quality
1428 /* Normal Rate/distortion allocation */
1430 tcd_rateallocate(dest, len, info_IM);
1433 /* Fixed layer allocation */
1435 tcd_rateallocate_fixed();
1437 /*--------------TIER2------------------*/
1438 info_IM->index_write = 1; /* INDEX */
1440 l = t2_encode_packets(tcd_img, tcd_cp, tileno, tile,
1441 tcd_tcp->numlayers, dest, len, info_IM);
1443 /*---------------CLEAN-------------------*/
1444 time = clock() - time;
1445 fprintf(stdout,"total: %ld.%.3ld s\n", time / CLOCKS_PER_SEC,
1446 (time % CLOCKS_PER_SEC) * 1000 / CLOCKS_PER_SEC);
1448 for (compno = 0; compno < tile->numcomps; compno++) {
1449 tilec = &tile->comps[compno];
1457 int tcd_decode_tile(unsigned char *src, int len, int tileno)
1465 tcd_tileno = tileno;
1466 tcd_tile = &tcd_image.tiles[tileno];
1467 tcd_tcp = &tcd_cp->tcps[tileno];
1472 fprintf(stdout, "Tile %d of %d decoded in ", tileno + 1,
1473 tcd_cp->tw * tcd_cp->th);
1475 /*--------------TIER2------------------*/
1477 l = t2_decode_packets(src, len, tcd_img, tcd_cp, tileno, tile);
1481 fprintf(stderr, "tcd_decode: incomplete bistream\n");
1484 /*------------------TIER1-----------------*/
1486 t1_decode_cblks(tile, tcd_tcp);
1488 /*----------------DWT---------------------*/
1490 for (compno = 0; compno < tile->numcomps; compno++) {
1491 tcd_tilecomp_t *tilec = &tile->comps[compno];
1492 if (tcd_cp->reduce != 0) {
1493 tcd_img->comps[compno].resno_decoded =
1494 tile->comps[compno].numresolutions - tcd_cp->reduce - 1;
1499 if (tcd_tcp->tccps[compno].qmfbid == 1) {
1501 tilec->numresolutions - 1 -
1502 tcd_img->comps[compno].resno_decoded);
1504 dwt_decode_real(tilec,
1505 tilec->numresolutions - 1 -
1506 tcd_img->comps[compno].resno_decoded);
1510 if (tile->comps[compno].numresolutions > 0)
1511 tcd_img->comps[compno].factor =
1512 tile->comps[compno].numresolutions -
1513 (tcd_img->comps[compno].resno_decoded + 1);
1516 /*----------------MCT-------------------*/
1519 if (tcd_tcp->tccps[0].qmfbid == 1) {
1520 mct_decode(tile->comps[0].data, tile->comps[1].data,
1521 tile->comps[2].data,
1522 (tile->comps[0].x1 -
1523 tile->comps[0].x0) * (tile->comps[0].y1 -
1524 tile->comps[0].y0));
1526 mct_decode_real(tile->comps[0].data, tile->comps[1].data,
1527 tile->comps[2].data,
1528 (tile->comps[0].x1 -
1529 tile->comps[0].x0) * (tile->comps[0].y1 -
1530 tile->comps[0].y0));
1534 /*---------------TILE-------------------*/
1536 for (compno = 0; compno < tile->numcomps; compno++) {
1537 tcd_tilecomp_t *tilec = &tile->comps[compno];
1538 tcd_resolution_t *res =
1539 &tilec->resolutions[tcd_img->comps[compno].resno_decoded];
1541 tcd_img->comps[compno].sgnd ? 0 : 1 << (tcd_img->comps[compno].
1544 tcd_img->comps[compno].
1545 sgnd ? -(1 << (tcd_img->comps[compno].prec - 1)) : 0;
1547 tcd_img->comps[compno].
1548 sgnd ? (1 << (tcd_img->comps[compno].prec - 1)) -
1549 1 : (1 << tcd_img->comps[compno].prec) - 1;
1551 int tw = tilec->x1 - tilec->x0;
1552 int w = tcd_img->comps[compno].w;
1555 int offset_x = int_ceildivpow2(tcd_img->comps[compno].x0,
1556 tcd_img->comps[compno].factor);
1557 int offset_y = int_ceildivpow2(tcd_img->comps[compno].y0,
1558 tcd_img->comps[compno].factor);
1560 for (j = res->y0; j < res->y1; j++) {
1561 for (i = res->x0; i < res->x1; i++) {
1564 float tmp = (tilec->data[i - res->x0 + (j - res->y0) * tw])/8192.0;
1567 if (tcd_tcp->tccps[compno].qmfbid == 1) {
1568 v = tilec->data[i - res->x0 + (j - res->y0) * tw];
1570 tmp2=((int) (floor(fabs(tmp)))) + ((int) floor(fabs(tmp*2))%2);
1571 v = ((tmp<0)?-tmp2:tmp2);
1576 tcd_img->comps[compno].data[(i - offset_x) +
1577 (j - offset_y) * w] =
1578 int_clamp(v, min, max);
1583 time = clock() - time;
1584 fprintf(stdout, "%ld.%.3ld s\n", time / CLOCKS_PER_SEC,
1585 (time % 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()
1606 int tileno,compno,resno,bandno,precno;
1608 for (tileno=0;tileno<tcd_image.tw*tcd_image.th;tileno++) {
1610 tcd_tile_t tile=tcd_image.tiles[tileno];
1612 for (compno=0;compno<tile.numcomps;compno++) {
1614 tcd_tilecomp_t tilec=tile.comps[compno];
1616 for (resno=0;resno<tilec.numresolutions;resno++) {
1618 tcd_resolution_t res=tilec.resolutions[resno];
1620 for (bandno=0;bandno<res.numbands;bandno++) {
1622 tcd_band_t band=res.bands[bandno];
1624 for (precno=0;precno<res.ph*res.pw;precno++) {
1626 tcd_precinct_t prec=band.precincts[precno];
1628 if (prec.cblks!=NULL) free(prec.cblks);
1630 if (prec.imsbtree!=NULL) free(prec.imsbtree);
1632 if (prec.incltree!=NULL) free(prec.incltree);
1636 if (band.precincts!=NULL) free(band.precincts);
1642 if (tilec.resolutions!=NULL) free(tilec.resolutions);
1646 if (tile.comps!=NULL) free(tile.comps);
1650 if (tcd_image.tiles!=NULL) free(tcd_image.tiles);