Merge pull request #1295 from rouault/fix_1293
[openjpeg.git] / src / lib / openjp3d / tcd.h
1 /*
2  * The copyright in this software is being made available under the 2-clauses
3  * BSD License, included below. This software may be subject to other third
4  * party and contributor rights, including patent rights, and no such rights
5  * are granted under this license.
6  *
7  * Copyright (c) 2001-2003, David Janssens
8  * Copyright (c) 2002-2003, Yannick Verschueren
9  * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe
10  * Copyright (c) 2005, Herve Drolon, FreeImage Team
11  * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
12  * Copyright (c) 2006, Mónica Díez García, Image Processing Laboratory, University of Valladolid, Spain
13  * All rights reserved.
14  *
15  * Redistribution and use in source and binary forms, with or without
16  * modification, are permitted provided that the following conditions
17  * are met:
18  * 1. Redistributions of source code must retain the above copyright
19  *    notice, this list of conditions and the following disclaimer.
20  * 2. Redistributions in binary form must reproduce the above copyright
21  *    notice, this list of conditions and the following disclaimer in the
22  *    documentation and/or other materials provided with the distribution.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
25  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
28  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34  * POSSIBILITY OF SUCH DAMAGE.
35  */
36 #ifndef __TCD_H
37 #define __TCD_H
38 /**
39 @file tcd.h
40 @brief Implementation of a tile coder/decoder (TCD)
41
42 The functions in TCD.C have for goal to encode or decode each tile independently from
43 each other. The functions in TCD.C are used by some function in JP3D.C.
44 */
45
46 /** @defgroup TCD TCD - Implementation of a tile coder/decoder */
47 /*@{*/
48
49 /**
50 Tile coder/decoder: segment instance
51 */
52 typedef struct opj_tcd_seg {
53     /** Number of passes in the segment */
54     int numpasses;
55     /** Length of information */
56     int len;
57     /** Data */
58     unsigned char *data;
59     /** Number of passes possible for the segment */
60     int maxpasses;
61     /** Number of passes added to the segment */
62     int numnewpasses;
63     /** New length after inclusion of segments */
64     int newlen;
65 } opj_tcd_seg_t;
66
67 /**
68 Tile coder/decoder: pass instance
69 */
70 typedef struct opj_tcd_pass {
71     /** Rate obtained in the pass*/
72     int rate;
73     /** Distorsion obtained in the pass*/
74     double distortiondec;
75     int term;
76     /** Length of information */
77     int len;
78 } opj_tcd_pass_t;
79
80 /**
81 Tile coder/decoder: layer instance
82 */
83 typedef struct opj_tcd_layer {
84     /** Number of passes in the layer */
85     int numpasses;
86     /** Length of information */
87     int len;
88     /** Distortion within layer */
89     double disto;             /* add for index (Cfr. Marcela) */
90     unsigned char *data;      /* data */
91 } opj_tcd_layer_t;
92
93 /**
94 Tile coder/decoder: codeblock instance
95 */
96 typedef struct opj_tcd_cblk {
97     /** Dimension of the code-blocks : left upper corner (x0, y0, z0) */
98     int x0, y0, z0;
99     /** Dimension of the code-blocks : right low corner (x1,y1,z1) */
100     int x1, y1, z1;
101     /** Number of bits per simbol in codeblock */
102     int numbps;
103     int numlenbits;
104     int len;                      /* length */
105     /** Number of pass already done for the code-blocks */
106     int numpasses;
107     /** number of pass added to the code-blocks */
108     int numnewpasses;
109     /** Number of segments */
110     int numsegs;
111     /** Segments information */
112     opj_tcd_seg_t segs[100];
113     /** Number of passes in the layer */
114     int numpassesinlayers;
115     /** Layer information */
116     opj_tcd_layer_t layers[100];
117     /** Total number of passes */
118     int totalpasses;
119     /** Information about the passes */
120     opj_tcd_pass_t passes[100];
121     /* Data */
122     unsigned char data[524288];
123     /*unsigned char *data;*/
124 } opj_tcd_cblk_t;
125
126 /**
127 Tile coder/decoder: precint instance
128 */
129 typedef struct opj_tcd_precinct {
130     /** Dimension of the precint : left upper corner (x0, y0, z0) */
131     int x0, y0, z0;
132     /** Dimension of the precint : right low corner (x1,y1,z1) */
133     int x1, y1, z1;
134     /** Number of codeblocks in precinct in width and height and length*/
135     int cblkno[3];
136     /** Information about the codeblocks */
137     opj_tcd_cblk_t *cblks;
138     /** Inclusion tree */
139     opj_tgt_tree_t *incltree;
140     /** Missing MSBs tree */
141     opj_tgt_tree_t *imsbtree;
142 } opj_tcd_precinct_t;
143
144 /**
145 Tile coder/decoder: subband instance
146 */
147 typedef struct opj_tcd_band {
148     /** Dimension of the subband : left upper corner (x0, y0, z0) */
149     int x0, y0, z0;
150     /** Dimension of the subband : right low corner (x1,y1,z1) */
151     int x1, y1, z1;
152     /** Information about the precints */
153     opj_tcd_precinct_t *precincts;    /* precinct information */
154     /** Number of bits per symbol in band */
155     int numbps;
156     /** Quantization stepsize associated */
157     float stepsize;
158     /** Band orientation (O->LLL,...,7->HHH) */
159     int bandno;
160 } opj_tcd_band_t;
161
162 /**
163 Tile coder/decoder: resolution instance
164 */
165 typedef struct opj_tcd_resolution {
166     /** Dimension of the resolution level : left upper corner (x0, y0, z0) */
167     int x0, y0, z0;
168     /** Dimension of the resolution level : right low corner (x1,y1,z1) */
169     int x1, y1, z1;
170     /** Number of precints in each dimension for the resolution level */
171     int prctno[3];
172     /** Number of subbands for the resolution level */
173     int numbands;
174     /** Subband information */
175     opj_tcd_band_t *bands;
176 } opj_tcd_resolution_t;
177
178 /**
179 Tile coder/decoder: component instance
180 */
181 typedef struct opj_tcd_tilecomp {
182     /** Dimension of the component : left upper corner (x0, y0, z0) */
183     int x0, y0, z0;
184     /** Dimension of the component : right low corner (x1,y1,z1) */
185     int x1, y1, z1;
186     /** Number of resolutions level if DWT transform*/
187     int numresolution[3];
188     /** Resolution information */
189     opj_tcd_resolution_t *resolutions;
190     /** Data of the component */
191     int *data;
192     /** Fixed_quality related */
193     int nbpix;
194     /** Number of bits per voxel in component */
195     int bpp;
196 } opj_tcd_tilecomp_t;
197
198 /**
199 Tile coder/decoder: tile instance
200 */
201 typedef struct opj_tcd_tile {
202     /** Dimension of the tile : left upper corner (x0, y0, z0) */
203     int x0, y0, z0;
204     /** Dimension of the tile : right low corner (x1,y1,z1) */
205     int x1, y1, z1;
206     /** Number of components in tile */
207     int numcomps;
208     /** Components information */
209     opj_tcd_tilecomp_t *comps;
210     /** Fixed_quality related : no of bytes of data*/
211     int nbpix;
212     /** Fixed_quality related : distortion achieved in tile */
213     double distotile;
214     /** Fixed_quality related : distortion achieved in each layer */
215     double distolayer[100];
216 } opj_tcd_tile_t;
217
218 /**
219 Tile coder/decoder: volume instance
220 */
221 typedef struct opj_tcd_volume {
222     /** Number of tiles in width and height and length */
223     int tw, th, tl;
224     /** Tiles information */
225     opj_tcd_tile_t *tiles;
226 } opj_tcd_volume_t;
227
228 /**
229 Tile coder/decoder
230 */
231 typedef struct opj_tcd {
232     /** Codec context */
233     opj_common_ptr cinfo;
234     /** Volume information */
235     opj_volume_t *volume;
236     /** Coding parameters */
237     opj_cp_t *cp;
238     /** Coding/decoding parameters common to all tiles */
239     opj_tcp_t *tcp;
240     /** Info on each volume tile */
241     opj_tcd_volume_t *tcd_volume;
242     /** Pointer to the current encoded/decoded tile */
243     opj_tcd_tile_t *tcd_tile;
244     /** Current encoded/decoded tile */
245     int tcd_tileno;
246
247     /**@name working variables */
248     /*@{*/
249     opj_tcd_tile_t *tile;
250     opj_tcd_tilecomp_t *tilec;
251     opj_tcd_resolution_t *res;
252     opj_tcd_band_t *band;
253     opj_tcd_precinct_t *prc;
254     opj_tcd_cblk_t *cblk;
255     /*@}*/
256 } opj_tcd_t;
257
258 /** @name Funciones generales */
259 /*@{*/
260 /* ----------------------------------------------------------------------- */
261
262 /**
263 Dump the content of a tcd structure
264 */
265 void tcd_dump(FILE *fd, opj_tcd_t *tcd, opj_tcd_volume_t *img);
266 /**
267 Create a new TCD handle
268 @param cinfo Codec context info
269 @return Returns a new TCD handle if successful returns NULL otherwise
270 */
271 opj_tcd_t* tcd_create(opj_common_ptr cinfo);
272 /**
273 Destroy a previously created TCD handle
274 @param tcd TCD handle to destroy
275 */
276 void tcd_destroy(opj_tcd_t *tcd);
277 /**
278 Initialize the tile coder (allocate the memory)
279 @param tcd TCD handle
280 @param volume Raw volume
281 @param cp Coding parameters
282 @param curtileno Number that identifies the tile that will be encoded
283 */
284 void tcd_malloc_encode(opj_tcd_t *tcd, opj_volume_t * volume, opj_cp_t * cp,
285                        int curtileno);
286 /**
287 Initialize the tile coder (reuses the memory allocated by tcd_malloc_encode)(for 3D-DWT)
288 @param tcd TCD handle
289 @param volume Raw volume
290 @param cp Coding parameters
291 @param curtileno Number that identifies the tile that will be encoded
292 */
293 void tcd_init_encode(opj_tcd_t *tcd, opj_volume_t * volume, opj_cp_t * cp,
294                      int curtileno);
295 /**
296 Free the memory allocated for encoding
297 @param tcd TCD handle
298 */
299 void tcd_free_encode(opj_tcd_t *tcd);
300 /**
301 Initialize the tile decoder
302 @param tcd TCD handle
303 @param volume Raw volume
304 @param cp Coding parameters
305 */
306 void tcd_malloc_decode(opj_tcd_t *tcd, opj_volume_t * volume, opj_cp_t * cp);
307
308 void tcd_makelayer_fixed(opj_tcd_t *tcd, int layno, int final);
309 void tcd_rateallocate_fixed(opj_tcd_t *tcd);
310 void tcd_makelayer(opj_tcd_t *tcd, int layno, double thresh, int final);
311 bool tcd_rateallocate(opj_tcd_t *tcd, unsigned char *dest, int len,
312                       opj_volume_info_t * volume_info);
313 /**
314 Encode a tile from the raw volume into a buffer
315 @param tcd TCD handle
316 @param tileno Number that identifies one of the tiles to be encoded
317 @param dest Destination buffer
318 @param len Length of destination buffer
319 @param volume_info Creation of index file
320 @return
321 */
322 int tcd_encode_tile(opj_tcd_t *tcd, int tileno, unsigned char *dest, int len,
323                     opj_volume_info_t * volume_info);
324 /**
325 Decode a tile from a buffer into a raw volume
326 @param tcd TCD handle
327 @param src Source buffer
328 @param len Length of source buffer
329 @param tileno Number that identifies one of the tiles to be decoded
330 */
331 bool tcd_decode_tile(opj_tcd_t *tcd, unsigned char *src, int len, int tileno);
332 /**
333 Free the memory allocated for decoding
334 @param tcd TCD handle
335 */
336 void tcd_free_decode(opj_tcd_t *tcd);
337
338 /* ----------------------------------------------------------------------- */
339 /*@}*/
340
341 /*@}*/
342
343 #endif /* __TCD_H */