2 * Copyright (c) 2001-2002, David Janssens
3 * Copyright (c) 2003, Yannick Verschueren
4 * Copyright (c) 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.
40 static tcd_image_t tcd_image;
42 static j2k_image_t *tcd_img;
43 static j2k_cp_t *tcd_cp;
45 extern jmp_buf j2k_error;
47 void tcd_init(j2k_image_t *img, j2k_cp_t *cp, info_image_t *imgg) {
48 int tileno, compno, resno, bandno, precno, cblkno;
53 tcd_image.tiles=(tcd_tile_t*)malloc(cp->tw*cp->th*sizeof(tcd_tile_t));
54 for (tileno=0; tileno<cp->tw*cp->th; tileno++) {
55 j2k_tcp_t *tcp=&cp->tcps[tileno];
56 tcd_tile_t *tile=&tcd_image.tiles[tileno];
57 // cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000)
58 int p=tileno%cp->tw; // si numerotation matricielle ..
59 int q=tileno/cp->tw; // .. coordonnees de la tile (q,p) q pour ligne et p pour colonne
60 info_tile_t *tile_Idx=&imgg->tile[tileno]; // INDEX
62 // 4 borders of the tile rescale on the image if necessary
63 tile->x0=int_max(cp->tx0+p*cp->tdx, img->x0);
64 tile->y0=int_max(cp->ty0+q*cp->tdy, img->y0);
65 tile->x1=int_min(cp->tx0+(p+1)*cp->tdx, img->x1);
66 tile->y1=int_min(cp->ty0+(q+1)*cp->tdy, img->y1);
68 tile->numcomps=img->numcomps;
69 tile->comps=(tcd_tilecomp_t*)malloc(img->numcomps*sizeof(tcd_tilecomp_t));
70 tile_Idx->compo=(info_compo_t*)malloc(img->numcomps*sizeof(info_compo_t)); // INDEX
71 for (compno=0; compno<tile->numcomps; compno++) {
72 j2k_tccp_t *tccp=&tcp->tccps[compno];
73 tcd_tilecomp_t *tilec=&tile->comps[compno];
74 info_compo_t *compo_Idx=&tile_Idx->compo[compno]; // INDEX
76 // border of each tile component (global)
77 tilec->x0=int_ceildiv(tile->x0, img->comps[compno].dx);
78 tilec->y0=int_ceildiv(tile->y0, img->comps[compno].dy);
79 tilec->x1=int_ceildiv(tile->x1, img->comps[compno].dx);
80 tilec->y1=int_ceildiv(tile->y1, img->comps[compno].dy);
82 tilec->data=(int*)malloc(sizeof(int)*(tilec->x1-tilec->x0)*(tilec->y1-tilec->y0));
83 tilec->numresolutions=tccp->numresolutions;
84 tilec->resolutions=(tcd_resolution_t*)malloc(tilec->numresolutions*sizeof(tcd_resolution_t));
85 compo_Idx->reso=(info_reso_t*)malloc(tilec->numresolutions*sizeof(info_reso_t)); // INDEX
86 for (resno=0; resno<tilec->numresolutions; resno++) {
88 int levelno=tilec->numresolutions-1-resno;
89 int tlprcxstart, tlprcystart, brprcxend, brprcyend;
90 int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend;
91 int cbgwidthexpn, cbgheightexpn;
92 int cblkwidthexpn, cblkheightexpn;
93 tcd_resolution_t *res=&tilec->resolutions[resno];
94 info_reso_t *res_Idx=&compo_Idx->reso[resno]; // INDEX
95 int precno_Idx; // INDEX
97 // border for each resolution level (global)
98 res->x0=int_ceildivpow2(tilec->x0, levelno);
99 res->y0=int_ceildivpow2(tilec->y0, levelno);
100 res->x1=int_ceildivpow2(tilec->x1, levelno);
101 res->y1=int_ceildivpow2(tilec->y1, levelno);
103 res->numbands=resno==0?1:3;
104 // p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000)
105 if (tccp->csty&J2K_CCP_CSTY_PRT) {
106 pdx=tccp->prcw[resno];
107 pdy=tccp->prch[resno];
112 // p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000)
113 tlprcxstart=int_floordivpow2(res->x0, pdx)<<pdx;
114 tlprcystart=int_floordivpow2(res->y0, pdy)<<pdy;
115 brprcxend=int_ceildivpow2(res->x1, pdx)<<pdx;
116 brprcyend=int_ceildivpow2(res->y1, pdy)<<pdy;
117 res->pw=(brprcxend-tlprcxstart)>>pdx;
118 res->ph=(brprcyend-tlprcystart)>>pdy;
121 imgg->tile[tileno].pw=res->pw;
122 imgg->tile[tileno].ph=res->ph;
124 res_Idx->prec=(info_prec_t*)malloc(res->pw*res->ph*sizeof(info_prec_t));
125 for (precno_Idx=0;precno_Idx<res->pw*res->ph;precno_Idx++)
127 info_prec_t *prec_Idx = &res_Idx->prec[precno_Idx];
128 prec_Idx->layer=(info_layer_t*)malloc(imgg->Layer*sizeof(info_layer_t));
131 imgg->pw=res->pw; // old parser version
132 imgg->ph=res->ph; // old parser version
138 tlcbgxstart=tlprcxstart;
139 tlcbgystart=tlprcystart;
145 tlcbgxstart=int_ceildivpow2(tlprcxstart, 1);
146 tlcbgystart=int_ceildivpow2(tlprcystart, 1);
147 brcbgxend=int_ceildivpow2(brprcxend, 1);
148 brcbgyend=int_ceildivpow2(brprcyend, 1);
153 cblkwidthexpn=int_min(tccp->cblkw, cbgwidthexpn);
154 cblkheightexpn=int_min(tccp->cblkh, cbgheightexpn);
156 for (bandno=0; bandno<res->numbands; bandno++) {
158 tcd_band_t *band=&res->bands[bandno];
159 band->bandno=resno==0?0:bandno+1;
160 x0b=(band->bandno==1)||(band->bandno==3)?1:0;
161 y0b=(band->bandno==2)||(band->bandno==3)?1:0;
163 if (band->bandno==0) {
164 // band border (global)
165 band->x0=int_ceildivpow2(tilec->x0, levelno);
166 band->y0=int_ceildivpow2(tilec->y0, levelno);
167 band->x1=int_ceildivpow2(tilec->x1, levelno);
168 band->y1=int_ceildivpow2(tilec->y1, levelno);
170 // band border (global)
171 band->x0=int_ceildivpow2(tilec->x0-(1<<levelno)*x0b, levelno+1);
172 band->y0=int_ceildivpow2(tilec->y0-(1<<levelno)*y0b, levelno+1);
173 band->x1=int_ceildivpow2(tilec->x1-(1<<levelno)*x0b, levelno+1);
174 band->y1=int_ceildivpow2(tilec->y1-(1<<levelno)*y0b, levelno+1);
177 band->precincts=(tcd_precinct_t*)malloc(res->pw*res->ph*sizeof(tcd_precinct_t));
179 for (precno=0; precno<res->pw*res->ph; precno++) {
180 int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
181 int cbgxstart=tlcbgxstart+(precno%res->pw)*(1<<cbgwidthexpn);
182 int cbgystart=tlcbgystart+(precno/res->pw)*(1<<cbgheightexpn);
183 int cbgxend=cbgxstart+(1<<cbgwidthexpn);
184 int cbgyend=cbgystart+(1<<cbgheightexpn);
185 tcd_precinct_t *prc=&band->precincts[precno];
186 // precinct size (global)
187 prc->x0=int_max(cbgxstart, band->x0);
188 prc->y0=int_max(cbgystart, band->y0);
189 prc->x1=int_min(cbgxend, band->x1);
190 prc->y1=int_min(cbgyend, band->y1);
192 tlcblkxstart=int_floordivpow2(prc->x0, cblkwidthexpn)<<cblkwidthexpn;
193 tlcblkystart=int_floordivpow2(prc->y0, cblkheightexpn)<<cblkheightexpn;
194 brcblkxend=int_ceildivpow2(prc->x1, cblkwidthexpn)<<cblkwidthexpn;
195 brcblkyend=int_ceildivpow2(prc->y1, cblkheightexpn)<<cblkheightexpn;
196 prc->cw=(brcblkxend-tlcblkxstart)>>cblkwidthexpn;
197 prc->ch=(brcblkyend-tlcblkystart)>>cblkheightexpn;
199 prc->cblks=(tcd_cblk_t*)malloc(prc->cw*prc->ch*sizeof(tcd_cblk_t));
201 prc->incltree=tgt_create(prc->cw, prc->ch);
202 prc->imsbtree=tgt_create(prc->cw, prc->ch);
204 for (cblkno=0; cblkno<prc->cw*prc->ch; cblkno++) {
205 int cblkxstart=tlcblkxstart+(cblkno%prc->cw)*(1<<cblkwidthexpn);
206 int cblkystart=tlcblkystart+(cblkno/prc->cw)*(1<<cblkheightexpn);
207 int cblkxend=cblkxstart+(1<<cblkwidthexpn);
208 int cblkyend=cblkystart+(1<<cblkheightexpn);
209 tcd_cblk_t *cblk=&prc->cblks[cblkno];
210 // code-block size (global)
211 cblk->x0=int_max(cblkxstart, prc->x0);
212 cblk->y0=int_max(cblkystart, prc->y0);
213 cblk->x1=int_min(cblkxend, prc->x1);
214 cblk->y1=int_min(cblkyend, prc->y1);
224 void tcd_free(j2k_image_t *img, j2k_cp_t *cp) {
225 int tileno, compno, resno, bandno, precno;
230 for (tileno=0; tileno<tcd_image.tw*tcd_image.th; tileno++)
232 // j2k_tcp_t *tcp=&cp->tcps[curtileno];
233 tcd_tile_t *tile=&tcd_image.tiles[tileno];
234 for (compno=0; compno<tile->numcomps; compno++)
236 tcd_tilecomp_t *tilec=&tile->comps[compno];
237 for (resno=0; resno<tilec->numresolutions; resno++)
239 tcd_resolution_t *res=&tilec->resolutions[resno];
240 for (bandno=0; bandno<res->numbands; bandno++)
242 tcd_band_t *band=&res->bands[bandno];
243 for (precno=0; precno<res->pw*res->ph; precno++)
245 tcd_precinct_t *prc=&band->precincts[precno];
247 if (prc->incltree!=NULL)
248 tgt_destroy(prc->incltree);
249 if (prc->imsbtree!=NULL)
250 tgt_destroy(prc->imsbtree);
253 free(band->precincts);
256 free(tilec->resolutions);
260 free(tcd_image.tiles);
264 int tcd_decode_tile(unsigned char *src, int len, int tileno, info_image_t *imgg) {
269 tile = &tcd_image.tiles[tileno];
271 l = t2_decode_packets(src, len, tcd_img, tcd_cp, tileno, tile, imgg);
276 fprintf(stderr, "tcd_decode: incomplete bistream\n");
280 longjmp(j2k_error, 1);