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
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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.
34 @brief The JPEG-2000 Codestream Reader/Writer (J2K)
36 The functions in J2K.C have for goal to read/write the several parts of the codestream: markers and data.
39 /** @defgroup J2K J2K - JPEG-2000 codestream reader/writer */
42 #define J2K_CP_CSTY_PRT 0x01
43 #define J2K_CP_CSTY_SOP 0x02
44 #define J2K_CP_CSTY_EPH 0x04
45 #define J2K_CCP_CSTY_PRT 0x01
46 #define J2K_CCP_CBLKSTY_LAZY 0x01
47 #define J2K_CCP_CBLKSTY_RESET 0x02
48 #define J2K_CCP_CBLKSTY_TERMALL 0x04
49 #define J2K_CCP_CBLKSTY_VSC 0x08
50 #define J2K_CCP_CBLKSTY_PTERM 0x10
51 #define J2K_CCP_CBLKSTY_SEGSYM 0x20
52 #define J2K_CCP_QNTSTY_NOQNT 0
53 #define J2K_CCP_QNTSTY_SIQNT 1
54 #define J2K_CCP_QNTSTY_SEQNT 2
56 /* ----------------------------------------------------------------------- */
58 #define J2K_MS_SOC 0xff4f /**< SOC marker value */
59 #define J2K_MS_SOT 0xff90 /**< SOT marker value */
60 #define J2K_MS_SOD 0xff93 /**< SOD marker value */
61 #define J2K_MS_EOC 0xffd9 /**< EOC marker value */
62 #define J2K_MS_SIZ 0xff51 /**< SIZ marker value */
63 #define J2K_MS_COD 0xff52 /**< COD marker value */
64 #define J2K_MS_COC 0xff53 /**< COC marker value */
65 #define J2K_MS_RGN 0xff5e /**< RGN marker value */
66 #define J2K_MS_QCD 0xff5c /**< QCD marker value */
67 #define J2K_MS_QCC 0xff5d /**< QCC marker value */
68 #define J2K_MS_POC 0xff5f /**< POC marker value */
69 #define J2K_MS_TLM 0xff55 /**< TLM marker value */
70 #define J2K_MS_PLM 0xff57 /**< PLM marker value */
71 #define J2K_MS_PLT 0xff58 /**< PLT marker value */
72 #define J2K_MS_PPM 0xff60 /**< PPM marker value */
73 #define J2K_MS_PPT 0xff61 /**< PPT marker value */
74 #define J2K_MS_SOP 0xff91 /**< SOP marker value */
75 #define J2K_MS_EPH 0xff92 /**< EPH marker value */
76 #define J2K_MS_CRG 0xff63 /**< CRG marker value */
77 #define J2K_MS_COM 0xff64 /**< COM marker value */
79 /* ----------------------------------------------------------------------- */
82 Values that specify the status of the decoding process when decoding the main header.
83 These values may be combined with a | operator.
85 typedef enum J2K_STATUS {
86 J2K_STATE_MHSOC = 0x0001, /**< a SOC marker is expected */
87 J2K_STATE_MHSIZ = 0x0002, /**< a SIZ marker is expected */
88 J2K_STATE_MH = 0x0004, /**< the decoding process is in the main header */
89 J2K_STATE_TPHSOT = 0x0008, /**< the decoding process is in a tile part header and expects a SOT marker */
90 J2K_STATE_TPH = 0x0010, /**< the decoding process is in a tile part header */
91 J2K_STATE_MT = 0x0020, /**< the EOC marker has just been read */
92 J2K_STATE_NEOC = 0x0040 /**< the decoding process must not expect a EOC marker because the codestream is truncated */
95 /* ----------------------------------------------------------------------- */
100 typedef struct opj_stepsize {
108 Tile-component coding parameters
110 typedef struct opj_tccp {
113 /** number of resolutions */
115 /** code-blocks width */
117 /** code-blocks height */
119 /** code-block coding style */
121 /** discrete wavelet transform identifier */
123 /** quantisation style */
125 /** stepsizes used for quantization */
126 opj_stepsize_t stepsizes[J2K_MAXBANDS];
127 /** number of guard bits */
129 /** Region Of Interest shift */
131 /** precinct width */
132 int prcw[J2K_MAXRLVLS];
133 /** precinct height */
134 int prch[J2K_MAXRLVLS];
138 Tile coding parameters :
139 this structure is used to store coding/decoding parameters common to all
140 tiles (information like COD, COC in main header)
142 typedef struct opj_tcp {
143 /** 1 : first part-tile of a tile */
147 /** progression order */
149 /** number of layers */
151 /** multi-component transform identifier */
153 /** rates of layers */
155 /** number of progression order changes */
157 /** indicates if a POC marker has been used O:NO, 1:YES */
159 /** progression order changes */
161 /** packet header store there for futur use in t2_decode_packet */
162 unsigned char *ppt_data;
163 /** pointer remaining on the first byte of the first header if ppt is used */
164 unsigned char *ppt_data_first;
165 /** If ppt == 1 --> there was a PPT marker for the present tile */
167 /** used in case of multiple marker PPT (number of info already stored) */
171 /** add fixed_quality */
172 float distoratio[100];
173 /** tile-component coding parameters */
180 typedef struct opj_cp {
181 /** allocation by rate/distortion */
183 /** allocation by fixed layer */
185 /** add fixed_quality */
187 /** if != 0, then original dimension divided by 2^(reduce); if == 0 or not used, image is decoded to the full resolution */
189 /** if != 0, then only the first "layer" layers are decoded; if == 0 or not used, all the quality layers are decoded */
191 /** 0 = no index || 1 = index */
201 /** comment for coding */
203 /** number of tiles in width */
205 /** number of tiles in heigth */
207 /** ID number of the tiles present in the codestream */
209 /** size of the vector tileno */
211 /** packet header store there for futur use in t2_decode_packet */
212 unsigned char *ppm_data;
213 /** pointer remaining on the first byte of the first header if ppm is used */
214 unsigned char *ppm_data_first;
215 /** if ppm == 1 --> there was a PPM marker for the present tile */
217 /** use in case of multiple marker PPM (number of info already store) */
219 /** use in case of multiple marker PPM (case on non-finished previous info) */
223 /** tile coding parameters */
230 Information concerning a packet inside tile
232 typedef struct opj_packet_info {
233 /** start position */
237 /** ADD for Marcela */
242 Index structure : information regarding tiles inside image
244 typedef struct opj_tile_info {
245 /** value of thresh for each layer by tile cfr. Marcela */
247 /** number of tile */
249 /** start position */
251 /** end position of the header */
255 /** precinct number for each resolution level (width) */
257 /** precinct number for each resolution level (height) */
259 /** precinct size (in power of 2), in X for each resolution level */
261 /** precinct size (in power of 2), in Y for each resolution level */
263 /** information concerning packets inside tile */
264 opj_packet_info_t *packet;
265 /** add fixed_quality */
267 /** add fixed_quality */
274 typedef struct opj_image_info {
275 /** 0 = no index || 1 = index */
277 /** maximum distortion reduction on the whole image (add for Marcela) */
281 /** writing the packet in the index with t2_encode_packets */
287 /** progression order */
289 /** tile size in x */
291 /** tile size in y */
297 /** number of tiles in X */
299 /** number of tiles in Y */
301 /** component numbers */
303 /** number of layer */
305 /** number of decomposition */
307 /** main header position */
309 /** codestream's size */
311 /** information regarding tiles inside image */
312 opj_tile_info_t *tile;
316 JPEG-2000 codestream reader/writer
318 typedef struct opj_j2k {
320 opj_common_ptr cinfo;
322 /** locate in which part of the codestream the decoder is (main header, tile header, end) */
324 /** number of the tile curently concern by coding/decoding */
327 locate the position of the end of the tile in the codestream,
328 used to detect a truncated codestream (in j2k_read_sod)
332 locate the start position of the SOT marker of the current coded tile:
333 after encoding the tile, a jump (in j2k_write_sod) is done to the SOT marker to store the value of its length.
338 as the J2K-file is written in several parts during encoding,
339 it enables to make the right correction in position return by cio_tell
342 /** array used to store the data of each tile */
343 unsigned char **tile_data;
344 /** array used to store the length of each tile */
348 store decoding parameters common to all tiles (information like COD, COC in main header)
350 opj_tcp_t *default_tcp;
351 /** pointer to the encoded / decoded image */
353 /** pointer to the coding parameters */
355 /** helper used to write the index file */
356 opj_image_info_t *image_info;
357 /** pointer to the byte i/o stream */
361 /** @name Exported functions */
363 /* ----------------------------------------------------------------------- */
365 Creates a J2K decompression structure
366 @param cinfo Codec context info
367 @return Returns a handle to a J2K decompressor if successful, returns NULL otherwise
369 opj_j2k_t* j2k_create_decompress(opj_common_ptr cinfo);
371 Destroy a J2K decompressor handle
372 @param j2k J2K decompressor handle to destroy
374 void j2k_destroy_decompress(opj_j2k_t *j2k);
376 Setup the decoder decoding parameters using user parameters.
377 Decoding parameters are returned in j2k->cp.
378 @param j2k J2K decompressor handle
379 @param parameters decompression parameters
381 void j2k_setup_decoder(opj_j2k_t *j2k, opj_dparameters_t *parameters);
383 Decode an image from a JPEG-2000 codestream
384 @param j2k J2K decompressor handle
385 @param cio Input buffer stream
386 @return Returns a decoded image if successful, returns NULL otherwise
388 opj_image_t* j2k_decode(opj_j2k_t *j2k, opj_cio_t *cio);
390 Decode an image form a JPT-stream (JPEG 2000, JPIP)
391 @param j2k J2K decompressor handle
392 @param cio Input buffer stream
393 @return Returns a decoded image if successful, returns NULL otherwise
395 opj_image_t* j2k_decode_jpt_stream(opj_j2k_t *j2k, opj_cio_t *cio);
397 Creates a J2K compression structure
398 @param cinfo Codec context info
399 @return Returns a handle to a J2K compressor if successful, returns NULL otherwise
401 opj_j2k_t* j2k_create_compress(opj_common_ptr cinfo);
403 Destroy a J2K compressor handle
404 @param j2k J2K compressor handle to destroy
406 void j2k_destroy_compress(opj_j2k_t *j2k);
408 Setup the encoder parameters using the current image and using user parameters.
409 Coding parameters are returned in j2k->cp.
410 @param j2k J2K compressor handle
411 @param parameters compression parameters
412 @param image input filled image
414 void j2k_setup_encoder(opj_j2k_t *j2k, opj_cparameters_t *parameters, opj_image_t *image);
416 Encode an image into a JPEG-2000 codestream
417 @param j2k J2K compressor handle
418 @param cio Output buffer stream
419 @param image Image to encode
420 @param index Name of the index file if required, NULL otherwise
421 @return Returns true if successful, returns false otherwise
423 bool j2k_encode(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image, char *index);
424 /* ----------------------------------------------------------------------- */