2 * Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium
3 * Copyright (c) 2002-2007, Professor Benoit Macq
4 * Copyright (c) 2001-2003, David Janssens
5 * Copyright (c) 2002-2003, Yannick Verschueren
6 * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe
7 * Copyright (c) 2005, Herve Drolon, FreeImage Team
8 * Copyright (c) 2010-2011, Kaori Hagihara
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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.
32 #include "opj_includes.h"
34 /** @defgroup JP2 JP2 - JPEG-2000 file format reader/writer */
39 /** @name Local static functions */
44 @param cinfo Codec context info
45 @param cio Input stream
47 @return Returns true if successful, returns false otherwise
49 static opj_bool jp2_read_boxhdr(opj_common_ptr cinfo, opj_cio_t *cio, opj_jp2_box_t *box);
50 /*static void jp2_write_url(opj_cio_t *cio, char *Idx_file);*/
53 * Reads a IHDR box - Image Header box
55 * @param p_image_header_data pointer to actual data (already read from file)
56 * @param jp2 the jpeg2000 file codec.
57 * @param p_image_header_size the size of the image header
58 * @param p_manager the user event manager.
60 * @return true if the image header is valid, false else.
62 static opj_bool opj_jp2_read_ihdr( opj_jp2_v2_t *jp2,
63 OPJ_BYTE *p_image_header_data,
64 OPJ_UINT32 p_image_header_size,
65 opj_event_mgr_t * p_manager );
67 static void jp2_write_ihdr(opj_jp2_t *jp2, opj_cio_t *cio);
70 * Writes the Image Header box - Image Header box.
72 * @param jp2 jpeg2000 file codec.
73 * @param p_nb_bytes_written pointer to store the nb of bytes written by the function.
75 * @return the data being copied.
77 static unsigned char * jp2_write_ihdr_v2( opj_jp2_v2_t *jp2,
78 unsigned int * p_nb_bytes_written );
80 static void jp2_write_bpcc(opj_jp2_t *jp2, opj_cio_t *cio);
83 * Writes the Bit per Component box.
85 * @param jp2 jpeg2000 file codec.
86 * @param p_nb_bytes_written pointer to store the nb of bytes written by the function.
88 * @return the data being copied.
90 static unsigned char * jp2_write_bpcc_v2( opj_jp2_v2_t *jp2,
91 unsigned int * p_nb_bytes_written );
94 * Reads a Bit per Component box.
96 * @param p_bpc_header_data pointer to actual data (already read from file)
97 * @param jp2 the jpeg2000 file codec.
98 * @param p_bpc_header_size the size of the bpc header
99 * @param p_manager the user event manager.
101 * @return true if the bpc header is valid, fale else.
103 static opj_bool opj_jp2_read_bpcc( opj_jp2_v2_t *jp2,
104 OPJ_BYTE * p_bpc_header_data,
105 OPJ_UINT32 p_bpc_header_size,
106 opj_event_mgr_t * p_manager );
108 static opj_bool opj_jp2_read_cdef( opj_jp2_v2_t * jp2,
109 OPJ_BYTE * p_cdef_header_data,
110 OPJ_UINT32 p_cdef_header_size,
111 opj_event_mgr_t * p_manager );
113 static void jp2_write_colr(opj_jp2_t *jp2, opj_cio_t *cio);
115 * Writes the Colour Specification box.
117 * @param jp2 jpeg2000 file codec.
118 * @param p_nb_bytes_written pointer to store the nb of bytes written by the function.
120 * @return the data being copied.
122 static unsigned char *jp2_write_colr_v2(opj_jp2_v2_t *jp2,
123 unsigned int * p_nb_bytes_written );
126 Write the FTYP box - File type box
127 @param jp2 JP2 handle
128 @param cio Output buffer stream
130 static void jp2_write_ftyp(opj_jp2_t *jp2, opj_cio_t *cio);
134 * Writes a FTYP box - File type box
136 * @param cio the stream to write data to.
137 * @param jp2 the jpeg2000 file codec.
138 * @param p_manager the user event manager.
140 * @return true if writting was successful.
142 static opj_bool jp2_write_ftyp_v2( opj_jp2_v2_t *jp2,
143 struct opj_stream_private *cio,
144 struct opj_event_mgr * p_manager );
147 * Reads a a FTYP box - File type box
149 * @param p_header_data the data contained in the FTYP box.
150 * @param jp2 the jpeg2000 file codec.
151 * @param p_header_size the size of the data contained in the FTYP box.
152 * @param p_manager the user event manager.
154 * @return true if the FTYP box is valid.
156 static opj_bool opj_jp2_read_ftyp( opj_jp2_v2_t *jp2,
157 OPJ_BYTE * p_header_data,
158 OPJ_UINT32 p_header_size,
159 opj_event_mgr_t * p_manager );
162 * Skips the Jpeg2000 Codestream Header box - JP2C Header box.
164 * @param cio the stream to write data to.
165 * @param jp2 the jpeg2000 file codec.
166 * @param p_manager user event manager.
168 * @return true if writting was successful.
170 opj_bool jp2_skip_jp2c( opj_jp2_v2_t *jp2,
171 struct opj_stream_private *cio,
172 struct opj_event_mgr * p_manager );
175 * Reads the Jpeg2000 file Header box - JP2 Header box (warning, this is a super box).
177 * @param p_header_data the data contained in the file header box.
178 * @param jp2 the jpeg2000 file codec.
179 * @param p_header_size the size of the data contained in the file header box.
180 * @param p_manager the user event manager.
182 * @return true if the JP2 Header box was successfully reconized.
184 static opj_bool opj_jp2_read_jp2h( opj_jp2_v2_t *jp2,
185 OPJ_BYTE *p_header_data,
186 OPJ_UINT32 p_header_size,
187 opj_event_mgr_t * p_manager );
189 static int jp2_write_jp2c(opj_jp2_t *jp2, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info);
193 * Writes the Jpeg2000 codestream Header box - JP2C Header box. This function must be called AFTER the coding has been done.
195 * @param cio the stream to write data to.
196 * @param jp2 the jpeg2000 file codec.
197 * @param p_manager user event manager.
199 * @return true if writting was successful.
201 static opj_bool jp2_write_jp2c_v2( opj_jp2_v2_t *jp2,
202 struct opj_stream_private *cio,
203 struct opj_event_mgr * p_manager );
205 static void jp2_write_jp(opj_cio_t *cio);
208 * Reads a jpeg2000 file signature box.
210 * @param p_header_data the data contained in the signature box.
211 * @param jp2 the jpeg2000 file codec.
212 * @param p_header_size the size of the data contained in the signature box.
213 * @param p_manager the user event manager.
215 * @return true if the file signature box is valid.
217 static opj_bool opj_jp2_read_jp(opj_jp2_v2_t *jp2,
218 OPJ_BYTE * p_header_data,
219 OPJ_UINT32 p_header_size,
220 opj_event_mgr_t * p_manager);
223 Apply collected palette data
224 @param color Collector for profile, cdef and pclr data
227 static void jp2_apply_pclr(opj_image_t *image, opj_jp2_color_t *color);
230 @param jp2 JP2 handle
231 @param cio Input buffer stream
233 @param color Collector for profile, cdef and pclr data
234 @return Returns true if successful, returns false otherwise
236 static opj_bool opj_jp2_read_pclr( opj_jp2_v2_t *jp2,
237 OPJ_BYTE * p_pclr_header_data,
238 OPJ_UINT32 p_pclr_header_size,
239 opj_event_mgr_t * p_manager );
242 Collect component mapping data
243 @param jp2 JP2 handle
244 @param cio Input buffer stream
246 @param color Collector for profile, cdef and pclr data
247 @return Returns true if successful, returns false otherwise
250 static opj_bool opj_jp2_read_cmap( opj_jp2_v2_t * jp2,
251 OPJ_BYTE * p_cmap_header_data,
252 OPJ_UINT32 p_cmap_header_size,
253 opj_event_mgr_t * p_manager );
256 * Reads the Color Specification box.
258 * @param p_colr_header_data pointer to actual data (already read from file)
259 * @param jp2 the jpeg2000 file codec.
260 * @param p_colr_header_size the size of the color header
261 * @param p_manager the user event manager.
263 * @return true if the bpc header is valid, fale else.
265 static opj_bool opj_jp2_read_colr( opj_jp2_v2_t *jp2,
266 OPJ_BYTE * p_colr_header_data,
267 OPJ_UINT32 p_colr_header_size,
268 opj_event_mgr_t * p_manager );
271 Write file Index (superbox)
272 @param[in] offset_jp2c offset of jp2c box
273 @param[in] length_jp2c length of jp2c box
274 @param[in] offset_idx offset of cidx box
275 @param[in] length_idx length of cidx box
276 @param[in] cio file output handle
277 @return length of fidx box
279 static int write_fidx( int offset_jp2c, int length_jp2c, int offset_idx, int length_idx, opj_cio_t *cio);
281 Write index Finder box
282 @param[in] offset offset of fidx box
283 @param[in] length length of fidx box
284 @param[in] cio file output handle
286 static void write_iptr( int offset, int length, opj_cio_t *cio);
290 @param[in] offset_jp2c offset of jp2c box
291 @param[in] length_jp2c length of jp2c box
292 @param[in] offset_idx offset of cidx box
293 @param[in] length_idx length of cidx box
294 @param[in] cio file output handle
296 static void write_prxy( int offset_jp2c, int length_jp2c, int offset_idx, int length_idx, opj_cio_t *cio);
302 * Sets up the procedures to do on writting header after the codestream.
303 * Developpers wanting to extend the library can add their own writting procedures.
305 static void jp2_setup_end_header_writting (opj_jp2_v2_t *jp2);
309 * Sets up the procedures to do on reading header after the codestream.
310 * Developpers wanting to extend the library can add their own writting procedures.
312 static void jp2_setup_end_header_reading (opj_jp2_v2_t *jp2);
315 * Reads a jpeg2000 file header structure.
317 * @param cio the stream to read data from.
318 * @param jp2 the jpeg2000 file header structure.
319 * @param p_manager the user event manager.
321 * @return true if the box is valid.
323 opj_bool jp2_read_header_procedure(
325 struct opj_stream_private *cio,
326 struct opj_event_mgr * p_manager
330 * Excutes the given procedures on the given codec.
332 * @param p_procedure_list the list of procedures to execute
333 * @param jp2 the jpeg2000 file codec to execute the procedures on.
334 * @param cio the stream to execute the procedures on.
335 * @param p_manager the user manager.
337 * @return true if all the procedures were successfully executed.
339 static opj_bool jp2_exec (
341 struct opj_procedure_list * p_procedure_list,
342 struct opj_stream_private *cio,
343 struct opj_event_mgr * p_manager
347 * Reads a box header. The box is the way data is packed inside a jpeg2000 file structure.
349 * @param cio the input stream to read data from.
350 * @param box the box structure to fill.
351 * @param p_number_bytes_read pointer to an int that will store the number of bytes read from the stream (shoul usually be 2).
352 * @param p_manager user event manager.
354 * @return true if the box is reconized, false otherwise
356 static opj_bool jp2_read_boxhdr_v2(
358 OPJ_UINT32 * p_number_bytes_read,
359 struct opj_stream_private *cio,
360 struct opj_event_mgr * p_manager
364 * Finds the execution function related to the given box id.
366 * @param p_id the id of the handler to fetch.
368 * @return the given handler or NULL if it could not be found.
370 static const opj_jp2_header_handler_t * jp2_find_handler (int p_id );
373 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
374 * are valid. Developpers wanting to extend the library can add their own validation procedures.
376 static void jp2_setup_encoding_validation (opj_jp2_v2_t *jp2);
380 * Sets up the procedures to do on writting header. Developpers wanting to extend the library can add their own writting procedures.
382 static void jp2_setup_header_writting (opj_jp2_v2_t *jp2);
385 * The default validation procedure without any extension.
387 * @param jp2 the jpeg2000 codec to validate.
388 * @param cio the input stream to validate.
389 * @param p_manager the user event manager.
391 * @return true if the parameters are correct.
393 opj_bool jp2_default_validation ( opj_jp2_v2_t * jp2,
394 struct opj_stream_private *cio,
395 struct opj_event_mgr * p_manager );
398 * Finds the image execution function related to the given box id.
400 * @param p_id the id of the handler to fetch.
402 * @return the given handler or NULL if it could not be found.
404 static const opj_jp2_header_handler_t * jp2_img_find_handler (int p_id);
406 const opj_jp2_header_handler_t jp2_header [] =
408 {JP2_JP,opj_jp2_read_jp},
409 {JP2_FTYP,opj_jp2_read_ftyp},
410 {JP2_JP2H,opj_jp2_read_jp2h}
413 const opj_jp2_header_handler_t jp2_img_header [] =
415 {JP2_IHDR,opj_jp2_read_ihdr},
416 {JP2_COLR,opj_jp2_read_colr},
417 {JP2_BPCC,opj_jp2_read_bpcc},
418 {JP2_PCLR,opj_jp2_read_pclr},
419 {JP2_CMAP,opj_jp2_read_cmap},
420 {JP2_CDEF,opj_jp2_read_cdef}
425 * Reads a box header. The box is the way data is packed inside a jpeg2000 file structure. Data is read from a character string
427 * @param p_data the character string to read data from.
428 * @param box the box structure to fill.
429 * @param p_number_bytes_read pointer to an int that will store the number of bytes read from the stream (shoul usually be 2).
430 * @param p_box_max_size the maximum number of bytes in the box.
432 * @return true if the box is reconized, false otherwise
434 static opj_bool jp2_read_boxhdr_char(
437 OPJ_UINT32 * p_number_bytes_read,
438 OPJ_UINT32 p_box_max_size,
439 struct opj_event_mgr * p_manager
443 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
444 * are valid. Developpers wanting to extend the library can add their own validation procedures.
446 static void jp2_setup_decoding_validation (opj_jp2_v2_t *jp2);
449 * Sets up the procedures to do on reading header.
450 * Developpers wanting to extend the library can add their own writting procedures.
452 static void jp2_setup_header_reading (opj_jp2_v2_t *jp2);
456 /* ----------------------------------------------------------------------- */
458 static opj_bool jp2_read_boxhdr(opj_common_ptr cinfo, opj_cio_t *cio, opj_jp2_box_t *box) {
459 box->init_pos = cio_tell(cio);
460 box->length = cio_read(cio, 4);
461 box->type = cio_read(cio, 4);
462 if (box->length == 1) {
463 if (cio_read(cio, 4) != 0) {
464 opj_event_msg(cinfo, EVT_ERROR, "Cannot handle box sizes higher than 2^32\n");
467 box->length = cio_read(cio, 4);
468 if (box->length == 0)
469 box->length = cio_numbytesleft(cio) + 12;
471 else if (box->length == 0) {
472 box->length = cio_numbytesleft(cio) + 8;
479 * Reads a box header. The box is the way data is packed inside a jpeg2000 file structure.
481 * @param cio the input stream to read data from.
482 * @param box the box structure to fill.
483 * @param p_number_bytes_read pointer to an int that will store the number of bytes read from the stream (should usually be 8).
484 * @param p_manager user event manager.
486 * @return true if the box is reconized, false otherwise
488 opj_bool jp2_read_boxhdr_v2(opj_jp2_box_t *box, OPJ_UINT32 * p_number_bytes_read, opj_stream_private_t *cio, opj_event_mgr_t * p_manager)
490 /* read header from file */
491 unsigned char l_data_header [8];
496 assert(p_number_bytes_read != 00);
497 assert(p_manager != 00);
499 *p_number_bytes_read = opj_stream_read_data(cio,l_data_header,8,p_manager);
500 if (*p_number_bytes_read != 8) {
504 /* process read data */
505 opj_read_bytes(l_data_header,&(box->length), 4);
506 opj_read_bytes(l_data_header+4,&(box->type), 4);
508 /* do we have a "special very large box ?" */
509 /* read then the XLBox */
510 if (box->length == 1) {
511 OPJ_UINT32 l_xl_part_size;
513 OPJ_UINT32 l_nb_bytes_read = opj_stream_read_data(cio,l_data_header,8,p_manager);
514 if (l_nb_bytes_read != 8) {
515 if (l_nb_bytes_read > 0) {
516 *p_number_bytes_read += l_nb_bytes_read;
522 opj_read_bytes(l_data_header,&l_xl_part_size, 4);
523 if (l_xl_part_size != 0) {
524 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot handle box sizes higher than 2^32\n");
527 opj_read_bytes(l_data_header,&(box->length), 4);
533 static void jp2_write_url(opj_cio_t *cio, char *Idx_file) {
537 box.init_pos = cio_tell(cio);
539 cio_write(cio, JP2_URL, 4); /* DBTL */
540 cio_write(cio, 0, 1); /* VERS */
541 cio_write(cio, 0, 3); /* FLAG */
544 for (i = 0; i < strlen(Idx_file); i++) {
545 cio_write(cio, Idx_file[i], 1);
549 box.length = cio_tell(cio) - box.init_pos;
550 cio_seek(cio, box.init_pos);
551 cio_write(cio, box.length, 4); /* L */
552 cio_seek(cio, box.init_pos + box.length);
558 * Reads a IHDR box - Image Header box
560 * @param jp2 the jpeg2000 file codec.
561 * @param p_image_header_data pointer to actual data (already read from file)
562 * @param p_image_header_size the size of the image header
563 * @param p_manager the user event manager.
565 * @return true if the image header is valid, fale else.
567 opj_bool opj_jp2_read_ihdr( opj_jp2_v2_t *jp2,
568 OPJ_BYTE *p_image_header_data,
569 OPJ_UINT32 p_image_header_size,
570 opj_event_mgr_t * p_manager )
573 assert(p_image_header_data != 00);
575 assert(p_manager != 00);
577 if (p_image_header_size != 14) {
578 opj_event_msg_v2(p_manager, EVT_ERROR, "Bad image header box (bad size)\n");
582 opj_read_bytes(p_image_header_data,&(jp2->h),4); /* HEIGHT */
583 p_image_header_data += 4;
584 opj_read_bytes(p_image_header_data,&(jp2->w),4); /* WIDTH */
585 p_image_header_data += 4;
586 opj_read_bytes(p_image_header_data,&(jp2->numcomps),2); /* NC */
587 p_image_header_data += 2;
589 /* allocate memory for components */
590 jp2->comps = (opj_jp2_comps_t*) opj_malloc(jp2->numcomps * sizeof(opj_jp2_comps_t));
591 if (jp2->comps == 0) {
592 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to handle image header (ihdr)\n");
595 memset(jp2->comps,0,jp2->numcomps * sizeof(opj_jp2_comps_t));
597 opj_read_bytes(p_image_header_data,&(jp2->bpc),1); /* BPC */
598 ++ p_image_header_data;
600 /* if equal to 0 then need a BPC box (cf. chapter about image header box of the norm) */
601 /*if (jp2->bpc == 0){
602 indicate with a flag that we will wait a BPC box
605 opj_read_bytes(p_image_header_data,&(jp2->C),1); /* C */
606 ++ p_image_header_data;
608 /* Should be equal to 7 cf. chapter about image header box of the norm */
610 opj_event_msg_v2(p_manager, EVT_INFO, "JP2 IHDR box: compression type indicate that the file is not a conforming JP2 file (%d) \n", jp2->C);
613 opj_read_bytes(p_image_header_data,&(jp2->UnkC),1); /* UnkC */
614 ++ p_image_header_data;
615 opj_read_bytes(p_image_header_data,&(jp2->IPR),1); /* IPR */
616 ++ p_image_header_data;
621 static void jp2_write_ihdr(opj_jp2_t *jp2, opj_cio_t *cio) {
624 box.init_pos = cio_tell(cio);
626 cio_write(cio, JP2_IHDR, 4); /* IHDR */
628 cio_write(cio, jp2->h, 4); /* HEIGHT */
629 cio_write(cio, jp2->w, 4); /* WIDTH */
630 cio_write(cio, jp2->numcomps, 2); /* NC */
632 cio_write(cio, jp2->bpc, 1); /* BPC */
634 cio_write(cio, jp2->C, 1); /* C : Always 7 */
635 cio_write(cio, jp2->UnkC, 1); /* UnkC, colorspace unknown */
636 cio_write(cio, jp2->IPR, 1); /* IPR, no intellectual property */
638 box.length = cio_tell(cio) - box.init_pos;
639 cio_seek(cio, box.init_pos);
640 cio_write(cio, box.length, 4); /* L */
641 cio_seek(cio, box.init_pos + box.length);
645 * Writes the Image Header box - Image Header box.
647 * @param jp2 jpeg2000 file codec.
648 * @param p_nb_bytes_written pointer to store the nb of bytes written by the function.
650 * @return the data being copied.
652 static unsigned char * jp2_write_ihdr_v2( opj_jp2_v2_t *jp2,
653 unsigned int * p_nb_bytes_written )
655 unsigned char * l_ihdr_data,* l_current_ihdr_ptr;
659 assert(p_nb_bytes_written != 00);
661 /* default image header is 22 bytes wide */
662 l_ihdr_data = (unsigned char *) opj_malloc(22);
663 if (l_ihdr_data == 00) {
666 memset(l_ihdr_data,0,22);
668 l_current_ihdr_ptr = l_ihdr_data;
670 opj_write_bytes(l_current_ihdr_ptr,22,4); /* write box size */
671 l_current_ihdr_ptr+=4;
673 opj_write_bytes(l_current_ihdr_ptr,JP2_IHDR, 4); /* IHDR */
674 l_current_ihdr_ptr+=4;
676 opj_write_bytes(l_current_ihdr_ptr,jp2->h, 4); /* HEIGHT */
677 l_current_ihdr_ptr+=4;
679 opj_write_bytes(l_current_ihdr_ptr, jp2->w, 4); /* WIDTH */
680 l_current_ihdr_ptr+=4;
682 opj_write_bytes(l_current_ihdr_ptr, jp2->numcomps, 2); /* NC */
683 l_current_ihdr_ptr+=2;
685 opj_write_bytes(l_current_ihdr_ptr, jp2->bpc, 1); /* BPC */
686 ++l_current_ihdr_ptr;
688 opj_write_bytes(l_current_ihdr_ptr, jp2->C, 1); /* C : Always 7 */
689 ++l_current_ihdr_ptr;
691 opj_write_bytes(l_current_ihdr_ptr, jp2->UnkC, 1); /* UnkC, colorspace unknown */
692 ++l_current_ihdr_ptr;
694 opj_write_bytes(l_current_ihdr_ptr, jp2->IPR, 1); /* IPR, no intellectual property */
695 ++l_current_ihdr_ptr;
697 *p_nb_bytes_written = 22;
702 static void jp2_write_bpcc(opj_jp2_t *jp2, opj_cio_t *cio) {
706 box.init_pos = cio_tell(cio);
708 cio_write(cio, JP2_BPCC, 4); /* BPCC */
710 for (i = 0; i < jp2->numcomps; i++) {
711 cio_write(cio, jp2->comps[i].bpcc, 1);
714 box.length = cio_tell(cio) - box.init_pos;
715 cio_seek(cio, box.init_pos);
716 cio_write(cio, box.length, 4); /* L */
717 cio_seek(cio, box.init_pos + box.length);
722 * Writes the Bit per Component box.
724 * @param jp2 jpeg2000 file codec.
725 * @param p_nb_bytes_written pointer to store the nb of bytes written by the function.
727 * @return the data being copied.
729 unsigned char * jp2_write_bpcc_v2( opj_jp2_v2_t *jp2,
730 unsigned int * p_nb_bytes_written )
733 /* room for 8 bytes for box and 1 byte for each component */
734 int l_bpcc_size = 8 + jp2->numcomps;
735 unsigned char * l_bpcc_data,* l_current_bpcc_ptr;
739 assert(p_nb_bytes_written != 00);
741 l_bpcc_data = (unsigned char *) opj_malloc(l_bpcc_size);
742 if (l_bpcc_data == 00) {
745 memset(l_bpcc_data,0,l_bpcc_size);
747 l_current_bpcc_ptr = l_bpcc_data;
749 opj_write_bytes(l_current_bpcc_ptr,l_bpcc_size,4); /* write box size */
750 l_current_bpcc_ptr += 4;
752 opj_write_bytes(l_current_bpcc_ptr,JP2_BPCC,4); /* BPCC */
753 l_current_bpcc_ptr += 4;
755 for (i = 0; i < jp2->numcomps; ++i) {
756 opj_write_bytes(l_current_bpcc_ptr, jp2->comps[i].bpcc, 1); /* write each component information */
757 ++l_current_bpcc_ptr;
760 *p_nb_bytes_written = l_bpcc_size;
767 * Reads a Bit per Component box.
769 * @param jp2 the jpeg2000 file codec.
770 * @param p_bpc_header_data pointer to actual data (already read from file)
771 * @param p_bpc_header_size pointer that will hold the size of the bpc header
772 * @param p_manager the user event manager.
774 * @return true if the bpc header is valid, false otherwise.
776 static opj_bool opj_jp2_read_bpcc( opj_jp2_v2_t *jp2,
777 OPJ_BYTE * p_bpc_header_data,
778 OPJ_UINT32 p_bpc_header_size,
779 opj_event_mgr_t * p_manager
785 assert(p_bpc_header_data != 00);
787 assert(p_manager != 00);
790 /*if (jp2->bpc != 0 ){
791 opj_event_msg_v2(p_manager, EVT_WARNING, "A BPCC header box is available although BPC is different to zero (%d)\n",jp2->bpc);
794 /* and length is relevant */
795 if (p_bpc_header_size != jp2->numcomps) {
796 opj_event_msg_v2(p_manager, EVT_ERROR, "Bad BPCC header box (bad size)\n");
800 /* read info for each component */
801 for (i = 0; i < jp2->numcomps; ++i) {
802 opj_read_bytes(p_bpc_header_data,&jp2->comps[i].bpcc ,1); /* read each BPCC component */
809 static void jp2_write_colr(opj_jp2_t *jp2, opj_cio_t *cio) {
812 box.init_pos = cio_tell(cio);
814 cio_write(cio, JP2_COLR, 4); /* COLR */
816 cio_write(cio, jp2->meth, 1); /* METH */
817 cio_write(cio, jp2->precedence, 1); /* PRECEDENCE */
818 cio_write(cio, jp2->approx, 1); /* APPROX */
823 cio_write(cio, jp2->enumcs, 4); /* EnumCS */
825 box.length = cio_tell(cio) - box.init_pos;
826 cio_seek(cio, box.init_pos);
827 cio_write(cio, box.length, 4); /* L */
828 cio_seek(cio, box.init_pos + box.length);
832 * Writes the Colour Specification box.
834 * @param jp2 jpeg2000 file codec.
835 * @param p_nb_bytes_written pointer to store the nb of bytes written by the function.
837 * @return the data being copied.
839 unsigned char *jp2_write_colr_v2( opj_jp2_v2_t *jp2,
840 unsigned int * p_nb_bytes_written )
842 /* room for 8 bytes for box 3 for common data and variable upon profile*/
843 unsigned int l_colr_size = 11;
844 unsigned char * l_colr_data,* l_current_colr_ptr;
848 assert(p_nb_bytes_written != 00);
861 l_colr_data = (unsigned char *) opj_malloc(l_colr_size);
862 if (l_colr_data == 00) {
865 memset(l_colr_data,0,l_colr_size);
867 l_current_colr_ptr = l_colr_data;
869 opj_write_bytes(l_current_colr_ptr,l_colr_size,4); /* write box size */
870 l_current_colr_ptr += 4;
872 opj_write_bytes(l_current_colr_ptr,JP2_COLR,4); /* BPCC */
873 l_current_colr_ptr += 4;
875 opj_write_bytes(l_current_colr_ptr, jp2->meth,1); /* METH */
876 ++l_current_colr_ptr;
878 opj_write_bytes(l_current_colr_ptr, jp2->precedence,1); /* PRECEDENCE */
879 ++l_current_colr_ptr;
881 opj_write_bytes(l_current_colr_ptr, jp2->approx,1); /* APPROX */
882 ++l_current_colr_ptr;
884 if (jp2->meth == 1) {
885 opj_write_bytes(l_current_colr_ptr, jp2->enumcs,4); /* EnumCS */
888 opj_write_bytes(l_current_colr_ptr, 0, 1); /* PROFILE (??) */
891 *p_nb_bytes_written = l_colr_size;
896 static void jp2_free_pclr(opj_jp2_color_t *color)
898 opj_free(color->jp2_pclr->channel_sign);
899 opj_free(color->jp2_pclr->channel_size);
900 opj_free(color->jp2_pclr->entries);
902 if(color->jp2_pclr->cmap) opj_free(color->jp2_pclr->cmap);
904 opj_free(color->jp2_pclr); color->jp2_pclr = NULL;
907 static void free_color_data(opj_jp2_color_t *color)
911 jp2_free_pclr(color);
915 if(color->jp2_cdef->info) opj_free(color->jp2_cdef->info);
916 opj_free(color->jp2_cdef);
918 if(color->icc_profile_buf) opj_free(color->icc_profile_buf);
922 static void jp2_apply_pclr(opj_image_t *image, opj_jp2_color_t *color)
924 opj_image_comp_t *old_comps, *new_comps;
925 OPJ_BYTE *channel_size, *channel_sign;
927 opj_jp2_cmap_comp_t *cmap;
928 OPJ_INT32 *src, *dst;
930 OPJ_UINT16 i, nr_channels, cmp, pcol;
933 channel_size = color->jp2_pclr->channel_size;
934 channel_sign = color->jp2_pclr->channel_sign;
935 entries = color->jp2_pclr->entries;
936 cmap = color->jp2_pclr->cmap;
937 nr_channels = color->jp2_pclr->nr_channels;
939 old_comps = image->comps;
940 new_comps = (opj_image_comp_t*)
941 opj_malloc(nr_channels * sizeof(opj_image_comp_t));
943 for(i = 0; i < nr_channels; ++i) {
944 pcol = cmap[i].pcol; cmp = cmap[i].cmp;
946 new_comps[pcol] = old_comps[cmp];
949 if(cmap[i].mtyp == 0){
950 old_comps[cmp].data = NULL; continue;
953 /* Palette mapping: */
954 new_comps[pcol].data = (int*)
955 opj_malloc(old_comps[cmp].w * old_comps[cmp].h * sizeof(int));
956 new_comps[pcol].prec = channel_size[i];
957 new_comps[pcol].sgnd = channel_sign[i];
960 top_k = color->jp2_pclr->nr_entries - 1;
962 for(i = 0; i < nr_channels; ++i) {
964 if(cmap[i].mtyp == 0) continue;
966 /* Palette mapping: */
967 cmp = cmap[i].cmp; pcol = cmap[i].pcol;
968 src = old_comps[cmp].data;
969 dst = new_comps[pcol].data;
970 max = new_comps[pcol].w * new_comps[pcol].h;
972 for(j = 0; j < max; ++j)
975 if((k = src[j]) < 0) k = 0; else if(k > top_k) k = top_k;
978 dst[j] = entries[k * nr_channels + pcol];
982 max = image->numcomps;
983 for(i = 0; i < max; ++i) {
984 if(old_comps[i].data) opj_free(old_comps[i].data);
988 image->comps = new_comps;
989 image->numcomps = nr_channels;
991 jp2_free_pclr(color);
996 * Reads a palette box.
998 * @param jp2 the jpeg2000 file codec.
999 * @param p_pclr_header_data pointer to actual data (already read from file)
1000 * @param p_pclr_header_size pointer that will hold the size of the PCLR header
1001 * @param p_manager the user event manager.
1003 * @return true if the bpc header is valid, fale else.
1005 static opj_bool opj_jp2_read_pclr( opj_jp2_v2_t *jp2,
1006 OPJ_BYTE * p_pclr_header_data,
1007 OPJ_UINT32 p_pclr_header_size,
1008 opj_event_mgr_t * p_manager
1011 opj_jp2_pclr_t *jp2_pclr;
1012 OPJ_BYTE *channel_size, *channel_sign;
1013 OPJ_UINT32 *entries;
1014 OPJ_UINT16 nr_entries,nr_channels;
1019 assert(p_pclr_header_data != 00);
1021 assert(p_manager != 00);
1022 (void)p_pclr_header_size;
1024 if(jp2->color.jp2_pclr)
1027 opj_read_bytes(p_pclr_header_data, &l_value , 2); /* NE */
1028 p_pclr_header_data += 2;
1029 nr_entries = (OPJ_UINT16) l_value;
1031 opj_read_bytes(p_pclr_header_data, &l_value , 1); /* NPC */
1032 ++p_pclr_header_data;
1033 nr_channels = (OPJ_UINT16) l_value;
1035 entries = (OPJ_UINT32*) opj_malloc(nr_channels * nr_entries * sizeof(OPJ_UINT32));
1036 channel_size = (OPJ_BYTE*) opj_malloc(nr_channels);
1037 channel_sign = (OPJ_BYTE*) opj_malloc(nr_channels);
1039 jp2_pclr = (opj_jp2_pclr_t*)opj_malloc(sizeof(opj_jp2_pclr_t));
1040 jp2_pclr->channel_sign = channel_sign;
1041 jp2_pclr->channel_size = channel_size;
1042 jp2_pclr->entries = entries;
1043 jp2_pclr->nr_entries = nr_entries;
1044 jp2_pclr->nr_channels = nr_channels;
1045 jp2_pclr->cmap = NULL;
1047 jp2->color.jp2_pclr = jp2_pclr;
1049 for(i = 0; i < nr_channels; ++i) {
1050 opj_read_bytes(p_pclr_header_data, &l_value , 1); /* Bi */
1051 ++p_pclr_header_data;
1053 channel_size[i] = (l_value & 0x7f) + 1;
1054 channel_sign[i] = (l_value & 0x80)? 1 : 0;
1057 for(j = 0; j < nr_entries; ++j) {
1058 for(i = 0; i < nr_channels; ++i) {
1059 int bytes_to_read = (channel_size[i]+7)>>3;
1061 opj_read_bytes(p_pclr_header_data, &l_value , bytes_to_read); /* Cji */
1062 p_pclr_header_data += bytes_to_read;
1063 *entries = (OPJ_UINT32) l_value;
1072 * Reads the Component Mapping box.
1074 * @param p_cmap_header_data pointer to actual data (already read from file)
1075 * @param jp2 the jpeg2000 file codec.
1076 * @param p_cmap_header_size pointer that will hold the size of the color header
1077 * @param p_manager the user event manager.
1079 * @return true if the cdef header is valid, false else.
1081 static opj_bool opj_jp2_read_cmap( opj_jp2_v2_t * jp2,
1082 OPJ_BYTE * p_cmap_header_data,
1083 OPJ_UINT32 p_cmap_header_size,
1084 opj_event_mgr_t * p_manager
1087 opj_jp2_cmap_comp_t *cmap;
1088 OPJ_BYTE i, nr_channels;
1093 assert(p_cmap_header_data != 00);
1094 assert(p_manager != 00);
1095 (void)p_cmap_header_size;
1097 /* Need nr_channels: */
1098 if(jp2->color.jp2_pclr == NULL) {
1099 opj_event_msg_v2(p_manager, EVT_ERROR, "Need to read a PCLR box before the CMAP box.\n");
1103 /* Part 1, I.5.3.5: 'There shall be at most one Component Mapping box
1104 * inside a JP2 Header box' :
1106 if(jp2->color.jp2_pclr->cmap) {
1107 opj_event_msg_v2(p_manager, EVT_ERROR, "Only one CMAP box is allowed.\n");
1111 nr_channels = jp2->color.jp2_pclr->nr_channels;
1112 cmap = (opj_jp2_cmap_comp_t*) opj_malloc(nr_channels * sizeof(opj_jp2_cmap_comp_t));
1114 for(i = 0; i < nr_channels; ++i) {
1115 opj_read_bytes(p_cmap_header_data, &l_value, 2); /* CMP^i */
1116 p_cmap_header_data +=2;
1117 cmap[i].cmp = (OPJ_UINT16) l_value;
1119 opj_read_bytes(p_cmap_header_data, &l_value, 1); /* MTYP^i */
1120 ++p_cmap_header_data;
1121 cmap[i].mtyp = (OPJ_BYTE) l_value;
1123 opj_read_bytes(p_cmap_header_data, &l_value, 1); /* PCOL^i */
1124 ++p_cmap_header_data;
1125 cmap[i].pcol = (OPJ_BYTE) l_value;
1128 jp2->color.jp2_pclr->cmap = cmap;
1133 static void jp2_apply_cdef(opj_image_t *image, opj_jp2_color_t *color)
1135 opj_jp2_cdef_info_t *info;
1136 OPJ_INT32 color_space;
1137 OPJ_UINT16 i, n, cn, typ, asoc, acn;
1139 color_space = image->color_space;
1140 info = color->jp2_cdef->info;
1141 n = color->jp2_cdef->n;
1143 for(i = 0; i < n; ++i)
1145 /* WATCH: acn = asoc - 1 ! */
1146 if((asoc = info[i].asoc) == 0) continue;
1148 cn = info[i].cn; typ = info[i].typ; acn = asoc - 1;
1152 opj_image_comp_t saved;
1154 memcpy(&saved, &image->comps[cn], sizeof(opj_image_comp_t));
1155 memcpy(&image->comps[cn], &image->comps[acn], sizeof(opj_image_comp_t));
1156 memcpy(&image->comps[acn], &saved, sizeof(opj_image_comp_t));
1158 info[i].asoc = cn + 1;
1159 info[acn].asoc = info[acn].cn + 1;
1163 if(color->jp2_cdef->info) opj_free(color->jp2_cdef->info);
1165 opj_free(color->jp2_cdef); color->jp2_cdef = NULL;
1167 }/* jp2_apply_cdef() */
1170 * Reads the Component Definition box.
1172 * @param p_cdef_header_data pointer to actual data (already read from file)
1173 * @param jp2 the jpeg2000 file codec.
1174 * @param p_cdef_header_size pointer that will hold the size of the color header
1175 * @param p_manager the user event manager.
1177 * @return true if the cdef header is valid, false else.
1179 static opj_bool opj_jp2_read_cdef( opj_jp2_v2_t * jp2,
1180 OPJ_BYTE * p_cdef_header_data,
1181 OPJ_UINT32 p_cdef_header_size,
1182 opj_event_mgr_t * p_manager
1185 opj_jp2_cdef_info_t *cdef_info;
1191 assert(p_cdef_header_data != 00);
1192 assert(p_manager != 00);
1193 (void)p_cdef_header_size;
1195 /* Part 1, I.5.3.6: 'The shall be at most one Channel Definition box
1196 * inside a JP2 Header box.'*/
1197 if(jp2->color.jp2_cdef) return OPJ_FALSE;
1199 opj_read_bytes(p_cdef_header_data,&l_value ,2); /* N */
1200 p_cdef_header_data+= 2;
1202 if ( (OPJ_UINT16)l_value == 0){ /* szukw000: FIXME */
1203 opj_event_msg_v2(p_manager, EVT_ERROR, "Number of channel description is equal to zero in CDEF box.\n");
1207 cdef_info = (opj_jp2_cdef_info_t*) opj_malloc(l_value * sizeof(opj_jp2_cdef_info_t));
1209 jp2->color.jp2_cdef = (opj_jp2_cdef_t*)opj_malloc(sizeof(opj_jp2_cdef_t));
1210 jp2->color.jp2_cdef->info = cdef_info;
1211 jp2->color.jp2_cdef->n = (OPJ_UINT16) l_value;
1213 for(i = 0; i < jp2->color.jp2_cdef->n; ++i) {
1214 opj_read_bytes(p_cdef_header_data, &l_value, 2); /* Cn^i */
1215 p_cdef_header_data +=2;
1216 cdef_info[i].cn = (OPJ_UINT16) l_value;
1218 opj_read_bytes(p_cdef_header_data, &l_value, 2); /* Typ^i */
1219 p_cdef_header_data +=2;
1220 cdef_info[i].typ = (OPJ_UINT16) l_value;
1222 opj_read_bytes(p_cdef_header_data, &l_value, 2); /* Asoc^i */
1223 p_cdef_header_data +=2;
1224 cdef_info[i].asoc = (OPJ_UINT16) l_value;
1231 * Reads the Colour Specification box.
1233 * @param p_colr_header_data pointer to actual data (already read from file)
1234 * @param jp2 the jpeg2000 file codec.
1235 * @param p_colr_header_size pointer that will hold the size of the color header
1236 * @param p_colr_header_max_size maximum size of the header, any size bigger than this value should result the function to output false.
1237 * @param p_manager the user event manager.
1239 * @return true if the bpc header is valid, fale else.
1241 static opj_bool opj_jp2_read_colr( opj_jp2_v2_t *jp2,
1242 OPJ_BYTE * p_colr_header_data,
1243 OPJ_UINT32 p_colr_header_size,
1244 opj_event_mgr_t * p_manager
1251 assert(p_colr_header_data != 00);
1252 assert(p_manager != 00);
1254 if (p_colr_header_size < 3) {
1255 opj_event_msg_v2(p_manager, EVT_ERROR, "Bad COLR header box (bad size)\n");
1259 /* Part 1, I.5.3.3 : 'A conforming JP2 reader shall ignore all Colour
1260 * Specification boxes after the first.'
1262 if(jp2->color.jp2_has_colr) {
1263 opj_event_msg_v2(p_manager, EVT_INFO, "A conforming JP2 reader shall ignore all Colour Specification boxes after the first, so we ignore this one.\n");
1264 p_colr_header_data += p_colr_header_size;
1268 opj_read_bytes(p_colr_header_data,&jp2->meth ,1); /* METH */
1269 ++p_colr_header_data;
1271 opj_read_bytes(p_colr_header_data,&jp2->precedence ,1); /* PRECEDENCE */
1272 ++p_colr_header_data;
1274 opj_read_bytes(p_colr_header_data,&jp2->approx ,1); /* APPROX */
1275 ++p_colr_header_data;
1277 if (jp2->meth == 1) {
1278 if (p_colr_header_size != 7) {
1279 opj_event_msg_v2(p_manager, EVT_ERROR, "Bad BPCC header box (bad size)\n");
1283 opj_read_bytes(p_colr_header_data,&jp2->enumcs ,4); /* EnumCS */
1285 else if (jp2->meth == 2) {
1287 int it_icc_value = 0;
1288 int icc_len = p_colr_header_size - 3;
1290 jp2->color.icc_profile_len = icc_len;
1291 jp2->color.icc_profile_buf = (unsigned char*) opj_malloc(icc_len);
1293 memset(jp2->color.icc_profile_buf, 0, icc_len * sizeof(unsigned char));
1295 for (it_icc_value = 0; it_icc_value < icc_len; ++it_icc_value)
1297 opj_read_bytes(p_colr_header_data,&l_value,1); /* icc values */
1298 ++p_colr_header_data;
1299 jp2->color.icc_profile_buf[it_icc_value] = (OPJ_BYTE) l_value;
1304 opj_event_msg_v2(p_manager, EVT_INFO, "COLR BOX meth value is not a regular value (%d), so we will skip the fields following the approx field.\n", jp2->meth);
1306 jp2->color.jp2_has_colr = 1;
1312 opj_bool opj_jp2_decode(opj_jp2_v2_t *jp2,
1313 opj_stream_private_t *p_stream,
1314 opj_image_t* p_image,
1315 opj_event_mgr_t * p_manager)
1321 if( ! opj_j2k_decode(jp2->j2k, p_stream, p_image, p_manager) ) {
1322 opj_event_msg_v2(p_manager, EVT_ERROR, "Failed to decode the codestream in the JP2 file\n");
1326 if (!jp2->ignore_pclr_cmap_cdef){
1328 /* Set Image Color Space */
1329 if (jp2->enumcs == 16)
1330 p_image->color_space = CLRSPC_SRGB;
1331 else if (jp2->enumcs == 17)
1332 p_image->color_space = CLRSPC_GRAY;
1333 else if (jp2->enumcs == 18)
1334 p_image->color_space = CLRSPC_SYCC;
1336 p_image->color_space = CLRSPC_UNKNOWN;
1338 /* Apply the color space if needed */
1339 if(jp2->color.jp2_cdef) {
1340 jp2_apply_cdef(p_image, &(jp2->color));
1343 if(jp2->color.jp2_pclr) {
1344 /* Part 1, I.5.3.4: Either both or none : */
1345 if( !jp2->color.jp2_pclr->cmap)
1346 jp2_free_pclr(&(jp2->color));
1348 jp2_apply_pclr(p_image, &(jp2->color));
1351 if(jp2->color.icc_profile_buf) {
1352 p_image->icc_profile_buf = jp2->color.icc_profile_buf;
1353 p_image->icc_profile_len = jp2->color.icc_profile_len;
1354 jp2->color.icc_profile_buf = NULL;
1362 void jp2_write_jp2h(opj_jp2_t *jp2, opj_cio_t *cio) {
1365 box.init_pos = cio_tell(cio);
1367 cio_write(cio, JP2_JP2H, 4); /* JP2H */
1369 jp2_write_ihdr(jp2, cio);
1371 if (jp2->bpc == 255) {
1372 jp2_write_bpcc(jp2, cio);
1374 jp2_write_colr(jp2, cio);
1376 box.length = cio_tell(cio) - box.init_pos;
1377 cio_seek(cio, box.init_pos);
1378 cio_write(cio, box.length, 4); /* L */
1379 cio_seek(cio, box.init_pos + box.length);
1383 * Writes the Jpeg2000 file Header box - JP2 Header box (warning, this is a super box).
1385 * @param cio the stream to write data to.
1386 * @param jp2 the jpeg2000 file codec.
1387 * @param p_manager user event manager.
1389 * @return true if writting was successful.
1391 opj_bool jp2_write_jp2h_v2( opj_jp2_v2_t *jp2,
1392 opj_stream_private_t *cio,
1393 opj_event_mgr_t * p_manager )
1395 opj_jp2_img_header_writer_handler_t l_writers [3];
1396 opj_jp2_img_header_writer_handler_t * l_current_writer;
1399 /* size of data for super box*/
1400 int l_jp2h_size = 8;
1401 opj_bool l_result = OPJ_TRUE;
1403 /* to store the data of the super box */
1404 unsigned char l_jp2h_data [8];
1409 assert(p_manager != 00);
1411 memset(l_writers,0,sizeof(l_writers));
1413 if (jp2->bpc == 255) {
1415 l_writers[0].handler = jp2_write_ihdr_v2;
1416 l_writers[1].handler = jp2_write_bpcc_v2;
1417 l_writers[2].handler = jp2_write_colr_v2;
1421 l_writers[0].handler = jp2_write_ihdr_v2;
1422 l_writers[1].handler = jp2_write_colr_v2;
1425 /* write box header */
1426 /* write JP2H type */
1427 opj_write_bytes(l_jp2h_data+4,JP2_JP2H,4);
1429 l_current_writer = l_writers;
1430 for (i=0;i<l_nb_pass;++i) {
1431 l_current_writer->m_data = l_current_writer->handler(jp2,&(l_current_writer->m_size));
1432 if (l_current_writer->m_data == 00) {
1433 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to hold JP2 Header data\n");
1434 l_result = OPJ_FALSE;
1438 l_jp2h_size += l_current_writer->m_size;
1443 l_current_writer = l_writers;
1444 for (i=0;i<l_nb_pass;++i) {
1445 if (l_current_writer->m_data != 00) {
1446 opj_free(l_current_writer->m_data );
1454 /* write super box size */
1455 opj_write_bytes(l_jp2h_data,l_jp2h_size,4);
1457 /* write super box data on stream */
1458 if (opj_stream_write_data(cio,l_jp2h_data,8,p_manager) != 8) {
1459 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream error while writing JP2 Header box\n");
1460 l_result = OPJ_FALSE;
1464 l_current_writer = l_writers;
1465 for (i=0;i<l_nb_pass;++i) {
1466 if (opj_stream_write_data(cio,l_current_writer->m_data,l_current_writer->m_size,p_manager) != l_current_writer->m_size) {
1467 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream error while writting JP2 Header box\n");
1468 l_result = OPJ_FALSE;
1475 l_current_writer = l_writers;
1478 for (i=0;i<l_nb_pass;++i) {
1479 if (l_current_writer->m_data != 00) {
1480 opj_free(l_current_writer->m_data );
1488 static void jp2_write_ftyp(opj_jp2_t *jp2, opj_cio_t *cio) {
1492 box.init_pos = cio_tell(cio);
1494 cio_write(cio, JP2_FTYP, 4); /* FTYP */
1496 cio_write(cio, jp2->brand, 4); /* BR */
1497 cio_write(cio, jp2->minversion, 4); /* MinV */
1499 for (i = 0; i < jp2->numcl; i++) {
1500 cio_write(cio, jp2->cl[i], 4); /* CL */
1503 box.length = cio_tell(cio) - box.init_pos;
1504 cio_seek(cio, box.init_pos);
1505 cio_write(cio, box.length, 4); /* L */
1506 cio_seek(cio, box.init_pos + box.length);
1510 * Writes a FTYP box - File type box
1512 * @param cio the stream to write data to.
1513 * @param jp2 the jpeg2000 file codec.
1514 * @param p_manager the user event manager.
1516 * @return true if writting was successful.
1518 opj_bool jp2_write_ftyp_v2( opj_jp2_v2_t *jp2,
1519 opj_stream_private_t *cio,
1520 opj_event_mgr_t * p_manager )
1523 unsigned int l_ftyp_size = 16 + 4 * jp2->numcl;
1524 unsigned char * l_ftyp_data, * l_current_data_ptr;
1530 assert(p_manager != 00);
1532 l_ftyp_data = (unsigned char *) opj_malloc(l_ftyp_size);
1534 if (l_ftyp_data == 00) {
1535 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to handle ftyp data\n");
1539 memset(l_ftyp_data,0,l_ftyp_size);
1541 l_current_data_ptr = l_ftyp_data;
1543 opj_write_bytes(l_current_data_ptr, l_ftyp_size,4); /* box size */
1544 l_current_data_ptr += 4;
1546 opj_write_bytes(l_current_data_ptr, JP2_FTYP,4); /* FTYP */
1547 l_current_data_ptr += 4;
1549 opj_write_bytes(l_current_data_ptr, jp2->brand,4); /* BR */
1550 l_current_data_ptr += 4;
1552 opj_write_bytes(l_current_data_ptr, jp2->minversion,4); /* MinV */
1553 l_current_data_ptr += 4;
1555 for (i = 0; i < jp2->numcl; i++) {
1556 opj_write_bytes(l_current_data_ptr, jp2->cl[i],4); /* CL */
1559 l_result = (opj_stream_write_data(cio,l_ftyp_data,l_ftyp_size,p_manager) == l_ftyp_size);
1562 opj_event_msg_v2(p_manager, EVT_ERROR, "Error while writting ftyp data to stream\n");
1565 opj_free(l_ftyp_data);
1570 static int jp2_write_jp2c(opj_jp2_t *jp2, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info) {
1571 unsigned int j2k_codestream_offset, j2k_codestream_length;
1574 opj_j2k_t *j2k = jp2->j2k;
1576 box.init_pos = cio_tell(cio);
1578 cio_write(cio, JP2_JP2C, 4); /* JP2C */
1581 j2k_codestream_offset = cio_tell(cio);
1582 if(!j2k_encode(j2k, cio, image, cstr_info)) {
1583 opj_event_msg(j2k->cinfo, EVT_ERROR, "Failed to encode image\n");
1586 j2k_codestream_length = cio_tell(cio) - j2k_codestream_offset;
1588 jp2->j2k_codestream_offset = j2k_codestream_offset;
1589 jp2->j2k_codestream_length = j2k_codestream_length;
1591 box.length = 8 + jp2->j2k_codestream_length;
1592 cio_seek(cio, box.init_pos);
1593 cio_write(cio, box.length, 4); /* L */
1594 cio_seek(cio, box.init_pos + box.length);
1600 * Writes the Jpeg2000 codestream Header box - JP2C Header box.
1602 * @param cio the stream to write data to.
1603 * @param jp2 the jpeg2000 file codec.
1604 * @param p_manager user event manager.
1606 * @return true if writting was successful.
1608 opj_bool jp2_write_jp2c_v2( opj_jp2_v2_t *jp2,
1609 opj_stream_private_t *cio,
1610 opj_event_mgr_t * p_manager )
1612 unsigned int j2k_codestream_exit;
1613 unsigned char l_data_header [8];
1618 assert(p_manager != 00);
1619 assert(opj_stream_has_seek(cio));
1621 j2k_codestream_exit = opj_stream_tell(cio);
1622 opj_write_bytes(l_data_header,j2k_codestream_exit - jp2->j2k_codestream_offset,4); /* size of codestream */
1623 opj_write_bytes(l_data_header + 4,JP2_JP2C,4); /* JP2C */
1625 if (! opj_stream_seek(cio,jp2->j2k_codestream_offset,p_manager)) {
1626 opj_event_msg_v2(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
1630 if (opj_stream_write_data(cio,l_data_header,8,p_manager) != 8) {
1631 opj_event_msg_v2(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
1635 if (! opj_stream_seek(cio,j2k_codestream_exit,p_manager)) {
1636 opj_event_msg_v2(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
1643 static void jp2_write_jp(opj_cio_t *cio) {
1646 box.init_pos = cio_tell(cio);
1648 cio_write(cio, JP2_JP, 4); /* JP2 signature */
1649 cio_write(cio, 0x0d0a870a, 4);
1651 box.length = cio_tell(cio) - box.init_pos;
1652 cio_seek(cio, box.init_pos);
1653 cio_write(cio, box.length, 4); /* L */
1654 cio_seek(cio, box.init_pos + box.length);
1658 * Writes a jpeg2000 file signature box.
1660 * @param cio the stream to write data to.
1661 * @param jp2 the jpeg2000 file codec.
1662 * @param p_manager the user event manager.
1664 * @return true if writting was successful.
1666 opj_bool jp2_write_jp_v2( opj_jp2_v2_t *jp2,
1667 opj_stream_private_t *cio,
1668 opj_event_mgr_t * p_manager )
1670 /* 12 bytes will be read */
1671 unsigned char l_signature_data [12];
1676 assert(p_manager != 00);
1678 /* write box length */
1679 opj_write_bytes(l_signature_data,12,4);
1680 /* writes box type */
1681 opj_write_bytes(l_signature_data+4,JP2_JP,4);
1682 /* writes magic number*/
1683 opj_write_bytes(l_signature_data+8,0x0d0a870a,4);
1685 if (opj_stream_write_data(cio,l_signature_data,12,p_manager) != 12) {
1692 static int write_fidx( int offset_jp2c, int length_jp2c, int offset_idx, int length_idx, opj_cio_t *cio)
1696 lenp = cio_tell( cio);
1697 cio_skip( cio, 4); /* L [at the end] */
1698 cio_write( cio, JPIP_FIDX, 4); /* IPTR */
1700 write_prxy( offset_jp2c, length_jp2c, offset_idx, length_idx, cio);
1702 len = cio_tell( cio)-lenp;
1703 cio_seek( cio, lenp);
1704 cio_write( cio, len, 4); /* L */
1705 cio_seek( cio, lenp+len);
1710 static void write_prxy( int offset_jp2c, int length_jp2c, int offset_idx, int length_idx, opj_cio_t *cio)
1714 lenp = cio_tell( cio);
1715 cio_skip( cio, 4); /* L [at the end] */
1716 cio_write( cio, JPIP_PRXY, 4); /* IPTR */
1718 cio_write( cio, offset_jp2c, 8); /* OOFF */
1719 cio_write( cio, length_jp2c, 4); /* OBH part 1 */
1720 cio_write( cio, JP2_JP2C, 4); /* OBH part 2 */
1722 cio_write( cio, 1,1); /* NI */
1724 cio_write( cio, offset_idx, 8); /* IOFF */
1725 cio_write( cio, length_idx, 4); /* IBH part 1 */
1726 cio_write( cio, JPIP_CIDX, 4); /* IBH part 2 */
1728 len = cio_tell( cio)-lenp;
1729 cio_seek( cio, lenp);
1730 cio_write( cio, len, 4); /* L */
1731 cio_seek( cio, lenp+len);
1734 static void write_iptr( int offset, int length, opj_cio_t *cio)
1738 lenp = cio_tell( cio);
1739 cio_skip( cio, 4); /* L [at the end] */
1740 cio_write( cio, JPIP_IPTR, 4); /* IPTR */
1742 cio_write( cio, offset, 8);
1743 cio_write( cio, length, 8);
1745 len = cio_tell( cio)-lenp;
1746 cio_seek( cio, lenp);
1747 cio_write( cio, len, 4); /* L */
1748 cio_seek( cio, lenp+len);
1752 /* ----------------------------------------------------------------------- */
1753 /* JP2 decoder interface */
1754 /* ----------------------------------------------------------------------- */
1757 void opj_jp2_setup_decoder(opj_jp2_v2_t *jp2, opj_dparameters_t *parameters)
1759 /* setup the J2K codec */
1760 opj_j2k_setup_decoder(jp2->j2k, parameters);
1762 /* further JP2 initializations go here */
1763 jp2->color.jp2_has_colr = 0;
1764 jp2->ignore_pclr_cmap_cdef = parameters->flags & OPJ_DPARAMETERS_IGNORE_PCLR_CMAP_CDEF_FLAG;
1768 /* ----------------------------------------------------------------------- */
1769 /* JP2 encoder interface */
1770 /* ----------------------------------------------------------------------- */
1772 opj_jp2_t* jp2_create_compress(opj_common_ptr cinfo) {
1773 opj_jp2_t *jp2 = (opj_jp2_t*)opj_malloc(sizeof(opj_jp2_t));
1776 /* create the J2K codec */
1777 jp2->j2k = j2k_create_compress(cinfo);
1778 if(jp2->j2k == NULL) {
1779 jp2_destroy_compress(jp2);
1786 void jp2_destroy_compress(opj_jp2_t *jp2) {
1788 /* destroy the J2K codec */
1789 j2k_destroy_compress(jp2->j2k);
1792 opj_free(jp2->comps);
1801 void jp2_setup_encoder( opj_jp2_v2_t *jp2,
1802 opj_cparameters_t *parameters,
1804 opj_event_mgr_t * p_manager) {
1808 if(!jp2 || !parameters || !image)
1811 /* setup the J2K codec */
1812 /* ------------------- */
1814 /* Check if number of components respects standard */
1815 if (image->numcomps < 1 || image->numcomps > 16384) {
1816 opj_event_msg_v2(p_manager, EVT_ERROR, "Invalid number of components specified while setting up JP2 encoder\n");
1820 j2k_setup_encoder_v2(jp2->j2k, parameters, image, p_manager );
1822 /* setup the JP2 codec */
1823 /* ------------------- */
1827 jp2->brand = JP2_JP2; /* BR */
1828 jp2->minversion = 0; /* MinV */
1830 jp2->cl = (unsigned int*) opj_malloc(jp2->numcl * sizeof(unsigned int));
1831 jp2->cl[0] = JP2_JP2; /* CL0 : JP2 */
1833 /* Image Header box */
1835 jp2->numcomps = image->numcomps; /* NC */
1836 jp2->comps = (opj_jp2_comps_t*) opj_malloc(jp2->numcomps * sizeof(opj_jp2_comps_t));
1837 jp2->h = image->y1 - image->y0; /* HEIGHT */
1838 jp2->w = image->x1 - image->x0; /* WIDTH */
1840 depth_0 = image->comps[0].prec - 1;
1841 sign = image->comps[0].sgnd;
1842 jp2->bpc = depth_0 + (sign << 7);
1843 for (i = 1; i < image->numcomps; i++) {
1844 int depth = image->comps[i].prec - 1;
1845 sign = image->comps[i].sgnd;
1846 if (depth_0 != depth)
1849 jp2->C = 7; /* C : Always 7 */
1850 jp2->UnkC = 0; /* UnkC, colorspace specified in colr box */
1851 jp2->IPR = 0; /* IPR, no intellectual property */
1853 /* BitsPerComponent box */
1854 for (i = 0; i < image->numcomps; i++) {
1855 jp2->comps[i].bpcc = image->comps[i].prec - 1 + (image->comps[i].sgnd << 7);
1858 /* Colour Specification box */
1859 if ((image->numcomps == 1 || image->numcomps == 3) && (jp2->bpc != 255)) {
1860 jp2->meth = 1; /* METH: Enumerated colourspace */
1862 jp2->meth = 2; /* METH: Restricted ICC profile */
1864 if (jp2->meth == 1) {
1865 if (image->color_space == 1)
1866 jp2->enumcs = 16; /* sRGB as defined by IEC 61966�2�1 */
1867 else if (image->color_space == 2)
1868 jp2->enumcs = 17; /* greyscale */
1869 else if (image->color_space == 3)
1870 jp2->enumcs = 18; /* YUV */
1872 jp2->enumcs = 0; /* PROFILE (??) */
1874 jp2->precedence = 0; /* PRECEDENCE */
1875 jp2->approx = 0; /* APPROX */
1877 // jp2->jpip_on = parameters->jpip_on;
1881 opj_bool opj_jp2_encode_v2( opj_jp2_v2_t *jp2,
1882 opj_stream_private_t *stream,
1883 opj_event_mgr_t * p_manager)
1885 return j2k_encode_v2(jp2->j2k, stream, p_manager);
1888 opj_bool opj_jp2_encode(opj_jp2_t *jp2, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info) {
1890 int pos_iptr, pos_cidx, pos_jp2c, len_jp2c, len_cidx, end_pos, pos_fidx, len_fidx;
1891 pos_jp2c = pos_iptr = -1; /* remove a warning */
1895 /* JPEG 2000 Signature box */
1898 jp2_write_ftyp(jp2, cio);
1899 /* JP2 Header box */
1900 jp2_write_jp2h(jp2, cio);
1903 pos_iptr = cio_tell( cio);
1904 cio_skip( cio, 24); /* IPTR further ! */
1906 pos_jp2c = cio_tell( cio);
1910 if(!(len_jp2c = jp2_write_jp2c( jp2, cio, image, cstr_info))){
1911 opj_event_msg(jp2->cinfo, EVT_ERROR, "Failed to encode image\n");
1916 pos_cidx = cio_tell( cio);
1918 len_cidx = write_cidx( pos_jp2c+8, cio, image, *cstr_info, len_jp2c-8);
1920 pos_fidx = cio_tell( cio);
1921 len_fidx = write_fidx( pos_jp2c, len_jp2c, pos_cidx, len_cidx, cio);
1923 end_pos = cio_tell( cio);
1925 cio_seek( cio, pos_iptr);
1926 write_iptr( pos_fidx, len_fidx, cio);
1927 cio_seek( cio, end_pos);
1934 * Ends the decompression procedures and possibiliy add data to be read after the
1937 opj_bool jp2_end_decompress(opj_jp2_v2_t *jp2, opj_stream_private_t *cio, opj_event_mgr_t * p_manager)
1942 assert(p_manager != 00);
1944 /* customization of the end encoding */
1945 jp2_setup_end_header_reading(jp2);
1948 if (! jp2_exec (jp2,jp2->m_procedure_list,cio,p_manager)) {
1952 return j2k_end_decompress(jp2->j2k, cio, p_manager);
1956 * Ends the compression procedures and possibility add data to be read after the
1959 opj_bool jp2_end_compress( opj_jp2_v2_t *jp2,
1960 opj_stream_private_t *cio,
1961 opj_event_mgr_t * p_manager)
1966 assert(p_manager != 00);
1968 /* customization of the end encoding */
1969 jp2_setup_end_header_writting(jp2);
1971 if (! j2k_end_compress(jp2->j2k,cio,p_manager)) {
1976 return jp2_exec(jp2,jp2->m_procedure_list,cio,p_manager);
1981 * Sets up the procedures to do on writing header after the codestream.
1982 * Developers wanting to extend the library can add their own writing procedures.
1984 void jp2_setup_end_header_writting (opj_jp2_v2_t *jp2)
1989 opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)jp2_write_jp2c_v2 );
1990 /* DEVELOPER CORNER, add your custom procedures */
1994 * Sets up the procedures to do on reading header after the codestream.
1995 * Developers wanting to extend the library can add their own writing procedures.
1997 void jp2_setup_end_header_reading (opj_jp2_v2_t *jp2)
2001 opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)jp2_read_header_procedure );
2002 /* DEVELOPER CORNER, add your custom procedures */
2006 * The default validation procedure without any extension.
2008 * @param jp2 the jpeg2000 codec to validate.
2009 * @param cio the input stream to validate.
2010 * @param p_manager the user event manager.
2012 * @return true if the parameters are correct.
2014 opj_bool jp2_default_validation ( opj_jp2_v2_t * jp2,
2015 opj_stream_private_t *cio,
2016 opj_event_mgr_t * p_manager )
2018 opj_bool l_is_valid = OPJ_TRUE;
2024 assert(p_manager != 00);
2026 /* JPEG2000 codec validation */
2029 /* STATE checking */
2030 /* make sure the state is at 0 */
2031 l_is_valid &= (jp2->jp2_state == JP2_STATE_NONE);
2033 /* make sure not reading a jp2h ???? WEIRD */
2034 l_is_valid &= (jp2->jp2_img_state == JP2_IMG_STATE_NONE);
2036 /* POINTER validation */
2037 /* make sure a j2k codec is present */
2038 l_is_valid &= (jp2->j2k != 00);
2040 /* make sure a procedure list is present */
2041 l_is_valid &= (jp2->m_procedure_list != 00);
2043 /* make sure a validation list is present */
2044 l_is_valid &= (jp2->m_validation_list != 00);
2046 /* PARAMETER VALIDATION */
2047 /* number of components */
2048 l_is_valid &= (jp2->numcl > 0);
2050 l_is_valid &= (jp2->h > 0);
2052 l_is_valid &= (jp2->w > 0);
2054 for (i = 0; i < jp2->numcomps; ++i) {
2055 l_is_valid &= (jp2->comps[i].bpcc > 0);
2059 l_is_valid &= ((jp2->meth > 0) && (jp2->meth < 3));
2061 /* stream validation */
2062 /* back and forth is needed */
2063 l_is_valid &= opj_stream_has_seek(cio);
2069 * Reads a jpeg2000 file header structure.
2071 * @param cio the stream to read data from.
2072 * @param jp2 the jpeg2000 file header structure.
2073 * @param p_manager the user event manager.
2075 * @return true if the box is valid.
2077 opj_bool jp2_read_header_procedure(
2079 opj_stream_private_t *cio,
2080 opj_event_mgr_t * p_manager)
2083 OPJ_UINT32 l_nb_bytes_read;
2084 const opj_jp2_header_handler_t * l_current_handler;
2085 OPJ_UINT32 l_last_data_size = BOX_SIZE;
2086 OPJ_UINT32 l_current_data_size;
2087 unsigned char * l_current_data = 00;
2092 assert(p_manager != 00);
2094 l_current_data = (unsigned char*)opj_malloc(l_last_data_size);
2096 if (l_current_data == 00) {
2097 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to handle jpeg2000 file header\n");
2100 memset(l_current_data, 0 , l_last_data_size);
2102 while (jp2_read_boxhdr_v2(&box,&l_nb_bytes_read,cio,p_manager)) {
2103 /* is it the codestream box ? */
2104 if (box.type == JP2_JP2C) {
2105 if (jp2->jp2_state & JP2_STATE_HEADER) {
2106 jp2->jp2_state |= JP2_STATE_CODESTREAM;
2107 opj_free(l_current_data);
2111 opj_event_msg_v2(p_manager, EVT_ERROR, "bad placed jpeg codestream\n");
2112 opj_free(l_current_data);
2116 else if (box.length == 0) {
2117 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot handle box of undefined sizes\n");
2118 opj_free(l_current_data);
2122 l_current_handler = jp2_find_handler(box.type);
2123 l_current_data_size = box.length - l_nb_bytes_read;
2125 if (l_current_handler != 00) {
2126 if (l_current_data_size > l_last_data_size) {
2127 l_current_data = (unsigned char*)opj_realloc(l_current_data,l_current_data_size);
2128 if (!l_current_data){
2129 opj_free(l_current_data);
2132 l_last_data_size = l_current_data_size;
2135 l_nb_bytes_read = opj_stream_read_data(cio,l_current_data,l_current_data_size,p_manager);
2136 if (l_nb_bytes_read != l_current_data_size) {
2137 opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with reading JPEG2000 box, stream error\n");
2141 if (! l_current_handler->handler(jp2,l_current_data,l_current_data_size,p_manager)) {
2142 opj_free(l_current_data);
2147 jp2->jp2_state |= JP2_STATE_UNKNOWN;
2148 if (opj_stream_skip(cio,l_current_data_size,p_manager) != l_current_data_size) {
2149 opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with skipping JPEG2000 box, stream error\n");
2150 opj_free(l_current_data);
2156 opj_free(l_current_data);
2162 * Excutes the given procedures on the given codec.
2164 * @param p_procedure_list the list of procedures to execute
2165 * @param jp2 the jpeg2000 file codec to execute the procedures on.
2166 * @param cio the stream to execute the procedures on.
2167 * @param p_manager the user manager.
2169 * @return true if all the procedures were successfully executed.
2173 opj_procedure_list_t * p_procedure_list,
2174 opj_stream_private_t *cio,
2175 opj_event_mgr_t * p_manager
2178 opj_bool (** l_procedure) (opj_jp2_v2_t * jp2, opj_stream_private_t *, opj_event_mgr_t *) = 00;
2179 opj_bool l_result = OPJ_TRUE;
2180 OPJ_UINT32 l_nb_proc, i;
2183 assert(p_procedure_list != 00);
2186 assert(p_manager != 00);
2188 l_nb_proc = opj_procedure_list_get_nb_procedures(p_procedure_list);
2189 l_procedure = (opj_bool (**) (opj_jp2_v2_t * jp2, opj_stream_private_t *, opj_event_mgr_t *)) opj_procedure_list_get_first_procedure(p_procedure_list);
2191 for (i=0;i<l_nb_proc;++i) {
2192 l_result = l_result && (*l_procedure) (jp2,cio,p_manager);
2196 /* and clear the procedure list at the end. */
2197 opj_procedure_list_clear(p_procedure_list);
2202 * Starts a compression scheme, i.e. validates the codec parameters, writes the header.
2204 * @param jp2 the jpeg2000 file codec.
2205 * @param cio the stream object.
2207 * @return true if the codec is valid.
2209 opj_bool jp2_start_compress(opj_jp2_v2_t *jp2,
2210 struct opj_stream_private *cio,
2211 opj_image_t * p_image,
2212 struct opj_event_mgr * p_manager)
2217 assert(p_manager != 00);
2219 /* customization of the validation */
2220 jp2_setup_encoding_validation (jp2);
2222 /* validation of the parameters codec */
2223 if (! jp2_exec(jp2,jp2->m_validation_list,cio,p_manager)) {
2227 /* customization of the encoding */
2228 jp2_setup_header_writting(jp2);
2231 if (! jp2_exec (jp2,jp2->m_procedure_list,cio,p_manager)) {
2235 return j2k_start_compress(jp2->j2k,cio,p_image,p_manager);
2239 * Finds the execution function related to the given box id.
2241 * @param p_id the id of the handler to fetch.
2243 * @return the given handler or 00 if it could not be found.
2245 const opj_jp2_header_handler_t * jp2_find_handler (int p_id)
2247 OPJ_UINT32 i, l_handler_size = sizeof(jp2_header) / sizeof(opj_jp2_header_handler_t);
2249 for (i=0;i<l_handler_size;++i) {
2250 if (jp2_header[i].id == p_id) {
2251 return &jp2_header[i];
2258 * Finds the image execution function related to the given box id.
2260 * @param p_id the id of the handler to fetch.
2262 * @return the given handler or 00 if it could not be found.
2264 static const opj_jp2_header_handler_t * jp2_img_find_handler (
2268 OPJ_UINT32 i, l_handler_size = sizeof(jp2_img_header) / sizeof(opj_jp2_header_handler_t);
2269 for (i=0;i<l_handler_size;++i)
2271 if (jp2_img_header[i].id == p_id) {
2272 return &jp2_img_header[i];
2281 * Reads a jpeg2000 file signature box.
2283 * @param p_header_data the data contained in the signature box.
2284 * @param jp2 the jpeg2000 file codec.
2285 * @param p_header_size the size of the data contained in the signature box.
2286 * @param p_manager the user event manager.
2288 * @return true if the file signature box is valid.
2290 static opj_bool opj_jp2_read_jp(opj_jp2_v2_t *jp2,
2291 OPJ_BYTE * p_header_data,
2292 OPJ_UINT32 p_header_size,
2293 opj_event_mgr_t * p_manager
2297 unsigned int l_magic_number;
2300 assert(p_header_data != 00);
2302 assert(p_manager != 00);
2304 if (jp2->jp2_state != JP2_STATE_NONE) {
2305 opj_event_msg_v2(p_manager, EVT_ERROR, "The signature box must be the first box in the file.\n");
2309 /* assure length of data is correct (4 -> magic number) */
2310 if (p_header_size != 4) {
2311 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with JP signature Box size\n");
2315 /* rearrange data */
2316 opj_read_bytes(p_header_data,&l_magic_number,4);
2317 if (l_magic_number != 0x0d0a870a ) {
2318 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with JP Signature : bad magic number\n");
2322 jp2->jp2_state |= JP2_STATE_SIGNATURE;
2329 * Reads a a FTYP box - File type box
2331 * @param p_header_data the data contained in the FTYP box.
2332 * @param jp2 the jpeg2000 file codec.
2333 * @param p_header_size the size of the data contained in the FTYP box.
2334 * @param p_manager the user event manager.
2336 * @return true if the FTYP box is valid.
2338 static opj_bool opj_jp2_read_ftyp( opj_jp2_v2_t *jp2,
2339 OPJ_BYTE * p_header_data,
2340 OPJ_UINT32 p_header_size,
2341 opj_event_mgr_t * p_manager
2344 OPJ_UINT32 i, l_remaining_bytes;
2347 assert(p_header_data != 00);
2349 assert(p_manager != 00);
2351 if (jp2->jp2_state != JP2_STATE_SIGNATURE) {
2352 opj_event_msg_v2(p_manager, EVT_ERROR, "The ftyp box must be the second box in the file.\n");
2356 /* assure length of data is correct */
2357 if (p_header_size < 8) {
2358 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with FTYP signature Box size\n");
2362 opj_read_bytes(p_header_data,&jp2->brand,4); /* BR */
2365 opj_read_bytes(p_header_data,&jp2->minversion,4); /* MinV */
2368 l_remaining_bytes = p_header_size - 8;
2370 /* the number of remaining bytes should be a multiple of 4 */
2371 if ((l_remaining_bytes & 0x3) != 0) {
2372 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with FTYP signature Box size\n");
2377 jp2->numcl = l_remaining_bytes >> 2;
2379 jp2->cl = (unsigned int *) opj_malloc(jp2->numcl * sizeof(unsigned int));
2380 if (jp2->cl == 00) {
2381 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory with FTYP Box\n");
2384 memset(jp2->cl,0,jp2->numcl * sizeof(unsigned int));
2387 for (i = 0; i < jp2->numcl; ++i)
2389 opj_read_bytes(p_header_data,&jp2->cl[i],4); /* CLi */
2393 jp2->jp2_state |= JP2_STATE_FILE_TYPE;
2399 * Skips the Jpeg2000 Codestream Header box - JP2C Header box.
2401 * @param cio the stream to write data to.
2402 * @param jp2 the jpeg2000 file codec.
2403 * @param p_manager user event manager.
2405 * @return true if writting was successful.
2407 opj_bool jp2_skip_jp2c( opj_jp2_v2_t *jp2,
2408 struct opj_stream_private *stream,
2409 struct opj_event_mgr * p_manager )
2413 assert(stream != 00);
2414 assert(p_manager != 00);
2416 jp2->j2k_codestream_offset = opj_stream_tell(stream);
2418 if (opj_stream_skip(stream,8,p_manager) != 8) {
2426 * Reads the Jpeg2000 file Header box - JP2 Header box (warning, this is a super box).
2428 * @param p_header_data the data contained in the file header box.
2429 * @param jp2 the jpeg2000 file codec.
2430 * @param p_header_size the size of the data contained in the file header box.
2431 * @param p_manager the user event manager.
2433 * @return true if the JP2 Header box was successfully reconized.
2435 static opj_bool opj_jp2_read_jp2h( opj_jp2_v2_t *jp2,
2436 OPJ_BYTE *p_header_data,
2437 OPJ_UINT32 p_header_size,
2438 opj_event_mgr_t * p_manager
2441 OPJ_UINT32 l_box_size=0, l_current_data_size = 0;
2443 const opj_jp2_header_handler_t * l_current_handler;
2446 assert(p_header_data != 00);
2448 assert(p_manager != 00);
2450 /* make sure the box is well placed */
2451 if ((jp2->jp2_state & JP2_STATE_FILE_TYPE) != JP2_STATE_FILE_TYPE ) {
2452 opj_event_msg_v2(p_manager, EVT_ERROR, "The box must be the first box in the file.\n");
2456 jp2->jp2_img_state = JP2_IMG_STATE_NONE;
2458 /* iterate while remaining data */
2459 while (p_header_size > 0) {
2461 if (! jp2_read_boxhdr_char(&box,p_header_data,&l_box_size,p_header_size, p_manager)) {
2462 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream error while reading JP2 Header box\n");
2466 if (box.length > p_header_size) {
2467 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream error while reading JP2 Header box: box length is inconsistent.\n");
2471 l_current_handler = jp2_img_find_handler(box.type);
2472 l_current_data_size = box.length - l_box_size;
2473 p_header_data += l_box_size;
2475 if (l_current_handler != 00) {
2476 if (! l_current_handler->handler(jp2,p_header_data,l_current_data_size,p_manager)) {
2481 jp2->jp2_img_state |= JP2_IMG_STATE_UNKNOWN;
2484 p_header_data += l_current_data_size;
2485 p_header_size -= box.length;
2488 jp2->jp2_state |= JP2_STATE_HEADER;
2494 * Reads a box header. The box is the way data is packed inside a jpeg2000 file structure. Data is read from a character string
2496 * @param p_data the character string to read data from.
2497 * @param box the box structure to fill.
2498 * @param p_number_bytes_read pointer to an int that will store the number of bytes read from the stream (shoul usually be 2).
2499 * @param p_box_max_size the maximum number of bytes in the box.
2501 * @return true if the box is reconized, false otherwise
2503 static opj_bool jp2_read_boxhdr_char(
2506 OPJ_UINT32 * p_number_bytes_read,
2507 OPJ_UINT32 p_box_max_size,
2508 opj_event_mgr_t * p_manager
2514 assert(p_data != 00);
2516 assert(p_number_bytes_read != 00);
2517 assert(p_manager != 00);
2519 if (p_box_max_size < 8) {
2520 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot handle box of less than 8 bytes\n");
2524 /* process read data */
2525 opj_read_bytes(p_data, &l_value, 4);
2527 box->length = (OPJ_INT32)(l_value);
2529 opj_read_bytes(p_data, &l_value, 4);
2531 box->type = (OPJ_INT32)(l_value);
2533 *p_number_bytes_read = 8;
2535 /* do we have a "special very large box ?" */
2536 /* read then the XLBox */
2537 if (box->length == 1) {
2538 unsigned int l_xl_part_size;
2540 if (p_box_max_size < 16) {
2541 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot handle XL box of less than 16 bytes\n");
2545 opj_read_bytes(p_data,&l_xl_part_size, 4);
2547 *p_number_bytes_read += 4;
2549 if (l_xl_part_size != 0) {
2550 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot handle box sizes higher than 2^32\n");
2554 opj_read_bytes(p_data, &l_value, 4);
2555 *p_number_bytes_read += 4;
2556 box->length = (OPJ_INT32)(l_value);
2558 if (box->length == 0) {
2559 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot handle box of undefined sizes\n");
2563 else if (box->length == 0) {
2564 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot handle box of undefined sizes\n");
2573 * Reads a jpeg2000 file header structure.
2575 * @param cio the stream to read data from.
2576 * @param jp2 the jpeg2000 file header structure.
2577 * @param p_manager the user event manager.
2579 * @return true if the box is valid.
2581 opj_bool jp2_read_header( struct opj_stream_private *p_stream,
2583 opj_image_t** p_image,
2584 struct opj_event_mgr * p_manager
2589 assert(p_stream != 00);
2590 assert(p_manager != 00);
2592 /* customization of the validation */
2593 jp2_setup_decoding_validation (jp2);
2595 /* customization of the encoding */
2596 jp2_setup_header_reading(jp2);
2598 /* validation of the parameters codec */
2599 if (! jp2_exec(jp2,jp2->m_validation_list,p_stream,p_manager)) {
2604 if (! jp2_exec (jp2,jp2->m_procedure_list,p_stream,p_manager)) {
2608 return j2k_read_header( p_stream,
2615 * Sets up the validation ,i.e. adds the procedures to launch to make sure the codec parameters
2616 * are valid. Developers wanting to extend the library can add their own validation procedures.
2618 void jp2_setup_encoding_validation (opj_jp2_v2_t *jp2)
2623 opj_procedure_list_add_procedure(jp2->m_validation_list, (opj_procedure)jp2_default_validation);
2624 /* DEVELOPER CORNER, add your custom validation procedure */
2628 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
2629 * are valid. Developpers wanting to extend the library can add their own validation procedures.
2631 void jp2_setup_decoding_validation (opj_jp2_v2_t *jp2)
2635 /* DEVELOPER CORNER, add your custom validation procedure */
2639 * Sets up the procedures to do on writting header.
2640 * Developers wanting to extend the library can add their own writing procedures.
2642 void jp2_setup_header_writting (opj_jp2_v2_t *jp2)
2647 opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)jp2_write_jp_v2 );
2648 opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)jp2_write_ftyp_v2 );
2649 opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)jp2_write_jp2h_v2 );
2650 opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)jp2_skip_jp2c );
2652 /* DEVELOPER CORNER, insert your custom procedures */
2657 * Sets up the procedures to do on reading header.
2658 * Developpers wanting to extend the library can add their own writting procedures.
2660 void jp2_setup_header_reading (opj_jp2_v2_t *jp2)
2665 opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)jp2_read_header_procedure );
2666 /* DEVELOPER CORNER, add your custom procedures */
2671 * Reads a tile header.
2672 * @param p_j2k the jpeg2000 codec.
2673 * @param p_stream the stream to write data to.
2674 * @param p_manager the user event manager.
2676 opj_bool jp2_read_tile_header( opj_jp2_v2_t * p_jp2,
2677 OPJ_UINT32 * p_tile_index,
2678 OPJ_UINT32 * p_data_size,
2679 OPJ_INT32 * p_tile_x0, OPJ_INT32 * p_tile_y0,
2680 OPJ_INT32 * p_tile_x1, OPJ_INT32 * p_tile_y1,
2681 OPJ_UINT32 * p_nb_comps,
2683 opj_stream_private_t *p_stream,
2684 opj_event_mgr_t * p_manager )
2686 return j2k_read_tile_header(p_jp2->j2k,
2689 p_tile_x0, p_tile_y0,
2690 p_tile_x1, p_tile_y1,
2699 * @param p_j2k the jpeg2000 codec.
2700 * @param p_stream the stream to write data to.
2701 * @param p_manager the user event manager.
2703 opj_bool jp2_write_tile ( opj_jp2_v2_t *p_jp2,
2704 OPJ_UINT32 p_tile_index,
2706 OPJ_UINT32 p_data_size,
2707 struct opj_stream_private *p_stream,
2708 struct opj_event_mgr * p_manager )
2710 return j2k_write_tile (p_jp2->j2k,p_tile_index,p_data,p_data_size,p_stream,p_manager);
2715 * @param p_j2k the jpeg2000 codec.
2716 * @param p_stream the stream to write data to.
2717 * @param p_manager the user event manager.
2719 opj_bool jp2_decode_tile (
2720 opj_jp2_v2_t * p_jp2,
2721 OPJ_UINT32 p_tile_index,
2723 OPJ_UINT32 p_data_size,
2724 opj_stream_private_t *p_stream,
2725 opj_event_mgr_t * p_manager
2728 return j2k_decode_tile (p_jp2->j2k,p_tile_index,p_data,p_data_size,p_stream,p_manager);
2732 * Destroys a jpeg2000 file decompressor.
2734 * @param jp2 a jpeg2000 file decompressor.
2736 void jp2_destroy(opj_jp2_v2_t *jp2)
2739 /* destroy the J2K codec */
2740 j2k_destroy(jp2->j2k);
2744 opj_free(jp2->comps);
2753 if (jp2->color.icc_profile_buf) {
2754 opj_free(jp2->color.icc_profile_buf);
2755 jp2->color.icc_profile_buf = 00;
2758 if (jp2->color.jp2_cdef) {
2759 if (jp2->color.jp2_cdef->info) {
2760 opj_free(jp2->color.jp2_cdef->info);
2761 jp2->color.jp2_cdef->info = NULL;
2764 opj_free(jp2->color.jp2_cdef);
2765 jp2->color.jp2_cdef = 00;
2768 if (jp2->color.jp2_pclr) {
2769 if (jp2->color.jp2_pclr->cmap) {
2770 opj_free(jp2->color.jp2_pclr->cmap);
2771 jp2->color.jp2_pclr->cmap = NULL;
2773 if (jp2->color.jp2_pclr->channel_sign) {
2774 opj_free(jp2->color.jp2_pclr->channel_sign);
2775 jp2->color.jp2_pclr->channel_sign = NULL;
2777 if (jp2->color.jp2_pclr->channel_size) {
2778 opj_free(jp2->color.jp2_pclr->channel_size);
2779 jp2->color.jp2_pclr->channel_size = NULL;
2781 if (jp2->color.jp2_pclr->entries) {
2782 opj_free(jp2->color.jp2_pclr->entries);
2783 jp2->color.jp2_pclr->entries = NULL;
2786 opj_free(jp2->color.jp2_pclr);
2787 jp2->color.jp2_pclr = 00;
2790 if (jp2->m_validation_list) {
2791 opj_procedure_list_destroy(jp2->m_validation_list);
2792 jp2->m_validation_list = 00;
2795 if (jp2->m_procedure_list) {
2796 opj_procedure_list_destroy(jp2->m_procedure_list);
2797 jp2->m_procedure_list = 00;
2805 * Sets the given area to be decoded. This function should be called right after opj_read_header and before any tile header reading.
2807 * @param p_jp2 the jpeg2000 codec.
2808 * @param p_end_x the right position of the rectangle to decode (in image coordinates).
2809 * @param p_start_y the up position of the rectangle to decode (in image coordinates).
2810 * @param p_end_y the bottom position of the rectangle to decode (in image coordinates).
2811 * @param p_manager the user event manager
2813 * @return true if the area could be set.
2815 opj_bool jp2_set_decode_area( opj_jp2_v2_t *p_jp2,
2816 opj_image_t* p_image,
2817 OPJ_INT32 p_start_x, OPJ_INT32 p_start_y,
2818 OPJ_INT32 p_end_x, OPJ_INT32 p_end_y,
2819 struct opj_event_mgr * p_manager )
2821 return j2k_set_decode_area(p_jp2->j2k, p_image, p_start_x, p_start_y, p_end_x, p_end_y, p_manager);
2825 * Get the decoded tile.
2827 * @param jp2 the jpeg2000 codec.
2828 * @param p_stream input_stream
2829 * @param p_image output image. .
2830 * @param p_manager the user event manager
2831 * @param tile_index index of the tile we want decode
2833 * @return true if succeed.
2835 opj_bool jp2_get_tile( opj_jp2_v2_t *jp2,
2836 opj_stream_private_t *p_stream,
2837 opj_image_t* p_image,
2838 struct opj_event_mgr * p_manager,
2839 OPJ_UINT32 tile_index )
2844 opj_event_msg_v2(p_manager, EVT_WARNING, "JP2 box which are after the codestream will not be read by this function.\n");
2846 if (! j2k_get_tile(jp2->j2k, p_stream, p_image, p_manager, tile_index) ){
2847 opj_event_msg_v2(p_manager, EVT_ERROR, "Failed to decode the codestream in the JP2 file\n");
2851 /* Set Image Color Space */
2852 if (jp2->enumcs == 16)
2853 p_image->color_space = CLRSPC_SRGB;
2854 else if (jp2->enumcs == 17)
2855 p_image->color_space = CLRSPC_GRAY;
2856 else if (jp2->enumcs == 18)
2857 p_image->color_space = CLRSPC_SYCC;
2859 p_image->color_space = CLRSPC_UNKNOWN;
2861 /* Apply the color space if needed */
2862 if(jp2->color.jp2_cdef) {
2863 jp2_apply_cdef(p_image, &(jp2->color));
2866 if(jp2->color.jp2_pclr) {
2867 /* Part 1, I.5.3.4: Either both or none : */
2868 if( !jp2->color.jp2_pclr->cmap)
2869 jp2_free_pclr(&(jp2->color));
2871 jp2_apply_pclr(p_image, &(jp2->color));
2874 if(jp2->color.icc_profile_buf) {
2875 p_image->icc_profile_buf = jp2->color.icc_profile_buf;
2876 p_image->icc_profile_len = jp2->color.icc_profile_len;
2877 jp2->color.icc_profile_buf = NULL;
2885 /* ----------------------------------------------------------------------- */
2886 /* JP2 encoder interface */
2887 /* ----------------------------------------------------------------------- */
2889 opj_jp2_v2_t* jp2_create(opj_bool p_is_decoder)
2891 opj_jp2_v2_t *jp2 = (opj_jp2_v2_t*)opj_malloc(sizeof(opj_jp2_v2_t));
2893 memset(jp2,0,sizeof(opj_jp2_v2_t));
2895 /* create the J2K codec */
2896 if (! p_is_decoder) {
2897 jp2->j2k = j2k_create_compress_v2();
2900 jp2->j2k = opj_j2k_create_decompress();
2903 if (jp2->j2k == 00) {
2908 /* Color structure */
2909 jp2->color.icc_profile_buf = NULL;
2910 jp2->color.icc_profile_len = 0;
2911 jp2->color.jp2_cdef = NULL;
2912 jp2->color.jp2_pclr = NULL;
2913 jp2->color.jp2_has_colr = 0;
2915 /* validation list creation */
2916 jp2->m_validation_list = opj_procedure_list_create();
2917 if (! jp2->m_validation_list) {
2922 /* execution list creation */
2923 jp2->m_procedure_list = opj_procedure_list_create();
2924 if (! jp2->m_procedure_list) {
2933 void jp2_dump(opj_jp2_v2_t* p_jp2, OPJ_INT32 flag, FILE* out_stream)
2936 assert(p_jp2 != 00);
2938 j2k_dump(p_jp2->j2k,
2943 opj_codestream_index_t* jp2_get_cstr_index(opj_jp2_v2_t* p_jp2)
2945 return j2k_get_cstr_index(p_jp2->j2k);
2948 opj_codestream_info_v2_t* jp2_get_cstr_info(opj_jp2_v2_t* p_jp2)
2950 return j2k_get_cstr_info(p_jp2->j2k);
2953 opj_bool jp2_set_decoded_resolution_factor(opj_jp2_v2_t *p_jp2, OPJ_UINT32 res_factor, opj_event_mgr_t * p_manager)
2955 return j2k_set_decoded_resolution_factor(p_jp2->j2k, res_factor, p_manager);