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(sizeof(int) * (tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0));
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(sizeof(int) * (tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0));
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;
522 tcd_image.tw = cp->tw;
523 tcd_image.th = cp->th;
524 tcd_image.tiles = (tcd_tile_t *) malloc(cp->tw * cp->th * sizeof(tcd_tile_t));
525 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
526 j2k_tcp_t *tcp = &cp->tcps[tileno];
527 tcd_tile_t *tile = &tcd_image.tiles[tileno];
528 // int previous_x0, previous_x1, previous_y0, previous_y1;
529 /* cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
530 int p = tileno % cp->tw; /* si numerotation matricielle .. */
531 int q = tileno / cp->tw; /* .. coordonnees de la tile (q,p) q pour ligne et p pour colonne */
533 /* 4 borders of the tile rescale on the image if necessary */
534 tile->x0 = int_max(cp->tx0 + p * cp->tdx, img->x0);
535 tile->y0 = int_max(cp->ty0 + q * cp->tdy, img->y0);
536 tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, img->x1);
537 tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, img->y1);
539 tile->numcomps = img->numcomps;
540 tile->comps = (tcd_tilecomp_t *) malloc(img->numcomps * sizeof(tcd_tilecomp_t));
541 for (compno = 0; compno < tile->numcomps; compno++) {
542 j2k_tccp_t *tccp = &tcp->tccps[compno];
543 tcd_tilecomp_t *tilec = &tile->comps[compno];
544 /* border of each tile component (global) */
545 tilec->x0 = int_ceildiv(tile->x0, img->comps[compno].dx);
546 tilec->y0 = int_ceildiv(tile->y0, img->comps[compno].dy);
547 tilec->x1 = int_ceildiv(tile->x1, img->comps[compno].dx);
548 tilec->y1 = int_ceildiv(tile->y1, img->comps[compno].dy);
550 tilec->data = (int *) malloc(sizeof(int) * (tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0));
551 tilec->numresolutions = tccp->numresolutions;
552 tilec->resolutions = (tcd_resolution_t *) malloc(tilec->numresolutions * sizeof(tcd_resolution_t));
553 for (resno = 0; resno < tilec->numresolutions; resno++) {
555 int levelno = tilec->numresolutions - 1 - resno;
556 int tlprcxstart, tlprcystart, brprcxend, brprcyend;
557 int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend;
558 int cbgwidthexpn, cbgheightexpn;
559 int cblkwidthexpn, cblkheightexpn;
560 tcd_resolution_t *res = &tilec->resolutions[resno];
562 /* border for each resolution level (global) */
563 res->x0 = int_ceildivpow2(tilec->x0, levelno);
564 res->y0 = int_ceildivpow2(tilec->y0, levelno);
565 res->x1 = int_ceildivpow2(tilec->x1, levelno);
566 res->y1 = int_ceildivpow2(tilec->y1, levelno);
568 res->numbands = resno == 0 ? 1 : 3;
569 /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */
570 if (tccp->csty & J2K_CCP_CSTY_PRT) {
571 pdx = tccp->prcw[resno];
572 pdy = tccp->prch[resno];
577 /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
578 tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx;
579 tlprcystart = int_floordivpow2(res->y0, pdy) << pdy;
580 brprcxend = int_ceildivpow2(res->x1, pdx) << pdx;
581 brprcyend = int_ceildivpow2(res->y1, pdy) << pdy;
582 res->pw = (brprcxend - tlprcxstart) >> pdx;
583 res->ph = (brprcyend - tlprcystart) >> pdy;
586 tlcbgxstart = tlprcxstart;
587 tlcbgystart = tlprcystart;
588 brcbgxend = brprcxend;
589 brcbgyend = brprcyend;
593 tlcbgxstart = int_ceildivpow2(tlprcxstart, 1);
594 tlcbgystart = int_ceildivpow2(tlprcystart, 1);
595 brcbgxend = int_ceildivpow2(brprcxend, 1);
596 brcbgyend = int_ceildivpow2(brprcyend, 1);
597 cbgwidthexpn = pdx - 1;
598 cbgheightexpn = pdy - 1;
601 cblkwidthexpn = int_min(tccp->cblkw, cbgwidthexpn);
602 cblkheightexpn = int_min(tccp->cblkh, cbgheightexpn);
604 for (bandno = 0; bandno < res->numbands; bandno++) {
608 tcd_band_t *band = &res->bands[bandno];
609 band->bandno = resno == 0 ? 0 : bandno + 1;
610 x0b = (band->bandno == 1) || (band->bandno == 3) ? 1 : 0;
611 y0b = (band->bandno == 2) || (band->bandno == 3) ? 1 : 0;
613 if (band->bandno == 0) {
614 /* band border (global) */
615 band->x0 = int_ceildivpow2(tilec->x0, levelno);
616 band->y0 = int_ceildivpow2(tilec->y0, levelno);
617 band->x1 = int_ceildivpow2(tilec->x1, levelno);
618 band->y1 = int_ceildivpow2(tilec->y1, levelno);
620 /* band border (global) */
621 band->x0 = int_ceildivpow2(tilec->x0 - (1 << levelno) * x0b, levelno + 1);
622 band->y0 = int_ceildivpow2(tilec->y0 - (1 << levelno) * y0b, levelno + 1);
623 band->x1 = int_ceildivpow2(tilec->x1 - (1 << levelno) * x0b, levelno + 1);
624 band->y1 = int_ceildivpow2(tilec->y1 - (1 << levelno) * y0b, levelno + 1);
627 ss = &tccp->stepsizes[resno == 0 ? 0 : 3 * (resno - 1) + bandno + 1];
628 gain = tccp->qmfbid == 0 ? dwt_getgain_real(band->bandno) : dwt_getgain(band->bandno);
629 numbps = img->comps[compno].prec + gain;
630 band->stepsize = (int) floor((1.0 + ss->mant / 2048.0) * pow(2.0, numbps - ss->expn) * 8192.0);
631 band->numbps = ss->expn + tccp->numgbits - 1; /* WHY -1 ? */
633 band->precincts = (tcd_precinct_t *) malloc(res->pw * res->ph * sizeof(tcd_precinct_t));
635 for (precno = 0; precno < res->pw * res->ph; precno++) {
636 int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
637 int cbgxstart = tlcbgxstart + (precno % res->pw) * (1 << cbgwidthexpn);
638 int cbgystart = tlcbgystart + (precno / res->pw) * (1 << cbgheightexpn);
639 int cbgxend = cbgxstart + (1 << cbgwidthexpn);
640 int cbgyend = cbgystart + (1 << cbgheightexpn);
641 tcd_precinct_t *prc = &band->precincts[precno];
642 /* precinct size (global) */
643 prc->x0 = int_max(cbgxstart, band->x0);
644 prc->y0 = int_max(cbgystart, band->y0);
645 prc->x1 = int_min(cbgxend, band->x1);
646 prc->y1 = int_min(cbgyend, band->y1);
648 tlcblkxstart = int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn;
649 tlcblkystart = int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn;
650 brcblkxend = int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn;
651 brcblkyend = int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn;
652 prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;
653 prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn;
655 prc->cblks = (tcd_cblk_t *) malloc(prc->cw * prc->ch * sizeof(tcd_cblk_t));
657 prc->incltree = tgt_create(prc->cw, prc->ch);
658 prc->imsbtree = tgt_create(prc->cw, prc->ch);
660 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
661 int cblkxstart = tlcblkxstart + (cblkno % prc->cw) * (1 << cblkwidthexpn);
662 int cblkystart = tlcblkystart + (cblkno / prc->cw) * (1 << cblkheightexpn);
663 int cblkxend = cblkxstart + (1 << cblkwidthexpn);
664 int cblkyend = cblkystart + (1 << cblkheightexpn);
665 tcd_cblk_t *cblk = &prc->cblks[cblkno];
666 /* code-block size (global) */
667 cblk->x0 = int_max(cblkxstart, prc->x0);
668 cblk->y0 = int_max(cblkystart, prc->y0);
669 cblk->x1 = int_min(cblkxend, prc->x1);
670 cblk->y1 = int_min(cblkyend, prc->y1);
677 /* tcd_dump(&tcd_image,0); */
680 void tcd_makelayer_fixed(int layno, int final) {
681 int compno, resno, bandno, precno, cblkno;
682 int value; //, matrice[tcd_tcp->numlayers][tcd_tile->comps[0].numresolutions][3];
683 int matrice[10][10][3];
686 /*matrice=(int*)malloc(tcd_tcp->numlayers*tcd_tile->comps[0].numresolutions*3*sizeof(int));*/
688 for (compno=0; compno<tcd_tile->numcomps; compno++)
690 tcd_tilecomp_t *tilec=&tcd_tile->comps[compno];
691 for (i=0;i<tcd_tcp->numlayers;i++) {
692 for (j=0;j<tilec->numresolutions;j++) {
694 matrice[i][j][k]=(int)(tcd_cp->matrice[i*tilec->numresolutions*3+j*3+k]*(float)(tcd_img->comps[compno].prec/16.0));}}}
696 for (resno=0; resno<tilec->numresolutions; resno++) {
697 tcd_resolution_t *res=&tilec->resolutions[resno];
698 for (bandno=0; bandno<res->numbands; bandno++) {
699 tcd_band_t *band=&res->bands[bandno];
700 for (precno=0; precno<res->pw*res->ph; precno++) {
701 tcd_precinct_t *prc=&band->precincts[precno];
702 for (cblkno=0; cblkno<prc->cw*prc->ch; cblkno++) {
703 tcd_cblk_t *cblk=&prc->cblks[cblkno];
704 tcd_layer_t *layer=&cblk->layers[layno];
706 int imsb=tcd_img->comps[compno].prec-cblk->numbps; /* number of bit-plan equal to zero */
707 /* Correction of the matrix of coefficient to include the IMSB information */
711 value=matrice[layno][resno][bandno];
718 value=matrice[layno][resno][bandno]-matrice[layno-1][resno][bandno];
719 if (imsb>=matrice[layno-1][resno][bandno])
721 value-=(imsb-matrice[layno-1][resno][bandno]);
722 if (value<0) value=0;
727 cblk->numpassesinlayers=0;
729 n=cblk->numpassesinlayers;
730 if (cblk->numpassesinlayers==0)
733 n=3*value-2+cblk->numpassesinlayers;
735 n=cblk->numpassesinlayers;
737 n=3*value+cblk->numpassesinlayers;
739 layer->numpasses=n-cblk->numpassesinlayers;
741 if (!layer->numpasses)
744 if (cblk->numpassesinlayers==0)
746 layer->len=cblk->passes[n-1].rate;
747 layer->data=cblk->data;
750 layer->len=cblk->passes[n-1].rate-cblk->passes[cblk->numpassesinlayers-1].rate;
751 layer->data=cblk->data+cblk->passes[cblk->numpassesinlayers-1].rate;
754 cblk->numpassesinlayers=n;
762 void tcd_rateallocate_fixed() {
765 for (layno=0; layno<tcd_tcp->numlayers; layno++)
767 tcd_makelayer_fixed(layno, 1);
771 void tcd_makelayer(int layno, double thresh, int final)
773 int compno, resno, bandno, precno, cblkno, passno;
774 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
775 tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
776 for (resno = 0; resno < tilec->numresolutions; resno++) {
777 tcd_resolution_t *res = &tilec->resolutions[resno];
778 for (bandno = 0; bandno < res->numbands; bandno++) {
779 tcd_band_t *band = &res->bands[bandno];
780 for (precno = 0; precno < res->pw * res->ph; precno++) {
781 tcd_precinct_t *prc = &band->precincts[precno];
782 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
783 tcd_cblk_t *cblk = &prc->cblks[cblkno];
784 tcd_layer_t *layer = &cblk->layers[layno];
788 cblk->numpassesinlayers = 0;
790 n = cblk->numpassesinlayers;
791 for (passno = cblk->numpassesinlayers;
792 passno < cblk->totalpasses; passno++) {
795 tcd_pass_t *pass = &cblk->passes[passno];
798 dd = pass->distortiondec;
800 dr = pass->rate - cblk->passes[n - 1].rate;
801 dd = pass->distortiondec - cblk->passes[n - 1].distortiondec;
808 if (dd / dr > thresh)
811 layer->numpasses = n - cblk->numpassesinlayers;
812 if (!layer->numpasses)
814 if (cblk->numpassesinlayers == 0) {
815 layer->len = cblk->passes[n - 1].rate;
816 layer->data = cblk->data;
817 layer->disto = cblk->passes[n - 1].distortiondec;
819 layer->len = cblk->passes[n - 1].rate -
820 cblk->passes[cblk->numpassesinlayers - 1].rate;
821 layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate;
822 layer->disto = cblk->passes[n - 1].distortiondec -
823 cblk->passes[cblk->numpassesinlayers - 1].distortiondec;
827 cblk->numpassesinlayers = n;
835 void tcd_rateallocate(unsigned char *dest, int len, info_image * info_IM)
837 int compno, resno, bandno, precno, cblkno, passno, layno;
842 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
843 tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
844 for (resno = 0; resno < tilec->numresolutions; resno++) {
845 tcd_resolution_t *res = &tilec->resolutions[resno];
846 for (bandno = 0; bandno < res->numbands; bandno++) {
847 tcd_band_t *band = &res->bands[bandno];
848 for (precno = 0; precno < res->pw * res->ph; precno++) {
849 tcd_precinct_t *prc = &band->precincts[precno];
850 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
851 tcd_cblk_t *cblk = &prc->cblks[cblkno];
852 for (passno = 0; passno < cblk->totalpasses; passno++) {
853 tcd_pass_t *pass = &cblk->passes[passno];
858 dd = pass->distortiondec;
860 dr = pass->rate - cblk->passes[passno - 1].rate;
861 dd = pass->distortiondec - cblk->passes[passno - 1].distortiondec;
879 if (info_IM->index_on) { /* Threshold for Marcela Index */
880 info_IM->tile[tcd_tileno].thresh=(double*)malloc(tcd_tcp->numlayers*sizeof(double));
882 for (layno = 0; layno < tcd_tcp->numlayers; layno++) {
883 volatile double lo = min;
884 volatile double hi = max;
885 volatile int success = 0;
886 volatile int maxlen = int_min(tcd_tcp->rates[layno], len);
887 volatile double goodthresh;
888 volatile int goodlen;
891 for (i = 0; i < 32; i++) {
892 volatile double thresh = (lo + hi) / 2;
895 tcd_makelayer(layno, thresh, 0);
897 l = t2_encode_packets(tcd_img, tcd_cp, tcd_tileno, tcd_tile, layno + 1, dest, maxlen, info_IM);
898 /* fprintf(stderr, "rate alloc: len=%d, max=%d\n", l, maxlen); */
911 longjmp(j2k_error, 1);
914 if (info_IM->index_on) { /* Threshold for Marcela Index */
915 info_IM->tile[tcd_tileno].thresh[layno]=goodthresh;
917 tcd_makelayer(layno, goodthresh, 1);
921 int tcd_encode_tile_pxm(int tileno, unsigned char *dest, int len, info_image * info_IM)
927 j2k_tcp_t *tcp = &tcd_cp->tcps[0];
928 j2k_tccp_t *tccp = &tcp->tccps[0];
931 tcd_tile = tcd_image.tiles;
932 tcd_tcp = &tcd_cp->tcps[tileno];
934 /* INDEX >> "Precinct_nb_X et Precinct_nb_Y" */
935 if (info_IM->index_on) {
936 tcd_tilecomp_t *tilec_idx = &tile->comps[0]; /* old parser version */
937 tcd_resolution_t *res_idx = &tilec_idx->resolutions[0]; /* old parser version */
939 info_IM->tile[tileno].pw = res_idx->pw;
940 info_IM->tile[tileno].ph = res_idx->ph;
942 info_IM->pw = res_idx->pw; /* old parser version */
943 info_IM->ph = res_idx->ph; /* old parser version */
944 info_IM->pdx = 1 << tccp->prcw[tccp->numresolutions - 1];
945 info_IM->pdy = 1 << tccp->prch[tccp->numresolutions - 1];
949 /*---------------TILE-------------------*/
953 for (compno = 0; compno < tile->numcomps; compno++) {
960 tcd_tilecomp_t *tilec = &tile->comps[compno];
961 int adjust = tcd_img->comps[compno].sgnd ? 0 : 1 << (tcd_img->comps[compno].prec - 1);
962 int offset_x, offset_y;
964 offset_x = int_ceildiv(tcd_img->x0, tcd_img->comps[compno].dx);
965 offset_y = int_ceildiv(tcd_img->y0, tcd_img->comps[compno].dy);
966 tw = tilec->x1 - tilec->x0;
967 w = int_ceildiv(tcd_img->x1 - tcd_img->x0, tcd_img->comps[compno].dx);
968 sprintf(tmp, "Compo%d", compno); /* component file */
969 src = fopen(tmp, "rb");
971 fprintf(stderr, "failed to open %s for reading\n", tmp);
975 /* read the Compo file to extract data of the tile */
977 fseek(src, (tilec->x0 - offset_x) + (tilec->y0 - offset_y) * w, SEEK_SET);
978 k = (tilec->x0 - offset_x) + (tilec->y0 - offset_y) * w;
979 for (j = tilec->y0; j < tilec->y1; j++) {
980 for (i = tilec->x0; i < tilec->x1; i++) {
981 if (tcd_tcp->tccps[compno].qmfbid == 1) {
983 tilec->data[i - tilec->x0 + (j - tilec->y0) * tw] = elmt - adjust;
985 } else if (tcd_tcp->tccps[compno].qmfbid == 0) {
987 tilec->data[i - tilec->x0 + (j - tilec->y0) * tw] = (elmt - adjust) << 13;
991 fseek(src, (tilec->x0 - offset_x) + (j + 1 - offset_y) * w - k, SEEK_CUR);
992 k = tilec->x0 - offset_x + (j + 1 - offset_y) * w;
998 /*----------------MCT-------------------*/
1001 if (tcd_tcp->tccps[0].qmfbid == 0) {
1002 mct_encode_real(tile->comps[0].data, tile->comps[1].data,tile->comps[2].data,
1003 (tile->comps[0].x1 - tile->comps[0].x0) * (tile->comps[0].y1 - tile->comps[0].y0));
1005 mct_encode(tile->comps[0].data, tile->comps[1].data, tile->comps[2].data,
1006 (tile->comps[0].x1 - tile->comps[0].x0) * (tile->comps[0].y1 - tile->comps[0].y0));
1009 /*----------------DWT---------------------*/
1011 /* time3=clock(); */
1012 for (compno = 0; compno < tile->numcomps; compno++) {
1013 tcd_tilecomp_t *tilec = &tile->comps[compno];
1014 if (tcd_tcp->tccps[compno].qmfbid == 1) {
1015 dwt_encode(tilec->data, tilec->x1 - tilec->x0, tilec->y1 - tilec->y0, tilec, tilec->numresolutions - 1);
1016 } else if (tcd_tcp->tccps[compno].qmfbid == 0) {
1017 dwt_encode_real(tilec->data, tilec->x1 - tilec->x0, tilec->y1 - tilec->y0, tilec, tilec->numresolutions - 1);
1020 /*------------------TIER1-----------------*/
1023 t1_encode_cblks(tile, tcd_tcp);
1025 /*-----------RATE-ALLOCATE------------------*/
1026 info_IM->index_write = 0; /* INDEX */
1028 if (tcd_cp->disto_alloc)
1029 /* Normal Rate/distortion allocation */
1030 tcd_rateallocate(dest, len, info_IM);
1032 /* Fixed layer allocation */
1033 tcd_rateallocate_fixed();
1035 /*--------------TIER2------------------*/
1036 info_IM->index_write = 1; /* INDEX */
1037 l = t2_encode_packets(tcd_img, tcd_cp, tileno, tile, tcd_tcp->numlayers, dest, len, info_IM);
1038 /*---------------CLEAN-------------------*/
1040 time7 = clock() - time7;
1041 printf("total: %ld.%.3ld s\n", time7 / CLOCKS_PER_SEC, (time7 % CLOCKS_PER_SEC) * 1000 / CLOCKS_PER_SEC);
1043 /* cleaning memory */
1044 for (compno = 0; compno < tile->numcomps; compno++) {
1045 tilec = &tile->comps[compno];
1052 int tcd_encode_tile_pgx(int tileno, unsigned char *dest, int len, info_image * info_IM)
1058 j2k_tcp_t *tcp = &tcd_cp->tcps[0];
1059 j2k_tccp_t *tccp = &tcp->tccps[0];
1061 tcd_tileno = tileno;
1062 tcd_tile = tcd_image.tiles;
1063 tcd_tcp = &tcd_cp->tcps[tileno];
1065 /* INDEX >> "Precinct_nb_X et Precinct_nb_Y" */
1066 if (info_IM->index_on) {
1067 tcd_tilecomp_t *tilec_idx = &tile->comps[0];
1068 tcd_resolution_t *res_idx = &tilec_idx->resolutions[0];
1069 info_IM->tile[tileno].pw = res_idx->pw;
1070 info_IM->tile[tileno].ph = res_idx->ph;
1071 info_IM->pw = res_idx->pw; /* old parser version */
1072 info_IM->ph = res_idx->ph; /* old parser version */
1073 info_IM->pdx = 1 << tccp->prcw[tccp->numresolutions - 1];
1074 info_IM->pdy = 1 << tccp->prch[tccp->numresolutions - 1];
1077 /*---------------TILE-------------------*/
1080 for (compno = 0; compno < tile->numcomps; compno++) {
1087 tcd_tilecomp_t *tilec = &tile->comps[compno];
1088 int adjust = tcd_img->comps[compno].sgnd ? 0 : 1 << (tcd_img->comps[compno].prec - 1);
1089 int offset_x, offset_y;
1091 offset_x = int_ceildiv(tcd_img->x0, tcd_img->comps[compno].dx);
1092 offset_y = int_ceildiv(tcd_img->y0, tcd_img->comps[compno].dy);
1093 tw = tilec->x1 - tilec->x0;
1094 w = int_ceildiv(tcd_img->x1 - tcd_img->x0, tcd_img->comps[compno].dx);
1095 sprintf(tmp, "bandtile%d", tileno / tcd_cp->tw + 1); /* bandtile file opening */
1096 src = fopen(tmp, "rb");
1098 fprintf(stderr, "failed to open %s for reading\n", tmp);
1101 /* Extract data from bandtile file limited to the current tile */
1103 while (k < tilec->x0 - offset_x) {
1105 fscanf(src, "%d", &elmt);
1108 for (j = 0; j < tilec->y1 - tilec->y0; j++) {
1109 for (i = tilec->x0; i < tilec->x1; i++) {
1110 if (tcd_tcp->tccps[compno].qmfbid == 1) {
1111 fscanf(src, "%d", &elmt);
1112 tilec->data[i - tilec->x0 + (j) * tw] = elmt - adjust;
1114 } else if (tcd_tcp->tccps[compno].qmfbid == 0) {
1115 fscanf(src, "%d", &elmt);
1116 tilec->data[i - tilec->x0 + (j) * tw] = (elmt - adjust) << 13;
1120 while (k < tilec->x0 - offset_x + (j + 1) * w) {
1122 fscanf(src, "%d", &elmt);
1128 /*----------------MCT-------------------*/
1131 if (tcd_tcp->tccps[0].qmfbid == 0) {
1132 mct_encode_real(tile->comps[0].data, tile->comps[1].data, tile->comps[2].data,
1133 (tile->comps[0].x1 - tile->comps[0].x0) * (tile->comps[0].y1 - tile->comps[0].y0));
1135 mct_encode(tile->comps[0].data, tile->comps[1].data, tile->comps[2].data,
1136 (tile->comps[0].x1 - tile->comps[0].x0) * (tile->comps[0].y1 - tile->comps[0].y0));
1140 /*----------------DWT---------------------*/
1142 for (compno = 0; compno < tile->numcomps; compno++) {
1143 tcd_tilecomp_t *tilec = &tile->comps[compno];
1144 if (tcd_tcp->tccps[compno].qmfbid == 1) {
1145 dwt_encode(tilec->data, tilec->x1 - tilec->x0, tilec->y1 - tilec->y0, tilec, tilec->numresolutions - 1);
1146 } else if (tcd_tcp->tccps[compno].qmfbid == 0) {
1147 dwt_encode_real(tilec->data, tilec->x1 - tilec->x0, tilec->y1 - tilec->y0, tilec, tilec->numresolutions - 1);
1151 /*------------------TIER1-----------------*/
1154 t1_encode_cblks(tile, tcd_tcp);
1156 /*-----------RATE-ALLOCATE------------------*/
1157 info_IM->index_write = 0; /* INDEX */
1159 info_IM->index_write=0; /* INDEX */
1161 if (tcd_cp->disto_alloc)
1162 /* Normal Rate/distortion allocation */
1163 tcd_rateallocate(dest, len, info_IM);
1165 /* Fixed layer allocation */
1166 tcd_rateallocate_fixed();
1168 /*--------------TIER2------------------*/
1169 info_IM->index_write = 1; /* INDEX */
1171 l = t2_encode_packets(tcd_img, tcd_cp, tileno, tile, tcd_tcp->numlayers, dest, len, info_IM);
1173 /*---------------CLEAN-------------------*/
1174 time7 = clock() - time7;
1175 printf("total: %ld.%.3ld s\n", time7 / CLOCKS_PER_SEC, (time7 % CLOCKS_PER_SEC) * 1000 / CLOCKS_PER_SEC);
1177 for (compno = 0; compno < tile->numcomps; compno++) {
1178 tilec = &tile->comps[compno];
1186 int tcd_decode_tile(unsigned char *src, int len, int tileno)
1191 clock_t time1, time2, time3, time4, time5, time6;
1194 tcd_tileno = tileno;
1195 tcd_tile = &tcd_image.tiles[tileno];
1196 tcd_tcp = &tcd_cp->tcps[tileno];
1202 printf("tile decoding time %d/%d:\n", tileno + 1, tcd_cp->tw * tcd_cp->th);
1204 l = t2_decode_packets(src, len, tcd_img, tcd_cp, tileno, tile);
1208 fprintf(stderr, "tcd_decode: incomplete bistream\n");
1210 time1 = clock() - time1;
1212 /* printf("tier 2: %ld.%.3ld s\n", time1/CLOCKS_PER_SEC, (time1%CLOCKS_PER_SEC)*1000/CLOCKS_PER_SEC); */
1216 t1_decode_cblks(tile, tcd_tcp);
1217 time2 = clock() - time2;
1218 /* printf("tier 1: %ld.%.3ld s\n", time2/CLOCKS_PER_SEC, (time2%CLOCKS_PER_SEC)*1000/CLOCKS_PER_SEC); */
1221 for (compno = 0; compno < tile->numcomps; compno++)
1223 tcd_tilecomp_t *tilec = &tile->comps[compno];
1224 if (tcd_tcp->tccps[compno].qmfbid == 1)
1226 dwt_decode(tilec->data, tilec->x1 - tilec->x0,tilec->y1 - tilec->y0, tilec, tilec->numresolutions - 1);
1228 { /*if (tcd_tcp->tccps[compno].qmfbid == 0) {*/
1229 dwt_decode_real(tilec->data, tilec->x1 - tilec->x0, tilec->y1 - tilec->y0, tilec, tilec->numresolutions - 1);
1233 time3 = clock() - time3;
1234 /* printf("dwt: %ld.%.3ld s\n", time3/CLOCKS_PER_SEC, (time3%CLOCKS_PER_SEC)*1000/CLOCKS_PER_SEC); */
1238 if (tcd_tcp->tccps[0].qmfbid == 0) {
1239 mct_decode_real(tile->comps[0].data, tile->comps[1].data, tile->comps[2].data,
1240 (tile->comps[0].x1 - tile->comps[0].x0) * (tile->comps[0].y1 - tile->comps[0].y0));
1242 mct_decode(tile->comps[0].data, tile->comps[1].data, tile->comps[2].data,
1243 (tile->comps[0].x1 - tile->comps[0].x0) * (tile->comps[0].y1 - tile->comps[0].y0));
1246 time4 = clock() - time4;
1247 /* printf("mct: %ld.%.3ld s\n", time4/CLOCKS_PER_SEC, (time4%CLOCKS_PER_SEC)*1000/CLOCKS_PER_SEC); */
1250 for (compno = 0; compno < tile->numcomps; compno++) {
1251 tcd_tilecomp_t *tilec = &tile->comps[compno];
1252 int adjust = tcd_img->comps[compno].sgnd ? 0 : 1 << (tcd_img->comps[compno].prec - 1);
1253 int min = tcd_img->comps[compno].sgnd ? -(1 << (tcd_img->comps[compno].prec - 1)) : 0;
1254 int max = tcd_img->comps[compno].sgnd ? (1 << (tcd_img->comps[compno].prec - 1)) - 1 : (1 << tcd_img->comps[compno].prec) - 1;
1255 int tw = tilec->x1 - tilec->x0;
1256 int w = int_ceildiv(tcd_img->x1 - tcd_img->x0, tcd_img->comps[compno].dx);
1258 int offset_x = int_ceildiv(tcd_img->x0, tcd_img->comps[compno].dx);
1259 int offset_y = int_ceildiv(tcd_img->y0, tcd_img->comps[compno].dy);
1261 for (j = tilec->y0; j < tilec->y1; j++) {
1262 for (i = tilec->x0; i < tilec->x1; i++) {
1264 if (tcd_tcp->tccps[compno].qmfbid == 1) {
1265 v = tilec->data[i - tilec->x0 + (j - tilec->y0) * tw];
1266 } else { /* if (tcd_tcp->tccps[compno].qmfbid == 0) */
1267 v = tilec->data[i - tilec->x0 + (j - tilec->y0) * tw] >> 13;
1271 /* tcd_img->comps[compno].data[i+j*w]=int_clamp(v, min, max); */
1272 tcd_img->comps[compno].data[(i - offset_x) + (j - offset_y) * w] = int_clamp(v, min, max); /* change ! */
1276 time5 = clock() - time5;
1277 /* printf("tile->img: %ld.%.3ld s\n", time5/CLOCKS_PER_SEC, (time5%CLOCKS_PER_SEC)*1000/CLOCKS_PER_SEC); */
1279 time6 = clock() - time6;
1280 printf("total: %ld.%.3ld s\n\n", time6 / CLOCKS_PER_SEC, (time6 % CLOCKS_PER_SEC) * 1000 / CLOCKS_PER_SEC);
1283 longjmp(j2k_error, 1);