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);*/
52 Read the IHDR box - Image Header box
54 @param cio Input buffer stream
55 @return Returns true if successful, returns false otherwise
57 static opj_bool jp2_read_ihdr(opj_jp2_t *jp2, opj_cio_t *cio);
60 * Reads a IHDR box - Image Header box
62 * @param p_image_header_data pointer to actual data (already read from file)
63 * @param jp2 the jpeg2000 file codec.
64 * @param p_image_header_size the size of the image header
65 * @param p_manager the user event manager.
67 * @return true if the image header is valid, fale else.
69 static opj_bool jp2_read_ihdr_v2(
71 unsigned char * p_image_header_data,
72 unsigned int p_image_header_size,
73 struct opj_event_mgr * p_manager
76 static void jp2_write_ihdr(opj_jp2_t *jp2, opj_cio_t *cio);
77 static void jp2_write_bpcc(opj_jp2_t *jp2, opj_cio_t *cio);
78 static opj_bool jp2_read_bpcc(opj_jp2_t *jp2, opj_cio_t *cio);
81 * Reads a Bit per Component box.
83 * @param p_bpc_header_data pointer to actual data (already read from file)
84 * @param jp2 the jpeg2000 file codec.
85 * @param p_bpc_header_size the size of the bpc header
86 * @param p_manager the user event manager.
88 * @return true if the bpc header is valid, fale else.
90 static opj_bool jp2_read_bpcc_v2(
92 unsigned char * p_bpc_header_data,
93 unsigned int p_bpc_header_size,
94 struct opj_event_mgr * p_manager
97 static opj_bool jp2_read_cdef_v2( opj_jp2_v2_t * jp2,
98 unsigned char * p_cdef_header_data,
99 OPJ_UINT32 p_cdef_header_size,
100 opj_event_mgr_t * p_manager
103 static void jp2_write_colr(opj_jp2_t *jp2, opj_cio_t *cio);
105 Write the FTYP box - File type box
106 @param jp2 JP2 handle
107 @param cio Output buffer stream
109 static void jp2_write_ftyp(opj_jp2_t *jp2, opj_cio_t *cio);
111 Read the FTYP box - File type box
112 @param jp2 JP2 handle
113 @param cio Input buffer stream
114 @return Returns true if successful, returns false otherwise
116 static opj_bool jp2_read_ftyp(opj_jp2_t *jp2, opj_cio_t *cio);
119 * Reads a a FTYP box - File type box
121 * @param p_header_data the data contained in the FTYP box.
122 * @param jp2 the jpeg2000 file codec.
123 * @param p_header_size the size of the data contained in the FTYP box.
124 * @param p_manager the user event manager.
126 * @return true if the FTYP box is valid.
128 static opj_bool jp2_read_ftyp_v2(
130 unsigned char * p_header_data,
131 unsigned int p_header_size,
132 struct opj_event_mgr * p_manager
136 * Reads the Jpeg2000 file Header box - JP2 Header box (warning, this is a super box).
138 * @param p_header_data the data contained in the file header box.
139 * @param jp2 the jpeg2000 file codec.
140 * @param p_header_size the size of the data contained in the file header box.
141 * @param p_manager the user event manager.
143 * @return true if the JP2 Header box was successfully reconized.
145 static opj_bool jp2_read_jp2h_v2(
147 unsigned char * p_header_data,
148 unsigned int p_header_size,
149 struct opj_event_mgr * p_manager
152 static int jp2_write_jp2c(opj_jp2_t *jp2, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info);
153 static opj_bool jp2_read_jp2c(opj_jp2_t *jp2, opj_cio_t *cio, unsigned int *j2k_codestream_length, unsigned int *j2k_codestream_offset);
154 static void jp2_write_jp(opj_cio_t *cio);
156 Read the JP box - JPEG 2000 signature
157 @param jp2 JP2 handle
158 @param cio Input buffer stream
159 @return Returns true if successful, returns false otherwise
161 static opj_bool jp2_read_jp(opj_jp2_t *jp2, opj_cio_t *cio);
164 * Reads a jpeg2000 file signature box.
166 * @param p_header_data the data contained in the signature box.
167 * @param jp2 the jpeg2000 file codec.
168 * @param p_header_size the size of the data contained in the signature box.
169 * @param p_manager the user event manager.
171 * @return true if the file signature box is valid.
173 static opj_bool jp2_read_jp_v2(
175 unsigned char * p_header_data,
176 unsigned int p_header_size,
177 struct opj_event_mgr * p_manager
181 Decode the structure of a JP2 file
182 @param jp2 JP2 handle
183 @param cio Input buffer stream
184 @param color Collector for profile, cdef and pclr data
185 @return Returns true if successful, returns false otherwise
187 static opj_bool jp2_read_struct(opj_jp2_t *jp2, opj_cio_t *cio,
188 opj_jp2_color_t *color);
190 Apply collected palette data
191 @param color Collector for profile, cdef and pclr data
194 static void jp2_apply_pclr(opj_jp2_color_t *color, opj_image_t *image);
197 @param jp2 JP2 handle
198 @param cio Input buffer stream
200 @param color Collector for profile, cdef and pclr data
201 @return Returns true if successful, returns false otherwise
203 static opj_bool jp2_read_pclr(opj_jp2_t *jp2, opj_cio_t *cio,
204 opj_jp2_box_t *box, opj_jp2_color_t *color);
206 static opj_bool jp2_read_pclr_v2( opj_jp2_v2_t *jp2,
207 unsigned char * p_pclr_header_data,
208 OPJ_UINT32 p_pclr_header_size,
209 opj_event_mgr_t * p_manager
213 Collect component mapping data
214 @param jp2 JP2 handle
215 @param cio Input buffer stream
217 @param color Collector for profile, cdef and pclr data
218 @return Returns true if successful, returns false otherwise
220 static opj_bool jp2_read_cmap(opj_jp2_t *jp2, opj_cio_t *cio,
221 opj_jp2_box_t *box, opj_jp2_color_t *color);
224 static opj_bool jp2_read_cmap_v2( opj_jp2_v2_t * jp2,
225 unsigned char * p_cmap_header_data,
226 OPJ_UINT32 p_cmap_header_size,
227 opj_event_mgr_t * p_manager
231 Collect colour specification data
232 @param jp2 JP2 handle
233 @param cio Input buffer stream
235 @param color Collector for profile, cdef and pclr data
236 @return Returns true if successful, returns false otherwise
238 static opj_bool jp2_read_colr(opj_jp2_t *jp2, opj_cio_t *cio,
239 opj_jp2_box_t *box, opj_jp2_color_t *color);
242 * Reads the Color Specification box.
244 * @param p_colr_header_data pointer to actual data (already read from file)
245 * @param jp2 the jpeg2000 file codec.
246 * @param p_colr_header_size the size of the color header
247 * @param p_manager the user event manager.
249 * @return true if the bpc header is valid, fale else.
251 static opj_bool jp2_read_colr_v2(
253 unsigned char * p_colr_header_data,
254 OPJ_UINT32 p_colr_header_size,
255 struct opj_event_mgr * p_manager
259 Write file Index (superbox)
260 @param[in] offset_jp2c offset of jp2c box
261 @param[in] length_jp2c length of jp2c box
262 @param[in] offset_idx offset of cidx box
263 @param[in] length_idx length of cidx box
264 @param[in] cio file output handle
265 @return length of fidx box
267 static int write_fidx( int offset_jp2c, int length_jp2c, int offset_idx, int length_idx, opj_cio_t *cio);
269 Write index Finder box
270 @param[in] offset offset of fidx box
271 @param[in] length length of fidx box
272 @param[in] cio file output handle
274 static void write_iptr( int offset, int length, opj_cio_t *cio);
278 @param[in] offset_jp2c offset of jp2c box
279 @param[in] length_jp2c length of jp2c box
280 @param[in] offset_idx offset of cidx box
281 @param[in] length_idx length of cidx box
282 @param[in] cio file output handle
284 static void write_prxy( int offset_jp2c, int length_jp2c, int offset_idx, int length_idx, opj_cio_t *cio);
290 * Sets up the procedures to do on reading header after the codestream.
291 * Developpers wanting to extend the library can add their own writting procedures.
293 static void jp2_setup_end_header_reading (opj_jp2_v2_t *jp2);
296 * Reads a jpeg2000 file header structure.
298 * @param cio the stream to read data from.
299 * @param jp2 the jpeg2000 file header structure.
300 * @param p_manager the user event manager.
302 * @return true if the box is valid.
304 opj_bool jp2_read_header_procedure(
306 struct opj_stream_private *cio,
307 struct opj_event_mgr * p_manager
311 * Excutes the given procedures on the given codec.
313 * @param p_procedure_list the list of procedures to execute
314 * @param jp2 the jpeg2000 file codec to execute the procedures on.
315 * @param cio the stream to execute the procedures on.
316 * @param p_manager the user manager.
318 * @return true if all the procedures were successfully executed.
320 static opj_bool jp2_exec (
322 struct opj_procedure_list * p_procedure_list,
323 struct opj_stream_private *cio,
324 struct opj_event_mgr * p_manager
328 * Reads a box header. The box is the way data is packed inside a jpeg2000 file structure.
330 * @param cio the input stream to read data from.
331 * @param box the box structure to fill.
332 * @param p_number_bytes_read pointer to an int that will store the number of bytes read from the stream (shoul usually be 2).
333 * @param p_manager user event manager.
335 * @return true if the box is reconized, false otherwise
337 static opj_bool jp2_read_boxhdr_v2(
339 OPJ_UINT32 * p_number_bytes_read,
340 struct opj_stream_private *cio,
341 struct opj_event_mgr * p_manager
345 * Finds the execution function related to the given box id.
347 * @param p_id the id of the handler to fetch.
349 * @return the given handler or NULL if it could not be found.
351 static const opj_jp2_header_handler_t * jp2_find_handler (int p_id );
354 * Finds the image execution function related to the given box id.
356 * @param p_id the id of the handler to fetch.
358 * @return the given handler or NULL if it could not be found.
360 static const opj_jp2_header_handler_t * jp2_img_find_handler (int p_id);
362 const opj_jp2_header_handler_t jp2_header [] =
364 {JP2_JP,jp2_read_jp_v2},
365 {JP2_FTYP,jp2_read_ftyp_v2},
366 {JP2_JP2H,jp2_read_jp2h_v2}
369 const opj_jp2_header_handler_t jp2_img_header [] =
371 {JP2_IHDR,jp2_read_ihdr_v2},
372 {JP2_COLR,jp2_read_colr_v2},
373 {JP2_BPCC,jp2_read_bpcc_v2},
374 {JP2_PCLR,jp2_read_pclr_v2},
375 {JP2_CMAP,jp2_read_cmap_v2},
376 {JP2_CDEF,jp2_read_cdef_v2}
381 * Reads a box header. The box is the way data is packed inside a jpeg2000 file structure. Data is read from a character string
383 * @param p_data the character string to read data from.
384 * @param box the box structure to fill.
385 * @param p_number_bytes_read pointer to an int that will store the number of bytes read from the stream (shoul usually be 2).
386 * @param p_box_max_size the maximum number of bytes in the box.
388 * @return true if the box is reconized, false otherwise
390 static opj_bool jp2_read_boxhdr_char(
393 OPJ_UINT32 * p_number_bytes_read,
394 OPJ_UINT32 p_box_max_size,
395 struct opj_event_mgr * p_manager
399 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
400 * are valid. Developpers wanting to extend the library can add their own validation procedures.
402 static void jp2_setup_decoding_validation (opj_jp2_v2_t *jp2);
405 * Sets up the procedures to do on reading header.
406 * Developpers wanting to extend the library can add their own writting procedures.
408 static void jp2_setup_header_reading (opj_jp2_v2_t *jp2);
412 /* ----------------------------------------------------------------------- */
414 static opj_bool jp2_read_boxhdr(opj_common_ptr cinfo, opj_cio_t *cio, opj_jp2_box_t *box) {
415 box->init_pos = cio_tell(cio);
416 box->length = cio_read(cio, 4);
417 box->type = cio_read(cio, 4);
418 if (box->length == 1) {
419 if (cio_read(cio, 4) != 0) {
420 opj_event_msg(cinfo, EVT_ERROR, "Cannot handle box sizes higher than 2^32\n");
423 box->length = cio_read(cio, 4);
424 if (box->length == 0)
425 box->length = cio_numbytesleft(cio) + 12;
427 else if (box->length == 0) {
428 box->length = cio_numbytesleft(cio) + 8;
435 * Reads a box header. The box is the way data is packed inside a jpeg2000 file structure.
437 * @param cio the input stream to read data from.
438 * @param box the box structure to fill.
439 * @param p_number_bytes_read pointer to an int that will store the number of bytes read from the stream (should usually be 8).
440 * @param p_manager user event manager.
442 * @return true if the box is reconized, false otherwise
444 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)
446 /* read header from file */
447 unsigned char l_data_header [8];
452 assert(p_number_bytes_read != 00);
453 assert(p_manager != 00);
455 *p_number_bytes_read = opj_stream_read_data(cio,l_data_header,8,p_manager);
456 if (*p_number_bytes_read != 8) {
460 /* process read data */
461 opj_read_bytes(l_data_header,&(box->length), 4);
462 opj_read_bytes(l_data_header+4,&(box->type), 4);
464 // do we have a "special very large box ?"
465 // read then the XLBox
466 if (box->length == 1) {
467 OPJ_UINT32 l_xl_part_size;
469 OPJ_UINT32 l_nb_bytes_read = opj_stream_read_data(cio,l_data_header,8,p_manager);
470 if (l_nb_bytes_read != 8) {
471 if (l_nb_bytes_read > 0) {
472 *p_number_bytes_read += l_nb_bytes_read;
478 opj_read_bytes(l_data_header,&l_xl_part_size, 4);
479 if (l_xl_part_size != 0) {
480 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot handle box sizes higher than 2^32\n");
483 opj_read_bytes(l_data_header,&(box->length), 4);
489 static void jp2_write_url(opj_cio_t *cio, char *Idx_file) {
493 box.init_pos = cio_tell(cio);
495 cio_write(cio, JP2_URL, 4); /* DBTL */
496 cio_write(cio, 0, 1); /* VERS */
497 cio_write(cio, 0, 3); /* FLAG */
500 for (i = 0; i < strlen(Idx_file); i++) {
501 cio_write(cio, Idx_file[i], 1);
505 box.length = cio_tell(cio) - box.init_pos;
506 cio_seek(cio, box.init_pos);
507 cio_write(cio, box.length, 4); /* L */
508 cio_seek(cio, box.init_pos + box.length);
512 static opj_bool jp2_read_ihdr(opj_jp2_t *jp2, opj_cio_t *cio) {
515 opj_common_ptr cinfo = jp2->cinfo;
517 jp2_read_boxhdr(cinfo, cio, &box);
518 if (JP2_IHDR != box.type) {
519 opj_event_msg(cinfo, EVT_ERROR, "Expected IHDR Marker\n");
523 jp2->h = cio_read(cio, 4); /* HEIGHT */
524 jp2->w = cio_read(cio, 4); /* WIDTH */
525 jp2->numcomps = cio_read(cio, 2); /* NC */
526 jp2->comps = (opj_jp2_comps_t*) opj_malloc(jp2->numcomps * sizeof(opj_jp2_comps_t));
528 jp2->bpc = cio_read(cio, 1); /* BPC */
530 jp2->C = cio_read(cio, 1); /* C */
531 jp2->UnkC = cio_read(cio, 1); /* UnkC */
532 jp2->IPR = cio_read(cio, 1); /* IPR */
534 if (cio_tell(cio) - box.init_pos != box.length) {
535 opj_event_msg(cinfo, EVT_ERROR, "Error with IHDR Box\n");
543 * Reads a IHDR box - Image Header box
545 * @param p_image_header_data pointer to actual data (already read from file)
546 * @param jp2 the jpeg2000 file codec.
547 * @param p_image_header_size the size of the image header
548 * @param p_image_header_max_size maximum size of the header, any size bigger than this value should result the function to output false.
549 * @param p_manager the user event manager.
551 * @return true if the image header is valid, fale else.
553 opj_bool jp2_read_ihdr_v2(
555 unsigned char * p_image_header_data,
556 unsigned int p_image_header_size,
557 opj_event_mgr_t * p_manager
561 assert(p_image_header_data != 00);
563 assert(p_manager != 00);
565 if (p_image_header_size != 14) {
566 opj_event_msg_v2(p_manager, EVT_ERROR, "Bad image header box (bad size)\n");
570 opj_read_bytes(p_image_header_data,&(jp2->h),4); /* HEIGHT */
571 p_image_header_data += 4;
572 opj_read_bytes(p_image_header_data,&(jp2->w),4); /* WIDTH */
573 p_image_header_data += 4;
574 opj_read_bytes(p_image_header_data,&(jp2->numcomps),2); /* NC */
575 p_image_header_data += 2;
577 /* allocate memory for components */
578 jp2->comps = (opj_jp2_comps_t*) opj_malloc(jp2->numcomps * sizeof(opj_jp2_comps_t));
579 if (jp2->comps == 0) {
580 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to handle image header (ihdr)\n");
583 memset(jp2->comps,0,jp2->numcomps * sizeof(opj_jp2_comps_t));
585 opj_read_bytes(p_image_header_data,&(jp2->bpc),1); /* BPC */
586 ++ p_image_header_data;
588 // if equal to 0 then need a BPC box (cf. chapter about image header box of the norm)
589 /*if (jp2->bpc == 0){
590 // indicate with a flag that we will wait a BPC box
593 opj_read_bytes(p_image_header_data,&(jp2->C),1); /* C */
594 ++ p_image_header_data;
596 // Should be equal to 7 cf. chapter about image header box of the norm
598 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);
601 opj_read_bytes(p_image_header_data,&(jp2->UnkC),1); /* UnkC */
602 ++ p_image_header_data;
603 opj_read_bytes(p_image_header_data,&(jp2->IPR),1); /* IPR */
604 ++ p_image_header_data;
609 static void jp2_write_ihdr(opj_jp2_t *jp2, opj_cio_t *cio) {
612 box.init_pos = cio_tell(cio);
614 cio_write(cio, JP2_IHDR, 4); /* IHDR */
616 cio_write(cio, jp2->h, 4); /* HEIGHT */
617 cio_write(cio, jp2->w, 4); /* WIDTH */
618 cio_write(cio, jp2->numcomps, 2); /* NC */
620 cio_write(cio, jp2->bpc, 1); /* BPC */
622 cio_write(cio, jp2->C, 1); /* C : Always 7 */
623 cio_write(cio, jp2->UnkC, 1); /* UnkC, colorspace unknown */
624 cio_write(cio, jp2->IPR, 1); /* IPR, no intellectual property */
626 box.length = cio_tell(cio) - box.init_pos;
627 cio_seek(cio, box.init_pos);
628 cio_write(cio, box.length, 4); /* L */
629 cio_seek(cio, box.init_pos + box.length);
632 static void jp2_write_bpcc(opj_jp2_t *jp2, opj_cio_t *cio) {
636 box.init_pos = cio_tell(cio);
638 cio_write(cio, JP2_BPCC, 4); /* BPCC */
640 for (i = 0; i < jp2->numcomps; i++) {
641 cio_write(cio, jp2->comps[i].bpcc, 1);
644 box.length = cio_tell(cio) - box.init_pos;
645 cio_seek(cio, box.init_pos);
646 cio_write(cio, box.length, 4); /* L */
647 cio_seek(cio, box.init_pos + box.length);
651 static opj_bool jp2_read_bpcc(opj_jp2_t *jp2, opj_cio_t *cio) {
655 opj_common_ptr cinfo = jp2->cinfo;
657 jp2_read_boxhdr(cinfo, cio, &box);
658 if (JP2_BPCC != box.type) {
659 opj_event_msg(cinfo, EVT_ERROR, "Expected BPCC Marker\n");
663 for (i = 0; i < jp2->numcomps; i++) {
664 jp2->comps[i].bpcc = cio_read(cio, 1);
667 if (cio_tell(cio) - box.init_pos != box.length) {
668 opj_event_msg(cinfo, EVT_ERROR, "Error with BPCC Box\n");
676 * Reads a Bit per Component box.
678 * @param p_bpc_header_data pointer to actual data (already read from file)
679 * @param jp2 the jpeg2000 file codec.
680 * @param p_bpc_header_size pointer that will hold the size of the bpc header
681 * @param p_bpc_header_max_size maximum size of the header, any size bigger than this value should result the function to output false.
682 * @param p_manager the user event manager.
684 * @return true if the bpc header is valid, fale else.
686 opj_bool jp2_read_bpcc_v2( opj_jp2_v2_t *jp2,
687 unsigned char * p_bpc_header_data,
688 unsigned int p_bpc_header_size,
689 opj_event_mgr_t * p_manager
695 assert(p_bpc_header_data != 00);
697 assert(p_manager != 00);
700 /*if (jp2->bpc != 0 ){
701 opj_event_msg_v2(p_manager, EVT_WARNING, "A BPCC header box is available although BPC is different to zero (%d)\n",jp2->bpc);
704 // and length is relevant
705 if (p_bpc_header_size != jp2->numcomps) {
706 opj_event_msg_v2(p_manager, EVT_ERROR, "Bad BPCC header box (bad size)\n");
710 // read info for each component
711 for (i = 0; i < jp2->numcomps; ++i) {
712 opj_read_bytes(p_bpc_header_data,&jp2->comps[i].bpcc ,1); /* read each BPCC component */
719 static void jp2_write_colr(opj_jp2_t *jp2, opj_cio_t *cio) {
722 box.init_pos = cio_tell(cio);
724 cio_write(cio, JP2_COLR, 4); /* COLR */
726 cio_write(cio, jp2->meth, 1); /* METH */
727 cio_write(cio, jp2->precedence, 1); /* PRECEDENCE */
728 cio_write(cio, jp2->approx, 1); /* APPROX */
733 cio_write(cio, jp2->enumcs, 4); /* EnumCS */
735 box.length = cio_tell(cio) - box.init_pos;
736 cio_seek(cio, box.init_pos);
737 cio_write(cio, box.length, 4); /* L */
738 cio_seek(cio, box.init_pos + box.length);
741 static void jp2_free_pclr(opj_jp2_color_t *color)
743 opj_free(color->jp2_pclr->channel_sign);
744 opj_free(color->jp2_pclr->channel_size);
745 opj_free(color->jp2_pclr->entries);
747 if(color->jp2_pclr->cmap) opj_free(color->jp2_pclr->cmap);
749 opj_free(color->jp2_pclr); color->jp2_pclr = NULL;
752 static void free_color_data(opj_jp2_color_t *color)
756 jp2_free_pclr(color);
760 if(color->jp2_cdef->info) opj_free(color->jp2_cdef->info);
761 opj_free(color->jp2_cdef);
763 if(color->icc_profile_buf) opj_free(color->icc_profile_buf);
766 static void jp2_apply_pclr(opj_jp2_color_t *color, opj_image_t *image)
768 opj_image_comp_t *old_comps, *new_comps;
769 unsigned char *channel_size, *channel_sign;
770 unsigned int *entries;
771 opj_jp2_cmap_comp_t *cmap;
774 unsigned short i, nr_channels, cmp, pcol;
777 channel_size = color->jp2_pclr->channel_size;
778 channel_sign = color->jp2_pclr->channel_sign;
779 entries = color->jp2_pclr->entries;
780 cmap = color->jp2_pclr->cmap;
781 nr_channels = color->jp2_pclr->nr_channels;
783 old_comps = image->comps;
784 new_comps = (opj_image_comp_t*)
785 opj_malloc(nr_channels * sizeof(opj_image_comp_t));
787 for(i = 0; i < nr_channels; ++i)
789 pcol = cmap[i].pcol; cmp = cmap[i].cmp;
791 new_comps[pcol] = old_comps[cmp];
793 if(cmap[i].mtyp == 0) /* Direct use */
795 old_comps[cmp].data = NULL; continue;
797 /* Palette mapping: */
798 new_comps[pcol].data = (int*)
799 opj_malloc(old_comps[cmp].w * old_comps[cmp].h * sizeof(int));
800 new_comps[pcol].prec = channel_size[i];
801 new_comps[pcol].sgnd = channel_sign[i];
803 top_k = color->jp2_pclr->nr_entries - 1;
805 for(i = 0; i < nr_channels; ++i)
808 if(cmap[i].mtyp == 0) continue;
810 /* Palette mapping: */
811 cmp = cmap[i].cmp; pcol = cmap[i].pcol;
812 src = old_comps[cmp].data;
813 dst = new_comps[pcol].data;
814 max = new_comps[pcol].w * new_comps[pcol].h;
816 for(j = 0; j < max; ++j)
819 if((k = src[j]) < 0) k = 0; else if(k > top_k) k = top_k;
821 dst[j] = entries[k * nr_channels + pcol];
824 max = image->numcomps;
825 for(i = 0; i < max; ++i)
827 if(old_comps[i].data) opj_free(old_comps[i].data);
830 image->comps = new_comps;
831 image->numcomps = nr_channels;
833 jp2_free_pclr(color);
837 static opj_bool jp2_read_pclr(opj_jp2_t *jp2, opj_cio_t *cio,
838 opj_jp2_box_t *box, opj_jp2_color_t *color)
840 opj_jp2_pclr_t *jp2_pclr;
841 unsigned char *channel_size, *channel_sign;
842 unsigned int *entries;
843 unsigned short nr_entries, nr_channels;
847 OPJ_ARG_NOT_USED(box);
848 OPJ_ARG_NOT_USED(jp2);
850 /* Part 1, I.5.3.4: 'There shall be at most one Palette box inside
851 * a JP2 Header box' :
853 if(color->jp2_pclr) return OPJ_FALSE;
855 nr_entries = (unsigned short)cio_read(cio, 2); /* NE */
856 nr_channels = (unsigned short)cio_read(cio, 1);/* NPC */
858 entries = (unsigned int*)
859 opj_malloc(nr_channels * nr_entries * sizeof(unsigned int));
860 channel_size = (unsigned char*)opj_malloc(nr_channels);
861 channel_sign = (unsigned char*)opj_malloc(nr_channels);
863 jp2_pclr = (opj_jp2_pclr_t*)opj_malloc(sizeof(opj_jp2_pclr_t));
864 jp2_pclr->channel_sign = channel_sign;
865 jp2_pclr->channel_size = channel_size;
866 jp2_pclr->entries = entries;
867 jp2_pclr->nr_entries = nr_entries;
868 jp2_pclr->nr_channels = nr_channels;
869 jp2_pclr->cmap = NULL;
871 color->jp2_pclr = jp2_pclr;
873 for(i = 0; i < nr_channels; ++i)
875 uc = cio_read(cio, 1); /* Bi */
876 channel_size[i] = (uc & 0x7f) + 1;
877 channel_sign[i] = (uc & 0x80)?1:0;
880 for(j = 0; j < nr_entries; ++j)
882 for(i = 0; i < nr_channels; ++i)
885 *entries++ = cio_read(cio, channel_size[i]>>3);
890 }/* jp2_read_pclr() */
893 * Reads a palette box.
895 * @param p_bpc_header_data pointer to actual data (already read from file)
896 * @param jp2 the jpeg2000 file codec.
897 * @param p_bpc_header_size pointer that will hold the size of the bpc header
898 * @param p_bpc_header_max_size maximum size of the header, any size bigger than this value should result the function to output false.
899 * @param p_manager the user event manager.
901 * @return true if the bpc header is valid, fale else.
903 opj_bool jp2_read_pclr_v2( opj_jp2_v2_t *jp2,
904 unsigned char * p_pclr_header_data,
905 OPJ_UINT32 p_pclr_header_size,
906 opj_event_mgr_t * p_manager
908 opj_jp2_pclr_t *jp2_pclr;
909 OPJ_BYTE *channel_size, *channel_sign;
911 OPJ_UINT16 nr_entries,nr_channels;
916 assert(p_pclr_header_data != 00);
918 assert(p_manager != 00);
920 if(jp2->color.jp2_pclr)
923 opj_read_bytes(p_pclr_header_data, &l_value , 2); /* NE */
924 p_pclr_header_data += 2;
925 nr_entries = (OPJ_UINT16) l_value;
927 opj_read_bytes(p_pclr_header_data, &l_value , 1); /* NPC */
928 ++p_pclr_header_data;
929 nr_channels = (OPJ_UINT16) l_value;
931 entries = (OPJ_UINT32*) opj_malloc(nr_channels * nr_entries * sizeof(OPJ_UINT32));
932 channel_size = (OPJ_BYTE*) opj_malloc(nr_channels);
933 channel_sign = (OPJ_BYTE*) opj_malloc(nr_channels);
935 jp2_pclr = (opj_jp2_pclr_t*)opj_malloc(sizeof(opj_jp2_pclr_t));
936 jp2_pclr->channel_sign = channel_sign;
937 jp2_pclr->channel_size = channel_size;
938 jp2_pclr->entries = entries;
939 jp2_pclr->nr_entries = nr_entries;
940 jp2_pclr->nr_channels = nr_channels;
941 jp2_pclr->cmap = NULL;
943 jp2->color.jp2_pclr = jp2_pclr;
945 for(i = 0; i < nr_channels; ++i) {
946 opj_read_bytes(p_pclr_header_data, &l_value , 1); /* Bi */
947 ++p_pclr_header_data;
949 channel_size[i] = (l_value & 0x7f) + 1;
950 channel_sign[i] = (l_value & 0x80)? 1 : 0;
953 for(j = 0; j < nr_entries; ++j) {
954 for(i = 0; i < nr_channels; ++i) {
955 //*entries++ = cio_read(cio, channel_size[i]>>3);
956 opj_read_bytes(p_pclr_header_data, &l_value , channel_size[i]>>3); /* Cji */
957 p_pclr_header_data += channel_size[i]>>3;
958 *entries = (OPJ_UINT32) l_value;
967 static opj_bool jp2_read_cmap(opj_jp2_t *jp2, opj_cio_t *cio,
968 opj_jp2_box_t *box, opj_jp2_color_t *color)
970 opj_jp2_cmap_comp_t *cmap;
971 unsigned short i, nr_channels;
973 OPJ_ARG_NOT_USED(box);
974 OPJ_ARG_NOT_USED(jp2);
976 /* Need nr_channels: */
977 if(color->jp2_pclr == NULL) return OPJ_FALSE;
979 /* Part 1, I.5.3.5: 'There shall be at most one Component Mapping box
980 * inside a JP2 Header box' :
982 if(color->jp2_pclr->cmap) return OPJ_FALSE;
984 nr_channels = color->jp2_pclr->nr_channels;
985 cmap = (opj_jp2_cmap_comp_t*)
986 opj_malloc(nr_channels * sizeof(opj_jp2_cmap_comp_t));
988 for(i = 0; i < nr_channels; ++i)
990 cmap[i].cmp = (unsigned short)cio_read(cio, 2);
991 cmap[i].mtyp = cio_read(cio, 1);
992 cmap[i].pcol = cio_read(cio, 1);
995 color->jp2_pclr->cmap = cmap;
999 }/* jp2_read_cmap() */
1002 * Reads the Component Mapping box.
1004 * @param p_cmap_header_data pointer to actual data (already read from file)
1005 * @param jp2 the jpeg2000 file codec.
1006 * @param p_cmap_header_size pointer that will hold the size of the color header
1007 * @param p_manager the user event manager.
1009 * @return true if the cdef header is valid, false else.
1011 static opj_bool jp2_read_cmap_v2( opj_jp2_v2_t * jp2,
1012 unsigned char * p_cmap_header_data,
1013 OPJ_UINT32 p_cmap_header_size,
1014 opj_event_mgr_t * p_manager
1017 opj_jp2_cmap_comp_t *cmap;
1018 OPJ_BYTE i, nr_channels;
1023 assert(p_cmap_header_data != 00);
1024 assert(p_manager != 00);
1026 /* Need nr_channels: */
1027 if(jp2->color.jp2_pclr == NULL) {
1028 opj_event_msg_v2(p_manager, EVT_ERROR, "Need to read a PCLR box before the CMAP box.\n");
1032 /* Part 1, I.5.3.5: 'There shall be at most one Component Mapping box
1033 * inside a JP2 Header box' :
1035 if(jp2->color.jp2_pclr->cmap) {
1036 opj_event_msg_v2(p_manager, EVT_ERROR, "Only one CMAP box is allowed.\n");
1040 nr_channels = jp2->color.jp2_pclr->nr_channels;
1041 cmap = (opj_jp2_cmap_comp_t*) opj_malloc(nr_channels * sizeof(opj_jp2_cmap_comp_t));
1043 for(i = 0; i < nr_channels; ++i) {
1044 opj_read_bytes(p_cmap_header_data, &l_value, 2); /* CMP^i */
1045 p_cmap_header_data +=2;
1046 cmap[i].cmp = (OPJ_UINT16) l_value;
1048 opj_read_bytes(p_cmap_header_data, &l_value, 1); /* MTYP^i */
1049 ++p_cmap_header_data;
1050 cmap[i].mtyp = (OPJ_BYTE) l_value;
1052 opj_read_bytes(p_cmap_header_data, &l_value, 1); /* PCOL^i */
1053 ++p_cmap_header_data;
1054 cmap[i].pcol = (OPJ_BYTE) l_value;
1057 jp2->color.jp2_pclr->cmap = cmap;
1062 static void jp2_apply_cdef(opj_image_t *image, opj_jp2_color_t *color)
1064 opj_jp2_cdef_info_t *info;
1066 unsigned short i, n, cn, typ, asoc, acn;
1068 color_space = image->color_space;
1069 info = color->jp2_cdef->info;
1070 n = color->jp2_cdef->n;
1072 for(i = 0; i < n; ++i)
1074 /* WATCH: acn = asoc - 1 ! */
1075 if((asoc = info[i].asoc) == 0) continue;
1077 cn = info[i].cn; typ = info[i].typ; acn = asoc - 1;
1081 opj_image_comp_t saved;
1083 memcpy(&saved, &image->comps[cn], sizeof(opj_image_comp_t));
1084 memcpy(&image->comps[cn], &image->comps[acn], sizeof(opj_image_comp_t));
1085 memcpy(&image->comps[acn], &saved, sizeof(opj_image_comp_t));
1087 info[i].asoc = cn + 1;
1088 info[acn].asoc = info[acn].cn + 1;
1091 if(color->jp2_cdef->info) opj_free(color->jp2_cdef->info);
1093 opj_free(color->jp2_cdef); color->jp2_cdef = NULL;
1095 }/* jp2_apply_cdef() */
1097 static opj_bool jp2_read_cdef(opj_jp2_t *jp2, opj_cio_t *cio,
1098 opj_jp2_box_t *box, opj_jp2_color_t *color)
1100 opj_jp2_cdef_info_t *info;
1101 unsigned short i, n;
1103 OPJ_ARG_NOT_USED(box);
1104 OPJ_ARG_NOT_USED(jp2);
1106 /* Part 1, I.5.3.6: 'The shall be at most one Channel Definition box
1107 * inside a JP2 Header box.'
1109 if(color->jp2_cdef) return OPJ_FALSE;
1111 if((n = (unsigned short)cio_read(cio, 2)) == 0) return OPJ_FALSE; /* szukw000: FIXME */
1113 info = (opj_jp2_cdef_info_t*)
1114 opj_malloc(n * sizeof(opj_jp2_cdef_info_t));
1116 color->jp2_cdef = (opj_jp2_cdef_t*)opj_malloc(sizeof(opj_jp2_cdef_t));
1117 color->jp2_cdef->info = info;
1118 color->jp2_cdef->n = n;
1120 for(i = 0; i < n; ++i)
1122 info[i].cn = (unsigned short)cio_read(cio, 2);
1123 info[i].typ = (unsigned short)cio_read(cio, 2);
1124 info[i].asoc = (unsigned short)cio_read(cio, 2);
1128 }/* jp2_read_cdef() */
1131 * Reads the Component Definition box.
1133 * @param p_cdef_header_data pointer to actual data (already read from file)
1134 * @param jp2 the jpeg2000 file codec.
1135 * @param p_cdef_header_size pointer that will hold the size of the color header
1136 * @param p_manager the user event manager.
1138 * @return true if the cdef header is valid, false else.
1140 static opj_bool jp2_read_cdef_v2( opj_jp2_v2_t * jp2,
1141 unsigned char * p_cdef_header_data,
1142 OPJ_UINT32 p_cdef_header_size,
1143 opj_event_mgr_t * p_manager
1146 opj_jp2_cdef_info_t *cdef_info;
1152 assert(p_cdef_header_data != 00);
1153 assert(p_manager != 00);
1155 /* Part 1, I.5.3.6: 'The shall be at most one Channel Definition box
1156 * inside a JP2 Header box.'*/
1157 if(jp2->color.jp2_cdef) return OPJ_FALSE;
1159 opj_read_bytes(p_cdef_header_data,&l_value ,2); /* N */
1160 p_cdef_header_data+= 2;
1162 if ( (OPJ_UINT16)l_value == 0){ /* szukw000: FIXME */
1163 opj_event_msg_v2(p_manager, EVT_ERROR, "Number of channel description is equal to zero in CDEF box.\n");
1167 cdef_info = (opj_jp2_cdef_info_t*) opj_malloc(l_value * sizeof(opj_jp2_cdef_info_t));
1169 jp2->color.jp2_cdef = (opj_jp2_cdef_t*)opj_malloc(sizeof(opj_jp2_cdef_t));
1170 jp2->color.jp2_cdef->info = cdef_info;
1171 jp2->color.jp2_cdef->n = (OPJ_UINT16) l_value;
1173 for(i = 0; i < jp2->color.jp2_cdef->n; ++i) {
1174 opj_read_bytes(p_cdef_header_data, &l_value, 2); /* Cn^i */
1175 p_cdef_header_data +=2;
1176 cdef_info[i].cn = (OPJ_UINT16) l_value;
1178 opj_read_bytes(p_cdef_header_data, &l_value, 2); /* Typ^i */
1179 p_cdef_header_data +=2;
1180 cdef_info[i].typ = (OPJ_UINT16) l_value;
1182 opj_read_bytes(p_cdef_header_data, &l_value, 2); /* Asoc^i */
1183 p_cdef_header_data +=2;
1184 cdef_info[i].asoc = (OPJ_UINT16) l_value;
1191 static opj_bool jp2_read_colr(opj_jp2_t *jp2, opj_cio_t *cio,
1192 opj_jp2_box_t *box, opj_jp2_color_t *color)
1195 opj_common_ptr cinfo;
1197 /* Part 1, I.5.3.3 : 'A conforming JP2 reader shall ignore all Colour
1198 * Specification boxes after the first.'
1200 if(color->jp2_has_colr) return OPJ_FALSE;
1204 jp2->meth = cio_read(cio, 1); /* METH */
1205 jp2->precedence = cio_read(cio, 1); /* PRECEDENCE */
1206 jp2->approx = cio_read(cio, 1); /* APPROX */
1210 jp2->enumcs = cio_read(cio, 4); /* EnumCS */
1216 skip_len = box->init_pos + box->length - cio_tell(cio);
1219 opj_event_msg(cinfo, EVT_ERROR, "Error with COLR box size\n");
1224 unsigned char *start;
1226 start = cio_getbp(cio);
1227 color->icc_profile_buf = (unsigned char*)opj_malloc(skip_len);
1228 color->icc_profile_len = skip_len;
1230 cio_skip(cio, box->init_pos + box->length - cio_tell(cio));
1232 memcpy(color->icc_profile_buf, start, skip_len);
1236 if (cio_tell(cio) - box->init_pos != box->length)
1238 opj_event_msg(cinfo, EVT_ERROR, "Error with COLR Box\n");
1241 color->jp2_has_colr = 1;
1244 }/* jp2_read_colr() */
1247 * Reads the Colour Specification box.
1249 * @param p_colr_header_data pointer to actual data (already read from file)
1250 * @param jp2 the jpeg2000 file codec.
1251 * @param p_colr_header_size pointer that will hold the size of the color header
1252 * @param p_colr_header_max_size maximum size of the header, any size bigger than this value should result the function to output false.
1253 * @param p_manager the user event manager.
1255 * @return true if the bpc header is valid, fale else.
1257 static opj_bool jp2_read_colr_v2( opj_jp2_v2_t * jp2,
1258 unsigned char * p_colr_header_data,
1259 OPJ_UINT32 p_colr_header_size,
1260 opj_event_mgr_t * p_manager
1267 assert(p_colr_header_data != 00);
1268 assert(p_manager != 00);
1270 if (p_colr_header_size < 3) {
1271 opj_event_msg_v2(p_manager, EVT_ERROR, "Bad COLR header box (bad size)\n");
1275 /* Part 1, I.5.3.3 : 'A conforming JP2 reader shall ignore all Colour
1276 * Specification boxes after the first.'
1278 if(jp2->color.jp2_has_colr) {
1279 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");
1280 p_colr_header_data += p_colr_header_size;
1284 opj_read_bytes(p_colr_header_data,&jp2->meth ,1); /* METH */
1285 ++p_colr_header_data;
1287 opj_read_bytes(p_colr_header_data,&jp2->precedence ,1); /* PRECEDENCE */
1288 ++p_colr_header_data;
1290 opj_read_bytes(p_colr_header_data,&jp2->approx ,1); /* APPROX */
1291 ++p_colr_header_data;
1293 if (jp2->meth == 1) {
1294 if (p_colr_header_size != 7) {
1295 opj_event_msg_v2(p_manager, EVT_ERROR, "Bad BPCC header box (bad size)\n");
1299 opj_read_bytes(p_colr_header_data,&jp2->enumcs ,4); /* EnumCS */
1301 else if (jp2->meth == 2) {
1303 int it_icc_value = 0;
1304 int icc_len = p_colr_header_size - 3;
1306 jp2->color.icc_profile_len = icc_len;
1307 jp2->color.icc_profile_buf = (unsigned char*) opj_malloc(icc_len);
1309 memset(jp2->color.icc_profile_buf, 0, icc_len * sizeof(unsigned char));
1311 for (it_icc_value = 0; it_icc_value < icc_len; ++it_icc_value)
1313 opj_read_bytes(p_colr_header_data,&l_value,1); /* icc values */
1314 ++p_colr_header_data;
1315 jp2->color.icc_profile_buf[it_icc_value] = (OPJ_BYTE) l_value;
1320 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);
1322 jp2->color.jp2_has_colr = 1;
1327 opj_bool jp2_read_jp2h(opj_jp2_t *jp2, opj_cio_t *cio, opj_jp2_color_t *color)
1332 opj_common_ptr cinfo = jp2->cinfo;
1334 jp2_read_boxhdr(cinfo, cio, &box);
1337 if (JP2_JP2H != box.type)
1339 if (box.type == JP2_JP2C)
1341 opj_event_msg(cinfo, EVT_ERROR, "Expected JP2H Marker\n");
1344 cio_skip(cio, box.length - 8);
1346 if(cio->bp >= cio->end) return OPJ_FALSE;
1348 jp2_read_boxhdr(cinfo, cio, &box);
1350 } while(JP2_JP2H != box.type);
1352 if (!jp2_read_ihdr(jp2, cio))
1354 jp2h_end = box.init_pos + box.length;
1356 if (jp2->bpc == 255)
1358 if (!jp2_read_bpcc(jp2, cio))
1361 jp2_read_boxhdr(cinfo, cio, &box);
1363 while(cio_tell(cio) < jp2h_end)
1365 if(box.type == JP2_COLR)
1367 if( !jp2_read_colr(jp2, cio, &box, color))
1369 cio_seek(cio, box.init_pos + 8);
1370 cio_skip(cio, box.length - 8);
1372 jp2_read_boxhdr(cinfo, cio, &box);
1375 if(box.type == JP2_CDEF)
1377 if( !jp2_read_cdef(jp2, cio, &box, color))
1379 cio_seek(cio, box.init_pos + 8);
1380 cio_skip(cio, box.length - 8);
1382 jp2_read_boxhdr(cinfo, cio, &box);
1385 if(box.type == JP2_PCLR)
1387 if( !jp2_read_pclr(jp2, cio, &box, color))
1389 cio_seek(cio, box.init_pos + 8);
1390 cio_skip(cio, box.length - 8);
1392 jp2_read_boxhdr(cinfo, cio, &box);
1395 if(box.type == JP2_CMAP)
1397 if( !jp2_read_cmap(jp2, cio, &box, color))
1399 cio_seek(cio, box.init_pos + 8);
1400 cio_skip(cio, box.length - 8);
1402 jp2_read_boxhdr(cinfo, cio, &box);
1405 cio_seek(cio, box.init_pos + 8);
1406 cio_skip(cio, box.length - 8);
1407 jp2_read_boxhdr(cinfo, cio, &box);
1409 }/* while(cio_tell(cio) < box_end) */
1411 cio_seek(cio, jp2h_end);
1413 /* Part 1, I.5.3.3 : 'must contain at least one' */
1414 return (color->jp2_has_colr == 1);
1416 }/* jp2_read_jp2h() */
1418 opj_image_t* opj_jp2_decode(opj_jp2_t *jp2, opj_cio_t *cio,
1419 opj_codestream_info_t *cstr_info)
1421 opj_common_ptr cinfo;
1422 opj_image_t *image = NULL;
1423 opj_jp2_color_t color;
1429 memset(&color, 0, sizeof(opj_jp2_color_t));
1433 if(!jp2_read_struct(jp2, cio, &color))
1435 free_color_data(&color);
1436 opj_event_msg(cinfo, EVT_ERROR, "Failed to decode jp2 structure\n");
1441 image = j2k_decode(jp2->j2k, cio, cstr_info);
1445 free_color_data(&color);
1446 opj_event_msg(cinfo, EVT_ERROR, "Failed to decode J2K image\n");
1450 /* Set Image Color Space */
1451 if (jp2->enumcs == 16)
1452 image->color_space = CLRSPC_SRGB;
1453 else if (jp2->enumcs == 17)
1454 image->color_space = CLRSPC_GRAY;
1455 else if (jp2->enumcs == 18)
1456 image->color_space = CLRSPC_SYCC;
1458 image->color_space = CLRSPC_UNKNOWN;
1462 jp2_apply_cdef(image, &color);
1466 /* Part 1, I.5.3.4: Either both or none : */
1467 if( !color.jp2_pclr->cmap)
1468 jp2_free_pclr(&color);
1470 jp2_apply_pclr(&color, image);
1472 if(color.icc_profile_buf)
1474 image->icc_profile_buf = color.icc_profile_buf;
1475 color.icc_profile_buf = NULL;
1476 image->icc_profile_len = color.icc_profile_len;
1480 }/* opj_jp2_decode() */
1483 void jp2_write_jp2h(opj_jp2_t *jp2, opj_cio_t *cio) {
1486 box.init_pos = cio_tell(cio);
1488 cio_write(cio, JP2_JP2H, 4); /* JP2H */
1490 jp2_write_ihdr(jp2, cio);
1492 if (jp2->bpc == 255) {
1493 jp2_write_bpcc(jp2, cio);
1495 jp2_write_colr(jp2, cio);
1497 box.length = cio_tell(cio) - box.init_pos;
1498 cio_seek(cio, box.init_pos);
1499 cio_write(cio, box.length, 4); /* L */
1500 cio_seek(cio, box.init_pos + box.length);
1503 static void jp2_write_ftyp(opj_jp2_t *jp2, opj_cio_t *cio) {
1507 box.init_pos = cio_tell(cio);
1509 cio_write(cio, JP2_FTYP, 4); /* FTYP */
1511 cio_write(cio, jp2->brand, 4); /* BR */
1512 cio_write(cio, jp2->minversion, 4); /* MinV */
1514 for (i = 0; i < jp2->numcl; i++) {
1515 cio_write(cio, jp2->cl[i], 4); /* CL */
1518 box.length = cio_tell(cio) - box.init_pos;
1519 cio_seek(cio, box.init_pos);
1520 cio_write(cio, box.length, 4); /* L */
1521 cio_seek(cio, box.init_pos + box.length);
1524 static opj_bool jp2_read_ftyp(opj_jp2_t *jp2, opj_cio_t *cio) {
1528 opj_common_ptr cinfo = jp2->cinfo;
1530 jp2_read_boxhdr(cinfo, cio, &box);
1532 if (JP2_FTYP != box.type) {
1533 opj_event_msg(cinfo, EVT_ERROR, "Expected FTYP Marker\n");
1537 jp2->brand = cio_read(cio, 4); /* BR */
1538 jp2->minversion = cio_read(cio, 4); /* MinV */
1539 jp2->numcl = (box.length - 16) / 4;
1540 jp2->cl = (unsigned int *) opj_malloc(jp2->numcl * sizeof(unsigned int));
1542 for (i = 0; i < (int)jp2->numcl; i++) {
1543 jp2->cl[i] = cio_read(cio, 4); /* CLi */
1546 if (cio_tell(cio) - box.init_pos != box.length) {
1547 opj_event_msg(cinfo, EVT_ERROR, "Error with FTYP Box\n");
1554 static int jp2_write_jp2c(opj_jp2_t *jp2, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info) {
1555 unsigned int j2k_codestream_offset, j2k_codestream_length;
1558 opj_j2k_t *j2k = jp2->j2k;
1560 box.init_pos = cio_tell(cio);
1562 cio_write(cio, JP2_JP2C, 4); /* JP2C */
1565 j2k_codestream_offset = cio_tell(cio);
1566 if(!j2k_encode(j2k, cio, image, cstr_info)) {
1567 opj_event_msg(j2k->cinfo, EVT_ERROR, "Failed to encode image\n");
1570 j2k_codestream_length = cio_tell(cio) - j2k_codestream_offset;
1572 jp2->j2k_codestream_offset = j2k_codestream_offset;
1573 jp2->j2k_codestream_length = j2k_codestream_length;
1575 box.length = 8 + jp2->j2k_codestream_length;
1576 cio_seek(cio, box.init_pos);
1577 cio_write(cio, box.length, 4); /* L */
1578 cio_seek(cio, box.init_pos + box.length);
1583 static opj_bool jp2_read_jp2c(opj_jp2_t *jp2, opj_cio_t *cio, unsigned int *j2k_codestream_length, unsigned int *j2k_codestream_offset) {
1586 opj_common_ptr cinfo = jp2->cinfo;
1588 jp2_read_boxhdr(cinfo, cio, &box);
1590 if(JP2_JP2C != box.type) {
1591 cio_skip(cio, box.length - 8);
1592 jp2_read_boxhdr(cinfo, cio, &box);
1594 } while(JP2_JP2C != box.type);
1596 *j2k_codestream_offset = cio_tell(cio);
1597 *j2k_codestream_length = box.length - 8;
1602 static void jp2_write_jp(opj_cio_t *cio) {
1605 box.init_pos = cio_tell(cio);
1607 cio_write(cio, JP2_JP, 4); /* JP2 signature */
1608 cio_write(cio, 0x0d0a870a, 4);
1610 box.length = cio_tell(cio) - box.init_pos;
1611 cio_seek(cio, box.init_pos);
1612 cio_write(cio, box.length, 4); /* L */
1613 cio_seek(cio, box.init_pos + box.length);
1616 static opj_bool jp2_read_jp(opj_jp2_t *jp2, opj_cio_t *cio) {
1619 opj_common_ptr cinfo = jp2->cinfo;
1621 jp2_read_boxhdr(cinfo, cio, &box);
1622 if (JP2_JP != box.type) {
1623 opj_event_msg(cinfo, EVT_ERROR, "Expected JP Marker\n");
1626 if (0x0d0a870a != cio_read(cio, 4)) {
1627 opj_event_msg(cinfo, EVT_ERROR, "Error with JP Marker\n");
1630 if (cio_tell(cio) - box.init_pos != box.length) {
1631 opj_event_msg(cinfo, EVT_ERROR, "Error with JP Box size\n");
1639 static opj_bool jp2_read_struct(opj_jp2_t *jp2, opj_cio_t *cio,
1640 opj_jp2_color_t *color) {
1641 if (!jp2_read_jp(jp2, cio))
1643 if (!jp2_read_ftyp(jp2, cio))
1645 if (!jp2_read_jp2h(jp2, cio, color))
1647 if (!jp2_read_jp2c(jp2, cio, &jp2->j2k_codestream_length, &jp2->j2k_codestream_offset))
1654 static int write_fidx( int offset_jp2c, int length_jp2c, int offset_idx, int length_idx, opj_cio_t *cio)
1658 lenp = cio_tell( cio);
1659 cio_skip( cio, 4); /* L [at the end] */
1660 cio_write( cio, JPIP_FIDX, 4); /* IPTR */
1662 write_prxy( offset_jp2c, length_jp2c, offset_idx, length_idx, cio);
1664 len = cio_tell( cio)-lenp;
1665 cio_seek( cio, lenp);
1666 cio_write( cio, len, 4); /* L */
1667 cio_seek( cio, lenp+len);
1672 static void write_prxy( int offset_jp2c, int length_jp2c, int offset_idx, int length_idx, opj_cio_t *cio)
1676 lenp = cio_tell( cio);
1677 cio_skip( cio, 4); /* L [at the end] */
1678 cio_write( cio, JPIP_PRXY, 4); /* IPTR */
1680 cio_write( cio, offset_jp2c, 8); /* OOFF */
1681 cio_write( cio, length_jp2c, 4); /* OBH part 1 */
1682 cio_write( cio, JP2_JP2C, 4); /* OBH part 2 */
1684 cio_write( cio, 1,1); /* NI */
1686 cio_write( cio, offset_idx, 8); /* IOFF */
1687 cio_write( cio, length_idx, 4); /* IBH part 1 */
1688 cio_write( cio, JPIP_CIDX, 4); /* IBH part 2 */
1690 len = cio_tell( cio)-lenp;
1691 cio_seek( cio, lenp);
1692 cio_write( cio, len, 4); /* L */
1693 cio_seek( cio, lenp+len);
1696 static void write_iptr( int offset, int length, opj_cio_t *cio)
1700 lenp = cio_tell( cio);
1701 cio_skip( cio, 4); /* L [at the end] */
1702 cio_write( cio, JPIP_IPTR, 4); /* IPTR */
1704 cio_write( cio, offset, 8);
1705 cio_write( cio, length, 8);
1707 len = cio_tell( cio)-lenp;
1708 cio_seek( cio, lenp);
1709 cio_write( cio, len, 4); /* L */
1710 cio_seek( cio, lenp+len);
1714 /* ----------------------------------------------------------------------- */
1715 /* JP2 decoder interface */
1716 /* ----------------------------------------------------------------------- */
1718 opj_jp2_t* jp2_create_decompress(opj_common_ptr cinfo) {
1719 opj_jp2_t *jp2 = (opj_jp2_t*) opj_calloc(1, sizeof(opj_jp2_t));
1722 /* create the J2K codec */
1723 jp2->j2k = j2k_create_decompress(cinfo);
1724 if(jp2->j2k == NULL) {
1725 jp2_destroy_decompress(jp2);
1732 void jp2_destroy_decompress(opj_jp2_t *jp2) {
1734 /* destroy the J2K codec */
1735 j2k_destroy_decompress(jp2->j2k);
1738 opj_free(jp2->comps);
1747 void jp2_setup_decoder(opj_jp2_t *jp2, opj_dparameters_t *parameters) {
1748 /* setup the J2K codec */
1749 j2k_setup_decoder(jp2->j2k, parameters);
1750 /* further JP2 initializations go here */
1753 void jp2_setup_decoder_v2(opj_jp2_v2_t *jp2, opj_dparameters_t *parameters)
1755 /* setup the J2K codec */
1756 j2k_setup_decoder_v2(jp2->j2k, parameters);
1758 /* further JP2 initializations go here */
1759 jp2->color.jp2_has_colr = 0;
1763 /* ----------------------------------------------------------------------- */
1764 /* JP2 encoder interface */
1765 /* ----------------------------------------------------------------------- */
1767 opj_jp2_t* jp2_create_compress(opj_common_ptr cinfo) {
1768 opj_jp2_t *jp2 = (opj_jp2_t*)opj_malloc(sizeof(opj_jp2_t));
1771 /* create the J2K codec */
1772 jp2->j2k = j2k_create_compress(cinfo);
1773 if(jp2->j2k == NULL) {
1774 jp2_destroy_compress(jp2);
1781 void jp2_destroy_compress(opj_jp2_t *jp2) {
1783 /* destroy the J2K codec */
1784 j2k_destroy_compress(jp2->j2k);
1787 opj_free(jp2->comps);
1796 void jp2_setup_encoder(opj_jp2_t *jp2, opj_cparameters_t *parameters, opj_image_t *image) {
1800 if(!jp2 || !parameters || !image)
1803 /* setup the J2K codec */
1804 /* ------------------- */
1806 /* Check if number of components respects standard */
1807 if (image->numcomps < 1 || image->numcomps > 16384) {
1808 opj_event_msg(jp2->cinfo, EVT_ERROR, "Invalid number of components specified while setting up JP2 encoder\n");
1812 j2k_setup_encoder(jp2->j2k, parameters, image);
1814 /* setup the JP2 codec */
1815 /* ------------------- */
1819 jp2->brand = JP2_JP2; /* BR */
1820 jp2->minversion = 0; /* MinV */
1822 jp2->cl = (unsigned int*) opj_malloc(jp2->numcl * sizeof(unsigned int));
1823 jp2->cl[0] = JP2_JP2; /* CL0 : JP2 */
1825 /* Image Header box */
1827 jp2->numcomps = image->numcomps; /* NC */
1828 jp2->comps = (opj_jp2_comps_t*) opj_malloc(jp2->numcomps * sizeof(opj_jp2_comps_t));
1829 jp2->h = image->y1 - image->y0; /* HEIGHT */
1830 jp2->w = image->x1 - image->x0; /* WIDTH */
1832 depth_0 = image->comps[0].prec - 1;
1833 sign = image->comps[0].sgnd;
1834 jp2->bpc = depth_0 + (sign << 7);
1835 for (i = 1; i < image->numcomps; i++) {
1836 int depth = image->comps[i].prec - 1;
1837 sign = image->comps[i].sgnd;
1838 if (depth_0 != depth)
1841 jp2->C = 7; /* C : Always 7 */
1842 jp2->UnkC = 0; /* UnkC, colorspace specified in colr box */
1843 jp2->IPR = 0; /* IPR, no intellectual property */
1845 /* BitsPerComponent box */
1847 for (i = 0; i < image->numcomps; i++) {
1848 jp2->comps[i].bpcc = image->comps[i].prec - 1 + (image->comps[i].sgnd << 7);
1851 if (image->color_space == 1)
1852 jp2->enumcs = 16; /* sRGB as defined by IEC 61966-2.1 */
1853 else if (image->color_space == 2)
1854 jp2->enumcs = 17; /* greyscale */
1855 else if (image->color_space == 3)
1856 jp2->enumcs = 18; /* YUV */
1857 jp2->precedence = 0; /* PRECEDENCE */
1858 jp2->approx = 0; /* APPROX */
1860 jp2->jpip_on = parameters->jpip_on;
1863 opj_bool opj_jp2_encode(opj_jp2_t *jp2, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info) {
1865 int pos_iptr, pos_cidx, pos_jp2c, len_jp2c, len_cidx, end_pos, pos_fidx, len_fidx;
1869 /* JPEG 2000 Signature box */
1872 jp2_write_ftyp(jp2, cio);
1873 /* JP2 Header box */
1874 jp2_write_jp2h(jp2, cio);
1877 pos_iptr = cio_tell( cio);
1878 cio_skip( cio, 24); /* IPTR further ! */
1880 pos_jp2c = cio_tell( cio);
1884 if(!(len_jp2c = jp2_write_jp2c( jp2, cio, image, cstr_info))){
1885 opj_event_msg(jp2->cinfo, EVT_ERROR, "Failed to encode image\n");
1890 pos_cidx = cio_tell( cio);
1892 len_cidx = write_cidx( pos_jp2c+8, cio, image, *cstr_info, len_jp2c-8);
1894 pos_fidx = cio_tell( cio);
1895 len_fidx = write_fidx( pos_jp2c, len_jp2c, pos_cidx, len_cidx, cio);
1897 end_pos = cio_tell( cio);
1899 cio_seek( cio, pos_iptr);
1900 write_iptr( pos_fidx, len_fidx, cio);
1901 cio_seek( cio, end_pos);
1908 * Ends the decompression procedures and possibiliy add data to be read after the
1911 opj_bool jp2_end_decompress(opj_jp2_v2_t *jp2, opj_stream_private_t *cio, opj_event_mgr_t * p_manager)
1916 assert(p_manager != 00);
1918 /* customization of the end encoding */
1919 jp2_setup_end_header_reading(jp2);
1922 if (! jp2_exec (jp2,jp2->m_procedure_list,cio,p_manager)) {
1926 return j2k_end_decompress(jp2->j2k, cio, p_manager);
1930 * Sets up the procedures to do on reading header after the codestream.
1931 * Developpers wanting to extend the library can add their own writting procedures.
1933 void jp2_setup_end_header_reading (opj_jp2_v2_t *jp2)
1937 opj_procedure_list_add_procedure(jp2->m_procedure_list,(void*)jp2_read_header_procedure );
1938 /* DEVELOPER CORNER, add your custom procedures */
1943 * Reads a jpeg2000 file header structure.
1945 * @param cio the stream to read data from.
1946 * @param jp2 the jpeg2000 file header structure.
1947 * @param p_manager the user event manager.
1949 * @return true if the box is valid.
1951 opj_bool jp2_read_header_procedure(
1953 opj_stream_private_t *cio,
1954 opj_event_mgr_t * p_manager)
1957 OPJ_UINT32 l_nb_bytes_read;
1958 const opj_jp2_header_handler_t * l_current_handler;
1959 OPJ_UINT32 l_last_data_size = BOX_SIZE;
1960 OPJ_UINT32 l_current_data_size;
1961 unsigned char * l_current_data = 00;
1966 assert(p_manager != 00);
1968 l_current_data = (unsigned char*)opj_malloc(l_last_data_size);
1970 if (l_current_data == 00) {
1971 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to handle jpeg2000 file header\n");
1974 memset(l_current_data, 0 , l_last_data_size);
1976 while (jp2_read_boxhdr_v2(&box,&l_nb_bytes_read,cio,p_manager)) {
1977 // is it the codestream box ?
1978 if (box.type == JP2_JP2C) {
1979 if (jp2->jp2_state & JP2_STATE_HEADER) {
1980 jp2->jp2_state |= JP2_STATE_CODESTREAM;
1981 opj_free(l_current_data);
1985 opj_event_msg_v2(p_manager, EVT_ERROR, "bad placed jpeg codestream\n");
1986 opj_free(l_current_data);
1990 else if (box.length == 0) {
1991 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot handle box of undefined sizes\n");
1992 opj_free(l_current_data);
1996 l_current_handler = jp2_find_handler(box.type);
1997 l_current_data_size = box.length - l_nb_bytes_read;
1999 if (l_current_handler != 00) {
2000 if (l_current_data_size > l_last_data_size) {
2001 l_current_data = (unsigned char*)opj_realloc(l_current_data,l_current_data_size);
2002 l_last_data_size = l_current_data_size;
2005 l_nb_bytes_read = opj_stream_read_data(cio,l_current_data,l_current_data_size,p_manager);
2006 if (l_nb_bytes_read != l_current_data_size) {
2007 opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with reading JPEG2000 box, stream error\n");
2011 if (! l_current_handler->handler(jp2,l_current_data,l_current_data_size,p_manager)) {
2012 opj_free(l_current_data);
2017 jp2->jp2_state |= JP2_STATE_UNKNOWN;
2018 if (opj_stream_skip(cio,l_current_data_size,p_manager) != l_current_data_size) {
2019 opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with skipping JPEG2000 box, stream error\n");
2020 opj_free(l_current_data);
2026 opj_free(l_current_data);
2032 * Excutes the given procedures on the given codec.
2034 * @param p_procedure_list the list of procedures to execute
2035 * @param jp2 the jpeg2000 file codec to execute the procedures on.
2036 * @param cio the stream to execute the procedures on.
2037 * @param p_manager the user manager.
2039 * @return true if all the procedures were successfully executed.
2043 opj_procedure_list_t * p_procedure_list,
2044 opj_stream_private_t *cio,
2045 opj_event_mgr_t * p_manager
2048 opj_bool (** l_procedure) (opj_jp2_v2_t * jp2, opj_stream_private_t *, opj_event_mgr_t *) = 00;
2049 opj_bool l_result = OPJ_TRUE;
2050 OPJ_UINT32 l_nb_proc, i;
2053 assert(p_procedure_list != 00);
2056 assert(p_manager != 00);
2058 l_nb_proc = opj_procedure_list_get_nb_procedures(p_procedure_list);
2059 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);
2061 for (i=0;i<l_nb_proc;++i) {
2062 l_result = l_result && (*l_procedure) (jp2,cio,p_manager);
2066 // and clear the procedure list at the end.
2067 opj_procedure_list_clear(p_procedure_list);
2073 * Finds the execution function related to the given box id.
2075 * @param p_id the id of the handler to fetch.
2077 * @return the given handler or 00 if it could not be found.
2079 const opj_jp2_header_handler_t * jp2_find_handler (int p_id)
2081 OPJ_UINT32 i, l_handler_size = sizeof(jp2_header) / sizeof(opj_jp2_header_handler_t);
2083 for (i=0;i<l_handler_size;++i) {
2084 if (jp2_header[i].id == p_id) {
2085 return &jp2_header[i];
2092 * Finds the image execution function related to the given box id.
2094 * @param p_id the id of the handler to fetch.
2096 * @return the given handler or 00 if it could not be found.
2098 static const opj_jp2_header_handler_t * jp2_img_find_handler (
2102 OPJ_UINT32 i, l_handler_size = sizeof(jp2_img_header) / sizeof(opj_jp2_header_handler_t);
2103 for (i=0;i<l_handler_size;++i)
2105 if (jp2_img_header[i].id == p_id) {
2106 return &jp2_img_header[i];
2115 * Reads a jpeg2000 file signature box.
2117 * @param p_header_data the data contained in the signature box.
2118 * @param jp2 the jpeg2000 file codec.
2119 * @param p_header_size the size of the data contained in the signature box.
2120 * @param p_manager the user event manager.
2122 * @return true if the file signature box is valid.
2124 opj_bool jp2_read_jp_v2(
2126 unsigned char * p_header_data,
2127 unsigned int p_header_size,
2128 opj_event_mgr_t * p_manager
2131 unsigned int l_magic_number;
2134 assert(p_header_data != 00);
2136 assert(p_manager != 00);
2138 if (jp2->jp2_state != JP2_STATE_NONE) {
2139 opj_event_msg_v2(p_manager, EVT_ERROR, "The signature box must be the first box in the file.\n");
2143 /* assure length of data is correct (4 -> magic number) */
2144 if (p_header_size != 4) {
2145 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with JP signature Box size\n");
2150 opj_read_bytes(p_header_data,&l_magic_number,4);
2151 if (l_magic_number != 0x0d0a870a ) {
2152 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with JP Signature : bad magic number\n");
2156 jp2->jp2_state |= JP2_STATE_SIGNATURE;
2163 * Reads a a FTYP box - File type box
2165 * @param p_header_data the data contained in the FTYP box.
2166 * @param jp2 the jpeg2000 file codec.
2167 * @param p_header_size the size of the data contained in the FTYP box.
2168 * @param p_manager the user event manager.
2170 * @return true if the FTYP box is valid.
2172 opj_bool jp2_read_ftyp_v2(
2174 unsigned char * p_header_data,
2175 unsigned int p_header_size,
2176 opj_event_mgr_t * p_manager
2179 OPJ_UINT32 i, l_remaining_bytes;
2182 assert(p_header_data != 00);
2184 assert(p_manager != 00);
2186 if (jp2->jp2_state != JP2_STATE_SIGNATURE) {
2187 opj_event_msg_v2(p_manager, EVT_ERROR, "The ftyp box must be the second box in the file.\n");
2191 /* assure length of data is correct */
2192 if (p_header_size < 8) {
2193 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with FTYP signature Box size\n");
2197 opj_read_bytes(p_header_data,&jp2->brand,4); /* BR */
2200 opj_read_bytes(p_header_data,&jp2->minversion,4); /* MinV */
2203 l_remaining_bytes = p_header_size - 8;
2205 /* the number of remaining bytes should be a multiple of 4 */
2206 if ((l_remaining_bytes & 0x3) != 0) {
2207 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with FTYP signature Box size\n");
2212 jp2->numcl = l_remaining_bytes >> 2;
2214 jp2->cl = (unsigned int *) opj_malloc(jp2->numcl * sizeof(unsigned int));
2215 if (jp2->cl == 00) {
2216 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory with FTYP Box\n");
2219 memset(jp2->cl,0,jp2->numcl * sizeof(unsigned int));
2222 for (i = 0; i < jp2->numcl; ++i)
2224 opj_read_bytes(p_header_data,&jp2->cl[i],4); /* CLi */
2228 jp2->jp2_state |= JP2_STATE_FILE_TYPE;
2235 * Reads the Jpeg2000 file Header box - JP2 Header box (warning, this is a super box).
2237 * @param p_header_data the data contained in the file header box.
2238 * @param jp2 the jpeg2000 file codec.
2239 * @param p_header_size the size of the data contained in the file header box.
2240 * @param p_manager the user event manager.
2242 * @return true if the JP2 Header box was successfully reconized.
2244 opj_bool jp2_read_jp2h_v2(
2246 unsigned char * p_header_data,
2247 unsigned int p_header_size,
2248 opj_event_mgr_t * p_manager
2251 OPJ_UINT32 l_box_size=0, l_current_data_size = 0;
2253 const opj_jp2_header_handler_t * l_current_handler;
2256 assert(p_header_data != 00);
2258 assert(p_manager != 00);
2260 /* make sure the box is well placed */
2261 if ((jp2->jp2_state & JP2_STATE_FILE_TYPE) != JP2_STATE_FILE_TYPE ) {
2262 opj_event_msg_v2(p_manager, EVT_ERROR, "The box must be the first box in the file.\n");
2266 jp2->jp2_img_state = JP2_IMG_STATE_NONE;
2268 /* iterate while remaining data */
2269 while (p_header_size > 0) {
2271 if (! jp2_read_boxhdr_char(&box,p_header_data,&l_box_size,p_header_size, p_manager)) {
2272 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream error while reading JP2 Header box\n");
2276 if (box.length > p_header_size) {
2277 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream error while reading JP2 Header box: box length is inconsistent.\n");
2281 l_current_handler = jp2_img_find_handler(box.type);
2282 l_current_data_size = box.length - l_box_size;
2283 p_header_data += l_box_size;
2285 if (l_current_handler != 00) {
2286 if (! l_current_handler->handler(jp2,p_header_data,l_current_data_size,p_manager)) {
2291 jp2->jp2_img_state |= JP2_IMG_STATE_UNKNOWN;
2294 p_header_data += l_current_data_size;
2295 p_header_size -= box.length;
2298 jp2->jp2_state |= JP2_STATE_HEADER;
2304 * Reads a box header. The box is the way data is packed inside a jpeg2000 file structure. Data is read from a character string
2306 * @param p_data the character string to read data from.
2307 * @param box the box structure to fill.
2308 * @param p_number_bytes_read pointer to an int that will store the number of bytes read from the stream (shoul usually be 2).
2309 * @param p_box_max_size the maximum number of bytes in the box.
2311 * @return true if the box is reconized, false otherwise
2313 static opj_bool jp2_read_boxhdr_char(
2316 OPJ_UINT32 * p_number_bytes_read,
2317 OPJ_UINT32 p_box_max_size,
2318 opj_event_mgr_t * p_manager
2324 assert(p_data != 00);
2326 assert(p_number_bytes_read != 00);
2327 assert(p_manager != 00);
2329 if (p_box_max_size < 8) {
2330 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot handle box of less than 8 bytes\n");
2334 /* process read data */
2335 opj_read_bytes(p_data, &l_value, 4);
2337 box->length = (OPJ_INT32)(l_value);
2339 opj_read_bytes(p_data, &l_value, 4);
2341 box->type = (OPJ_INT32)(l_value);
2343 *p_number_bytes_read = 8;
2345 // do we have a "special very large box ?"
2346 // read then the XLBox
2347 if (box->length == 1) {
2348 unsigned int l_xl_part_size;
2350 if (p_box_max_size < 16) {
2351 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot handle XL box of less than 16 bytes\n");
2355 opj_read_bytes(p_data,&l_xl_part_size, 4);
2357 *p_number_bytes_read += 4;
2359 if (l_xl_part_size != 0) {
2360 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot handle box sizes higher than 2^32\n");
2364 opj_read_bytes(p_data, &l_value, 4);
2365 *p_number_bytes_read += 4;
2366 box->length = (OPJ_INT32)(l_value);
2368 if (box->length == 0) {
2369 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot handle box of undefined sizes\n");
2373 else if (box->length == 0) {
2374 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot handle box of undefined sizes\n");
2383 * Reads a jpeg2000 file header structure.
2385 * @param cio the stream to read data from.
2386 * @param jp2 the jpeg2000 file header structure.
2387 * @param p_manager the user event manager.
2389 * @return true if the box is valid.
2391 opj_bool jp2_read_header( struct opj_stream_private *p_stream,
2393 opj_image_header_t* p_img_header,
2394 struct opj_event_mgr * p_manager
2399 assert(p_stream != 00);
2400 assert(p_manager != 00);
2402 /* customization of the validation */
2403 jp2_setup_decoding_validation (jp2);
2405 /* customization of the encoding */
2406 jp2_setup_header_reading(jp2);
2408 /* validation of the parameters codec */
2409 if (! jp2_exec(jp2,jp2->m_validation_list,p_stream,p_manager)) {
2414 if (! jp2_exec (jp2,jp2->m_procedure_list,p_stream,p_manager)) {
2418 return j2k_read_header( p_stream,
2425 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
2426 * are valid. Developpers wanting to extend the library can add their own validation procedures.
2428 void jp2_setup_decoding_validation (opj_jp2_v2_t *jp2)
2432 /* DEVELOPER CORNER, add your custom validation procedure */
2436 * Sets up the procedures to do on reading header.
2437 * Developpers wanting to extend the library can add their own writting procedures.
2439 void jp2_setup_header_reading (opj_jp2_v2_t *jp2)
2444 opj_procedure_list_add_procedure(jp2->m_procedure_list,(void*)jp2_read_header_procedure );
2445 /* DEVELOPER CORNER, add your custom procedures */
2450 * Reads a tile header.
2451 * @param p_j2k the jpeg2000 codec.
2452 * @param p_stream the stream to write data to.
2453 * @param p_manager the user event manager.
2455 opj_bool jp2_read_tile_header( opj_jp2_v2_t * p_jp2,
2456 OPJ_UINT32 * p_tile_index,
2457 OPJ_UINT32 * p_data_size,
2458 OPJ_INT32 * p_tile_x0, OPJ_INT32 * p_tile_y0,
2459 OPJ_INT32 * p_tile_x1, OPJ_INT32 * p_tile_y1,
2460 OPJ_UINT32 * p_nb_comps,
2462 opj_stream_private_t *p_stream,
2463 opj_event_mgr_t * p_manager )
2465 return j2k_read_tile_header(p_jp2->j2k,
2468 p_tile_x0, p_tile_y0,
2469 p_tile_x1, p_tile_y1,
2478 * @param p_j2k the jpeg2000 codec.
2479 * @param p_stream the stream to write data to.
2480 * @param p_manager the user event manager.
2482 opj_bool opj_jp2_decode_tile (
2483 opj_jp2_v2_t * p_jp2,
2484 OPJ_UINT32 p_tile_index,
2486 OPJ_UINT32 p_data_size,
2487 opj_stream_private_t *p_stream,
2488 opj_event_mgr_t * p_manager
2491 return j2k_decode_tile (p_jp2->j2k,p_tile_index,p_data,p_data_size,p_stream,p_manager);
2495 * Destroys a jpeg2000 file decompressor.
2497 * @param jp2 a jpeg2000 file decompressor.
2499 void jp2_destroy(opj_jp2_v2_t *jp2)
2502 /* destroy the J2K codec */
2503 j2k_destroy(jp2->j2k);
2507 opj_free(jp2->comps);
2516 if (jp2->color.icc_profile_buf) {
2517 opj_free(jp2->color.icc_profile_buf);
2518 jp2->color.icc_profile_buf = 00;
2521 if (jp2->color.jp2_cdef) {
2522 opj_free(jp2->color.jp2_cdef);
2523 jp2->color.jp2_cdef = 00;
2526 if (jp2->color.jp2_pclr) {
2527 opj_free(jp2->color.jp2_pclr);
2528 jp2->color.jp2_pclr = 00;
2531 if (jp2->m_validation_list) {
2532 opj_procedure_list_destroy(jp2->m_validation_list);
2533 jp2->m_validation_list = 00;
2536 if (jp2->m_procedure_list) {
2537 opj_procedure_list_destroy(jp2->m_procedure_list);
2538 jp2->m_procedure_list = 00;
2546 * Sets the given area to be decoded. This function should be called right after opj_read_header and before any tile header reading.
2548 * @param p_jp2 the jpeg2000 codec.
2549 * @param p_end_x the right position of the rectangle to decode (in image coordinates).
2550 * @param p_start_y the up position of the rectangle to decode (in image coordinates).
2551 * @param p_end_y the bottom position of the rectangle to decode (in image coordinates).
2552 * @param p_manager the user event manager
2554 * @return true if the area could be set.
2556 opj_bool jp2_set_decode_area( opj_jp2_v2_t *p_jp2,
2557 OPJ_INT32 p_start_x, OPJ_INT32 p_start_y,
2558 OPJ_INT32 p_end_x, OPJ_INT32 p_end_y,
2559 struct opj_event_mgr * p_manager )
2561 return j2k_set_decode_area(p_jp2->j2k, p_start_x, p_start_y, p_end_x, p_end_y, p_manager);
2564 /* ----------------------------------------------------------------------- */
2565 /* JP2 encoder interface */
2566 /* ----------------------------------------------------------------------- */
2568 opj_jp2_v2_t* jp2_create(opj_bool p_is_decoder)
2570 opj_jp2_v2_t *jp2 = (opj_jp2_v2_t*)opj_malloc(sizeof(opj_jp2_v2_t));
2572 memset(jp2,0,sizeof(opj_jp2_t));
2574 /* create the J2K codec */
2575 if (! p_is_decoder) {
2576 jp2->j2k = j2k_create_compress_v2();
2579 jp2->j2k = j2k_create_decompress_v2();
2582 if (jp2->j2k == 00) {
2587 // validation list creation
2588 jp2->m_validation_list = opj_procedure_list_create();
2589 if (! jp2->m_validation_list) {
2594 // execution list creation
2595 jp2->m_procedure_list = opj_procedure_list_create();
2596 if (! jp2->m_procedure_list) {