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 * Skips the Jpeg2000 Codestream Header box - JP2C Header box.
138 * @param cio the stream to write data to.
139 * @param jp2 the jpeg2000 file codec.
140 * @param p_manager user event manager.
142 * @return true if writting was successful.
144 opj_bool jp2_skip_jp2c( opj_jp2_v2_t *jp2,
145 struct opj_stream_private *cio,
146 struct opj_event_mgr * p_manager );
149 * Reads the Jpeg2000 file Header box - JP2 Header box (warning, this is a super box).
151 * @param p_header_data the data contained in the file header box.
152 * @param jp2 the jpeg2000 file codec.
153 * @param p_header_size the size of the data contained in the file header box.
154 * @param p_manager the user event manager.
156 * @return true if the JP2 Header box was successfully reconized.
158 static opj_bool jp2_read_jp2h_v2(
160 unsigned char * p_header_data,
161 unsigned int p_header_size,
162 struct opj_event_mgr * p_manager
165 static int jp2_write_jp2c(opj_jp2_t *jp2, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info);
166 static opj_bool jp2_read_jp2c(opj_jp2_t *jp2, opj_cio_t *cio, unsigned int *j2k_codestream_length, unsigned int *j2k_codestream_offset);
167 static void jp2_write_jp(opj_cio_t *cio);
169 Read the JP box - JPEG 2000 signature
170 @param jp2 JP2 handle
171 @param cio Input buffer stream
172 @return Returns true if successful, returns false otherwise
174 static opj_bool jp2_read_jp(opj_jp2_t *jp2, opj_cio_t *cio);
177 * Reads a jpeg2000 file signature box.
179 * @param p_header_data the data contained in the signature box.
180 * @param jp2 the jpeg2000 file codec.
181 * @param p_header_size the size of the data contained in the signature box.
182 * @param p_manager the user event manager.
184 * @return true if the file signature box is valid.
186 static opj_bool jp2_read_jp_v2(
188 unsigned char * p_header_data,
189 unsigned int p_header_size,
190 struct opj_event_mgr * p_manager
194 Decode the structure of a JP2 file
195 @param jp2 JP2 handle
196 @param cio Input buffer stream
197 @param color Collector for profile, cdef and pclr data
198 @return Returns true if successful, returns false otherwise
200 static opj_bool jp2_read_struct(opj_jp2_t *jp2, opj_cio_t *cio,
201 opj_jp2_color_t *color);
203 Apply collected palette data
204 @param color Collector for profile, cdef and pclr data
207 static void jp2_apply_pclr(opj_image_t *image, opj_jp2_color_t *color);
210 @param jp2 JP2 handle
211 @param cio Input buffer stream
213 @param color Collector for profile, cdef and pclr data
214 @return Returns true if successful, returns false otherwise
216 static opj_bool jp2_read_pclr(opj_jp2_t *jp2, opj_cio_t *cio,
217 opj_jp2_box_t *box, opj_jp2_color_t *color);
219 static opj_bool jp2_read_pclr_v2( opj_jp2_v2_t *jp2,
220 unsigned char * p_pclr_header_data,
221 OPJ_UINT32 p_pclr_header_size,
222 opj_event_mgr_t * p_manager
226 Collect component mapping data
227 @param jp2 JP2 handle
228 @param cio Input buffer stream
230 @param color Collector for profile, cdef and pclr data
231 @return Returns true if successful, returns false otherwise
233 static opj_bool jp2_read_cmap(opj_jp2_t *jp2, opj_cio_t *cio,
234 opj_jp2_box_t *box, opj_jp2_color_t *color);
237 static opj_bool jp2_read_cmap_v2( opj_jp2_v2_t * jp2,
238 unsigned char * p_cmap_header_data,
239 OPJ_UINT32 p_cmap_header_size,
240 opj_event_mgr_t * p_manager
244 Collect colour specification data
245 @param jp2 JP2 handle
246 @param cio Input buffer stream
248 @param color Collector for profile, cdef and pclr data
249 @return Returns true if successful, returns false otherwise
251 static opj_bool jp2_read_colr(opj_jp2_t *jp2, opj_cio_t *cio,
252 opj_jp2_box_t *box, opj_jp2_color_t *color);
255 * Reads the Color Specification box.
257 * @param p_colr_header_data pointer to actual data (already read from file)
258 * @param jp2 the jpeg2000 file codec.
259 * @param p_colr_header_size the size of the color header
260 * @param p_manager the user event manager.
262 * @return true if the bpc header is valid, fale else.
264 static opj_bool jp2_read_colr_v2(
266 unsigned char * p_colr_header_data,
267 OPJ_UINT32 p_colr_header_size,
268 struct opj_event_mgr * p_manager
272 Write file Index (superbox)
273 @param[in] offset_jp2c offset of jp2c box
274 @param[in] length_jp2c length of jp2c box
275 @param[in] offset_idx offset of cidx box
276 @param[in] length_idx length of cidx box
277 @param[in] cio file output handle
278 @return length of fidx box
280 static int write_fidx( int offset_jp2c, int length_jp2c, int offset_idx, int length_idx, opj_cio_t *cio);
282 Write index Finder box
283 @param[in] offset offset of fidx box
284 @param[in] length length of fidx box
285 @param[in] cio file output handle
287 static void write_iptr( int offset, int length, opj_cio_t *cio);
291 @param[in] offset_jp2c offset of jp2c box
292 @param[in] length_jp2c length of jp2c box
293 @param[in] offset_idx offset of cidx box
294 @param[in] length_idx length of cidx box
295 @param[in] cio file output handle
297 static void write_prxy( int offset_jp2c, int length_jp2c, int offset_idx, int length_idx, opj_cio_t *cio);
303 * Sets up the procedures to do on writting header after the codestream.
304 * Developpers wanting to extend the library can add their own writting procedures.
306 static void jp2_setup_end_header_writting (opj_jp2_v2_t *jp2);
310 * Sets up the procedures to do on reading header after the codestream.
311 * Developpers wanting to extend the library can add their own writting procedures.
313 static void jp2_setup_end_header_reading (opj_jp2_v2_t *jp2);
316 * Reads a jpeg2000 file header structure.
318 * @param cio the stream to read data from.
319 * @param jp2 the jpeg2000 file header structure.
320 * @param p_manager the user event manager.
322 * @return true if the box is valid.
324 opj_bool jp2_read_header_procedure(
326 struct opj_stream_private *cio,
327 struct opj_event_mgr * p_manager
331 * Excutes the given procedures on the given codec.
333 * @param p_procedure_list the list of procedures to execute
334 * @param jp2 the jpeg2000 file codec to execute the procedures on.
335 * @param cio the stream to execute the procedures on.
336 * @param p_manager the user manager.
338 * @return true if all the procedures were successfully executed.
340 static opj_bool jp2_exec (
342 struct opj_procedure_list * p_procedure_list,
343 struct opj_stream_private *cio,
344 struct opj_event_mgr * p_manager
348 * Reads a box header. The box is the way data is packed inside a jpeg2000 file structure.
350 * @param cio the input stream to read data from.
351 * @param box the box structure to fill.
352 * @param p_number_bytes_read pointer to an int that will store the number of bytes read from the stream (shoul usually be 2).
353 * @param p_manager user event manager.
355 * @return true if the box is reconized, false otherwise
357 static opj_bool jp2_read_boxhdr_v2(
359 OPJ_UINT32 * p_number_bytes_read,
360 struct opj_stream_private *cio,
361 struct opj_event_mgr * p_manager
365 * Finds the execution function related to the given box id.
367 * @param p_id the id of the handler to fetch.
369 * @return the given handler or NULL if it could not be found.
371 static const opj_jp2_header_handler_t * jp2_find_handler (int p_id );
374 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
375 * are valid. Developpers wanting to extend the library can add their own validation procedures.
377 static void jp2_setup_encoding_validation (opj_jp2_v2_t *jp2);
381 * Sets up the procedures to do on writting header. Developpers wanting to extend the library can add their own writting procedures.
383 static void jp2_setup_header_writting (opj_jp2_v2_t *jp2);
386 * The default validation procedure without any extension.
388 * @param jp2 the jpeg2000 codec to validate.
389 * @param cio the input stream to validate.
390 * @param p_manager the user event manager.
392 * @return true if the parameters are correct.
394 opj_bool jp2_default_validation ( opj_jp2_v2_t * jp2,
395 struct opj_stream_private *cio,
396 struct opj_event_mgr * p_manager );
399 * Finds the image execution function related to the given box id.
401 * @param p_id the id of the handler to fetch.
403 * @return the given handler or NULL if it could not be found.
405 static const opj_jp2_header_handler_t * jp2_img_find_handler (int p_id);
407 const opj_jp2_header_handler_t jp2_header [] =
409 {JP2_JP,jp2_read_jp_v2},
410 {JP2_FTYP,jp2_read_ftyp_v2},
411 {JP2_JP2H,jp2_read_jp2h_v2}
414 const opj_jp2_header_handler_t jp2_img_header [] =
416 {JP2_IHDR,jp2_read_ihdr_v2},
417 {JP2_COLR,jp2_read_colr_v2},
418 {JP2_BPCC,jp2_read_bpcc_v2},
419 {JP2_PCLR,jp2_read_pclr_v2},
420 {JP2_CMAP,jp2_read_cmap_v2},
421 {JP2_CDEF,jp2_read_cdef_v2}
426 * Reads a box header. The box is the way data is packed inside a jpeg2000 file structure. Data is read from a character string
428 * @param p_data the character string to read data from.
429 * @param box the box structure to fill.
430 * @param p_number_bytes_read pointer to an int that will store the number of bytes read from the stream (shoul usually be 2).
431 * @param p_box_max_size the maximum number of bytes in the box.
433 * @return true if the box is reconized, false otherwise
435 static opj_bool jp2_read_boxhdr_char(
438 OPJ_UINT32 * p_number_bytes_read,
439 OPJ_UINT32 p_box_max_size,
440 struct opj_event_mgr * p_manager
444 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
445 * are valid. Developpers wanting to extend the library can add their own validation procedures.
447 static void jp2_setup_decoding_validation (opj_jp2_v2_t *jp2);
450 * Sets up the procedures to do on reading header.
451 * Developpers wanting to extend the library can add their own writting procedures.
453 static void jp2_setup_header_reading (opj_jp2_v2_t *jp2);
457 /* ----------------------------------------------------------------------- */
459 static opj_bool jp2_read_boxhdr(opj_common_ptr cinfo, opj_cio_t *cio, opj_jp2_box_t *box) {
460 box->init_pos = cio_tell(cio);
461 box->length = cio_read(cio, 4);
462 box->type = cio_read(cio, 4);
463 if (box->length == 1) {
464 if (cio_read(cio, 4) != 0) {
465 opj_event_msg(cinfo, EVT_ERROR, "Cannot handle box sizes higher than 2^32\n");
468 box->length = cio_read(cio, 4);
469 if (box->length == 0)
470 box->length = cio_numbytesleft(cio) + 12;
472 else if (box->length == 0) {
473 box->length = cio_numbytesleft(cio) + 8;
480 * Reads a box header. The box is the way data is packed inside a jpeg2000 file structure.
482 * @param cio the input stream to read data from.
483 * @param box the box structure to fill.
484 * @param p_number_bytes_read pointer to an int that will store the number of bytes read from the stream (should usually be 8).
485 * @param p_manager user event manager.
487 * @return true if the box is reconized, false otherwise
489 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)
491 /* read header from file */
492 unsigned char l_data_header [8];
497 assert(p_number_bytes_read != 00);
498 assert(p_manager != 00);
500 *p_number_bytes_read = opj_stream_read_data(cio,l_data_header,8,p_manager);
501 if (*p_number_bytes_read != 8) {
505 /* process read data */
506 opj_read_bytes(l_data_header,&(box->length), 4);
507 opj_read_bytes(l_data_header+4,&(box->type), 4);
509 /* do we have a "special very large box ?" */
510 /* read then the XLBox */
511 if (box->length == 1) {
512 OPJ_UINT32 l_xl_part_size;
514 OPJ_UINT32 l_nb_bytes_read = opj_stream_read_data(cio,l_data_header,8,p_manager);
515 if (l_nb_bytes_read != 8) {
516 if (l_nb_bytes_read > 0) {
517 *p_number_bytes_read += l_nb_bytes_read;
523 opj_read_bytes(l_data_header,&l_xl_part_size, 4);
524 if (l_xl_part_size != 0) {
525 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot handle box sizes higher than 2^32\n");
528 opj_read_bytes(l_data_header,&(box->length), 4);
534 static void jp2_write_url(opj_cio_t *cio, char *Idx_file) {
538 box.init_pos = cio_tell(cio);
540 cio_write(cio, JP2_URL, 4); /* DBTL */
541 cio_write(cio, 0, 1); /* VERS */
542 cio_write(cio, 0, 3); /* FLAG */
545 for (i = 0; i < strlen(Idx_file); i++) {
546 cio_write(cio, Idx_file[i], 1);
550 box.length = cio_tell(cio) - box.init_pos;
551 cio_seek(cio, box.init_pos);
552 cio_write(cio, box.length, 4); /* L */
553 cio_seek(cio, box.init_pos + box.length);
557 static opj_bool jp2_read_ihdr(opj_jp2_t *jp2, opj_cio_t *cio) {
560 opj_common_ptr cinfo = jp2->cinfo;
562 jp2_read_boxhdr(cinfo, cio, &box);
563 if (JP2_IHDR != box.type) {
564 opj_event_msg(cinfo, EVT_ERROR, "Expected IHDR Marker\n");
568 jp2->h = cio_read(cio, 4); /* HEIGHT */
569 jp2->w = cio_read(cio, 4); /* WIDTH */
570 jp2->numcomps = cio_read(cio, 2); /* NC */
571 jp2->comps = (opj_jp2_comps_t*) opj_malloc(jp2->numcomps * sizeof(opj_jp2_comps_t));
573 jp2->bpc = cio_read(cio, 1); /* BPC */
575 jp2->C = cio_read(cio, 1); /* C */
576 jp2->UnkC = cio_read(cio, 1); /* UnkC */
577 jp2->IPR = cio_read(cio, 1); /* IPR */
579 if (cio_tell(cio) - box.init_pos != box.length) {
580 opj_event_msg(cinfo, EVT_ERROR, "Error with IHDR Box\n");
588 * Reads a IHDR box - Image Header box
590 * @param jp2 the jpeg2000 file codec.
591 * @param p_image_header_data pointer to actual data (already read from file)
592 * @param p_image_header_size the size of the image header
593 * @param p_manager the user event manager.
595 * @return true if the image header is valid, fale else.
597 opj_bool jp2_read_ihdr_v2(
599 unsigned char * p_image_header_data,
600 unsigned int p_image_header_size,
601 opj_event_mgr_t * p_manager
605 assert(p_image_header_data != 00);
607 assert(p_manager != 00);
609 if (p_image_header_size != 14) {
610 opj_event_msg_v2(p_manager, EVT_ERROR, "Bad image header box (bad size)\n");
614 opj_read_bytes(p_image_header_data,&(jp2->h),4); /* HEIGHT */
615 p_image_header_data += 4;
616 opj_read_bytes(p_image_header_data,&(jp2->w),4); /* WIDTH */
617 p_image_header_data += 4;
618 opj_read_bytes(p_image_header_data,&(jp2->numcomps),2); /* NC */
619 p_image_header_data += 2;
621 /* allocate memory for components */
622 jp2->comps = (opj_jp2_comps_t*) opj_malloc(jp2->numcomps * sizeof(opj_jp2_comps_t));
623 if (jp2->comps == 0) {
624 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to handle image header (ihdr)\n");
627 memset(jp2->comps,0,jp2->numcomps * sizeof(opj_jp2_comps_t));
629 opj_read_bytes(p_image_header_data,&(jp2->bpc),1); /* BPC */
630 ++ p_image_header_data;
632 /* if equal to 0 then need a BPC box (cf. chapter about image header box of the norm) */
633 /*if (jp2->bpc == 0){
634 indicate with a flag that we will wait a BPC box
637 opj_read_bytes(p_image_header_data,&(jp2->C),1); /* C */
638 ++ p_image_header_data;
640 /* Should be equal to 7 cf. chapter about image header box of the norm */
642 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);
645 opj_read_bytes(p_image_header_data,&(jp2->UnkC),1); /* UnkC */
646 ++ p_image_header_data;
647 opj_read_bytes(p_image_header_data,&(jp2->IPR),1); /* IPR */
648 ++ p_image_header_data;
653 static void jp2_write_ihdr(opj_jp2_t *jp2, opj_cio_t *cio) {
656 box.init_pos = cio_tell(cio);
658 cio_write(cio, JP2_IHDR, 4); /* IHDR */
660 cio_write(cio, jp2->h, 4); /* HEIGHT */
661 cio_write(cio, jp2->w, 4); /* WIDTH */
662 cio_write(cio, jp2->numcomps, 2); /* NC */
664 cio_write(cio, jp2->bpc, 1); /* BPC */
666 cio_write(cio, jp2->C, 1); /* C : Always 7 */
667 cio_write(cio, jp2->UnkC, 1); /* UnkC, colorspace unknown */
668 cio_write(cio, jp2->IPR, 1); /* IPR, no intellectual property */
670 box.length = cio_tell(cio) - box.init_pos;
671 cio_seek(cio, box.init_pos);
672 cio_write(cio, box.length, 4); /* L */
673 cio_seek(cio, box.init_pos + box.length);
676 static void jp2_write_bpcc(opj_jp2_t *jp2, opj_cio_t *cio) {
680 box.init_pos = cio_tell(cio);
682 cio_write(cio, JP2_BPCC, 4); /* BPCC */
684 for (i = 0; i < jp2->numcomps; i++) {
685 cio_write(cio, jp2->comps[i].bpcc, 1);
688 box.length = cio_tell(cio) - box.init_pos;
689 cio_seek(cio, box.init_pos);
690 cio_write(cio, box.length, 4); /* L */
691 cio_seek(cio, box.init_pos + box.length);
695 static opj_bool jp2_read_bpcc(opj_jp2_t *jp2, opj_cio_t *cio) {
699 opj_common_ptr cinfo = jp2->cinfo;
701 jp2_read_boxhdr(cinfo, cio, &box);
702 if (JP2_BPCC != box.type) {
703 opj_event_msg(cinfo, EVT_ERROR, "Expected BPCC Marker\n");
707 for (i = 0; i < jp2->numcomps; i++) {
708 jp2->comps[i].bpcc = cio_read(cio, 1);
711 if (cio_tell(cio) - box.init_pos != box.length) {
712 opj_event_msg(cinfo, EVT_ERROR, "Error with BPCC Box\n");
720 * Reads a Bit per Component box.
722 * @param jp2 the jpeg2000 file codec.
723 * @param p_bpc_header_data pointer to actual data (already read from file)
724 * @param p_bpc_header_size pointer that will hold the size of the bpc header
725 * @param p_manager the user event manager.
727 * @return true if the bpc header is valid, false otherwise.
729 opj_bool jp2_read_bpcc_v2( opj_jp2_v2_t *jp2,
730 unsigned char * p_bpc_header_data,
731 unsigned int p_bpc_header_size,
732 opj_event_mgr_t * p_manager
738 assert(p_bpc_header_data != 00);
740 assert(p_manager != 00);
743 /*if (jp2->bpc != 0 ){
744 opj_event_msg_v2(p_manager, EVT_WARNING, "A BPCC header box is available although BPC is different to zero (%d)\n",jp2->bpc);
747 /* and length is relevant */
748 if (p_bpc_header_size != jp2->numcomps) {
749 opj_event_msg_v2(p_manager, EVT_ERROR, "Bad BPCC header box (bad size)\n");
753 /* read info for each component */
754 for (i = 0; i < jp2->numcomps; ++i) {
755 opj_read_bytes(p_bpc_header_data,&jp2->comps[i].bpcc ,1); /* read each BPCC component */
762 static void jp2_write_colr(opj_jp2_t *jp2, opj_cio_t *cio) {
765 box.init_pos = cio_tell(cio);
767 cio_write(cio, JP2_COLR, 4); /* COLR */
769 cio_write(cio, jp2->meth, 1); /* METH */
770 cio_write(cio, jp2->precedence, 1); /* PRECEDENCE */
771 cio_write(cio, jp2->approx, 1); /* APPROX */
776 cio_write(cio, jp2->enumcs, 4); /* EnumCS */
778 box.length = cio_tell(cio) - box.init_pos;
779 cio_seek(cio, box.init_pos);
780 cio_write(cio, box.length, 4); /* L */
781 cio_seek(cio, box.init_pos + box.length);
784 static void jp2_free_pclr(opj_jp2_color_t *color)
786 opj_free(color->jp2_pclr->channel_sign);
787 opj_free(color->jp2_pclr->channel_size);
788 opj_free(color->jp2_pclr->entries);
790 if(color->jp2_pclr->cmap) opj_free(color->jp2_pclr->cmap);
792 opj_free(color->jp2_pclr); color->jp2_pclr = NULL;
795 static void free_color_data(opj_jp2_color_t *color)
799 jp2_free_pclr(color);
803 if(color->jp2_cdef->info) opj_free(color->jp2_cdef->info);
804 opj_free(color->jp2_cdef);
806 if(color->icc_profile_buf) opj_free(color->icc_profile_buf);
810 static void jp2_apply_pclr(opj_image_t *image, opj_jp2_color_t *color)
812 opj_image_comp_t *old_comps, *new_comps;
813 OPJ_BYTE *channel_size, *channel_sign;
815 opj_jp2_cmap_comp_t *cmap;
816 OPJ_INT32 *src, *dst;
818 OPJ_UINT16 i, nr_channels, cmp, pcol;
821 channel_size = color->jp2_pclr->channel_size;
822 channel_sign = color->jp2_pclr->channel_sign;
823 entries = color->jp2_pclr->entries;
824 cmap = color->jp2_pclr->cmap;
825 nr_channels = color->jp2_pclr->nr_channels;
827 old_comps = image->comps;
828 new_comps = (opj_image_comp_t*)
829 opj_malloc(nr_channels * sizeof(opj_image_comp_t));
831 for(i = 0; i < nr_channels; ++i) {
832 pcol = cmap[i].pcol; cmp = cmap[i].cmp;
834 new_comps[pcol] = old_comps[cmp];
837 if(cmap[i].mtyp == 0){
838 old_comps[cmp].data = NULL; continue;
841 /* Palette mapping: */
842 new_comps[pcol].data = (int*)
843 opj_malloc(old_comps[cmp].w * old_comps[cmp].h * sizeof(int));
844 new_comps[pcol].prec = channel_size[i];
845 new_comps[pcol].sgnd = channel_sign[i];
848 top_k = color->jp2_pclr->nr_entries - 1;
850 for(i = 0; i < nr_channels; ++i) {
852 if(cmap[i].mtyp == 0) continue;
854 /* Palette mapping: */
855 cmp = cmap[i].cmp; pcol = cmap[i].pcol;
856 src = old_comps[cmp].data;
857 dst = new_comps[pcol].data;
858 max = new_comps[pcol].w * new_comps[pcol].h;
860 for(j = 0; j < max; ++j)
863 if((k = src[j]) < 0) k = 0; else if(k > top_k) k = top_k;
866 dst[j] = entries[k * nr_channels + pcol];
870 max = image->numcomps;
871 for(i = 0; i < max; ++i) {
872 if(old_comps[i].data) opj_free(old_comps[i].data);
876 image->comps = new_comps;
877 image->numcomps = nr_channels;
879 jp2_free_pclr(color);
884 static opj_bool jp2_read_pclr(opj_jp2_t *jp2, opj_cio_t *cio,
885 opj_jp2_box_t *box, opj_jp2_color_t *color)
887 opj_jp2_pclr_t *jp2_pclr;
888 unsigned char *channel_size, *channel_sign;
889 unsigned int *entries;
890 unsigned short nr_entries, nr_channels;
894 OPJ_ARG_NOT_USED(box);
895 OPJ_ARG_NOT_USED(jp2);
897 /* Part 1, I.5.3.4: 'There shall be at most one Palette box inside
898 * a JP2 Header box' :
900 if(color->jp2_pclr) return OPJ_FALSE;
902 nr_entries = (unsigned short)cio_read(cio, 2); /* NE */
903 nr_channels = (unsigned short)cio_read(cio, 1);/* NPC */
905 entries = (unsigned int*)
906 opj_malloc(nr_channels * nr_entries * sizeof(unsigned int));
907 channel_size = (unsigned char*)opj_malloc(nr_channels);
908 channel_sign = (unsigned char*)opj_malloc(nr_channels);
910 jp2_pclr = (opj_jp2_pclr_t*)opj_malloc(sizeof(opj_jp2_pclr_t));
911 jp2_pclr->channel_sign = channel_sign;
912 jp2_pclr->channel_size = channel_size;
913 jp2_pclr->entries = entries;
914 jp2_pclr->nr_entries = nr_entries;
915 jp2_pclr->nr_channels = nr_channels;
916 jp2_pclr->cmap = NULL;
918 color->jp2_pclr = jp2_pclr;
920 for(i = 0; i < nr_channels; ++i)
922 uc = cio_read(cio, 1); /* Bi */
923 channel_size[i] = (uc & 0x7f) + 1;
924 channel_sign[i] = (uc & 0x80)?1:0;
927 for(j = 0; j < nr_entries; ++j)
929 for(i = 0; i < nr_channels; ++i)
932 *entries++ = cio_read(cio, channel_size[i]>>3);
937 }/* jp2_read_pclr() */
940 * Reads a palette box.
942 * @param jp2 the jpeg2000 file codec.
943 * @param p_pclr_header_data pointer to actual data (already read from file)
944 * @param p_pclr_header_size pointer that will hold the size of the PCLR header
945 * @param p_manager the user event manager.
947 * @return true if the bpc header is valid, fale else.
949 opj_bool jp2_read_pclr_v2( opj_jp2_v2_t *jp2,
950 unsigned char * p_pclr_header_data,
951 OPJ_UINT32 p_pclr_header_size,
952 opj_event_mgr_t * p_manager
954 opj_jp2_pclr_t *jp2_pclr;
955 OPJ_BYTE *channel_size, *channel_sign;
957 OPJ_UINT16 nr_entries,nr_channels;
962 assert(p_pclr_header_data != 00);
964 assert(p_manager != 00);
966 if(jp2->color.jp2_pclr)
969 opj_read_bytes(p_pclr_header_data, &l_value , 2); /* NE */
970 p_pclr_header_data += 2;
971 nr_entries = (OPJ_UINT16) l_value;
973 opj_read_bytes(p_pclr_header_data, &l_value , 1); /* NPC */
974 ++p_pclr_header_data;
975 nr_channels = (OPJ_UINT16) l_value;
977 entries = (OPJ_UINT32*) opj_malloc(nr_channels * nr_entries * sizeof(OPJ_UINT32));
978 channel_size = (OPJ_BYTE*) opj_malloc(nr_channels);
979 channel_sign = (OPJ_BYTE*) opj_malloc(nr_channels);
981 jp2_pclr = (opj_jp2_pclr_t*)opj_malloc(sizeof(opj_jp2_pclr_t));
982 jp2_pclr->channel_sign = channel_sign;
983 jp2_pclr->channel_size = channel_size;
984 jp2_pclr->entries = entries;
985 jp2_pclr->nr_entries = nr_entries;
986 jp2_pclr->nr_channels = nr_channels;
987 jp2_pclr->cmap = NULL;
989 jp2->color.jp2_pclr = jp2_pclr;
991 for(i = 0; i < nr_channels; ++i) {
992 opj_read_bytes(p_pclr_header_data, &l_value , 1); /* Bi */
993 ++p_pclr_header_data;
995 channel_size[i] = (l_value & 0x7f) + 1;
996 channel_sign[i] = (l_value & 0x80)? 1 : 0;
999 for(j = 0; j < nr_entries; ++j) {
1000 for(i = 0; i < nr_channels; ++i) {
1001 /**entries++ = cio_read(cio, channel_size[i]>>3); */
1002 opj_read_bytes(p_pclr_header_data, &l_value , channel_size[i]>>3); /* Cji */
1003 p_pclr_header_data += channel_size[i]>>3;
1004 *entries = (OPJ_UINT32) l_value;
1013 static opj_bool jp2_read_cmap(opj_jp2_t *jp2, opj_cio_t *cio,
1014 opj_jp2_box_t *box, opj_jp2_color_t *color)
1016 opj_jp2_cmap_comp_t *cmap;
1017 unsigned short i, nr_channels;
1019 OPJ_ARG_NOT_USED(box);
1020 OPJ_ARG_NOT_USED(jp2);
1022 /* Need nr_channels: */
1023 if(color->jp2_pclr == NULL) return OPJ_FALSE;
1025 /* Part 1, I.5.3.5: 'There shall be at most one Component Mapping box
1026 * inside a JP2 Header box' :
1028 if(color->jp2_pclr->cmap) return OPJ_FALSE;
1030 nr_channels = color->jp2_pclr->nr_channels;
1031 cmap = (opj_jp2_cmap_comp_t*)
1032 opj_malloc(nr_channels * sizeof(opj_jp2_cmap_comp_t));
1034 for(i = 0; i < nr_channels; ++i)
1036 cmap[i].cmp = (unsigned short)cio_read(cio, 2);
1037 cmap[i].mtyp = cio_read(cio, 1);
1038 cmap[i].pcol = cio_read(cio, 1);
1041 color->jp2_pclr->cmap = cmap;
1045 }/* jp2_read_cmap() */
1048 * Reads the Component Mapping box.
1050 * @param p_cmap_header_data pointer to actual data (already read from file)
1051 * @param jp2 the jpeg2000 file codec.
1052 * @param p_cmap_header_size pointer that will hold the size of the color header
1053 * @param p_manager the user event manager.
1055 * @return true if the cdef header is valid, false else.
1057 static opj_bool jp2_read_cmap_v2( opj_jp2_v2_t * jp2,
1058 unsigned char * p_cmap_header_data,
1059 OPJ_UINT32 p_cmap_header_size,
1060 opj_event_mgr_t * p_manager
1063 opj_jp2_cmap_comp_t *cmap;
1064 OPJ_BYTE i, nr_channels;
1069 assert(p_cmap_header_data != 00);
1070 assert(p_manager != 00);
1072 /* Need nr_channels: */
1073 if(jp2->color.jp2_pclr == NULL) {
1074 opj_event_msg_v2(p_manager, EVT_ERROR, "Need to read a PCLR box before the CMAP box.\n");
1078 /* Part 1, I.5.3.5: 'There shall be at most one Component Mapping box
1079 * inside a JP2 Header box' :
1081 if(jp2->color.jp2_pclr->cmap) {
1082 opj_event_msg_v2(p_manager, EVT_ERROR, "Only one CMAP box is allowed.\n");
1086 nr_channels = jp2->color.jp2_pclr->nr_channels;
1087 cmap = (opj_jp2_cmap_comp_t*) opj_malloc(nr_channels * sizeof(opj_jp2_cmap_comp_t));
1089 for(i = 0; i < nr_channels; ++i) {
1090 opj_read_bytes(p_cmap_header_data, &l_value, 2); /* CMP^i */
1091 p_cmap_header_data +=2;
1092 cmap[i].cmp = (OPJ_UINT16) l_value;
1094 opj_read_bytes(p_cmap_header_data, &l_value, 1); /* MTYP^i */
1095 ++p_cmap_header_data;
1096 cmap[i].mtyp = (OPJ_BYTE) l_value;
1098 opj_read_bytes(p_cmap_header_data, &l_value, 1); /* PCOL^i */
1099 ++p_cmap_header_data;
1100 cmap[i].pcol = (OPJ_BYTE) l_value;
1103 jp2->color.jp2_pclr->cmap = cmap;
1108 static void jp2_apply_cdef(opj_image_t *image, opj_jp2_color_t *color)
1110 opj_jp2_cdef_info_t *info;
1111 OPJ_INT32 color_space;
1112 OPJ_UINT16 i, n, cn, typ, asoc, acn;
1114 color_space = image->color_space;
1115 info = color->jp2_cdef->info;
1116 n = color->jp2_cdef->n;
1118 for(i = 0; i < n; ++i)
1120 /* WATCH: acn = asoc - 1 ! */
1121 if((asoc = info[i].asoc) == 0) continue;
1123 cn = info[i].cn; typ = info[i].typ; acn = asoc - 1;
1127 opj_image_comp_t saved;
1129 memcpy(&saved, &image->comps[cn], sizeof(opj_image_comp_t));
1130 memcpy(&image->comps[cn], &image->comps[acn], sizeof(opj_image_comp_t));
1131 memcpy(&image->comps[acn], &saved, sizeof(opj_image_comp_t));
1133 info[i].asoc = cn + 1;
1134 info[acn].asoc = info[acn].cn + 1;
1138 if(color->jp2_cdef->info) opj_free(color->jp2_cdef->info);
1140 opj_free(color->jp2_cdef); color->jp2_cdef = NULL;
1142 }/* jp2_apply_cdef() */
1144 static opj_bool jp2_read_cdef(opj_jp2_t *jp2, opj_cio_t *cio,
1145 opj_jp2_box_t *box, opj_jp2_color_t *color)
1147 opj_jp2_cdef_info_t *info;
1148 unsigned short i, n;
1150 OPJ_ARG_NOT_USED(box);
1151 OPJ_ARG_NOT_USED(jp2);
1153 /* Part 1, I.5.3.6: 'The shall be at most one Channel Definition box
1154 * inside a JP2 Header box.'
1156 if(color->jp2_cdef) return OPJ_FALSE;
1158 if((n = (unsigned short)cio_read(cio, 2)) == 0) return OPJ_FALSE; /* szukw000: FIXME */
1160 info = (opj_jp2_cdef_info_t*)
1161 opj_malloc(n * sizeof(opj_jp2_cdef_info_t));
1163 color->jp2_cdef = (opj_jp2_cdef_t*)opj_malloc(sizeof(opj_jp2_cdef_t));
1164 color->jp2_cdef->info = info;
1165 color->jp2_cdef->n = n;
1167 for(i = 0; i < n; ++i)
1169 info[i].cn = (unsigned short)cio_read(cio, 2);
1170 info[i].typ = (unsigned short)cio_read(cio, 2);
1171 info[i].asoc = (unsigned short)cio_read(cio, 2);
1175 }/* jp2_read_cdef() */
1178 * Reads the Component Definition box.
1180 * @param p_cdef_header_data pointer to actual data (already read from file)
1181 * @param jp2 the jpeg2000 file codec.
1182 * @param p_cdef_header_size pointer that will hold the size of the color header
1183 * @param p_manager the user event manager.
1185 * @return true if the cdef header is valid, false else.
1187 static opj_bool jp2_read_cdef_v2( opj_jp2_v2_t * jp2,
1188 unsigned char * p_cdef_header_data,
1189 OPJ_UINT32 p_cdef_header_size,
1190 opj_event_mgr_t * p_manager
1193 opj_jp2_cdef_info_t *cdef_info;
1199 assert(p_cdef_header_data != 00);
1200 assert(p_manager != 00);
1202 /* Part 1, I.5.3.6: 'The shall be at most one Channel Definition box
1203 * inside a JP2 Header box.'*/
1204 if(jp2->color.jp2_cdef) return OPJ_FALSE;
1206 opj_read_bytes(p_cdef_header_data,&l_value ,2); /* N */
1207 p_cdef_header_data+= 2;
1209 if ( (OPJ_UINT16)l_value == 0){ /* szukw000: FIXME */
1210 opj_event_msg_v2(p_manager, EVT_ERROR, "Number of channel description is equal to zero in CDEF box.\n");
1214 cdef_info = (opj_jp2_cdef_info_t*) opj_malloc(l_value * sizeof(opj_jp2_cdef_info_t));
1216 jp2->color.jp2_cdef = (opj_jp2_cdef_t*)opj_malloc(sizeof(opj_jp2_cdef_t));
1217 jp2->color.jp2_cdef->info = cdef_info;
1218 jp2->color.jp2_cdef->n = (OPJ_UINT16) l_value;
1220 for(i = 0; i < jp2->color.jp2_cdef->n; ++i) {
1221 opj_read_bytes(p_cdef_header_data, &l_value, 2); /* Cn^i */
1222 p_cdef_header_data +=2;
1223 cdef_info[i].cn = (OPJ_UINT16) l_value;
1225 opj_read_bytes(p_cdef_header_data, &l_value, 2); /* Typ^i */
1226 p_cdef_header_data +=2;
1227 cdef_info[i].typ = (OPJ_UINT16) l_value;
1229 opj_read_bytes(p_cdef_header_data, &l_value, 2); /* Asoc^i */
1230 p_cdef_header_data +=2;
1231 cdef_info[i].asoc = (OPJ_UINT16) l_value;
1238 static opj_bool jp2_read_colr(opj_jp2_t *jp2, opj_cio_t *cio,
1239 opj_jp2_box_t *box, opj_jp2_color_t *color)
1242 opj_common_ptr cinfo;
1244 /* Part 1, I.5.3.3 : 'A conforming JP2 reader shall ignore all Colour
1245 * Specification boxes after the first.'
1247 if(color->jp2_has_colr) return OPJ_FALSE;
1251 jp2->meth = cio_read(cio, 1); /* METH */
1252 jp2->precedence = cio_read(cio, 1); /* PRECEDENCE */
1253 jp2->approx = cio_read(cio, 1); /* APPROX */
1257 jp2->enumcs = cio_read(cio, 4); /* EnumCS */
1263 skip_len = box->init_pos + box->length - cio_tell(cio);
1266 opj_event_msg(cinfo, EVT_ERROR, "Error with COLR box size\n");
1271 unsigned char *start;
1273 start = cio_getbp(cio);
1274 color->icc_profile_buf = (unsigned char*)opj_malloc(skip_len);
1275 color->icc_profile_len = skip_len;
1277 cio_skip(cio, box->init_pos + box->length - cio_tell(cio));
1279 memcpy(color->icc_profile_buf, start, skip_len);
1283 if (cio_tell(cio) - box->init_pos != box->length)
1285 opj_event_msg(cinfo, EVT_ERROR, "Error with COLR Box\n");
1288 color->jp2_has_colr = 1;
1291 }/* jp2_read_colr() */
1294 * Reads the Colour Specification box.
1296 * @param p_colr_header_data pointer to actual data (already read from file)
1297 * @param jp2 the jpeg2000 file codec.
1298 * @param p_colr_header_size pointer that will hold the size of the color header
1299 * @param p_colr_header_max_size maximum size of the header, any size bigger than this value should result the function to output false.
1300 * @param p_manager the user event manager.
1302 * @return true if the bpc header is valid, fale else.
1304 static opj_bool jp2_read_colr_v2( opj_jp2_v2_t * jp2,
1305 unsigned char * p_colr_header_data,
1306 OPJ_UINT32 p_colr_header_size,
1307 opj_event_mgr_t * p_manager
1314 assert(p_colr_header_data != 00);
1315 assert(p_manager != 00);
1317 if (p_colr_header_size < 3) {
1318 opj_event_msg_v2(p_manager, EVT_ERROR, "Bad COLR header box (bad size)\n");
1322 /* Part 1, I.5.3.3 : 'A conforming JP2 reader shall ignore all Colour
1323 * Specification boxes after the first.'
1325 if(jp2->color.jp2_has_colr) {
1326 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");
1327 p_colr_header_data += p_colr_header_size;
1331 opj_read_bytes(p_colr_header_data,&jp2->meth ,1); /* METH */
1332 ++p_colr_header_data;
1334 opj_read_bytes(p_colr_header_data,&jp2->precedence ,1); /* PRECEDENCE */
1335 ++p_colr_header_data;
1337 opj_read_bytes(p_colr_header_data,&jp2->approx ,1); /* APPROX */
1338 ++p_colr_header_data;
1340 if (jp2->meth == 1) {
1341 if (p_colr_header_size != 7) {
1342 opj_event_msg_v2(p_manager, EVT_ERROR, "Bad BPCC header box (bad size)\n");
1346 opj_read_bytes(p_colr_header_data,&jp2->enumcs ,4); /* EnumCS */
1348 else if (jp2->meth == 2) {
1350 int it_icc_value = 0;
1351 int icc_len = p_colr_header_size - 3;
1353 jp2->color.icc_profile_len = icc_len;
1354 jp2->color.icc_profile_buf = (unsigned char*) opj_malloc(icc_len);
1356 memset(jp2->color.icc_profile_buf, 0, icc_len * sizeof(unsigned char));
1358 for (it_icc_value = 0; it_icc_value < icc_len; ++it_icc_value)
1360 opj_read_bytes(p_colr_header_data,&l_value,1); /* icc values */
1361 ++p_colr_header_data;
1362 jp2->color.icc_profile_buf[it_icc_value] = (OPJ_BYTE) l_value;
1367 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);
1369 jp2->color.jp2_has_colr = 1;
1374 opj_bool jp2_read_jp2h(opj_jp2_t *jp2, opj_cio_t *cio, opj_jp2_color_t *color)
1379 opj_common_ptr cinfo = jp2->cinfo;
1381 jp2_read_boxhdr(cinfo, cio, &box);
1384 if (JP2_JP2H != box.type)
1386 if (box.type == JP2_JP2C)
1388 opj_event_msg(cinfo, EVT_ERROR, "Expected JP2H Marker\n");
1391 cio_skip(cio, box.length - 8);
1393 if(cio->bp >= cio->end) return OPJ_FALSE;
1395 jp2_read_boxhdr(cinfo, cio, &box);
1397 } while(JP2_JP2H != box.type);
1399 if (!jp2_read_ihdr(jp2, cio))
1401 jp2h_end = box.init_pos + box.length;
1403 if (jp2->bpc == 255)
1405 if (!jp2_read_bpcc(jp2, cio))
1408 jp2_read_boxhdr(cinfo, cio, &box);
1410 while(cio_tell(cio) < jp2h_end)
1412 if(box.type == JP2_COLR)
1414 if( !jp2_read_colr(jp2, cio, &box, color))
1416 cio_seek(cio, box.init_pos + 8);
1417 cio_skip(cio, box.length - 8);
1419 jp2_read_boxhdr(cinfo, cio, &box);
1422 if(box.type == JP2_CDEF && !jp2->ignore_pclr_cmap_cdef)
1424 if( !jp2_read_cdef(jp2, cio, &box, color))
1426 cio_seek(cio, box.init_pos + 8);
1427 cio_skip(cio, box.length - 8);
1429 jp2_read_boxhdr(cinfo, cio, &box);
1432 if(box.type == JP2_PCLR && !jp2->ignore_pclr_cmap_cdef)
1434 if( !jp2_read_pclr(jp2, cio, &box, color))
1436 cio_seek(cio, box.init_pos + 8);
1437 cio_skip(cio, box.length - 8);
1439 jp2_read_boxhdr(cinfo, cio, &box);
1442 if(box.type == JP2_CMAP && !jp2->ignore_pclr_cmap_cdef)
1444 if( !jp2_read_cmap(jp2, cio, &box, color))
1446 cio_seek(cio, box.init_pos + 8);
1447 cio_skip(cio, box.length - 8);
1449 jp2_read_boxhdr(cinfo, cio, &box);
1452 cio_seek(cio, box.init_pos + 8);
1453 cio_skip(cio, box.length - 8);
1454 jp2_read_boxhdr(cinfo, cio, &box);
1456 }/* while(cio_tell(cio) < box_end) */
1458 cio_seek(cio, jp2h_end);
1460 /* Part 1, I.5.3.3 : 'must contain at least one' */
1461 return (color->jp2_has_colr == 1);
1463 }/* jp2_read_jp2h() */
1465 opj_image_t* opj_jp2_decode(opj_jp2_t *jp2, opj_cio_t *cio,
1466 opj_codestream_info_t *cstr_info)
1468 opj_common_ptr cinfo;
1469 opj_image_t *image = NULL;
1470 opj_jp2_color_t color;
1476 memset(&color, 0, sizeof(opj_jp2_color_t));
1480 if(!jp2_read_struct(jp2, cio, &color))
1482 free_color_data(&color);
1483 opj_event_msg(cinfo, EVT_ERROR, "Failed to decode jp2 structure\n");
1488 image = j2k_decode(jp2->j2k, cio, cstr_info);
1492 free_color_data(&color);
1493 opj_event_msg(cinfo, EVT_ERROR, "Failed to decode J2K image\n");
1497 if (!jp2->ignore_pclr_cmap_cdef){
1499 /* Set Image Color Space */
1500 if (jp2->enumcs == 16)
1501 image->color_space = CLRSPC_SRGB;
1502 else if (jp2->enumcs == 17)
1503 image->color_space = CLRSPC_GRAY;
1504 else if (jp2->enumcs == 18)
1505 image->color_space = CLRSPC_SYCC;
1507 image->color_space = CLRSPC_UNKNOWN;
1511 jp2_apply_cdef(image, &color);
1515 /* Part 1, I.5.3.4: Either both or none : */
1516 if( !color.jp2_pclr->cmap)
1517 jp2_free_pclr(&color);
1519 jp2_apply_pclr(image, &color);
1521 if(color.icc_profile_buf)
1523 image->icc_profile_buf = color.icc_profile_buf;
1524 color.icc_profile_buf = NULL;
1525 image->icc_profile_len = color.icc_profile_len;
1531 }/* opj_jp2_decode() */
1533 opj_bool jp2_decode_v2( opj_jp2_v2_t *jp2,
1534 struct opj_stream_private *cio,
1535 opj_image_t* p_image,
1536 struct opj_event_mgr * p_manager)
1542 if( ! j2k_decode_v2(jp2->j2k, cio, p_image, p_manager) ) {
1543 opj_event_msg_v2(p_manager, EVT_ERROR, "Failed to decode the codestream in the JP2 file\n");
1547 /* Set Image Color Space */
1548 if (jp2->enumcs == 16)
1549 p_image->color_space = CLRSPC_SRGB;
1550 else if (jp2->enumcs == 17)
1551 p_image->color_space = CLRSPC_GRAY;
1552 else if (jp2->enumcs == 18)
1553 p_image->color_space = CLRSPC_SYCC;
1555 p_image->color_space = CLRSPC_UNKNOWN;
1557 /* Apply the color space if needed */
1558 if(jp2->color.jp2_cdef) {
1559 jp2_apply_cdef(p_image, &(jp2->color));
1562 if(jp2->color.jp2_pclr) {
1563 /* Part 1, I.5.3.4: Either both or none : */
1564 if( !jp2->color.jp2_pclr->cmap)
1565 jp2_free_pclr(&(jp2->color));
1567 jp2_apply_pclr(p_image, &(jp2->color));
1570 if(jp2->color.icc_profile_buf) {
1571 p_image->icc_profile_buf = jp2->color.icc_profile_buf;
1572 p_image->icc_profile_len = jp2->color.icc_profile_len;
1573 jp2->color.icc_profile_buf = NULL;
1580 void jp2_write_jp2h(opj_jp2_t *jp2, opj_cio_t *cio) {
1583 box.init_pos = cio_tell(cio);
1585 cio_write(cio, JP2_JP2H, 4); /* JP2H */
1587 jp2_write_ihdr(jp2, cio);
1589 if (jp2->bpc == 255) {
1590 jp2_write_bpcc(jp2, cio);
1592 jp2_write_colr(jp2, cio);
1594 box.length = cio_tell(cio) - box.init_pos;
1595 cio_seek(cio, box.init_pos);
1596 cio_write(cio, box.length, 4); /* L */
1597 cio_seek(cio, box.init_pos + box.length);
1600 static void jp2_write_ftyp(opj_jp2_t *jp2, opj_cio_t *cio) {
1604 box.init_pos = cio_tell(cio);
1606 cio_write(cio, JP2_FTYP, 4); /* FTYP */
1608 cio_write(cio, jp2->brand, 4); /* BR */
1609 cio_write(cio, jp2->minversion, 4); /* MinV */
1611 for (i = 0; i < jp2->numcl; i++) {
1612 cio_write(cio, jp2->cl[i], 4); /* CL */
1615 box.length = cio_tell(cio) - box.init_pos;
1616 cio_seek(cio, box.init_pos);
1617 cio_write(cio, box.length, 4); /* L */
1618 cio_seek(cio, box.init_pos + box.length);
1621 static opj_bool jp2_read_ftyp(opj_jp2_t *jp2, opj_cio_t *cio) {
1625 opj_common_ptr cinfo = jp2->cinfo;
1627 jp2_read_boxhdr(cinfo, cio, &box);
1629 if (JP2_FTYP != box.type) {
1630 opj_event_msg(cinfo, EVT_ERROR, "Expected FTYP Marker\n");
1634 jp2->brand = cio_read(cio, 4); /* BR */
1635 jp2->minversion = cio_read(cio, 4); /* MinV */
1636 jp2->numcl = (box.length - 16) / 4;
1637 jp2->cl = (unsigned int *) opj_malloc(jp2->numcl * sizeof(unsigned int));
1639 for (i = 0; i < (int)jp2->numcl; i++) {
1640 jp2->cl[i] = cio_read(cio, 4); /* CLi */
1643 if (cio_tell(cio) - box.init_pos != box.length) {
1644 opj_event_msg(cinfo, EVT_ERROR, "Error with FTYP Box\n");
1651 static int jp2_write_jp2c(opj_jp2_t *jp2, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info) {
1652 unsigned int j2k_codestream_offset, j2k_codestream_length;
1655 opj_j2k_t *j2k = jp2->j2k;
1657 box.init_pos = cio_tell(cio);
1659 cio_write(cio, JP2_JP2C, 4); /* JP2C */
1662 j2k_codestream_offset = cio_tell(cio);
1663 if(!j2k_encode(j2k, cio, image, cstr_info)) {
1664 opj_event_msg(j2k->cinfo, EVT_ERROR, "Failed to encode image\n");
1667 j2k_codestream_length = cio_tell(cio) - j2k_codestream_offset;
1669 jp2->j2k_codestream_offset = j2k_codestream_offset;
1670 jp2->j2k_codestream_length = j2k_codestream_length;
1672 box.length = 8 + jp2->j2k_codestream_length;
1673 cio_seek(cio, box.init_pos);
1674 cio_write(cio, box.length, 4); /* L */
1675 cio_seek(cio, box.init_pos + box.length);
1680 static opj_bool jp2_read_jp2c(opj_jp2_t *jp2, opj_cio_t *cio, unsigned int *j2k_codestream_length, unsigned int *j2k_codestream_offset) {
1683 opj_common_ptr cinfo = jp2->cinfo;
1685 jp2_read_boxhdr(cinfo, cio, &box);
1687 if(JP2_JP2C != box.type) {
1688 cio_skip(cio, box.length - 8);
1689 jp2_read_boxhdr(cinfo, cio, &box);
1691 } while(JP2_JP2C != box.type);
1693 *j2k_codestream_offset = cio_tell(cio);
1694 *j2k_codestream_length = box.length - 8;
1699 static void jp2_write_jp(opj_cio_t *cio) {
1702 box.init_pos = cio_tell(cio);
1704 cio_write(cio, JP2_JP, 4); /* JP2 signature */
1705 cio_write(cio, 0x0d0a870a, 4);
1707 box.length = cio_tell(cio) - box.init_pos;
1708 cio_seek(cio, box.init_pos);
1709 cio_write(cio, box.length, 4); /* L */
1710 cio_seek(cio, box.init_pos + box.length);
1713 static opj_bool jp2_read_jp(opj_jp2_t *jp2, opj_cio_t *cio) {
1716 opj_common_ptr cinfo = jp2->cinfo;
1718 jp2_read_boxhdr(cinfo, cio, &box);
1719 if (JP2_JP != box.type) {
1720 opj_event_msg(cinfo, EVT_ERROR, "Expected JP Marker\n");
1723 if (0x0d0a870a != cio_read(cio, 4)) {
1724 opj_event_msg(cinfo, EVT_ERROR, "Error with JP Marker\n");
1727 if (cio_tell(cio) - box.init_pos != box.length) {
1728 opj_event_msg(cinfo, EVT_ERROR, "Error with JP Box size\n");
1736 static opj_bool jp2_read_struct(opj_jp2_t *jp2, opj_cio_t *cio,
1737 opj_jp2_color_t *color) {
1738 if (!jp2_read_jp(jp2, cio))
1740 if (!jp2_read_ftyp(jp2, cio))
1742 if (!jp2_read_jp2h(jp2, cio, color))
1744 if (!jp2_read_jp2c(jp2, cio, &jp2->j2k_codestream_length, &jp2->j2k_codestream_offset))
1751 static int write_fidx( int offset_jp2c, int length_jp2c, int offset_idx, int length_idx, opj_cio_t *cio)
1755 lenp = cio_tell( cio);
1756 cio_skip( cio, 4); /* L [at the end] */
1757 cio_write( cio, JPIP_FIDX, 4); /* IPTR */
1759 write_prxy( offset_jp2c, length_jp2c, offset_idx, length_idx, cio);
1761 len = cio_tell( cio)-lenp;
1762 cio_seek( cio, lenp);
1763 cio_write( cio, len, 4); /* L */
1764 cio_seek( cio, lenp+len);
1769 static void write_prxy( int offset_jp2c, int length_jp2c, int offset_idx, int length_idx, opj_cio_t *cio)
1773 lenp = cio_tell( cio);
1774 cio_skip( cio, 4); /* L [at the end] */
1775 cio_write( cio, JPIP_PRXY, 4); /* IPTR */
1777 cio_write( cio, offset_jp2c, 8); /* OOFF */
1778 cio_write( cio, length_jp2c, 4); /* OBH part 1 */
1779 cio_write( cio, JP2_JP2C, 4); /* OBH part 2 */
1781 cio_write( cio, 1,1); /* NI */
1783 cio_write( cio, offset_idx, 8); /* IOFF */
1784 cio_write( cio, length_idx, 4); /* IBH part 1 */
1785 cio_write( cio, JPIP_CIDX, 4); /* IBH part 2 */
1787 len = cio_tell( cio)-lenp;
1788 cio_seek( cio, lenp);
1789 cio_write( cio, len, 4); /* L */
1790 cio_seek( cio, lenp+len);
1793 static void write_iptr( int offset, int length, opj_cio_t *cio)
1797 lenp = cio_tell( cio);
1798 cio_skip( cio, 4); /* L [at the end] */
1799 cio_write( cio, JPIP_IPTR, 4); /* IPTR */
1801 cio_write( cio, offset, 8);
1802 cio_write( cio, length, 8);
1804 len = cio_tell( cio)-lenp;
1805 cio_seek( cio, lenp);
1806 cio_write( cio, len, 4); /* L */
1807 cio_seek( cio, lenp+len);
1811 /* ----------------------------------------------------------------------- */
1812 /* JP2 decoder interface */
1813 /* ----------------------------------------------------------------------- */
1815 opj_jp2_t* jp2_create_decompress(opj_common_ptr cinfo) {
1816 opj_jp2_t *jp2 = (opj_jp2_t*) opj_calloc(1, sizeof(opj_jp2_t));
1819 /* create the J2K codec */
1820 jp2->j2k = j2k_create_decompress(cinfo);
1821 if(jp2->j2k == NULL) {
1822 jp2_destroy_decompress(jp2);
1829 void jp2_destroy_decompress(opj_jp2_t *jp2) {
1831 /* destroy the J2K codec */
1832 j2k_destroy_decompress(jp2->j2k);
1835 opj_free(jp2->comps);
1844 void jp2_setup_decoder(opj_jp2_t *jp2, opj_dparameters_t *parameters) {
1845 /* setup the J2K codec */
1846 j2k_setup_decoder(jp2->j2k, parameters);
1847 /* further JP2 initializations go here */
1848 jp2->ignore_pclr_cmap_cdef = parameters->flags & OPJ_DPARAMETERS_IGNORE_PCLR_CMAP_CDEF_FLAG;
1851 void jp2_setup_decoder_v2(opj_jp2_v2_t *jp2, opj_dparameters_t *parameters)
1853 /* setup the J2K codec */
1854 j2k_setup_decoder_v2(jp2->j2k, parameters);
1856 /* further JP2 initializations go here */
1857 jp2->color.jp2_has_colr = 0;
1861 /* ----------------------------------------------------------------------- */
1862 /* JP2 encoder interface */
1863 /* ----------------------------------------------------------------------- */
1865 opj_jp2_t* jp2_create_compress(opj_common_ptr cinfo) {
1866 opj_jp2_t *jp2 = (opj_jp2_t*)opj_malloc(sizeof(opj_jp2_t));
1869 /* create the J2K codec */
1870 jp2->j2k = j2k_create_compress(cinfo);
1871 if(jp2->j2k == NULL) {
1872 jp2_destroy_compress(jp2);
1879 void jp2_destroy_compress(opj_jp2_t *jp2) {
1881 /* destroy the J2K codec */
1882 j2k_destroy_compress(jp2->j2k);
1885 opj_free(jp2->comps);
1894 void jp2_setup_encoder(opj_jp2_t *jp2, opj_cparameters_t *parameters, opj_image_t *image) {
1898 if(!jp2 || !parameters || !image)
1901 /* setup the J2K codec */
1902 /* ------------------- */
1904 /* Check if number of components respects standard */
1905 if (image->numcomps < 1 || image->numcomps > 16384) {
1906 opj_event_msg(jp2->cinfo, EVT_ERROR, "Invalid number of components specified while setting up JP2 encoder\n");
1910 j2k_setup_encoder(jp2->j2k, parameters, image);
1912 /* setup the JP2 codec */
1913 /* ------------------- */
1917 jp2->brand = JP2_JP2; /* BR */
1918 jp2->minversion = 0; /* MinV */
1920 jp2->cl = (unsigned int*) opj_malloc(jp2->numcl * sizeof(unsigned int));
1921 jp2->cl[0] = JP2_JP2; /* CL0 : JP2 */
1923 /* Image Header box */
1925 jp2->numcomps = image->numcomps; /* NC */
1926 jp2->comps = (opj_jp2_comps_t*) opj_malloc(jp2->numcomps * sizeof(opj_jp2_comps_t));
1927 jp2->h = image->y1 - image->y0; /* HEIGHT */
1928 jp2->w = image->x1 - image->x0; /* WIDTH */
1930 depth_0 = image->comps[0].prec - 1;
1931 sign = image->comps[0].sgnd;
1932 jp2->bpc = depth_0 + (sign << 7);
1933 for (i = 1; i < image->numcomps; i++) {
1934 int depth = image->comps[i].prec - 1;
1935 sign = image->comps[i].sgnd;
1936 if (depth_0 != depth)
1939 jp2->C = 7; /* C : Always 7 */
1940 jp2->UnkC = 0; /* UnkC, colorspace specified in colr box */
1941 jp2->IPR = 0; /* IPR, no intellectual property */
1943 /* BitsPerComponent box */
1945 for (i = 0; i < image->numcomps; i++) {
1946 jp2->comps[i].bpcc = image->comps[i].prec - 1 + (image->comps[i].sgnd << 7);
1949 if (image->color_space == 1)
1950 jp2->enumcs = 16; /* sRGB as defined by IEC 61966-2.1 */
1951 else if (image->color_space == 2)
1952 jp2->enumcs = 17; /* greyscale */
1953 else if (image->color_space == 3)
1954 jp2->enumcs = 18; /* YUV */
1955 jp2->precedence = 0; /* PRECEDENCE */
1956 jp2->approx = 0; /* APPROX */
1958 jp2->jpip_on = parameters->jpip_on;
1961 opj_bool opj_jp2_encode(opj_jp2_t *jp2, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info) {
1963 int pos_iptr, pos_cidx, pos_jp2c, len_jp2c, len_cidx, end_pos, pos_fidx, len_fidx;
1964 pos_jp2c = pos_iptr = -1; /* remove a warning */
1968 /* JPEG 2000 Signature box */
1971 jp2_write_ftyp(jp2, cio);
1972 /* JP2 Header box */
1973 jp2_write_jp2h(jp2, cio);
1976 pos_iptr = cio_tell( cio);
1977 cio_skip( cio, 24); /* IPTR further ! */
1979 pos_jp2c = cio_tell( cio);
1983 if(!(len_jp2c = jp2_write_jp2c( jp2, cio, image, cstr_info))){
1984 opj_event_msg(jp2->cinfo, EVT_ERROR, "Failed to encode image\n");
1989 pos_cidx = cio_tell( cio);
1991 len_cidx = write_cidx( pos_jp2c+8, cio, image, *cstr_info, len_jp2c-8);
1993 pos_fidx = cio_tell( cio);
1994 len_fidx = write_fidx( pos_jp2c, len_jp2c, pos_cidx, len_cidx, cio);
1996 end_pos = cio_tell( cio);
1998 cio_seek( cio, pos_iptr);
1999 write_iptr( pos_fidx, len_fidx, cio);
2000 cio_seek( cio, end_pos);
2007 * Ends the decompression procedures and possibiliy add data to be read after the
2010 opj_bool jp2_end_decompress(opj_jp2_v2_t *jp2, opj_stream_private_t *cio, opj_event_mgr_t * p_manager)
2015 assert(p_manager != 00);
2017 /* customization of the end encoding */
2018 jp2_setup_end_header_reading(jp2);
2021 if (! jp2_exec (jp2,jp2->m_procedure_list,cio,p_manager)) {
2025 return j2k_end_decompress(jp2->j2k, cio, p_manager);
2029 * Ends the compression procedures and possibility add data to be read after the
2032 opj_bool jp2_end_compress( opj_jp2_v2_t *jp2,
2033 opj_stream_private_t *cio,
2034 opj_event_mgr_t * p_manager)
2039 assert(p_manager != 00);
2041 /* customization of the end encoding */
2042 jp2_setup_end_header_writting(jp2);
2044 if (! j2k_end_compress(jp2->j2k,cio,p_manager)) {
2049 return jp2_exec(jp2,jp2->m_procedure_list,cio,p_manager);
2054 * Sets up the procedures to do on writing header after the codestream.
2055 * Developers wanting to extend the library can add their own writing procedures.
2057 void jp2_setup_end_header_writting (opj_jp2_v2_t *jp2)
2062 opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)jp2_write_jp2c );
2063 /* DEVELOPER CORNER, add your custom procedures */
2067 * Sets up the procedures to do on reading header after the codestream.
2068 * Developers wanting to extend the library can add their own writing procedures.
2070 void jp2_setup_end_header_reading (opj_jp2_v2_t *jp2)
2074 opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)jp2_read_header_procedure );
2075 /* DEVELOPER CORNER, add your custom procedures */
2079 * The default validation procedure without any extension.
2081 * @param jp2 the jpeg2000 codec to validate.
2082 * @param cio the input stream to validate.
2083 * @param p_manager the user event manager.
2085 * @return true if the parameters are correct.
2087 opj_bool jp2_default_validation ( opj_jp2_v2_t * jp2,
2088 opj_stream_private_t *cio,
2089 opj_event_mgr_t * p_manager )
2091 opj_bool l_is_valid = OPJ_TRUE;
2097 assert(p_manager != 00);
2099 /* JPEG2000 codec validation */
2102 /* STATE checking */
2103 /* make sure the state is at 0 */
2104 l_is_valid &= (jp2->jp2_state == JP2_STATE_NONE);
2106 /* make sure not reading a jp2h ???? WEIRD */
2107 l_is_valid &= (jp2->jp2_img_state == JP2_IMG_STATE_NONE);
2109 /* POINTER validation */
2110 /* make sure a j2k codec is present */
2111 l_is_valid &= (jp2->j2k != 00);
2113 /* make sure a procedure list is present */
2114 l_is_valid &= (jp2->m_procedure_list != 00);
2116 /* make sure a validation list is present */
2117 l_is_valid &= (jp2->m_validation_list != 00);
2119 /* PARAMETER VALIDATION */
2120 /* number of components */
2121 l_is_valid &= (jp2->numcl > 0);
2123 l_is_valid &= (jp2->h > 0);
2125 l_is_valid &= (jp2->w > 0);
2127 for (i = 0; i < jp2->numcomps; ++i) {
2128 l_is_valid &= (jp2->comps[i].bpcc > 0);
2132 l_is_valid &= ((jp2->meth > 0) && (jp2->meth < 3));
2134 /* stream validation */
2135 /* back and forth is needed */
2136 l_is_valid &= opj_stream_has_seek(cio);
2142 * Reads a jpeg2000 file header structure.
2144 * @param cio the stream to read data from.
2145 * @param jp2 the jpeg2000 file header structure.
2146 * @param p_manager the user event manager.
2148 * @return true if the box is valid.
2150 opj_bool jp2_read_header_procedure(
2152 opj_stream_private_t *cio,
2153 opj_event_mgr_t * p_manager)
2156 OPJ_UINT32 l_nb_bytes_read;
2157 const opj_jp2_header_handler_t * l_current_handler;
2158 OPJ_UINT32 l_last_data_size = BOX_SIZE;
2159 OPJ_UINT32 l_current_data_size;
2160 unsigned char * l_current_data = 00;
2165 assert(p_manager != 00);
2167 l_current_data = (unsigned char*)opj_malloc(l_last_data_size);
2169 if (l_current_data == 00) {
2170 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to handle jpeg2000 file header\n");
2173 memset(l_current_data, 0 , l_last_data_size);
2175 while (jp2_read_boxhdr_v2(&box,&l_nb_bytes_read,cio,p_manager)) {
2176 /* is it the codestream box ? */
2177 if (box.type == JP2_JP2C) {
2178 if (jp2->jp2_state & JP2_STATE_HEADER) {
2179 jp2->jp2_state |= JP2_STATE_CODESTREAM;
2180 opj_free(l_current_data);
2184 opj_event_msg_v2(p_manager, EVT_ERROR, "bad placed jpeg codestream\n");
2185 opj_free(l_current_data);
2189 else if (box.length == 0) {
2190 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot handle box of undefined sizes\n");
2191 opj_free(l_current_data);
2195 l_current_handler = jp2_find_handler(box.type);
2196 l_current_data_size = box.length - l_nb_bytes_read;
2198 if (l_current_handler != 00) {
2199 if (l_current_data_size > l_last_data_size) {
2200 l_current_data = (unsigned char*)opj_realloc(l_current_data,l_current_data_size);
2201 if (!l_current_data){
2202 opj_free(l_current_data);
2205 l_last_data_size = l_current_data_size;
2208 l_nb_bytes_read = opj_stream_read_data(cio,l_current_data,l_current_data_size,p_manager);
2209 if (l_nb_bytes_read != l_current_data_size) {
2210 opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with reading JPEG2000 box, stream error\n");
2214 if (! l_current_handler->handler(jp2,l_current_data,l_current_data_size,p_manager)) {
2215 opj_free(l_current_data);
2220 jp2->jp2_state |= JP2_STATE_UNKNOWN;
2221 if (opj_stream_skip(cio,l_current_data_size,p_manager) != l_current_data_size) {
2222 opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with skipping JPEG2000 box, stream error\n");
2223 opj_free(l_current_data);
2229 opj_free(l_current_data);
2235 * Excutes the given procedures on the given codec.
2237 * @param p_procedure_list the list of procedures to execute
2238 * @param jp2 the jpeg2000 file codec to execute the procedures on.
2239 * @param cio the stream to execute the procedures on.
2240 * @param p_manager the user manager.
2242 * @return true if all the procedures were successfully executed.
2246 opj_procedure_list_t * p_procedure_list,
2247 opj_stream_private_t *cio,
2248 opj_event_mgr_t * p_manager
2251 opj_bool (** l_procedure) (opj_jp2_v2_t * jp2, opj_stream_private_t *, opj_event_mgr_t *) = 00;
2252 opj_bool l_result = OPJ_TRUE;
2253 OPJ_UINT32 l_nb_proc, i;
2256 assert(p_procedure_list != 00);
2259 assert(p_manager != 00);
2261 l_nb_proc = opj_procedure_list_get_nb_procedures(p_procedure_list);
2262 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);
2264 for (i=0;i<l_nb_proc;++i) {
2265 l_result = l_result && (*l_procedure) (jp2,cio,p_manager);
2269 /* and clear the procedure list at the end. */
2270 opj_procedure_list_clear(p_procedure_list);
2275 * Starts a compression scheme, i.e. validates the codec parameters, writes the header.
2277 * @param jp2 the jpeg2000 file codec.
2278 * @param cio the stream object.
2280 * @return true if the codec is valid.
2282 opj_bool jp2_start_compress(opj_jp2_v2_t *jp2,
2283 struct opj_stream_private *cio,
2284 opj_image_t * p_image,
2285 struct opj_event_mgr * p_manager)
2290 assert(p_manager != 00);
2292 /* customization of the validation */
2293 jp2_setup_encoding_validation (jp2);
2295 /* validation of the parameters codec */
2296 if (! jp2_exec(jp2,jp2->m_validation_list,cio,p_manager)) {
2300 /* customization of the encoding */
2301 jp2_setup_header_writting(jp2);
2304 if (! jp2_exec (jp2,jp2->m_procedure_list,cio,p_manager)) {
2308 return j2k_start_compress(jp2->j2k,cio,p_image,p_manager);
2312 * Finds the execution function related to the given box id.
2314 * @param p_id the id of the handler to fetch.
2316 * @return the given handler or 00 if it could not be found.
2318 const opj_jp2_header_handler_t * jp2_find_handler (int p_id)
2320 OPJ_UINT32 i, l_handler_size = sizeof(jp2_header) / sizeof(opj_jp2_header_handler_t);
2322 for (i=0;i<l_handler_size;++i) {
2323 if (jp2_header[i].id == p_id) {
2324 return &jp2_header[i];
2331 * Finds the image execution function related to the given box id.
2333 * @param p_id the id of the handler to fetch.
2335 * @return the given handler or 00 if it could not be found.
2337 static const opj_jp2_header_handler_t * jp2_img_find_handler (
2341 OPJ_UINT32 i, l_handler_size = sizeof(jp2_img_header) / sizeof(opj_jp2_header_handler_t);
2342 for (i=0;i<l_handler_size;++i)
2344 if (jp2_img_header[i].id == p_id) {
2345 return &jp2_img_header[i];
2354 * Reads a jpeg2000 file signature box.
2356 * @param p_header_data the data contained in the signature box.
2357 * @param jp2 the jpeg2000 file codec.
2358 * @param p_header_size the size of the data contained in the signature box.
2359 * @param p_manager the user event manager.
2361 * @return true if the file signature box is valid.
2363 opj_bool jp2_read_jp_v2(
2365 unsigned char * p_header_data,
2366 unsigned int p_header_size,
2367 opj_event_mgr_t * p_manager
2370 unsigned int l_magic_number;
2373 assert(p_header_data != 00);
2375 assert(p_manager != 00);
2377 if (jp2->jp2_state != JP2_STATE_NONE) {
2378 opj_event_msg_v2(p_manager, EVT_ERROR, "The signature box must be the first box in the file.\n");
2382 /* assure length of data is correct (4 -> magic number) */
2383 if (p_header_size != 4) {
2384 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with JP signature Box size\n");
2388 /* rearrange data */
2389 opj_read_bytes(p_header_data,&l_magic_number,4);
2390 if (l_magic_number != 0x0d0a870a ) {
2391 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with JP Signature : bad magic number\n");
2395 jp2->jp2_state |= JP2_STATE_SIGNATURE;
2402 * Reads a a FTYP box - File type box
2404 * @param p_header_data the data contained in the FTYP box.
2405 * @param jp2 the jpeg2000 file codec.
2406 * @param p_header_size the size of the data contained in the FTYP box.
2407 * @param p_manager the user event manager.
2409 * @return true if the FTYP box is valid.
2411 opj_bool jp2_read_ftyp_v2(
2413 unsigned char * p_header_data,
2414 unsigned int p_header_size,
2415 opj_event_mgr_t * p_manager
2418 OPJ_UINT32 i, l_remaining_bytes;
2421 assert(p_header_data != 00);
2423 assert(p_manager != 00);
2425 if (jp2->jp2_state != JP2_STATE_SIGNATURE) {
2426 opj_event_msg_v2(p_manager, EVT_ERROR, "The ftyp box must be the second box in the file.\n");
2430 /* assure length of data is correct */
2431 if (p_header_size < 8) {
2432 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with FTYP signature Box size\n");
2436 opj_read_bytes(p_header_data,&jp2->brand,4); /* BR */
2439 opj_read_bytes(p_header_data,&jp2->minversion,4); /* MinV */
2442 l_remaining_bytes = p_header_size - 8;
2444 /* the number of remaining bytes should be a multiple of 4 */
2445 if ((l_remaining_bytes & 0x3) != 0) {
2446 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with FTYP signature Box size\n");
2451 jp2->numcl = l_remaining_bytes >> 2;
2453 jp2->cl = (unsigned int *) opj_malloc(jp2->numcl * sizeof(unsigned int));
2454 if (jp2->cl == 00) {
2455 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory with FTYP Box\n");
2458 memset(jp2->cl,0,jp2->numcl * sizeof(unsigned int));
2461 for (i = 0; i < jp2->numcl; ++i)
2463 opj_read_bytes(p_header_data,&jp2->cl[i],4); /* CLi */
2467 jp2->jp2_state |= JP2_STATE_FILE_TYPE;
2473 * Skips the Jpeg2000 Codestream Header box - JP2C Header box.
2475 * @param cio the stream to write data to.
2476 * @param jp2 the jpeg2000 file codec.
2477 * @param p_manager user event manager.
2479 * @return true if writting was successful.
2481 opj_bool jp2_skip_jp2c( opj_jp2_v2_t *jp2,
2482 struct opj_stream_private *cio,
2483 struct opj_event_mgr * p_manager )
2488 assert(p_manager != 00);
2490 jp2->j2k_codestream_offset = opj_stream_tell(cio);
2492 if (opj_stream_skip(cio,8,p_manager) != 8) {
2500 * Reads the Jpeg2000 file Header box - JP2 Header box (warning, this is a super box).
2502 * @param p_header_data the data contained in the file header box.
2503 * @param jp2 the jpeg2000 file codec.
2504 * @param p_header_size the size of the data contained in the file header box.
2505 * @param p_manager the user event manager.
2507 * @return true if the JP2 Header box was successfully reconized.
2509 opj_bool jp2_read_jp2h_v2(
2511 unsigned char * p_header_data,
2512 unsigned int p_header_size,
2513 opj_event_mgr_t * p_manager
2516 OPJ_UINT32 l_box_size=0, l_current_data_size = 0;
2518 const opj_jp2_header_handler_t * l_current_handler;
2521 assert(p_header_data != 00);
2523 assert(p_manager != 00);
2525 /* make sure the box is well placed */
2526 if ((jp2->jp2_state & JP2_STATE_FILE_TYPE) != JP2_STATE_FILE_TYPE ) {
2527 opj_event_msg_v2(p_manager, EVT_ERROR, "The box must be the first box in the file.\n");
2531 jp2->jp2_img_state = JP2_IMG_STATE_NONE;
2533 /* iterate while remaining data */
2534 while (p_header_size > 0) {
2536 if (! jp2_read_boxhdr_char(&box,p_header_data,&l_box_size,p_header_size, p_manager)) {
2537 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream error while reading JP2 Header box\n");
2541 if (box.length > p_header_size) {
2542 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream error while reading JP2 Header box: box length is inconsistent.\n");
2546 l_current_handler = jp2_img_find_handler(box.type);
2547 l_current_data_size = box.length - l_box_size;
2548 p_header_data += l_box_size;
2550 if (l_current_handler != 00) {
2551 if (! l_current_handler->handler(jp2,p_header_data,l_current_data_size,p_manager)) {
2556 jp2->jp2_img_state |= JP2_IMG_STATE_UNKNOWN;
2559 p_header_data += l_current_data_size;
2560 p_header_size -= box.length;
2563 jp2->jp2_state |= JP2_STATE_HEADER;
2569 * Reads a box header. The box is the way data is packed inside a jpeg2000 file structure. Data is read from a character string
2571 * @param p_data the character string to read data from.
2572 * @param box the box structure to fill.
2573 * @param p_number_bytes_read pointer to an int that will store the number of bytes read from the stream (shoul usually be 2).
2574 * @param p_box_max_size the maximum number of bytes in the box.
2576 * @return true if the box is reconized, false otherwise
2578 static opj_bool jp2_read_boxhdr_char(
2581 OPJ_UINT32 * p_number_bytes_read,
2582 OPJ_UINT32 p_box_max_size,
2583 opj_event_mgr_t * p_manager
2589 assert(p_data != 00);
2591 assert(p_number_bytes_read != 00);
2592 assert(p_manager != 00);
2594 if (p_box_max_size < 8) {
2595 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot handle box of less than 8 bytes\n");
2599 /* process read data */
2600 opj_read_bytes(p_data, &l_value, 4);
2602 box->length = (OPJ_INT32)(l_value);
2604 opj_read_bytes(p_data, &l_value, 4);
2606 box->type = (OPJ_INT32)(l_value);
2608 *p_number_bytes_read = 8;
2610 /* do we have a "special very large box ?" */
2611 /* read then the XLBox */
2612 if (box->length == 1) {
2613 unsigned int l_xl_part_size;
2615 if (p_box_max_size < 16) {
2616 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot handle XL box of less than 16 bytes\n");
2620 opj_read_bytes(p_data,&l_xl_part_size, 4);
2622 *p_number_bytes_read += 4;
2624 if (l_xl_part_size != 0) {
2625 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot handle box sizes higher than 2^32\n");
2629 opj_read_bytes(p_data, &l_value, 4);
2630 *p_number_bytes_read += 4;
2631 box->length = (OPJ_INT32)(l_value);
2633 if (box->length == 0) {
2634 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot handle box of undefined sizes\n");
2638 else if (box->length == 0) {
2639 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot handle box of undefined sizes\n");
2648 * Reads a jpeg2000 file header structure.
2650 * @param cio the stream to read data from.
2651 * @param jp2 the jpeg2000 file header structure.
2652 * @param p_manager the user event manager.
2654 * @return true if the box is valid.
2656 opj_bool jp2_read_header( struct opj_stream_private *p_stream,
2658 opj_image_t** p_image,
2659 struct opj_event_mgr * p_manager
2664 assert(p_stream != 00);
2665 assert(p_manager != 00);
2667 /* customization of the validation */
2668 jp2_setup_decoding_validation (jp2);
2670 /* customization of the encoding */
2671 jp2_setup_header_reading(jp2);
2673 /* validation of the parameters codec */
2674 if (! jp2_exec(jp2,jp2->m_validation_list,p_stream,p_manager)) {
2679 if (! jp2_exec (jp2,jp2->m_procedure_list,p_stream,p_manager)) {
2683 return j2k_read_header( p_stream,
2690 * Sets up the validation ,i.e. adds the procedures to launch to make sure the codec parameters
2691 * are valid. Developers wanting to extend the library can add their own validation procedures.
2693 void jp2_setup_encoding_validation (opj_jp2_v2_t *jp2)
2698 opj_procedure_list_add_procedure(jp2->m_validation_list, (opj_procedure)jp2_default_validation);
2699 /* DEVELOPER CORNER, add your custom validation procedure */
2703 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
2704 * are valid. Developpers wanting to extend the library can add their own validation procedures.
2706 void jp2_setup_decoding_validation (opj_jp2_v2_t *jp2)
2710 /* DEVELOPER CORNER, add your custom validation procedure */
2714 * Sets up the procedures to do on writting header.
2715 * Developers wanting to extend the library can add their own writing procedures.
2717 void jp2_setup_header_writting (opj_jp2_v2_t *jp2)
2722 opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)jp2_write_jp );
2723 opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)jp2_write_ftyp );
2724 opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)jp2_write_jp2h );
2725 opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)jp2_skip_jp2c );
2727 /* DEVELOPER CORNER, insert your custom procedures */
2732 * Sets up the procedures to do on reading header.
2733 * Developpers wanting to extend the library can add their own writting procedures.
2735 void jp2_setup_header_reading (opj_jp2_v2_t *jp2)
2740 opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)jp2_read_header_procedure );
2741 /* DEVELOPER CORNER, add your custom procedures */
2746 * Reads a tile header.
2747 * @param p_j2k the jpeg2000 codec.
2748 * @param p_stream the stream to write data to.
2749 * @param p_manager the user event manager.
2751 opj_bool jp2_read_tile_header( opj_jp2_v2_t * p_jp2,
2752 OPJ_UINT32 * p_tile_index,
2753 OPJ_UINT32 * p_data_size,
2754 OPJ_INT32 * p_tile_x0, OPJ_INT32 * p_tile_y0,
2755 OPJ_INT32 * p_tile_x1, OPJ_INT32 * p_tile_y1,
2756 OPJ_UINT32 * p_nb_comps,
2758 opj_stream_private_t *p_stream,
2759 opj_event_mgr_t * p_manager )
2761 return j2k_read_tile_header(p_jp2->j2k,
2764 p_tile_x0, p_tile_y0,
2765 p_tile_x1, p_tile_y1,
2774 * @param p_j2k the jpeg2000 codec.
2775 * @param p_stream the stream to write data to.
2776 * @param p_manager the user event manager.
2778 opj_bool jp2_write_tile ( opj_jp2_v2_t *p_jp2,
2779 OPJ_UINT32 p_tile_index,
2781 OPJ_UINT32 p_data_size,
2782 struct opj_stream_private *p_stream,
2783 struct opj_event_mgr * p_manager )
2785 return j2k_write_tile (p_jp2->j2k,p_tile_index,p_data,p_data_size,p_stream,p_manager);
2790 * @param p_j2k the jpeg2000 codec.
2791 * @param p_stream the stream to write data to.
2792 * @param p_manager the user event manager.
2794 opj_bool jp2_decode_tile (
2795 opj_jp2_v2_t * p_jp2,
2796 OPJ_UINT32 p_tile_index,
2798 OPJ_UINT32 p_data_size,
2799 opj_stream_private_t *p_stream,
2800 opj_event_mgr_t * p_manager
2803 return j2k_decode_tile (p_jp2->j2k,p_tile_index,p_data,p_data_size,p_stream,p_manager);
2807 * Destroys a jpeg2000 file decompressor.
2809 * @param jp2 a jpeg2000 file decompressor.
2811 void jp2_destroy(opj_jp2_v2_t *jp2)
2814 /* destroy the J2K codec */
2815 j2k_destroy(jp2->j2k);
2819 opj_free(jp2->comps);
2828 if (jp2->color.icc_profile_buf) {
2829 opj_free(jp2->color.icc_profile_buf);
2830 jp2->color.icc_profile_buf = 00;
2833 if (jp2->color.jp2_cdef) {
2834 if (jp2->color.jp2_cdef->info) {
2835 opj_free(jp2->color.jp2_cdef->info);
2836 jp2->color.jp2_cdef->info = NULL;
2839 opj_free(jp2->color.jp2_cdef);
2840 jp2->color.jp2_cdef = 00;
2843 if (jp2->color.jp2_pclr) {
2844 if (jp2->color.jp2_pclr->cmap) {
2845 opj_free(jp2->color.jp2_pclr->cmap);
2846 jp2->color.jp2_pclr->cmap = NULL;
2848 if (jp2->color.jp2_pclr->channel_sign) {
2849 opj_free(jp2->color.jp2_pclr->channel_sign);
2850 jp2->color.jp2_pclr->channel_sign = NULL;
2852 if (jp2->color.jp2_pclr->channel_size) {
2853 opj_free(jp2->color.jp2_pclr->channel_size);
2854 jp2->color.jp2_pclr->channel_size = NULL;
2856 if (jp2->color.jp2_pclr->entries) {
2857 opj_free(jp2->color.jp2_pclr->entries);
2858 jp2->color.jp2_pclr->entries = NULL;
2861 opj_free(jp2->color.jp2_pclr);
2862 jp2->color.jp2_pclr = 00;
2865 if (jp2->m_validation_list) {
2866 opj_procedure_list_destroy(jp2->m_validation_list);
2867 jp2->m_validation_list = 00;
2870 if (jp2->m_procedure_list) {
2871 opj_procedure_list_destroy(jp2->m_procedure_list);
2872 jp2->m_procedure_list = 00;
2880 * Sets the given area to be decoded. This function should be called right after opj_read_header and before any tile header reading.
2882 * @param p_jp2 the jpeg2000 codec.
2883 * @param p_end_x the right position of the rectangle to decode (in image coordinates).
2884 * @param p_start_y the up position of the rectangle to decode (in image coordinates).
2885 * @param p_end_y the bottom position of the rectangle to decode (in image coordinates).
2886 * @param p_manager the user event manager
2888 * @return true if the area could be set.
2890 opj_bool jp2_set_decode_area( opj_jp2_v2_t *p_jp2,
2891 opj_image_t* p_image,
2892 OPJ_INT32 p_start_x, OPJ_INT32 p_start_y,
2893 OPJ_INT32 p_end_x, OPJ_INT32 p_end_y,
2894 struct opj_event_mgr * p_manager )
2896 return j2k_set_decode_area(p_jp2->j2k, p_image, p_start_x, p_start_y, p_end_x, p_end_y, p_manager);
2900 * Get the decoded tile.
2902 * @param jp2 the jpeg2000 codec.
2903 * @param p_stream input_stream
2904 * @param p_image output image. .
2905 * @param p_manager the user event manager
2906 * @param tile_index index of the tile we want decode
2908 * @return true if succeed.
2910 opj_bool jp2_get_tile( opj_jp2_v2_t *jp2,
2911 opj_stream_private_t *p_stream,
2912 opj_image_t* p_image,
2913 struct opj_event_mgr * p_manager,
2914 OPJ_UINT32 tile_index )
2919 opj_event_msg_v2(p_manager, EVT_WARNING, "JP2 box which are after the codestream will not be read by this function.\n");
2921 if (! j2k_get_tile(jp2->j2k, p_stream, p_image, p_manager, tile_index) ){
2922 opj_event_msg_v2(p_manager, EVT_ERROR, "Failed to decode the codestream in the JP2 file\n");
2926 /* Set Image Color Space */
2927 if (jp2->enumcs == 16)
2928 p_image->color_space = CLRSPC_SRGB;
2929 else if (jp2->enumcs == 17)
2930 p_image->color_space = CLRSPC_GRAY;
2931 else if (jp2->enumcs == 18)
2932 p_image->color_space = CLRSPC_SYCC;
2934 p_image->color_space = CLRSPC_UNKNOWN;
2936 /* Apply the color space if needed */
2937 if(jp2->color.jp2_cdef) {
2938 jp2_apply_cdef(p_image, &(jp2->color));
2941 if(jp2->color.jp2_pclr) {
2942 /* Part 1, I.5.3.4: Either both or none : */
2943 if( !jp2->color.jp2_pclr->cmap)
2944 jp2_free_pclr(&(jp2->color));
2946 jp2_apply_pclr(p_image, &(jp2->color));
2949 if(jp2->color.icc_profile_buf) {
2950 p_image->icc_profile_buf = jp2->color.icc_profile_buf;
2951 p_image->icc_profile_len = jp2->color.icc_profile_len;
2952 jp2->color.icc_profile_buf = NULL;
2960 /* ----------------------------------------------------------------------- */
2961 /* JP2 encoder interface */
2962 /* ----------------------------------------------------------------------- */
2964 opj_jp2_v2_t* jp2_create(opj_bool p_is_decoder)
2966 opj_jp2_v2_t *jp2 = (opj_jp2_v2_t*)opj_malloc(sizeof(opj_jp2_v2_t));
2968 memset(jp2,0,sizeof(opj_jp2_t));
2970 /* create the J2K codec */
2971 if (! p_is_decoder) {
2972 jp2->j2k = j2k_create_compress_v2();
2975 jp2->j2k = j2k_create_decompress_v2();
2978 if (jp2->j2k == 00) {
2983 /* Color structure */
2984 jp2->color.icc_profile_buf = NULL;
2985 jp2->color.icc_profile_len = 0;
2986 jp2->color.jp2_cdef = NULL;
2987 jp2->color.jp2_pclr = NULL;
2988 jp2->color.jp2_has_colr = 0;
2990 /* validation list creation */
2991 jp2->m_validation_list = opj_procedure_list_create();
2992 if (! jp2->m_validation_list) {
2997 /* execution list creation */
2998 jp2->m_procedure_list = opj_procedure_list_create();
2999 if (! jp2->m_procedure_list) {
3008 void jp2_dump(opj_jp2_v2_t* p_jp2, OPJ_INT32 flag, FILE* out_stream)
3011 assert(p_jp2 != 00);
3013 j2k_dump(p_jp2->j2k,
3018 opj_codestream_index_t* jp2_get_cstr_index(opj_jp2_v2_t* p_jp2)
3020 return j2k_get_cstr_index(p_jp2->j2k);
3023 opj_codestream_info_v2_t* jp2_get_cstr_info(opj_jp2_v2_t* p_jp2)
3025 return j2k_get_cstr_info(p_jp2->j2k);
3028 opj_bool jp2_set_decoded_resolution_factor(opj_jp2_v2_t *p_jp2, OPJ_UINT32 res_factor, opj_event_mgr_t * p_manager)
3030 return j2k_set_decoded_resolution_factor(p_jp2->j2k, res_factor, p_manager);