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
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
35 @brief Implementation of a tile coder/decoder (TCD)
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.
41 /** @defgroup TCD TCD - Implementation of a tile coder/decoder */
47 //typedef struct opj_tcd_seg {
48 // unsigned char** data;
60 typedef struct opj_tcd_seg {
64 OPJ_UINT32 real_num_passes;
67 OPJ_UINT32 numnewpasses;
74 typedef struct opj_tcd_pass {
83 typedef struct opj_tcd_layer {
84 int numpasses; /* Number of passes in the layer */
85 int len; /* len of information */
86 double disto; /* add for index (Cfr. Marcela) */
87 unsigned char *data; /* data */
93 typedef struct opj_tcd_cblk_enc {
94 unsigned char* data; /* Data */
95 opj_tcd_layer_t* layers; /* layer information */
96 opj_tcd_pass_t* passes; /* information about the passes */
97 int x0, y0, x1, y1; /* dimension of the code-blocks : left upper corner (x0, y0) right low corner (x1,y1) */
100 int numpasses; /* number of pass already done for the code-blocks */
101 int numpassesinlayers; /* number of passes in the layer */
102 int totalpasses; /* total number of passes */
103 } opj_tcd_cblk_enc_t;
105 //typedef struct opj_tcd_cblk_dec {
106 // unsigned char* data; /* Data */
107 // opj_tcd_seg_t* segs; /* segments informations */
108 // int x0, y0, x1, y1; /* dimension of the code-blocks : left upper corner (x0, y0) right low corner (x1,y1) */
111 // int len; /* length */
112 // int numnewpasses; /* number of pass added to the code-blocks */
113 // int numsegs; /* number of segments */
114 //} opj_tcd_cblk_dec_t;
116 typedef struct opj_tcd_cblk_dec {
117 OPJ_BYTE * data; /* Data */
118 opj_tcd_seg_t* segs; /* segments informations */
119 OPJ_INT32 x0, y0, x1, y1; /* dimension of the code-blocks : left upper corner (x0, y0) right low corner (x1,y1) */
121 OPJ_UINT32 numlenbits;
122 OPJ_UINT32 len; /* length */
123 OPJ_UINT32 numnewpasses; /* number of pass added to the code-blocks */
124 OPJ_UINT32 numsegs; /* number of segments */
125 OPJ_UINT32 real_num_segs;
126 OPJ_UINT32 m_current_max_segs;
127 } opj_tcd_cblk_dec_t;
132 //typedef struct opj_tcd_precinct {
133 // int x0, y0, x1, y1; /* dimension of the precinct : left upper corner (x0, y0) right low corner (x1,y1) */
134 // int cw, ch; /* number of precinct in width and heigth */
135 // union{ /* code-blocks informations */
136 // opj_tcd_cblk_enc_t* enc;
137 // opj_tcd_cblk_dec_t* dec;
139 // opj_tgt_tree_t *incltree; /* inclusion tree */
140 // opj_tgt_tree_t *imsbtree; /* IMSB tree */
141 //} opj_tcd_precinct_t;
144 typedef struct opj_tcd_precinct {
145 OPJ_INT32 x0, y0, x1, y1; /* dimension of the precinct : left upper corner (x0, y0) right low corner (x1,y1) */
146 OPJ_UINT32 cw, ch; /* number of precinct in width and heigth */
147 union{ /* code-blocks informations */
148 opj_tcd_cblk_enc_t* enc;
149 opj_tcd_cblk_dec_t* dec;
151 OPJ_UINT32 block_size; /* size taken by cblks (in bytes) */
152 struct opj_tgt_tree *incltree; /* inclusion tree */
153 struct opj_tgt_tree *imsbtree; /* IMSB tree */
154 } opj_tcd_precinct_t;
159 //typedef struct opj_tcd_band {
160 // int x0, y0, x1, y1; /* dimension of the subband : left upper corner (x0, y0) right low corner (x1,y1) */
162 // opj_tcd_precinct_t *precincts; /* precinct information */
167 typedef struct opj_tcd_band {
168 OPJ_INT32 x0, y0, x1, y1; /* dimension of the subband : left upper corner (x0, y0) right low corner (x1,y1) */
170 opj_tcd_precinct_t *precincts; /* precinct information */
171 OPJ_UINT32 precincts_data_size; /* size of data taken by precincts */
173 OPJ_FLOAT32 stepsize;
179 typedef struct opj_tcd_resolution {
180 int x0, y0, x1, y1; /* dimension of the resolution level : left upper corner (x0, y0) right low corner (x1,y1) */
182 int numbands; /* number sub-band for the resolution level */
183 opj_tcd_band_t bands[3]; /* subband information */
184 } opj_tcd_resolution_t;
189 //typedef struct opj_tcd_tilecomp {
190 // int x0, y0, x1, y1; /* dimension of component : left upper corner (x0, y0) right low corner (x1,y1) */
191 // int numresolutions; /* number of resolutions level */
192 // opj_tcd_resolution_t *resolutions; /* resolutions information */
193 // int *data; /* data of the component */
194 // int numpix; /* add fixed_quality */
195 //} opj_tcd_tilecomp_t;
197 typedef struct opj_tcd_tilecomp
199 OPJ_INT32 x0, y0, x1, y1; /* dimension of component : left upper corner (x0, y0) right low corner (x1,y1) */
200 OPJ_UINT32 numresolutions; /* number of resolutions level */
201 OPJ_UINT32 minimum_num_resolutions; /* number of resolutions level to decode (at max)*/
202 opj_tcd_resolution_t *resolutions; /* resolutions information */
203 OPJ_UINT32 resolutions_size; /* size of data for resolutions (in bytes) */
204 OPJ_INT32 *data; /* data of the component */
205 OPJ_UINT32 data_size; /* size of the data of the component */
206 OPJ_INT32 numpix; /* add fixed_quality */
207 } opj_tcd_tilecomp_t;
213 typedef struct opj_tcd_tile {
214 int x0, y0, x1, y1; /* dimension of the tile : left upper corner (x0, y0) right low corner (x1,y1) */
215 int numcomps; /* number of components in tile */
216 opj_tcd_tilecomp_t *comps; /* Components information */
217 int numpix; /* add fixed_quality */
218 double distotile; /* add fixed_quality */
219 double distolayer[100]; /* add fixed_quality */
227 typedef struct opj_tcd_image {
228 int tw, th; /* number of tiles in width and heigth */
229 opj_tcd_tile_t *tiles; /* Tiles information */
235 typedef struct opj_tcd {
236 /** Position of the tilepart flag in Progression order*/
238 /** Tile part number*/
240 /** Current tile part number*/
242 /** Total number of tileparts of the current tile*/
244 /** Current Packet iterator number */
247 opj_common_ptr cinfo;
249 /** info on each image tile */
250 opj_tcd_image_t *tcd_image;
253 /** coding parameters */
255 /** pointer to the current encoded/decoded tile */
256 opj_tcd_tile_t *tcd_tile;
257 /** coding/decoding parameters common to all tiles */
259 /** current encoded/decoded tile */
261 /** Time taken to encode a tile*/
262 double encoding_time;
272 typedef struct opj_tcd_v2
274 /** Position of the tilepart flag in Progression order*/
276 /** Tile part number*/
278 /** Current tile part number*/
279 OPJ_UINT32 cur_tp_num;
280 /** Total number of tileparts of the current tile*/
281 OPJ_UINT32 cur_totnum_tp;
282 /** Current Packet iterator number */
284 /** info on each image tile */
285 opj_tcd_image_t *tcd_image;
287 struct opj_image_header *image_header;
288 /** coding parameters */
289 struct opj_cp_v2 *cp;
290 /** coding/decoding parameters common to all tiles */
291 struct opj_tcp_v2 *tcp;
292 /** current encoded/decoded tile */
293 OPJ_UINT32 tcd_tileno;
294 /** tell if the tcd is a decoder. */
295 OPJ_UINT32 m_is_decoder : 1;
298 /** @name Exported functions */
300 /* ----------------------------------------------------------------------- */
303 Dump the content of a tcd structure
305 void tcd_dump(FILE *fd, opj_tcd_t *tcd, opj_tcd_image_t *img);
307 Create a new TCD handle
308 @param cinfo Codec context info
309 @return Returns a new TCD handle if successful returns NULL otherwise
311 opj_tcd_t* tcd_create(opj_common_ptr cinfo);
314 Create a new TCD handle
316 @return Returns a new TCD handle if successful returns NULL otherwise
318 opj_tcd_v2_t* tcd_create_v2(opj_bool p_is_decoder);
321 Destroy a previously created TCD handle
322 @param tcd TCD handle to destroy
324 void tcd_destroy_v2(opj_tcd_v2_t *tcd);
327 * Initialize the tile coder and may reuse some meory.
328 * @param p_tcd TCD handle.
329 * @param p_image raw image.
330 * @param p_cp coding parameters.
331 * @param p_tile_no current tile index to encode.
333 * @return true if the encoding values could be set (false otherwise).
335 opj_bool tcd_init_v2(
337 //struct opj_image * p_image,
338 opj_image_header_t * p_image_header,
339 //struct opj_cp * p_cp
344 * Allocates memory for decoding a specific tile.
346 * @param p_tcd the tile decoder.
347 * @param p_image the image to decode.
348 * @param p_cp the decoding parameters.
349 * @param p_tile_no the index of the tile received in sequence. This not necesseraly lead to the
350 * tile at index p_tile_no.
351 * @param p_cstr_info codestream info (if any).
353 * @return true if the remaining data is sufficient.s
355 opj_bool tcd_init_decode_tile(
361 Destroy a previously created TCD handle
362 @param tcd TCD handle to destroy
364 void tcd_destroy(opj_tcd_t *tcd);
366 Initialize the tile coder (allocate the memory)
367 @param tcd TCD handle
368 @param image Raw image
369 @param cp Coding parameters
370 @param curtileno Number that identifies the tile that will be encoded
372 void tcd_malloc_encode(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp, int curtileno);
374 Free the memory allocated for encoding
375 @param tcd TCD handle
377 void tcd_free_encode(opj_tcd_t *tcd);
379 Initialize the tile coder (reuses the memory allocated by tcd_malloc_encode)
380 @param tcd TCD handle
381 @param image Raw image
382 @param cp Coding parameters
383 @param curtileno Number that identifies the tile that will be encoded
385 void tcd_init_encode(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp, int curtileno);
387 Initialize the tile decoder
388 @param tcd TCD handle
389 @param image Raw image
390 @param cp Coding parameters
392 void tcd_malloc_decode(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp);
393 void tcd_malloc_decode_tile(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp, int tileno, opj_codestream_info_t *cstr_info);
394 void tcd_makelayer_fixed(opj_tcd_t *tcd, int layno, int final);
395 void tcd_rateallocate_fixed(opj_tcd_t *tcd);
396 void tcd_makelayer(opj_tcd_t *tcd, int layno, double thresh, int final);
397 opj_bool tcd_rateallocate(opj_tcd_t *tcd, unsigned char *dest, int len, opj_codestream_info_t *cstr_info);
399 Encode a tile from the raw image into a buffer
400 @param tcd TCD handle
401 @param tileno Number that identifies one of the tiles to be encoded
402 @param dest Destination buffer
403 @param len Length of destination buffer
404 @param cstr_info Codestream information structure
407 int tcd_encode_tile(opj_tcd_t *tcd, int tileno, unsigned char *dest, int len, opj_codestream_info_t *cstr_info);
409 Decode a tile from a buffer into a raw image
410 @param tcd TCD handle
411 @param src Source buffer
412 @param len Length of source buffer
413 @param tileno Number that identifies one of the tiles to be decoded
414 @param cstr_info Codestream information structure
416 opj_bool tcd_decode_tile(opj_tcd_t *tcd, unsigned char *src, int len, int tileno, opj_codestream_info_t *cstr_info);
418 Free the memory allocated for decoding
419 @param tcd TCD handle
421 void tcd_free_decode(opj_tcd_t *tcd);
422 void tcd_free_decode_tile(opj_tcd_t *tcd, int tileno);
426 * Gets the maximum tile size that will be taken by the tile once decoded.
428 OPJ_UINT32 tcd_get_decoded_tile_size (
433 Decode a tile from a buffer into a raw image
434 @param tcd TCD handle
435 @param src Source buffer
436 @param len Length of source buffer
437 @param tileno Number that identifies one of the tiles to be decoded
439 opj_bool tcd_decode_tile_v2(opj_tcd_v2_t *tcd, OPJ_BYTE *src, OPJ_UINT32 len, OPJ_UINT32 tileno, struct opj_codestream_info *cstr_info);
443 * Copies tile data from the system onto the given memory block.
445 opj_bool tcd_update_tile_data (
448 OPJ_UINT32 p_dest_length
451 /* ----------------------------------------------------------------------- */