1bd7b4a9aba9d4d123542aed973f920e162a4b04
[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) 2008, Jerome Fimes, Communications & Systemes <jerome.fimes@c-s.fr>
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 "jp2.h"
33 #include "cio.h"
34 #include "opj_malloc.h"
35 #include "event.h"
36 #include "j2k.h"
37 #include "function_list.h"
38 #include "assert.h"
39
40 /** @defgroup JP2 JP2 - JPEG-2000 file format reader/writer */
41 /*@{*/
42
43 #define BOX_SIZE        1024
44
45
46
47 /** @name Local static functions */
48 /*@{*/
49
50
51
52 /**
53  * Writes the Jpeg2000 file Header box - JP2 Header box (warning, this is a super box).
54  *
55  * @param       cio                     the stream to write data to.
56  * @param       jp2                     the jpeg2000 file codec.
57  * @param       p_manager       user event manager.
58  *
59  * @return true if writting was successful.
60 */
61 bool jp2_write_jp2h(
62                                                 opj_jp2_t *jp2,
63                                                 struct opj_stream_private *cio,
64                                                 struct opj_event_mgr * p_manager
65                                         );
66
67 /**
68  * Skips the Jpeg2000 Codestream Header box - JP2C Header box.
69  *
70  * @param       cio                     the stream to write data to.
71  * @param       jp2                     the jpeg2000 file codec.
72  * @param       p_manager       user event manager.
73  *
74  * @return true if writting was successful.
75 */
76 bool jp2_skip_jp2c(
77                                                 opj_jp2_t *jp2,
78                                                 struct opj_stream_private *cio,
79                                                 struct opj_event_mgr * p_manager
80                                         );
81
82 /**
83  * Reads the Jpeg2000 file Header box - JP2 Header box (warning, this is a super box).
84  *
85  * @param       p_header_data   the data contained in the file header box.
86  * @param       jp2                             the jpeg2000 file codec.
87  * @param       p_header_size   the size of the data contained in the file header box.
88  * @param       p_manager               the user event manager.
89  *
90  * @return true if the JP2 Header box was successfully reconized.
91 */
92 bool jp2_read_jp2h(
93                                                 opj_jp2_t *jp2, 
94                                                 unsigned char * p_header_data, 
95                                                 unsigned int p_header_size,
96                                                 struct opj_event_mgr * p_manager
97                                         );
98
99 /**
100  * Writes the Jpeg2000 codestream Header box - JP2C Header box. This function must be called AFTER the coding has been done.
101  *
102  * @param       cio                     the stream to write data to.
103  * @param       jp2                     the jpeg2000 file codec.
104  * @param       p_manager       user event manager.
105  *
106  * @return true if writting was successful.
107 */
108 static bool jp2_write_jp2c(
109                                    opj_jp2_t *jp2,
110                                    struct opj_stream_private *cio,
111                                    struct opj_event_mgr * p_manager
112                                    );
113
114 /**
115  * Reads a box header. The box is the way data is packed inside a jpeg2000 file structure.
116  * 
117  * @param       cio                                             the input stream to read data from.
118  * @param       box                                             the box structure to fill.
119  * @param       p_number_bytes_read             pointer to an int that will store the number of bytes read from the stream (shoul usually be 2).
120  * @param       p_manager                               user event manager.
121  * 
122  * @return      true if the box is reconized, false otherwise
123 */
124 static bool jp2_read_boxhdr(
125                                                                 opj_jp2_box_t *box,
126                                                                 OPJ_UINT32 * p_number_bytes_read, 
127                                                                 struct opj_stream_private *cio, 
128                                                                 struct opj_event_mgr * p_manager
129                                                         );
130
131 /**
132  * Reads a box header. The box is the way data is packed inside a jpeg2000 file structure. Data is read from a character string
133  * 
134  * @param       p_data                                  the character string to read data from.
135  * @param       box                                             the box structure to fill.
136  * @param       p_number_bytes_read             pointer to an int that will store the number of bytes read from the stream (shoul usually be 2).
137  * @param       p_box_max_size                  the maximum number of bytes in the box.
138  * 
139  * @return      true if the box is reconized, false otherwise
140 */
141 static bool jp2_read_boxhdr_char(
142                                                                 opj_jp2_box_t *box,
143                                                                 OPJ_BYTE * p_data, 
144                                                                 OPJ_UINT32 * p_number_bytes_read, 
145                                                                 OPJ_UINT32 p_box_max_size,
146                                                                 struct opj_event_mgr * p_manager
147                                                         );
148
149 /**
150  * Reads a jpeg2000 file signature box.
151  *
152  * @param       p_header_data   the data contained in the signature box.
153  * @param       jp2                             the jpeg2000 file codec.
154  * @param       p_header_size   the size of the data contained in the signature box.
155  * @param       p_manager               the user event manager.
156  * 
157  * @return true if the file signature box is valid.
158  */
159 static bool jp2_read_jp(
160                                         opj_jp2_t *jp2, 
161                                         unsigned char * p_header_data, 
162                                         unsigned int p_header_size,
163                                         struct opj_event_mgr * p_manager
164                                  ); 
165
166 /**
167  * Writes a jpeg2000 file signature box.
168  *
169  * @param       cio                     the stream to write data to.
170  * @param       jp2                     the jpeg2000 file codec.
171  * @param       p_manager       the user event manager.
172  * 
173  * @return true if writting was successful.
174  */
175 static bool jp2_write_jp(
176                                                         opj_jp2_t *jp2,
177                                                         struct opj_stream_private *cio,
178                                                         struct opj_event_mgr * p_manager
179                                                 );
180
181 /**
182  * Writes a FTYP box - File type box
183  *
184  * @param       cio                     the stream to write data to.
185  * @param       jp2                     the jpeg2000 file codec.
186  * @param       p_manager       the user event manager.
187  * 
188  * @return      true if writting was successful.
189  */
190 static bool jp2_write_ftyp(
191                                                         opj_jp2_t *jp2,
192                                                         struct opj_stream_private *cio,
193                                                         struct opj_event_mgr * p_manager
194                                                   );
195
196 /**
197  * Reads a a FTYP box - File type box
198  *
199  * @param       p_header_data   the data contained in the FTYP box.
200  * @param       jp2                             the jpeg2000 file codec.
201  * @param       p_header_size   the size of the data contained in the FTYP box.
202  * @param       p_manager               the user event manager.
203  * 
204  * @return true if the FTYP box is valid.
205  */
206 static bool jp2_read_ftyp(
207                                                         opj_jp2_t *jp2, 
208                                                         unsigned char * p_header_data, 
209                                                         unsigned int p_header_size,
210                                                         struct opj_event_mgr * p_manager
211                                                 );
212
213 /**
214  * Reads a IHDR box - Image Header box
215  * 
216  * @param       p_image_header_data                     pointer to actual data (already read from file)
217  * @param       jp2                                                     the jpeg2000 file codec.
218  * @param       p_image_header_size                     the size of the image header
219  * @param       p_manager                                       the user event manager.
220  * 
221  * @return      true if the image header is valid, fale else.
222  */
223 static bool jp2_read_ihdr(
224                                                         opj_jp2_t *jp2, 
225                                                         unsigned char * p_image_header_data, 
226                                                         unsigned int p_image_header_size, 
227                                                         struct opj_event_mgr * p_manager
228                                                   );
229
230 /**
231  * Writes the Image Header box - Image Header box.
232  *
233  * @param jp2                                   jpeg2000 file codec.
234  * @param p_nb_bytes_written    pointer to store the nb of bytes written by the function.
235  * 
236  * @return      the data being copied.
237 */
238 static unsigned char * jp2_write_ihdr(
239                                                                 opj_jp2_t *jp2, 
240                                                                 unsigned int * p_nb_bytes_written
241                                                          );
242
243 /**
244  * Reads a Bit per Component box.
245  * 
246  * @param       p_bpc_header_data                       pointer to actual data (already read from file)
247  * @param       jp2                                                     the jpeg2000 file codec.
248  * @param       p_bpc_header_size                       the size of the bpc header
249  * @param       p_manager                                       the user event manager.
250  * 
251  * @return      true if the bpc header is valid, fale else.
252  */
253 static bool jp2_read_bpcc(
254                                                         opj_jp2_t *jp2,
255                                                         unsigned char * p_bpc_header_data, 
256                                                         unsigned int p_bpc_header_size, 
257                                                         struct opj_event_mgr * p_manager
258                                                   );
259
260
261 /**
262  * Writes the Bit per Component box.
263  *
264  * @param       jp2                                             jpeg2000 file codec.
265  * @param       p_nb_bytes_written              pointer to store the nb of bytes written by the function.
266  * 
267  * @return      the data being copied.
268 */
269 static unsigned char * jp2_write_bpcc(
270                                                                 opj_jp2_t *jp2, 
271                                                                 unsigned int * p_nb_bytes_written
272                                                          );
273
274 /**
275  * Reads the Colour Specification box.
276  *
277  * @param       p_colr_header_data                      pointer to actual data (already read from file)
278  * @param       jp2                                                     the jpeg2000 file codec.
279  * @param       p_colr_header_size                      the size of the color header
280  * @param       p_manager                                       the user event manager.
281  * 
282  * @return      true if the bpc header is valid, fale else.
283 */
284 static bool jp2_read_colr(
285                                                         opj_jp2_t *jp2,
286                                                         unsigned char * p_colr_header_data, 
287                                                         unsigned int p_colr_header_size, 
288                                                         struct opj_event_mgr * p_manager
289                                                   );
290
291 /**
292  * Writes the Colour Specification box.
293  *
294  * @param jp2                                   jpeg2000 file codec.
295  * @param p_nb_bytes_written    pointer to store the nb of bytes written by the function.
296  * 
297  * @return      the data being copied.
298 */
299 static unsigned char *jp2_write_colr(
300                                                                 opj_jp2_t *jp2, 
301                                                                 unsigned int * p_nb_bytes_written
302                                                         );
303
304
305 /**
306  * Reads a jpeg2000 file header structure.
307  *
308  * @param cio the stream to read data from.
309  * @param jp2 the jpeg2000 file header structure.
310  * @param p_manager the user event manager.
311  *
312  * @return true if the box is valid.
313  */
314 bool jp2_read_header_procedure(
315                                                                 opj_jp2_t *jp2,
316                                                                 struct opj_stream_private *cio,
317                                                                 struct opj_event_mgr * p_manager
318                                                         );
319 /**
320  * Excutes the given procedures on the given codec.
321  *
322  * @param       p_procedure_list        the list of procedures to execute
323  * @param       jp2                                     the jpeg2000 file codec to execute the procedures on.
324  * @param       cio                                     the stream to execute the procedures on.
325  * @param       p_manager                       the user manager.
326  * 
327  * @return      true                            if all the procedures were successfully executed.
328  */
329 static bool jp2_exec (
330                                         opj_jp2_t * jp2,
331                                         struct opj_procedure_list * p_procedure_list,
332                                         struct opj_stream_private *cio,
333                                         struct opj_event_mgr * p_manager
334                                   );
335 /**
336  * Finds the execution function related to the given box id.
337  * 
338  * @param       p_id    the id of the handler to fetch.
339  *
340  * @return      the given handler or NULL if it could not be found.
341  */
342 static const opj_jp2_header_handler_t * jp2_find_handler (int p_id);
343
344 /**
345  * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
346  * are valid. Developpers wanting to extend the library can add their own validation procedures.
347  */
348 static void jp2_setup_encoding_validation (opj_jp2_t *jp2);
349
350 /**
351  * Sets up the procedures to do on writting header. Developpers wanting to extend the library can add their own writting procedures.
352  */
353 static void jp2_setup_header_writting (opj_jp2_t *jp2);
354
355 /** 
356  * The default validation procedure without any extension.
357  * 
358  * @param       jp2                             the jpeg2000 codec to validate.
359  * @param       cio                             the input stream to validate.
360  * @param       p_manager               the user event manager.
361  *
362  * @return true if the parameters are correct.
363  */
364 bool jp2_default_validation (
365                                                                         opj_jp2_t * jp2,
366                                                                         struct opj_stream_private *cio,
367                                                                         struct opj_event_mgr * p_manager
368                                                                                 );
369
370 /**
371  * Finds the execution function related to the given box id.
372  * 
373  * @param       p_id    the id of the handler to fetch.
374  *
375  * @return      the given handler or NULL if it could not be found.
376  */
377 static const opj_jp2_header_handler_t * jp2_find_handler (
378                                                                                                 int p_id
379                                                                                                 );
380
381 /**
382  * Finds the image execution function related to the given box id.
383  * 
384  * @param       p_id    the id of the handler to fetch.
385  *
386  * @return      the given handler or NULL if it could not be found.
387  */
388 static const opj_jp2_header_handler_t * jp2_img_find_handler (
389                                                                                                 int p_id
390                                                                                                 );
391
392 /**
393  * Sets up the procedures to do on writting header after the codestream. 
394  * Developpers wanting to extend the library can add their own writting procedures.
395  */
396 static void jp2_setup_end_header_writting (opj_jp2_t *jp2);
397
398 /**
399  * Sets up the procedures to do on reading header after the codestream. 
400  * Developpers wanting to extend the library can add their own writting procedures.
401  */
402 static void jp2_setup_end_header_reading (opj_jp2_t *jp2);
403
404 /**
405  * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
406  * are valid. Developpers wanting to extend the library can add their own validation procedures.
407  */
408 static void jp2_setup_decoding_validation (opj_jp2_t *jp2);
409
410 /**
411  * Sets up the procedures to do on reading header. 
412  * Developpers wanting to extend the library can add their own writting procedures.
413  */
414 static void jp2_setup_header_reading (opj_jp2_t *jp2);
415 /*@}*/
416
417 /*@}*/
418
419 /* ----------------------------------------------------------------------- */
420 const opj_jp2_header_handler_t jp2_header [] = 
421 {
422         {JP2_JP,jp2_read_jp},
423         {JP2_FTYP,jp2_read_ftyp},
424         {JP2_JP2H,jp2_read_jp2h}
425 };
426
427 const opj_jp2_header_handler_t jp2_img_header [] = 
428 {
429         {JP2_IHDR,jp2_read_ihdr},
430         {JP2_COLR,jp2_read_colr},
431         {JP2_BPCC,jp2_read_bpcc}
432 };
433 /**
434  * Finds the execution function related to the given box id.
435  * 
436  * @param       p_id    the id of the handler to fetch.
437  *
438  * @return      the given handler or 00 if it could not be found.
439  */
440 const opj_jp2_header_handler_t * jp2_find_handler (
441                                                                                                 int p_id
442                                                                                                 )
443 {
444         unsigned int i, l_handler_size = sizeof(jp2_header) / sizeof(opj_jp2_header_handler_t);
445         for
446                 (i=0;i<l_handler_size;++i)
447         {
448                 if
449                         (jp2_header[i].id == p_id)
450                 {
451                         return &jp2_header[i];
452                 }
453         }
454         return 00;
455 }
456
457 /**
458  * Finds the image execution function related to the given box id.
459  * 
460  * @param       p_id    the id of the handler to fetch.
461  *
462  * @return      the given handler or 00 if it could not be found.
463  */
464 static const opj_jp2_header_handler_t * jp2_img_find_handler (
465                                                                                                 int p_id
466                                                                                                 )
467 {
468         unsigned int i, l_handler_size = sizeof(jp2_img_header) / sizeof(opj_jp2_header_handler_t);
469         for
470                 (i=0;i<l_handler_size;++i)
471         {
472                 if
473                         (jp2_img_header[i].id == p_id)
474                 {
475                         return &jp2_img_header[i];
476                 }
477         }
478         return 00;
479
480 }
481
482 /**
483  * Reads a jpeg2000 file header structure.
484  *
485  * @param cio the stream to read data from.
486  * @param jp2 the jpeg2000 file header structure.
487  * @param p_manager the user event manager.
488  *
489  * @return true if the box is valid.
490  */
491 bool jp2_read_header_procedure(
492                                          opj_jp2_t *jp2,
493                                          opj_stream_private_t *cio,
494                                          opj_event_mgr_t * p_manager) 
495 {
496         opj_jp2_box_t box;
497         unsigned int l_nb_bytes_read;
498         const opj_jp2_header_handler_t * l_current_handler;
499         unsigned int l_last_data_size = BOX_SIZE;
500         unsigned int l_current_data_size;
501         unsigned char * l_current_data = 00;
502
503         // preconditions
504         assert(cio != 00);
505         assert(jp2 != 00);
506         assert(p_manager != 00);
507
508         l_current_data = opj_malloc(l_last_data_size);
509         
510         if
511                 (l_current_data == 00)
512         {
513                 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to handle jpeg2000 file header\n");
514                 return false;
515         }
516         memset(l_current_data, 0 , l_last_data_size);
517         while
518                 (jp2_read_boxhdr(&box,&l_nb_bytes_read,cio,p_manager))
519         {
520                 // is it the codestream box ?
521                 if
522                         (box.type == JP2_JP2C)
523                 {
524                         if
525                                 (jp2->jp2_state & JP2_STATE_HEADER)
526                         {
527                                 jp2->jp2_state |= JP2_STATE_CODESTREAM;
528                                 return true;
529                         }
530                         else
531                         {
532                                 opj_event_msg(p_manager, EVT_ERROR, "bad placed jpeg codestream\n");
533                                 return false;
534                         }
535                 }
536                 else if
537                         (box.length == 0)
538                 {
539                         opj_event_msg(p_manager, EVT_ERROR, "Cannot handle box of undefined sizes\n");
540                         return false;
541                 }
542
543                 l_current_handler = jp2_find_handler(box.type);
544                 l_current_data_size = box.length - l_nb_bytes_read;
545
546                 if
547                         (l_current_handler != 00)
548                 {
549                         if
550                                 (l_current_data_size > l_last_data_size)
551                         {
552                                 l_current_data = opj_realloc(l_current_data,l_current_data_size);
553                                 l_last_data_size = l_current_data_size;
554                         }
555                         l_nb_bytes_read = opj_stream_read_data(cio,l_current_data,l_current_data_size,p_manager);
556                         if
557                                 (l_nb_bytes_read != l_current_data_size)
558                         {
559                                 opj_event_msg(p_manager, EVT_ERROR, "Problem with reading JPEG2000 box, stream error\n");
560                                 return false;
561                         }
562                         if
563                                 (! l_current_handler->handler(jp2,l_current_data,l_current_data_size,p_manager))
564                         {
565                                 return false;
566                         }
567                 }
568                 else
569                 {
570                         jp2->jp2_state |= JP2_STATE_UNKNOWN;
571                         if
572                                 (opj_stream_skip(cio,l_current_data_size,p_manager) != l_current_data_size)
573                         {
574                                 opj_event_msg(p_manager, EVT_ERROR, "Problem with skipping JPEG2000 box, stream error\n");
575                                 return false;
576                         }
577                 }
578         }
579         return true;
580 }
581
582 /**
583  * Reads a box header. The box is the way data is packed inside a jpeg2000 file structure.
584  * 
585  * @param       cio                                             the input stream to read data from.
586  * @param       box                                             the box structure to fill.
587  * @param       p_number_bytes_read             pointer to an int that will store the number of bytes read from the stream (should usually be 8).
588  * @param       p_manager                               user event manager.
589  * 
590  * @return      true if the box is reconized, false otherwise
591 */
592 bool jp2_read_boxhdr(opj_jp2_box_t *box, OPJ_UINT32 * p_number_bytes_read,opj_stream_private_t *cio, opj_event_mgr_t * p_manager) 
593 {
594         /* read header from file */
595         unsigned char l_data_header [8];
596
597         // preconditions
598         assert(cio != 00);
599         assert(box != 00);
600         assert(p_number_bytes_read != 00);
601         assert(p_manager != 00);
602
603         *p_number_bytes_read = opj_stream_read_data(cio,l_data_header,8,p_manager);
604         if
605                 (*p_number_bytes_read != 8)
606         {
607                 return false;
608         }
609         /* process read data */
610         opj_read_bytes(l_data_header,&(box->length), 4);
611         opj_read_bytes(l_data_header+4,&(box->type), 4);
612         
613         // do we have a "special very large box ?"
614         // read then the XLBox
615         if 
616                 (box->length == 1) 
617         {
618                 OPJ_UINT32 l_xl_part_size;
619                 OPJ_UINT32 l_nb_bytes_read = opj_stream_read_data(cio,l_data_header,8,p_manager);
620                 if
621                         (l_nb_bytes_read != 8)
622                 {
623                         if
624                                 (l_nb_bytes_read > 0)
625                         {
626                                 *p_number_bytes_read += l_nb_bytes_read;
627                         }
628                         return false;
629                 }
630                 opj_read_bytes(l_data_header,&l_xl_part_size, 4);
631                 if 
632                         (l_xl_part_size != 0) 
633                 {
634                         opj_event_msg(p_manager, EVT_ERROR, "Cannot handle box sizes higher than 2^32\n");
635                         return false;
636                 }
637                 opj_read_bytes(l_data_header,&(box->length), 4);
638         }
639         return true;
640 }
641
642 /**
643  * Reads a box header. The box is the way data is packed inside a jpeg2000 file structure. Data is read from a character string
644  * 
645  * @param       p_data                                  the character string to read data from.
646  * @param       box                                             the box structure to fill.
647  * @param       p_number_bytes_read             pointer to an int that will store the number of bytes read from the stream (shoul usually be 2).
648  * @param       p_box_max_size                  the maximum number of bytes in the box.
649  * 
650  * @return      true if the box is reconized, false otherwise
651 */
652 static bool jp2_read_boxhdr_char(
653                                                                 opj_jp2_box_t *box,
654                                                                 OPJ_BYTE * p_data,
655                                                                 OPJ_UINT32 * p_number_bytes_read, 
656                                                                 OPJ_UINT32 p_box_max_size,
657                                                                 opj_event_mgr_t * p_manager
658                                                         )
659 {
660         // preconditions
661         assert(p_data != 00);
662         assert(box != 00);
663         assert(p_number_bytes_read != 00);
664         assert(p_manager != 00);
665
666         if
667                 (p_box_max_size < 8)
668         {
669                 opj_event_msg(p_manager, EVT_ERROR, "Cannot handle box of less than 8 bytes\n");
670                 return false;
671         }
672         /* process read data */
673         opj_read_bytes(p_data,&(box->length), 4);
674         p_data += 4;
675         opj_read_bytes(p_data,&(box->type), 4);
676         p_data += 4;
677         *p_number_bytes_read = 8;
678
679         // do we have a "special very large box ?"
680         // read then the XLBox
681         if 
682                 (box->length == 1) 
683         {
684                 unsigned int l_xl_part_size;
685                 if
686                         (p_box_max_size < 16)
687                 {
688                         opj_event_msg(p_manager, EVT_ERROR, "Cannot handle XL box of less than 16 bytes\n");
689                         return false;
690                 }
691
692                 opj_read_bytes(p_data,&l_xl_part_size, 4);
693                 p_data += 4;
694                 *p_number_bytes_read += 4;
695                 if 
696                         (l_xl_part_size != 0) 
697                 {
698                         opj_event_msg(p_manager, EVT_ERROR, "Cannot handle box sizes higher than 2^32\n");
699                         return false;
700                 }
701                 opj_read_bytes(p_data,&(box->length), 4);
702                 *p_number_bytes_read += 4;
703                 if 
704                         (box->length == 0) 
705                 {
706                         opj_event_msg(p_manager, EVT_ERROR, "Cannot handle box of undefined sizes\n");
707                         return false;
708                 }
709                 
710         }
711         else if 
712                 (box->length == 0) 
713         {
714                 opj_event_msg(p_manager, EVT_ERROR, "Cannot handle box of undefined sizes\n");
715                 return false;
716         }
717         return true;
718 }
719
720
721 /**
722  * Reads a jpeg2000 file signature box.
723  *
724  * @param       p_header_data   the data contained in the signature box.
725  * @param       jp2                             the jpeg2000 file codec.
726  * @param       p_header_size   the size of the data contained in the signature box.
727  * @param       p_manager               the user event manager.
728  * 
729  * @return true if the file signature box is valid.
730  */
731 bool jp2_read_jp(
732                                         opj_jp2_t *jp2, 
733                                         unsigned char * p_header_data, 
734                                         unsigned int p_header_size,
735                                         opj_event_mgr_t * p_manager
736                                  ) 
737 {
738         unsigned int l_magic_number;
739         
740         // preconditions
741         assert(p_header_data != 00);
742         assert(jp2 != 00);
743         assert(p_manager != 00);
744
745         if
746                 (jp2->jp2_state != JP2_STATE_NONE)
747         {
748                 opj_event_msg(p_manager, EVT_ERROR, "The signature box must be the first box in the file.\n");
749                 return false;
750         }
751         
752
753         /* assure length of data is correct (4 -> magic number) */
754         if
755                 (p_header_size != 4)
756         {
757                 opj_event_msg(p_manager, EVT_ERROR, "Error with JP signature Box size\n");
758                 return false;
759         }
760         
761         // rearrange data
762         opj_read_bytes(p_header_data,&l_magic_number,4);
763         if 
764                 (l_magic_number != 0x0d0a870a ) 
765         {
766                 opj_event_msg(p_manager, EVT_ERROR, "Error with JP Signature : bad magic number\n");
767                 return false;
768         }
769         jp2->jp2_state |= JP2_STATE_SIGNATURE;
770         return true;
771 }
772
773 /**
774  * Reads a a FTYP box - File type box
775  *
776  * @param       p_header_data   the data contained in the FTYP box.
777  * @param       jp2                             the jpeg2000 file codec.
778  * @param       p_header_size   the size of the data contained in the FTYP box.
779  * @param       p_manager               the user event manager.
780  * 
781  * @return true if the FTYP box is valid.
782  */
783 bool jp2_read_ftyp(
784                                                         opj_jp2_t *jp2, 
785                                                         unsigned char * p_header_data, 
786                                                         unsigned int p_header_size,
787                                                         opj_event_mgr_t * p_manager
788                                                 ) 
789 {
790         unsigned int i;
791         unsigned int l_remaining_bytes;
792
793         // preconditions
794         assert(p_header_data != 00);
795         assert(jp2 != 00);
796         assert(p_manager != 00);
797
798         if
799                 (jp2->jp2_state != JP2_STATE_SIGNATURE)
800         {
801                 opj_event_msg(p_manager, EVT_ERROR, "The ftyp box must be the second box in the file.\n");
802                 return false;
803         }
804         
805         /* assure length of data is correct */
806         if
807                 (p_header_size < 8)
808         {
809                 opj_event_msg(p_manager, EVT_ERROR, "Error with FTYP signature Box size\n");
810                 return false;
811         }
812         
813         opj_read_bytes(p_header_data,&jp2->brand,4);            /* BR */
814         p_header_data += 4;
815
816         opj_read_bytes(p_header_data,&jp2->minversion,4);               /* MinV */
817         p_header_data += 4;
818         
819         l_remaining_bytes = p_header_size - 8;
820
821         /* the number of remaining bytes should be a multiple of 4 */
822         if
823                 ((l_remaining_bytes & 0x3) != 0)
824         {
825                 opj_event_msg(p_manager, EVT_ERROR, "Error with FTYP signature Box size\n");
826                 return false;
827         }
828         /* div by 4 */
829         jp2->numcl = l_remaining_bytes >> 2;
830         if
831                 (jp2->numcl)
832         {
833                 jp2->cl = (unsigned int *) opj_malloc(jp2->numcl * sizeof(unsigned int));
834                 if
835                         (jp2->cl == 00)
836                 {
837                         opj_event_msg(p_manager, EVT_ERROR, "Not enough memory with FTYP Box\n");
838                         return false;
839                 }
840                 memset(jp2->cl,0,jp2->numcl * sizeof(unsigned int));
841         }
842
843
844         for 
845                 (i = 0; i < jp2->numcl; ++i) 
846         {
847                 opj_read_bytes(p_header_data,&jp2->cl[i],4);            /* CLi */
848                 p_header_data += 4;
849
850         }
851         jp2->jp2_state |= JP2_STATE_FILE_TYPE;
852         return true;
853 }
854
855 /**
856  * Writes a jpeg2000 file signature box.
857  *
858  * @param cio the stream to write data to.
859  * @param       jp2                     the jpeg2000 file codec.
860  * @param p_manager the user event manager.
861  * 
862  * @return true if writting was successful.
863  */
864 bool jp2_write_jp (
865                                         opj_jp2_t *jp2,
866                                         opj_stream_private_t *cio,
867                                         opj_event_mgr_t * p_manager
868                                    ) 
869 {
870         /* 12 bytes will be read */
871         unsigned char l_signature_data [12];
872
873         // preconditions
874         assert(cio != 00);
875         assert(jp2 != 00);
876         assert(p_manager != 00);
877
878
879         /* write box length */
880         opj_write_bytes(l_signature_data,12,4);
881         /* writes box type */
882         opj_write_bytes(l_signature_data+4,JP2_JP,4);
883         /* writes magic number*/
884         opj_write_bytes(l_signature_data+8,0x0d0a870a,4);
885         if
886                 (opj_stream_write_data(cio,l_signature_data,12,p_manager) != 12)
887         {
888                 return false;
889         }
890         return true;
891 }
892
893
894 /**
895  * Writes a FTYP box - File type box
896  *
897  * @param       cio                     the stream to write data to.
898  * @param       jp2                     the jpeg2000 file codec.
899  * @param       p_manager       the user event manager.
900  * 
901  * @return      true if writting was successful.
902  */
903 bool jp2_write_ftyp(
904                                                 opj_jp2_t *jp2,
905                                                 opj_stream_private_t *cio,
906                                                 opj_event_mgr_t * p_manager
907                                   )
908 {
909         unsigned int i;
910         unsigned int l_ftyp_size = 16 + 4 * jp2->numcl;
911         unsigned char * l_ftyp_data, * l_current_data_ptr;
912         bool l_result;
913
914         // preconditions
915         assert(cio != 00);
916         assert(jp2 != 00);
917         assert(p_manager != 00);
918
919         l_ftyp_data = (unsigned char *) opj_malloc(l_ftyp_size);
920         
921         if
922                 (l_ftyp_data == 00)
923         {
924                 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to handle ftyp data\n");
925                 return false;
926         }
927         memset(l_ftyp_data,0,l_ftyp_size);
928
929         l_current_data_ptr = l_ftyp_data;
930
931         opj_write_bytes(l_current_data_ptr, l_ftyp_size,4); /* box size */
932         l_current_data_ptr += 4;
933
934         opj_write_bytes(l_current_data_ptr, JP2_FTYP,4); /* FTYP */
935         l_current_data_ptr += 4;
936
937         opj_write_bytes(l_current_data_ptr, jp2->brand,4); /* BR */
938         l_current_data_ptr += 4;
939
940         opj_write_bytes(l_current_data_ptr, jp2->minversion,4); /* MinV */
941         l_current_data_ptr += 4;
942
943         for 
944                 (i = 0; i < jp2->numcl; i++) 
945         {
946                 opj_write_bytes(l_current_data_ptr, jp2->cl[i],4);      /* CL */
947         }
948         
949         l_result = (opj_stream_write_data(cio,l_ftyp_data,l_ftyp_size,p_manager) == l_ftyp_size);
950         if
951                 (! l_result)
952         {
953                 opj_event_msg(p_manager, EVT_ERROR, "Error while writting ftyp data to stream\n");
954         }
955         opj_free(l_ftyp_data);
956         return l_result;
957 }
958
959 /**
960  * Writes the Jpeg2000 file Header box - JP2 Header box (warning, this is a super box).
961  *
962  * @param cio                   the stream to write data to.
963  * @param jp2                   the jpeg2000 file codec.
964  * @param p_manager             user event manager.
965  *
966  * @return true if writting was successful.
967 */
968 bool jp2_write_jp2h(
969                                                 opj_jp2_t *jp2,
970                                                 opj_stream_private_t *cio,
971                                                 opj_event_mgr_t * p_manager
972                                         )
973 {
974         opj_jp2_img_header_writer_handler_t l_writers [3];
975         opj_jp2_img_header_writer_handler_t * l_current_writer;
976
977         int i, l_nb_pass;
978         /* size of data for super box*/
979         int l_jp2h_size = 8;
980         bool l_result = true;
981
982         /* to store the data of the super box */
983         unsigned char l_jp2h_data [8];
984         
985         // preconditions
986         assert(cio != 00);
987         assert(jp2 != 00);
988         assert(p_manager != 00);
989
990         memset(l_writers,0,sizeof(l_writers));
991
992         if
993                 (jp2->bpc == 255)
994         {
995                 l_nb_pass = 3;
996                 l_writers[0].handler = jp2_write_ihdr;
997                 l_writers[1].handler = jp2_write_bpcc;
998                 l_writers[2].handler = jp2_write_colr;
999         }
1000         else
1001         {
1002                 l_nb_pass = 2;
1003                 l_writers[0].handler = jp2_write_ihdr;
1004                 l_writers[1].handler = jp2_write_colr;
1005         }
1006         
1007         /* write box header */
1008         /* write JP2H type */
1009         opj_write_bytes(l_jp2h_data+4,JP2_JP2H,4);
1010
1011         l_current_writer = l_writers;
1012         for
1013                 (i=0;i<l_nb_pass;++i)
1014         {
1015                 l_current_writer->m_data = l_current_writer->handler(jp2,&(l_current_writer->m_size));
1016                 if
1017                         (l_current_writer->m_data == 00)
1018                 {
1019                         opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to hold JP2 Header data\n");
1020                         l_result = false;
1021                         break;
1022                 }
1023                 l_jp2h_size += l_current_writer->m_size;
1024                 ++l_current_writer;
1025         }
1026
1027         if
1028                 (! l_result)
1029         {
1030                 l_current_writer = l_writers;
1031                 for
1032                         (i=0;i<l_nb_pass;++i)
1033                 {
1034                         if
1035                                 (l_current_writer->m_data != 00)
1036                         {
1037                                 opj_free(l_current_writer->m_data );
1038                         }
1039                         ++l_current_writer;
1040                 }
1041                 return false;
1042         }
1043
1044         /* write super box size */
1045         opj_write_bytes(l_jp2h_data,l_jp2h_size,4);
1046         
1047         /* write super box data on stream */
1048         if
1049                 (opj_stream_write_data(cio,l_jp2h_data,8,p_manager) != 8)
1050         {
1051                 opj_event_msg(p_manager, EVT_ERROR, "Stream error while writting JP2 Header box\n");
1052                 l_result = false;
1053         }
1054         
1055         if
1056                 (l_result)
1057         {
1058                 l_current_writer = l_writers;
1059                 for
1060                         (i=0;i<l_nb_pass;++i)
1061                 {
1062                         if
1063                                 (opj_stream_write_data(cio,l_current_writer->m_data,l_current_writer->m_size,p_manager) != l_current_writer->m_size)
1064                         {
1065                                 opj_event_msg(p_manager, EVT_ERROR, "Stream error while writting JP2 Header box\n");
1066                                 l_result = false;
1067                                 break;
1068                         }
1069                         ++l_current_writer;
1070                 }
1071         }
1072         l_current_writer = l_writers;
1073         /* cleanup */
1074         for
1075                 (i=0;i<l_nb_pass;++i)
1076         {
1077                 if
1078                         (l_current_writer->m_data != 00)
1079                 {
1080                         opj_free(l_current_writer->m_data );
1081                 }
1082                 ++l_current_writer;
1083         }
1084         return l_result;
1085 }
1086
1087 /**
1088  * Reads the Jpeg2000 file Header box - JP2 Header box (warning, this is a super box).
1089  *
1090  * @param       p_header_data   the data contained in the file header box.
1091  * @param       jp2                             the jpeg2000 file codec.
1092  * @param       p_header_size   the size of the data contained in the file header box.
1093  * @param       p_manager               the user event manager.
1094  *
1095  * @return true if the JP2 Header box was successfully reconized.
1096 */
1097 bool jp2_read_jp2h(
1098                                                 opj_jp2_t *jp2, 
1099                                                 unsigned char * p_header_data, 
1100                                                 unsigned int p_header_size,
1101                                                 opj_event_mgr_t * p_manager
1102                                         )
1103 {
1104         unsigned int l_box_size=0, l_current_data_size = 0;
1105         opj_jp2_box_t box;
1106         const opj_jp2_header_handler_t * l_current_handler;
1107
1108         // preconditions
1109         assert(p_header_data != 00);
1110         assert(jp2 != 00);
1111         assert(p_manager != 00);
1112
1113         /* make sure the box is well placed */
1114         if
1115                 ((jp2->jp2_state & JP2_STATE_FILE_TYPE) != JP2_STATE_FILE_TYPE )
1116         {
1117                 opj_event_msg(p_manager, EVT_ERROR, "The  box must be the first box in the file.\n");
1118                 return false;
1119         }
1120         jp2->jp2_img_state = JP2_IMG_STATE_NONE;
1121
1122         /* iterate while remaining data */
1123         while
1124                 (p_header_size > 0)
1125         {
1126                 if
1127                         (! jp2_read_boxhdr_char(&box,p_header_data,&l_box_size,p_header_size, p_manager))
1128                 {
1129                         opj_event_msg(p_manager, EVT_ERROR, "Stream error while reading JP2 Header box\n");
1130                         return false;
1131                 }
1132                 if
1133                         (box.length > p_header_size)
1134                 {
1135                         opj_event_msg(p_manager, EVT_ERROR, "Stream error while reading JP2 Header box\n");
1136                         return false;
1137                 }
1138                 l_current_handler = jp2_img_find_handler(box.type);
1139                 
1140                 l_current_data_size = box.length - l_box_size;
1141                 p_header_data += l_box_size;
1142                 
1143                 if
1144                         (l_current_handler != 00)
1145                 {
1146                         if
1147                                 (! l_current_handler->handler(jp2,p_header_data,l_current_data_size,p_manager))
1148                         {
1149                                 return false;
1150                         }
1151                 }
1152                 else
1153                 {
1154                         jp2->jp2_img_state |= JP2_IMG_STATE_UNKNOWN;
1155                 }
1156                 p_header_data += l_current_data_size;
1157                 p_header_size -= box.length;
1158         }
1159         jp2->jp2_state |= JP2_STATE_HEADER;
1160         return true;
1161 }
1162
1163 /**
1164  * Reads a IHDR box - Image Header box
1165  * 
1166  * @param       p_image_header_data                     pointer to actual data (already read from file)
1167  * @param       jp2                                                     the jpeg2000 file codec.
1168  * @param       p_image_header_size                     the size of the image header
1169  * @param       p_image_header_max_size         maximum size of the header, any size bigger than this value should result the function to output false.
1170  * @param       p_manager                                       the user event manager.
1171  * 
1172  * @return      true if the image header is valid, fale else.
1173  */
1174 bool jp2_read_ihdr(
1175                                                         opj_jp2_t *jp2, 
1176                                                         unsigned char * p_image_header_data, 
1177                                                         unsigned int p_image_header_size, 
1178                                                         opj_event_mgr_t * p_manager
1179                                                   )
1180 {
1181         // preconditions
1182         assert(p_image_header_data != 00);
1183         assert(jp2 != 00);
1184         assert(p_manager != 00);
1185
1186         if
1187                 (p_image_header_size != 14)
1188         {
1189                 opj_event_msg(p_manager, EVT_ERROR, "Bad image header box (bad size)\n");
1190                 return false;
1191         }
1192         opj_read_bytes(p_image_header_data,&(jp2->h),4);                        /* HEIGHT */
1193         p_image_header_data += 4;
1194         opj_read_bytes(p_image_header_data,&(jp2->w),4);                        /* WIDTH */
1195         p_image_header_data += 4;
1196         opj_read_bytes(p_image_header_data,&(jp2->numcomps),2);                 /* NC */
1197         p_image_header_data += 2;
1198         
1199         /* allocate memory for components */
1200         jp2->comps = (opj_jp2_comps_t*) opj_malloc(jp2->numcomps * sizeof(opj_jp2_comps_t));
1201         if
1202                 (jp2->comps == 0)
1203         {
1204                 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to handle image header (ihdr)\n");
1205                 return false;
1206         }
1207         memset(jp2->comps,0,jp2->numcomps * sizeof(opj_jp2_comps_t));
1208
1209         opj_read_bytes(p_image_header_data,&(jp2->bpc),1);                      /* BPC */
1210         ++ p_image_header_data;
1211         opj_read_bytes(p_image_header_data,&(jp2->C),1);                        /* C */
1212         ++ p_image_header_data;
1213         opj_read_bytes(p_image_header_data,&(jp2->UnkC),1);                     /* UnkC */
1214         ++ p_image_header_data;
1215         opj_read_bytes(p_image_header_data,&(jp2->IPR),1);                      /* IPR */
1216         ++ p_image_header_data;
1217         return true;
1218 }
1219
1220 /**
1221  * Writes the Image Header box - Image Header box.
1222  *
1223  * @param jp2                                   jpeg2000 file codec.
1224  * @param p_nb_bytes_written    pointer to store the nb of bytes written by the function.
1225  * 
1226  * @return      the data being copied.
1227 */
1228 static unsigned char * jp2_write_ihdr(
1229                                                                 opj_jp2_t *jp2, 
1230                                                                 unsigned int * p_nb_bytes_written
1231                                                          )
1232 {
1233         unsigned char * l_ihdr_data,* l_current_ihdr_ptr;
1234         
1235         // preconditions
1236         assert(jp2 != 00);
1237         assert(p_nb_bytes_written != 00);
1238
1239         /* default image header is 22 bytes wide */
1240         l_ihdr_data = (unsigned char *) opj_malloc(22);
1241         if
1242                 (l_ihdr_data == 00)
1243         {
1244                 return 00;
1245         }
1246         memset(l_ihdr_data,0,22);
1247
1248         l_current_ihdr_ptr = l_ihdr_data;
1249         
1250         opj_write_bytes(l_current_ihdr_ptr,22,4);                               /* write box size */
1251         l_current_ihdr_ptr+=4;
1252         opj_write_bytes(l_current_ihdr_ptr,JP2_IHDR, 4);                /* IHDR */
1253         l_current_ihdr_ptr+=4;
1254         opj_write_bytes(l_current_ihdr_ptr,jp2->h, 4);          /* HEIGHT */
1255         l_current_ihdr_ptr+=4;
1256         opj_write_bytes(l_current_ihdr_ptr, jp2->w, 4);         /* WIDTH */
1257         l_current_ihdr_ptr+=4;
1258         opj_write_bytes(l_current_ihdr_ptr, jp2->numcomps, 2);          /* NC */
1259         l_current_ihdr_ptr+=2;
1260         opj_write_bytes(l_current_ihdr_ptr, jp2->bpc, 1);               /* BPC */
1261         ++l_current_ihdr_ptr;
1262         opj_write_bytes(l_current_ihdr_ptr, jp2->C, 1);         /* C : Always 7 */
1263         ++l_current_ihdr_ptr;
1264         opj_write_bytes(l_current_ihdr_ptr, jp2->UnkC, 1);              /* UnkC, colorspace unknown */
1265         ++l_current_ihdr_ptr;
1266         opj_write_bytes(l_current_ihdr_ptr, jp2->IPR, 1);               /* IPR, no intellectual property */
1267         ++l_current_ihdr_ptr;
1268         *p_nb_bytes_written = 22;
1269         return l_ihdr_data;
1270 }
1271
1272 /**
1273  * Writes the Bit per Component box.
1274  *
1275  * @param       jp2                                             jpeg2000 file codec.
1276  * @param       p_nb_bytes_written              pointer to store the nb of bytes written by the function.
1277  * 
1278  * @return      the data being copied.
1279 */
1280 unsigned char * jp2_write_bpcc(
1281                                                                 opj_jp2_t *jp2, 
1282                                                                 unsigned int * p_nb_bytes_written
1283                                                          )
1284 {
1285         unsigned int i;
1286         /* room for 8 bytes for box and 1 byte for each component */
1287         int l_bpcc_size = 8 + jp2->numcomps;
1288         unsigned char * l_bpcc_data,* l_current_bpcc_ptr;
1289         
1290         // preconditions
1291         assert(jp2 != 00);
1292         assert(p_nb_bytes_written != 00);
1293
1294         l_bpcc_data = (unsigned char *) opj_malloc(l_bpcc_size);
1295         if
1296                 (l_bpcc_data == 00)
1297         {
1298                 return 00;
1299         }
1300         memset(l_bpcc_data,0,l_bpcc_size);
1301
1302         l_current_bpcc_ptr = l_bpcc_data;
1303
1304         opj_write_bytes(l_current_bpcc_ptr,l_bpcc_size,4);                              /* write box size */
1305         l_current_bpcc_ptr += 4;
1306         opj_write_bytes(l_current_bpcc_ptr,JP2_BPCC,4);                                 /* BPCC */
1307         l_current_bpcc_ptr += 4;
1308
1309         for 
1310                 (i = 0; i < jp2->numcomps; ++i) 
1311         {
1312                 opj_write_bytes(l_current_bpcc_ptr, jp2->comps[i].bpcc, 1); /* write each component information */
1313                 ++l_current_bpcc_ptr;
1314         }
1315         *p_nb_bytes_written = l_bpcc_size;
1316         return l_bpcc_data;
1317 }
1318
1319 /**
1320  * Reads a Bit per Component box.
1321  * 
1322  * @param       p_bpc_header_data                       pointer to actual data (already read from file)
1323  * @param       jp2                                                     the jpeg2000 file codec.
1324  * @param       p_bpc_header_size                       pointer that will hold the size of the bpc header
1325  * @param       p_bpc_header_max_size           maximum size of the header, any size bigger than this value should result the function to output false.
1326  * @param       p_manager                                       the user event manager.
1327  * 
1328  * @return      true if the bpc header is valid, fale else.
1329  */
1330 bool jp2_read_bpcc(
1331                                                         opj_jp2_t *jp2,
1332                                                         unsigned char * p_bpc_header_data, 
1333                                                         unsigned int p_bpc_header_size, 
1334                                                         opj_event_mgr_t * p_manager
1335                                                   )
1336 {
1337         unsigned int i;
1338
1339         // preconditions
1340         assert(p_bpc_header_data != 00);
1341         assert(jp2 != 00);
1342         assert(p_manager != 00);
1343
1344         // and length is relevant
1345         if
1346                 (p_bpc_header_size != jp2->numcomps)
1347         {
1348                 opj_event_msg(p_manager, EVT_ERROR, "Bad BPCC header box (bad size)\n");
1349                 return false;
1350         }
1351
1352         // read info for each component
1353         for 
1354                 (i = 0; i < jp2->numcomps; ++i) 
1355         {
1356                 opj_read_bytes(p_bpc_header_data,&jp2->comps[i].bpcc ,1);       /* read each BPCC component */
1357                 ++p_bpc_header_data;
1358         }
1359         return true;
1360 }
1361
1362 /**
1363  * Writes the Colour Specification box.
1364  *
1365  * @param jp2                                   jpeg2000 file codec.
1366  * @param p_nb_bytes_written    pointer to store the nb of bytes written by the function.
1367  * 
1368  * @return      the data being copied.
1369 */
1370 unsigned char *jp2_write_colr(
1371                                                                 opj_jp2_t *jp2, 
1372                                                                 unsigned int * p_nb_bytes_written
1373                                                         )
1374 {
1375         /* room for 8 bytes for box 3 for common data and variable upon profile*/
1376         unsigned int l_colr_size = 11;
1377         unsigned char * l_colr_data,* l_current_colr_ptr;
1378         
1379         // preconditions
1380         assert(jp2 != 00);
1381         assert(p_nb_bytes_written != 00);
1382
1383         switch
1384                 (jp2->meth)
1385         {
1386                 case 1 :
1387                         l_colr_size += 4;
1388                         break;
1389                 case 2 :
1390                         ++l_colr_size;
1391                         break;
1392                 default :
1393                         return 00;
1394         }
1395
1396         l_colr_data = (unsigned char *) opj_malloc(l_colr_size);
1397         if
1398                 (l_colr_data == 00)
1399         {
1400                 return 00;
1401         }
1402         memset(l_colr_data,0,l_colr_size);
1403         l_current_colr_ptr = l_colr_data;
1404
1405         opj_write_bytes(l_current_colr_ptr,l_colr_size,4);                              /* write box size */
1406         l_current_colr_ptr += 4;
1407         opj_write_bytes(l_current_colr_ptr,JP2_COLR,4);                                 /* BPCC */
1408         l_current_colr_ptr += 4;
1409         
1410         opj_write_bytes(l_current_colr_ptr, jp2->meth,1);                               /* METH */
1411         ++l_current_colr_ptr;
1412         opj_write_bytes(l_current_colr_ptr, jp2->precedence,1);                 /* PRECEDENCE */
1413         ++l_current_colr_ptr;
1414         opj_write_bytes(l_current_colr_ptr, jp2->approx,1);                             /* APPROX */
1415         ++l_current_colr_ptr;
1416         
1417         if 
1418                 (jp2->meth == 1) 
1419         {
1420                 opj_write_bytes(l_current_colr_ptr, jp2->enumcs,4);                     /* EnumCS */
1421         } 
1422         else 
1423         {
1424                 opj_write_bytes(l_current_colr_ptr, 0, 1);                                              /* PROFILE (??) */
1425         }
1426         *p_nb_bytes_written = l_colr_size;
1427         return l_colr_data;
1428 }
1429
1430 /**
1431  * Reads the Colour Specification box.
1432  *
1433  * @param       p_colr_header_data                      pointer to actual data (already read from file)
1434  * @param       jp2                                                     the jpeg2000 file codec.
1435  * @param       p_colr_header_size                      pointer that will hold the size of the color header
1436  * @param       p_colr_header_max_size          maximum size of the header, any size bigger than this value should result the function to output false.
1437  * @param       p_manager                                       the user event manager.
1438  * 
1439  * @return      true if the bpc header is valid, fale else.
1440 */
1441 bool jp2_read_colr(
1442                                                         opj_jp2_t * jp2,
1443                                                         unsigned char * p_colr_header_data, 
1444                                                         unsigned int p_colr_header_size, 
1445                                                         opj_event_mgr_t * p_manager
1446                                                   )
1447 {
1448         // preconditions
1449         assert(jp2 != 00);
1450         assert(p_colr_header_data != 00);
1451         assert(p_manager != 00);
1452
1453         if
1454                 (p_colr_header_size < 3)
1455         {
1456                 opj_event_msg(p_manager, EVT_ERROR, "Bad BPCC header box (bad size)\n");
1457                 return false;
1458         }
1459         
1460         opj_read_bytes(p_colr_header_data,&jp2->meth ,1);                       /* METH */
1461         ++p_colr_header_data;
1462
1463         opj_read_bytes(p_colr_header_data,&jp2->precedence ,1);                 /* PRECEDENCE */
1464         ++p_colr_header_data;
1465
1466         opj_read_bytes(p_colr_header_data,&jp2->approx ,1);                     /* APPROX */
1467         ++p_colr_header_data;
1468         
1469
1470         if 
1471                 (jp2->meth == 1) 
1472         {
1473                 if
1474                         (p_colr_header_size != 7)
1475                 {
1476                         opj_event_msg(p_manager, EVT_ERROR, "Bad BPCC header box (bad size)\n");
1477                         return false;
1478                 }
1479                 opj_read_bytes(p_colr_header_data,&jp2->enumcs ,4);                     /* EnumCS */
1480         }
1481         /*else
1482         {
1483                 // do not care with profiles.
1484         }*/
1485         return true;
1486 }
1487
1488 /**
1489  * Writes the Jpeg2000 codestream Header box - JP2C Header box.
1490  *
1491  * @param       cio                     the stream to write data to.
1492  * @param       jp2                     the jpeg2000 file codec.
1493  * @param       p_manager       user event manager.
1494  *
1495  * @return true if writting was successful.
1496 */
1497 bool jp2_write_jp2c(
1498                                    opj_jp2_t *jp2,
1499                                    opj_stream_private_t *cio,
1500                                    opj_event_mgr_t * p_manager
1501                                    ) 
1502 {
1503         unsigned int j2k_codestream_exit;
1504         unsigned char l_data_header [8];
1505         
1506         // preconditions
1507         assert(jp2 != 00);
1508         assert(cio != 00);
1509         assert(p_manager != 00);
1510         assert(opj_stream_has_seek(cio));
1511         
1512         j2k_codestream_exit = opj_stream_tell(cio);
1513         opj_write_bytes(l_data_header,j2k_codestream_exit - jp2->j2k_codestream_offset,4); /* size of codestream */
1514         opj_write_bytes(l_data_header + 4,JP2_JP2C,4);                                                                     /* JP2C */
1515
1516         if
1517                 (! opj_stream_seek(cio,jp2->j2k_codestream_offset,p_manager))
1518         {
1519                 opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
1520                 return false;
1521         }
1522         
1523         if
1524                 (opj_stream_write_data(cio,l_data_header,8,p_manager) != 8)
1525         {
1526                 opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
1527                 return false;
1528         }
1529
1530         if
1531                 (! opj_stream_seek(cio,j2k_codestream_exit,p_manager))
1532         {
1533                 opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
1534                 return false;
1535         }
1536         return true;
1537 }
1538
1539 /**
1540  * Destroys a jpeg2000 file decompressor.
1541  * 
1542  * @param       jp2             a jpeg2000 file decompressor.
1543  */
1544 void jp2_destroy(opj_jp2_t *jp2) 
1545 {
1546         if
1547                 (jp2) 
1548         {
1549                 /* destroy the J2K codec */
1550                 j2k_destroy(jp2->j2k);
1551                 jp2->j2k = 00;
1552                 if
1553                         (jp2->comps) 
1554                 {
1555                         opj_free(jp2->comps);
1556                         jp2->comps = 00;
1557                 }
1558                 if
1559                         (jp2->cl) 
1560                 {
1561                         opj_free(jp2->cl);
1562                         jp2->cl = 00;
1563                 }
1564                 if
1565                         (jp2->m_validation_list)
1566                 {
1567                         opj_procedure_list_destroy(jp2->m_validation_list);
1568                         jp2->m_validation_list = 00;
1569                 }
1570                 if
1571                         (jp2->m_procedure_list)
1572                 {
1573                         opj_procedure_list_destroy(jp2->m_procedure_list);
1574                         jp2->m_procedure_list = 00;
1575                 }
1576                 opj_free(jp2);
1577         }
1578 }
1579
1580
1581
1582
1583
1584 /* ----------------------------------------------------------------------- */
1585 /* JP2 encoder interface                                             */
1586 /* ----------------------------------------------------------------------- */
1587
1588 opj_jp2_t* jp2_create(bool p_is_decoder) 
1589 {
1590         opj_jp2_t *jp2 = (opj_jp2_t*)opj_malloc(sizeof(opj_jp2_t));
1591         if
1592                 (jp2) 
1593         {
1594                 memset(jp2,0,sizeof(opj_jp2_t));
1595                 /* create the J2K codec */
1596                 if
1597                         (! p_is_decoder)
1598                 {
1599                         jp2->j2k = j2k_create_compress();
1600                 }
1601                 else
1602                 {
1603                         jp2->j2k = j2k_create_decompress();
1604                 }
1605                 if
1606                         (jp2->j2k == 00) 
1607                 {
1608                         jp2_destroy(jp2);
1609                         return 00;
1610                 }
1611                 // validation list creation
1612                 jp2->m_validation_list = opj_procedure_list_create();
1613                 if
1614                         (! jp2->m_validation_list)
1615                 {
1616                         jp2_destroy(jp2);
1617                         return 00;
1618                 }
1619
1620                 // execution list creation
1621                 jp2->m_procedure_list = opj_procedure_list_create();
1622                 if
1623                         (! jp2->m_procedure_list)
1624                 {
1625                         jp2_destroy(jp2);
1626                         return 00;
1627                 }
1628         }
1629         return jp2;
1630 }
1631
1632 /**
1633  * Excutes the given procedures on the given codec.
1634  *
1635  * @param       p_procedure_list        the list of procedures to execute
1636  * @param       jp2                                     the jpeg2000 file codec to execute the procedures on.
1637  * @param       cio                                     the stream to execute the procedures on.
1638  * @param       p_manager                       the user manager.
1639  * 
1640  * @return      true                            if all the procedures were successfully executed.
1641  */
1642 bool jp2_exec (
1643                                         opj_jp2_t * jp2,
1644                                         opj_procedure_list_t * p_procedure_list,
1645                                         opj_stream_private_t *cio,
1646                                         opj_event_mgr_t * p_manager
1647                                   )
1648 {
1649         bool (** l_procedure) (opj_jp2_t * jp2,opj_stream_private_t *,opj_event_mgr_t *) = 00;
1650         bool l_result = true;
1651         unsigned int l_nb_proc, i;
1652
1653         // preconditions
1654         assert(p_procedure_list != 00);
1655         assert(jp2 != 00);
1656         assert(cio != 00);
1657         assert(p_manager != 00);
1658
1659         l_nb_proc = opj_procedure_list_get_nb_procedures(p_procedure_list);
1660         l_procedure = (bool (**) (opj_jp2_t * jp2,opj_stream_private_t *,opj_event_mgr_t *)) opj_procedure_list_get_first_procedure(p_procedure_list);
1661         for
1662                 (i=0;i<l_nb_proc;++i)
1663         {
1664                 l_result = l_result && (*l_procedure) (jp2,cio,p_manager);
1665                 ++l_procedure;
1666         }
1667         // and clear the procedure list at the end.
1668         opj_procedure_list_clear(p_procedure_list);
1669         return l_result;
1670 }
1671
1672
1673 /**
1674  * Starts a compression scheme, i.e. validates the codec parameters, writes the header.
1675  *
1676  * @param       jp2             the jpeg2000 file codec.
1677  * @param       cio             the stream object.
1678  *
1679  * @return true if the codec is valid.
1680  */
1681 bool jp2_start_compress(opj_jp2_t *jp2,  struct opj_stream_private *cio,opj_image_t * p_image, struct opj_event_mgr * p_manager)
1682 {
1683         // preconditions
1684         assert(jp2 != 00);
1685         assert(cio != 00);
1686         assert(p_manager != 00);
1687
1688         /* customization of the validation */
1689         jp2_setup_encoding_validation (jp2);
1690         
1691         /* validation of the parameters codec */
1692         if
1693                 (! jp2_exec(jp2,jp2->m_validation_list,cio,p_manager))
1694         {
1695                 return false;
1696         }
1697
1698         /* customization of the encoding */
1699         jp2_setup_header_writting(jp2);
1700
1701         /* write header */
1702         if
1703                 (! jp2_exec (jp2,jp2->m_procedure_list,cio,p_manager))
1704         {
1705                 return false;
1706         }
1707         return j2k_start_compress(jp2->j2k,cio,p_image,p_manager);
1708 }
1709
1710 /**
1711  * Reads a jpeg2000 file header structure.
1712  *
1713  * @param cio the stream to read data from.
1714  * @param jp2 the jpeg2000 file header structure.
1715  * @param p_manager the user event manager.
1716  *
1717  * @return true if the box is valid.
1718  */
1719 bool jp2_read_header(
1720                                                                 opj_jp2_t *jp2,
1721                                                                 opj_image_t ** p_image,
1722                                                                 OPJ_INT32 * p_tile_x0,
1723                                                                 OPJ_INT32 * p_tile_y0,
1724                                                                 OPJ_UINT32 * p_tile_width,
1725                                                                 OPJ_UINT32 * p_tile_height,
1726                                                                 OPJ_UINT32 * p_nb_tiles_x,
1727                                                                 OPJ_UINT32 * p_nb_tiles_y,
1728                                                                 struct opj_stream_private *cio,
1729                                                                 struct opj_event_mgr * p_manager
1730                                                         )
1731 {
1732         // preconditions
1733         assert(jp2 != 00);
1734         assert(cio != 00);
1735         assert(p_manager != 00);
1736
1737         /* customization of the validation */
1738         jp2_setup_decoding_validation (jp2);
1739         
1740         /* customization of the encoding */
1741         jp2_setup_header_reading(jp2);
1742
1743         /* validation of the parameters codec */
1744         if
1745                 (! jp2_exec(jp2,jp2->m_validation_list,cio,p_manager))
1746         {
1747                 return false;
1748         }
1749
1750         /* read header */
1751         if
1752                 (! jp2_exec (jp2,jp2->m_procedure_list,cio,p_manager))
1753         {
1754                 return false;
1755         }
1756         return j2k_read_header(
1757                 jp2->j2k,
1758                 p_image,
1759                 p_tile_x0,
1760                 p_tile_y0,
1761                 p_tile_width,
1762                 p_tile_height,
1763                 p_nb_tiles_x,
1764                 p_nb_tiles_y,
1765                 cio,
1766                 p_manager);
1767 }
1768
1769 /**
1770  * Ends the decompression procedures and possibiliy add data to be read after the 
1771  * codestream.
1772  */
1773 bool jp2_end_decompress(opj_jp2_t *jp2, opj_stream_private_t *cio, opj_event_mgr_t * p_manager)
1774 {
1775         // preconditions
1776         assert(jp2 != 00);
1777         assert(cio != 00);
1778         assert(p_manager != 00);
1779
1780         /* customization of the end encoding */
1781         jp2_setup_end_header_reading(jp2);
1782
1783         /* write header */
1784         if
1785                 (! jp2_exec (jp2,jp2->m_procedure_list,cio,p_manager))
1786         {
1787                 return false;
1788         }
1789         return j2k_end_decompress(jp2->j2k, cio, p_manager);
1790 }
1791
1792
1793 /**
1794  * Ends the compression procedures and possibiliy add data to be read after the 
1795  * codestream.
1796  */
1797 bool jp2_end_compress(opj_jp2_t *jp2, opj_stream_private_t *cio, opj_event_mgr_t * p_manager)
1798 {
1799         // preconditions
1800         assert(jp2 != 00);
1801         assert(cio != 00);
1802         assert(p_manager != 00);
1803
1804         /* customization of the end encoding */
1805         jp2_setup_end_header_writting(jp2);
1806
1807         if
1808                 (! j2k_end_compress(jp2->j2k,cio,p_manager))
1809         {
1810                 return false;
1811         }
1812         /* write header */
1813         return jp2_exec (jp2,jp2->m_procedure_list,cio,p_manager);
1814 }
1815
1816 /**
1817 Encode an image into a JPEG-2000 file stream
1818 @param jp2 JP2 compressor handle
1819 @param cio Output buffer stream
1820 @param image Image to encode
1821 @param cstr_info Codestream information structure if required, NULL otherwise
1822 @return Returns true if successful, returns false otherwise
1823 */
1824 bool jp2_encode(opj_jp2_t *jp2, struct opj_stream_private *cio, struct opj_event_mgr * p_manager)
1825 {
1826         return j2k_encode(jp2->j2k,cio,p_manager);
1827 }
1828 /**
1829  * Writes a tile.
1830  * @param       p_j2k           the jpeg2000 codec.
1831  * @param       p_stream                        the stream to write data to.
1832  * @param       p_manager       the user event manager.
1833  */
1834 bool jp2_write_tile (
1835                                          opj_jp2_t *p_jp2,
1836                                          OPJ_UINT32 p_tile_index,
1837                                          OPJ_BYTE * p_data,
1838                                          OPJ_UINT32 p_data_size,
1839                                          struct opj_stream_private *p_stream,
1840                                          struct opj_event_mgr * p_manager
1841                                         )
1842 {
1843         return j2k_write_tile (p_jp2->j2k,p_tile_index,p_data,p_data_size,p_stream,p_manager);
1844 }
1845
1846 /**
1847  * Decode tile data.
1848  * @param       p_j2k           the jpeg2000 codec.
1849  * @param       p_stream                        the stream to write data to.
1850  * @param       p_manager       the user event manager.
1851  */
1852 bool jp2_decode_tile (
1853                                         opj_jp2_t * p_jp2,
1854                                         OPJ_UINT32 p_tile_index,
1855                                         OPJ_BYTE * p_data,
1856                                         OPJ_UINT32 p_data_size,
1857                                         opj_stream_private_t *p_stream,
1858                                         opj_event_mgr_t * p_manager
1859                                         )
1860 {
1861         return j2k_decode_tile (p_jp2->j2k,p_tile_index,p_data,p_data_size,p_stream,p_manager);
1862 }
1863 /**
1864  * Reads a tile header.
1865  * @param       p_j2k           the jpeg2000 codec.
1866  * @param       p_stream                        the stream to write data to.
1867  * @param       p_manager       the user event manager.
1868  */
1869 bool jp2_read_tile_header (
1870                                          opj_jp2_t * p_jp2,
1871                                          OPJ_UINT32 * p_tile_index,
1872                                          OPJ_UINT32 * p_data_size,
1873                                          OPJ_INT32 * p_tile_x0,
1874                                          OPJ_INT32 * p_tile_y0,
1875                                          OPJ_INT32 * p_tile_x1,
1876                                          OPJ_INT32 * p_tile_y1,
1877                                          OPJ_UINT32 * p_nb_comps,
1878                                          bool * p_go_on,
1879                                          opj_stream_private_t *p_stream,
1880                                          opj_event_mgr_t * p_manager
1881                                         )
1882 {
1883         return j2k_read_tile_header (p_jp2->j2k,
1884                                                                 p_tile_index,
1885                                                                 p_data_size,
1886                                                                 p_tile_x0,
1887                                                                 p_tile_y0,
1888                                                                 p_tile_x1,
1889                                                                 p_tile_y1,
1890                                                                 p_nb_comps,
1891                                                                 p_go_on,
1892                                                                 p_stream,
1893                                                                 p_manager);
1894 }
1895
1896 /**
1897  * Sets up the procedures to do on writting header after the codestream. 
1898  * Developpers wanting to extend the library can add their own writting procedures.
1899  */
1900 void jp2_setup_end_header_writting (opj_jp2_t *jp2)
1901 {
1902         // preconditions
1903         assert(jp2 != 00);
1904
1905         opj_procedure_list_add_procedure(jp2->m_procedure_list,jp2_write_jp2c );
1906         /* DEVELOPER CORNER, add your custom procedures */
1907 }
1908
1909 /**
1910  * Sets up the procedures to do on reading header. 
1911  * Developpers wanting to extend the library can add their own writting procedures.
1912  */
1913 void jp2_setup_header_reading (opj_jp2_t *jp2)
1914 {
1915         // preconditions
1916         assert(jp2 != 00);
1917
1918         opj_procedure_list_add_procedure(jp2->m_procedure_list,jp2_read_header_procedure );
1919         /* DEVELOPER CORNER, add your custom procedures */
1920 }
1921
1922 /**
1923  * Sets up the procedures to do on reading header after the codestream. 
1924  * Developpers wanting to extend the library can add their own writting procedures.
1925  */
1926 void jp2_setup_end_header_reading (opj_jp2_t *jp2)
1927 {
1928         // preconditions
1929         assert(jp2 != 00);
1930         opj_procedure_list_add_procedure(jp2->m_procedure_list,jp2_read_header_procedure );
1931         /* DEVELOPER CORNER, add your custom procedures */
1932 }
1933
1934
1935 /** 
1936  * The default validation procedure without any extension.
1937  * 
1938  * @param       jp2                             the jpeg2000 codec to validate.
1939  * @param       cio                             the input stream to validate.
1940  * @param       p_manager               the user event manager.
1941  *
1942  * @return true if the parameters are correct.
1943  */
1944 bool jp2_default_validation (
1945                                                                 opj_jp2_t * jp2,
1946                                                                 opj_stream_private_t *cio,
1947                                                                 opj_event_mgr_t * p_manager
1948                                                         )
1949 {
1950         bool l_is_valid = true;
1951         unsigned int i;
1952         
1953         // preconditions
1954         assert(jp2 != 00);
1955         assert(cio != 00);
1956         assert(p_manager != 00);
1957         /* JPEG2000 codec validation */
1958         /*TODO*/
1959
1960         /* STATE checking */
1961         /* make sure the state is at 0 */
1962         l_is_valid &= (jp2->jp2_state == JP2_STATE_NONE);
1963         /* make sure not reading a jp2h ???? WEIRD */
1964         l_is_valid &= (jp2->jp2_img_state == JP2_IMG_STATE_NONE);
1965         
1966         /* POINTER validation */
1967         /* make sure a j2k codec is present */
1968         l_is_valid &= (jp2->j2k != 00);
1969         /* make sure a procedure list is present */
1970         l_is_valid &= (jp2->m_procedure_list != 00);
1971         /* make sure a validation list is present */
1972         l_is_valid &= (jp2->m_validation_list != 00);
1973
1974         /* PARAMETER VALIDATION */
1975         /* number of components */
1976         l_is_valid &= (jp2->numcl > 0);
1977         /* width */
1978         l_is_valid &= (jp2->h > 0);
1979         /* height */
1980         l_is_valid &= (jp2->w > 0);
1981         /* precision */
1982         for 
1983                 (i = 0; i < jp2->numcomps; ++i) 
1984         {
1985                 l_is_valid &= (jp2->comps[i].bpcc > 0);
1986         }
1987         /* METH */
1988         l_is_valid &= ((jp2->meth > 0) && (jp2->meth < 3));
1989         
1990
1991
1992         /* stream validation */
1993         /* back and forth is needed */
1994         l_is_valid &= opj_stream_has_seek(cio);
1995         
1996         return l_is_valid;
1997
1998 }
1999
2000 /**
2001  * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
2002  * are valid. Developpers wanting to extend the library can add their own validation procedures.
2003  */
2004 void jp2_setup_encoding_validation (opj_jp2_t *jp2)
2005 {
2006         // preconditions
2007         assert(jp2 != 00);
2008         opj_procedure_list_add_procedure(jp2->m_validation_list, jp2_default_validation);
2009         /* DEVELOPER CORNER, add your custom validation procedure */
2010 }
2011
2012 /**
2013  * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
2014  * are valid. Developpers wanting to extend the library can add their own validation procedures.
2015  */
2016 void jp2_setup_decoding_validation (opj_jp2_t *jp2)
2017 {
2018         // preconditions
2019         assert(jp2 != 00);
2020         /* DEVELOPER CORNER, add your custom validation procedure */
2021 }
2022
2023 /**
2024  * Sets up the procedures to do on writting header. Developpers wanting to extend the library can add their own writting procedures.
2025  */
2026 void jp2_setup_header_writting (opj_jp2_t *jp2)
2027 {
2028         // preconditions
2029         assert(jp2 != 00);
2030         opj_procedure_list_add_procedure(jp2->m_procedure_list,jp2_write_jp );
2031         opj_procedure_list_add_procedure(jp2->m_procedure_list,jp2_write_ftyp );
2032         opj_procedure_list_add_procedure(jp2->m_procedure_list,jp2_write_jp2h );
2033         opj_procedure_list_add_procedure(jp2->m_procedure_list,jp2_skip_jp2c );
2034         
2035         /* DEVELOPER CORNER, insert your custom procedures */
2036
2037 }
2038
2039
2040 /**
2041  * Skips the Jpeg2000 Codestream Header box - JP2C Header box.
2042  *
2043  * @param       cio                     the stream to write data to.
2044  * @param       jp2                     the jpeg2000 file codec.
2045  * @param       p_manager       user event manager.
2046  *
2047  * @return true if writting was successful.
2048 */
2049 bool jp2_skip_jp2c(
2050                                                 opj_jp2_t *jp2,
2051                                                 struct opj_stream_private *cio,
2052                                                 struct opj_event_mgr * p_manager
2053                                         )
2054 {
2055         // preconditions
2056         assert(jp2 != 00);
2057         assert(cio != 00);
2058         assert(p_manager != 00);
2059
2060         jp2->j2k_codestream_offset = opj_stream_tell(cio);
2061         if
2062                 (opj_stream_skip(cio,8,p_manager) != 8)
2063         {
2064                 return false;
2065         }
2066         return true;
2067 }
2068
2069 struct opj_image * jp2_decode(opj_jp2_t *jp2, struct opj_stream_private *cio, struct opj_event_mgr * p_manager) 
2070 {
2071         /* J2K decoding */
2072         struct opj_image * image = j2k_decode(jp2->j2k, cio, p_manager);
2073         if
2074                 (!image) 
2075         {
2076                 opj_event_msg(p_manager, EVT_ERROR, "Failed to decode J2K image\n");
2077                 return false;
2078         }
2079
2080         /* Set Image Color Space */
2081         if (jp2->enumcs == 16)
2082                 image->color_space = CLRSPC_SRGB;
2083         else if (jp2->enumcs == 17)
2084                 image->color_space = CLRSPC_GRAY;
2085         else if (jp2->enumcs == 18)
2086                 image->color_space = CLRSPC_SYCC;
2087         else
2088                 image->color_space = CLRSPC_UNKNOWN;
2089         return image;
2090 }
2091
2092 void jp2_setup_encoder(opj_jp2_t *jp2, opj_cparameters_t *parameters, opj_image_t *image,opj_event_mgr_t * p_manager) 
2093 {
2094         unsigned int i;
2095         int depth_0, sign;
2096
2097         if(!jp2 || !parameters || !image)
2098                 return;
2099
2100         /* setup the J2K codec */
2101         /* ------------------- */
2102
2103         /* Check if number of components respects standard */
2104         if (image->numcomps < 1 || image->numcomps > 16384) {
2105                 opj_event_msg(p_manager, EVT_ERROR, "Invalid number of components specified while setting up JP2 encoder\n");
2106                 return;
2107         }
2108
2109         j2k_setup_encoder(jp2->j2k, parameters, image,p_manager);
2110
2111         /* setup the JP2 codec */
2112         /* ------------------- */
2113         
2114         /* Profile box */
2115
2116         jp2->brand = JP2_JP2;   /* BR */
2117         jp2->minversion = 0;    /* MinV */
2118         jp2->numcl = 1;
2119         jp2->cl = (unsigned int*) opj_malloc(jp2->numcl * sizeof(unsigned int));
2120         jp2->cl[0] = JP2_JP2;   /* CL0 : JP2 */
2121
2122         /* Image Header box */
2123
2124         jp2->numcomps = image->numcomps;        /* NC */
2125         jp2->comps = (opj_jp2_comps_t*) opj_malloc(jp2->numcomps * sizeof(opj_jp2_comps_t));
2126         jp2->h = image->y1 - image->y0;         /* HEIGHT */
2127         jp2->w = image->x1 - image->x0;         /* WIDTH */
2128         /* BPC */
2129         depth_0 = image->comps[0].prec - 1;
2130         sign = image->comps[0].sgnd;
2131         jp2->bpc = depth_0 + (sign << 7);
2132         for (i = 1; i < image->numcomps; i++) {
2133                 int depth = image->comps[i].prec - 1;
2134                 sign = image->comps[i].sgnd;
2135                 if (depth_0 != depth)
2136                         jp2->bpc = 255;
2137         }
2138         jp2->C = 7;                     /* C : Always 7 */
2139         jp2->UnkC = 0;          /* UnkC, colorspace specified in colr box */
2140         jp2->IPR = 0;           /* IPR, no intellectual property */
2141         
2142         /* BitsPerComponent box */
2143
2144         for (i = 0; i < image->numcomps; i++) {
2145                 jp2->comps[i].bpcc = image->comps[i].prec - 1 + (image->comps[i].sgnd << 7);
2146         }
2147
2148         /* Colour Specification box */
2149
2150         if ((image->numcomps == 1 || image->numcomps == 3) && (jp2->bpc != 255)) {
2151                 jp2->meth = 1;  /* METH: Enumerated colourspace */
2152         } else {
2153                 jp2->meth = 2;  /* METH: Restricted ICC profile */
2154         }
2155         if (jp2->meth == 1) {
2156                 if (image->color_space == 1)
2157                         jp2->enumcs = 16;       /* sRGB as defined by IEC 61966�2�1 */
2158                 else if (image->color_space == 2)
2159                         jp2->enumcs = 17;       /* greyscale */
2160                 else if (image->color_space == 3)
2161                         jp2->enumcs = 18;       /* YUV */
2162         } else {
2163                 jp2->enumcs = 0;                /* PROFILE (??) */
2164         }
2165         jp2->precedence = 0;    /* PRECEDENCE */
2166         jp2->approx = 0;                /* APPROX */
2167
2168 }
2169
2170 void jp2_setup_decoder(opj_jp2_t *jp2, opj_dparameters_t *parameters) 
2171 {
2172         if(!jp2 || !parameters)
2173                 return;
2174
2175         /* setup the J2K codec */
2176         /* ------------------- */
2177         j2k_setup_decoder(jp2->j2k, parameters);
2178 }
2179 /**
2180  * Sets the given area to be decoded. This function should be called right after opj_read_header and before any tile header reading.
2181  *
2182  * @param       p_jp2                   the jpeg2000 codec.
2183  * @param       p_end_x                 the right position of the rectangle to decode (in image coordinates).
2184  * @param       p_start_y               the up position of the rectangle to decode (in image coordinates).
2185  * @param       p_end_y                 the bottom position of the rectangle to decode (in image coordinates).
2186  * @param       p_manager               the user event manager
2187  *
2188  * @return      true                    if the area could be set.
2189  */                             
2190 bool jp2_set_decode_area(
2191                         opj_jp2_t *p_jp2,
2192                         OPJ_INT32 p_start_x,
2193                         OPJ_INT32 p_start_y,
2194                         OPJ_INT32 p_end_x,
2195                         OPJ_INT32 p_end_y,
2196                         struct opj_event_mgr * p_manager
2197                         )
2198 {
2199         return j2k_set_decode_area(p_jp2->j2k,p_start_x,p_start_y,p_end_x,p_end_y,p_manager);
2200 }
2201
2202 #if 0
2203
2204
2205
2206
2207
2208 static void jp2_write_url(opj_cio_t *cio, char *Idx_file) {
2209         unsigned int i;
2210         opj_jp2_box_t box;
2211
2212         box.init_pos = cio_tell(cio);
2213         cio_skip(cio, 4);
2214         cio_write(cio, JP2_URL, 4);     /* DBTL */
2215         cio_write(cio, 0, 1);           /* VERS */
2216         cio_write(cio, 0, 3);           /* FLAG */
2217
2218         if(Idx_file) {
2219                 for (i = 0; i < strlen(Idx_file); i++) {
2220                         cio_write(cio, Idx_file[i], 1);
2221                 }
2222         }
2223
2224         box.length = cio_tell(cio) - box.init_pos;
2225         cio_seek(cio, box.init_pos);
2226         cio_write(cio, box.length, 4);  /* L */
2227         cio_seek(cio, box.init_pos + box.length);
2228 }
2229 #endif