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] ?
155 (((float) (tile->numcomps
156 * (tile->x1 - tile->x0)
157 * (tile->y1 - tile->y0)
158 * image->comps[0].prec))
159 /(tcp->rates[j] * 8 * image->comps[0].dx * image->comps[0].dy)) - (((tcd->cur_totnum_tp - 1) * 14 )/ tcp->numlayers)
161 ((float) (tile->numcomps
162 * (tile->x1 - tile->x0)
163 * (tile->y1 - tile->y0)
164 * image->comps[0].prec))/
165 (tcp->rates[j] * 8 * image->comps[0].dx * image->comps[0].dy)
169 if (j && tcp->rates[j] < tcp->rates[j - 1] + 10) {
170 tcp->rates[j] = tcp->rates[j - 1] + 20;
172 if (!j && tcp->rates[j] < 30)
176 if(j == (tcp->numlayers-1)){
177 tcp->rates[j] = tcp->rates[j]- 2;
181 /* << Modification of the RATE */
183 tile->comps = (opj_tcd_tilecomp_t *) opj_malloc(image->numcomps * sizeof(opj_tcd_tilecomp_t));
184 for (compno = 0; compno < tile->numcomps; compno++) {
185 opj_tccp_t *tccp = &tcp->tccps[compno];
187 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
189 /* border of each tile component (global) */
190 tilec->x0 = int_ceildiv(tile->x0, image->comps[compno].dx);
191 tilec->y0 = int_ceildiv(tile->y0, image->comps[compno].dy);
192 tilec->x1 = int_ceildiv(tile->x1, image->comps[compno].dx);
193 tilec->y1 = int_ceildiv(tile->y1, image->comps[compno].dy);
195 tilec->data = (int *) opj_aligned_malloc((tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0) * sizeof(int));
196 tilec->numresolutions = tccp->numresolutions;
198 tilec->resolutions = (opj_tcd_resolution_t *) opj_malloc(tilec->numresolutions * sizeof(opj_tcd_resolution_t));
200 for (resno = 0; resno < tilec->numresolutions; resno++) {
202 int levelno = tilec->numresolutions - 1 - resno;
203 int tlprcxstart, tlprcystart, brprcxend, brprcyend;
204 int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend;
205 int cbgwidthexpn, cbgheightexpn;
206 int cblkwidthexpn, cblkheightexpn;
208 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
210 /* border for each resolution level (global) */
211 res->x0 = int_ceildivpow2(tilec->x0, levelno);
212 res->y0 = int_ceildivpow2(tilec->y0, levelno);
213 res->x1 = int_ceildivpow2(tilec->x1, levelno);
214 res->y1 = int_ceildivpow2(tilec->y1, levelno);
216 res->numbands = resno == 0 ? 1 : 3;
217 /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */
218 if (tccp->csty & J2K_CCP_CSTY_PRT) {
219 pdx = tccp->prcw[resno];
220 pdy = tccp->prch[resno];
225 /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
226 tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx;
227 tlprcystart = int_floordivpow2(res->y0, pdy) << pdy;
229 brprcxend = int_ceildivpow2(res->x1, pdx) << pdx;
230 brprcyend = int_ceildivpow2(res->y1, pdy) << pdy;
232 res->pw = (brprcxend - tlprcxstart) >> pdx;
233 res->ph = (brprcyend - tlprcystart) >> pdy;
236 tlcbgxstart = tlprcxstart;
237 tlcbgystart = tlprcystart;
238 brcbgxend = brprcxend;
239 brcbgyend = brprcyend;
243 tlcbgxstart = int_ceildivpow2(tlprcxstart, 1);
244 tlcbgystart = int_ceildivpow2(tlprcystart, 1);
245 brcbgxend = int_ceildivpow2(brprcxend, 1);
246 brcbgyend = int_ceildivpow2(brprcyend, 1);
247 cbgwidthexpn = pdx - 1;
248 cbgheightexpn = pdy - 1;
251 cblkwidthexpn = int_min(tccp->cblkw, cbgwidthexpn);
252 cblkheightexpn = int_min(tccp->cblkh, cbgheightexpn);
254 for (bandno = 0; bandno < res->numbands; bandno++) {
257 opj_stepsize_t *ss = NULL;
259 opj_tcd_band_t *band = &res->bands[bandno];
261 band->bandno = resno == 0 ? 0 : bandno + 1;
262 x0b = (band->bandno == 1) || (band->bandno == 3) ? 1 : 0;
263 y0b = (band->bandno == 2) || (band->bandno == 3) ? 1 : 0;
265 if (band->bandno == 0) {
266 /* band border (global) */
267 band->x0 = int_ceildivpow2(tilec->x0, levelno);
268 band->y0 = int_ceildivpow2(tilec->y0, levelno);
269 band->x1 = int_ceildivpow2(tilec->x1, levelno);
270 band->y1 = int_ceildivpow2(tilec->y1, levelno);
272 /* band border (global) */
273 band->x0 = int_ceildivpow2(tilec->x0 - (1 << levelno) * x0b, levelno + 1);
274 band->y0 = int_ceildivpow2(tilec->y0 - (1 << levelno) * y0b, levelno + 1);
275 band->x1 = int_ceildivpow2(tilec->x1 - (1 << levelno) * x0b, levelno + 1);
276 band->y1 = int_ceildivpow2(tilec->y1 - (1 << levelno) * y0b, levelno + 1);
279 ss = &tccp->stepsizes[resno == 0 ? 0 : 3 * (resno - 1) + bandno + 1];
280 gain = tccp->qmfbid == 0 ? dwt_getgain_real(band->bandno) : dwt_getgain(band->bandno);
281 numbps = image->comps[compno].prec + gain;
283 band->stepsize = (float)((1.0 + ss->mant / 2048.0) * pow(2.0, numbps - ss->expn));
284 band->numbps = ss->expn + tccp->numgbits - 1; /* WHY -1 ? */
286 band->precincts = (opj_tcd_precinct_t *) opj_malloc(3 * res->pw * res->ph * sizeof(opj_tcd_precinct_t));
288 for (i = 0; i < res->pw * res->ph * 3; i++) {
289 band->precincts[i].imsbtree = NULL;
290 band->precincts[i].incltree = NULL;
293 for (precno = 0; precno < res->pw * res->ph; precno++) {
294 int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
296 int cbgxstart = tlcbgxstart + (precno % res->pw) * (1 << cbgwidthexpn);
297 int cbgystart = tlcbgystart + (precno / res->pw) * (1 << cbgheightexpn);
298 int cbgxend = cbgxstart + (1 << cbgwidthexpn);
299 int cbgyend = cbgystart + (1 << cbgheightexpn);
301 opj_tcd_precinct_t *prc = &band->precincts[precno];
303 /* precinct size (global) */
304 prc->x0 = int_max(cbgxstart, band->x0);
305 prc->y0 = int_max(cbgystart, band->y0);
306 prc->x1 = int_min(cbgxend, band->x1);
307 prc->y1 = int_min(cbgyend, band->y1);
309 tlcblkxstart = int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn;
310 tlcblkystart = int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn;
311 brcblkxend = int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn;
312 brcblkyend = int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn;
313 prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;
314 prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn;
316 prc->cblks = (opj_tcd_cblk_t *) opj_malloc((prc->cw * prc->ch) * sizeof(opj_tcd_cblk_t));
317 prc->incltree = tgt_create(prc->cw, prc->ch);
318 prc->imsbtree = tgt_create(prc->cw, prc->ch);
320 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
321 int cblkxstart = tlcblkxstart + (cblkno % prc->cw) * (1 << cblkwidthexpn);
322 int cblkystart = tlcblkystart + (cblkno / prc->cw) * (1 << cblkheightexpn);
323 int cblkxend = cblkxstart + (1 << cblkwidthexpn);
324 int cblkyend = cblkystart + (1 << cblkheightexpn);
326 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
328 /* code-block size (global) */
329 cblk->x0 = int_max(cblkxstart, prc->x0);
330 cblk->y0 = int_max(cblkystart, prc->y0);
331 cblk->x1 = int_min(cblkxend, prc->x1);
332 cblk->y1 = int_min(cblkyend, prc->y1);
340 /* tcd_dump(stdout, tcd, &tcd->tcd_image); */
343 void tcd_free_encode(opj_tcd_t *tcd) {
344 int tileno, compno, resno, bandno, precno;
346 for (tileno = 0; tileno < 1; tileno++) {
347 opj_tcd_tile_t *tile = tcd->tcd_image->tiles;
349 for (compno = 0; compno < tile->numcomps; compno++) {
350 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
352 for (resno = 0; resno < tilec->numresolutions; resno++) {
353 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
355 for (bandno = 0; bandno < res->numbands; bandno++) {
356 opj_tcd_band_t *band = &res->bands[bandno];
358 for (precno = 0; precno < res->pw * res->ph; precno++) {
359 opj_tcd_precinct_t *prc = &band->precincts[precno];
361 if (prc->incltree != NULL) {
362 tgt_destroy(prc->incltree);
363 prc->incltree = NULL;
365 if (prc->imsbtree != NULL) {
366 tgt_destroy(prc->imsbtree);
367 prc->imsbtree = NULL;
369 opj_free(prc->cblks);
372 opj_free(band->precincts);
373 band->precincts = NULL;
376 opj_free(tilec->resolutions);
377 tilec->resolutions = NULL;
379 opj_free(tile->comps);
382 opj_free(tcd->tcd_image->tiles);
383 tcd->tcd_image->tiles = NULL;
386 void tcd_init_encode(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp, int curtileno) {
387 int tileno, compno, resno, bandno, precno, cblkno;
389 for (tileno = 0; tileno < 1; tileno++) {
390 opj_tcp_t *tcp = &cp->tcps[curtileno];
392 /* cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
393 int p = curtileno % cp->tw;
394 int q = curtileno / cp->tw;
396 opj_tcd_tile_t *tile = tcd->tcd_image->tiles;
398 /* 4 borders of the tile rescale on the image if necessary */
399 tile->x0 = int_max(cp->tx0 + p * cp->tdx, image->x0);
400 tile->y0 = int_max(cp->ty0 + q * cp->tdy, image->y0);
401 tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, image->x1);
402 tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, image->y1);
404 tile->numcomps = image->numcomps;
405 /* tile->PPT=image->PPT; */
407 /* Modification of the RATE >> */
408 for (j = 0; j < tcp->numlayers; j++) {
409 tcp->rates[j] = tcp->rates[j] ?
410 ((float) (tile->numcomps
411 * (tile->x1 - tile->x0)
412 * (tile->y1 - tile->y0)
413 * image->comps[0].prec))/
414 (tcp->rates[j] * 8 * image->comps[0].dx * image->comps[0].dy)
418 if (j && tcp->rates[j] < tcp->rates[j - 1] + 10) {
419 tcp->rates[j] = tcp->rates[j - 1] + 20;
421 if (!j && tcp->rates[j] < 30)
426 /* << Modification of the RATE */
428 /* tile->comps=(opj_tcd_tilecomp_t*)opj_realloc(tile->comps,image->numcomps*sizeof(opj_tcd_tilecomp_t)); */
429 for (compno = 0; compno < tile->numcomps; compno++) {
430 opj_tccp_t *tccp = &tcp->tccps[compno];
432 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
434 /* border of each tile component (global) */
435 tilec->x0 = int_ceildiv(tile->x0, image->comps[compno].dx);
436 tilec->y0 = int_ceildiv(tile->y0, image->comps[compno].dy);
437 tilec->x1 = int_ceildiv(tile->x1, image->comps[compno].dx);
438 tilec->y1 = int_ceildiv(tile->y1, image->comps[compno].dy);
440 tilec->data = (int *) opj_aligned_malloc((tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0) * sizeof(int));
441 tilec->numresolutions = tccp->numresolutions;
442 /* tilec->resolutions=(opj_tcd_resolution_t*)opj_realloc(tilec->resolutions,tilec->numresolutions*sizeof(opj_tcd_resolution_t)); */
443 for (resno = 0; resno < tilec->numresolutions; resno++) {
446 int levelno = tilec->numresolutions - 1 - resno;
447 int tlprcxstart, tlprcystart, brprcxend, brprcyend;
448 int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend;
449 int cbgwidthexpn, cbgheightexpn;
450 int cblkwidthexpn, cblkheightexpn;
452 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
454 /* border for each resolution level (global) */
455 res->x0 = int_ceildivpow2(tilec->x0, levelno);
456 res->y0 = int_ceildivpow2(tilec->y0, levelno);
457 res->x1 = int_ceildivpow2(tilec->x1, levelno);
458 res->y1 = int_ceildivpow2(tilec->y1, levelno);
459 res->numbands = resno == 0 ? 1 : 3;
461 /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */
462 if (tccp->csty & J2K_CCP_CSTY_PRT) {
463 pdx = tccp->prcw[resno];
464 pdy = tccp->prch[resno];
469 /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
470 tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx;
471 tlprcystart = int_floordivpow2(res->y0, pdy) << pdy;
472 brprcxend = int_ceildivpow2(res->x1, pdx) << pdx;
473 brprcyend = int_ceildivpow2(res->y1, pdy) << pdy;
475 res->pw = (brprcxend - tlprcxstart) >> pdx;
476 res->ph = (brprcyend - tlprcystart) >> pdy;
479 tlcbgxstart = tlprcxstart;
480 tlcbgystart = tlprcystart;
481 brcbgxend = brprcxend;
482 brcbgyend = brprcyend;
486 tlcbgxstart = int_ceildivpow2(tlprcxstart, 1);
487 tlcbgystart = int_ceildivpow2(tlprcystart, 1);
488 brcbgxend = int_ceildivpow2(brprcxend, 1);
489 brcbgyend = int_ceildivpow2(brprcyend, 1);
490 cbgwidthexpn = pdx - 1;
491 cbgheightexpn = pdy - 1;
494 cblkwidthexpn = int_min(tccp->cblkw, cbgwidthexpn);
495 cblkheightexpn = int_min(tccp->cblkh, cbgheightexpn);
497 for (bandno = 0; bandno < res->numbands; bandno++) {
500 opj_stepsize_t *ss = NULL;
502 opj_tcd_band_t *band = &res->bands[bandno];
504 band->bandno = resno == 0 ? 0 : bandno + 1;
505 x0b = (band->bandno == 1) || (band->bandno == 3) ? 1 : 0;
506 y0b = (band->bandno == 2) || (band->bandno == 3) ? 1 : 0;
508 if (band->bandno == 0) {
510 band->x0 = int_ceildivpow2(tilec->x0, levelno);
511 band->y0 = int_ceildivpow2(tilec->y0, levelno);
512 band->x1 = int_ceildivpow2(tilec->x1, levelno);
513 band->y1 = int_ceildivpow2(tilec->y1, levelno);
515 band->x0 = int_ceildivpow2(tilec->x0 - (1 << levelno) * x0b, levelno + 1);
516 band->y0 = int_ceildivpow2(tilec->y0 - (1 << levelno) * y0b, levelno + 1);
517 band->x1 = int_ceildivpow2(tilec->x1 - (1 << levelno) * x0b, levelno + 1);
518 band->y1 = int_ceildivpow2(tilec->y1 - (1 << levelno) * y0b, levelno + 1);
521 ss = &tccp->stepsizes[resno == 0 ? 0 : 3 * (resno - 1) + bandno + 1];
522 gain = tccp->qmfbid == 0 ? dwt_getgain_real(band->bandno) : dwt_getgain(band->bandno);
523 numbps = image->comps[compno].prec + gain;
524 band->stepsize = (float)((1.0 + ss->mant / 2048.0) * pow(2.0, numbps - ss->expn));
525 band->numbps = ss->expn + tccp->numgbits - 1; /* WHY -1 ? */
527 for (precno = 0; precno < res->pw * res->ph; precno++) {
528 int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
530 int cbgxstart = tlcbgxstart + (precno % res->pw) * (1 << cbgwidthexpn);
531 int cbgystart = tlcbgystart + (precno / res->pw) * (1 << cbgheightexpn);
532 int cbgxend = cbgxstart + (1 << cbgwidthexpn);
533 int cbgyend = cbgystart + (1 << cbgheightexpn);
535 opj_tcd_precinct_t *prc = &band->precincts[precno];
537 /* precinct size (global) */
538 prc->x0 = int_max(cbgxstart, band->x0);
539 prc->y0 = int_max(cbgystart, band->y0);
540 prc->x1 = int_min(cbgxend, band->x1);
541 prc->y1 = int_min(cbgyend, band->y1);
543 tlcblkxstart = int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn;
544 tlcblkystart = int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn;
545 brcblkxend = int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn;
546 brcblkyend = int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn;
547 prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;
548 prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn;
550 opj_free(prc->cblks);
551 prc->cblks = (opj_tcd_cblk_t *) opj_malloc(prc->cw * prc->ch * sizeof(opj_tcd_cblk_t));
553 if (prc->incltree != NULL) {
554 tgt_destroy(prc->incltree);
556 if (prc->imsbtree != NULL) {
557 tgt_destroy(prc->imsbtree);
560 prc->incltree = tgt_create(prc->cw, prc->ch);
561 prc->imsbtree = tgt_create(prc->cw, prc->ch);
563 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
564 int cblkxstart = tlcblkxstart + (cblkno % prc->cw) * (1 << cblkwidthexpn);
565 int cblkystart = tlcblkystart + (cblkno / prc->cw) * (1 << cblkheightexpn);
566 int cblkxend = cblkxstart + (1 << cblkwidthexpn);
567 int cblkyend = cblkystart + (1 << cblkheightexpn);
569 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
571 /* code-block size (global) */
572 cblk->x0 = int_max(cblkxstart, prc->x0);
573 cblk->y0 = int_max(cblkystart, prc->y0);
574 cblk->x1 = int_min(cblkxend, prc->x1);
575 cblk->y1 = int_min(cblkyend, prc->y1);
583 /* tcd_dump(stdout, tcd, &tcd->tcd_image); */
586 void tcd_malloc_decode(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp) {
587 int i, j, tileno, p, q;
588 unsigned int x0 = 0, y0 = 0, x1 = 0, y1 = 0, w, h;
591 tcd->tcd_image->tw = cp->tw;
592 tcd->tcd_image->th = cp->th;
593 tcd->tcd_image->tiles = (opj_tcd_tile_t *) opj_malloc(cp->tw * cp->th * sizeof(opj_tcd_tile_t));
596 Allocate place to store the decoded data = final image
597 Place limited by the tile really present in the codestream
600 for (j = 0; j < cp->tileno_size; j++) {
601 opj_tcd_tile_t *tile;
603 tileno = cp->tileno[j];
605 tile = &(tcd->tcd_image->tiles[cp->tileno[tileno]]);
607 tile->numcomps = image->numcomps;
608 tile->comps = (opj_tcd_tilecomp_t *) opj_malloc(image->numcomps * sizeof(opj_tcd_tilecomp_t));
612 for (i = 0; i < image->numcomps; i++) {
613 for (j = 0; j < cp->tileno_size; j++) {
614 opj_tcd_tile_t *tile;
615 opj_tcd_tilecomp_t *tilec;
617 /* cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
619 tileno = cp->tileno[j];
621 tile = &(tcd->tcd_image->tiles[cp->tileno[tileno]]);
622 tilec = &tile->comps[i];
624 p = tileno % cp->tw; /* si numerotation matricielle .. */
625 q = tileno / cp->tw; /* .. coordonnees de la tile (q,p) q pour ligne et p pour colonne */
627 /* 4 borders of the tile rescale on the image if necessary */
628 tile->x0 = int_max(cp->tx0 + p * cp->tdx, image->x0);
629 tile->y0 = int_max(cp->ty0 + q * cp->tdy, image->y0);
630 tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, image->x1);
631 tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, image->y1);
633 tilec->x0 = int_ceildiv(tile->x0, image->comps[i].dx);
634 tilec->y0 = int_ceildiv(tile->y0, image->comps[i].dy);
635 tilec->x1 = int_ceildiv(tile->x1, image->comps[i].dx);
636 tilec->y1 = int_ceildiv(tile->y1, image->comps[i].dy);
638 x0 = j == 0 ? tilec->x0 : int_min(x0, (unsigned int) tilec->x0);
639 y0 = j == 0 ? tilec->y0 : int_min(y0, (unsigned int) tilec->x0);
640 x1 = j == 0 ? tilec->x1 : int_max(x1, (unsigned int) tilec->x1);
641 y1 = j == 0 ? tilec->y1 : int_max(y1, (unsigned int) tilec->y1);
647 image->comps[i].data = (int *) opj_malloc(w * h * sizeof(int));
648 image->comps[i].w = w;
649 image->comps[i].h = h;
650 image->comps[i].x0 = x0;
651 image->comps[i].y0 = y0;
656 void tcd_malloc_decode_tile(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp, int tileno) {
657 int compno, resno, bandno, precno, cblkno;
658 unsigned int x0 = 0, y0 = 0, x1 = 0, y1 = 0;
660 opj_tcd_tile_t *tile;
664 tcp = &(cp->tcps[cp->tileno[tileno]]);
665 tile = &(tcd->tcd_image->tiles[cp->tileno[tileno]]);
667 tileno = cp->tileno[tileno];
669 for (compno = 0; compno < tile->numcomps; compno++) {
670 opj_tccp_t *tccp = &tcp->tccps[compno];
671 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
673 /* border of each tile component (global) */
674 tilec->x0 = int_ceildiv(tile->x0, image->comps[compno].dx);
675 tilec->y0 = int_ceildiv(tile->y0, image->comps[compno].dy);
676 tilec->x1 = int_ceildiv(tile->x1, image->comps[compno].dx);
677 tilec->y1 = int_ceildiv(tile->y1, image->comps[compno].dy);
679 tilec->data = (int*) opj_aligned_malloc((tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0) * sizeof(int));
680 tilec->numresolutions = tccp->numresolutions;
681 tilec->resolutions = (opj_tcd_resolution_t *) opj_malloc(tilec->numresolutions * sizeof(opj_tcd_resolution_t));
683 for (resno = 0; resno < tilec->numresolutions; resno++) {
685 int levelno = tilec->numresolutions - 1 - resno;
686 int tlprcxstart, tlprcystart, brprcxend, brprcyend;
687 int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend;
688 int cbgwidthexpn, cbgheightexpn;
689 int cblkwidthexpn, cblkheightexpn;
691 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
693 /* border for each resolution level (global) */
694 res->x0 = int_ceildivpow2(tilec->x0, levelno);
695 res->y0 = int_ceildivpow2(tilec->y0, levelno);
696 res->x1 = int_ceildivpow2(tilec->x1, levelno);
697 res->y1 = int_ceildivpow2(tilec->y1, levelno);
698 res->numbands = resno == 0 ? 1 : 3;
700 /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */
701 if (tccp->csty & J2K_CCP_CSTY_PRT) {
702 pdx = tccp->prcw[resno];
703 pdy = tccp->prch[resno];
709 /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
710 tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx;
711 tlprcystart = int_floordivpow2(res->y0, pdy) << pdy;
712 brprcxend = int_ceildivpow2(res->x1, pdx) << pdx;
713 brprcyend = int_ceildivpow2(res->y1, pdy) << pdy;
715 res->pw = (res->x0 == res->x1) ? 0 : ((brprcxend - tlprcxstart) >> pdx);
716 res->ph = (res->y0 == res->y1) ? 0 : ((brprcyend - tlprcystart) >> pdy);
719 tlcbgxstart = tlprcxstart;
720 tlcbgystart = tlprcystart;
721 brcbgxend = brprcxend;
722 brcbgyend = brprcyend;
726 tlcbgxstart = int_ceildivpow2(tlprcxstart, 1);
727 tlcbgystart = int_ceildivpow2(tlprcystart, 1);
728 brcbgxend = int_ceildivpow2(brprcxend, 1);
729 brcbgyend = int_ceildivpow2(brprcyend, 1);
730 cbgwidthexpn = pdx - 1;
731 cbgheightexpn = pdy - 1;
734 cblkwidthexpn = int_min(tccp->cblkw, cbgwidthexpn);
735 cblkheightexpn = int_min(tccp->cblkh, cbgheightexpn);
737 for (bandno = 0; bandno < res->numbands; bandno++) {
740 opj_stepsize_t *ss = NULL;
742 opj_tcd_band_t *band = &res->bands[bandno];
743 band->bandno = resno == 0 ? 0 : bandno + 1;
744 x0b = (band->bandno == 1) || (band->bandno == 3) ? 1 : 0;
745 y0b = (band->bandno == 2) || (band->bandno == 3) ? 1 : 0;
747 if (band->bandno == 0) {
748 /* band border (global) */
749 band->x0 = int_ceildivpow2(tilec->x0, levelno);
750 band->y0 = int_ceildivpow2(tilec->y0, levelno);
751 band->x1 = int_ceildivpow2(tilec->x1, levelno);
752 band->y1 = int_ceildivpow2(tilec->y1, levelno);
754 /* band border (global) */
755 band->x0 = int_ceildivpow2(tilec->x0 - (1 << levelno) * x0b, levelno + 1);
756 band->y0 = int_ceildivpow2(tilec->y0 - (1 << levelno) * y0b, levelno + 1);
757 band->x1 = int_ceildivpow2(tilec->x1 - (1 << levelno) * x0b, levelno + 1);
758 band->y1 = int_ceildivpow2(tilec->y1 - (1 << levelno) * y0b, levelno + 1);
761 ss = &tccp->stepsizes[resno == 0 ? 0 : 3 * (resno - 1) + bandno + 1];
762 gain = tccp->qmfbid == 0 ? dwt_getgain_real(band->bandno) : dwt_getgain(band->bandno);
763 numbps = image->comps[compno].prec + gain;
764 band->stepsize = (float)((1.0 + ss->mant / 2048.0) * pow(2.0, numbps - ss->expn));
765 band->numbps = ss->expn + tccp->numgbits - 1; /* WHY -1 ? */
767 band->precincts = (opj_tcd_precinct_t *) opj_malloc(res->pw * res->ph * sizeof(opj_tcd_precinct_t));
769 for (precno = 0; precno < res->pw * res->ph; precno++) {
770 int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
771 int cbgxstart = tlcbgxstart + (precno % res->pw) * (1 << cbgwidthexpn);
772 int cbgystart = tlcbgystart + (precno / res->pw) * (1 << cbgheightexpn);
773 int cbgxend = cbgxstart + (1 << cbgwidthexpn);
774 int cbgyend = cbgystart + (1 << cbgheightexpn);
776 opj_tcd_precinct_t *prc = &band->precincts[precno];
777 /* precinct size (global) */
778 prc->x0 = int_max(cbgxstart, band->x0);
779 prc->y0 = int_max(cbgystart, band->y0);
780 prc->x1 = int_min(cbgxend, band->x1);
781 prc->y1 = int_min(cbgyend, band->y1);
783 tlcblkxstart = int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn;
784 tlcblkystart = int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn;
785 brcblkxend = int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn;
786 brcblkyend = int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn;
787 prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;
788 prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn;
790 prc->cblks = (opj_tcd_cblk_t *) opj_malloc(prc->cw * prc->ch * sizeof(opj_tcd_cblk_t));
792 prc->incltree = tgt_create(prc->cw, prc->ch);
793 prc->imsbtree = tgt_create(prc->cw, prc->ch);
795 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
796 int cblkxstart = tlcblkxstart + (cblkno % prc->cw) * (1 << cblkwidthexpn);
797 int cblkystart = tlcblkystart + (cblkno / prc->cw) * (1 << cblkheightexpn);
798 int cblkxend = cblkxstart + (1 << cblkwidthexpn);
799 int cblkyend = cblkystart + (1 << cblkheightexpn);
801 /* code-block size (global) */
802 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
803 cblk->x0 = int_max(cblkxstart, prc->x0);
804 cblk->y0 = int_max(cblkystart, prc->y0);
805 cblk->x1 = int_min(cblkxend, prc->x1);
806 cblk->y1 = int_min(cblkyend, prc->y1);
813 /* tcd_dump(stdout, tcd, &tcd->tcd_image); */
816 void tcd_makelayer_fixed(opj_tcd_t *tcd, int layno, int final) {
817 int compno, resno, bandno, precno, cblkno;
818 int value; /*, matrice[tcd_tcp->numlayers][tcd_tile->comps[0].numresolutions][3]; */
819 int matrice[10][10][3];
822 opj_cp_t *cp = tcd->cp;
823 opj_tcd_tile_t *tcd_tile = tcd->tcd_tile;
824 opj_tcp_t *tcd_tcp = tcd->tcp;
826 /*matrice=(int*)opj_malloc(tcd_tcp->numlayers*tcd_tile->comps[0].numresolutions*3*sizeof(int)); */
828 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
829 opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
830 for (i = 0; i < tcd_tcp->numlayers; i++) {
831 for (j = 0; j < tilec->numresolutions; j++) {
832 for (k = 0; k < 3; k++) {
834 (int) (cp->matrice[i * tilec->numresolutions * 3 + j * 3 + k]
835 * (float) (tcd->image->comps[compno].prec / 16.0));
840 for (resno = 0; resno < tilec->numresolutions; resno++) {
841 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
842 for (bandno = 0; bandno < res->numbands; bandno++) {
843 opj_tcd_band_t *band = &res->bands[bandno];
844 for (precno = 0; precno < res->pw * res->ph; precno++) {
845 opj_tcd_precinct_t *prc = &band->precincts[precno];
846 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
847 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
848 opj_tcd_layer_t *layer = &cblk->layers[layno];
850 int imsb = tcd->image->comps[compno].prec - cblk->numbps; /* number of bit-plan equal to zero */
851 /* Correction of the matrix of coefficient to include the IMSB information */
853 value = matrice[layno][resno][bandno];
860 value = matrice[layno][resno][bandno] - matrice[layno - 1][resno][bandno];
861 if (imsb >= matrice[layno - 1][resno][bandno]) {
862 value -= (imsb - matrice[layno - 1][resno][bandno]);
870 cblk->numpassesinlayers = 0;
873 n = cblk->numpassesinlayers;
874 if (cblk->numpassesinlayers == 0) {
876 n = 3 * value - 2 + cblk->numpassesinlayers;
878 n = cblk->numpassesinlayers;
881 n = 3 * value + cblk->numpassesinlayers;
884 layer->numpasses = n - cblk->numpassesinlayers;
886 if (!layer->numpasses)
889 if (cblk->numpassesinlayers == 0) {
890 layer->len = cblk->passes[n - 1].rate;
891 layer->data = cblk->data;
893 layer->len = cblk->passes[n - 1].rate - cblk->passes[cblk->numpassesinlayers - 1].rate;
894 layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate;
897 cblk->numpassesinlayers = n;
905 void tcd_rateallocate_fixed(opj_tcd_t *tcd) {
907 for (layno = 0; layno < tcd->tcp->numlayers; layno++) {
908 tcd_makelayer_fixed(tcd, layno, 1);
912 void tcd_makelayer(opj_tcd_t *tcd, int layno, double thresh, int final) {
913 int compno, resno, bandno, precno, cblkno, passno;
915 opj_tcd_tile_t *tcd_tile = tcd->tcd_tile;
917 tcd_tile->distolayer[layno] = 0; /* fixed_quality */
919 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
920 opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
921 for (resno = 0; resno < tilec->numresolutions; resno++) {
922 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
923 for (bandno = 0; bandno < res->numbands; bandno++) {
924 opj_tcd_band_t *band = &res->bands[bandno];
925 for (precno = 0; precno < res->pw * res->ph; precno++) {
926 opj_tcd_precinct_t *prc = &band->precincts[precno];
927 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
928 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
929 opj_tcd_layer_t *layer = &cblk->layers[layno];
933 cblk->numpassesinlayers = 0;
935 n = cblk->numpassesinlayers;
936 for (passno = cblk->numpassesinlayers; passno < cblk->totalpasses; passno++) {
939 opj_tcd_pass_t *pass = &cblk->passes[passno];
942 dd = pass->distortiondec;
944 dr = pass->rate - cblk->passes[n - 1].rate;
945 dd = pass->distortiondec - cblk->passes[n - 1].distortiondec;
952 if (dd / dr >= thresh)
955 layer->numpasses = n - cblk->numpassesinlayers;
957 if (!layer->numpasses) {
961 if (cblk->numpassesinlayers == 0) {
962 layer->len = cblk->passes[n - 1].rate;
963 layer->data = cblk->data;
964 layer->disto = cblk->passes[n - 1].distortiondec;
966 layer->len = cblk->passes[n - 1].rate - cblk->passes[cblk->numpassesinlayers - 1].rate;
967 layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate;
968 layer->disto = cblk->passes[n - 1].distortiondec - cblk->passes[cblk->numpassesinlayers - 1].distortiondec;
971 tcd_tile->distolayer[layno] += layer->disto; /* fixed_quality */
974 cblk->numpassesinlayers = n;
982 bool tcd_rateallocate(opj_tcd_t *tcd, unsigned char *dest, int len, opj_image_info_t * image_info) {
983 int compno, resno, bandno, precno, cblkno, passno, layno;
985 double cumdisto[100]; /* fixed_quality */
986 const double K = 1; /* 1.1; fixed_quality */
989 opj_cp_t *cp = tcd->cp;
990 opj_tcd_tile_t *tcd_tile = tcd->tcd_tile;
991 opj_tcp_t *tcd_tcp = tcd->tcp;
996 tcd_tile->nbpix = 0; /* fixed_quality */
998 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
999 opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
1002 for (resno = 0; resno < tilec->numresolutions; resno++) {
1003 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
1005 for (bandno = 0; bandno < res->numbands; bandno++) {
1006 opj_tcd_band_t *band = &res->bands[bandno];
1008 for (precno = 0; precno < res->pw * res->ph; precno++) {
1009 opj_tcd_precinct_t *prc = &band->precincts[precno];
1011 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
1012 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
1014 for (passno = 0; passno < cblk->totalpasses; passno++) {
1015 opj_tcd_pass_t *pass = &cblk->passes[passno];
1020 dd = pass->distortiondec;
1022 dr = pass->rate - cblk->passes[passno - 1].rate;
1023 dd = pass->distortiondec - cblk->passes[passno - 1].distortiondec;
1029 if (rdslope < min) {
1032 if (rdslope > max) {
1038 tcd_tile->nbpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0));
1039 tilec->nbpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0));
1045 maxSE += (((double)(1 << tcd->image->comps[compno].prec) - 1.0)
1046 * ((double)(1 << tcd->image->comps[compno].prec) -1.0))
1047 * ((double)(tilec->nbpix));
1051 if(image_info && image_info->index_on) {
1052 opj_tile_info_t *tile_info = &image_info->tile[tcd->tcd_tileno];
1053 tile_info->nbpix = tcd_tile->nbpix;
1054 tile_info->distotile = tcd_tile->distotile;
1055 tile_info->thresh = (double *) opj_malloc(tcd_tcp->numlayers * sizeof(double));
1058 for (layno = 0; layno < tcd_tcp->numlayers; layno++) {
1062 int maxlen = tcd_tcp->rates[layno] ? int_min(((int) ceil(tcd_tcp->rates[layno])), len) : len;
1063 double goodthresh = 0;
1064 double stable_thresh = 0;
1066 double distotarget; /* fixed_quality */
1069 distotarget = tcd_tile->distotile - ((K * maxSE) / pow((float)10, tcd_tcp->distoratio[layno] / 10));
1071 if ((tcd_tcp->rates[layno]) || (cp->disto_alloc == 0)) {
1072 opj_t2_t *t2 = t2_create(tcd->cinfo, tcd->image, cp);
1074 for (i = 0; i < 32; i++) {
1075 double thresh = (lo + hi) / 2;
1077 double distoachieved = 0; /* fixed_quality */
1079 tcd_makelayer(tcd, layno, thresh, 0);
1081 if (cp->fixed_quality) { /* fixed_quality */
1083 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);
1088 distoachieved = layno == 0 ?
1089 tcd_tile->distolayer[0] : cumdisto[layno - 1] + tcd_tile->distolayer[layno];
1090 if (distoachieved < distotarget) {
1092 stable_thresh = thresh;
1099 distoachieved = (layno == 0) ?
1100 tcd_tile->distolayer[0] : (cumdisto[layno - 1] + tcd_tile->distolayer[layno]);
1101 if (distoachieved < distotarget) {
1103 stable_thresh = thresh;
1109 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);
1110 /* TODO: what to do with l ??? seek / tell ??? */
1111 /* opj_event_msg(tcd->cinfo, EVT_INFO, "rate alloc: len=%d, max=%d\n", l, maxlen); */
1117 stable_thresh = thresh;
1121 goodthresh = stable_thresh;
1132 if(image_info && image_info->index_on) { /* Threshold for Marcela Index */
1133 image_info->tile[tcd->tcd_tileno].thresh[layno] = goodthresh;
1135 tcd_makelayer(tcd, layno, goodthresh, 1);
1138 cumdisto[layno] = (layno == 0) ? tcd_tile->distolayer[0] : (cumdisto[layno - 1] + tcd_tile->distolayer[layno]);
1144 int tcd_encode_tile(opj_tcd_t *tcd, int tileno, unsigned char *dest, int len, opj_image_info_t * image_info) {
1147 opj_tcd_tile_t *tile = NULL;
1148 opj_tcp_t *tcd_tcp = NULL;
1149 opj_cp_t *cp = NULL;
1151 opj_tcp_t *tcp = &tcd->cp->tcps[0];
1152 opj_tccp_t *tccp = &tcp->tccps[0];
1153 opj_image_t *image = tcd->image;
1155 opj_t1_t *t1 = NULL; /* T1 component */
1156 opj_t2_t *t2 = NULL; /* T2 component */
1158 tcd->tcd_tileno = tileno;
1159 tcd->tcd_tile = tcd->tcd_image->tiles;
1160 tcd->tcp = &tcd->cp->tcps[tileno];
1162 tile = tcd->tcd_tile;
1166 if(tcd->cur_tp_num == 0){
1167 tcd->encoding_time = opj_clock(); /* time needed to encode a tile */
1168 /* INDEX >> "Precinct_nb_X et Precinct_nb_Y" */
1169 if(image_info && image_info->index_on) {
1170 opj_tcd_tilecomp_t *tilec_idx = &tile->comps[0]; /* based on component 0 */
1171 for (i = 0; i < tilec_idx->numresolutions; i++) {
1172 opj_tcd_resolution_t *res_idx = &tilec_idx->resolutions[i];
1174 image_info->tile[tileno].pw[i] = res_idx->pw;
1175 image_info->tile[tileno].ph[i] = res_idx->ph;
1177 npck += res_idx->pw * res_idx->ph;
1179 image_info->tile[tileno].pdx[i] = tccp->prcw[i];
1180 image_info->tile[tileno].pdy[i] = tccp->prch[i];
1182 image_info->tile[tileno].packet = (opj_packet_info_t *) opj_malloc(image_info->comp * image_info->layer * npck * sizeof(opj_packet_info_t));
1186 /*---------------TILE-------------------*/
1188 for (compno = 0; compno < tile->numcomps; compno++) {
1191 int adjust = image->comps[compno].sgnd ? 0 : 1 << (image->comps[compno].prec - 1);
1192 int offset_x = int_ceildiv(image->x0, image->comps[compno].dx);
1193 int offset_y = int_ceildiv(image->y0, image->comps[compno].dy);
1195 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1196 int tw = tilec->x1 - tilec->x0;
1197 int w = int_ceildiv(image->x1 - image->x0, image->comps[compno].dx);
1199 /* extract tile data */
1201 if (tcd_tcp->tccps[compno].qmfbid == 1) {
1202 for (y = tilec->y0; y < tilec->y1; y++) {
1203 /* start of the src tile scanline */
1204 int *data = &image->comps[compno].data[(tilec->x0 - offset_x) + (y - offset_y) * w];
1205 /* start of the dst tile scanline */
1206 int *tile_data = &tilec->data[(y - tilec->y0) * tw];
1207 for (x = tilec->x0; x < tilec->x1; x++) {
1208 *tile_data++ = *data++ - adjust;
1211 } else if (tcd_tcp->tccps[compno].qmfbid == 0) {
1212 for (y = tilec->y0; y < tilec->y1; y++) {
1213 /* start of the src tile scanline */
1214 int *data = &image->comps[compno].data[(tilec->x0 - offset_x) + (y - offset_y) * w];
1215 /* start of the dst tile scanline */
1216 int *tile_data = &tilec->data[(y - tilec->y0) * tw];
1217 for (x = tilec->x0; x < tilec->x1; x++) {
1218 *tile_data++ = (*data++ - adjust) << 11;
1225 /*----------------MCT-------------------*/
1227 int samples = (tile->comps[0].x1 - tile->comps[0].x0) * (tile->comps[0].y1 - tile->comps[0].y0);
1228 if (tcd_tcp->tccps[0].qmfbid == 0) {
1229 mct_encode_real(tile->comps[0].data, tile->comps[1].data, tile->comps[2].data, samples);
1231 mct_encode(tile->comps[0].data, tile->comps[1].data, tile->comps[2].data, samples);
1235 /*----------------DWT---------------------*/
1237 for (compno = 0; compno < tile->numcomps; compno++) {
1238 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1239 if (tcd_tcp->tccps[compno].qmfbid == 1) {
1241 } else if (tcd_tcp->tccps[compno].qmfbid == 0) {
1242 dwt_encode_real(tilec);
1246 /*------------------TIER1-----------------*/
1247 t1 = t1_create(tcd->cinfo);
1248 t1_encode_cblks(t1, tile, tcd_tcp);
1251 /*-----------RATE-ALLOCATE------------------*/
1255 image_info->index_write = 0;
1257 if (cp->disto_alloc || cp->fixed_quality) { /* fixed_quality */
1258 /* Normal Rate/distortion allocation */
1259 tcd_rateallocate(tcd, dest, len, image_info);
1261 /* Fixed layer allocation */
1262 tcd_rateallocate_fixed(tcd);
1265 /*--------------TIER2------------------*/
1269 image_info->index_write = 1;
1272 t2 = t2_create(tcd->cinfo, image, cp);
1273 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);
1276 /*---------------CLEAN-------------------*/
1279 if(tcd->cur_tp_num == tcd->cur_totnum_tp - 1){
1280 tcd->encoding_time = opj_clock() - tcd->encoding_time;
1281 opj_event_msg(tcd->cinfo, EVT_INFO, "- tile encoded in %f s\n", tcd->encoding_time);
1283 /* cleaning memory */
1284 for (compno = 0; compno < tile->numcomps; compno++) {
1285 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1286 opj_aligned_free(tilec->data);
1293 bool tcd_decode_tile(opj_tcd_t *tcd, unsigned char *src, int len, int tileno) {
1297 double tile_time, t1_time, dwt_time;
1298 opj_tcd_tile_t *tile = NULL;
1300 opj_t1_t *t1 = NULL; /* T1 component */
1301 opj_t2_t *t2 = NULL; /* T2 component */
1303 tcd->tcd_tileno = tileno;
1304 tcd->tcd_tile = &(tcd->tcd_image->tiles[tileno]);
1305 tcd->tcp = &(tcd->cp->tcps[tileno]);
1306 tile = tcd->tcd_tile;
1308 tile_time = opj_clock(); /* time needed to decode a tile */
1309 opj_event_msg(tcd->cinfo, EVT_INFO, "tile %d of %d\n", tileno + 1, tcd->cp->tw * tcd->cp->th);
1311 /*--------------TIER2------------------*/
1313 t2 = t2_create(tcd->cinfo, tcd->image, tcd->cp);
1314 l = t2_decode_packets(t2, src, len, tileno, tile);
1319 opj_event_msg(tcd->cinfo, EVT_ERROR, "tcd_decode: incomplete bistream\n");
1322 /*------------------TIER1-----------------*/
1324 t1_time = opj_clock(); /* time needed to decode a tile */
1325 t1 = t1_create(tcd->cinfo);
1326 t1_decode_cblks(t1, tile, tcd->tcp);
1328 t1_time = opj_clock() - t1_time;
1329 opj_event_msg(tcd->cinfo, EVT_INFO, "- tiers-1 took %f s\n", t1_time);
1331 /*----------------DWT---------------------*/
1333 dwt_time = opj_clock(); /* time needed to decode a tile */
1334 for (compno = 0; compno < tile->numcomps; compno++) {
1335 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1336 if (tcd->cp->reduce != 0) {
1337 tcd->image->comps[compno].resno_decoded =
1338 tile->comps[compno].numresolutions - tcd->cp->reduce - 1;
1341 if (tcd->tcp->tccps[compno].qmfbid == 1) {
1342 dwt_decode(tilec, tilec->numresolutions - 1 - tcd->image->comps[compno].resno_decoded);
1344 dwt_decode_real(tilec, tilec->numresolutions - 1 - tcd->image->comps[compno].resno_decoded);
1347 if (tile->comps[compno].numresolutions > 0) {
1348 tcd->image->comps[compno].factor = tile->comps[compno].numresolutions - (tcd->image->comps[compno].resno_decoded + 1);
1351 dwt_time = opj_clock() - dwt_time;
1352 opj_event_msg(tcd->cinfo, EVT_INFO, "- dwt took %f s\n", dwt_time);
1354 /*----------------MCT-------------------*/
1356 if (tcd->tcp->mct) {
1357 if (tcd->tcp->tccps[0].qmfbid == 1) {
1358 mct_decode(tile->comps[0].data, tile->comps[1].data, tile->comps[2].data,
1359 (tile->comps[0].x1 - tile->comps[0].x0) * (tile->comps[0].y1 - tile->comps[0].y0));
1361 mct_decode_real(tile->comps[0].data, tile->comps[1].data, tile->comps[2].data,
1362 (tile->comps[0].x1 - tile->comps[0].x0) * (tile->comps[0].y1 - tile->comps[0].y0));
1366 /*---------------TILE-------------------*/
1368 for (compno = 0; compno < tile->numcomps; compno++) {
1369 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1370 opj_tcd_resolution_t *res = &tilec->resolutions[tcd->image->comps[compno].resno_decoded];
1371 int adjust = tcd->image->comps[compno].sgnd ? 0 : 1 << (tcd->image->comps[compno].prec - 1);
1372 int min = tcd->image->comps[compno].sgnd ?
1373 -(1 << (tcd->image->comps[compno].prec - 1)) : 0;
1374 int max = tcd->image->comps[compno].sgnd ?
1375 (1 << (tcd->image->comps[compno].prec - 1)) - 1 : (1 << tcd->image->comps[compno].prec) - 1;
1377 int tw = tilec->x1 - tilec->x0;
1378 int w = tcd->image->comps[compno].w;
1381 int offset_x = int_ceildivpow2(tcd->image->comps[compno].x0, tcd->image->comps[compno].factor);
1382 int offset_y = int_ceildivpow2(tcd->image->comps[compno].y0, tcd->image->comps[compno].factor);
1384 for (j = res->y0; j < res->y1; j++) {
1385 for (i = res->x0; i < res->x1; i++) {
1387 float tmp = (float)((tilec->data[i - res->x0 + (j - res->y0) * tw]) / 8192.0);
1389 if (tcd->tcp->tccps[compno].qmfbid == 1) {
1390 v = tilec->data[i - res->x0 + (j - res->y0) * tw];
1392 int tmp2 = ((int) (floor(fabs(tmp)))) + ((int) floor(fabs(tmp*2))%2);
1393 v = ((tmp < 0) ? -tmp2:tmp2);
1397 tcd->image->comps[compno].data[(i - offset_x) + (j - offset_y) * w] = int_clamp(v, min, max);
1402 tile_time = opj_clock() - tile_time; /* time needed to decode a tile */
1403 opj_event_msg(tcd->cinfo, EVT_INFO, "- tile decoded in %f s\n", tile_time);
1405 for (compno = 0; compno < tile->numcomps; compno++) {
1406 opj_aligned_free(tcd->tcd_image->tiles[tileno].comps[compno].data);
1407 tcd->tcd_image->tiles[tileno].comps[compno].data = NULL;
1417 void tcd_free_decode(opj_tcd_t *tcd) {
1418 opj_tcd_image_t *tcd_image = tcd->tcd_image;
1419 if (tcd_image->tiles != NULL) opj_free(tcd_image->tiles);
1422 void tcd_free_decode_tile(opj_tcd_t *tcd, int tileno) {
1423 int compno,resno,bandno,precno;
1425 opj_tcd_image_t *tcd_image = tcd->tcd_image;
1427 opj_tcd_tile_t *tile = &tcd_image->tiles[tileno];
1428 for (compno = 0; compno < tile->numcomps; compno++) {
1429 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1430 for (resno = 0; resno < tilec->numresolutions; resno++) {
1431 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
1432 for (bandno = 0; bandno < res->numbands; bandno++) {
1433 opj_tcd_band_t *band = &res->bands[bandno];
1434 for (precno = 0; precno < res->ph * res->pw; precno++) {
1435 opj_tcd_precinct_t *prec = &band->precincts[precno];
1436 if (prec->cblks != NULL) opj_free(prec->cblks);
1437 if (prec->imsbtree != NULL) tgt_destroy(prec->imsbtree);
1438 if (prec->incltree != NULL) tgt_destroy(prec->incltree);
1440 if (band->precincts != NULL) opj_free(band->precincts);
1443 if (tilec->resolutions != NULL) opj_free(tilec->resolutions);
1445 if (tile->comps != NULL) opj_free(tile->comps);