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