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
8 * Copyright (c) 2006-2007, Parvatha Elangovan
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
33 #include "opj_includes.h"
35 void tcd_dump(FILE *fd, opj_tcd_t *tcd, opj_tcd_image_t * img) {
36 int tileno, compno, resno, bandno, precno, cblkno;
38 fprintf(fd, "image {\n");
39 fprintf(fd, " tw=%d, th=%d x0=%d x1=%d y0=%d y1=%d\n",
40 img->tw, img->th, tcd->image->x0, tcd->image->x1, tcd->image->y0, tcd->image->y1);
42 for (tileno = 0; tileno < img->th * img->tw; tileno++) {
43 opj_tcd_tile_t *tile = &tcd->tcd_image->tiles[tileno];
44 fprintf(fd, " tile {\n");
45 fprintf(fd, " x0=%d, y0=%d, x1=%d, y1=%d, numcomps=%d\n",
46 tile->x0, tile->y0, tile->x1, tile->y1, tile->numcomps);
47 for (compno = 0; compno < tile->numcomps; compno++) {
48 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
49 fprintf(fd, " tilec {\n");
51 " x0=%d, y0=%d, x1=%d, y1=%d, numresolutions=%d\n",
52 tilec->x0, tilec->y0, tilec->x1, tilec->y1, tilec->numresolutions);
53 for (resno = 0; resno < tilec->numresolutions; resno++) {
54 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
55 fprintf(fd, "\n res {\n");
57 " x0=%d, y0=%d, x1=%d, y1=%d, pw=%d, ph=%d, numbands=%d\n",
58 res->x0, res->y0, res->x1, res->y1, res->pw, res->ph, res->numbands);
59 for (bandno = 0; bandno < res->numbands; bandno++) {
60 opj_tcd_band_t *band = &res->bands[bandno];
61 fprintf(fd, " band {\n");
63 " x0=%d, y0=%d, x1=%d, y1=%d, stepsize=%f, numbps=%d\n",
64 band->x0, band->y0, band->x1, band->y1, band->stepsize, band->numbps);
65 for (precno = 0; precno < res->pw * res->ph; precno++) {
66 opj_tcd_precinct_t *prec = &band->precincts[precno];
67 fprintf(fd, " prec {\n");
69 " x0=%d, y0=%d, x1=%d, y1=%d, cw=%d, ch=%d\n",
70 prec->x0, prec->y0, prec->x1, prec->y1, prec->cw, prec->ch);
71 for (cblkno = 0; cblkno < prec->cw * prec->ch; cblkno++) {
72 opj_tcd_cblk_t *cblk = &prec->cblks[cblkno];
73 fprintf(fd, " cblk {\n");
75 " x0=%d, y0=%d, x1=%d, y1=%d\n",
76 cblk->x0, cblk->y0, cblk->x1, cblk->y1);
92 /* ----------------------------------------------------------------------- */
95 Create a new TCD handle
97 opj_tcd_t* tcd_create(opj_common_ptr cinfo) {
98 /* create the tcd structure */
99 opj_tcd_t *tcd = (opj_tcd_t*)opj_malloc(sizeof(opj_tcd_t));
100 if(!tcd) return NULL;
102 tcd->tcd_image = (opj_tcd_image_t*)opj_malloc(sizeof(opj_tcd_image_t));
103 if(!tcd->tcd_image) {
112 Destroy a previously created TCD handle
114 void tcd_destroy(opj_tcd_t *tcd) {
116 opj_free(tcd->tcd_image);
121 /* ----------------------------------------------------------------------- */
123 void tcd_malloc_encode(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp, int curtileno) {
124 int tileno, compno, resno, bandno, precno, cblkno;
128 tcd->tcd_image->tw = cp->tw;
129 tcd->tcd_image->th = cp->th;
130 tcd->tcd_image->tiles = (opj_tcd_tile_t *) opj_malloc(sizeof(opj_tcd_tile_t));
132 for (tileno = 0; tileno < 1; tileno++) {
133 opj_tcp_t *tcp = &cp->tcps[curtileno];
136 /* cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
137 int p = curtileno % cp->tw; /* si numerotation matricielle .. */
138 int q = curtileno / cp->tw; /* .. coordonnees de la tile (q,p) q pour ligne et p pour colonne */
140 /* opj_tcd_tile_t *tile=&tcd->tcd_image->tiles[tileno]; */
141 opj_tcd_tile_t *tile = tcd->tcd_image->tiles;
143 /* 4 borders of the tile rescale on the image if necessary */
144 tile->x0 = int_max(cp->tx0 + p * cp->tdx, image->x0);
145 tile->y0 = int_max(cp->ty0 + q * cp->tdy, image->y0);
146 tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, image->x1);
147 tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, image->y1);
148 tile->numcomps = image->numcomps;
149 /* tile->PPT=image->PPT; */
151 /* Modification of the RATE >> */
152 for (j = 0; j < tcp->numlayers; j++) {
153 tcp->rates[j] = tcp->rates[j] ?
154 ((float) (tile->numcomps
155 * (tile->x1 - tile->x0)
156 * (tile->y1 - tile->y0)
157 * image->comps[0].prec))/
158 (tcp->rates[j] * 8 * image->comps[0].dx * image->comps[0].dy)
162 if (j && tcp->rates[j] < tcp->rates[j - 1] + 10) {
163 tcp->rates[j] = tcp->rates[j - 1] + 20;
165 if (!j && tcp->rates[j] < 30)
170 /* << Modification of the RATE */
172 tile->comps = (opj_tcd_tilecomp_t *) opj_malloc(image->numcomps * sizeof(opj_tcd_tilecomp_t));
173 for (compno = 0; compno < tile->numcomps; compno++) {
174 opj_tccp_t *tccp = &tcp->tccps[compno];
176 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
178 /* border of each tile component (global) */
179 tilec->x0 = int_ceildiv(tile->x0, image->comps[compno].dx);
180 tilec->y0 = int_ceildiv(tile->y0, image->comps[compno].dy);
181 tilec->x1 = int_ceildiv(tile->x1, image->comps[compno].dx);
182 tilec->y1 = int_ceildiv(tile->y1, image->comps[compno].dy);
184 tilec->data = (int *) opj_malloc((tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0) * sizeof(int));
185 tilec->numresolutions = tccp->numresolutions;
187 tilec->resolutions = (opj_tcd_resolution_t *) opj_malloc(tilec->numresolutions * sizeof(opj_tcd_resolution_t));
189 for (resno = 0; resno < tilec->numresolutions; resno++) {
191 int levelno = tilec->numresolutions - 1 - resno;
192 int tlprcxstart, tlprcystart, brprcxend, brprcyend;
193 int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend;
194 int cbgwidthexpn, cbgheightexpn;
195 int cblkwidthexpn, cblkheightexpn;
197 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
199 /* border for each resolution level (global) */
200 res->x0 = int_ceildivpow2(tilec->x0, levelno);
201 res->y0 = int_ceildivpow2(tilec->y0, levelno);
202 res->x1 = int_ceildivpow2(tilec->x1, levelno);
203 res->y1 = int_ceildivpow2(tilec->y1, levelno);
205 res->numbands = resno == 0 ? 1 : 3;
206 /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */
207 if (tccp->csty & J2K_CCP_CSTY_PRT) {
208 pdx = tccp->prcw[resno];
209 pdy = tccp->prch[resno];
214 /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
215 tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx;
216 tlprcystart = int_floordivpow2(res->y0, pdy) << pdy;
218 brprcxend = int_ceildivpow2(res->x1, pdx) << pdx;
219 brprcyend = int_ceildivpow2(res->y1, pdy) << pdy;
221 res->pw = (brprcxend - tlprcxstart) >> pdx;
222 res->ph = (brprcyend - tlprcystart) >> pdy;
225 tlcbgxstart = tlprcxstart;
226 tlcbgystart = tlprcystart;
227 brcbgxend = brprcxend;
228 brcbgyend = brprcyend;
232 tlcbgxstart = int_ceildivpow2(tlprcxstart, 1);
233 tlcbgystart = int_ceildivpow2(tlprcystart, 1);
234 brcbgxend = int_ceildivpow2(brprcxend, 1);
235 brcbgyend = int_ceildivpow2(brprcyend, 1);
236 cbgwidthexpn = pdx - 1;
237 cbgheightexpn = pdy - 1;
240 cblkwidthexpn = int_min(tccp->cblkw, cbgwidthexpn);
241 cblkheightexpn = int_min(tccp->cblkh, cbgheightexpn);
243 for (bandno = 0; bandno < res->numbands; bandno++) {
246 opj_stepsize_t *ss = NULL;
248 opj_tcd_band_t *band = &res->bands[bandno];
250 band->bandno = resno == 0 ? 0 : bandno + 1;
251 x0b = (band->bandno == 1) || (band->bandno == 3) ? 1 : 0;
252 y0b = (band->bandno == 2) || (band->bandno == 3) ? 1 : 0;
254 if (band->bandno == 0) {
255 /* band border (global) */
256 band->x0 = int_ceildivpow2(tilec->x0, levelno);
257 band->y0 = int_ceildivpow2(tilec->y0, levelno);
258 band->x1 = int_ceildivpow2(tilec->x1, levelno);
259 band->y1 = int_ceildivpow2(tilec->y1, levelno);
261 /* band border (global) */
262 band->x0 = int_ceildivpow2(tilec->x0 - (1 << levelno) * x0b, levelno + 1);
263 band->y0 = int_ceildivpow2(tilec->y0 - (1 << levelno) * y0b, levelno + 1);
264 band->x1 = int_ceildivpow2(tilec->x1 - (1 << levelno) * x0b, levelno + 1);
265 band->y1 = int_ceildivpow2(tilec->y1 - (1 << levelno) * y0b, levelno + 1);
268 ss = &tccp->stepsizes[resno == 0 ? 0 : 3 * (resno - 1) + bandno + 1];
269 gain = tccp->qmfbid == 0 ? dwt_getgain_real(band->bandno) : dwt_getgain(band->bandno);
270 numbps = image->comps[compno].prec + gain;
272 band->stepsize = (float)((1.0 + ss->mant / 2048.0) * pow(2.0, numbps - ss->expn));
273 band->numbps = ss->expn + tccp->numgbits - 1; /* WHY -1 ? */
275 band->precincts = (opj_tcd_precinct_t *) opj_malloc(3 * res->pw * res->ph * sizeof(opj_tcd_precinct_t));
277 for (i = 0; i < res->pw * res->ph * 3; i++) {
278 band->precincts[i].imsbtree = NULL;
279 band->precincts[i].incltree = NULL;
282 for (precno = 0; precno < res->pw * res->ph; precno++) {
283 int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
285 int cbgxstart = tlcbgxstart + (precno % res->pw) * (1 << cbgwidthexpn);
286 int cbgystart = tlcbgystart + (precno / res->pw) * (1 << cbgheightexpn);
287 int cbgxend = cbgxstart + (1 << cbgwidthexpn);
288 int cbgyend = cbgystart + (1 << cbgheightexpn);
290 opj_tcd_precinct_t *prc = &band->precincts[precno];
292 /* precinct size (global) */
293 prc->x0 = int_max(cbgxstart, band->x0);
294 prc->y0 = int_max(cbgystart, band->y0);
295 prc->x1 = int_min(cbgxend, band->x1);
296 prc->y1 = int_min(cbgyend, band->y1);
298 tlcblkxstart = int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn;
299 tlcblkystart = int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn;
300 brcblkxend = int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn;
301 brcblkyend = int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn;
302 prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;
303 prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn;
305 prc->cblks = (opj_tcd_cblk_t *) opj_malloc((prc->cw * prc->ch) * sizeof(opj_tcd_cblk_t));
306 prc->incltree = tgt_create(prc->cw, prc->ch);
307 prc->imsbtree = tgt_create(prc->cw, prc->ch);
309 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
310 int cblkxstart = tlcblkxstart + (cblkno % prc->cw) * (1 << cblkwidthexpn);
311 int cblkystart = tlcblkystart + (cblkno / prc->cw) * (1 << cblkheightexpn);
312 int cblkxend = cblkxstart + (1 << cblkwidthexpn);
313 int cblkyend = cblkystart + (1 << cblkheightexpn);
315 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
317 /* code-block size (global) */
318 cblk->x0 = int_max(cblkxstart, prc->x0);
319 cblk->y0 = int_max(cblkystart, prc->y0);
320 cblk->x1 = int_min(cblkxend, prc->x1);
321 cblk->y1 = int_min(cblkyend, prc->y1);
329 /* tcd_dump(stdout, tcd, &tcd->tcd_image); */
332 void tcd_free_encode(opj_tcd_t *tcd) {
333 int tileno, compno, resno, bandno, precno;
335 for (tileno = 0; tileno < 1; tileno++) {
336 opj_tcd_tile_t *tile = tcd->tcd_image->tiles;
338 for (compno = 0; compno < tile->numcomps; compno++) {
339 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
341 for (resno = 0; resno < tilec->numresolutions; resno++) {
342 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
344 for (bandno = 0; bandno < res->numbands; bandno++) {
345 opj_tcd_band_t *band = &res->bands[bandno];
347 for (precno = 0; precno < res->pw * res->ph; precno++) {
348 opj_tcd_precinct_t *prc = &band->precincts[precno];
350 if (prc->incltree != NULL) {
351 tgt_destroy(prc->incltree);
352 prc->incltree = NULL;
354 if (prc->imsbtree != NULL) {
355 tgt_destroy(prc->imsbtree);
356 prc->imsbtree = NULL;
358 opj_free(prc->cblks);
361 opj_free(band->precincts);
362 band->precincts = NULL;
365 opj_free(tilec->resolutions);
366 tilec->resolutions = NULL;
368 opj_free(tile->comps);
371 opj_free(tcd->tcd_image->tiles);
372 tcd->tcd_image->tiles = NULL;
375 void tcd_init_encode(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp, int curtileno) {
376 int tileno, compno, resno, bandno, precno, cblkno;
378 for (tileno = 0; tileno < 1; tileno++) {
379 opj_tcp_t *tcp = &cp->tcps[curtileno];
381 /* cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
382 int p = curtileno % cp->tw;
383 int q = curtileno / cp->tw;
385 opj_tcd_tile_t *tile = tcd->tcd_image->tiles;
387 /* 4 borders of the tile rescale on the image if necessary */
388 tile->x0 = int_max(cp->tx0 + p * cp->tdx, image->x0);
389 tile->y0 = int_max(cp->ty0 + q * cp->tdy, image->y0);
390 tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, image->x1);
391 tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, image->y1);
393 tile->numcomps = image->numcomps;
394 /* tile->PPT=image->PPT; */
396 /* Modification of the RATE >> */
397 for (j = 0; j < tcp->numlayers; j++) {
398 tcp->rates[j] = tcp->rates[j] ?
399 ((float) (tile->numcomps
400 * (tile->x1 - tile->x0)
401 * (tile->y1 - tile->y0)
402 * image->comps[0].prec))/
403 (tcp->rates[j] * 8 * image->comps[0].dx * image->comps[0].dy)
407 if (j && tcp->rates[j] < tcp->rates[j - 1] + 10) {
408 tcp->rates[j] = tcp->rates[j - 1] + 20;
410 if (!j && tcp->rates[j] < 30)
415 /* << Modification of the RATE */
417 /* tile->comps=(opj_tcd_tilecomp_t*)opj_realloc(tile->comps,image->numcomps*sizeof(opj_tcd_tilecomp_t)); */
418 for (compno = 0; compno < tile->numcomps; compno++) {
419 opj_tccp_t *tccp = &tcp->tccps[compno];
421 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
423 /* border of each tile component (global) */
424 tilec->x0 = int_ceildiv(tile->x0, image->comps[compno].dx);
425 tilec->y0 = int_ceildiv(tile->y0, image->comps[compno].dy);
426 tilec->x1 = int_ceildiv(tile->x1, image->comps[compno].dx);
427 tilec->y1 = int_ceildiv(tile->y1, image->comps[compno].dy);
429 tilec->data = (int *) opj_malloc((tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0) * sizeof(int));
430 tilec->numresolutions = tccp->numresolutions;
431 /* tilec->resolutions=(opj_tcd_resolution_t*)opj_realloc(tilec->resolutions,tilec->numresolutions*sizeof(opj_tcd_resolution_t)); */
432 for (resno = 0; resno < tilec->numresolutions; resno++) {
435 int levelno = tilec->numresolutions - 1 - resno;
436 int tlprcxstart, tlprcystart, brprcxend, brprcyend;
437 int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend;
438 int cbgwidthexpn, cbgheightexpn;
439 int cblkwidthexpn, cblkheightexpn;
441 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
443 /* border for each resolution level (global) */
444 res->x0 = int_ceildivpow2(tilec->x0, levelno);
445 res->y0 = int_ceildivpow2(tilec->y0, levelno);
446 res->x1 = int_ceildivpow2(tilec->x1, levelno);
447 res->y1 = int_ceildivpow2(tilec->y1, levelno);
448 res->numbands = resno == 0 ? 1 : 3;
450 /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */
451 if (tccp->csty & J2K_CCP_CSTY_PRT) {
452 pdx = tccp->prcw[resno];
453 pdy = tccp->prch[resno];
458 /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
459 tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx;
460 tlprcystart = int_floordivpow2(res->y0, pdy) << pdy;
461 brprcxend = int_ceildivpow2(res->x1, pdx) << pdx;
462 brprcyend = int_ceildivpow2(res->y1, pdy) << pdy;
464 res->pw = (brprcxend - tlprcxstart) >> pdx;
465 res->ph = (brprcyend - tlprcystart) >> pdy;
468 tlcbgxstart = tlprcxstart;
469 tlcbgystart = tlprcystart;
470 brcbgxend = brprcxend;
471 brcbgyend = brprcyend;
475 tlcbgxstart = int_ceildivpow2(tlprcxstart, 1);
476 tlcbgystart = int_ceildivpow2(tlprcystart, 1);
477 brcbgxend = int_ceildivpow2(brprcxend, 1);
478 brcbgyend = int_ceildivpow2(brprcyend, 1);
479 cbgwidthexpn = pdx - 1;
480 cbgheightexpn = pdy - 1;
483 cblkwidthexpn = int_min(tccp->cblkw, cbgwidthexpn);
484 cblkheightexpn = int_min(tccp->cblkh, cbgheightexpn);
486 for (bandno = 0; bandno < res->numbands; bandno++) {
489 opj_stepsize_t *ss = NULL;
491 opj_tcd_band_t *band = &res->bands[bandno];
493 band->bandno = resno == 0 ? 0 : bandno + 1;
494 x0b = (band->bandno == 1) || (band->bandno == 3) ? 1 : 0;
495 y0b = (band->bandno == 2) || (band->bandno == 3) ? 1 : 0;
497 if (band->bandno == 0) {
499 band->x0 = int_ceildivpow2(tilec->x0, levelno);
500 band->y0 = int_ceildivpow2(tilec->y0, levelno);
501 band->x1 = int_ceildivpow2(tilec->x1, levelno);
502 band->y1 = int_ceildivpow2(tilec->y1, levelno);
504 band->x0 = int_ceildivpow2(tilec->x0 - (1 << levelno) * x0b, levelno + 1);
505 band->y0 = int_ceildivpow2(tilec->y0 - (1 << levelno) * y0b, levelno + 1);
506 band->x1 = int_ceildivpow2(tilec->x1 - (1 << levelno) * x0b, levelno + 1);
507 band->y1 = int_ceildivpow2(tilec->y1 - (1 << levelno) * y0b, levelno + 1);
510 ss = &tccp->stepsizes[resno == 0 ? 0 : 3 * (resno - 1) + bandno + 1];
511 gain = tccp->qmfbid == 0 ? dwt_getgain_real(band->bandno) : dwt_getgain(band->bandno);
512 numbps = image->comps[compno].prec + gain;
513 band->stepsize = (float)((1.0 + ss->mant / 2048.0) * pow(2.0, numbps - ss->expn));
514 band->numbps = ss->expn + tccp->numgbits - 1; /* WHY -1 ? */
516 for (precno = 0; precno < res->pw * res->ph; precno++) {
517 int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
519 int cbgxstart = tlcbgxstart + (precno % res->pw) * (1 << cbgwidthexpn);
520 int cbgystart = tlcbgystart + (precno / res->pw) * (1 << cbgheightexpn);
521 int cbgxend = cbgxstart + (1 << cbgwidthexpn);
522 int cbgyend = cbgystart + (1 << cbgheightexpn);
524 opj_tcd_precinct_t *prc = &band->precincts[precno];
526 /* precinct size (global) */
527 prc->x0 = int_max(cbgxstart, band->x0);
528 prc->y0 = int_max(cbgystart, band->y0);
529 prc->x1 = int_min(cbgxend, band->x1);
530 prc->y1 = int_min(cbgyend, band->y1);
532 tlcblkxstart = int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn;
533 tlcblkystart = int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn;
534 brcblkxend = int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn;
535 brcblkyend = int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn;
536 prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;
537 prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn;
539 opj_free(prc->cblks);
540 prc->cblks = (opj_tcd_cblk_t *) opj_malloc(prc->cw * prc->ch * sizeof(opj_tcd_cblk_t));
542 if (prc->incltree != NULL) {
543 tgt_destroy(prc->incltree);
545 if (prc->imsbtree != NULL) {
546 tgt_destroy(prc->imsbtree);
549 prc->incltree = tgt_create(prc->cw, prc->ch);
550 prc->imsbtree = tgt_create(prc->cw, prc->ch);
552 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
553 int cblkxstart = tlcblkxstart + (cblkno % prc->cw) * (1 << cblkwidthexpn);
554 int cblkystart = tlcblkystart + (cblkno / prc->cw) * (1 << cblkheightexpn);
555 int cblkxend = cblkxstart + (1 << cblkwidthexpn);
556 int cblkyend = cblkystart + (1 << cblkheightexpn);
558 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
560 /* code-block size (global) */
561 cblk->x0 = int_max(cblkxstart, prc->x0);
562 cblk->y0 = int_max(cblkystart, prc->y0);
563 cblk->x1 = int_min(cblkxend, prc->x1);
564 cblk->y1 = int_min(cblkyend, prc->y1);
572 /* tcd_dump(stdout, tcd, &tcd->tcd_image); */
575 void tcd_malloc_decode(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp) {
576 int tileno, compno, resno, bandno, precno, cblkno, i, j, p, q;
577 unsigned int x0 = 0, y0 = 0, x1 = 0, y1 = 0, w, h;
581 tcd->tcd_image->tw = cp->tw;
582 tcd->tcd_image->th = cp->th;
583 tcd->tcd_image->tiles = (opj_tcd_tile_t *) opj_malloc(cp->tw * cp->th * sizeof(opj_tcd_tile_t));
585 for (i = 0; i < cp->tileno_size; i++) {
586 opj_tcp_t *tcp = &(cp->tcps[cp->tileno[i]]);
587 opj_tcd_tile_t *tile = &(tcd->tcd_image->tiles[cp->tileno[i]]);
589 /* cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
590 tileno = cp->tileno[i];
591 p = tileno % cp->tw; /* si numerotation matricielle .. */
592 q = tileno / cp->tw; /* .. coordonnees de la tile (q,p) q pour ligne et p pour colonne */
594 /* 4 borders of the tile rescale on the image if necessary */
595 tile->x0 = int_max(cp->tx0 + p * cp->tdx, image->x0);
596 tile->y0 = int_max(cp->ty0 + q * cp->tdy, image->y0);
597 tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, image->x1);
598 tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, image->y1);
600 tile->numcomps = image->numcomps;
601 tile->comps = (opj_tcd_tilecomp_t *) opj_malloc(image->numcomps * sizeof(opj_tcd_tilecomp_t));
602 for (compno = 0; compno < tile->numcomps; compno++) {
603 opj_tccp_t *tccp = &tcp->tccps[compno];
604 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
606 /* border of each tile component (global) */
607 tilec->x0 = int_ceildiv(tile->x0, image->comps[compno].dx);
608 tilec->y0 = int_ceildiv(tile->y0, image->comps[compno].dy);
609 tilec->x1 = int_ceildiv(tile->x1, image->comps[compno].dx);
610 tilec->y1 = int_ceildiv(tile->y1, image->comps[compno].dy);
612 tilec->data = (int *) opj_malloc((tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0) * sizeof(int));
613 tilec->numresolutions = tccp->numresolutions;
614 tilec->resolutions = (opj_tcd_resolution_t *) opj_malloc(tilec->numresolutions * sizeof(opj_tcd_resolution_t));
616 for (resno = 0; resno < tilec->numresolutions; resno++) {
618 int levelno = tilec->numresolutions - 1 - resno;
619 int tlprcxstart, tlprcystart, brprcxend, brprcyend;
620 int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend;
621 int cbgwidthexpn, cbgheightexpn;
622 int cblkwidthexpn, cblkheightexpn;
624 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
626 /* border for each resolution level (global) */
627 res->x0 = int_ceildivpow2(tilec->x0, levelno);
628 res->y0 = int_ceildivpow2(tilec->y0, levelno);
629 res->x1 = int_ceildivpow2(tilec->x1, levelno);
630 res->y1 = int_ceildivpow2(tilec->y1, levelno);
631 res->numbands = resno == 0 ? 1 : 3;
633 /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */
634 if (tccp->csty & J2K_CCP_CSTY_PRT) {
635 pdx = tccp->prcw[resno];
636 pdy = tccp->prch[resno];
642 /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
643 tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx;
644 tlprcystart = int_floordivpow2(res->y0, pdy) << pdy;
645 brprcxend = int_ceildivpow2(res->x1, pdx) << pdx;
646 brprcyend = int_ceildivpow2(res->y1, pdy) << pdy;
648 res->pw = (res->x0 == res->x1) ? 0 : ((brprcxend - tlprcxstart) >> pdx);
649 res->ph = (res->y0 == res->y1) ? 0 : ((brprcyend - tlprcystart) >> pdy);
652 tlcbgxstart = tlprcxstart;
653 tlcbgystart = tlprcystart;
654 brcbgxend = brprcxend;
655 brcbgyend = brprcyend;
659 tlcbgxstart = int_ceildivpow2(tlprcxstart, 1);
660 tlcbgystart = int_ceildivpow2(tlprcystart, 1);
661 brcbgxend = int_ceildivpow2(brprcxend, 1);
662 brcbgyend = int_ceildivpow2(brprcyend, 1);
663 cbgwidthexpn = pdx - 1;
664 cbgheightexpn = pdy - 1;
667 cblkwidthexpn = int_min(tccp->cblkw, cbgwidthexpn);
668 cblkheightexpn = int_min(tccp->cblkh, cbgheightexpn);
670 for (bandno = 0; bandno < res->numbands; bandno++) {
673 opj_stepsize_t *ss = NULL;
675 opj_tcd_band_t *band = &res->bands[bandno];
676 band->bandno = resno == 0 ? 0 : bandno + 1;
677 x0b = (band->bandno == 1) || (band->bandno == 3) ? 1 : 0;
678 y0b = (band->bandno == 2) || (band->bandno == 3) ? 1 : 0;
680 if (band->bandno == 0) {
681 /* band border (global) */
682 band->x0 = int_ceildivpow2(tilec->x0, levelno);
683 band->y0 = int_ceildivpow2(tilec->y0, levelno);
684 band->x1 = int_ceildivpow2(tilec->x1, levelno);
685 band->y1 = int_ceildivpow2(tilec->y1, levelno);
687 /* band border (global) */
688 band->x0 = int_ceildivpow2(tilec->x0 - (1 << levelno) * x0b, levelno + 1);
689 band->y0 = int_ceildivpow2(tilec->y0 - (1 << levelno) * y0b, levelno + 1);
690 band->x1 = int_ceildivpow2(tilec->x1 - (1 << levelno) * x0b, levelno + 1);
691 band->y1 = int_ceildivpow2(tilec->y1 - (1 << levelno) * y0b, levelno + 1);
694 ss = &tccp->stepsizes[resno == 0 ? 0 : 3 * (resno - 1) + bandno + 1];
695 gain = tccp->qmfbid == 0 ? dwt_getgain_real(band->bandno) : dwt_getgain(band->bandno);
696 numbps = image->comps[compno].prec + gain;
697 band->stepsize = (float)((1.0 + ss->mant / 2048.0) * pow(2.0, numbps - ss->expn));
698 band->numbps = ss->expn + tccp->numgbits - 1; /* WHY -1 ? */
700 band->precincts = (opj_tcd_precinct_t *) opj_malloc(res->pw * res->ph * sizeof(opj_tcd_precinct_t));
702 for (precno = 0; precno < res->pw * res->ph; precno++) {
703 int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
704 int cbgxstart = tlcbgxstart + (precno % res->pw) * (1 << cbgwidthexpn);
705 int cbgystart = tlcbgystart + (precno / res->pw) * (1 << cbgheightexpn);
706 int cbgxend = cbgxstart + (1 << cbgwidthexpn);
707 int cbgyend = cbgystart + (1 << cbgheightexpn);
709 opj_tcd_precinct_t *prc = &band->precincts[precno];
710 /* precinct size (global) */
711 prc->x0 = int_max(cbgxstart, band->x0);
712 prc->y0 = int_max(cbgystart, band->y0);
713 prc->x1 = int_min(cbgxend, band->x1);
714 prc->y1 = int_min(cbgyend, band->y1);
716 tlcblkxstart = int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn;
717 tlcblkystart = int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn;
718 brcblkxend = int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn;
719 brcblkyend = int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn;
720 prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;
721 prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn;
723 prc->cblks = (opj_tcd_cblk_t *) opj_malloc(prc->cw * prc->ch * sizeof(opj_tcd_cblk_t));
725 prc->incltree = tgt_create(prc->cw, prc->ch);
726 prc->imsbtree = tgt_create(prc->cw, prc->ch);
728 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
729 int cblkxstart = tlcblkxstart + (cblkno % prc->cw) * (1 << cblkwidthexpn);
730 int cblkystart = tlcblkystart + (cblkno / prc->cw) * (1 << cblkheightexpn);
731 int cblkxend = cblkxstart + (1 << cblkwidthexpn);
732 int cblkyend = cblkystart + (1 << cblkheightexpn);
734 /* code-block size (global) */
735 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
736 cblk->x0 = int_max(cblkxstart, prc->x0);
737 cblk->y0 = int_max(cblkystart, prc->y0);
738 cblk->x1 = int_min(cblkxend, prc->x1);
739 cblk->y1 = int_min(cblkyend, prc->y1);
745 } /* i = 0..cp->tileno_size */
747 /* tcd_dump(stdout, tcd, &tcd->tcd_image); */
750 Allocate place to store the decoded data = final image
751 Place limited by the tile really present in the codestream
754 for (i = 0; i < image->numcomps; i++) {
755 for (j = 0; j < cp->tileno_size; j++) {
756 tileno = cp->tileno[j];
757 x0 = j == 0 ? tcd->tcd_image->tiles[tileno].comps[i].x0 : int_min(x0,
758 (unsigned int) tcd->tcd_image->tiles[tileno].comps[i].x0);
759 y0 = j == 0 ? tcd->tcd_image->tiles[tileno].comps[i].y0 : int_min(y0,
760 (unsigned int) tcd->tcd_image->tiles[tileno].comps[i].y0);
761 x1 = j == 0 ? tcd->tcd_image->tiles[tileno].comps[i].x1 : int_max(x1,
762 (unsigned int) tcd->tcd_image->tiles[tileno].comps[i].x1);
763 y1 = j == 0 ? tcd->tcd_image->tiles[tileno].comps[i].y1 : int_max(y1,
764 (unsigned int) tcd->tcd_image->tiles[tileno].comps[i].y1);
770 image->comps[i].data = (int *) opj_malloc(w * h * sizeof(int));
771 image->comps[i].w = w;
772 image->comps[i].h = h;
773 image->comps[i].x0 = x0;
774 image->comps[i].y0 = y0;
778 void tcd_makelayer_fixed(opj_tcd_t *tcd, int layno, int final) {
779 int compno, resno, bandno, precno, cblkno;
780 int value; /*, matrice[tcd_tcp->numlayers][tcd_tile->comps[0].numresolutions][3]; */
781 int matrice[10][10][3];
784 opj_cp_t *cp = tcd->cp;
785 opj_tcd_tile_t *tcd_tile = tcd->tcd_tile;
786 opj_tcp_t *tcd_tcp = tcd->tcp;
788 /*matrice=(int*)opj_malloc(tcd_tcp->numlayers*tcd_tile->comps[0].numresolutions*3*sizeof(int)); */
790 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
791 opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
792 for (i = 0; i < tcd_tcp->numlayers; i++) {
793 for (j = 0; j < tilec->numresolutions; j++) {
794 for (k = 0; k < 3; k++) {
796 (int) (cp->matrice[i * tilec->numresolutions * 3 + j * 3 + k]
797 * (float) (tcd->image->comps[compno].prec / 16.0));
802 for (resno = 0; resno < tilec->numresolutions; resno++) {
803 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
804 for (bandno = 0; bandno < res->numbands; bandno++) {
805 opj_tcd_band_t *band = &res->bands[bandno];
806 for (precno = 0; precno < res->pw * res->ph; precno++) {
807 opj_tcd_precinct_t *prc = &band->precincts[precno];
808 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
809 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
810 opj_tcd_layer_t *layer = &cblk->layers[layno];
812 int imsb = tcd->image->comps[compno].prec - cblk->numbps; /* number of bit-plan equal to zero */
813 /* Correction of the matrix of coefficient to include the IMSB information */
815 value = matrice[layno][resno][bandno];
822 value = matrice[layno][resno][bandno] - matrice[layno - 1][resno][bandno];
823 if (imsb >= matrice[layno - 1][resno][bandno]) {
824 value -= (imsb - matrice[layno - 1][resno][bandno]);
832 cblk->numpassesinlayers = 0;
835 n = cblk->numpassesinlayers;
836 if (cblk->numpassesinlayers == 0) {
838 n = 3 * value - 2 + cblk->numpassesinlayers;
840 n = cblk->numpassesinlayers;
843 n = 3 * value + cblk->numpassesinlayers;
846 layer->numpasses = n - cblk->numpassesinlayers;
848 if (!layer->numpasses)
851 if (cblk->numpassesinlayers == 0) {
852 layer->len = cblk->passes[n - 1].rate;
853 layer->data = cblk->data;
855 layer->len = cblk->passes[n - 1].rate - cblk->passes[cblk->numpassesinlayers - 1].rate;
856 layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate;
859 cblk->numpassesinlayers = n;
867 void tcd_rateallocate_fixed(opj_tcd_t *tcd) {
869 for (layno = 0; layno < tcd->tcp->numlayers; layno++) {
870 tcd_makelayer_fixed(tcd, layno, 1);
874 void tcd_makelayer(opj_tcd_t *tcd, int layno, double thresh, int final) {
875 int compno, resno, bandno, precno, cblkno, passno;
877 opj_tcd_tile_t *tcd_tile = tcd->tcd_tile;
879 tcd_tile->distolayer[layno] = 0; /* fixed_quality */
881 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
882 opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
883 for (resno = 0; resno < tilec->numresolutions; resno++) {
884 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
885 for (bandno = 0; bandno < res->numbands; bandno++) {
886 opj_tcd_band_t *band = &res->bands[bandno];
887 for (precno = 0; precno < res->pw * res->ph; precno++) {
888 opj_tcd_precinct_t *prc = &band->precincts[precno];
889 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
890 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
891 opj_tcd_layer_t *layer = &cblk->layers[layno];
895 cblk->numpassesinlayers = 0;
897 n = cblk->numpassesinlayers;
898 for (passno = cblk->numpassesinlayers; passno < cblk->totalpasses; passno++) {
901 opj_tcd_pass_t *pass = &cblk->passes[passno];
904 dd = pass->distortiondec;
906 dr = pass->rate - cblk->passes[n - 1].rate;
907 dd = pass->distortiondec - cblk->passes[n - 1].distortiondec;
914 if (dd / dr >= thresh)
917 layer->numpasses = n - cblk->numpassesinlayers;
919 if (!layer->numpasses) {
923 if (cblk->numpassesinlayers == 0) {
924 layer->len = cblk->passes[n - 1].rate;
925 layer->data = cblk->data;
926 layer->disto = cblk->passes[n - 1].distortiondec;
928 layer->len = cblk->passes[n - 1].rate - cblk->passes[cblk->numpassesinlayers - 1].rate;
929 layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate;
930 layer->disto = cblk->passes[n - 1].distortiondec - cblk->passes[cblk->numpassesinlayers - 1].distortiondec;
933 tcd_tile->distolayer[layno] += layer->disto; /* fixed_quality */
936 cblk->numpassesinlayers = n;
944 bool tcd_rateallocate(opj_tcd_t *tcd, unsigned char *dest, int len, opj_image_info_t * image_info) {
945 int compno, resno, bandno, precno, cblkno, passno, layno;
947 double cumdisto[100]; /* fixed_quality */
948 const double K = 1; /* 1.1; fixed_quality */
951 opj_cp_t *cp = tcd->cp;
952 opj_tcd_tile_t *tcd_tile = tcd->tcd_tile;
953 opj_tcp_t *tcd_tcp = tcd->tcp;
958 tcd_tile->nbpix = 0; /* fixed_quality */
960 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
961 opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
964 for (resno = 0; resno < tilec->numresolutions; resno++) {
965 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
967 for (bandno = 0; bandno < res->numbands; bandno++) {
968 opj_tcd_band_t *band = &res->bands[bandno];
970 for (precno = 0; precno < res->pw * res->ph; precno++) {
971 opj_tcd_precinct_t *prc = &band->precincts[precno];
973 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
974 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
976 for (passno = 0; passno < cblk->totalpasses; passno++) {
977 opj_tcd_pass_t *pass = &cblk->passes[passno];
982 dd = pass->distortiondec;
984 dr = pass->rate - cblk->passes[passno - 1].rate;
985 dd = pass->distortiondec - cblk->passes[passno - 1].distortiondec;
1000 tcd_tile->nbpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0));
1001 tilec->nbpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0));
1007 maxSE += (((double)(1 << tcd->image->comps[compno].prec) - 1.0)
1008 * ((double)(1 << tcd->image->comps[compno].prec) -1.0))
1009 * ((double)(tilec->nbpix));
1013 if(image_info && image_info->index_on) {
1014 opj_tile_info_t *tile_info = &image_info->tile[tcd->tcd_tileno];
1015 tile_info->nbpix = tcd_tile->nbpix;
1016 tile_info->distotile = tcd_tile->distotile;
1017 tile_info->thresh = (double *) opj_malloc(tcd_tcp->numlayers * sizeof(double));
1020 for (layno = 0; layno < tcd_tcp->numlayers; layno++) {
1024 /* TODO: remove maxlen */
1025 int maxlen = tcd_tcp->rates[layno] ? int_min(((int) ceil(tcd_tcp->rates[layno])), len) : len;
1026 double goodthresh = 0;
1027 double stable_thresh = 0;
1029 double distotarget; /* fixed_quality */
1032 distotarget = tcd_tile->distotile - ((K * maxSE) / pow((float)10, tcd_tcp->distoratio[layno] / 10));
1034 if ((tcd_tcp->rates[layno]) || (cp->disto_alloc == 0)) {
1035 opj_t2_t *t2 = t2_create(tcd->cinfo, tcd->image, cp);
1037 for (i = 0; i < 32; i++) {
1038 double thresh = (lo + hi) / 2;
1040 double distoachieved = 0; /* fixed_quality */
1042 tcd_makelayer(tcd, layno, thresh, 0);
1044 if (cp->fixed_quality) { /* fixed_quality */
1046 l = t2_encode_packets(t2,tcd->tcd_tileno, tcd_tile, layno + 1, dest, maxlen, image_info,tcd->cur_tp_num,tcd->tp_pos,tcd->cur_pino,THRESH_CALC);
1051 distoachieved = layno == 0 ?
1052 tcd_tile->distolayer[0] : cumdisto[layno - 1] + tcd_tile->distolayer[layno];
1053 if (distoachieved < distotarget) {
1055 stable_thresh = thresh;
1062 distoachieved = (layno == 0) ?
1063 tcd_tile->distolayer[0] : (cumdisto[layno - 1] + tcd_tile->distolayer[layno]);
1064 if (distoachieved < distotarget) {
1066 stable_thresh = thresh;
1072 l = t2_encode_packets(t2, tcd->tcd_tileno, tcd_tile, layno + 1, dest, maxlen, image_info,tcd->cur_tp_num,tcd->tp_pos,tcd->cur_pino,THRESH_CALC);
1073 /* TODO: what to do with l ??? seek / tell ??? */
1074 /* opj_event_msg(tcd->cinfo, EVT_INFO, "rate alloc: len=%d, max=%d\n", l, maxlen); */
1080 stable_thresh = thresh;
1084 goodthresh = stable_thresh;
1095 if(image_info && image_info->index_on) { /* Threshold for Marcela Index */
1096 image_info->tile[tcd->tcd_tileno].thresh[layno] = goodthresh;
1098 tcd_makelayer(tcd, layno, goodthresh, 1);
1101 cumdisto[layno] = (layno == 0) ? tcd_tile->distolayer[0] : (cumdisto[layno - 1] + tcd_tile->distolayer[layno]);
1107 int tcd_encode_tile(opj_tcd_t *tcd, int tileno, unsigned char *dest, int len, opj_image_info_t * image_info) {
1110 opj_tcd_tile_t *tile = NULL;
1111 opj_tcp_t *tcd_tcp = NULL;
1112 opj_cp_t *cp = NULL;
1114 opj_tcp_t *tcp = &tcd->cp->tcps[0];
1115 opj_tccp_t *tccp = &tcp->tccps[0];
1116 opj_image_t *image = tcd->image;
1118 opj_t1_t *t1 = NULL; /* T1 component */
1119 opj_t2_t *t2 = NULL; /* T2 component */
1121 tcd->tcd_tileno = tileno;
1122 tcd->tcd_tile = tcd->tcd_image->tiles;
1123 tcd->tcp = &tcd->cp->tcps[tileno];
1125 tile = tcd->tcd_tile;
1129 if(tcd->cur_tp_num == 0){
1130 tcd->encoding_time = opj_clock(); /* time needed to encode a tile */
1131 /* INDEX >> "Precinct_nb_X et Precinct_nb_Y" */
1132 if(image_info && image_info->index_on) {
1133 opj_tcd_tilecomp_t *tilec_idx = &tile->comps[0]; /* based on component 0 */
1134 for (i = 0; i < tilec_idx->numresolutions; i++) {
1135 opj_tcd_resolution_t *res_idx = &tilec_idx->resolutions[i];
1137 image_info->tile[tileno].pw[i] = res_idx->pw;
1138 image_info->tile[tileno].ph[i] = res_idx->ph;
1140 npck += res_idx->pw * res_idx->ph;
1142 image_info->tile[tileno].pdx[i] = tccp->prcw[i];
1143 image_info->tile[tileno].pdy[i] = tccp->prch[i];
1145 image_info->tile[tileno].packet = (opj_packet_info_t *) opj_malloc(image_info->comp * image_info->layer * npck * sizeof(opj_packet_info_t));
1149 /*---------------TILE-------------------*/
1151 for (compno = 0; compno < tile->numcomps; compno++) {
1154 int adjust = image->comps[compno].sgnd ? 0 : 1 << (image->comps[compno].prec - 1);
1155 int offset_x = int_ceildiv(image->x0, image->comps[compno].dx);
1156 int offset_y = int_ceildiv(image->y0, image->comps[compno].dy);
1158 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1159 int tw = tilec->x1 - tilec->x0;
1160 int w = int_ceildiv(image->x1 - image->x0, image->comps[compno].dx);
1162 /* extract tile data */
1164 if (tcd_tcp->tccps[compno].qmfbid == 1) {
1165 for (y = tilec->y0; y < tilec->y1; y++) {
1166 /* start of the src tile scanline */
1167 int *data = &image->comps[compno].data[(tilec->x0 - offset_x) + (y - offset_y) * w];
1168 /* start of the dst tile scanline */
1169 int *tile_data = &tilec->data[(y - tilec->y0) * tw];
1170 for (x = tilec->x0; x < tilec->x1; x++) {
1171 *tile_data++ = *data++ - adjust;
1174 } else if (tcd_tcp->tccps[compno].qmfbid == 0) {
1175 for (y = tilec->y0; y < tilec->y1; y++) {
1176 /* start of the src tile scanline */
1177 int *data = &image->comps[compno].data[(tilec->x0 - offset_x) + (y - offset_y) * w];
1178 /* start of the dst tile scanline */
1179 int *tile_data = &tilec->data[(y - tilec->y0) * tw];
1180 for (x = tilec->x0; x < tilec->x1; x++) {
1181 *tile_data++ = (*data++ - adjust) << 11;
1188 /*----------------MCT-------------------*/
1190 int samples = (tile->comps[0].x1 - tile->comps[0].x0) * (tile->comps[0].y1 - tile->comps[0].y0);
1191 if (tcd_tcp->tccps[0].qmfbid == 0) {
1192 mct_encode_real(tile->comps[0].data, tile->comps[1].data, tile->comps[2].data, samples);
1194 mct_encode(tile->comps[0].data, tile->comps[1].data, tile->comps[2].data, samples);
1198 /*----------------DWT---------------------*/
1200 for (compno = 0; compno < tile->numcomps; compno++) {
1201 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1202 if (tcd_tcp->tccps[compno].qmfbid == 1) {
1204 } else if (tcd_tcp->tccps[compno].qmfbid == 0) {
1205 dwt_encode_real(tilec);
1209 /*------------------TIER1-----------------*/
1210 t1 = t1_create(tcd->cinfo);
1211 t1_encode_cblks(t1, tile, tcd_tcp);
1214 /*-----------RATE-ALLOCATE------------------*/
1218 image_info->index_write = 0;
1220 if (cp->disto_alloc || cp->fixed_quality) { /* fixed_quality */
1221 /* Normal Rate/distortion allocation */
1222 tcd_rateallocate(tcd, dest, len, image_info);
1224 /* Fixed layer allocation */
1225 tcd_rateallocate_fixed(tcd);
1228 /*--------------TIER2------------------*/
1232 image_info->index_write = 1;
1235 t2 = t2_create(tcd->cinfo, image, cp);
1236 l = t2_encode_packets(t2,tileno, tile, tcd_tcp->numlayers, dest, len, image_info,tcd->tp_num,tcd->tp_pos,tcd->cur_pino,FINAL_PASS);
1239 /*---------------CLEAN-------------------*/
1242 if(tcd->cur_tp_num == tcd->cur_totnum_tp - 1){
1243 tcd->encoding_time = opj_clock() - tcd->encoding_time;
1244 opj_event_msg(tcd->cinfo, EVT_INFO, "- tile encoded in %f s\n", tcd->encoding_time);
1246 /* cleaning memory */
1247 for (compno = 0; compno < tile->numcomps; compno++) {
1248 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1249 opj_free(tilec->data);
1256 bool tcd_decode_tile(opj_tcd_t *tcd, unsigned char *src, int len, int tileno) {
1260 double tile_time, t1_time, dwt_time;
1261 opj_tcd_tile_t *tile = NULL;
1263 opj_t1_t *t1 = NULL; /* T1 component */
1264 opj_t2_t *t2 = NULL; /* T2 component */
1266 tcd->tcd_tileno = tileno;
1267 tcd->tcd_tile = &(tcd->tcd_image->tiles[tileno]);
1268 tcd->tcp = &(tcd->cp->tcps[tileno]);
1269 tile = tcd->tcd_tile;
1271 tile_time = opj_clock(); /* time needed to decode a tile */
1272 opj_event_msg(tcd->cinfo, EVT_INFO, "tile %d of %d\n", tileno + 1, tcd->cp->tw * tcd->cp->th);
1274 /*--------------TIER2------------------*/
1276 t2 = t2_create(tcd->cinfo, tcd->image, tcd->cp);
1277 l = t2_decode_packets(t2, src, len, tileno, tile);
1282 opj_event_msg(tcd->cinfo, EVT_ERROR, "tcd_decode: incomplete bistream\n");
1285 /*------------------TIER1-----------------*/
1287 t1_time = opj_clock(); /* time needed to decode a tile */
1288 t1 = t1_create(tcd->cinfo);
1289 t1_decode_cblks(t1, tile, tcd->tcp);
1291 t1_time = opj_clock() - t1_time;
1292 opj_event_msg(tcd->cinfo, EVT_INFO, "- tiers-1 took %f s\n", t1_time);
1294 /*----------------DWT---------------------*/
1296 dwt_time = opj_clock(); /* time needed to decode a tile */
1297 for (compno = 0; compno < tile->numcomps; compno++) {
1298 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1299 if (tcd->cp->reduce != 0) {
1300 tcd->image->comps[compno].resno_decoded =
1301 tile->comps[compno].numresolutions - tcd->cp->reduce - 1;
1304 if (tcd->tcp->tccps[compno].qmfbid == 1) {
1305 dwt_decode(tilec, tilec->numresolutions - 1 - tcd->image->comps[compno].resno_decoded);
1307 dwt_decode_real(tilec, tilec->numresolutions - 1 - tcd->image->comps[compno].resno_decoded);
1310 if (tile->comps[compno].numresolutions > 0) {
1311 tcd->image->comps[compno].factor = tile->comps[compno].numresolutions - (tcd->image->comps[compno].resno_decoded + 1);
1314 dwt_time = opj_clock() - dwt_time;
1315 opj_event_msg(tcd->cinfo, EVT_INFO, "- dwt took %f s\n", dwt_time);
1317 /*----------------MCT-------------------*/
1319 if (tcd->tcp->mct) {
1320 if (tcd->tcp->tccps[0].qmfbid == 1) {
1321 mct_decode(tile->comps[0].data, tile->comps[1].data, tile->comps[2].data,
1322 (tile->comps[0].x1 - tile->comps[0].x0) * (tile->comps[0].y1 - tile->comps[0].y0));
1324 mct_decode_real(tile->comps[0].data, tile->comps[1].data, tile->comps[2].data,
1325 (tile->comps[0].x1 - tile->comps[0].x0) * (tile->comps[0].y1 - tile->comps[0].y0));
1329 /*---------------TILE-------------------*/
1331 for (compno = 0; compno < tile->numcomps; compno++) {
1332 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1333 opj_tcd_resolution_t *res = &tilec->resolutions[tcd->image->comps[compno].resno_decoded];
1334 int adjust = tcd->image->comps[compno].sgnd ? 0 : 1 << (tcd->image->comps[compno].prec - 1);
1335 int min = tcd->image->comps[compno].sgnd ?
1336 -(1 << (tcd->image->comps[compno].prec - 1)) : 0;
1337 int max = tcd->image->comps[compno].sgnd ?
1338 (1 << (tcd->image->comps[compno].prec - 1)) - 1 : (1 << tcd->image->comps[compno].prec) - 1;
1340 int tw = tilec->x1 - tilec->x0;
1341 int w = tcd->image->comps[compno].w;
1344 int offset_x = int_ceildivpow2(tcd->image->comps[compno].x0, tcd->image->comps[compno].factor);
1345 int offset_y = int_ceildivpow2(tcd->image->comps[compno].y0, tcd->image->comps[compno].factor);
1347 for (j = res->y0; j < res->y1; j++) {
1348 for (i = res->x0; i < res->x1; i++) {
1350 float tmp = (float)((tilec->data[i - res->x0 + (j - res->y0) * tw]) / 8192.0);
1352 if (tcd->tcp->tccps[compno].qmfbid == 1) {
1353 v = tilec->data[i - res->x0 + (j - res->y0) * tw];
1355 int tmp2 = ((int) (floor(fabs(tmp)))) + ((int) floor(fabs(tmp*2))%2);
1356 v = ((tmp < 0) ? -tmp2:tmp2);
1360 tcd->image->comps[compno].data[(i - offset_x) + (j - offset_y) * w] = int_clamp(v, min, max);
1365 tile_time = opj_clock() - tile_time; /* time needed to decode a tile */
1366 opj_event_msg(tcd->cinfo, EVT_INFO, "- tile decoded in %f s\n", tile_time);
1368 for (compno = 0; compno < tile->numcomps; compno++) {
1369 opj_free(tcd->tcd_image->tiles[tileno].comps[compno].data);
1370 tcd->tcd_image->tiles[tileno].comps[compno].data = NULL;
1380 void tcd_free_decode(opj_tcd_t *tcd) {
1381 int tileno,compno,resno,bandno,precno;
1383 opj_tcd_image_t *tcd_image = tcd->tcd_image;
1385 for (tileno = 0; tileno < tcd_image->tw * tcd_image->th; tileno++) {
1386 opj_tcd_tile_t *tile = &tcd_image->tiles[tileno];
1387 for (compno = 0; compno < tile->numcomps; compno++) {
1388 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1389 for (resno = 0; resno < tilec->numresolutions; resno++) {
1390 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
1391 for (bandno = 0; bandno < res->numbands; bandno++) {
1392 opj_tcd_band_t *band = &res->bands[bandno];
1393 for (precno = 0; precno < res->ph * res->pw; precno++) {
1394 opj_tcd_precinct_t *prec = &band->precincts[precno];
1395 if (prec->cblks != NULL) opj_free(prec->cblks);
1396 if (prec->imsbtree != NULL) tgt_destroy(prec->imsbtree);
1397 if (prec->incltree != NULL) tgt_destroy(prec->incltree);
1399 if (band->precincts != NULL) opj_free(band->precincts);
1402 if (tilec->resolutions != NULL) opj_free(tilec->resolutions);
1404 if (tile->comps != NULL) opj_free(tile->comps);
1407 if (tcd_image->tiles != NULL) opj_free(tcd_image->tiles);