Initial revision
[openjpeg.git] / indexer_JPIP / tcd.c
1 /*
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
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
15  *
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.
27  */
28
29 #include "tcd.h"
30 #include "int.h"
31 #include "t2.h"
32 #include <setjmp.h>
33 #include <float.h>
34 #include <stdio.h>
35 #include <time.h>
36 #include <math.h>
37 #include <stdlib.h>
38 #include <string.h>
39
40 static tcd_image_t tcd_image;
41
42 static j2k_image_t *tcd_img;
43 static j2k_cp_t *tcd_cp;
44
45 static tcd_tile_t *tcd_tile;
46 static j2k_tcp_t *tcd_tcp;
47 static int tcd_tileno;
48
49 extern jmp_buf j2k_error;
50
51 void tcd_init(j2k_image_t *img, j2k_cp_t *cp, info_image_t *imgg) {
52     int tileno, compno, resno, bandno, precno, cblkno;
53     tcd_img=img;
54     tcd_cp=cp;
55     tcd_image.tw=cp->tw;
56     tcd_image.th=cp->th;
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
65
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);
71         
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
79
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);
85             
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++) {
91                 int pdx, pdy;
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
100
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);
106                 
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];
112                 } else {
113                     pdx=15;
114                     pdy=15;
115                 }
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;
123
124                 // <INDEX>
125                 imgg->tile[tileno].pw=res->pw;
126                 imgg->tile[tileno].ph=res->ph;
127                 
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++)
130                   {
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));
133                   }
134                 
135                 imgg->pw=res->pw;  // old parser version
136                 imgg->ph=res->ph;  // old parser version
137                 imgg->pdx=1<<pdx;
138                 imgg->pdy=1<<pdy;
139                 // </INDEX>
140
141                 if (resno==0) {
142                     tlcbgxstart=tlprcxstart;
143                     tlcbgystart=tlprcystart;
144                     brcbgxend=brprcxend;
145                     brcbgyend=brprcyend;
146                     cbgwidthexpn=pdx;
147                     cbgheightexpn=pdy;
148                 } else {
149                     tlcbgxstart=int_ceildivpow2(tlprcxstart, 1);
150                     tlcbgystart=int_ceildivpow2(tlprcystart, 1);
151                     brcbgxend=int_ceildivpow2(brprcxend, 1);
152                     brcbgyend=int_ceildivpow2(brprcyend, 1);
153                     cbgwidthexpn=pdx-1;
154                     cbgheightexpn=pdy-1;
155                 }
156
157                 cblkwidthexpn=int_min(tccp->cblkw, cbgwidthexpn);
158                 cblkheightexpn=int_min(tccp->cblkh, cbgheightexpn);
159
160                 for (bandno=0; bandno<res->numbands; bandno++) {
161                     int x0b, y0b;
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;
166
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);
173                     } else {
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);
179                     }
180
181                     band->precincts=(tcd_precinct_t*)malloc(res->pw*res->ph*sizeof(tcd_precinct_t));
182
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);
195
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;
202
203                         prc->cblks=(tcd_cblk_t*)malloc(prc->cw*prc->ch*sizeof(tcd_cblk_t));
204
205                         prc->incltree=tgt_create(prc->cw, prc->ch);
206                         prc->imsbtree=tgt_create(prc->cw, prc->ch);
207
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);
219                         }
220                     }
221                 }
222             }
223         }
224     }
225 }
226
227
228 void tcd_free(j2k_image_t *img, j2k_cp_t *cp) {
229   int tileno, compno, resno, bandno, precno;
230   tcd_img=img;
231   tcd_cp=cp;
232   tcd_image.tw=cp->tw;
233   tcd_image.th=cp->th;
234   for (tileno=0; tileno<tcd_image.tw*tcd_image.th; tileno++) 
235     {
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++) 
239         {
240          tcd_tilecomp_t *tilec=&tile->comps[compno];
241           for (resno=0; resno<tilec->numresolutions; resno++) 
242             {
243               tcd_resolution_t *res=&tilec->resolutions[resno];
244               for (bandno=0; bandno<res->numbands; bandno++) 
245                 {
246                  tcd_band_t *band=&res->bands[bandno];
247                   for (precno=0; precno<res->pw*res->ph; precno++) 
248                     {
249                      tcd_precinct_t *prc=&band->precincts[precno];
250                       
251                       if (prc->incltree!=NULL)
252                         tgt_destroy(prc->incltree);
253                       if (prc->imsbtree!=NULL)
254                         tgt_destroy(prc->imsbtree);
255                       free(prc->cblks);
256                     } // for (precno
257                   free(band->precincts);
258                 } // for (bandno
259             }   // for (resno
260           free(tilec->resolutions);
261         }       // for (compno
262       free(tile->comps);
263     }   // for (tileno
264   free(tcd_image.tiles);
265 }
266
267
268 int tcd_decode_tile(unsigned char *src, int len, int tileno, info_image_t *imgg) {
269     int l;
270     int eof=0;
271
272     tcd_tile_t *tile;
273     tcd_tileno=tileno;
274     tcd_tile=&tcd_image.tiles[tileno];
275     tcd_tcp=&tcd_cp->tcps[tileno];
276     tile=tcd_tile;
277
278     l=t2_decode_packets(src, len, tcd_img, tcd_cp, tileno, tile, imgg);
279
280     if (l==-999)
281       {
282         eof=1;
283         fprintf(stderr, "tcd_decode: incomplete bistream\n");
284       }
285     
286      if (eof) {
287        longjmp(j2k_error, 1);
288      }
289
290     l=1;
291     return l;
292 }