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;
126 tcd->tcd_image->tw = cp->tw;
127 tcd->tcd_image->th = cp->th;
128 tcd->tcd_image->tiles = (opj_tcd_tile_t *) opj_malloc(sizeof(opj_tcd_tile_t));
130 for (tileno = 0; tileno < 1; tileno++) {
131 opj_tcp_t *tcp = &cp->tcps[curtileno];
134 /* cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
135 int p = curtileno % cp->tw; /* si numerotation matricielle .. */
136 int q = curtileno / cp->tw; /* .. coordonnees de la tile (q,p) q pour ligne et p pour colonne */
138 /* opj_tcd_tile_t *tile=&tcd->tcd_image->tiles[tileno]; */
139 opj_tcd_tile_t *tile = tcd->tcd_image->tiles;
141 /* 4 borders of the tile rescale on the image if necessary */
142 tile->x0 = int_max(cp->tx0 + p * cp->tdx, image->x0);
143 tile->y0 = int_max(cp->ty0 + q * cp->tdy, image->y0);
144 tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, image->x1);
145 tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, image->y1);
146 tile->numcomps = image->numcomps;
147 /* tile->PPT=image->PPT; */
149 /* Modification of the RATE >> */
150 for (j = 0; j < tcp->numlayers; j++) {
151 tcp->rates[j] = tcp->rates[j] ?
152 ((float) (tile->numcomps
153 * (tile->x1 - tile->x0)
154 * (tile->y1 - tile->y0)
155 * image->comps[0].prec))/
156 (tcp->rates[j] * 8 * image->comps[0].dx * image->comps[0].dy)
160 if (j && tcp->rates[j] < tcp->rates[j - 1] + 10) {
161 tcp->rates[j] = tcp->rates[j - 1] + 20;
163 if (!j && tcp->rates[j] < 30)
168 /* << Modification of the RATE */
170 tile->comps = (opj_tcd_tilecomp_t *) opj_malloc(image->numcomps * sizeof(opj_tcd_tilecomp_t));
171 for (compno = 0; compno < tile->numcomps; compno++) {
172 opj_tccp_t *tccp = &tcp->tccps[compno];
174 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
176 /* border of each tile component (global) */
177 tilec->x0 = int_ceildiv(tile->x0, image->comps[compno].dx);
178 tilec->y0 = int_ceildiv(tile->y0, image->comps[compno].dy);
179 tilec->x1 = int_ceildiv(tile->x1, image->comps[compno].dx);
180 tilec->y1 = int_ceildiv(tile->y1, image->comps[compno].dy);
182 tilec->data = (int *) opj_malloc((tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0) * sizeof(int));
183 tilec->numresolutions = tccp->numresolutions;
185 tilec->resolutions = (opj_tcd_resolution_t *) opj_malloc(tilec->numresolutions * sizeof(opj_tcd_resolution_t));
187 for (resno = 0; resno < tilec->numresolutions; resno++) {
189 int levelno = tilec->numresolutions - 1 - resno;
190 int tlprcxstart, tlprcystart, brprcxend, brprcyend;
191 int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend;
192 int cbgwidthexpn, cbgheightexpn;
193 int cblkwidthexpn, cblkheightexpn;
195 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
197 /* border for each resolution level (global) */
198 res->x0 = int_ceildivpow2(tilec->x0, levelno);
199 res->y0 = int_ceildivpow2(tilec->y0, levelno);
200 res->x1 = int_ceildivpow2(tilec->x1, levelno);
201 res->y1 = int_ceildivpow2(tilec->y1, levelno);
203 res->numbands = resno == 0 ? 1 : 3;
204 /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */
205 if (tccp->csty & J2K_CCP_CSTY_PRT) {
206 pdx = tccp->prcw[resno];
207 pdy = tccp->prch[resno];
212 /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
213 tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx;
214 tlprcystart = int_floordivpow2(res->y0, pdy) << pdy;
216 brprcxend = int_ceildivpow2(res->x1, pdx) << pdx;
217 brprcyend = int_ceildivpow2(res->y1, pdy) << pdy;
219 res->pw = (brprcxend - tlprcxstart) >> pdx;
220 res->ph = (brprcyend - tlprcystart) >> pdy;
223 tlcbgxstart = tlprcxstart;
224 tlcbgystart = tlprcystart;
225 brcbgxend = brprcxend;
226 brcbgyend = brprcyend;
230 tlcbgxstart = int_ceildivpow2(tlprcxstart, 1);
231 tlcbgystart = int_ceildivpow2(tlprcystart, 1);
232 brcbgxend = int_ceildivpow2(brprcxend, 1);
233 brcbgyend = int_ceildivpow2(brprcyend, 1);
234 cbgwidthexpn = pdx - 1;
235 cbgheightexpn = pdy - 1;
238 cblkwidthexpn = int_min(tccp->cblkw, cbgwidthexpn);
239 cblkheightexpn = int_min(tccp->cblkh, cbgheightexpn);
241 for (bandno = 0; bandno < res->numbands; bandno++) {
244 opj_stepsize_t *ss = NULL;
246 opj_tcd_band_t *band = &res->bands[bandno];
248 band->bandno = resno == 0 ? 0 : bandno + 1;
249 x0b = (band->bandno == 1) || (band->bandno == 3) ? 1 : 0;
250 y0b = (band->bandno == 2) || (band->bandno == 3) ? 1 : 0;
252 if (band->bandno == 0) {
253 /* band border (global) */
254 band->x0 = int_ceildivpow2(tilec->x0, levelno);
255 band->y0 = int_ceildivpow2(tilec->y0, levelno);
256 band->x1 = int_ceildivpow2(tilec->x1, levelno);
257 band->y1 = int_ceildivpow2(tilec->y1, levelno);
259 /* band border (global) */
260 band->x0 = int_ceildivpow2(tilec->x0 - (1 << levelno) * x0b, levelno + 1);
261 band->y0 = int_ceildivpow2(tilec->y0 - (1 << levelno) * y0b, levelno + 1);
262 band->x1 = int_ceildivpow2(tilec->x1 - (1 << levelno) * x0b, levelno + 1);
263 band->y1 = int_ceildivpow2(tilec->y1 - (1 << levelno) * y0b, levelno + 1);
266 ss = &tccp->stepsizes[resno == 0 ? 0 : 3 * (resno - 1) + bandno + 1];
267 gain = tccp->qmfbid == 0 ? dwt_getgain_real(band->bandno) : dwt_getgain(band->bandno);
268 numbps = image->comps[compno].prec + gain;
270 band->stepsize = (float)((1.0 + ss->mant / 2048.0) * pow(2.0, numbps - ss->expn));
271 band->numbps = ss->expn + tccp->numgbits - 1; /* WHY -1 ? */
273 band->precincts = (opj_tcd_precinct_t *) opj_malloc(3 * res->pw * res->ph * sizeof(opj_tcd_precinct_t));
275 for (i = 0; i < res->pw * res->ph * 3; i++) {
276 band->precincts[i].imsbtree = NULL;
277 band->precincts[i].incltree = NULL;
280 for (precno = 0; precno < res->pw * res->ph; precno++) {
281 int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
283 int cbgxstart = tlcbgxstart + (precno % res->pw) * (1 << cbgwidthexpn);
284 int cbgystart = tlcbgystart + (precno / res->pw) * (1 << cbgheightexpn);
285 int cbgxend = cbgxstart + (1 << cbgwidthexpn);
286 int cbgyend = cbgystart + (1 << cbgheightexpn);
288 opj_tcd_precinct_t *prc = &band->precincts[precno];
290 /* precinct size (global) */
291 prc->x0 = int_max(cbgxstart, band->x0);
292 prc->y0 = int_max(cbgystart, band->y0);
293 prc->x1 = int_min(cbgxend, band->x1);
294 prc->y1 = int_min(cbgyend, band->y1);
296 tlcblkxstart = int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn;
297 tlcblkystart = int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn;
298 brcblkxend = int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn;
299 brcblkyend = int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn;
300 prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;
301 prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn;
303 prc->cblks = (opj_tcd_cblk_t *) opj_malloc((prc->cw * prc->ch) * sizeof(opj_tcd_cblk_t));
304 prc->incltree = tgt_create(prc->cw, prc->ch);
305 prc->imsbtree = tgt_create(prc->cw, prc->ch);
307 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
308 int cblkxstart = tlcblkxstart + (cblkno % prc->cw) * (1 << cblkwidthexpn);
309 int cblkystart = tlcblkystart + (cblkno / prc->cw) * (1 << cblkheightexpn);
310 int cblkxend = cblkxstart + (1 << cblkwidthexpn);
311 int cblkyend = cblkystart + (1 << cblkheightexpn);
313 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
315 /* code-block size (global) */
316 cblk->x0 = int_max(cblkxstart, prc->x0);
317 cblk->y0 = int_max(cblkystart, prc->y0);
318 cblk->x1 = int_min(cblkxend, prc->x1);
319 cblk->y1 = int_min(cblkyend, prc->y1);
327 /* tcd_dump(stdout, tcd, &tcd->tcd_image); */
330 void tcd_free_encode(opj_tcd_t *tcd) {
331 int tileno, compno, resno, bandno, precno;
333 for (tileno = 0; tileno < 1; tileno++) {
334 opj_tcd_tile_t *tile = tcd->tcd_image->tiles;
336 for (compno = 0; compno < tile->numcomps; compno++) {
337 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
339 for (resno = 0; resno < tilec->numresolutions; resno++) {
340 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
342 for (bandno = 0; bandno < res->numbands; bandno++) {
343 opj_tcd_band_t *band = &res->bands[bandno];
345 for (precno = 0; precno < res->pw * res->ph; precno++) {
346 opj_tcd_precinct_t *prc = &band->precincts[precno];
348 if (prc->incltree != NULL) {
349 tgt_destroy(prc->incltree);
350 prc->incltree = NULL;
352 if (prc->imsbtree != NULL) {
353 tgt_destroy(prc->imsbtree);
354 prc->imsbtree = NULL;
356 opj_free(prc->cblks);
359 opj_free(band->precincts);
360 band->precincts = NULL;
363 opj_free(tilec->resolutions);
364 tilec->resolutions = NULL;
366 opj_free(tile->comps);
369 opj_free(tcd->tcd_image->tiles);
370 tcd->tcd_image->tiles = NULL;
373 void tcd_init_encode(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp, int curtileno) {
374 int tileno, compno, resno, bandno, precno, cblkno;
376 for (tileno = 0; tileno < 1; tileno++) {
377 opj_tcp_t *tcp = &cp->tcps[curtileno];
379 /* cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
380 int p = curtileno % cp->tw;
381 int q = curtileno / cp->tw;
383 opj_tcd_tile_t *tile = tcd->tcd_image->tiles;
385 /* 4 borders of the tile rescale on the image if necessary */
386 tile->x0 = int_max(cp->tx0 + p * cp->tdx, image->x0);
387 tile->y0 = int_max(cp->ty0 + q * cp->tdy, image->y0);
388 tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, image->x1);
389 tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, image->y1);
391 tile->numcomps = image->numcomps;
392 /* tile->PPT=image->PPT; */
394 /* Modification of the RATE >> */
395 for (j = 0; j < tcp->numlayers; j++) {
396 tcp->rates[j] = tcp->rates[j] ?
397 ((float) (tile->numcomps
398 * (tile->x1 - tile->x0)
399 * (tile->y1 - tile->y0)
400 * image->comps[0].prec))/
401 (tcp->rates[j] * 8 * image->comps[0].dx * image->comps[0].dy)
405 if (j && tcp->rates[j] < tcp->rates[j - 1] + 10) {
406 tcp->rates[j] = tcp->rates[j - 1] + 20;
408 if (!j && tcp->rates[j] < 30)
413 /* << Modification of the RATE */
415 /* tile->comps=(opj_tcd_tilecomp_t*)opj_realloc(tile->comps,image->numcomps*sizeof(opj_tcd_tilecomp_t)); */
416 for (compno = 0; compno < tile->numcomps; compno++) {
417 opj_tccp_t *tccp = &tcp->tccps[compno];
419 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
421 /* border of each tile component (global) */
422 tilec->x0 = int_ceildiv(tile->x0, image->comps[compno].dx);
423 tilec->y0 = int_ceildiv(tile->y0, image->comps[compno].dy);
424 tilec->x1 = int_ceildiv(tile->x1, image->comps[compno].dx);
425 tilec->y1 = int_ceildiv(tile->y1, image->comps[compno].dy);
427 tilec->data = (int *) opj_malloc((tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0) * sizeof(int));
428 tilec->numresolutions = tccp->numresolutions;
429 /* tilec->resolutions=(opj_tcd_resolution_t*)opj_realloc(tilec->resolutions,tilec->numresolutions*sizeof(opj_tcd_resolution_t)); */
430 for (resno = 0; resno < tilec->numresolutions; resno++) {
433 int levelno = tilec->numresolutions - 1 - resno;
434 int tlprcxstart, tlprcystart, brprcxend, brprcyend;
435 int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend;
436 int cbgwidthexpn, cbgheightexpn;
437 int cblkwidthexpn, cblkheightexpn;
439 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
441 /* border for each resolution level (global) */
442 res->x0 = int_ceildivpow2(tilec->x0, levelno);
443 res->y0 = int_ceildivpow2(tilec->y0, levelno);
444 res->x1 = int_ceildivpow2(tilec->x1, levelno);
445 res->y1 = int_ceildivpow2(tilec->y1, levelno);
446 res->numbands = resno == 0 ? 1 : 3;
448 /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */
449 if (tccp->csty & J2K_CCP_CSTY_PRT) {
450 pdx = tccp->prcw[resno];
451 pdy = tccp->prch[resno];
456 /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
457 tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx;
458 tlprcystart = int_floordivpow2(res->y0, pdy) << pdy;
459 brprcxend = int_ceildivpow2(res->x1, pdx) << pdx;
460 brprcyend = int_ceildivpow2(res->y1, pdy) << pdy;
462 res->pw = (brprcxend - tlprcxstart) >> pdx;
463 res->ph = (brprcyend - tlprcystart) >> pdy;
466 tlcbgxstart = tlprcxstart;
467 tlcbgystart = tlprcystart;
468 brcbgxend = brprcxend;
469 brcbgyend = brprcyend;
473 tlcbgxstart = int_ceildivpow2(tlprcxstart, 1);
474 tlcbgystart = int_ceildivpow2(tlprcystart, 1);
475 brcbgxend = int_ceildivpow2(brprcxend, 1);
476 brcbgyend = int_ceildivpow2(brprcyend, 1);
477 cbgwidthexpn = pdx - 1;
478 cbgheightexpn = pdy - 1;
481 cblkwidthexpn = int_min(tccp->cblkw, cbgwidthexpn);
482 cblkheightexpn = int_min(tccp->cblkh, cbgheightexpn);
484 for (bandno = 0; bandno < res->numbands; bandno++) {
487 opj_stepsize_t *ss = NULL;
489 opj_tcd_band_t *band = &res->bands[bandno];
491 band->bandno = resno == 0 ? 0 : bandno + 1;
492 x0b = (band->bandno == 1) || (band->bandno == 3) ? 1 : 0;
493 y0b = (band->bandno == 2) || (band->bandno == 3) ? 1 : 0;
495 if (band->bandno == 0) {
497 band->x0 = int_ceildivpow2(tilec->x0, levelno);
498 band->y0 = int_ceildivpow2(tilec->y0, levelno);
499 band->x1 = int_ceildivpow2(tilec->x1, levelno);
500 band->y1 = int_ceildivpow2(tilec->y1, levelno);
502 band->x0 = int_ceildivpow2(tilec->x0 - (1 << levelno) * x0b, levelno + 1);
503 band->y0 = int_ceildivpow2(tilec->y0 - (1 << levelno) * y0b, levelno + 1);
504 band->x1 = int_ceildivpow2(tilec->x1 - (1 << levelno) * x0b, levelno + 1);
505 band->y1 = int_ceildivpow2(tilec->y1 - (1 << levelno) * y0b, levelno + 1);
508 ss = &tccp->stepsizes[resno == 0 ? 0 : 3 * (resno - 1) + bandno + 1];
509 gain = tccp->qmfbid == 0 ? dwt_getgain_real(band->bandno) : dwt_getgain(band->bandno);
510 numbps = image->comps[compno].prec + gain;
511 band->stepsize = (float)((1.0 + ss->mant / 2048.0) * pow(2.0, numbps - ss->expn));
512 band->numbps = ss->expn + tccp->numgbits - 1; /* WHY -1 ? */
514 for (precno = 0; precno < res->pw * res->ph; precno++) {
515 int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
517 int cbgxstart = tlcbgxstart + (precno % res->pw) * (1 << cbgwidthexpn);
518 int cbgystart = tlcbgystart + (precno / res->pw) * (1 << cbgheightexpn);
519 int cbgxend = cbgxstart + (1 << cbgwidthexpn);
520 int cbgyend = cbgystart + (1 << cbgheightexpn);
522 opj_tcd_precinct_t *prc = &band->precincts[precno];
524 /* precinct size (global) */
525 prc->x0 = int_max(cbgxstart, band->x0);
526 prc->y0 = int_max(cbgystart, band->y0);
527 prc->x1 = int_min(cbgxend, band->x1);
528 prc->y1 = int_min(cbgyend, band->y1);
530 tlcblkxstart = int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn;
531 tlcblkystart = int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn;
532 brcblkxend = int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn;
533 brcblkyend = int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn;
534 prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;
535 prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn;
537 opj_free(prc->cblks);
538 prc->cblks = (opj_tcd_cblk_t *) opj_malloc(prc->cw * prc->ch * sizeof(opj_tcd_cblk_t));
540 if (prc->incltree != NULL) {
541 tgt_destroy(prc->incltree);
543 if (prc->imsbtree != NULL) {
544 tgt_destroy(prc->imsbtree);
547 prc->incltree = tgt_create(prc->cw, prc->ch);
548 prc->imsbtree = tgt_create(prc->cw, prc->ch);
550 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
551 int cblkxstart = tlcblkxstart + (cblkno % prc->cw) * (1 << cblkwidthexpn);
552 int cblkystart = tlcblkystart + (cblkno / prc->cw) * (1 << cblkheightexpn);
553 int cblkxend = cblkxstart + (1 << cblkwidthexpn);
554 int cblkyend = cblkystart + (1 << cblkheightexpn);
556 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
558 /* code-block size (global) */
559 cblk->x0 = int_max(cblkxstart, prc->x0);
560 cblk->y0 = int_max(cblkystart, prc->y0);
561 cblk->x1 = int_min(cblkxend, prc->x1);
562 cblk->y1 = int_min(cblkyend, prc->y1);
570 /* tcd_dump(stdout, tcd, &tcd->tcd_image); */
573 void tcd_malloc_decode(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp) {
574 int tileno, compno, resno, bandno, precno, cblkno, i, j, p, q;
575 unsigned int x0 = 0, y0 = 0, x1 = 0, y1 = 0, w, h;
579 tcd->tcd_image->tw = cp->tw;
580 tcd->tcd_image->th = cp->th;
581 tcd->tcd_image->tiles = (opj_tcd_tile_t *) opj_malloc(cp->tw * cp->th * sizeof(opj_tcd_tile_t));
583 for (i = 0; i < cp->tileno_size; i++) {
584 opj_tcp_t *tcp = &(cp->tcps[cp->tileno[i]]);
585 opj_tcd_tile_t *tile = &(tcd->tcd_image->tiles[cp->tileno[i]]);
587 /* cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
588 tileno = cp->tileno[i];
589 p = tileno % cp->tw; /* si numerotation matricielle .. */
590 q = tileno / cp->tw; /* .. coordonnees de la tile (q,p) q pour ligne et p pour colonne */
592 /* 4 borders of the tile rescale on the image if necessary */
593 tile->x0 = int_max(cp->tx0 + p * cp->tdx, image->x0);
594 tile->y0 = int_max(cp->ty0 + q * cp->tdy, image->y0);
595 tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, image->x1);
596 tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, image->y1);
598 tile->numcomps = image->numcomps;
599 tile->comps = (opj_tcd_tilecomp_t *) opj_malloc(image->numcomps * sizeof(opj_tcd_tilecomp_t));
600 for (compno = 0; compno < tile->numcomps; compno++) {
601 opj_tccp_t *tccp = &tcp->tccps[compno];
602 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
604 /* border of each tile component (global) */
605 tilec->x0 = int_ceildiv(tile->x0, image->comps[compno].dx);
606 tilec->y0 = int_ceildiv(tile->y0, image->comps[compno].dy);
607 tilec->x1 = int_ceildiv(tile->x1, image->comps[compno].dx);
608 tilec->y1 = int_ceildiv(tile->y1, image->comps[compno].dy);
610 tilec->data = (int *) opj_malloc((tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0) * sizeof(int));
611 tilec->numresolutions = tccp->numresolutions;
612 tilec->resolutions = (opj_tcd_resolution_t *) opj_malloc(tilec->numresolutions * sizeof(opj_tcd_resolution_t));
614 for (resno = 0; resno < tilec->numresolutions; resno++) {
616 int levelno = tilec->numresolutions - 1 - resno;
617 int tlprcxstart, tlprcystart, brprcxend, brprcyend;
618 int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend;
619 int cbgwidthexpn, cbgheightexpn;
620 int cblkwidthexpn, cblkheightexpn;
622 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
624 /* border for each resolution level (global) */
625 res->x0 = int_ceildivpow2(tilec->x0, levelno);
626 res->y0 = int_ceildivpow2(tilec->y0, levelno);
627 res->x1 = int_ceildivpow2(tilec->x1, levelno);
628 res->y1 = int_ceildivpow2(tilec->y1, levelno);
629 res->numbands = resno == 0 ? 1 : 3;
631 /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */
632 if (tccp->csty & J2K_CCP_CSTY_PRT) {
633 pdx = tccp->prcw[resno];
634 pdy = tccp->prch[resno];
640 /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
641 tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx;
642 tlprcystart = int_floordivpow2(res->y0, pdy) << pdy;
643 brprcxend = int_ceildivpow2(res->x1, pdx) << pdx;
644 brprcyend = int_ceildivpow2(res->y1, pdy) << pdy;
646 res->pw = (res->x0 == res->x1) ? 0 : ((brprcxend - tlprcxstart) >> pdx);
647 res->ph = (res->y0 == res->y1) ? 0 : ((brprcyend - tlprcystart) >> pdy);
650 tlcbgxstart = tlprcxstart;
651 tlcbgystart = tlprcystart;
652 brcbgxend = brprcxend;
653 brcbgyend = brprcyend;
657 tlcbgxstart = int_ceildivpow2(tlprcxstart, 1);
658 tlcbgystart = int_ceildivpow2(tlprcystart, 1);
659 brcbgxend = int_ceildivpow2(brprcxend, 1);
660 brcbgyend = int_ceildivpow2(brprcyend, 1);
661 cbgwidthexpn = pdx - 1;
662 cbgheightexpn = pdy - 1;
665 cblkwidthexpn = int_min(tccp->cblkw, cbgwidthexpn);
666 cblkheightexpn = int_min(tccp->cblkh, cbgheightexpn);
668 for (bandno = 0; bandno < res->numbands; bandno++) {
671 opj_stepsize_t *ss = NULL;
673 opj_tcd_band_t *band = &res->bands[bandno];
674 band->bandno = resno == 0 ? 0 : bandno + 1;
675 x0b = (band->bandno == 1) || (band->bandno == 3) ? 1 : 0;
676 y0b = (band->bandno == 2) || (band->bandno == 3) ? 1 : 0;
678 if (band->bandno == 0) {
679 /* band border (global) */
680 band->x0 = int_ceildivpow2(tilec->x0, levelno);
681 band->y0 = int_ceildivpow2(tilec->y0, levelno);
682 band->x1 = int_ceildivpow2(tilec->x1, levelno);
683 band->y1 = int_ceildivpow2(tilec->y1, levelno);
685 /* band border (global) */
686 band->x0 = int_ceildivpow2(tilec->x0 - (1 << levelno) * x0b, levelno + 1);
687 band->y0 = int_ceildivpow2(tilec->y0 - (1 << levelno) * y0b, levelno + 1);
688 band->x1 = int_ceildivpow2(tilec->x1 - (1 << levelno) * x0b, levelno + 1);
689 band->y1 = int_ceildivpow2(tilec->y1 - (1 << levelno) * y0b, levelno + 1);
692 ss = &tccp->stepsizes[resno == 0 ? 0 : 3 * (resno - 1) + bandno + 1];
693 gain = tccp->qmfbid == 0 ? dwt_getgain_real(band->bandno) : dwt_getgain(band->bandno);
694 numbps = image->comps[compno].prec + gain;
695 band->stepsize = (float)((1.0 + ss->mant / 2048.0) * pow(2.0, numbps - ss->expn));
696 band->numbps = ss->expn + tccp->numgbits - 1; /* WHY -1 ? */
698 band->precincts = (opj_tcd_precinct_t *) opj_malloc(res->pw * res->ph * sizeof(opj_tcd_precinct_t));
700 for (precno = 0; precno < res->pw * res->ph; precno++) {
701 int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
702 int cbgxstart = tlcbgxstart + (precno % res->pw) * (1 << cbgwidthexpn);
703 int cbgystart = tlcbgystart + (precno / res->pw) * (1 << cbgheightexpn);
704 int cbgxend = cbgxstart + (1 << cbgwidthexpn);
705 int cbgyend = cbgystart + (1 << cbgheightexpn);
707 opj_tcd_precinct_t *prc = &band->precincts[precno];
708 /* precinct size (global) */
709 prc->x0 = int_max(cbgxstart, band->x0);
710 prc->y0 = int_max(cbgystart, band->y0);
711 prc->x1 = int_min(cbgxend, band->x1);
712 prc->y1 = int_min(cbgyend, band->y1);
714 tlcblkxstart = int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn;
715 tlcblkystart = int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn;
716 brcblkxend = int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn;
717 brcblkyend = int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn;
718 prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;
719 prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn;
721 prc->cblks = (opj_tcd_cblk_t *) opj_malloc(prc->cw * prc->ch * sizeof(opj_tcd_cblk_t));
723 prc->incltree = tgt_create(prc->cw, prc->ch);
724 prc->imsbtree = tgt_create(prc->cw, prc->ch);
726 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
727 int cblkxstart = tlcblkxstart + (cblkno % prc->cw) * (1 << cblkwidthexpn);
728 int cblkystart = tlcblkystart + (cblkno / prc->cw) * (1 << cblkheightexpn);
729 int cblkxend = cblkxstart + (1 << cblkwidthexpn);
730 int cblkyend = cblkystart + (1 << cblkheightexpn);
732 /* code-block size (global) */
733 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
734 cblk->x0 = int_max(cblkxstart, prc->x0);
735 cblk->y0 = int_max(cblkystart, prc->y0);
736 cblk->x1 = int_min(cblkxend, prc->x1);
737 cblk->y1 = int_min(cblkyend, prc->y1);
743 } /* i = 0..cp->tileno_size */
745 /* tcd_dump(stdout, tcd, &tcd->tcd_image); */
748 Allocate place to store the decoded data = final image
749 Place limited by the tile really present in the codestream
752 for (i = 0; i < image->numcomps; i++) {
753 for (j = 0; j < cp->tileno_size; j++) {
754 tileno = cp->tileno[j];
755 x0 = j == 0 ? tcd->tcd_image->tiles[tileno].comps[i].x0 : int_min(x0,
756 (unsigned int) tcd->tcd_image->tiles[tileno].comps[i].x0);
757 y0 = j == 0 ? tcd->tcd_image->tiles[tileno].comps[i].y0 : int_min(y0,
758 (unsigned int) tcd->tcd_image->tiles[tileno].comps[i].y0);
759 x1 = j == 0 ? tcd->tcd_image->tiles[tileno].comps[i].x1 : int_max(x1,
760 (unsigned int) tcd->tcd_image->tiles[tileno].comps[i].x1);
761 y1 = j == 0 ? tcd->tcd_image->tiles[tileno].comps[i].y1 : int_max(y1,
762 (unsigned int) tcd->tcd_image->tiles[tileno].comps[i].y1);
768 image->comps[i].data = (int *) opj_malloc(w * h * sizeof(int));
769 image->comps[i].w = w;
770 image->comps[i].h = h;
771 image->comps[i].x0 = x0;
772 image->comps[i].y0 = y0;
776 void tcd_makelayer_fixed(opj_tcd_t *tcd, int layno, int final) {
777 int compno, resno, bandno, precno, cblkno;
778 int value; /*, matrice[tcd_tcp->numlayers][tcd_tile->comps[0].numresolutions][3]; */
779 int matrice[10][10][3];
782 opj_cp_t *cp = tcd->cp;
783 opj_tcd_tile_t *tcd_tile = tcd->tcd_tile;
784 opj_tcp_t *tcd_tcp = tcd->tcp;
786 /*matrice=(int*)opj_malloc(tcd_tcp->numlayers*tcd_tile->comps[0].numresolutions*3*sizeof(int)); */
788 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
789 opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
790 for (i = 0; i < tcd_tcp->numlayers; i++) {
791 for (j = 0; j < tilec->numresolutions; j++) {
792 for (k = 0; k < 3; k++) {
794 (int) (cp->matrice[i * tilec->numresolutions * 3 + j * 3 + k]
795 * (float) (tcd->image->comps[compno].prec / 16.0));
800 for (resno = 0; resno < tilec->numresolutions; resno++) {
801 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
802 for (bandno = 0; bandno < res->numbands; bandno++) {
803 opj_tcd_band_t *band = &res->bands[bandno];
804 for (precno = 0; precno < res->pw * res->ph; precno++) {
805 opj_tcd_precinct_t *prc = &band->precincts[precno];
806 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
807 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
808 opj_tcd_layer_t *layer = &cblk->layers[layno];
810 int imsb = tcd->image->comps[compno].prec - cblk->numbps; /* number of bit-plan equal to zero */
811 /* Correction of the matrix of coefficient to include the IMSB information */
813 value = matrice[layno][resno][bandno];
820 value = matrice[layno][resno][bandno] - matrice[layno - 1][resno][bandno];
821 if (imsb >= matrice[layno - 1][resno][bandno]) {
822 value -= (imsb - matrice[layno - 1][resno][bandno]);
830 cblk->numpassesinlayers = 0;
833 n = cblk->numpassesinlayers;
834 if (cblk->numpassesinlayers == 0) {
836 n = 3 * value - 2 + cblk->numpassesinlayers;
838 n = cblk->numpassesinlayers;
841 n = 3 * value + cblk->numpassesinlayers;
844 layer->numpasses = n - cblk->numpassesinlayers;
846 if (!layer->numpasses)
849 if (cblk->numpassesinlayers == 0) {
850 layer->len = cblk->passes[n - 1].rate;
851 layer->data = cblk->data;
853 layer->len = cblk->passes[n - 1].rate - cblk->passes[cblk->numpassesinlayers - 1].rate;
854 layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate;
857 cblk->numpassesinlayers = n;
865 void tcd_rateallocate_fixed(opj_tcd_t *tcd) {
867 for (layno = 0; layno < tcd->tcp->numlayers; layno++) {
868 tcd_makelayer_fixed(tcd, layno, 1);
872 void tcd_makelayer(opj_tcd_t *tcd, int layno, double thresh, int final) {
873 int compno, resno, bandno, precno, cblkno, passno;
875 opj_tcd_tile_t *tcd_tile = tcd->tcd_tile;
877 tcd_tile->distolayer[layno] = 0; /* fixed_quality */
879 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
880 opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
881 for (resno = 0; resno < tilec->numresolutions; resno++) {
882 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
883 for (bandno = 0; bandno < res->numbands; bandno++) {
884 opj_tcd_band_t *band = &res->bands[bandno];
885 for (precno = 0; precno < res->pw * res->ph; precno++) {
886 opj_tcd_precinct_t *prc = &band->precincts[precno];
887 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
888 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
889 opj_tcd_layer_t *layer = &cblk->layers[layno];
893 cblk->numpassesinlayers = 0;
895 n = cblk->numpassesinlayers;
896 for (passno = cblk->numpassesinlayers; passno < cblk->totalpasses; passno++) {
899 opj_tcd_pass_t *pass = &cblk->passes[passno];
902 dd = pass->distortiondec;
904 dr = pass->rate - cblk->passes[n - 1].rate;
905 dd = pass->distortiondec - cblk->passes[n - 1].distortiondec;
912 if (dd / dr >= thresh)
915 layer->numpasses = n - cblk->numpassesinlayers;
917 if (!layer->numpasses) {
921 if (cblk->numpassesinlayers == 0) {
922 layer->len = cblk->passes[n - 1].rate;
923 layer->data = cblk->data;
924 layer->disto = cblk->passes[n - 1].distortiondec;
926 layer->len = cblk->passes[n - 1].rate - cblk->passes[cblk->numpassesinlayers - 1].rate;
927 layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate;
928 layer->disto = cblk->passes[n - 1].distortiondec - cblk->passes[cblk->numpassesinlayers - 1].distortiondec;
931 tcd_tile->distolayer[layno] += layer->disto; /* fixed_quality */
934 cblk->numpassesinlayers = n;
942 bool tcd_rateallocate(opj_tcd_t *tcd, unsigned char *dest, int len, opj_image_info_t * image_info) {
943 int compno, resno, bandno, precno, cblkno, passno, layno;
945 double cumdisto[100]; /* fixed_quality */
946 const double K = 1; /* 1.1; fixed_quality */
949 opj_cp_t *cp = tcd->cp;
950 opj_tcd_tile_t *tcd_tile = tcd->tcd_tile;
951 opj_tcp_t *tcd_tcp = tcd->tcp;
956 tcd_tile->nbpix = 0; /* fixed_quality */
958 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
959 opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
962 for (resno = 0; resno < tilec->numresolutions; resno++) {
963 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
965 for (bandno = 0; bandno < res->numbands; bandno++) {
966 opj_tcd_band_t *band = &res->bands[bandno];
968 for (precno = 0; precno < res->pw * res->ph; precno++) {
969 opj_tcd_precinct_t *prc = &band->precincts[precno];
971 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
972 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
974 for (passno = 0; passno < cblk->totalpasses; passno++) {
975 opj_tcd_pass_t *pass = &cblk->passes[passno];
980 dd = pass->distortiondec;
982 dr = pass->rate - cblk->passes[passno - 1].rate;
983 dd = pass->distortiondec - cblk->passes[passno - 1].distortiondec;
998 tcd_tile->nbpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0));
999 tilec->nbpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0));
1005 maxSE += (((double)(1 << tcd->image->comps[compno].prec) - 1.0)
1006 * ((double)(1 << tcd->image->comps[compno].prec) -1.0))
1007 * ((double)(tilec->nbpix));
1011 if(image_info && image_info->index_on) {
1012 opj_tile_info_t *tile_info = &image_info->tile[tcd->tcd_tileno];
1013 tile_info->nbpix = tcd_tile->nbpix;
1014 tile_info->distotile = tcd_tile->distotile;
1015 tile_info->thresh = (double *) opj_malloc(tcd_tcp->numlayers * sizeof(double));
1018 for (layno = 0; layno < tcd_tcp->numlayers; layno++) {
1022 /* TODO: remove maxlen */
1023 int maxlen = tcd_tcp->rates[layno] ? (tcd_tcp->rates[layno] < len ? tcd_tcp->rates[layno] : len) : len;
1024 double goodthresh = 0;
1026 double distotarget; /* fixed_quality */
1029 distotarget = tcd_tile->distotile - ((K * maxSE) / pow((float)10, tcd_tcp->distoratio[layno] / 10));
1031 if ((tcd_tcp->rates[layno]) || (cp->disto_alloc == 0)) {
1032 opj_t2_t *t2 = t2_create(tcd->cinfo, tcd->image, cp);
1034 for (i = 0; i < 32; i++) {
1035 double thresh = (lo + hi) / 2;
1037 double distoachieved = 0; /* fixed_quality */
1039 tcd_makelayer(tcd, layno, thresh, 0);
1041 if (cp->fixed_quality) { /* fixed_quality */
1042 distoachieved = (layno == 0) ?
1043 tcd_tile->distolayer[0] : (cumdisto[layno - 1] + tcd_tile->distolayer[layno]);
1044 if (distoachieved < distotarget) {
1050 l = t2_encode_packets(t2, tcd->tcd_tileno, tcd_tile, layno + 1, dest, maxlen, image_info);
1051 /* TODO: what to do with l ??? seek / tell ??? */
1052 /* opj_event_msg(tcd->cinfo, EVT_INFO, "rate alloc: len=%d, max=%d\n", l, maxlen); */
1061 goodthresh = thresh;
1073 if(image_info && image_info->index_on) { /* Threshold for Marcela Index */
1074 image_info->tile[tcd->tcd_tileno].thresh[layno] = goodthresh;
1076 tcd_makelayer(tcd, layno, goodthresh, 1);
1079 cumdisto[layno] = (layno == 0) ? tcd_tile->distolayer[0] : (cumdisto[layno - 1] + tcd_tile->distolayer[layno]);
1085 int tcd_encode_tile(opj_tcd_t *tcd, int tileno, unsigned char *dest, int len, opj_image_info_t * image_info) {
1088 double encoding_time;
1089 opj_tcd_tile_t *tile = NULL;
1090 opj_tcp_t *tcd_tcp = NULL;
1091 opj_cp_t *cp = NULL;
1093 opj_tcp_t *tcp = &tcd->cp->tcps[0];
1094 opj_tccp_t *tccp = &tcp->tccps[0];
1095 opj_image_t *image = tcd->image;
1097 opj_t1_t *t1 = NULL; /* T1 component */
1098 opj_t2_t *t2 = NULL; /* T2 component */
1100 tcd->tcd_tileno = tileno;
1101 tcd->tcd_tile = tcd->tcd_image->tiles;
1102 tcd->tcp = &tcd->cp->tcps[tileno];
1104 tile = tcd->tcd_tile;
1108 /* INDEX >> "Precinct_nb_X et Precinct_nb_Y" */
1109 if(image_info && image_info->index_on) {
1110 opj_tcd_tilecomp_t *tilec_idx = &tile->comps[0]; /* based on component 0 */
1111 for (i = 0; i < tilec_idx->numresolutions; i++) {
1112 opj_tcd_resolution_t *res_idx = &tilec_idx->resolutions[i];
1114 image_info->tile[tileno].pw[i] = res_idx->pw;
1115 image_info->tile[tileno].ph[i] = res_idx->ph;
1117 npck += res_idx->pw * res_idx->ph;
1119 image_info->tile[tileno].pdx[i] = tccp->prcw[i];
1120 image_info->tile[tileno].pdy[i] = tccp->prch[i];
1122 image_info->tile[tileno].packet = (opj_packet_info_t *) opj_malloc(image_info->comp * image_info->layer * npck * sizeof(opj_packet_info_t));
1126 /*---------------TILE-------------------*/
1127 encoding_time = opj_clock(); /* time needed to encode a tile */
1129 for (compno = 0; compno < tile->numcomps; compno++) {
1132 int adjust = image->comps[compno].sgnd ? 0 : 1 << (image->comps[compno].prec - 1);
1133 int offset_x = int_ceildiv(image->x0, image->comps[compno].dx);
1134 int offset_y = int_ceildiv(image->y0, image->comps[compno].dy);
1136 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1137 int tw = tilec->x1 - tilec->x0;
1138 int w = int_ceildiv(image->x1 - image->x0, image->comps[compno].dx);
1140 /* extract tile data */
1142 if (tcd_tcp->tccps[compno].qmfbid == 1) {
1143 for (y = tilec->y0; y < tilec->y1; y++) {
1144 /* start of the src tile scanline */
1145 int *data = &image->comps[compno].data[(tilec->x0 - offset_x) + (y - offset_y) * w];
1146 /* start of the dst tile scanline */
1147 int *tile_data = &tilec->data[(y - tilec->y0) * tw];
1148 for (x = tilec->x0; x < tilec->x1; x++) {
1149 *tile_data++ = *data++ - adjust;
1152 } else if (tcd_tcp->tccps[compno].qmfbid == 0) {
1153 for (y = tilec->y0; y < tilec->y1; y++) {
1154 /* start of the src tile scanline */
1155 int *data = &image->comps[compno].data[(tilec->x0 - offset_x) + (y - offset_y) * w];
1156 /* start of the dst tile scanline */
1157 int *tile_data = &tilec->data[(y - tilec->y0) * tw];
1158 for (x = tilec->x0; x < tilec->x1; x++) {
1159 *tile_data++ = (*data++ - adjust) << 13;
1165 /*----------------MCT-------------------*/
1167 int samples = (tile->comps[0].x1 - tile->comps[0].x0) * (tile->comps[0].y1 - tile->comps[0].y0);
1168 if (tcd_tcp->tccps[0].qmfbid == 0) {
1169 mct_encode_real(tile->comps[0].data, tile->comps[1].data, tile->comps[2].data, samples);
1171 mct_encode(tile->comps[0].data, tile->comps[1].data, tile->comps[2].data, samples);
1175 /*----------------DWT---------------------*/
1177 for (compno = 0; compno < tile->numcomps; compno++) {
1178 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1179 if (tcd_tcp->tccps[compno].qmfbid == 1) {
1181 } else if (tcd_tcp->tccps[compno].qmfbid == 0) {
1182 dwt_encode_real(tilec);
1186 /*------------------TIER1-----------------*/
1187 t1 = t1_create(tcd->cinfo);
1188 t1_encode_cblks(t1, tile, tcd_tcp);
1191 /*-----------RATE-ALLOCATE------------------*/
1195 image_info->index_write = 0;
1197 if (cp->disto_alloc || cp->fixed_quality) { /* fixed_quality */
1198 /* Normal Rate/distortion allocation */
1199 tcd_rateallocate(tcd, dest, len, image_info);
1201 /* Fixed layer allocation */
1202 tcd_rateallocate_fixed(tcd);
1205 /*--------------TIER2------------------*/
1209 image_info->index_write = 1;
1212 t2 = t2_create(tcd->cinfo, image, cp);
1213 l = t2_encode_packets(t2, tileno, tile, tcd_tcp->numlayers, dest, len, image_info);
1216 /*---------------CLEAN-------------------*/
1218 encoding_time = opj_clock() - encoding_time;
1219 opj_event_msg(tcd->cinfo, EVT_INFO, "- tile encoded in %f s\n", encoding_time);
1221 /* cleaning memory */
1222 for (compno = 0; compno < tile->numcomps; compno++) {
1223 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1224 opj_free(tilec->data);
1230 bool tcd_decode_tile(opj_tcd_t *tcd, unsigned char *src, int len, int tileno) {
1234 double tile_time, t1_time, dwt_time;
1235 opj_tcd_tile_t *tile = NULL;
1237 opj_t1_t *t1 = NULL; /* T1 component */
1238 opj_t2_t *t2 = NULL; /* T2 component */
1240 tcd->tcd_tileno = tileno;
1241 tcd->tcd_tile = &(tcd->tcd_image->tiles[tileno]);
1242 tcd->tcp = &(tcd->cp->tcps[tileno]);
1243 tile = tcd->tcd_tile;
1245 tile_time = opj_clock(); /* time needed to decode a tile */
1246 opj_event_msg(tcd->cinfo, EVT_INFO, "tile %d of %d\n", tileno + 1, tcd->cp->tw * tcd->cp->th);
1248 /*--------------TIER2------------------*/
1250 t2 = t2_create(tcd->cinfo, tcd->image, tcd->cp);
1251 l = t2_decode_packets(t2, src, len, tileno, tile);
1256 opj_event_msg(tcd->cinfo, EVT_ERROR, "tcd_decode: incomplete bistream\n");
1259 /*------------------TIER1-----------------*/
1261 t1_time = opj_clock(); /* time needed to decode a tile */
1262 t1 = t1_create(tcd->cinfo);
1263 t1_decode_cblks(t1, tile, tcd->tcp);
1265 t1_time = opj_clock() - t1_time;
1266 opj_event_msg(tcd->cinfo, EVT_INFO, "- tiers-1 took %f s\n", t1_time);
1268 /*----------------DWT---------------------*/
1270 dwt_time = opj_clock(); /* time needed to decode a tile */
1271 for (compno = 0; compno < tile->numcomps; compno++) {
1272 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1273 if (tcd->cp->reduce != 0) {
1274 tcd->image->comps[compno].resno_decoded =
1275 tile->comps[compno].numresolutions - tcd->cp->reduce - 1;
1278 if (tcd->tcp->tccps[compno].qmfbid == 1) {
1279 dwt_decode(tilec, tilec->numresolutions - 1 - tcd->image->comps[compno].resno_decoded);
1281 dwt_decode_real(tilec, tilec->numresolutions - 1 - tcd->image->comps[compno].resno_decoded);
1284 if (tile->comps[compno].numresolutions > 0) {
1285 tcd->image->comps[compno].factor = tile->comps[compno].numresolutions - (tcd->image->comps[compno].resno_decoded + 1);
1288 dwt_time = opj_clock() - dwt_time;
1289 opj_event_msg(tcd->cinfo, EVT_INFO, "- dwt took %f s\n", dwt_time);
1291 /*----------------MCT-------------------*/
1293 if (tcd->tcp->mct) {
1294 if (tcd->tcp->tccps[0].qmfbid == 1) {
1295 mct_decode(tile->comps[0].data, tile->comps[1].data, tile->comps[2].data,
1296 (tile->comps[0].x1 - tile->comps[0].x0) * (tile->comps[0].y1 - tile->comps[0].y0));
1298 mct_decode_real(tile->comps[0].data, tile->comps[1].data, tile->comps[2].data,
1299 (tile->comps[0].x1 - tile->comps[0].x0) * (tile->comps[0].y1 - tile->comps[0].y0));
1303 /*---------------TILE-------------------*/
1305 for (compno = 0; compno < tile->numcomps; compno++) {
1306 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1307 opj_tcd_resolution_t *res = &tilec->resolutions[tcd->image->comps[compno].resno_decoded];
1308 int adjust = tcd->image->comps[compno].sgnd ? 0 : 1 << (tcd->image->comps[compno].prec - 1);
1309 int min = tcd->image->comps[compno].sgnd ?
1310 -(1 << (tcd->image->comps[compno].prec - 1)) : 0;
1311 int max = tcd->image->comps[compno].sgnd ?
1312 (1 << (tcd->image->comps[compno].prec - 1)) - 1 : (1 << tcd->image->comps[compno].prec) - 1;
1314 int tw = tilec->x1 - tilec->x0;
1315 int w = tcd->image->comps[compno].w;
1318 int offset_x = int_ceildivpow2(tcd->image->comps[compno].x0, tcd->image->comps[compno].factor);
1319 int offset_y = int_ceildivpow2(tcd->image->comps[compno].y0, tcd->image->comps[compno].factor);
1321 for (j = res->y0; j < res->y1; j++) {
1322 for (i = res->x0; i < res->x1; i++) {
1324 float tmp = (float)((tilec->data[i - res->x0 + (j - res->y0) * tw]) / 8192.0);
1326 if (tcd->tcp->tccps[compno].qmfbid == 1) {
1327 v = tilec->data[i - res->x0 + (j - res->y0) * tw];
1329 int tmp2 = ((int) (floor(fabs(tmp)))) + ((int) floor(fabs(tmp*2))%2);
1330 v = ((tmp < 0) ? -tmp2:tmp2);
1334 tcd->image->comps[compno].data[(i - offset_x) + (j - offset_y) * w] = int_clamp(v, min, max);
1339 tile_time = opj_clock() - tile_time; /* time needed to decode a tile */
1340 opj_event_msg(tcd->cinfo, EVT_INFO, "- tile decoded in %f s\n", tile_time);
1342 for (compno = 0; compno < tile->numcomps; compno++) {
1343 opj_free(tcd->tcd_image->tiles[tileno].comps[compno].data);
1344 tcd->tcd_image->tiles[tileno].comps[compno].data = NULL;
1354 void tcd_free_decode(opj_tcd_t *tcd) {
1355 int tileno,compno,resno,bandno,precno;
1357 opj_tcd_image_t *tcd_image = tcd->tcd_image;
1359 for (tileno = 0; tileno < tcd_image->tw * tcd_image->th; tileno++) {
1360 opj_tcd_tile_t *tile = &tcd_image->tiles[tileno];
1361 for (compno = 0; compno < tile->numcomps; compno++) {
1362 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1363 for (resno = 0; resno < tilec->numresolutions; resno++) {
1364 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
1365 for (bandno = 0; bandno < res->numbands; bandno++) {
1366 opj_tcd_band_t *band = &res->bands[bandno];
1367 for (precno = 0; precno < res->ph * res->pw; precno++) {
1368 opj_tcd_precinct_t *prec = &band->precincts[precno];
1369 if (prec->cblks != NULL) opj_free(prec->cblks);
1370 if (prec->imsbtree != NULL) tgt_destroy(prec->imsbtree);
1371 if (prec->incltree != NULL) tgt_destroy(prec->incltree);
1373 if (band->precincts != NULL) opj_free(band->precincts);
1376 if (tilec->resolutions != NULL) opj_free(tilec->resolutions);
1378 if (tile->comps != NULL) opj_free(tile->comps);
1381 if (tcd_image->tiles != NULL) opj_free(tcd_image->tiles);