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_image_t *image, opj_jp2_color_t *color);
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 jp2 the jpeg2000 file codec.
546 * @param p_image_header_data pointer to actual data (already read from file)
547 * @param p_image_header_size the size of the image header
548 * @param p_manager the user event manager.
550 * @return true if the image header is valid, fale else.
552 opj_bool jp2_read_ihdr_v2(
554 unsigned char * p_image_header_data,
555 unsigned int p_image_header_size,
556 opj_event_mgr_t * p_manager
560 assert(p_image_header_data != 00);
562 assert(p_manager != 00);
564 if (p_image_header_size != 14) {
565 opj_event_msg_v2(p_manager, EVT_ERROR, "Bad image header box (bad size)\n");
569 opj_read_bytes(p_image_header_data,&(jp2->h),4); /* HEIGHT */
570 p_image_header_data += 4;
571 opj_read_bytes(p_image_header_data,&(jp2->w),4); /* WIDTH */
572 p_image_header_data += 4;
573 opj_read_bytes(p_image_header_data,&(jp2->numcomps),2); /* NC */
574 p_image_header_data += 2;
576 /* allocate memory for components */
577 jp2->comps = (opj_jp2_comps_t*) opj_malloc(jp2->numcomps * sizeof(opj_jp2_comps_t));
578 if (jp2->comps == 0) {
579 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to handle image header (ihdr)\n");
582 memset(jp2->comps,0,jp2->numcomps * sizeof(opj_jp2_comps_t));
584 opj_read_bytes(p_image_header_data,&(jp2->bpc),1); /* BPC */
585 ++ p_image_header_data;
587 // if equal to 0 then need a BPC box (cf. chapter about image header box of the norm)
588 /*if (jp2->bpc == 0){
589 // indicate with a flag that we will wait a BPC box
592 opj_read_bytes(p_image_header_data,&(jp2->C),1); /* C */
593 ++ p_image_header_data;
595 // Should be equal to 7 cf. chapter about image header box of the norm
597 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);
600 opj_read_bytes(p_image_header_data,&(jp2->UnkC),1); /* UnkC */
601 ++ p_image_header_data;
602 opj_read_bytes(p_image_header_data,&(jp2->IPR),1); /* IPR */
603 ++ p_image_header_data;
608 static void jp2_write_ihdr(opj_jp2_t *jp2, opj_cio_t *cio) {
611 box.init_pos = cio_tell(cio);
613 cio_write(cio, JP2_IHDR, 4); /* IHDR */
615 cio_write(cio, jp2->h, 4); /* HEIGHT */
616 cio_write(cio, jp2->w, 4); /* WIDTH */
617 cio_write(cio, jp2->numcomps, 2); /* NC */
619 cio_write(cio, jp2->bpc, 1); /* BPC */
621 cio_write(cio, jp2->C, 1); /* C : Always 7 */
622 cio_write(cio, jp2->UnkC, 1); /* UnkC, colorspace unknown */
623 cio_write(cio, jp2->IPR, 1); /* IPR, no intellectual property */
625 box.length = cio_tell(cio) - box.init_pos;
626 cio_seek(cio, box.init_pos);
627 cio_write(cio, box.length, 4); /* L */
628 cio_seek(cio, box.init_pos + box.length);
631 static void jp2_write_bpcc(opj_jp2_t *jp2, opj_cio_t *cio) {
635 box.init_pos = cio_tell(cio);
637 cio_write(cio, JP2_BPCC, 4); /* BPCC */
639 for (i = 0; i < jp2->numcomps; i++) {
640 cio_write(cio, jp2->comps[i].bpcc, 1);
643 box.length = cio_tell(cio) - box.init_pos;
644 cio_seek(cio, box.init_pos);
645 cio_write(cio, box.length, 4); /* L */
646 cio_seek(cio, box.init_pos + box.length);
650 static opj_bool jp2_read_bpcc(opj_jp2_t *jp2, opj_cio_t *cio) {
654 opj_common_ptr cinfo = jp2->cinfo;
656 jp2_read_boxhdr(cinfo, cio, &box);
657 if (JP2_BPCC != box.type) {
658 opj_event_msg(cinfo, EVT_ERROR, "Expected BPCC Marker\n");
662 for (i = 0; i < jp2->numcomps; i++) {
663 jp2->comps[i].bpcc = cio_read(cio, 1);
666 if (cio_tell(cio) - box.init_pos != box.length) {
667 opj_event_msg(cinfo, EVT_ERROR, "Error with BPCC Box\n");
675 * Reads a Bit per Component box.
677 * @param jp2 the jpeg2000 file codec.
678 * @param p_bpc_header_data pointer to actual data (already read from file)
679 * @param p_bpc_header_size pointer that will hold the size of the bpc header
680 * @param p_manager the user event manager.
682 * @return true if the bpc header is valid, false otherwise.
684 opj_bool jp2_read_bpcc_v2( opj_jp2_v2_t *jp2,
685 unsigned char * p_bpc_header_data,
686 unsigned int p_bpc_header_size,
687 opj_event_mgr_t * p_manager
693 assert(p_bpc_header_data != 00);
695 assert(p_manager != 00);
698 /*if (jp2->bpc != 0 ){
699 opj_event_msg_v2(p_manager, EVT_WARNING, "A BPCC header box is available although BPC is different to zero (%d)\n",jp2->bpc);
702 // and length is relevant
703 if (p_bpc_header_size != jp2->numcomps) {
704 opj_event_msg_v2(p_manager, EVT_ERROR, "Bad BPCC header box (bad size)\n");
708 // read info for each component
709 for (i = 0; i < jp2->numcomps; ++i) {
710 opj_read_bytes(p_bpc_header_data,&jp2->comps[i].bpcc ,1); /* read each BPCC component */
717 static void jp2_write_colr(opj_jp2_t *jp2, opj_cio_t *cio) {
720 box.init_pos = cio_tell(cio);
722 cio_write(cio, JP2_COLR, 4); /* COLR */
724 cio_write(cio, jp2->meth, 1); /* METH */
725 cio_write(cio, jp2->precedence, 1); /* PRECEDENCE */
726 cio_write(cio, jp2->approx, 1); /* APPROX */
731 cio_write(cio, jp2->enumcs, 4); /* EnumCS */
733 box.length = cio_tell(cio) - box.init_pos;
734 cio_seek(cio, box.init_pos);
735 cio_write(cio, box.length, 4); /* L */
736 cio_seek(cio, box.init_pos + box.length);
739 static void jp2_free_pclr(opj_jp2_color_t *color)
741 opj_free(color->jp2_pclr->channel_sign);
742 opj_free(color->jp2_pclr->channel_size);
743 opj_free(color->jp2_pclr->entries);
745 if(color->jp2_pclr->cmap) opj_free(color->jp2_pclr->cmap);
747 opj_free(color->jp2_pclr); color->jp2_pclr = NULL;
750 static void free_color_data(opj_jp2_color_t *color)
754 jp2_free_pclr(color);
758 if(color->jp2_cdef->info) opj_free(color->jp2_cdef->info);
759 opj_free(color->jp2_cdef);
761 if(color->icc_profile_buf) opj_free(color->icc_profile_buf);
765 static void jp2_apply_pclr(opj_image_t *image, opj_jp2_color_t *color)
767 opj_image_comp_t *old_comps, *new_comps;
768 OPJ_BYTE *channel_size, *channel_sign;
770 opj_jp2_cmap_comp_t *cmap;
771 OPJ_INT32 *src, *dst;
773 OPJ_UINT16 i, nr_channels, cmp, pcol;
776 channel_size = color->jp2_pclr->channel_size;
777 channel_sign = color->jp2_pclr->channel_sign;
778 entries = color->jp2_pclr->entries;
779 cmap = color->jp2_pclr->cmap;
780 nr_channels = color->jp2_pclr->nr_channels;
782 old_comps = image->comps;
783 new_comps = (opj_image_comp_t*)
784 opj_malloc(nr_channels * sizeof(opj_image_comp_t));
786 for(i = 0; i < nr_channels; ++i) {
787 pcol = cmap[i].pcol; cmp = cmap[i].cmp;
789 new_comps[pcol] = old_comps[cmp];
792 if(cmap[i].mtyp == 0){
793 old_comps[cmp].data = NULL; continue;
796 /* Palette mapping: */
797 new_comps[pcol].data = (int*)
798 opj_malloc(old_comps[cmp].w * old_comps[cmp].h * sizeof(int));
799 new_comps[pcol].prec = channel_size[i];
800 new_comps[pcol].sgnd = channel_sign[i];
803 top_k = color->jp2_pclr->nr_entries - 1;
805 for(i = 0; i < nr_channels; ++i) {
807 if(cmap[i].mtyp == 0) continue;
809 /* Palette mapping: */
810 cmp = cmap[i].cmp; pcol = cmap[i].pcol;
811 src = old_comps[cmp].data;
812 dst = new_comps[pcol].data;
813 max = new_comps[pcol].w * new_comps[pcol].h;
815 for(j = 0; j < max; ++j)
818 if((k = src[j]) < 0) k = 0; else if(k > top_k) k = top_k;
821 dst[j] = entries[k * nr_channels + pcol];
825 max = image->numcomps;
826 for(i = 0; i < max; ++i) {
827 if(old_comps[i].data) opj_free(old_comps[i].data);
831 image->comps = new_comps;
832 image->numcomps = nr_channels;
834 jp2_free_pclr(color);
839 static opj_bool jp2_read_pclr(opj_jp2_t *jp2, opj_cio_t *cio,
840 opj_jp2_box_t *box, opj_jp2_color_t *color)
842 opj_jp2_pclr_t *jp2_pclr;
843 unsigned char *channel_size, *channel_sign;
844 unsigned int *entries;
845 unsigned short nr_entries, nr_channels;
849 OPJ_ARG_NOT_USED(box);
850 OPJ_ARG_NOT_USED(jp2);
852 /* Part 1, I.5.3.4: 'There shall be at most one Palette box inside
853 * a JP2 Header box' :
855 if(color->jp2_pclr) return OPJ_FALSE;
857 nr_entries = (unsigned short)cio_read(cio, 2); /* NE */
858 nr_channels = (unsigned short)cio_read(cio, 1);/* NPC */
860 entries = (unsigned int*)
861 opj_malloc(nr_channels * nr_entries * sizeof(unsigned int));
862 channel_size = (unsigned char*)opj_malloc(nr_channels);
863 channel_sign = (unsigned char*)opj_malloc(nr_channels);
865 jp2_pclr = (opj_jp2_pclr_t*)opj_malloc(sizeof(opj_jp2_pclr_t));
866 jp2_pclr->channel_sign = channel_sign;
867 jp2_pclr->channel_size = channel_size;
868 jp2_pclr->entries = entries;
869 jp2_pclr->nr_entries = nr_entries;
870 jp2_pclr->nr_channels = nr_channels;
871 jp2_pclr->cmap = NULL;
873 color->jp2_pclr = jp2_pclr;
875 for(i = 0; i < nr_channels; ++i)
877 uc = cio_read(cio, 1); /* Bi */
878 channel_size[i] = (uc & 0x7f) + 1;
879 channel_sign[i] = (uc & 0x80)?1:0;
882 for(j = 0; j < nr_entries; ++j)
884 for(i = 0; i < nr_channels; ++i)
887 *entries++ = cio_read(cio, channel_size[i]>>3);
892 }/* jp2_read_pclr() */
895 * Reads a palette box.
897 * @param jp2 the jpeg2000 file codec.
898 * @param p_pclr_header_data pointer to actual data (already read from file)
899 * @param p_pclr_header_size pointer that will hold the size of the PCLR header
900 * @param p_manager the user event manager.
902 * @return true if the bpc header is valid, fale else.
904 opj_bool jp2_read_pclr_v2( opj_jp2_v2_t *jp2,
905 unsigned char * p_pclr_header_data,
906 OPJ_UINT32 p_pclr_header_size,
907 opj_event_mgr_t * p_manager
909 opj_jp2_pclr_t *jp2_pclr;
910 OPJ_BYTE *channel_size, *channel_sign;
912 OPJ_UINT16 nr_entries,nr_channels;
917 assert(p_pclr_header_data != 00);
919 assert(p_manager != 00);
921 if(jp2->color.jp2_pclr)
924 opj_read_bytes(p_pclr_header_data, &l_value , 2); /* NE */
925 p_pclr_header_data += 2;
926 nr_entries = (OPJ_UINT16) l_value;
928 opj_read_bytes(p_pclr_header_data, &l_value , 1); /* NPC */
929 ++p_pclr_header_data;
930 nr_channels = (OPJ_UINT16) l_value;
932 entries = (OPJ_UINT32*) opj_malloc(nr_channels * nr_entries * sizeof(OPJ_UINT32));
933 channel_size = (OPJ_BYTE*) opj_malloc(nr_channels);
934 channel_sign = (OPJ_BYTE*) opj_malloc(nr_channels);
936 jp2_pclr = (opj_jp2_pclr_t*)opj_malloc(sizeof(opj_jp2_pclr_t));
937 jp2_pclr->channel_sign = channel_sign;
938 jp2_pclr->channel_size = channel_size;
939 jp2_pclr->entries = entries;
940 jp2_pclr->nr_entries = nr_entries;
941 jp2_pclr->nr_channels = nr_channels;
942 jp2_pclr->cmap = NULL;
944 jp2->color.jp2_pclr = jp2_pclr;
946 for(i = 0; i < nr_channels; ++i) {
947 opj_read_bytes(p_pclr_header_data, &l_value , 1); /* Bi */
948 ++p_pclr_header_data;
950 channel_size[i] = (l_value & 0x7f) + 1;
951 channel_sign[i] = (l_value & 0x80)? 1 : 0;
954 for(j = 0; j < nr_entries; ++j) {
955 for(i = 0; i < nr_channels; ++i) {
956 //*entries++ = cio_read(cio, channel_size[i]>>3);
957 opj_read_bytes(p_pclr_header_data, &l_value , channel_size[i]>>3); /* Cji */
958 p_pclr_header_data += channel_size[i]>>3;
959 *entries = (OPJ_UINT32) l_value;
968 static opj_bool jp2_read_cmap(opj_jp2_t *jp2, opj_cio_t *cio,
969 opj_jp2_box_t *box, opj_jp2_color_t *color)
971 opj_jp2_cmap_comp_t *cmap;
972 unsigned short i, nr_channels;
974 OPJ_ARG_NOT_USED(box);
975 OPJ_ARG_NOT_USED(jp2);
977 /* Need nr_channels: */
978 if(color->jp2_pclr == NULL) return OPJ_FALSE;
980 /* Part 1, I.5.3.5: 'There shall be at most one Component Mapping box
981 * inside a JP2 Header box' :
983 if(color->jp2_pclr->cmap) return OPJ_FALSE;
985 nr_channels = color->jp2_pclr->nr_channels;
986 cmap = (opj_jp2_cmap_comp_t*)
987 opj_malloc(nr_channels * sizeof(opj_jp2_cmap_comp_t));
989 for(i = 0; i < nr_channels; ++i)
991 cmap[i].cmp = (unsigned short)cio_read(cio, 2);
992 cmap[i].mtyp = cio_read(cio, 1);
993 cmap[i].pcol = cio_read(cio, 1);
996 color->jp2_pclr->cmap = cmap;
1000 }/* jp2_read_cmap() */
1003 * Reads the Component Mapping box.
1005 * @param p_cmap_header_data pointer to actual data (already read from file)
1006 * @param jp2 the jpeg2000 file codec.
1007 * @param p_cmap_header_size pointer that will hold the size of the color header
1008 * @param p_manager the user event manager.
1010 * @return true if the cdef header is valid, false else.
1012 static opj_bool jp2_read_cmap_v2( opj_jp2_v2_t * jp2,
1013 unsigned char * p_cmap_header_data,
1014 OPJ_UINT32 p_cmap_header_size,
1015 opj_event_mgr_t * p_manager
1018 opj_jp2_cmap_comp_t *cmap;
1019 OPJ_BYTE i, nr_channels;
1024 assert(p_cmap_header_data != 00);
1025 assert(p_manager != 00);
1027 /* Need nr_channels: */
1028 if(jp2->color.jp2_pclr == NULL) {
1029 opj_event_msg_v2(p_manager, EVT_ERROR, "Need to read a PCLR box before the CMAP box.\n");
1033 /* Part 1, I.5.3.5: 'There shall be at most one Component Mapping box
1034 * inside a JP2 Header box' :
1036 if(jp2->color.jp2_pclr->cmap) {
1037 opj_event_msg_v2(p_manager, EVT_ERROR, "Only one CMAP box is allowed.\n");
1041 nr_channels = jp2->color.jp2_pclr->nr_channels;
1042 cmap = (opj_jp2_cmap_comp_t*) opj_malloc(nr_channels * sizeof(opj_jp2_cmap_comp_t));
1044 for(i = 0; i < nr_channels; ++i) {
1045 opj_read_bytes(p_cmap_header_data, &l_value, 2); /* CMP^i */
1046 p_cmap_header_data +=2;
1047 cmap[i].cmp = (OPJ_UINT16) l_value;
1049 opj_read_bytes(p_cmap_header_data, &l_value, 1); /* MTYP^i */
1050 ++p_cmap_header_data;
1051 cmap[i].mtyp = (OPJ_BYTE) l_value;
1053 opj_read_bytes(p_cmap_header_data, &l_value, 1); /* PCOL^i */
1054 ++p_cmap_header_data;
1055 cmap[i].pcol = (OPJ_BYTE) l_value;
1058 jp2->color.jp2_pclr->cmap = cmap;
1063 static void jp2_apply_cdef(opj_image_t *image, opj_jp2_color_t *color)
1065 opj_jp2_cdef_info_t *info;
1066 OPJ_INT32 color_space;
1067 OPJ_UINT16 i, n, cn, typ, asoc, acn;
1069 color_space = image->color_space;
1070 info = color->jp2_cdef->info;
1071 n = color->jp2_cdef->n;
1073 for(i = 0; i < n; ++i)
1075 /* WATCH: acn = asoc - 1 ! */
1076 if((asoc = info[i].asoc) == 0) continue;
1078 cn = info[i].cn; typ = info[i].typ; acn = asoc - 1;
1082 opj_image_comp_t saved;
1084 memcpy(&saved, &image->comps[cn], sizeof(opj_image_comp_t));
1085 memcpy(&image->comps[cn], &image->comps[acn], sizeof(opj_image_comp_t));
1086 memcpy(&image->comps[acn], &saved, sizeof(opj_image_comp_t));
1088 info[i].asoc = cn + 1;
1089 info[acn].asoc = info[acn].cn + 1;
1093 if(color->jp2_cdef->info) opj_free(color->jp2_cdef->info);
1095 opj_free(color->jp2_cdef); color->jp2_cdef = NULL;
1097 }/* jp2_apply_cdef() */
1099 static opj_bool jp2_read_cdef(opj_jp2_t *jp2, opj_cio_t *cio,
1100 opj_jp2_box_t *box, opj_jp2_color_t *color)
1102 opj_jp2_cdef_info_t *info;
1103 unsigned short i, n;
1105 OPJ_ARG_NOT_USED(box);
1106 OPJ_ARG_NOT_USED(jp2);
1108 /* Part 1, I.5.3.6: 'The shall be at most one Channel Definition box
1109 * inside a JP2 Header box.'
1111 if(color->jp2_cdef) return OPJ_FALSE;
1113 if((n = (unsigned short)cio_read(cio, 2)) == 0) return OPJ_FALSE; /* szukw000: FIXME */
1115 info = (opj_jp2_cdef_info_t*)
1116 opj_malloc(n * sizeof(opj_jp2_cdef_info_t));
1118 color->jp2_cdef = (opj_jp2_cdef_t*)opj_malloc(sizeof(opj_jp2_cdef_t));
1119 color->jp2_cdef->info = info;
1120 color->jp2_cdef->n = n;
1122 for(i = 0; i < n; ++i)
1124 info[i].cn = (unsigned short)cio_read(cio, 2);
1125 info[i].typ = (unsigned short)cio_read(cio, 2);
1126 info[i].asoc = (unsigned short)cio_read(cio, 2);
1130 }/* jp2_read_cdef() */
1133 * Reads the Component Definition box.
1135 * @param p_cdef_header_data pointer to actual data (already read from file)
1136 * @param jp2 the jpeg2000 file codec.
1137 * @param p_cdef_header_size pointer that will hold the size of the color header
1138 * @param p_manager the user event manager.
1140 * @return true if the cdef header is valid, false else.
1142 static opj_bool jp2_read_cdef_v2( opj_jp2_v2_t * jp2,
1143 unsigned char * p_cdef_header_data,
1144 OPJ_UINT32 p_cdef_header_size,
1145 opj_event_mgr_t * p_manager
1148 opj_jp2_cdef_info_t *cdef_info;
1154 assert(p_cdef_header_data != 00);
1155 assert(p_manager != 00);
1157 /* Part 1, I.5.3.6: 'The shall be at most one Channel Definition box
1158 * inside a JP2 Header box.'*/
1159 if(jp2->color.jp2_cdef) return OPJ_FALSE;
1161 opj_read_bytes(p_cdef_header_data,&l_value ,2); /* N */
1162 p_cdef_header_data+= 2;
1164 if ( (OPJ_UINT16)l_value == 0){ /* szukw000: FIXME */
1165 opj_event_msg_v2(p_manager, EVT_ERROR, "Number of channel description is equal to zero in CDEF box.\n");
1169 cdef_info = (opj_jp2_cdef_info_t*) opj_malloc(l_value * sizeof(opj_jp2_cdef_info_t));
1171 jp2->color.jp2_cdef = (opj_jp2_cdef_t*)opj_malloc(sizeof(opj_jp2_cdef_t));
1172 jp2->color.jp2_cdef->info = cdef_info;
1173 jp2->color.jp2_cdef->n = (OPJ_UINT16) l_value;
1175 for(i = 0; i < jp2->color.jp2_cdef->n; ++i) {
1176 opj_read_bytes(p_cdef_header_data, &l_value, 2); /* Cn^i */
1177 p_cdef_header_data +=2;
1178 cdef_info[i].cn = (OPJ_UINT16) l_value;
1180 opj_read_bytes(p_cdef_header_data, &l_value, 2); /* Typ^i */
1181 p_cdef_header_data +=2;
1182 cdef_info[i].typ = (OPJ_UINT16) l_value;
1184 opj_read_bytes(p_cdef_header_data, &l_value, 2); /* Asoc^i */
1185 p_cdef_header_data +=2;
1186 cdef_info[i].asoc = (OPJ_UINT16) l_value;
1193 static opj_bool jp2_read_colr(opj_jp2_t *jp2, opj_cio_t *cio,
1194 opj_jp2_box_t *box, opj_jp2_color_t *color)
1197 opj_common_ptr cinfo;
1199 /* Part 1, I.5.3.3 : 'A conforming JP2 reader shall ignore all Colour
1200 * Specification boxes after the first.'
1202 if(color->jp2_has_colr) return OPJ_FALSE;
1206 jp2->meth = cio_read(cio, 1); /* METH */
1207 jp2->precedence = cio_read(cio, 1); /* PRECEDENCE */
1208 jp2->approx = cio_read(cio, 1); /* APPROX */
1212 jp2->enumcs = cio_read(cio, 4); /* EnumCS */
1218 skip_len = box->init_pos + box->length - cio_tell(cio);
1221 opj_event_msg(cinfo, EVT_ERROR, "Error with COLR box size\n");
1226 unsigned char *start;
1228 start = cio_getbp(cio);
1229 color->icc_profile_buf = (unsigned char*)opj_malloc(skip_len);
1230 color->icc_profile_len = skip_len;
1232 cio_skip(cio, box->init_pos + box->length - cio_tell(cio));
1234 memcpy(color->icc_profile_buf, start, skip_len);
1238 if (cio_tell(cio) - box->init_pos != box->length)
1240 opj_event_msg(cinfo, EVT_ERROR, "Error with COLR Box\n");
1243 color->jp2_has_colr = 1;
1246 }/* jp2_read_colr() */
1249 * Reads the Colour Specification box.
1251 * @param p_colr_header_data pointer to actual data (already read from file)
1252 * @param jp2 the jpeg2000 file codec.
1253 * @param p_colr_header_size pointer that will hold the size of the color header
1254 * @param p_colr_header_max_size maximum size of the header, any size bigger than this value should result the function to output false.
1255 * @param p_manager the user event manager.
1257 * @return true if the bpc header is valid, fale else.
1259 static opj_bool jp2_read_colr_v2( opj_jp2_v2_t * jp2,
1260 unsigned char * p_colr_header_data,
1261 OPJ_UINT32 p_colr_header_size,
1262 opj_event_mgr_t * p_manager
1269 assert(p_colr_header_data != 00);
1270 assert(p_manager != 00);
1272 if (p_colr_header_size < 3) {
1273 opj_event_msg_v2(p_manager, EVT_ERROR, "Bad COLR header box (bad size)\n");
1277 /* Part 1, I.5.3.3 : 'A conforming JP2 reader shall ignore all Colour
1278 * Specification boxes after the first.'
1280 if(jp2->color.jp2_has_colr) {
1281 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");
1282 p_colr_header_data += p_colr_header_size;
1286 opj_read_bytes(p_colr_header_data,&jp2->meth ,1); /* METH */
1287 ++p_colr_header_data;
1289 opj_read_bytes(p_colr_header_data,&jp2->precedence ,1); /* PRECEDENCE */
1290 ++p_colr_header_data;
1292 opj_read_bytes(p_colr_header_data,&jp2->approx ,1); /* APPROX */
1293 ++p_colr_header_data;
1295 if (jp2->meth == 1) {
1296 if (p_colr_header_size != 7) {
1297 opj_event_msg_v2(p_manager, EVT_ERROR, "Bad BPCC header box (bad size)\n");
1301 opj_read_bytes(p_colr_header_data,&jp2->enumcs ,4); /* EnumCS */
1303 else if (jp2->meth == 2) {
1305 int it_icc_value = 0;
1306 int icc_len = p_colr_header_size - 3;
1308 jp2->color.icc_profile_len = icc_len;
1309 jp2->color.icc_profile_buf = (unsigned char*) opj_malloc(icc_len);
1311 memset(jp2->color.icc_profile_buf, 0, icc_len * sizeof(unsigned char));
1313 for (it_icc_value = 0; it_icc_value < icc_len; ++it_icc_value)
1315 opj_read_bytes(p_colr_header_data,&l_value,1); /* icc values */
1316 ++p_colr_header_data;
1317 jp2->color.icc_profile_buf[it_icc_value] = (OPJ_BYTE) l_value;
1322 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);
1324 jp2->color.jp2_has_colr = 1;
1329 opj_bool jp2_read_jp2h(opj_jp2_t *jp2, opj_cio_t *cio, opj_jp2_color_t *color)
1334 opj_common_ptr cinfo = jp2->cinfo;
1336 jp2_read_boxhdr(cinfo, cio, &box);
1339 if (JP2_JP2H != box.type)
1341 if (box.type == JP2_JP2C)
1343 opj_event_msg(cinfo, EVT_ERROR, "Expected JP2H Marker\n");
1346 cio_skip(cio, box.length - 8);
1348 if(cio->bp >= cio->end) return OPJ_FALSE;
1350 jp2_read_boxhdr(cinfo, cio, &box);
1352 } while(JP2_JP2H != box.type);
1354 if (!jp2_read_ihdr(jp2, cio))
1356 jp2h_end = box.init_pos + box.length;
1358 if (jp2->bpc == 255)
1360 if (!jp2_read_bpcc(jp2, cio))
1363 jp2_read_boxhdr(cinfo, cio, &box);
1365 while(cio_tell(cio) < jp2h_end)
1367 if(box.type == JP2_COLR)
1369 if( !jp2_read_colr(jp2, cio, &box, color))
1371 cio_seek(cio, box.init_pos + 8);
1372 cio_skip(cio, box.length - 8);
1374 jp2_read_boxhdr(cinfo, cio, &box);
1377 if(box.type == JP2_CDEF)
1379 if( !jp2_read_cdef(jp2, cio, &box, color))
1381 cio_seek(cio, box.init_pos + 8);
1382 cio_skip(cio, box.length - 8);
1384 jp2_read_boxhdr(cinfo, cio, &box);
1387 if(box.type == JP2_PCLR)
1389 if( !jp2_read_pclr(jp2, cio, &box, color))
1391 cio_seek(cio, box.init_pos + 8);
1392 cio_skip(cio, box.length - 8);
1394 jp2_read_boxhdr(cinfo, cio, &box);
1397 if(box.type == JP2_CMAP)
1399 if( !jp2_read_cmap(jp2, cio, &box, color))
1401 cio_seek(cio, box.init_pos + 8);
1402 cio_skip(cio, box.length - 8);
1404 jp2_read_boxhdr(cinfo, cio, &box);
1407 cio_seek(cio, box.init_pos + 8);
1408 cio_skip(cio, box.length - 8);
1409 jp2_read_boxhdr(cinfo, cio, &box);
1411 }/* while(cio_tell(cio) < box_end) */
1413 cio_seek(cio, jp2h_end);
1415 /* Part 1, I.5.3.3 : 'must contain at least one' */
1416 return (color->jp2_has_colr == 1);
1418 }/* jp2_read_jp2h() */
1420 opj_image_t* opj_jp2_decode(opj_jp2_t *jp2, opj_cio_t *cio,
1421 opj_codestream_info_t *cstr_info)
1423 opj_common_ptr cinfo;
1424 opj_image_t *image = NULL;
1425 opj_jp2_color_t color;
1431 memset(&color, 0, sizeof(opj_jp2_color_t));
1435 if(!jp2_read_struct(jp2, cio, &color))
1437 free_color_data(&color);
1438 opj_event_msg(cinfo, EVT_ERROR, "Failed to decode jp2 structure\n");
1443 image = j2k_decode(jp2->j2k, cio, cstr_info);
1447 free_color_data(&color);
1448 opj_event_msg(cinfo, EVT_ERROR, "Failed to decode J2K image\n");
1452 /* Set Image Color Space */
1453 if (jp2->enumcs == 16)
1454 image->color_space = CLRSPC_SRGB;
1455 else if (jp2->enumcs == 17)
1456 image->color_space = CLRSPC_GRAY;
1457 else if (jp2->enumcs == 18)
1458 image->color_space = CLRSPC_SYCC;
1460 image->color_space = CLRSPC_UNKNOWN;
1464 jp2_apply_cdef(image, &color);
1468 /* Part 1, I.5.3.4: Either both or none : */
1469 if( !color.jp2_pclr->cmap)
1470 jp2_free_pclr(&color);
1472 jp2_apply_pclr(image, &color);
1474 if(color.icc_profile_buf)
1476 image->icc_profile_buf = color.icc_profile_buf;
1477 color.icc_profile_buf = NULL;
1478 image->icc_profile_len = color.icc_profile_len;
1482 }/* opj_jp2_decode() */
1484 opj_bool jp2_decode_v2( opj_jp2_v2_t *jp2,
1485 struct opj_stream_private *cio,
1486 opj_image_t* p_image,
1487 struct opj_event_mgr * p_manager)
1493 if( ! j2k_decode_v2(jp2->j2k, cio, p_image, p_manager) ) {
1494 opj_event_msg_v2(p_manager, EVT_ERROR, "Failed to decode the codestream in the JP2 file\n");
1498 /* Set Image Color Space */
1499 if (jp2->enumcs == 16)
1500 p_image->color_space = CLRSPC_SRGB;
1501 else if (jp2->enumcs == 17)
1502 p_image->color_space = CLRSPC_GRAY;
1503 else if (jp2->enumcs == 18)
1504 p_image->color_space = CLRSPC_SYCC;
1506 p_image->color_space = CLRSPC_UNKNOWN;
1508 /* Apply the color space if needed */
1509 if(jp2->color.jp2_cdef) {
1510 jp2_apply_cdef(p_image, &(jp2->color));
1513 if(jp2->color.jp2_pclr) {
1514 /* Part 1, I.5.3.4: Either both or none : */
1515 if( !jp2->color.jp2_pclr->cmap)
1516 jp2_free_pclr(&(jp2->color));
1518 jp2_apply_pclr(p_image, &(jp2->color));
1521 if(jp2->color.icc_profile_buf) {
1522 p_image->icc_profile_buf = jp2->color.icc_profile_buf;
1523 p_image->icc_profile_len = jp2->color.icc_profile_len;
1524 jp2->color.icc_profile_buf = NULL;
1531 void jp2_write_jp2h(opj_jp2_t *jp2, opj_cio_t *cio) {
1534 box.init_pos = cio_tell(cio);
1536 cio_write(cio, JP2_JP2H, 4); /* JP2H */
1538 jp2_write_ihdr(jp2, cio);
1540 if (jp2->bpc == 255) {
1541 jp2_write_bpcc(jp2, cio);
1543 jp2_write_colr(jp2, cio);
1545 box.length = cio_tell(cio) - box.init_pos;
1546 cio_seek(cio, box.init_pos);
1547 cio_write(cio, box.length, 4); /* L */
1548 cio_seek(cio, box.init_pos + box.length);
1551 static void jp2_write_ftyp(opj_jp2_t *jp2, opj_cio_t *cio) {
1555 box.init_pos = cio_tell(cio);
1557 cio_write(cio, JP2_FTYP, 4); /* FTYP */
1559 cio_write(cio, jp2->brand, 4); /* BR */
1560 cio_write(cio, jp2->minversion, 4); /* MinV */
1562 for (i = 0; i < jp2->numcl; i++) {
1563 cio_write(cio, jp2->cl[i], 4); /* CL */
1566 box.length = cio_tell(cio) - box.init_pos;
1567 cio_seek(cio, box.init_pos);
1568 cio_write(cio, box.length, 4); /* L */
1569 cio_seek(cio, box.init_pos + box.length);
1572 static opj_bool jp2_read_ftyp(opj_jp2_t *jp2, opj_cio_t *cio) {
1576 opj_common_ptr cinfo = jp2->cinfo;
1578 jp2_read_boxhdr(cinfo, cio, &box);
1580 if (JP2_FTYP != box.type) {
1581 opj_event_msg(cinfo, EVT_ERROR, "Expected FTYP Marker\n");
1585 jp2->brand = cio_read(cio, 4); /* BR */
1586 jp2->minversion = cio_read(cio, 4); /* MinV */
1587 jp2->numcl = (box.length - 16) / 4;
1588 jp2->cl = (unsigned int *) opj_malloc(jp2->numcl * sizeof(unsigned int));
1590 for (i = 0; i < (int)jp2->numcl; i++) {
1591 jp2->cl[i] = cio_read(cio, 4); /* CLi */
1594 if (cio_tell(cio) - box.init_pos != box.length) {
1595 opj_event_msg(cinfo, EVT_ERROR, "Error with FTYP Box\n");
1602 static int jp2_write_jp2c(opj_jp2_t *jp2, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info) {
1603 unsigned int j2k_codestream_offset, j2k_codestream_length;
1606 opj_j2k_t *j2k = jp2->j2k;
1608 box.init_pos = cio_tell(cio);
1610 cio_write(cio, JP2_JP2C, 4); /* JP2C */
1613 j2k_codestream_offset = cio_tell(cio);
1614 if(!j2k_encode(j2k, cio, image, cstr_info)) {
1615 opj_event_msg(j2k->cinfo, EVT_ERROR, "Failed to encode image\n");
1618 j2k_codestream_length = cio_tell(cio) - j2k_codestream_offset;
1620 jp2->j2k_codestream_offset = j2k_codestream_offset;
1621 jp2->j2k_codestream_length = j2k_codestream_length;
1623 box.length = 8 + jp2->j2k_codestream_length;
1624 cio_seek(cio, box.init_pos);
1625 cio_write(cio, box.length, 4); /* L */
1626 cio_seek(cio, box.init_pos + box.length);
1631 static opj_bool jp2_read_jp2c(opj_jp2_t *jp2, opj_cio_t *cio, unsigned int *j2k_codestream_length, unsigned int *j2k_codestream_offset) {
1634 opj_common_ptr cinfo = jp2->cinfo;
1636 jp2_read_boxhdr(cinfo, cio, &box);
1638 if(JP2_JP2C != box.type) {
1639 cio_skip(cio, box.length - 8);
1640 jp2_read_boxhdr(cinfo, cio, &box);
1642 } while(JP2_JP2C != box.type);
1644 *j2k_codestream_offset = cio_tell(cio);
1645 *j2k_codestream_length = box.length - 8;
1650 static void jp2_write_jp(opj_cio_t *cio) {
1653 box.init_pos = cio_tell(cio);
1655 cio_write(cio, JP2_JP, 4); /* JP2 signature */
1656 cio_write(cio, 0x0d0a870a, 4);
1658 box.length = cio_tell(cio) - box.init_pos;
1659 cio_seek(cio, box.init_pos);
1660 cio_write(cio, box.length, 4); /* L */
1661 cio_seek(cio, box.init_pos + box.length);
1664 static opj_bool jp2_read_jp(opj_jp2_t *jp2, opj_cio_t *cio) {
1667 opj_common_ptr cinfo = jp2->cinfo;
1669 jp2_read_boxhdr(cinfo, cio, &box);
1670 if (JP2_JP != box.type) {
1671 opj_event_msg(cinfo, EVT_ERROR, "Expected JP Marker\n");
1674 if (0x0d0a870a != cio_read(cio, 4)) {
1675 opj_event_msg(cinfo, EVT_ERROR, "Error with JP Marker\n");
1678 if (cio_tell(cio) - box.init_pos != box.length) {
1679 opj_event_msg(cinfo, EVT_ERROR, "Error with JP Box size\n");
1687 static opj_bool jp2_read_struct(opj_jp2_t *jp2, opj_cio_t *cio,
1688 opj_jp2_color_t *color) {
1689 if (!jp2_read_jp(jp2, cio))
1691 if (!jp2_read_ftyp(jp2, cio))
1693 if (!jp2_read_jp2h(jp2, cio, color))
1695 if (!jp2_read_jp2c(jp2, cio, &jp2->j2k_codestream_length, &jp2->j2k_codestream_offset))
1702 static int write_fidx( int offset_jp2c, int length_jp2c, int offset_idx, int length_idx, opj_cio_t *cio)
1706 lenp = cio_tell( cio);
1707 cio_skip( cio, 4); /* L [at the end] */
1708 cio_write( cio, JPIP_FIDX, 4); /* IPTR */
1710 write_prxy( offset_jp2c, length_jp2c, offset_idx, length_idx, cio);
1712 len = cio_tell( cio)-lenp;
1713 cio_seek( cio, lenp);
1714 cio_write( cio, len, 4); /* L */
1715 cio_seek( cio, lenp+len);
1720 static void write_prxy( int offset_jp2c, int length_jp2c, int offset_idx, int length_idx, opj_cio_t *cio)
1724 lenp = cio_tell( cio);
1725 cio_skip( cio, 4); /* L [at the end] */
1726 cio_write( cio, JPIP_PRXY, 4); /* IPTR */
1728 cio_write( cio, offset_jp2c, 8); /* OOFF */
1729 cio_write( cio, length_jp2c, 4); /* OBH part 1 */
1730 cio_write( cio, JP2_JP2C, 4); /* OBH part 2 */
1732 cio_write( cio, 1,1); /* NI */
1734 cio_write( cio, offset_idx, 8); /* IOFF */
1735 cio_write( cio, length_idx, 4); /* IBH part 1 */
1736 cio_write( cio, JPIP_CIDX, 4); /* IBH part 2 */
1738 len = cio_tell( cio)-lenp;
1739 cio_seek( cio, lenp);
1740 cio_write( cio, len, 4); /* L */
1741 cio_seek( cio, lenp+len);
1744 static void write_iptr( int offset, int length, opj_cio_t *cio)
1748 lenp = cio_tell( cio);
1749 cio_skip( cio, 4); /* L [at the end] */
1750 cio_write( cio, JPIP_IPTR, 4); /* IPTR */
1752 cio_write( cio, offset, 8);
1753 cio_write( cio, length, 8);
1755 len = cio_tell( cio)-lenp;
1756 cio_seek( cio, lenp);
1757 cio_write( cio, len, 4); /* L */
1758 cio_seek( cio, lenp+len);
1762 /* ----------------------------------------------------------------------- */
1763 /* JP2 decoder interface */
1764 /* ----------------------------------------------------------------------- */
1766 opj_jp2_t* jp2_create_decompress(opj_common_ptr cinfo) {
1767 opj_jp2_t *jp2 = (opj_jp2_t*) opj_calloc(1, sizeof(opj_jp2_t));
1770 /* create the J2K codec */
1771 jp2->j2k = j2k_create_decompress(cinfo);
1772 if(jp2->j2k == NULL) {
1773 jp2_destroy_decompress(jp2);
1780 void jp2_destroy_decompress(opj_jp2_t *jp2) {
1782 /* destroy the J2K codec */
1783 j2k_destroy_decompress(jp2->j2k);
1786 opj_free(jp2->comps);
1795 void jp2_setup_decoder(opj_jp2_t *jp2, opj_dparameters_t *parameters) {
1796 /* setup the J2K codec */
1797 j2k_setup_decoder(jp2->j2k, parameters);
1798 /* further JP2 initializations go here */
1801 void jp2_setup_decoder_v2(opj_jp2_v2_t *jp2, opj_dparameters_t *parameters)
1803 /* setup the J2K codec */
1804 j2k_setup_decoder_v2(jp2->j2k, parameters);
1806 /* further JP2 initializations go here */
1807 jp2->color.jp2_has_colr = 0;
1811 /* ----------------------------------------------------------------------- */
1812 /* JP2 encoder interface */
1813 /* ----------------------------------------------------------------------- */
1815 opj_jp2_t* jp2_create_compress(opj_common_ptr cinfo) {
1816 opj_jp2_t *jp2 = (opj_jp2_t*)opj_malloc(sizeof(opj_jp2_t));
1819 /* create the J2K codec */
1820 jp2->j2k = j2k_create_compress(cinfo);
1821 if(jp2->j2k == NULL) {
1822 jp2_destroy_compress(jp2);
1829 void jp2_destroy_compress(opj_jp2_t *jp2) {
1831 /* destroy the J2K codec */
1832 j2k_destroy_compress(jp2->j2k);
1835 opj_free(jp2->comps);
1844 void jp2_setup_encoder(opj_jp2_t *jp2, opj_cparameters_t *parameters, opj_image_t *image) {
1848 if(!jp2 || !parameters || !image)
1851 /* setup the J2K codec */
1852 /* ------------------- */
1854 /* Check if number of components respects standard */
1855 if (image->numcomps < 1 || image->numcomps > 16384) {
1856 opj_event_msg(jp2->cinfo, EVT_ERROR, "Invalid number of components specified while setting up JP2 encoder\n");
1860 j2k_setup_encoder(jp2->j2k, parameters, image);
1862 /* setup the JP2 codec */
1863 /* ------------------- */
1867 jp2->brand = JP2_JP2; /* BR */
1868 jp2->minversion = 0; /* MinV */
1870 jp2->cl = (unsigned int*) opj_malloc(jp2->numcl * sizeof(unsigned int));
1871 jp2->cl[0] = JP2_JP2; /* CL0 : JP2 */
1873 /* Image Header box */
1875 jp2->numcomps = image->numcomps; /* NC */
1876 jp2->comps = (opj_jp2_comps_t*) opj_malloc(jp2->numcomps * sizeof(opj_jp2_comps_t));
1877 jp2->h = image->y1 - image->y0; /* HEIGHT */
1878 jp2->w = image->x1 - image->x0; /* WIDTH */
1880 depth_0 = image->comps[0].prec - 1;
1881 sign = image->comps[0].sgnd;
1882 jp2->bpc = depth_0 + (sign << 7);
1883 for (i = 1; i < image->numcomps; i++) {
1884 int depth = image->comps[i].prec - 1;
1885 sign = image->comps[i].sgnd;
1886 if (depth_0 != depth)
1889 jp2->C = 7; /* C : Always 7 */
1890 jp2->UnkC = 0; /* UnkC, colorspace specified in colr box */
1891 jp2->IPR = 0; /* IPR, no intellectual property */
1893 /* BitsPerComponent box */
1895 for (i = 0; i < image->numcomps; i++) {
1896 jp2->comps[i].bpcc = image->comps[i].prec - 1 + (image->comps[i].sgnd << 7);
1899 if (image->color_space == 1)
1900 jp2->enumcs = 16; /* sRGB as defined by IEC 61966-2.1 */
1901 else if (image->color_space == 2)
1902 jp2->enumcs = 17; /* greyscale */
1903 else if (image->color_space == 3)
1904 jp2->enumcs = 18; /* YUV */
1905 jp2->precedence = 0; /* PRECEDENCE */
1906 jp2->approx = 0; /* APPROX */
1908 jp2->jpip_on = parameters->jpip_on;
1911 opj_bool opj_jp2_encode(opj_jp2_t *jp2, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info) {
1913 int pos_iptr, pos_cidx, pos_jp2c, len_jp2c, len_cidx, end_pos, pos_fidx, len_fidx;
1917 /* JPEG 2000 Signature box */
1920 jp2_write_ftyp(jp2, cio);
1921 /* JP2 Header box */
1922 jp2_write_jp2h(jp2, cio);
1925 pos_iptr = cio_tell( cio);
1926 cio_skip( cio, 24); /* IPTR further ! */
1928 pos_jp2c = cio_tell( cio);
1932 if(!(len_jp2c = jp2_write_jp2c( jp2, cio, image, cstr_info))){
1933 opj_event_msg(jp2->cinfo, EVT_ERROR, "Failed to encode image\n");
1938 pos_cidx = cio_tell( cio);
1940 len_cidx = write_cidx( pos_jp2c+8, cio, image, *cstr_info, len_jp2c-8);
1942 pos_fidx = cio_tell( cio);
1943 len_fidx = write_fidx( pos_jp2c, len_jp2c, pos_cidx, len_cidx, cio);
1945 end_pos = cio_tell( cio);
1947 cio_seek( cio, pos_iptr);
1948 write_iptr( pos_fidx, len_fidx, cio);
1949 cio_seek( cio, end_pos);
1956 * Ends the decompression procedures and possibiliy add data to be read after the
1959 opj_bool jp2_end_decompress(opj_jp2_v2_t *jp2, opj_stream_private_t *cio, opj_event_mgr_t * p_manager)
1964 assert(p_manager != 00);
1966 /* customization of the end encoding */
1967 jp2_setup_end_header_reading(jp2);
1970 if (! jp2_exec (jp2,jp2->m_procedure_list,cio,p_manager)) {
1974 return j2k_end_decompress(jp2->j2k, cio, p_manager);
1978 * Sets up the procedures to do on reading header after the codestream.
1979 * Developpers wanting to extend the library can add their own writting procedures.
1981 void jp2_setup_end_header_reading (opj_jp2_v2_t *jp2)
1985 opj_procedure_list_add_procedure(jp2->m_procedure_list,(void*)jp2_read_header_procedure );
1986 /* DEVELOPER CORNER, add your custom procedures */
1991 * Reads a jpeg2000 file header structure.
1993 * @param cio the stream to read data from.
1994 * @param jp2 the jpeg2000 file header structure.
1995 * @param p_manager the user event manager.
1997 * @return true if the box is valid.
1999 opj_bool jp2_read_header_procedure(
2001 opj_stream_private_t *cio,
2002 opj_event_mgr_t * p_manager)
2005 OPJ_UINT32 l_nb_bytes_read;
2006 const opj_jp2_header_handler_t * l_current_handler;
2007 OPJ_UINT32 l_last_data_size = BOX_SIZE;
2008 OPJ_UINT32 l_current_data_size;
2009 unsigned char * l_current_data = 00;
2014 assert(p_manager != 00);
2016 l_current_data = (unsigned char*)opj_malloc(l_last_data_size);
2018 if (l_current_data == 00) {
2019 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to handle jpeg2000 file header\n");
2022 memset(l_current_data, 0 , l_last_data_size);
2024 while (jp2_read_boxhdr_v2(&box,&l_nb_bytes_read,cio,p_manager)) {
2025 // is it the codestream box ?
2026 if (box.type == JP2_JP2C) {
2027 if (jp2->jp2_state & JP2_STATE_HEADER) {
2028 jp2->jp2_state |= JP2_STATE_CODESTREAM;
2029 opj_free(l_current_data);
2033 opj_event_msg_v2(p_manager, EVT_ERROR, "bad placed jpeg codestream\n");
2034 opj_free(l_current_data);
2038 else if (box.length == 0) {
2039 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot handle box of undefined sizes\n");
2040 opj_free(l_current_data);
2044 l_current_handler = jp2_find_handler(box.type);
2045 l_current_data_size = box.length - l_nb_bytes_read;
2047 if (l_current_handler != 00) {
2048 if (l_current_data_size > l_last_data_size) {
2049 l_current_data = (unsigned char*)opj_realloc(l_current_data,l_current_data_size);
2050 l_last_data_size = l_current_data_size;
2053 l_nb_bytes_read = opj_stream_read_data(cio,l_current_data,l_current_data_size,p_manager);
2054 if (l_nb_bytes_read != l_current_data_size) {
2055 opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with reading JPEG2000 box, stream error\n");
2059 if (! l_current_handler->handler(jp2,l_current_data,l_current_data_size,p_manager)) {
2060 opj_free(l_current_data);
2065 jp2->jp2_state |= JP2_STATE_UNKNOWN;
2066 if (opj_stream_skip(cio,l_current_data_size,p_manager) != l_current_data_size) {
2067 opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with skipping JPEG2000 box, stream error\n");
2068 opj_free(l_current_data);
2074 opj_free(l_current_data);
2080 * Excutes the given procedures on the given codec.
2082 * @param p_procedure_list the list of procedures to execute
2083 * @param jp2 the jpeg2000 file codec to execute the procedures on.
2084 * @param cio the stream to execute the procedures on.
2085 * @param p_manager the user manager.
2087 * @return true if all the procedures were successfully executed.
2091 opj_procedure_list_t * p_procedure_list,
2092 opj_stream_private_t *cio,
2093 opj_event_mgr_t * p_manager
2096 opj_bool (** l_procedure) (opj_jp2_v2_t * jp2, opj_stream_private_t *, opj_event_mgr_t *) = 00;
2097 opj_bool l_result = OPJ_TRUE;
2098 OPJ_UINT32 l_nb_proc, i;
2101 assert(p_procedure_list != 00);
2104 assert(p_manager != 00);
2106 l_nb_proc = opj_procedure_list_get_nb_procedures(p_procedure_list);
2107 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);
2109 for (i=0;i<l_nb_proc;++i) {
2110 l_result = l_result && (*l_procedure) (jp2,cio,p_manager);
2114 // and clear the procedure list at the end.
2115 opj_procedure_list_clear(p_procedure_list);
2121 * Finds the execution function related to the given box id.
2123 * @param p_id the id of the handler to fetch.
2125 * @return the given handler or 00 if it could not be found.
2127 const opj_jp2_header_handler_t * jp2_find_handler (int p_id)
2129 OPJ_UINT32 i, l_handler_size = sizeof(jp2_header) / sizeof(opj_jp2_header_handler_t);
2131 for (i=0;i<l_handler_size;++i) {
2132 if (jp2_header[i].id == p_id) {
2133 return &jp2_header[i];
2140 * Finds the image execution function related to the given box id.
2142 * @param p_id the id of the handler to fetch.
2144 * @return the given handler or 00 if it could not be found.
2146 static const opj_jp2_header_handler_t * jp2_img_find_handler (
2150 OPJ_UINT32 i, l_handler_size = sizeof(jp2_img_header) / sizeof(opj_jp2_header_handler_t);
2151 for (i=0;i<l_handler_size;++i)
2153 if (jp2_img_header[i].id == p_id) {
2154 return &jp2_img_header[i];
2163 * Reads a jpeg2000 file signature box.
2165 * @param p_header_data the data contained in the signature box.
2166 * @param jp2 the jpeg2000 file codec.
2167 * @param p_header_size the size of the data contained in the signature box.
2168 * @param p_manager the user event manager.
2170 * @return true if the file signature box is valid.
2172 opj_bool jp2_read_jp_v2(
2174 unsigned char * p_header_data,
2175 unsigned int p_header_size,
2176 opj_event_mgr_t * p_manager
2179 unsigned int l_magic_number;
2182 assert(p_header_data != 00);
2184 assert(p_manager != 00);
2186 if (jp2->jp2_state != JP2_STATE_NONE) {
2187 opj_event_msg_v2(p_manager, EVT_ERROR, "The signature box must be the first box in the file.\n");
2191 /* assure length of data is correct (4 -> magic number) */
2192 if (p_header_size != 4) {
2193 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with JP signature Box size\n");
2198 opj_read_bytes(p_header_data,&l_magic_number,4);
2199 if (l_magic_number != 0x0d0a870a ) {
2200 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with JP Signature : bad magic number\n");
2204 jp2->jp2_state |= JP2_STATE_SIGNATURE;
2211 * Reads a a FTYP box - File type box
2213 * @param p_header_data the data contained in the FTYP box.
2214 * @param jp2 the jpeg2000 file codec.
2215 * @param p_header_size the size of the data contained in the FTYP box.
2216 * @param p_manager the user event manager.
2218 * @return true if the FTYP box is valid.
2220 opj_bool jp2_read_ftyp_v2(
2222 unsigned char * p_header_data,
2223 unsigned int p_header_size,
2224 opj_event_mgr_t * p_manager
2227 OPJ_UINT32 i, l_remaining_bytes;
2230 assert(p_header_data != 00);
2232 assert(p_manager != 00);
2234 if (jp2->jp2_state != JP2_STATE_SIGNATURE) {
2235 opj_event_msg_v2(p_manager, EVT_ERROR, "The ftyp box must be the second box in the file.\n");
2239 /* assure length of data is correct */
2240 if (p_header_size < 8) {
2241 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with FTYP signature Box size\n");
2245 opj_read_bytes(p_header_data,&jp2->brand,4); /* BR */
2248 opj_read_bytes(p_header_data,&jp2->minversion,4); /* MinV */
2251 l_remaining_bytes = p_header_size - 8;
2253 /* the number of remaining bytes should be a multiple of 4 */
2254 if ((l_remaining_bytes & 0x3) != 0) {
2255 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with FTYP signature Box size\n");
2260 jp2->numcl = l_remaining_bytes >> 2;
2262 jp2->cl = (unsigned int *) opj_malloc(jp2->numcl * sizeof(unsigned int));
2263 if (jp2->cl == 00) {
2264 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory with FTYP Box\n");
2267 memset(jp2->cl,0,jp2->numcl * sizeof(unsigned int));
2270 for (i = 0; i < jp2->numcl; ++i)
2272 opj_read_bytes(p_header_data,&jp2->cl[i],4); /* CLi */
2276 jp2->jp2_state |= JP2_STATE_FILE_TYPE;
2283 * Reads the Jpeg2000 file Header box - JP2 Header box (warning, this is a super box).
2285 * @param p_header_data the data contained in the file header box.
2286 * @param jp2 the jpeg2000 file codec.
2287 * @param p_header_size the size of the data contained in the file header box.
2288 * @param p_manager the user event manager.
2290 * @return true if the JP2 Header box was successfully reconized.
2292 opj_bool jp2_read_jp2h_v2(
2294 unsigned char * p_header_data,
2295 unsigned int p_header_size,
2296 opj_event_mgr_t * p_manager
2299 OPJ_UINT32 l_box_size=0, l_current_data_size = 0;
2301 const opj_jp2_header_handler_t * l_current_handler;
2304 assert(p_header_data != 00);
2306 assert(p_manager != 00);
2308 /* make sure the box is well placed */
2309 if ((jp2->jp2_state & JP2_STATE_FILE_TYPE) != JP2_STATE_FILE_TYPE ) {
2310 opj_event_msg_v2(p_manager, EVT_ERROR, "The box must be the first box in the file.\n");
2314 jp2->jp2_img_state = JP2_IMG_STATE_NONE;
2316 /* iterate while remaining data */
2317 while (p_header_size > 0) {
2319 if (! jp2_read_boxhdr_char(&box,p_header_data,&l_box_size,p_header_size, p_manager)) {
2320 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream error while reading JP2 Header box\n");
2324 if (box.length > p_header_size) {
2325 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream error while reading JP2 Header box: box length is inconsistent.\n");
2329 l_current_handler = jp2_img_find_handler(box.type);
2330 l_current_data_size = box.length - l_box_size;
2331 p_header_data += l_box_size;
2333 if (l_current_handler != 00) {
2334 if (! l_current_handler->handler(jp2,p_header_data,l_current_data_size,p_manager)) {
2339 jp2->jp2_img_state |= JP2_IMG_STATE_UNKNOWN;
2342 p_header_data += l_current_data_size;
2343 p_header_size -= box.length;
2346 jp2->jp2_state |= JP2_STATE_HEADER;
2352 * Reads a box header. The box is the way data is packed inside a jpeg2000 file structure. Data is read from a character string
2354 * @param p_data the character string to read data from.
2355 * @param box the box structure to fill.
2356 * @param p_number_bytes_read pointer to an int that will store the number of bytes read from the stream (shoul usually be 2).
2357 * @param p_box_max_size the maximum number of bytes in the box.
2359 * @return true if the box is reconized, false otherwise
2361 static opj_bool jp2_read_boxhdr_char(
2364 OPJ_UINT32 * p_number_bytes_read,
2365 OPJ_UINT32 p_box_max_size,
2366 opj_event_mgr_t * p_manager
2372 assert(p_data != 00);
2374 assert(p_number_bytes_read != 00);
2375 assert(p_manager != 00);
2377 if (p_box_max_size < 8) {
2378 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot handle box of less than 8 bytes\n");
2382 /* process read data */
2383 opj_read_bytes(p_data, &l_value, 4);
2385 box->length = (OPJ_INT32)(l_value);
2387 opj_read_bytes(p_data, &l_value, 4);
2389 box->type = (OPJ_INT32)(l_value);
2391 *p_number_bytes_read = 8;
2393 // do we have a "special very large box ?"
2394 // read then the XLBox
2395 if (box->length == 1) {
2396 unsigned int l_xl_part_size;
2398 if (p_box_max_size < 16) {
2399 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot handle XL box of less than 16 bytes\n");
2403 opj_read_bytes(p_data,&l_xl_part_size, 4);
2405 *p_number_bytes_read += 4;
2407 if (l_xl_part_size != 0) {
2408 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot handle box sizes higher than 2^32\n");
2412 opj_read_bytes(p_data, &l_value, 4);
2413 *p_number_bytes_read += 4;
2414 box->length = (OPJ_INT32)(l_value);
2416 if (box->length == 0) {
2417 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot handle box of undefined sizes\n");
2421 else if (box->length == 0) {
2422 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot handle box of undefined sizes\n");
2431 * Reads a jpeg2000 file header structure.
2433 * @param cio the stream to read data from.
2434 * @param jp2 the jpeg2000 file header structure.
2435 * @param p_manager the user event manager.
2437 * @return true if the box is valid.
2439 opj_bool jp2_read_header( struct opj_stream_private *p_stream,
2441 opj_image_t** p_image,
2442 struct opj_event_mgr * p_manager
2447 assert(p_stream != 00);
2448 assert(p_manager != 00);
2450 /* customization of the validation */
2451 jp2_setup_decoding_validation (jp2);
2453 /* customization of the encoding */
2454 jp2_setup_header_reading(jp2);
2456 /* validation of the parameters codec */
2457 if (! jp2_exec(jp2,jp2->m_validation_list,p_stream,p_manager)) {
2462 if (! jp2_exec (jp2,jp2->m_procedure_list,p_stream,p_manager)) {
2466 return j2k_read_header( p_stream,
2473 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
2474 * are valid. Developpers wanting to extend the library can add their own validation procedures.
2476 void jp2_setup_decoding_validation (opj_jp2_v2_t *jp2)
2480 /* DEVELOPER CORNER, add your custom validation procedure */
2484 * Sets up the procedures to do on reading header.
2485 * Developpers wanting to extend the library can add their own writting procedures.
2487 void jp2_setup_header_reading (opj_jp2_v2_t *jp2)
2492 opj_procedure_list_add_procedure(jp2->m_procedure_list,(void*)jp2_read_header_procedure );
2493 /* DEVELOPER CORNER, add your custom procedures */
2498 * Reads a tile header.
2499 * @param p_j2k the jpeg2000 codec.
2500 * @param p_stream the stream to write data to.
2501 * @param p_manager the user event manager.
2503 opj_bool jp2_read_tile_header( opj_jp2_v2_t * p_jp2,
2504 OPJ_UINT32 * p_tile_index,
2505 OPJ_UINT32 * p_data_size,
2506 OPJ_INT32 * p_tile_x0, OPJ_INT32 * p_tile_y0,
2507 OPJ_INT32 * p_tile_x1, OPJ_INT32 * p_tile_y1,
2508 OPJ_UINT32 * p_nb_comps,
2510 opj_stream_private_t *p_stream,
2511 opj_event_mgr_t * p_manager )
2513 return j2k_read_tile_header(p_jp2->j2k,
2516 p_tile_x0, p_tile_y0,
2517 p_tile_x1, p_tile_y1,
2526 * @param p_j2k the jpeg2000 codec.
2527 * @param p_stream the stream to write data to.
2528 * @param p_manager the user event manager.
2530 opj_bool jp2_decode_tile (
2531 opj_jp2_v2_t * p_jp2,
2532 OPJ_UINT32 p_tile_index,
2534 OPJ_UINT32 p_data_size,
2535 opj_stream_private_t *p_stream,
2536 opj_event_mgr_t * p_manager
2539 return j2k_decode_tile (p_jp2->j2k,p_tile_index,p_data,p_data_size,p_stream,p_manager);
2543 * Destroys a jpeg2000 file decompressor.
2545 * @param jp2 a jpeg2000 file decompressor.
2547 void jp2_destroy(opj_jp2_v2_t *jp2)
2550 /* destroy the J2K codec */
2551 j2k_destroy(jp2->j2k);
2555 opj_free(jp2->comps);
2564 if (jp2->color.icc_profile_buf) {
2565 opj_free(jp2->color.icc_profile_buf);
2566 jp2->color.icc_profile_buf = 00;
2569 if (jp2->color.jp2_cdef) {
2570 if (jp2->color.jp2_cdef->info) {
2571 opj_free(jp2->color.jp2_cdef->info);
2572 jp2->color.jp2_cdef->info = NULL;
2575 opj_free(jp2->color.jp2_cdef);
2576 jp2->color.jp2_cdef = 00;
2579 if (jp2->color.jp2_pclr) {
2580 if (jp2->color.jp2_pclr->cmap) {
2581 opj_free(jp2->color.jp2_pclr->cmap);
2582 jp2->color.jp2_pclr->cmap = NULL;
2584 if (jp2->color.jp2_pclr->channel_sign) {
2585 opj_free(jp2->color.jp2_pclr->channel_sign);
2586 jp2->color.jp2_pclr->channel_sign = NULL;
2588 if (jp2->color.jp2_pclr->channel_size) {
2589 opj_free(jp2->color.jp2_pclr->channel_size);
2590 jp2->color.jp2_pclr->channel_size = NULL;
2592 if (jp2->color.jp2_pclr->entries) {
2593 opj_free(jp2->color.jp2_pclr->entries);
2594 jp2->color.jp2_pclr->entries = NULL;
2597 opj_free(jp2->color.jp2_pclr);
2598 jp2->color.jp2_pclr = 00;
2601 if (jp2->m_validation_list) {
2602 opj_procedure_list_destroy(jp2->m_validation_list);
2603 jp2->m_validation_list = 00;
2606 if (jp2->m_procedure_list) {
2607 opj_procedure_list_destroy(jp2->m_procedure_list);
2608 jp2->m_procedure_list = 00;
2616 * Sets the given area to be decoded. This function should be called right after opj_read_header and before any tile header reading.
2618 * @param p_jp2 the jpeg2000 codec.
2619 * @param p_end_x the right position of the rectangle to decode (in image coordinates).
2620 * @param p_start_y the up position of the rectangle to decode (in image coordinates).
2621 * @param p_end_y the bottom position of the rectangle to decode (in image coordinates).
2622 * @param p_manager the user event manager
2624 * @return true if the area could be set.
2626 opj_bool jp2_set_decode_area( opj_jp2_v2_t *p_jp2,
2627 opj_image_t* p_image,
2628 OPJ_INT32 p_start_x, OPJ_INT32 p_start_y,
2629 OPJ_INT32 p_end_x, OPJ_INT32 p_end_y,
2630 struct opj_event_mgr * p_manager )
2632 return j2k_set_decode_area(p_jp2->j2k, p_image, p_start_x, p_start_y, p_end_x, p_end_y, p_manager);
2636 * Get the decoded tile.
2638 * @param jp2 the jpeg2000 codec.
2639 * @param p_stream input_stream
2640 * @param p_image output image. .
2641 * @param p_manager the user event manager
2642 * @param tile_index index of the tile we want decode
2644 * @return true if succeed.
2646 opj_bool jp2_get_tile( opj_jp2_v2_t *jp2,
2647 opj_stream_private_t *p_stream,
2648 opj_image_t* p_image,
2649 struct opj_event_mgr * p_manager,
2650 OPJ_UINT32 tile_index )
2655 opj_event_msg_v2(p_manager, EVT_WARNING, "JP2 box which are after the codestream will not be read by this function.\n");
2657 if (! j2k_get_tile(jp2->j2k, p_stream, p_image, p_manager, tile_index) ){
2658 opj_event_msg_v2(p_manager, EVT_ERROR, "Failed to decode the codestream in the JP2 file\n");
2662 /* Set Image Color Space */
2663 if (jp2->enumcs == 16)
2664 p_image->color_space = CLRSPC_SRGB;
2665 else if (jp2->enumcs == 17)
2666 p_image->color_space = CLRSPC_GRAY;
2667 else if (jp2->enumcs == 18)
2668 p_image->color_space = CLRSPC_SYCC;
2670 p_image->color_space = CLRSPC_UNKNOWN;
2672 /* Apply the color space if needed */
2673 if(jp2->color.jp2_cdef) {
2674 jp2_apply_cdef(p_image, &(jp2->color));
2677 if(jp2->color.jp2_pclr) {
2678 /* Part 1, I.5.3.4: Either both or none : */
2679 if( !jp2->color.jp2_pclr->cmap)
2680 jp2_free_pclr(&(jp2->color));
2682 jp2_apply_pclr(p_image, &(jp2->color));
2685 if(jp2->color.icc_profile_buf) {
2686 p_image->icc_profile_buf = jp2->color.icc_profile_buf;
2687 p_image->icc_profile_len = jp2->color.icc_profile_len;
2688 jp2->color.icc_profile_buf = NULL;
2696 /* ----------------------------------------------------------------------- */
2697 /* JP2 encoder interface */
2698 /* ----------------------------------------------------------------------- */
2700 opj_jp2_v2_t* jp2_create(opj_bool p_is_decoder)
2702 opj_jp2_v2_t *jp2 = (opj_jp2_v2_t*)opj_malloc(sizeof(opj_jp2_v2_t));
2704 memset(jp2,0,sizeof(opj_jp2_t));
2706 /* create the J2K codec */
2707 if (! p_is_decoder) {
2708 jp2->j2k = j2k_create_compress_v2();
2711 jp2->j2k = j2k_create_decompress_v2();
2714 if (jp2->j2k == 00) {
2719 /* Color structure */
2720 jp2->color.icc_profile_buf = NULL;
2721 jp2->color.icc_profile_len = 0;
2722 jp2->color.jp2_cdef = NULL;
2723 jp2->color.jp2_pclr = NULL;
2724 jp2->color.jp2_has_colr = 0;
2726 // validation list creation
2727 jp2->m_validation_list = opj_procedure_list_create();
2728 if (! jp2->m_validation_list) {
2733 // execution list creation
2734 jp2->m_procedure_list = opj_procedure_list_create();
2735 if (! jp2->m_procedure_list) {
2744 void jp2_dump(opj_jp2_v2_t* p_jp2, OPJ_INT32 flag, FILE* out_stream)
2747 assert(p_jp2 != 00);
2749 j2k_dump(p_jp2->j2k,
2754 opj_codestream_index_t* jp2_get_cstr_index(opj_jp2_v2_t* p_jp2)
2756 return j2k_get_cstr_index(p_jp2->j2k);
2759 opj_codestream_info_v2_t* jp2_get_cstr_info(opj_jp2_v2_t* p_jp2)
2761 return j2k_get_cstr_info(p_jp2->j2k);
2764 opj_bool jp2_set_decoded_resolution_factor(opj_jp2_v2_t *p_jp2, OPJ_UINT32 res_factor, opj_event_mgr_t * p_manager)
2766 return j2k_set_decoded_resolution_factor(p_jp2->j2k, res_factor, p_manager);