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