2 * Copyright (c) 2001-2003, David Janssens
3 * Copyright (c) 2002-2003, Yannick Verschueren
4 * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe
5 * Copyright (c) 2005, Hervé Drolon, FreeImage Team
6 * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
31 #include "opj_includes.h"
33 void tcd_dump(FILE *fd, opj_tcd_t *tcd, opj_tcd_image_t * img) {
34 int tileno, compno, resno, bandno, precno, cblkno;
36 fprintf(fd, "image {\n");
37 fprintf(fd, " tw=%d, th=%d x0=%d x1=%d y0=%d y1=%d\n",
38 img->tw, img->th, tcd->image->x0, tcd->image->x1, tcd->image->y0, tcd->image->y1);
40 for (tileno = 0; tileno < img->th * img->tw; tileno++) {
41 opj_tcd_tile_t *tile = &tcd->tcd_image->tiles[tileno];
42 fprintf(fd, " tile {\n");
43 fprintf(fd, " x0=%d, y0=%d, x1=%d, y1=%d, numcomps=%d\n",
44 tile->x0, tile->y0, tile->x1, tile->y1, tile->numcomps);
45 for (compno = 0; compno < tile->numcomps; compno++) {
46 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
47 fprintf(fd, " tilec {\n");
49 " x0=%d, y0=%d, x1=%d, y1=%d, numresolutions=%d\n",
50 tilec->x0, tilec->y0, tilec->x1, tilec->y1, tilec->numresolutions);
51 for (resno = 0; resno < tilec->numresolutions; resno++) {
52 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
53 fprintf(fd, "\n res {\n");
55 " x0=%d, y0=%d, x1=%d, y1=%d, pw=%d, ph=%d, numbands=%d\n",
56 res->x0, res->y0, res->x1, res->y1, res->pw, res->ph, res->numbands);
57 for (bandno = 0; bandno < res->numbands; bandno++) {
58 opj_tcd_band_t *band = &res->bands[bandno];
59 fprintf(fd, " band {\n");
61 " x0=%d, y0=%d, x1=%d, y1=%d, stepsize=%f, numbps=%d\n",
62 band->x0, band->y0, band->x1, band->y1, band->stepsize, band->numbps);
63 for (precno = 0; precno < res->pw * res->ph; precno++) {
64 opj_tcd_precinct_t *prec = &band->precincts[precno];
65 fprintf(fd, " prec {\n");
67 " x0=%d, y0=%d, x1=%d, y1=%d, cw=%d, ch=%d\n",
68 prec->x0, prec->y0, prec->x1, prec->y1, prec->cw, prec->ch);
69 for (cblkno = 0; cblkno < prec->cw * prec->ch; cblkno++) {
70 opj_tcd_cblk_t *cblk = &prec->cblks[cblkno];
71 fprintf(fd, " cblk {\n");
73 " x0=%d, y0=%d, x1=%d, y1=%d\n",
74 cblk->x0, cblk->y0, cblk->x1, cblk->y1);
90 /* ----------------------------------------------------------------------- */
93 Create a new TCD handle
95 opj_tcd_t* tcd_create(opj_common_ptr cinfo) {
96 /* create the tcd structure */
97 opj_tcd_t *tcd = (opj_tcd_t*)opj_malloc(sizeof(opj_tcd_t));
100 tcd->tcd_image = (opj_tcd_image_t*)opj_malloc(sizeof(opj_tcd_image_t));
101 if(!tcd->tcd_image) {
110 Destroy a previously created TCD handle
112 void tcd_destroy(opj_tcd_t *tcd) {
114 opj_free(tcd->tcd_image);
119 /* ----------------------------------------------------------------------- */
121 void tcd_malloc_encode(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp, int curtileno) {
122 int tileno, compno, resno, bandno, precno, cblkno;
124 opj_tcd_tile_t *tile = NULL; /* pointer to tcd->tile */
125 opj_tcd_tilecomp_t *tilec = NULL; /* pointer to tcd->tilec */
126 opj_tcd_resolution_t *res = NULL; /* pointer to tcd->res */
127 opj_tcd_band_t *band = NULL; /* pointer to tcd->band */
128 opj_tcd_precinct_t *prc = NULL; /* pointer to tcd->prc */
129 opj_tcd_cblk_t *cblk = NULL; /* pointer to tcd->cblk */
133 tcd->tcd_image->tw = cp->tw;
134 tcd->tcd_image->th = cp->th;
135 tcd->tcd_image->tiles = (opj_tcd_tile_t *) opj_malloc(sizeof(opj_tcd_tile_t));
137 for (tileno = 0; tileno < 1; tileno++) {
138 opj_tcp_t *tcp = &cp->tcps[curtileno];
141 /* cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
142 int p = curtileno % cp->tw; /* si numerotation matricielle .. */
143 int q = curtileno / cp->tw; /* .. coordonnees de la tile (q,p) q pour ligne et p pour colonne */
145 /* opj_tcd_tile_t *tile=&tcd->tcd_image->tiles[tileno]; */
146 tcd->tile = tcd->tcd_image->tiles;
149 /* 4 borders of the tile rescale on the image if necessary */
150 tile->x0 = int_max(cp->tx0 + p * cp->tdx, image->x0);
151 tile->y0 = int_max(cp->ty0 + q * cp->tdy, image->y0);
152 tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, image->x1);
153 tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, image->y1);
154 tile->numcomps = image->numcomps;
155 /* tile->PPT=image->PPT; */
157 /* Modification of the RATE >> */
158 for (j = 0; j < tcp->numlayers; j++) {
159 tcp->rates[j] = tcp->rates[j] ?
160 int_ceildiv(tile->numcomps
161 * (tile->x1 - tile->x0)
162 * (tile->y1 - tile->y0)
163 * image->comps[0].prec,
164 (tcp->rates[j] * 8 * image->comps[0].dx * image->comps[0].dy))
168 if (j && tcp->rates[j] < tcp->rates[j - 1] + 10) {
169 tcp->rates[j] = tcp->rates[j - 1] + 20;
171 if (!j && tcp->rates[j] < 30)
176 /* << Modification of the RATE */
178 tile->comps = (opj_tcd_tilecomp_t *) opj_malloc(image->numcomps * sizeof(opj_tcd_tilecomp_t));
179 for (compno = 0; compno < tile->numcomps; compno++) {
180 opj_tccp_t *tccp = &tcp->tccps[compno];
182 /* opj_tcd_tilecomp_t *tilec=&tile->comps[compno]; */
183 tcd->tilec = &tile->comps[compno];
186 /* border of each tile component (global) */
187 tilec->x0 = int_ceildiv(tile->x0, image->comps[compno].dx);
188 tilec->y0 = int_ceildiv(tile->y0, image->comps[compno].dy);
189 tilec->x1 = int_ceildiv(tile->x1, image->comps[compno].dx);
190 tilec->y1 = int_ceildiv(tile->y1, image->comps[compno].dy);
192 tilec->data = (int *) opj_malloc((tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0) * sizeof(int));
193 tilec->numresolutions = tccp->numresolutions;
195 tilec->resolutions = (opj_tcd_resolution_t *) opj_malloc(tilec->numresolutions * sizeof(opj_tcd_resolution_t));
197 for (resno = 0; resno < tilec->numresolutions; resno++) {
199 int levelno = tilec->numresolutions - 1 - resno;
200 int tlprcxstart, tlprcystart, brprcxend, brprcyend;
201 int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend;
202 int cbgwidthexpn, cbgheightexpn;
203 int cblkwidthexpn, cblkheightexpn;
205 /* opj_tcd_resolution_t *res=&tilec->resolutions[resno]; */
206 tcd->res = &tilec->resolutions[resno];
209 /* border for each resolution level (global) */
210 res->x0 = int_ceildivpow2(tilec->x0, levelno);
211 res->y0 = int_ceildivpow2(tilec->y0, levelno);
212 res->x1 = int_ceildivpow2(tilec->x1, levelno);
213 res->y1 = int_ceildivpow2(tilec->y1, levelno);
215 res->numbands = resno == 0 ? 1 : 3;
216 /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */
217 if (tccp->csty & J2K_CCP_CSTY_PRT) {
218 pdx = tccp->prcw[resno];
219 pdy = tccp->prch[resno];
224 /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
225 tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx;
226 tlprcystart = int_floordivpow2(res->y0, pdy) << pdy;
228 brprcxend = int_ceildivpow2(res->x1, pdx) << pdx;
229 brprcyend = int_ceildivpow2(res->y1, pdy) << pdy;
231 res->pw = (brprcxend - tlprcxstart) >> pdx;
232 res->ph = (brprcyend - tlprcystart) >> pdy;
235 tlcbgxstart = tlprcxstart;
236 tlcbgystart = tlprcystart;
237 brcbgxend = brprcxend;
238 brcbgyend = brprcyend;
242 tlcbgxstart = int_ceildivpow2(tlprcxstart, 1);
243 tlcbgystart = int_ceildivpow2(tlprcystart, 1);
244 brcbgxend = int_ceildivpow2(brprcxend, 1);
245 brcbgyend = int_ceildivpow2(brprcyend, 1);
246 cbgwidthexpn = pdx - 1;
247 cbgheightexpn = pdy - 1;
250 cblkwidthexpn = int_min(tccp->cblkw, cbgwidthexpn);
251 cblkheightexpn = int_min(tccp->cblkh, cbgheightexpn);
253 for (bandno = 0; bandno < res->numbands; bandno++) {
256 opj_stepsize_t *ss = NULL;
258 tcd->band = &res->bands[bandno];
261 band->bandno = resno == 0 ? 0 : bandno + 1;
262 x0b = (band->bandno == 1) || (band->bandno == 3) ? 1 : 0;
263 y0b = (band->bandno == 2) || (band->bandno == 3) ? 1 : 0;
265 if (band->bandno == 0) {
266 /* band border (global) */
267 band->x0 = int_ceildivpow2(tilec->x0, levelno);
268 band->y0 = int_ceildivpow2(tilec->y0, levelno);
269 band->x1 = int_ceildivpow2(tilec->x1, levelno);
270 band->y1 = int_ceildivpow2(tilec->y1, levelno);
272 /* band border (global) */
273 band->x0 = int_ceildivpow2(tilec->x0 - (1 << levelno) * x0b, levelno + 1);
274 band->y0 = int_ceildivpow2(tilec->y0 - (1 << levelno) * y0b, levelno + 1);
275 band->x1 = int_ceildivpow2(tilec->x1 - (1 << levelno) * x0b, levelno + 1);
276 band->y1 = int_ceildivpow2(tilec->y1 - (1 << levelno) * y0b, levelno + 1);
279 ss = &tccp->stepsizes[resno == 0 ? 0 : 3 * (resno - 1) + bandno + 1];
280 gain = tccp->qmfbid == 0 ? dwt_getgain_real(band->bandno) : dwt_getgain(band->bandno);
281 numbps = image->comps[compno].prec + gain;
283 band->stepsize = (float)((1.0 + ss->mant / 2048.0) * pow(2.0, numbps - ss->expn));
284 band->numbps = ss->expn + tccp->numgbits - 1; /* WHY -1 ? */
286 band->precincts = (opj_tcd_precinct_t *) opj_malloc(3 * res->pw * res->ph * sizeof(opj_tcd_precinct_t));
288 for (i = 0; i < res->pw * res->ph * 3; i++) {
289 band->precincts[i].imsbtree = NULL;
290 band->precincts[i].incltree = NULL;
293 for (precno = 0; precno < res->pw * res->ph; precno++) {
294 int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
296 int cbgxstart = tlcbgxstart + (precno % res->pw) * (1 << cbgwidthexpn);
297 int cbgystart = tlcbgystart + (precno / res->pw) * (1 << cbgheightexpn);
298 int cbgxend = cbgxstart + (1 << cbgwidthexpn);
299 int cbgyend = cbgystart + (1 << cbgheightexpn);
301 /* opj_tcd_precinct_t *prc=&band->precincts[precno]; */
302 tcd->prc = &band->precincts[precno];
305 /* precinct size (global) */
306 prc->x0 = int_max(cbgxstart, band->x0);
307 prc->y0 = int_max(cbgystart, band->y0);
308 prc->x1 = int_min(cbgxend, band->x1);
309 prc->y1 = int_min(cbgyend, band->y1);
311 tlcblkxstart = int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn;
312 tlcblkystart = int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn;
313 brcblkxend = int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn;
314 brcblkyend = int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn;
315 prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;
316 prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn;
318 prc->cblks = (opj_tcd_cblk_t *) opj_malloc((prc->cw * prc->ch) * sizeof(opj_tcd_cblk_t));
319 prc->incltree = tgt_create(prc->cw, prc->ch);
320 prc->imsbtree = tgt_create(prc->cw, prc->ch);
322 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
323 int cblkxstart = tlcblkxstart + (cblkno % prc->cw) * (1 << cblkwidthexpn);
324 int cblkystart = tlcblkystart + (cblkno / prc->cw) * (1 << cblkheightexpn);
325 int cblkxend = cblkxstart + (1 << cblkwidthexpn);
326 int cblkyend = cblkystart + (1 << cblkheightexpn);
328 tcd->cblk = &prc->cblks[cblkno];
331 /* code-block size (global) */
332 cblk->x0 = int_max(cblkxstart, prc->x0);
333 cblk->y0 = int_max(cblkystart, prc->y0);
334 cblk->x1 = int_min(cblkxend, prc->x1);
335 cblk->y1 = int_min(cblkyend, prc->y1);
343 /* tcd_dump(stdout, tcd, &tcd->tcd_image); */
346 void tcd_free_encode(opj_tcd_t *tcd) {
347 int tileno, compno, resno, bandno, precno;
349 opj_tcd_tile_t *tile = NULL; /* pointer to tcd->tile */
350 opj_tcd_tilecomp_t *tilec = NULL; /* pointer to tcd->tilec */
351 opj_tcd_resolution_t *res = NULL; /* pointer to tcd->res */
352 opj_tcd_band_t *band = NULL; /* pointer to tcd->band */
353 opj_tcd_precinct_t *prc = NULL; /* pointer to tcd->prc */
355 for (tileno = 0; tileno < 1; tileno++) {
356 tcd->tile = tcd->tcd_image->tiles;
359 for (compno = 0; compno < tile->numcomps; compno++) {
360 tcd->tilec = &tile->comps[compno];
363 for (resno = 0; resno < tilec->numresolutions; resno++) {
364 tcd->res = &tilec->resolutions[resno];
367 for (bandno = 0; bandno < res->numbands; bandno++) {
368 tcd->band = &res->bands[bandno];
371 for (precno = 0; precno < res->pw * res->ph; precno++) {
372 tcd->prc = &band->precincts[precno];
375 if (prc->incltree != NULL) {
376 tgt_destroy(prc->incltree);
377 prc->incltree = NULL;
379 if (prc->imsbtree != NULL) {
380 tgt_destroy(prc->imsbtree);
381 prc->imsbtree = NULL;
383 opj_free(prc->cblks);
386 opj_free(band->precincts);
387 band->precincts = NULL;
390 opj_free(tilec->resolutions);
391 tilec->resolutions = NULL;
393 opj_free(tile->comps);
396 opj_free(tcd->tcd_image->tiles);
397 tcd->tcd_image->tiles = NULL;
400 void tcd_init_encode(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp, int curtileno) {
401 int tileno, compno, resno, bandno, precno, cblkno;
403 opj_tcd_tile_t *tile = NULL; /* pointer to tcd->tile */
404 opj_tcd_tilecomp_t *tilec = NULL; /* pointer to tcd->tilec */
405 opj_tcd_resolution_t *res = NULL; /* pointer to tcd->res */
406 opj_tcd_band_t *band = NULL; /* pointer to tcd->band */
407 opj_tcd_precinct_t *prc = NULL; /* pointer to tcd->prc */
408 opj_tcd_cblk_t *cblk = NULL; /* pointer to tcd->cblk */
410 for (tileno = 0; tileno < 1; tileno++) {
411 opj_tcp_t *tcp = &cp->tcps[curtileno];
413 /* cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
414 int p = curtileno % cp->tw;
415 int q = curtileno / cp->tw;
417 tcd->tile = tcd->tcd_image->tiles;
420 /* 4 borders of the tile rescale on the image if necessary */
421 tile->x0 = int_max(cp->tx0 + p * cp->tdx, image->x0);
422 tile->y0 = int_max(cp->ty0 + q * cp->tdy, image->y0);
423 tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, image->x1);
424 tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, image->y1);
426 tile->numcomps = image->numcomps;
427 /* tile->PPT=image->PPT; */
429 /* Modification of the RATE >> */
430 for (j = 0; j < tcp->numlayers; j++) {
431 tcp->rates[j] = tcp->rates[j] ?
432 int_ceildiv(tile->numcomps
433 * (tile->x1 - tile->x0)
434 * (tile->y1 - tile->y0)
435 * image->comps[0].prec,
436 (tcp->rates[j] * 8 * image->comps[0].dx * image->comps[0].dy))
440 if (j && tcp->rates[j] < tcp->rates[j - 1] + 10) {
441 tcp->rates[j] = tcp->rates[j - 1] + 20;
443 if (!j && tcp->rates[j] < 30)
448 /* << Modification of the RATE */
450 /* tile->comps=(opj_tcd_tilecomp_t*)opj_realloc(tile->comps,image->numcomps*sizeof(opj_tcd_tilecomp_t)); */
451 for (compno = 0; compno < tile->numcomps; compno++) {
452 opj_tccp_t *tccp = &tcp->tccps[compno];
454 tcd->tilec = &tile->comps[compno];
457 /* border of each tile component (global) */
458 tilec->x0 = int_ceildiv(tile->x0, image->comps[compno].dx);
459 tilec->y0 = int_ceildiv(tile->y0, image->comps[compno].dy);
460 tilec->x1 = int_ceildiv(tile->x1, image->comps[compno].dx);
461 tilec->y1 = int_ceildiv(tile->y1, image->comps[compno].dy);
463 tilec->data = (int *) opj_malloc((tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0) * sizeof(int));
464 tilec->numresolutions = tccp->numresolutions;
465 /* tilec->resolutions=(opj_tcd_resolution_t*)opj_realloc(tilec->resolutions,tilec->numresolutions*sizeof(opj_tcd_resolution_t)); */
466 for (resno = 0; resno < tilec->numresolutions; resno++) {
469 int levelno = tilec->numresolutions - 1 - resno;
470 int tlprcxstart, tlprcystart, brprcxend, brprcyend;
471 int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend;
472 int cbgwidthexpn, cbgheightexpn;
473 int cblkwidthexpn, cblkheightexpn;
475 tcd->res = &tilec->resolutions[resno];
478 /* border for each resolution level (global) */
479 res->x0 = int_ceildivpow2(tilec->x0, levelno);
480 res->y0 = int_ceildivpow2(tilec->y0, levelno);
481 res->x1 = int_ceildivpow2(tilec->x1, levelno);
482 res->y1 = int_ceildivpow2(tilec->y1, levelno);
483 res->numbands = resno == 0 ? 1 : 3;
485 /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */
486 if (tccp->csty & J2K_CCP_CSTY_PRT) {
487 pdx = tccp->prcw[resno];
488 pdy = tccp->prch[resno];
493 /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
494 tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx;
495 tlprcystart = int_floordivpow2(res->y0, pdy) << pdy;
496 brprcxend = int_ceildivpow2(res->x1, pdx) << pdx;
497 brprcyend = int_ceildivpow2(res->y1, pdy) << pdy;
499 res->pw = (brprcxend - tlprcxstart) >> pdx;
500 res->ph = (brprcyend - tlprcystart) >> pdy;
503 tlcbgxstart = tlprcxstart;
504 tlcbgystart = tlprcystart;
505 brcbgxend = brprcxend;
506 brcbgyend = brprcyend;
510 tlcbgxstart = int_ceildivpow2(tlprcxstart, 1);
511 tlcbgystart = int_ceildivpow2(tlprcystart, 1);
512 brcbgxend = int_ceildivpow2(brprcxend, 1);
513 brcbgyend = int_ceildivpow2(brprcyend, 1);
514 cbgwidthexpn = pdx - 1;
515 cbgheightexpn = pdy - 1;
518 cblkwidthexpn = int_min(tccp->cblkw, cbgwidthexpn);
519 cblkheightexpn = int_min(tccp->cblkh, cbgheightexpn);
521 for (bandno = 0; bandno < res->numbands; bandno++) {
524 opj_stepsize_t *ss = NULL;
526 tcd->band = &res->bands[bandno];
529 band->bandno = resno == 0 ? 0 : bandno + 1;
530 x0b = (band->bandno == 1) || (band->bandno == 3) ? 1 : 0;
531 y0b = (band->bandno == 2) || (band->bandno == 3) ? 1 : 0;
533 if (band->bandno == 0) {
535 band->x0 = int_ceildivpow2(tilec->x0, levelno);
536 band->y0 = int_ceildivpow2(tilec->y0, levelno);
537 band->x1 = int_ceildivpow2(tilec->x1, levelno);
538 band->y1 = int_ceildivpow2(tilec->y1, levelno);
540 band->x0 = int_ceildivpow2(tilec->x0 - (1 << levelno) * x0b, levelno + 1);
541 band->y0 = int_ceildivpow2(tilec->y0 - (1 << levelno) * y0b, levelno + 1);
542 band->x1 = int_ceildivpow2(tilec->x1 - (1 << levelno) * x0b, levelno + 1);
543 band->y1 = int_ceildivpow2(tilec->y1 - (1 << levelno) * y0b, levelno + 1);
546 ss = &tccp->stepsizes[resno == 0 ? 0 : 3 * (resno - 1) + bandno + 1];
547 gain = tccp->qmfbid == 0 ? dwt_getgain_real(band->bandno) : dwt_getgain(band->bandno);
548 numbps = image->comps[compno].prec + gain;
549 band->stepsize = (float)((1.0 + ss->mant / 2048.0) * pow(2.0, numbps - ss->expn));
550 band->numbps = ss->expn + tccp->numgbits - 1; /* WHY -1 ? */
552 for (precno = 0; precno < res->pw * res->ph; precno++) {
553 int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
555 int cbgxstart = tlcbgxstart + (precno % res->pw) * (1 << cbgwidthexpn);
556 int cbgystart = tlcbgystart + (precno / res->pw) * (1 << cbgheightexpn);
557 int cbgxend = cbgxstart + (1 << cbgwidthexpn);
558 int cbgyend = cbgystart + (1 << cbgheightexpn);
560 tcd->prc = &band->precincts[precno];
563 /* precinct size (global) */
564 prc->x0 = int_max(cbgxstart, band->x0);
565 prc->y0 = int_max(cbgystart, band->y0);
566 prc->x1 = int_min(cbgxend, band->x1);
567 prc->y1 = int_min(cbgyend, band->y1);
569 tlcblkxstart = int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn;
570 tlcblkystart = int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn;
571 brcblkxend = int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn;
572 brcblkyend = int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn;
573 prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;
574 prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn;
576 opj_free(prc->cblks);
577 prc->cblks = (opj_tcd_cblk_t *) opj_malloc(prc->cw * prc->ch * sizeof(opj_tcd_cblk_t));
579 if (prc->incltree != NULL) {
580 tgt_destroy(prc->incltree);
582 if (prc->imsbtree != NULL) {
583 tgt_destroy(prc->imsbtree);
586 prc->incltree = tgt_create(prc->cw, prc->ch);
587 prc->imsbtree = tgt_create(prc->cw, prc->ch);
589 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
590 int cblkxstart = tlcblkxstart + (cblkno % prc->cw) * (1 << cblkwidthexpn);
591 int cblkystart = tlcblkystart + (cblkno / prc->cw) * (1 << cblkheightexpn);
592 int cblkxend = cblkxstart + (1 << cblkwidthexpn);
593 int cblkyend = cblkystart + (1 << cblkheightexpn);
595 tcd->cblk = &prc->cblks[cblkno];
598 /* code-block size (global) */
599 cblk->x0 = int_max(cblkxstart, prc->x0);
600 cblk->y0 = int_max(cblkystart, prc->y0);
601 cblk->x1 = int_min(cblkxend, prc->x1);
602 cblk->y1 = int_min(cblkyend, prc->y1);
610 /* tcd_dump(stdout, tcd, &tcd->tcd_image); */
613 void tcd_malloc_decode(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp) {
614 int tileno, compno, resno, bandno, precno, cblkno, i, j, p, q;
615 unsigned int x0 = 0, y0 = 0, x1 = 0, y1 = 0, w, h;
619 tcd->tcd_image->tw = cp->tw;
620 tcd->tcd_image->th = cp->th;
621 tcd->tcd_image->tiles = (opj_tcd_tile_t *) opj_malloc(cp->tw * cp->th * sizeof(opj_tcd_tile_t));
623 for (i = 0; i < cp->tileno_size; i++) {
624 opj_tcp_t *tcp = &(cp->tcps[cp->tileno[i]]);
625 opj_tcd_tile_t *tile = &(tcd->tcd_image->tiles[cp->tileno[i]]);
627 /* cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
628 tileno = cp->tileno[i];
629 p = tileno % cp->tw; /* si numerotation matricielle .. */
630 q = tileno / cp->tw; /* .. coordonnees de la tile (q,p) q pour ligne et p pour colonne */
632 /* 4 borders of the tile rescale on the image if necessary */
633 tile->x0 = int_max(cp->tx0 + p * cp->tdx, image->x0);
634 tile->y0 = int_max(cp->ty0 + q * cp->tdy, image->y0);
635 tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, image->x1);
636 tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, image->y1);
638 tile->numcomps = image->numcomps;
639 tile->comps = (opj_tcd_tilecomp_t *) opj_malloc(image->numcomps * sizeof(opj_tcd_tilecomp_t));
640 for (compno = 0; compno < tile->numcomps; compno++) {
641 opj_tccp_t *tccp = &tcp->tccps[compno];
642 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
644 /* border of each tile component (global) */
645 tilec->x0 = int_ceildiv(tile->x0, image->comps[compno].dx);
646 tilec->y0 = int_ceildiv(tile->y0, image->comps[compno].dy);
647 tilec->x1 = int_ceildiv(tile->x1, image->comps[compno].dx);
648 tilec->y1 = int_ceildiv(tile->y1, image->comps[compno].dy);
650 tilec->data = (int *) opj_malloc((tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0) * sizeof(int));
651 tilec->numresolutions = tccp->numresolutions;
652 tilec->resolutions = (opj_tcd_resolution_t *) opj_malloc(tilec->numresolutions * sizeof(opj_tcd_resolution_t));
654 for (resno = 0; resno < tilec->numresolutions; resno++) {
656 int levelno = tilec->numresolutions - 1 - resno;
657 int tlprcxstart, tlprcystart, brprcxend, brprcyend;
658 int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend;
659 int cbgwidthexpn, cbgheightexpn;
660 int cblkwidthexpn, cblkheightexpn;
662 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
664 /* border for each resolution level (global) */
665 res->x0 = int_ceildivpow2(tilec->x0, levelno);
666 res->y0 = int_ceildivpow2(tilec->y0, levelno);
667 res->x1 = int_ceildivpow2(tilec->x1, levelno);
668 res->y1 = int_ceildivpow2(tilec->y1, levelno);
669 res->numbands = resno == 0 ? 1 : 3;
671 /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */
672 if (tccp->csty & J2K_CCP_CSTY_PRT) {
673 pdx = tccp->prcw[resno];
674 pdy = tccp->prch[resno];
680 /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
681 tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx;
682 tlprcystart = int_floordivpow2(res->y0, pdy) << pdy;
683 brprcxend = int_ceildivpow2(res->x1, pdx) << pdx;
684 brprcyend = int_ceildivpow2(res->y1, pdy) << pdy;
686 res->pw = (res->x0 == res->x1) ? 0 : ((brprcxend - tlprcxstart) >> pdx);
687 res->ph = (res->y0 == res->y1) ? 0 : ((brprcyend - tlprcystart) >> pdy);
690 tlcbgxstart = tlprcxstart;
691 tlcbgystart = tlprcystart;
692 brcbgxend = brprcxend;
693 brcbgyend = brprcyend;
697 tlcbgxstart = int_ceildivpow2(tlprcxstart, 1);
698 tlcbgystart = int_ceildivpow2(tlprcystart, 1);
699 brcbgxend = int_ceildivpow2(brprcxend, 1);
700 brcbgyend = int_ceildivpow2(brprcyend, 1);
701 cbgwidthexpn = pdx - 1;
702 cbgheightexpn = pdy - 1;
705 cblkwidthexpn = int_min(tccp->cblkw, cbgwidthexpn);
706 cblkheightexpn = int_min(tccp->cblkh, cbgheightexpn);
708 for (bandno = 0; bandno < res->numbands; bandno++) {
711 opj_stepsize_t *ss = NULL;
713 opj_tcd_band_t *band = &res->bands[bandno];
714 band->bandno = resno == 0 ? 0 : bandno + 1;
715 x0b = (band->bandno == 1) || (band->bandno == 3) ? 1 : 0;
716 y0b = (band->bandno == 2) || (band->bandno == 3) ? 1 : 0;
718 if (band->bandno == 0) {
719 /* band border (global) */
720 band->x0 = int_ceildivpow2(tilec->x0, levelno);
721 band->y0 = int_ceildivpow2(tilec->y0, levelno);
722 band->x1 = int_ceildivpow2(tilec->x1, levelno);
723 band->y1 = int_ceildivpow2(tilec->y1, levelno);
725 /* band border (global) */
726 band->x0 = int_ceildivpow2(tilec->x0 - (1 << levelno) * x0b, levelno + 1);
727 band->y0 = int_ceildivpow2(tilec->y0 - (1 << levelno) * y0b, levelno + 1);
728 band->x1 = int_ceildivpow2(tilec->x1 - (1 << levelno) * x0b, levelno + 1);
729 band->y1 = int_ceildivpow2(tilec->y1 - (1 << levelno) * y0b, levelno + 1);
732 ss = &tccp->stepsizes[resno == 0 ? 0 : 3 * (resno - 1) + bandno + 1];
733 gain = tccp->qmfbid == 0 ? dwt_getgain_real(band->bandno) : dwt_getgain(band->bandno);
734 numbps = image->comps[compno].prec + gain;
735 band->stepsize = (float)((1.0 + ss->mant / 2048.0) * pow(2.0, numbps - ss->expn));
736 band->numbps = ss->expn + tccp->numgbits - 1; /* WHY -1 ? */
738 band->precincts = (opj_tcd_precinct_t *) opj_malloc(res->pw * res->ph * sizeof(opj_tcd_precinct_t));
740 for (precno = 0; precno < res->pw * res->ph; precno++) {
741 int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
742 int cbgxstart = tlcbgxstart + (precno % res->pw) * (1 << cbgwidthexpn);
743 int cbgystart = tlcbgystart + (precno / res->pw) * (1 << cbgheightexpn);
744 int cbgxend = cbgxstart + (1 << cbgwidthexpn);
745 int cbgyend = cbgystart + (1 << cbgheightexpn);
747 opj_tcd_precinct_t *prc = &band->precincts[precno];
748 /* precinct size (global) */
749 prc->x0 = int_max(cbgxstart, band->x0);
750 prc->y0 = int_max(cbgystart, band->y0);
751 prc->x1 = int_min(cbgxend, band->x1);
752 prc->y1 = int_min(cbgyend, band->y1);
754 tlcblkxstart = int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn;
755 tlcblkystart = int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn;
756 brcblkxend = int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn;
757 brcblkyend = int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn;
758 prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;
759 prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn;
761 prc->cblks = (opj_tcd_cblk_t *) opj_malloc(prc->cw * prc->ch * sizeof(opj_tcd_cblk_t));
763 prc->incltree = tgt_create(prc->cw, prc->ch);
764 prc->imsbtree = tgt_create(prc->cw, prc->ch);
766 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
767 int cblkxstart = tlcblkxstart + (cblkno % prc->cw) * (1 << cblkwidthexpn);
768 int cblkystart = tlcblkystart + (cblkno / prc->cw) * (1 << cblkheightexpn);
769 int cblkxend = cblkxstart + (1 << cblkwidthexpn);
770 int cblkyend = cblkystart + (1 << cblkheightexpn);
772 /* code-block size (global) */
773 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
774 cblk->x0 = int_max(cblkxstart, prc->x0);
775 cblk->y0 = int_max(cblkystart, prc->y0);
776 cblk->x1 = int_min(cblkxend, prc->x1);
777 cblk->y1 = int_min(cblkyend, prc->y1);
783 } /* i = 0..cp->tileno_size */
785 /* tcd_dump(stdout, tcd, &tcd->tcd_image); */
788 Allocate place to store the decoded data = final image
789 Place limited by the tile really present in the codestream
792 for (i = 0; i < image->numcomps; i++) {
793 for (j = 0; j < cp->tileno_size; j++) {
794 tileno = cp->tileno[j];
795 x0 = j == 0 ? tcd->tcd_image->tiles[tileno].comps[i].x0 : int_min(x0,
796 (unsigned int) tcd->tcd_image->tiles[tileno].comps[i].x0);
797 y0 = j == 0 ? tcd->tcd_image->tiles[tileno].comps[i].y0 : int_min(y0,
798 (unsigned int) tcd->tcd_image->tiles[tileno].comps[i].y0);
799 x1 = j == 0 ? tcd->tcd_image->tiles[tileno].comps[i].x1 : int_max(x1,
800 (unsigned int) tcd->tcd_image->tiles[tileno].comps[i].x1);
801 y1 = j == 0 ? tcd->tcd_image->tiles[tileno].comps[i].y1 : int_max(y1,
802 (unsigned int) tcd->tcd_image->tiles[tileno].comps[i].y1);
808 image->comps[i].data = (int *) opj_malloc(w * h * sizeof(int));
809 image->comps[i].w = w;
810 image->comps[i].h = h;
811 image->comps[i].x0 = x0;
812 image->comps[i].y0 = y0;
816 void tcd_makelayer_fixed(opj_tcd_t *tcd, int layno, int final) {
817 int compno, resno, bandno, precno, cblkno;
818 int value; /*, matrice[tcd_tcp->numlayers][tcd_tile->comps[0].numresolutions][3]; */
819 int matrice[10][10][3];
822 opj_cp_t *cp = tcd->cp;
823 opj_tcd_tile_t *tcd_tile = tcd->tcd_tile;
824 opj_tcp_t *tcd_tcp = tcd->tcp;
826 /*matrice=(int*)opj_malloc(tcd_tcp->numlayers*tcd_tile->comps[0].numresolutions*3*sizeof(int)); */
828 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
829 opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
830 for (i = 0; i < tcd_tcp->numlayers; i++) {
831 for (j = 0; j < tilec->numresolutions; j++) {
832 for (k = 0; k < 3; k++) {
834 (int) (cp->matrice[i * tilec->numresolutions * 3 + j * 3 + k]
835 * (float) (tcd->image->comps[compno].prec / 16.0));
840 for (resno = 0; resno < tilec->numresolutions; resno++) {
841 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
842 for (bandno = 0; bandno < res->numbands; bandno++) {
843 opj_tcd_band_t *band = &res->bands[bandno];
844 for (precno = 0; precno < res->pw * res->ph; precno++) {
845 opj_tcd_precinct_t *prc = &band->precincts[precno];
846 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
847 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
848 opj_tcd_layer_t *layer = &cblk->layers[layno];
850 int imsb = tcd->image->comps[compno].prec - cblk->numbps; /* number of bit-plan equal to zero */
851 /* Correction of the matrix of coefficient to include the IMSB information */
853 value = matrice[layno][resno][bandno];
860 value = matrice[layno][resno][bandno] - matrice[layno - 1][resno][bandno];
861 if (imsb >= matrice[layno - 1][resno][bandno]) {
862 value -= (imsb - matrice[layno - 1][resno][bandno]);
870 cblk->numpassesinlayers = 0;
873 n = cblk->numpassesinlayers;
874 if (cblk->numpassesinlayers == 0) {
876 n = 3 * value - 2 + cblk->numpassesinlayers;
878 n = cblk->numpassesinlayers;
881 n = 3 * value + cblk->numpassesinlayers;
884 layer->numpasses = n - cblk->numpassesinlayers;
886 if (!layer->numpasses)
889 if (cblk->numpassesinlayers == 0) {
890 layer->len = cblk->passes[n - 1].rate;
891 layer->data = cblk->data;
893 layer->len = cblk->passes[n - 1].rate - cblk->passes[cblk->numpassesinlayers - 1].rate;
894 layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate;
897 cblk->numpassesinlayers = n;
905 void tcd_rateallocate_fixed(opj_tcd_t *tcd) {
907 for (layno = 0; layno < tcd->tcp->numlayers; layno++) {
908 tcd_makelayer_fixed(tcd, layno, 1);
912 void tcd_makelayer(opj_tcd_t *tcd, int layno, double thresh, int final) {
913 int compno, resno, bandno, precno, cblkno, passno;
915 opj_tcd_tile_t *tcd_tile = tcd->tcd_tile;
917 tcd_tile->distolayer[layno] = 0; /* fixed_quality */
919 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
920 opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
921 for (resno = 0; resno < tilec->numresolutions; resno++) {
922 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
923 for (bandno = 0; bandno < res->numbands; bandno++) {
924 opj_tcd_band_t *band = &res->bands[bandno];
925 for (precno = 0; precno < res->pw * res->ph; precno++) {
926 opj_tcd_precinct_t *prc = &band->precincts[precno];
927 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
928 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
929 opj_tcd_layer_t *layer = &cblk->layers[layno];
933 cblk->numpassesinlayers = 0;
935 n = cblk->numpassesinlayers;
936 for (passno = cblk->numpassesinlayers; passno < cblk->totalpasses; passno++) {
939 opj_tcd_pass_t *pass = &cblk->passes[passno];
942 dd = pass->distortiondec;
944 dr = pass->rate - cblk->passes[n - 1].rate;
945 dd = pass->distortiondec - cblk->passes[n - 1].distortiondec;
952 if (dd / dr >= thresh)
955 layer->numpasses = n - cblk->numpassesinlayers;
957 if (!layer->numpasses) {
961 if (cblk->numpassesinlayers == 0) {
962 layer->len = cblk->passes[n - 1].rate;
963 layer->data = cblk->data;
964 layer->disto = cblk->passes[n - 1].distortiondec;
966 layer->len = cblk->passes[n - 1].rate - cblk->passes[cblk->numpassesinlayers - 1].rate;
967 layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate;
968 layer->disto = cblk->passes[n - 1].distortiondec - cblk->passes[cblk->numpassesinlayers - 1].distortiondec;
971 tcd_tile->distolayer[layno] += layer->disto; /* fixed_quality */
974 cblk->numpassesinlayers = n;
982 bool tcd_rateallocate(opj_tcd_t *tcd, unsigned char *dest, int len, opj_image_info_t * image_info) {
983 int compno, resno, bandno, precno, cblkno, passno, layno;
985 double cumdisto[100]; /* fixed_quality */
986 const double K = 1; /* 1.1; // fixed_quality */
989 opj_cp_t *cp = tcd->cp;
990 opj_tcd_tile_t *tcd_tile = tcd->tcd_tile;
991 opj_tcp_t *tcd_tcp = tcd->tcp;
996 tcd_tile->nbpix = 0; /* fixed_quality */
998 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
999 opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
1001 for (resno = 0; resno < tilec->numresolutions; resno++) {
1002 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
1003 for (bandno = 0; bandno < res->numbands; bandno++) {
1004 opj_tcd_band_t *band = &res->bands[bandno];
1005 for (precno = 0; precno < res->pw * res->ph; precno++) {
1006 opj_tcd_precinct_t *prc = &band->precincts[precno];
1007 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
1008 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
1009 for (passno = 0; passno < cblk->totalpasses; passno++) {
1010 opj_tcd_pass_t *pass = &cblk->passes[passno];
1015 dd = pass->distortiondec;
1017 dr = pass->rate - cblk->passes[passno - 1].rate;
1018 dd = pass->distortiondec - cblk->passes[passno - 1].distortiondec;
1024 if (rdslope < min) {
1027 if (rdslope > max) {
1033 tcd_tile->nbpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0));
1034 tilec->nbpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0));
1040 maxSE += (((double)(1 << tcd->image->comps[compno].prec) - 1.0)
1041 * ((double)(1 << tcd->image->comps[compno].prec) -1.0))
1042 * ((double)(tilec->nbpix));
1045 /* add antonin index */
1046 if(image_info && image_info->index_on) {
1047 opj_tile_info_t *info_TL = &image_info->tile[tcd->tcd_tileno];
1048 info_TL->nbpix = tcd_tile->nbpix;
1049 info_TL->distotile = tcd_tile->distotile;
1050 info_TL->thresh = (double *) opj_malloc(tcd_tcp->numlayers * sizeof(double));
1054 for (layno = 0; layno < tcd_tcp->numlayers; layno++) {
1058 int maxlen = tcd_tcp->rates[layno] ? int_min(tcd_tcp->rates[layno], len) : len;
1059 double goodthresh = 0;
1061 double distotarget; /* fixed_quality */
1064 distotarget = tcd_tile->distotile - ((K * maxSE) / pow((float)10, tcd_tcp->distoratio[layno] / 10));
1066 if ((tcd_tcp->rates[layno]) || (cp->disto_alloc==0)) {
1067 opj_t2_t *t2 = t2_create(tcd->cinfo, tcd->image, cp);
1069 for (i = 0; i < 32; i++) {
1070 double thresh = (lo + hi) / 2;
1072 double distoachieved = 0; /* fixed_quality */
1074 tcd_makelayer(tcd, layno, thresh, 0);
1076 if (cp->fixed_quality) { /* fixed_quality */
1077 distoachieved = layno == 0 ?
1078 tcd_tile->distolayer[0] : cumdisto[layno - 1] + tcd_tile->distolayer[layno];
1079 if (distoachieved < distotarget) {
1085 l = t2_encode_packets(t2, tcd->tcd_tileno, tcd_tile, layno + 1, dest, maxlen, image_info);
1086 /* opj_event_msg(tcd->cinfo, EVT_INFO, "rate alloc: len=%d, max=%d\n", l, maxlen); */
1095 goodthresh = thresh;
1107 if(image_info && image_info->index_on) { /* Threshold for Marcela Index */
1108 image_info->tile[tcd->tcd_tileno].thresh[layno] = goodthresh;
1110 tcd_makelayer(tcd, layno, goodthresh, 1);
1113 cumdisto[layno] = layno == 0 ? tcd_tile->distolayer[0] : cumdisto[layno - 1] + tcd_tile->distolayer[layno];
1119 int tcd_encode_tile(opj_tcd_t *tcd, int tileno, unsigned char *dest, int len, opj_image_info_t * image_info) {
1122 double encoding_time;
1123 opj_tcd_tile_t *tile = NULL;
1124 opj_tcp_t *tcd_tcp = NULL;
1125 opj_cp_t *cp = NULL;
1127 opj_tcp_t *tcp = &tcd->cp->tcps[0];
1128 opj_tccp_t *tccp = &tcp->tccps[0];
1129 opj_image_t *image = tcd->image;
1131 opj_t1_t *t1 = NULL; /* T1 component */
1132 opj_t2_t *t2 = NULL; /* T2 component */
1134 tcd->tcd_tileno = tileno;
1135 tcd->tcd_tile = tcd->tcd_image->tiles;
1136 tcd->tcp = &tcd->cp->tcps[tileno];
1138 tile = tcd->tcd_tile;
1142 /* INDEX >> "Precinct_nb_X et Precinct_nb_Y" */
1143 if(image_info && image_info->index_on) {
1144 opj_tcd_tilecomp_t *tilec_idx = &tile->comps[0]; /* based on component 0 */
1145 for (i = 0; i < tilec_idx->numresolutions; i++) {
1146 opj_tcd_resolution_t *res_idx = &tilec_idx->resolutions[i];
1148 image_info->tile[tileno].pw[i] = res_idx->pw;
1149 image_info->tile[tileno].ph[i] = res_idx->ph;
1151 npck += res_idx->pw * res_idx->ph;
1153 image_info->tile[tileno].pdx[i] = tccp->prcw[i];
1154 image_info->tile[tileno].pdy[i] = tccp->prch[i];
1156 image_info->tile[tileno].packet = (opj_packet_info_t *) opj_malloc(image_info->comp * image_info->layer * npck * sizeof(opj_packet_info_t));
1160 /*---------------TILE-------------------*/
1161 encoding_time = opj_clock(); /* time needed to encode a tile */
1163 for (compno = 0; compno < tile->numcomps; compno++) {
1166 int adjust = image->comps[compno].sgnd ? 0 : 1 << (image->comps[compno].prec - 1);
1167 int offset_x = int_ceildiv(image->x0, image->comps[compno].dx);
1168 int offset_y = int_ceildiv(image->y0, image->comps[compno].dy);
1170 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1171 int tw = tilec->x1 - tilec->x0;
1172 int w = int_ceildiv(image->x1 - image->x0, image->comps[compno].dx);
1174 /* extract tile data */
1176 if (tcd_tcp->tccps[compno].qmfbid == 1) {
1177 for (y = tilec->y0; y < tilec->y1; y++) {
1178 /* start of the src tile scanline */
1179 int *data = &image->comps[compno].data[(tilec->x0 - offset_x) + (y - offset_y) * w];
1180 /* start of the dst tile scanline */
1181 int *tile_data = &tilec->data[(y - tilec->y0) * tw];
1182 for (x = tilec->x0; x < tilec->x1; x++) {
1183 *tile_data++ = *data++ - adjust;
1186 } else if (tcd_tcp->tccps[compno].qmfbid == 0) {
1187 for (y = tilec->y0; y < tilec->y1; y++) {
1188 /* start of the src tile scanline */
1189 int *data = &image->comps[compno].data[(tilec->x0 - offset_x) + (y - offset_y) * w];
1190 /* start of the dst tile scanline */
1191 int *tile_data = &tilec->data[(y - tilec->y0) * tw];
1192 for (x = tilec->x0; x < tilec->x1; x++) {
1193 *tile_data++ = (*data++ - adjust) << 13;
1199 /*----------------MCT-------------------*/
1201 int samples = (tile->comps[0].x1 - tile->comps[0].x0) * (tile->comps[0].y1 - tile->comps[0].y0);
1202 if (tcd_tcp->tccps[0].qmfbid == 0) {
1203 mct_encode_real(tile->comps[0].data, tile->comps[1].data, tile->comps[2].data, samples);
1205 mct_encode(tile->comps[0].data, tile->comps[1].data, tile->comps[2].data, samples);
1209 /*----------------DWT---------------------*/
1211 for (compno = 0; compno < tile->numcomps; compno++) {
1212 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1213 if (tcd_tcp->tccps[compno].qmfbid == 1) {
1215 } else if (tcd_tcp->tccps[compno].qmfbid == 0) {
1216 dwt_encode_real(tilec);
1220 /*------------------TIER1-----------------*/
1221 t1 = t1_create(tcd->cinfo);
1222 t1_encode_cblks(t1, tile, tcd_tcp);
1225 /*-----------RATE-ALLOCATE------------------*/
1229 image_info->index_write = 0;
1231 if (cp->disto_alloc || cp->fixed_quality) { /* fixed_quality */
1232 /* Normal Rate/distortion allocation */
1233 tcd_rateallocate(tcd, dest, len, image_info);
1235 /* Fixed layer allocation */
1236 tcd_rateallocate_fixed(tcd);
1239 /*--------------TIER2------------------*/
1243 image_info->index_write = 1;
1246 t2 = t2_create(tcd->cinfo, image, cp);
1247 l = t2_encode_packets(t2, tileno, tile, tcd_tcp->numlayers, dest, len, image_info);
1250 /*---------------CLEAN-------------------*/
1252 encoding_time = opj_clock() - encoding_time;
1253 opj_event_msg(tcd->cinfo, EVT_INFO, "- tile encoded in %f s\n", encoding_time);
1255 /* cleaning memory */
1256 for (compno = 0; compno < tile->numcomps; compno++) {
1257 tcd->tilec = &tile->comps[compno];
1258 opj_free(tcd->tilec->data);
1264 bool tcd_decode_tile(opj_tcd_t *tcd, unsigned char *src, int len, int tileno) {
1268 double tile_time, t1_time, dwt_time;
1269 opj_tcd_tile_t *tile = NULL;
1271 opj_t1_t *t1 = NULL; /* T1 component */
1272 opj_t2_t *t2 = NULL; /* T2 component */
1274 tcd->tcd_tileno = tileno;
1275 tcd->tcd_tile = &(tcd->tcd_image->tiles[tileno]);
1276 tcd->tcp = &(tcd->cp->tcps[tileno]);
1277 tile = tcd->tcd_tile;
1279 tile_time = opj_clock(); /* time needed to decode a tile */
1280 opj_event_msg(tcd->cinfo, EVT_INFO, "tile %d of %d\n", tileno + 1, tcd->cp->tw * tcd->cp->th);
1282 /*--------------TIER2------------------*/
1284 t2 = t2_create(tcd->cinfo, tcd->image, tcd->cp);
1285 l = t2_decode_packets(t2, src, len, tileno, tile);
1290 opj_event_msg(tcd->cinfo, EVT_ERROR, "tcd_decode: incomplete bistream\n");
1293 /*------------------TIER1-----------------*/
1295 t1_time = opj_clock(); /* time needed to decode a tile */
1296 t1 = t1_create(tcd->cinfo);
1297 t1_decode_cblks(t1, tile, tcd->tcp);
1299 t1_time = opj_clock() - t1_time;
1300 opj_event_msg(tcd->cinfo, EVT_INFO, "- tiers-1 took %f s\n", t1_time);
1302 /*----------------DWT---------------------*/
1304 dwt_time = opj_clock(); /* time needed to decode a tile */
1305 for (compno = 0; compno < tile->numcomps; compno++) {
1306 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1307 if (tcd->cp->reduce != 0) {
1308 tcd->image->comps[compno].resno_decoded =
1309 tile->comps[compno].numresolutions - tcd->cp->reduce - 1;
1312 if (tcd->tcp->tccps[compno].qmfbid == 1) {
1313 dwt_decode(tilec, tilec->numresolutions - 1 - tcd->image->comps[compno].resno_decoded);
1315 dwt_decode_real(tilec, tilec->numresolutions - 1 - tcd->image->comps[compno].resno_decoded);
1318 if (tile->comps[compno].numresolutions > 0) {
1319 tcd->image->comps[compno].factor = tile->comps[compno].numresolutions - (tcd->image->comps[compno].resno_decoded + 1);
1322 dwt_time = opj_clock() - dwt_time;
1323 opj_event_msg(tcd->cinfo, EVT_INFO, "- dwt took %f s\n", dwt_time);
1325 /*----------------MCT-------------------*/
1327 if (tcd->tcp->mct) {
1328 if (tcd->tcp->tccps[0].qmfbid == 1) {
1329 mct_decode(tile->comps[0].data, tile->comps[1].data, tile->comps[2].data,
1330 (tile->comps[0].x1 - tile->comps[0].x0) * (tile->comps[0].y1 - tile->comps[0].y0));
1332 mct_decode_real(tile->comps[0].data, tile->comps[1].data, tile->comps[2].data,
1333 (tile->comps[0].x1 - tile->comps[0].x0) * (tile->comps[0].y1 - tile->comps[0].y0));
1337 /*---------------TILE-------------------*/
1339 for (compno = 0; compno < tile->numcomps; compno++) {
1340 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1341 opj_tcd_resolution_t *res = &tilec->resolutions[tcd->image->comps[compno].resno_decoded];
1342 int adjust = tcd->image->comps[compno].sgnd ? 0 : 1 << (tcd->image->comps[compno].prec - 1);
1343 int min = tcd->image->comps[compno].sgnd ?
1344 -(1 << (tcd->image->comps[compno].prec - 1)) : 0;
1345 int max = tcd->image->comps[compno].sgnd ?
1346 (1 << (tcd->image->comps[compno].prec - 1)) - 1 : (1 << tcd->image->comps[compno].prec) - 1;
1348 int tw = tilec->x1 - tilec->x0;
1349 int w = tcd->image->comps[compno].w;
1352 int offset_x = int_ceildivpow2(tcd->image->comps[compno].x0, tcd->image->comps[compno].factor);
1353 int offset_y = int_ceildivpow2(tcd->image->comps[compno].y0, tcd->image->comps[compno].factor);
1355 for (j = res->y0; j < res->y1; j++) {
1356 for (i = res->x0; i < res->x1; i++) {
1358 float tmp = (float)((tilec->data[i - res->x0 + (j - res->y0) * tw]) / 8192.0);
1360 if (tcd->tcp->tccps[compno].qmfbid == 1) {
1361 v = tilec->data[i - res->x0 + (j - res->y0) * tw];
1363 int tmp2 = ((int) (floor(fabs(tmp)))) + ((int) floor(fabs(tmp*2))%2);
1364 v = ((tmp < 0) ? -tmp2:tmp2);
1368 tcd->image->comps[compno].data[(i - offset_x) + (j - offset_y) * w] = int_clamp(v, min, max);
1373 tile_time = opj_clock() - tile_time; /* time needed to decode a tile */
1374 opj_event_msg(tcd->cinfo, EVT_INFO, "- tile decoded in %f s\n", tile_time);
1376 for (compno = 0; compno < tile->numcomps; compno++) {
1377 opj_free(tcd->tcd_image->tiles[tileno].comps[compno].data);
1378 tcd->tcd_image->tiles[tileno].comps[compno].data = NULL;
1388 void tcd_free_decode(opj_tcd_t *tcd) {
1389 int tileno,compno,resno,bandno,precno;
1391 opj_tcd_image_t *tcd_image = tcd->tcd_image;
1393 for (tileno = 0; tileno < tcd_image->tw * tcd_image->th; tileno++) {
1394 opj_tcd_tile_t *tile = &tcd_image->tiles[tileno];
1395 for (compno = 0; compno < tile->numcomps; compno++) {
1396 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1397 for (resno = 0; resno < tilec->numresolutions; resno++) {
1398 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
1399 for (bandno = 0; bandno < res->numbands; bandno++) {
1400 opj_tcd_band_t *band = &res->bands[bandno];
1401 for (precno = 0; precno < res->ph * res->pw; precno++) {
1402 opj_tcd_precinct_t *prec = &band->precincts[precno];
1403 if (prec->cblks != NULL) opj_free(prec->cblks);
1404 if (prec->imsbtree != NULL) tgt_destroy(prec->imsbtree);
1405 if (prec->incltree != NULL) tgt_destroy(prec->incltree);
1407 if (band->precincts != NULL) opj_free(band->precincts);
1410 if (tilec->resolutions != NULL) opj_free(tilec->resolutions);
1412 if (tile->comps != NULL) opj_free(tile->comps);
1415 if (tcd_image->tiles != NULL) opj_free(tcd_image->tiles);