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 && !jp2->ignore_pclr_cmap_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 && !jp2->ignore_pclr_cmap_cdef)
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 && !jp2->ignore_pclr_cmap_cdef)
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 if (!jp2->ignore_pclr_cmap_cdef){
1454 /* Set Image Color Space */
1455 if (jp2->enumcs == 16)
1456 image->color_space = CLRSPC_SRGB;
1457 else if (jp2->enumcs == 17)
1458 image->color_space = CLRSPC_GRAY;
1459 else if (jp2->enumcs == 18)
1460 image->color_space = CLRSPC_SYCC;
1462 image->color_space = CLRSPC_UNKNOWN;
1466 jp2_apply_cdef(image, &color);
1470 /* Part 1, I.5.3.4: Either both or none : */
1471 if( !color.jp2_pclr->cmap)
1472 jp2_free_pclr(&color);
1474 jp2_apply_pclr(image, &color);
1476 if(color.icc_profile_buf)
1478 image->icc_profile_buf = color.icc_profile_buf;
1479 color.icc_profile_buf = NULL;
1480 image->icc_profile_len = color.icc_profile_len;
1486 }/* opj_jp2_decode() */
1488 opj_bool jp2_decode_v2( opj_jp2_v2_t *jp2,
1489 struct opj_stream_private *cio,
1490 opj_image_t* p_image,
1491 struct opj_event_mgr * p_manager)
1497 if( ! j2k_decode_v2(jp2->j2k, cio, p_image, p_manager) ) {
1498 opj_event_msg_v2(p_manager, EVT_ERROR, "Failed to decode the codestream in the JP2 file\n");
1502 /* Set Image Color Space */
1503 if (jp2->enumcs == 16)
1504 p_image->color_space = CLRSPC_SRGB;
1505 else if (jp2->enumcs == 17)
1506 p_image->color_space = CLRSPC_GRAY;
1507 else if (jp2->enumcs == 18)
1508 p_image->color_space = CLRSPC_SYCC;
1510 p_image->color_space = CLRSPC_UNKNOWN;
1512 /* Apply the color space if needed */
1513 if(jp2->color.jp2_cdef) {
1514 jp2_apply_cdef(p_image, &(jp2->color));
1517 if(jp2->color.jp2_pclr) {
1518 /* Part 1, I.5.3.4: Either both or none : */
1519 if( !jp2->color.jp2_pclr->cmap)
1520 jp2_free_pclr(&(jp2->color));
1522 jp2_apply_pclr(p_image, &(jp2->color));
1525 if(jp2->color.icc_profile_buf) {
1526 p_image->icc_profile_buf = jp2->color.icc_profile_buf;
1527 p_image->icc_profile_len = jp2->color.icc_profile_len;
1528 jp2->color.icc_profile_buf = NULL;
1535 void jp2_write_jp2h(opj_jp2_t *jp2, opj_cio_t *cio) {
1538 box.init_pos = cio_tell(cio);
1540 cio_write(cio, JP2_JP2H, 4); /* JP2H */
1542 jp2_write_ihdr(jp2, cio);
1544 if (jp2->bpc == 255) {
1545 jp2_write_bpcc(jp2, cio);
1547 jp2_write_colr(jp2, cio);
1549 box.length = cio_tell(cio) - box.init_pos;
1550 cio_seek(cio, box.init_pos);
1551 cio_write(cio, box.length, 4); /* L */
1552 cio_seek(cio, box.init_pos + box.length);
1555 static void jp2_write_ftyp(opj_jp2_t *jp2, opj_cio_t *cio) {
1559 box.init_pos = cio_tell(cio);
1561 cio_write(cio, JP2_FTYP, 4); /* FTYP */
1563 cio_write(cio, jp2->brand, 4); /* BR */
1564 cio_write(cio, jp2->minversion, 4); /* MinV */
1566 for (i = 0; i < jp2->numcl; i++) {
1567 cio_write(cio, jp2->cl[i], 4); /* CL */
1570 box.length = cio_tell(cio) - box.init_pos;
1571 cio_seek(cio, box.init_pos);
1572 cio_write(cio, box.length, 4); /* L */
1573 cio_seek(cio, box.init_pos + box.length);
1576 static opj_bool jp2_read_ftyp(opj_jp2_t *jp2, opj_cio_t *cio) {
1580 opj_common_ptr cinfo = jp2->cinfo;
1582 jp2_read_boxhdr(cinfo, cio, &box);
1584 if (JP2_FTYP != box.type) {
1585 opj_event_msg(cinfo, EVT_ERROR, "Expected FTYP Marker\n");
1589 jp2->brand = cio_read(cio, 4); /* BR */
1590 jp2->minversion = cio_read(cio, 4); /* MinV */
1591 jp2->numcl = (box.length - 16) / 4;
1592 jp2->cl = (unsigned int *) opj_malloc(jp2->numcl * sizeof(unsigned int));
1594 for (i = 0; i < (int)jp2->numcl; i++) {
1595 jp2->cl[i] = cio_read(cio, 4); /* CLi */
1598 if (cio_tell(cio) - box.init_pos != box.length) {
1599 opj_event_msg(cinfo, EVT_ERROR, "Error with FTYP Box\n");
1606 static int jp2_write_jp2c(opj_jp2_t *jp2, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info) {
1607 unsigned int j2k_codestream_offset, j2k_codestream_length;
1610 opj_j2k_t *j2k = jp2->j2k;
1612 box.init_pos = cio_tell(cio);
1614 cio_write(cio, JP2_JP2C, 4); /* JP2C */
1617 j2k_codestream_offset = cio_tell(cio);
1618 if(!j2k_encode(j2k, cio, image, cstr_info)) {
1619 opj_event_msg(j2k->cinfo, EVT_ERROR, "Failed to encode image\n");
1622 j2k_codestream_length = cio_tell(cio) - j2k_codestream_offset;
1624 jp2->j2k_codestream_offset = j2k_codestream_offset;
1625 jp2->j2k_codestream_length = j2k_codestream_length;
1627 box.length = 8 + jp2->j2k_codestream_length;
1628 cio_seek(cio, box.init_pos);
1629 cio_write(cio, box.length, 4); /* L */
1630 cio_seek(cio, box.init_pos + box.length);
1635 static opj_bool jp2_read_jp2c(opj_jp2_t *jp2, opj_cio_t *cio, unsigned int *j2k_codestream_length, unsigned int *j2k_codestream_offset) {
1638 opj_common_ptr cinfo = jp2->cinfo;
1640 jp2_read_boxhdr(cinfo, cio, &box);
1642 if(JP2_JP2C != box.type) {
1643 cio_skip(cio, box.length - 8);
1644 jp2_read_boxhdr(cinfo, cio, &box);
1646 } while(JP2_JP2C != box.type);
1648 *j2k_codestream_offset = cio_tell(cio);
1649 *j2k_codestream_length = box.length - 8;
1654 static void jp2_write_jp(opj_cio_t *cio) {
1657 box.init_pos = cio_tell(cio);
1659 cio_write(cio, JP2_JP, 4); /* JP2 signature */
1660 cio_write(cio, 0x0d0a870a, 4);
1662 box.length = cio_tell(cio) - box.init_pos;
1663 cio_seek(cio, box.init_pos);
1664 cio_write(cio, box.length, 4); /* L */
1665 cio_seek(cio, box.init_pos + box.length);
1668 static opj_bool jp2_read_jp(opj_jp2_t *jp2, opj_cio_t *cio) {
1671 opj_common_ptr cinfo = jp2->cinfo;
1673 jp2_read_boxhdr(cinfo, cio, &box);
1674 if (JP2_JP != box.type) {
1675 opj_event_msg(cinfo, EVT_ERROR, "Expected JP Marker\n");
1678 if (0x0d0a870a != cio_read(cio, 4)) {
1679 opj_event_msg(cinfo, EVT_ERROR, "Error with JP Marker\n");
1682 if (cio_tell(cio) - box.init_pos != box.length) {
1683 opj_event_msg(cinfo, EVT_ERROR, "Error with JP Box size\n");
1691 static opj_bool jp2_read_struct(opj_jp2_t *jp2, opj_cio_t *cio,
1692 opj_jp2_color_t *color) {
1693 if (!jp2_read_jp(jp2, cio))
1695 if (!jp2_read_ftyp(jp2, cio))
1697 if (!jp2_read_jp2h(jp2, cio, color))
1699 if (!jp2_read_jp2c(jp2, cio, &jp2->j2k_codestream_length, &jp2->j2k_codestream_offset))
1706 static int write_fidx( int offset_jp2c, int length_jp2c, int offset_idx, int length_idx, opj_cio_t *cio)
1710 lenp = cio_tell( cio);
1711 cio_skip( cio, 4); /* L [at the end] */
1712 cio_write( cio, JPIP_FIDX, 4); /* IPTR */
1714 write_prxy( offset_jp2c, length_jp2c, offset_idx, length_idx, cio);
1716 len = cio_tell( cio)-lenp;
1717 cio_seek( cio, lenp);
1718 cio_write( cio, len, 4); /* L */
1719 cio_seek( cio, lenp+len);
1724 static void write_prxy( int offset_jp2c, int length_jp2c, int offset_idx, int length_idx, opj_cio_t *cio)
1728 lenp = cio_tell( cio);
1729 cio_skip( cio, 4); /* L [at the end] */
1730 cio_write( cio, JPIP_PRXY, 4); /* IPTR */
1732 cio_write( cio, offset_jp2c, 8); /* OOFF */
1733 cio_write( cio, length_jp2c, 4); /* OBH part 1 */
1734 cio_write( cio, JP2_JP2C, 4); /* OBH part 2 */
1736 cio_write( cio, 1,1); /* NI */
1738 cio_write( cio, offset_idx, 8); /* IOFF */
1739 cio_write( cio, length_idx, 4); /* IBH part 1 */
1740 cio_write( cio, JPIP_CIDX, 4); /* IBH part 2 */
1742 len = cio_tell( cio)-lenp;
1743 cio_seek( cio, lenp);
1744 cio_write( cio, len, 4); /* L */
1745 cio_seek( cio, lenp+len);
1748 static void write_iptr( int offset, int length, opj_cio_t *cio)
1752 lenp = cio_tell( cio);
1753 cio_skip( cio, 4); /* L [at the end] */
1754 cio_write( cio, JPIP_IPTR, 4); /* IPTR */
1756 cio_write( cio, offset, 8);
1757 cio_write( cio, length, 8);
1759 len = cio_tell( cio)-lenp;
1760 cio_seek( cio, lenp);
1761 cio_write( cio, len, 4); /* L */
1762 cio_seek( cio, lenp+len);
1766 /* ----------------------------------------------------------------------- */
1767 /* JP2 decoder interface */
1768 /* ----------------------------------------------------------------------- */
1770 opj_jp2_t* jp2_create_decompress(opj_common_ptr cinfo) {
1771 opj_jp2_t *jp2 = (opj_jp2_t*) opj_calloc(1, sizeof(opj_jp2_t));
1774 /* create the J2K codec */
1775 jp2->j2k = j2k_create_decompress(cinfo);
1776 if(jp2->j2k == NULL) {
1777 jp2_destroy_decompress(jp2);
1784 void jp2_destroy_decompress(opj_jp2_t *jp2) {
1786 /* destroy the J2K codec */
1787 j2k_destroy_decompress(jp2->j2k);
1790 opj_free(jp2->comps);
1799 void jp2_setup_decoder(opj_jp2_t *jp2, opj_dparameters_t *parameters) {
1800 /* setup the J2K codec */
1801 j2k_setup_decoder(jp2->j2k, parameters);
1802 /* further JP2 initializations go here */
1803 jp2->ignore_pclr_cmap_cdef = parameters->flags & OPJ_DPARAMETERS_IGNORE_PCLR_CMAP_CDEF_FLAG;
1806 void jp2_setup_decoder_v2(opj_jp2_v2_t *jp2, opj_dparameters_t *parameters)
1808 /* setup the J2K codec */
1809 j2k_setup_decoder_v2(jp2->j2k, parameters);
1811 /* further JP2 initializations go here */
1812 jp2->color.jp2_has_colr = 0;
1816 /* ----------------------------------------------------------------------- */
1817 /* JP2 encoder interface */
1818 /* ----------------------------------------------------------------------- */
1820 opj_jp2_t* jp2_create_compress(opj_common_ptr cinfo) {
1821 opj_jp2_t *jp2 = (opj_jp2_t*)opj_malloc(sizeof(opj_jp2_t));
1824 /* create the J2K codec */
1825 jp2->j2k = j2k_create_compress(cinfo);
1826 if(jp2->j2k == NULL) {
1827 jp2_destroy_compress(jp2);
1834 void jp2_destroy_compress(opj_jp2_t *jp2) {
1836 /* destroy the J2K codec */
1837 j2k_destroy_compress(jp2->j2k);
1840 opj_free(jp2->comps);
1849 void jp2_setup_encoder(opj_jp2_t *jp2, opj_cparameters_t *parameters, opj_image_t *image) {
1853 if(!jp2 || !parameters || !image)
1856 /* setup the J2K codec */
1857 /* ------------------- */
1859 /* Check if number of components respects standard */
1860 if (image->numcomps < 1 || image->numcomps > 16384) {
1861 opj_event_msg(jp2->cinfo, EVT_ERROR, "Invalid number of components specified while setting up JP2 encoder\n");
1865 j2k_setup_encoder(jp2->j2k, parameters, image);
1867 /* setup the JP2 codec */
1868 /* ------------------- */
1872 jp2->brand = JP2_JP2; /* BR */
1873 jp2->minversion = 0; /* MinV */
1875 jp2->cl = (unsigned int*) opj_malloc(jp2->numcl * sizeof(unsigned int));
1876 jp2->cl[0] = JP2_JP2; /* CL0 : JP2 */
1878 /* Image Header box */
1880 jp2->numcomps = image->numcomps; /* NC */
1881 jp2->comps = (opj_jp2_comps_t*) opj_malloc(jp2->numcomps * sizeof(opj_jp2_comps_t));
1882 jp2->h = image->y1 - image->y0; /* HEIGHT */
1883 jp2->w = image->x1 - image->x0; /* WIDTH */
1885 depth_0 = image->comps[0].prec - 1;
1886 sign = image->comps[0].sgnd;
1887 jp2->bpc = depth_0 + (sign << 7);
1888 for (i = 1; i < image->numcomps; i++) {
1889 int depth = image->comps[i].prec - 1;
1890 sign = image->comps[i].sgnd;
1891 if (depth_0 != depth)
1894 jp2->C = 7; /* C : Always 7 */
1895 jp2->UnkC = 0; /* UnkC, colorspace specified in colr box */
1896 jp2->IPR = 0; /* IPR, no intellectual property */
1898 /* BitsPerComponent box */
1900 for (i = 0; i < image->numcomps; i++) {
1901 jp2->comps[i].bpcc = image->comps[i].prec - 1 + (image->comps[i].sgnd << 7);
1904 if (image->color_space == 1)
1905 jp2->enumcs = 16; /* sRGB as defined by IEC 61966-2.1 */
1906 else if (image->color_space == 2)
1907 jp2->enumcs = 17; /* greyscale */
1908 else if (image->color_space == 3)
1909 jp2->enumcs = 18; /* YUV */
1910 jp2->precedence = 0; /* PRECEDENCE */
1911 jp2->approx = 0; /* APPROX */
1913 jp2->jpip_on = parameters->jpip_on;
1916 opj_bool opj_jp2_encode(opj_jp2_t *jp2, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info) {
1918 int pos_iptr, pos_cidx, pos_jp2c, len_jp2c, len_cidx, end_pos, pos_fidx, len_fidx;
1922 /* JPEG 2000 Signature box */
1925 jp2_write_ftyp(jp2, cio);
1926 /* JP2 Header box */
1927 jp2_write_jp2h(jp2, cio);
1930 pos_iptr = cio_tell( cio);
1931 cio_skip( cio, 24); /* IPTR further ! */
1933 pos_jp2c = cio_tell( cio);
1937 if(!(len_jp2c = jp2_write_jp2c( jp2, cio, image, cstr_info))){
1938 opj_event_msg(jp2->cinfo, EVT_ERROR, "Failed to encode image\n");
1943 pos_cidx = cio_tell( cio);
1945 len_cidx = write_cidx( pos_jp2c+8, cio, image, *cstr_info, len_jp2c-8);
1947 pos_fidx = cio_tell( cio);
1948 len_fidx = write_fidx( pos_jp2c, len_jp2c, pos_cidx, len_cidx, cio);
1950 end_pos = cio_tell( cio);
1952 cio_seek( cio, pos_iptr);
1953 write_iptr( pos_fidx, len_fidx, cio);
1954 cio_seek( cio, end_pos);
1961 * Ends the decompression procedures and possibiliy add data to be read after the
1964 opj_bool jp2_end_decompress(opj_jp2_v2_t *jp2, opj_stream_private_t *cio, opj_event_mgr_t * p_manager)
1969 assert(p_manager != 00);
1971 /* customization of the end encoding */
1972 jp2_setup_end_header_reading(jp2);
1975 if (! jp2_exec (jp2,jp2->m_procedure_list,cio,p_manager)) {
1979 return j2k_end_decompress(jp2->j2k, cio, p_manager);
1983 * Sets up the procedures to do on reading header after the codestream.
1984 * Developpers wanting to extend the library can add their own writting procedures.
1986 void jp2_setup_end_header_reading (opj_jp2_v2_t *jp2)
1990 opj_procedure_list_add_procedure(jp2->m_procedure_list,(void*)jp2_read_header_procedure );
1991 /* DEVELOPER CORNER, add your custom procedures */
1996 * Reads a jpeg2000 file header structure.
1998 * @param cio the stream to read data from.
1999 * @param jp2 the jpeg2000 file header structure.
2000 * @param p_manager the user event manager.
2002 * @return true if the box is valid.
2004 opj_bool jp2_read_header_procedure(
2006 opj_stream_private_t *cio,
2007 opj_event_mgr_t * p_manager)
2010 OPJ_UINT32 l_nb_bytes_read;
2011 const opj_jp2_header_handler_t * l_current_handler;
2012 OPJ_UINT32 l_last_data_size = BOX_SIZE;
2013 OPJ_UINT32 l_current_data_size;
2014 unsigned char * l_current_data = 00;
2019 assert(p_manager != 00);
2021 l_current_data = (unsigned char*)opj_malloc(l_last_data_size);
2023 if (l_current_data == 00) {
2024 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to handle jpeg2000 file header\n");
2027 memset(l_current_data, 0 , l_last_data_size);
2029 while (jp2_read_boxhdr_v2(&box,&l_nb_bytes_read,cio,p_manager)) {
2030 // is it the codestream box ?
2031 if (box.type == JP2_JP2C) {
2032 if (jp2->jp2_state & JP2_STATE_HEADER) {
2033 jp2->jp2_state |= JP2_STATE_CODESTREAM;
2034 opj_free(l_current_data);
2038 opj_event_msg_v2(p_manager, EVT_ERROR, "bad placed jpeg codestream\n");
2039 opj_free(l_current_data);
2043 else if (box.length == 0) {
2044 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot handle box of undefined sizes\n");
2045 opj_free(l_current_data);
2049 l_current_handler = jp2_find_handler(box.type);
2050 l_current_data_size = box.length - l_nb_bytes_read;
2052 if (l_current_handler != 00) {
2053 if (l_current_data_size > l_last_data_size) {
2054 l_current_data = (unsigned char*)opj_realloc(l_current_data,l_current_data_size);
2055 if (!l_current_data){
2056 opj_free(l_current_data);
2059 l_last_data_size = l_current_data_size;
2062 l_nb_bytes_read = opj_stream_read_data(cio,l_current_data,l_current_data_size,p_manager);
2063 if (l_nb_bytes_read != l_current_data_size) {
2064 opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with reading JPEG2000 box, stream error\n");
2068 if (! l_current_handler->handler(jp2,l_current_data,l_current_data_size,p_manager)) {
2069 opj_free(l_current_data);
2074 jp2->jp2_state |= JP2_STATE_UNKNOWN;
2075 if (opj_stream_skip(cio,l_current_data_size,p_manager) != l_current_data_size) {
2076 opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with skipping JPEG2000 box, stream error\n");
2077 opj_free(l_current_data);
2083 opj_free(l_current_data);
2089 * Excutes the given procedures on the given codec.
2091 * @param p_procedure_list the list of procedures to execute
2092 * @param jp2 the jpeg2000 file codec to execute the procedures on.
2093 * @param cio the stream to execute the procedures on.
2094 * @param p_manager the user manager.
2096 * @return true if all the procedures were successfully executed.
2100 opj_procedure_list_t * p_procedure_list,
2101 opj_stream_private_t *cio,
2102 opj_event_mgr_t * p_manager
2105 opj_bool (** l_procedure) (opj_jp2_v2_t * jp2, opj_stream_private_t *, opj_event_mgr_t *) = 00;
2106 opj_bool l_result = OPJ_TRUE;
2107 OPJ_UINT32 l_nb_proc, i;
2110 assert(p_procedure_list != 00);
2113 assert(p_manager != 00);
2115 l_nb_proc = opj_procedure_list_get_nb_procedures(p_procedure_list);
2116 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);
2118 for (i=0;i<l_nb_proc;++i) {
2119 l_result = l_result && (*l_procedure) (jp2,cio,p_manager);
2123 // and clear the procedure list at the end.
2124 opj_procedure_list_clear(p_procedure_list);
2130 * Finds the execution function related to the given box id.
2132 * @param p_id the id of the handler to fetch.
2134 * @return the given handler or 00 if it could not be found.
2136 const opj_jp2_header_handler_t * jp2_find_handler (int p_id)
2138 OPJ_UINT32 i, l_handler_size = sizeof(jp2_header) / sizeof(opj_jp2_header_handler_t);
2140 for (i=0;i<l_handler_size;++i) {
2141 if (jp2_header[i].id == p_id) {
2142 return &jp2_header[i];
2149 * Finds the image execution function related to the given box id.
2151 * @param p_id the id of the handler to fetch.
2153 * @return the given handler or 00 if it could not be found.
2155 static const opj_jp2_header_handler_t * jp2_img_find_handler (
2159 OPJ_UINT32 i, l_handler_size = sizeof(jp2_img_header) / sizeof(opj_jp2_header_handler_t);
2160 for (i=0;i<l_handler_size;++i)
2162 if (jp2_img_header[i].id == p_id) {
2163 return &jp2_img_header[i];
2172 * Reads a jpeg2000 file signature box.
2174 * @param p_header_data the data contained in the signature box.
2175 * @param jp2 the jpeg2000 file codec.
2176 * @param p_header_size the size of the data contained in the signature box.
2177 * @param p_manager the user event manager.
2179 * @return true if the file signature box is valid.
2181 opj_bool jp2_read_jp_v2(
2183 unsigned char * p_header_data,
2184 unsigned int p_header_size,
2185 opj_event_mgr_t * p_manager
2188 unsigned int l_magic_number;
2191 assert(p_header_data != 00);
2193 assert(p_manager != 00);
2195 if (jp2->jp2_state != JP2_STATE_NONE) {
2196 opj_event_msg_v2(p_manager, EVT_ERROR, "The signature box must be the first box in the file.\n");
2200 /* assure length of data is correct (4 -> magic number) */
2201 if (p_header_size != 4) {
2202 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with JP signature Box size\n");
2207 opj_read_bytes(p_header_data,&l_magic_number,4);
2208 if (l_magic_number != 0x0d0a870a ) {
2209 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with JP Signature : bad magic number\n");
2213 jp2->jp2_state |= JP2_STATE_SIGNATURE;
2220 * Reads a a FTYP box - File type box
2222 * @param p_header_data the data contained in the FTYP box.
2223 * @param jp2 the jpeg2000 file codec.
2224 * @param p_header_size the size of the data contained in the FTYP box.
2225 * @param p_manager the user event manager.
2227 * @return true if the FTYP box is valid.
2229 opj_bool jp2_read_ftyp_v2(
2231 unsigned char * p_header_data,
2232 unsigned int p_header_size,
2233 opj_event_mgr_t * p_manager
2236 OPJ_UINT32 i, l_remaining_bytes;
2239 assert(p_header_data != 00);
2241 assert(p_manager != 00);
2243 if (jp2->jp2_state != JP2_STATE_SIGNATURE) {
2244 opj_event_msg_v2(p_manager, EVT_ERROR, "The ftyp box must be the second box in the file.\n");
2248 /* assure length of data is correct */
2249 if (p_header_size < 8) {
2250 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with FTYP signature Box size\n");
2254 opj_read_bytes(p_header_data,&jp2->brand,4); /* BR */
2257 opj_read_bytes(p_header_data,&jp2->minversion,4); /* MinV */
2260 l_remaining_bytes = p_header_size - 8;
2262 /* the number of remaining bytes should be a multiple of 4 */
2263 if ((l_remaining_bytes & 0x3) != 0) {
2264 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with FTYP signature Box size\n");
2269 jp2->numcl = l_remaining_bytes >> 2;
2271 jp2->cl = (unsigned int *) opj_malloc(jp2->numcl * sizeof(unsigned int));
2272 if (jp2->cl == 00) {
2273 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory with FTYP Box\n");
2276 memset(jp2->cl,0,jp2->numcl * sizeof(unsigned int));
2279 for (i = 0; i < jp2->numcl; ++i)
2281 opj_read_bytes(p_header_data,&jp2->cl[i],4); /* CLi */
2285 jp2->jp2_state |= JP2_STATE_FILE_TYPE;
2292 * Reads the Jpeg2000 file Header box - JP2 Header box (warning, this is a super box).
2294 * @param p_header_data the data contained in the file header box.
2295 * @param jp2 the jpeg2000 file codec.
2296 * @param p_header_size the size of the data contained in the file header box.
2297 * @param p_manager the user event manager.
2299 * @return true if the JP2 Header box was successfully reconized.
2301 opj_bool jp2_read_jp2h_v2(
2303 unsigned char * p_header_data,
2304 unsigned int p_header_size,
2305 opj_event_mgr_t * p_manager
2308 OPJ_UINT32 l_box_size=0, l_current_data_size = 0;
2310 const opj_jp2_header_handler_t * l_current_handler;
2313 assert(p_header_data != 00);
2315 assert(p_manager != 00);
2317 /* make sure the box is well placed */
2318 if ((jp2->jp2_state & JP2_STATE_FILE_TYPE) != JP2_STATE_FILE_TYPE ) {
2319 opj_event_msg_v2(p_manager, EVT_ERROR, "The box must be the first box in the file.\n");
2323 jp2->jp2_img_state = JP2_IMG_STATE_NONE;
2325 /* iterate while remaining data */
2326 while (p_header_size > 0) {
2328 if (! jp2_read_boxhdr_char(&box,p_header_data,&l_box_size,p_header_size, p_manager)) {
2329 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream error while reading JP2 Header box\n");
2333 if (box.length > p_header_size) {
2334 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream error while reading JP2 Header box: box length is inconsistent.\n");
2338 l_current_handler = jp2_img_find_handler(box.type);
2339 l_current_data_size = box.length - l_box_size;
2340 p_header_data += l_box_size;
2342 if (l_current_handler != 00) {
2343 if (! l_current_handler->handler(jp2,p_header_data,l_current_data_size,p_manager)) {
2348 jp2->jp2_img_state |= JP2_IMG_STATE_UNKNOWN;
2351 p_header_data += l_current_data_size;
2352 p_header_size -= box.length;
2355 jp2->jp2_state |= JP2_STATE_HEADER;
2361 * Reads a box header. The box is the way data is packed inside a jpeg2000 file structure. Data is read from a character string
2363 * @param p_data the character string to read data from.
2364 * @param box the box structure to fill.
2365 * @param p_number_bytes_read pointer to an int that will store the number of bytes read from the stream (shoul usually be 2).
2366 * @param p_box_max_size the maximum number of bytes in the box.
2368 * @return true if the box is reconized, false otherwise
2370 static opj_bool jp2_read_boxhdr_char(
2373 OPJ_UINT32 * p_number_bytes_read,
2374 OPJ_UINT32 p_box_max_size,
2375 opj_event_mgr_t * p_manager
2381 assert(p_data != 00);
2383 assert(p_number_bytes_read != 00);
2384 assert(p_manager != 00);
2386 if (p_box_max_size < 8) {
2387 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot handle box of less than 8 bytes\n");
2391 /* process read data */
2392 opj_read_bytes(p_data, &l_value, 4);
2394 box->length = (OPJ_INT32)(l_value);
2396 opj_read_bytes(p_data, &l_value, 4);
2398 box->type = (OPJ_INT32)(l_value);
2400 *p_number_bytes_read = 8;
2402 // do we have a "special very large box ?"
2403 // read then the XLBox
2404 if (box->length == 1) {
2405 unsigned int l_xl_part_size;
2407 if (p_box_max_size < 16) {
2408 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot handle XL box of less than 16 bytes\n");
2412 opj_read_bytes(p_data,&l_xl_part_size, 4);
2414 *p_number_bytes_read += 4;
2416 if (l_xl_part_size != 0) {
2417 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot handle box sizes higher than 2^32\n");
2421 opj_read_bytes(p_data, &l_value, 4);
2422 *p_number_bytes_read += 4;
2423 box->length = (OPJ_INT32)(l_value);
2425 if (box->length == 0) {
2426 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot handle box of undefined sizes\n");
2430 else if (box->length == 0) {
2431 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot handle box of undefined sizes\n");
2440 * Reads a jpeg2000 file header structure.
2442 * @param cio the stream to read data from.
2443 * @param jp2 the jpeg2000 file header structure.
2444 * @param p_manager the user event manager.
2446 * @return true if the box is valid.
2448 opj_bool jp2_read_header( struct opj_stream_private *p_stream,
2450 opj_image_t** p_image,
2451 struct opj_event_mgr * p_manager
2456 assert(p_stream != 00);
2457 assert(p_manager != 00);
2459 /* customization of the validation */
2460 jp2_setup_decoding_validation (jp2);
2462 /* customization of the encoding */
2463 jp2_setup_header_reading(jp2);
2465 /* validation of the parameters codec */
2466 if (! jp2_exec(jp2,jp2->m_validation_list,p_stream,p_manager)) {
2471 if (! jp2_exec (jp2,jp2->m_procedure_list,p_stream,p_manager)) {
2475 return j2k_read_header( p_stream,
2482 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
2483 * are valid. Developpers wanting to extend the library can add their own validation procedures.
2485 void jp2_setup_decoding_validation (opj_jp2_v2_t *jp2)
2489 /* DEVELOPER CORNER, add your custom validation procedure */
2493 * Sets up the procedures to do on reading header.
2494 * Developpers wanting to extend the library can add their own writting procedures.
2496 void jp2_setup_header_reading (opj_jp2_v2_t *jp2)
2501 opj_procedure_list_add_procedure(jp2->m_procedure_list,(void*)jp2_read_header_procedure );
2502 /* DEVELOPER CORNER, add your custom procedures */
2507 * Reads a tile header.
2508 * @param p_j2k the jpeg2000 codec.
2509 * @param p_stream the stream to write data to.
2510 * @param p_manager the user event manager.
2512 opj_bool jp2_read_tile_header( opj_jp2_v2_t * p_jp2,
2513 OPJ_UINT32 * p_tile_index,
2514 OPJ_UINT32 * p_data_size,
2515 OPJ_INT32 * p_tile_x0, OPJ_INT32 * p_tile_y0,
2516 OPJ_INT32 * p_tile_x1, OPJ_INT32 * p_tile_y1,
2517 OPJ_UINT32 * p_nb_comps,
2519 opj_stream_private_t *p_stream,
2520 opj_event_mgr_t * p_manager )
2522 return j2k_read_tile_header(p_jp2->j2k,
2525 p_tile_x0, p_tile_y0,
2526 p_tile_x1, p_tile_y1,
2535 * @param p_j2k the jpeg2000 codec.
2536 * @param p_stream the stream to write data to.
2537 * @param p_manager the user event manager.
2539 opj_bool jp2_decode_tile (
2540 opj_jp2_v2_t * p_jp2,
2541 OPJ_UINT32 p_tile_index,
2543 OPJ_UINT32 p_data_size,
2544 opj_stream_private_t *p_stream,
2545 opj_event_mgr_t * p_manager
2548 return j2k_decode_tile (p_jp2->j2k,p_tile_index,p_data,p_data_size,p_stream,p_manager);
2552 * Destroys a jpeg2000 file decompressor.
2554 * @param jp2 a jpeg2000 file decompressor.
2556 void jp2_destroy(opj_jp2_v2_t *jp2)
2559 /* destroy the J2K codec */
2560 j2k_destroy(jp2->j2k);
2564 opj_free(jp2->comps);
2573 if (jp2->color.icc_profile_buf) {
2574 opj_free(jp2->color.icc_profile_buf);
2575 jp2->color.icc_profile_buf = 00;
2578 if (jp2->color.jp2_cdef) {
2579 if (jp2->color.jp2_cdef->info) {
2580 opj_free(jp2->color.jp2_cdef->info);
2581 jp2->color.jp2_cdef->info = NULL;
2584 opj_free(jp2->color.jp2_cdef);
2585 jp2->color.jp2_cdef = 00;
2588 if (jp2->color.jp2_pclr) {
2589 if (jp2->color.jp2_pclr->cmap) {
2590 opj_free(jp2->color.jp2_pclr->cmap);
2591 jp2->color.jp2_pclr->cmap = NULL;
2593 if (jp2->color.jp2_pclr->channel_sign) {
2594 opj_free(jp2->color.jp2_pclr->channel_sign);
2595 jp2->color.jp2_pclr->channel_sign = NULL;
2597 if (jp2->color.jp2_pclr->channel_size) {
2598 opj_free(jp2->color.jp2_pclr->channel_size);
2599 jp2->color.jp2_pclr->channel_size = NULL;
2601 if (jp2->color.jp2_pclr->entries) {
2602 opj_free(jp2->color.jp2_pclr->entries);
2603 jp2->color.jp2_pclr->entries = NULL;
2606 opj_free(jp2->color.jp2_pclr);
2607 jp2->color.jp2_pclr = 00;
2610 if (jp2->m_validation_list) {
2611 opj_procedure_list_destroy(jp2->m_validation_list);
2612 jp2->m_validation_list = 00;
2615 if (jp2->m_procedure_list) {
2616 opj_procedure_list_destroy(jp2->m_procedure_list);
2617 jp2->m_procedure_list = 00;
2625 * Sets the given area to be decoded. This function should be called right after opj_read_header and before any tile header reading.
2627 * @param p_jp2 the jpeg2000 codec.
2628 * @param p_end_x the right position of the rectangle to decode (in image coordinates).
2629 * @param p_start_y the up position of the rectangle to decode (in image coordinates).
2630 * @param p_end_y the bottom position of the rectangle to decode (in image coordinates).
2631 * @param p_manager the user event manager
2633 * @return true if the area could be set.
2635 opj_bool jp2_set_decode_area( opj_jp2_v2_t *p_jp2,
2636 opj_image_t* p_image,
2637 OPJ_INT32 p_start_x, OPJ_INT32 p_start_y,
2638 OPJ_INT32 p_end_x, OPJ_INT32 p_end_y,
2639 struct opj_event_mgr * p_manager )
2641 return j2k_set_decode_area(p_jp2->j2k, p_image, p_start_x, p_start_y, p_end_x, p_end_y, p_manager);
2645 * Get the decoded tile.
2647 * @param jp2 the jpeg2000 codec.
2648 * @param p_stream input_stream
2649 * @param p_image output image. .
2650 * @param p_manager the user event manager
2651 * @param tile_index index of the tile we want decode
2653 * @return true if succeed.
2655 opj_bool jp2_get_tile( opj_jp2_v2_t *jp2,
2656 opj_stream_private_t *p_stream,
2657 opj_image_t* p_image,
2658 struct opj_event_mgr * p_manager,
2659 OPJ_UINT32 tile_index )
2664 opj_event_msg_v2(p_manager, EVT_WARNING, "JP2 box which are after the codestream will not be read by this function.\n");
2666 if (! j2k_get_tile(jp2->j2k, p_stream, p_image, p_manager, tile_index) ){
2667 opj_event_msg_v2(p_manager, EVT_ERROR, "Failed to decode the codestream in the JP2 file\n");
2671 /* Set Image Color Space */
2672 if (jp2->enumcs == 16)
2673 p_image->color_space = CLRSPC_SRGB;
2674 else if (jp2->enumcs == 17)
2675 p_image->color_space = CLRSPC_GRAY;
2676 else if (jp2->enumcs == 18)
2677 p_image->color_space = CLRSPC_SYCC;
2679 p_image->color_space = CLRSPC_UNKNOWN;
2681 /* Apply the color space if needed */
2682 if(jp2->color.jp2_cdef) {
2683 jp2_apply_cdef(p_image, &(jp2->color));
2686 if(jp2->color.jp2_pclr) {
2687 /* Part 1, I.5.3.4: Either both or none : */
2688 if( !jp2->color.jp2_pclr->cmap)
2689 jp2_free_pclr(&(jp2->color));
2691 jp2_apply_pclr(p_image, &(jp2->color));
2694 if(jp2->color.icc_profile_buf) {
2695 p_image->icc_profile_buf = jp2->color.icc_profile_buf;
2696 p_image->icc_profile_len = jp2->color.icc_profile_len;
2697 jp2->color.icc_profile_buf = NULL;
2705 /* ----------------------------------------------------------------------- */
2706 /* JP2 encoder interface */
2707 /* ----------------------------------------------------------------------- */
2709 opj_jp2_v2_t* jp2_create(opj_bool p_is_decoder)
2711 opj_jp2_v2_t *jp2 = (opj_jp2_v2_t*)opj_malloc(sizeof(opj_jp2_v2_t));
2713 memset(jp2,0,sizeof(opj_jp2_t));
2715 /* create the J2K codec */
2716 if (! p_is_decoder) {
2717 jp2->j2k = j2k_create_compress_v2();
2720 jp2->j2k = j2k_create_decompress_v2();
2723 if (jp2->j2k == 00) {
2728 /* Color structure */
2729 jp2->color.icc_profile_buf = NULL;
2730 jp2->color.icc_profile_len = 0;
2731 jp2->color.jp2_cdef = NULL;
2732 jp2->color.jp2_pclr = NULL;
2733 jp2->color.jp2_has_colr = 0;
2735 // validation list creation
2736 jp2->m_validation_list = opj_procedure_list_create();
2737 if (! jp2->m_validation_list) {
2742 // execution list creation
2743 jp2->m_procedure_list = opj_procedure_list_create();
2744 if (! jp2->m_procedure_list) {
2753 void jp2_dump(opj_jp2_v2_t* p_jp2, OPJ_INT32 flag, FILE* out_stream)
2756 assert(p_jp2 != 00);
2758 j2k_dump(p_jp2->j2k,
2763 opj_codestream_index_t* jp2_get_cstr_index(opj_jp2_v2_t* p_jp2)
2765 return j2k_get_cstr_index(p_jp2->j2k);
2768 opj_codestream_info_v2_t* jp2_get_cstr_info(opj_jp2_v2_t* p_jp2)
2770 return j2k_get_cstr_info(p_jp2->j2k);
2773 opj_bool jp2_set_decoded_resolution_factor(opj_jp2_v2_t *p_jp2, OPJ_UINT32 res_factor, opj_event_mgr_t * p_manager)
2775 return j2k_set_decoded_resolution_factor(p_jp2->j2k, res_factor, p_manager);