2 * Copyright (c) 2001-2002, David Janssens
3 * Copyright (c) 2002-2003, Yannick Verschueren
4 * Copyright (c) 2002-2003, 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 ceil(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);
160 /* Special DWT (always this case) */
161 tilec->previous_row = 0;
162 tilec->previous_col = 0;
165 (int *) malloc(sizeof(int) * (tilec->x1 - tilec->x0) *
166 (tilec->y1 - tilec->y0));
167 tilec->numresolutions = tccp->numresolutions;
170 (tcd_resolution_t *) malloc(tilec->numresolutions *
171 sizeof(tcd_resolution_t));
173 for (resno = 0; resno < tilec->numresolutions; resno++) {
175 int levelno = tilec->numresolutions - 1 - resno;
176 int tlprcxstart, tlprcystart, brprcxend, brprcyend;
177 int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend;
178 int cbgwidthexpn, cbgheightexpn;
179 int cblkwidthexpn, cblkheightexpn;
180 /* tcd_resolution_t *res=&tilec->resolutions[resno]; */
182 res = &tilec->resolutions[resno];
184 /* border for each resolution level (global) */
185 res->x0 = int_ceildivpow2(tilec->x0, levelno);
186 res->y0 = int_ceildivpow2(tilec->y0, levelno);
187 res->x1 = int_ceildivpow2(tilec->x1, levelno);
188 res->y1 = int_ceildivpow2(tilec->y1, levelno);
190 /* Special DWT (always this case) */
191 res->previous_x0 = 0;
192 res->previous_y0 = 0;
193 res->previous_x1 = 0;
194 res->previous_y1 = 0;
198 res->numbands = resno == 0 ? 1 : 3;
199 /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */
200 if (tccp->csty & J2K_CCP_CSTY_PRT) {
201 pdx = tccp->prcw[resno];
202 pdy = tccp->prch[resno];
207 /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
208 tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx;
209 tlprcystart = int_floordivpow2(res->y0, pdy) << pdy;
210 brprcxend = int_ceildivpow2(res->x1, pdx) << pdx;
211 brprcyend = int_ceildivpow2(res->y1, pdy) << pdy;
213 res->pw = (brprcxend - tlprcxstart) >> pdx;
214 res->ph = (brprcyend - tlprcystart) >> pdy;
217 tlcbgxstart = tlprcxstart;
218 tlcbgystart = tlprcystart;
219 brcbgxend = brprcxend;
220 brcbgyend = brprcyend;
224 tlcbgxstart = int_ceildivpow2(tlprcxstart, 1);
225 tlcbgystart = int_ceildivpow2(tlprcystart, 1);
226 brcbgxend = int_ceildivpow2(brprcxend, 1);
227 brcbgyend = int_ceildivpow2(brprcyend, 1);
228 cbgwidthexpn = pdx - 1;
229 cbgheightexpn = pdy - 1;
232 cblkwidthexpn = int_min(tccp->cblkw, cbgwidthexpn);
233 cblkheightexpn = int_min(tccp->cblkh, cbgheightexpn);
235 for (bandno = 0; bandno < res->numbands; bandno++) {
239 band = &res->bands[bandno];
240 band->bandno = resno == 0 ? 0 : bandno + 1;
241 x0b = (band->bandno == 1) || (band->bandno == 3) ? 1 : 0;
242 y0b = (band->bandno == 2) || (band->bandno == 3) ? 1 : 0;
244 if (band->bandno == 0) {
245 /* band border (global) */
246 band->x0 = int_ceildivpow2(tilec->x0, levelno);
247 band->y0 = int_ceildivpow2(tilec->y0, levelno);
248 band->x1 = int_ceildivpow2(tilec->x1, levelno);
249 band->y1 = int_ceildivpow2(tilec->y1, levelno);
251 /* band border (global) */
253 int_ceildivpow2(tilec->x0 - (1 << levelno) * x0b,
256 int_ceildivpow2(tilec->y0 - (1 << levelno) * y0b,
259 int_ceildivpow2(tilec->x1 - (1 << levelno) * x0b,
262 int_ceildivpow2(tilec->y1 - (1 << levelno) * y0b,
268 &tccp->stepsizes[resno ==
269 0 ? 0 : 3 * (resno - 1) + bandno + 1];
272 0 ? dwt_getgain_real(band->bandno) : dwt_getgain(band->bandno);
273 numbps = img->comps[compno].prec + gain;
275 (int) floor((1.0 + ss->mant / 2048.0) *
276 pow(2.0, numbps - ss->expn) * 8192.0);
277 band->numbps = ss->expn + tccp->numgbits - 1; /* WHY -1 ? */
280 (tcd_precinct_t *) malloc(3 * res->pw * res->ph *
281 sizeof(tcd_precinct_t));
283 for (i = 0; i < res->pw * res->ph * 3; i++) {
284 band->precincts[i].imsbtree = NULL;
285 band->precincts[i].incltree = NULL;
288 for (precno = 0; precno < res->pw * res->ph; precno++) {
289 int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
291 tlcbgxstart + (precno % res->pw) * (1 << cbgwidthexpn);
293 tlcbgystart + (precno / res->pw) * (1 << cbgheightexpn);
294 int cbgxend = cbgxstart + (1 << cbgwidthexpn);
295 int cbgyend = cbgystart + (1 << cbgheightexpn);
296 /* tcd_precinct_t *prc=&band->precincts[precno]; */
297 prc = &band->precincts[precno];
298 /* precinct size (global) */
299 prc->x0 = int_max(cbgxstart, band->x0);
300 prc->y0 = int_max(cbgystart, band->y0);
301 prc->x1 = int_min(cbgxend, band->x1);
302 prc->y1 = int_min(cbgyend, band->y1);
305 int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn;
307 int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn;
309 int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn;
311 int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn;
312 prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;
313 prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn;
316 (tcd_cblk_t *) malloc((prc->cw * prc->ch) *
318 prc->incltree = tgt_create(prc->cw, prc->ch);
319 prc->imsbtree = tgt_create(prc->cw, prc->ch);
321 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
323 tlcblkxstart + (cblkno % prc->cw) * (1 << cblkwidthexpn);
325 tlcblkystart + (cblkno / prc->cw) * (1 << cblkheightexpn);
326 int cblkxend = cblkxstart + (1 << cblkwidthexpn);
327 int cblkyend = cblkystart + (1 << cblkheightexpn);
329 cblk = &prc->cblks[cblkno];
330 /* code-block size (global) */
331 cblk->x0 = int_max(cblkxstart, prc->x0);
332 cblk->y0 = int_max(cblkystart, prc->y0);
333 cblk->x1 = int_min(cblkxend, prc->x1);
334 cblk->y1 = int_min(cblkyend, prc->y1);
342 /* tcd_dump(&tcd_image,curtileno); */
345 void tcd_free_encode(j2k_image_t * img, j2k_cp_t * cp, int curtileno)
347 int tileno, compno, resno, bandno, precno;
350 tcd_image.tw = cp->tw;
351 tcd_image.th = cp->th;
352 for (tileno = 0; tileno < 1; tileno++) {
353 /* j2k_tcp_t *tcp=&cp->tcps[curtileno]; */
354 tile = tcd_image.tiles;
355 for (compno = 0; compno < tile->numcomps; compno++) {
356 tilec = &tile->comps[compno];
357 for (resno = 0; resno < tilec->numresolutions; resno++) {
358 res = &tilec->resolutions[resno];
359 for (bandno = 0; bandno < res->numbands; bandno++) {
360 band = &res->bands[bandno];
361 for (precno = 0; precno < res->pw * res->ph; precno++) {
362 prc = &band->precincts[precno];
364 if (prc->incltree != NULL)
365 tgt_destroy(prc->incltree);
366 if (prc->imsbtree != NULL)
367 tgt_destroy(prc->imsbtree);
370 free(band->precincts);
373 free(tilec->resolutions);
377 free(tcd_image.tiles);
380 void tcd_init_encode(j2k_image_t * img, j2k_cp_t * cp, int curtileno)
382 int tileno, compno, resno, bandno, precno, cblkno;
384 for (tileno = 0; tileno < 1; tileno++) {
385 j2k_tcp_t *tcp = &cp->tcps[curtileno];
387 int previous_x0, previous_x1, previous_y0, previous_y1;
388 /* cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
389 int p = curtileno % cp->tw;
390 int q = curtileno / cp->tw;
391 tile = tcd_image.tiles;
393 /* 4 borders of the tile rescale on the image if necessary */
394 tile->x0 = int_max(cp->tx0 + p * cp->tdx, img->x0);
395 tile->y0 = int_max(cp->ty0 + q * cp->tdy, img->y0);
396 tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, img->x1);
397 tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, img->y1);
400 previous_x0 = int_max(cp->tx0 + (p - 1) * cp->tdx, img->x0);
401 previous_x1 = int_min(cp->tx0 + p * cp->tdx, img->x1);
408 previous_y0 = int_max(cp->ty0 + (q - 1) * cp->tdy, img->y0);
409 previous_y1 = int_min(cp->ty0 + q * cp->tdy, img->y1);
416 tile->numcomps = img->numcomps;
417 /* tile->PPT=img->PPT; */
418 /* Modification of the RATE >> */
419 for (j = 0; j < tcp->numlayers; j++) {
421 ceil(tile->numcomps * (tile->x1 - tile->x0) *
423 tile->y0) * img->comps[0].prec / (tcp->rates[j] * 8 *
426 if (j && tcp->rates[j] < tcp->rates[j - 1] + 10) {
427 tcp->rates[j] = tcp->rates[j - 1] + 20;
429 if (!j && tcp->rates[j] < 30)
433 /* << Modification of the RATE */
434 /* tile->comps=(tcd_tilecomp_t*)realloc(tile->comps,img->numcomps*sizeof(tcd_tilecomp_t)); */
435 for (compno = 0; compno < tile->numcomps; compno++) {
436 j2k_tccp_t *tccp = &tcp->tccps[compno];
437 /* int realloc_op; */
439 tilec = &tile->comps[compno];
440 /* border of each tile component (global) */
441 tilec->x0 = int_ceildiv(tile->x0, img->comps[compno].dx);
442 tilec->y0 = int_ceildiv(tile->y0, img->comps[compno].dy);
443 tilec->x1 = int_ceildiv(tile->x1, img->comps[compno].dx);
444 tilec->y1 = int_ceildiv(tile->y1, img->comps[compno].dy);
446 /* special for DWT */
448 previous_x0 = int_ceildiv(previous_x0, img->comps[compno].dx);
449 previous_x1 = int_ceildiv(previous_x1, img->comps[compno].dx);
451 tilec->previous_row = 1;
455 tilec->previous_row = 0;
459 previous_y0 = int_ceildiv(previous_y0, img->comps[compno].dx);
460 previous_y1 = int_ceildiv(previous_y1, img->comps[compno].dx);
461 tilec->previous_col = 1;
465 tilec->previous_col = 0;
469 (int *) malloc(sizeof(int) * (tilec->x1 - tilec->x0) *
470 (tilec->y1 - tilec->y0));
471 tilec->numresolutions = tccp->numresolutions;
472 /* tilec->resolutions=(tcd_resolution_t*)realloc(tilec->resolutions,tilec->numresolutions*sizeof(tcd_resolution_t)); */
473 for (resno = 0; resno < tilec->numresolutions; resno++) {
475 int levelno = tilec->numresolutions - 1 - resno;
476 int tlprcxstart, tlprcystart, brprcxend, brprcyend;
477 int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend;
478 int cbgwidthexpn, cbgheightexpn;
479 int cblkwidthexpn, cblkheightexpn;
481 res = &tilec->resolutions[resno];
482 /* border for each resolution level (global) */
483 res->x0 = int_ceildivpow2(tilec->x0, levelno);
484 res->y0 = int_ceildivpow2(tilec->y0, levelno);
485 res->x1 = int_ceildivpow2(tilec->x1, levelno);
486 res->y1 = int_ceildivpow2(tilec->y1, levelno);
488 /* special for DWT */
489 res->previous_x0 = int_ceildivpow2(previous_x0, levelno);
490 res->previous_y0 = int_ceildivpow2(previous_y0, levelno);
491 res->previous_x1 = int_ceildivpow2(previous_x1, levelno);
492 res->previous_y1 = int_ceildivpow2(previous_y1, levelno);
494 res->numbands = resno == 0 ? 1 : 3;
495 /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */
496 if (tccp->csty & J2K_CCP_CSTY_PRT) {
497 pdx = tccp->prcw[resno];
498 pdy = tccp->prch[resno];
503 /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
504 tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx;
505 tlprcystart = int_floordivpow2(res->y0, pdy) << pdy;
506 brprcxend = int_ceildivpow2(res->x1, pdx) << pdx;
507 brprcyend = int_ceildivpow2(res->y1, pdy) << pdy;
509 res->pw = (brprcxend - tlprcxstart) >> pdx;
510 res->ph = (brprcyend - tlprcystart) >> pdy;
513 tlcbgxstart = tlprcxstart;
514 tlcbgystart = tlprcystart;
515 brcbgxend = brprcxend;
516 brcbgyend = brprcyend;
520 tlcbgxstart = int_ceildivpow2(tlprcxstart, 1);
521 tlcbgystart = int_ceildivpow2(tlprcystart, 1);
522 brcbgxend = int_ceildivpow2(brprcxend, 1);
523 brcbgyend = int_ceildivpow2(brprcyend, 1);
524 cbgwidthexpn = pdx - 1;
525 cbgheightexpn = pdy - 1;
528 cblkwidthexpn = int_min(tccp->cblkw, cbgwidthexpn);
529 cblkheightexpn = int_min(tccp->cblkh, cbgheightexpn);
531 for (bandno = 0; bandno < res->numbands; bandno++) {
535 band = &res->bands[bandno];
536 band->bandno = resno == 0 ? 0 : bandno + 1;
537 x0b = (band->bandno == 1) || (band->bandno == 3) ? 1 : 0;
538 y0b = (band->bandno == 2) || (band->bandno == 3) ? 1 : 0;
540 if (band->bandno == 0) {
542 band->x0 = int_ceildivpow2(tilec->x0, levelno);
543 band->y0 = int_ceildivpow2(tilec->y0, levelno);
544 band->x1 = int_ceildivpow2(tilec->x1, levelno);
545 band->y1 = int_ceildivpow2(tilec->y1, levelno);
548 int_ceildivpow2(tilec->x0 - (1 << levelno) * x0b,
551 int_ceildivpow2(tilec->y0 - (1 << levelno) * y0b,
554 int_ceildivpow2(tilec->x1 - (1 << levelno) * x0b,
557 int_ceildivpow2(tilec->y1 - (1 << levelno) * y0b,
562 &tccp->stepsizes[resno ==
563 0 ? 0 : 3 * (resno - 1) + bandno + 1];
566 0 ? dwt_getgain_real(band->bandno) : dwt_getgain(band->bandno);
567 numbps = img->comps[compno].prec + gain;
569 (int) floor((1.0 + ss->mant / 2048.0) *
570 pow(2.0, numbps - ss->expn) * 8192.0);
571 band->numbps = ss->expn + tccp->numgbits - 1; /* WHY -1 ? */
573 for (precno = 0; precno < res->pw * res->ph; precno++) {
574 int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
576 tlcbgxstart + (precno % res->pw) * (1 << cbgwidthexpn);
578 tlcbgystart + (precno / res->pw) * (1 << cbgheightexpn);
579 int cbgxend = cbgxstart + (1 << cbgwidthexpn);
580 int cbgyend = cbgystart + (1 << cbgheightexpn);
582 prc = &band->precincts[precno];
583 /* precinct size (global) */
584 prc->x0 = int_max(cbgxstart, band->x0);
585 prc->y0 = int_max(cbgystart, band->y0);
586 prc->x1 = int_min(cbgxend, band->x1);
587 prc->y1 = int_min(cbgyend, band->y1);
590 int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn;
592 int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn;
594 int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn;
596 int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn;
597 prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;
598 prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn;
602 (tcd_cblk_t *) malloc(prc->cw * prc->ch *
605 if (prc->incltree != NULL)
606 tgt_destroy(prc->incltree);
607 if (prc->imsbtree != NULL)
608 tgt_destroy(prc->imsbtree);
610 prc->incltree = tgt_create(prc->cw, prc->ch);
611 prc->imsbtree = tgt_create(prc->cw, prc->ch);
613 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
615 tlcblkxstart + (cblkno % prc->cw) * (1 << cblkwidthexpn);
617 tlcblkystart + (cblkno / prc->cw) * (1 << cblkheightexpn);
618 int cblkxend = cblkxstart + (1 << cblkwidthexpn);
619 int cblkyend = cblkystart + (1 << cblkheightexpn);
620 cblk = &prc->cblks[cblkno];
622 /* code-block size (global) */
623 cblk->x0 = int_max(cblkxstart, prc->x0);
624 cblk->y0 = int_max(cblkystart, prc->y0);
625 cblk->x1 = int_min(cblkxend, prc->x1);
626 cblk->y1 = int_min(cblkyend, prc->y1);
634 /* tcd_dump(&tcd_image,0); */
637 void tcd_init(j2k_image_t * img, j2k_cp_t * cp)
639 int tileno, compno, resno, bandno, precno, cblkno;
642 tcd_image.tw = cp->tw;
643 tcd_image.th = cp->th;
645 (tcd_tile_t *) malloc(cp->tw * cp->th * sizeof(tcd_tile_t));
646 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
647 j2k_tcp_t *tcp = &cp->tcps[tileno];
648 tcd_tile_t *tile = &tcd_image.tiles[tileno];
649 int previous_x0, previous_x1, previous_y0, previous_y1;
650 /* cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
651 int p = tileno % cp->tw; /* si numerotation matricielle .. */
652 int q = tileno / cp->tw; /* .. coordonnees de la tile (q,p) q pour ligne et p pour colonne */
654 /* 4 borders of the tile rescale on the image if necessary */
655 tile->x0 = int_max(cp->tx0 + p * cp->tdx, img->x0);
656 tile->y0 = int_max(cp->ty0 + q * cp->tdy, img->y0);
657 tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, img->x1);
658 tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, img->y1);
661 previous_x0 = int_max(cp->tx0 + (p - 1) * cp->tdx, img->x0);
662 previous_x1 = int_min(cp->tx0 + p * cp->tdx, img->x1);
669 previous_y0 = int_max(cp->ty0 + (q - 1) * cp->tdy, img->y0);
670 previous_y1 = int_min(cp->ty0 + q * cp->tdy, img->y1);
677 tile->numcomps = img->numcomps;
679 (tcd_tilecomp_t *) malloc(img->numcomps * sizeof(tcd_tilecomp_t));
680 for (compno = 0; compno < tile->numcomps; compno++) {
681 j2k_tccp_t *tccp = &tcp->tccps[compno];
682 tcd_tilecomp_t *tilec = &tile->comps[compno];
683 /* border of each tile component (global) */
684 tilec->x0 = int_ceildiv(tile->x0, img->comps[compno].dx);
685 tilec->y0 = int_ceildiv(tile->y0, img->comps[compno].dy);
686 tilec->x1 = int_ceildiv(tile->x1, img->comps[compno].dx);
687 tilec->y1 = int_ceildiv(tile->y1, img->comps[compno].dy);
688 /* special for DWT */
690 previous_x0 = int_ceildiv(previous_x0, img->comps[compno].dx);
691 previous_x1 = int_ceildiv(previous_x1, img->comps[compno].dx);
693 tilec->previous_row = 1;
697 tilec->previous_row = 0;
701 previous_y0 = int_ceildiv(previous_y0, img->comps[compno].dx);
702 previous_y1 = int_ceildiv(previous_y1, img->comps[compno].dx);
703 tilec->previous_col = 1;
707 tilec->previous_col = 0;
711 (int *) malloc(sizeof(int) * (tilec->x1 - tilec->x0) *
712 (tilec->y1 - tilec->y0));
713 tilec->numresolutions = tccp->numresolutions;
715 (tcd_resolution_t *) malloc(tilec->numresolutions *
716 sizeof(tcd_resolution_t));
717 for (resno = 0; resno < tilec->numresolutions; resno++) {
719 int levelno = tilec->numresolutions - 1 - resno;
720 int tlprcxstart, tlprcystart, brprcxend, brprcyend;
721 int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend;
722 int cbgwidthexpn, cbgheightexpn;
723 int cblkwidthexpn, cblkheightexpn;
724 tcd_resolution_t *res = &tilec->resolutions[resno];
726 /* border for each resolution level (global) */
727 res->x0 = int_ceildivpow2(tilec->x0, levelno);
728 res->y0 = int_ceildivpow2(tilec->y0, levelno);
729 res->x1 = int_ceildivpow2(tilec->x1, levelno);
730 res->y1 = int_ceildivpow2(tilec->y1, levelno);
731 /* special for DWT */
732 res->previous_x0 = int_ceildivpow2(previous_x0, levelno);
733 res->previous_y0 = int_ceildivpow2(previous_y0, levelno);
734 res->previous_x1 = int_ceildivpow2(previous_x1, levelno);
735 res->previous_y1 = int_ceildivpow2(previous_y1, levelno);
741 res->numbands = resno == 0 ? 1 : 3;
742 /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */
743 if (tccp->csty & J2K_CCP_CSTY_PRT) {
744 pdx = tccp->prcw[resno];
745 pdy = tccp->prch[resno];
750 /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
751 tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx;
752 tlprcystart = int_floordivpow2(res->y0, pdy) << pdy;
753 brprcxend = int_ceildivpow2(res->x1, pdx) << pdx;
754 brprcyend = int_ceildivpow2(res->y1, pdy) << pdy;
755 res->pw = (brprcxend - tlprcxstart) >> pdx;
756 res->ph = (brprcyend - tlprcystart) >> pdy;
759 tlcbgxstart = tlprcxstart;
760 tlcbgystart = tlprcystart;
761 brcbgxend = brprcxend;
762 brcbgyend = brprcyend;
766 tlcbgxstart = int_ceildivpow2(tlprcxstart, 1);
767 tlcbgystart = int_ceildivpow2(tlprcystart, 1);
768 brcbgxend = int_ceildivpow2(brprcxend, 1);
769 brcbgyend = int_ceildivpow2(brprcyend, 1);
770 cbgwidthexpn = pdx - 1;
771 cbgheightexpn = pdy - 1;
774 cblkwidthexpn = int_min(tccp->cblkw, cbgwidthexpn);
775 cblkheightexpn = int_min(tccp->cblkh, cbgheightexpn);
777 for (bandno = 0; bandno < res->numbands; bandno++) {
781 tcd_band_t *band = &res->bands[bandno];
782 band->bandno = resno == 0 ? 0 : bandno + 1;
783 x0b = (band->bandno == 1) || (band->bandno == 3) ? 1 : 0;
784 y0b = (band->bandno == 2) || (band->bandno == 3) ? 1 : 0;
786 if (band->bandno == 0) {
787 /* band border (global) */
788 band->x0 = int_ceildivpow2(tilec->x0, levelno);
789 band->y0 = int_ceildivpow2(tilec->y0, levelno);
790 band->x1 = int_ceildivpow2(tilec->x1, levelno);
791 band->y1 = int_ceildivpow2(tilec->y1, levelno);
793 /* band border (global) */
795 int_ceildivpow2(tilec->x0 - (1 << levelno) * x0b,
798 int_ceildivpow2(tilec->y0 - (1 << levelno) * y0b,
801 int_ceildivpow2(tilec->x1 - (1 << levelno) * x0b,
804 int_ceildivpow2(tilec->y1 - (1 << levelno) * y0b,
809 &tccp->stepsizes[resno ==
810 0 ? 0 : 3 * (resno - 1) + bandno + 1];
813 0 ? dwt_getgain_real(band->bandno) : dwt_getgain(band->bandno);
814 numbps = img->comps[compno].prec + gain;
816 (int) floor((1.0 + ss->mant / 2048.0) *
817 pow(2.0, numbps - ss->expn) * 8192.0);
818 band->numbps = ss->expn + tccp->numgbits - 1; /* WHY -1 ? */
821 (tcd_precinct_t *) malloc(res->pw * res->ph *
822 sizeof(tcd_precinct_t));
824 for (precno = 0; precno < res->pw * res->ph; precno++) {
825 int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
827 tlcbgxstart + (precno % res->pw) * (1 << cbgwidthexpn);
829 tlcbgystart + (precno / res->pw) * (1 << cbgheightexpn);
830 int cbgxend = cbgxstart + (1 << cbgwidthexpn);
831 int cbgyend = cbgystart + (1 << cbgheightexpn);
832 tcd_precinct_t *prc = &band->precincts[precno];
833 /* precinct size (global) */
834 prc->x0 = int_max(cbgxstart, band->x0);
835 prc->y0 = int_max(cbgystart, band->y0);
836 prc->x1 = int_min(cbgxend, band->x1);
837 prc->y1 = int_min(cbgyend, band->y1);
840 int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn;
842 int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn;
844 int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn;
846 int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn;
847 prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;
848 prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn;
851 (tcd_cblk_t *) malloc(prc->cw * prc->ch *
854 prc->incltree = tgt_create(prc->cw, prc->ch);
855 prc->imsbtree = tgt_create(prc->cw, prc->ch);
857 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
859 tlcblkxstart + (cblkno % prc->cw) * (1 << cblkwidthexpn);
861 tlcblkystart + (cblkno / prc->cw) * (1 << cblkheightexpn);
862 int cblkxend = cblkxstart + (1 << cblkwidthexpn);
863 int cblkyend = cblkystart + (1 << cblkheightexpn);
864 tcd_cblk_t *cblk = &prc->cblks[cblkno];
865 /* code-block size (global) */
866 cblk->x0 = int_max(cblkxstart, prc->x0);
867 cblk->y0 = int_max(cblkystart, prc->y0);
868 cblk->x1 = int_min(cblkxend, prc->x1);
869 cblk->y1 = int_min(cblkyend, prc->y1);
876 /* tcd_dump(&tcd_image,0); */
879 void tcd_makelayer_fixed(int layno, int matrice_data[2][5][3], int final)
881 int compno, resno, bandno, precno, cblkno;
882 int vector[3], matrice[2][5][3];
883 int sum_mat, i, j, k;
884 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
885 tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
886 for (i = 0; i < 2; i++) {
887 for (j = 0; j < 5; j++) {
888 for (k = 0; k < 3; k++) {
890 (int) (matrice_data[i][j][k] *
891 (float) (tcd_img->comps[compno].prec / 16.0));
895 for (resno = 0; resno < tilec->numresolutions; resno++) {
896 tcd_resolution_t *res = &tilec->resolutions[resno];
897 for (bandno = 0; bandno < res->numbands; bandno++) {
898 tcd_band_t *band = &res->bands[bandno];
899 for (precno = 0; precno < res->pw * res->ph; precno++) {
900 tcd_precinct_t *prc = &band->precincts[precno];
901 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
902 tcd_cblk_t *cblk = &prc->cblks[cblkno];
903 tcd_layer_t *layer = &cblk->layers[layno];
905 int imsb = tcd_img->comps[compno].prec - cblk->numbps; /* number of bit-plan equal to zero */
906 /* Correction of the matrix of coefficient to include the IMSB information */
909 for (i = 0; i < layno + 1; i++) {
911 if (i != tcd_tcp->numlayers) { /* -1 must add to have a lossless last quality layer */
912 /* different quality (layer) */
913 vector[bandno] = matrice[i][resno][bandno];
914 sum_mat += vector[bandno];
916 /* Full quality (layer) */
917 vector[bandno] = tcd_img->comps[compno].prec - sum_mat;
920 mat_old = vector[bandno];
922 if (imsb >= vector[bandno]) {
925 vector[bandno] -= imsb;
931 cblk->numpassesinlayers = 0;
933 n = cblk->numpassesinlayers;
934 if (cblk->numpassesinlayers == 0) {
935 if (vector[bandno] != 0) {
936 n = 3 * vector[bandno] - 2 + cblk->numpassesinlayers;
938 n = cblk->numpassesinlayers;
942 n = 3 * vector[bandno] + cblk->numpassesinlayers;
945 layer->numpasses = n - cblk->numpassesinlayers;
947 if (!layer->numpasses) {
950 if (cblk->numpassesinlayers == 0) {
951 layer->len = cblk->passes[n - 1].rate;
952 layer->data = cblk->data;
955 cblk->passes[n - 1].rate -
956 cblk->passes[cblk->numpassesinlayers - 1].rate;
958 cblk->data + cblk->passes[cblk->numpassesinlayers -
962 cblk->numpassesinlayers = n;
972 void tcd_rateallocate_fixed(unsigned char *dest, int len,
973 info_image * info_IM)
976 int matrice[2][5][3] = { {{16, 0, 0},
989 for (layno = 0; layno < tcd_tcp->numlayers; layno++) {
990 tcd_makelayer_fixed(layno, matrice, 1);
994 void tcd_makelayer(int layno, double thresh, int final)
996 int compno, resno, bandno, precno, cblkno, passno;
997 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
998 tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
999 for (resno = 0; resno < tilec->numresolutions; resno++) {
1000 tcd_resolution_t *res = &tilec->resolutions[resno];
1001 for (bandno = 0; bandno < res->numbands; bandno++) {
1002 tcd_band_t *band = &res->bands[bandno];
1003 for (precno = 0; precno < res->pw * res->ph; precno++) {
1004 tcd_precinct_t *prc = &band->precincts[precno];
1005 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
1006 tcd_cblk_t *cblk = &prc->cblks[cblkno];
1007 tcd_layer_t *layer = &cblk->layers[layno];
1011 cblk->numpassesinlayers = 0;
1013 n = cblk->numpassesinlayers;
1014 for (passno = cblk->numpassesinlayers;
1015 passno < cblk->totalpasses; passno++) {
1018 tcd_pass_t *pass = &cblk->passes[passno];
1021 dd = pass->distortiondec;
1023 dr = pass->rate - cblk->passes[n - 1].rate;
1025 pass->distortiondec - cblk->passes[n - 1].distortiondec;
1032 if (dd / dr > thresh)
1035 layer->numpasses = n - cblk->numpassesinlayers;
1036 if (!layer->numpasses)
1038 if (cblk->numpassesinlayers == 0) {
1039 layer->len = cblk->passes[n - 1].rate;
1040 layer->data = cblk->data;
1041 layer->disto = cblk->passes[n - 1].distortiondec;
1044 cblk->passes[n - 1].rate -
1045 cblk->passes[cblk->numpassesinlayers - 1].rate;
1047 cblk->data + cblk->passes[cblk->numpassesinlayers -
1050 cblk->passes[n - 1].distortiondec -
1051 cblk->passes[cblk->numpassesinlayers - 1].distortiondec;
1055 cblk->numpassesinlayers = n;
1063 void tcd_rateallocate(unsigned char *dest, int len, info_image * info_IM)
1065 int compno, resno, bandno, precno, cblkno, passno, layno;
1070 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
1071 tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
1072 for (resno = 0; resno < tilec->numresolutions; resno++) {
1073 tcd_resolution_t *res = &tilec->resolutions[resno];
1074 for (bandno = 0; bandno < res->numbands; bandno++) {
1075 tcd_band_t *band = &res->bands[bandno];
1076 for (precno = 0; precno < res->pw * res->ph; precno++) {
1077 tcd_precinct_t *prc = &band->precincts[precno];
1078 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
1079 tcd_cblk_t *cblk = &prc->cblks[cblkno];
1080 for (passno = 0; passno < cblk->totalpasses; passno++) {
1081 tcd_pass_t *pass = &cblk->passes[passno];
1086 dd = pass->distortiondec;
1088 dr = pass->rate - cblk->passes[passno - 1].rate;
1090 pass->distortiondec - cblk->passes[passno -
1097 if (rdslope < min) {
1100 if (rdslope > max) {
1110 for (layno = 0; layno < tcd_tcp->numlayers; layno++) {
1111 volatile double lo = min;
1112 volatile double hi = max;
1113 volatile int success = 0;
1114 volatile int maxlen = int_min(tcd_tcp->rates[layno], len);
1115 volatile double goodthresh;
1116 volatile int goodlen;
1119 for (i = 0; i < 32; i++) {
1120 volatile double thresh = (lo + hi) / 2;
1123 tcd_makelayer(layno, thresh, 0);
1126 t2_encode_packets(tcd_img, tcd_cp, tcd_tileno, tcd_tile, layno + 1,
1127 dest, maxlen, info_IM);
1128 /* fprintf(stderr, "rate alloc: len=%d, max=%d\n", l, maxlen); */
1136 goodthresh = thresh;
1141 longjmp(j2k_error, 1);
1143 tcd_makelayer(layno, goodthresh, 1);
1147 int tcd_encode_tile_pxm(int tileno, unsigned char *dest, int len,
1148 info_image * info_IM)
1152 clock_t time7; /* , time2, time3, time4, time5, time6, time1; */
1154 j2k_tcp_t *tcp = &tcd_cp->tcps[0];
1155 j2k_tccp_t *tccp = &tcp->tccps[0];
1157 tcd_tileno = tileno;
1158 tcd_tile = tcd_image.tiles;
1159 tcd_tcp = &tcd_cp->tcps[tileno];
1161 /* INDEX >> "Precinct_nb_X et Precinct_nb_Y" */
1162 if (info_IM->index_on) {
1163 tcd_tilecomp_t *tilec_idx = &tile->comps[0]; /* old parser version */
1164 tcd_resolution_t *res_idx = &tilec_idx->resolutions[0]; /* old parser version */
1166 info_IM->tile[tileno].pw = res_idx->pw;
1167 info_IM->tile[tileno].ph = res_idx->ph;
1169 info_IM->pw = res_idx->pw; /* old parser version */
1170 info_IM->ph = res_idx->ph; /* old parser version */
1171 info_IM->pdx = 1 << tccp->prcw[tccp->numresolutions - 1];
1172 info_IM->pdy = 1 << tccp->prch[tccp->numresolutions - 1];
1176 /*---------------TILE-------------------*/
1179 /* time1=clock(); */
1181 for (compno = 0; compno < tile->numcomps; compno++) {
1188 tcd_tilecomp_t *tilec = &tile->comps[compno];
1190 tcd_img->comps[compno].sgnd ? 0 : 1 << (tcd_img->comps[compno].prec -
1192 int offset_x, offset_y;
1194 offset_x = int_ceildiv(tcd_img->x0, tcd_img->comps[compno].dx);
1195 offset_y = int_ceildiv(tcd_img->y0, tcd_img->comps[compno].dy);
1196 tw = tilec->x1 - tilec->x0;
1197 w = int_ceildiv(tcd_img->x1 - tcd_img->x0, tcd_img->comps[compno].dx);
1198 sprintf(tmp, "Compo%d", compno); /* component file */
1199 src = fopen(tmp, "rb");
1201 fprintf(stderr, "failed to open %s for reading\n", tmp);
1205 /* read the Compo file to extract data of the tile */
1207 fseek(src, (tilec->x0 - offset_x) + (tilec->y0 - offset_y) * w,
1209 k = (tilec->x0 - offset_x) + (tilec->y0 - offset_y) * w;
1210 for (j = tilec->y0; j < tilec->y1; j++) {
1211 for (i = tilec->x0; i < tilec->x1; i++) {
1212 if (tcd_tcp->tccps[compno].qmfbid == 1) {
1214 tilec->data[i - tilec->x0 + (j - tilec->y0) * tw] =
1217 } else if (tcd_tcp->tccps[compno].qmfbid == 0) {
1219 tilec->data[i - tilec->x0 + (j - tilec->y0) * tw] =
1220 (elmt - adjust) << 13;
1224 fseek(src, (tilec->x0 - offset_x) + (j + 1 - offset_y) * w - k,
1226 k = tilec->x0 - offset_x + (j + 1 - offset_y) * w;
1232 /* time1=clock()-time1; */
1233 /* printf("tile encoding times:\n"); */
1234 /* printf("img->tile: %d.%.3d s\n", time1/CLOCKS_PER_SEC, (time1%CLOCKS_PER_SEC)*1000/CLOCKS_PER_SEC); */
1236 /*----------------MCT-------------------*/
1238 /* time2=clock(); */
1240 if (tcd_tcp->tccps[0].qmfbid == 0) {
1241 mct_encode_real(tile->comps[0].data, tile->comps[1].data,
1242 tile->comps[2].data,
1243 (tile->comps[0].x1 -
1244 tile->comps[0].x0) * (tile->comps[0].y1 -
1245 tile->comps[0].y0));
1247 mct_encode(tile->comps[0].data, tile->comps[1].data,
1248 tile->comps[2].data,
1249 (tile->comps[0].x1 -
1250 tile->comps[0].x0) * (tile->comps[0].y1 -
1251 tile->comps[0].y0));
1254 /* time2=clock()-time2; */
1255 /* printf("mct: %ld.%.3ld s\n", time2/CLOCKS_PER_SEC, (time2%CLOCKS_PER_SEC)*1000/CLOCKS_PER_SEC); */
1257 /*----------------DWT---------------------*/
1259 /* time3=clock(); */
1260 for (compno = 0; compno < tile->numcomps; compno++) {
1261 tcd_tilecomp_t *tilec = &tile->comps[compno];
1262 if (tcd_tcp->tccps[compno].qmfbid == 1) {
1263 dwt_encode(tilec->data, tilec->x1 - tilec->x0, tilec->y1 - tilec->y0,
1264 tilec, tilec->numresolutions - 1);
1265 } else if (tcd_tcp->tccps[compno].qmfbid == 0) {
1266 dwt_encode_real(tilec->data, tilec->x1 - tilec->x0,
1267 tilec->y1 - tilec->y0, tilec,
1268 tilec->numresolutions - 1);
1271 /* time3=clock()-time3; */
1272 /* printf("dwt: %ld.%.3ld s\n", time3/CLOCKS_PER_SEC, (time3%CLOCKS_PER_SEC)*1000/CLOCKS_PER_SEC); */
1274 /*------------------TIER1-----------------*/
1276 /* time4=clock(); */
1278 t1_encode_cblks(tile, tcd_tcp);
1279 /* time4=clock()-time4; */
1280 /* printf("tier 1: %ld.%.3ld s\n", time4/CLOCKS_PER_SEC, (time4%CLOCKS_PER_SEC)*1000/CLOCKS_PER_SEC); */
1282 /*-----------RATE-ALLOCATE------------------*/
1284 info_IM->index_write = 0; /* INDEX */
1285 /* time5=clock(); */
1286 if (tcd_cp->disto_alloc)
1287 /* Normal Rate/distortion allocation */
1288 tcd_rateallocate(dest, len, info_IM);
1290 /* Fixed layer allocation */
1291 tcd_rateallocate_fixed(dest, len, info_IM);
1293 /* time5=clock()-time5; */
1294 /* printf("ratealloc: %ld.%.3ld s\n", time5/CLOCKS_PER_SEC, (time5%CLOCKS_PER_SEC)*1000/CLOCKS_PER_SEC); */
1296 /*--------------TIER2------------------*/
1298 info_IM->index_write = 1; /* INDEX */
1299 /* time6=clock(); */
1301 t2_encode_packets(tcd_img, tcd_cp, tileno, tile, tcd_tcp->numlayers,
1302 dest, len, info_IM);
1303 /* time6=clock()-time6; */
1304 /* printf("tier 2: %ld.%.3ld s\n", time6/CLOCKS_PER_SEC, (time6%CLOCKS_PER_SEC)*1000/CLOCKS_PER_SEC); */
1306 /*---------------CLEAN-------------------*/
1308 time7 = clock() - time7;
1309 printf("total: %ld.%.3ld s\n", time7 / CLOCKS_PER_SEC,
1310 (time7 % CLOCKS_PER_SEC) * 1000 / CLOCKS_PER_SEC);
1312 /* cleaning memory */
1313 for (compno = 0; compno < tile->numcomps; compno++) {
1314 tilec = &tile->comps[compno];
1321 int tcd_encode_tile_pgx(int tileno, unsigned char *dest, int len,
1322 info_image * info_IM)
1326 clock_t time7; /* , time2, time3, time4, time5, time6, time1; */
1328 j2k_tcp_t *tcp = &tcd_cp->tcps[0];
1329 j2k_tccp_t *tccp = &tcp->tccps[0];
1331 tcd_tileno = tileno;
1332 tcd_tile = tcd_image.tiles;
1333 tcd_tcp = &tcd_cp->tcps[tileno];
1335 /* INDEX >> "Precinct_nb_X et Precinct_nb_Y" */
1336 if (info_IM->index_on) {
1337 tcd_tilecomp_t *tilec_idx = &tile->comps[0];
1338 tcd_resolution_t *res_idx = &tilec_idx->resolutions[0];
1339 info_IM->tile[tileno].pw = res_idx->pw;
1340 info_IM->tile[tileno].ph = res_idx->ph;
1341 info_IM->pw = res_idx->pw; /* old parser version */
1342 info_IM->ph = res_idx->ph; /* old parser version */
1343 info_IM->pdx = 1 << tccp->prcw[tccp->numresolutions - 1];
1344 info_IM->pdy = 1 << tccp->prch[tccp->numresolutions - 1];
1347 /*---------------TILE-------------------*/
1349 /* time1=clock(); */
1350 for (compno = 0; compno < tile->numcomps; compno++) {
1357 tcd_tilecomp_t *tilec = &tile->comps[compno];
1359 tcd_img->comps[compno].sgnd ? 0 : 1 << (tcd_img->comps[compno].prec -
1361 int offset_x, offset_y;
1363 offset_x = int_ceildiv(tcd_img->x0, tcd_img->comps[compno].dx);
1364 offset_y = int_ceildiv(tcd_img->y0, tcd_img->comps[compno].dy);
1365 tw = tilec->x1 - tilec->x0;
1366 w = int_ceildiv(tcd_img->x1 - tcd_img->x0, tcd_img->comps[compno].dx);
1367 sprintf(tmp, "bandtile%d", tileno / tcd_cp->tw + 1); /* bandtile file opening */
1368 src = fopen(tmp, "rb");
1370 fprintf(stderr, "failed to open %s for reading\n", tmp);
1373 /* Extract data from bandtile file limited to the current tile */
1375 while (k < tilec->x0 - offset_x) {
1377 fscanf(src, "%d", &elmt);
1380 for (j = 0; j < tilec->y1 - tilec->y0; j++) {
1381 for (i = tilec->x0; i < tilec->x1; i++) {
1382 if (tcd_tcp->tccps[compno].qmfbid == 1) {
1383 fscanf(src, "%d", &elmt);
1384 tilec->data[i - tilec->x0 + (j) * tw] = elmt - adjust;
1386 } else if (tcd_tcp->tccps[compno].qmfbid == 0) {
1387 fscanf(src, "%d", &elmt);
1388 tilec->data[i - tilec->x0 + (j) * tw] = (elmt - adjust) << 13;
1392 while (k < tilec->x0 - offset_x + (j + 1) * w) {
1394 fscanf(src, "%d", &elmt);
1400 /* time1=clock()-time1; */
1401 /* printf("tile encoding times:\n"); */
1402 /* printf("img->tile: %d.%.3d s\n", time1/CLOCKS_PER_SEC, (time1%CLOCKS_PER_SEC)*1000/CLOCKS_PER_SEC); */
1403 /*----------------MCT-------------------*/
1404 /* time2=clock(); */
1406 if (tcd_tcp->tccps[0].qmfbid == 0) {
1407 mct_encode_real(tile->comps[0].data, tile->comps[1].data,
1408 tile->comps[2].data,
1409 (tile->comps[0].x1 -
1410 tile->comps[0].x0) * (tile->comps[0].y1 -
1411 tile->comps[0].y0));
1413 mct_encode(tile->comps[0].data, tile->comps[1].data,
1414 tile->comps[2].data,
1415 (tile->comps[0].x1 -
1416 tile->comps[0].x0) * (tile->comps[0].y1 -
1417 tile->comps[0].y0));
1421 /* time2=clock()-time2; */
1422 /* printf("mct: %d.%.3d s\n", time2/CLOCKS_PER_SEC, (time2%CLOCKS_PER_SEC)*1000/CLOCKS_PER_SEC); */
1424 /*----------------DWT---------------------*/
1425 /* time3=clock(); */
1426 for (compno = 0; compno < tile->numcomps; compno++) {
1427 tcd_tilecomp_t *tilec = &tile->comps[compno];
1428 if (tcd_tcp->tccps[compno].qmfbid == 1) {
1429 dwt_encode(tilec->data, tilec->x1 - tilec->x0, tilec->y1 - tilec->y0,
1430 tilec, tilec->numresolutions - 1);
1431 } else if (tcd_tcp->tccps[compno].qmfbid == 0) {
1432 dwt_encode_real(tilec->data, tilec->x1 - tilec->x0,
1433 tilec->y1 - tilec->y0, tilec,
1434 tilec->numresolutions - 1);
1437 /* time3=clock()-time3; */
1438 /* printf("dwt: %d.%.3d s\n", time3/CLOCKS_PER_SEC, (time3%CLOCKS_PER_SEC)*1000/CLOCKS_PER_SEC); */
1440 /*------------------TIER1-----------------*/
1441 /* time4=clock(); */
1443 t1_encode_cblks(tile, tcd_tcp);
1444 /* time4=clock()-time4; */
1445 /* printf("tier 1: %d.%.3d s\n", time4/CLOCKS_PER_SEC, (time4%CLOCKS_PER_SEC)*1000/CLOCKS_PER_SEC); */
1447 /*-----------RATE-ALLOCATE------------------*/
1448 info_IM->index_write = 0; /* INDEX */
1450 /* time5=clock(); */
1451 tcd_rateallocate(dest, len, info_IM);
1452 /* time5=clock()-time5; */
1453 /* printf("ratealloc: %d.%.3d s\n", time5/CLOCKS_PER_SEC, (time5%CLOCKS_PER_SEC)*1000/CLOCKS_PER_SEC); */
1455 /*--------------TIER2------------------*/
1456 info_IM->index_write = 1; /* INDEX */
1458 /* time6=clock(); */
1460 t2_encode_packets(tcd_img, tcd_cp, tileno, tile, tcd_tcp->numlayers,
1461 dest, len, info_IM);
1462 /* time6=clock()-time6; */
1463 /* printf("tier 2: %d.%.3d s\n", time6/CLOCKS_PER_SEC, (time6%CLOCKS_PER_SEC)*1000/CLOCKS_PER_SEC); */
1465 /*---------------CLEAN-------------------*/
1466 time7 = clock() - time7;
1467 printf("total: %ld.%.3ld s\n", time7 / CLOCKS_PER_SEC,
1468 (time7 % CLOCKS_PER_SEC) * 1000 / CLOCKS_PER_SEC);
1470 for (compno = 0; compno < tile->numcomps; compno++) {
1471 tilec = &tile->comps[compno];
1479 int tcd_decode_tile(unsigned char *src, int len, int tileno)
1484 clock_t time1, time2, time3, time4, time5, time6;
1487 tcd_tileno = tileno;
1488 tcd_tile = &tcd_image.tiles[tileno];
1489 tcd_tcp = &tcd_cp->tcps[tileno];
1496 l = t2_decode_packets(src, len, tcd_img, tcd_cp, tileno, tile);
1499 fprintf(stderr, "tcd_decode: incomplete bistream\n");
1501 time1 = clock() - time1;
1502 printf("tile decoding time %d/%d:\n", tileno + 1,
1503 tcd_cp->tw * tcd_cp->th);
1504 /* printf("tier 2: %ld.%.3ld s\n", time1/CLOCKS_PER_SEC, (time1%CLOCKS_PER_SEC)*1000/CLOCKS_PER_SEC); */
1508 t1_decode_cblks(tile, tcd_tcp);
1509 time2 = clock() - time2;
1510 /* printf("tier 1: %ld.%.3ld s\n", time2/CLOCKS_PER_SEC, (time2%CLOCKS_PER_SEC)*1000/CLOCKS_PER_SEC); */
1513 for (compno = 0; compno < tile->numcomps; compno++) {
1514 tcd_tilecomp_t *tilec = &tile->comps[compno];
1515 if (tcd_tcp->tccps[compno].qmfbid == 1) {
1516 if (tileno % tcd_cp->tw) {
1517 tcd_tile_t *tile_row = &tcd_image.tiles[tileno - 1];
1518 tcd_tilecomp_t *tilec_row = &tile_row->comps[compno];
1519 if (tileno / tcd_cp->tw) {
1520 tcd_tile_t *tile_col = &tcd_image.tiles[tileno - 1];
1521 tcd_tilecomp_t *tilec_col = &tile_col->comps[compno];
1522 dwt_decode(tilec->data, tilec->x1 - tilec->x0,
1523 tilec->y1 - tilec->y0, tilec,
1524 tilec->numresolutions - 1, tilec_row, tilec_col);
1526 dwt_decode(tilec->data, tilec->x1 - tilec->x0,
1527 tilec->y1 - tilec->y0, tilec,
1528 tilec->numresolutions - 1, tilec_row, tilec);
1530 if (tileno / tcd_cp->tw) {
1531 tcd_tile_t *tile_col = &tcd_image.tiles[tileno - 1];
1532 tcd_tilecomp_t *tilec_col = &tile_col->comps[compno];
1533 dwt_decode(tilec->data, tilec->x1 - tilec->x0,
1534 tilec->y1 - tilec->y0, tilec,
1535 tilec->numresolutions - 1, tilec, tilec_col);
1537 dwt_decode(tilec->data, tilec->x1 - tilec->x0,
1538 tilec->y1 - tilec->y0, tilec,
1539 tilec->numresolutions - 1, tilec, tilec);
1541 } else if (tcd_tcp->tccps[compno].qmfbid == 0) {
1542 if (tileno % tcd_cp->tw) {
1543 tcd_tile_t *tile_row = &tcd_image.tiles[tileno - 1];
1544 tcd_tilecomp_t *tilec_row = &tile_row->comps[compno];
1545 if (tileno / tcd_cp->tw) {
1546 tcd_tile_t *tile_col = &tcd_image.tiles[tileno - 1];
1547 tcd_tilecomp_t *tilec_col = &tile_col->comps[compno];
1548 dwt_decode_real(tilec->data, tilec->x1 - tilec->x0,
1549 tilec->y1 - tilec->y0, tilec,
1550 tilec->numresolutions - 1, tilec_row, tilec_col);
1552 dwt_decode_real(tilec->data, tilec->x1 - tilec->x0,
1553 tilec->y1 - tilec->y0, tilec,
1554 tilec->numresolutions - 1, tilec_row, tilec);
1556 if (tileno / tcd_cp->tw) {
1557 tcd_tile_t *tile_col = &tcd_image.tiles[tileno - 1];
1558 tcd_tilecomp_t *tilec_col = &tile_col->comps[compno];
1559 dwt_decode_real(tilec->data, tilec->x1 - tilec->x0,
1560 tilec->y1 - tilec->y0, tilec,
1561 tilec->numresolutions - 1, tilec, tilec_col);
1563 dwt_decode_real(tilec->data, tilec->x1 - tilec->x0,
1564 tilec->y1 - tilec->y0, tilec,
1565 tilec->numresolutions - 1, tilec, tilec);
1569 time3 = clock() - time3;
1570 /* printf("dwt: %ld.%.3ld s\n", time3/CLOCKS_PER_SEC, (time3%CLOCKS_PER_SEC)*1000/CLOCKS_PER_SEC); */
1574 if (tcd_tcp->tccps[0].qmfbid == 0) {
1575 mct_decode_real(tile->comps[0].data, tile->comps[1].data,
1576 tile->comps[2].data,
1577 (tile->comps[0].x1 -
1578 tile->comps[0].x0) * (tile->comps[0].y1 -
1579 tile->comps[0].y0));
1581 mct_decode(tile->comps[0].data, tile->comps[1].data,
1582 tile->comps[2].data,
1583 (tile->comps[0].x1 -
1584 tile->comps[0].x0) * (tile->comps[0].y1 -
1585 tile->comps[0].y0));
1588 time4 = clock() - time4;
1589 /* printf("mct: %ld.%.3ld s\n", time4/CLOCKS_PER_SEC, (time4%CLOCKS_PER_SEC)*1000/CLOCKS_PER_SEC); */
1592 for (compno = 0; compno < tile->numcomps; compno++) {
1593 tcd_tilecomp_t *tilec = &tile->comps[compno];
1595 tcd_img->comps[compno].sgnd ? 0 : 1 << (tcd_img->comps[compno].prec -
1598 tcd_img->comps[compno].
1599 sgnd ? -(1 << (tcd_img->comps[compno].prec - 1)) : 0;
1601 tcd_img->comps[compno].
1602 sgnd ? (1 << (tcd_img->comps[compno].prec - 1)) - 1
1603 : (1 << tcd_img->comps[compno].prec) - 1;
1604 int tw = tilec->x1 - tilec->x0;
1606 int_ceildiv(tcd_img->x1 - tcd_img->x0, tcd_img->comps[compno].dx);
1608 int offset_x = int_ceildiv(tcd_img->x0, tcd_img->comps[compno].dx);
1609 int offset_y = int_ceildiv(tcd_img->y0, tcd_img->comps[compno].dy);
1611 for (j = tilec->y0; j < tilec->y1; j++) {
1612 for (i = tilec->x0; i < tilec->x1; i++) {
1614 if (tcd_tcp->tccps[compno].qmfbid == 1) {
1615 v = tilec->data[i - tilec->x0 + (j - tilec->y0) * tw];
1616 } else { /* if (tcd_tcp->tccps[compno].qmfbid == 0) */
1617 v = tilec->data[i - tilec->x0 + (j - tilec->y0) * tw] >> 13;
1620 /* tcd_img->comps[compno].data[i+j*w]=int_clamp(v, min, max); */
1621 tcd_img->comps[compno].data[(i - offset_x) + (j - offset_y) * w] = int_clamp(v, min, max); /* change ! */
1625 time5 = clock() - time5;
1626 /* printf("tile->img: %ld.%.3ld s\n", time5/CLOCKS_PER_SEC, (time5%CLOCKS_PER_SEC)*1000/CLOCKS_PER_SEC); */
1628 time6 = clock() - time6;
1629 printf("total: %ld.%.3ld s\n\n", time6 / CLOCKS_PER_SEC,
1630 (time6 % CLOCKS_PER_SEC) * 1000 / CLOCKS_PER_SEC);
1633 longjmp(j2k_error, 1);