1/*
2 * The copyright in this software is being made available under the 2-clauses
3 * BSD License, included below. This software may be subject to other third
4 * party and contributor rights, including patent rights, and no such rights
5 * are granted under this license.
6 *
7 * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium
8 * Copyright (c) 2002-2014, Professor Benoit Macq
9 * Copyright (c) 2002-2003, Yannick Verschueren
10 * Copyright (c) 2005, Herve Drolon, FreeImage Team
11 * Copyright (c) 2008, 2011-2012, Centre National d'Etudes Spatiales (CNES), FR
12 * Copyright (c) 2012, CS Systemes d'Information, France
13 * All rights reserved.
14 *
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
17 * are met:
18 * 1. Redistributions of source code must retain the above copyright
19 *    notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 *    notice, this list of conditions and the following disclaimer in the
22 *    documentation and/or other materials provided with the distribution.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 * POSSIBILITY OF SUCH DAMAGE.
35 */
36#ifndef __JP2_H
37#define __JP2_H
38/**
39@file jp2.h
40@brief The JPEG-2000 file format Reader/Writer (JP2)
41
42*/
43
44/** @defgroup JP2 JP2 - JPEG-2000 file format reader/writer */
45/*@{*/
46
47/*#define JPIP_JPIP 0x6a706970*/
48
49#define     JP2_JP   0x6a502020    /**< JPEG 2000 signature box */
50#define     JP2_FTYP 0x66747970    /**< File type box */
51#define     JP2_JP2H 0x6a703268    /**< JP2 header box (super-box) */
52#define     JP2_IHDR 0x69686472    /**< Image header box */
53#define     JP2_COLR 0x636f6c72    /**< Colour specification box */
54#define     JP2_JP2C 0x6a703263    /**< Contiguous codestream box */
55#define     JP2_URL  0x75726c20    /**< Data entry URL box */
56#define     JP2_PCLR 0x70636c72    /**< Palette box */
57#define     JP2_CMAP 0x636d6170    /**< Component Mapping box */
58#define     JP2_CDEF 0x63646566    /**< Channel Definition box */
59#define     JP2_DTBL 0x6474626c    /**< Data Reference box */
60#define     JP2_BPCC 0x62706363    /**< Bits per component box */
61#define     JP2_JP2  0x6a703220    /**< File type fields */
62
63/* For the future */
64/* #define JP2_RES 0x72657320 */  /**< Resolution box (super-box) */
65/* #define JP2_JP2I 0x6a703269 */  /**< Intellectual property box */
66/* #define JP2_XML  0x786d6c20 */  /**< XML box */
67/* #define JP2_UUID 0x75756994 */  /**< UUID box */
68/* #define JP2_UINF 0x75696e66 */  /**< UUID info box (super-box) */
69/* #define JP2_ULST 0x756c7374 */  /**< UUID list box */
70
71/* ----------------------------------------------------------------------- */
72
73typedef enum
74{
75  JP2_STATE_NONE            = 0x0,
76  JP2_STATE_SIGNATURE       = 0x1,
77  JP2_STATE_FILE_TYPE       = 0x2,
78  JP2_STATE_HEADER          = 0x4,
79  JP2_STATE_CODESTREAM      = 0x8,
80  JP2_STATE_END_CODESTREAM  = 0x10,
81  JP2_STATE_UNKNOWN         = 0x7fffffff /* ISO C restricts enumerator values to range of 'int' */
82}
83JP2_STATE;
84
85typedef enum
86{
87  JP2_IMG_STATE_NONE        = 0x0,
88  JP2_IMG_STATE_UNKNOWN     = 0x7fffffff
89}
90JP2_IMG_STATE;
91
92/**
93Channel description: channel index, type, assocation
94*/
95typedef struct opj_jp2_cdef_info
96{
97    OPJ_UINT16 cn, typ, asoc;
98} opj_jp2_cdef_info_t;
99
100/**
101Channel descriptions and number of descriptions
102*/
103typedef struct opj_jp2_cdef
104{
105    opj_jp2_cdef_info_t *info;
106    OPJ_UINT16 n;
107} opj_jp2_cdef_t;
108
109/**
110Component mappings: channel index, mapping type, palette index
111*/
112typedef struct opj_jp2_cmap_comp
113{
114    OPJ_UINT16 cmp;
115    OPJ_BYTE mtyp, pcol;
116} opj_jp2_cmap_comp_t;
117
118/**
119Palette data: table entries, palette columns
120*/
121typedef struct opj_jp2_pclr
122{
123    OPJ_UINT32 *entries;
124    OPJ_BYTE *channel_sign;
125    OPJ_BYTE *channel_size;
126    opj_jp2_cmap_comp_t *cmap;
127    OPJ_UINT16 nr_entries;
128    OPJ_BYTE nr_channels;
129} opj_jp2_pclr_t;
130
131/**
132Collector for ICC profile, palette, component mapping, channel description
133*/
134typedef struct opj_jp2_color
135{
136    OPJ_BYTE *icc_profile_buf;
137    OPJ_UINT32 icc_profile_len;
138
139    opj_jp2_cdef_t *jp2_cdef;
140    opj_jp2_pclr_t *jp2_pclr;
141    OPJ_BYTE jp2_has_colr;
142} opj_jp2_color_t;
143
144/**
145JP2 component
146*/
147typedef struct opj_jp2_comps {
148  OPJ_UINT32 depth;
149  OPJ_UINT32 sgnd;
150  OPJ_UINT32 bpcc;
151} opj_jp2_comps_t;
152
153/**
154JPEG-2000 file format reader/writer
155*/
156typedef struct opj_jp2
157{
158  /** handle to the J2K codec  */
159  opj_j2k_t *j2k;
160  /** list of validation procedures */
161  struct opj_procedure_list * m_validation_list;
162  /** list of execution procedures */
163  struct opj_procedure_list * m_procedure_list;
164
165  /* width of image */
166  OPJ_UINT32 w;
167  /* height of image */
168  OPJ_UINT32 h;
169  /* number of components in the image */
170  OPJ_UINT32 numcomps;
171  OPJ_UINT32 bpc;
172  OPJ_UINT32 C;
173  OPJ_UINT32 UnkC;
174  OPJ_UINT32 IPR;
175  OPJ_UINT32 meth;
176  OPJ_UINT32 approx;
177  OPJ_UINT32 enumcs;
178  OPJ_UINT32 precedence;
179  OPJ_UINT32 brand;
180  OPJ_UINT32 minversion;
181  OPJ_UINT32 numcl;
182  OPJ_UINT32 *cl;
183  opj_jp2_comps_t *comps;
184  /* FIXME: The following two variables are used to save offset
185    as we write out a JP2 file to disk. This mecanism is not flexible
186    as codec writers will need to extand those fields as new part
187    of the standard are implemented.
188  */
189    OPJ_OFF_T j2k_codestream_offset;
190    OPJ_OFF_T jpip_iptr_offset;
191	OPJ_BOOL jpip_on;
192  OPJ_UINT32 jp2_state;
193  OPJ_UINT32 jp2_img_state;
194
195  opj_jp2_color_t color;
196
197    OPJ_BOOL ignore_pclr_cmap_cdef;
198}
199opj_jp2_t;
200
201/**
202JP2 Box
203*/
204typedef struct opj_jp2_box {
205    OPJ_UINT32 length;
206    OPJ_UINT32 type;
207    OPJ_INT32 init_pos;
208} opj_jp2_box_t;
209
210typedef struct opj_jp2_header_handler
211{
212  /* marker value */
213  OPJ_UINT32 id;
214  /* action linked to the marker */
215  OPJ_BOOL (*handler) (     opj_jp2_t *jp2,
216                            OPJ_BYTE *p_header_data,
217                            OPJ_UINT32 p_header_size,
218                            opj_event_mgr_t * p_manager);
219}
220opj_jp2_header_handler_t;
221
222
223typedef struct opj_jp2_img_header_writer_handler
224{
225  /* action to perform */
226  OPJ_BYTE*   (*handler) (opj_jp2_t *jp2, OPJ_UINT32 * p_data_size);
227  /* result of the action : data */
228  OPJ_BYTE*   m_data;
229  /* size of data */
230  OPJ_UINT32  m_size;
231}
232opj_jp2_img_header_writer_handler_t;
233
234/** @name Exported functions */
235/*@{*/
236/* ----------------------------------------------------------------------- */
237
238/**
239 * Writes the Jpeg2000 file Header box - JP2 Header box (warning, this is a super box).
240 *
241 * @param  jp2      the jpeg2000 file codec.
242 * @param  stream      the stream to write data to.
243 * @param  p_manager  user event manager.
244 *
245 * @return true if writing was successful.
246*/
247OPJ_BOOL opj_jp2_write_jp2h(opj_jp2_t *jp2,
248                            opj_stream_private_t *stream,
249                            opj_event_mgr_t * p_manager );
250
251/**
252Setup the decoder decoding parameters using user parameters.
253Decoding parameters are returned in jp2->j2k->cp.
254@param jp2 JP2 decompressor handle
255@param parameters decompression parameters
256*/
257void opj_jp2_setup_decoder(opj_jp2_t *jp2, opj_dparameters_t *parameters);
258
259/**
260 * Decode an image from a JPEG-2000 file stream
261 * @param jp2 JP2 decompressor handle
262 * @param p_stream  FIXME DOC
263 * @param p_image   FIXME DOC
264 * @param p_manager FIXME DOC
265 *
266 * @return Returns a decoded image if successful, returns NULL otherwise
267*/
268OPJ_BOOL opj_jp2_decode(opj_jp2_t *jp2,
269                        opj_stream_private_t *p_stream,
270            opj_image_t* p_image,
271            opj_event_mgr_t * p_manager);
272
273/**
274 * Setup the encoder parameters using the current image and using user parameters.
275 * Coding parameters are returned in jp2->j2k->cp.
276 *
277 * @param jp2 JP2 compressor handle
278 * @param parameters compression parameters
279 * @param image input filled image
280 * @param p_manager  FIXME DOC
281*/
282void opj_jp2_setup_encoder(  opj_jp2_t *jp2,
283                            opj_cparameters_t *parameters,
284                            opj_image_t *image,
285                            opj_event_mgr_t * p_manager);
286
287/**
288Encode an image into a JPEG-2000 file stream
289@param jp2      JP2 compressor handle
290@param stream    Output buffer stream
291@param p_manager  event manager
292@return Returns true if successful, returns false otherwise
293*/
294OPJ_BOOL opj_jp2_encode(  opj_jp2_t *jp2,
295              opj_stream_private_t *stream,
296              opj_event_mgr_t * p_manager);
297
298
299/**
300 * Starts a compression scheme, i.e. validates the codec parameters, writes the header.
301 *
302 * @param  jp2    the jpeg2000 file codec.
303 * @param  stream    the stream object.
304 * @param  p_image   FIXME DOC
305 * @param p_manager FIXME DOC
306 *
307 * @return true if the codec is valid.
308 */
309OPJ_BOOL opj_jp2_start_compress(opj_jp2_t *jp2,
310                                opj_stream_private_t *stream,
311                                opj_image_t * p_image,
312                                opj_event_mgr_t * p_manager);
313
314
315/**
316 * Ends the compression procedures and possibiliy add data to be read after the
317 * codestream.
318 */
319OPJ_BOOL opj_jp2_end_compress(  opj_jp2_t *jp2,
320                  opj_stream_private_t *cio,
321                  opj_event_mgr_t * p_manager);
322
323/* ----------------------------------------------------------------------- */
324
325/**
326 * Ends the decompression procedures and possibiliy add data to be read after the
327 * codestream.
328 */
329OPJ_BOOL opj_jp2_end_decompress(opj_jp2_t *jp2,
330                                opj_stream_private_t *cio,
331                                opj_event_mgr_t * p_manager);
332
333/**
334 * Reads a jpeg2000 file header structure.
335 *
336 * @param p_stream the stream to read data from.
337 * @param jp2 the jpeg2000 file header structure.
338 * @param p_image   FIXME DOC
339 * @param p_manager the user event manager.
340 *
341 * @return true if the box is valid.
342 */
343OPJ_BOOL opj_jp2_read_header(  opj_stream_private_t *p_stream,
344                                opj_jp2_t *jp2,
345                                opj_image_t ** p_image,
346                                opj_event_mgr_t * p_manager );
347
348/**
349 * Reads a tile header.
350 * @param  p_jp2         the jpeg2000 codec.
351 * @param  p_tile_index  FIXME DOC
352 * @param  p_data_size   FIXME DOC
353 * @param  p_tile_x0     FIXME DOC
354 * @param  p_tile_y0     FIXME DOC
355 * @param  p_tile_x1     FIXME DOC
356 * @param  p_tile_y1     FIXME DOC
357 * @param  p_nb_comps    FIXME DOC
358 * @param  p_go_on       FIXME DOC
359 * @param  p_stream      the stream to write data to.
360 * @param  p_manager     the user event manager.
361 */
362OPJ_BOOL opj_jp2_read_tile_header ( opj_jp2_t * p_jp2,
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                                    opj_stream_private_t *p_stream,
372                                    opj_event_mgr_t * p_manager );
373
374/**
375 * Writes a tile.
376 *
377 * @param  p_jp2    the jpeg2000 codec.
378 * @param p_tile_index  FIXME DOC
379 * @param p_data        FIXME DOC
380 * @param p_data_size   FIXME DOC
381 * @param  p_stream      the stream to write data to.
382 * @param  p_manager  the user event manager.
383 */
384OPJ_BOOL opj_jp2_write_tile (  opj_jp2_t *p_jp2,
385                    OPJ_UINT32 p_tile_index,
386                    OPJ_BYTE * p_data,
387                    OPJ_UINT32 p_data_size,
388                    opj_stream_private_t *p_stream,
389                    opj_event_mgr_t * p_manager );
390
391/**
392 * Decode tile data.
393 * @param  p_jp2    the jpeg2000 codec.
394 * @param  p_tile_index FIXME DOC
395 * @param  p_data       FIXME DOC
396 * @param  p_data_size  FIXME DOC
397 * @param  p_stream      the stream to write data to.
398 * @param  p_manager  the user event manager.
399 *
400 * @return FIXME DOC
401 */
402OPJ_BOOL opj_jp2_decode_tile (  opj_jp2_t * p_jp2,
403                                OPJ_UINT32 p_tile_index,
404                                OPJ_BYTE * p_data,
405                                OPJ_UINT32 p_data_size,
406                                opj_stream_private_t *p_stream,
407                                opj_event_mgr_t * p_manager );
408
409/**
410 * Creates a jpeg2000 file decompressor.
411 *
412 * @return  an empty jpeg2000 file codec.
413 */
414opj_jp2_t* opj_jp2_create (OPJ_BOOL p_is_decoder);
415
416/**
417Destroy a JP2 decompressor handle
418@param jp2 JP2 decompressor handle to destroy
419*/
420void opj_jp2_destroy(opj_jp2_t *jp2);
421
422
423/**
424 * Sets the given area to be decoded. This function should be called right after opj_read_header and before any tile header reading.
425 *
426 * @param  p_jp2      the jpeg2000 codec.
427 * @param  p_image     FIXME DOC
428 * @param  p_start_x   the left position of the rectangle to decode (in image coordinates).
429 * @param  p_start_y    the up position of the rectangle to decode (in image coordinates).
430 * @param  p_end_x      the right position of the rectangle to decode (in image coordinates).
431 * @param  p_end_y      the bottom position of the rectangle to decode (in image coordinates).
432 * @param  p_manager    the user event manager
433 *
434 * @return  true      if the area could be set.
435 */
436OPJ_BOOL opj_jp2_set_decode_area(  opj_jp2_t *p_jp2,
437                    opj_image_t* p_image,
438                    OPJ_INT32 p_start_x, OPJ_INT32 p_start_y,
439                    OPJ_INT32 p_end_x, OPJ_INT32 p_end_y,
440                    opj_event_mgr_t * p_manager );
441
442 /**
443 *
444 */
445OPJ_BOOL opj_jp2_get_tile(  opj_jp2_t *p_jp2,
446                            opj_stream_private_t *p_stream,
447                            opj_image_t* p_image,
448                            opj_event_mgr_t * p_manager,
449                            OPJ_UINT32 tile_index );
450
451
452/**
453 *
454 */
455OPJ_BOOL opj_jp2_set_decoded_resolution_factor(opj_jp2_t *p_jp2,
456                                               OPJ_UINT32 res_factor,
457                                               opj_event_mgr_t * p_manager);
458
459
460/* TODO MSD: clean these 3 functions */
461/**
462 * Dump some elements from the JP2 decompression structure .
463 *
464 *@param p_jp2        the jp2 codec.
465 *@param flag        flag to describe what elments are dump.
466 *@param out_stream      output stream where dump the elements.
467 *
468*/
469void jp2_dump (opj_jp2_t* p_jp2, OPJ_INT32 flag, FILE* out_stream);
470
471/**
472 * Get the codestream info from a JPEG2000 codec.
473 *
474 *@param  p_jp2        jp2 codec.
475 *
476 *@return  the codestream information extract from the jpg2000 codec
477 */
478opj_codestream_info_v2_t* jp2_get_cstr_info(opj_jp2_t* p_jp2);
479
480/**
481 * Get the codestream index from a JPEG2000 codec.
482 *
483 *@param  p_jp2        jp2 codec.
484 *
485 *@return  the codestream index extract from the jpg2000 codec
486 */
487opj_codestream_index_t* jp2_get_cstr_index(opj_jp2_t* p_jp2);
488
489
490/*@}*/
491
492/*@}*/
493
494#endif /* __JP2_H */
495
496