2 * Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium
3 * Copyright (c) 2002-2007, Professor Benoit Macq
4 * Copyright (c) 2001-2003, David Janssens
5 * Copyright (c) 2002-2003, Yannick Verschueren
6 * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe
7 * Copyright (c) 2005, Herve Drolon, FreeImage Team
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #include "opj_includes.h"
34 void tcd_dump(FILE *fd, opj_tcd_t *tcd, opj_tcd_image_t * img) {
35 int tileno, compno, resno, bandno, precno, cblkno;
37 fprintf(fd, "image {\n");
38 fprintf(fd, " tw=%d, th=%d x0=%d x1=%d y0=%d y1=%d\n",
39 img->tw, img->th, tcd->image->x0, tcd->image->x1, tcd->image->y0, tcd->image->y1);
41 for (tileno = 0; tileno < img->th * img->tw; tileno++) {
42 opj_tcd_tile_t *tile = &tcd->tcd_image->tiles[tileno];
43 fprintf(fd, " tile {\n");
44 fprintf(fd, " x0=%d, y0=%d, x1=%d, y1=%d, numcomps=%d\n",
45 tile->x0, tile->y0, tile->x1, tile->y1, tile->numcomps);
46 for (compno = 0; compno < tile->numcomps; compno++) {
47 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
48 fprintf(fd, " tilec {\n");
50 " x0=%d, y0=%d, x1=%d, y1=%d, numresolutions=%d\n",
51 tilec->x0, tilec->y0, tilec->x1, tilec->y1, tilec->numresolutions);
52 for (resno = 0; resno < tilec->numresolutions; resno++) {
53 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
54 fprintf(fd, "\n res {\n");
56 " x0=%d, y0=%d, x1=%d, y1=%d, pw=%d, ph=%d, numbands=%d\n",
57 res->x0, res->y0, res->x1, res->y1, res->pw, res->ph, res->numbands);
58 for (bandno = 0; bandno < res->numbands; bandno++) {
59 opj_tcd_band_t *band = &res->bands[bandno];
60 fprintf(fd, " band {\n");
62 " x0=%d, y0=%d, x1=%d, y1=%d, stepsize=%f, numbps=%d\n",
63 band->x0, band->y0, band->x1, band->y1, band->stepsize, band->numbps);
64 for (precno = 0; precno < res->pw * res->ph; precno++) {
65 opj_tcd_precinct_t *prec = &band->precincts[precno];
66 fprintf(fd, " prec {\n");
68 " x0=%d, y0=%d, x1=%d, y1=%d, cw=%d, ch=%d\n",
69 prec->x0, prec->y0, prec->x1, prec->y1, prec->cw, prec->ch);
70 for (cblkno = 0; cblkno < prec->cw * prec->ch; cblkno++) {
71 opj_tcd_cblk_t *cblk = &prec->cblks[cblkno];
72 fprintf(fd, " cblk {\n");
74 " x0=%d, y0=%d, x1=%d, y1=%d\n",
75 cblk->x0, cblk->y0, cblk->x1, cblk->y1);
91 /* ----------------------------------------------------------------------- */
94 Create a new TCD handle
96 opj_tcd_t* tcd_create(opj_common_ptr cinfo) {
97 /* create the tcd structure */
98 opj_tcd_t *tcd = (opj_tcd_t*)opj_malloc(sizeof(opj_tcd_t));
101 tcd->tcd_image = (opj_tcd_image_t*)opj_malloc(sizeof(opj_tcd_image_t));
102 if(!tcd->tcd_image) {
111 Destroy a previously created TCD handle
113 void tcd_destroy(opj_tcd_t *tcd) {
115 opj_free(tcd->tcd_image);
120 /* ----------------------------------------------------------------------- */
122 void tcd_malloc_encode(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp, int curtileno) {
123 int tileno, compno, resno, bandno, precno, cblkno;
127 tcd->tcd_image->tw = cp->tw;
128 tcd->tcd_image->th = cp->th;
129 tcd->tcd_image->tiles = (opj_tcd_tile_t *) opj_malloc(sizeof(opj_tcd_tile_t));
131 for (tileno = 0; tileno < 1; tileno++) {
132 opj_tcp_t *tcp = &cp->tcps[curtileno];
135 /* cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
136 int p = curtileno % cp->tw; /* si numerotation matricielle .. */
137 int q = curtileno / cp->tw; /* .. coordonnees de la tile (q,p) q pour ligne et p pour colonne */
139 /* opj_tcd_tile_t *tile=&tcd->tcd_image->tiles[tileno]; */
140 opj_tcd_tile_t *tile = tcd->tcd_image->tiles;
142 /* 4 borders of the tile rescale on the image if necessary */
143 tile->x0 = int_max(cp->tx0 + p * cp->tdx, image->x0);
144 tile->y0 = int_max(cp->ty0 + q * cp->tdy, image->y0);
145 tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, image->x1);
146 tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, image->y1);
147 tile->numcomps = image->numcomps;
148 /* tile->PPT=image->PPT; */
150 /* Modification of the RATE >> */
151 for (j = 0; j < tcp->numlayers; j++) {
152 tcp->rates[j] = tcp->rates[j] ?
153 ((float) (tile->numcomps
154 * (tile->x1 - tile->x0)
155 * (tile->y1 - tile->y0)
156 * image->comps[0].prec))/
157 (tcp->rates[j] * 8 * image->comps[0].dx * image->comps[0].dy)
161 if (j && tcp->rates[j] < tcp->rates[j - 1] + 10) {
162 tcp->rates[j] = tcp->rates[j - 1] + 20;
164 if (!j && tcp->rates[j] < 30)
169 /* << Modification of the RATE */
171 tile->comps = (opj_tcd_tilecomp_t *) opj_malloc(image->numcomps * sizeof(opj_tcd_tilecomp_t));
172 for (compno = 0; compno < tile->numcomps; compno++) {
173 opj_tccp_t *tccp = &tcp->tccps[compno];
175 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
177 /* border of each tile component (global) */
178 tilec->x0 = int_ceildiv(tile->x0, image->comps[compno].dx);
179 tilec->y0 = int_ceildiv(tile->y0, image->comps[compno].dy);
180 tilec->x1 = int_ceildiv(tile->x1, image->comps[compno].dx);
181 tilec->y1 = int_ceildiv(tile->y1, image->comps[compno].dy);
183 tilec->data = (int *) opj_malloc((tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0) * sizeof(int));
184 tilec->numresolutions = tccp->numresolutions;
186 tilec->resolutions = (opj_tcd_resolution_t *) opj_malloc(tilec->numresolutions * sizeof(opj_tcd_resolution_t));
188 for (resno = 0; resno < tilec->numresolutions; resno++) {
190 int levelno = tilec->numresolutions - 1 - resno;
191 int tlprcxstart, tlprcystart, brprcxend, brprcyend;
192 int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend;
193 int cbgwidthexpn, cbgheightexpn;
194 int cblkwidthexpn, cblkheightexpn;
196 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
198 /* border for each resolution level (global) */
199 res->x0 = int_ceildivpow2(tilec->x0, levelno);
200 res->y0 = int_ceildivpow2(tilec->y0, levelno);
201 res->x1 = int_ceildivpow2(tilec->x1, levelno);
202 res->y1 = int_ceildivpow2(tilec->y1, levelno);
204 res->numbands = resno == 0 ? 1 : 3;
205 /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */
206 if (tccp->csty & J2K_CCP_CSTY_PRT) {
207 pdx = tccp->prcw[resno];
208 pdy = tccp->prch[resno];
213 /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
214 tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx;
215 tlprcystart = int_floordivpow2(res->y0, pdy) << pdy;
217 brprcxend = int_ceildivpow2(res->x1, pdx) << pdx;
218 brprcyend = int_ceildivpow2(res->y1, pdy) << pdy;
220 res->pw = (brprcxend - tlprcxstart) >> pdx;
221 res->ph = (brprcyend - tlprcystart) >> pdy;
224 tlcbgxstart = tlprcxstart;
225 tlcbgystart = tlprcystart;
226 brcbgxend = brprcxend;
227 brcbgyend = brprcyend;
231 tlcbgxstart = int_ceildivpow2(tlprcxstart, 1);
232 tlcbgystart = int_ceildivpow2(tlprcystart, 1);
233 brcbgxend = int_ceildivpow2(brprcxend, 1);
234 brcbgyend = int_ceildivpow2(brprcyend, 1);
235 cbgwidthexpn = pdx - 1;
236 cbgheightexpn = pdy - 1;
239 cblkwidthexpn = int_min(tccp->cblkw, cbgwidthexpn);
240 cblkheightexpn = int_min(tccp->cblkh, cbgheightexpn);
242 for (bandno = 0; bandno < res->numbands; bandno++) {
245 opj_stepsize_t *ss = NULL;
247 opj_tcd_band_t *band = &res->bands[bandno];
249 band->bandno = resno == 0 ? 0 : bandno + 1;
250 x0b = (band->bandno == 1) || (band->bandno == 3) ? 1 : 0;
251 y0b = (band->bandno == 2) || (band->bandno == 3) ? 1 : 0;
253 if (band->bandno == 0) {
254 /* band border (global) */
255 band->x0 = int_ceildivpow2(tilec->x0, levelno);
256 band->y0 = int_ceildivpow2(tilec->y0, levelno);
257 band->x1 = int_ceildivpow2(tilec->x1, levelno);
258 band->y1 = int_ceildivpow2(tilec->y1, levelno);
260 /* band border (global) */
261 band->x0 = int_ceildivpow2(tilec->x0 - (1 << levelno) * x0b, levelno + 1);
262 band->y0 = int_ceildivpow2(tilec->y0 - (1 << levelno) * y0b, levelno + 1);
263 band->x1 = int_ceildivpow2(tilec->x1 - (1 << levelno) * x0b, levelno + 1);
264 band->y1 = int_ceildivpow2(tilec->y1 - (1 << levelno) * y0b, levelno + 1);
267 ss = &tccp->stepsizes[resno == 0 ? 0 : 3 * (resno - 1) + bandno + 1];
268 gain = tccp->qmfbid == 0 ? dwt_getgain_real(band->bandno) : dwt_getgain(band->bandno);
269 numbps = image->comps[compno].prec + gain;
271 band->stepsize = (float)((1.0 + ss->mant / 2048.0) * pow(2.0, numbps - ss->expn));
272 band->numbps = ss->expn + tccp->numgbits - 1; /* WHY -1 ? */
274 band->precincts = (opj_tcd_precinct_t *) opj_malloc(3 * res->pw * res->ph * sizeof(opj_tcd_precinct_t));
276 for (i = 0; i < res->pw * res->ph * 3; i++) {
277 band->precincts[i].imsbtree = NULL;
278 band->precincts[i].incltree = NULL;
281 for (precno = 0; precno < res->pw * res->ph; precno++) {
282 int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
284 int cbgxstart = tlcbgxstart + (precno % res->pw) * (1 << cbgwidthexpn);
285 int cbgystart = tlcbgystart + (precno / res->pw) * (1 << cbgheightexpn);
286 int cbgxend = cbgxstart + (1 << cbgwidthexpn);
287 int cbgyend = cbgystart + (1 << cbgheightexpn);
289 opj_tcd_precinct_t *prc = &band->precincts[precno];
291 /* precinct size (global) */
292 prc->x0 = int_max(cbgxstart, band->x0);
293 prc->y0 = int_max(cbgystart, band->y0);
294 prc->x1 = int_min(cbgxend, band->x1);
295 prc->y1 = int_min(cbgyend, band->y1);
297 tlcblkxstart = int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn;
298 tlcblkystart = int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn;
299 brcblkxend = int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn;
300 brcblkyend = int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn;
301 prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;
302 prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn;
304 prc->cblks = (opj_tcd_cblk_t *) opj_malloc((prc->cw * prc->ch) * sizeof(opj_tcd_cblk_t));
305 prc->incltree = tgt_create(prc->cw, prc->ch);
306 prc->imsbtree = tgt_create(prc->cw, prc->ch);
308 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
309 int cblkxstart = tlcblkxstart + (cblkno % prc->cw) * (1 << cblkwidthexpn);
310 int cblkystart = tlcblkystart + (cblkno / prc->cw) * (1 << cblkheightexpn);
311 int cblkxend = cblkxstart + (1 << cblkwidthexpn);
312 int cblkyend = cblkystart + (1 << cblkheightexpn);
314 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
316 /* code-block size (global) */
317 cblk->x0 = int_max(cblkxstart, prc->x0);
318 cblk->y0 = int_max(cblkystart, prc->y0);
319 cblk->x1 = int_min(cblkxend, prc->x1);
320 cblk->y1 = int_min(cblkyend, prc->y1);
328 /* tcd_dump(stdout, tcd, &tcd->tcd_image); */
331 void tcd_free_encode(opj_tcd_t *tcd) {
332 int tileno, compno, resno, bandno, precno;
334 for (tileno = 0; tileno < 1; tileno++) {
335 opj_tcd_tile_t *tile = tcd->tcd_image->tiles;
337 for (compno = 0; compno < tile->numcomps; compno++) {
338 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
340 for (resno = 0; resno < tilec->numresolutions; resno++) {
341 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
343 for (bandno = 0; bandno < res->numbands; bandno++) {
344 opj_tcd_band_t *band = &res->bands[bandno];
346 for (precno = 0; precno < res->pw * res->ph; precno++) {
347 opj_tcd_precinct_t *prc = &band->precincts[precno];
349 if (prc->incltree != NULL) {
350 tgt_destroy(prc->incltree);
351 prc->incltree = NULL;
353 if (prc->imsbtree != NULL) {
354 tgt_destroy(prc->imsbtree);
355 prc->imsbtree = NULL;
357 opj_free(prc->cblks);
360 opj_free(band->precincts);
361 band->precincts = NULL;
364 opj_free(tilec->resolutions);
365 tilec->resolutions = NULL;
367 opj_free(tile->comps);
370 opj_free(tcd->tcd_image->tiles);
371 tcd->tcd_image->tiles = NULL;
374 void tcd_init_encode(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp, int curtileno) {
375 int tileno, compno, resno, bandno, precno, cblkno;
377 for (tileno = 0; tileno < 1; tileno++) {
378 opj_tcp_t *tcp = &cp->tcps[curtileno];
380 /* cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
381 int p = curtileno % cp->tw;
382 int q = curtileno / cp->tw;
384 opj_tcd_tile_t *tile = tcd->tcd_image->tiles;
386 /* 4 borders of the tile rescale on the image if necessary */
387 tile->x0 = int_max(cp->tx0 + p * cp->tdx, image->x0);
388 tile->y0 = int_max(cp->ty0 + q * cp->tdy, image->y0);
389 tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, image->x1);
390 tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, image->y1);
392 tile->numcomps = image->numcomps;
393 /* tile->PPT=image->PPT; */
395 /* Modification of the RATE >> */
396 for (j = 0; j < tcp->numlayers; j++) {
397 tcp->rates[j] = tcp->rates[j] ?
398 ((float) (tile->numcomps
399 * (tile->x1 - tile->x0)
400 * (tile->y1 - tile->y0)
401 * image->comps[0].prec))/
402 (tcp->rates[j] * 8 * image->comps[0].dx * image->comps[0].dy)
406 if (j && tcp->rates[j] < tcp->rates[j - 1] + 10) {
407 tcp->rates[j] = tcp->rates[j - 1] + 20;
409 if (!j && tcp->rates[j] < 30)
414 /* << Modification of the RATE */
416 /* tile->comps=(opj_tcd_tilecomp_t*)opj_realloc(tile->comps,image->numcomps*sizeof(opj_tcd_tilecomp_t)); */
417 for (compno = 0; compno < tile->numcomps; compno++) {
418 opj_tccp_t *tccp = &tcp->tccps[compno];
420 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
422 /* border of each tile component (global) */
423 tilec->x0 = int_ceildiv(tile->x0, image->comps[compno].dx);
424 tilec->y0 = int_ceildiv(tile->y0, image->comps[compno].dy);
425 tilec->x1 = int_ceildiv(tile->x1, image->comps[compno].dx);
426 tilec->y1 = int_ceildiv(tile->y1, image->comps[compno].dy);
428 tilec->data = (int *) opj_malloc((tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0) * sizeof(int));
429 tilec->numresolutions = tccp->numresolutions;
430 /* tilec->resolutions=(opj_tcd_resolution_t*)opj_realloc(tilec->resolutions,tilec->numresolutions*sizeof(opj_tcd_resolution_t)); */
431 for (resno = 0; resno < tilec->numresolutions; resno++) {
434 int levelno = tilec->numresolutions - 1 - resno;
435 int tlprcxstart, tlprcystart, brprcxend, brprcyend;
436 int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend;
437 int cbgwidthexpn, cbgheightexpn;
438 int cblkwidthexpn, cblkheightexpn;
440 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
442 /* border for each resolution level (global) */
443 res->x0 = int_ceildivpow2(tilec->x0, levelno);
444 res->y0 = int_ceildivpow2(tilec->y0, levelno);
445 res->x1 = int_ceildivpow2(tilec->x1, levelno);
446 res->y1 = int_ceildivpow2(tilec->y1, levelno);
447 res->numbands = resno == 0 ? 1 : 3;
449 /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */
450 if (tccp->csty & J2K_CCP_CSTY_PRT) {
451 pdx = tccp->prcw[resno];
452 pdy = tccp->prch[resno];
457 /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
458 tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx;
459 tlprcystart = int_floordivpow2(res->y0, pdy) << pdy;
460 brprcxend = int_ceildivpow2(res->x1, pdx) << pdx;
461 brprcyend = int_ceildivpow2(res->y1, pdy) << pdy;
463 res->pw = (brprcxend - tlprcxstart) >> pdx;
464 res->ph = (brprcyend - tlprcystart) >> pdy;
467 tlcbgxstart = tlprcxstart;
468 tlcbgystart = tlprcystart;
469 brcbgxend = brprcxend;
470 brcbgyend = brprcyend;
474 tlcbgxstart = int_ceildivpow2(tlprcxstart, 1);
475 tlcbgystart = int_ceildivpow2(tlprcystart, 1);
476 brcbgxend = int_ceildivpow2(brprcxend, 1);
477 brcbgyend = int_ceildivpow2(brprcyend, 1);
478 cbgwidthexpn = pdx - 1;
479 cbgheightexpn = pdy - 1;
482 cblkwidthexpn = int_min(tccp->cblkw, cbgwidthexpn);
483 cblkheightexpn = int_min(tccp->cblkh, cbgheightexpn);
485 for (bandno = 0; bandno < res->numbands; bandno++) {
488 opj_stepsize_t *ss = NULL;
490 opj_tcd_band_t *band = &res->bands[bandno];
492 band->bandno = resno == 0 ? 0 : bandno + 1;
493 x0b = (band->bandno == 1) || (band->bandno == 3) ? 1 : 0;
494 y0b = (band->bandno == 2) || (band->bandno == 3) ? 1 : 0;
496 if (band->bandno == 0) {
498 band->x0 = int_ceildivpow2(tilec->x0, levelno);
499 band->y0 = int_ceildivpow2(tilec->y0, levelno);
500 band->x1 = int_ceildivpow2(tilec->x1, levelno);
501 band->y1 = int_ceildivpow2(tilec->y1, levelno);
503 band->x0 = int_ceildivpow2(tilec->x0 - (1 << levelno) * x0b, levelno + 1);
504 band->y0 = int_ceildivpow2(tilec->y0 - (1 << levelno) * y0b, levelno + 1);
505 band->x1 = int_ceildivpow2(tilec->x1 - (1 << levelno) * x0b, levelno + 1);
506 band->y1 = int_ceildivpow2(tilec->y1 - (1 << levelno) * y0b, levelno + 1);
509 ss = &tccp->stepsizes[resno == 0 ? 0 : 3 * (resno - 1) + bandno + 1];
510 gain = tccp->qmfbid == 0 ? dwt_getgain_real(band->bandno) : dwt_getgain(band->bandno);
511 numbps = image->comps[compno].prec + gain;
512 band->stepsize = (float)((1.0 + ss->mant / 2048.0) * pow(2.0, numbps - ss->expn));
513 band->numbps = ss->expn + tccp->numgbits - 1; /* WHY -1 ? */
515 for (precno = 0; precno < res->pw * res->ph; precno++) {
516 int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
518 int cbgxstart = tlcbgxstart + (precno % res->pw) * (1 << cbgwidthexpn);
519 int cbgystart = tlcbgystart + (precno / res->pw) * (1 << cbgheightexpn);
520 int cbgxend = cbgxstart + (1 << cbgwidthexpn);
521 int cbgyend = cbgystart + (1 << cbgheightexpn);
523 opj_tcd_precinct_t *prc = &band->precincts[precno];
525 /* precinct size (global) */
526 prc->x0 = int_max(cbgxstart, band->x0);
527 prc->y0 = int_max(cbgystart, band->y0);
528 prc->x1 = int_min(cbgxend, band->x1);
529 prc->y1 = int_min(cbgyend, band->y1);
531 tlcblkxstart = int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn;
532 tlcblkystart = int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn;
533 brcblkxend = int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn;
534 brcblkyend = int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn;
535 prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;
536 prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn;
538 opj_free(prc->cblks);
539 prc->cblks = (opj_tcd_cblk_t *) opj_malloc(prc->cw * prc->ch * sizeof(opj_tcd_cblk_t));
541 if (prc->incltree != NULL) {
542 tgt_destroy(prc->incltree);
544 if (prc->imsbtree != NULL) {
545 tgt_destroy(prc->imsbtree);
548 prc->incltree = tgt_create(prc->cw, prc->ch);
549 prc->imsbtree = tgt_create(prc->cw, prc->ch);
551 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
552 int cblkxstart = tlcblkxstart + (cblkno % prc->cw) * (1 << cblkwidthexpn);
553 int cblkystart = tlcblkystart + (cblkno / prc->cw) * (1 << cblkheightexpn);
554 int cblkxend = cblkxstart + (1 << cblkwidthexpn);
555 int cblkyend = cblkystart + (1 << cblkheightexpn);
557 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
559 /* code-block size (global) */
560 cblk->x0 = int_max(cblkxstart, prc->x0);
561 cblk->y0 = int_max(cblkystart, prc->y0);
562 cblk->x1 = int_min(cblkxend, prc->x1);
563 cblk->y1 = int_min(cblkyend, prc->y1);
571 /* tcd_dump(stdout, tcd, &tcd->tcd_image); */
574 void tcd_malloc_decode(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp) {
575 int tileno, compno, resno, bandno, precno, cblkno, i, j, p, q;
576 unsigned int x0 = 0, y0 = 0, x1 = 0, y1 = 0, w, h;
580 tcd->tcd_image->tw = cp->tw;
581 tcd->tcd_image->th = cp->th;
582 tcd->tcd_image->tiles = (opj_tcd_tile_t *) opj_malloc(cp->tw * cp->th * sizeof(opj_tcd_tile_t));
584 for (i = 0; i < cp->tileno_size; i++) {
585 opj_tcp_t *tcp = &(cp->tcps[cp->tileno[i]]);
586 opj_tcd_tile_t *tile = &(tcd->tcd_image->tiles[cp->tileno[i]]);
588 /* cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
589 tileno = cp->tileno[i];
590 p = tileno % cp->tw; /* si numerotation matricielle .. */
591 q = tileno / cp->tw; /* .. coordonnees de la tile (q,p) q pour ligne et p pour colonne */
593 /* 4 borders of the tile rescale on the image if necessary */
594 tile->x0 = int_max(cp->tx0 + p * cp->tdx, image->x0);
595 tile->y0 = int_max(cp->ty0 + q * cp->tdy, image->y0);
596 tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, image->x1);
597 tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, image->y1);
599 tile->numcomps = image->numcomps;
600 tile->comps = (opj_tcd_tilecomp_t *) opj_malloc(image->numcomps * sizeof(opj_tcd_tilecomp_t));
601 for (compno = 0; compno < tile->numcomps; compno++) {
602 opj_tccp_t *tccp = &tcp->tccps[compno];
603 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
605 /* border of each tile component (global) */
606 tilec->x0 = int_ceildiv(tile->x0, image->comps[compno].dx);
607 tilec->y0 = int_ceildiv(tile->y0, image->comps[compno].dy);
608 tilec->x1 = int_ceildiv(tile->x1, image->comps[compno].dx);
609 tilec->y1 = int_ceildiv(tile->y1, image->comps[compno].dy);
611 tilec->data = (int *) opj_malloc((tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0) * sizeof(int));
612 tilec->numresolutions = tccp->numresolutions;
613 tilec->resolutions = (opj_tcd_resolution_t *) opj_malloc(tilec->numresolutions * sizeof(opj_tcd_resolution_t));
615 for (resno = 0; resno < tilec->numresolutions; resno++) {
617 int levelno = tilec->numresolutions - 1 - resno;
618 int tlprcxstart, tlprcystart, brprcxend, brprcyend;
619 int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend;
620 int cbgwidthexpn, cbgheightexpn;
621 int cblkwidthexpn, cblkheightexpn;
623 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
625 /* border for each resolution level (global) */
626 res->x0 = int_ceildivpow2(tilec->x0, levelno);
627 res->y0 = int_ceildivpow2(tilec->y0, levelno);
628 res->x1 = int_ceildivpow2(tilec->x1, levelno);
629 res->y1 = int_ceildivpow2(tilec->y1, levelno);
630 res->numbands = resno == 0 ? 1 : 3;
632 /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */
633 if (tccp->csty & J2K_CCP_CSTY_PRT) {
634 pdx = tccp->prcw[resno];
635 pdy = tccp->prch[resno];
641 /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
642 tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx;
643 tlprcystart = int_floordivpow2(res->y0, pdy) << pdy;
644 brprcxend = int_ceildivpow2(res->x1, pdx) << pdx;
645 brprcyend = int_ceildivpow2(res->y1, pdy) << pdy;
647 res->pw = (res->x0 == res->x1) ? 0 : ((brprcxend - tlprcxstart) >> pdx);
648 res->ph = (res->y0 == res->y1) ? 0 : ((brprcyend - tlprcystart) >> pdy);
651 tlcbgxstart = tlprcxstart;
652 tlcbgystart = tlprcystart;
653 brcbgxend = brprcxend;
654 brcbgyend = brprcyend;
658 tlcbgxstart = int_ceildivpow2(tlprcxstart, 1);
659 tlcbgystart = int_ceildivpow2(tlprcystart, 1);
660 brcbgxend = int_ceildivpow2(brprcxend, 1);
661 brcbgyend = int_ceildivpow2(brprcyend, 1);
662 cbgwidthexpn = pdx - 1;
663 cbgheightexpn = pdy - 1;
666 cblkwidthexpn = int_min(tccp->cblkw, cbgwidthexpn);
667 cblkheightexpn = int_min(tccp->cblkh, cbgheightexpn);
669 for (bandno = 0; bandno < res->numbands; bandno++) {
672 opj_stepsize_t *ss = NULL;
674 opj_tcd_band_t *band = &res->bands[bandno];
675 band->bandno = resno == 0 ? 0 : bandno + 1;
676 x0b = (band->bandno == 1) || (band->bandno == 3) ? 1 : 0;
677 y0b = (band->bandno == 2) || (band->bandno == 3) ? 1 : 0;
679 if (band->bandno == 0) {
680 /* band border (global) */
681 band->x0 = int_ceildivpow2(tilec->x0, levelno);
682 band->y0 = int_ceildivpow2(tilec->y0, levelno);
683 band->x1 = int_ceildivpow2(tilec->x1, levelno);
684 band->y1 = int_ceildivpow2(tilec->y1, levelno);
686 /* band border (global) */
687 band->x0 = int_ceildivpow2(tilec->x0 - (1 << levelno) * x0b, levelno + 1);
688 band->y0 = int_ceildivpow2(tilec->y0 - (1 << levelno) * y0b, levelno + 1);
689 band->x1 = int_ceildivpow2(tilec->x1 - (1 << levelno) * x0b, levelno + 1);
690 band->y1 = int_ceildivpow2(tilec->y1 - (1 << levelno) * y0b, levelno + 1);
693 ss = &tccp->stepsizes[resno == 0 ? 0 : 3 * (resno - 1) + bandno + 1];
694 gain = tccp->qmfbid == 0 ? dwt_getgain_real(band->bandno) : dwt_getgain(band->bandno);
695 numbps = image->comps[compno].prec + gain;
696 band->stepsize = (float)((1.0 + ss->mant / 2048.0) * pow(2.0, numbps - ss->expn));
697 band->numbps = ss->expn + tccp->numgbits - 1; /* WHY -1 ? */
699 band->precincts = (opj_tcd_precinct_t *) opj_malloc(res->pw * res->ph * sizeof(opj_tcd_precinct_t));
701 for (precno = 0; precno < res->pw * res->ph; precno++) {
702 int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
703 int cbgxstart = tlcbgxstart + (precno % res->pw) * (1 << cbgwidthexpn);
704 int cbgystart = tlcbgystart + (precno / res->pw) * (1 << cbgheightexpn);
705 int cbgxend = cbgxstart + (1 << cbgwidthexpn);
706 int cbgyend = cbgystart + (1 << cbgheightexpn);
708 opj_tcd_precinct_t *prc = &band->precincts[precno];
709 /* precinct size (global) */
710 prc->x0 = int_max(cbgxstart, band->x0);
711 prc->y0 = int_max(cbgystart, band->y0);
712 prc->x1 = int_min(cbgxend, band->x1);
713 prc->y1 = int_min(cbgyend, band->y1);
715 tlcblkxstart = int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn;
716 tlcblkystart = int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn;
717 brcblkxend = int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn;
718 brcblkyend = int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn;
719 prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;
720 prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn;
722 prc->cblks = (opj_tcd_cblk_t *) opj_malloc(prc->cw * prc->ch * sizeof(opj_tcd_cblk_t));
724 prc->incltree = tgt_create(prc->cw, prc->ch);
725 prc->imsbtree = tgt_create(prc->cw, prc->ch);
727 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
728 int cblkxstart = tlcblkxstart + (cblkno % prc->cw) * (1 << cblkwidthexpn);
729 int cblkystart = tlcblkystart + (cblkno / prc->cw) * (1 << cblkheightexpn);
730 int cblkxend = cblkxstart + (1 << cblkwidthexpn);
731 int cblkyend = cblkystart + (1 << cblkheightexpn);
733 /* code-block size (global) */
734 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
735 cblk->x0 = int_max(cblkxstart, prc->x0);
736 cblk->y0 = int_max(cblkystart, prc->y0);
737 cblk->x1 = int_min(cblkxend, prc->x1);
738 cblk->y1 = int_min(cblkyend, prc->y1);
744 } /* i = 0..cp->tileno_size */
746 /* tcd_dump(stdout, tcd, &tcd->tcd_image); */
749 Allocate place to store the decoded data = final image
750 Place limited by the tile really present in the codestream
753 for (i = 0; i < image->numcomps; i++) {
754 for (j = 0; j < cp->tileno_size; j++) {
755 tileno = cp->tileno[j];
756 x0 = j == 0 ? tcd->tcd_image->tiles[tileno].comps[i].x0 : int_min(x0,
757 (unsigned int) tcd->tcd_image->tiles[tileno].comps[i].x0);
758 y0 = j == 0 ? tcd->tcd_image->tiles[tileno].comps[i].y0 : int_min(y0,
759 (unsigned int) tcd->tcd_image->tiles[tileno].comps[i].y0);
760 x1 = j == 0 ? tcd->tcd_image->tiles[tileno].comps[i].x1 : int_max(x1,
761 (unsigned int) tcd->tcd_image->tiles[tileno].comps[i].x1);
762 y1 = j == 0 ? tcd->tcd_image->tiles[tileno].comps[i].y1 : int_max(y1,
763 (unsigned int) tcd->tcd_image->tiles[tileno].comps[i].y1);
769 image->comps[i].data = (int *) opj_malloc(w * h * sizeof(int));
770 image->comps[i].w = w;
771 image->comps[i].h = h;
772 image->comps[i].x0 = x0;
773 image->comps[i].y0 = y0;
777 void tcd_makelayer_fixed(opj_tcd_t *tcd, int layno, int final) {
778 int compno, resno, bandno, precno, cblkno;
779 int value; /*, matrice[tcd_tcp->numlayers][tcd_tile->comps[0].numresolutions][3]; */
780 int matrice[10][10][3];
783 opj_cp_t *cp = tcd->cp;
784 opj_tcd_tile_t *tcd_tile = tcd->tcd_tile;
785 opj_tcp_t *tcd_tcp = tcd->tcp;
787 /*matrice=(int*)opj_malloc(tcd_tcp->numlayers*tcd_tile->comps[0].numresolutions*3*sizeof(int)); */
789 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
790 opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
791 for (i = 0; i < tcd_tcp->numlayers; i++) {
792 for (j = 0; j < tilec->numresolutions; j++) {
793 for (k = 0; k < 3; k++) {
795 (int) (cp->matrice[i * tilec->numresolutions * 3 + j * 3 + k]
796 * (float) (tcd->image->comps[compno].prec / 16.0));
801 for (resno = 0; resno < tilec->numresolutions; resno++) {
802 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
803 for (bandno = 0; bandno < res->numbands; bandno++) {
804 opj_tcd_band_t *band = &res->bands[bandno];
805 for (precno = 0; precno < res->pw * res->ph; precno++) {
806 opj_tcd_precinct_t *prc = &band->precincts[precno];
807 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
808 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
809 opj_tcd_layer_t *layer = &cblk->layers[layno];
811 int imsb = tcd->image->comps[compno].prec - cblk->numbps; /* number of bit-plan equal to zero */
812 /* Correction of the matrix of coefficient to include the IMSB information */
814 value = matrice[layno][resno][bandno];
821 value = matrice[layno][resno][bandno] - matrice[layno - 1][resno][bandno];
822 if (imsb >= matrice[layno - 1][resno][bandno]) {
823 value -= (imsb - matrice[layno - 1][resno][bandno]);
831 cblk->numpassesinlayers = 0;
834 n = cblk->numpassesinlayers;
835 if (cblk->numpassesinlayers == 0) {
837 n = 3 * value - 2 + cblk->numpassesinlayers;
839 n = cblk->numpassesinlayers;
842 n = 3 * value + cblk->numpassesinlayers;
845 layer->numpasses = n - cblk->numpassesinlayers;
847 if (!layer->numpasses)
850 if (cblk->numpassesinlayers == 0) {
851 layer->len = cblk->passes[n - 1].rate;
852 layer->data = cblk->data;
854 layer->len = cblk->passes[n - 1].rate - cblk->passes[cblk->numpassesinlayers - 1].rate;
855 layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate;
858 cblk->numpassesinlayers = n;
866 void tcd_rateallocate_fixed(opj_tcd_t *tcd) {
868 for (layno = 0; layno < tcd->tcp->numlayers; layno++) {
869 tcd_makelayer_fixed(tcd, layno, 1);
873 void tcd_makelayer(opj_tcd_t *tcd, int layno, double thresh, int final) {
874 int compno, resno, bandno, precno, cblkno, passno;
876 opj_tcd_tile_t *tcd_tile = tcd->tcd_tile;
878 tcd_tile->distolayer[layno] = 0; /* fixed_quality */
880 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
881 opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
882 for (resno = 0; resno < tilec->numresolutions; resno++) {
883 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
884 for (bandno = 0; bandno < res->numbands; bandno++) {
885 opj_tcd_band_t *band = &res->bands[bandno];
886 for (precno = 0; precno < res->pw * res->ph; precno++) {
887 opj_tcd_precinct_t *prc = &band->precincts[precno];
888 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
889 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
890 opj_tcd_layer_t *layer = &cblk->layers[layno];
894 cblk->numpassesinlayers = 0;
896 n = cblk->numpassesinlayers;
897 for (passno = cblk->numpassesinlayers; passno < cblk->totalpasses; passno++) {
900 opj_tcd_pass_t *pass = &cblk->passes[passno];
903 dd = pass->distortiondec;
905 dr = pass->rate - cblk->passes[n - 1].rate;
906 dd = pass->distortiondec - cblk->passes[n - 1].distortiondec;
913 if (dd / dr >= thresh)
916 layer->numpasses = n - cblk->numpassesinlayers;
918 if (!layer->numpasses) {
922 if (cblk->numpassesinlayers == 0) {
923 layer->len = cblk->passes[n - 1].rate;
924 layer->data = cblk->data;
925 layer->disto = cblk->passes[n - 1].distortiondec;
927 layer->len = cblk->passes[n - 1].rate - cblk->passes[cblk->numpassesinlayers - 1].rate;
928 layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate;
929 layer->disto = cblk->passes[n - 1].distortiondec - cblk->passes[cblk->numpassesinlayers - 1].distortiondec;
932 tcd_tile->distolayer[layno] += layer->disto; /* fixed_quality */
935 cblk->numpassesinlayers = n;
943 bool tcd_rateallocate(opj_tcd_t *tcd, unsigned char *dest, int len, opj_image_info_t * image_info) {
944 int compno, resno, bandno, precno, cblkno, passno, layno;
946 double cumdisto[100]; /* fixed_quality */
947 const double K = 1; /* 1.1; fixed_quality */
950 opj_cp_t *cp = tcd->cp;
951 opj_tcd_tile_t *tcd_tile = tcd->tcd_tile;
952 opj_tcp_t *tcd_tcp = tcd->tcp;
957 tcd_tile->nbpix = 0; /* fixed_quality */
959 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
960 opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
963 for (resno = 0; resno < tilec->numresolutions; resno++) {
964 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
966 for (bandno = 0; bandno < res->numbands; bandno++) {
967 opj_tcd_band_t *band = &res->bands[bandno];
969 for (precno = 0; precno < res->pw * res->ph; precno++) {
970 opj_tcd_precinct_t *prc = &band->precincts[precno];
972 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
973 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
975 for (passno = 0; passno < cblk->totalpasses; passno++) {
976 opj_tcd_pass_t *pass = &cblk->passes[passno];
981 dd = pass->distortiondec;
983 dr = pass->rate - cblk->passes[passno - 1].rate;
984 dd = pass->distortiondec - cblk->passes[passno - 1].distortiondec;
999 tcd_tile->nbpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0));
1000 tilec->nbpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0));
1006 maxSE += (((double)(1 << tcd->image->comps[compno].prec) - 1.0)
1007 * ((double)(1 << tcd->image->comps[compno].prec) -1.0))
1008 * ((double)(tilec->nbpix));
1012 if(image_info && image_info->index_on) {
1013 opj_tile_info_t *tile_info = &image_info->tile[tcd->tcd_tileno];
1014 tile_info->nbpix = tcd_tile->nbpix;
1015 tile_info->distotile = tcd_tile->distotile;
1016 tile_info->thresh = (double *) opj_malloc(tcd_tcp->numlayers * sizeof(double));
1019 for (layno = 0; layno < tcd_tcp->numlayers; layno++) {
1023 /* TODO: remove maxlen */
1024 int maxlen = tcd_tcp->rates[layno] ? int_min(((int) ceil(tcd_tcp->rates[layno])), len) : len;
1025 double goodthresh = 0;
1027 double distotarget; /* fixed_quality */
1030 distotarget = tcd_tile->distotile - ((K * maxSE) / pow((float)10, tcd_tcp->distoratio[layno] / 10));
1032 if ((tcd_tcp->rates[layno]) || (cp->disto_alloc == 0)) {
1033 opj_t2_t *t2 = t2_create(tcd->cinfo, tcd->image, cp);
1035 for (i = 0; i < 32; i++) {
1036 double thresh = (lo + hi) / 2;
1038 double distoachieved = 0; /* fixed_quality */
1040 tcd_makelayer(tcd, layno, thresh, 0);
1042 if (cp->fixed_quality) { /* fixed_quality */
1043 distoachieved = (layno == 0) ?
1044 tcd_tile->distolayer[0] : (cumdisto[layno - 1] + tcd_tile->distolayer[layno]);
1045 if (distoachieved < distotarget) {
1051 l = t2_encode_packets(t2, tcd->tcd_tileno, tcd_tile, layno + 1, dest, maxlen, image_info);
1052 /* TODO: what to do with l ??? seek / tell ??? */
1053 /* opj_event_msg(tcd->cinfo, EVT_INFO, "rate alloc: len=%d, max=%d\n", l, maxlen); */
1062 goodthresh = thresh;
1074 if(image_info && image_info->index_on) { /* Threshold for Marcela Index */
1075 image_info->tile[tcd->tcd_tileno].thresh[layno] = goodthresh;
1077 tcd_makelayer(tcd, layno, goodthresh, 1);
1080 cumdisto[layno] = (layno == 0) ? tcd_tile->distolayer[0] : (cumdisto[layno - 1] + tcd_tile->distolayer[layno]);
1086 int tcd_encode_tile(opj_tcd_t *tcd, int tileno, unsigned char *dest, int len, opj_image_info_t * image_info) {
1089 double encoding_time;
1090 opj_tcd_tile_t *tile = NULL;
1091 opj_tcp_t *tcd_tcp = NULL;
1092 opj_cp_t *cp = NULL;
1094 opj_tcp_t *tcp = &tcd->cp->tcps[0];
1095 opj_tccp_t *tccp = &tcp->tccps[0];
1096 opj_image_t *image = tcd->image;
1098 opj_t1_t *t1 = NULL; /* T1 component */
1099 opj_t2_t *t2 = NULL; /* T2 component */
1101 tcd->tcd_tileno = tileno;
1102 tcd->tcd_tile = tcd->tcd_image->tiles;
1103 tcd->tcp = &tcd->cp->tcps[tileno];
1105 tile = tcd->tcd_tile;
1109 /* INDEX >> "Precinct_nb_X et Precinct_nb_Y" */
1110 if(image_info && image_info->index_on) {
1111 opj_tcd_tilecomp_t *tilec_idx = &tile->comps[0]; /* based on component 0 */
1112 for (i = 0; i < tilec_idx->numresolutions; i++) {
1113 opj_tcd_resolution_t *res_idx = &tilec_idx->resolutions[i];
1115 image_info->tile[tileno].pw[i] = res_idx->pw;
1116 image_info->tile[tileno].ph[i] = res_idx->ph;
1118 npck += res_idx->pw * res_idx->ph;
1120 image_info->tile[tileno].pdx[i] = tccp->prcw[i];
1121 image_info->tile[tileno].pdy[i] = tccp->prch[i];
1123 image_info->tile[tileno].packet = (opj_packet_info_t *) opj_malloc(image_info->comp * image_info->layer * npck * sizeof(opj_packet_info_t));
1127 /*---------------TILE-------------------*/
1128 encoding_time = opj_clock(); /* time needed to encode a tile */
1130 for (compno = 0; compno < tile->numcomps; compno++) {
1133 int adjust = image->comps[compno].sgnd ? 0 : 1 << (image->comps[compno].prec - 1);
1134 int offset_x = int_ceildiv(image->x0, image->comps[compno].dx);
1135 int offset_y = int_ceildiv(image->y0, image->comps[compno].dy);
1137 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1138 int tw = tilec->x1 - tilec->x0;
1139 int w = int_ceildiv(image->x1 - image->x0, image->comps[compno].dx);
1141 /* extract tile data */
1143 if (tcd_tcp->tccps[compno].qmfbid == 1) {
1144 for (y = tilec->y0; y < tilec->y1; y++) {
1145 /* start of the src tile scanline */
1146 int *data = &image->comps[compno].data[(tilec->x0 - offset_x) + (y - offset_y) * w];
1147 /* start of the dst tile scanline */
1148 int *tile_data = &tilec->data[(y - tilec->y0) * tw];
1149 for (x = tilec->x0; x < tilec->x1; x++) {
1150 *tile_data++ = *data++ - adjust;
1153 } else if (tcd_tcp->tccps[compno].qmfbid == 0) {
1154 for (y = tilec->y0; y < tilec->y1; y++) {
1155 /* start of the src tile scanline */
1156 int *data = &image->comps[compno].data[(tilec->x0 - offset_x) + (y - offset_y) * w];
1157 /* start of the dst tile scanline */
1158 int *tile_data = &tilec->data[(y - tilec->y0) * tw];
1159 for (x = tilec->x0; x < tilec->x1; x++) {
1160 *tile_data++ = (*data++ - adjust) << 13;
1166 /*----------------MCT-------------------*/
1168 int samples = (tile->comps[0].x1 - tile->comps[0].x0) * (tile->comps[0].y1 - tile->comps[0].y0);
1169 if (tcd_tcp->tccps[0].qmfbid == 0) {
1170 mct_encode_real(tile->comps[0].data, tile->comps[1].data, tile->comps[2].data, samples);
1172 mct_encode(tile->comps[0].data, tile->comps[1].data, tile->comps[2].data, samples);
1176 /*----------------DWT---------------------*/
1178 for (compno = 0; compno < tile->numcomps; compno++) {
1179 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1180 if (tcd_tcp->tccps[compno].qmfbid == 1) {
1182 } else if (tcd_tcp->tccps[compno].qmfbid == 0) {
1183 dwt_encode_real(tilec);
1187 /*------------------TIER1-----------------*/
1188 t1 = t1_create(tcd->cinfo);
1189 t1_encode_cblks(t1, tile, tcd_tcp);
1192 /*-----------RATE-ALLOCATE------------------*/
1196 image_info->index_write = 0;
1198 if (cp->disto_alloc || cp->fixed_quality) { /* fixed_quality */
1199 /* Normal Rate/distortion allocation */
1200 tcd_rateallocate(tcd, dest, len, image_info);
1202 /* Fixed layer allocation */
1203 tcd_rateallocate_fixed(tcd);
1206 /*--------------TIER2------------------*/
1210 image_info->index_write = 1;
1213 t2 = t2_create(tcd->cinfo, image, cp);
1214 l = t2_encode_packets(t2, tileno, tile, tcd_tcp->numlayers, dest, len, image_info);
1217 /*---------------CLEAN-------------------*/
1219 encoding_time = opj_clock() - encoding_time;
1220 opj_event_msg(tcd->cinfo, EVT_INFO, "- tile encoded in %f s\n", encoding_time);
1222 /* cleaning memory */
1223 for (compno = 0; compno < tile->numcomps; compno++) {
1224 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1225 opj_free(tilec->data);
1231 bool tcd_decode_tile(opj_tcd_t *tcd, unsigned char *src, int len, int tileno) {
1235 double tile_time, t1_time, dwt_time;
1236 opj_tcd_tile_t *tile = NULL;
1238 opj_t1_t *t1 = NULL; /* T1 component */
1239 opj_t2_t *t2 = NULL; /* T2 component */
1241 tcd->tcd_tileno = tileno;
1242 tcd->tcd_tile = &(tcd->tcd_image->tiles[tileno]);
1243 tcd->tcp = &(tcd->cp->tcps[tileno]);
1244 tile = tcd->tcd_tile;
1246 tile_time = opj_clock(); /* time needed to decode a tile */
1247 opj_event_msg(tcd->cinfo, EVT_INFO, "tile %d of %d\n", tileno + 1, tcd->cp->tw * tcd->cp->th);
1249 /*--------------TIER2------------------*/
1251 t2 = t2_create(tcd->cinfo, tcd->image, tcd->cp);
1252 l = t2_decode_packets(t2, src, len, tileno, tile);
1257 opj_event_msg(tcd->cinfo, EVT_ERROR, "tcd_decode: incomplete bistream\n");
1260 /*------------------TIER1-----------------*/
1262 t1_time = opj_clock(); /* time needed to decode a tile */
1263 t1 = t1_create(tcd->cinfo);
1264 t1_decode_cblks(t1, tile, tcd->tcp);
1266 t1_time = opj_clock() - t1_time;
1267 opj_event_msg(tcd->cinfo, EVT_INFO, "- tiers-1 took %f s\n", t1_time);
1269 /*----------------DWT---------------------*/
1271 dwt_time = opj_clock(); /* time needed to decode a tile */
1272 for (compno = 0; compno < tile->numcomps; compno++) {
1273 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1274 if (tcd->cp->reduce != 0) {
1275 tcd->image->comps[compno].resno_decoded =
1276 tile->comps[compno].numresolutions - tcd->cp->reduce - 1;
1279 if (tcd->tcp->tccps[compno].qmfbid == 1) {
1280 dwt_decode(tilec, tilec->numresolutions - 1 - tcd->image->comps[compno].resno_decoded);
1282 dwt_decode_real(tilec, tilec->numresolutions - 1 - tcd->image->comps[compno].resno_decoded);
1285 if (tile->comps[compno].numresolutions > 0) {
1286 tcd->image->comps[compno].factor = tile->comps[compno].numresolutions - (tcd->image->comps[compno].resno_decoded + 1);
1289 dwt_time = opj_clock() - dwt_time;
1290 opj_event_msg(tcd->cinfo, EVT_INFO, "- dwt took %f s\n", dwt_time);
1292 /*----------------MCT-------------------*/
1294 if (tcd->tcp->mct) {
1295 if (tcd->tcp->tccps[0].qmfbid == 1) {
1296 mct_decode(tile->comps[0].data, tile->comps[1].data, tile->comps[2].data,
1297 (tile->comps[0].x1 - tile->comps[0].x0) * (tile->comps[0].y1 - tile->comps[0].y0));
1299 mct_decode_real(tile->comps[0].data, tile->comps[1].data, tile->comps[2].data,
1300 (tile->comps[0].x1 - tile->comps[0].x0) * (tile->comps[0].y1 - tile->comps[0].y0));
1304 /*---------------TILE-------------------*/
1306 for (compno = 0; compno < tile->numcomps; compno++) {
1307 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1308 opj_tcd_resolution_t *res = &tilec->resolutions[tcd->image->comps[compno].resno_decoded];
1309 int adjust = tcd->image->comps[compno].sgnd ? 0 : 1 << (tcd->image->comps[compno].prec - 1);
1310 int min = tcd->image->comps[compno].sgnd ?
1311 -(1 << (tcd->image->comps[compno].prec - 1)) : 0;
1312 int max = tcd->image->comps[compno].sgnd ?
1313 (1 << (tcd->image->comps[compno].prec - 1)) - 1 : (1 << tcd->image->comps[compno].prec) - 1;
1315 int tw = tilec->x1 - tilec->x0;
1316 int w = tcd->image->comps[compno].w;
1319 int offset_x = int_ceildivpow2(tcd->image->comps[compno].x0, tcd->image->comps[compno].factor);
1320 int offset_y = int_ceildivpow2(tcd->image->comps[compno].y0, tcd->image->comps[compno].factor);
1322 for (j = res->y0; j < res->y1; j++) {
1323 for (i = res->x0; i < res->x1; i++) {
1325 float tmp = (float)((tilec->data[i - res->x0 + (j - res->y0) * tw]) / 8192.0);
1327 if (tcd->tcp->tccps[compno].qmfbid == 1) {
1328 v = tilec->data[i - res->x0 + (j - res->y0) * tw];
1330 int tmp2 = ((int) (floor(fabs(tmp)))) + ((int) floor(fabs(tmp*2))%2);
1331 v = ((tmp < 0) ? -tmp2:tmp2);
1335 tcd->image->comps[compno].data[(i - offset_x) + (j - offset_y) * w] = int_clamp(v, min, max);
1340 tile_time = opj_clock() - tile_time; /* time needed to decode a tile */
1341 opj_event_msg(tcd->cinfo, EVT_INFO, "- tile decoded in %f s\n", tile_time);
1343 for (compno = 0; compno < tile->numcomps; compno++) {
1344 opj_free(tcd->tcd_image->tiles[tileno].comps[compno].data);
1345 tcd->tcd_image->tiles[tileno].comps[compno].data = NULL;
1355 void tcd_free_decode(opj_tcd_t *tcd) {
1356 int tileno,compno,resno,bandno,precno;
1358 opj_tcd_image_t *tcd_image = tcd->tcd_image;
1360 for (tileno = 0; tileno < tcd_image->tw * tcd_image->th; tileno++) {
1361 opj_tcd_tile_t *tile = &tcd_image->tiles[tileno];
1362 for (compno = 0; compno < tile->numcomps; compno++) {
1363 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1364 for (resno = 0; resno < tilec->numresolutions; resno++) {
1365 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
1366 for (bandno = 0; bandno < res->numbands; bandno++) {
1367 opj_tcd_band_t *band = &res->bands[bandno];
1368 for (precno = 0; precno < res->ph * res->pw; precno++) {
1369 opj_tcd_precinct_t *prec = &band->precincts[precno];
1370 if (prec->cblks != NULL) opj_free(prec->cblks);
1371 if (prec->imsbtree != NULL) tgt_destroy(prec->imsbtree);
1372 if (prec->incltree != NULL) tgt_destroy(prec->incltree);
1374 if (band->precincts != NULL) opj_free(band->precincts);
1377 if (tilec->resolutions != NULL) opj_free(tilec->resolutions);
1379 if (tile->comps != NULL) opj_free(tile->comps);
1382 if (tcd_image->tiles != NULL) opj_free(tcd_image->tiles);