OpenJPEG version 1.1
[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 #ifndef __TCD_H
31 #define __TCD_H
32 /**
33 @file tcd.h
34 @brief Implementation of a tile coder/decoder (TCD)
35
36 The functions in TCD.C have for goal to encode or decode each tile independently from
37 each other. The functions in TCD.C are used by some function in J2K.C.
38 */
39
40 /** @defgroup TCD TCD - Implementation of a tile coder/decoder */
41 /*@{*/
42
43 /**
44 FIXME: documentation
45 */
46 typedef struct opj_tcd_seg {
47   int numpasses;
48   int len;
49   unsigned char *data;
50   int maxpasses;
51   int numnewpasses;
52   int newlen;
53 } opj_tcd_seg_t;
54
55 /**
56 FIXME: documentation
57 */
58 typedef struct opj_tcd_pass {
59   int rate;
60   double distortiondec;
61   int term, len;
62 } opj_tcd_pass_t;
63
64 /**
65 FIXME: documentation
66 */
67 typedef struct opj_tcd_layer {
68   int numpasses;                /* Number of passes in the layer */
69   int len;                      /* len of information */
70   double disto;                 /* add for index (Cfr. Marcela) */
71   unsigned char *data;          /* data */
72 } opj_tcd_layer_t;
73
74 /**
75 FIXME: documentation
76 */
77 typedef struct opj_tcd_cblk {
78   int x0, y0, x1, y1;           /* dimension of the code-blocks : left upper corner (x0, y0) right low corner (x1,y1) */
79   int numbps;
80   int numlenbits;
81   int len;                      /* length */
82   int numpasses;                /* number of pass already done for the code-blocks */
83   int numnewpasses;             /* number of pass added to the code-blocks */
84   int numsegs;                  /* number of segments */
85   opj_tcd_seg_t segs[100];              /* segments informations */
86   unsigned char data[8192];     /* Data */
87   int numpassesinlayers;        /* number of passes in the layer */
88   opj_tcd_layer_t layers[100];  /* layer information */
89   int totalpasses;              /* total number of passes */
90   opj_tcd_pass_t passes[100];   /* information about the passes */
91 } opj_tcd_cblk_t;
92
93 /**
94 FIXME: documentation
95 */
96 typedef struct opj_tcd_precinct {
97   int x0, y0, x1, y1;           /* dimension of the precinct : left upper corner (x0, y0) right low corner (x1,y1) */
98   int cw, ch;                   /* number of precinct in width and heigth */
99   opj_tcd_cblk_t *cblks;                /* code-blocks informations */
100   opj_tgt_tree_t *incltree;             /* inclusion tree */
101   opj_tgt_tree_t *imsbtree;             /* IMSB tree */
102 } opj_tcd_precinct_t;
103
104 /**
105 FIXME: documentation
106 */
107 typedef struct opj_tcd_band {
108   int x0, y0, x1, y1;           /* dimension of the subband : left upper corner (x0, y0) right low corner (x1,y1) */
109   int bandno;
110   opj_tcd_precinct_t *precincts;        /* precinct information */
111   int numbps;
112   float stepsize;
113 } opj_tcd_band_t;
114
115 /**
116 FIXME: documentation
117 */
118 typedef struct opj_tcd_resolution {
119   int x0, y0, x1, y1;           /* dimension of the resolution level : left upper corner (x0, y0) right low corner (x1,y1) */
120   int pw, ph;
121   int numbands;                 /* number sub-band for the resolution level */
122   opj_tcd_band_t bands[3];              /* subband information */
123 } opj_tcd_resolution_t;
124
125 /**
126 FIXME: documentation
127 */
128 typedef struct opj_tcd_tilecomp {
129   int x0, y0, x1, y1;           /* dimension of component : left upper corner (x0, y0) right low corner (x1,y1) */
130   int numresolutions;           /* number of resolutions level */
131   opj_tcd_resolution_t *resolutions;    /* resolutions information */
132   int *data;                    /* data of the component */
133   int nbpix;                    /* add fixed_quality */
134 } opj_tcd_tilecomp_t;
135
136 /**
137 FIXME: documentation
138 */
139 typedef struct opj_tcd_tile {
140   int x0, y0, x1, y1;           /* dimension of the tile : left upper corner (x0, y0) right low corner (x1,y1) */
141   int numcomps;                 /* number of components in tile */
142   opj_tcd_tilecomp_t *comps;    /* Components information */
143   int nbpix;                    /* add fixed_quality */
144   double distotile;             /* add fixed_quality */
145   double distolayer[100];       /* add fixed_quality */
146 } opj_tcd_tile_t;
147
148 /**
149 FIXME: documentation
150 */
151 typedef struct opj_tcd_image {
152   int tw, th;                   /* number of tiles in width and heigth */
153   opj_tcd_tile_t *tiles;                /* Tiles information */
154 } opj_tcd_image_t;
155
156 /**
157 Tile coder/decoder
158 */
159 typedef struct opj_tcd {
160         /** codec context */
161         opj_common_ptr cinfo;
162
163         /** info on each image tile */
164         opj_tcd_image_t *tcd_image;
165         /** image */
166         opj_image_t *image;
167         /** coding parameters */
168         opj_cp_t *cp;
169         /** pointer to the current encoded/decoded tile */
170         opj_tcd_tile_t *tcd_tile;
171         /** coding/decoding parameters common to all tiles */
172         opj_tcp_t *tcp;
173         /** current encoded/decoded tile */
174         int tcd_tileno;
175         /**@name working variables */
176         /*@{*/
177         opj_tcd_tile_t *tile;
178         opj_tcd_tilecomp_t *tilec;
179         opj_tcd_resolution_t *res;
180         opj_tcd_band_t *band;
181         opj_tcd_precinct_t *prc;
182         opj_tcd_cblk_t *cblk;
183         /*@}*/
184 } opj_tcd_t;
185
186 /** @name Exported functions */
187 /*@{*/
188 /* ----------------------------------------------------------------------- */
189
190 /**
191 Dump the content of a tcd structure
192 */
193 void tcd_dump(FILE *fd, opj_tcd_t *tcd, opj_tcd_image_t *img);
194 /**
195 Create a new TCD handle
196 @param cinfo Codec context info
197 @return Returns a new TCD handle if successful returns NULL otherwise
198 */
199 opj_tcd_t* tcd_create(opj_common_ptr cinfo);
200 /**
201 Destroy a previously created TCD handle
202 @param tcd TCD handle to destroy
203 */
204 void tcd_destroy(opj_tcd_t *tcd);
205 /**
206 Initialize the tile coder (allocate the memory)
207 @param tcd TCD handle
208 @param image Raw image
209 @param cp Coding parameters
210 @param curtileno Number that identifies the tile that will be encoded
211 */
212 void tcd_malloc_encode(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp, int curtileno);
213 /**
214 Free the memory allocated for encoding
215 @param tcd TCD handle
216 */
217 void tcd_free_encode(opj_tcd_t *tcd);
218 /**
219 Initialize the tile coder (reuses the memory allocated by tcd_malloc_encode)
220 @param tcd TCD handle
221 @param image Raw image
222 @param cp Coding parameters
223 @param curtileno Number that identifies the tile that will be encoded
224 */
225 void tcd_init_encode(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp, int curtileno);
226 /**
227 Initialize the tile decoder
228 @param tcd TCD handle
229 @param image Raw image
230 @param cp Coding parameters
231 */
232 void tcd_malloc_decode(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp);
233 void tcd_makelayer_fixed(opj_tcd_t *tcd, int layno, int final);
234 void tcd_rateallocate_fixed(opj_tcd_t *tcd);
235 void tcd_makelayer(opj_tcd_t *tcd, int layno, double thresh, int final);
236 bool tcd_rateallocate(opj_tcd_t *tcd, unsigned char *dest, int len, opj_image_info_t * image_info);
237 /**
238 Encode a tile from the raw image into a buffer
239 @param tcd TCD handle
240 @param tileno Number that identifies one of the tiles to be encoded
241 @param dest Destination buffer
242 @param len Length of destination buffer
243 @param image_info Creation of index file
244 @return 
245 */
246 int tcd_encode_tile(opj_tcd_t *tcd, int tileno, unsigned char *dest, int len, opj_image_info_t * image_info);
247 /**
248 Decode a tile from a buffer into a raw image
249 @param tcd TCD handle
250 @param src Source buffer
251 @param len Length of source buffer
252 @param tileno Number that identifies one of the tiles to be decoded
253 */
254 bool tcd_decode_tile(opj_tcd_t *tcd, unsigned char *src, int len, int tileno);
255 /**
256 Free the memory allocated for decoding
257 @param tcd TCD handle
258 */
259 void tcd_free_decode(opj_tcd_t *tcd);
260
261 /* ----------------------------------------------------------------------- */
262 /*@}*/
263
264 /*@}*/
265
266 #endif /* __TCD_H */