1 /*
2  * Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium
3  * Copyright (c) 2002-2007, Professor Benoit Macq
4  * Copyright (c) 2001-2003, David Janssens
5  * Copyright (c) 2002-2003, Yannick Verschueren
6  * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe
7  * Copyright (c) 2005, Herve Drolon, FreeImage Team
8  * Copyright (c) 2008, Jerome Fimes, Communications & Systemes <jerome.fimes@c-s.fr>
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 #ifndef __TCD_H
33 #define __TCD_H
34 /**
35 @file tcd.h
36 @brief Implementation of a tile coder/decoder (TCD)
37 
38 The functions in TCD.C have for goal to encode or decode each tile independently from
39 each other. The functions in TCD.C are used by some function in J2K.C.
40 */
41 #include "openjpeg.h"
42 /** @defgroup TCD TCD - Implementation of a tile coder/decoder */
43 /*@{*/
44 struct opj_common_struct;
45 struct opj_codestream_info;
46 struct opj_image;
47 struct opj_tcp;
48 struct opj_cp;
49 struct opj_tgt_tree;
50 
51 /**
52 FIXME: documentation
53 */
54 typedef struct opj_tcd_seg {
55   OPJ_BYTE ** data;
56   OPJ_UINT32 dataindex;
57   OPJ_UINT32 numpasses;
58   OPJ_UINT32 real_num_passes;
59   OPJ_UINT32 len;
60   OPJ_UINT32 maxpasses;
61   OPJ_UINT32 numnewpasses;
62   OPJ_UINT32 newlen;
63 } opj_tcd_seg_t;
64 
65 /**
66 FIXME: documentation
67 */
68 typedef struct opj_tcd_pass {
69   OPJ_UINT32 rate;
70   OPJ_FLOAT64 distortiondec;
71   OPJ_UINT32 len;
72   OPJ_UINT32 term : 1;
73 } opj_tcd_pass_t;
74 
75 /**
76 FIXME: documentation
77 */
78 typedef struct opj_tcd_layer {
79   OPJ_UINT32 numpasses;                /* Number of passes in the layer */
80   OPJ_UINT32 len;                        /* len of information */
81   OPJ_FLOAT64 disto;                        /* add for index (Cfr. Marcela) */
82   OPJ_BYTE *data;                /* data */
83 } opj_tcd_layer_t;
84 
85 /**
86 FIXME: documentation
87 */
88 typedef struct opj_tcd_cblk_enc {
89   OPJ_BYTE* data;        /* Data */
90   opj_tcd_layer_t* layers;        /* layer information */
91   opj_tcd_pass_t* passes;        /* information about the passes */
92   OPJ_INT32 x0, y0, x1, y1;                /* dimension of the code-blocks : left upper corner (x0, y0) right low corner (x1,y1) */
93   OPJ_UINT32 numbps;
94   OPJ_UINT32 numlenbits;
95   OPJ_UINT32 numpasses;                /* number of pass already done for the code-blocks */
96   OPJ_UINT32 numpassesinlayers;        /* number of passes in the layer */
97   OPJ_UINT32 totalpasses;                /* total number of passes */
98 } opj_tcd_cblk_enc_t;
99 
100 typedef struct opj_tcd_cblk_dec {
101   OPJ_BYTE * data;        /* Data */
102   opj_tcd_seg_t* segs;                /* segments informations */
103   OPJ_INT32 x0, y0, x1, y1;                /* dimension of the code-blocks : left upper corner (x0, y0) right low corner (x1,y1) */
104   OPJ_UINT32 numbps;
105   OPJ_UINT32 numlenbits;
106   OPJ_UINT32 len;                        /* length */
107   OPJ_UINT32 numnewpasses;                /* number of pass added to the code-blocks */
108   OPJ_UINT32 numsegs;                        /* number of segments */
109   OPJ_UINT32 real_num_segs;
110   OPJ_UINT32 m_current_max_segs;
111 } opj_tcd_cblk_dec_t;
112 
113 /**
114 FIXME: documentation
115 */
116 typedef struct opj_tcd_precinct {
117   OPJ_INT32 x0, y0, x1, y1;                /* dimension of the precinct : left upper corner (x0, y0) right low corner (x1,y1) */
118   OPJ_UINT32 cw, ch;                        /* number of precinct in width and heigth */
119   union{                /* code-blocks informations */
120           opj_tcd_cblk_enc_t* enc;
121           opj_tcd_cblk_dec_t* dec;
122   } cblks;
123   OPJ_UINT32 block_size;                /* size taken by cblks (in bytes) */
124   struct opj_tgt_tree *incltree;                /* inclusion tree */
125  struct opj_tgt_tree *imsbtree;                /* IMSB tree */
126 } opj_tcd_precinct_t;
127 
128 /**
129 FIXME: documentation
130 */
131 typedef struct opj_tcd_band {
132   OPJ_INT32 x0, y0, x1, y1;                /* dimension of the subband : left upper corner (x0, y0) right low corner (x1,y1) */
133   OPJ_UINT32 bandno;
134   opj_tcd_precinct_t *precincts;        /* precinct information */
135   OPJ_UINT32 precincts_data_size;                /* size of data taken by precincts */
136   OPJ_INT32 numbps;
137   OPJ_FLOAT32 stepsize;
138 } opj_tcd_band_t;
139 
140 /**
141 FIXME: documentation
142 */
143 typedef struct opj_tcd_resolution {
144   OPJ_INT32 x0, y0, x1, y1;                /* dimension of the resolution level : left upper corner (x0, y0) right low corner (x1,y1) */
145   OPJ_UINT32 pw, ph;
146   OPJ_UINT32 numbands;                        /* number sub-band for the resolution level */
147   opj_tcd_band_t bands[3];                /* subband information */
148 } opj_tcd_resolution_t;
149 
150 /**
151 FIXME: documentation
152 */
153 typedef struct opj_tcd_tilecomp
154 {
155   OPJ_INT32 x0, y0, x1, y1;                /* dimension of component : left upper corner (x0, y0) right low corner (x1,y1) */
156   OPJ_UINT32 numresolutions;                /* number of resolutions level */
157   OPJ_UINT32 minimum_num_resolutions;                /* number of resolutions level to decode (at max)*/
158   opj_tcd_resolution_t *resolutions;        /* resolutions information */
159   OPJ_UINT32 resolutions_size;                /* size of data for resolutions (in bytes) */
160   OPJ_INT32 *data;                        /* data of the component */
161   OPJ_UINT32 data_size;                        /* size of the data of the component */
162   OPJ_INT32 numpix;                        /* add fixed_quality */
163 } opj_tcd_tilecomp_t;
164 
165 /**
166 FIXME: documentation
167 */
168 typedef struct opj_tcd_tile {
169   OPJ_INT32 x0, y0, x1, y1;                /* dimension of the tile : left upper corner (x0, y0) right low corner (x1,y1) */
170   OPJ_UINT32 numcomps;                        /* number of components in tile */
171   opj_tcd_tilecomp_t *comps;        /* Components information */
172   OPJ_INT32 numpix;                        /* add fixed_quality */
173   OPJ_FLOAT64 distotile;                /* add fixed_quality */
174   OPJ_FLOAT64 distolayer[100];        /* add fixed_quality */
175   /** packet number */
176   OPJ_UINT32 packno;
177 } opj_tcd_tile_t;
178 
179 /**
180 FIXME: documentation
181 */
182 typedef struct opj_tcd_image
183 {
184   opj_tcd_tile_t *tiles;                /* Tiles information */
185 }
186 opj_tcd_image_t;
187 
188 /**
189 Tile coder/decoder
190 */
191 typedef struct opj_tcd
192 {
193         /** Position of the tilepart flag in Progression order*/
194         OPJ_INT32 tp_pos;
195         /** Tile part number*/
196         OPJ_UINT32 tp_num;
197         /** Current tile part number*/
198         OPJ_UINT32 cur_tp_num;
199         /** Total number of tileparts of the current tile*/
200         OPJ_UINT32 cur_totnum_tp;
201         /** Current Packet iterator number */
202         OPJ_UINT32 cur_pino;
203         /** info on each image tile */
204         struct opj_tcd_image *tcd_image;
205         /** image */
206         struct opj_image *image;
207         /** coding parameters */
208         struct opj_cp *cp;
209         /** coding/decoding parameters common to all tiles */
210         struct opj_tcp *tcp;
211         /** current encoded/decoded tile */
212         OPJ_UINT32 tcd_tileno;
213         /** tell if the tcd is a decoder. */
214         OPJ_UINT32 m_is_decoder : 1;
215 } opj_tcd_t;
216 
217 /** @name Exported functions */
218 /*@{*/
219 /* ----------------------------------------------------------------------- */
220 
221 /**
222 Dump the content of a tcd structure
223 */
224 //void tcd_dump(FILE *fd, opj_tcd_t *tcd, opj_tcd_image_t *img);
225 /**
226 Create a new TCD handle
227 @param cinfo Codec context info
228 @return Returns a new TCD handle if successful returns NULL otherwise
229 */
230 opj_tcd_t* tcd_create(bool p_is_decoder);
231 
232 /**
233 Destroy a previously created TCD handle
234 @param tcd TCD handle to destroy
235 */
236 void tcd_destroy(opj_tcd_t *tcd);
237 
238 
239 
240 
241 /**
242  * Initialize the tile coder and may reuse some meory.
243  * @param        p_tcd                TCD handle.
244  * @param        p_image                raw image.
245  * @param        p_cp                coding parameters.
246  * @param        p_tile_no        current tile index to encode.
247  *
248  * @return true if the encoding values could be set (false otherwise).
249 */
250 bool tcd_init(
251                                                 opj_tcd_t *p_tcd,
252                                                 struct opj_image * p_image,
253                                                 struct opj_cp * p_cp
254                                         );
255 
256 
257 /**
258  * Allocates memory for decoding a specific tile.
259  *
260  * @param        p_tcd                the tile decoder.
261  * @param        p_image                the image to decode.
262  * @param        p_cp                the decoding parameters.
263  * @param        p_tile_no        the index of the tile received in sequence. This not necesseraly lead to the
264  * tile at index p_tile_no.
265  * @param        p_cstr_info        codestream info (if any).
266  *
267  * @return        true if the remaining data is sufficient.s
268  */
269 bool tcd_init_decode_tile(
270                                                         opj_tcd_t *p_tcd,
271                                                         OPJ_UINT32 p_tile_no
272                                                         );
273 /**
274  * Initialize the tile coder and may reuse some meory.
275  * @param        p_tcd                TCD handle.
276  * @param        p_image                raw image.
277  * @param        p_cp                coding parameters.
278  * @param        p_tile_no        current tile index to encode.
279  *
280  * @return true if the encoding values could be set (false otherwise).
281 */
282 bool tcd_init_encode_tile
283                                                         (
284                                                         opj_tcd_t *p_tcd,
285                                                         OPJ_UINT32 p_tile_no
286                                                         );
287 
288 void tcd_makelayer_fixed(opj_tcd_t *tcd, OPJ_UINT32 layno, OPJ_UINT32 final);
289 void tcd_rateallocate_fixed(opj_tcd_t *tcd);
290 void tcd_makelayer(opj_tcd_t *tcd, OPJ_UINT32 layno, OPJ_FLOAT64 thresh, OPJ_UINT32 final);
291 bool tcd_rateallocate(opj_tcd_t *tcd, OPJ_BYTE *dest, OPJ_UINT32 * p_data_written, OPJ_UINT32 len, struct opj_codestream_info *cstr_info);
292 /**
293  * Encodes a tile from the raw image into the given buffer.
294  * @param        p_tcd                        Tile Coder handle
295  * @param        p_tile_no                Index of the tile to encode.
296  * @param        p_dest                        Destination buffer
297  * @param        p_data_written        pointer to an int that is incremented by the number of bytes really written on p_dest
298  * @param        p_len                        Maximum length of the destination buffer
299  * @param        p_cstr_info                Codestream information structure
300  * @return  true if the coding is successfull.
301 */
302 bool tcd_encode_tile(opj_tcd_t *p_tcd, OPJ_UINT32 p_tile_no, OPJ_BYTE *p_dest, OPJ_UINT32 * p_data_written, OPJ_UINT32 p_len, struct opj_codestream_info *p_cstr_info);
303 
304 /**
305 Decode a tile from a buffer into a raw image
306 @param tcd TCD handle
307 @param src Source buffer
308 @param len Length of source buffer
309 @param tileno Number that identifies one of the tiles to be decoded
310 */
311 bool tcd_decode_tile(opj_tcd_t *tcd, OPJ_BYTE *src, OPJ_UINT32 len, OPJ_UINT32 tileno, struct opj_codestream_info *cstr_info);
312 
313 /**
314  * Copies tile data from the given memory block onto the system.
315  */
316 bool tcd_copy_tile_data (
317                                                  opj_tcd_t *p_tcd,
318                                                  OPJ_BYTE * p_src,
319                                                  OPJ_UINT32 p_src_length
320                                                  );
321 /**
322  * Copies tile data from the system onto the given memory block.
323  */
324 bool tcd_update_tile_data (
325                                                  opj_tcd_t *p_tcd,
326                                                  OPJ_BYTE * p_dest,
327                                                  OPJ_UINT32 p_dest_length
328                                                  );
329 /**
330  * Gets the maximum tile size that will be taken by the tile once decoded.
331  */
332 OPJ_UINT32 tcd_get_decoded_tile_size (
333                                                  opj_tcd_t *p_tcd
334                                                  );
335 
336 OPJ_UINT32 tcd_get_encoded_tile_size (
337                                                  opj_tcd_t *p_tcd
338                                                  );
339 /* ----------------------------------------------------------------------- */
340 /*@}*/
341 
342 /*@}*/
343 
344 #endif /* __TCD_H */
345