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