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 */
45 #define JP2_JP2H 0x6a703268 /**< JP2 header box (super-box) */
46 #define JP2_IHDR 0x69686472 /**< Image header box */
47 #define JP2_BPCC 0x62706363 /**< Bits per component box */
48 #define JP2_COLR 0x636f6c72 /**< Colour specification 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 /* For the future MSD */
53 /* #define JP2_RES 0x72657320 */ /**< Resolution box (super-box) */
55 #define JP2_JP2C 0x6a703263 /**< Contiguous codestream box */
57 /* For the future MSD */
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 */
63 #define JP2_URL 0x75726c20 /**< Data entry URL box */
65 #define JP2_DTBL 0x6474626c /**< Data Reference box */
67 #define JP2_JP2 0x6a703220 /**< File type fields */
70 /* ----------------------------------------------------------------------- */
75 JP2_STATE_SIGNATURE = 0x1,
76 JP2_STATE_FILE_TYPE = 0x2,
77 JP2_STATE_HEADER = 0x4,
78 JP2_STATE_CODESTREAM = 0x8,
79 JP2_STATE_END_CODESTREAM = 0x10,
80 JP2_STATE_UNKNOWN = 0x7fffffff /* ISO C restricts enumerator values to range of 'int' */
86 JP2_IMG_STATE_NONE = 0x0,
87 JP2_IMG_STATE_UNKNOWN = 0x7fffffff
92 Channel description: channel index, type, assocation
94 typedef struct opj_jp2_cdef_info
96 unsigned short cn, typ, asoc;
97 } opj_jp2_cdef_info_t;
100 Channel descriptions and number of descriptions
102 typedef struct opj_jp2_cdef
104 opj_jp2_cdef_info_t *info;
109 Component mappings: channel index, mapping type, palette index
111 typedef struct opj_jp2_cmap_comp
114 unsigned char mtyp, pcol;
115 } opj_jp2_cmap_comp_t;
118 Palette data: table entries, palette columns
120 typedef struct opj_jp2_pclr
122 unsigned int *entries;
123 unsigned char *channel_sign;
124 unsigned char *channel_size;
125 opj_jp2_cmap_comp_t *cmap;
126 unsigned short nr_entries, nr_channels;
130 Collector for ICC profile, palette, component mapping, channel description
132 typedef struct opj_jp2_color
134 unsigned char *icc_profile_buf;
137 opj_jp2_cdef_t *jp2_cdef;
138 opj_jp2_pclr_t *jp2_pclr;
139 unsigned char jp2_has_colr;
145 typedef struct opj_jp2_comps {
152 JPEG-2000 file format reader/writer
154 typedef struct opj_jp2 {
156 opj_common_ptr cinfo;
157 /** handle to the J2K codec */
161 unsigned int numcomps;
169 unsigned int precedence;
171 unsigned int minversion;
174 opj_jp2_comps_t *comps;
175 unsigned int j2k_codestream_offset;
176 unsigned int j2k_codestream_length;
178 opj_bool ignore_pclr_cmap_cdef;
182 JPEG-2000 file format reader/writer
184 typedef struct opj_jp2_v2
186 /** handle to the J2K codec */
187 struct opj_j2k_v2 *j2k;
188 /** list of validation procedures */
189 struct opj_procedure_list * m_validation_list;
190 /** list of execution procedures */
191 struct opj_procedure_list * m_procedure_list;
195 /* height of image */
197 /* number of components in the image */
206 OPJ_UINT32 precedence;
208 OPJ_UINT32 minversion;
211 opj_jp2_comps_t *comps;
212 OPJ_UINT32 j2k_codestream_offset;
213 OPJ_UINT32 jp2_state;
214 OPJ_UINT32 jp2_img_state;
216 opj_jp2_color_t color;
218 opj_bool ignore_pclr_cmap_cdef;
225 typedef struct opj_jp2_box {
231 typedef struct opj_jp2_header_handler
235 /* action linked to the marker */
236 opj_bool (*handler) (opj_jp2_v2_t *jp2, unsigned char * p_header_data, OPJ_UINT32 p_header_size, struct opj_event_mgr * p_manager);
238 opj_jp2_header_handler_t;
241 typedef struct opj_jp2_img_header_writer_handler
243 /* action to perform */
244 unsigned char* (*handler) (opj_jp2_v2_t *jp2, unsigned int * p_data_size);
245 /* result of the action : data */
246 unsigned char * m_data;
250 opj_jp2_img_header_writer_handler_t;
252 /** @name Exported functions */
254 /* ----------------------------------------------------------------------- */
256 Write the JP2H box - JP2 Header box (used in MJ2)
257 @param jp2 JP2 handle
258 @param cio Output buffer stream
260 void jp2_write_jp2h(opj_jp2_t *jp2, opj_cio_t *cio);
263 * Writes the Jpeg2000 file Header box - JP2 Header box (warning, this is a super box).
265 * @param cio the stream to write data to.
266 * @param jp2 the jpeg2000 file codec.
267 * @param p_manager user event manager.
269 * @return true if writting was successful.
271 opj_bool jp2_write_jp2h_v2( opj_jp2_v2_t *jp2,
272 struct opj_stream_private *cio,
273 struct opj_event_mgr * p_manager );
276 Read the JP2H box - JP2 Header box (used in MJ2)
277 @param jp2 JP2 handle
278 @param cio Input buffer stream
279 @param ext Collector for profile, cdef and pclr data
280 @return Returns true if successful, returns false otherwise
282 opj_bool jp2_read_jp2h(opj_jp2_t *jp2, opj_cio_t *cio, opj_jp2_color_t *color);
284 Creates a JP2 decompression structure
285 @param cinfo Codec context info
286 @return Returns a handle to a JP2 decompressor if successful, returns NULL otherwise
288 opj_jp2_t* jp2_create_decompress(opj_common_ptr cinfo);
290 Destroy a JP2 decompressor handle
291 @param jp2 JP2 decompressor handle to destroy
293 void jp2_destroy_decompress(opj_jp2_t *jp2);
296 Setup the decoder decoding parameters using user parameters.
297 Decoding parameters are returned in jp2->j2k->cp.
298 @param jp2 JP2 decompressor handle
299 @param parameters decompression parameters
301 void jp2_setup_decoder_v2(opj_jp2_v2_t *jp2, opj_dparameters_t *parameters);
303 Decode an image from a JPEG-2000 file stream
304 @param jp2 JP2 decompressor handle
305 @param cio Input buffer stream
306 @param cstr_info Codestream information structure if required, NULL otherwise
307 @return Returns a decoded image if successful, returns NULL otherwise
309 opj_image_t* opj_jp2_decode(opj_jp2_t *jp2, opj_cio_t *cio, opj_codestream_info_t *cstr_info);
312 * Decode an image from a JPEG-2000 file stream
313 * @param jp2 JP2 decompressor handle
314 * @param cio Input buffer stream
315 * @param cstr_info Codestream information structure if required, NULL otherwise
316 * @return Returns a decoded image if successful, returns NULL otherwise
318 opj_bool jp2_decode_v2( opj_jp2_v2_t *jp2,
319 struct opj_stream_private *cio,
320 opj_image_t* p_image,
321 struct opj_event_mgr * p_manager);
325 Creates a JP2 compression structure
326 @param cinfo Codec context info
327 @return Returns a handle to a JP2 compressor if successful, returns NULL otherwise
329 opj_jp2_t* jp2_create_compress(opj_common_ptr cinfo);
331 Destroy a JP2 compressor handle
332 @param jp2 JP2 compressor handle to destroy
334 void jp2_destroy_compress(opj_jp2_t *jp2);
336 Setup the encoder parameters using the current image and using user parameters.
337 Coding parameters are returned in jp2->j2k->cp.
338 @param jp2 JP2 compressor handle
339 @param parameters compression parameters
340 @param image input filled image
342 void jp2_setup_encoder( opj_jp2_v2_t *jp2,
343 opj_cparameters_t *parameters,
345 struct opj_event_mgr * p_manager);
347 Encode an image into a JPEG-2000 file stream
348 @param jp2 JP2 compressor handle
349 @param cio Output buffer stream
350 @param image Image to encode
351 @param cstr_info Codestream information structure if required, NULL otherwise
352 @return Returns true if successful, returns false otherwise
354 opj_bool opj_jp2_encode(opj_jp2_t *jp2, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info);
357 Encode an image into a JPEG-2000 file stream
358 @param jp2 JP2 compressor handle
359 @param stream Output buffer stream
360 @param p_manager event manager
361 @return Returns true if successful, returns false otherwise
363 opj_bool opj_jp2_encode_v2( opj_jp2_v2_t *jp2,
364 opj_stream_private_t *stream,
365 opj_event_mgr_t * p_manager);
369 * Starts a compression scheme, i.e. validates the codec parameters, writes the header.
371 * @param jp2 the jpeg2000 file codec.
372 * @param cio the stream object.
374 * @return true if the codec is valid.
376 opj_bool jp2_start_compress(opj_jp2_v2_t *jp2,
377 struct opj_stream_private *cio,
378 struct opj_image * p_image,
379 struct opj_event_mgr * p_manager);
383 * Ends the compression procedures and possibiliy add data to be read after the
386 opj_bool jp2_end_compress( opj_jp2_v2_t *jp2,
387 struct opj_stream_private *cio,
388 struct opj_event_mgr * p_manager);
390 /* ----------------------------------------------------------------------- */
393 * Ends the decompression procedures and possibiliy add data to be read after the
396 opj_bool jp2_end_decompress(opj_jp2_v2_t *jp2, struct opj_stream_private *cio, struct opj_event_mgr * p_manager);
399 * Reads a jpeg2000 file header structure.
401 * @param cio the stream to read data from.
402 * @param jp2 the jpeg2000 file header structure.
403 * @param p_manager the user event manager.
405 * @return true if the box is valid.
407 opj_bool jp2_read_header( struct opj_stream_private *p_stream,
409 opj_image_t ** p_img_header,
410 struct opj_event_mgr * p_manager
414 * Reads a tile header.
415 * @param p_j2k the jpeg2000 codec.
416 * @param p_stream the stream to write data to.
417 * @param p_manager the user event manager.
419 opj_bool jp2_read_tile_header (
420 opj_jp2_v2_t * p_j2k,
421 OPJ_UINT32 * p_tile_index,
422 OPJ_UINT32 * p_data_size,
423 OPJ_INT32 * p_tile_x0,
424 OPJ_INT32 * p_tile_y0,
425 OPJ_INT32 * p_tile_x1,
426 OPJ_INT32 * p_tile_y1,
427 OPJ_UINT32 * p_nb_comps,
429 struct opj_stream_private *p_stream,
430 struct opj_event_mgr * p_manager
437 * @param p_j2k the jpeg2000 codec.
438 * @param p_stream the stream to write data to.
439 * @param p_manager the user event manager.
441 opj_bool jp2_write_tile ( opj_jp2_v2_t *p_jp2,
442 OPJ_UINT32 p_tile_index,
444 OPJ_UINT32 p_data_size,
445 struct opj_stream_private *p_stream,
446 struct opj_event_mgr * p_manager );
450 * @param p_j2k the jpeg2000 codec.
451 * @param p_stream the stream to write data to.
452 * @param p_manager the user event manager.
454 opj_bool jp2_decode_tile (
455 opj_jp2_v2_t * p_jp2,
456 OPJ_UINT32 p_tile_index,
458 OPJ_UINT32 p_data_size,
459 struct opj_stream_private *p_stream,
460 struct opj_event_mgr * p_manager
464 * Creates a jpeg2000 file decompressor.
466 * @return an empty jpeg2000 file codec.
468 opj_jp2_v2_t* jp2_create (opj_bool p_is_decoder);
471 Destroy a JP2 decompressor handle
472 @param jp2 JP2 decompressor handle to destroy
474 void jp2_destroy(opj_jp2_v2_t *jp2);
478 * Sets the given area to be decoded. This function should be called right after opj_read_header and before any tile header reading.
480 * @param p_jp2 the jpeg2000 codec.
481 * @param p_end_x the right position of the rectangle to decode (in image coordinates).
482 * @param p_start_y the up position of the rectangle to decode (in image coordinates).
483 * @param p_end_y the bottom position of the rectangle to decode (in image coordinates).
484 * @param p_manager the user event manager
486 * @return true if the area could be set.
488 opj_bool jp2_set_decode_area( opj_jp2_v2_t *p_jp2,
489 opj_image_t* p_image,
490 OPJ_INT32 p_start_x, OPJ_INT32 p_start_y,
491 OPJ_INT32 p_end_x, OPJ_INT32 p_end_y,
492 struct opj_event_mgr * p_manager );
494 opj_bool jp2_get_tile( opj_jp2_v2_t *p_jp2,
495 opj_stream_private_t *p_stream,
496 opj_image_t* p_image,
497 struct opj_event_mgr * p_manager,
498 OPJ_UINT32 tile_index );
502 * Dump some elements from the JP2 decompression structure .
504 *@param p_jp2 the jp2 codec.
505 *@param flag flag to describe what elments are dump.
506 *@param out_stream output stream where dump the elements.
509 void jp2_dump (opj_jp2_v2_t* p_jp2, OPJ_INT32 flag, FILE* out_stream);
512 * Get the codestream info from a JPEG2000 codec.
514 *@param p_jp2 jp2 codec.
516 *@return the codestream information extract from the jpg2000 codec
518 opj_codestream_info_v2_t* jp2_get_cstr_info(opj_jp2_v2_t* p_jp2);
521 * Get the codestream index from a JPEG2000 codec.
523 *@param p_jp2 jp2 codec.
525 *@return the codestream index extract from the jpg2000 codec
527 opj_codestream_index_t* jp2_get_cstr_index(opj_jp2_v2_t* p_jp2);
529 opj_bool jp2_set_decoded_resolution_factor(opj_jp2_v2_t *p_jp2, OPJ_UINT32 res_factor, opj_event_mgr_t * p_manager);