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];
604 tile = &(tcd->tcd_image->tiles[cp->tileno[tileno]]);
605 tile->numcomps = image->numcomps;
606 tile->comps = (opj_tcd_tilecomp_t *) opj_malloc(image->numcomps * sizeof(opj_tcd_tilecomp_t));
609 for (i = 0; i < image->numcomps; i++) {
610 for (j = 0; j < cp->tileno_size; j++) {
611 opj_tcd_tile_t *tile;
612 opj_tcd_tilecomp_t *tilec;
614 /* cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
616 tileno = cp->tileno[j];
618 tile = &(tcd->tcd_image->tiles[cp->tileno[tileno]]);
619 tilec = &tile->comps[i];
621 p = tileno % cp->tw; /* si numerotation matricielle .. */
622 q = tileno / cp->tw; /* .. coordonnees de la tile (q,p) q pour ligne et p pour colonne */
624 /* 4 borders of the tile rescale on the image if necessary */
625 tile->x0 = int_max(cp->tx0 + p * cp->tdx, image->x0);
626 tile->y0 = int_max(cp->ty0 + q * cp->tdy, image->y0);
627 tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, image->x1);
628 tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, image->y1);
630 tilec->x0 = int_ceildiv(tile->x0, image->comps[i].dx);
631 tilec->y0 = int_ceildiv(tile->y0, image->comps[i].dy);
632 tilec->x1 = int_ceildiv(tile->x1, image->comps[i].dx);
633 tilec->y1 = int_ceildiv(tile->y1, image->comps[i].dy);
635 x0 = j == 0 ? tilec->x0 : int_min(x0, (unsigned int) tilec->x0);
636 y0 = j == 0 ? tilec->y0 : int_min(y0, (unsigned int) tilec->x0);
637 x1 = j == 0 ? tilec->x1 : int_max(x1, (unsigned int) tilec->x1);
638 y1 = j == 0 ? tilec->y1 : int_max(y1, (unsigned int) tilec->y1);
644 image->comps[i].data = (int *) opj_malloc(w * h * sizeof(int));
645 image->comps[i].w = w;
646 image->comps[i].h = h;
647 image->comps[i].x0 = x0;
648 image->comps[i].y0 = y0;
652 void tcd_malloc_decode_tile(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp, int tileno, opj_codestream_info_t *cstr_info) {
653 int compno, resno, bandno, precno, cblkno;
654 unsigned int x0 = 0, y0 = 0, x1 = 0, y1 = 0;
656 opj_tcd_tile_t *tile;
660 tcp = &(cp->tcps[cp->tileno[tileno]]);
661 tile = &(tcd->tcd_image->tiles[cp->tileno[tileno]]);
663 tileno = cp->tileno[tileno];
665 for (compno = 0; compno < tile->numcomps; compno++) {
666 opj_tccp_t *tccp = &tcp->tccps[compno];
667 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
669 /* border of each tile component (global) */
670 tilec->x0 = int_ceildiv(tile->x0, image->comps[compno].dx);
671 tilec->y0 = int_ceildiv(tile->y0, image->comps[compno].dy);
672 tilec->x1 = int_ceildiv(tile->x1, image->comps[compno].dx);
673 tilec->y1 = int_ceildiv(tile->y1, image->comps[compno].dy);
675 tilec->data = (int*) opj_aligned_malloc((tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0) * sizeof(int));
676 tilec->numresolutions = tccp->numresolutions;
677 tilec->resolutions = (opj_tcd_resolution_t *) opj_malloc(tilec->numresolutions * sizeof(opj_tcd_resolution_t));
679 for (resno = 0; resno < tilec->numresolutions; resno++) {
681 int levelno = tilec->numresolutions - 1 - resno;
682 int tlprcxstart, tlprcystart, brprcxend, brprcyend;
683 int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend;
684 int cbgwidthexpn, cbgheightexpn;
685 int cblkwidthexpn, cblkheightexpn;
687 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
689 /* border for each resolution level (global) */
690 res->x0 = int_ceildivpow2(tilec->x0, levelno);
691 res->y0 = int_ceildivpow2(tilec->y0, levelno);
692 res->x1 = int_ceildivpow2(tilec->x1, levelno);
693 res->y1 = int_ceildivpow2(tilec->y1, levelno);
694 res->numbands = resno == 0 ? 1 : 3;
696 /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */
697 if (tccp->csty & J2K_CCP_CSTY_PRT) {
698 pdx = tccp->prcw[resno];
699 pdy = tccp->prch[resno];
705 /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
706 tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx;
707 tlprcystart = int_floordivpow2(res->y0, pdy) << pdy;
708 brprcxend = int_ceildivpow2(res->x1, pdx) << pdx;
709 brprcyend = int_ceildivpow2(res->y1, pdy) << pdy;
711 res->pw = (res->x0 == res->x1) ? 0 : ((brprcxend - tlprcxstart) >> pdx);
712 res->ph = (res->y0 == res->y1) ? 0 : ((brprcyend - tlprcystart) >> pdy);
715 tlcbgxstart = tlprcxstart;
716 tlcbgystart = tlprcystart;
717 brcbgxend = brprcxend;
718 brcbgyend = brprcyend;
722 tlcbgxstart = int_ceildivpow2(tlprcxstart, 1);
723 tlcbgystart = int_ceildivpow2(tlprcystart, 1);
724 brcbgxend = int_ceildivpow2(brprcxend, 1);
725 brcbgyend = int_ceildivpow2(brprcyend, 1);
726 cbgwidthexpn = pdx - 1;
727 cbgheightexpn = pdy - 1;
730 cblkwidthexpn = int_min(tccp->cblkw, cbgwidthexpn);
731 cblkheightexpn = int_min(tccp->cblkh, cbgheightexpn);
733 for (bandno = 0; bandno < res->numbands; bandno++) {
736 opj_stepsize_t *ss = NULL;
738 opj_tcd_band_t *band = &res->bands[bandno];
739 band->bandno = resno == 0 ? 0 : bandno + 1;
740 x0b = (band->bandno == 1) || (band->bandno == 3) ? 1 : 0;
741 y0b = (band->bandno == 2) || (band->bandno == 3) ? 1 : 0;
743 if (band->bandno == 0) {
744 /* band border (global) */
745 band->x0 = int_ceildivpow2(tilec->x0, levelno);
746 band->y0 = int_ceildivpow2(tilec->y0, levelno);
747 band->x1 = int_ceildivpow2(tilec->x1, levelno);
748 band->y1 = int_ceildivpow2(tilec->y1, levelno);
750 /* band border (global) */
751 band->x0 = int_ceildivpow2(tilec->x0 - (1 << levelno) * x0b, levelno + 1);
752 band->y0 = int_ceildivpow2(tilec->y0 - (1 << levelno) * y0b, levelno + 1);
753 band->x1 = int_ceildivpow2(tilec->x1 - (1 << levelno) * x0b, levelno + 1);
754 band->y1 = int_ceildivpow2(tilec->y1 - (1 << levelno) * y0b, levelno + 1);
757 ss = &tccp->stepsizes[resno == 0 ? 0 : 3 * (resno - 1) + bandno + 1];
758 gain = tccp->qmfbid == 0 ? dwt_getgain_real(band->bandno) : dwt_getgain(band->bandno);
759 numbps = image->comps[compno].prec + gain;
760 band->stepsize = (float)((1.0 + ss->mant / 2048.0) * pow(2.0, numbps - ss->expn));
761 band->numbps = ss->expn + tccp->numgbits - 1; /* WHY -1 ? */
763 band->precincts = (opj_tcd_precinct_t *) opj_malloc(res->pw * res->ph * sizeof(opj_tcd_precinct_t));
765 for (precno = 0; precno < res->pw * res->ph; precno++) {
766 int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
767 int cbgxstart = tlcbgxstart + (precno % res->pw) * (1 << cbgwidthexpn);
768 int cbgystart = tlcbgystart + (precno / res->pw) * (1 << cbgheightexpn);
769 int cbgxend = cbgxstart + (1 << cbgwidthexpn);
770 int cbgyend = cbgystart + (1 << cbgheightexpn);
772 opj_tcd_precinct_t *prc = &band->precincts[precno];
773 /* precinct size (global) */
774 prc->x0 = int_max(cbgxstart, band->x0);
775 prc->y0 = int_max(cbgystart, band->y0);
776 prc->x1 = int_min(cbgxend, band->x1);
777 prc->y1 = int_min(cbgyend, band->y1);
779 tlcblkxstart = int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn;
780 tlcblkystart = int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn;
781 brcblkxend = int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn;
782 brcblkyend = int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn;
783 prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;
784 prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn;
786 prc->cblks = (opj_tcd_cblk_t *) opj_malloc(prc->cw * prc->ch * sizeof(opj_tcd_cblk_t));
788 prc->incltree = tgt_create(prc->cw, prc->ch);
789 prc->imsbtree = tgt_create(prc->cw, prc->ch);
791 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
792 int cblkxstart = tlcblkxstart + (cblkno % prc->cw) * (1 << cblkwidthexpn);
793 int cblkystart = tlcblkystart + (cblkno / prc->cw) * (1 << cblkheightexpn);
794 int cblkxend = cblkxstart + (1 << cblkwidthexpn);
795 int cblkyend = cblkystart + (1 << cblkheightexpn);
797 /* code-block size (global) */
798 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
799 cblk->x0 = int_max(cblkxstart, prc->x0);
800 cblk->y0 = int_max(cblkystart, prc->y0);
801 cblk->x1 = int_min(cblkxend, prc->x1);
802 cblk->y1 = int_min(cblkyend, prc->y1);
808 /* tcd_dump(stdout, tcd, &tcd->tcd_image); */
811 void tcd_makelayer_fixed(opj_tcd_t *tcd, int layno, int final) {
812 int compno, resno, bandno, precno, cblkno;
813 int value; /*, matrice[tcd_tcp->numlayers][tcd_tile->comps[0].numresolutions][3]; */
814 int matrice[10][10][3];
817 opj_cp_t *cp = tcd->cp;
818 opj_tcd_tile_t *tcd_tile = tcd->tcd_tile;
819 opj_tcp_t *tcd_tcp = tcd->tcp;
821 /*matrice=(int*)opj_malloc(tcd_tcp->numlayers*tcd_tile->comps[0].numresolutions*3*sizeof(int)); */
823 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
824 opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
825 for (i = 0; i < tcd_tcp->numlayers; i++) {
826 for (j = 0; j < tilec->numresolutions; j++) {
827 for (k = 0; k < 3; k++) {
829 (int) (cp->matrice[i * tilec->numresolutions * 3 + j * 3 + k]
830 * (float) (tcd->image->comps[compno].prec / 16.0));
835 for (resno = 0; resno < tilec->numresolutions; resno++) {
836 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
837 for (bandno = 0; bandno < res->numbands; bandno++) {
838 opj_tcd_band_t *band = &res->bands[bandno];
839 for (precno = 0; precno < res->pw * res->ph; precno++) {
840 opj_tcd_precinct_t *prc = &band->precincts[precno];
841 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
842 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
843 opj_tcd_layer_t *layer = &cblk->layers[layno];
845 int imsb = tcd->image->comps[compno].prec - cblk->numbps; /* number of bit-plan equal to zero */
846 /* Correction of the matrix of coefficient to include the IMSB information */
848 value = matrice[layno][resno][bandno];
855 value = matrice[layno][resno][bandno] - matrice[layno - 1][resno][bandno];
856 if (imsb >= matrice[layno - 1][resno][bandno]) {
857 value -= (imsb - matrice[layno - 1][resno][bandno]);
865 cblk->numpassesinlayers = 0;
868 n = cblk->numpassesinlayers;
869 if (cblk->numpassesinlayers == 0) {
871 n = 3 * value - 2 + cblk->numpassesinlayers;
873 n = cblk->numpassesinlayers;
876 n = 3 * value + cblk->numpassesinlayers;
879 layer->numpasses = n - cblk->numpassesinlayers;
881 if (!layer->numpasses)
884 if (cblk->numpassesinlayers == 0) {
885 layer->len = cblk->passes[n - 1].rate;
886 layer->data = cblk->data;
888 layer->len = cblk->passes[n - 1].rate - cblk->passes[cblk->numpassesinlayers - 1].rate;
889 layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate;
892 cblk->numpassesinlayers = n;
900 void tcd_rateallocate_fixed(opj_tcd_t *tcd) {
902 for (layno = 0; layno < tcd->tcp->numlayers; layno++) {
903 tcd_makelayer_fixed(tcd, layno, 1);
907 void tcd_makelayer(opj_tcd_t *tcd, int layno, double thresh, int final) {
908 int compno, resno, bandno, precno, cblkno, passno;
910 opj_tcd_tile_t *tcd_tile = tcd->tcd_tile;
912 tcd_tile->distolayer[layno] = 0; /* fixed_quality */
914 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
915 opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
916 for (resno = 0; resno < tilec->numresolutions; resno++) {
917 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
918 for (bandno = 0; bandno < res->numbands; bandno++) {
919 opj_tcd_band_t *band = &res->bands[bandno];
920 for (precno = 0; precno < res->pw * res->ph; precno++) {
921 opj_tcd_precinct_t *prc = &band->precincts[precno];
922 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
923 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
924 opj_tcd_layer_t *layer = &cblk->layers[layno];
928 cblk->numpassesinlayers = 0;
930 n = cblk->numpassesinlayers;
931 for (passno = cblk->numpassesinlayers; passno < cblk->totalpasses; passno++) {
934 opj_tcd_pass_t *pass = &cblk->passes[passno];
937 dd = pass->distortiondec;
939 dr = pass->rate - cblk->passes[n - 1].rate;
940 dd = pass->distortiondec - cblk->passes[n - 1].distortiondec;
947 if (dd / dr >= thresh)
950 layer->numpasses = n - cblk->numpassesinlayers;
952 if (!layer->numpasses) {
956 if (cblk->numpassesinlayers == 0) {
957 layer->len = cblk->passes[n - 1].rate;
958 layer->data = cblk->data;
959 layer->disto = cblk->passes[n - 1].distortiondec;
961 layer->len = cblk->passes[n - 1].rate - cblk->passes[cblk->numpassesinlayers - 1].rate;
962 layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate;
963 layer->disto = cblk->passes[n - 1].distortiondec - cblk->passes[cblk->numpassesinlayers - 1].distortiondec;
966 tcd_tile->distolayer[layno] += layer->disto; /* fixed_quality */
969 cblk->numpassesinlayers = n;
977 bool tcd_rateallocate(opj_tcd_t *tcd, unsigned char *dest, int len, opj_codestream_info_t *cstr_info) {
978 int compno, resno, bandno, precno, cblkno, passno, layno;
980 double cumdisto[100]; /* fixed_quality */
981 const double K = 1; /* 1.1; fixed_quality */
984 opj_cp_t *cp = tcd->cp;
985 opj_tcd_tile_t *tcd_tile = tcd->tcd_tile;
986 opj_tcp_t *tcd_tcp = tcd->tcp;
991 tcd_tile->numpix = 0; /* fixed_quality */
993 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
994 opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
997 for (resno = 0; resno < tilec->numresolutions; resno++) {
998 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
1000 for (bandno = 0; bandno < res->numbands; bandno++) {
1001 opj_tcd_band_t *band = &res->bands[bandno];
1003 for (precno = 0; precno < res->pw * res->ph; precno++) {
1004 opj_tcd_precinct_t *prc = &band->precincts[precno];
1006 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
1007 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
1009 for (passno = 0; passno < cblk->totalpasses; passno++) {
1010 opj_tcd_pass_t *pass = &cblk->passes[passno];
1015 dd = pass->distortiondec;
1017 dr = pass->rate - cblk->passes[passno - 1].rate;
1018 dd = pass->distortiondec - cblk->passes[passno - 1].distortiondec;
1024 if (rdslope < min) {
1027 if (rdslope > max) {
1033 tcd_tile->numpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0));
1034 tilec->numpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0));
1040 maxSE += (((double)(1 << tcd->image->comps[compno].prec) - 1.0)
1041 * ((double)(1 << tcd->image->comps[compno].prec) -1.0))
1042 * ((double)(tilec->numpix));
1047 opj_tile_info_t *tile_info = &cstr_info->tile[tcd->tcd_tileno];
1048 tile_info->numpix = tcd_tile->numpix;
1049 tile_info->distotile = tcd_tile->distotile;
1050 tile_info->thresh = (double *) opj_malloc(tcd_tcp->numlayers * sizeof(double));
1053 for (layno = 0; layno < tcd_tcp->numlayers; layno++) {
1057 int maxlen = tcd_tcp->rates[layno] ? int_min(((int) ceil(tcd_tcp->rates[layno])), len) : len;
1058 double goodthresh = 0;
1059 double stable_thresh = 0;
1061 double distotarget; /* fixed_quality */
1064 distotarget = tcd_tile->distotile - ((K * maxSE) / pow((float)10, tcd_tcp->distoratio[layno] / 10));
1066 /* Don't try to find an optimal threshold but rather take everything not included yet, if
1067 -r xx,yy,zz,0 (disto_alloc == 1 and rates == 0)
1068 -q xx,yy,zz,0 (fixed_quality == 1 and distoratio == 0)
1069 ==> possible to have some lossy layers and the last layer for sure lossless */
1070 if ( ((cp->disto_alloc==1) && (tcd_tcp->rates[layno]>0)) || ((cp->fixed_quality==1) && (tcd_tcp->distoratio[layno]>0))) {
1071 opj_t2_t *t2 = t2_create(tcd->cinfo, tcd->image, cp);
1073 for (i = 0; i < 32; i++) {
1074 double thresh = (lo + hi) / 2;
1076 double distoachieved = 0; /* fixed_quality */
1078 tcd_makelayer(tcd, layno, thresh, 0);
1080 if (cp->fixed_quality) { /* fixed_quality */
1082 l = t2_encode_packets(t2,tcd->tcd_tileno, tcd_tile, layno + 1, dest, maxlen, cstr_info,tcd->cur_tp_num,tcd->tp_pos,tcd->cur_pino,THRESH_CALC);
1087 distoachieved = layno == 0 ?
1088 tcd_tile->distolayer[0] : cumdisto[layno - 1] + tcd_tile->distolayer[layno];
1089 if (distoachieved < distotarget) {
1091 stable_thresh = thresh;
1098 distoachieved = (layno == 0) ?
1099 tcd_tile->distolayer[0] : (cumdisto[layno - 1] + tcd_tile->distolayer[layno]);
1100 if (distoachieved < distotarget) {
1102 stable_thresh = thresh;
1108 l = t2_encode_packets(t2, tcd->tcd_tileno, tcd_tile, layno + 1, dest, maxlen, cstr_info,tcd->cur_tp_num,tcd->tp_pos,tcd->cur_pino,THRESH_CALC);
1109 /* TODO: what to do with l ??? seek / tell ??? */
1110 /* opj_event_msg(tcd->cinfo, EVT_INFO, "rate alloc: len=%d, max=%d\n", l, maxlen); */
1116 stable_thresh = thresh;
1120 goodthresh = stable_thresh;
1131 if(cstr_info) { /* Threshold for Marcela Index */
1132 cstr_info->tile[tcd->tcd_tileno].thresh[layno] = goodthresh;
1134 tcd_makelayer(tcd, layno, goodthresh, 1);
1137 cumdisto[layno] = (layno == 0) ? tcd_tile->distolayer[0] : (cumdisto[layno - 1] + tcd_tile->distolayer[layno]);
1143 int tcd_encode_tile(opj_tcd_t *tcd, int tileno, unsigned char *dest, int len, opj_codestream_info_t *cstr_info) {
1145 int l, i, numpacks = 0;
1146 opj_tcd_tile_t *tile = NULL;
1147 opj_tcp_t *tcd_tcp = NULL;
1148 opj_cp_t *cp = NULL;
1150 opj_tcp_t *tcp = &tcd->cp->tcps[0];
1151 opj_tccp_t *tccp = &tcp->tccps[0];
1152 opj_image_t *image = tcd->image;
1154 opj_t1_t *t1 = NULL; /* T1 component */
1155 opj_t2_t *t2 = NULL; /* T2 component */
1157 tcd->tcd_tileno = tileno;
1158 tcd->tcd_tile = tcd->tcd_image->tiles;
1159 tcd->tcp = &tcd->cp->tcps[tileno];
1161 tile = tcd->tcd_tile;
1165 if(tcd->cur_tp_num == 0){
1166 tcd->encoding_time = opj_clock(); /* time needed to encode a tile */
1167 /* INDEX >> "Precinct_nb_X et Precinct_nb_Y" */
1169 opj_tcd_tilecomp_t *tilec_idx = &tile->comps[0]; /* based on component 0 */
1170 for (i = 0; i < tilec_idx->numresolutions; i++) {
1171 opj_tcd_resolution_t *res_idx = &tilec_idx->resolutions[i];
1173 cstr_info->tile[tileno].pw[i] = res_idx->pw;
1174 cstr_info->tile[tileno].ph[i] = res_idx->ph;
1176 numpacks += res_idx->pw * res_idx->ph;
1178 cstr_info->tile[tileno].pdx[i] = tccp->prcw[i];
1179 cstr_info->tile[tileno].pdy[i] = tccp->prch[i];
1181 cstr_info->tile[tileno].packet = (opj_packet_info_t *) opj_malloc(cstr_info->numcomps * cstr_info->numlayers * numpacks * sizeof(opj_packet_info_t));
1185 /*---------------TILE-------------------*/
1187 for (compno = 0; compno < tile->numcomps; compno++) {
1190 int adjust = image->comps[compno].sgnd ? 0 : 1 << (image->comps[compno].prec - 1);
1191 int offset_x = int_ceildiv(image->x0, image->comps[compno].dx);
1192 int offset_y = int_ceildiv(image->y0, image->comps[compno].dy);
1194 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1195 int tw = tilec->x1 - tilec->x0;
1196 int w = int_ceildiv(image->x1 - image->x0, image->comps[compno].dx);
1198 /* extract tile data */
1200 if (tcd_tcp->tccps[compno].qmfbid == 1) {
1201 for (y = tilec->y0; y < tilec->y1; y++) {
1202 /* start of the src tile scanline */
1203 int *data = &image->comps[compno].data[(tilec->x0 - offset_x) + (y - offset_y) * w];
1204 /* start of the dst tile scanline */
1205 int *tile_data = &tilec->data[(y - tilec->y0) * tw];
1206 for (x = tilec->x0; x < tilec->x1; x++) {
1207 *tile_data++ = *data++ - adjust;
1210 } else if (tcd_tcp->tccps[compno].qmfbid == 0) {
1211 for (y = tilec->y0; y < tilec->y1; y++) {
1212 /* start of the src tile scanline */
1213 int *data = &image->comps[compno].data[(tilec->x0 - offset_x) + (y - offset_y) * w];
1214 /* start of the dst tile scanline */
1215 int *tile_data = &tilec->data[(y - tilec->y0) * tw];
1216 for (x = tilec->x0; x < tilec->x1; x++) {
1217 *tile_data++ = (*data++ - adjust) << 11;
1224 /*----------------MCT-------------------*/
1226 int samples = (tile->comps[0].x1 - tile->comps[0].x0) * (tile->comps[0].y1 - tile->comps[0].y0);
1227 if (tcd_tcp->tccps[0].qmfbid == 0) {
1228 mct_encode_real(tile->comps[0].data, tile->comps[1].data, tile->comps[2].data, samples);
1230 mct_encode(tile->comps[0].data, tile->comps[1].data, tile->comps[2].data, samples);
1234 /*----------------DWT---------------------*/
1236 for (compno = 0; compno < tile->numcomps; compno++) {
1237 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1238 if (tcd_tcp->tccps[compno].qmfbid == 1) {
1240 } else if (tcd_tcp->tccps[compno].qmfbid == 0) {
1241 dwt_encode_real(tilec);
1245 /*------------------TIER1-----------------*/
1246 t1 = t1_create(tcd->cinfo);
1247 t1_encode_cblks(t1, tile, tcd_tcp);
1250 /*-----------RATE-ALLOCATE------------------*/
1254 cstr_info->index_write = 0;
1256 if (cp->disto_alloc || cp->fixed_quality) { /* fixed_quality */
1257 /* Normal Rate/distortion allocation */
1258 tcd_rateallocate(tcd, dest, len, cstr_info);
1260 /* Fixed layer allocation */
1261 tcd_rateallocate_fixed(tcd);
1264 /*--------------TIER2------------------*/
1268 cstr_info->index_write = 1;
1271 t2 = t2_create(tcd->cinfo, image, cp);
1272 l = t2_encode_packets(t2,tileno, tile, tcd_tcp->numlayers, dest, len, cstr_info,tcd->tp_num,tcd->tp_pos,tcd->cur_pino,FINAL_PASS);
1275 /*---------------CLEAN-------------------*/
1278 if(tcd->cur_tp_num == tcd->cur_totnum_tp - 1){
1279 tcd->encoding_time = opj_clock() - tcd->encoding_time;
1280 opj_event_msg(tcd->cinfo, EVT_INFO, "- tile encoded in %f s\n", tcd->encoding_time);
1282 /* cleaning memory */
1283 for (compno = 0; compno < tile->numcomps; compno++) {
1284 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1285 opj_aligned_free(tilec->data);
1292 bool tcd_decode_tile(opj_tcd_t *tcd, unsigned char *src, int len, int tileno, opj_codestream_info_t *cstr_info) {
1296 double tile_time, t1_time, dwt_time;
1297 opj_tcd_tile_t *tile = NULL;
1299 opj_t1_t *t1 = NULL; /* T1 component */
1300 opj_t2_t *t2 = NULL; /* T2 component */
1302 tcd->tcd_tileno = tileno;
1303 tcd->tcd_tile = &(tcd->tcd_image->tiles[tileno]);
1304 tcd->tcp = &(tcd->cp->tcps[tileno]);
1305 tile = tcd->tcd_tile;
1307 tile_time = opj_clock(); /* time needed to decode a tile */
1308 opj_event_msg(tcd->cinfo, EVT_INFO, "tile %d of %d\n", tileno + 1, tcd->cp->tw * tcd->cp->th);
1312 int i, numpacks = 0;
1313 opj_tcp_t *tcp = &tcd->cp->tcps[0];
1314 opj_tccp_t *tccp = &tcp->tccps[0];
1315 opj_tcd_tilecomp_t *tilec_idx = &tile->comps[0]; /* based on component 0 */
1316 for (i = 0; i < tilec_idx->numresolutions; i++) {
1317 opj_tcd_resolution_t *res_idx = &tilec_idx->resolutions[i];
1318 cstr_info->tile[tileno].pw[i] = res_idx->pw;
1319 cstr_info->tile[tileno].ph[i] = res_idx->ph;
1320 numpacks += res_idx->pw * res_idx->ph;
1321 if (tccp->csty & J2K_CP_CSTY_PRT) {
1322 cstr_info->tile[tileno].pdx[i] = tccp->prcw[i];
1323 cstr_info->tile[tileno].pdy[i] = tccp->prch[i];
1326 cstr_info->tile[tileno].pdx[i] = 15;
1327 cstr_info->tile[tileno].pdx[i] = 15;
1330 cstr_info->tile[tileno].packet = (opj_packet_info_t *) opj_malloc(cstr_info->numcomps * cstr_info->numlayers * numpacks * sizeof(opj_packet_info_t));
1331 cstr_info->packno = 0;
1335 /*--------------TIER2------------------*/
1337 t2 = t2_create(tcd->cinfo, tcd->image, tcd->cp);
1338 l = t2_decode_packets(t2, src, len, tileno, tile, cstr_info);
1343 opj_event_msg(tcd->cinfo, EVT_ERROR, "tcd_decode: incomplete bistream\n");
1346 /*------------------TIER1-----------------*/
1348 t1_time = opj_clock(); /* time needed to decode a tile */
1349 t1 = t1_create(tcd->cinfo);
1350 t1_decode_cblks(t1, tile, tcd->tcp);
1352 t1_time = opj_clock() - t1_time;
1353 opj_event_msg(tcd->cinfo, EVT_INFO, "- tiers-1 took %f s\n", t1_time);
1355 /*----------------DWT---------------------*/
1357 dwt_time = opj_clock(); /* time needed to decode a tile */
1358 for (compno = 0; compno < tile->numcomps; compno++) {
1359 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1360 if (tcd->cp->reduce != 0) {
1361 tcd->image->comps[compno].resno_decoded =
1362 tile->comps[compno].numresolutions - tcd->cp->reduce - 1;
1365 if (tcd->tcp->tccps[compno].qmfbid == 1) {
1366 dwt_decode(tilec, tilec->numresolutions - 1 - tcd->image->comps[compno].resno_decoded);
1368 dwt_decode_real(tilec, tilec->numresolutions - 1 - tcd->image->comps[compno].resno_decoded);
1371 if (tile->comps[compno].numresolutions > 0) {
1372 tcd->image->comps[compno].factor = tile->comps[compno].numresolutions - (tcd->image->comps[compno].resno_decoded + 1);
1375 dwt_time = opj_clock() - dwt_time;
1376 opj_event_msg(tcd->cinfo, EVT_INFO, "- dwt took %f s\n", dwt_time);
1378 /*----------------MCT-------------------*/
1380 if (tcd->tcp->mct) {
1381 if (tcd->tcp->tccps[0].qmfbid == 1) {
1382 mct_decode(tile->comps[0].data, tile->comps[1].data, tile->comps[2].data,
1383 (tile->comps[0].x1 - tile->comps[0].x0) * (tile->comps[0].y1 - tile->comps[0].y0));
1385 mct_decode_real(tile->comps[0].data, tile->comps[1].data, tile->comps[2].data,
1386 (tile->comps[0].x1 - tile->comps[0].x0) * (tile->comps[0].y1 - tile->comps[0].y0));
1390 /*---------------TILE-------------------*/
1392 for (compno = 0; compno < tile->numcomps; compno++) {
1393 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1394 opj_tcd_resolution_t *res = &tilec->resolutions[tcd->image->comps[compno].resno_decoded];
1395 int adjust = tcd->image->comps[compno].sgnd ? 0 : 1 << (tcd->image->comps[compno].prec - 1);
1396 int min = tcd->image->comps[compno].sgnd ?
1397 -(1 << (tcd->image->comps[compno].prec - 1)) : 0;
1398 int max = tcd->image->comps[compno].sgnd ?
1399 (1 << (tcd->image->comps[compno].prec - 1)) - 1 : (1 << tcd->image->comps[compno].prec) - 1;
1401 int tw = tilec->x1 - tilec->x0;
1402 int w = tcd->image->comps[compno].w;
1405 int offset_x = int_ceildivpow2(tcd->image->comps[compno].x0, tcd->image->comps[compno].factor);
1406 int offset_y = int_ceildivpow2(tcd->image->comps[compno].y0, tcd->image->comps[compno].factor);
1408 for (j = res->y0; j < res->y1; j++) {
1409 for (i = res->x0; i < res->x1; i++) {
1411 float tmp = (float)((tilec->data[i - res->x0 + (j - res->y0) * tw]) / 8192.0);
1413 if (tcd->tcp->tccps[compno].qmfbid == 1) {
1414 v = tilec->data[i - res->x0 + (j - res->y0) * tw];
1416 int tmp2 = ((int) (floor(fabs(tmp)))) + ((int) floor(fabs(tmp*2))%2);
1417 v = ((tmp < 0) ? -tmp2:tmp2);
1421 tcd->image->comps[compno].data[(i - offset_x) + (j - offset_y) * w] = int_clamp(v, min, max);
1426 tile_time = opj_clock() - tile_time; /* time needed to decode a tile */
1427 opj_event_msg(tcd->cinfo, EVT_INFO, "- tile decoded in %f s\n", tile_time);
1429 for (compno = 0; compno < tile->numcomps; compno++) {
1430 opj_aligned_free(tcd->tcd_image->tiles[tileno].comps[compno].data);
1431 tcd->tcd_image->tiles[tileno].comps[compno].data = NULL;
1441 void tcd_free_decode(opj_tcd_t *tcd) {
1442 opj_tcd_image_t *tcd_image = tcd->tcd_image;
1443 if (tcd_image->tiles != NULL) opj_free(tcd_image->tiles);
1446 void tcd_free_decode_tile(opj_tcd_t *tcd, int tileno) {
1447 int compno,resno,bandno,precno;
1449 opj_tcd_image_t *tcd_image = tcd->tcd_image;
1451 opj_tcd_tile_t *tile = &tcd_image->tiles[tileno];
1452 for (compno = 0; compno < tile->numcomps; compno++) {
1453 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1454 for (resno = 0; resno < tilec->numresolutions; resno++) {
1455 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
1456 for (bandno = 0; bandno < res->numbands; bandno++) {
1457 opj_tcd_band_t *band = &res->bands[bandno];
1458 for (precno = 0; precno < res->ph * res->pw; precno++) {
1459 opj_tcd_precinct_t *prec = &band->precincts[precno];
1460 if (prec->cblks != NULL) opj_free(prec->cblks);
1461 if (prec->imsbtree != NULL) tgt_destroy(prec->imsbtree);
1462 if (prec->incltree != NULL) tgt_destroy(prec->incltree);
1464 if (band->precincts != NULL) opj_free(band->precincts);
1467 if (tilec->resolutions != NULL) opj_free(tilec->resolutions);
1469 if (tile->comps != NULL) opj_free(tile->comps);