corrade-vassal – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 vero 1 /*
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
9 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
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.
19 *
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.
31 */
32  
33 #include "opj_includes.h"
34  
35 void tcd_dump(FILE *fd, opj_tcd_t *tcd, opj_tcd_image_t * img) {
36 int tileno, compno, resno, bandno, precno;/*, cblkno;*/
37  
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);
41  
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");
50 fprintf(fd,
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");
56 fprintf(fd,
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");
62 fprintf(fd,
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");
68 fprintf(fd,
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 /*
72 for (cblkno = 0; cblkno < prec->cw * prec->ch; cblkno++) {
73 opj_tcd_cblk_t *cblk = &prec->cblks[cblkno];
74 fprintf(fd, " cblk {\n");
75 fprintf(fd,
76 " x0=%d, y0=%d, x1=%d, y1=%d\n",
77 cblk->x0, cblk->y0, cblk->x1, cblk->y1);
78 fprintf(fd, " }\n");
79 }
80 */
81 fprintf(fd, " }\n");
82 }
83 fprintf(fd, " }\n");
84 }
85 fprintf(fd, " }\n");
86 }
87 fprintf(fd, " }\n");
88 }
89 fprintf(fd, " }\n");
90 }
91 fprintf(fd, "}\n");
92 }
93  
94 /* ----------------------------------------------------------------------- */
95  
96 /**
97 Create a new TCD handle
98 */
99 opj_tcd_t* tcd_create(opj_common_ptr cinfo) {
100 /* create the tcd structure */
101 opj_tcd_t *tcd = (opj_tcd_t*)opj_malloc(sizeof(opj_tcd_t));
102 if(!tcd) return NULL;
103 tcd->cinfo = cinfo;
104 tcd->tcd_image = (opj_tcd_image_t*)opj_malloc(sizeof(opj_tcd_image_t));
105 if(!tcd->tcd_image) {
106 opj_free(tcd);
107 return NULL;
108 }
109  
110 return tcd;
111 }
112  
113 /**
114 Destroy a previously created TCD handle
115 */
116 void tcd_destroy(opj_tcd_t *tcd) {
117 if(tcd) {
118 opj_free(tcd->tcd_image);
119 opj_free(tcd);
120 }
121 }
122  
123 /* ----------------------------------------------------------------------- */
124  
125 void tcd_malloc_encode(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp, int curtileno) {
126 int tileno, compno, resno, bandno, precno, cblkno;
127  
128 tcd->image = image;
129 tcd->cp = cp;
130 tcd->tcd_image->tw = cp->tw;
131 tcd->tcd_image->th = cp->th;
132 tcd->tcd_image->tiles = (opj_tcd_tile_t *) opj_malloc(sizeof(opj_tcd_tile_t));
133  
134 for (tileno = 0; tileno < 1; tileno++) {
135 opj_tcp_t *tcp = &cp->tcps[curtileno];
136 int j;
137  
138 /* cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
139 int p = curtileno % cp->tw; /* si numerotation matricielle .. */
140 int q = curtileno / cp->tw; /* .. coordonnees de la tile (q,p) q pour ligne et p pour colonne */
141  
142 /* opj_tcd_tile_t *tile=&tcd->tcd_image->tiles[tileno]; */
143 opj_tcd_tile_t *tile = tcd->tcd_image->tiles;
144  
145 /* 4 borders of the tile rescale on the image if necessary */
146 tile->x0 = int_max(cp->tx0 + p * cp->tdx, image->x0);
147 tile->y0 = int_max(cp->ty0 + q * cp->tdy, image->y0);
148 tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, image->x1);
149 tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, image->y1);
150 tile->numcomps = image->numcomps;
151 /* tile->PPT=image->PPT; */
152  
153 /* Modification of the RATE >> */
154 for (j = 0; j < tcp->numlayers; j++) {
155 tcp->rates[j] = tcp->rates[j] ?
156 cp->tp_on ?
157 (((float) (tile->numcomps
158 * (tile->x1 - tile->x0)
159 * (tile->y1 - tile->y0)
160 * image->comps[0].prec))
161 /(tcp->rates[j] * 8 * image->comps[0].dx * image->comps[0].dy)) - (((tcd->cur_totnum_tp - 1) * 14 )/ tcp->numlayers)
162 :
163 ((float) (tile->numcomps
164 * (tile->x1 - tile->x0)
165 * (tile->y1 - tile->y0)
166 * image->comps[0].prec))/
167 (tcp->rates[j] * 8 * image->comps[0].dx * image->comps[0].dy)
168 : 0;
169  
170 if (tcp->rates[j]) {
171 if (j && tcp->rates[j] < tcp->rates[j - 1] + 10) {
172 tcp->rates[j] = tcp->rates[j - 1] + 20;
173 } else {
174 if (!j && tcp->rates[j] < 30)
175 tcp->rates[j] = 30;
176 }
177  
178 if(j == (tcp->numlayers-1)){
179 tcp->rates[j] = tcp->rates[j]- 2;
180 }
181 }
182 }
183 /* << Modification of the RATE */
184  
185 tile->comps = (opj_tcd_tilecomp_t *) opj_malloc(image->numcomps * sizeof(opj_tcd_tilecomp_t));
186 for (compno = 0; compno < tile->numcomps; compno++) {
187 opj_tccp_t *tccp = &tcp->tccps[compno];
188  
189 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
190  
191 /* border of each tile component (global) */
192 tilec->x0 = int_ceildiv(tile->x0, image->comps[compno].dx);
193 tilec->y0 = int_ceildiv(tile->y0, image->comps[compno].dy);
194 tilec->x1 = int_ceildiv(tile->x1, image->comps[compno].dx);
195 tilec->y1 = int_ceildiv(tile->y1, image->comps[compno].dy);
196  
197 tilec->data = (int *) opj_aligned_malloc((tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0) * sizeof(int));
198 tilec->numresolutions = tccp->numresolutions;
199  
200 tilec->resolutions = (opj_tcd_resolution_t *) opj_malloc(tilec->numresolutions * sizeof(opj_tcd_resolution_t));
201  
202 for (resno = 0; resno < tilec->numresolutions; resno++) {
203 int pdx, pdy;
204 int levelno = tilec->numresolutions - 1 - resno;
205 int tlprcxstart, tlprcystart, brprcxend, brprcyend;
206 int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend;
207 int cbgwidthexpn, cbgheightexpn;
208 int cblkwidthexpn, cblkheightexpn;
209  
210 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
211  
212 /* border for each resolution level (global) */
213 res->x0 = int_ceildivpow2(tilec->x0, levelno);
214 res->y0 = int_ceildivpow2(tilec->y0, levelno);
215 res->x1 = int_ceildivpow2(tilec->x1, levelno);
216 res->y1 = int_ceildivpow2(tilec->y1, levelno);
217  
218 res->numbands = resno == 0 ? 1 : 3;
219 /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */
220 if (tccp->csty & J2K_CCP_CSTY_PRT) {
221 pdx = tccp->prcw[resno];
222 pdy = tccp->prch[resno];
223 } else {
224 pdx = 15;
225 pdy = 15;
226 }
227 /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
228 tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx;
229 tlprcystart = int_floordivpow2(res->y0, pdy) << pdy;
230  
231 brprcxend = int_ceildivpow2(res->x1, pdx) << pdx;
232 brprcyend = int_ceildivpow2(res->y1, pdy) << pdy;
233  
234 res->pw = (brprcxend - tlprcxstart) >> pdx;
235 res->ph = (brprcyend - tlprcystart) >> pdy;
236  
237 if (resno == 0) {
238 tlcbgxstart = tlprcxstart;
239 tlcbgystart = tlprcystart;
240 brcbgxend = brprcxend;
241 brcbgyend = brprcyend;
242 cbgwidthexpn = pdx;
243 cbgheightexpn = pdy;
244 } else {
245 tlcbgxstart = int_ceildivpow2(tlprcxstart, 1);
246 tlcbgystart = int_ceildivpow2(tlprcystart, 1);
247 brcbgxend = int_ceildivpow2(brprcxend, 1);
248 brcbgyend = int_ceildivpow2(brprcyend, 1);
249 cbgwidthexpn = pdx - 1;
250 cbgheightexpn = pdy - 1;
251 }
252  
253 cblkwidthexpn = int_min(tccp->cblkw, cbgwidthexpn);
254 cblkheightexpn = int_min(tccp->cblkh, cbgheightexpn);
255  
256 for (bandno = 0; bandno < res->numbands; bandno++) {
257 int x0b, y0b, i;
258 int gain, numbps;
259 opj_stepsize_t *ss = NULL;
260  
261 opj_tcd_band_t *band = &res->bands[bandno];
262  
263 band->bandno = resno == 0 ? 0 : bandno + 1;
264 x0b = (band->bandno == 1) || (band->bandno == 3) ? 1 : 0;
265 y0b = (band->bandno == 2) || (band->bandno == 3) ? 1 : 0;
266  
267 if (band->bandno == 0) {
268 /* band border (global) */
269 band->x0 = int_ceildivpow2(tilec->x0, levelno);
270 band->y0 = int_ceildivpow2(tilec->y0, levelno);
271 band->x1 = int_ceildivpow2(tilec->x1, levelno);
272 band->y1 = int_ceildivpow2(tilec->y1, levelno);
273 } else {
274 /* band border (global) */
275 band->x0 = int_ceildivpow2(tilec->x0 - (1 << levelno) * x0b, levelno + 1);
276 band->y0 = int_ceildivpow2(tilec->y0 - (1 << levelno) * y0b, levelno + 1);
277 band->x1 = int_ceildivpow2(tilec->x1 - (1 << levelno) * x0b, levelno + 1);
278 band->y1 = int_ceildivpow2(tilec->y1 - (1 << levelno) * y0b, levelno + 1);
279 }
280  
281 ss = &tccp->stepsizes[resno == 0 ? 0 : 3 * (resno - 1) + bandno + 1];
282 gain = tccp->qmfbid == 0 ? dwt_getgain_real(band->bandno) : dwt_getgain(band->bandno);
283 numbps = image->comps[compno].prec + gain;
284  
285 band->stepsize = (float)((1.0 + ss->mant / 2048.0) * pow(2.0, numbps - ss->expn));
286 band->numbps = ss->expn + tccp->numgbits - 1; /* WHY -1 ? */
287  
288 band->precincts = (opj_tcd_precinct_t *) opj_malloc(3 * res->pw * res->ph * sizeof(opj_tcd_precinct_t));
289  
290 for (i = 0; i < res->pw * res->ph * 3; i++) {
291 band->precincts[i].imsbtree = NULL;
292 band->precincts[i].incltree = NULL;
293 band->precincts[i].cblks.enc = NULL;
294 }
295  
296 for (precno = 0; precno < res->pw * res->ph; precno++) {
297 int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
298  
299 int cbgxstart = tlcbgxstart + (precno % res->pw) * (1 << cbgwidthexpn);
300 int cbgystart = tlcbgystart + (precno / res->pw) * (1 << cbgheightexpn);
301 int cbgxend = cbgxstart + (1 << cbgwidthexpn);
302 int cbgyend = cbgystart + (1 << cbgheightexpn);
303  
304 opj_tcd_precinct_t *prc = &band->precincts[precno];
305  
306 /* precinct size (global) */
307 prc->x0 = int_max(cbgxstart, band->x0);
308 prc->y0 = int_max(cbgystart, band->y0);
309 prc->x1 = int_min(cbgxend, band->x1);
310 prc->y1 = int_min(cbgyend, band->y1);
311  
312 tlcblkxstart = int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn;
313 tlcblkystart = int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn;
314 brcblkxend = int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn;
315 brcblkyend = int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn;
316 prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;
317 prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn;
318  
319 prc->cblks.enc = (opj_tcd_cblk_enc_t*) opj_calloc((prc->cw * prc->ch), sizeof(opj_tcd_cblk_enc_t));
320 prc->incltree = tgt_create(prc->cw, prc->ch);
321 prc->imsbtree = tgt_create(prc->cw, prc->ch);
322  
323 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
324 int cblkxstart = tlcblkxstart + (cblkno % prc->cw) * (1 << cblkwidthexpn);
325 int cblkystart = tlcblkystart + (cblkno / prc->cw) * (1 << cblkheightexpn);
326 int cblkxend = cblkxstart + (1 << cblkwidthexpn);
327 int cblkyend = cblkystart + (1 << cblkheightexpn);
328  
329 opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno];
330  
331 /* code-block size (global) */
332 cblk->x0 = int_max(cblkxstart, prc->x0);
333 cblk->y0 = int_max(cblkystart, prc->y0);
334 cblk->x1 = int_min(cblkxend, prc->x1);
335 cblk->y1 = int_min(cblkyend, prc->y1);
336 cblk->data = (unsigned char*) opj_calloc(8192+2, sizeof(unsigned char));
337 /* FIXME: mqc_init_enc and mqc_byteout underrun the buffer if we don't do this. Why? */
338 cblk->data += 2;
339 cblk->layers = (opj_tcd_layer_t*) opj_calloc(100, sizeof(opj_tcd_layer_t));
340 cblk->passes = (opj_tcd_pass_t*) opj_calloc(100, sizeof(opj_tcd_pass_t));
341 }
342 }
343 }
344 }
345 }
346 }
347  
348 /* tcd_dump(stdout, tcd, &tcd->tcd_image); */
349 }
350  
351 void tcd_free_encode(opj_tcd_t *tcd) {
352 int tileno, compno, resno, bandno, precno, cblkno;
353  
354 for (tileno = 0; tileno < 1; tileno++) {
355 opj_tcd_tile_t *tile = tcd->tcd_image->tiles;
356  
357 for (compno = 0; compno < tile->numcomps; compno++) {
358 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
359  
360 for (resno = 0; resno < tilec->numresolutions; resno++) {
361 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
362  
363 for (bandno = 0; bandno < res->numbands; bandno++) {
364 opj_tcd_band_t *band = &res->bands[bandno];
365  
366 for (precno = 0; precno < res->pw * res->ph; precno++) {
367 opj_tcd_precinct_t *prc = &band->precincts[precno];
368  
369 if (prc->incltree != NULL) {
370 tgt_destroy(prc->incltree);
371 prc->incltree = NULL;
372 }
373 if (prc->imsbtree != NULL) {
374 tgt_destroy(prc->imsbtree);
375 prc->imsbtree = NULL;
376 }
377 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
378 opj_free(prc->cblks.enc[cblkno].data - 2);
379 opj_free(prc->cblks.enc[cblkno].layers);
380 opj_free(prc->cblks.enc[cblkno].passes);
381 }
382 opj_free(prc->cblks.enc);
383 } /* for (precno */
384 opj_free(band->precincts);
385 band->precincts = NULL;
386 } /* for (bandno */
387 } /* for (resno */
388 opj_free(tilec->resolutions);
389 tilec->resolutions = NULL;
390 } /* for (compno */
391 opj_free(tile->comps);
392 tile->comps = NULL;
393 } /* for (tileno */
394 opj_free(tcd->tcd_image->tiles);
395 tcd->tcd_image->tiles = NULL;
396 }
397  
398 void tcd_init_encode(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp, int curtileno) {
399 int tileno, compno, resno, bandno, precno, cblkno;
400  
401 for (tileno = 0; tileno < 1; tileno++) {
402 opj_tcp_t *tcp = &cp->tcps[curtileno];
403 int j;
404 /* cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
405 int p = curtileno % cp->tw;
406 int q = curtileno / cp->tw;
407  
408 opj_tcd_tile_t *tile = tcd->tcd_image->tiles;
409  
410 /* 4 borders of the tile rescale on the image if necessary */
411 tile->x0 = int_max(cp->tx0 + p * cp->tdx, image->x0);
412 tile->y0 = int_max(cp->ty0 + q * cp->tdy, image->y0);
413 tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, image->x1);
414 tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, image->y1);
415  
416 tile->numcomps = image->numcomps;
417 /* tile->PPT=image->PPT; */
418  
419 /* Modification of the RATE >> */
420 for (j = 0; j < tcp->numlayers; j++) {
421 tcp->rates[j] = tcp->rates[j] ?
422 cp->tp_on ?
423 (((float) (tile->numcomps
424 * (tile->x1 - tile->x0)
425 * (tile->y1 - tile->y0)
426 * image->comps[0].prec))
427 /(tcp->rates[j] * 8 * image->comps[0].dx * image->comps[0].dy)) - (((tcd->cur_totnum_tp - 1) * 14 )/ tcp->numlayers)
428 :
429 ((float) (tile->numcomps
430 * (tile->x1 - tile->x0)
431 * (tile->y1 - tile->y0)
432 * image->comps[0].prec))/
433 (tcp->rates[j] * 8 * image->comps[0].dx * image->comps[0].dy)
434 : 0;
435  
436 if (tcp->rates[j]) {
437 if (j && tcp->rates[j] < tcp->rates[j - 1] + 10) {
438 tcp->rates[j] = tcp->rates[j - 1] + 20;
439 } else {
440 if (!j && tcp->rates[j] < 30)
441 tcp->rates[j] = 30;
442 }
443 }
444 }
445 /* << Modification of the RATE */
446  
447 /* tile->comps=(opj_tcd_tilecomp_t*)opj_realloc(tile->comps,image->numcomps*sizeof(opj_tcd_tilecomp_t)); */
448 for (compno = 0; compno < tile->numcomps; compno++) {
449 opj_tccp_t *tccp = &tcp->tccps[compno];
450  
451 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
452  
453 /* border of each tile component (global) */
454 tilec->x0 = int_ceildiv(tile->x0, image->comps[compno].dx);
455 tilec->y0 = int_ceildiv(tile->y0, image->comps[compno].dy);
456 tilec->x1 = int_ceildiv(tile->x1, image->comps[compno].dx);
457 tilec->y1 = int_ceildiv(tile->y1, image->comps[compno].dy);
458  
459 tilec->data = (int *) opj_aligned_malloc((tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0) * sizeof(int));
460 tilec->numresolutions = tccp->numresolutions;
461 /* tilec->resolutions=(opj_tcd_resolution_t*)opj_realloc(tilec->resolutions,tilec->numresolutions*sizeof(opj_tcd_resolution_t)); */
462 for (resno = 0; resno < tilec->numresolutions; resno++) {
463 int pdx, pdy;
464  
465 int levelno = tilec->numresolutions - 1 - resno;
466 int tlprcxstart, tlprcystart, brprcxend, brprcyend;
467 int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend;
468 int cbgwidthexpn, cbgheightexpn;
469 int cblkwidthexpn, cblkheightexpn;
470  
471 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
472  
473 /* border for each resolution level (global) */
474 res->x0 = int_ceildivpow2(tilec->x0, levelno);
475 res->y0 = int_ceildivpow2(tilec->y0, levelno);
476 res->x1 = int_ceildivpow2(tilec->x1, levelno);
477 res->y1 = int_ceildivpow2(tilec->y1, levelno);
478 res->numbands = resno == 0 ? 1 : 3;
479  
480 /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */
481 if (tccp->csty & J2K_CCP_CSTY_PRT) {
482 pdx = tccp->prcw[resno];
483 pdy = tccp->prch[resno];
484 } else {
485 pdx = 15;
486 pdy = 15;
487 }
488 /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
489 tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx;
490 tlprcystart = int_floordivpow2(res->y0, pdy) << pdy;
491 brprcxend = int_ceildivpow2(res->x1, pdx) << pdx;
492 brprcyend = int_ceildivpow2(res->y1, pdy) << pdy;
493  
494 res->pw = (brprcxend - tlprcxstart) >> pdx;
495 res->ph = (brprcyend - tlprcystart) >> pdy;
496  
497 if (resno == 0) {
498 tlcbgxstart = tlprcxstart;
499 tlcbgystart = tlprcystart;
500 brcbgxend = brprcxend;
501 brcbgyend = brprcyend;
502 cbgwidthexpn = pdx;
503 cbgheightexpn = pdy;
504 } else {
505 tlcbgxstart = int_ceildivpow2(tlprcxstart, 1);
506 tlcbgystart = int_ceildivpow2(tlprcystart, 1);
507 brcbgxend = int_ceildivpow2(brprcxend, 1);
508 brcbgyend = int_ceildivpow2(brprcyend, 1);
509 cbgwidthexpn = pdx - 1;
510 cbgheightexpn = pdy - 1;
511 }
512  
513 cblkwidthexpn = int_min(tccp->cblkw, cbgwidthexpn);
514 cblkheightexpn = int_min(tccp->cblkh, cbgheightexpn);
515  
516 for (bandno = 0; bandno < res->numbands; bandno++) {
517 int x0b, y0b;
518 int gain, numbps;
519 opj_stepsize_t *ss = NULL;
520  
521 opj_tcd_band_t *band = &res->bands[bandno];
522  
523 band->bandno = resno == 0 ? 0 : bandno + 1;
524 x0b = (band->bandno == 1) || (band->bandno == 3) ? 1 : 0;
525 y0b = (band->bandno == 2) || (band->bandno == 3) ? 1 : 0;
526  
527 if (band->bandno == 0) {
528 /* band border */
529 band->x0 = int_ceildivpow2(tilec->x0, levelno);
530 band->y0 = int_ceildivpow2(tilec->y0, levelno);
531 band->x1 = int_ceildivpow2(tilec->x1, levelno);
532 band->y1 = int_ceildivpow2(tilec->y1, levelno);
533 } else {
534 band->x0 = int_ceildivpow2(tilec->x0 - (1 << levelno) * x0b, levelno + 1);
535 band->y0 = int_ceildivpow2(tilec->y0 - (1 << levelno) * y0b, levelno + 1);
536 band->x1 = int_ceildivpow2(tilec->x1 - (1 << levelno) * x0b, levelno + 1);
537 band->y1 = int_ceildivpow2(tilec->y1 - (1 << levelno) * y0b, levelno + 1);
538 }
539  
540 ss = &tccp->stepsizes[resno == 0 ? 0 : 3 * (resno - 1) + bandno + 1];
541 gain = tccp->qmfbid == 0 ? dwt_getgain_real(band->bandno) : dwt_getgain(band->bandno);
542 numbps = image->comps[compno].prec + gain;
543 band->stepsize = (float)((1.0 + ss->mant / 2048.0) * pow(2.0, numbps - ss->expn));
544 band->numbps = ss->expn + tccp->numgbits - 1; /* WHY -1 ? */
545  
546 for (precno = 0; precno < res->pw * res->ph; precno++) {
547 int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
548  
549 int cbgxstart = tlcbgxstart + (precno % res->pw) * (1 << cbgwidthexpn);
550 int cbgystart = tlcbgystart + (precno / res->pw) * (1 << cbgheightexpn);
551 int cbgxend = cbgxstart + (1 << cbgwidthexpn);
552 int cbgyend = cbgystart + (1 << cbgheightexpn);
553  
554 opj_tcd_precinct_t *prc = &band->precincts[precno];
555  
556 /* precinct size (global) */
557 prc->x0 = int_max(cbgxstart, band->x0);
558 prc->y0 = int_max(cbgystart, band->y0);
559 prc->x1 = int_min(cbgxend, band->x1);
560 prc->y1 = int_min(cbgyend, band->y1);
561  
562 tlcblkxstart = int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn;
563 tlcblkystart = int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn;
564 brcblkxend = int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn;
565 brcblkyend = int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn;
566 prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;
567 prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn;
568  
569 opj_free(prc->cblks.enc);
570 prc->cblks.enc = (opj_tcd_cblk_enc_t*) opj_calloc(prc->cw * prc->ch, sizeof(opj_tcd_cblk_enc_t));
571  
572 if (prc->incltree != NULL) {
573 tgt_destroy(prc->incltree);
574 }
575 if (prc->imsbtree != NULL) {
576 tgt_destroy(prc->imsbtree);
577 }
578  
579 prc->incltree = tgt_create(prc->cw, prc->ch);
580 prc->imsbtree = tgt_create(prc->cw, prc->ch);
581  
582 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
583 int cblkxstart = tlcblkxstart + (cblkno % prc->cw) * (1 << cblkwidthexpn);
584 int cblkystart = tlcblkystart + (cblkno / prc->cw) * (1 << cblkheightexpn);
585 int cblkxend = cblkxstart + (1 << cblkwidthexpn);
586 int cblkyend = cblkystart + (1 << cblkheightexpn);
587  
588 opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno];
589  
590 /* code-block size (global) */
591 cblk->x0 = int_max(cblkxstart, prc->x0);
592 cblk->y0 = int_max(cblkystart, prc->y0);
593 cblk->x1 = int_min(cblkxend, prc->x1);
594 cblk->y1 = int_min(cblkyend, prc->y1);
595 cblk->data = (unsigned char*) opj_calloc(8192+2, sizeof(unsigned char));
596 /* FIXME: mqc_init_enc and mqc_byteout underrun the buffer if we don't do this. Why? */
597 cblk->data += 2;
598 cblk->layers = (opj_tcd_layer_t*) opj_calloc(100, sizeof(opj_tcd_layer_t));
599 cblk->passes = (opj_tcd_pass_t*) opj_calloc(100, sizeof(opj_tcd_pass_t));
600 }
601 } /* precno */
602 } /* bandno */
603 } /* resno */
604 } /* compno */
605 } /* tileno */
606  
607 /* tcd_dump(stdout, tcd, &tcd->tcd_image); */
608 }
609  
610 void tcd_malloc_decode(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp) {
611 int i, j, tileno, p, q;
612 unsigned int x0 = 0, y0 = 0, x1 = 0, y1 = 0, w, h;
613  
614 tcd->image = image;
615 tcd->tcd_image->tw = cp->tw;
616 tcd->tcd_image->th = cp->th;
617 tcd->tcd_image->tiles = (opj_tcd_tile_t *) opj_malloc(cp->tw * cp->th * sizeof(opj_tcd_tile_t));
618  
619 /*
620 Allocate place to store the decoded data = final image
621 Place limited by the tile really present in the codestream
622 */
623  
624 for (j = 0; j < cp->tileno_size; j++) {
625 opj_tcd_tile_t *tile;
626  
627 tileno = cp->tileno[j];
628 tile = &(tcd->tcd_image->tiles[cp->tileno[tileno]]);
629 tile->numcomps = image->numcomps;
630 tile->comps = (opj_tcd_tilecomp_t*) opj_calloc(image->numcomps, sizeof(opj_tcd_tilecomp_t));
631 }
632  
633 for (i = 0; i < image->numcomps; i++) {
634 for (j = 0; j < cp->tileno_size; j++) {
635 opj_tcd_tile_t *tile;
636 opj_tcd_tilecomp_t *tilec;
637  
638 /* cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
639  
640 tileno = cp->tileno[j];
641  
642 tile = &(tcd->tcd_image->tiles[cp->tileno[tileno]]);
643 tilec = &tile->comps[i];
644  
645 p = tileno % cp->tw; /* si numerotation matricielle .. */
646 q = tileno / cp->tw; /* .. coordonnees de la tile (q,p) q pour ligne et p pour colonne */
647  
648 /* 4 borders of the tile rescale on the image if necessary */
649 tile->x0 = int_max(cp->tx0 + p * cp->tdx, image->x0);
650 tile->y0 = int_max(cp->ty0 + q * cp->tdy, image->y0);
651 tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, image->x1);
652 tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, image->y1);
653  
654 tilec->x0 = int_ceildiv(tile->x0, image->comps[i].dx);
655 tilec->y0 = int_ceildiv(tile->y0, image->comps[i].dy);
656 tilec->x1 = int_ceildiv(tile->x1, image->comps[i].dx);
657 tilec->y1 = int_ceildiv(tile->y1, image->comps[i].dy);
658  
659 x0 = j == 0 ? tilec->x0 : int_min(x0, (unsigned int) tilec->x0);
660 y0 = j == 0 ? tilec->y0 : int_min(y0, (unsigned int) tilec->y0);
661 x1 = j == 0 ? tilec->x1 : int_max(x1, (unsigned int) tilec->x1);
662 y1 = j == 0 ? tilec->y1 : int_max(y1, (unsigned int) tilec->y1);
663 }
664  
665 w = int_ceildivpow2(x1 - x0, image->comps[i].factor);
666 h = int_ceildivpow2(y1 - y0, image->comps[i].factor);
667  
668 image->comps[i].w = w;
669 image->comps[i].h = h;
670 image->comps[i].x0 = x0;
671 image->comps[i].y0 = y0;
672 }
673 }
674  
675 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) {
676 int compno, resno, bandno, precno, cblkno;
677 opj_tcp_t *tcp;
678 opj_tcd_tile_t *tile;
679  
680 OPJ_ARG_NOT_USED(cstr_info);
681  
682 tcd->cp = cp;
683  
684 tcp = &(cp->tcps[cp->tileno[tileno]]);
685 tile = &(tcd->tcd_image->tiles[cp->tileno[tileno]]);
686  
687 tileno = cp->tileno[tileno];
688  
689 for (compno = 0; compno < tile->numcomps; compno++) {
690 opj_tccp_t *tccp = &tcp->tccps[compno];
691 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
692  
693 /* border of each tile component (global) */
694 tilec->x0 = int_ceildiv(tile->x0, image->comps[compno].dx);
695 tilec->y0 = int_ceildiv(tile->y0, image->comps[compno].dy);
696 tilec->x1 = int_ceildiv(tile->x1, image->comps[compno].dx);
697 tilec->y1 = int_ceildiv(tile->y1, image->comps[compno].dy);
698  
699 tilec->numresolutions = tccp->numresolutions;
700 tilec->resolutions = (opj_tcd_resolution_t *) opj_malloc(tilec->numresolutions * sizeof(opj_tcd_resolution_t));
701  
702 for (resno = 0; resno < tilec->numresolutions; resno++) {
703 int pdx, pdy;
704 int levelno = tilec->numresolutions - 1 - resno;
705 int tlprcxstart, tlprcystart, brprcxend, brprcyend;
706 int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend;
707 int cbgwidthexpn, cbgheightexpn;
708 int cblkwidthexpn, cblkheightexpn;
709  
710 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
711  
712 /* border for each resolution level (global) */
713 res->x0 = int_ceildivpow2(tilec->x0, levelno);
714 res->y0 = int_ceildivpow2(tilec->y0, levelno);
715 res->x1 = int_ceildivpow2(tilec->x1, levelno);
716 res->y1 = int_ceildivpow2(tilec->y1, levelno);
717 res->numbands = resno == 0 ? 1 : 3;
718  
719 /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */
720 if (tccp->csty & J2K_CCP_CSTY_PRT) {
721 pdx = tccp->prcw[resno];
722 pdy = tccp->prch[resno];
723 } else {
724 pdx = 15;
725 pdy = 15;
726 }
727  
728 /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
729 tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx;
730 tlprcystart = int_floordivpow2(res->y0, pdy) << pdy;
731 brprcxend = int_ceildivpow2(res->x1, pdx) << pdx;
732 brprcyend = int_ceildivpow2(res->y1, pdy) << pdy;
733  
734 res->pw = (res->x0 == res->x1) ? 0 : ((brprcxend - tlprcxstart) >> pdx);
735 res->ph = (res->y0 == res->y1) ? 0 : ((brprcyend - tlprcystart) >> pdy);
736  
737 if (resno == 0) {
738 tlcbgxstart = tlprcxstart;
739 tlcbgystart = tlprcystart;
740 brcbgxend = brprcxend;
741 brcbgyend = brprcyend;
742 cbgwidthexpn = pdx;
743 cbgheightexpn = pdy;
744 } else {
745 tlcbgxstart = int_ceildivpow2(tlprcxstart, 1);
746 tlcbgystart = int_ceildivpow2(tlprcystart, 1);
747 brcbgxend = int_ceildivpow2(brprcxend, 1);
748 brcbgyend = int_ceildivpow2(brprcyend, 1);
749 cbgwidthexpn = pdx - 1;
750 cbgheightexpn = pdy - 1;
751 }
752  
753 cblkwidthexpn = int_min(tccp->cblkw, cbgwidthexpn);
754 cblkheightexpn = int_min(tccp->cblkh, cbgheightexpn);
755  
756 for (bandno = 0; bandno < res->numbands; bandno++) {
757 int x0b, y0b;
758 int gain, numbps;
759 opj_stepsize_t *ss = NULL;
760  
761 opj_tcd_band_t *band = &res->bands[bandno];
762 band->bandno = resno == 0 ? 0 : bandno + 1;
763 x0b = (band->bandno == 1) || (band->bandno == 3) ? 1 : 0;
764 y0b = (band->bandno == 2) || (band->bandno == 3) ? 1 : 0;
765  
766 if (band->bandno == 0) {
767 /* band border (global) */
768 band->x0 = int_ceildivpow2(tilec->x0, levelno);
769 band->y0 = int_ceildivpow2(tilec->y0, levelno);
770 band->x1 = int_ceildivpow2(tilec->x1, levelno);
771 band->y1 = int_ceildivpow2(tilec->y1, levelno);
772 } else {
773 /* band border (global) */
774 band->x0 = int_ceildivpow2(tilec->x0 - (1 << levelno) * x0b, levelno + 1);
775 band->y0 = int_ceildivpow2(tilec->y0 - (1 << levelno) * y0b, levelno + 1);
776 band->x1 = int_ceildivpow2(tilec->x1 - (1 << levelno) * x0b, levelno + 1);
777 band->y1 = int_ceildivpow2(tilec->y1 - (1 << levelno) * y0b, levelno + 1);
778 }
779  
780 ss = &tccp->stepsizes[resno == 0 ? 0 : 3 * (resno - 1) + bandno + 1];
781 gain = tccp->qmfbid == 0 ? dwt_getgain_real(band->bandno) : dwt_getgain(band->bandno);
782 numbps = image->comps[compno].prec + gain;
783 band->stepsize = (float)(((1.0 + ss->mant / 2048.0) * pow(2.0, numbps - ss->expn)) * 0.5);
784 band->numbps = ss->expn + tccp->numgbits - 1; /* WHY -1 ? */
785  
786 band->precincts = (opj_tcd_precinct_t *) opj_malloc(res->pw * res->ph * sizeof(opj_tcd_precinct_t));
787  
788 for (precno = 0; precno < res->pw * res->ph; precno++) {
789 int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
790 int cbgxstart = tlcbgxstart + (precno % res->pw) * (1 << cbgwidthexpn);
791 int cbgystart = tlcbgystart + (precno / res->pw) * (1 << cbgheightexpn);
792 int cbgxend = cbgxstart + (1 << cbgwidthexpn);
793 int cbgyend = cbgystart + (1 << cbgheightexpn);
794  
795 opj_tcd_precinct_t *prc = &band->precincts[precno];
796 /* precinct size (global) */
797 prc->x0 = int_max(cbgxstart, band->x0);
798 prc->y0 = int_max(cbgystart, band->y0);
799 prc->x1 = int_min(cbgxend, band->x1);
800 prc->y1 = int_min(cbgyend, band->y1);
801  
802 tlcblkxstart = int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn;
803 tlcblkystart = int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn;
804 brcblkxend = int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn;
805 brcblkyend = int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn;
806 prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;
807 prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn;
808  
809 prc->cblks.dec = (opj_tcd_cblk_dec_t*) opj_malloc(prc->cw * prc->ch * sizeof(opj_tcd_cblk_dec_t));
810  
811 prc->incltree = tgt_create(prc->cw, prc->ch);
812 prc->imsbtree = tgt_create(prc->cw, prc->ch);
813  
814 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
815 int cblkxstart = tlcblkxstart + (cblkno % prc->cw) * (1 << cblkwidthexpn);
816 int cblkystart = tlcblkystart + (cblkno / prc->cw) * (1 << cblkheightexpn);
817 int cblkxend = cblkxstart + (1 << cblkwidthexpn);
818 int cblkyend = cblkystart + (1 << cblkheightexpn);
819  
820 opj_tcd_cblk_dec_t* cblk = &prc->cblks.dec[cblkno];
821 cblk->data = NULL;
822 cblk->segs = NULL;
823 /* code-block size (global) */
824 cblk->x0 = int_max(cblkxstart, prc->x0);
825 cblk->y0 = int_max(cblkystart, prc->y0);
826 cblk->x1 = int_min(cblkxend, prc->x1);
827 cblk->y1 = int_min(cblkyend, prc->y1);
828 cblk->numsegs = 0;
829 }
830 } /* precno */
831 } /* bandno */
832 } /* resno */
833 } /* compno */
834 /* tcd_dump(stdout, tcd, &tcd->tcd_image); */
835 }
836  
837 void tcd_makelayer_fixed(opj_tcd_t *tcd, int layno, int final) {
838 int compno, resno, bandno, precno, cblkno;
839 int value; /*, matrice[tcd_tcp->numlayers][tcd_tile->comps[0].numresolutions][3]; */
840 int matrice[10][10][3];
841 int i, j, k;
842  
843 opj_cp_t *cp = tcd->cp;
844 opj_tcd_tile_t *tcd_tile = tcd->tcd_tile;
845 opj_tcp_t *tcd_tcp = tcd->tcp;
846  
847 /*matrice=(int*)opj_malloc(tcd_tcp->numlayers*tcd_tile->comps[0].numresolutions*3*sizeof(int)); */
848  
849 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
850 opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
851 for (i = 0; i < tcd_tcp->numlayers; i++) {
852 for (j = 0; j < tilec->numresolutions; j++) {
853 for (k = 0; k < 3; k++) {
854 matrice[i][j][k] =
855 (int) (cp->matrice[i * tilec->numresolutions * 3 + j * 3 + k]
856 * (float) (tcd->image->comps[compno].prec / 16.0));
857 }
858 }
859 }
860  
861 for (resno = 0; resno < tilec->numresolutions; resno++) {
862 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
863 for (bandno = 0; bandno < res->numbands; bandno++) {
864 opj_tcd_band_t *band = &res->bands[bandno];
865 for (precno = 0; precno < res->pw * res->ph; precno++) {
866 opj_tcd_precinct_t *prc = &band->precincts[precno];
867 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
868 opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno];
869 opj_tcd_layer_t *layer = &cblk->layers[layno];
870 int n;
871 int imsb = tcd->image->comps[compno].prec - cblk->numbps; /* number of bit-plan equal to zero */
872 /* Correction of the matrix of coefficient to include the IMSB information */
873 if (layno == 0) {
874 value = matrice[layno][resno][bandno];
875 if (imsb >= value) {
876 value = 0;
877 } else {
878 value -= imsb;
879 }
880 } else {
881 value = matrice[layno][resno][bandno] - matrice[layno - 1][resno][bandno];
882 if (imsb >= matrice[layno - 1][resno][bandno]) {
883 value -= (imsb - matrice[layno - 1][resno][bandno]);
884 if (value < 0) {
885 value = 0;
886 }
887 }
888 }
889  
890 if (layno == 0) {
891 cblk->numpassesinlayers = 0;
892 }
893  
894 n = cblk->numpassesinlayers;
895 if (cblk->numpassesinlayers == 0) {
896 if (value != 0) {
897 n = 3 * value - 2 + cblk->numpassesinlayers;
898 } else {
899 n = cblk->numpassesinlayers;
900 }
901 } else {
902 n = 3 * value + cblk->numpassesinlayers;
903 }
904  
905 layer->numpasses = n - cblk->numpassesinlayers;
906  
907 if (!layer->numpasses)
908 continue;
909  
910 if (cblk->numpassesinlayers == 0) {
911 layer->len = cblk->passes[n - 1].rate;
912 layer->data = cblk->data;
913 } else {
914 layer->len = cblk->passes[n - 1].rate - cblk->passes[cblk->numpassesinlayers - 1].rate;
915 layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate;
916 }
917 if (final)
918 cblk->numpassesinlayers = n;
919 }
920 }
921 }
922 }
923 }
924 }
925  
926 void tcd_rateallocate_fixed(opj_tcd_t *tcd) {
927 int layno;
928 for (layno = 0; layno < tcd->tcp->numlayers; layno++) {
929 tcd_makelayer_fixed(tcd, layno, 1);
930 }
931 }
932  
933 void tcd_makelayer(opj_tcd_t *tcd, int layno, double thresh, int final) {
934 int compno, resno, bandno, precno, cblkno, passno;
935  
936 opj_tcd_tile_t *tcd_tile = tcd->tcd_tile;
937  
938 tcd_tile->distolayer[layno] = 0; /* fixed_quality */
939  
940 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
941 opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
942 for (resno = 0; resno < tilec->numresolutions; resno++) {
943 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
944 for (bandno = 0; bandno < res->numbands; bandno++) {
945 opj_tcd_band_t *band = &res->bands[bandno];
946 for (precno = 0; precno < res->pw * res->ph; precno++) {
947 opj_tcd_precinct_t *prc = &band->precincts[precno];
948 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
949 opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno];
950 opj_tcd_layer_t *layer = &cblk->layers[layno];
951  
952 int n;
953 if (layno == 0) {
954 cblk->numpassesinlayers = 0;
955 }
956 n = cblk->numpassesinlayers;
957 for (passno = cblk->numpassesinlayers; passno < cblk->totalpasses; passno++) {
958 int dr;
959 double dd;
960 opj_tcd_pass_t *pass = &cblk->passes[passno];
961 if (n == 0) {
962 dr = pass->rate;
963 dd = pass->distortiondec;
964 } else {
965 dr = pass->rate - cblk->passes[n - 1].rate;
966 dd = pass->distortiondec - cblk->passes[n - 1].distortiondec;
967 }
968 if (!dr) {
969 if (dd != 0)
970 n = passno + 1;
971 continue;
972 }
973 if (dd / dr >= thresh)
974 n = passno + 1;
975 }
976 layer->numpasses = n - cblk->numpassesinlayers;
977  
978 if (!layer->numpasses) {
979 layer->disto = 0;
980 continue;
981 }
982 if (cblk->numpassesinlayers == 0) {
983 layer->len = cblk->passes[n - 1].rate;
984 layer->data = cblk->data;
985 layer->disto = cblk->passes[n - 1].distortiondec;
986 } else {
987 layer->len = cblk->passes[n - 1].rate - cblk->passes[cblk->numpassesinlayers - 1].rate;
988 layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate;
989 layer->disto = cblk->passes[n - 1].distortiondec - cblk->passes[cblk->numpassesinlayers - 1].distortiondec;
990 }
991  
992 tcd_tile->distolayer[layno] += layer->disto; /* fixed_quality */
993  
994 if (final)
995 cblk->numpassesinlayers = n;
996 }
997 }
998 }
999 }
1000 }
1001 }
1002  
1003 opj_bool tcd_rateallocate(opj_tcd_t *tcd, unsigned char *dest, int len, opj_codestream_info_t *cstr_info) {
1004 int compno, resno, bandno, precno, cblkno, passno, layno;
1005 double min, max;
1006 double cumdisto[100]; /* fixed_quality */
1007 const double K = 1; /* 1.1; fixed_quality */
1008 double maxSE = 0;
1009  
1010 opj_cp_t *cp = tcd->cp;
1011 opj_tcd_tile_t *tcd_tile = tcd->tcd_tile;
1012 opj_tcp_t *tcd_tcp = tcd->tcp;
1013  
1014 min = DBL_MAX;
1015 max = 0;
1016  
1017 tcd_tile->numpix = 0; /* fixed_quality */
1018  
1019 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
1020 opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
1021 tilec->numpix = 0;
1022  
1023 for (resno = 0; resno < tilec->numresolutions; resno++) {
1024 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
1025  
1026 for (bandno = 0; bandno < res->numbands; bandno++) {
1027 opj_tcd_band_t *band = &res->bands[bandno];
1028  
1029 for (precno = 0; precno < res->pw * res->ph; precno++) {
1030 opj_tcd_precinct_t *prc = &band->precincts[precno];
1031  
1032 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
1033 opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno];
1034  
1035 for (passno = 0; passno < cblk->totalpasses; passno++) {
1036 opj_tcd_pass_t *pass = &cblk->passes[passno];
1037 int dr;
1038 double dd, rdslope;
1039 if (passno == 0) {
1040 dr = pass->rate;
1041 dd = pass->distortiondec;
1042 } else {
1043 dr = pass->rate - cblk->passes[passno - 1].rate;
1044 dd = pass->distortiondec - cblk->passes[passno - 1].distortiondec;
1045 }
1046 if (dr == 0) {
1047 continue;
1048 }
1049 rdslope = dd / dr;
1050 if (rdslope < min) {
1051 min = rdslope;
1052 }
1053 if (rdslope > max) {
1054 max = rdslope;
1055 }
1056 } /* passno */
1057  
1058 /* fixed_quality */
1059 tcd_tile->numpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0));
1060 tilec->numpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0));
1061 } /* cbklno */
1062 } /* precno */
1063 } /* bandno */
1064 } /* resno */
1065  
1066 maxSE += (((double)(1 << tcd->image->comps[compno].prec) - 1.0)
1067 * ((double)(1 << tcd->image->comps[compno].prec) -1.0))
1068 * ((double)(tilec->numpix));
1069 } /* compno */
1070  
1071 /* index file */
1072 if(cstr_info) {
1073 opj_tile_info_t *tile_info = &cstr_info->tile[tcd->tcd_tileno];
1074 tile_info->numpix = tcd_tile->numpix;
1075 tile_info->distotile = tcd_tile->distotile;
1076 tile_info->thresh = (double *) opj_malloc(tcd_tcp->numlayers * sizeof(double));
1077 }
1078  
1079 for (layno = 0; layno < tcd_tcp->numlayers; layno++) {
1080 double lo = min;
1081 double hi = max;
1082 int success = 0;
1083 int maxlen = tcd_tcp->rates[layno] ? int_min(((int) ceil(tcd_tcp->rates[layno])), len) : len;
1084 double goodthresh = 0;
1085 double stable_thresh = 0;
1086 int i;
1087 double distotarget; /* fixed_quality */
1088  
1089 /* fixed_quality */
1090 distotarget = tcd_tile->distotile - ((K * maxSE) / pow((float)10, tcd_tcp->distoratio[layno] / 10));
1091  
1092 /* Don't try to find an optimal threshold but rather take everything not included yet, if
1093 -r xx,yy,zz,0 (disto_alloc == 1 and rates == 0)
1094 -q xx,yy,zz,0 (fixed_quality == 1 and distoratio == 0)
1095 ==> possible to have some lossy layers and the last layer for sure lossless */
1096 if ( ((cp->disto_alloc==1) && (tcd_tcp->rates[layno]>0)) || ((cp->fixed_quality==1) && (tcd_tcp->distoratio[layno]>0))) {
1097 opj_t2_t *t2 = t2_create(tcd->cinfo, tcd->image, cp);
1098 double thresh = 0;
1099  
1100 for (i = 0; i < 128; i++) {
1101 int l = 0;
1102 double distoachieved = 0; /* fixed_quality */
1103 thresh = (lo + hi) / 2;
1104  
1105 tcd_makelayer(tcd, layno, thresh, 0);
1106  
1107 if (cp->fixed_quality) { /* fixed_quality */
1108 if(cp->cinema){
1109 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, tcd->cur_totnum_tp);
1110 if (l == -999) {
1111 lo = thresh;
1112 continue;
1113 }else{
1114 distoachieved = layno == 0 ?
1115 tcd_tile->distolayer[0] : cumdisto[layno - 1] + tcd_tile->distolayer[layno];
1116 if (distoachieved < distotarget) {
1117 hi=thresh;
1118 stable_thresh = thresh;
1119 continue;
1120 }else{
1121 lo=thresh;
1122 }
1123 }
1124 }else{
1125 distoachieved = (layno == 0) ?
1126 tcd_tile->distolayer[0] : (cumdisto[layno - 1] + tcd_tile->distolayer[layno]);
1127 if (distoachieved < distotarget) {
1128 hi = thresh;
1129 stable_thresh = thresh;
1130 continue;
1131 }
1132 lo = thresh;
1133 }
1134 } else {
1135 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, tcd->cur_totnum_tp);
1136 /* TODO: what to do with l ??? seek / tell ??? */
1137 /* opj_event_msg(tcd->cinfo, EVT_INFO, "rate alloc: len=%d, max=%d\n", l, maxlen); */
1138 if (l == -999) {
1139 lo = thresh;
1140 continue;
1141 }
1142 hi = thresh;
1143 stable_thresh = thresh;
1144 }
1145 }
1146 success = 1;
1147 goodthresh = stable_thresh == 0? thresh : stable_thresh;
1148 t2_destroy(t2);
1149 } else {
1150 success = 1;
1151 goodthresh = min;
1152 }
1153  
1154 if (!success) {
1155 return OPJ_FALSE;
1156 }
1157  
1158 if(cstr_info) { /* Threshold for Marcela Index */
1159 cstr_info->tile[tcd->tcd_tileno].thresh[layno] = goodthresh;
1160 }
1161 tcd_makelayer(tcd, layno, goodthresh, 1);
1162  
1163 /* fixed_quality */
1164 cumdisto[layno] = (layno == 0) ? tcd_tile->distolayer[0] : (cumdisto[layno - 1] + tcd_tile->distolayer[layno]);
1165 }
1166  
1167 return OPJ_TRUE;
1168 }
1169  
1170 int tcd_encode_tile(opj_tcd_t *tcd, int tileno, unsigned char *dest, int len, opj_codestream_info_t *cstr_info) {
1171 int compno;
1172 int l, i, numpacks = 0;
1173 opj_tcd_tile_t *tile = NULL;
1174 opj_tcp_t *tcd_tcp = NULL;
1175 opj_cp_t *cp = NULL;
1176  
1177 opj_tcp_t *tcp = &tcd->cp->tcps[0];
1178 opj_tccp_t *tccp = &tcp->tccps[0];
1179 opj_image_t *image = tcd->image;
1180  
1181 opj_t1_t *t1 = NULL; /* T1 component */
1182 opj_t2_t *t2 = NULL; /* T2 component */
1183  
1184 tcd->tcd_tileno = tileno;
1185 tcd->tcd_tile = tcd->tcd_image->tiles;
1186 tcd->tcp = &tcd->cp->tcps[tileno];
1187  
1188 tile = tcd->tcd_tile;
1189 tcd_tcp = tcd->tcp;
1190 cp = tcd->cp;
1191  
1192 if(tcd->cur_tp_num == 0){
1193 tcd->encoding_time = opj_clock(); /* time needed to encode a tile */
1194 /* INDEX >> "Precinct_nb_X et Precinct_nb_Y" */
1195 if(cstr_info) {
1196 opj_tcd_tilecomp_t *tilec_idx = &tile->comps[0]; /* based on component 0 */
1197 for (i = 0; i < tilec_idx->numresolutions; i++) {
1198 opj_tcd_resolution_t *res_idx = &tilec_idx->resolutions[i];
1199  
1200 cstr_info->tile[tileno].pw[i] = res_idx->pw;
1201 cstr_info->tile[tileno].ph[i] = res_idx->ph;
1202  
1203 numpacks += res_idx->pw * res_idx->ph;
1204  
1205 cstr_info->tile[tileno].pdx[i] = tccp->prcw[i];
1206 cstr_info->tile[tileno].pdy[i] = tccp->prch[i];
1207 }
1208 cstr_info->tile[tileno].packet = (opj_packet_info_t*) opj_calloc(cstr_info->numcomps * cstr_info->numlayers * numpacks, sizeof(opj_packet_info_t));
1209 }
1210 /* << INDEX */
1211  
1212 /*---------------TILE-------------------*/
1213  
1214 for (compno = 0; compno < tile->numcomps; compno++) {
1215 int x, y;
1216  
1217 int adjust = image->comps[compno].sgnd ? 0 : 1 << (image->comps[compno].prec - 1);
1218 int offset_x = int_ceildiv(image->x0, image->comps[compno].dx);
1219 int offset_y = int_ceildiv(image->y0, image->comps[compno].dy);
1220  
1221 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1222 int tw = tilec->x1 - tilec->x0;
1223 int w = int_ceildiv(image->x1 - image->x0, image->comps[compno].dx);
1224  
1225 /* extract tile data */
1226  
1227 if (tcd_tcp->tccps[compno].qmfbid == 1) {
1228 for (y = tilec->y0; y < tilec->y1; y++) {
1229 /* start of the src tile scanline */
1230 int *data = &image->comps[compno].data[(tilec->x0 - offset_x) + (y - offset_y) * w];
1231 /* start of the dst tile scanline */
1232 int *tile_data = &tilec->data[(y - tilec->y0) * tw];
1233 for (x = tilec->x0; x < tilec->x1; x++) {
1234 *tile_data++ = *data++ - adjust;
1235 }
1236 }
1237 } else if (tcd_tcp->tccps[compno].qmfbid == 0) {
1238 for (y = tilec->y0; y < tilec->y1; y++) {
1239 /* start of the src tile scanline */
1240 int *data = &image->comps[compno].data[(tilec->x0 - offset_x) + (y - offset_y) * w];
1241 /* start of the dst tile scanline */
1242 int *tile_data = &tilec->data[(y - tilec->y0) * tw];
1243 for (x = tilec->x0; x < tilec->x1; x++) {
1244 *tile_data++ = (*data++ - adjust) << 11;
1245 }
1246  
1247 }
1248 }
1249 }
1250  
1251 /*----------------MCT-------------------*/
1252 if (tcd_tcp->mct) {
1253 int samples = (tile->comps[0].x1 - tile->comps[0].x0) * (tile->comps[0].y1 - tile->comps[0].y0);
1254 if (tcd_tcp->tccps[0].qmfbid == 0) {
1255 mct_encode_real(tile->comps[0].data, tile->comps[1].data, tile->comps[2].data, samples);
1256 } else {
1257 mct_encode(tile->comps[0].data, tile->comps[1].data, tile->comps[2].data, samples);
1258 }
1259 }
1260  
1261 /*----------------DWT---------------------*/
1262  
1263 for (compno = 0; compno < tile->numcomps; compno++) {
1264 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1265 if (tcd_tcp->tccps[compno].qmfbid == 1) {
1266 dwt_encode(tilec);
1267 } else if (tcd_tcp->tccps[compno].qmfbid == 0) {
1268 dwt_encode_real(tilec);
1269 }
1270 }
1271  
1272 /*------------------TIER1-----------------*/
1273 t1 = t1_create(tcd->cinfo);
1274 t1_encode_cblks(t1, tile, tcd_tcp);
1275 t1_destroy(t1);
1276  
1277 /*-----------RATE-ALLOCATE------------------*/
1278  
1279 /* INDEX */
1280 if(cstr_info) {
1281 cstr_info->index_write = 0;
1282 }
1283 if (cp->disto_alloc || cp->fixed_quality) { /* fixed_quality */
1284 /* Normal Rate/distortion allocation */
1285 tcd_rateallocate(tcd, dest, len, cstr_info);
1286 } else {
1287 /* Fixed layer allocation */
1288 tcd_rateallocate_fixed(tcd);
1289 }
1290 }
1291 /*--------------TIER2------------------*/
1292  
1293 /* INDEX */
1294 if(cstr_info) {
1295 cstr_info->index_write = 1;
1296 }
1297  
1298 t2 = t2_create(tcd->cinfo, image, cp);
1299 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,tcd->cur_totnum_tp);
1300 t2_destroy(t2);
1301  
1302 /*---------------CLEAN-------------------*/
1303  
1304  
1305 if(tcd->cur_tp_num == tcd->cur_totnum_tp - 1){
1306 tcd->encoding_time = opj_clock() - tcd->encoding_time;
1307 opj_event_msg(tcd->cinfo, EVT_INFO, "- tile encoded in %f s\n", tcd->encoding_time);
1308  
1309 /* cleaning memory */
1310 for (compno = 0; compno < tile->numcomps; compno++) {
1311 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1312 opj_aligned_free(tilec->data);
1313 }
1314 }
1315  
1316 return l;
1317 }
1318  
1319 opj_bool tcd_decode_tile(opj_tcd_t *tcd, unsigned char *src, int len, int tileno, opj_codestream_info_t *cstr_info) {
1320 int l;
1321 int compno;
1322 int eof = 0;
1323 double tile_time, t1_time, dwt_time;
1324 opj_tcd_tile_t *tile = NULL;
1325  
1326 opj_t1_t *t1 = NULL; /* T1 component */
1327 opj_t2_t *t2 = NULL; /* T2 component */
1328  
1329 tcd->tcd_tileno = tileno;
1330 tcd->tcd_tile = &(tcd->tcd_image->tiles[tileno]);
1331 tcd->tcp = &(tcd->cp->tcps[tileno]);
1332 tile = tcd->tcd_tile;
1333  
1334 tile_time = opj_clock(); /* time needed to decode a tile */
1335 opj_event_msg(tcd->cinfo, EVT_INFO, "tile %d of %d\n", tileno + 1, tcd->cp->tw * tcd->cp->th);
1336  
1337 /* INDEX >> */
1338 if(cstr_info) {
1339 int resno, compno, numprec = 0;
1340 for (compno = 0; compno < cstr_info->numcomps; compno++) {
1341 opj_tcp_t *tcp = &tcd->cp->tcps[0];
1342 opj_tccp_t *tccp = &tcp->tccps[compno];
1343 opj_tcd_tilecomp_t *tilec_idx = &tile->comps[compno];
1344 for (resno = 0; resno < tilec_idx->numresolutions; resno++) {
1345 opj_tcd_resolution_t *res_idx = &tilec_idx->resolutions[resno];
1346 cstr_info->tile[tileno].pw[resno] = res_idx->pw;
1347 cstr_info->tile[tileno].ph[resno] = res_idx->ph;
1348 numprec += res_idx->pw * res_idx->ph;
1349 if (tccp->csty & J2K_CP_CSTY_PRT) {
1350 cstr_info->tile[tileno].pdx[resno] = tccp->prcw[resno];
1351 cstr_info->tile[tileno].pdy[resno] = tccp->prch[resno];
1352 }
1353 else {
1354 cstr_info->tile[tileno].pdx[resno] = 15;
1355 cstr_info->tile[tileno].pdy[resno] = 15;
1356 }
1357 }
1358 }
1359 cstr_info->tile[tileno].packet = (opj_packet_info_t *) opj_malloc(cstr_info->numlayers * numprec * sizeof(opj_packet_info_t));
1360 cstr_info->packno = 0;
1361 }
1362 /* << INDEX */
1363  
1364 /*--------------TIER2------------------*/
1365  
1366 t2 = t2_create(tcd->cinfo, tcd->image, tcd->cp);
1367 l = t2_decode_packets(t2, src, len, tileno, tile, cstr_info);
1368 t2_destroy(t2);
1369  
1370 if (l == -999) {
1371 eof = 1;
1372 opj_event_msg(tcd->cinfo, EVT_ERROR, "tcd_decode: incomplete bistream\n");
1373 }
1374  
1375 /*------------------TIER1-----------------*/
1376  
1377 t1_time = opj_clock(); /* time needed to decode a tile */
1378 t1 = t1_create(tcd->cinfo);
1379 for (compno = 0; compno < tile->numcomps; ++compno) {
1380 opj_tcd_tilecomp_t* tilec = &tile->comps[compno];
1381 /* The +3 is headroom required by the vectorized DWT */
1382 tilec->data = (int*) opj_aligned_malloc((((tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0))+3) * sizeof(int));
1383 t1_decode_cblks(t1, tilec, &tcd->tcp->tccps[compno]);
1384 }
1385 t1_destroy(t1);
1386 t1_time = opj_clock() - t1_time;
1387 opj_event_msg(tcd->cinfo, EVT_INFO, "- tiers-1 took %f s\n", t1_time);
1388  
1389 /*----------------DWT---------------------*/
1390  
1391 dwt_time = opj_clock(); /* time needed to decode a tile */
1392 for (compno = 0; compno < tile->numcomps; compno++) {
1393 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1394 int numres2decode;
1395  
1396 if (tcd->cp->reduce != 0) {
1397 tcd->image->comps[compno].resno_decoded =
1398 tile->comps[compno].numresolutions - tcd->cp->reduce - 1;
1399 if (tcd->image->comps[compno].resno_decoded < 0) {
1400 opj_event_msg(tcd->cinfo, EVT_ERROR, "Error decoding tile. The number of resolutions to remove [%d+1] is higher than the number "
1401 " of resolutions in the original codestream [%d]\nModify the cp_reduce parameter.\n", tcd->cp->reduce, tile->comps[compno].numresolutions);
1402 return OPJ_FALSE;
1403 }
1404 }
1405  
1406 numres2decode = tcd->image->comps[compno].resno_decoded + 1;
1407 if(numres2decode > 0){
1408 if (tcd->tcp->tccps[compno].qmfbid == 1) {
1409 dwt_decode(tilec, numres2decode);
1410 } else {
1411 dwt_decode_real(tilec, numres2decode);
1412 }
1413 }
1414 }
1415 dwt_time = opj_clock() - dwt_time;
1416 opj_event_msg(tcd->cinfo, EVT_INFO, "- dwt took %f s\n", dwt_time);
1417  
1418 /*----------------MCT-------------------*/
1419  
1420 if (tcd->tcp->mct) {
1421 int n = (tile->comps[0].x1 - tile->comps[0].x0) * (tile->comps[0].y1 - tile->comps[0].y0);
1422  
1423 if (tile->numcomps >= 3 ){
1424 if (tcd->tcp->tccps[0].qmfbid == 1) {
1425 mct_decode(
1426 tile->comps[0].data,
1427 tile->comps[1].data,
1428 tile->comps[2].data,
1429 n);
1430 } else {
1431 mct_decode_real(
1432 (float*)tile->comps[0].data,
1433 (float*)tile->comps[1].data,
1434 (float*)tile->comps[2].data,
1435 n);
1436 }
1437 } else{
1438 opj_event_msg(tcd->cinfo, EVT_WARNING,"Number of components (%d) is inconsistent with a MCT. Skip the MCT step.\n",tile->numcomps);
1439 }
1440 }
1441  
1442 /*---------------TILE-------------------*/
1443  
1444 for (compno = 0; compno < tile->numcomps; ++compno) {
1445 opj_tcd_tilecomp_t* tilec = &tile->comps[compno];
1446 opj_image_comp_t* imagec = &tcd->image->comps[compno];
1447 opj_tcd_resolution_t* res = &tilec->resolutions[imagec->resno_decoded];
1448 int adjust = imagec->sgnd ? 0 : 1 << (imagec->prec - 1);
1449 int min = imagec->sgnd ? -(1 << (imagec->prec - 1)) : 0;
1450 int max = imagec->sgnd ? (1 << (imagec->prec - 1)) - 1 : (1 << imagec->prec) - 1;
1451  
1452 int tw = tilec->x1 - tilec->x0;
1453 int w = imagec->w;
1454  
1455 int offset_x = int_ceildivpow2(imagec->x0, imagec->factor);
1456 int offset_y = int_ceildivpow2(imagec->y0, imagec->factor);
1457  
1458 int i, j;
1459 if(!imagec->data){
1460 imagec->data = (int*) opj_malloc(imagec->w * imagec->h * sizeof(int));
1461 }
1462 if(tcd->tcp->tccps[compno].qmfbid == 1) {
1463 for(j = res->y0; j < res->y1; ++j) {
1464 for(i = res->x0; i < res->x1; ++i) {
1465 int v = tilec->data[i - res->x0 + (j - res->y0) * tw];
1466 v += adjust;
1467 imagec->data[(i - offset_x) + (j - offset_y) * w] = int_clamp(v, min, max);
1468 }
1469 }
1470 }else{
1471 for(j = res->y0; j < res->y1; ++j) {
1472 for(i = res->x0; i < res->x1; ++i) {
1473 float tmp = ((float*)tilec->data)[i - res->x0 + (j - res->y0) * tw];
1474 int v = lrintf(tmp);
1475 v += adjust;
1476 imagec->data[(i - offset_x) + (j - offset_y) * w] = int_clamp(v, min, max);
1477 }
1478 }
1479 }
1480 opj_aligned_free(tilec->data);
1481 }
1482  
1483 tile_time = opj_clock() - tile_time; /* time needed to decode a tile */
1484 opj_event_msg(tcd->cinfo, EVT_INFO, "- tile decoded in %f s\n", tile_time);
1485  
1486 if (eof) {
1487 return OPJ_FALSE;
1488 }
1489  
1490 return OPJ_TRUE;
1491 }
1492  
1493 void tcd_free_decode(opj_tcd_t *tcd) {
1494 opj_tcd_image_t *tcd_image = tcd->tcd_image;
1495 opj_free(tcd_image->tiles);
1496 }
1497  
1498 void tcd_free_decode_tile(opj_tcd_t *tcd, int tileno) {
1499 int compno,resno,bandno,precno;
1500  
1501 opj_tcd_image_t *tcd_image = tcd->tcd_image;
1502  
1503 opj_tcd_tile_t *tile = &tcd_image->tiles[tileno];
1504 for (compno = 0; compno < tile->numcomps; compno++) {
1505 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1506 for (resno = 0; resno < tilec->numresolutions; resno++) {
1507 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
1508 for (bandno = 0; bandno < res->numbands; bandno++) {
1509 opj_tcd_band_t *band = &res->bands[bandno];
1510 for (precno = 0; precno < res->ph * res->pw; precno++) {
1511 opj_tcd_precinct_t *prec = &band->precincts[precno];
1512 if (prec->imsbtree != NULL) tgt_destroy(prec->imsbtree);
1513 if (prec->incltree != NULL) tgt_destroy(prec->incltree);
1514 }
1515 opj_free(band->precincts);
1516 }
1517 }
1518 opj_free(tilec->resolutions);
1519 }
1520 opj_free(tile->comps);
1521 }
1522  
1523  
1524