[trunk] update jp2./.h to v2 style: use new OPJ type, manage case where bpcc box...
[openjpeg.git] / src / lib / openjp2 / 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 #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 */
55
56 /* For the future */
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 */
63
64 /* ----------------------------------------------------------------------- */
65
66 typedef enum
67 {
68   JP2_STATE_NONE            = 0x0,
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' */
75 }
76 JP2_STATE;
77
78 typedef enum
79 {
80   JP2_IMG_STATE_NONE        = 0x0,
81   JP2_IMG_STATE_UNKNOWN     = 0x7fffffff
82 }
83 JP2_IMG_STATE;
84
85 /** 
86 Channel description: channel index, type, assocation
87 */
88 typedef struct opj_jp2_cdef_info
89 {
90     OPJ_UINT16 cn, typ, asoc;
91 } opj_jp2_cdef_info_t;
92
93 /** 
94 Channel descriptions and number of descriptions
95 */
96 typedef struct opj_jp2_cdef
97 {
98     opj_jp2_cdef_info_t *info;
99     OPJ_UINT16 n;
100 } opj_jp2_cdef_t;
101
102 /** 
103 Component mappings: channel index, mapping type, palette index
104 */
105 typedef struct opj_jp2_cmap_comp
106 {
107     OPJ_UINT16 cmp;
108     OPJ_BYTE mtyp, pcol;
109 } opj_jp2_cmap_comp_t;
110
111 /** 
112 Palette data: table entries, palette columns
113 */
114 typedef struct opj_jp2_pclr
115 {
116     OPJ_UINT32 *entries;
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;
122 } opj_jp2_pclr_t;
123
124 /** 
125 Collector for ICC profile, palette, component mapping, channel description 
126 */
127 typedef struct opj_jp2_color
128 {
129     OPJ_BYTE *icc_profile_buf;
130     OPJ_UINT32 icc_profile_len;
131
132     opj_jp2_cdef_t *jp2_cdef;
133     opj_jp2_pclr_t *jp2_pclr;
134     OPJ_BYTE jp2_has_colr;
135 } opj_jp2_color_t;
136
137 /** 
138 JP2 component
139 */
140 typedef struct opj_jp2_comps {
141   OPJ_UINT32 depth;      
142   OPJ_UINT32 sgnd;       
143   OPJ_UINT32 bpcc;
144 } opj_jp2_comps_t;
145
146 /**
147 JPEG-2000 file format reader/writer
148 */
149 typedef struct opj_jp2 {
150   /** codec context */
151   opj_common_ptr cinfo;
152   /** handle to the J2K codec  */
153   opj_j2k_t *j2k;
154   unsigned int w;
155   unsigned int h;
156   unsigned int numcomps;
157   unsigned int bpc;
158   unsigned int C;
159   unsigned int UnkC;
160   unsigned int IPR;
161   unsigned int meth;
162   unsigned int approx;
163   unsigned int enumcs;
164   unsigned int precedence;
165   unsigned int brand;
166   unsigned int minversion;
167   unsigned int numcl;
168   unsigned int *cl;
169   opj_jp2_comps_t *comps;
170   unsigned int j2k_codestream_offset;
171   unsigned int j2k_codestream_length;
172   opj_bool ignore_pclr_cmap_cdef;
173 } opj_jp2_t;
174
175 /**
176 JPEG-2000 file format reader/writer
177 */
178 typedef struct opj_jp2_v2
179 {
180   /** handle to the J2K codec  */
181   struct opj_j2k_v2 *j2k;
182   /** list of validation procedures */
183   struct opj_procedure_list * m_validation_list;
184   /** list of execution procedures */
185   struct opj_procedure_list * m_procedure_list;
186
187   /* width of image */
188   OPJ_UINT32 w;
189   /* height of image */
190   OPJ_UINT32 h;
191   /* number of components in the image */
192   OPJ_UINT32 numcomps;
193   OPJ_UINT32 bpc;
194   OPJ_UINT32 C;
195   OPJ_UINT32 UnkC;
196   OPJ_UINT32 IPR;
197   OPJ_UINT32 meth;
198   OPJ_UINT32 approx;
199   OPJ_UINT32 enumcs;
200   OPJ_UINT32 precedence;
201   OPJ_UINT32 brand;
202   OPJ_UINT32 minversion;
203   OPJ_UINT32 numcl;
204   OPJ_UINT32 *cl;
205   opj_jp2_comps_t *comps;
206   /* FIXME: The following two variables are used to save offset
207     as we write out a JP2 file to disk. This mecanism is not flexible
208     as codec writers will need to extand those fields as new part
209     of the standard are implemented.
210   */
211     OPJ_OFF_T j2k_codestream_offset;
212     OPJ_OFF_T jpip_iptr_offset;
213   OPJ_UINT32 jp2_state;
214   OPJ_UINT32 jp2_img_state;
215
216   opj_jp2_color_t color;
217     
218     opj_bool ignore_pclr_cmap_cdef;
219 }
220 opj_jp2_v2_t;
221
222 /**
223 JP2 Box
224 */
225 typedef struct opj_jp2_box {
226     OPJ_UINT32 length;
227     OPJ_UINT32 type;
228     OPJ_INT32 init_pos;
229 } opj_jp2_box_t;
230
231 typedef struct opj_jp2_header_handler
232 {
233   /* marker value */
234   OPJ_UINT32 id;
235   /* action linked to the marker */
236   opj_bool (*handler) (     opj_jp2_v2_t *jp2, 
237                             OPJ_BYTE *p_header_data, 
238                             OPJ_UINT32 p_header_size, 
239                             opj_event_mgr_t * p_manager);
240 }
241 opj_jp2_header_handler_t;
242
243
244 typedef struct opj_jp2_img_header_writer_handler 
245 {
246   /* action to perform */
247   OPJ_BYTE*   (*handler) (opj_jp2_v2_t *jp2, OPJ_UINT32 * p_data_size);
248   /* result of the action : data */
249   OPJ_BYTE*   m_data;
250   /* size of data */
251   OPJ_UINT32  m_size;
252
253 opj_jp2_img_header_writer_handler_t;
254
255 /** @name Exported functions */
256 /*@{*/
257 /* ----------------------------------------------------------------------- */
258
259 /**
260  * Writes the Jpeg2000 file Header box - JP2 Header box (warning, this is a super box).
261  *
262  * @param  jp2      the jpeg2000 file codec.
263  * @param  stream      the stream to write data to.
264  * @param  p_manager  user event manager.
265  *
266  * @return true if writing was successful.
267 */
268 OPJ_API opj_bool OPJ_CALLCONV opj_jp2_write_jp2h(opj_jp2_v2_t *jp2,
269                             opj_stream_private_t *stream,
270                             opj_event_mgr_t * p_manager );
271
272 /**
273 Setup the decoder decoding parameters using user parameters.
274 Decoding parameters are returned in jp2->j2k->cp.
275 @param jp2 JP2 decompressor handle
276 @param parameters decompression parameters
277 */
278 void opj_jp2_setup_decoder(opj_jp2_v2_t *jp2, opj_dparameters_t *parameters);
279
280 /**
281  * Decode an image from a JPEG-2000 file stream
282  * @param jp2 JP2 decompressor handle
283  * @param p_stream  FIXME DOC
284  * @param p_image   FIXME DOC
285  * @param p_manager FIXME DOC
286  *
287  * @return Returns a decoded image if successful, returns NULL otherwise
288 */
289 opj_bool opj_jp2_decode(opj_jp2_v2_t *jp2,
290                         opj_stream_private_t *p_stream,
291             opj_image_t* p_image,
292             opj_event_mgr_t * p_manager);
293
294 /**
295  * Setup the encoder parameters using the current image and using user parameters. 
296  * Coding parameters are returned in jp2->j2k->cp. 
297  *
298  * @param jp2 JP2 compressor handle
299  * @param parameters compression parameters
300  * @param image input filled image
301  * @param p_manager  FIXME DOC
302 */
303 OPJ_API void OPJ_CALLCONV opj_jp2_setup_encoder(  opj_jp2_v2_t *jp2, 
304                             opj_cparameters_t *parameters, 
305                             opj_image_t *image, 
306                             opj_event_mgr_t * p_manager);
307
308 /**
309 Encode an image into a JPEG-2000 file stream
310 @param jp2      JP2 compressor handle
311 @param stream    Output buffer stream
312 @param p_manager  event manager
313 @return Returns true if successful, returns false otherwise
314 */
315 OPJ_API opj_bool OPJ_CALLCONV opj_jp2_encode(  opj_jp2_v2_t *jp2, 
316               opj_stream_private_t *stream, 
317               opj_event_mgr_t * p_manager);
318
319
320 /**
321  * Starts a compression scheme, i.e. validates the codec parameters, writes the header.
322  *
323  * @param  jp2    the jpeg2000 file codec.
324  * @param  stream    the stream object.
325  * @param  p_image   FIXME DOC
326  * @param p_manager FIXME DOC
327  *
328  * @return true if the codec is valid.
329  */
330 opj_bool opj_jp2_start_compress(opj_jp2_v2_t *jp2,
331                                 opj_stream_private_t *stream,
332                                 opj_image_t * p_image,
333                                 opj_event_mgr_t * p_manager);
334
335
336 /**
337  * Ends the compression procedures and possibiliy add data to be read after the
338  * codestream.
339  */
340 opj_bool opj_jp2_end_compress(  opj_jp2_v2_t *jp2,
341                   opj_stream_private_t *cio,
342                   opj_event_mgr_t * p_manager);
343
344 /* ----------------------------------------------------------------------- */
345
346 /**
347  * Ends the decompression procedures and possibiliy add data to be read after the
348  * codestream.
349  */
350 opj_bool opj_jp2_end_decompress(opj_jp2_v2_t *jp2, 
351                                 opj_stream_private_t *cio,
352                                 opj_event_mgr_t * p_manager);
353
354 /**
355  * Reads a jpeg2000 file header structure.
356  *
357  * @param p_stream the stream to read data from.
358  * @param jp2 the jpeg2000 file header structure.
359  * @param p_image   FIXME DOC
360  * @param p_manager the user event manager.
361  *
362  * @return true if the box is valid.
363  */
364 opj_bool opj_jp2_read_header(  opj_stream_private_t *p_stream,
365                                 opj_jp2_v2_t *jp2,
366                                 opj_image_t ** p_image,
367                                 opj_event_mgr_t * p_manager );
368
369 /**
370  * Reads a tile header.
371  * @param  p_jp2         the jpeg2000 codec.
372  * @param  p_tile_index  FIXME DOC
373  * @param  p_data_size   FIXME DOC
374  * @param  p_tile_x0     FIXME DOC
375  * @param  p_tile_y0     FIXME DOC
376  * @param  p_tile_x1     FIXME DOC
377  * @param  p_tile_y1     FIXME DOC
378  * @param  p_nb_comps    FIXME DOC
379  * @param  p_go_on       FIXME DOC
380  * @param  p_stream      the stream to write data to.
381  * @param  p_manager     the user event manager.
382  */
383 opj_bool opj_jp2_read_tile_header ( opj_jp2_v2_t * p_jp2,
384                                     OPJ_UINT32 * p_tile_index,
385                                     OPJ_UINT32 * p_data_size,
386                                     OPJ_INT32 * p_tile_x0,
387                                     OPJ_INT32 * p_tile_y0,
388                                     OPJ_INT32 * p_tile_x1,
389                                     OPJ_INT32 * p_tile_y1,
390                                     OPJ_UINT32 * p_nb_comps,
391                                     opj_bool * p_go_on,
392                                     opj_stream_private_t *p_stream,
393                                     opj_event_mgr_t * p_manager );
394
395 /**
396  * Writes a tile.
397  *
398  * @param  p_jp2    the jpeg2000 codec.
399  * @param p_tile_index  FIXME DOC
400  * @param p_data        FIXME DOC
401  * @param p_data_size   FIXME DOC
402  * @param  p_stream      the stream to write data to.
403  * @param  p_manager  the user event manager.
404  */
405 OPJ_API opj_bool OPJ_CALLCONV opj_jp2_write_tile (  opj_jp2_v2_t *p_jp2,
406                     OPJ_UINT32 p_tile_index,
407                     OPJ_BYTE * p_data,
408                     OPJ_UINT32 p_data_size,
409                     opj_stream_private_t *p_stream,
410                     opj_event_mgr_t * p_manager );
411
412 /**
413  * Decode tile data.
414  * @param  p_jp2    the jpeg2000 codec.
415  * @param  p_tile_index FIXME DOC
416  * @param  p_data       FIXME DOC
417  * @param  p_data_size  FIXME DOC
418  * @param  p_stream      the stream to write data to.
419  * @param  p_manager  the user event manager.
420  *
421  * @return FIXME DOC
422  */
423 opj_bool opj_jp2_decode_tile (  opj_jp2_v2_t * p_jp2,
424                                 OPJ_UINT32 p_tile_index,
425                                 OPJ_BYTE * p_data,
426                                 OPJ_UINT32 p_data_size,
427                                 opj_stream_private_t *p_stream,
428                                 opj_event_mgr_t * p_manager );
429
430 /**
431  * Creates a jpeg2000 file decompressor.
432  *
433  * @return  an empty jpeg2000 file codec.
434  */
435 OPJ_API opj_jp2_v2_t* OPJ_CALLCONV opj_jp2_create (opj_bool p_is_decoder);
436
437 /**
438 Destroy a JP2 decompressor handle
439 @param jp2 JP2 decompressor handle to destroy
440 */
441 OPJ_API void OPJ_CALLCONV opj_jp2_destroy(opj_jp2_v2_t *jp2);
442
443
444 /**
445  * Sets the given area to be decoded. This function should be called right after opj_read_header and before any tile header reading.
446  *
447  * @param  p_jp2      the jpeg2000 codec.
448  * @param  p_image     FIXME DOC
449  * @param  p_start_x   the left position of the rectangle to decode (in image coordinates).
450  * @param  p_start_y    the up position of the rectangle to decode (in image coordinates).
451  * @param  p_end_x      the right position of the rectangle to decode (in image coordinates).
452  * @param  p_end_y      the bottom position of the rectangle to decode (in image coordinates).
453  * @param  p_manager    the user event manager
454  *
455  * @return  true      if the area could be set.
456  */
457 opj_bool opj_jp2_set_decode_area(  opj_jp2_v2_t *p_jp2,
458                     opj_image_t* p_image,
459                     OPJ_INT32 p_start_x, OPJ_INT32 p_start_y,
460                     OPJ_INT32 p_end_x, OPJ_INT32 p_end_y,
461                     opj_event_mgr_t * p_manager );
462
463  /**
464  * 
465  */
466 opj_bool opj_jp2_get_tile(  opj_jp2_v2_t *p_jp2,
467                             opj_stream_private_t *p_stream,
468                             opj_image_t* p_image,
469                             opj_event_mgr_t * p_manager,
470                             OPJ_UINT32 tile_index );
471
472
473 /**
474  * 
475  */
476 opj_bool opj_jp2_set_decoded_resolution_factor(opj_jp2_v2_t *p_jp2, 
477                                                OPJ_UINT32 res_factor, 
478                                                opj_event_mgr_t * p_manager);
479
480
481 /* TODO MSD: clean these 3 functions */
482 /**
483  * Dump some elements from the JP2 decompression structure .
484  *
485  *@param p_jp2        the jp2 codec.
486  *@param flag        flag to describe what elments are dump.
487  *@param out_stream      output stream where dump the elements.
488  *
489 */
490 void jp2_dump (opj_jp2_v2_t* p_jp2, OPJ_INT32 flag, FILE* out_stream);
491
492 /**
493  * Get the codestream info from a JPEG2000 codec.
494  *
495  *@param  p_jp2        jp2 codec.
496  *
497  *@return  the codestream information extract from the jpg2000 codec
498  */
499 OPJ_API opj_codestream_info_v2_t* OPJ_CALLCONV jp2_get_cstr_info(opj_jp2_v2_t* p_jp2);
500
501 /**
502  * Get the codestream index from a JPEG2000 codec.
503  *
504  *@param  p_jp2        jp2 codec.
505  *
506  *@return  the codestream index extract from the jpg2000 codec
507  */
508 opj_codestream_index_t* jp2_get_cstr_index(opj_jp2_v2_t* p_jp2);
509
510
511 /*@}*/
512
513 /*@}*/
514
515 #endif /* __JP2_H */
516