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