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