[trunk] WIP: add a read CBD marker function (JPEG2000 part 2)
[openjpeg.git] / libopenjpeg / jp2.h
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) 2002-2003, Yannick Verschueren
5  * Copyright (c) 2005, Herve Drolon, FreeImage Team
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
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.
16  *
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.
28  */
29 #ifndef __JP2_H
30 #define __JP2_H
31 /**
32 @file jp2.h
33 @brief The JPEG-2000 file format Reader/Writer (JP2)
34
35 */
36
37 /** @defgroup JP2 JP2 - JPEG-2000 file format reader/writer */
38 /*@{*/
39
40 #define JPIP_JPIP 0x6a706970
41
42 #define JP2_JP   0x6a502020             /**< JPEG 2000 signature box */
43 #define JP2_FTYP 0x66747970             /**< File type box */
44
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) */
54
55 #define JP2_JP2C 0x6a703263             /**< Contiguous codestream box */
56
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 */
64
65 #define JP2_DTBL 0x6474626c             /**< Data Reference box */
66
67 #define JP2_JP2  0x6a703220             /**< File type fields */
68
69
70 /* ----------------------------------------------------------------------- */
71
72 typedef enum
73 {
74         JP2_STATE_NONE                  = 0x0,
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               = 0x80000000
81 }
82 JP2_STATE;
83
84 typedef enum
85 {
86         JP2_IMG_STATE_NONE                      = 0x0,
87         JP2_IMG_STATE_UNKNOWN           = 0x80000000
88 }
89 JP2_IMG_STATE;
90
91 /** 
92 Channel description: channel index, type, assocation
93 */
94 typedef struct opj_jp2_cdef_info
95 {
96     unsigned short cn, typ, asoc;
97 } opj_jp2_cdef_info_t;
98
99 /** 
100 Channel descriptions and number of descriptions
101 */
102 typedef struct opj_jp2_cdef
103 {
104     opj_jp2_cdef_info_t *info;
105     unsigned short n;
106 } opj_jp2_cdef_t;
107
108 /** 
109 Component mappings: channel index, mapping type, palette index
110 */
111 typedef struct opj_jp2_cmap_comp
112 {
113     unsigned short cmp;
114     unsigned char mtyp, pcol;
115 } opj_jp2_cmap_comp_t;
116
117 /** 
118 Palette data: table entries, palette columns
119 */
120 typedef struct opj_jp2_pclr
121 {
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;
127 } opj_jp2_pclr_t;
128
129 /** 
130 Collector for ICC profile, palette, component mapping, channel description 
131 */
132 typedef struct opj_jp2_color
133 {
134     unsigned char *icc_profile_buf;
135     int icc_profile_len;
136
137     opj_jp2_cdef_t *jp2_cdef;
138     opj_jp2_pclr_t *jp2_pclr;
139     unsigned char jp2_has_colr;
140 } opj_jp2_color_t;
141
142 /** 
143 JP2 component
144 */
145 typedef struct opj_jp2_comps {
146   int depth;              
147   int sgnd;                
148   OPJ_UINT32 bpcc;
149 } opj_jp2_comps_t;
150
151 /**
152 JPEG-2000 file format reader/writer
153 */
154 typedef struct opj_jp2 {
155         /** codec context */
156         opj_common_ptr cinfo;
157         /** handle to the J2K codec  */
158         opj_j2k_t *j2k;
159         unsigned int w;
160         unsigned int h;
161         unsigned int numcomps;
162         unsigned int bpc;
163         unsigned int C;
164         unsigned int UnkC;
165         unsigned int IPR;
166         unsigned int meth;
167         unsigned int approx;
168         unsigned int enumcs;
169         unsigned int precedence;
170         unsigned int brand;
171         unsigned int minversion;
172         unsigned int numcl;
173         unsigned int *cl;
174         opj_jp2_comps_t *comps;
175         unsigned int j2k_codestream_offset;
176         unsigned int j2k_codestream_length;
177         opj_bool jpip_on;
178 } opj_jp2_t;
179
180 /**
181 JPEG-2000 file format reader/writer
182 */
183 typedef struct opj_jp2_v2
184 {
185         /** handle to the J2K codec  */
186         struct opj_j2k_v2 *j2k;
187         /** list of validation procedures */
188         struct opj_procedure_list * m_validation_list;
189         /** list of execution procedures */
190         struct opj_procedure_list * m_procedure_list;
191
192         /* width of image */
193         OPJ_UINT32 w;
194         /* height of image */
195         OPJ_UINT32 h;
196         /* number of components in the image */
197         OPJ_UINT32 numcomps;
198         OPJ_UINT32 bpc;
199         OPJ_UINT32 C;
200         OPJ_UINT32 UnkC;
201         OPJ_UINT32 IPR;
202         OPJ_UINT32 meth;
203         OPJ_UINT32 approx;
204         OPJ_UINT32 enumcs;
205         OPJ_UINT32 precedence;
206         OPJ_UINT32 brand;
207         OPJ_UINT32 minversion;
208         OPJ_UINT32 numcl;
209         OPJ_UINT32 *cl;
210         opj_jp2_comps_t *comps;
211         OPJ_UINT32 j2k_codestream_offset;
212         OPJ_UINT32 jp2_state;
213         OPJ_UINT32 jp2_img_state;
214
215         opj_jp2_color_t color;
216
217 }
218 opj_jp2_v2_t;
219
220 /**
221 JP2 Box
222 */
223 typedef struct opj_jp2_box {
224   OPJ_UINT32 length;
225   OPJ_UINT32 type;
226   OPJ_INT32 init_pos;
227 } opj_jp2_box_t;
228
229 typedef struct opj_jp2_header_handler
230 {
231         /* marker value */
232         int id;
233         /* action linked to the marker */
234         opj_bool (*handler) (opj_jp2_v2_t *jp2, unsigned char * p_header_data, OPJ_UINT32 p_header_size, struct opj_event_mgr * p_manager);
235 }
236 opj_jp2_header_handler_t;
237
238 /** @name Exported functions */
239 /*@{*/
240 /* ----------------------------------------------------------------------- */
241 /**
242 Write the JP2H box - JP2 Header box (used in MJ2)
243 @param jp2 JP2 handle
244 @param cio Output buffer stream
245 */
246 void jp2_write_jp2h(opj_jp2_t *jp2, opj_cio_t *cio);
247 /**
248 Read the JP2H box - JP2 Header box (used in MJ2)
249 @param jp2 JP2 handle
250 @param cio Input buffer stream
251 @param ext Collector for profile, cdef and pclr data
252 @return Returns true if successful, returns false otherwise
253 */
254 opj_bool jp2_read_jp2h(opj_jp2_t *jp2, opj_cio_t *cio, opj_jp2_color_t *color);
255 /**
256 Creates a JP2 decompression structure
257 @param cinfo Codec context info
258 @return Returns a handle to a JP2 decompressor if successful, returns NULL otherwise
259 */
260 opj_jp2_t* jp2_create_decompress(opj_common_ptr cinfo);
261 /**
262 Destroy a JP2 decompressor handle
263 @param jp2 JP2 decompressor handle to destroy
264 */
265 void jp2_destroy_decompress(opj_jp2_t *jp2);
266 /**
267 Setup the decoder decoding parameters using user parameters.
268 Decoding parameters are returned in jp2->j2k->cp. 
269 @param jp2 JP2 decompressor handle
270 @param parameters decompression parameters
271 */
272 void jp2_setup_decoder(opj_jp2_t *jp2, opj_dparameters_t *parameters);
273 /**
274 Setup the decoder decoding parameters using user parameters.
275 Decoding parameters are returned in jp2->j2k->cp.
276 @param jp2 JP2 decompressor handle
277 @param parameters decompression parameters
278 */
279 void jp2_setup_decoder_v2(opj_jp2_v2_t *jp2, opj_dparameters_t *parameters);
280 /**
281 Decode an image from a JPEG-2000 file stream
282 @param jp2 JP2 decompressor handle
283 @param cio Input buffer stream
284 @param cstr_info Codestream information structure if required, NULL otherwise
285 @return Returns a decoded image if successful, returns NULL otherwise
286 */
287 opj_image_t* opj_jp2_decode(opj_jp2_t *jp2, opj_cio_t *cio, opj_codestream_info_t *cstr_info);
288
289 /**
290  * Decode an image from a JPEG-2000 file stream
291  * @param jp2 JP2 decompressor handle
292  * @param cio Input buffer stream
293  * @param cstr_info Codestream information structure if required, NULL otherwise
294  * @return Returns a decoded image if successful, returns NULL otherwise
295 */
296 opj_bool opj_jp2_decode_v2(     opj_jp2_v2_t *jp2,
297                                                         struct opj_stream_private *cio,
298                                                         opj_image_t* p_image,
299                                                         struct opj_event_mgr * p_manager);
300
301
302 /**
303 Creates a JP2 compression structure
304 @param cinfo Codec context info
305 @return Returns a handle to a JP2 compressor if successful, returns NULL otherwise
306 */
307 opj_jp2_t* jp2_create_compress(opj_common_ptr cinfo);
308 /**
309 Destroy a JP2 compressor handle
310 @param jp2 JP2 compressor handle to destroy
311 */
312 void jp2_destroy_compress(opj_jp2_t *jp2);
313 /**
314 Setup the encoder parameters using the current image and using user parameters. 
315 Coding parameters are returned in jp2->j2k->cp. 
316 @param jp2 JP2 compressor handle
317 @param parameters compression parameters
318 @param image input filled image
319 */
320 void jp2_setup_encoder(opj_jp2_t *jp2, opj_cparameters_t *parameters, opj_image_t *image);
321 /**
322 Encode an image into a JPEG-2000 file stream
323 @param jp2 JP2 compressor handle
324 @param cio Output buffer stream
325 @param image Image to encode
326 @param cstr_info Codestream information structure if required, NULL otherwise
327 @return Returns true if successful, returns false otherwise
328 */
329 opj_bool opj_jp2_encode(opj_jp2_t *jp2, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info);
330
331
332 /* ----------------------------------------------------------------------- */
333
334 /**
335  * Ends the decompression procedures and possibiliy add data to be read after the
336  * codestream.
337  */
338 opj_bool jp2_end_decompress(opj_jp2_v2_t *jp2, struct opj_stream_private *cio, struct opj_event_mgr * p_manager);
339
340 /**
341  * Reads a jpeg2000 file header structure.
342  *
343  * @param cio the stream to read data from.
344  * @param jp2 the jpeg2000 file header structure.
345  * @param p_manager the user event manager.
346  *
347  * @return true if the box is valid.
348  */
349 opj_bool jp2_read_header(       struct opj_stream_private *p_stream,
350                                                         opj_jp2_v2_t *jp2,
351                                                         opj_image_t * p_img_header,
352                                                         struct opj_event_mgr * p_manager
353                                                         );
354
355 /**
356  * Reads a tile header.
357  * @param       p_j2k           the jpeg2000 codec.
358  * @param       p_stream                        the stream to write data to.
359  * @param       p_manager       the user event manager.
360  */
361 opj_bool jp2_read_tile_header (
362                                          opj_jp2_v2_t * p_j2k,
363                                          OPJ_UINT32 * p_tile_index,
364                                          OPJ_UINT32 * p_data_size,
365                                          OPJ_INT32 * p_tile_x0,
366                                          OPJ_INT32 * p_tile_y0,
367                                          OPJ_INT32 * p_tile_x1,
368                                          OPJ_INT32 * p_tile_y1,
369                                          OPJ_UINT32 * p_nb_comps,
370                                          opj_bool * p_go_on,
371                                          struct opj_stream_private *p_stream,
372                                          struct opj_event_mgr * p_manager
373                                         );
374
375 /**
376  * Decode tile data.
377  * @param       p_j2k           the jpeg2000 codec.
378  * @param       p_stream                        the stream to write data to.
379  * @param       p_manager       the user event manager.
380  */
381 opj_bool opj_jp2_decode_tile (
382                                         opj_jp2_v2_t * p_jp2,
383                                         OPJ_UINT32 p_tile_index,
384                                         OPJ_BYTE * p_data,
385                                         OPJ_UINT32 p_data_size,
386                                         struct opj_stream_private *p_stream,
387                                         struct opj_event_mgr * p_manager
388                                         );
389
390 /**
391  * Creates a jpeg2000 file decompressor.
392  *
393  * @return      an empty jpeg2000 file codec.
394  */
395 opj_jp2_v2_t* jp2_create (opj_bool p_is_decoder);
396
397 /**
398 Destroy a JP2 decompressor handle
399 @param jp2 JP2 decompressor handle to destroy
400 */
401 void jp2_destroy(opj_jp2_v2_t *jp2);
402
403
404 /**
405  * Sets the given area to be decoded. This function should be called right after opj_read_header and before any tile header reading.
406  *
407  * @param       p_jp2                   the jpeg2000 codec.
408  * @param       p_end_x                 the right position of the rectangle to decode (in image coordinates).
409  * @param       p_start_y               the up position of the rectangle to decode (in image coordinates).
410  * @param       p_end_y                 the bottom position of the rectangle to decode (in image coordinates).
411  * @param       p_manager               the user event manager
412  *
413  * @return      true                    if the area could be set.
414  */
415 opj_bool jp2_set_decode_area(
416                         opj_jp2_v2_t *p_jp2,
417                         OPJ_INT32 p_start_x,
418                         OPJ_INT32 p_start_y,
419                         OPJ_INT32 p_end_x,
420                         OPJ_INT32 p_end_y,
421                         struct opj_event_mgr * p_manager
422                         );
423
424
425 /**
426  * Dump some elements from the JP2 decompression structure .
427  *
428  *@param p_jp2                          the jp2 codec.
429  *@param flag                           flag to describe what elments are dump.
430  *@param out_stream                     output stream where dump the elements.
431  *
432 */
433 void jp2_dump (opj_jp2_v2_t* p_jp2, OPJ_INT32 flag, FILE* out_stream);
434
435 /**
436  * Get the codestream info from a JPEG2000 codec.
437  *
438  *@param        p_jp2                           jp2 codec.
439  *
440  *@return       the codestream information extract from the jpg2000 codec
441  */
442 opj_codestream_info_v2_t* jp2_get_cstr_info(opj_jp2_v2_t* p_jp2);
443
444 /**
445  * Get the codestream index from a JPEG2000 codec.
446  *
447  *@param        p_jp2                           jp2 codec.
448  *
449  *@return       the codestream index extract from the jpg2000 codec
450  */
451 opj_codestream_index_t* jp2_get_cstr_index(opj_jp2_v2_t* p_jp2);
452
453 /*@}*/
454
455 /*@}*/
456
457 #endif /* __JP2_H */
458