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++) {
133 int_ceildiv(tile->numcomps * (tile->x1 - tile->x0) *
135 tile->y0) * img->comps[0].prec, (tcp->rates[j] * 8 *
138 if (j && tcp->rates[j] < tcp->rates[j - 1] + 10) {
139 tcp->rates[j] = tcp->rates[j - 1] + 20;
141 if (!j && tcp->rates[j] < 30)
145 /* << Modification of the RATE */
148 (tcd_tilecomp_t *) malloc(img->numcomps * sizeof(tcd_tilecomp_t));
149 for (compno = 0; compno < tile->numcomps; compno++) {
150 j2k_tccp_t *tccp = &tcp->tccps[compno];
151 /* tcd_tilecomp_t *tilec=&tile->comps[compno]; */
152 tilec = &tile->comps[compno];
153 /* border of each tile component (global) */
154 tilec->x0 = int_ceildiv(tile->x0, img->comps[compno].dx);
156 tilec->y0 = int_ceildiv(tile->y0, img->comps[compno].dy);
157 tilec->x1 = int_ceildiv(tile->x1, img->comps[compno].dx);
158 tilec->y1 = int_ceildiv(tile->y1, img->comps[compno].dy);
161 (int *) malloc((tilec->x1 - tilec->x0) *
162 (tilec->y1 - tilec->y0) * sizeof(int));
163 tilec->numresolutions = tccp->numresolutions;
166 (tcd_resolution_t *) malloc(tilec->numresolutions *
167 sizeof(tcd_resolution_t));
169 for (resno = 0; resno < tilec->numresolutions; resno++) {
171 int levelno = tilec->numresolutions - 1 - resno;
172 int tlprcxstart, tlprcystart, brprcxend, brprcyend;
173 int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend;
174 int cbgwidthexpn, cbgheightexpn;
175 int cblkwidthexpn, cblkheightexpn;
176 /* tcd_resolution_t *res=&tilec->resolutions[resno]; */
178 res = &tilec->resolutions[resno];
180 /* border for each resolution level (global) */
181 res->x0 = int_ceildivpow2(tilec->x0, levelno);
182 res->y0 = int_ceildivpow2(tilec->y0, levelno);
183 res->x1 = int_ceildivpow2(tilec->x1, levelno);
184 res->y1 = int_ceildivpow2(tilec->y1, levelno);
186 res->numbands = resno == 0 ? 1 : 3;
187 /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */
188 if (tccp->csty & J2K_CCP_CSTY_PRT) {
189 pdx = tccp->prcw[resno];
190 pdy = tccp->prch[resno];
195 /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
196 tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx;
197 tlprcystart = int_floordivpow2(res->y0, pdy) << pdy;
198 brprcxend = int_ceildivpow2(res->x1, pdx) << pdx;
199 brprcyend = int_ceildivpow2(res->y1, pdy) << pdy;
201 res->pw = (brprcxend - tlprcxstart) >> pdx;
202 res->ph = (brprcyend - tlprcystart) >> pdy;
205 tlcbgxstart = tlprcxstart;
206 tlcbgystart = tlprcystart;
207 brcbgxend = brprcxend;
208 brcbgyend = brprcyend;
212 tlcbgxstart = int_ceildivpow2(tlprcxstart, 1);
213 tlcbgystart = int_ceildivpow2(tlprcystart, 1);
214 brcbgxend = int_ceildivpow2(brprcxend, 1);
215 brcbgyend = int_ceildivpow2(brprcyend, 1);
216 cbgwidthexpn = pdx - 1;
217 cbgheightexpn = pdy - 1;
220 cblkwidthexpn = int_min(tccp->cblkw, cbgwidthexpn);
221 cblkheightexpn = int_min(tccp->cblkh, cbgheightexpn);
223 for (bandno = 0; bandno < res->numbands; bandno++) {
227 band = &res->bands[bandno];
228 band->bandno = resno == 0 ? 0 : bandno + 1;
229 x0b = (band->bandno == 1)
230 || (band->bandno == 3) ? 1 : 0;
231 y0b = (band->bandno == 2)
232 || (band->bandno == 3) ? 1 : 0;
234 if (band->bandno == 0) {
235 /* band border (global) */
236 band->x0 = int_ceildivpow2(tilec->x0, levelno);
237 band->y0 = int_ceildivpow2(tilec->y0, levelno);
238 band->x1 = int_ceildivpow2(tilec->x1, levelno);
239 band->y1 = int_ceildivpow2(tilec->y1, levelno);
241 /* band border (global) */
243 int_ceildivpow2(tilec->x0 -
244 (1 << levelno) * x0b, levelno + 1);
246 int_ceildivpow2(tilec->y0 -
247 (1 << levelno) * y0b, levelno + 1);
249 int_ceildivpow2(tilec->x1 -
250 (1 << levelno) * x0b, levelno + 1);
252 int_ceildivpow2(tilec->y1 -
253 (1 << levelno) * y0b, levelno + 1);
257 ss = &tccp->stepsizes[resno ==
258 0 ? 0 : 3 * (resno - 1) + bandno + 1];
261 0 ? dwt_getgain_real(band->bandno) : dwt_getgain(band->bandno);
262 numbps = img->comps[compno].prec + gain;
264 (int) floor((1.0 + ss->mant / 2048.0) *
265 pow(2.0, numbps - ss->expn) * 8192.0);
266 band->numbps = ss->expn + tccp->numgbits - 1; /* WHY -1 ? */
269 (tcd_precinct_t *) malloc(3 * res->pw * res->ph *
270 sizeof(tcd_precinct_t));
272 for (i = 0; i < res->pw * res->ph * 3; i++) {
273 band->precincts[i].imsbtree = NULL;
274 band->precincts[i].incltree = NULL;
277 for (precno = 0; precno < res->pw * res->ph; precno++) {
278 int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
280 tlcbgxstart + (precno % res->pw) * (1 << cbgwidthexpn);
282 tlcbgystart + (precno / res->pw) * (1 << cbgheightexpn);
283 int cbgxend = cbgxstart + (1 << cbgwidthexpn);
284 int cbgyend = cbgystart + (1 << cbgheightexpn);
285 /* tcd_precinct_t *prc=&band->precincts[precno]; */
286 prc = &band->precincts[precno];
287 /* precinct size (global) */
288 prc->x0 = int_max(cbgxstart, band->x0);
289 prc->y0 = int_max(cbgystart, band->y0);
290 prc->x1 = int_min(cbgxend, band->x1);
291 prc->y1 = int_min(cbgyend, band->y1);
294 int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn;
296 int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn;
298 int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn;
300 int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn;
301 prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;
302 prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn;
305 (tcd_cblk_t *) malloc((prc->cw * prc->ch) *
307 prc->incltree = tgt_create(prc->cw, prc->ch);
308 prc->imsbtree = tgt_create(prc->cw, prc->ch);
310 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
312 tlcblkxstart + (cblkno % prc->cw) * (1 << cblkwidthexpn);
314 tlcblkystart + (cblkno / prc->cw) * (1 << cblkheightexpn);
315 int cblkxend = cblkxstart + (1 << cblkwidthexpn);
316 int cblkyend = cblkystart + (1 << cblkheightexpn);
318 cblk = &prc->cblks[cblkno];
319 /* code-block size (global) */
320 cblk->x0 = int_max(cblkxstart, prc->x0);
321 cblk->y0 = int_max(cblkystart, prc->y0);
322 cblk->x1 = int_min(cblkxend, prc->x1);
323 cblk->y1 = int_min(cblkyend, prc->y1);
330 /* tcd_dump(&tcd_image,curtileno); */
333 void tcd_free_encode(j2k_image_t * img, j2k_cp_t * cp, int curtileno)
335 int tileno, compno, resno, bandno, precno;
338 tcd_image.tw = cp->tw;
339 tcd_image.th = cp->th;
340 for (tileno = 0; tileno < 1; tileno++) {
341 /* j2k_tcp_t *tcp=&cp->tcps[curtileno]; */
342 tile = tcd_image.tiles;
343 for (compno = 0; compno < tile->numcomps; compno++) {
344 tilec = &tile->comps[compno];
345 for (resno = 0; resno < tilec->numresolutions; resno++) {
346 res = &tilec->resolutions[resno];
347 for (bandno = 0; bandno < res->numbands; bandno++) {
348 band = &res->bands[bandno];
349 for (precno = 0; precno < res->pw * res->ph; precno++) {
350 prc = &band->precincts[precno];
352 if (prc->incltree != NULL)
353 tgt_destroy(prc->incltree);
354 if (prc->imsbtree != NULL)
355 tgt_destroy(prc->imsbtree);
358 free(band->precincts);
361 free(tilec->resolutions);
365 free(tcd_image.tiles);
368 void tcd_init_encode(j2k_image_t * img, j2k_cp_t * cp, int curtileno)
370 int tileno, compno, resno, bandno, precno, cblkno;
372 for (tileno = 0; tileno < 1; tileno++) {
373 j2k_tcp_t *tcp = &cp->tcps[curtileno];
375 // int previous_x0, previous_x1, previous_y0, previous_y1;
376 /* cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
377 int p = curtileno % cp->tw;
378 int q = curtileno / cp->tw;
379 tile = tcd_image.tiles;
381 /* 4 borders of the tile rescale on the image if necessary */
382 tile->x0 = int_max(cp->tx0 + p * cp->tdx, img->x0);
383 tile->y0 = int_max(cp->ty0 + q * cp->tdy, img->y0);
384 tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, img->x1);
385 tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, img->y1);
387 tile->numcomps = img->numcomps;
388 /* tile->PPT=img->PPT; */
389 /* Modification of the RATE >> */
390 for (j = 0; j < tcp->numlayers; j++) {
392 int_ceildiv(tile->numcomps * (tile->x1 - tile->x0) *
394 tile->y0) * img->comps[0].prec, (tcp->rates[j] * 8 *
397 if (j && tcp->rates[j] < tcp->rates[j - 1] + 10) {
398 tcp->rates[j] = tcp->rates[j - 1] + 20;
400 if (!j && tcp->rates[j] < 30)
404 /* << Modification of the RATE */
405 /* tile->comps=(tcd_tilecomp_t*)realloc(tile->comps,img->numcomps*sizeof(tcd_tilecomp_t)); */
406 for (compno = 0; compno < tile->numcomps; compno++) {
407 j2k_tccp_t *tccp = &tcp->tccps[compno];
408 /* int realloc_op; */
410 tilec = &tile->comps[compno];
411 /* border of each tile component (global) */
412 tilec->x0 = int_ceildiv(tile->x0, img->comps[compno].dx);
413 tilec->y0 = int_ceildiv(tile->y0, img->comps[compno].dy);
414 tilec->x1 = int_ceildiv(tile->x1, img->comps[compno].dx);
415 tilec->y1 = int_ceildiv(tile->y1, img->comps[compno].dy);
418 (int *) malloc((tilec->x1 - tilec->x0) *
419 (tilec->y1 - tilec->y0) * sizeof(int));
420 tilec->numresolutions = tccp->numresolutions;
421 /* tilec->resolutions=(tcd_resolution_t*)realloc(tilec->resolutions,tilec->numresolutions*sizeof(tcd_resolution_t)); */
422 for (resno = 0; resno < tilec->numresolutions; resno++) {
424 int levelno = tilec->numresolutions - 1 - resno;
425 int tlprcxstart, tlprcystart, brprcxend, brprcyend;
426 int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend;
427 int cbgwidthexpn, cbgheightexpn;
428 int cblkwidthexpn, cblkheightexpn;
430 res = &tilec->resolutions[resno];
431 /* border for each resolution level (global) */
432 res->x0 = int_ceildivpow2(tilec->x0, levelno);
433 res->y0 = int_ceildivpow2(tilec->y0, levelno);
434 res->x1 = int_ceildivpow2(tilec->x1, levelno);
435 res->y1 = int_ceildivpow2(tilec->y1, levelno);
437 res->numbands = resno == 0 ? 1 : 3;
438 /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */
439 if (tccp->csty & J2K_CCP_CSTY_PRT) {
440 pdx = tccp->prcw[resno];
441 pdy = tccp->prch[resno];
446 /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
447 tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx;
448 tlprcystart = int_floordivpow2(res->y0, pdy) << pdy;
449 brprcxend = int_ceildivpow2(res->x1, pdx) << pdx;
450 brprcyend = int_ceildivpow2(res->y1, pdy) << pdy;
452 res->pw = (brprcxend - tlprcxstart) >> pdx;
453 res->ph = (brprcyend - tlprcystart) >> pdy;
456 tlcbgxstart = tlprcxstart;
457 tlcbgystart = tlprcystart;
458 brcbgxend = brprcxend;
459 brcbgyend = brprcyend;
463 tlcbgxstart = int_ceildivpow2(tlprcxstart, 1);
464 tlcbgystart = int_ceildivpow2(tlprcystart, 1);
465 brcbgxend = int_ceildivpow2(brprcxend, 1);
466 brcbgyend = int_ceildivpow2(brprcyend, 1);
467 cbgwidthexpn = pdx - 1;
468 cbgheightexpn = pdy - 1;
471 cblkwidthexpn = int_min(tccp->cblkw, cbgwidthexpn);
472 cblkheightexpn = int_min(tccp->cblkh, cbgheightexpn);
474 for (bandno = 0; bandno < res->numbands; bandno++) {
478 band = &res->bands[bandno];
479 band->bandno = resno == 0 ? 0 : bandno + 1;
480 x0b = (band->bandno == 1)
481 || (band->bandno == 3) ? 1 : 0;
482 y0b = (band->bandno == 2)
483 || (band->bandno == 3) ? 1 : 0;
485 if (band->bandno == 0) {
487 band->x0 = int_ceildivpow2(tilec->x0, levelno);
488 band->y0 = int_ceildivpow2(tilec->y0, levelno);
489 band->x1 = int_ceildivpow2(tilec->x1, levelno);
490 band->y1 = int_ceildivpow2(tilec->y1, levelno);
493 int_ceildivpow2(tilec->x0 -
494 (1 << levelno) * x0b, levelno + 1);
496 int_ceildivpow2(tilec->y0 -
497 (1 << levelno) * y0b, levelno + 1);
499 int_ceildivpow2(tilec->x1 -
500 (1 << levelno) * x0b, levelno + 1);
502 int_ceildivpow2(tilec->y1 -
503 (1 << levelno) * y0b, levelno + 1);
506 ss = &tccp->stepsizes[resno ==
507 0 ? 0 : 3 * (resno - 1) + bandno + 1];
510 0 ? dwt_getgain_real(band->bandno) : dwt_getgain(band->bandno);
511 numbps = img->comps[compno].prec + gain;
513 (int) floor((1.0 + ss->mant / 2048.0) *
514 pow(2.0, numbps - ss->expn) * 8192.0);
515 band->numbps = ss->expn + tccp->numgbits - 1; /* WHY -1 ? */
517 for (precno = 0; precno < res->pw * res->ph; precno++) {
518 int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
520 tlcbgxstart + (precno % res->pw) * (1 << cbgwidthexpn);
522 tlcbgystart + (precno / res->pw) * (1 << cbgheightexpn);
523 int cbgxend = cbgxstart + (1 << cbgwidthexpn);
524 int cbgyend = cbgystart + (1 << cbgheightexpn);
526 prc = &band->precincts[precno];
527 /* precinct size (global) */
528 prc->x0 = int_max(cbgxstart, band->x0);
529 prc->y0 = int_max(cbgystart, band->y0);
530 prc->x1 = int_min(cbgxend, band->x1);
531 prc->y1 = int_min(cbgyend, band->y1);
534 int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn;
536 int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn;
538 int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn;
540 int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn;
541 prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;
542 prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn;
546 (tcd_cblk_t *) malloc(prc->cw * prc->ch *
549 if (prc->incltree != NULL)
550 tgt_destroy(prc->incltree);
551 if (prc->imsbtree != NULL)
552 tgt_destroy(prc->imsbtree);
554 prc->incltree = tgt_create(prc->cw, prc->ch);
555 prc->imsbtree = tgt_create(prc->cw, prc->ch);
557 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
559 tlcblkxstart + (cblkno % prc->cw) * (1 << cblkwidthexpn);
561 tlcblkystart + (cblkno / prc->cw) * (1 << cblkheightexpn);
562 int cblkxend = cblkxstart + (1 << cblkwidthexpn);
563 int cblkyend = cblkystart + (1 << cblkheightexpn);
564 cblk = &prc->cblks[cblkno];
566 /* code-block size (global) */
567 cblk->x0 = int_max(cblkxstart, prc->x0);
568 cblk->y0 = int_max(cblkystart, prc->y0);
569 cblk->x1 = int_min(cblkxend, prc->x1);
570 cblk->y1 = int_min(cblkyend, prc->y1);
578 /* tcd_dump(&tcd_image,0); */
581 void tcd_init(j2k_image_t * img, j2k_cp_t * cp)
583 int tileno, compno, resno, bandno, precno, cblkno, i, j;
584 unsigned int x0 = 0, y0 = 0, x1 = 0, y1 = 0, w, h, p, q;
587 tcd_image.tw = cp->tw;
588 tcd_image.th = cp->th;
590 (tcd_tile_t *) malloc(cp->tw * cp->th * sizeof(tcd_tile_t));
592 /*for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
593 j2k_tcp_t *tcp = &cp->tcps[tileno];
594 tcd_tile_t *tile = &tcd_image.tiles[tileno]; */
596 for (i = 0; i < cp->tileno_size; i++) {
597 j2k_tcp_t *tcp = &cp->tcps[cp->tileno[i]];
598 tcd_tile_t *tile = &tcd_image.tiles[cp->tileno[i]];
599 tileno = cp->tileno[i];
602 // int previous_x0, previous_x1, previous_y0, previous_y1;
603 /* cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
604 p = tileno % cp->tw; /* si numerotation matricielle .. */
605 q = tileno / cp->tw; /* .. coordonnees de la tile (q,p) q pour ligne et p pour colonne */
607 /* 4 borders of the tile rescale on the image if necessary */
608 tile->x0 = int_max(cp->tx0 + p * cp->tdx, img->x0);
609 tile->y0 = int_max(cp->ty0 + q * cp->tdy, img->y0);
610 tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, img->x1);
611 tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, img->y1);
613 tile->numcomps = img->numcomps;
615 (tcd_tilecomp_t *) malloc(img->numcomps * sizeof(tcd_tilecomp_t));
616 for (compno = 0; compno < tile->numcomps; compno++) {
617 j2k_tccp_t *tccp = &tcp->tccps[compno];
618 tcd_tilecomp_t *tilec = &tile->comps[compno];
619 /* border of each tile component (global) */
620 tilec->x0 = int_ceildiv(tile->x0, img->comps[compno].dx);
621 tilec->y0 = int_ceildiv(tile->y0, img->comps[compno].dy);
622 tilec->x1 = int_ceildiv(tile->x1, img->comps[compno].dx);
623 tilec->y1 = int_ceildiv(tile->y1, img->comps[compno].dy);
626 (int *) malloc((tilec->x1 - tilec->x0) *
627 (tilec->y1 - tilec->y0) * sizeof(int));
628 tilec->numresolutions = tccp->numresolutions;
630 (tcd_resolution_t *) malloc(tilec->numresolutions *
631 sizeof(tcd_resolution_t));
632 for (resno = 0; resno < tilec->numresolutions; resno++) {
634 int levelno = tilec->numresolutions - 1 - resno;
635 int tlprcxstart, tlprcystart, brprcxend, brprcyend;
636 int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend;
637 int cbgwidthexpn, cbgheightexpn;
638 int cblkwidthexpn, cblkheightexpn;
639 tcd_resolution_t *res = &tilec->resolutions[resno];
641 /* border for each resolution level (global) */
642 res->x0 = int_ceildivpow2(tilec->x0, levelno);
643 res->y0 = int_ceildivpow2(tilec->y0, levelno);
644 res->x1 = int_ceildivpow2(tilec->x1, levelno);
645 res->y1 = int_ceildivpow2(tilec->y1, levelno);
647 res->numbands = resno == 0 ? 1 : 3;
648 /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */
649 if (tccp->csty & J2K_CCP_CSTY_PRT) {
650 pdx = tccp->prcw[resno];
651 pdy = tccp->prch[resno];
656 /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
657 tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx;
658 tlprcystart = int_floordivpow2(res->y0, pdy) << pdy;
659 brprcxend = int_ceildivpow2(res->x1, pdx) << pdx;
660 brprcyend = int_ceildivpow2(res->y1, pdy) << pdy;
661 res->pw = (brprcxend - tlprcxstart) >> pdx;
662 res->ph = (brprcyend - tlprcystart) >> pdy;
665 tlcbgxstart = tlprcxstart;
666 tlcbgystart = tlprcystart;
667 brcbgxend = brprcxend;
668 brcbgyend = brprcyend;
672 tlcbgxstart = int_ceildivpow2(tlprcxstart, 1);
673 tlcbgystart = int_ceildivpow2(tlprcystart, 1);
674 brcbgxend = int_ceildivpow2(brprcxend, 1);
675 brcbgyend = int_ceildivpow2(brprcyend, 1);
676 cbgwidthexpn = pdx - 1;
677 cbgheightexpn = pdy - 1;
680 cblkwidthexpn = int_min(tccp->cblkw, cbgwidthexpn);
681 cblkheightexpn = int_min(tccp->cblkh, cbgheightexpn);
683 for (bandno = 0; bandno < res->numbands; bandno++) {
687 tcd_band_t *band = &res->bands[bandno];
688 band->bandno = resno == 0 ? 0 : bandno + 1;
689 x0b = (band->bandno == 1)
690 || (band->bandno == 3) ? 1 : 0;
691 y0b = (band->bandno == 2)
692 || (band->bandno == 3) ? 1 : 0;
694 if (band->bandno == 0) {
695 /* band border (global) */
696 band->x0 = int_ceildivpow2(tilec->x0, levelno);
697 band->y0 = int_ceildivpow2(tilec->y0, levelno);
698 band->x1 = int_ceildivpow2(tilec->x1, levelno);
699 band->y1 = int_ceildivpow2(tilec->y1, levelno);
701 /* band border (global) */
703 int_ceildivpow2(tilec->x0 -
704 (1 << levelno) * x0b, levelno + 1);
706 int_ceildivpow2(tilec->y0 -
707 (1 << levelno) * y0b, levelno + 1);
709 int_ceildivpow2(tilec->x1 -
710 (1 << levelno) * x0b, levelno + 1);
712 int_ceildivpow2(tilec->y1 -
713 (1 << levelno) * y0b, levelno + 1);
716 ss = &tccp->stepsizes[resno ==
717 0 ? 0 : 3 * (resno - 1) + bandno + 1];
720 0 ? dwt_getgain_real(band->bandno) : dwt_getgain(band->bandno);
721 numbps = img->comps[compno].prec + gain;
723 (int) floor((1.0 + ss->mant / 2048.0) *
724 pow(2.0, numbps - ss->expn) * 8192.0);
725 band->numbps = ss->expn + tccp->numgbits - 1; /* WHY -1 ? */
728 (tcd_precinct_t *) malloc(res->pw * res->ph *
729 sizeof(tcd_precinct_t));
731 for (precno = 0; precno < res->pw * res->ph; precno++) {
732 int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
734 tlcbgxstart + (precno % res->pw) * (1 << cbgwidthexpn);
736 tlcbgystart + (precno / res->pw) * (1 << cbgheightexpn);
737 int cbgxend = cbgxstart + (1 << cbgwidthexpn);
738 int cbgyend = cbgystart + (1 << cbgheightexpn);
739 tcd_precinct_t *prc = &band->precincts[precno];
740 /* precinct size (global) */
741 prc->x0 = int_max(cbgxstart, band->x0);
742 prc->y0 = int_max(cbgystart, band->y0);
743 prc->x1 = int_min(cbgxend, band->x1);
744 prc->y1 = int_min(cbgyend, band->y1);
747 int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn;
749 int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn;
751 int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn;
753 int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn;
754 prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;
755 prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn;
758 (tcd_cblk_t *) malloc(prc->cw * prc->ch *
761 prc->incltree = tgt_create(prc->cw, prc->ch);
762 prc->imsbtree = tgt_create(prc->cw, prc->ch);
764 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
766 tlcblkxstart + (cblkno % prc->cw) * (1 << cblkwidthexpn);
768 tlcblkystart + (cblkno / prc->cw) * (1 << cblkheightexpn);
769 int cblkxend = cblkxstart + (1 << cblkwidthexpn);
770 int cblkyend = cblkystart + (1 << cblkheightexpn);
771 tcd_cblk_t *cblk = &prc->cblks[cblkno];
772 /* code-block size (global) */
773 cblk->x0 = int_max(cblkxstart, prc->x0);
774 cblk->y0 = int_max(cblkystart, prc->y0);
775 cblk->x1 = int_min(cblkxend, prc->x1);
776 cblk->y1 = int_min(cblkyend, prc->y1);
783 /* tcd_dump(&tcd_image,0); */
786 /* Allocate place to store the date decoded = fianl image */
787 /* Place limited by the tile really present in the codestream */
788 for (i = 0; i < img->numcomps; i++) {
789 for (j = 0; j < cp->tileno_size; j++) {
790 tileno = cp->tileno[j];
791 x0 = j == 0 ? tcd_image.tiles[tileno].x0 : int_min(x0,
794 y0 = j == 0 ? tcd_image.tiles[tileno].y0 : int_min(y0,
797 x1 = j == 0 ? tcd_image.tiles[tileno].x1 : int_max(x1,
800 y1 = j == 0 ? tcd_image.tiles[tileno].y1 : int_max(y1,
804 w = int_ceildiv(x1 - x0, img->comps[i].dx);
805 h = int_ceildiv(y1 - y0, img->comps[i].dy);
806 img->comps[i].data = (int *) calloc(w * h, sizeof(int));
809 img->comps[i].x0 = x0;
810 img->comps[i].y0 = y0;
815 void tcd_makelayer_fixed(int layno, int final)
817 int compno, resno, bandno, precno, cblkno;
818 int value; //, matrice[tcd_tcp->numlayers][tcd_tile->comps[0].numresolutions][3];
819 int matrice[10][10][3];
822 /*matrice=(int*)malloc(tcd_tcp->numlayers*tcd_tile->comps[0].numresolutions*3*sizeof(int)); */
824 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
825 tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
826 for (i = 0; i < tcd_tcp->numlayers; i++) {
827 for (j = 0; j < tilec->numresolutions; j++) {
828 for (k = 0; k < 3; k++) {
831 matrice[i * tilec->numresolutions * 3 +
834 (float) (tcd_img->comps[compno].prec / 16.0));
837 for (resno = 0; resno < tilec->numresolutions; resno++) {
838 tcd_resolution_t *res = &tilec->resolutions[resno];
839 for (bandno = 0; bandno < res->numbands; bandno++) {
840 tcd_band_t *band = &res->bands[bandno];
841 for (precno = 0; precno < res->pw * res->ph; precno++) {
842 tcd_precinct_t *prc = &band->precincts[precno];
843 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
844 tcd_cblk_t *cblk = &prc->cblks[cblkno];
845 tcd_layer_t *layer = &cblk->layers[layno];
847 int imsb = tcd_img->comps[compno].prec - cblk->numbps; /* number of bit-plan equal to zero */
848 /* Correction of the matrix of coefficient to include the IMSB information */
851 value = matrice[layno][resno][bandno];
858 matrice[layno][resno][bandno] -
859 matrice[layno - 1][resno][bandno];
860 if (imsb >= matrice[layno - 1][resno][bandno]) {
861 value -= (imsb - matrice[layno - 1][resno][bandno]);
868 cblk->numpassesinlayers = 0;
870 n = cblk->numpassesinlayers;
871 if (cblk->numpassesinlayers == 0) {
873 n = 3 * value - 2 + cblk->numpassesinlayers;
875 n = cblk->numpassesinlayers;
877 n = 3 * value + cblk->numpassesinlayers;
879 layer->numpasses = n - cblk->numpassesinlayers;
881 if (!layer->numpasses)
884 if (cblk->numpassesinlayers == 0) {
885 layer->len = cblk->passes[n - 1].rate;
886 layer->data = cblk->data;
889 cblk->passes[n - 1].rate -
890 cblk->passes[cblk->numpassesinlayers - 1].rate;
893 cblk->passes[cblk->numpassesinlayers - 1].rate;
896 cblk->numpassesinlayers = n;
904 void tcd_rateallocate_fixed()
908 for (layno = 0; layno < tcd_tcp->numlayers; layno++) {
909 tcd_makelayer_fixed(layno, 1);
913 void tcd_makelayer(int layno, double thresh, int final)
915 int compno, resno, bandno, precno, cblkno, passno;
917 tcd_tile->distolayer[layno] = 0; //add fixed_quality
919 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
920 tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
921 for (resno = 0; resno < tilec->numresolutions; resno++) {
922 tcd_resolution_t *res = &tilec->resolutions[resno];
923 for (bandno = 0; bandno < res->numbands; bandno++) {
924 tcd_band_t *band = &res->bands[bandno];
925 for (precno = 0; precno < res->pw * res->ph; precno++) {
926 tcd_precinct_t *prc = &band->precincts[precno];
927 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
928 tcd_cblk_t *cblk = &prc->cblks[cblkno];
929 tcd_layer_t *layer = &cblk->layers[layno];
933 cblk->numpassesinlayers = 0;
935 n = cblk->numpassesinlayers;
936 for (passno = cblk->numpassesinlayers;
937 passno < cblk->totalpasses; passno++) {
940 tcd_pass_t *pass = &cblk->passes[passno];
943 dd = pass->distortiondec;
945 dr = pass->rate - cblk->passes[n - 1].rate;
946 dd = pass->distortiondec - cblk->passes[n -
954 if (dd / dr > thresh)
957 layer->numpasses = n - cblk->numpassesinlayers;
959 if (!layer->numpasses) {
964 if (cblk->numpassesinlayers == 0) {
965 layer->len = cblk->passes[n - 1].rate;
966 layer->data = cblk->data;
967 layer->disto = cblk->passes[n - 1].distortiondec;
969 layer->len = cblk->passes[n - 1].rate -
970 cblk->passes[cblk->numpassesinlayers - 1].rate;
973 cblk->passes[cblk->numpassesinlayers - 1].rate;
975 cblk->passes[n - 1].distortiondec -
976 cblk->passes[cblk->numpassesinlayers - 1].distortiondec;
979 tcd_tile->distolayer[layno] += layer->disto; //add fixed_quality
982 cblk->numpassesinlayers = n;
990 void tcd_rateallocate(unsigned char *dest, int len, info_image * info_IM)
992 int compno, resno, bandno, precno, cblkno, passno, layno;
994 double cumdisto[100]; //add fixed_quality
995 const double K = 1; // 1.1; //add fixed_quality
1000 tcd_tile->nbpix = 0; //add fixed_quality
1002 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
1003 tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
1005 for (resno = 0; resno < tilec->numresolutions; resno++) {
1006 tcd_resolution_t *res = &tilec->resolutions[resno];
1007 for (bandno = 0; bandno < res->numbands; bandno++) {
1008 tcd_band_t *band = &res->bands[bandno];
1009 for (precno = 0; precno < res->pw * res->ph; precno++) {
1010 tcd_precinct_t *prc = &band->precincts[precno];
1011 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
1012 tcd_cblk_t *cblk = &prc->cblks[cblkno];
1013 for (passno = 0; passno < cblk->totalpasses; passno++) {
1014 tcd_pass_t *pass = &cblk->passes[passno];
1019 dd = pass->distortiondec;
1021 dr = pass->rate - cblk->passes[passno - 1].rate;
1022 dd = pass->distortiondec -
1023 cblk->passes[passno - 1].distortiondec;
1029 if (rdslope < min) {
1032 if (rdslope > max) {
1037 tcd_tile->nbpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0)); //add fixed_quality
1038 tilec->nbpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0)); //add fixed_quality
1044 maxSE+=(double)(((1<<tcd_img->comps[compno].prec)-1)*((1<<tcd_img->comps[compno].prec)-1))*(tilec->nbpix);
1047 /* add antonin index */
1048 if (info_IM->index_on) {
1049 info_tile *info_TL = &info_IM->tile[tcd_tileno];
1050 info_TL->nbpix = tcd_tile->nbpix;
1051 info_TL->distotile = tcd_tile->distotile;
1053 (double *) malloc(tcd_tcp->numlayers * sizeof(double));
1057 for (layno = 0; layno < tcd_tcp->numlayers; layno++) {
1058 volatile double lo = min;
1059 volatile double hi = max;
1060 volatile int success = 0;
1061 volatile int maxlen = int_min(tcd_tcp->rates[layno], len);
1062 volatile double goodthresh;
1063 volatile int goodlen;
1065 double distotarget; //add fixed_quality
1067 distotarget = tcd_tile->distotile - ((K * maxSE) / pow(10, tcd_tcp->distoratio[layno] / 10)); // add fixed_quality
1069 for (i = 0; i < 32; i++) {
1070 volatile double thresh = (lo + hi) / 2;
1072 double distoachieved = 0; // add fixed_quality
1074 tcd_makelayer(layno, thresh, 0);
1076 if (tcd_cp->fixed_quality) { // add fixed_quality
1079 0 ? tcd_tile->distolayer[0] : cumdisto[layno - 1] +
1080 tcd_tile->distolayer[layno];
1081 if (distoachieved < distotarget) {
1088 t2_encode_packets(tcd_img, tcd_cp, tcd_tileno, tcd_tile,
1089 layno + 1, dest, maxlen, info_IM);
1090 /* fprintf(stderr, "rate alloc: len=%d, max=%d\n", l, maxlen); */
1099 goodthresh = thresh;
1105 longjmp(j2k_error, 1);
1108 if (info_IM->index_on) { /* Threshold for Marcela Index */
1109 info_IM->tile[tcd_tileno].thresh[layno] = goodthresh;
1111 tcd_makelayer(layno, goodthresh, 1);
1114 0 ? tcd_tile->distolayer[0] : cumdisto[layno - 1] +
1115 tcd_tile->distolayer[layno]; // add fixed_quality
1119 int tcd_encode_tile_pxm(int tileno, unsigned char *dest, int len,
1120 info_image * info_IM)
1126 j2k_tcp_t *tcp = &tcd_cp->tcps[0];
1127 j2k_tccp_t *tccp = &tcp->tccps[0];
1129 tcd_tileno = tileno;
1130 tcd_tile = tcd_image.tiles;
1131 tcd_tcp = &tcd_cp->tcps[tileno];
1133 /* INDEX >> "Precinct_nb_X et Precinct_nb_Y" */
1134 if (info_IM->index_on) {
1135 tcd_tilecomp_t *tilec_idx = &tile->comps[0]; //Based on Component 0
1136 for (i=0;i<tilec_idx->numresolutions;i++) {
1137 tcd_resolution_t *res_idx = &tilec_idx->resolutions[i];
1139 info_IM->tile[tileno].pw[i] = res_idx->pw;
1140 info_IM->tile[tileno].ph[i] = res_idx->ph;
1142 info_IM->tile[tileno].pdx[i] = tccp->prcw[i];
1143 info_IM->tile[tileno].pdy[i] = tccp->prch[i];
1148 /*---------------TILE-------------------*/
1152 for (compno = 0; compno < tile->numcomps; compno++) {
1159 tcd_tilecomp_t *tilec = &tile->comps[compno];
1161 tcd_img->comps[compno].sgnd ? 0 : 1 << (tcd_img->comps[compno].
1163 int offset_x, offset_y;
1165 offset_x = int_ceildiv(tcd_img->x0, tcd_img->comps[compno].dx);
1166 offset_y = int_ceildiv(tcd_img->y0, tcd_img->comps[compno].dy);
1167 tw = tilec->x1 - tilec->x0;
1168 w = int_ceildiv(tcd_img->x1 - tcd_img->x0, tcd_img->comps[compno].dx);
1169 sprintf(tmp, "Compo%d", compno); /* component file */
1170 src = fopen(tmp, "rb");
1172 fprintf(stderr, "failed to open %s for reading\n", tmp);
1176 /* read the Compo file to extract data of the tile */
1178 fseek(src, (tilec->x0 - offset_x) + (tilec->y0 - offset_y) * w,
1180 k = (tilec->x0 - offset_x) + (tilec->y0 - offset_y) * w;
1181 for (j = tilec->y0; j < tilec->y1; j++) {
1182 for (i = tilec->x0; i < tilec->x1; i++) {
1183 if (tcd_tcp->tccps[compno].qmfbid == 1) {
1185 tilec->data[i - tilec->x0 + (j - tilec->y0) * tw] =
1188 } else if (tcd_tcp->tccps[compno].qmfbid == 0) {
1190 tilec->data[i - tilec->x0 + (j - tilec->y0) * tw] =
1191 (elmt - adjust) << 13;
1195 fseek(src, (tilec->x0 - offset_x) + (j + 1 - offset_y) * w - k,
1197 k = tilec->x0 - offset_x + (j + 1 - offset_y) * w;
1203 /*----------------MCT-------------------*/
1206 if (tcd_tcp->tccps[0].qmfbid == 0) {
1207 mct_encode_real(tile->comps[0].data, tile->comps[1].data,
1208 tile->comps[2].data,
1209 (tile->comps[0].x1 -
1210 tile->comps[0].x0) * (tile->comps[0].y1 -
1211 tile->comps[0].y0));
1213 mct_encode(tile->comps[0].data, tile->comps[1].data,
1214 tile->comps[2].data,
1215 (tile->comps[0].x1 -
1216 tile->comps[0].x0) * (tile->comps[0].y1 -
1217 tile->comps[0].y0));
1220 /*----------------DWT---------------------*/
1222 /* time3=clock(); */
1223 for (compno = 0; compno < tile->numcomps; compno++) {
1224 tcd_tilecomp_t *tilec = &tile->comps[compno];
1225 if (tcd_tcp->tccps[compno].qmfbid == 1) {
1226 dwt_encode(tilec->data, tilec->x1 - tilec->x0,
1227 tilec->y1 - tilec->y0, tilec, tilec->numresolutions - 1);
1228 } else if (tcd_tcp->tccps[compno].qmfbid == 0) {
1229 dwt_encode_real(tilec->data, tilec->x1 - tilec->x0,
1230 tilec->y1 - tilec->y0, tilec,
1231 tilec->numresolutions - 1);
1234 /*------------------TIER1-----------------*/
1237 t1_encode_cblks(tile, tcd_tcp);
1239 /*-----------RATE-ALLOCATE------------------*/
1240 info_IM->index_write = 0; /* INDEX */
1242 if (tcd_cp->disto_alloc || tcd_cp->fixed_quality) // mod fixed_quality
1243 /* Normal Rate/distortion allocation */
1244 tcd_rateallocate(dest, len, info_IM);
1246 /* Fixed layer allocation */
1247 tcd_rateallocate_fixed();
1249 /*--------------TIER2------------------*/
1250 info_IM->index_write = 1; /* INDEX */
1251 l = t2_encode_packets(tcd_img, tcd_cp, tileno, tile,
1252 tcd_tcp->numlayers, dest, len, info_IM);
1253 /*---------------CLEAN-------------------*/
1255 time7 = clock() - time7;
1256 printf("total: %ld.%.3ld s\n", time7 / CLOCKS_PER_SEC,
1257 (time7 % CLOCKS_PER_SEC) * 1000 / CLOCKS_PER_SEC);
1259 /* cleaning memory */
1260 for (compno = 0; compno < tile->numcomps; compno++) {
1261 tilec = &tile->comps[compno];
1268 int tcd_encode_tile_pgx(int tileno, unsigned char *dest, int len,
1269 info_image * info_IM)
1275 j2k_tcp_t *tcp = &tcd_cp->tcps[0];
1276 j2k_tccp_t *tccp = &tcp->tccps[0];
1278 tcd_tileno = tileno;
1279 tcd_tile = tcd_image.tiles;
1280 tcd_tcp = &tcd_cp->tcps[tileno];
1282 /* INDEX >> "Precinct_nb_X et Precinct_nb_Y" */
1283 if (info_IM->index_on) {
1284 tcd_tilecomp_t *tilec_idx = &tile->comps[0]; //Based on Component 0
1285 for (i=0;i<tilec_idx->numresolutions;i++) {
1286 tcd_resolution_t *res_idx = &tilec_idx->resolutions[i];
1288 info_IM->tile[tileno].pw[i] = res_idx->pw;
1289 info_IM->tile[tileno].ph[i] = res_idx->ph;
1291 info_IM->tile[tileno].pdx[i] = tccp->prcw[i];
1292 info_IM->tile[tileno].pdy[i] = tccp->prch[i];
1296 /*---------------TILE-------------------*/
1299 for (compno = 0; compno < tile->numcomps; compno++) {
1306 tcd_tilecomp_t *tilec = &tile->comps[compno];
1308 tcd_img->comps[compno].sgnd ? 0 : 1 << (tcd_img->comps[compno].
1310 int offset_x, offset_y;
1312 offset_x = int_ceildiv(tcd_img->x0, tcd_img->comps[compno].dx);
1313 offset_y = int_ceildiv(tcd_img->y0, tcd_img->comps[compno].dy);
1314 tw = tilec->x1 - tilec->x0;
1315 w = int_ceildiv(tcd_img->x1 - tcd_img->x0, tcd_img->comps[compno].dx);
1316 sprintf(tmp, "bandtile%d", tileno / tcd_cp->tw + 1); /* bandtile file opening */
1317 src = fopen(tmp, "rb");
1319 fprintf(stderr, "failed to open %s for reading\n", tmp);
1322 /* Extract data from bandtile file limited to the current tile */
1324 while (k < tilec->x0 - offset_x) {
1326 fscanf(src, "%d", &elmt);
1329 for (j = 0; j < tilec->y1 - tilec->y0; j++) {
1330 for (i = tilec->x0; i < tilec->x1; i++) {
1331 if (tcd_tcp->tccps[compno].qmfbid == 1) {
1332 fscanf(src, "%d", &elmt);
1333 tilec->data[i - tilec->x0 + (j) * tw] = elmt - adjust;
1335 } else if (tcd_tcp->tccps[compno].qmfbid == 0) {
1336 fscanf(src, "%d", &elmt);
1337 tilec->data[i - tilec->x0 + (j) * tw] = (elmt - adjust) << 13;
1341 while (k < tilec->x0 - offset_x + (j + 1) * w) {
1343 fscanf(src, "%d", &elmt);
1349 /*----------------MCT-------------------*/
1352 if (tcd_tcp->tccps[0].qmfbid == 0) {
1353 mct_encode_real(tile->comps[0].data, tile->comps[1].data,
1354 tile->comps[2].data,
1355 (tile->comps[0].x1 -
1356 tile->comps[0].x0) * (tile->comps[0].y1 -
1357 tile->comps[0].y0));
1359 mct_encode(tile->comps[0].data, tile->comps[1].data,
1360 tile->comps[2].data,
1361 (tile->comps[0].x1 -
1362 tile->comps[0].x0) * (tile->comps[0].y1 -
1363 tile->comps[0].y0));
1367 /*----------------DWT---------------------*/
1369 for (compno = 0; compno < tile->numcomps; compno++) {
1370 tcd_tilecomp_t *tilec = &tile->comps[compno];
1371 if (tcd_tcp->tccps[compno].qmfbid == 1) {
1372 dwt_encode(tilec->data, tilec->x1 - tilec->x0,
1373 tilec->y1 - tilec->y0, tilec, tilec->numresolutions - 1);
1374 } else if (tcd_tcp->tccps[compno].qmfbid == 0) {
1375 dwt_encode_real(tilec->data, tilec->x1 - tilec->x0,
1376 tilec->y1 - tilec->y0, tilec,
1377 tilec->numresolutions - 1);
1381 /*------------------TIER1-----------------*/
1384 t1_encode_cblks(tile, tcd_tcp);
1386 /*-----------RATE-ALLOCATE------------------*/
1388 info_IM->index_write = 0; /* INDEX */
1390 if (tcd_cp->disto_alloc || tcd_cp->fixed_quality) // mod fixed_quality
1391 /* Normal Rate/distortion allocation */
1392 tcd_rateallocate(dest, len, info_IM);
1394 /* Fixed layer allocation */
1395 tcd_rateallocate_fixed();
1397 /*--------------TIER2------------------*/
1398 info_IM->index_write = 1; /* INDEX */
1400 l = t2_encode_packets(tcd_img, tcd_cp, tileno, tile,
1401 tcd_tcp->numlayers, dest, len, info_IM);
1403 /*---------------CLEAN-------------------*/
1404 time = clock() - time;
1405 printf("total: %ld.%.3ld s\n", time / CLOCKS_PER_SEC,
1406 (time % CLOCKS_PER_SEC) * 1000 / CLOCKS_PER_SEC);
1408 for (compno = 0; compno < tile->numcomps; compno++) {
1409 tilec = &tile->comps[compno];
1417 int tcd_decode_tile(unsigned char *src, int len, int tileno)
1425 tcd_tileno = tileno;
1426 tcd_tile = &tcd_image.tiles[tileno];
1427 tcd_tcp = &tcd_cp->tcps[tileno];
1432 fprintf(stderr, "tile decoding time %d/%d: ", tileno + 1,
1433 tcd_cp->tw * tcd_cp->th);
1435 /*--------------TIER2------------------*/
1437 l = t2_decode_packets(src, len, tcd_img, tcd_cp, tileno, tile);
1441 fprintf(stderr, "tcd_decode: incomplete bistream\n");
1444 /*------------------TIER1-----------------*/
1446 t1_decode_cblks(tile, tcd_tcp);
1448 /*----------------DWT---------------------*/
1450 for (compno = 0; compno < tile->numcomps; compno++) {
1451 tcd_tilecomp_t *tilec = &tile->comps[compno];
1452 if (tcd_cp->reduce_on == 1) {
1453 tcd_img->comps[compno].resno_decoded =
1454 tile->comps[compno].numresolutions - tcd_cp->reduce_value - 1;
1458 if (tcd_tcp->tccps[compno].qmfbid == 1) {
1459 dwt_decode(tilec->data, tilec->x1 - tilec->x0,
1460 tilec->y1 - tilec->y0, tilec,
1461 tilec->numresolutions - 1,
1462 tilec->numresolutions - 1 -
1463 tcd_img->comps[compno].resno_decoded);
1465 dwt_decode_real(tilec->data, tilec->x1 - tilec->x0,
1466 tilec->y1 - tilec->y0, tilec,
1467 tilec->numresolutions - 1,
1468 tilec->numresolutions - 1 -
1469 tcd_img->comps[compno].resno_decoded);
1472 if (tile->comps[compno].numresolutions > 0)
1473 tcd_img->comps[compno].factor =
1474 tile->comps[compno].numresolutions -
1475 (tcd_img->comps[compno].resno_decoded + 1);
1478 /*----------------MCT-------------------*/
1481 if (tcd_tcp->tccps[0].qmfbid == 1) {
1482 mct_decode(tile->comps[0].data, tile->comps[1].data,
1483 tile->comps[2].data,
1484 (tile->comps[0].x1 -
1485 tile->comps[0].x0) * (tile->comps[0].y1 -
1486 tile->comps[0].y0));
1488 mct_decode_real(tile->comps[0].data, tile->comps[1].data,
1489 tile->comps[2].data,
1490 (tile->comps[0].x1 -
1491 tile->comps[0].x0) * (tile->comps[0].y1 -
1492 tile->comps[0].y0));
1496 /*---------------TILE-------------------*/
1498 for (compno = 0; compno < tile->numcomps; compno++) {
1499 tcd_tilecomp_t *tilec = &tile->comps[compno];
1500 tcd_resolution_t *res =
1501 &tilec->resolutions[tcd_img->comps[compno].resno_decoded];
1503 tcd_img->comps[compno].sgnd ? 0 : 1 << (tcd_img->comps[compno].
1506 tcd_img->comps[compno].
1507 sgnd ? -(1 << (tcd_img->comps[compno].prec - 1)) : 0;
1509 tcd_img->comps[compno].
1510 sgnd ? (1 << (tcd_img->comps[compno].prec - 1)) -
1511 1 : (1 << tcd_img->comps[compno].prec) - 1;
1513 int tw = tilec->x1 - tilec->x0;
1514 int w = tcd_img->comps[compno].w;
1517 int offset_x = int_ceildivpow2(tcd_img->comps[compno].x0,
1518 tcd_img->comps[compno].factor);
1519 int offset_y = int_ceildivpow2(tcd_img->comps[compno].y0,
1520 tcd_img->comps[compno].factor);
1522 for (j = res->y0; j < res->y1; j++) {
1523 for (i = res->x0; i < res->x1; i++) {
1526 if (tcd_tcp->tccps[compno].qmfbid == 1) {
1527 v = tilec->data[i - res->x0 + (j - res->y0) * tw];
1529 v = tilec->data[i - res->x0 + (j - res->y0) * tw] >> 13;
1533 tcd_img->comps[compno].data[(i - offset_x) +
1534 (j - offset_y) * w] =
1535 int_clamp(v, min, max);
1540 time = clock() - time;
1541 fprintf(stderr, "total: %ld.%.3ld s\n", time / CLOCKS_PER_SEC,
1542 (time % CLOCKS_PER_SEC) * 1000 / CLOCKS_PER_SEC);
1545 longjmp(j2k_error, 1);