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) 2002-2003, Yannick Verschueren
5 * Copyright (c) 2005, Herve Drolon, FreeImage Team
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
33 @brief The JPEG-2000 file format Reader/Writer (JP2)
37 /** @defgroup JP2 JP2 - JPEG-2000 file format reader/writer */
40 /*#define JPIP_JPIP 0x6a706970*/
42 #define JP2_JP 0x6a502020 /**< JPEG 2000 signature box */
43 #define JP2_FTYP 0x66747970 /**< File type box */
44 #define JP2_JP2H 0x6a703268 /**< JP2 header box (super-box) */
45 #define JP2_IHDR 0x69686472 /**< Image header box */
46 #define JP2_COLR 0x636f6c72 /**< Colour specification box */
47 #define JP2_JP2C 0x6a703263 /**< Contiguous codestream box */
48 #define JP2_URL 0x75726c20 /**< Data entry URL box */
49 #define JP2_PCLR 0x70636c72 /**< Palette box */
50 #define JP2_CMAP 0x636d6170 /**< Component Mapping box */
51 #define JP2_CDEF 0x63646566 /**< Channel Definition box */
52 #define JP2_DTBL 0x6474626c /**< Data Reference box */
53 #define JP2_BPCC 0x62706363 /**< Bits per component box */
54 #define JP2_JP2 0x6a703220 /**< File type fields */
57 /* #define JP2_RES 0x72657320 */ /**< Resolution box (super-box) */
58 /* #define JP2_JP2I 0x6a703269 */ /**< Intellectual property box */
59 /* #define JP2_XML 0x786d6c20 */ /**< XML box */
60 /* #define JP2_UUID 0x75756994 */ /**< UUID box */
61 /* #define JP2_UINF 0x75696e66 */ /**< UUID info box (super-box) */
62 /* #define JP2_ULST 0x756c7374 */ /**< UUID list box */
64 /* ----------------------------------------------------------------------- */
69 JP2_STATE_SIGNATURE = 0x1,
70 JP2_STATE_FILE_TYPE = 0x2,
71 JP2_STATE_HEADER = 0x4,
72 JP2_STATE_CODESTREAM = 0x8,
73 JP2_STATE_END_CODESTREAM = 0x10,
74 JP2_STATE_UNKNOWN = 0x7fffffff /* ISO C restricts enumerator values to range of 'int' */
80 JP2_IMG_STATE_NONE = 0x0,
81 JP2_IMG_STATE_UNKNOWN = 0x7fffffff
86 Channel description: channel index, type, assocation
88 typedef struct opj_jp2_cdef_info
90 OPJ_UINT16 cn, typ, asoc;
91 } opj_jp2_cdef_info_t;
94 Channel descriptions and number of descriptions
96 typedef struct opj_jp2_cdef
98 opj_jp2_cdef_info_t *info;
103 Component mappings: channel index, mapping type, palette index
105 typedef struct opj_jp2_cmap_comp
109 } opj_jp2_cmap_comp_t;
112 Palette data: table entries, palette columns
114 typedef struct opj_jp2_pclr
117 OPJ_BYTE *channel_sign;
118 OPJ_BYTE *channel_size;
119 opj_jp2_cmap_comp_t *cmap;
120 OPJ_UINT16 nr_entries;
121 OPJ_BYTE nr_channels;
125 Collector for ICC profile, palette, component mapping, channel description
127 typedef struct opj_jp2_color
129 OPJ_BYTE *icc_profile_buf;
130 OPJ_UINT32 icc_profile_len;
132 opj_jp2_cdef_t *jp2_cdef;
133 opj_jp2_pclr_t *jp2_pclr;
134 OPJ_BYTE jp2_has_colr;
140 typedef struct opj_jp2_comps {
147 JPEG-2000 file format reader/writer
149 typedef struct opj_jp2
151 /** handle to the J2K codec */
153 /** list of validation procedures */
154 struct opj_procedure_list * m_validation_list;
155 /** list of execution procedures */
156 struct opj_procedure_list * m_procedure_list;
160 /* height of image */
162 /* number of components in the image */
171 OPJ_UINT32 precedence;
173 OPJ_UINT32 minversion;
176 opj_jp2_comps_t *comps;
177 /* FIXME: The following two variables are used to save offset
178 as we write out a JP2 file to disk. This mecanism is not flexible
179 as codec writers will need to extand those fields as new part
180 of the standard are implemented.
182 OPJ_OFF_T j2k_codestream_offset;
183 OPJ_OFF_T jpip_iptr_offset;
185 OPJ_UINT32 jp2_state;
186 OPJ_UINT32 jp2_img_state;
188 opj_jp2_color_t color;
190 OPJ_BOOL ignore_pclr_cmap_cdef;
197 typedef struct opj_jp2_box {
203 typedef struct opj_jp2_header_handler
207 /* action linked to the marker */
208 OPJ_BOOL (*handler) ( opj_jp2_t *jp2,
209 OPJ_BYTE *p_header_data,
210 OPJ_UINT32 p_header_size,
211 opj_event_mgr_t * p_manager);
213 opj_jp2_header_handler_t;
216 typedef struct opj_jp2_img_header_writer_handler
218 /* action to perform */
219 OPJ_BYTE* (*handler) (opj_jp2_t *jp2, OPJ_UINT32 * p_data_size);
220 /* result of the action : data */
225 opj_jp2_img_header_writer_handler_t;
227 /** @name Exported functions */
229 /* ----------------------------------------------------------------------- */
232 * Writes the Jpeg2000 file Header box - JP2 Header box (warning, this is a super box).
234 * @param jp2 the jpeg2000 file codec.
235 * @param stream the stream to write data to.
236 * @param p_manager user event manager.
238 * @return true if writing was successful.
240 OPJ_BOOL opj_jp2_write_jp2h(opj_jp2_t *jp2,
241 opj_stream_private_t *stream,
242 opj_event_mgr_t * p_manager );
245 Setup the decoder decoding parameters using user parameters.
246 Decoding parameters are returned in jp2->j2k->cp.
247 @param jp2 JP2 decompressor handle
248 @param parameters decompression parameters
250 void opj_jp2_setup_decoder(opj_jp2_t *jp2, opj_dparameters_t *parameters);
253 * Decode an image from a JPEG-2000 file stream
254 * @param jp2 JP2 decompressor handle
255 * @param p_stream FIXME DOC
256 * @param p_image FIXME DOC
257 * @param p_manager FIXME DOC
259 * @return Returns a decoded image if successful, returns NULL otherwise
261 OPJ_BOOL opj_jp2_decode(opj_jp2_t *jp2,
262 opj_stream_private_t *p_stream,
263 opj_image_t* p_image,
264 opj_event_mgr_t * p_manager);
267 * Setup the encoder parameters using the current image and using user parameters.
268 * Coding parameters are returned in jp2->j2k->cp.
270 * @param jp2 JP2 compressor handle
271 * @param parameters compression parameters
272 * @param image input filled image
273 * @param p_manager FIXME DOC
275 void opj_jp2_setup_encoder( opj_jp2_t *jp2,
276 opj_cparameters_t *parameters,
278 opj_event_mgr_t * p_manager);
281 Encode an image into a JPEG-2000 file stream
282 @param jp2 JP2 compressor handle
283 @param stream Output buffer stream
284 @param p_manager event manager
285 @return Returns true if successful, returns false otherwise
287 OPJ_BOOL opj_jp2_encode( opj_jp2_t *jp2,
288 opj_stream_private_t *stream,
289 opj_event_mgr_t * p_manager);
293 * Starts a compression scheme, i.e. validates the codec parameters, writes the header.
295 * @param jp2 the jpeg2000 file codec.
296 * @param stream the stream object.
297 * @param p_image FIXME DOC
298 * @param p_manager FIXME DOC
300 * @return true if the codec is valid.
302 OPJ_BOOL opj_jp2_start_compress(opj_jp2_t *jp2,
303 opj_stream_private_t *stream,
304 opj_image_t * p_image,
305 opj_event_mgr_t * p_manager);
309 * Ends the compression procedures and possibiliy add data to be read after the
312 OPJ_BOOL opj_jp2_end_compress( opj_jp2_t *jp2,
313 opj_stream_private_t *cio,
314 opj_event_mgr_t * p_manager);
316 /* ----------------------------------------------------------------------- */
319 * Ends the decompression procedures and possibiliy add data to be read after the
322 OPJ_BOOL opj_jp2_end_decompress(opj_jp2_t *jp2,
323 opj_stream_private_t *cio,
324 opj_event_mgr_t * p_manager);
327 * Reads a jpeg2000 file header structure.
329 * @param p_stream the stream to read data from.
330 * @param jp2 the jpeg2000 file header structure.
331 * @param p_image FIXME DOC
332 * @param p_manager the user event manager.
334 * @return true if the box is valid.
336 OPJ_BOOL opj_jp2_read_header( opj_stream_private_t *p_stream,
338 opj_image_t ** p_image,
339 opj_event_mgr_t * p_manager );
342 * Reads a tile header.
343 * @param p_jp2 the jpeg2000 codec.
344 * @param p_tile_index FIXME DOC
345 * @param p_data_size FIXME DOC
346 * @param p_tile_x0 FIXME DOC
347 * @param p_tile_y0 FIXME DOC
348 * @param p_tile_x1 FIXME DOC
349 * @param p_tile_y1 FIXME DOC
350 * @param p_nb_comps FIXME DOC
351 * @param p_go_on FIXME DOC
352 * @param p_stream the stream to write data to.
353 * @param p_manager the user event manager.
355 OPJ_BOOL opj_jp2_read_tile_header ( opj_jp2_t * p_jp2,
356 OPJ_UINT32 * p_tile_index,
357 OPJ_UINT32 * p_data_size,
358 OPJ_INT32 * p_tile_x0,
359 OPJ_INT32 * p_tile_y0,
360 OPJ_INT32 * p_tile_x1,
361 OPJ_INT32 * p_tile_y1,
362 OPJ_UINT32 * p_nb_comps,
364 opj_stream_private_t *p_stream,
365 opj_event_mgr_t * p_manager );
370 * @param p_jp2 the jpeg2000 codec.
371 * @param p_tile_index FIXME DOC
372 * @param p_data FIXME DOC
373 * @param p_data_size FIXME DOC
374 * @param p_stream the stream to write data to.
375 * @param p_manager the user event manager.
377 OPJ_BOOL opj_jp2_write_tile ( opj_jp2_t *p_jp2,
378 OPJ_UINT32 p_tile_index,
380 OPJ_UINT32 p_data_size,
381 opj_stream_private_t *p_stream,
382 opj_event_mgr_t * p_manager );
386 * @param p_jp2 the jpeg2000 codec.
387 * @param p_tile_index FIXME DOC
388 * @param p_data FIXME DOC
389 * @param p_data_size FIXME DOC
390 * @param p_stream the stream to write data to.
391 * @param p_manager the user event manager.
395 OPJ_BOOL opj_jp2_decode_tile ( opj_jp2_t * p_jp2,
396 OPJ_UINT32 p_tile_index,
398 OPJ_UINT32 p_data_size,
399 opj_stream_private_t *p_stream,
400 opj_event_mgr_t * p_manager );
403 * Creates a jpeg2000 file decompressor.
405 * @return an empty jpeg2000 file codec.
407 opj_jp2_t* opj_jp2_create (OPJ_BOOL p_is_decoder);
410 Destroy a JP2 decompressor handle
411 @param jp2 JP2 decompressor handle to destroy
413 void opj_jp2_destroy(opj_jp2_t *jp2);
417 * Sets the given area to be decoded. This function should be called right after opj_read_header and before any tile header reading.
419 * @param p_jp2 the jpeg2000 codec.
420 * @param p_image FIXME DOC
421 * @param p_start_x the left position of the rectangle to decode (in image coordinates).
422 * @param p_start_y the up position of the rectangle to decode (in image coordinates).
423 * @param p_end_x the right position of the rectangle to decode (in image coordinates).
424 * @param p_end_y the bottom position of the rectangle to decode (in image coordinates).
425 * @param p_manager the user event manager
427 * @return true if the area could be set.
429 OPJ_BOOL opj_jp2_set_decode_area( opj_jp2_t *p_jp2,
430 opj_image_t* p_image,
431 OPJ_INT32 p_start_x, OPJ_INT32 p_start_y,
432 OPJ_INT32 p_end_x, OPJ_INT32 p_end_y,
433 opj_event_mgr_t * p_manager );
438 OPJ_BOOL opj_jp2_get_tile( opj_jp2_t *p_jp2,
439 opj_stream_private_t *p_stream,
440 opj_image_t* p_image,
441 opj_event_mgr_t * p_manager,
442 OPJ_UINT32 tile_index );
448 OPJ_BOOL opj_jp2_set_decoded_resolution_factor(opj_jp2_t *p_jp2,
449 OPJ_UINT32 res_factor,
450 opj_event_mgr_t * p_manager);
453 /* TODO MSD: clean these 3 functions */
455 * Dump some elements from the JP2 decompression structure .
457 *@param p_jp2 the jp2 codec.
458 *@param flag flag to describe what elments are dump.
459 *@param out_stream output stream where dump the elements.
462 void jp2_dump (opj_jp2_t* p_jp2, OPJ_INT32 flag, FILE* out_stream);
465 * Get the codestream info from a JPEG2000 codec.
467 *@param p_jp2 jp2 codec.
469 *@return the codestream information extract from the jpg2000 codec
471 opj_codestream_info_v2_t* jp2_get_cstr_info(opj_jp2_t* p_jp2);
474 * Get the codestream index from a JPEG2000 codec.
476 *@param p_jp2 jp2 codec.
478 *@return the codestream index extract from the jpg2000 codec
480 opj_codestream_index_t* jp2_get_cstr_index(opj_jp2_t* p_jp2);