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