2 * Copyright (c) 2001-2002, David Janssens
3 * Copyright (c) 2002-2004, Yannick Verschueren
4 * Copyright (c) 2002-2004, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
43 static tcd_image_t tcd_image;
45 static j2k_image_t *tcd_img;
46 static j2k_cp_t *tcd_cp;
48 static tcd_tile_t *tcd_tile;
49 static j2k_tcp_t *tcd_tcp;
50 static int tcd_tileno;
52 static tcd_tile_t *tile;
53 static tcd_tilecomp_t *tilec;
54 static tcd_resolution_t *res;
55 static tcd_band_t *band;
56 static tcd_precinct_t *prc;
57 static tcd_cblk_t *cblk;
59 extern jmp_buf j2k_error;
61 void tcd_dump(tcd_image_t * img, int curtileno)
63 int tileno, compno, resno, bandno, precno, cblkno;
64 /* fprintf(stderr, "image {\n"); */
65 fprintf(stderr, " tw=%d, th=%d x0 %d x1 %d\n", img->tw, img->th,
66 tcd_img->x0, tcd_img->x1);
67 for (tileno = 0; tileno < 1; tileno++) {
68 tcd_tile_t *tile = &tcd_image.tiles[curtileno];
69 /* fprintf(stderr, " tile {\n"); */
70 /* fprintf(stderr, " x0=%d, y0=%d, x1=%d, y1=%d, numcomps=%d\n", tile->x0, tile->y0, tile->x1, tile->y1, tile->numcomps); */
71 for (compno = 0; compno < tile->numcomps; compno++) {
72 tcd_tilecomp_t *tilec = &tile->comps[compno];
73 /* fprintf(stderr, " tilec {\n"); */
74 /* fprintf(stderr, " x0=%d, y0=%d, x1=%d, y1=%d, numresolutions=%d\n", tilec->x0, tilec->y0, tilec->x1, tilec->y1, tilec->numresolutions); */
75 for (resno = 0; resno < tilec->numresolutions; resno++) {
76 tcd_resolution_t *res = &tilec->resolutions[resno];
77 /* fprintf(stderr, "\n res {\n"); */
78 /* fprintf(stderr, " x0=%d, y0=%d, x1=%d, y1=%d, pw=%d, ph=%d, numbands=%d\n", res->x0, res->y0, res->x1, res->y1, res->pw, res->ph, res->numbands); */
79 for (bandno = 0; bandno < res->numbands; bandno++) {
80 tcd_band_t *band = &res->bands[bandno];
81 /* fprintf(stderr, " band {\n"); */
82 /* fprintf(stderr, " x0=%d, y0=%d, x1=%d, y1=%d, stepsize=%d, numbps=%d\n", band->x0, band->y0, band->x1, band->y1, band->stepsize, band->numbps); */
83 for (precno = 0; precno < res->pw * res->ph; precno++) {
84 tcd_precinct_t *prec = &band->precincts[precno];
85 /* fprintf(stderr, " prec {\n"); */
86 /* fprintf(stderr, " x0=%d, y0=%d, x1=%d, y1=%d, cw=%d, ch=%d\n", prec->x0, prec->y0, prec->x1, prec->y1, prec->cw, prec->ch); */
87 for (cblkno = 0; cblkno < prec->cw * prec->ch; cblkno++) {
88 /* tcd_cblk_t *cblk=&prec->cblks[cblkno]; */
89 /* fprintf(stderr, " cblk {\n"); */
90 /* fprintf(stderr, " x0=%d, y0=%d, x1=%d, y1=%d\n", cblk->x0, cblk->y0, cblk->x1, cblk->y1); */
91 /* fprintf(stderr, " }\n"); */
93 /* fprintf(stderr, " }\n"); */
95 /* fprintf(stderr, " }\n"); */
97 /* fprintf(stderr, " }\n"); */
99 /* fprintf(stderr, " }\n"); */
101 /* fprintf(stderr, " }\n"); */
103 /* fprintf(stderr, "}\n"); */
106 void tcd_malloc_encode(j2k_image_t * img, j2k_cp_t * cp, int curtileno)
108 int tileno, compno, resno, bandno, precno, cblkno;
111 tcd_image.tw = cp->tw;
112 tcd_image.th = cp->th;
113 tcd_image.tiles = (tcd_tile_t *) malloc(sizeof(tcd_tile_t));
115 for (tileno = 0; tileno < 1; tileno++) {
116 j2k_tcp_t *tcp = &cp->tcps[curtileno];
118 /* cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
119 int p = curtileno % cp->tw; /* si numerotation matricielle .. */
120 int q = curtileno / cp->tw; /* .. coordonnees de la tile (q,p) q pour ligne et p pour colonne */
121 /* tcd_tile_t *tile=&tcd_image.tiles[tileno]; */
122 tile = tcd_image.tiles;
123 /* 4 borders of the tile rescale on the image if necessary */
124 tile->x0 = int_max(cp->tx0 + p * cp->tdx, img->x0);
125 tile->y0 = int_max(cp->ty0 + q * cp->tdy, img->y0);
126 tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, img->x1);
127 tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, img->y1);
128 tile->numcomps = img->numcomps;
129 /* tile->PPT=img->PPT; */
130 /* Modification of the RATE >> */
131 for (j = 0; j < tcp->numlayers; j++) {
132 tcp->rates[j] = ceil(tile->numcomps * (tile->x1 - tile->x0) * (tile->y1 - tile->y0) * img->comps[0].prec / (tcp->rates[j] * 8 * img->comps[0].dx * img->comps[0].dy));
133 if (j && tcp->rates[j] < tcp->rates[j - 1] + 10) {
134 tcp->rates[j] = tcp->rates[j - 1] + 20;
136 if (!j && tcp->rates[j] < 30)
140 /* << Modification of the RATE */
142 tile->comps = (tcd_tilecomp_t *) malloc(img->numcomps * sizeof(tcd_tilecomp_t));
143 for (compno = 0; compno < tile->numcomps; compno++) {
144 j2k_tccp_t *tccp = &tcp->tccps[compno];
145 /* tcd_tilecomp_t *tilec=&tile->comps[compno]; */
146 tilec = &tile->comps[compno];
147 /* border of each tile component (global) */
148 tilec->x0 = int_ceildiv(tile->x0, img->comps[compno].dx);
150 tilec->y0 = int_ceildiv(tile->y0, img->comps[compno].dy);
151 tilec->x1 = int_ceildiv(tile->x1, img->comps[compno].dx);
152 tilec->y1 = int_ceildiv(tile->y1, img->comps[compno].dy);
154 tilec->data = (int *) malloc((tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0) * sizeof(int));
155 tilec->numresolutions = tccp->numresolutions;
157 tilec->resolutions = (tcd_resolution_t *) malloc(tilec->numresolutions * sizeof(tcd_resolution_t));
159 for (resno = 0; resno < tilec->numresolutions; resno++) {
161 int levelno = tilec->numresolutions - 1 - resno;
162 int tlprcxstart, tlprcystart, brprcxend, brprcyend;
163 int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend;
164 int cbgwidthexpn, cbgheightexpn;
165 int cblkwidthexpn, cblkheightexpn;
166 /* tcd_resolution_t *res=&tilec->resolutions[resno]; */
168 res = &tilec->resolutions[resno];
170 /* border for each resolution level (global) */
171 res->x0 = int_ceildivpow2(tilec->x0, levelno);
172 res->y0 = int_ceildivpow2(tilec->y0, levelno);
173 res->x1 = int_ceildivpow2(tilec->x1, levelno);
174 res->y1 = int_ceildivpow2(tilec->y1, levelno);
176 res->numbands = resno == 0 ? 1 : 3;
177 /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */
178 if (tccp->csty & J2K_CCP_CSTY_PRT) {
179 pdx = tccp->prcw[resno];
180 pdy = tccp->prch[resno];
185 /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
186 tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx;
187 tlprcystart = int_floordivpow2(res->y0, pdy) << pdy;
188 brprcxend = int_ceildivpow2(res->x1, pdx) << pdx;
189 brprcyend = int_ceildivpow2(res->y1, pdy) << pdy;
191 res->pw = (brprcxend - tlprcxstart) >> pdx;
192 res->ph = (brprcyend - tlprcystart) >> pdy;
195 tlcbgxstart = tlprcxstart;
196 tlcbgystart = tlprcystart;
197 brcbgxend = brprcxend;
198 brcbgyend = brprcyend;
202 tlcbgxstart = int_ceildivpow2(tlprcxstart, 1);
203 tlcbgystart = int_ceildivpow2(tlprcystart, 1);
204 brcbgxend = int_ceildivpow2(brprcxend, 1);
205 brcbgyend = int_ceildivpow2(brprcyend, 1);
206 cbgwidthexpn = pdx - 1;
207 cbgheightexpn = pdy - 1;
210 cblkwidthexpn = int_min(tccp->cblkw, cbgwidthexpn);
211 cblkheightexpn = int_min(tccp->cblkh, cbgheightexpn);
213 for (bandno = 0; bandno < res->numbands; bandno++) {
217 band = &res->bands[bandno];
218 band->bandno = resno == 0 ? 0 : bandno + 1;
219 x0b = (band->bandno == 1) || (band->bandno == 3) ? 1 : 0;
220 y0b = (band->bandno == 2) || (band->bandno == 3) ? 1 : 0;
222 if (band->bandno == 0) {
223 /* band border (global) */
224 band->x0 = int_ceildivpow2(tilec->x0, levelno);
225 band->y0 = int_ceildivpow2(tilec->y0, levelno);
226 band->x1 = int_ceildivpow2(tilec->x1, levelno);
227 band->y1 = int_ceildivpow2(tilec->y1, levelno);
229 /* band border (global) */
230 band->x0 = int_ceildivpow2(tilec->x0 - (1 << levelno) * x0b, levelno + 1);
231 band->y0 = int_ceildivpow2(tilec->y0 - (1 << levelno) * y0b, levelno + 1);
232 band->x1 = int_ceildivpow2(tilec->x1 - (1 << levelno) * x0b, levelno + 1);
233 band->y1 = int_ceildivpow2(tilec->y1 - (1 << levelno) * y0b, levelno + 1);
237 ss = &tccp->stepsizes[resno == 0 ? 0 : 3 * (resno - 1) + bandno + 1];
238 gain = tccp->qmfbid == 0 ? dwt_getgain_real(band->bandno) : dwt_getgain(band->bandno);
239 numbps = img->comps[compno].prec + gain;
240 band->stepsize = (int) floor((1.0 + ss->mant / 2048.0) * pow(2.0, numbps - ss->expn) * 8192.0);
241 band->numbps = ss->expn + tccp->numgbits - 1; /* WHY -1 ? */
243 band->precincts = (tcd_precinct_t *) malloc(3 * res->pw * res->ph * sizeof(tcd_precinct_t));
245 for (i = 0; i < res->pw * res->ph * 3; i++) {
246 band->precincts[i].imsbtree = NULL;
247 band->precincts[i].incltree = NULL;
250 for (precno = 0; precno < res->pw * res->ph; precno++) {
251 int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
253 tlcbgxstart + (precno % res->pw) * (1 << cbgwidthexpn);
255 tlcbgystart + (precno / res->pw) * (1 << cbgheightexpn);
256 int cbgxend = cbgxstart + (1 << cbgwidthexpn);
257 int cbgyend = cbgystart + (1 << cbgheightexpn);
258 /* tcd_precinct_t *prc=&band->precincts[precno]; */
259 prc = &band->precincts[precno];
260 /* precinct size (global) */
261 prc->x0 = int_max(cbgxstart, band->x0);
262 prc->y0 = int_max(cbgystart, band->y0);
263 prc->x1 = int_min(cbgxend, band->x1);
264 prc->y1 = int_min(cbgyend, band->y1);
266 tlcblkxstart = int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn;
267 tlcblkystart = int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn;
268 brcblkxend = int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn;
269 brcblkyend = int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn;
270 prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;
271 prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn;
273 prc->cblks = (tcd_cblk_t *) malloc((prc->cw * prc->ch) * sizeof(tcd_cblk_t));
274 prc->incltree = tgt_create(prc->cw, prc->ch);
275 prc->imsbtree = tgt_create(prc->cw, prc->ch);
277 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
278 int cblkxstart = tlcblkxstart + (cblkno % prc->cw) * (1 << cblkwidthexpn);
279 int cblkystart = tlcblkystart + (cblkno / prc->cw) * (1 << cblkheightexpn);
280 int cblkxend = cblkxstart + (1 << cblkwidthexpn);
281 int cblkyend = cblkystart + (1 << cblkheightexpn);
283 cblk = &prc->cblks[cblkno];
284 /* code-block size (global) */
285 cblk->x0 = int_max(cblkxstart, prc->x0);
286 cblk->y0 = int_max(cblkystart, prc->y0);
287 cblk->x1 = int_min(cblkxend, prc->x1);
288 cblk->y1 = int_min(cblkyend, prc->y1);
295 /* tcd_dump(&tcd_image,curtileno); */
298 void tcd_free_encode(j2k_image_t * img, j2k_cp_t * cp, int curtileno)
300 int tileno, compno, resno, bandno, precno;
303 tcd_image.tw = cp->tw;
304 tcd_image.th = cp->th;
305 for (tileno = 0; tileno < 1; tileno++) {
306 /* j2k_tcp_t *tcp=&cp->tcps[curtileno]; */
307 tile = tcd_image.tiles;
308 for (compno = 0; compno < tile->numcomps; compno++) {
309 tilec = &tile->comps[compno];
310 for (resno = 0; resno < tilec->numresolutions; resno++) {
311 res = &tilec->resolutions[resno];
312 for (bandno = 0; bandno < res->numbands; bandno++) {
313 band = &res->bands[bandno];
314 for (precno = 0; precno < res->pw * res->ph; precno++) {
315 prc = &band->precincts[precno];
317 if (prc->incltree != NULL)
318 tgt_destroy(prc->incltree);
319 if (prc->imsbtree != NULL)
320 tgt_destroy(prc->imsbtree);
323 free(band->precincts);
326 free(tilec->resolutions);
330 free(tcd_image.tiles);
333 void tcd_init_encode(j2k_image_t * img, j2k_cp_t * cp, int curtileno)
335 int tileno, compno, resno, bandno, precno, cblkno;
337 for (tileno = 0; tileno < 1; tileno++) {
338 j2k_tcp_t *tcp = &cp->tcps[curtileno];
340 // int previous_x0, previous_x1, previous_y0, previous_y1;
341 /* cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
342 int p = curtileno % cp->tw;
343 int q = curtileno / cp->tw;
344 tile = tcd_image.tiles;
346 /* 4 borders of the tile rescale on the image if necessary */
347 tile->x0 = int_max(cp->tx0 + p * cp->tdx, img->x0);
348 tile->y0 = int_max(cp->ty0 + q * cp->tdy, img->y0);
349 tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, img->x1);
350 tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, img->y1);
352 tile->numcomps = img->numcomps;
353 /* tile->PPT=img->PPT; */
354 /* Modification of the RATE >> */
355 for (j = 0; j < tcp->numlayers; j++) {
356 tcp->rates[j] = ceil(tile->numcomps * (tile->x1 - tile->x0) * (tile->y1 - tile->y0) * img->comps[0].prec
357 / (tcp->rates[j] * 8 * img->comps[0].dx * img->comps[0].dy));
358 if (j && tcp->rates[j] < tcp->rates[j - 1] + 10) {
359 tcp->rates[j] = tcp->rates[j - 1] + 20;
361 if (!j && tcp->rates[j] < 30)
365 /* << Modification of the RATE */
366 /* tile->comps=(tcd_tilecomp_t*)realloc(tile->comps,img->numcomps*sizeof(tcd_tilecomp_t)); */
367 for (compno = 0; compno < tile->numcomps; compno++) {
368 j2k_tccp_t *tccp = &tcp->tccps[compno];
369 /* int realloc_op; */
371 tilec = &tile->comps[compno];
372 /* border of each tile component (global) */
373 tilec->x0 = int_ceildiv(tile->x0, img->comps[compno].dx);
374 tilec->y0 = int_ceildiv(tile->y0, img->comps[compno].dy);
375 tilec->x1 = int_ceildiv(tile->x1, img->comps[compno].dx);
376 tilec->y1 = int_ceildiv(tile->y1, img->comps[compno].dy);
378 tilec->data = (int *) malloc((tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0) * sizeof(int));
379 tilec->numresolutions = tccp->numresolutions;
380 /* tilec->resolutions=(tcd_resolution_t*)realloc(tilec->resolutions,tilec->numresolutions*sizeof(tcd_resolution_t)); */
381 for (resno = 0; resno < tilec->numresolutions; resno++) {
383 int levelno = tilec->numresolutions - 1 - resno;
384 int tlprcxstart, tlprcystart, brprcxend, brprcyend;
385 int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend;
386 int cbgwidthexpn, cbgheightexpn;
387 int cblkwidthexpn, cblkheightexpn;
389 res = &tilec->resolutions[resno];
390 /* border for each resolution level (global) */
391 res->x0 = int_ceildivpow2(tilec->x0, levelno);
392 res->y0 = int_ceildivpow2(tilec->y0, levelno);
393 res->x1 = int_ceildivpow2(tilec->x1, levelno);
394 res->y1 = int_ceildivpow2(tilec->y1, levelno);
396 res->numbands = resno == 0 ? 1 : 3;
397 /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */
398 if (tccp->csty & J2K_CCP_CSTY_PRT) {
399 pdx = tccp->prcw[resno];
400 pdy = tccp->prch[resno];
405 /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
406 tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx;
407 tlprcystart = int_floordivpow2(res->y0, pdy) << pdy;
408 brprcxend = int_ceildivpow2(res->x1, pdx) << pdx;
409 brprcyend = int_ceildivpow2(res->y1, pdy) << pdy;
411 res->pw = (brprcxend - tlprcxstart) >> pdx;
412 res->ph = (brprcyend - tlprcystart) >> pdy;
415 tlcbgxstart = tlprcxstart;
416 tlcbgystart = tlprcystart;
417 brcbgxend = brprcxend;
418 brcbgyend = brprcyend;
422 tlcbgxstart = int_ceildivpow2(tlprcxstart, 1);
423 tlcbgystart = int_ceildivpow2(tlprcystart, 1);
424 brcbgxend = int_ceildivpow2(brprcxend, 1);
425 brcbgyend = int_ceildivpow2(brprcyend, 1);
426 cbgwidthexpn = pdx - 1;
427 cbgheightexpn = pdy - 1;
430 cblkwidthexpn = int_min(tccp->cblkw, cbgwidthexpn);
431 cblkheightexpn = int_min(tccp->cblkh, cbgheightexpn);
433 for (bandno = 0; bandno < res->numbands; bandno++) {
437 band = &res->bands[bandno];
438 band->bandno = resno == 0 ? 0 : bandno + 1;
439 x0b = (band->bandno == 1) || (band->bandno == 3) ? 1 : 0;
440 y0b = (band->bandno == 2) || (band->bandno == 3) ? 1 : 0;
442 if (band->bandno == 0) {
444 band->x0 = int_ceildivpow2(tilec->x0, levelno);
445 band->y0 = int_ceildivpow2(tilec->y0, levelno);
446 band->x1 = int_ceildivpow2(tilec->x1, levelno);
447 band->y1 = int_ceildivpow2(tilec->y1, levelno);
449 band->x0 = int_ceildivpow2(tilec->x0 - (1 << levelno) * x0b, levelno + 1);
450 band->y0 = int_ceildivpow2(tilec->y0 - (1 << levelno) * y0b, levelno + 1);
451 band->x1 = int_ceildivpow2(tilec->x1 - (1 << levelno) * x0b, levelno + 1);
452 band->y1 = int_ceildivpow2(tilec->y1 - (1 << levelno) * y0b, levelno + 1);
455 ss = &tccp->stepsizes[resno == 0 ? 0 : 3 * (resno - 1) + bandno + 1];
456 gain = tccp->qmfbid == 0 ? dwt_getgain_real(band->bandno) : dwt_getgain(band->bandno);
457 numbps = img->comps[compno].prec + gain;
458 band->stepsize = (int) floor((1.0 + ss->mant / 2048.0) * pow(2.0, numbps - ss->expn) * 8192.0);
459 band->numbps = ss->expn + tccp->numgbits - 1; /* WHY -1 ? */
461 for (precno = 0; precno < res->pw * res->ph; precno++) {
462 int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
464 tlcbgxstart + (precno % res->pw) * (1 << cbgwidthexpn);
466 tlcbgystart + (precno / res->pw) * (1 << cbgheightexpn);
467 int cbgxend = cbgxstart + (1 << cbgwidthexpn);
468 int cbgyend = cbgystart + (1 << cbgheightexpn);
470 prc = &band->precincts[precno];
471 /* precinct size (global) */
472 prc->x0 = int_max(cbgxstart, band->x0);
473 prc->y0 = int_max(cbgystart, band->y0);
474 prc->x1 = int_min(cbgxend, band->x1);
475 prc->y1 = int_min(cbgyend, band->y1);
477 tlcblkxstart = int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn;
478 tlcblkystart = int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn;
479 brcblkxend = int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn;
480 brcblkyend = int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn;
481 prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;
482 prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn;
485 prc->cblks = (tcd_cblk_t *) malloc(prc->cw * prc->ch * sizeof(tcd_cblk_t));
487 if (prc->incltree != NULL)
488 tgt_destroy(prc->incltree);
489 if (prc->imsbtree != NULL)
490 tgt_destroy(prc->imsbtree);
492 prc->incltree = tgt_create(prc->cw, prc->ch);
493 prc->imsbtree = tgt_create(prc->cw, prc->ch);
495 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
496 int cblkxstart = tlcblkxstart + (cblkno % prc->cw) * (1 << cblkwidthexpn);
497 int cblkystart = tlcblkystart + (cblkno / prc->cw) * (1 << cblkheightexpn);
498 int cblkxend = cblkxstart + (1 << cblkwidthexpn);
499 int cblkyend = cblkystart + (1 << cblkheightexpn);
500 cblk = &prc->cblks[cblkno];
502 /* code-block size (global) */
503 cblk->x0 = int_max(cblkxstart, prc->x0);
504 cblk->y0 = int_max(cblkystart, prc->y0);
505 cblk->x1 = int_min(cblkxend, prc->x1);
506 cblk->y1 = int_min(cblkyend, prc->y1);
514 /* tcd_dump(&tcd_image,0); */
517 void tcd_init(j2k_image_t * img, j2k_cp_t * cp)
519 int tileno, compno, resno, bandno, precno, cblkno, i;
520 unsigned int x0=0 , y0=0, x1 = 0, y1 = 0, w, h, j, p, q;
523 tcd_image.tw = cp->tw;
524 tcd_image.th = cp->th;
525 tcd_image.tiles = (tcd_tile_t *) malloc(cp->tw * cp->th * sizeof(tcd_tile_t));
527 /*for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
528 j2k_tcp_t *tcp = &cp->tcps[tileno];
529 tcd_tile_t *tile = &tcd_image.tiles[tileno];*/
531 for (i = 0 ; i < cp->tileno_size ; i++){
532 j2k_tcp_t *tcp = &cp->tcps[cp->tileno[i]];
533 tcd_tile_t *tile = &tcd_image.tiles[cp->tileno[i]];
534 tileno = cp->tileno[i];
537 // int previous_x0, previous_x1, previous_y0, previous_y1;
538 /* cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
539 p = tileno % cp->tw; /* si numerotation matricielle .. */
540 q = tileno / cp->tw; /* .. coordonnees de la tile (q,p) q pour ligne et p pour colonne */
542 /* 4 borders of the tile rescale on the image if necessary */
543 tile->x0 = int_max(cp->tx0 + p * cp->tdx, img->x0);
544 tile->y0 = int_max(cp->ty0 + q * cp->tdy, img->y0);
545 tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, img->x1);
546 tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, img->y1);
548 tile->numcomps = img->numcomps;
549 tile->comps = (tcd_tilecomp_t *) malloc(img->numcomps * sizeof(tcd_tilecomp_t));
550 for (compno = 0; compno < tile->numcomps; compno++) {
551 j2k_tccp_t *tccp = &tcp->tccps[compno];
552 tcd_tilecomp_t *tilec = &tile->comps[compno];
553 /* border of each tile component (global) */
554 tilec->x0 = int_ceildiv(tile->x0, img->comps[compno].dx);
555 tilec->y0 = int_ceildiv(tile->y0, img->comps[compno].dy);
556 tilec->x1 = int_ceildiv(tile->x1, img->comps[compno].dx);
557 tilec->y1 = int_ceildiv(tile->y1, img->comps[compno].dy);
559 tilec->data = (int *) malloc((tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0) * sizeof(int));
560 tilec->numresolutions = tccp->numresolutions;
561 tilec->resolutions = (tcd_resolution_t *) malloc(tilec->numresolutions * sizeof(tcd_resolution_t));
562 for (resno = 0; resno < tilec->numresolutions; resno++) {
564 int levelno = tilec->numresolutions - 1 - resno;
565 int tlprcxstart, tlprcystart, brprcxend, brprcyend;
566 int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend;
567 int cbgwidthexpn, cbgheightexpn;
568 int cblkwidthexpn, cblkheightexpn;
569 tcd_resolution_t *res = &tilec->resolutions[resno];
571 /* border for each resolution level (global) */
572 res->x0 = int_ceildivpow2(tilec->x0, levelno);
573 res->y0 = int_ceildivpow2(tilec->y0, levelno);
574 res->x1 = int_ceildivpow2(tilec->x1, levelno);
575 res->y1 = int_ceildivpow2(tilec->y1, levelno);
577 res->numbands = resno == 0 ? 1 : 3;
578 /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */
579 if (tccp->csty & J2K_CCP_CSTY_PRT) {
580 pdx = tccp->prcw[resno];
581 pdy = tccp->prch[resno];
586 /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
587 tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx;
588 tlprcystart = int_floordivpow2(res->y0, pdy) << pdy;
589 brprcxend = int_ceildivpow2(res->x1, pdx) << pdx;
590 brprcyend = int_ceildivpow2(res->y1, pdy) << pdy;
591 res->pw = (brprcxend - tlprcxstart) >> pdx;
592 res->ph = (brprcyend - tlprcystart) >> pdy;
595 tlcbgxstart = tlprcxstart;
596 tlcbgystart = tlprcystart;
597 brcbgxend = brprcxend;
598 brcbgyend = brprcyend;
602 tlcbgxstart = int_ceildivpow2(tlprcxstart, 1);
603 tlcbgystart = int_ceildivpow2(tlprcystart, 1);
604 brcbgxend = int_ceildivpow2(brprcxend, 1);
605 brcbgyend = int_ceildivpow2(brprcyend, 1);
606 cbgwidthexpn = pdx - 1;
607 cbgheightexpn = pdy - 1;
610 cblkwidthexpn = int_min(tccp->cblkw, cbgwidthexpn);
611 cblkheightexpn = int_min(tccp->cblkh, cbgheightexpn);
613 for (bandno = 0; bandno < res->numbands; bandno++) {
617 tcd_band_t *band = &res->bands[bandno];
618 band->bandno = resno == 0 ? 0 : bandno + 1;
619 x0b = (band->bandno == 1) || (band->bandno == 3) ? 1 : 0;
620 y0b = (band->bandno == 2) || (band->bandno == 3) ? 1 : 0;
622 if (band->bandno == 0) {
623 /* band border (global) */
624 band->x0 = int_ceildivpow2(tilec->x0, levelno);
625 band->y0 = int_ceildivpow2(tilec->y0, levelno);
626 band->x1 = int_ceildivpow2(tilec->x1, levelno);
627 band->y1 = int_ceildivpow2(tilec->y1, levelno);
629 /* band border (global) */
630 band->x0 = int_ceildivpow2(tilec->x0 - (1 << levelno) * x0b, levelno + 1);
631 band->y0 = int_ceildivpow2(tilec->y0 - (1 << levelno) * y0b, levelno + 1);
632 band->x1 = int_ceildivpow2(tilec->x1 - (1 << levelno) * x0b, levelno + 1);
633 band->y1 = int_ceildivpow2(tilec->y1 - (1 << levelno) * y0b, levelno + 1);
636 ss = &tccp->stepsizes[resno == 0 ? 0 : 3 * (resno - 1) + bandno + 1];
637 gain = tccp->qmfbid == 0 ? dwt_getgain_real(band->bandno) : dwt_getgain(band->bandno);
638 numbps = img->comps[compno].prec + gain;
639 band->stepsize = (int) floor((1.0 + ss->mant / 2048.0) * pow(2.0, numbps - ss->expn) * 8192.0);
640 band->numbps = ss->expn + tccp->numgbits - 1; /* WHY -1 ? */
642 band->precincts = (tcd_precinct_t *) malloc(res->pw * res->ph * sizeof(tcd_precinct_t));
644 for (precno = 0; precno < res->pw * res->ph; precno++) {
645 int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
646 int cbgxstart = tlcbgxstart + (precno % res->pw) * (1 << cbgwidthexpn);
647 int cbgystart = tlcbgystart + (precno / res->pw) * (1 << cbgheightexpn);
648 int cbgxend = cbgxstart + (1 << cbgwidthexpn);
649 int cbgyend = cbgystart + (1 << cbgheightexpn);
650 tcd_precinct_t *prc = &band->precincts[precno];
651 /* precinct size (global) */
652 prc->x0 = int_max(cbgxstart, band->x0);
653 prc->y0 = int_max(cbgystart, band->y0);
654 prc->x1 = int_min(cbgxend, band->x1);
655 prc->y1 = int_min(cbgyend, band->y1);
657 tlcblkxstart = int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn;
658 tlcblkystart = int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn;
659 brcblkxend = int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn;
660 brcblkyend = int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn;
661 prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;
662 prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn;
664 prc->cblks = (tcd_cblk_t *) malloc(prc->cw * prc->ch * sizeof(tcd_cblk_t));
666 prc->incltree = tgt_create(prc->cw, prc->ch);
667 prc->imsbtree = tgt_create(prc->cw, prc->ch);
669 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
670 int cblkxstart = tlcblkxstart + (cblkno % prc->cw) * (1 << cblkwidthexpn);
671 int cblkystart = tlcblkystart + (cblkno / prc->cw) * (1 << cblkheightexpn);
672 int cblkxend = cblkxstart + (1 << cblkwidthexpn);
673 int cblkyend = cblkystart + (1 << cblkheightexpn);
674 tcd_cblk_t *cblk = &prc->cblks[cblkno];
675 /* code-block size (global) */
676 cblk->x0 = int_max(cblkxstart, prc->x0);
677 cblk->y0 = int_max(cblkystart, prc->y0);
678 cblk->x1 = int_min(cblkxend, prc->x1);
679 cblk->y1 = int_min(cblkyend, prc->y1);
686 /* tcd_dump(&tcd_image,0); */
689 /* Allocate place to store the date decoded = fianl image */
690 /* Place limited by the tile really present in the codestream */
691 for (i = 0; i < img->numcomps; i++) {
692 for (j = 0; j < cp->tileno_size; j++) {
693 tileno = cp->tileno[j];
694 x0 = j == 0 ? tcd_image.tiles[tileno].x0 : int_min(x0 , tcd_image.tiles[tileno].x0);
695 y0 = j == 0 ? tcd_image.tiles[tileno].y0 : int_min(y0 , tcd_image.tiles[tileno].y0);
696 x1 = j == 0 ? tcd_image.tiles[tileno].x1 : int_max(x1 , tcd_image.tiles[tileno].x1);
697 y1 = j == 0 ? tcd_image.tiles[tileno].y1 : int_max(y1 , tcd_image.tiles[tileno].y1);
699 w = int_ceildiv(x1 - x0, img->comps[i].dx);
700 h = int_ceildiv(y1 - y0, img->comps[i].dy);
701 img->comps[i].data = (int *) calloc(w * h,sizeof(int));
704 img->comps[i].x0 = x0;
705 img->comps[i].y0 = y0;
710 void tcd_makelayer_fixed(int layno, int final) {
711 int compno, resno, bandno, precno, cblkno;
712 int value; //, matrice[tcd_tcp->numlayers][tcd_tile->comps[0].numresolutions][3];
713 int matrice[10][10][3];
716 /*matrice=(int*)malloc(tcd_tcp->numlayers*tcd_tile->comps[0].numresolutions*3*sizeof(int));*/
718 for (compno=0; compno<tcd_tile->numcomps; compno++)
720 tcd_tilecomp_t *tilec=&tcd_tile->comps[compno];
721 for (i=0;i<tcd_tcp->numlayers;i++) {
722 for (j=0;j<tilec->numresolutions;j++) {
724 matrice[i][j][k]=(int)(tcd_cp->matrice[i*tilec->numresolutions*3+j*3+k]*(float)(tcd_img->comps[compno].prec/16.0));}}}
726 for (resno=0; resno<tilec->numresolutions; resno++) {
727 tcd_resolution_t *res=&tilec->resolutions[resno];
728 for (bandno=0; bandno<res->numbands; bandno++) {
729 tcd_band_t *band=&res->bands[bandno];
730 for (precno=0; precno<res->pw*res->ph; precno++) {
731 tcd_precinct_t *prc=&band->precincts[precno];
732 for (cblkno=0; cblkno<prc->cw*prc->ch; cblkno++) {
733 tcd_cblk_t *cblk=&prc->cblks[cblkno];
734 tcd_layer_t *layer=&cblk->layers[layno];
736 int imsb=tcd_img->comps[compno].prec-cblk->numbps; /* number of bit-plan equal to zero */
737 /* Correction of the matrix of coefficient to include the IMSB information */
741 value=matrice[layno][resno][bandno];
748 value=matrice[layno][resno][bandno]-matrice[layno-1][resno][bandno];
749 if (imsb>=matrice[layno-1][resno][bandno])
751 value-=(imsb-matrice[layno-1][resno][bandno]);
752 if (value<0) value=0;
757 cblk->numpassesinlayers=0;
759 n=cblk->numpassesinlayers;
760 if (cblk->numpassesinlayers==0)
763 n=3*value-2+cblk->numpassesinlayers;
765 n=cblk->numpassesinlayers;
767 n=3*value+cblk->numpassesinlayers;
769 layer->numpasses=n-cblk->numpassesinlayers;
771 if (!layer->numpasses)
774 if (cblk->numpassesinlayers==0)
776 layer->len=cblk->passes[n-1].rate;
777 layer->data=cblk->data;
780 layer->len=cblk->passes[n-1].rate-cblk->passes[cblk->numpassesinlayers-1].rate;
781 layer->data=cblk->data+cblk->passes[cblk->numpassesinlayers-1].rate;
784 cblk->numpassesinlayers=n;
792 void tcd_rateallocate_fixed() {
795 for (layno=0; layno<tcd_tcp->numlayers; layno++)
797 tcd_makelayer_fixed(layno, 1);
801 void tcd_makelayer(int layno, double thresh, int final)
803 int compno, resno, bandno, precno, cblkno, passno;
804 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
805 tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
806 for (resno = 0; resno < tilec->numresolutions; resno++) {
807 tcd_resolution_t *res = &tilec->resolutions[resno];
808 for (bandno = 0; bandno < res->numbands; bandno++) {
809 tcd_band_t *band = &res->bands[bandno];
810 for (precno = 0; precno < res->pw * res->ph; precno++) {
811 tcd_precinct_t *prc = &band->precincts[precno];
812 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
813 tcd_cblk_t *cblk = &prc->cblks[cblkno];
814 tcd_layer_t *layer = &cblk->layers[layno];
818 cblk->numpassesinlayers = 0;
820 n = cblk->numpassesinlayers;
821 for (passno = cblk->numpassesinlayers;
822 passno < cblk->totalpasses; passno++) {
825 tcd_pass_t *pass = &cblk->passes[passno];
828 dd = pass->distortiondec;
830 dr = pass->rate - cblk->passes[n - 1].rate;
831 dd = pass->distortiondec - cblk->passes[n - 1].distortiondec;
838 if (dd / dr > thresh)
841 layer->numpasses = n - cblk->numpassesinlayers;
842 if (!layer->numpasses)
844 if (cblk->numpassesinlayers == 0) {
845 layer->len = cblk->passes[n - 1].rate;
846 layer->data = cblk->data;
847 layer->disto = cblk->passes[n - 1].distortiondec;
849 layer->len = cblk->passes[n - 1].rate -
850 cblk->passes[cblk->numpassesinlayers - 1].rate;
851 layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate;
852 layer->disto = cblk->passes[n - 1].distortiondec -
853 cblk->passes[cblk->numpassesinlayers - 1].distortiondec;
857 cblk->numpassesinlayers = n;
865 void tcd_rateallocate(unsigned char *dest, int len, info_image * info_IM)
867 int compno, resno, bandno, precno, cblkno, passno, layno;
872 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
873 tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
874 for (resno = 0; resno < tilec->numresolutions; resno++) {
875 tcd_resolution_t *res = &tilec->resolutions[resno];
876 for (bandno = 0; bandno < res->numbands; bandno++) {
877 tcd_band_t *band = &res->bands[bandno];
878 for (precno = 0; precno < res->pw * res->ph; precno++) {
879 tcd_precinct_t *prc = &band->precincts[precno];
880 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
881 tcd_cblk_t *cblk = &prc->cblks[cblkno];
882 for (passno = 0; passno < cblk->totalpasses; passno++) {
883 tcd_pass_t *pass = &cblk->passes[passno];
888 dd = pass->distortiondec;
890 dr = pass->rate - cblk->passes[passno - 1].rate;
891 dd = pass->distortiondec - cblk->passes[passno - 1].distortiondec;
909 if (info_IM->index_on) { /* Threshold for Marcela Index */
910 info_IM->tile[tcd_tileno].thresh=(double*)malloc(tcd_tcp->numlayers*sizeof(double));
912 for (layno = 0; layno < tcd_tcp->numlayers; layno++) {
913 volatile double lo = min;
914 volatile double hi = max;
915 volatile int success = 0;
916 volatile int maxlen = int_min(tcd_tcp->rates[layno], len);
917 volatile double goodthresh;
918 volatile int goodlen;
921 for (i = 0; i < 32; i++) {
922 volatile double thresh = (lo + hi) / 2;
925 tcd_makelayer(layno, thresh, 0);
927 l = t2_encode_packets(tcd_img, tcd_cp, tcd_tileno, tcd_tile, layno + 1, dest, maxlen, info_IM);
928 /* fprintf(stderr, "rate alloc: len=%d, max=%d\n", l, maxlen); */
941 longjmp(j2k_error, 1);
944 if (info_IM->index_on) { /* Threshold for Marcela Index */
945 info_IM->tile[tcd_tileno].thresh[layno]=goodthresh;
947 tcd_makelayer(layno, goodthresh, 1);
951 int tcd_encode_tile_pxm(int tileno, unsigned char *dest, int len, info_image * info_IM)
957 j2k_tcp_t *tcp = &tcd_cp->tcps[0];
958 j2k_tccp_t *tccp = &tcp->tccps[0];
961 tcd_tile = tcd_image.tiles;
962 tcd_tcp = &tcd_cp->tcps[tileno];
964 /* INDEX >> "Precinct_nb_X et Precinct_nb_Y" */
965 if (info_IM->index_on) {
966 tcd_tilecomp_t *tilec_idx = &tile->comps[0]; /* old parser version */
967 tcd_resolution_t *res_idx = &tilec_idx->resolutions[0]; /* old parser version */
969 info_IM->tile[tileno].pw = res_idx->pw;
970 info_IM->tile[tileno].ph = res_idx->ph;
972 info_IM->pw = res_idx->pw; /* old parser version */
973 info_IM->ph = res_idx->ph; /* old parser version */
974 info_IM->pdx = 1 << tccp->prcw[tccp->numresolutions - 1];
975 info_IM->pdy = 1 << tccp->prch[tccp->numresolutions - 1];
979 /*---------------TILE-------------------*/
983 for (compno = 0; compno < tile->numcomps; compno++) {
990 tcd_tilecomp_t *tilec = &tile->comps[compno];
991 int adjust = tcd_img->comps[compno].sgnd ? 0 : 1 << (tcd_img->comps[compno].prec - 1);
992 int offset_x, offset_y;
994 offset_x = int_ceildiv(tcd_img->x0, tcd_img->comps[compno].dx);
995 offset_y = int_ceildiv(tcd_img->y0, tcd_img->comps[compno].dy);
996 tw = tilec->x1 - tilec->x0;
997 w = int_ceildiv(tcd_img->x1 - tcd_img->x0, tcd_img->comps[compno].dx);
998 sprintf(tmp, "Compo%d", compno); /* component file */
999 src = fopen(tmp, "rb");
1001 fprintf(stderr, "failed to open %s for reading\n", tmp);
1005 /* read the Compo file to extract data of the tile */
1007 fseek(src, (tilec->x0 - offset_x) + (tilec->y0 - offset_y) * w, SEEK_SET);
1008 k = (tilec->x0 - offset_x) + (tilec->y0 - offset_y) * w;
1009 for (j = tilec->y0; j < tilec->y1; j++) {
1010 for (i = tilec->x0; i < tilec->x1; i++) {
1011 if (tcd_tcp->tccps[compno].qmfbid == 1) {
1013 tilec->data[i - tilec->x0 + (j - tilec->y0) * tw] = elmt - adjust;
1015 } else if (tcd_tcp->tccps[compno].qmfbid == 0) {
1017 tilec->data[i - tilec->x0 + (j - tilec->y0) * tw] = (elmt - adjust) << 13;
1021 fseek(src, (tilec->x0 - offset_x) + (j + 1 - offset_y) * w - k, SEEK_CUR);
1022 k = tilec->x0 - offset_x + (j + 1 - offset_y) * w;
1028 /*----------------MCT-------------------*/
1031 if (tcd_tcp->tccps[0].qmfbid == 0) {
1032 mct_encode_real(tile->comps[0].data, tile->comps[1].data,tile->comps[2].data,
1033 (tile->comps[0].x1 - tile->comps[0].x0) * (tile->comps[0].y1 - tile->comps[0].y0));
1035 mct_encode(tile->comps[0].data, tile->comps[1].data, tile->comps[2].data,
1036 (tile->comps[0].x1 - tile->comps[0].x0) * (tile->comps[0].y1 - tile->comps[0].y0));
1039 /*----------------DWT---------------------*/
1041 /* time3=clock(); */
1042 for (compno = 0; compno < tile->numcomps; compno++) {
1043 tcd_tilecomp_t *tilec = &tile->comps[compno];
1044 if (tcd_tcp->tccps[compno].qmfbid == 1) {
1045 dwt_encode(tilec->data, tilec->x1 - tilec->x0, tilec->y1 - tilec->y0, tilec, tilec->numresolutions - 1);
1046 } else if (tcd_tcp->tccps[compno].qmfbid == 0) {
1047 dwt_encode_real(tilec->data, tilec->x1 - tilec->x0, tilec->y1 - tilec->y0, tilec, tilec->numresolutions - 1);
1050 /*------------------TIER1-----------------*/
1053 t1_encode_cblks(tile, tcd_tcp);
1055 /*-----------RATE-ALLOCATE------------------*/
1056 info_IM->index_write = 0; /* INDEX */
1058 if (tcd_cp->disto_alloc)
1059 /* Normal Rate/distortion allocation */
1060 tcd_rateallocate(dest, len, info_IM);
1062 /* Fixed layer allocation */
1063 tcd_rateallocate_fixed();
1065 /*--------------TIER2------------------*/
1066 info_IM->index_write = 1; /* INDEX */
1067 l = t2_encode_packets(tcd_img, tcd_cp, tileno, tile, tcd_tcp->numlayers, dest, len, info_IM);
1068 /*---------------CLEAN-------------------*/
1070 time7 = clock() - time7;
1071 printf("total: %ld.%.3ld s\n", time7 / CLOCKS_PER_SEC, (time7 % CLOCKS_PER_SEC) * 1000 / CLOCKS_PER_SEC);
1073 /* cleaning memory */
1074 for (compno = 0; compno < tile->numcomps; compno++) {
1075 tilec = &tile->comps[compno];
1082 int tcd_encode_tile_pgx(int tileno, unsigned char *dest, int len, info_image * info_IM)
1088 j2k_tcp_t *tcp = &tcd_cp->tcps[0];
1089 j2k_tccp_t *tccp = &tcp->tccps[0];
1091 tcd_tileno = tileno;
1092 tcd_tile = tcd_image.tiles;
1093 tcd_tcp = &tcd_cp->tcps[tileno];
1095 /* INDEX >> "Precinct_nb_X et Precinct_nb_Y" */
1096 if (info_IM->index_on) {
1097 tcd_tilecomp_t *tilec_idx = &tile->comps[0];
1098 tcd_resolution_t *res_idx = &tilec_idx->resolutions[0];
1099 info_IM->tile[tileno].pw = res_idx->pw;
1100 info_IM->tile[tileno].ph = res_idx->ph;
1101 info_IM->pw = res_idx->pw; /* old parser version */
1102 info_IM->ph = res_idx->ph; /* old parser version */
1103 info_IM->pdx = 1 << tccp->prcw[tccp->numresolutions - 1];
1104 info_IM->pdy = 1 << tccp->prch[tccp->numresolutions - 1];
1107 /*---------------TILE-------------------*/
1110 for (compno = 0; compno < tile->numcomps; compno++) {
1117 tcd_tilecomp_t *tilec = &tile->comps[compno];
1118 int adjust = tcd_img->comps[compno].sgnd ? 0 : 1 << (tcd_img->comps[compno].prec - 1);
1119 int offset_x, offset_y;
1121 offset_x = int_ceildiv(tcd_img->x0, tcd_img->comps[compno].dx);
1122 offset_y = int_ceildiv(tcd_img->y0, tcd_img->comps[compno].dy);
1123 tw = tilec->x1 - tilec->x0;
1124 w = int_ceildiv(tcd_img->x1 - tcd_img->x0, tcd_img->comps[compno].dx);
1125 sprintf(tmp, "bandtile%d", tileno / tcd_cp->tw + 1); /* bandtile file opening */
1126 src = fopen(tmp, "rb");
1128 fprintf(stderr, "failed to open %s for reading\n", tmp);
1131 /* Extract data from bandtile file limited to the current tile */
1133 while (k < tilec->x0 - offset_x) {
1135 fscanf(src, "%d", &elmt);
1138 for (j = 0; j < tilec->y1 - tilec->y0; j++) {
1139 for (i = tilec->x0; i < tilec->x1; i++) {
1140 if (tcd_tcp->tccps[compno].qmfbid == 1) {
1141 fscanf(src, "%d", &elmt);
1142 tilec->data[i - tilec->x0 + (j) * tw] = elmt - adjust;
1144 } else if (tcd_tcp->tccps[compno].qmfbid == 0) {
1145 fscanf(src, "%d", &elmt);
1146 tilec->data[i - tilec->x0 + (j) * tw] = (elmt - adjust) << 13;
1150 while (k < tilec->x0 - offset_x + (j + 1) * w) {
1152 fscanf(src, "%d", &elmt);
1158 /*----------------MCT-------------------*/
1161 if (tcd_tcp->tccps[0].qmfbid == 0) {
1162 mct_encode_real(tile->comps[0].data, tile->comps[1].data, tile->comps[2].data,
1163 (tile->comps[0].x1 - tile->comps[0].x0) * (tile->comps[0].y1 - tile->comps[0].y0));
1165 mct_encode(tile->comps[0].data, tile->comps[1].data, tile->comps[2].data,
1166 (tile->comps[0].x1 - tile->comps[0].x0) * (tile->comps[0].y1 - tile->comps[0].y0));
1170 /*----------------DWT---------------------*/
1172 for (compno = 0; compno < tile->numcomps; compno++) {
1173 tcd_tilecomp_t *tilec = &tile->comps[compno];
1174 if (tcd_tcp->tccps[compno].qmfbid == 1) {
1175 dwt_encode(tilec->data, tilec->x1 - tilec->x0, tilec->y1 - tilec->y0, tilec, tilec->numresolutions - 1);
1176 } else if (tcd_tcp->tccps[compno].qmfbid == 0) {
1177 dwt_encode_real(tilec->data, tilec->x1 - tilec->x0, tilec->y1 - tilec->y0, tilec, tilec->numresolutions - 1);
1181 /*------------------TIER1-----------------*/
1184 t1_encode_cblks(tile, tcd_tcp);
1186 /*-----------RATE-ALLOCATE------------------*/
1187 info_IM->index_write = 0; /* INDEX */
1189 info_IM->index_write=0; /* INDEX */
1191 if (tcd_cp->disto_alloc)
1192 /* Normal Rate/distortion allocation */
1193 tcd_rateallocate(dest, len, info_IM);
1195 /* Fixed layer allocation */
1196 tcd_rateallocate_fixed();
1198 /*--------------TIER2------------------*/
1199 info_IM->index_write = 1; /* INDEX */
1201 l = t2_encode_packets(tcd_img, tcd_cp, tileno, tile, tcd_tcp->numlayers, dest, len, info_IM);
1203 /*---------------CLEAN-------------------*/
1204 time = clock() - time;
1205 printf("total: %ld.%.3ld s\n", time / CLOCKS_PER_SEC, (time % CLOCKS_PER_SEC) * 1000 / CLOCKS_PER_SEC);
1207 for (compno = 0; compno < tile->numcomps; compno++) {
1208 tilec = &tile->comps[compno];
1216 int tcd_decode_tile(unsigned char *src, int len, int tileno)
1224 tcd_tileno = tileno;
1225 tcd_tile = &tcd_image.tiles[tileno];
1226 tcd_tcp = &tcd_cp->tcps[tileno];
1231 fprintf(stderr,"tile decoding time %d/%d: ", tileno + 1, tcd_cp->tw * tcd_cp->th);
1233 /*--------------TIER2------------------*/
1235 l = t2_decode_packets(src, len, tcd_img, tcd_cp, tileno, tile);
1239 fprintf(stderr, "tcd_decode: incomplete bistream\n");
1242 /*------------------TIER1-----------------*/
1244 t1_decode_cblks(tile, tcd_tcp);
1246 /*----------------DWT---------------------*/
1248 for (compno = 0; compno < tile->numcomps; compno++)
1250 tcd_tilecomp_t *tilec = &tile->comps[compno];
1251 if (tcd_cp->reduce_on == 1)
1253 tcd_img->comps[compno].resno_decoded = tile->comps[compno].numresolutions - tcd_cp->reduce_value - 1;
1257 if (tcd_tcp->tccps[compno].qmfbid == 1)
1259 dwt_decode(tilec->data, tilec->x1 - tilec->x0, tilec->y1 - tilec->y0, tilec, tilec->numresolutions - 1,
1260 tilec->numresolutions - 1 - tcd_img->comps[compno].resno_decoded);
1262 dwt_decode_real(tilec->data, tilec->x1 - tilec->x0, tilec->y1 - tilec->y0, tilec, tilec->numresolutions - 1,
1263 tilec->numresolutions - 1 - tcd_img->comps[compno].resno_decoded);
1266 if (tile->comps[compno].numresolutions > 0)
1267 tcd_img->comps[compno].factor = tile->comps[compno].numresolutions - (tcd_img->comps[compno].resno_decoded + 1);
1270 /*----------------MCT-------------------*/
1273 if (tcd_tcp->tccps[0].qmfbid == 1) {
1274 mct_decode(tile->comps[0].data, tile->comps[1].data, tile->comps[2].data,
1275 (tile->comps[0].x1 - tile->comps[0].x0) * (tile->comps[0].y1 - tile->comps[0].y0));
1277 mct_decode_real(tile->comps[0].data, tile->comps[1].data, tile->comps[2].data,
1278 (tile->comps[0].x1 - tile->comps[0].x0) * (tile->comps[0].y1 - tile->comps[0].y0));
1282 /*---------------TILE-------------------*/
1284 for (compno = 0; compno < tile->numcomps; compno++) {
1285 tcd_tilecomp_t *tilec = &tile->comps[compno];
1286 tcd_resolution_t *res = &tilec->resolutions[tcd_img->comps[compno].resno_decoded];
1287 int adjust = tcd_img->comps[compno].sgnd ? 0 : 1 << (tcd_img->comps[compno].prec - 1);
1288 int min = tcd_img->comps[compno].sgnd ? - (1 << (tcd_img->comps[compno].prec - 1)) : 0;
1289 int max = tcd_img->comps[compno].sgnd ? (1 << (tcd_img->comps[compno].prec - 1)) - 1 : (1 << tcd_img->comps[compno].prec) - 1;
1291 int tw = tilec->x1 - tilec->x0;
1292 int w = tcd_img->comps[compno].w;
1295 int offset_x = int_ceildivpow2(tcd_img->comps[compno].x0, tcd_img->comps[compno].factor);
1296 int offset_y = int_ceildivpow2(tcd_img->comps[compno].y0, tcd_img->comps[compno].factor);
1298 for (j = res->y0; j < res->y1; j++) {
1299 for (i = res->x0; i < res->x1; i++) {
1302 if (tcd_tcp->tccps[compno].qmfbid == 1) {
1303 v = tilec->data[i - res->x0 + (j - res->y0) * tw];
1305 v = tilec->data[i - res->x0 + (j - res->y0) * tw] >> 13;
1309 tcd_img->comps[compno].data[(i - offset_x) + (j - offset_y) * w] = int_clamp(v, min, max);
1314 time = clock() - time;
1315 fprintf(stderr,"total: %ld.%.3ld s\n", time / CLOCKS_PER_SEC, (time % CLOCKS_PER_SEC) * 1000 / CLOCKS_PER_SEC);
1318 longjmp(j2k_error, 1);