openjpeg version 1.0 (previous version still available with tag opj0-97)
[openjpeg.git] / libopenjpeg / tcd.h
1 /*
2  * Copyright (c) 2001-2003, David Janssens
3  * Copyright (c) 2002-2003, Yannick Verschueren
4  * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe
5  * Copyright (c) 2005, Herv� Drolon, FreeImage Team
6  * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 #ifndef __TCD_H
32 #define __TCD_H
33 /**
34 @file tcd.h
35 @brief Implementation of a tile coder/decoder (TCD)
36
37 The functions in TCD.C have for goal to encode or decode each tile independently from
38 each other. The functions in TCD.C are used by some function in J2K.C.
39 */
40
41 /** @defgroup TCD TCD - Implementation of a tile coder/decoder */
42 /*@{*/
43
44 /**
45 FIXME: documentation
46 */
47 typedef struct opj_tcd_seg {
48   int numpasses;
49   int len;
50   unsigned char *data;
51   int maxpasses;
52   int numnewpasses;
53   int newlen;
54 } opj_tcd_seg_t;
55
56 /**
57 FIXME: documentation
58 */
59 typedef struct opj_tcd_pass {
60   int rate;
61   double distortiondec;
62   int term, len;
63 } opj_tcd_pass_t;
64
65 /**
66 FIXME: documentation
67 */
68 typedef struct opj_tcd_layer {
69   int numpasses;    /* Number of passes in the layer */
70   int len;      /* len of information */
71   double disto;     /* add for index (Cfr. Marcela) */
72   unsigned char *data;    /* data */
73 } opj_tcd_layer_t;
74
75 /**
76 FIXME: documentation
77 */
78 typedef struct opj_tcd_cblk {
79   int x0, y0, x1, y1;   /* dimension of the code-blocks : left upper corner (x0, y0) right low corner (x1,y1) */
80   int numbps;
81   int numlenbits;
82   int len;      /* length */
83   int numpasses;    /* number of pass already done for the code-blocks */
84   int numnewpasses;   /* number of pass added to the code-blocks */
85   int numsegs;      /* number of segments */
86   opj_tcd_seg_t segs[100];    /* segments informations */
87   unsigned char data[8192]; /* Data */
88   int numpassesinlayers;  /* number of passes in the layer */
89   opj_tcd_layer_t layers[100];  /* layer information */
90   int totalpasses;    /* total number of passes */
91   opj_tcd_pass_t passes[100]; /* information about the passes */
92 } opj_tcd_cblk_t;
93
94 /**
95 FIXME: documentation
96 */
97 typedef struct opj_tcd_precinct {
98   int x0, y0, x1, y1;   /* dimension of the precinct : left upper corner (x0, y0) right low corner (x1,y1) */
99   int cw, ch;     /* number of precinct in width and heigth */
100   opj_tcd_cblk_t *cblks;    /* code-blocks informations */
101   opj_tgt_tree_t *incltree;   /* inclusion tree */
102   opj_tgt_tree_t *imsbtree;   /* IMSB tree */
103 } opj_tcd_precinct_t;
104
105 /**
106 FIXME: documentation
107 */
108 typedef struct opj_tcd_band {
109   int x0, y0, x1, y1;   /* dimension of the subband : left upper corner (x0, y0) right low corner (x1,y1) */
110   int bandno;
111   opj_tcd_precinct_t *precincts;  /* precinct information */
112   int numbps;
113   float stepsize;
114 } opj_tcd_band_t;
115
116 /**
117 FIXME: documentation
118 */
119 typedef struct opj_tcd_resolution {
120   int x0, y0, x1, y1;   /* dimension of the resolution level : left upper corner (x0, y0) right low corner (x1,y1) */
121   int pw, ph;
122   int numbands;     /* number sub-band for the resolution level */
123   opj_tcd_band_t bands[3];    /* subband information */
124 } opj_tcd_resolution_t;
125
126 /**
127 FIXME: documentation
128 */
129 typedef struct opj_tcd_tilecomp {
130   int x0, y0, x1, y1;   /* dimension of component : left upper corner (x0, y0) right low corner (x1,y1) */
131   int numresolutions;   /* number of resolutions level */
132   opj_tcd_resolution_t *resolutions;  /* resolutions information */
133   int *data;      /* data of the component */
134   int nbpix;      /* add fixed_quality */
135 } opj_tcd_tilecomp_t;
136
137 /**
138 FIXME: documentation
139 */
140 typedef struct opj_tcd_tile {
141   int x0, y0, x1, y1;   /* dimension of the tile : left upper corner (x0, y0) right low corner (x1,y1) */
142   int numcomps;     /* number of components in tile */
143   opj_tcd_tilecomp_t *comps;  /* Components information */
144   int nbpix;      /* add fixed_quality */
145   double distotile;   /* add fixed_quality */
146   double distolayer[100]; /* add fixed_quality */
147 } opj_tcd_tile_t;
148
149 /**
150 FIXME: documentation
151 */
152 typedef struct opj_tcd_image {
153   int tw, th;     /* number of tiles in width and heigth */
154   opj_tcd_tile_t *tiles;    /* Tiles information */
155 } opj_tcd_image_t;
156
157 /**
158 Tile coder/decoder
159 */
160 typedef struct opj_tcd {
161   /** codec context */
162   opj_common_ptr cinfo;
163
164   /** info on each image tile */
165   opj_tcd_image_t *tcd_image;
166   /** image */
167   opj_image_t *image;
168   /** coding parameters */
169   opj_cp_t *cp;
170   /** pointer to the current encoded/decoded tile */
171   opj_tcd_tile_t *tcd_tile;
172   /** coding/decoding parameters common to all tiles */
173   opj_tcp_t *tcp;
174   /** current encoded/decoded tile */
175   int tcd_tileno;
176   /**@name working variables */
177   /*@{*/
178   opj_tcd_tile_t *tile;
179   opj_tcd_tilecomp_t *tilec;
180   opj_tcd_resolution_t *res;
181   opj_tcd_band_t *band;
182   opj_tcd_precinct_t *prc;
183   opj_tcd_cblk_t *cblk;
184   /*@}*/
185 } opj_tcd_t;
186
187 /** @name Exported functions */
188 /*@{*/
189 /* ----------------------------------------------------------------------- */
190
191 /**
192 Dump the content of a tcd structure
193 */
194 void tcd_dump(FILE *fd, opj_tcd_t *tcd, opj_tcd_image_t * img, int curtileno);
195 /**
196 Create a new TCD handle
197 @param cinfo Codec context info
198 @return Returns a new TCD handle if successful returns NULL otherwise
199 */
200 opj_tcd_t* tcd_create(opj_common_ptr cinfo);
201 /**
202 Destroy a previously created TCD handle
203 @param tcd TCD handle to destroy
204 */
205 void tcd_destroy(opj_tcd_t *tcd);
206 /**
207 Initialize the tile coder (allocate the memory)
208 @param tcd TCD handle
209 @param image Raw image
210 @param cp Coding parameters
211 @param curtileno Number that identifies the tile that will be encoded
212 */
213 void tcd_malloc_encode(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp, int curtileno);
214 /**
215 Free the memory allocated for encoding
216 @param tcd TCD handle
217 */
218 void tcd_free_encode(opj_tcd_t *tcd);
219 /**
220 Initialize the tile coder (reuses the memory allocated by tcd_malloc_encode)
221 @param tcd TCD handle
222 @param image Raw image
223 @param cp Coding parameters
224 @param curtileno Number that identifies the tile that will be encoded
225 */
226 void tcd_init_encode(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp, int curtileno);
227 /**
228 Initialize the tile decoder
229 @param tcd TCD handle
230 @param image Raw image
231 @param cp Coding parameters
232 */
233 void tcd_malloc_decode(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp);
234 void tcd_makelayer_fixed(opj_tcd_t *tcd, int layno, int final);
235 void tcd_rateallocate_fixed(opj_tcd_t *tcd);
236 void tcd_makelayer(opj_tcd_t *tcd, int layno, double thresh, int final);
237 bool tcd_rateallocate(opj_tcd_t *tcd, unsigned char *dest, int len, opj_image_info_t * image_info);
238 /**
239 Encode a tile from the raw image into a buffer
240 @param tcd TCD handle
241 @param tileno Number that identifies one of the tiles to be encoded
242 @param dest Destination buffer
243 @param len Length of destination buffer
244 @param image_info Creation of index file
245 @return 
246 */
247 int tcd_encode_tile(opj_tcd_t *tcd, int tileno, unsigned char *dest, int len, opj_image_info_t * image_info);
248 /**
249 Decode a tile from a buffer into a raw image
250 @param tcd TCD handle
251 @param src Source buffer
252 @param len Length of source buffer
253 @param tileno Number that identifies one of the tiles to be decoded
254 */
255 bool tcd_decode_tile(opj_tcd_t *tcd, unsigned char *src, int len, int tileno);
256 /**
257 Free the memory allocated for decoding
258 @param tcd TCD handle
259 */
260 void tcd_free_decode(opj_tcd_t *tcd);
261
262 /* ----------------------------------------------------------------------- */
263 /*@}*/
264
265 /*@}*/
266
267 #endif /* __TCD_H */