rename local jp2 functions with opj_ prefix
[openjpeg.git] / libopenjpeg / jp2.c
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) 2001-2003, David Janssens
5  * Copyright (c) 2002-2003, Yannick Verschueren
6  * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe
7  * Copyright (c) 2005, Herve Drolon, FreeImage Team
8  * Copyright (c) 2010-2011, Kaori Hagihara
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 #include "opj_includes.h"
33
34 /** @defgroup JP2 JP2 - JPEG-2000 file format reader/writer */
35 /*@{*/
36
37 #define BOX_SIZE        1024
38
39 /** @name Local static functions */
40 /*@{*/
41
42
43 /*static void jp2_write_url(opj_cio_t *cio, char *Idx_file);*/
44
45 /**
46  * Reads a IHDR box - Image Header box
47  *
48  * @param       p_image_header_data                     pointer to actual data (already read from file)
49  * @param       jp2                                                     the jpeg2000 file codec.
50  * @param       p_image_header_size                     the size of the image header
51  * @param       p_manager                                       the user event manager.
52  *
53  * @return      true if the image header is valid, false else.
54  */
55 static opj_bool opj_jp2_read_ihdr(  opj_jp2_v2_t *jp2,
56                                     OPJ_BYTE *p_image_header_data,
57                                     OPJ_UINT32 p_image_header_size,
58                                     opj_event_mgr_t * p_manager );
59
60 /**
61  * Writes the Image Header box - Image Header box.
62  *
63  * @param jp2                                   jpeg2000 file codec.
64  * @param p_nb_bytes_written    pointer to store the nb of bytes written by the function.
65  * 
66  * @return      the data being copied.
67 */
68 static OPJ_BYTE * opj_jp2_write_ihdr(opj_jp2_v2_t *jp2, 
69                                      OPJ_UINT32 * p_nb_bytes_written );
70
71 /**
72  * Writes the Bit per Component box.
73  *
74  * @param       jp2                                             jpeg2000 file codec.
75  * @param       p_nb_bytes_written              pointer to store the nb of bytes written by the function.
76  * 
77  * @return      the data being copied.
78 */
79 static OPJ_BYTE * opj_jp2_write_bpcc(   opj_jp2_v2_t *jp2, 
80                                                                         OPJ_UINT32 * p_nb_bytes_written );
81
82 /**
83  * Reads a Bit per Component box.
84  *
85  * @param       p_bpc_header_data                       pointer to actual data (already read from file)
86  * @param       jp2                                                     the jpeg2000 file codec.
87  * @param       p_bpc_header_size                       the size of the bpc header
88  * @param       p_manager                                       the user event manager.
89  *
90  * @return      true if the bpc header is valid, fale else.
91  */
92 static opj_bool opj_jp2_read_bpcc(  opj_jp2_v2_t *jp2,
93                                     OPJ_BYTE * p_bpc_header_data,
94                                     OPJ_UINT32 p_bpc_header_size,
95                                     opj_event_mgr_t * p_manager );
96
97 static opj_bool opj_jp2_read_cdef(      opj_jp2_v2_t * jp2,
98                                     OPJ_BYTE * p_cdef_header_data,
99                                                                         OPJ_UINT32 p_cdef_header_size,
100                                                                         opj_event_mgr_t * p_manager );
101
102 static void opj_jp2_apply_cdef(opj_image_t *image, opj_jp2_color_t *color);
103
104 /**
105  * Writes the Colour Specification box.
106  *
107  * @param jp2                                   jpeg2000 file codec.
108  * @param p_nb_bytes_written    pointer to store the nb of bytes written by the function.
109  * 
110  * @return      the data being copied.
111 */
112 static OPJ_BYTE * opj_jp2_write_colr(   opj_jp2_v2_t *jp2, 
113                                                                             OPJ_UINT32 * p_nb_bytes_written );
114
115 /**
116  * Writes a FTYP box - File type box
117  *
118  * @param       cio                     the stream to write data to.
119  * @param       jp2                     the jpeg2000 file codec.
120  * @param       p_manager       the user event manager.
121  * 
122  * @return      true if writting was successful.
123  */
124 static opj_bool opj_jp2_write_ftyp(     opj_jp2_v2_t *jp2,
125                                                                         opj_stream_private_t *cio,
126                                                                         opj_event_mgr_t * p_manager );
127
128 /**
129  * Reads a a FTYP box - File type box
130  *
131  * @param       p_header_data   the data contained in the FTYP box.
132  * @param       jp2                             the jpeg2000 file codec.
133  * @param       p_header_size   the size of the data contained in the FTYP box.
134  * @param       p_manager               the user event manager.
135  *
136  * @return true if the FTYP box is valid.
137  */
138 static opj_bool opj_jp2_read_ftyp(      opj_jp2_v2_t *jp2,
139                                                                         OPJ_BYTE * p_header_data,
140                                                                         OPJ_UINT32 p_header_size,
141                                                                         opj_event_mgr_t * p_manager );
142
143 /**
144  * Skips the Jpeg2000 Codestream Header box - JP2C Header box.
145  *
146  * @param       cio                     the stream to write data to.
147  * @param       jp2                     the jpeg2000 file codec.
148  * @param       p_manager       user event manager.
149  *
150  * @return true if writting was successful.
151 */
152 opj_bool opj_jp2_skip_jp2c(     opj_jp2_v2_t *jp2,
153                                                     opj_stream_private_t *cio,
154                                                     opj_event_mgr_t * p_manager );
155
156 /**
157  * Reads the Jpeg2000 file Header box - JP2 Header box (warning, this is a super box).
158  *
159  * @param       p_header_data   the data contained in the file header box.
160  * @param       jp2                             the jpeg2000 file codec.
161  * @param       p_header_size   the size of the data contained in the file header box.
162  * @param       p_manager               the user event manager.
163  *
164  * @return true if the JP2 Header box was successfully reconized.
165 */
166 static opj_bool opj_jp2_read_jp2h(  opj_jp2_v2_t *jp2,
167                                     OPJ_BYTE *p_header_data,
168                                     OPJ_UINT32 p_header_size,
169                                     opj_event_mgr_t * p_manager );
170
171 /**
172  * Writes the Jpeg2000 codestream Header box - JP2C Header box. This function must be called AFTER the coding has been done.
173  *
174  * @param       cio                     the stream to write data to.
175  * @param       jp2                     the jpeg2000 file codec.
176  * @param       p_manager       user event manager.
177  *
178  * @return true if writting was successful.
179 */
180 static opj_bool opj_jp2_write_jp2c(     opj_jp2_v2_t *jp2,
181                                                                     opj_stream_private_t *cio,
182                                                                     opj_event_mgr_t * p_manager );
183
184 /**
185  * Reads a jpeg2000 file signature box.
186  *
187  * @param       p_header_data   the data contained in the signature box.
188  * @param       jp2                             the jpeg2000 file codec.
189  * @param       p_header_size   the size of the data contained in the signature box.
190  * @param       p_manager               the user event manager.
191  *
192  * @return true if the file signature box is valid.
193  */
194 static opj_bool opj_jp2_read_jp(opj_jp2_v2_t *jp2,
195                                 OPJ_BYTE * p_header_data,
196                                 OPJ_UINT32 p_header_size,
197                                 opj_event_mgr_t * p_manager);
198
199 /**
200  * Writes a jpeg2000 file signature box.
201  *
202  * @param cio the stream to write data to.
203  * @param       jp2                     the jpeg2000 file codec.
204  * @param p_manager the user event manager.
205  * 
206  * @return true if writting was successful.
207  */
208 static opj_bool opj_jp2_write_jp(       opj_jp2_v2_t *jp2,
209                                                     opj_stream_private_t *cio,
210                                                         opj_event_mgr_t * p_manager );
211
212 /**
213 Apply collected palette data
214 @param color Collector for profile, cdef and pclr data
215 @param image 
216 */
217 static void opj_jp2_apply_pclr(opj_image_t *image, opj_jp2_color_t *color);
218
219 static void opj_jp2_free_pclr(opj_jp2_color_t *color);
220
221 /**
222 Collect palette data
223 @param jp2 JP2 handle
224 @param cio Input buffer stream
225 @param box
226 @param color Collector for profile, cdef and pclr data
227 @return Returns true if successful, returns false otherwise
228 */
229 static opj_bool opj_jp2_read_pclr(      opj_jp2_v2_t *jp2,
230                                     OPJ_BYTE * p_pclr_header_data,
231                                     OPJ_UINT32 p_pclr_header_size,
232                                     opj_event_mgr_t * p_manager );
233
234 /**
235 Collect component mapping data
236 @param jp2 JP2 handle
237 @param cio Input buffer stream
238 @param box
239 @param color Collector for profile, cdef and pclr data
240 @return Returns true if successful, returns false otherwise
241 */
242
243 static opj_bool opj_jp2_read_cmap(      opj_jp2_v2_t * jp2,
244                                     OPJ_BYTE * p_cmap_header_data,
245                                     OPJ_UINT32 p_cmap_header_size,
246                                     opj_event_mgr_t * p_manager );
247
248 /**
249  * Reads the Color Specification box.
250  *
251  * @param       p_colr_header_data                      pointer to actual data (already read from file)
252  * @param       jp2                                                     the jpeg2000 file codec.
253  * @param       p_colr_header_size                      the size of the color header
254  * @param       p_manager                                       the user event manager.
255  *
256  * @return      true if the bpc header is valid, fale else.
257 */
258 static opj_bool opj_jp2_read_colr(  opj_jp2_v2_t *jp2,
259                                     OPJ_BYTE * p_colr_header_data,
260                                     OPJ_UINT32 p_colr_header_size,
261                                     opj_event_mgr_t * p_manager );
262
263 /**
264 Write file Index (superbox)
265 @param[in] offset_jp2c offset of jp2c box
266 @param[in] length_jp2c length of jp2c box
267 @param[in] offset_idx  offset of cidx box
268 @param[in] length_idx  length of cidx box
269 @param[in] cio         file output handle
270 @return                length of fidx box
271 */
272 static int write_fidx( int offset_jp2c, int length_jp2c, int offset_idx, int length_idx, opj_cio_t *cio);
273 /**
274 Write index Finder box
275 @param[in] offset offset of fidx box
276 @param[in] length length of fidx box
277 @param[in] cio         file output handle
278 */
279 static void write_iptr( int offset, int length, opj_cio_t *cio);
280 /**
281
282 Write proxy box
283 @param[in] offset_jp2c offset of jp2c box
284 @param[in] length_jp2c length of jp2c box
285 @param[in] offset_idx  offset of cidx box
286 @param[in] length_idx  length of cidx box
287 @param[in] cio         file output handle
288 */
289 static void write_prxy( int offset_jp2c, int length_jp2c, int offset_idx, int length_idx, opj_cio_t *cio);
290 /*@}*/
291
292 /*@}*/
293
294 /**
295  * Sets up the procedures to do on writting header after the codestream.
296  * Developpers wanting to extend the library can add their own writting procedures.
297  */
298 static void opj_jp2_setup_end_header_writting (opj_jp2_v2_t *jp2);
299
300
301 /**
302  * Sets up the procedures to do on reading header after the codestream.
303  * Developpers wanting to extend the library can add their own writting procedures.
304  */
305 static void opj_jp2_setup_end_header_reading (opj_jp2_v2_t *jp2);
306
307 /**
308  * Reads a jpeg2000 file header structure.
309  *
310  * @param cio the stream to read data from.
311  * @param jp2 the jpeg2000 file header structure.
312  * @param p_manager the user event manager.
313  *
314  * @return true if the box is valid.
315  */
316 static opj_bool opj_jp2_read_header_procedure(  opj_jp2_v2_t *jp2,
317                                                 opj_stream_private_t *stream,
318                                                 opj_event_mgr_t * p_manager );
319
320 /**
321  * Excutes the given procedures on the given codec.
322  *
323  * @param       p_procedure_list        the list of procedures to execute
324  * @param       jp2                                     the jpeg2000 file codec to execute the procedures on.
325  * @param       stream                                  the stream to execute the procedures on.
326  * @param       p_manager                       the user manager.
327  *
328  * @return      true                            if all the procedures were successfully executed.
329  */
330 static opj_bool opj_jp2_exec (  opj_jp2_v2_t * jp2,
331                                 opj_procedure_list_t * p_procedure_list,
332                                 opj_stream_private_t *stream,
333                                 opj_event_mgr_t * p_manager );
334
335 /**
336  * Reads a box header. The box is the way data is packed inside a jpeg2000 file structure.
337  *
338  * @param       cio                                             the input stream to read data from.
339  * @param       box                                             the box structure to fill.
340  * @param       p_number_bytes_read             pointer to an int that will store the number of bytes read from the stream (shoul usually be 2).
341  * @param       p_manager                               user event manager.
342  *
343  * @return      true if the box is reconized, false otherwise
344 */
345 static opj_bool opj_jp2_read_boxhdr(opj_jp2_box_t *box,
346                                     OPJ_UINT32 * p_number_bytes_read,
347                                     opj_stream_private_t *cio,
348                                     opj_event_mgr_t * p_manager);
349
350 /**
351  * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
352  * are valid. Developpers wanting to extend the library can add their own validation procedures.
353  */
354 static void opj_jp2_setup_encoding_validation (opj_jp2_v2_t *jp2);
355
356
357 /**
358  * Sets up the procedures to do on writting header. Developpers wanting to extend the library can add their own writting procedures.
359  */
360 static void opj_jp2_setup_header_writting (opj_jp2_v2_t *jp2);
361
362 /**
363  * The default validation procedure without any extension.
364  *
365  * @param       jp2                             the jpeg2000 codec to validate.
366  * @param       cio                             the input stream to validate.
367  * @param       p_manager               the user event manager.
368  *
369  * @return true if the parameters are correct.
370  */
371 opj_bool opj_jp2_default_validation (   opj_jp2_v2_t * jp2,
372                                         opj_stream_private_t *cio,
373                                         opj_event_mgr_t * p_manager );
374
375 /**
376  * Finds the image execution function related to the given box id.
377  *
378  * @param       p_id    the id of the handler to fetch.
379  *
380  * @return      the given handler or NULL if it could not be found.
381  */
382 static const opj_jp2_header_handler_t * opj_jp2_img_find_handler (OPJ_UINT32 p_id);
383
384 /**
385  * Finds the execution function related to the given box id.
386  *
387  * @param       p_id    the id of the handler to fetch.
388  *
389  * @return      the given handler or NULL if it could not be found.
390  */
391 static const opj_jp2_header_handler_t * opj_jp2_find_handler (OPJ_UINT32 p_id );
392
393 const opj_jp2_header_handler_t jp2_header [] =
394 {
395         {JP2_JP,opj_jp2_read_jp},
396         {JP2_FTYP,opj_jp2_read_ftyp},
397         {JP2_JP2H,opj_jp2_read_jp2h}
398 };
399
400 const opj_jp2_header_handler_t jp2_img_header [] =
401 {
402         {JP2_IHDR,opj_jp2_read_ihdr},
403         {JP2_COLR,opj_jp2_read_colr},
404         {JP2_BPCC,opj_jp2_read_bpcc},
405         {JP2_PCLR,opj_jp2_read_pclr},
406         {JP2_CMAP,opj_jp2_read_cmap},
407         {JP2_CDEF,opj_jp2_read_cdef}
408
409 };
410
411 /**
412  * Reads a box header. The box is the way data is packed inside a jpeg2000 file structure. Data is read from a character string
413  *
414  * @param       p_data                                  the character string to read data from.
415  * @param       box                                             the box structure to fill.
416  * @param       p_number_bytes_read             pointer to an int that will store the number of bytes read from the stream (shoul usually be 2).
417  * @param       p_box_max_size                  the maximum number of bytes in the box.
418  *
419  * @return      true if the box is reconized, false otherwise
420 */
421 static opj_bool opj_jp2_read_boxhdr_char(   opj_jp2_box_t *box,
422                                             OPJ_BYTE * p_data,
423                                             OPJ_UINT32 * p_number_bytes_read,
424                                             OPJ_UINT32 p_box_max_size,
425                                             opj_event_mgr_t * p_manager );
426
427 /**
428  * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
429  * are valid. Developpers wanting to extend the library can add their own validation procedures.
430  */
431 static void opj_jp2_setup_decoding_validation (opj_jp2_v2_t *jp2);
432
433 /**
434  * Sets up the procedures to do on reading header.
435  * Developpers wanting to extend the library can add their own writting procedures.
436  */
437 static void opj_jp2_setup_header_reading (opj_jp2_v2_t *jp2);
438
439
440
441 /* ----------------------------------------------------------------------- */
442
443 /**
444  * Reads a box header. The box is the way data is packed inside a jpeg2000 file structure.
445  *
446  * @param       cio                                             the input stream to read data from.
447  * @param       box                                             the box structure to fill.
448  * @param       p_number_bytes_read             pointer to an int that will store the number of bytes read from the stream (should usually be 8).
449  * @param       p_manager                               user event manager.
450  *
451  * @return      true if the box is reconized, false otherwise
452 */
453  opj_bool opj_jp2_read_boxhdr(opj_jp2_box_t *box,
454                                     OPJ_UINT32 * p_number_bytes_read,
455                                     opj_stream_private_t *cio,
456                                     opj_event_mgr_t * p_manager
457                                     )
458 {
459         /* read header from file */
460         unsigned char l_data_header [8];
461
462         /* preconditions */
463         assert(cio != 00);
464         assert(box != 00);
465         assert(p_number_bytes_read != 00);
466         assert(p_manager != 00);
467
468         *p_number_bytes_read = opj_stream_read_data(cio,l_data_header,8,p_manager);
469         if (*p_number_bytes_read != 8) {
470                 return OPJ_FALSE;
471         }
472
473         /* process read data */
474         opj_read_bytes(l_data_header,&(box->length), 4);
475         opj_read_bytes(l_data_header+4,&(box->type), 4);
476
477         /* do we have a "special very large box ?" */
478         /* read then the XLBox */
479         if (box->length == 1) {
480                 OPJ_UINT32 l_xl_part_size;
481
482                 OPJ_UINT32 l_nb_bytes_read = opj_stream_read_data(cio,l_data_header,8,p_manager);
483                 if (l_nb_bytes_read != 8) {
484                         if (l_nb_bytes_read > 0) {
485                                 *p_number_bytes_read += l_nb_bytes_read;
486                         }
487
488                         return OPJ_FALSE;
489                 }
490
491                 opj_read_bytes(l_data_header,&l_xl_part_size, 4);
492                 if (l_xl_part_size != 0) {
493                         opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot handle box sizes higher than 2^32\n");
494                         return OPJ_FALSE;
495                 }
496                 opj_read_bytes(l_data_header,&(box->length), 4);
497         }
498         return OPJ_TRUE;
499 }
500
501 #if 0
502 static void jp2_write_url(opj_cio_t *cio, char *Idx_file) {
503         unsigned int i;
504         opj_jp2_box_t box;
505
506         box.init_pos = cio_tell(cio);
507         cio_skip(cio, 4);
508         cio_write(cio, JP2_URL, 4);     /* DBTL */
509         cio_write(cio, 0, 1);           /* VERS */
510         cio_write(cio, 0, 3);           /* FLAG */
511
512         if(Idx_file) {
513                 for (i = 0; i < strlen(Idx_file); i++) {
514                         cio_write(cio, Idx_file[i], 1);
515                 }
516         }
517
518         box.length = cio_tell(cio) - box.init_pos;
519         cio_seek(cio, box.init_pos);
520         cio_write(cio, box.length, 4);  /* L */
521         cio_seek(cio, box.init_pos + box.length);
522 }
523 #endif
524
525
526 /**
527  * Reads a IHDR box - Image Header box
528  *
529  * @param       jp2                                                     the jpeg2000 file codec.
530  * @param       p_image_header_data                     pointer to actual data (already read from file)
531  * @param       p_image_header_size                     the size of the image header
532  * @param       p_manager                                       the user event manager.
533  *
534  * @return      true if the image header is valid, fale else.
535  */
536 opj_bool opj_jp2_read_ihdr( opj_jp2_v2_t *jp2,
537                             OPJ_BYTE *p_image_header_data,
538                             OPJ_UINT32 p_image_header_size,
539                             opj_event_mgr_t * p_manager )
540 {
541         /* preconditions */
542         assert(p_image_header_data != 00);
543         assert(jp2 != 00);
544         assert(p_manager != 00);
545
546         if (p_image_header_size != 14) {
547                 opj_event_msg_v2(p_manager, EVT_ERROR, "Bad image header box (bad size)\n");
548                 return OPJ_FALSE;
549         }
550
551         opj_read_bytes(p_image_header_data,&(jp2->h),4);                        /* HEIGHT */
552         p_image_header_data += 4;
553         opj_read_bytes(p_image_header_data,&(jp2->w),4);                        /* WIDTH */
554         p_image_header_data += 4;
555         opj_read_bytes(p_image_header_data,&(jp2->numcomps),2);         /* NC */
556         p_image_header_data += 2;
557
558         /* allocate memory for components */
559         jp2->comps = (opj_jp2_comps_t*) opj_malloc(jp2->numcomps * sizeof(opj_jp2_comps_t));
560         if (jp2->comps == 0) {
561                 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to handle image header (ihdr)\n");
562                 return OPJ_FALSE;
563         }
564         memset(jp2->comps,0,jp2->numcomps * sizeof(opj_jp2_comps_t));
565
566         opj_read_bytes(p_image_header_data,&(jp2->bpc),1);                      /* BPC */
567         ++ p_image_header_data;
568
569         /* if equal to 0 then need a BPC box (cf. chapter about image header box of the norm) */
570         /*if (jp2->bpc == 0){
571                          indicate with a flag that we will wait a BPC box 
572                 }*/
573
574         opj_read_bytes(p_image_header_data,&(jp2->C),1);                        /* C */
575         ++ p_image_header_data;
576
577         /* Should be equal to 7 cf. chapter about image header box of the norm */
578         if (jp2->C != 7){
579                 opj_event_msg_v2(p_manager, EVT_INFO, "JP2 IHDR box: compression type indicate that the file is not a conforming JP2 file (%d) \n", jp2->C);
580         }
581
582         opj_read_bytes(p_image_header_data,&(jp2->UnkC),1);                     /* UnkC */
583         ++ p_image_header_data;
584         opj_read_bytes(p_image_header_data,&(jp2->IPR),1);                      /* IPR */
585         ++ p_image_header_data;
586
587         return OPJ_TRUE;
588 }
589
590 /**
591  * Writes the Image Header box - Image Header box.
592  *
593  * @param jp2                                   jpeg2000 file codec.
594  * @param p_nb_bytes_written    pointer to store the nb of bytes written by the function.
595  * 
596  * @return      the data being copied.
597 */
598 OPJ_BYTE * opj_jp2_write_ihdr(opj_jp2_v2_t *jp2, 
599                               OPJ_UINT32 * p_nb_bytes_written 
600                               )
601 {
602         unsigned char * l_ihdr_data,* l_current_ihdr_ptr;
603         
604         // preconditions
605         assert(jp2 != 00);
606         assert(p_nb_bytes_written != 00);
607
608         /* default image header is 22 bytes wide */
609         l_ihdr_data = (unsigned char *) opj_malloc(22);
610         if (l_ihdr_data == 00) {
611                 return 00;
612         }
613         memset(l_ihdr_data,0,22);
614
615         l_current_ihdr_ptr = l_ihdr_data;
616         
617         opj_write_bytes(l_current_ihdr_ptr,22,4);                               /* write box size */
618         l_current_ihdr_ptr+=4;
619
620         opj_write_bytes(l_current_ihdr_ptr,JP2_IHDR, 4);                /* IHDR */
621         l_current_ihdr_ptr+=4;
622         
623         opj_write_bytes(l_current_ihdr_ptr,jp2->h, 4);          /* HEIGHT */
624         l_current_ihdr_ptr+=4;
625         
626         opj_write_bytes(l_current_ihdr_ptr, jp2->w, 4);         /* WIDTH */
627         l_current_ihdr_ptr+=4;
628         
629         opj_write_bytes(l_current_ihdr_ptr, jp2->numcomps, 2);          /* NC */
630         l_current_ihdr_ptr+=2;
631         
632         opj_write_bytes(l_current_ihdr_ptr, jp2->bpc, 1);               /* BPC */
633         ++l_current_ihdr_ptr;
634         
635         opj_write_bytes(l_current_ihdr_ptr, jp2->C, 1);         /* C : Always 7 */
636         ++l_current_ihdr_ptr;
637         
638         opj_write_bytes(l_current_ihdr_ptr, jp2->UnkC, 1);              /* UnkC, colorspace unknown */
639         ++l_current_ihdr_ptr;
640         
641         opj_write_bytes(l_current_ihdr_ptr, jp2->IPR, 1);               /* IPR, no intellectual property */
642         ++l_current_ihdr_ptr;
643         
644         *p_nb_bytes_written = 22;
645         
646         return l_ihdr_data;
647 }
648
649 /**
650  * Writes the Bit per Component box.
651  *
652  * @param       jp2                                             jpeg2000 file codec.
653  * @param       p_nb_bytes_written              pointer to store the nb of bytes written by the function.
654  * 
655  * @return      the data being copied.
656 */
657 OPJ_BYTE * opj_jp2_write_bpcc(  opj_jp2_v2_t *jp2, 
658                                                         OPJ_UINT32 * p_nb_bytes_written 
659                                 )
660 {
661         unsigned int i;
662         /* room for 8 bytes for box and 1 byte for each component */
663         int l_bpcc_size = 8 + jp2->numcomps;
664         unsigned char * l_bpcc_data,* l_current_bpcc_ptr;
665         
666         // preconditions
667         assert(jp2 != 00);
668         assert(p_nb_bytes_written != 00);
669
670         l_bpcc_data = (unsigned char *) opj_malloc(l_bpcc_size);
671         if (l_bpcc_data == 00) {
672                 return 00;
673         }
674         memset(l_bpcc_data,0,l_bpcc_size);
675
676         l_current_bpcc_ptr = l_bpcc_data;
677
678         opj_write_bytes(l_current_bpcc_ptr,l_bpcc_size,4);                              /* write box size */
679         l_current_bpcc_ptr += 4;
680         
681         opj_write_bytes(l_current_bpcc_ptr,JP2_BPCC,4);                                 /* BPCC */
682         l_current_bpcc_ptr += 4;
683
684         for (i = 0; i < jp2->numcomps; ++i)  {
685                 opj_write_bytes(l_current_bpcc_ptr, jp2->comps[i].bpcc, 1); /* write each component information */
686                 ++l_current_bpcc_ptr;
687         }
688
689         *p_nb_bytes_written = l_bpcc_size;
690         
691         return l_bpcc_data;
692 }
693
694
695 /**
696  * Reads a Bit per Component box.
697  *
698  * @param       jp2                                                     the jpeg2000 file codec.
699  * @param       p_bpc_header_data                       pointer to actual data (already read from file)
700  * @param       p_bpc_header_size                       pointer that will hold the size of the bpc header
701  * @param       p_manager                                       the user event manager.
702  *
703  * @return      true if the bpc header is valid, false otherwise.
704  */
705 opj_bool opj_jp2_read_bpcc( opj_jp2_v2_t *jp2,
706                             OPJ_BYTE * p_bpc_header_data,
707                             OPJ_UINT32 p_bpc_header_size,
708                             opj_event_mgr_t * p_manager 
709                             )
710 {
711         OPJ_UINT32 i;
712
713         /* preconditions */
714         assert(p_bpc_header_data != 00);
715         assert(jp2 != 00);
716         assert(p_manager != 00);
717
718         /* TODO MSD */
719         /*if (jp2->bpc != 0 ){
720                 opj_event_msg_v2(p_manager, EVT_WARNING, "A BPCC header box is available although BPC is different to zero (%d)\n",jp2->bpc);
721         }*/
722
723         /* and length is relevant */
724         if (p_bpc_header_size != jp2->numcomps) {
725                 opj_event_msg_v2(p_manager, EVT_ERROR, "Bad BPCC header box (bad size)\n");
726                 return OPJ_FALSE;
727         }
728
729         /* read info for each component */
730         for (i = 0; i < jp2->numcomps; ++i) {
731                 opj_read_bytes(p_bpc_header_data,&jp2->comps[i].bpcc ,1);       /* read each BPCC component */
732                 ++p_bpc_header_data;
733         }
734
735         return OPJ_TRUE;
736 }
737
738 /**
739  * Writes the Colour Specification box.
740  *
741  * @param jp2                                   jpeg2000 file codec.
742  * @param p_nb_bytes_written    pointer to store the nb of bytes written by the function.
743  * 
744  * @return      the data being copied.
745 */
746 OPJ_BYTE * opj_jp2_write_colr(  opj_jp2_v2_t *jp2, 
747                                                             OPJ_UINT32 * p_nb_bytes_written 
748                                 )
749 {
750         /* room for 8 bytes for box 3 for common data and variable upon profile*/
751         unsigned int l_colr_size = 11;
752         unsigned char * l_colr_data,* l_current_colr_ptr;
753         
754         // preconditions
755         assert(jp2 != 00);
756         assert(p_nb_bytes_written != 00);
757
758         switch (jp2->meth) {
759                 case 1 :
760                         l_colr_size += 4;
761                         break;
762                 case 2 :
763                         ++l_colr_size;
764                         break;
765                 default :
766                         return 00;
767         }
768
769         l_colr_data = (unsigned char *) opj_malloc(l_colr_size);
770         if (l_colr_data == 00) {
771                 return 00;
772         }
773         memset(l_colr_data,0,l_colr_size);
774         
775         l_current_colr_ptr = l_colr_data;
776
777         opj_write_bytes(l_current_colr_ptr,l_colr_size,4);                              /* write box size */
778         l_current_colr_ptr += 4;
779         
780         opj_write_bytes(l_current_colr_ptr,JP2_COLR,4);                                 /* BPCC */
781         l_current_colr_ptr += 4;
782         
783         opj_write_bytes(l_current_colr_ptr, jp2->meth,1);                               /* METH */
784         ++l_current_colr_ptr;
785         
786         opj_write_bytes(l_current_colr_ptr, jp2->precedence,1);                 /* PRECEDENCE */
787         ++l_current_colr_ptr;
788         
789         opj_write_bytes(l_current_colr_ptr, jp2->approx,1);                             /* APPROX */
790         ++l_current_colr_ptr;
791         
792         if (jp2->meth == 1) {
793                 opj_write_bytes(l_current_colr_ptr, jp2->enumcs,4);                     /* EnumCS */
794         } 
795         else {
796                 opj_write_bytes(l_current_colr_ptr, 0, 1);                                      /* PROFILE (??) */
797         }
798
799         *p_nb_bytes_written = l_colr_size;
800         
801         return l_colr_data;
802 }
803
804 void opj_jp2_free_pclr(opj_jp2_color_t *color)
805 {
806     opj_free(color->jp2_pclr->channel_sign);
807     opj_free(color->jp2_pclr->channel_size);
808     opj_free(color->jp2_pclr->entries);
809
810         if(color->jp2_pclr->cmap) opj_free(color->jp2_pclr->cmap);
811
812     opj_free(color->jp2_pclr); color->jp2_pclr = NULL;
813 }
814
815 static void free_color_data(opj_jp2_color_t *color)
816 {
817         if(color->jp2_pclr)
818    {
819         jp2_free_pclr(color);
820    }
821         if(color->jp2_cdef) 
822    {
823         if(color->jp2_cdef->info) opj_free(color->jp2_cdef->info);
824         opj_free(color->jp2_cdef);
825    }
826         if(color->icc_profile_buf) opj_free(color->icc_profile_buf);
827 }
828
829
830 void opj_jp2_apply_pclr(opj_image_t *image, opj_jp2_color_t *color)
831 {
832         opj_image_comp_t *old_comps, *new_comps;
833         OPJ_BYTE *channel_size, *channel_sign;
834         OPJ_UINT32 *entries;
835         opj_jp2_cmap_comp_t *cmap;
836         OPJ_INT32 *src, *dst;
837         OPJ_UINT32 j, max;
838         OPJ_UINT16 i, nr_channels, cmp, pcol;
839         OPJ_INT32 k, top_k;
840
841         channel_size = color->jp2_pclr->channel_size;
842         channel_sign = color->jp2_pclr->channel_sign;
843         entries = color->jp2_pclr->entries;
844         cmap = color->jp2_pclr->cmap;
845         nr_channels = color->jp2_pclr->nr_channels;
846
847         old_comps = image->comps;
848         new_comps = (opj_image_comp_t*)
849                         opj_malloc(nr_channels * sizeof(opj_image_comp_t));
850
851         for(i = 0; i < nr_channels; ++i) {
852                 pcol = cmap[i].pcol; cmp = cmap[i].cmp;
853
854                 new_comps[pcol] = old_comps[cmp];
855
856                 /* Direct use */
857                 if(cmap[i].mtyp == 0){
858                         old_comps[cmp].data = NULL; continue;
859                 }
860
861                 /* Palette mapping: */
862                 new_comps[pcol].data = (int*)
863                                 opj_malloc(old_comps[cmp].w * old_comps[cmp].h * sizeof(int));
864                 new_comps[pcol].prec = channel_size[i];
865                 new_comps[pcol].sgnd = channel_sign[i];
866         }
867
868         top_k = color->jp2_pclr->nr_entries - 1;
869
870         for(i = 0; i < nr_channels; ++i) {
871                 /* Direct use: */
872                 if(cmap[i].mtyp == 0) continue;
873
874                 /* Palette mapping: */
875                 cmp = cmap[i].cmp; pcol = cmap[i].pcol;
876                 src = old_comps[cmp].data;
877                 dst = new_comps[pcol].data;
878                 max = new_comps[pcol].w * new_comps[pcol].h;
879
880                 for(j = 0; j < max; ++j)
881                 {
882                         /* The index */
883                         if((k = src[j]) < 0) k = 0; else if(k > top_k) k = top_k;
884
885                         /* The colour */
886                         dst[j] = entries[k * nr_channels + pcol];
887                 }
888         }
889
890         max = image->numcomps;
891         for(i = 0; i < max; ++i) {
892                 if(old_comps[i].data) opj_free(old_comps[i].data);
893         }
894
895         opj_free(old_comps);
896         image->comps = new_comps;
897         image->numcomps = nr_channels;
898
899         opj_jp2_free_pclr(color);
900
901 }/* apply_pclr() */
902
903 /**
904  * Reads a palette box.
905  *
906  * @param       jp2                                                     the jpeg2000 file codec.
907  * @param       p_pclr_header_data                      pointer to actual data (already read from file)
908  * @param       p_pclr_header_size                      pointer that will hold the size of the PCLR header
909  * @param       p_manager                                       the user event manager.
910  *
911  * @return      true if the bpc header is valid, fale else.
912  */
913 opj_bool opj_jp2_read_pclr(     opj_jp2_v2_t *jp2,
914                             OPJ_BYTE * p_pclr_header_data,
915                             OPJ_UINT32 p_pclr_header_size,
916                             opj_event_mgr_t * p_manager 
917                             )
918 {
919         opj_jp2_pclr_t *jp2_pclr;
920         OPJ_BYTE *channel_size, *channel_sign;
921         OPJ_UINT32 *entries;
922         OPJ_UINT16 nr_entries,nr_channels;
923         OPJ_UINT16 i, j;
924         OPJ_UINT32 l_value;
925
926         /* preconditions */
927         assert(p_pclr_header_data != 00);
928         assert(jp2 != 00);
929         assert(p_manager != 00);
930   (void)p_pclr_header_size;
931
932         if(jp2->color.jp2_pclr)
933                 return OPJ_FALSE;
934
935         opj_read_bytes(p_pclr_header_data, &l_value , 2);       /* NE */
936         p_pclr_header_data += 2;
937         nr_entries = (OPJ_UINT16) l_value;
938
939         opj_read_bytes(p_pclr_header_data, &l_value , 1);       /* NPC */
940         ++p_pclr_header_data;
941         nr_channels = (OPJ_UINT16) l_value;
942
943         entries = (OPJ_UINT32*) opj_malloc(nr_channels * nr_entries * sizeof(OPJ_UINT32));
944         channel_size = (OPJ_BYTE*) opj_malloc(nr_channels);
945         channel_sign = (OPJ_BYTE*) opj_malloc(nr_channels);
946
947         jp2_pclr = (opj_jp2_pclr_t*)opj_malloc(sizeof(opj_jp2_pclr_t));
948         jp2_pclr->channel_sign = channel_sign;
949         jp2_pclr->channel_size = channel_size;
950         jp2_pclr->entries = entries;
951         jp2_pclr->nr_entries = nr_entries;
952         jp2_pclr->nr_channels = (OPJ_BYTE) l_value;
953         jp2_pclr->cmap = NULL;
954
955         jp2->color.jp2_pclr = jp2_pclr;
956
957         for(i = 0; i < nr_channels; ++i) {
958                 opj_read_bytes(p_pclr_header_data, &l_value , 1);       /* Bi */
959                 ++p_pclr_header_data;
960
961                 channel_size[i] = (l_value & 0x7f) + 1;
962                 channel_sign[i] = (l_value & 0x80)? 1 : 0;
963         }
964
965         for(j = 0; j < nr_entries; ++j) {
966                 for(i = 0; i < nr_channels; ++i) {
967                         int bytes_to_read = (channel_size[i]+7)>>3;
968
969                         opj_read_bytes(p_pclr_header_data, &l_value , bytes_to_read);   /* Cji */
970                         p_pclr_header_data += bytes_to_read;
971                         *entries = (OPJ_UINT32) l_value;
972                         entries++;
973                 }
974         }
975
976         return OPJ_TRUE;
977 }
978
979 /**
980  * Reads the Component Mapping box.
981  *
982  * @param       p_cmap_header_data                      pointer to actual data (already read from file)
983  * @param       jp2                                                     the jpeg2000 file codec.
984  * @param       p_cmap_header_size                      pointer that will hold the size of the color header
985  * @param       p_manager                                       the user event manager.
986  *
987  * @return      true if the cdef header is valid, false else.
988 */
989 opj_bool opj_jp2_read_cmap(     opj_jp2_v2_t * jp2,
990                             OPJ_BYTE * p_cmap_header_data,
991                             OPJ_UINT32 p_cmap_header_size,
992                             opj_event_mgr_t * p_manager 
993                             )
994 {
995         opj_jp2_cmap_comp_t *cmap;
996         OPJ_BYTE i, nr_channels;
997         OPJ_UINT32 l_value;
998
999         /* preconditions */
1000         assert(jp2 != 00);
1001         assert(p_cmap_header_data != 00);
1002         assert(p_manager != 00);
1003   (void)p_cmap_header_size;
1004
1005         /* Need nr_channels: */
1006         if(jp2->color.jp2_pclr == NULL) {
1007                 opj_event_msg_v2(p_manager, EVT_ERROR, "Need to read a PCLR box before the CMAP box.\n");
1008                 return OPJ_FALSE;
1009         }
1010
1011         /* Part 1, I.5.3.5: 'There shall be at most one Component Mapping box
1012          * inside a JP2 Header box' :
1013         */
1014         if(jp2->color.jp2_pclr->cmap) {
1015                 opj_event_msg_v2(p_manager, EVT_ERROR, "Only one CMAP box is allowed.\n");
1016                 return OPJ_FALSE;
1017         }
1018
1019         nr_channels = jp2->color.jp2_pclr->nr_channels;
1020         cmap = (opj_jp2_cmap_comp_t*) opj_malloc(nr_channels * sizeof(opj_jp2_cmap_comp_t));
1021
1022         for(i = 0; i < nr_channels; ++i) {
1023                 opj_read_bytes(p_cmap_header_data, &l_value, 2);                        /* CMP^i */
1024                 p_cmap_header_data +=2;
1025                 cmap[i].cmp = (OPJ_UINT16) l_value;
1026
1027                 opj_read_bytes(p_cmap_header_data, &l_value, 1);                        /* MTYP^i */
1028                 ++p_cmap_header_data;
1029                 cmap[i].mtyp = (OPJ_BYTE) l_value;
1030
1031                 opj_read_bytes(p_cmap_header_data, &l_value, 1);                        /* PCOL^i */
1032                 ++p_cmap_header_data;
1033                 cmap[i].pcol = (OPJ_BYTE) l_value;
1034         }
1035
1036         jp2->color.jp2_pclr->cmap = cmap;
1037
1038         return OPJ_TRUE;
1039 }
1040
1041 void opj_jp2_apply_cdef(opj_image_t *image, opj_jp2_color_t *color)
1042 {
1043         opj_jp2_cdef_info_t *info;
1044         OPJ_INT32 color_space;
1045         OPJ_UINT16 i, n, cn, typ, asoc, acn;
1046
1047         color_space = image->color_space;
1048         info = color->jp2_cdef->info;
1049         n = color->jp2_cdef->n;
1050
1051         for(i = 0; i < n; ++i)
1052         {
1053                 /* WATCH: acn = asoc - 1 ! */
1054                 if((asoc = info[i].asoc) == 0) continue;
1055
1056                 cn = info[i].cn; typ = info[i].typ; acn = asoc - 1;
1057
1058                 if(cn != acn)
1059                 {
1060                         opj_image_comp_t saved;
1061
1062                         memcpy(&saved, &image->comps[cn], sizeof(opj_image_comp_t));
1063                         memcpy(&image->comps[cn], &image->comps[acn], sizeof(opj_image_comp_t));
1064                         memcpy(&image->comps[acn], &saved, sizeof(opj_image_comp_t));
1065
1066                         info[i].asoc = cn + 1;
1067                         info[acn].asoc = info[acn].cn + 1;
1068                 }
1069         }
1070
1071         if(color->jp2_cdef->info) opj_free(color->jp2_cdef->info);
1072
1073         opj_free(color->jp2_cdef); color->jp2_cdef = NULL;
1074
1075 }/* jp2_apply_cdef() */
1076
1077 /**
1078  * Reads the Component Definition box.
1079  *
1080  * @param       p_cdef_header_data                      pointer to actual data (already read from file)
1081  * @param       jp2                                                     the jpeg2000 file codec.
1082  * @param       p_cdef_header_size                      pointer that will hold the size of the color header
1083  * @param       p_manager                                       the user event manager.
1084  *
1085  * @return      true if the cdef header is valid, false else.
1086 */
1087 opj_bool opj_jp2_read_cdef(     opj_jp2_v2_t * jp2,
1088                             OPJ_BYTE * p_cdef_header_data,
1089                                                         OPJ_UINT32 p_cdef_header_size,
1090                                                         opj_event_mgr_t * p_manager 
1091                             )
1092 {
1093         opj_jp2_cdef_info_t *cdef_info;
1094         unsigned short i;
1095         OPJ_UINT32 l_value;
1096
1097         /* preconditions */
1098         assert(jp2 != 00);
1099         assert(p_cdef_header_data != 00);
1100         assert(p_manager != 00);
1101   (void)p_cdef_header_size;
1102
1103         /* Part 1, I.5.3.6: 'The shall be at most one Channel Definition box
1104          * inside a JP2 Header box.'*/
1105         if(jp2->color.jp2_cdef) return OPJ_FALSE;
1106
1107         opj_read_bytes(p_cdef_header_data,&l_value ,2);                 /* N */
1108         p_cdef_header_data+= 2;
1109
1110         if ( (OPJ_UINT16)l_value == 0){ /* szukw000: FIXME */
1111                 opj_event_msg_v2(p_manager, EVT_ERROR, "Number of channel description is equal to zero in CDEF box.\n");
1112                 return OPJ_FALSE;
1113         }
1114
1115         cdef_info = (opj_jp2_cdef_info_t*) opj_malloc(l_value * sizeof(opj_jp2_cdef_info_t));
1116
1117         jp2->color.jp2_cdef = (opj_jp2_cdef_t*)opj_malloc(sizeof(opj_jp2_cdef_t));
1118         jp2->color.jp2_cdef->info = cdef_info;
1119         jp2->color.jp2_cdef->n = (OPJ_UINT16) l_value;
1120
1121         for(i = 0; i < jp2->color.jp2_cdef->n; ++i) {
1122                 opj_read_bytes(p_cdef_header_data, &l_value, 2);                        /* Cn^i */
1123                 p_cdef_header_data +=2;
1124                 cdef_info[i].cn = (OPJ_UINT16) l_value;
1125
1126                 opj_read_bytes(p_cdef_header_data, &l_value, 2);                        /* Typ^i */
1127                 p_cdef_header_data +=2;
1128                 cdef_info[i].typ = (OPJ_UINT16) l_value;
1129
1130                 opj_read_bytes(p_cdef_header_data, &l_value, 2);                        /* Asoc^i */
1131                 p_cdef_header_data +=2;
1132                 cdef_info[i].asoc = (OPJ_UINT16) l_value;
1133    }
1134
1135         return OPJ_TRUE;
1136 }
1137
1138 /**
1139  * Reads the Colour Specification box.
1140  *
1141  * @param       p_colr_header_data                      pointer to actual data (already read from file)
1142  * @param       jp2                                                     the jpeg2000 file codec.
1143  * @param       p_colr_header_size                      pointer that will hold the size of the color header
1144  * @param       p_colr_header_max_size          maximum size of the header, any size bigger than this value should result the function to output false.
1145  * @param       p_manager                                       the user event manager.
1146  *
1147  * @return      true if the bpc header is valid, fale else.
1148 */
1149 opj_bool opj_jp2_read_colr( opj_jp2_v2_t *jp2,
1150                             OPJ_BYTE * p_colr_header_data,
1151                             OPJ_UINT32 p_colr_header_size,
1152                             opj_event_mgr_t * p_manager 
1153                             )
1154 {
1155         OPJ_UINT32 l_value;
1156
1157         /* preconditions */
1158         assert(jp2 != 00);
1159         assert(p_colr_header_data != 00);
1160         assert(p_manager != 00);
1161
1162         if (p_colr_header_size < 3) {
1163                 opj_event_msg_v2(p_manager, EVT_ERROR, "Bad COLR header box (bad size)\n");
1164                 return OPJ_FALSE;
1165         }
1166
1167         /* Part 1, I.5.3.3 : 'A conforming JP2 reader shall ignore all Colour
1168          * Specification boxes after the first.'
1169         */
1170         if(jp2->color.jp2_has_colr) {
1171                 opj_event_msg_v2(p_manager, EVT_INFO, "A conforming JP2 reader shall ignore all Colour Specification boxes after the first, so we ignore this one.\n");
1172                 p_colr_header_data += p_colr_header_size;
1173                 return OPJ_TRUE;
1174         }
1175
1176         opj_read_bytes(p_colr_header_data,&jp2->meth ,1);                       /* METH */
1177         ++p_colr_header_data;
1178
1179         opj_read_bytes(p_colr_header_data,&jp2->precedence ,1);         /* PRECEDENCE */
1180         ++p_colr_header_data;
1181
1182         opj_read_bytes(p_colr_header_data,&jp2->approx ,1);                     /* APPROX */
1183         ++p_colr_header_data;
1184
1185         if (jp2->meth == 1) {
1186                 if (p_colr_header_size != 7) {
1187                         opj_event_msg_v2(p_manager, EVT_ERROR, "Bad BPCC header box (bad size)\n");
1188                         return OPJ_FALSE;
1189                 }
1190
1191                 opj_read_bytes(p_colr_header_data,&jp2->enumcs ,4);                     /* EnumCS */
1192         }
1193         else if (jp2->meth == 2) {
1194                 /* ICC profile */
1195                 int it_icc_value = 0;
1196                 int icc_len = p_colr_header_size - 3;
1197
1198                 jp2->color.icc_profile_len = icc_len;
1199                 jp2->color.icc_profile_buf = (unsigned char*) opj_malloc(icc_len);
1200
1201                 memset(jp2->color.icc_profile_buf, 0, icc_len * sizeof(unsigned char));
1202
1203                 for (it_icc_value = 0; it_icc_value < icc_len; ++it_icc_value)
1204                 {
1205                         opj_read_bytes(p_colr_header_data,&l_value,1);          /* icc values */
1206                         ++p_colr_header_data;
1207                         jp2->color.icc_profile_buf[it_icc_value] = (OPJ_BYTE) l_value;
1208                 }
1209
1210         }
1211         else /* TODO MSD */
1212                 opj_event_msg_v2(p_manager, EVT_INFO, "COLR BOX meth value is not a regular value (%d), so we will skip the fields following the approx field.\n", jp2->meth);
1213
1214         jp2->color.jp2_has_colr = 1;
1215
1216         return OPJ_TRUE;
1217 }
1218
1219
1220 opj_bool opj_jp2_decode(opj_jp2_v2_t *jp2,
1221                         opj_stream_private_t *p_stream,
1222                         opj_image_t* p_image,
1223                         opj_event_mgr_t * p_manager)
1224 {
1225         if (!p_image)
1226                 return OPJ_FALSE;
1227
1228         /* J2K decoding */
1229         if( ! opj_j2k_decode(jp2->j2k, p_stream, p_image, p_manager) ) {
1230                 opj_event_msg_v2(p_manager, EVT_ERROR, "Failed to decode the codestream in the JP2 file\n");
1231                 return OPJ_FALSE;
1232         }
1233     
1234     if (!jp2->ignore_pclr_cmap_cdef){
1235
1236             /* Set Image Color Space */
1237             if (jp2->enumcs == 16)
1238                     p_image->color_space = CLRSPC_SRGB;
1239             else if (jp2->enumcs == 17)
1240                     p_image->color_space = CLRSPC_GRAY;
1241             else if (jp2->enumcs == 18)
1242                     p_image->color_space = CLRSPC_SYCC;
1243             else
1244                     p_image->color_space = CLRSPC_UNKNOWN;
1245
1246             /* Apply the color space if needed */
1247             if(jp2->color.jp2_cdef) {
1248                     opj_jp2_apply_cdef(p_image, &(jp2->color));
1249             }
1250
1251             if(jp2->color.jp2_pclr) {
1252                     /* Part 1, I.5.3.4: Either both or none : */
1253                     if( !jp2->color.jp2_pclr->cmap)
1254                             opj_jp2_free_pclr(&(jp2->color));
1255                     else
1256                             opj_jp2_apply_pclr(p_image, &(jp2->color));
1257             }
1258
1259             if(jp2->color.icc_profile_buf) {
1260                     p_image->icc_profile_buf = jp2->color.icc_profile_buf;
1261                     p_image->icc_profile_len = jp2->color.icc_profile_len;
1262                     jp2->color.icc_profile_buf = NULL;
1263             }
1264     }
1265
1266         return OPJ_TRUE;
1267 }
1268
1269
1270 /**
1271  * Writes the Jpeg2000 file Header box - JP2 Header box (warning, this is a super box).
1272  *
1273  * @param cio                   the stream to write data to.
1274  * @param jp2                   the jpeg2000 file codec.
1275  * @param p_manager             user event manager.
1276  *
1277  * @return true if writting was successful.
1278 */
1279 opj_bool opj_jp2_write_jp2h(opj_jp2_v2_t *jp2,
1280                             opj_stream_private_t *stream,
1281                             opj_event_mgr_t * p_manager 
1282                             )
1283 {
1284         opj_jp2_img_header_writer_handler_t l_writers [3];
1285         opj_jp2_img_header_writer_handler_t * l_current_writer;
1286
1287         int i, l_nb_pass;
1288         /* size of data for super box*/
1289         int l_jp2h_size = 8;
1290         opj_bool l_result = OPJ_TRUE;
1291
1292         /* to store the data of the super box */
1293         unsigned char l_jp2h_data [8];
1294         
1295         // preconditions
1296         assert(stream != 00);
1297         assert(jp2 != 00);
1298         assert(p_manager != 00);
1299
1300         memset(l_writers,0,sizeof(l_writers));
1301
1302         if (jp2->bpc == 255) {
1303                 l_nb_pass = 3;
1304                 l_writers[0].handler = opj_jp2_write_ihdr;
1305                 l_writers[1].handler = opj_jp2_write_bpcc;
1306                 l_writers[2].handler = opj_jp2_write_colr;
1307         }
1308         else {
1309                 l_nb_pass = 2;
1310                 l_writers[0].handler = opj_jp2_write_ihdr;
1311                 l_writers[1].handler = opj_jp2_write_colr;
1312         }
1313         
1314         /* write box header */
1315         /* write JP2H type */
1316         opj_write_bytes(l_jp2h_data+4,JP2_JP2H,4);
1317
1318         l_current_writer = l_writers;
1319         for (i=0;i<l_nb_pass;++i) {
1320                 l_current_writer->m_data = l_current_writer->handler(jp2,&(l_current_writer->m_size));
1321                 if (l_current_writer->m_data == 00) {
1322                         opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to hold JP2 Header data\n");
1323                         l_result = OPJ_FALSE;
1324                         break;
1325                 }
1326
1327                 l_jp2h_size += l_current_writer->m_size;
1328                 ++l_current_writer;
1329         }
1330
1331         if (! l_result) {
1332                 l_current_writer = l_writers;
1333                 for (i=0;i<l_nb_pass;++i) {
1334                         if (l_current_writer->m_data != 00) {
1335                                 opj_free(l_current_writer->m_data );
1336                         }
1337                         ++l_current_writer;
1338                 }
1339
1340                 return OPJ_FALSE;
1341         }
1342
1343         /* write super box size */
1344         opj_write_bytes(l_jp2h_data,l_jp2h_size,4);
1345         
1346         /* write super box data on stream */
1347         if (opj_stream_write_data(stream,l_jp2h_data,8,p_manager) != 8) {
1348                 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream error while writing JP2 Header box\n");
1349                 l_result = OPJ_FALSE;
1350         }
1351         
1352         if (l_result) {
1353                 l_current_writer = l_writers;
1354                 for (i=0;i<l_nb_pass;++i) {
1355                         if (opj_stream_write_data(stream,l_current_writer->m_data,l_current_writer->m_size,p_manager) != l_current_writer->m_size) {
1356                                 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream error while writting JP2 Header box\n");
1357                                 l_result = OPJ_FALSE;
1358                                 break;
1359                         }
1360                         ++l_current_writer;
1361                 }
1362         }
1363
1364         l_current_writer = l_writers;
1365         
1366         /* cleanup */
1367         for (i=0;i<l_nb_pass;++i) {
1368                 if (l_current_writer->m_data != 00) {
1369                         opj_free(l_current_writer->m_data );
1370                 }
1371                 ++l_current_writer;
1372         }
1373
1374         return l_result;
1375 }
1376
1377 /**
1378  * Writes a FTYP box - File type box
1379  *
1380  * @param       cio                     the stream to write data to.
1381  * @param       jp2                     the jpeg2000 file codec.
1382  * @param       p_manager       the user event manager.
1383  * 
1384  * @return      true if writting was successful.
1385  */
1386 opj_bool opj_jp2_write_ftyp(opj_jp2_v2_t *jp2,
1387                                                         opj_stream_private_t *cio,
1388                                                         opj_event_mgr_t * p_manager )
1389 {
1390         unsigned int i;
1391         unsigned int l_ftyp_size = 16 + 4 * jp2->numcl;
1392         unsigned char * l_ftyp_data, * l_current_data_ptr;
1393         opj_bool l_result;
1394
1395         // preconditions
1396         assert(cio != 00);
1397         assert(jp2 != 00);
1398         assert(p_manager != 00);
1399
1400         l_ftyp_data = (unsigned char *) opj_malloc(l_ftyp_size);
1401         
1402         if (l_ftyp_data == 00) {
1403                 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to handle ftyp data\n");
1404                 return OPJ_FALSE;
1405         }
1406
1407         memset(l_ftyp_data,0,l_ftyp_size);
1408
1409         l_current_data_ptr = l_ftyp_data;
1410
1411         opj_write_bytes(l_current_data_ptr, l_ftyp_size,4); /* box size */
1412         l_current_data_ptr += 4;
1413
1414         opj_write_bytes(l_current_data_ptr, JP2_FTYP,4); /* FTYP */
1415         l_current_data_ptr += 4;
1416
1417         opj_write_bytes(l_current_data_ptr, jp2->brand,4); /* BR */
1418         l_current_data_ptr += 4;
1419
1420         opj_write_bytes(l_current_data_ptr, jp2->minversion,4); /* MinV */
1421         l_current_data_ptr += 4;
1422
1423         for (i = 0; i < jp2->numcl; i++)  {
1424                 opj_write_bytes(l_current_data_ptr, jp2->cl[i],4);      /* CL */
1425         }
1426         
1427         l_result = (opj_stream_write_data(cio,l_ftyp_data,l_ftyp_size,p_manager) == l_ftyp_size);
1428         if (! l_result)
1429         {
1430                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error while writting ftyp data to stream\n");
1431         }
1432
1433         opj_free(l_ftyp_data);
1434         
1435         return l_result;
1436 }
1437
1438 /**
1439  * Writes the Jpeg2000 codestream Header box - JP2C Header box.
1440  *
1441  * @param       cio                     the stream to write data to.
1442  * @param       jp2                     the jpeg2000 file codec.
1443  * @param       p_manager       user event manager.
1444  *
1445  * @return true if writting was successful.
1446 */
1447 opj_bool opj_jp2_write_jp2c(opj_jp2_v2_t *jp2,
1448                                                         opj_stream_private_t *cio,
1449                                                         opj_event_mgr_t * p_manager ) 
1450 {
1451         OPJ_OFF_T j2k_codestream_exit;
1452         OPJ_BYTE l_data_header [8];
1453         
1454         // preconditions
1455         assert(jp2 != 00);
1456         assert(cio != 00);
1457         assert(p_manager != 00);
1458         assert(opj_stream_has_seek(cio));
1459         
1460         j2k_codestream_exit = opj_stream_tell(cio);
1461         opj_write_bytes(l_data_header,
1462                     (OPJ_UINT32) (j2k_codestream_exit - jp2->j2k_codestream_offset),
1463                     4); /* size of codestream */
1464         opj_write_bytes(l_data_header + 4,JP2_JP2C,4);                                                                     /* JP2C */
1465
1466         if (! opj_stream_seek(cio,jp2->j2k_codestream_offset,p_manager)) {
1467                 opj_event_msg_v2(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
1468                 return OPJ_FALSE;
1469         }
1470         
1471         if (opj_stream_write_data(cio,l_data_header,8,p_manager) != 8) {
1472                 opj_event_msg_v2(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
1473                 return OPJ_FALSE;
1474         }
1475
1476         if (! opj_stream_seek(cio,j2k_codestream_exit,p_manager)) {
1477                 opj_event_msg_v2(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
1478                 return OPJ_FALSE;
1479         }
1480
1481         return OPJ_TRUE;
1482 }
1483
1484 /**
1485  * Writes a jpeg2000 file signature box.
1486  *
1487  * @param cio the stream to write data to.
1488  * @param       jp2                     the jpeg2000 file codec.
1489  * @param p_manager the user event manager.
1490  * 
1491  * @return true if writting was successful.
1492  */
1493 opj_bool opj_jp2_write_jp(      opj_jp2_v2_t *jp2,
1494                                             opj_stream_private_t *cio,
1495                                                 opj_event_mgr_t * p_manager ) 
1496 {
1497         /* 12 bytes will be read */
1498         unsigned char l_signature_data [12];
1499
1500         // preconditions
1501         assert(cio != 00);
1502         assert(jp2 != 00);
1503         assert(p_manager != 00);
1504
1505         /* write box length */
1506         opj_write_bytes(l_signature_data,12,4);
1507         /* writes box type */
1508         opj_write_bytes(l_signature_data+4,JP2_JP,4);
1509         /* writes magic number*/
1510         opj_write_bytes(l_signature_data+8,0x0d0a870a,4);
1511         
1512         if (opj_stream_write_data(cio,l_signature_data,12,p_manager) != 12) {
1513                 return OPJ_FALSE;
1514         }
1515
1516         return OPJ_TRUE;
1517 }
1518
1519 static int write_fidx( int offset_jp2c, int length_jp2c, int offset_idx, int length_idx, opj_cio_t *cio)
1520 {  
1521   int len, lenp;
1522   
1523   lenp = cio_tell( cio);
1524   cio_skip( cio, 4);              /* L [at the end] */
1525   cio_write( cio, JPIP_FIDX, 4);  /* IPTR           */
1526   
1527   write_prxy( offset_jp2c, length_jp2c, offset_idx, length_idx, cio);
1528
1529   len = cio_tell( cio)-lenp;
1530   cio_seek( cio, lenp);
1531   cio_write( cio, len, 4);        /* L              */
1532   cio_seek( cio, lenp+len);  
1533
1534   return len;
1535 }
1536
1537 static void write_prxy( int offset_jp2c, int length_jp2c, int offset_idx, int length_idx, opj_cio_t *cio)
1538 {
1539   int len, lenp;
1540
1541   lenp = cio_tell( cio);
1542   cio_skip( cio, 4);              /* L [at the end] */
1543   cio_write( cio, JPIP_PRXY, 4);  /* IPTR           */
1544   
1545   cio_write( cio, offset_jp2c, 8); /* OOFF           */
1546   cio_write( cio, length_jp2c, 4); /* OBH part 1     */
1547   cio_write( cio, JP2_JP2C, 4);        /* OBH part 2     */
1548   
1549   cio_write( cio, 1,1);           /* NI             */
1550
1551   cio_write( cio, offset_idx, 8);  /* IOFF           */
1552   cio_write( cio, length_idx, 4);  /* IBH part 1     */
1553   cio_write( cio, JPIP_CIDX, 4);   /* IBH part 2     */
1554
1555   len = cio_tell( cio)-lenp;
1556   cio_seek( cio, lenp);
1557   cio_write( cio, len, 4);        /* L              */
1558   cio_seek( cio, lenp+len);
1559 }
1560
1561 static void write_iptr( int offset, int length, opj_cio_t *cio)
1562 {
1563   int len, lenp;
1564   
1565   lenp = cio_tell( cio);
1566   cio_skip( cio, 4);              /* L [at the end] */
1567   cio_write( cio, JPIP_IPTR, 4);  /* IPTR           */
1568   
1569   cio_write( cio, offset, 8);
1570   cio_write( cio, length, 8);
1571
1572   len = cio_tell( cio)-lenp;
1573   cio_seek( cio, lenp);
1574   cio_write( cio, len, 4);        /* L             */
1575   cio_seek( cio, lenp+len);
1576 }
1577
1578
1579 /* ----------------------------------------------------------------------- */
1580 /* JP2 decoder interface                                             */
1581 /* ----------------------------------------------------------------------- */
1582
1583
1584 void opj_jp2_setup_decoder(opj_jp2_v2_t *jp2, opj_dparameters_t *parameters)
1585 {
1586         /* setup the J2K codec */
1587         opj_j2k_setup_decoder(jp2->j2k, parameters);
1588
1589         /* further JP2 initializations go here */
1590         jp2->color.jp2_has_colr = 0;
1591     jp2->ignore_pclr_cmap_cdef = parameters->flags & OPJ_DPARAMETERS_IGNORE_PCLR_CMAP_CDEF_FLAG;
1592 }
1593
1594
1595 /* ----------------------------------------------------------------------- */
1596 /* JP2 encoder interface                                             */
1597 /* ----------------------------------------------------------------------- */
1598
1599 void opj_jp2_setup_encoder(     opj_jp2_v2_t *jp2, 
1600                             opj_cparameters_t *parameters, 
1601                             opj_image_t *image, 
1602                             opj_event_mgr_t * p_manager) 
1603 {                           
1604     OPJ_UINT32 i;
1605         int depth_0, sign;
1606
1607         if(!jp2 || !parameters || !image)
1608                 return;
1609
1610         /* setup the J2K codec */
1611         /* ------------------- */
1612
1613         /* Check if number of components respects standard */
1614         if (image->numcomps < 1 || image->numcomps > 16384) {
1615                 opj_event_msg_v2(p_manager, EVT_ERROR, "Invalid number of components specified while setting up JP2 encoder\n");
1616                 return;
1617         }
1618
1619         j2k_setup_encoder_v2(jp2->j2k, parameters, image, p_manager );
1620
1621         /* setup the JP2 codec */
1622         /* ------------------- */
1623         
1624         /* Profile box */
1625
1626         jp2->brand = JP2_JP2;   /* BR */
1627         jp2->minversion = 0;    /* MinV */
1628         jp2->numcl = 1;
1629         jp2->cl = (unsigned int*) opj_malloc(jp2->numcl * sizeof(unsigned int));
1630         jp2->cl[0] = JP2_JP2;   /* CL0 : JP2 */
1631
1632         /* Image Header box */
1633
1634         jp2->numcomps = image->numcomps;        /* NC */
1635         jp2->comps = (opj_jp2_comps_t*) opj_malloc(jp2->numcomps * sizeof(opj_jp2_comps_t));
1636         jp2->h = image->y1 - image->y0;         /* HEIGHT */
1637         jp2->w = image->x1 - image->x0;         /* WIDTH */
1638         /* BPC */
1639         depth_0 = image->comps[0].prec - 1;
1640         sign = image->comps[0].sgnd;
1641         jp2->bpc = depth_0 + (sign << 7);
1642         for (i = 1; i < image->numcomps; i++) {
1643                 int depth = image->comps[i].prec - 1;
1644                 sign = image->comps[i].sgnd;
1645                 if (depth_0 != depth)
1646                         jp2->bpc = 255;
1647         }
1648         jp2->C = 7;                     /* C : Always 7 */
1649         jp2->UnkC = 0;          /* UnkC, colorspace specified in colr box */
1650         jp2->IPR = 0;           /* IPR, no intellectual property */
1651         
1652         /* BitsPerComponent box */
1653         for (i = 0; i < image->numcomps; i++) {
1654                 jp2->comps[i].bpcc = image->comps[i].prec - 1 + (image->comps[i].sgnd << 7);
1655         }
1656
1657         /* Colour Specification box */
1658         if ((image->numcomps == 1 || image->numcomps == 3) && (jp2->bpc != 255)) {
1659                 jp2->meth = 1;  /* METH: Enumerated colourspace */
1660         } else {
1661                 jp2->meth = 2;  /* METH: Restricted ICC profile */
1662         }
1663         if (jp2->meth == 1) {
1664                 if (image->color_space == 1)
1665                         jp2->enumcs = 16;       /* sRGB as defined by IEC 61966�2�1 */
1666                 else if (image->color_space == 2)
1667                         jp2->enumcs = 17;       /* greyscale */
1668                 else if (image->color_space == 3)
1669                         jp2->enumcs = 18;       /* YUV */
1670         } else {
1671                 jp2->enumcs = 0;                /* PROFILE (??) */
1672         }
1673         jp2->precedence = 0;    /* PRECEDENCE */
1674         jp2->approx = 0;                /* APPROX */
1675         
1676         // jp2->jpip_on = parameters->jpip_on;
1677 }
1678
1679
1680 opj_bool opj_jp2_encode(opj_jp2_v2_t *jp2, 
1681                                                 opj_stream_private_t *stream, 
1682                                                 opj_event_mgr_t * p_manager)
1683 {
1684         return j2k_encode_v2(jp2->j2k, stream, p_manager);
1685 }
1686
1687
1688 /**
1689  * Ends the decompression procedures and possibiliy add data to be read after the
1690  * codestream.
1691  */
1692 opj_bool opj_jp2_end_decompress(opj_jp2_v2_t *jp2, 
1693                                 opj_stream_private_t *cio,
1694                                 opj_event_mgr_t * p_manager
1695                                 )
1696 {
1697         /* preconditions */
1698         assert(jp2 != 00);
1699         assert(cio != 00);
1700         assert(p_manager != 00);
1701
1702         /* customization of the end encoding */
1703         opj_jp2_setup_end_header_reading(jp2);
1704
1705         /* write header */
1706         if (! opj_jp2_exec (jp2,jp2->m_procedure_list,cio,p_manager)) {
1707                 return OPJ_FALSE;
1708         }
1709
1710         return j2k_end_decompress(jp2->j2k, cio, p_manager);
1711 }
1712
1713 /**
1714  * Ends the compression procedures and possibility add data to be read after the
1715  * codestream.
1716  */
1717 opj_bool opj_jp2_end_compress(  opj_jp2_v2_t *jp2,
1718                                                             opj_stream_private_t *cio,
1719                                                             opj_event_mgr_t * p_manager
1720                                 )
1721 {
1722         /* preconditions */
1723         assert(jp2 != 00);
1724         assert(cio != 00);
1725         assert(p_manager != 00);
1726
1727         /* customization of the end encoding */
1728         opj_jp2_setup_end_header_writting(jp2);
1729
1730         if (! j2k_end_compress(jp2->j2k,cio,p_manager)) {
1731                 return OPJ_FALSE;
1732         }
1733
1734         /* write header */
1735         return opj_jp2_exec(jp2,jp2->m_procedure_list,cio,p_manager);
1736 }
1737
1738
1739 /**
1740  * Sets up the procedures to do on writing header after the codestream.
1741  * Developers wanting to extend the library can add their own writing procedures.
1742  */
1743 void opj_jp2_setup_end_header_writting (opj_jp2_v2_t *jp2)
1744 {
1745         /* preconditions */
1746         assert(jp2 != 00);
1747
1748         opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)opj_jp2_write_jp2c );
1749         /* DEVELOPER CORNER, add your custom procedures */
1750 }
1751
1752 /**
1753  * Sets up the procedures to do on reading header after the codestream.
1754  * Developers wanting to extend the library can add their own writing procedures.
1755  */
1756 void opj_jp2_setup_end_header_reading (opj_jp2_v2_t *jp2)
1757 {
1758         /* preconditions */
1759         assert(jp2 != 00);
1760         opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)opj_jp2_read_header_procedure );
1761         /* DEVELOPER CORNER, add your custom procedures */
1762 }
1763
1764 /**
1765  * The default validation procedure without any extension.
1766  *
1767  * @param       jp2                             the jpeg2000 codec to validate.
1768  * @param       cio                             the input stream to validate.
1769  * @param       p_manager               the user event manager.
1770  *
1771  * @return true if the parameters are correct.
1772  */
1773 opj_bool opj_jp2_default_validation (   opj_jp2_v2_t * jp2,
1774                                         opj_stream_private_t *cio,
1775                                         opj_event_mgr_t * p_manager 
1776                                         )
1777 {
1778         opj_bool l_is_valid = OPJ_TRUE;
1779         unsigned int i;
1780
1781         /* preconditions */
1782         assert(jp2 != 00);
1783         assert(cio != 00);
1784         assert(p_manager != 00);
1785
1786         /* JPEG2000 codec validation */
1787         /*TODO*/
1788
1789         /* STATE checking */
1790         /* make sure the state is at 0 */
1791         l_is_valid &= (jp2->jp2_state == JP2_STATE_NONE);
1792
1793         /* make sure not reading a jp2h ???? WEIRD */
1794         l_is_valid &= (jp2->jp2_img_state == JP2_IMG_STATE_NONE);
1795
1796         /* POINTER validation */
1797         /* make sure a j2k codec is present */
1798         l_is_valid &= (jp2->j2k != 00);
1799
1800         /* make sure a procedure list is present */
1801         l_is_valid &= (jp2->m_procedure_list != 00);
1802
1803         /* make sure a validation list is present */
1804         l_is_valid &= (jp2->m_validation_list != 00);
1805
1806         /* PARAMETER VALIDATION */
1807         /* number of components */
1808         l_is_valid &= (jp2->numcl > 0);
1809         /* width */
1810         l_is_valid &= (jp2->h > 0);
1811         /* height */
1812         l_is_valid &= (jp2->w > 0);
1813         /* precision */
1814         for (i = 0; i < jp2->numcomps; ++i)     {
1815                 l_is_valid &= (jp2->comps[i].bpcc > 0);
1816         }
1817
1818         /* METH */
1819         l_is_valid &= ((jp2->meth > 0) && (jp2->meth < 3));
1820
1821         /* stream validation */
1822         /* back and forth is needed */
1823         l_is_valid &= opj_stream_has_seek(cio);
1824
1825         return l_is_valid;
1826 }
1827
1828 /**
1829  * Reads a jpeg2000 file header structure.
1830  *
1831  * @param stream the stream to read data from.
1832  * @param jp2 the jpeg2000 file header structure.
1833  * @param p_manager the user event manager.
1834  *
1835  * @return true if the box is valid.
1836  */
1837 static opj_bool opj_jp2_read_header_procedure(  opj_jp2_v2_t *jp2,
1838                                                 opj_stream_private_t *stream,
1839                                                 opj_event_mgr_t * p_manager 
1840                                                 )
1841 {
1842         opj_jp2_box_t box;
1843         OPJ_UINT32 l_nb_bytes_read;
1844         const opj_jp2_header_handler_t * l_current_handler;
1845         OPJ_UINT32 l_last_data_size = BOX_SIZE;
1846         OPJ_UINT32 l_current_data_size;
1847         unsigned char * l_current_data = 00;
1848
1849         /* preconditions */
1850         assert(stream != 00);
1851         assert(jp2 != 00);
1852         assert(p_manager != 00);
1853
1854         l_current_data = (unsigned char*)opj_malloc(l_last_data_size);
1855
1856         if (l_current_data == 00) {
1857                 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to handle jpeg2000 file header\n");
1858                 return OPJ_FALSE;
1859         }
1860         memset(l_current_data, 0 , l_last_data_size);
1861
1862         while (opj_jp2_read_boxhdr(&box,&l_nb_bytes_read,stream,p_manager)) {
1863                 /* is it the codestream box ? */
1864                 if (box.type == JP2_JP2C) {
1865                         if (jp2->jp2_state & JP2_STATE_HEADER) {
1866                                 jp2->jp2_state |= JP2_STATE_CODESTREAM;
1867                 opj_free(l_current_data);
1868                                 return OPJ_TRUE;
1869                         }
1870                         else {
1871                                 opj_event_msg_v2(p_manager, EVT_ERROR, "bad placed jpeg codestream\n");
1872                                 opj_free(l_current_data);
1873                                 return OPJ_FALSE;
1874                         }
1875                 }
1876                 else if (box.length == 0) {
1877                         opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot handle box of undefined sizes\n");
1878                         opj_free(l_current_data);
1879                         return OPJ_FALSE;
1880                 }
1881
1882                 l_current_handler = opj_jp2_find_handler(box.type);
1883                 l_current_data_size = box.length - l_nb_bytes_read;
1884
1885                 if (l_current_handler != 00) {
1886                         if (l_current_data_size > l_last_data_size) {
1887                                 l_current_data = (unsigned char*)opj_realloc(l_current_data,l_current_data_size);
1888                                 if (!l_current_data){
1889                                         opj_free(l_current_data);
1890                                         return OPJ_FALSE;
1891                                 }
1892                                 l_last_data_size = l_current_data_size;
1893                         }
1894
1895                         l_nb_bytes_read = opj_stream_read_data(stream,l_current_data,l_current_data_size,p_manager);
1896                         if (l_nb_bytes_read != l_current_data_size) {
1897                                 opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with reading JPEG2000 box, stream error\n");
1898                                 return OPJ_FALSE;
1899                         }
1900
1901                         if (! l_current_handler->handler(jp2,l_current_data,l_current_data_size,p_manager)) {
1902                                 opj_free(l_current_data);
1903                                 return OPJ_FALSE;
1904                         }
1905                 }
1906                 else {
1907                         jp2->jp2_state |= JP2_STATE_UNKNOWN;
1908                         if (opj_stream_skip(stream,l_current_data_size,p_manager) != l_current_data_size) {
1909                                 opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with skipping JPEG2000 box, stream error\n");
1910                                 opj_free(l_current_data);
1911                                 return OPJ_FALSE;
1912                         }
1913                 }
1914         }
1915
1916         opj_free(l_current_data);
1917
1918         return OPJ_TRUE;
1919 }
1920
1921 /**
1922  * Excutes the given procedures on the given codec.
1923  *
1924  * @param       p_procedure_list        the list of procedures to execute
1925  * @param       jp2                                     the jpeg2000 file codec to execute the procedures on.
1926  * @param       stream                                  the stream to execute the procedures on.
1927  * @param       p_manager                       the user manager.
1928  *
1929  * @return      true                            if all the procedures were successfully executed.
1930  */
1931 static opj_bool opj_jp2_exec (  opj_jp2_v2_t * jp2,
1932                                 opj_procedure_list_t * p_procedure_list,
1933                                 opj_stream_private_t *stream,
1934                                 opj_event_mgr_t * p_manager 
1935                                 )
1936
1937 {
1938         opj_bool (** l_procedure) (opj_jp2_v2_t * jp2, opj_stream_private_t *, opj_event_mgr_t *) = 00;
1939         opj_bool l_result = OPJ_TRUE;
1940         OPJ_UINT32 l_nb_proc, i;
1941
1942         /* preconditions */
1943         assert(p_procedure_list != 00);
1944         assert(jp2 != 00);
1945         assert(stream != 00);
1946         assert(p_manager != 00);
1947
1948         l_nb_proc = opj_procedure_list_get_nb_procedures(p_procedure_list);
1949         l_procedure = (opj_bool (**) (opj_jp2_v2_t * jp2, opj_stream_private_t *, opj_event_mgr_t *)) opj_procedure_list_get_first_procedure(p_procedure_list);
1950
1951         for     (i=0;i<l_nb_proc;++i) {
1952                 l_result = l_result && (*l_procedure) (jp2,stream,p_manager);
1953                 ++l_procedure;
1954         }
1955
1956         /* and clear the procedure list at the end. */
1957         opj_procedure_list_clear(p_procedure_list);
1958         return l_result;
1959 }
1960
1961 /**
1962  * Starts a compression scheme, i.e. validates the codec parameters, writes the header.
1963  *
1964  * @param       jp2             the jpeg2000 file codec.
1965  * @param       cio             the stream object.
1966  *
1967  * @return true if the codec is valid.
1968  */
1969 opj_bool opj_jp2_start_compress(opj_jp2_v2_t *jp2,
1970                                 opj_stream_private_t *stream,
1971                                 opj_image_t * p_image,
1972                                 opj_event_mgr_t * p_manager
1973                                 )
1974 {
1975         /* preconditions */
1976         assert(jp2 != 00);
1977         assert(stream != 00);
1978         assert(p_manager != 00);
1979
1980         /* customization of the validation */
1981         opj_jp2_setup_encoding_validation (jp2);
1982
1983         /* validation of the parameters codec */
1984         if (! opj_jp2_exec(jp2,jp2->m_validation_list,stream,p_manager)) {
1985                 return OPJ_FALSE;
1986         }
1987
1988         /* customization of the encoding */
1989         opj_jp2_setup_header_writting(jp2);
1990
1991         /* write header */
1992         if (! opj_jp2_exec (jp2,jp2->m_procedure_list,stream,p_manager)) {
1993                 return OPJ_FALSE;
1994         }
1995
1996         return j2k_start_compress(jp2->j2k,stream,p_image,p_manager);
1997 }
1998
1999 /**
2000  * Finds the execution function related to the given box id.
2001  *
2002  * @param       p_id    the id of the handler to fetch.
2003  *
2004  * @return      the given handler or 00 if it could not be found.
2005  */
2006 const opj_jp2_header_handler_t * opj_jp2_find_handler (OPJ_UINT32 p_id)
2007 {
2008         OPJ_UINT32 i, l_handler_size = sizeof(jp2_header) / sizeof(opj_jp2_header_handler_t);
2009
2010         for (i=0;i<l_handler_size;++i) {
2011                 if (jp2_header[i].id == p_id) {
2012                         return &jp2_header[i];
2013                 }
2014         }
2015         return NULL;
2016 }
2017
2018 /**
2019  * Finds the image execution function related to the given box id.
2020  *
2021  * @param       p_id    the id of the handler to fetch.
2022  *
2023  * @return      the given handler or 00 if it could not be found.
2024  */
2025 static const opj_jp2_header_handler_t * opj_jp2_img_find_handler (OPJ_UINT32 p_id)
2026 {
2027         OPJ_UINT32 i, l_handler_size = sizeof(jp2_img_header) / sizeof(opj_jp2_header_handler_t);
2028         for (i=0;i<l_handler_size;++i)
2029         {
2030                 if (jp2_img_header[i].id == p_id) {
2031                         return &jp2_img_header[i];
2032                 }
2033         }
2034
2035         return NULL;
2036 }
2037
2038
2039 /**
2040  * Reads a jpeg2000 file signature box.
2041  *
2042  * @param       p_header_data   the data contained in the signature box.
2043  * @param       jp2                             the jpeg2000 file codec.
2044  * @param       p_header_size   the size of the data contained in the signature box.
2045  * @param       p_manager               the user event manager.
2046  *
2047  * @return true if the file signature box is valid.
2048  */
2049 static opj_bool opj_jp2_read_jp(opj_jp2_v2_t *jp2,
2050                                 OPJ_BYTE * p_header_data,
2051                                 OPJ_UINT32 p_header_size,
2052                                 opj_event_mgr_t * p_manager
2053                                 )
2054
2055 {
2056         unsigned int l_magic_number;
2057
2058         /* preconditions */
2059         assert(p_header_data != 00);
2060         assert(jp2 != 00);
2061         assert(p_manager != 00);
2062
2063         if (jp2->jp2_state != JP2_STATE_NONE) {
2064                 opj_event_msg_v2(p_manager, EVT_ERROR, "The signature box must be the first box in the file.\n");
2065                 return OPJ_FALSE;
2066         }
2067
2068         /* assure length of data is correct (4 -> magic number) */
2069         if (p_header_size != 4) {
2070                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with JP signature Box size\n");
2071                 return OPJ_FALSE;
2072         }
2073
2074         /* rearrange data */
2075         opj_read_bytes(p_header_data,&l_magic_number,4);
2076         if (l_magic_number != 0x0d0a870a ) {
2077                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with JP Signature : bad magic number\n");
2078                 return OPJ_FALSE;
2079         }
2080
2081         jp2->jp2_state |= JP2_STATE_SIGNATURE;
2082
2083         return OPJ_TRUE;
2084 }
2085
2086
2087 /**
2088  * Reads a a FTYP box - File type box
2089  *
2090  * @param       p_header_data   the data contained in the FTYP box.
2091  * @param       jp2                             the jpeg2000 file codec.
2092  * @param       p_header_size   the size of the data contained in the FTYP box.
2093  * @param       p_manager               the user event manager.
2094  *
2095  * @return true if the FTYP box is valid.
2096  */
2097 static opj_bool opj_jp2_read_ftyp(      opj_jp2_v2_t *jp2,
2098                                                                         OPJ_BYTE * p_header_data,
2099                                                                         OPJ_UINT32 p_header_size,
2100                                                                         opj_event_mgr_t * p_manager 
2101                                     )
2102 {
2103         OPJ_UINT32 i, l_remaining_bytes;
2104
2105         /* preconditions */
2106         assert(p_header_data != 00);
2107         assert(jp2 != 00);
2108         assert(p_manager != 00);
2109
2110         if (jp2->jp2_state != JP2_STATE_SIGNATURE) {
2111                 opj_event_msg_v2(p_manager, EVT_ERROR, "The ftyp box must be the second box in the file.\n");
2112                 return OPJ_FALSE;
2113         }
2114
2115         /* assure length of data is correct */
2116         if (p_header_size < 8) {
2117                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with FTYP signature Box size\n");
2118                 return OPJ_FALSE;
2119         }
2120
2121         opj_read_bytes(p_header_data,&jp2->brand,4);            /* BR */
2122         p_header_data += 4;
2123
2124         opj_read_bytes(p_header_data,&jp2->minversion,4);               /* MinV */
2125         p_header_data += 4;
2126
2127         l_remaining_bytes = p_header_size - 8;
2128
2129         /* the number of remaining bytes should be a multiple of 4 */
2130         if ((l_remaining_bytes & 0x3) != 0) {
2131                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with FTYP signature Box size\n");
2132                 return OPJ_FALSE;
2133         }
2134
2135         /* div by 4 */
2136         jp2->numcl = l_remaining_bytes >> 2;
2137         if (jp2->numcl) {
2138                 jp2->cl = (unsigned int *) opj_malloc(jp2->numcl * sizeof(unsigned int));
2139                 if (jp2->cl == 00) {
2140                         opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory with FTYP Box\n");
2141                         return OPJ_FALSE;
2142                 }
2143                 memset(jp2->cl,0,jp2->numcl * sizeof(unsigned int));
2144         }
2145
2146         for (i = 0; i < jp2->numcl; ++i)
2147         {
2148                 opj_read_bytes(p_header_data,&jp2->cl[i],4);            /* CLi */
2149                 p_header_data += 4;
2150         }
2151
2152         jp2->jp2_state |= JP2_STATE_FILE_TYPE;
2153
2154         return OPJ_TRUE;
2155 }
2156
2157 /**
2158  * Skips the Jpeg2000 Codestream Header box - JP2C Header box.
2159  *
2160  * @param       cio                     the stream to write data to.
2161  * @param       jp2                     the jpeg2000 file codec.
2162  * @param       p_manager       user event manager.
2163  *
2164  * @return true if writting was successful.
2165 */
2166 opj_bool opj_jp2_skip_jp2c(     opj_jp2_v2_t *jp2,
2167                                                 opj_stream_private_t *stream,
2168                                                 opj_event_mgr_t * p_manager )
2169 {
2170         /* preconditions */
2171         assert(jp2 != 00);
2172         assert(stream != 00);
2173         assert(p_manager != 00);
2174
2175         jp2->j2k_codestream_offset = opj_stream_tell(stream);
2176
2177         if (opj_stream_skip(stream,8,p_manager) != 8) {
2178                 return OPJ_FALSE;
2179         }
2180
2181         return OPJ_TRUE;
2182 }
2183
2184 /**
2185  * Reads the Jpeg2000 file Header box - JP2 Header box (warning, this is a super box).
2186  *
2187  * @param       p_header_data   the data contained in the file header box.
2188  * @param       jp2                             the jpeg2000 file codec.
2189  * @param       p_header_size   the size of the data contained in the file header box.
2190  * @param       p_manager               the user event manager.
2191  *
2192  * @return true if the JP2 Header box was successfully reconized.
2193 */
2194 static opj_bool opj_jp2_read_jp2h(  opj_jp2_v2_t *jp2,
2195                                     OPJ_BYTE *p_header_data,
2196                                     OPJ_UINT32 p_header_size,
2197                                     opj_event_mgr_t * p_manager 
2198                                     )
2199 {
2200         OPJ_UINT32 l_box_size=0, l_current_data_size = 0;
2201         opj_jp2_box_t box;
2202         const opj_jp2_header_handler_t * l_current_handler;
2203
2204         /* preconditions */
2205         assert(p_header_data != 00);
2206         assert(jp2 != 00);
2207         assert(p_manager != 00);
2208
2209         /* make sure the box is well placed */
2210         if ((jp2->jp2_state & JP2_STATE_FILE_TYPE) != JP2_STATE_FILE_TYPE ) {
2211                 opj_event_msg_v2(p_manager, EVT_ERROR, "The  box must be the first box in the file.\n");
2212                 return OPJ_FALSE;
2213         }
2214
2215         jp2->jp2_img_state = JP2_IMG_STATE_NONE;
2216
2217         /* iterate while remaining data */
2218         while (p_header_size > 0) {
2219
2220                 if (! opj_jp2_read_boxhdr_char(&box,p_header_data,&l_box_size,p_header_size, p_manager)) {
2221                         opj_event_msg_v2(p_manager, EVT_ERROR, "Stream error while reading JP2 Header box\n");
2222                         return OPJ_FALSE;
2223                 }
2224
2225                 if (box.length > p_header_size) {
2226                         opj_event_msg_v2(p_manager, EVT_ERROR, "Stream error while reading JP2 Header box: box length is inconsistent.\n");
2227                         return OPJ_FALSE;
2228                 }
2229
2230                 l_current_handler = opj_jp2_img_find_handler(box.type);
2231                 l_current_data_size = box.length - l_box_size;
2232                 p_header_data += l_box_size;
2233
2234                 if (l_current_handler != 00) {
2235                         if (! l_current_handler->handler(jp2,p_header_data,l_current_data_size,p_manager)) {
2236                                 return OPJ_FALSE;
2237                         }
2238                 }
2239                 else {
2240                         jp2->jp2_img_state |= JP2_IMG_STATE_UNKNOWN;
2241                 }
2242
2243                 p_header_data += l_current_data_size;
2244                 p_header_size -= box.length;
2245         }
2246
2247         jp2->jp2_state |= JP2_STATE_HEADER;
2248
2249         return OPJ_TRUE;
2250 }
2251
2252 /**
2253  * Reads a box header. The box is the way data is packed inside a jpeg2000 file structure. Data is read from a character string
2254  *
2255  * @param       p_data                                  the character string to read data from.
2256  * @param       box                                             the box structure to fill.
2257  * @param       p_number_bytes_read             pointer to an int that will store the number of bytes read from the stream (shoul usually be 2).
2258  * @param       p_box_max_size                  the maximum number of bytes in the box.
2259  *
2260  * @return      true if the box is reconized, false otherwise
2261 */
2262 opj_bool opj_jp2_read_boxhdr_char(   opj_jp2_box_t *box,
2263                                      OPJ_BYTE * p_data,
2264                                      OPJ_UINT32 * p_number_bytes_read,
2265                                      OPJ_UINT32 p_box_max_size,
2266                                      opj_event_mgr_t * p_manager 
2267                                      )
2268 {
2269         OPJ_UINT32 l_value;
2270
2271         /* preconditions */
2272         assert(p_data != 00);
2273         assert(box != 00);
2274         assert(p_number_bytes_read != 00);
2275         assert(p_manager != 00);
2276
2277         if (p_box_max_size < 8) {
2278                 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot handle box of less than 8 bytes\n");
2279                 return OPJ_FALSE;
2280         }
2281
2282         /* process read data */
2283         opj_read_bytes(p_data, &l_value, 4);
2284         p_data += 4;
2285         box->length = (OPJ_INT32)(l_value);
2286
2287         opj_read_bytes(p_data, &l_value, 4);
2288         p_data += 4;
2289         box->type = (OPJ_INT32)(l_value);
2290
2291         *p_number_bytes_read = 8;
2292
2293         /* do we have a "special very large box ?" */
2294         /* read then the XLBox */
2295         if (box->length == 1) {
2296                 unsigned int l_xl_part_size;
2297
2298                 if (p_box_max_size < 16) {
2299                         opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot handle XL box of less than 16 bytes\n");
2300                         return OPJ_FALSE;
2301                 }
2302
2303                 opj_read_bytes(p_data,&l_xl_part_size, 4);
2304                 p_data += 4;
2305                 *p_number_bytes_read += 4;
2306
2307                 if (l_xl_part_size != 0) {
2308                         opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot handle box sizes higher than 2^32\n");
2309                         return OPJ_FALSE;
2310                 }
2311
2312                 opj_read_bytes(p_data, &l_value, 4);
2313                 *p_number_bytes_read += 4;
2314                 box->length = (OPJ_INT32)(l_value);
2315
2316                 if (box->length == 0) {
2317                         opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot handle box of undefined sizes\n");
2318                         return OPJ_FALSE;
2319                 }
2320         }
2321         else if (box->length == 0) {
2322                 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot handle box of undefined sizes\n");
2323                 return OPJ_FALSE;
2324         }
2325
2326         return OPJ_TRUE;
2327 }
2328
2329
2330 /**
2331  * Reads a jpeg2000 file header structure.
2332  *
2333  * @param cio the stream to read data from.
2334  * @param jp2 the jpeg2000 file header structure.
2335  * @param p_manager the user event manager.
2336  *
2337  * @return true if the box is valid.
2338  */
2339 opj_bool opj_jp2_read_header(   opj_stream_private_t *p_stream,
2340                                 opj_jp2_v2_t *jp2,
2341                                 opj_image_t ** p_image,
2342                                 opj_event_mgr_t * p_manager 
2343                                 )
2344 {
2345         /* preconditions */
2346         assert(jp2 != 00);
2347         assert(p_stream != 00);
2348         assert(p_manager != 00);
2349
2350         /* customization of the validation */
2351         opj_jp2_setup_decoding_validation (jp2);
2352
2353         /* customization of the encoding */
2354         opj_jp2_setup_header_reading(jp2);
2355
2356         /* validation of the parameters codec */
2357         if (! opj_jp2_exec(jp2,jp2->m_validation_list,p_stream,p_manager)) {
2358                 return OPJ_FALSE;
2359         }
2360
2361         /* read header */
2362         if (! opj_jp2_exec (jp2,jp2->m_procedure_list,p_stream,p_manager)) {
2363                 return OPJ_FALSE;
2364         }
2365
2366         return j2k_read_header( p_stream,
2367                                                         jp2->j2k,
2368                                                         p_image,
2369                                                         p_manager);
2370 }
2371
2372 /**
2373  * Sets up the validation ,i.e. adds the procedures to launch to make sure the codec parameters
2374  * are valid. Developers wanting to extend the library can add their own validation procedures.
2375  */
2376 void opj_jp2_setup_encoding_validation (opj_jp2_v2_t *jp2)
2377 {
2378         /* preconditions */
2379         assert(jp2 != 00);
2380
2381         opj_procedure_list_add_procedure(jp2->m_validation_list, (opj_procedure)opj_jp2_default_validation);
2382         /* DEVELOPER CORNER, add your custom validation procedure */
2383 }
2384
2385 /**
2386  * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
2387  * are valid. Developpers wanting to extend the library can add their own validation procedures.
2388  */
2389 void opj_jp2_setup_decoding_validation (opj_jp2_v2_t *jp2)
2390 {
2391         /* preconditions */
2392         assert(jp2 != 00);
2393         /* DEVELOPER CORNER, add your custom validation procedure */
2394 }
2395
2396 /**
2397  * Sets up the procedures to do on writting header.
2398  * Developers wanting to extend the library can add their own writing procedures.
2399  */
2400 void opj_jp2_setup_header_writting (opj_jp2_v2_t *jp2)
2401 {
2402         /* preconditions */
2403         assert(jp2 != 00);
2404
2405         opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)opj_jp2_write_jp );
2406         opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)opj_jp2_write_ftyp );
2407         opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)opj_jp2_write_jp2h );
2408         opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)opj_jp2_skip_jp2c );
2409
2410         /* DEVELOPER CORNER, insert your custom procedures */
2411
2412 }
2413
2414 /**
2415  * Sets up the procedures to do on reading header.
2416  * Developpers wanting to extend the library can add their own writting procedures.
2417  */
2418 void opj_jp2_setup_header_reading (opj_jp2_v2_t *jp2)
2419 {
2420         /* preconditions */
2421         assert(jp2 != 00);
2422
2423         opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)opj_jp2_read_header_procedure );
2424         /* DEVELOPER CORNER, add your custom procedures */
2425 }
2426
2427
2428 /**
2429  * Reads a tile header.
2430  * @param       p_j2k           the jpeg2000 codec.
2431  * @param       p_stream                        the stream to write data to.
2432  * @param       p_manager       the user event manager.
2433  */
2434 opj_bool opj_jp2_read_tile_header ( opj_jp2_v2_t * p_jp2,
2435                                     OPJ_UINT32 * p_tile_index,
2436                                     OPJ_UINT32 * p_data_size,
2437                                     OPJ_INT32 * p_tile_x0,
2438                                     OPJ_INT32 * p_tile_y0,
2439                                     OPJ_INT32 * p_tile_x1,
2440                                     OPJ_INT32 * p_tile_y1,
2441                                     OPJ_UINT32 * p_nb_comps,
2442                                     opj_bool * p_go_on,
2443                                     opj_stream_private_t *p_stream,
2444                                     opj_event_mgr_t * p_manager 
2445                                     )
2446 {
2447         return j2k_read_tile_header(p_jp2->j2k,
2448                                                                 p_tile_index,
2449                                                                 p_data_size,
2450                                                                 p_tile_x0, p_tile_y0,
2451                                                                 p_tile_x1, p_tile_y1,
2452                                                                 p_nb_comps,
2453                                                                 p_go_on,
2454                                                                 p_stream,
2455                                                                 p_manager);
2456 }
2457
2458 /**
2459  * Writes a tile.
2460  * @param       p_j2k           the jpeg2000 codec.
2461  * @param       p_stream                        the stream to write data to.
2462  * @param       p_manager       the user event manager.
2463  */
2464 opj_bool opj_jp2_write_tile (   opj_jp2_v2_t *p_jp2,
2465                                                             OPJ_UINT32 p_tile_index,
2466                                                             OPJ_BYTE * p_data,
2467                                                             OPJ_UINT32 p_data_size,
2468                                                             opj_stream_private_t *p_stream,
2469                                                             opj_event_mgr_t * p_manager 
2470                                 )
2471                                 
2472 {
2473         return j2k_write_tile (p_jp2->j2k,p_tile_index,p_data,p_data_size,p_stream,p_manager);
2474 }
2475
2476 /**
2477  * Decode tile data.
2478  * @param       p_j2k           the jpeg2000 codec.
2479  * @param       p_stream                        the stream to write data to.
2480  * @param       p_manager       the user event manager.
2481  */
2482 opj_bool opj_jp2_decode_tile (  opj_jp2_v2_t * p_jp2,
2483                                 OPJ_UINT32 p_tile_index,
2484                                 OPJ_BYTE * p_data,
2485                                 OPJ_UINT32 p_data_size,
2486                                 opj_stream_private_t *p_stream,
2487                                 opj_event_mgr_t * p_manager 
2488                                 )
2489 {
2490         return j2k_decode_tile (p_jp2->j2k,p_tile_index,p_data,p_data_size,p_stream,p_manager);
2491 }
2492
2493 /**
2494  * Destroys a jpeg2000 file decompressor.
2495  *
2496  * @param       jp2             a jpeg2000 file decompressor.
2497  */
2498 void opj_jp2_destroy(opj_jp2_v2_t *jp2)
2499 {
2500         if (jp2) {
2501                 /* destroy the J2K codec */
2502                 j2k_destroy(jp2->j2k);
2503                 jp2->j2k = 00;
2504
2505                 if (jp2->comps) {
2506                         opj_free(jp2->comps);
2507                         jp2->comps = 00;
2508                 }
2509
2510                 if (jp2->cl) {
2511                         opj_free(jp2->cl);
2512                         jp2->cl = 00;
2513                 }
2514
2515                 if (jp2->color.icc_profile_buf) {
2516                         opj_free(jp2->color.icc_profile_buf);
2517                         jp2->color.icc_profile_buf = 00;
2518                 }
2519
2520                 if (jp2->color.jp2_cdef) {
2521                         if (jp2->color.jp2_cdef->info) {
2522                                 opj_free(jp2->color.jp2_cdef->info);
2523                                 jp2->color.jp2_cdef->info = NULL;
2524                         }
2525
2526                         opj_free(jp2->color.jp2_cdef);
2527                         jp2->color.jp2_cdef = 00;
2528                 }
2529
2530                 if (jp2->color.jp2_pclr) {
2531                         if (jp2->color.jp2_pclr->cmap) {
2532                                 opj_free(jp2->color.jp2_pclr->cmap);
2533                                 jp2->color.jp2_pclr->cmap = NULL;
2534                         }
2535                         if (jp2->color.jp2_pclr->channel_sign) {
2536                                 opj_free(jp2->color.jp2_pclr->channel_sign);
2537                                 jp2->color.jp2_pclr->channel_sign = NULL;
2538                         }
2539                         if (jp2->color.jp2_pclr->channel_size) {
2540                                 opj_free(jp2->color.jp2_pclr->channel_size);
2541                                 jp2->color.jp2_pclr->channel_size = NULL;
2542                         }
2543                         if (jp2->color.jp2_pclr->entries) {
2544                                 opj_free(jp2->color.jp2_pclr->entries);
2545                                 jp2->color.jp2_pclr->entries = NULL;
2546                         }
2547
2548                         opj_free(jp2->color.jp2_pclr);
2549                         jp2->color.jp2_pclr = 00;
2550                 }
2551
2552                 if (jp2->m_validation_list) {
2553                         opj_procedure_list_destroy(jp2->m_validation_list);
2554                         jp2->m_validation_list = 00;
2555                 }
2556
2557                 if (jp2->m_procedure_list) {
2558                         opj_procedure_list_destroy(jp2->m_procedure_list);
2559                         jp2->m_procedure_list = 00;
2560                 }
2561
2562                 opj_free(jp2);
2563         }
2564 }
2565
2566 /**
2567  * Sets the given area to be decoded. This function should be called right after opj_read_header and before any tile header reading.
2568  *
2569  * @param       p_jp2                   the jpeg2000 codec.
2570  * @param       p_end_x                 the right position of the rectangle to decode (in image coordinates).
2571  * @param       p_start_y               the up position of the rectangle to decode (in image coordinates).
2572  * @param       p_end_y                 the bottom position of the rectangle to decode (in image coordinates).
2573  * @param       p_manager               the user event manager
2574  *
2575  * @return      true                    if the area could be set.
2576  */
2577 opj_bool opj_jp2_set_decode_area(       opj_jp2_v2_t *p_jp2,
2578                                                                     opj_image_t* p_image,
2579                                                                     OPJ_INT32 p_start_x, OPJ_INT32 p_start_y,
2580                                                                     OPJ_INT32 p_end_x, OPJ_INT32 p_end_y,
2581                                                                     opj_event_mgr_t * p_manager 
2582                                     )
2583 {
2584         return j2k_set_decode_area(p_jp2->j2k, p_image, p_start_x, p_start_y, p_end_x, p_end_y, p_manager);
2585 }
2586
2587 /**
2588  * Get the decoded tile.
2589  *
2590  * @param       jp2                     the jpeg2000 codec.
2591  * @param       p_stream                input_stream
2592  * @param       p_image                 output image.   .
2593  * @param       p_manager               the user event manager
2594  * @param       tile_index              index of the tile we want decode
2595  *
2596  * @return      true                    if succeed.
2597  */
2598 opj_bool opj_jp2_get_tile(      opj_jp2_v2_t *p_jp2,
2599                             opj_stream_private_t *p_stream,
2600                             opj_image_t* p_image,
2601                             opj_event_mgr_t * p_manager,
2602                             OPJ_UINT32 tile_index 
2603                             )
2604 {
2605         if (!p_image)
2606                 return OPJ_FALSE;
2607
2608         opj_event_msg_v2(p_manager, EVT_WARNING, "JP2 box which are after the codestream will not be read by this function.\n");
2609
2610         if (! j2k_get_tile(p_jp2->j2k, p_stream, p_image, p_manager, tile_index) ){
2611                 opj_event_msg_v2(p_manager, EVT_ERROR, "Failed to decode the codestream in the JP2 file\n");
2612                 return OPJ_FALSE;
2613         }
2614
2615         /* Set Image Color Space */
2616         if (p_jp2->enumcs == 16)
2617                 p_image->color_space = CLRSPC_SRGB;
2618         else if (p_jp2->enumcs == 17)
2619                 p_image->color_space = CLRSPC_GRAY;
2620         else if (p_jp2->enumcs == 18)
2621                 p_image->color_space = CLRSPC_SYCC;
2622         else
2623                 p_image->color_space = CLRSPC_UNKNOWN;
2624
2625         /* Apply the color space if needed */
2626         if(p_jp2->color.jp2_cdef) {
2627                 opj_jp2_apply_cdef(p_image, &(p_jp2->color));
2628         }
2629
2630         if(p_jp2->color.jp2_pclr) {
2631                 /* Part 1, I.5.3.4: Either both or none : */
2632                 if( !p_jp2->color.jp2_pclr->cmap)
2633                         opj_jp2_free_pclr(&(p_jp2->color));
2634                 else
2635                         opj_jp2_apply_pclr(p_image, &(p_jp2->color));
2636         }
2637
2638         if(p_jp2->color.icc_profile_buf) {
2639                 p_image->icc_profile_buf = p_jp2->color.icc_profile_buf;
2640                 p_image->icc_profile_len = p_jp2->color.icc_profile_len;
2641                 p_jp2->color.icc_profile_buf = NULL;
2642         }
2643
2644         return OPJ_TRUE;
2645 }
2646
2647
2648
2649 /* ----------------------------------------------------------------------- */
2650 /* JP2 encoder interface                                             */
2651 /* ----------------------------------------------------------------------- */
2652
2653 opj_jp2_v2_t* opj_jp2_create(opj_bool p_is_decoder)
2654 {
2655         opj_jp2_v2_t *jp2 = (opj_jp2_v2_t*)opj_malloc(sizeof(opj_jp2_v2_t));
2656         if (jp2) {
2657                 memset(jp2,0,sizeof(opj_jp2_v2_t));
2658
2659                 /* create the J2K codec */
2660                 if (! p_is_decoder) {
2661                         jp2->j2k = j2k_create_compress_v2();
2662                 }
2663                 else {
2664                         jp2->j2k = opj_j2k_create_decompress();
2665                 }
2666
2667                 if (jp2->j2k == 00) {
2668                         opj_jp2_destroy(jp2);
2669                         return 00;
2670                 }
2671
2672                 /* Color structure */
2673                 jp2->color.icc_profile_buf = NULL;
2674                 jp2->color.icc_profile_len = 0;
2675                 jp2->color.jp2_cdef = NULL;
2676                 jp2->color.jp2_pclr = NULL;
2677                 jp2->color.jp2_has_colr = 0;
2678
2679                 /* validation list creation */
2680                 jp2->m_validation_list = opj_procedure_list_create();
2681                 if (! jp2->m_validation_list) {
2682                         opj_jp2_destroy(jp2);
2683                         return 00;
2684                 }
2685
2686                 /* execution list creation */
2687                 jp2->m_procedure_list = opj_procedure_list_create();
2688                 if (! jp2->m_procedure_list) {
2689                         opj_jp2_destroy(jp2);
2690                         return 00;
2691                 }
2692         }
2693
2694         return jp2;
2695 }
2696
2697 void jp2_dump(opj_jp2_v2_t* p_jp2, OPJ_INT32 flag, FILE* out_stream)
2698 {
2699         /* preconditions */
2700         assert(p_jp2 != 00);
2701
2702         j2k_dump(p_jp2->j2k,
2703                                         flag,
2704                                         out_stream);
2705 }
2706
2707 opj_codestream_index_t* jp2_get_cstr_index(opj_jp2_v2_t* p_jp2)
2708 {
2709         return j2k_get_cstr_index(p_jp2->j2k);
2710 }
2711
2712 opj_codestream_info_v2_t* jp2_get_cstr_info(opj_jp2_v2_t* p_jp2)
2713 {
2714         return j2k_get_cstr_info(p_jp2->j2k);
2715 }
2716
2717 opj_bool opj_jp2_set_decoded_resolution_factor(opj_jp2_v2_t *p_jp2, 
2718                                                OPJ_UINT32 res_factor, 
2719                                                opj_event_mgr_t * p_manager)
2720 {
2721         return j2k_set_decoded_resolution_factor(p_jp2->j2k, res_factor, p_manager);
2722 }
2723