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