2 * The copyright in this software is being made available under the 2-clauses
3 * BSD License, included below. This software may be subject to other third
4 * party and contributor rights, including patent rights, and no such rights
5 * are granted under this license.
7 * Copyright (c) 2001-2003, David Janssens
8 * Copyright (c) 2002-2003, Yannick Verschueren
9 * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe
10 * Copyright (c) 2005, Herve Drolon, FreeImage Team
11 * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
12 * Copyright (c) 2006, M�nica D�ez, LPI-UVA, Spain
13 * All rights reserved.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 * POSSIBILITY OF SUCH DAMAGE.
37 #include "opj_includes.h"
39 void tcd_dump(FILE *fd, opj_tcd_t *tcd, opj_tcd_volume_t * vol) {
40 int tileno, compno, resno, bandno, precno, cblkno;
42 fprintf(fd, "volume {\n");
43 fprintf(fd, " tw=%d, th=%d, tl=%d, x0=%d x1=%d y0=%d y1=%d z0=%d z1=%d\n",
44 vol->tw, vol->th, vol->tl, tcd->volume->x0, tcd->volume->x1, tcd->volume->y0, tcd->volume->y1, tcd->volume->z0, tcd->volume->z1);
46 for (tileno = 0; tileno < vol->th * vol->tw * vol->tl; tileno++) {
47 opj_tcd_tile_t *tile = &tcd->tcd_volume->tiles[tileno];
48 fprintf(fd, " tile {\n");
49 fprintf(fd, " x0=%d, y0=%d, z0=%d, x1=%d, y1=%d, z1=%d, numcomps=%d\n",
50 tile->x0, tile->y0, tile->z0, tile->x1, tile->y1, tile->z1, tile->numcomps);
51 for (compno = 0; compno < tile->numcomps; compno++) {
52 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
53 fprintf(fd, " tilecomp %d {\n",compno);
54 fprintf(fd, " x0=%d, y0=%d, z0=%d, x1=%d, y1=%d, z1=%d, numresx=%d, numresy=%d, numresz=%d\n",
55 tilec->x0, tilec->y0, tilec->z0, tilec->x1, tilec->y1, tilec->z1, tilec->numresolution[0], tilec->numresolution[1], tilec->numresolution[2]);
56 for (resno = 0; resno < tilec->numresolution[0]; resno++) {
57 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
58 fprintf(fd, " res %d{\n",resno);
59 fprintf(fd," x0=%d, y0=%d, z0=%d, x1=%d, y1=%d, z1=%d, pw=%d, ph=%d, pl=%d, numbands=%d\n",
60 res->x0, res->y0, res->z0, res->x1, res->y1, res->z1, res->prctno[0], res->prctno[1], res->prctno[2], res->numbands);
61 for (bandno = 0; bandno < res->numbands; bandno++) {
62 opj_tcd_band_t *band = &res->bands[bandno];
63 fprintf(fd, " band %d{\n", bandno);
64 fprintf(fd, " x0=%d, y0=%d, z0=%d, x1=%d, y1=%d, z1=%d, stepsize=%f, numbps=%d\n",
65 band->x0, band->y0, band->z0, band->x1, band->y1, band->z1, band->stepsize, band->numbps);
66 for (precno = 0; precno < (res->prctno[0] * res->prctno[1] * res->prctno[2]); precno++) {
67 opj_tcd_precinct_t *prec = &band->precincts[precno];
68 fprintf(fd, " prec %d{\n",precno);
69 fprintf(fd, " x0=%d, y0=%d, z0=%d, x1=%d, y1=%d, z1=%d, cw=%d, ch=%d, cl=%d,\n",
70 prec->x0, prec->y0, prec->z0, prec->x1, prec->y1, prec->z1, prec->cblkno[0], prec->cblkno[1], prec->cblkno[2]);
71 for (cblkno = 0; cblkno < (prec->cblkno[0] * prec->cblkno[1] * prec->cblkno[2]); cblkno++) {
72 opj_tcd_cblk_t *cblk = &prec->cblks[cblkno];
73 fprintf(fd, " cblk %d{\n",cblkno);
74 fprintf(fd, " x0=%d, y0=%d, z0=%d, x1=%d, y1=%d, z1=%d\n", cblk->x0, cblk->y0, cblk->z0, cblk->x1, cblk->y1, cblk->z1);
90 static void tilec_dump(FILE *fd, opj_tcd_tilecomp_t *tilec) {
96 fprintf(fd, " tilecomp{\n");
97 fprintf(fd, " x0=%d, y0=%d, z0=%d, x1=%d, y1=%d, z1=%d, numresx=%d, numresy=%d, numresz=%d\n",
98 tilec->x0, tilec->y0, tilec->z0, tilec->x1, tilec->y1, tilec->z1, tilec->numresolution[0], tilec->numresolution[1], tilec->numresolution[2]);
99 fprintf(fd, " data {\n");
100 datalen = (tilec->z1 - tilec->z0) * (tilec->y1 - tilec->y0) * (tilec->x1 - tilec->x0);
102 for (k = 0; k < datalen; k++) {
103 if (!(k % tilec->x1)){
106 if (!(k % (tilec->y1 * tilec->x1))){
107 fprintf(fd, "Slice %d\n",i++);
109 fprintf(fd," %d",a[k]);
115 fprintf(fd, "Slice %d\n");
116 if (tilec->prediction->prederr) {
117 fprintf(fd, " prederror {\n");
118 a = tilec->prediction->prederr;
119 for (k = 0; k < datalen; k++) {
120 fprintf(fd," %d",*(a++));
121 if (!(k % (tilec->y1 - tilec->y0) * (tilec->x1 - tilec->x0))){
122 fprintf(fd, "\n");fprintf(fd, "Slice %d\n",i++);
124 if (!(k % (tilec->x1 - tilec->x0))){
129 fprintf(fd, " }\n");*/
133 /* ----------------------------------------------------------------------- */
136 Create a new TCD handle
138 opj_tcd_t* tcd_create(opj_common_ptr cinfo) {
139 /* create the tcd structure */
140 opj_tcd_t *tcd = (opj_tcd_t*)opj_malloc(sizeof(opj_tcd_t));
141 if(!tcd) return NULL;
143 tcd->tcd_volume = (opj_tcd_volume_t*)opj_malloc(sizeof(opj_tcd_volume_t));
144 if(!tcd->tcd_volume) {
153 Destroy a previously created TCD handle
155 void tcd_destroy(opj_tcd_t *tcd) {
157 opj_free(tcd->tcd_volume);
162 /* ----------------------------------------------------------------------- */
163 void tcd_malloc_encode(opj_tcd_t *tcd, opj_volume_t * volume, opj_cp_t * cp, int curtileno) {
164 int compno, resno, bandno, precno, cblkno, i, j;/*, k;*/
166 opj_tcd_tile_t *tile = NULL; /* pointer to tcd->tile */
167 opj_tcd_tilecomp_t *tilec = NULL; /* pointer to tcd->tilec */
168 opj_tcd_resolution_t *res = NULL; /* pointer to tcd->res */
169 opj_tcd_band_t *band = NULL; /* pointer to tcd->band */
170 opj_tcd_precinct_t *prc = NULL; /* pointer to tcd->prc */
171 opj_tcd_cblk_t *cblk = NULL; /* pointer to tcd->cblk */
172 opj_tcp_t *tcp = &cp->tcps[curtileno];
175 tcd->volume = volume;
177 tcd->tcd_volume->tw = cp->tw;
178 tcd->tcd_volume->th = cp->th;
179 tcd->tcd_volume->tl = cp->tl;
180 tcd->tcd_volume->tiles = (opj_tcd_tile_t *) opj_malloc(sizeof(opj_tcd_tile_t));
181 tcd->tile = tcd->tcd_volume->tiles;
185 /* p61 ISO/IEC IS15444-1 : 2002 */
186 /* curtileno --> raster scanned index of tiles */
187 /* p,q,r --> matricial index of tiles */
188 p = curtileno % cp->tw;
189 q = curtileno / cp->tw;
190 r = curtileno / (cp->tw * cp->th); /* extension to 3-D */
192 /* 4 borders of the tile rescale on the volume if necessary (B.3)*/
193 tile->x0 = int_max(cp->tx0 + p * cp->tdx, volume->x0);
194 tile->y0 = int_max(cp->ty0 + q * cp->tdy, volume->y0);
195 tile->z0 = int_max(cp->tz0 + r * cp->tdz, volume->z0);
196 tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, volume->x1);
197 tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, volume->y1);
198 tile->z1 = int_min(cp->tz0 + (r + 1) * cp->tdz, volume->z1);
199 tile->numcomps = volume->numcomps;
201 /* Modification of the RATE >> */
202 for (j = 0; j < tcp->numlayers; j++) {
203 if (tcp->rates[j] <= 1) {
206 float num = (float) (tile->numcomps * (tile->x1 - tile->x0) * (tile->y1 - tile->y0) * (tile->z1 - tile->z0) * volume->comps[0].prec);
207 float den = (float) (8 * volume->comps[0].dx * volume->comps[0].dy * volume->comps[0].dz);
208 den = tcp->rates[j] * den;
209 tcp->rates[j] = (num + den - 1) / den;
211 /*tcp->rates[j] = tcp->rates[j] ? int_ceildiv(
212 tile->numcomps * (tile->x1 - tile->x0) * (tile->y1 - tile->y0) * (tile->z1 - tile->z0) * volume->comps[0].prec,
213 (tcp->rates[j] * 8 * volume->comps[0].dx * volume->comps[0].dy * volume->comps[0].dz)) : 0;*/
215 if (j && tcp->rates[j] < tcp->rates[j - 1] + 10) {
216 tcp->rates[j] = tcp->rates[j - 1] + 20;
217 } else if (!j && tcp->rates[j] < 30){
222 /* << Modification of the RATE */
224 tile->comps = (opj_tcd_tilecomp_t *) opj_malloc(volume->numcomps * sizeof(opj_tcd_tilecomp_t));
225 for (compno = 0; compno < tile->numcomps; compno++) {
226 opj_tccp_t *tccp = &tcp->tccps[compno];
228 int prevnumbands = 0;
230 /* opj_tcd_tilecomp_t *tilec=&tile->comps[compno]; */
231 tcd->tilec = &tile->comps[compno];
234 /* border of each tile component (global) (B.3) */
235 tilec->x0 = int_ceildiv(tile->x0, volume->comps[compno].dx);
236 tilec->y0 = int_ceildiv(tile->y0, volume->comps[compno].dy);
237 tilec->z0 = int_ceildiv(tile->z0, volume->comps[compno].dz);
238 tilec->x1 = int_ceildiv(tile->x1, volume->comps[compno].dx);
239 tilec->y1 = int_ceildiv(tile->y1, volume->comps[compno].dy);
240 tilec->z1 = int_ceildiv(tile->z1, volume->comps[compno].dz);
242 tilec->data = (int *) opj_malloc((tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0) * (tilec->z1 - tilec->z0) * sizeof(int));
245 for (i = 0;i < 3; i++){
246 tilec->numresolution[i] = tccp->numresolution[i];
247 /*Greater of 3 resolutions contains all information*/
248 res_max = (tilec->numresolution[i] > res_max) ? tilec->numresolution[i] : res_max;
252 tilec->resolutions = (opj_tcd_resolution_t *) opj_malloc(res_max * sizeof(opj_tcd_resolution_t));
253 for (resno = 0; resno < res_max; resno++) {
256 int tlprcxstart, tlprcystart, tlprczstart;
257 int brprcxend, brprcyend, brprczend;
258 int tlcbgxstart, tlcbgystart, tlcbgzstart;
259 int brcbgxend, brcbgyend, brcbgzend;
260 int cbgwidthexpn, cbgheightexpn, cbglengthexpn;
261 int cblkwidthexpn, cblkheightexpn, cblklengthexpn;
263 int diff = tccp->numresolution[0] - tccp->numresolution[2];
264 int levelnox = tilec->numresolution[0] - 1 - resno;
265 int levelnoy = tilec->numresolution[1] - 1 - resno;
266 int levelnoz = tilec->numresolution[2] - 1 - ((resno <= diff) ? 0 : (resno - diff));
267 if (levelnoz < 0) levelnoz = 0;
269 /* opj_tcd_resolution_t *res=&tilec->resolutions[resno]; */
270 tcd->res = &tilec->resolutions[resno];
273 /* border for each resolution level (global) (B.14)*/
274 res->x0 = int_ceildivpow2(tilec->x0, levelnox);
275 res->y0 = int_ceildivpow2(tilec->y0, levelnoy);
276 res->z0 = int_ceildivpow2(tilec->z0, levelnoz);
277 res->x1 = int_ceildivpow2(tilec->x1, levelnox);
278 res->y1 = int_ceildivpow2(tilec->y1, levelnoy);
279 res->z1 = int_ceildivpow2(tilec->z1, levelnoz);
280 /*if (res->z1 < 0)fprintf(stdout,"Res: %d %d/%d --> %d\n",resno,tilec->z1, levelnoz, int_ceildivpow2(tilec->z1, levelnoz));*/
282 res->numbands = (resno == 0) ? 1 : (resno <= diff) ? 3 : 7; /* --> 3D */
284 /* p. 30, table A-13, ISO/IEC IS154444-1 : 2002 */
285 if (tccp->csty & J3D_CCP_CSTY_PRT) {
286 pdx = tccp->prctsiz[0][resno];
287 pdy = tccp->prctsiz[1][resno];
288 pdz = tccp->prctsiz[2][resno];
295 /* p. 66, B.16, ISO/IEC IS15444-1 : 2002 */
296 tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx;
297 tlprcystart = int_floordivpow2(res->y0, pdy) << pdy;
298 tlprczstart = int_floordivpow2(res->z0, pdz) << pdz;
299 brprcxend = int_ceildivpow2(res->x1, pdx) << pdx;
300 brprcyend = int_ceildivpow2(res->y1, pdy) << pdy;
301 brprczend = int_ceildivpow2(res->z1, pdz) << pdz;
303 res->prctno[0] = (brprcxend - tlprcxstart) >> pdx;
304 res->prctno[1] = (brprcyend - tlprcystart) >> pdy;
305 res->prctno[2] = (brprczend - tlprczstart) >> pdz;
306 if (res->prctno[2] == 0) res->prctno[2] = 1;
308 /* p. 67, B.17 & B.18, ISO/IEC IS15444-1 : 2002 */
310 tlcbgxstart = tlprcxstart;
311 tlcbgystart = tlprcystart;
312 tlcbgzstart = tlprczstart;
313 brcbgxend = brprcxend;
314 brcbgyend = brprcyend;
315 brcbgzend = brprczend;
320 tlcbgxstart = int_ceildivpow2(tlprcxstart, 1);
321 tlcbgystart = int_ceildivpow2(tlprcystart, 1);
322 tlcbgzstart = int_ceildivpow2(tlprczstart, 1);
323 brcbgxend = int_ceildivpow2(brprcxend, 1);
324 brcbgyend = int_ceildivpow2(brprcyend, 1);
325 brcbgzend = int_ceildivpow2(brprczend, 1);
326 cbgwidthexpn = pdx - 1;
327 cbgheightexpn = pdy - 1;
328 cbglengthexpn = pdz - 1;
331 cblkwidthexpn = int_min(tccp->cblk[0], cbgwidthexpn); /*6*/
332 cblkheightexpn = int_min(tccp->cblk[1], cbgheightexpn); /*6*/
333 cblklengthexpn = int_min(tccp->cblk[2], cbglengthexpn); /*6*/
335 res->bands = (opj_tcd_band_t *) opj_malloc(res->numbands * sizeof(opj_tcd_band_t));
336 for (bandno = 0; bandno < res->numbands; bandno++) {
337 int x0b, y0b, z0b, i;
339 opj_stepsize_t *ss = NULL;
341 tcd->band = &res->bands[bandno];
344 band->bandno = (resno == 0) ? 0 : bandno + 1;
345 /* Bandno: 0 - LLL 2 - LHL
349 x0b = (band->bandno == 1) || (band->bandno == 3) || (band->bandno == 5 ) || (band->bandno == 7 ) ? 1 : 0;
350 y0b = (band->bandno == 2) || (band->bandno == 3) || (band->bandno == 6 ) || (band->bandno == 7 ) ? 1 : 0;
351 z0b = (band->bandno == 4) || (band->bandno == 5) || (band->bandno == 6 ) || (band->bandno == 7 ) ? 1 : 0;
353 /* p. 65, B.15, ISO/IEC IS15444-1 : 2002 */
354 if (band->bandno == 0) {
355 /* band border (global) */
356 band->x0 = int_ceildivpow2(tilec->x0, levelnox);
357 band->y0 = int_ceildivpow2(tilec->y0, levelnoy);
358 band->z0 = int_ceildivpow2(tilec->z0, levelnoz);
359 band->x1 = int_ceildivpow2(tilec->x1, levelnox);
360 band->y1 = int_ceildivpow2(tilec->y1, levelnoy);
361 band->z1 = int_ceildivpow2(tilec->z1, levelnoz);
363 band->x0 = int_ceildivpow2(tilec->x0 - (1 << levelnox) * x0b, levelnox + 1);
364 band->y0 = int_ceildivpow2(tilec->y0 - (1 << levelnoy) * y0b, levelnoy + 1);
365 band->z0 = int_ceildivpow2(tilec->z0 - (1 << levelnoz) * z0b, (resno <= diff) ? levelnoz : levelnoz + 1);
366 band->x1 = int_ceildivpow2(tilec->x1 - (1 << levelnox) * x0b, levelnox + 1);
367 band->y1 = int_ceildivpow2(tilec->y1 - (1 << levelnoy) * y0b, levelnoy + 1);
368 band->z1 = int_ceildivpow2(tilec->z1 - (1 << levelnoz) * z0b, (resno <= diff) ? levelnoz : levelnoz + 1);
371 ss = &tccp->stepsizes[(resno == 0) ? 0 : (prevnumbands + bandno + 1)];
372 if (bandno == (res->numbands - 1))
373 prevnumbands += (resno == 0) ? 0 : res->numbands;
374 gain = dwt_getgain(band->bandno,tccp->reversible);
375 numbps = volume->comps[compno].prec + gain;
376 band->stepsize = (float)((1.0 + ss->mant / 2048.0) * pow(2.0, numbps - ss->expn));
377 band->numbps = ss->expn + tccp->numgbits - 1; /* WHY -1 ? */
379 band->precincts = (opj_tcd_precinct_t *) opj_malloc((res->prctno[0] * res->prctno[1] * res->prctno[2]) * sizeof(opj_tcd_precinct_t));
381 for (i = 0; i < (res->prctno[0] * res->prctno[1] * res->prctno[2]); i++) {
382 band->precincts[i].imsbtree = NULL;
383 band->precincts[i].incltree = NULL;
386 for (precno = 0; precno < (res->prctno[0] * res->prctno[1] * res->prctno[2]); precno++) {
387 int tlcblkxstart, tlcblkystart, tlcblkzstart, brcblkxend, brcblkyend, brcblkzend;
388 int cbgxstart, cbgystart, cbgzstart, cbgxend, cbgyend, cbgzend;
390 cbgxstart = tlcbgxstart + (precno % res->prctno[0]) * (1 << cbgwidthexpn);
391 cbgystart = tlcbgystart + ((precno % (res->prctno[0] * res->prctno[1])) / res->prctno[0]) * (1 << cbgheightexpn);
392 cbgzstart = tlcbgzstart + (precno / (res->prctno[0] * res->prctno[1])) * (1 << cbglengthexpn);
393 cbgxend = cbgxstart + (1 << cbgwidthexpn);
394 cbgyend = cbgystart + (1 << cbgheightexpn);
395 cbgzend = cbgzstart + (1 << cbglengthexpn);
397 tcd->prc = &band->precincts[precno];
400 /* precinct size (global) */
401 prc->x0 = int_max(cbgxstart, band->x0);
402 prc->y0 = int_max(cbgystart, band->y0);
403 prc->z0 = int_max(cbgzstart, band->z0);
404 prc->x1 = int_min(cbgxend, band->x1);
405 prc->y1 = int_min(cbgyend, band->y1);
406 prc->z1 = int_min(cbgzend, band->z1);
408 tlcblkxstart = int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn;
409 tlcblkystart = int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn;
410 tlcblkzstart = int_floordivpow2(prc->z0, cblklengthexpn) << cblklengthexpn;
411 brcblkxend = int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn;
412 brcblkyend = int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn;
413 brcblkzend = int_ceildivpow2(prc->z1, cblklengthexpn) << cblklengthexpn;
414 prc->cblkno[0] = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;
415 prc->cblkno[1] = (brcblkyend - tlcblkystart) >> cblkheightexpn;
416 prc->cblkno[2] = (brcblkzend - tlcblkzstart) >> cblklengthexpn;
417 prc->cblkno[2] = (prc->cblkno[2] == 0) ? 1 : prc->cblkno[2];
419 prc->cblks = (opj_tcd_cblk_t *) opj_malloc((prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2]) * sizeof(opj_tcd_cblk_t));
420 prc->incltree = tgt_create(prc->cblkno[0], prc->cblkno[1], prc->cblkno[2]);
421 prc->imsbtree = tgt_create(prc->cblkno[0], prc->cblkno[1], prc->cblkno[2]);
422 /*tgt_tree_dump(stdout,prc->incltree);*/
423 for (cblkno = 0; cblkno < (prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2]); cblkno++) {
424 int cblkxstart = tlcblkxstart + (cblkno % prc->cblkno[0]) * (1 << cblkwidthexpn);
425 int cblkystart = tlcblkystart + ((cblkno % (prc->cblkno[0] * prc->cblkno[1])) / prc->cblkno[0]) * (1 << cblkheightexpn);
426 int cblkzstart = tlcblkzstart + (cblkno / (prc->cblkno[0] * prc->cblkno[1])) * (1 << cblklengthexpn);
427 int cblkxend = cblkxstart + (1 << cblkwidthexpn);
428 int cblkyend = cblkystart + (1 << cblkheightexpn);
429 int cblkzend = cblkzstart + (1 << cblklengthexpn);
430 int prec = ((tilec->bpp > 16) ? 3 : ((tilec->bpp > 8) ? 2 : 1));
432 tcd->cblk = &prc->cblks[cblkno];
435 /* code-block size (global) */
436 cblk->x0 = int_max(cblkxstart, prc->x0);
437 cblk->y0 = int_max(cblkystart, prc->y0);
438 cblk->z0 = int_max(cblkzstart, prc->z0);
439 cblk->x1 = int_min(cblkxend, prc->x1);
440 cblk->y1 = int_min(cblkyend, prc->y1);
441 cblk->z1 = int_min(cblkzend, prc->z1);
447 /*tcd_dump(stdout, tcd, tcd->tcd_volume);*/
450 void tcd_init_encode(opj_tcd_t *tcd, opj_volume_t * volume, opj_cp_t * cp, int curtileno) {
451 int compno, resno, bandno, precno, cblkno;
454 opj_tcd_tile_t *tile = NULL; /* pointer to tcd->tile */
455 opj_tcd_tilecomp_t *tilec = NULL; /* pointer to tcd->tilec */
456 opj_tcd_resolution_t *res = NULL; /* pointer to tcd->res */
457 opj_tcd_band_t *band = NULL; /* pointer to tcd->band */
458 opj_tcd_precinct_t *prc = NULL; /* pointer to tcd->prc */
459 opj_tcd_cblk_t *cblk = NULL; /* pointer to tcd->cblk */
460 opj_tcp_t *tcp = &cp->tcps[curtileno];
462 tcd->tile = tcd->tcd_volume->tiles;
465 /* p61 ISO/IEC IS15444-1 : 2002 */
466 /* curtileno --> raster scanned index of tiles */
467 /* p,q,r --> matricial index of tiles */
468 p = curtileno % cp->tw;
469 q = curtileno / cp->tw;
470 r = curtileno / (cp->tw * cp->th); /* extension to 3-D */
472 /* 4 borders of the tile rescale on the volume if necessary (B.3)*/
473 tile->x0 = int_max(cp->tx0 + p * cp->tdx, volume->x0);
474 tile->y0 = int_max(cp->ty0 + q * cp->tdy, volume->y0);
475 tile->z0 = int_max(cp->tz0 + r * cp->tdz, volume->z0);
476 tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, volume->x1);
477 tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, volume->y1);
478 tile->z1 = int_min(cp->tz0 + (r + 1) * cp->tdz, volume->z1);
479 tile->numcomps = volume->numcomps;
481 /* Modification of the RATE >> */
482 for (j = 0; j < tcp->numlayers; j++) {
483 if (tcp->rates[j] <= 1) {
486 float num = (float) (tile->numcomps * (tile->x1 - tile->x0) * (tile->y1 - tile->y0) * (tile->z1 - tile->z0) * volume->comps[0].prec);
487 float den = (float) (8 * volume->comps[0].dx * volume->comps[0].dy * volume->comps[0].dz);
488 den = tcp->rates[j] * den;
489 tcp->rates[j] = (num + den - 1) / den;
491 /*tcp->rates[j] = tcp->rates[j] ? int_ceildiv(
492 tile->numcomps * (tile->x1 - tile->x0) * (tile->y1 - tile->y0) * (tile->z1 - tile->z0) * volume->comps[0].prec,
493 (tcp->rates[j] * 8 * volume->comps[0].dx * volume->comps[0].dy * volume->comps[0].dz)) : 0;*/
495 if (j && tcp->rates[j] < tcp->rates[j - 1] + 10) {
496 tcp->rates[j] = tcp->rates[j - 1] + 20;
497 } else if (!j && tcp->rates[j] < 30){
502 /* << Modification of the RATE */
504 for (compno = 0; compno < tile->numcomps; compno++) {
505 opj_tccp_t *tccp = &tcp->tccps[compno];
507 int prevnumbands = 0;
509 /* opj_tcd_tilecomp_t *tilec=&tile->comps[compno]; */
510 tcd->tilec = &tile->comps[compno];
513 /* border of each tile component (global) (B.3) */
514 tilec->x0 = int_ceildiv(tile->x0, volume->comps[compno].dx);
515 tilec->y0 = int_ceildiv(tile->y0, volume->comps[compno].dy);
516 tilec->z0 = int_ceildiv(tile->z0, volume->comps[compno].dz);
517 tilec->x1 = int_ceildiv(tile->x1, volume->comps[compno].dx);
518 tilec->y1 = int_ceildiv(tile->y1, volume->comps[compno].dy);
519 tilec->z1 = int_ceildiv(tile->z1, volume->comps[compno].dz);
521 tilec->data = (int *) opj_malloc((tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0) * (tilec->z1 - tilec->z0) * sizeof(int));
524 for (i = 0;i < 3; i++){
525 tilec->numresolution[i] = tccp->numresolution[i];
526 /*Greater of 3 resolutions contains all information*/
527 res_max = (tilec->numresolution[i] > res_max) ? tilec->numresolution[i] : res_max;
530 tilec->resolutions = (opj_tcd_resolution_t *) opj_malloc(res_max * sizeof(opj_tcd_resolution_t));
531 for (resno = 0; resno < res_max; resno++) {
533 int tlprcxstart, tlprcystart, tlprczstart, brprcxend, brprcyend, brprczend;
534 int tlcbgxstart, tlcbgystart, tlcbgzstart, brcbgxend, brcbgyend, brcbgzend;
535 int cbgwidthexpn, cbgheightexpn, cbglengthexpn;
536 int cblkwidthexpn, cblkheightexpn, cblklengthexpn;
538 int levelnox = tilec->numresolution[0] - 1 - resno;
539 int levelnoy = tilec->numresolution[1] - 1 - resno;
540 int diff = tccp->numresolution[0] - tccp->numresolution[2];
541 int levelnoz = tilec->numresolution[2] - 1 - ((resno <= diff) ? 0 : (resno - diff));
542 if (levelnoz < 0) levelnoz = 0;
544 tcd->res = &tilec->resolutions[resno];
547 /* border for each resolution level (global) (B.14)*/
548 res->x0 = int_ceildivpow2(tilec->x0, levelnox);
549 res->y0 = int_ceildivpow2(tilec->y0, levelnoy);
550 res->z0 = int_ceildivpow2(tilec->z0, levelnoz);
551 res->x1 = int_ceildivpow2(tilec->x1, levelnox);
552 res->y1 = int_ceildivpow2(tilec->y1, levelnoy);
553 res->z1 = int_ceildivpow2(tilec->z1, levelnoz);
555 /* res->numbands = resno == 0 ? 1 : 3; *//* --> 2D */
557 res->numbands = (resno == 0) ? 1 : (resno <= diff) ? 3 : 7; /* --> 3D */
559 /* p. 30, table A-13, ISO/IEC IS154444-1 : 2002 */
560 if (tccp->csty & J3D_CCP_CSTY_PRT) {
561 pdx = tccp->prctsiz[0][resno];
562 pdy = tccp->prctsiz[1][resno];
563 pdz = tccp->prctsiz[2][resno];
569 /* p. 66, B.16, ISO/IEC IS15444-1 : 2002 */
570 tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx;
571 tlprcystart = int_floordivpow2(res->y0, pdy) << pdy;
572 tlprczstart = int_floordivpow2(res->z0, pdz) << pdz;
573 brprcxend = int_ceildivpow2(res->x1, pdx) << pdx;
574 brprcyend = int_ceildivpow2(res->y1, pdy) << pdy;
575 brprczend = int_ceildivpow2(res->z1, pdz) << pdz;
577 res->prctno[0] = (brprcxend - tlprcxstart) >> pdx;
578 res->prctno[1] = (brprcyend - tlprcystart) >> pdy;
579 res->prctno[2] = (brprczend - tlprczstart) >> pdz;
580 if (res->prctno[2] == 0) res->prctno[2] = 1;
582 /* p. 67, B.17 & B.18, ISO/IEC IS15444-1 : 2002 */
584 tlcbgxstart = tlprcxstart;
585 tlcbgystart = tlprcystart;
586 tlcbgzstart = tlprczstart;
587 brcbgxend = brprcxend;
588 brcbgyend = brprcyend;
589 brcbgzend = brprczend;
594 tlcbgxstart = int_ceildivpow2(tlprcxstart, 1);
595 tlcbgystart = int_ceildivpow2(tlprcystart, 1);
596 tlcbgzstart = int_ceildivpow2(tlprczstart, 1);
597 brcbgxend = int_ceildivpow2(brprcxend, 1);
598 brcbgyend = int_ceildivpow2(brprcyend, 1);
599 brcbgzend = int_ceildivpow2(brprczend, 1);
600 cbgwidthexpn = pdx - 1;
601 cbgheightexpn = pdy - 1;
602 cbglengthexpn = pdz - 1;
605 cblkwidthexpn = int_min(tccp->cblk[0], cbgwidthexpn);
606 cblkheightexpn = int_min(tccp->cblk[1], cbgheightexpn);
607 cblklengthexpn = int_min(tccp->cblk[2], cbglengthexpn);
609 res->bands = (opj_tcd_band_t *) opj_malloc(res->numbands * sizeof(opj_tcd_band_t));
610 for (bandno = 0; bandno < res->numbands; bandno++) {
613 opj_stepsize_t *ss = NULL;
615 tcd->band = &res->bands[bandno];
618 band->bandno = resno == 0 ? 0 : bandno + 1;
619 /* Bandno: 0 - LLL 2 - LHL
623 x0b = (band->bandno == 1) || (band->bandno == 3) || (band->bandno == 5 ) || (band->bandno == 7 ) ? 1 : 0;
624 y0b = (band->bandno == 2) || (band->bandno == 3) || (band->bandno == 6 ) || (band->bandno == 7 ) ? 1 : 0;
625 z0b = (band->bandno == 4) || (band->bandno == 5) || (band->bandno == 6 ) || (band->bandno == 7 ) ? 1 : 0;
627 /* p. 65, B.15, ISO/IEC IS15444-1 : 2002 */
628 if (band->bandno == 0) {
629 /* band border (global) */
630 band->x0 = int_ceildivpow2(tilec->x0, levelnox);
631 band->y0 = int_ceildivpow2(tilec->y0, levelnoy);
632 band->z0 = int_ceildivpow2(tilec->z0, levelnoz);
633 band->x1 = int_ceildivpow2(tilec->x1, levelnox);
634 band->y1 = int_ceildivpow2(tilec->y1, levelnoy);
635 band->z1 = int_ceildivpow2(tilec->z1, levelnoz);
637 band->x0 = int_ceildivpow2(tilec->x0 - (1 << levelnox) * x0b, levelnox + 1);
638 band->y0 = int_ceildivpow2(tilec->y0 - (1 << levelnoy) * y0b, levelnoy + 1);
639 band->z0 = int_ceildivpow2(tilec->z0 - (1 << levelnoz) * z0b, (resno <= diff) ? levelnoz : levelnoz + 1);
640 band->x1 = int_ceildivpow2(tilec->x1 - (1 << levelnox) * x0b, levelnox + 1);
641 band->y1 = int_ceildivpow2(tilec->y1 - (1 << levelnoy) * y0b, levelnoy + 1);
642 band->z1 = int_ceildivpow2(tilec->z1 - (1 << levelnoz) * z0b, (resno <= diff) ? levelnoz : levelnoz + 1);
645 ss = &tccp->stepsizes[(resno == 0) ? 0 : (prevnumbands + bandno + 1)];
646 if (bandno == (res->numbands - 1))
647 prevnumbands += (resno == 0) ? 0 : res->numbands;
648 gain = dwt_getgain(band->bandno,tccp->reversible);
649 numbps = volume->comps[compno].prec + gain;
651 band->stepsize = (float)((1.0 + ss->mant / 2048.0) * pow(2.0, numbps - ss->expn));
652 band->numbps = ss->expn + tccp->numgbits - 1; /* WHY -1 ? */
654 for (precno = 0; precno < res->prctno[0] * res->prctno[1] * res->prctno[2]; precno++) {
655 int tlcblkxstart, tlcblkystart, tlcblkzstart, brcblkxend, brcblkyend, brcblkzend;
657 int cbgxstart = tlcbgxstart + (precno % res->prctno[0]) * (1 << cbgwidthexpn);
658 int cbgystart = tlcbgystart + ((precno / (res->prctno[0] * res->prctno[1])) / res->prctno[0]) * (1 << cbgheightexpn);
659 int cbgzstart = tlcbgzstart + (precno / (res->prctno[0] * res->prctno[1])) * (1 << cbglengthexpn);
660 int cbgxend = cbgxstart + (1 << cbgwidthexpn);
661 int cbgyend = cbgystart + (1 << cbgheightexpn);
662 int cbgzend = cbgzstart + (1 << cbglengthexpn);
664 /* opj_tcd_precinct_t *prc=&band->precincts[precno]; */
665 tcd->prc = &band->precincts[precno];
668 /* precinct size (global) */
669 prc->x0 = int_max(cbgxstart, band->x0);
670 prc->y0 = int_max(cbgystart, band->y0);
671 prc->z0 = int_max(cbgzstart, band->z0);
672 prc->x1 = int_min(cbgxend, band->x1);
673 prc->y1 = int_min(cbgyend, band->y1);
674 prc->z1 = int_min(cbgzend, band->z1);
676 tlcblkxstart = int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn;
677 tlcblkystart = int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn;
678 tlcblkzstart = int_floordivpow2(prc->z0, cblklengthexpn) << cblklengthexpn;
679 brcblkxend = int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn;
680 brcblkyend = int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn;
681 brcblkzend = int_ceildivpow2(prc->z1, cblklengthexpn) << cblklengthexpn;
682 prc->cblkno[0] = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;
683 prc->cblkno[1] = (brcblkyend - tlcblkystart) >> cblkheightexpn;
684 prc->cblkno[2] = (brcblkzend - tlcblkzstart) >> cblklengthexpn;
685 prc->cblkno[2] = (prc->cblkno[2] == 0) ? 1 : prc->cblkno[2];
687 opj_free(prc->cblks);
688 prc->cblks = (opj_tcd_cblk_t *) opj_malloc((prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2]) * sizeof(opj_tcd_cblk_t));
689 prc->incltree = tgt_create(prc->cblkno[0], prc->cblkno[1], prc->cblkno[2]);
690 prc->imsbtree = tgt_create(prc->cblkno[0], prc->cblkno[1], prc->cblkno[2]);
692 for (cblkno = 0; cblkno < (prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2]); cblkno++) {
693 int cblkxstart = tlcblkxstart + (cblkno % prc->cblkno[0]) * (1 << cblkwidthexpn);
694 int cblkystart = tlcblkystart + ((cblkno % (prc->cblkno[0] * prc->cblkno[1])) / prc->cblkno[0]) * (1 << cblkheightexpn);
695 int cblkzstart = tlcblkzstart + (cblkno / (prc->cblkno[0] * prc->cblkno[1])) * (1 << cblklengthexpn);
696 int cblkxend = cblkxstart + (1 << cblkwidthexpn);
697 int cblkyend = cblkystart + (1 << cblkheightexpn);
698 int cblkzend = cblkzstart + (1 << cblklengthexpn);
699 int prec = ((tilec->bpp > 16) ? 3 : ((tilec->bpp > 8) ? 2 : 1));
701 tcd->cblk = &prc->cblks[cblkno];
704 /* code-block size (global) */
705 cblk->x0 = int_max(cblkxstart, prc->x0);
706 cblk->y0 = int_max(cblkystart, prc->y0);
707 cblk->z0 = int_max(cblkzstart, prc->z0);
708 cblk->x1 = int_min(cblkxend, prc->x1);
709 cblk->y1 = int_min(cblkyend, prc->y1);
710 cblk->z1 = int_min(cblkzend, prc->z1);
716 /*tcd_dump(stdout, tcd, tcd->tcd_volume);*/
720 void tcd_free_encode(opj_tcd_t *tcd) {
721 int tileno, compno, resno, bandno, precno;
723 opj_tcd_tile_t *tile = NULL; /* pointer to tcd->tile */
724 /* opj_tcd_slice_t *slice = NULL; */ /* pointer to tcd->slice */
725 opj_tcd_tilecomp_t *tilec = NULL; /* pointer to tcd->tilec */
726 opj_tcd_resolution_t *res = NULL; /* pointer to tcd->res */
727 opj_tcd_band_t *band = NULL; /* pointer to tcd->band */
728 opj_tcd_precinct_t *prc = NULL; /* pointer to tcd->prc */
730 for (tileno = 0; tileno < 1; tileno++) {
731 tcd->tile = tcd->tcd_volume->tiles;
734 for (compno = 0; compno < tile->numcomps; compno++) {
735 tcd->tilec = &tile->comps[compno];
738 for (resno = 0; resno < tilec->numresolution[0]; resno++) {
739 tcd->res = &tilec->resolutions[resno];
742 for (bandno = 0; bandno < res->numbands; bandno++) {
743 tcd->band = &res->bands[bandno];
746 for (precno = 0; precno < res->prctno[0] * res->prctno[1] * res->prctno[2]; precno++) {
747 tcd->prc = &band->precincts[precno];
750 if (prc->incltree != NULL) {
751 tgt_destroy(prc->incltree);
752 prc->incltree = NULL;
754 if (prc->imsbtree != NULL) {
755 tgt_destroy(prc->imsbtree);
756 prc->imsbtree = NULL;
758 opj_free(prc->cblks);
761 opj_free(band->precincts);
762 band->precincts = NULL;
765 opj_free(tilec->resolutions);
766 tilec->resolutions = NULL;
768 opj_free(tile->comps);
771 opj_free(tcd->tcd_volume->tiles);
772 tcd->tcd_volume->tiles = NULL;
775 /* ----------------------------------------------------------------------- */
776 void tcd_malloc_decode(opj_tcd_t *tcd, opj_volume_t * volume, opj_cp_t * cp) {
777 int tileno, compno, resno, bandno, precno, cblkno, res_max,
779 unsigned int x0 = 0, y0 = 0, z0 = 0,
780 x1 = 0, y1 = 0, z1 = 0,
783 tcd->volume = volume;
785 tcd->tcd_volume->tw = cp->tw;
786 tcd->tcd_volume->th = cp->th;
787 tcd->tcd_volume->tl = cp->tl;
788 tcd->tcd_volume->tiles = (opj_tcd_tile_t *) opj_malloc(cp->tw * cp->th * cp->tl * sizeof(opj_tcd_tile_t));
790 for (i = 0; i < cp->tileno_size; i++) {
791 opj_tcp_t *tcp = &(cp->tcps[cp->tileno[i]]);
792 opj_tcd_tile_t *tile = &(tcd->tcd_volume->tiles[cp->tileno[i]]);
794 /* p61 ISO/IEC IS15444-1 : 2002 */
795 /* curtileno --> raster scanned index of tiles */
796 /* p,q,r --> matricial index of tiles */
797 tileno = cp->tileno[i];
800 r = tileno / (cp->tw * cp->th); /* extension to 3-D */
802 /* 4 borders of the tile rescale on the volume if necessary (B.3)*/
803 tile->x0 = int_max(cp->tx0 + p * cp->tdx, volume->x0);
804 tile->y0 = int_max(cp->ty0 + q * cp->tdy, volume->y0);
805 tile->z0 = int_max(cp->tz0 + r * cp->tdz, volume->z0);
806 tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, volume->x1);
807 tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, volume->y1);
808 tile->z1 = int_min(cp->tz0 + (r + 1) * cp->tdz, volume->z1);
809 tile->numcomps = volume->numcomps;
811 tile->comps = (opj_tcd_tilecomp_t *) opj_malloc(volume->numcomps * sizeof(opj_tcd_tilecomp_t));
812 for (compno = 0; compno < tile->numcomps; compno++) {
813 opj_tccp_t *tccp = &tcp->tccps[compno];
814 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
815 int prevnumbands = 0;
817 /* border of each tile component (global) */
818 tilec->x0 = int_ceildiv(tile->x0, volume->comps[compno].dx);
819 tilec->y0 = int_ceildiv(tile->y0, volume->comps[compno].dy);
820 tilec->z0 = int_ceildiv(tile->z0, volume->comps[compno].dz);
821 tilec->x1 = int_ceildiv(tile->x1, volume->comps[compno].dx);
822 tilec->y1 = int_ceildiv(tile->y1, volume->comps[compno].dy);
823 tilec->z1 = int_ceildiv(tile->z1, volume->comps[compno].dz);
825 tilec->data = (int *) opj_malloc((tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0) * (tilec->z1 - tilec->z0) * sizeof(int));
828 for (i = 0;i < 3; i++){
829 tilec->numresolution[i] = tccp->numresolution[i];
830 /*Greater of 3 resolutions contains all information*/
831 res_max = (tilec->numresolution[i] > res_max) ? tilec->numresolution[i] : res_max;
834 tilec->resolutions = (opj_tcd_resolution_t *) opj_malloc(res_max * sizeof(opj_tcd_resolution_t));
836 for (resno = 0; resno < res_max; resno++) {
837 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
839 int tlprcxstart, tlprcystart, tlprczstart, brprcxend, brprcyend, brprczend;
840 int tlcbgxstart, tlcbgystart, tlcbgzstart, brcbgxend, brcbgyend, brcbgzend;
841 int cbgwidthexpn, cbgheightexpn, cbglengthexpn;
842 int cblkwidthexpn, cblkheightexpn, cblklengthexpn;
843 int levelnox = tilec->numresolution[0] - 1 - resno;
844 int levelnoy = tilec->numresolution[1] - 1 - resno;
845 int diff = tccp->numresolution[0] - tccp->numresolution[2];
846 int levelnoz = tilec->numresolution[2] - 1 - ((resno <= diff) ? 0 : (resno - diff));
847 if (levelnoz < 0) levelnoz = 0;
849 /* border for each resolution level (global) */
850 res->x0 = int_ceildivpow2(tilec->x0, levelnox);
851 res->y0 = int_ceildivpow2(tilec->y0, levelnoy);
852 res->z0 = int_ceildivpow2(tilec->z0, levelnoz);
853 res->x1 = int_ceildivpow2(tilec->x1, levelnox);
854 res->y1 = int_ceildivpow2(tilec->y1, levelnoy);
855 res->z1 = int_ceildivpow2(tilec->z1, levelnoz);
856 res->numbands = (resno == 0) ? 1 : (resno <= diff) ? 3 : 7; /* --> 3D */
858 /* p. 30, table A-13, ISO/IEC IS154444-1 : 2002 */
859 if (tccp->csty & J3D_CCP_CSTY_PRT) {
860 pdx = tccp->prctsiz[0][resno];
861 pdy = tccp->prctsiz[1][resno];
862 pdz = tccp->prctsiz[2][resno];
869 /* p. 66, B.16, ISO/IEC IS15444-1 : 2002 */
870 tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx;
871 tlprcystart = int_floordivpow2(res->y0, pdy) << pdy;
872 tlprczstart = int_floordivpow2(res->z0, pdz) << pdz;
873 brprcxend = int_ceildivpow2(res->x1, pdx) << pdx;
874 brprcyend = int_ceildivpow2(res->y1, pdy) << pdy;
875 brprczend = int_ceildivpow2(res->z1, pdz) << pdz;
877 res->prctno[0] = (brprcxend - tlprcxstart) >> pdx;
878 res->prctno[1] = (brprcyend - tlprcystart) >> pdy;
879 res->prctno[2] = (brprczend - tlprczstart) >> pdz;
881 /* p. 67, B.17 & B.18, ISO/IEC IS15444-1 : 2002 */
883 tlcbgxstart = tlprcxstart;/*0*/
884 tlcbgystart = tlprcystart;
885 tlcbgzstart = tlprczstart;
886 brcbgxend = brprcxend;/*1*/
887 brcbgyend = brprcyend;
888 brcbgzend = brprczend;
889 cbgwidthexpn = pdx; /*15*/
893 tlcbgxstart = int_ceildivpow2(tlprcxstart, 1);
894 tlcbgystart = int_ceildivpow2(tlprcystart, 1);
895 tlcbgzstart = int_ceildivpow2(tlprczstart, 1);
896 brcbgxend = int_ceildivpow2(brprcxend, 1);
897 brcbgyend = int_ceildivpow2(brprcyend, 1);
898 brcbgzend = int_ceildivpow2(brprczend, 1);
899 cbgwidthexpn = pdx - 1;
900 cbgheightexpn = pdy - 1;
901 cbglengthexpn = pdz - 1;
904 cblkwidthexpn = int_min(tccp->cblk[0], cbgwidthexpn); /*6*/
905 cblkheightexpn = int_min(tccp->cblk[1], cbgheightexpn); /*6*/
906 cblklengthexpn = int_min(tccp->cblk[2], cbglengthexpn); /*6*/
908 res->bands = (opj_tcd_band_t *) opj_malloc(res->numbands * sizeof(opj_tcd_band_t));
909 for (bandno = 0; bandno < res->numbands; bandno++) {
912 opj_stepsize_t *ss = NULL;
914 opj_tcd_band_t *band = &res->bands[bandno];
915 band->bandno = resno == 0 ? 0 : bandno + 1;
916 /* Bandno: 0 - LLL 2 - LHL
920 x0b = (band->bandno == 1) || (band->bandno == 3) || (band->bandno == 5 ) || (band->bandno == 7 ) ? 1 : 0;
921 y0b = (band->bandno == 2) || (band->bandno == 3) || (band->bandno == 6 ) || (band->bandno == 7 ) ? 1 : 0;
922 z0b = (band->bandno == 4) || (band->bandno == 5) || (band->bandno == 6 ) || (band->bandno == 7 ) ? 1 : 0;
924 /* p. 65, B.15, ISO/IEC IS15444-1 : 2002 */
925 if (band->bandno == 0) {
926 /* band border (global) */
927 band->x0 = int_ceildivpow2(tilec->x0, levelnox);
928 band->y0 = int_ceildivpow2(tilec->y0, levelnoy);
929 band->z0 = int_ceildivpow2(tilec->z0, levelnoz);
930 band->x1 = int_ceildivpow2(tilec->x1, levelnox);
931 band->y1 = int_ceildivpow2(tilec->y1, levelnoy);
932 band->z1 = int_ceildivpow2(tilec->z1, levelnoz);
934 band->x0 = int_ceildivpow2(tilec->x0 - (1 << levelnox) * x0b, levelnox + 1);
935 band->y0 = int_ceildivpow2(tilec->y0 - (1 << levelnoy) * y0b, levelnoy + 1);
936 band->z0 = int_ceildivpow2(tilec->z0 - (1 << levelnoz) * z0b, (resno <= diff) ? levelnoz : levelnoz + 1);
937 band->x1 = int_ceildivpow2(tilec->x1 - (1 << levelnox) * x0b, levelnox + 1);
938 band->y1 = int_ceildivpow2(tilec->y1 - (1 << levelnoy) * y0b, levelnoy + 1);
939 band->z1 = int_ceildivpow2(tilec->z1 - (1 << levelnoz) * z0b, (resno <= diff) ? levelnoz : levelnoz + 1);
942 ss = &tccp->stepsizes[(resno == 0) ? 0 : (prevnumbands + bandno + 1)];
943 if (bandno == (res->numbands - 1))
944 prevnumbands += (resno == 0) ? 0 : res->numbands;
945 gain = dwt_getgain(band->bandno,tccp->reversible);
946 numbps = volume->comps[compno].prec + gain;
948 band->stepsize = (float)((1.0 + ss->mant / 2048.0) * pow(2.0, numbps - ss->expn));
949 band->numbps = ss->expn + tccp->numgbits - 1; /* WHY -1 ? */
951 band->precincts = (opj_tcd_precinct_t *) opj_malloc(res->prctno[0] * res->prctno[1] * res->prctno[2] * sizeof(opj_tcd_precinct_t));
953 for (precno = 0; precno < res->prctno[0] * res->prctno[1] * res->prctno[2]; precno++) {
954 int tlcblkxstart, tlcblkystart, tlcblkzstart, brcblkxend, brcblkyend, brcblkzend;
956 int cbgxstart = tlcbgxstart + (precno % res->prctno[0]) * (1 << cbgwidthexpn);
957 int cbgystart = tlcbgystart + (precno / res->prctno[0]) * (1 << cbgheightexpn);
958 int cbgzstart = tlcbgzstart + (precno / (res->prctno[0] * res->prctno[1])) * (1 << cbglengthexpn);
959 int cbgxend = cbgxstart + (1 << cbgwidthexpn);
960 int cbgyend = cbgystart + (1 << cbgheightexpn);
961 int cbgzend = cbgzstart + (1 << cbglengthexpn);
963 opj_tcd_precinct_t *prc = &band->precincts[precno];
964 /* precinct size (global) */
965 prc->x0 = int_max(cbgxstart, band->x0);
966 prc->y0 = int_max(cbgystart, band->y0);
967 prc->z0 = int_max(cbgzstart, band->z0);
968 prc->x1 = int_min(cbgxend, band->x1);
969 prc->y1 = int_min(cbgyend, band->y1);
970 prc->z1 = int_min(cbgzend, band->z1);
972 tlcblkxstart = int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn;
973 tlcblkystart = int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn;
974 tlcblkzstart = int_floordivpow2(prc->z0, cblklengthexpn) << cblklengthexpn;
975 brcblkxend = int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn;
976 brcblkyend = int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn;
977 brcblkzend = int_ceildivpow2(prc->z1, cblklengthexpn) << cblklengthexpn;
978 prc->cblkno[0] = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;
979 prc->cblkno[1] = (brcblkyend - tlcblkystart) >> cblkheightexpn;
980 prc->cblkno[2] = (brcblkzend - tlcblkzstart) >> cblklengthexpn;
981 prc->cblkno[2] = (prc->cblkno[2] == 0) ? 1 : prc->cblkno[2];
983 prc->cblks = (opj_tcd_cblk_t *) opj_malloc((prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2]) * sizeof(opj_tcd_cblk_t));
984 prc->incltree = tgt_create(prc->cblkno[0], prc->cblkno[1], prc->cblkno[2]);
985 prc->imsbtree = tgt_create(prc->cblkno[0], prc->cblkno[1], prc->cblkno[2]);
987 for (cblkno = 0; cblkno < prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2]; cblkno++) {
988 int cblkxstart = tlcblkxstart + (cblkno % prc->cblkno[0]) * (1 << cblkwidthexpn);
989 int cblkystart = tlcblkystart + ((cblkno % (prc->cblkno[0] * prc->cblkno[1])) / prc->cblkno[0]) * (1 << cblkheightexpn);
990 int cblkzstart = tlcblkzstart + (cblkno / (prc->cblkno[0] * prc->cblkno[1])) * (1 << cblklengthexpn);
991 int cblkxend = cblkxstart + (1 << cblkwidthexpn);
992 int cblkyend = cblkystart + (1 << cblkheightexpn);
993 int cblkzend = cblkzstart + (1 << cblklengthexpn);
994 int prec = ((tilec->bpp > 16) ? 3 : ((tilec->bpp > 8) ? 2 : 1));
995 /* code-block size (global) */
996 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
998 /* code-block size (global) */
999 cblk->x0 = int_max(cblkxstart, prc->x0);
1000 cblk->y0 = int_max(cblkystart, prc->y0);
1001 cblk->z0 = int_max(cblkzstart, prc->z0);
1002 cblk->x1 = int_min(cblkxend, prc->x1);
1003 cblk->y1 = int_min(cblkyend, prc->y1);
1004 cblk->z1 = int_min(cblkzend, prc->z1);
1010 } /* i = 0..cp->tileno_size */
1012 /*tcd_dump(stdout, tcd, tcd->tcd_volume);*/
1015 Allocate place to store the decoded data = final volume
1016 Place limited by the tile really present in the codestream
1019 for (i = 0; i < volume->numcomps; i++) {
1020 for (j = 0; j < cp->tileno_size; j++) {
1021 tileno = cp->tileno[j];
1022 x0 = (j == 0) ? tcd->tcd_volume->tiles[tileno].comps[i].x0 : int_min(x0,(unsigned int) tcd->tcd_volume->tiles[tileno].comps[i].x0);
1023 y0 = (j == 0) ? tcd->tcd_volume->tiles[tileno].comps[i].y0 : int_min(y0,(unsigned int) tcd->tcd_volume->tiles[tileno].comps[i].y0);
1024 z0 = (j == 0) ? tcd->tcd_volume->tiles[tileno].comps[i].z0 : int_min(z0,(unsigned int) tcd->tcd_volume->tiles[tileno].comps[i].z0);
1025 x1 = (j == 0) ? tcd->tcd_volume->tiles[tileno].comps[i].x1 : int_max(x1,(unsigned int) tcd->tcd_volume->tiles[tileno].comps[i].x1);
1026 y1 = (j == 0) ? tcd->tcd_volume->tiles[tileno].comps[i].y1 : int_max(y1,(unsigned int) tcd->tcd_volume->tiles[tileno].comps[i].y1);
1027 z1 = (j == 0) ? tcd->tcd_volume->tiles[tileno].comps[i].z1 : int_max(z1,(unsigned int) tcd->tcd_volume->tiles[tileno].comps[i].z1);
1034 volume->comps[i].data = (int *) opj_malloc(w * h * l * sizeof(int));
1035 volume->comps[i].w = w;
1036 volume->comps[i].h = h;
1037 volume->comps[i].l = l;
1038 volume->comps[i].x0 = x0;
1039 volume->comps[i].y0 = y0;
1040 volume->comps[i].z0 = z0;
1041 volume->comps[i].bigendian = cp->bigendian;
1045 void tcd_free_decode(opj_tcd_t *tcd) {
1046 int tileno,compno,resno,bandno,precno;
1048 opj_tcd_volume_t *tcd_volume = tcd->tcd_volume;
1050 for (tileno = 0; tileno < tcd_volume->tw * tcd_volume->th * tcd_volume->tl; tileno++) {
1051 opj_tcd_tile_t *tile = &tcd_volume->tiles[tileno];
1052 for (compno = 0; compno < tile->numcomps; compno++) {
1053 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1054 for (resno = 0; resno < tilec->numresolution[0]; resno++) {
1055 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
1056 for (bandno = 0; bandno < res->numbands; bandno++) {
1057 opj_tcd_band_t *band = &res->bands[bandno];
1058 for (precno = 0; precno < res->prctno[1] * res->prctno[0] * res->prctno[2]; precno++) {
1059 opj_tcd_precinct_t *prec = &band->precincts[precno];
1060 if (prec->cblks != NULL) opj_free(prec->cblks);
1061 if (prec->imsbtree != NULL) tgt_destroy(prec->imsbtree);
1062 if (prec->incltree != NULL) tgt_destroy(prec->incltree);
1063 /*for (treeno = 0; treeno < prec->numtrees; treeno++){
1064 if (prec->imsbtree[treeno] != NULL) tgt_destroy(prec->imsbtree[treeno]);
1065 if (prec->incltree[treeno] != NULL) tgt_destroy(prec->incltree[treeno]);
1068 if (band->precincts != NULL) opj_free(band->precincts);
1071 if (tilec->resolutions != NULL) opj_free(tilec->resolutions);
1073 if (tile->comps != NULL) opj_free(tile->comps);
1076 if (tcd_volume->tiles != NULL) opj_free(tcd_volume->tiles);
1081 /* ----------------------------------------------------------------------- */
1082 void tcd_makelayer_fixed(opj_tcd_t *tcd, int layno, int final) {
1083 int compno, resno, bandno, precno, cblkno;
1084 int value; /*, matrice[tcd_tcp->numlayers][tcd_tile->comps[0].numresolution[0]][3]; */
1085 int matrice[10][10][3];
1088 opj_cp_t *cp = tcd->cp;
1089 opj_tcd_tile_t *tcd_tile = tcd->tcd_tile;
1090 opj_tcp_t *tcd_tcp = tcd->tcp;
1092 /*matrice=(int*)opj_malloc(tcd_tcp->numlayers*tcd_tile->comps[0].numresolution[0]*3*sizeof(int)); */
1094 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
1095 opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
1096 for (i = 0; i < tcd_tcp->numlayers; i++) {
1097 for (j = 0; j < tilec->numresolution[0]; j++) {
1098 for (k = 0; k < 3; k++) {
1100 (int) (cp->matrice[i * tilec->numresolution[0] * 3 + j * 3 + k]
1101 * (float) (tcd->volume->comps[compno].prec / 16.0));
1106 for (resno = 0; resno < tilec->numresolution[0]; resno++) {
1107 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
1108 for (bandno = 0; bandno < res->numbands; bandno++) {
1109 opj_tcd_band_t *band = &res->bands[bandno];
1110 for (precno = 0; precno < res->prctno[0] * res->prctno[1] * res->prctno[2]; precno++) {
1111 opj_tcd_precinct_t *prc = &band->precincts[precno];
1112 for (cblkno = 0; cblkno < prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2]; cblkno++) {
1113 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
1114 opj_tcd_layer_t *layer = &cblk->layers[layno];
1116 int imsb = tcd->volume->comps[compno].prec - cblk->numbps; /* number of bit-plan equal to zero */
1117 /* Correction of the matrix of coefficient to include the IMSB information */
1119 value = matrice[layno][resno][bandno];
1120 if (imsb >= value) {
1126 value = matrice[layno][resno][bandno] - matrice[layno - 1][resno][bandno];
1127 if (imsb >= matrice[layno - 1][resno][bandno]) {
1128 value -= (imsb - matrice[layno - 1][resno][bandno]);
1136 cblk->numpassesinlayers = 0;
1139 n = cblk->numpassesinlayers;
1140 if (cblk->numpassesinlayers == 0) {
1142 n = 3 * value - 2 + cblk->numpassesinlayers;
1144 n = cblk->numpassesinlayers;
1147 n = 3 * value + cblk->numpassesinlayers;
1150 layer->numpasses = n - cblk->numpassesinlayers;
1152 if (!layer->numpasses)
1155 if (cblk->numpassesinlayers == 0) {
1156 layer->len = cblk->passes[n - 1].rate;
1157 layer->data = cblk->data;
1159 layer->len = cblk->passes[n - 1].rate - cblk->passes[cblk->numpassesinlayers - 1].rate;
1160 layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate;
1163 cblk->numpassesinlayers = n;
1171 void tcd_rateallocate_fixed(opj_tcd_t *tcd) {
1173 for (layno = 0; layno < tcd->tcp->numlayers; layno++) {
1174 tcd_makelayer_fixed(tcd, layno, 1);
1178 void tcd_makelayer(opj_tcd_t *tcd, int layno, double thresh, int final) {
1179 int compno, resno, bandno, precno, cblkno, passno;
1181 opj_tcd_tile_t *tcd_tile = tcd->tcd_tile;
1183 tcd_tile->distolayer[layno] = 0; /* fixed_quality */
1185 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
1186 opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
1187 for (resno = 0; resno < tilec->numresolution[0]; resno++) {
1188 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
1189 for (bandno = 0; bandno < res->numbands; bandno++) {
1190 opj_tcd_band_t *band = &res->bands[bandno];
1191 for (precno = 0; precno < res->prctno[0] * res->prctno[1] * res->prctno[2]; precno++) {
1192 opj_tcd_precinct_t *prc = &band->precincts[precno];
1193 for (cblkno = 0; cblkno < prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2]; cblkno++) {
1194 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
1195 opj_tcd_layer_t *layer = &cblk->layers[layno];
1199 cblk->numpassesinlayers = 0;
1201 n = cblk->numpassesinlayers;
1202 for (passno = cblk->numpassesinlayers; passno < cblk->totalpasses; passno++) {
1205 opj_tcd_pass_t *pass = &cblk->passes[passno];
1208 dd = pass->distortiondec;
1210 dr = pass->rate - cblk->passes[n - 1].rate;
1211 dd = pass->distortiondec - cblk->passes[n - 1].distortiondec;
1218 if (dd / dr >= thresh){
1222 layer->numpasses = n - cblk->numpassesinlayers;
1224 if (!layer->numpasses) {
1228 if (cblk->numpassesinlayers == 0) {
1229 layer->len = cblk->passes[n - 1].rate;
1230 layer->data = cblk->data;
1231 layer->disto = cblk->passes[n - 1].distortiondec;
1233 layer->len = cblk->passes[n - 1].rate - cblk->passes[cblk->numpassesinlayers - 1].rate;
1234 layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate;
1235 layer->disto = cblk->passes[n - 1].distortiondec - cblk->passes[cblk->numpassesinlayers - 1].distortiondec;
1238 tcd_tile->distolayer[layno] += layer->disto; /* fixed_quality */
1241 cblk->numpassesinlayers = n;
1243 /* fprintf(stdout,"MakeLayer : %d %f %d %d \n",layer->len, layer->disto, layer->numpasses, n);*/
1251 bool tcd_rateallocate(opj_tcd_t *tcd, unsigned char *dest, int len, opj_volume_info_t * volume_info) {
1252 int compno, resno, bandno, precno, cblkno, passno, layno;
1254 double cumdisto[100]; /* fixed_quality */
1255 const double K = 1; /* 1.1; // fixed_quality */
1258 opj_cp_t *cp = tcd->cp;
1259 opj_tcd_tile_t *tcd_tile = tcd->tcd_tile;
1260 opj_tcp_t *tcd_tcp = tcd->tcp;
1265 tcd_tile->nbpix = 0; /* fixed_quality */
1267 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
1268 opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
1270 for (resno = 0; resno < tilec->numresolution[0]; resno++) {
1271 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
1272 for (bandno = 0; bandno < res->numbands; bandno++) {
1273 opj_tcd_band_t *band = &res->bands[bandno];
1274 for (precno = 0; precno < res->prctno[0] * res->prctno[1] * res->prctno[2]; precno++) {
1275 opj_tcd_precinct_t *prc = &band->precincts[precno];
1276 for (cblkno = 0; cblkno < prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2]; cblkno++) {
1277 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
1278 for (passno = 0; passno < cblk->totalpasses; passno++) {
1279 opj_tcd_pass_t *pass = &cblk->passes[passno];
1284 dd = pass->distortiondec;
1286 dr = pass->rate - cblk->passes[passno - 1].rate;
1287 dd = pass->distortiondec - cblk->passes[passno - 1].distortiondec;
1293 if (rdslope < min) {
1296 if (rdslope > max) {
1303 tcd_tile->nbpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0) * (cblk->z1 - cblk->z0));
1304 tilec->nbpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0) * (cblk->z1 - cblk->z0));
1310 maxSE += (((double)(1 << tcd->volume->comps[compno].prec) - 1.0)
1311 * ((double)(1 << tcd->volume->comps[compno].prec) -1.0))
1312 * ((double)(tilec->nbpix));
1315 /* add antonin index */
1316 if(volume_info && volume_info->index_on) {
1317 opj_tile_info_t *info_TL = &volume_info->tile[tcd->tcd_tileno];
1318 info_TL->nbpix = tcd_tile->nbpix;
1319 info_TL->distotile = tcd_tile->distotile;
1320 info_TL->thresh = (double *) opj_malloc(tcd_tcp->numlayers * sizeof(double));
1324 for (layno = 0; layno < tcd_tcp->numlayers; layno++) {
1328 int maxlen = tcd_tcp->rates[layno] ? int_min(((int) tcd_tcp->rates[layno]), len) : len;
1330 double distotarget; /* fixed_quality */
1334 distotarget = tcd_tile->distotile - ((K * maxSE) / pow((float)10, tcd_tcp->distoratio[layno] / 10));
1336 if ((tcd_tcp->rates[layno]) || (cp->disto_alloc==0)) {
1337 opj_t2_t *t2 = t2_create(tcd->cinfo, tcd->volume, cp);
1338 int oldl = 0, oldoldl = 0;
1339 for (i = 0; i < 128; i++) {
1340 double thresh = (lo + hi) / 2;
1342 double distoachieved = 0; /* fixed_quality -q */
1344 tcd_makelayer(tcd, layno, thresh, 0);
1346 if (cp->fixed_quality) { /* fixed_quality -q */
1347 distoachieved = (layno == 0) ? tcd_tile->distolayer[0] : cumdisto[layno - 1] + tcd_tile->distolayer[layno];
1348 if (distoachieved < distotarget) {
1353 } else { /* disto_alloc -r, fixed_alloc -f */
1354 l = t2_encode_packets(t2, tcd->tcd_tileno, tcd_tile, layno + 1, dest, maxlen, volume_info);
1355 /*fprintf(stdout, "layno %d i %d len=%d max=%d \n",layno,i,l,maxlen);*/
1359 } else if (l == oldl && oldl == oldoldl && tcd_tile->distolayer[layno] > 0.0 && i>32)
1366 goodthresh = thresh;
1377 if(volume_info && volume_info->index_on) { /* Threshold for Marcela Index */
1378 volume_info->tile[tcd->tcd_tileno].thresh[layno] = goodthresh;
1380 tcd_makelayer(tcd, layno, goodthresh, 1);
1383 cumdisto[layno] = (layno == 0) ? tcd_tile->distolayer[0] : cumdisto[layno - 1] + tcd_tile->distolayer[layno];
1389 /* ----------------------------------------------------------------------- */
1390 int tcd_encode_tile(opj_tcd_t *tcd, int tileno, unsigned char *dest, int len, opj_volume_info_t * volume_info) {
1393 double encoding_time;
1395 opj_tcd_tile_t *tile = NULL;
1396 opj_tcp_t *tcd_tcp = NULL;
1397 opj_cp_t *cp = NULL;
1399 opj_tcp_t *tcp = &tcd->cp->tcps[0];
1400 opj_tccp_t *tccp = &tcp->tccps[0];
1401 opj_volume_t *volume = tcd->volume;
1402 opj_t2_t *t2 = NULL; /* T2 component */
1404 tcd->tcd_tileno = tileno; /* current encoded/decoded tile */
1406 tcd->tcd_tile = tcd->tcd_volume->tiles; /* tile information */
1407 tile = tcd->tcd_tile;
1409 tcd->tcp = &tcd->cp->tcps[tileno]; /* coding/decoding params of tileno */
1412 cp = tcd->cp; /* coding parameters */
1415 if(volume_info && volume_info->index_on) {
1416 opj_tcd_tilecomp_t *tilec_idx = &tile->comps[0]; /* based on component 0 */
1417 for (i = 0; i < tilec_idx->numresolution[0]; i++) {
1418 opj_tcd_resolution_t *res_idx = &tilec_idx->resolutions[i];
1420 volume_info->tile[tileno].prctno[0][i] = res_idx->prctno[0];
1421 volume_info->tile[tileno].prctno[1][i] = res_idx->prctno[1];
1422 volume_info->tile[tileno].prctno[2][i] = res_idx->prctno[2];
1424 npck += res_idx->prctno[0] * res_idx->prctno[1] * res_idx->prctno[2];
1426 volume_info->tile[tileno].prctsiz[0][i] = tccp->prctsiz[0][i];
1427 volume_info->tile[tileno].prctsiz[1][i] = tccp->prctsiz[1][i];
1428 volume_info->tile[tileno].prctsiz[2][i] = tccp->prctsiz[2][i];
1430 volume_info->tile[tileno].packet = (opj_packet_info_t *) opj_malloc(volume_info->comp * volume_info->layer * npck * sizeof(opj_packet_info_t));
1434 /*---------------TILE-------------------*/
1435 encoding_time = opj_clock(); /* time needed to encode a tile */
1437 for (compno = 0; compno < tile->numcomps; compno++) {
1439 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1442 int offset_x = int_ceildiv(volume->x0, volume->comps[compno].dx); /*ceil(x0 / subsampling_dx)*/
1443 int offset_y = int_ceildiv(volume->y0, volume->comps[compno].dy);
1444 int offset_z = int_ceildiv(volume->z0, volume->comps[compno].dz);
1446 int tw = tilec->x1 - tilec->x0;
1447 int w = int_ceildiv(volume->x1 - volume->x0, volume->comps[compno].dx);
1448 int th = tilec->y1 - tilec->y0;
1449 int h = int_ceildiv(volume->y1 - volume->y0, volume->comps[compno].dy);
1450 int tl = tilec->z1 - tilec->z0;
1451 int l = int_ceildiv(volume->z1 - volume->z0, volume->comps[compno].dz);
1455 /* extract tile data from volume.comps[0].data to tile.comps[0].data */
1456 /*fprintf(stdout,"[INFO] Extract tile data\n");*/
1457 if (tcd->cp->transform_format == TRF_3D_RLS || tcd->cp->transform_format == TRF_3D_LSE) {
1460 adjust = volume->comps[compno].sgnd ? 0 : 1 << (volume->comps[compno].prec - 1); /*sign=='+' --> 2^(prec-1)*/
1461 if (volume->comps[compno].dcoffset != 0){
1462 adjust += volume->comps[compno].dcoffset;
1463 fprintf(stdout,"[INFO] DC Offset applied: DCO = %d -> adjust = %d\n",volume->comps[compno].dcoffset,adjust);
1467 if (tcd_tcp->tccps[compno].reversible == 1) { /*IF perfect reconstruction (DWT.5-3)*/
1468 for (z = tilec->z0; z < tilec->z1; z++) {
1469 for (y = tilec->y0; y < tilec->y1; y++) {
1470 /* start of the src tile scanline */
1471 int *data = &volume->comps[compno].data[(tilec->x0 - offset_x) + (y - offset_y) * w + (z - offset_z) * w * h];
1472 /* start of the dst tile scanline */
1473 int *tile_data = &tilec->data[(y - tilec->y0) * tw + (z - tilec->z0) * tw * th];
1474 for (x = tilec->x0; x < tilec->x1; x++) {
1475 *tile_data++ = *data++ - adjust;
1479 } else if (tcd_tcp->tccps[compno].reversible == 0) { /*IF not (DWT.9-7)*/
1480 for (z = tilec->z0; z < tilec->z1; z++) {
1481 for (y = tilec->y0; y < tilec->y1; y++) {
1482 /* start of the src tile scanline */
1483 int *data = &volume->comps[compno].data[(tilec->x0 - offset_x) + (y - offset_y) * w + (z - offset_z) * w * h];
1484 /* start of the dst tile scanline */
1485 int *tile_data = &tilec->data[(y - tilec->y0) * tw + (z - tilec->z0) * tw * th];
1486 for (x = tilec->x0; x < tilec->x1; x++) {
1487 *tile_data++ = (*data++ - adjust) << 13;
1495 /*----------------MCT-------------------*/
1497 int samples = (tile->comps[0].x1 - tile->comps[0].x0) * (tile->comps[0].y1 - tile->comps[0].y0) * (tile->comps[0].z1 - tile->comps[0].z0);
1498 fprintf(stdout,"[INFO] Tcd_encode_tile: mct\n");
1499 if (tcd_tcp->tccps[0].reversible == 0) {
1500 mct_encode_real(tile->comps[0].data, tile->comps[1].data, tile->comps[2].data, samples);
1502 mct_encode(tile->comps[0].data, tile->comps[1].data, tile->comps[2].data, samples);
1505 /*----------------TRANSFORM---------------------------------*/
1506 fprintf(stdout,"[INFO] Tcd_encode_tile: Transform\n");
1507 for (compno = 0; compno < tile->numcomps; compno++) {
1508 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1509 dwt_encode(tilec, tcd_tcp->tccps[compno].dwtid);
1512 /*-------------------ENTROPY CODING-----------------------------*/
1513 fprintf(stdout,"[INFO] Tcd_encode_tile: Entropy coding\n");
1514 if ((cp->encoding_format == ENCOD_2EB)||(cp->encoding_format == ENCOD_3EB))
1516 if (cp->encoding_format == ENCOD_2EB) {
1517 opj_t1_t *t1 = NULL;
1518 t1 = t1_create(tcd->cinfo);
1519 t1_encode_cblks(t1, tile, tcd_tcp);
1521 } else if (cp->encoding_format == ENCOD_3EB) {
1522 opj_t1_3d_t *t1 = NULL;
1523 t1 = t1_3d_create(tcd->cinfo);
1524 t1_3d_encode_cblks(t1, tile, tcd_tcp);
1527 /*-----------RATE-ALLOCATE------------------*/
1530 volume_info->index_write = 0;
1532 if (cp->disto_alloc || cp->fixed_quality) {
1533 fprintf(stdout,"[INFO] Tcd_encode_tile: Rate-allocate\n");
1534 tcd_rateallocate(tcd, dest, len, volume_info); /* Normal Rate/distortion allocation */
1535 } else {/* fixed_alloc */
1536 fprintf(stdout,"[INFO] Tcd_encode_tile: Rate-allocate fixed\n");
1537 tcd_rateallocate_fixed(tcd); /* Fixed layer allocation */
1540 /*--------------TIER2------------------*/
1543 volume_info->index_write = 1;
1545 fprintf(stdout,"[INFO] Tcd_encode_tile: Tier - 2\n");
1546 t2 = t2_create(tcd->cinfo, volume, cp);
1547 l = t2_encode_packets(t2, tileno, tile, tcd_tcp->numlayers, dest, len, volume_info);
1549 } else if ((cp->encoding_format == ENCOD_2GR)||(cp->encoding_format == ENCOD_3GR)) {
1551 volume_info->index_write = 1;
1553 gr = golomb_create(tcd->cinfo, volume, cp);
1554 l = golomb_encode(gr, tileno, tile, dest, len, volume_info);
1555 golomb_destroy(gr);*/
1559 /*---------------CLEAN-------------------*/
1560 fprintf(stdout,"[INFO] Tcd_encode_tile: %d bytes coded\n",l);
1561 encoding_time = opj_clock() - encoding_time;
1562 opj_event_msg(tcd->cinfo, EVT_INFO, "- tile encoded in %f s\n", encoding_time);
1564 /* cleaning memory */
1565 for (compno = 0; compno < tile->numcomps; compno++) {
1566 tcd->tilec = &tile->comps[compno];
1567 opj_free(tcd->tilec->data);
1571 fprintf(stdout,"[ERROR] Unable to perform T2 tier. Return -999.\n");
1579 bool tcd_decode_tile(opj_tcd_t *tcd, unsigned char *src, int len, int tileno) {
1581 int compno, eof = 0;
1582 double tile_time, t1_time, dwt_time;
1584 opj_tcd_tile_t *tile = NULL;
1585 opj_t2_t *t2 = NULL; /* T2 component */
1587 tcd->tcd_tileno = tileno;
1588 tcd->tcd_tile = &(tcd->tcd_volume->tiles[tileno]);
1589 tcd->tcp = &(tcd->cp->tcps[tileno]);
1590 tile = tcd->tcd_tile;
1592 tile_time = opj_clock(); /* time needed to decode a tile */
1593 opj_event_msg(tcd->cinfo, EVT_INFO, "tile %d / %d\n", tileno + 1, tcd->cp->tw * tcd->cp->th * tcd->cp->tl);
1595 if ((tcd->cp->encoding_format == ENCOD_2EB) || (tcd->cp->encoding_format == ENCOD_3EB)) {
1596 /*--------------TIER2------------------*/
1597 t2 = t2_create(tcd->cinfo, tcd->volume, tcd->cp);
1598 l = t2_decode_packets(t2, src, len, tileno, tile);
1600 opj_event_msg(tcd->cinfo, EVT_INFO, "Tcd_decode_tile: %d bytes decoded\n",l);
1604 opj_event_msg(tcd->cinfo, EVT_ERROR, "Tcd_decode_tile: incomplete bistream\n");
1607 /*------------------TIER1-----------------*/
1608 opj_event_msg(tcd->cinfo, EVT_INFO, "Tcd_decode_tile: Entropy decoding %d \n",tcd->cp->encoding_format);
1609 t1_time = opj_clock(); /* time needed to decode a tile */
1610 if (tcd->cp->encoding_format == ENCOD_2EB) {
1611 opj_t1_t *t1 = NULL; /* T1 component */
1612 t1 = t1_create(tcd->cinfo);
1613 t1_decode_cblks(t1, tile, tcd->tcp);
1615 }else if (tcd->cp->encoding_format == ENCOD_3EB) {
1616 opj_t1_3d_t *t1 = NULL; /* T1 component */
1617 t1 = t1_3d_create(tcd->cinfo);
1618 t1_3d_decode_cblks(t1, tile, tcd->tcp);
1622 t1_time = opj_clock() - t1_time;
1624 opj_event_msg(tcd->cinfo, EVT_INFO, "- tier-1 took %f s\n", t1_time);
1626 } else if ((tcd->cp->encoding_format == ENCOD_2GR)||(tcd->cp->encoding_format == ENCOD_3GR)) {
1627 opj_event_msg(tcd->cinfo, EVT_INFO, "Tcd_decode_tile: Entropy decoding -- Does nothing :-D\n");
1629 gr = golomb_create(tcd->cinfo, tcd->volume, tcd->cp);
1630 l = golomb_decode(gr, tileno, tile, src, len);
1634 opj_event_msg(tcd->cinfo, EVT_ERROR, "Tcd_decode_tile: incomplete bistream\n");
1639 /*----------------DWT---------------------*/
1640 fprintf(stdout,"[INFO] Tcd_decode_tile: Inverse DWT\n");
1641 dwt_time = opj_clock(); /* time needed to decode a tile */
1642 for (compno = 0; compno < tile->numcomps; compno++) {
1643 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1644 int stops[3], dwtid[3];
1646 for (i = 0; i < 3; i++) {
1647 if (tcd->cp->reduce[i] != 0)
1648 tcd->volume->comps[compno].resno_decoded[i] = tile->comps[compno].numresolution[i] - tcd->cp->reduce[i] - 1;
1649 stops[i] = tilec->numresolution[i] - 1 - tcd->volume->comps[compno].resno_decoded[i];
1650 if (stops[i] < 0) stops[i]=0;
1651 dwtid[i] = tcd->cp->tcps->tccps[compno].dwtid[i];
1654 dwt_decode(tilec, stops, dwtid);
1656 for (i = 0; i < 3; i++) {
1657 if (tile->comps[compno].numresolution[i] > 0) {
1658 tcd->volume->comps[compno].factor[i] = tile->comps[compno].numresolution[i] - (tcd->volume->comps[compno].resno_decoded[i] + 1);
1659 if ( (tcd->volume->comps[compno].factor[i]) < 0 )
1660 tcd->volume->comps[compno].factor[i] = 0;
1664 dwt_time = opj_clock() - dwt_time;
1666 opj_event_msg(tcd->cinfo, EVT_INFO, "- dwt took %f s\n", dwt_time);
1669 /*----------------MCT-------------------*/
1671 if (tcd->tcp->mct) {
1672 if (tcd->tcp->tccps[0].reversible == 1) {
1673 mct_decode(tile->comps[0].data, tile->comps[1].data, tile->comps[2].data,
1674 (tile->comps[0].x1 - tile->comps[0].x0) * (tile->comps[0].y1 - tile->comps[0].y0) * (tile->comps[0].z1 - tile->comps[0].z0));
1676 mct_decode_real(tile->comps[0].data, tile->comps[1].data, tile->comps[2].data,
1677 (tile->comps[0].x1 - tile->comps[0].x0) * (tile->comps[0].y1 - tile->comps[0].y0)* (tile->comps[0].z1 - tile->comps[0].z0));
1681 /*---------------TILE-------------------*/
1683 for (compno = 0; compno < tile->numcomps; compno++) {
1684 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1685 opj_tcd_resolution_t *res = &tilec->resolutions[tcd->volume->comps[compno].resno_decoded[0]];
1687 int minval = tcd->volume->comps[compno].sgnd ? -(1 << (tcd->volume->comps[compno].prec - 1)) : 0;
1688 int maxval = tcd->volume->comps[compno].sgnd ? (1 << (tcd->volume->comps[compno].prec - 1)) - 1 : (1 << tcd->volume->comps[compno].prec) - 1;
1690 int tw = tilec->x1 - tilec->x0;
1691 int w = tcd->volume->comps[compno].w;
1692 int th = tilec->y1 - tilec->y0;
1693 int h = tcd->volume->comps[compno].h;
1696 int offset_x = int_ceildivpow2(tcd->volume->comps[compno].x0, tcd->volume->comps[compno].factor[0]);
1697 int offset_y = int_ceildivpow2(tcd->volume->comps[compno].y0, tcd->volume->comps[compno].factor[1]);
1698 int offset_z = int_ceildivpow2(tcd->volume->comps[compno].z0, tcd->volume->comps[compno].factor[2]);
1700 if (tcd->cp->transform_format == TRF_3D_RLS || tcd->cp->transform_format == TRF_3D_LSE) {
1703 adjust = tcd->volume->comps[compno].sgnd ? 0 : 1 << (tcd->volume->comps[compno].prec - 1); /*sign=='+' --> 2^(prec-1)*/
1704 if (tcd->volume->comps[compno].dcoffset != 0){
1705 adjust += tcd->volume->comps[compno].dcoffset;
1706 fprintf(stdout,"[INFO] DC Offset applied: DCO = %d -> adjust = %d\n",tcd->volume->comps[compno].dcoffset,adjust);
1710 for (k = res->z0; k < res->z1; k++) {
1711 for (j = res->y0; j < res->y1; j++) {
1712 for (i = res->x0; i < res->x1; i++) {
1714 float tmp = (float)((tilec->data[i - res->x0 + (j - res->y0) * tw + (k - res->z0) * tw * th]) / 8192.0);
1716 if (tcd->tcp->tccps[compno].reversible == 1) {
1717 v = tilec->data[i - res->x0 + (j - res->y0) * tw + (k - res->z0) * tw * th];
1719 int tmp2 = ((int) (floor(fabs(tmp)))) + ((int) floor(fabs(tmp*2))%2);
1720 v = ((tmp < 0) ? -tmp2:tmp2);
1724 tcd->volume->comps[compno].data[(i - offset_x) + (j - offset_y) * w + (k - offset_z) * w * h] = int_clamp(v, minval, maxval);
1730 tile_time = opj_clock() - tile_time; /* time needed to decode a tile */
1731 opj_event_msg(tcd->cinfo, EVT_INFO, "- tile decoded in %f s\n", tile_time);
1733 for (compno = 0; compno < tile->numcomps; compno++) {
1734 opj_free(tcd->tcd_volume->tiles[tileno].comps[compno].data);
1735 tcd->tcd_volume->tiles[tileno].comps[compno].data = NULL;