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 static tcd_tile_t *tcd_tile;
46 static j2k_tcp_t *tcd_tcp;
47 static int tcd_tileno;
49 extern jmp_buf j2k_error;
51 void tcd_init(j2k_image_t *img, j2k_cp_t *cp, info_image_t *imgg) {
52 int tileno, compno, resno, bandno, precno, cblkno;
57 tcd_image.tiles=(tcd_tile_t*)malloc(cp->tw*cp->th*sizeof(tcd_tile_t));
58 for (tileno=0; tileno<cp->tw*cp->th; tileno++) {
59 j2k_tcp_t *tcp=&cp->tcps[tileno];
60 tcd_tile_t *tile=&tcd_image.tiles[tileno];
61 // cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000)
62 int p=tileno%cp->tw; // si numerotation matricielle ..
63 int q=tileno/cp->tw; // .. coordonnees de la tile (q,p) q pour ligne et p pour colonne
64 info_tile_t *tile_Idx=&imgg->tile[tileno]; // INDEX
66 // 4 borders of the tile rescale on the image if necessary
67 tile->x0=int_max(cp->tx0+p*cp->tdx, img->x0);
68 tile->y0=int_max(cp->ty0+q*cp->tdy, img->y0);
69 tile->x1=int_min(cp->tx0+(p+1)*cp->tdx, img->x1);
70 tile->y1=int_min(cp->ty0+(q+1)*cp->tdy, img->y1);
72 tile->numcomps=img->numcomps;
73 tile->comps=(tcd_tilecomp_t*)malloc(img->numcomps*sizeof(tcd_tilecomp_t));
74 tile_Idx->compo=(info_compo_t*)malloc(img->numcomps*sizeof(info_compo_t)); // INDEX
75 for (compno=0; compno<tile->numcomps; compno++) {
76 j2k_tccp_t *tccp=&tcp->tccps[compno];
77 tcd_tilecomp_t *tilec=&tile->comps[compno];
78 info_compo_t *compo_Idx=&tile_Idx->compo[compno]; // INDEX
80 // border of each tile component (global)
81 tilec->x0=int_ceildiv(tile->x0, img->comps[compno].dx);
82 tilec->y0=int_ceildiv(tile->y0, img->comps[compno].dy);
83 tilec->x1=int_ceildiv(tile->x1, img->comps[compno].dx);
84 tilec->y1=int_ceildiv(tile->y1, img->comps[compno].dy);
86 tilec->data=(int*)malloc(sizeof(int)*(tilec->x1-tilec->x0)*(tilec->y1-tilec->y0));
87 tilec->numresolutions=tccp->numresolutions;
88 tilec->resolutions=(tcd_resolution_t*)malloc(tilec->numresolutions*sizeof(tcd_resolution_t));
89 compo_Idx->reso=(info_reso_t*)malloc(tilec->numresolutions*sizeof(info_reso_t)); // INDEX
90 for (resno=0; resno<tilec->numresolutions; resno++) {
92 int levelno=tilec->numresolutions-1-resno;
93 int tlprcxstart, tlprcystart, brprcxend, brprcyend;
94 int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend;
95 int cbgwidthexpn, cbgheightexpn;
96 int cblkwidthexpn, cblkheightexpn;
97 tcd_resolution_t *res=&tilec->resolutions[resno];
98 info_reso_t *res_Idx=&compo_Idx->reso[resno]; // INDEX
99 int precno_Idx; // INDEX
101 // border for each resolution level (global)
102 res->x0=int_ceildivpow2(tilec->x0, levelno);
103 res->y0=int_ceildivpow2(tilec->y0, levelno);
104 res->x1=int_ceildivpow2(tilec->x1, levelno);
105 res->y1=int_ceildivpow2(tilec->y1, levelno);
107 res->numbands=resno==0?1:3;
108 // p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000)
109 if (tccp->csty&J2K_CCP_CSTY_PRT) {
110 pdx=tccp->prcw[resno];
111 pdy=tccp->prch[resno];
116 // p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000)
117 tlprcxstart=int_floordivpow2(res->x0, pdx)<<pdx;
118 tlprcystart=int_floordivpow2(res->y0, pdy)<<pdy;
119 brprcxend=int_ceildivpow2(res->x1, pdx)<<pdx;
120 brprcyend=int_ceildivpow2(res->y1, pdy)<<pdy;
121 res->pw=(brprcxend-tlprcxstart)>>pdx;
122 res->ph=(brprcyend-tlprcystart)>>pdy;
125 imgg->tile[tileno].pw=res->pw;
126 imgg->tile[tileno].ph=res->ph;
128 res_Idx->prec=(info_prec_t*)malloc(res->pw*res->ph*sizeof(info_prec_t));
129 for (precno_Idx=0;precno_Idx<res->pw*res->ph;precno_Idx++)
131 info_prec_t *prec_Idx = &res_Idx->prec[precno_Idx];
132 prec_Idx->layer=(info_layer_t*)malloc(imgg->Layer*sizeof(info_layer_t));
135 imgg->pw=res->pw; // old parser version
136 imgg->ph=res->ph; // old parser version
142 tlcbgxstart=tlprcxstart;
143 tlcbgystart=tlprcystart;
149 tlcbgxstart=int_ceildivpow2(tlprcxstart, 1);
150 tlcbgystart=int_ceildivpow2(tlprcystart, 1);
151 brcbgxend=int_ceildivpow2(brprcxend, 1);
152 brcbgyend=int_ceildivpow2(brprcyend, 1);
157 cblkwidthexpn=int_min(tccp->cblkw, cbgwidthexpn);
158 cblkheightexpn=int_min(tccp->cblkh, cbgheightexpn);
160 for (bandno=0; bandno<res->numbands; bandno++) {
162 tcd_band_t *band=&res->bands[bandno];
163 band->bandno=resno==0?0:bandno+1;
164 x0b=(band->bandno==1)||(band->bandno==3)?1:0;
165 y0b=(band->bandno==2)||(band->bandno==3)?1:0;
167 if (band->bandno==0) {
168 // band border (global)
169 band->x0=int_ceildivpow2(tilec->x0, levelno);
170 band->y0=int_ceildivpow2(tilec->y0, levelno);
171 band->x1=int_ceildivpow2(tilec->x1, levelno);
172 band->y1=int_ceildivpow2(tilec->y1, levelno);
174 // band border (global)
175 band->x0=int_ceildivpow2(tilec->x0-(1<<levelno)*x0b, levelno+1);
176 band->y0=int_ceildivpow2(tilec->y0-(1<<levelno)*y0b, levelno+1);
177 band->x1=int_ceildivpow2(tilec->x1-(1<<levelno)*x0b, levelno+1);
178 band->y1=int_ceildivpow2(tilec->y1-(1<<levelno)*y0b, levelno+1);
181 band->precincts=(tcd_precinct_t*)malloc(res->pw*res->ph*sizeof(tcd_precinct_t));
183 for (precno=0; precno<res->pw*res->ph; precno++) {
184 int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
185 int cbgxstart=tlcbgxstart+(precno%res->pw)*(1<<cbgwidthexpn);
186 int cbgystart=tlcbgystart+(precno/res->pw)*(1<<cbgheightexpn);
187 int cbgxend=cbgxstart+(1<<cbgwidthexpn);
188 int cbgyend=cbgystart+(1<<cbgheightexpn);
189 tcd_precinct_t *prc=&band->precincts[precno];
190 // precinct size (global)
191 prc->x0=int_max(cbgxstart, band->x0);
192 prc->y0=int_max(cbgystart, band->y0);
193 prc->x1=int_min(cbgxend, band->x1);
194 prc->y1=int_min(cbgyend, band->y1);
196 tlcblkxstart=int_floordivpow2(prc->x0, cblkwidthexpn)<<cblkwidthexpn;
197 tlcblkystart=int_floordivpow2(prc->y0, cblkheightexpn)<<cblkheightexpn;
198 brcblkxend=int_ceildivpow2(prc->x1, cblkwidthexpn)<<cblkwidthexpn;
199 brcblkyend=int_ceildivpow2(prc->y1, cblkheightexpn)<<cblkheightexpn;
200 prc->cw=(brcblkxend-tlcblkxstart)>>cblkwidthexpn;
201 prc->ch=(brcblkyend-tlcblkystart)>>cblkheightexpn;
203 prc->cblks=(tcd_cblk_t*)malloc(prc->cw*prc->ch*sizeof(tcd_cblk_t));
205 prc->incltree=tgt_create(prc->cw, prc->ch);
206 prc->imsbtree=tgt_create(prc->cw, prc->ch);
208 for (cblkno=0; cblkno<prc->cw*prc->ch; cblkno++) {
209 int cblkxstart=tlcblkxstart+(cblkno%prc->cw)*(1<<cblkwidthexpn);
210 int cblkystart=tlcblkystart+(cblkno/prc->cw)*(1<<cblkheightexpn);
211 int cblkxend=cblkxstart+(1<<cblkwidthexpn);
212 int cblkyend=cblkystart+(1<<cblkheightexpn);
213 tcd_cblk_t *cblk=&prc->cblks[cblkno];
214 // code-block size (global)
215 cblk->x0=int_max(cblkxstart, prc->x0);
216 cblk->y0=int_max(cblkystart, prc->y0);
217 cblk->x1=int_min(cblkxend, prc->x1);
218 cblk->y1=int_min(cblkyend, prc->y1);
228 void tcd_free(j2k_image_t *img, j2k_cp_t *cp) {
229 int tileno, compno, resno, bandno, precno;
234 for (tileno=0; tileno<tcd_image.tw*tcd_image.th; tileno++)
236 // j2k_tcp_t *tcp=&cp->tcps[curtileno];
237 tcd_tile_t *tile=&tcd_image.tiles[tileno];
238 for (compno=0; compno<tile->numcomps; compno++)
240 tcd_tilecomp_t *tilec=&tile->comps[compno];
241 for (resno=0; resno<tilec->numresolutions; resno++)
243 tcd_resolution_t *res=&tilec->resolutions[resno];
244 for (bandno=0; bandno<res->numbands; bandno++)
246 tcd_band_t *band=&res->bands[bandno];
247 for (precno=0; precno<res->pw*res->ph; precno++)
249 tcd_precinct_t *prc=&band->precincts[precno];
251 if (prc->incltree!=NULL)
252 tgt_destroy(prc->incltree);
253 if (prc->imsbtree!=NULL)
254 tgt_destroy(prc->imsbtree);
257 free(band->precincts);
260 free(tilec->resolutions);
264 free(tcd_image.tiles);
268 int tcd_decode_tile(unsigned char *src, int len, int tileno, info_image_t *imgg) {
274 tcd_tile=&tcd_image.tiles[tileno];
275 tcd_tcp=&tcd_cp->tcps[tileno];
278 l=t2_decode_packets(src, len, tcd_img, tcd_cp, tileno, tile, imgg);
283 fprintf(stderr, "tcd_decode: incomplete bistream\n");
287 longjmp(j2k_error, 1);