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);
79 * Writes the Image Header box - Image Header box.
81 * @param jp2 jpeg2000 file codec.
82 * @param p_nb_bytes_written pointer to store the nb of bytes written by the function.
84 * @return the data being copied.
86 static unsigned char * jp2_write_ihdr_v2( opj_jp2_v2_t *jp2,
87 unsigned int * p_nb_bytes_written );
89 static void jp2_write_bpcc(opj_jp2_t *jp2, opj_cio_t *cio);
92 * Writes the Bit per Component box.
94 * @param jp2 jpeg2000 file codec.
95 * @param p_nb_bytes_written pointer to store the nb of bytes written by the function.
97 * @return the data being copied.
99 static unsigned char * jp2_write_bpcc_v2( opj_jp2_v2_t *jp2,
100 unsigned int * p_nb_bytes_written );
102 static opj_bool jp2_read_bpcc(opj_jp2_t *jp2, opj_cio_t *cio);
105 * Reads a Bit per Component box.
107 * @param p_bpc_header_data pointer to actual data (already read from file)
108 * @param jp2 the jpeg2000 file codec.
109 * @param p_bpc_header_size the size of the bpc header
110 * @param p_manager the user event manager.
112 * @return true if the bpc header is valid, fale else.
114 static opj_bool jp2_read_bpcc_v2(
116 unsigned char * p_bpc_header_data,
117 unsigned int p_bpc_header_size,
118 struct opj_event_mgr * p_manager
121 static opj_bool jp2_read_cdef_v2( opj_jp2_v2_t * jp2,
122 unsigned char * p_cdef_header_data,
123 OPJ_UINT32 p_cdef_header_size,
124 opj_event_mgr_t * p_manager
127 static void jp2_write_colr(opj_jp2_t *jp2, opj_cio_t *cio);
129 Write the FTYP box - File type box
130 @param jp2 JP2 handle
131 @param cio Output buffer stream
135 * Writes the Colour Specification box.
137 * @param jp2 jpeg2000 file codec.
138 * @param p_nb_bytes_written pointer to store the nb of bytes written by the function.
140 * @return the data being copied.
142 static unsigned char *jp2_write_colr_v2(opj_jp2_v2_t *jp2,
143 unsigned int * p_nb_bytes_written );
145 static void jp2_write_ftyp(opj_jp2_t *jp2, opj_cio_t *cio);
147 Read the FTYP box - File type box
148 @param jp2 JP2 handle
149 @param cio Input buffer stream
150 @return Returns true if successful, returns false otherwise
152 static opj_bool jp2_read_ftyp(opj_jp2_t *jp2, opj_cio_t *cio);
155 * Writes a FTYP box - File type box
157 * @param cio the stream to write data to.
158 * @param jp2 the jpeg2000 file codec.
159 * @param p_manager the user event manager.
161 * @return true if writting was successful.
163 static opj_bool jp2_write_ftyp_v2( opj_jp2_v2_t *jp2,
164 struct opj_stream_private *cio,
165 struct opj_event_mgr * p_manager );
168 * Reads a a FTYP box - File type box
170 * @param p_header_data the data contained in the FTYP box.
171 * @param jp2 the jpeg2000 file codec.
172 * @param p_header_size the size of the data contained in the FTYP box.
173 * @param p_manager the user event manager.
175 * @return true if the FTYP box is valid.
177 static opj_bool jp2_read_ftyp_v2( opj_jp2_v2_t *jp2,
178 unsigned char * p_header_data,
179 OPJ_UINT32 p_header_size,
180 struct opj_event_mgr * p_manager );
183 * Skips the Jpeg2000 Codestream Header box - JP2C Header box.
185 * @param cio the stream to write data to.
186 * @param jp2 the jpeg2000 file codec.
187 * @param p_manager user event manager.
189 * @return true if writting was successful.
191 opj_bool jp2_skip_jp2c( opj_jp2_v2_t *jp2,
192 struct opj_stream_private *cio,
193 struct opj_event_mgr * p_manager );
196 * Reads the Jpeg2000 file Header box - JP2 Header box (warning, this is a super box).
198 * @param p_header_data the data contained in the file header box.
199 * @param jp2 the jpeg2000 file codec.
200 * @param p_header_size the size of the data contained in the file header box.
201 * @param p_manager the user event manager.
203 * @return true if the JP2 Header box was successfully reconized.
205 static opj_bool jp2_read_jp2h_v2(
207 unsigned char * p_header_data,
208 unsigned int p_header_size,
209 struct opj_event_mgr * p_manager
212 static int jp2_write_jp2c(opj_jp2_t *jp2, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info);
213 static opj_bool jp2_read_jp2c(opj_jp2_t *jp2, opj_cio_t *cio, unsigned int *j2k_codestream_length, unsigned int *j2k_codestream_offset);
214 static void jp2_write_jp(opj_cio_t *cio);
216 Read the JP box - JPEG 2000 signature
217 @param jp2 JP2 handle
218 @param cio Input buffer stream
219 @return Returns true if successful, returns false otherwise
221 static opj_bool jp2_read_jp(opj_jp2_t *jp2, opj_cio_t *cio);
224 * Reads a jpeg2000 file signature box.
226 * @param p_header_data the data contained in the signature box.
227 * @param jp2 the jpeg2000 file codec.
228 * @param p_header_size the size of the data contained in the signature box.
229 * @param p_manager the user event manager.
231 * @return true if the file signature box is valid.
233 static opj_bool jp2_read_jp_v2(
235 unsigned char * p_header_data,
236 unsigned int p_header_size,
237 struct opj_event_mgr * p_manager
241 Decode the structure of a JP2 file
242 @param jp2 JP2 handle
243 @param cio Input buffer stream
244 @param color Collector for profile, cdef and pclr data
245 @return Returns true if successful, returns false otherwise
247 static opj_bool jp2_read_struct(opj_jp2_t *jp2, opj_cio_t *cio,
248 opj_jp2_color_t *color);
250 Apply collected palette data
251 @param color Collector for profile, cdef and pclr data
254 static void jp2_apply_pclr(opj_image_t *image, opj_jp2_color_t *color);
257 @param jp2 JP2 handle
258 @param cio Input buffer stream
260 @param color Collector for profile, cdef and pclr data
261 @return Returns true if successful, returns false otherwise
263 static opj_bool jp2_read_pclr(opj_jp2_t *jp2, opj_cio_t *cio,
264 opj_jp2_box_t *box, opj_jp2_color_t *color);
266 static opj_bool jp2_read_pclr_v2( opj_jp2_v2_t *jp2,
267 unsigned char * p_pclr_header_data,
268 OPJ_UINT32 p_pclr_header_size,
269 opj_event_mgr_t * p_manager
273 Collect component mapping data
274 @param jp2 JP2 handle
275 @param cio Input buffer stream
277 @param color Collector for profile, cdef and pclr data
278 @return Returns true if successful, returns false otherwise
280 static opj_bool jp2_read_cmap(opj_jp2_t *jp2, opj_cio_t *cio,
281 opj_jp2_box_t *box, opj_jp2_color_t *color);
284 static opj_bool jp2_read_cmap_v2( opj_jp2_v2_t * jp2,
285 unsigned char * p_cmap_header_data,
286 OPJ_UINT32 p_cmap_header_size,
287 opj_event_mgr_t * p_manager
291 Collect colour specification data
292 @param jp2 JP2 handle
293 @param cio Input buffer stream
295 @param color Collector for profile, cdef and pclr data
296 @return Returns true if successful, returns false otherwise
298 static opj_bool jp2_read_colr(opj_jp2_t *jp2, opj_cio_t *cio,
299 opj_jp2_box_t *box, opj_jp2_color_t *color);
302 * Reads the Color Specification box.
304 * @param p_colr_header_data pointer to actual data (already read from file)
305 * @param jp2 the jpeg2000 file codec.
306 * @param p_colr_header_size the size of the color header
307 * @param p_manager the user event manager.
309 * @return true if the bpc header is valid, fale else.
311 static opj_bool jp2_read_colr_v2(
313 unsigned char * p_colr_header_data,
314 OPJ_UINT32 p_colr_header_size,
315 struct opj_event_mgr * p_manager
319 Write file Index (superbox)
320 @param[in] offset_jp2c offset of jp2c box
321 @param[in] length_jp2c length of jp2c box
322 @param[in] offset_idx offset of cidx box
323 @param[in] length_idx length of cidx box
324 @param[in] cio file output handle
325 @return length of fidx box
327 static int write_fidx( int offset_jp2c, int length_jp2c, int offset_idx, int length_idx, opj_cio_t *cio);
329 Write index Finder box
330 @param[in] offset offset of fidx box
331 @param[in] length length of fidx box
332 @param[in] cio file output handle
334 static void write_iptr( int offset, int length, opj_cio_t *cio);
338 @param[in] offset_jp2c offset of jp2c box
339 @param[in] length_jp2c length of jp2c box
340 @param[in] offset_idx offset of cidx box
341 @param[in] length_idx length of cidx box
342 @param[in] cio file output handle
344 static void write_prxy( int offset_jp2c, int length_jp2c, int offset_idx, int length_idx, opj_cio_t *cio);
350 * Sets up the procedures to do on writting header after the codestream.
351 * Developpers wanting to extend the library can add their own writting procedures.
353 static void jp2_setup_end_header_writting (opj_jp2_v2_t *jp2);
357 * Sets up the procedures to do on reading header after the codestream.
358 * Developpers wanting to extend the library can add their own writting procedures.
360 static void jp2_setup_end_header_reading (opj_jp2_v2_t *jp2);
363 * Reads a jpeg2000 file header structure.
365 * @param cio the stream to read data from.
366 * @param jp2 the jpeg2000 file header structure.
367 * @param p_manager the user event manager.
369 * @return true if the box is valid.
371 opj_bool jp2_read_header_procedure(
373 struct opj_stream_private *cio,
374 struct opj_event_mgr * p_manager
378 * Excutes the given procedures on the given codec.
380 * @param p_procedure_list the list of procedures to execute
381 * @param jp2 the jpeg2000 file codec to execute the procedures on.
382 * @param cio the stream to execute the procedures on.
383 * @param p_manager the user manager.
385 * @return true if all the procedures were successfully executed.
387 static opj_bool jp2_exec (
389 struct opj_procedure_list * p_procedure_list,
390 struct opj_stream_private *cio,
391 struct opj_event_mgr * p_manager
395 * Reads a box header. The box is the way data is packed inside a jpeg2000 file structure.
397 * @param cio the input stream to read data from.
398 * @param box the box structure to fill.
399 * @param p_number_bytes_read pointer to an int that will store the number of bytes read from the stream (shoul usually be 2).
400 * @param p_manager user event manager.
402 * @return true if the box is reconized, false otherwise
404 static opj_bool jp2_read_boxhdr_v2(
406 OPJ_UINT32 * p_number_bytes_read,
407 struct opj_stream_private *cio,
408 struct opj_event_mgr * p_manager
412 * Finds the execution function related to the given box id.
414 * @param p_id the id of the handler to fetch.
416 * @return the given handler or NULL if it could not be found.
418 static const opj_jp2_header_handler_t * jp2_find_handler (int p_id );
421 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
422 * are valid. Developpers wanting to extend the library can add their own validation procedures.
424 static void jp2_setup_encoding_validation (opj_jp2_v2_t *jp2);
428 * Sets up the procedures to do on writting header. Developpers wanting to extend the library can add their own writting procedures.
430 static void jp2_setup_header_writting (opj_jp2_v2_t *jp2);
433 * The default validation procedure without any extension.
435 * @param jp2 the jpeg2000 codec to validate.
436 * @param cio the input stream to validate.
437 * @param p_manager the user event manager.
439 * @return true if the parameters are correct.
441 opj_bool jp2_default_validation ( opj_jp2_v2_t * jp2,
442 struct opj_stream_private *cio,
443 struct opj_event_mgr * p_manager );
446 * Finds the image execution function related to the given box id.
448 * @param p_id the id of the handler to fetch.
450 * @return the given handler or NULL if it could not be found.
452 static const opj_jp2_header_handler_t * jp2_img_find_handler (int p_id);
454 const opj_jp2_header_handler_t jp2_header [] =
456 {JP2_JP,jp2_read_jp_v2},
457 {JP2_FTYP,jp2_read_ftyp_v2},
458 {JP2_JP2H,jp2_read_jp2h_v2}
461 const opj_jp2_header_handler_t jp2_img_header [] =
463 {JP2_IHDR,jp2_read_ihdr_v2},
464 {JP2_COLR,jp2_read_colr_v2},
465 {JP2_BPCC,jp2_read_bpcc_v2},
466 {JP2_PCLR,jp2_read_pclr_v2},
467 {JP2_CMAP,jp2_read_cmap_v2},
468 {JP2_CDEF,jp2_read_cdef_v2}
473 * Reads a box header. The box is the way data is packed inside a jpeg2000 file structure. Data is read from a character string
475 * @param p_data the character string to read data from.
476 * @param box the box structure to fill.
477 * @param p_number_bytes_read pointer to an int that will store the number of bytes read from the stream (shoul usually be 2).
478 * @param p_box_max_size the maximum number of bytes in the box.
480 * @return true if the box is reconized, false otherwise
482 static opj_bool jp2_read_boxhdr_char(
485 OPJ_UINT32 * p_number_bytes_read,
486 OPJ_UINT32 p_box_max_size,
487 struct opj_event_mgr * p_manager
491 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
492 * are valid. Developpers wanting to extend the library can add their own validation procedures.
494 static void jp2_setup_decoding_validation (opj_jp2_v2_t *jp2);
497 * Sets up the procedures to do on reading header.
498 * Developpers wanting to extend the library can add their own writting procedures.
500 static void jp2_setup_header_reading (opj_jp2_v2_t *jp2);
504 /* ----------------------------------------------------------------------- */
506 static opj_bool jp2_read_boxhdr(opj_common_ptr cinfo, opj_cio_t *cio, opj_jp2_box_t *box) {
507 box->init_pos = cio_tell(cio);
508 box->length = cio_read(cio, 4);
509 box->type = cio_read(cio, 4);
510 if (box->length == 1) {
511 if (cio_read(cio, 4) != 0) {
512 opj_event_msg(cinfo, EVT_ERROR, "Cannot handle box sizes higher than 2^32\n");
515 box->length = cio_read(cio, 4);
516 if (box->length == 0)
517 box->length = cio_numbytesleft(cio) + 12;
519 else if (box->length == 0) {
520 box->length = cio_numbytesleft(cio) + 8;
527 * Reads a box header. The box is the way data is packed inside a jpeg2000 file structure.
529 * @param cio the input stream to read data from.
530 * @param box the box structure to fill.
531 * @param p_number_bytes_read pointer to an int that will store the number of bytes read from the stream (should usually be 8).
532 * @param p_manager user event manager.
534 * @return true if the box is reconized, false otherwise
536 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)
538 /* read header from file */
539 unsigned char l_data_header [8];
544 assert(p_number_bytes_read != 00);
545 assert(p_manager != 00);
547 *p_number_bytes_read = opj_stream_read_data(cio,l_data_header,8,p_manager);
548 if (*p_number_bytes_read != 8) {
552 /* process read data */
553 opj_read_bytes(l_data_header,&(box->length), 4);
554 opj_read_bytes(l_data_header+4,&(box->type), 4);
556 /* do we have a "special very large box ?" */
557 /* read then the XLBox */
558 if (box->length == 1) {
559 OPJ_UINT32 l_xl_part_size;
561 OPJ_UINT32 l_nb_bytes_read = opj_stream_read_data(cio,l_data_header,8,p_manager);
562 if (l_nb_bytes_read != 8) {
563 if (l_nb_bytes_read > 0) {
564 *p_number_bytes_read += l_nb_bytes_read;
570 opj_read_bytes(l_data_header,&l_xl_part_size, 4);
571 if (l_xl_part_size != 0) {
572 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot handle box sizes higher than 2^32\n");
575 opj_read_bytes(l_data_header,&(box->length), 4);
581 static void jp2_write_url(opj_cio_t *cio, char *Idx_file) {
585 box.init_pos = cio_tell(cio);
587 cio_write(cio, JP2_URL, 4); /* DBTL */
588 cio_write(cio, 0, 1); /* VERS */
589 cio_write(cio, 0, 3); /* FLAG */
592 for (i = 0; i < strlen(Idx_file); i++) {
593 cio_write(cio, Idx_file[i], 1);
597 box.length = cio_tell(cio) - box.init_pos;
598 cio_seek(cio, box.init_pos);
599 cio_write(cio, box.length, 4); /* L */
600 cio_seek(cio, box.init_pos + box.length);
604 static opj_bool jp2_read_ihdr(opj_jp2_t *jp2, opj_cio_t *cio) {
607 opj_common_ptr cinfo = jp2->cinfo;
609 jp2_read_boxhdr(cinfo, cio, &box);
610 if (JP2_IHDR != box.type) {
611 opj_event_msg(cinfo, EVT_ERROR, "Expected IHDR Marker\n");
615 jp2->h = cio_read(cio, 4); /* HEIGHT */
616 jp2->w = cio_read(cio, 4); /* WIDTH */
617 jp2->numcomps = cio_read(cio, 2); /* NC */
618 jp2->comps = (opj_jp2_comps_t*) opj_malloc(jp2->numcomps * sizeof(opj_jp2_comps_t));
620 jp2->bpc = cio_read(cio, 1); /* BPC */
622 jp2->C = cio_read(cio, 1); /* C */
623 jp2->UnkC = cio_read(cio, 1); /* UnkC */
624 jp2->IPR = cio_read(cio, 1); /* IPR */
626 if (cio_tell(cio) - box.init_pos != box.length) {
627 opj_event_msg(cinfo, EVT_ERROR, "Error with IHDR Box\n");
635 * Reads a IHDR box - Image Header box
637 * @param jp2 the jpeg2000 file codec.
638 * @param p_image_header_data pointer to actual data (already read from file)
639 * @param p_image_header_size the size of the image header
640 * @param p_manager the user event manager.
642 * @return true if the image header is valid, fale else.
644 opj_bool jp2_read_ihdr_v2(
646 unsigned char * p_image_header_data,
647 unsigned int p_image_header_size,
648 opj_event_mgr_t * p_manager
652 assert(p_image_header_data != 00);
654 assert(p_manager != 00);
656 if (p_image_header_size != 14) {
657 opj_event_msg_v2(p_manager, EVT_ERROR, "Bad image header box (bad size)\n");
661 opj_read_bytes(p_image_header_data,&(jp2->h),4); /* HEIGHT */
662 p_image_header_data += 4;
663 opj_read_bytes(p_image_header_data,&(jp2->w),4); /* WIDTH */
664 p_image_header_data += 4;
665 opj_read_bytes(p_image_header_data,&(jp2->numcomps),2); /* NC */
666 p_image_header_data += 2;
668 /* allocate memory for components */
669 jp2->comps = (opj_jp2_comps_t*) opj_malloc(jp2->numcomps * sizeof(opj_jp2_comps_t));
670 if (jp2->comps == 0) {
671 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to handle image header (ihdr)\n");
674 memset(jp2->comps,0,jp2->numcomps * sizeof(opj_jp2_comps_t));
676 opj_read_bytes(p_image_header_data,&(jp2->bpc),1); /* BPC */
677 ++ p_image_header_data;
679 /* if equal to 0 then need a BPC box (cf. chapter about image header box of the norm) */
680 /*if (jp2->bpc == 0){
681 indicate with a flag that we will wait a BPC box
684 opj_read_bytes(p_image_header_data,&(jp2->C),1); /* C */
685 ++ p_image_header_data;
687 /* Should be equal to 7 cf. chapter about image header box of the norm */
689 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);
692 opj_read_bytes(p_image_header_data,&(jp2->UnkC),1); /* UnkC */
693 ++ p_image_header_data;
694 opj_read_bytes(p_image_header_data,&(jp2->IPR),1); /* IPR */
695 ++ p_image_header_data;
700 static void jp2_write_ihdr(opj_jp2_t *jp2, opj_cio_t *cio) {
703 box.init_pos = cio_tell(cio);
705 cio_write(cio, JP2_IHDR, 4); /* IHDR */
707 cio_write(cio, jp2->h, 4); /* HEIGHT */
708 cio_write(cio, jp2->w, 4); /* WIDTH */
709 cio_write(cio, jp2->numcomps, 2); /* NC */
711 cio_write(cio, jp2->bpc, 1); /* BPC */
713 cio_write(cio, jp2->C, 1); /* C : Always 7 */
714 cio_write(cio, jp2->UnkC, 1); /* UnkC, colorspace unknown */
715 cio_write(cio, jp2->IPR, 1); /* IPR, no intellectual property */
717 box.length = cio_tell(cio) - box.init_pos;
718 cio_seek(cio, box.init_pos);
719 cio_write(cio, box.length, 4); /* L */
720 cio_seek(cio, box.init_pos + box.length);
724 * Writes the Image Header box - Image Header box.
726 * @param jp2 jpeg2000 file codec.
727 * @param p_nb_bytes_written pointer to store the nb of bytes written by the function.
729 * @return the data being copied.
731 static unsigned char * jp2_write_ihdr_v2( opj_jp2_v2_t *jp2,
732 unsigned int * p_nb_bytes_written )
734 unsigned char * l_ihdr_data,* l_current_ihdr_ptr;
738 assert(p_nb_bytes_written != 00);
740 /* default image header is 22 bytes wide */
741 l_ihdr_data = (unsigned char *) opj_malloc(22);
742 if (l_ihdr_data == 00) {
745 memset(l_ihdr_data,0,22);
747 l_current_ihdr_ptr = l_ihdr_data;
749 opj_write_bytes(l_current_ihdr_ptr,22,4); /* write box size */
750 l_current_ihdr_ptr+=4;
752 opj_write_bytes(l_current_ihdr_ptr,JP2_IHDR, 4); /* IHDR */
753 l_current_ihdr_ptr+=4;
755 opj_write_bytes(l_current_ihdr_ptr,jp2->h, 4); /* HEIGHT */
756 l_current_ihdr_ptr+=4;
758 opj_write_bytes(l_current_ihdr_ptr, jp2->w, 4); /* WIDTH */
759 l_current_ihdr_ptr+=4;
761 opj_write_bytes(l_current_ihdr_ptr, jp2->numcomps, 2); /* NC */
762 l_current_ihdr_ptr+=2;
764 opj_write_bytes(l_current_ihdr_ptr, jp2->bpc, 1); /* BPC */
765 ++l_current_ihdr_ptr;
767 opj_write_bytes(l_current_ihdr_ptr, jp2->C, 1); /* C : Always 7 */
768 ++l_current_ihdr_ptr;
770 opj_write_bytes(l_current_ihdr_ptr, jp2->UnkC, 1); /* UnkC, colorspace unknown */
771 ++l_current_ihdr_ptr;
773 opj_write_bytes(l_current_ihdr_ptr, jp2->IPR, 1); /* IPR, no intellectual property */
774 ++l_current_ihdr_ptr;
776 *p_nb_bytes_written = 22;
781 static void jp2_write_bpcc(opj_jp2_t *jp2, opj_cio_t *cio) {
785 box.init_pos = cio_tell(cio);
787 cio_write(cio, JP2_BPCC, 4); /* BPCC */
789 for (i = 0; i < jp2->numcomps; i++) {
790 cio_write(cio, jp2->comps[i].bpcc, 1);
793 box.length = cio_tell(cio) - box.init_pos;
794 cio_seek(cio, box.init_pos);
795 cio_write(cio, box.length, 4); /* L */
796 cio_seek(cio, box.init_pos + box.length);
801 * Writes the Bit per Component box.
803 * @param jp2 jpeg2000 file codec.
804 * @param p_nb_bytes_written pointer to store the nb of bytes written by the function.
806 * @return the data being copied.
808 unsigned char * jp2_write_bpcc_v2( opj_jp2_v2_t *jp2,
809 unsigned int * p_nb_bytes_written )
812 /* room for 8 bytes for box and 1 byte for each component */
813 int l_bpcc_size = 8 + jp2->numcomps;
814 unsigned char * l_bpcc_data,* l_current_bpcc_ptr;
818 assert(p_nb_bytes_written != 00);
820 l_bpcc_data = (unsigned char *) opj_malloc(l_bpcc_size);
821 if (l_bpcc_data == 00) {
824 memset(l_bpcc_data,0,l_bpcc_size);
826 l_current_bpcc_ptr = l_bpcc_data;
828 opj_write_bytes(l_current_bpcc_ptr,l_bpcc_size,4); /* write box size */
829 l_current_bpcc_ptr += 4;
831 opj_write_bytes(l_current_bpcc_ptr,JP2_BPCC,4); /* BPCC */
832 l_current_bpcc_ptr += 4;
834 for (i = 0; i < jp2->numcomps; ++i) {
835 opj_write_bytes(l_current_bpcc_ptr, jp2->comps[i].bpcc, 1); /* write each component information */
836 ++l_current_bpcc_ptr;
839 *p_nb_bytes_written = l_bpcc_size;
846 static opj_bool jp2_read_bpcc(opj_jp2_t *jp2, opj_cio_t *cio) {
850 opj_common_ptr cinfo = jp2->cinfo;
852 jp2_read_boxhdr(cinfo, cio, &box);
853 if (JP2_BPCC != box.type) {
854 opj_event_msg(cinfo, EVT_ERROR, "Expected BPCC Marker\n");
858 for (i = 0; i < jp2->numcomps; i++) {
859 jp2->comps[i].bpcc = cio_read(cio, 1);
862 if (cio_tell(cio) - box.init_pos != box.length) {
863 opj_event_msg(cinfo, EVT_ERROR, "Error with BPCC Box\n");
871 * Reads a Bit per Component box.
873 * @param jp2 the jpeg2000 file codec.
874 * @param p_bpc_header_data pointer to actual data (already read from file)
875 * @param p_bpc_header_size pointer that will hold the size of the bpc header
876 * @param p_manager the user event manager.
878 * @return true if the bpc header is valid, false otherwise.
880 opj_bool jp2_read_bpcc_v2( opj_jp2_v2_t *jp2,
881 unsigned char * p_bpc_header_data,
882 unsigned int p_bpc_header_size,
883 opj_event_mgr_t * p_manager
889 assert(p_bpc_header_data != 00);
891 assert(p_manager != 00);
894 /*if (jp2->bpc != 0 ){
895 opj_event_msg_v2(p_manager, EVT_WARNING, "A BPCC header box is available although BPC is different to zero (%d)\n",jp2->bpc);
898 /* and length is relevant */
899 if (p_bpc_header_size != jp2->numcomps) {
900 opj_event_msg_v2(p_manager, EVT_ERROR, "Bad BPCC header box (bad size)\n");
904 /* read info for each component */
905 for (i = 0; i < jp2->numcomps; ++i) {
906 opj_read_bytes(p_bpc_header_data,&jp2->comps[i].bpcc ,1); /* read each BPCC component */
913 static void jp2_write_colr(opj_jp2_t *jp2, opj_cio_t *cio) {
916 box.init_pos = cio_tell(cio);
918 cio_write(cio, JP2_COLR, 4); /* COLR */
920 cio_write(cio, jp2->meth, 1); /* METH */
921 cio_write(cio, jp2->precedence, 1); /* PRECEDENCE */
922 cio_write(cio, jp2->approx, 1); /* APPROX */
927 cio_write(cio, jp2->enumcs, 4); /* EnumCS */
929 box.length = cio_tell(cio) - box.init_pos;
930 cio_seek(cio, box.init_pos);
931 cio_write(cio, box.length, 4); /* L */
932 cio_seek(cio, box.init_pos + box.length);
936 * Writes the Colour Specification box.
938 * @param jp2 jpeg2000 file codec.
939 * @param p_nb_bytes_written pointer to store the nb of bytes written by the function.
941 * @return the data being copied.
943 unsigned char *jp2_write_colr_v2( opj_jp2_v2_t *jp2,
944 unsigned int * p_nb_bytes_written )
946 /* room for 8 bytes for box 3 for common data and variable upon profile*/
947 unsigned int l_colr_size = 11;
948 unsigned char * l_colr_data,* l_current_colr_ptr;
952 assert(p_nb_bytes_written != 00);
965 l_colr_data = (unsigned char *) opj_malloc(l_colr_size);
966 if (l_colr_data == 00) {
969 memset(l_colr_data,0,l_colr_size);
971 l_current_colr_ptr = l_colr_data;
973 opj_write_bytes(l_current_colr_ptr,l_colr_size,4); /* write box size */
974 l_current_colr_ptr += 4;
976 opj_write_bytes(l_current_colr_ptr,JP2_COLR,4); /* BPCC */
977 l_current_colr_ptr += 4;
979 opj_write_bytes(l_current_colr_ptr, jp2->meth,1); /* METH */
980 ++l_current_colr_ptr;
982 opj_write_bytes(l_current_colr_ptr, jp2->precedence,1); /* PRECEDENCE */
983 ++l_current_colr_ptr;
985 opj_write_bytes(l_current_colr_ptr, jp2->approx,1); /* APPROX */
986 ++l_current_colr_ptr;
988 if (jp2->meth == 1) {
989 opj_write_bytes(l_current_colr_ptr, jp2->enumcs,4); /* EnumCS */
992 opj_write_bytes(l_current_colr_ptr, 0, 1); /* PROFILE (??) */
995 *p_nb_bytes_written = l_colr_size;
1000 static void jp2_free_pclr(opj_jp2_color_t *color)
1002 opj_free(color->jp2_pclr->channel_sign);
1003 opj_free(color->jp2_pclr->channel_size);
1004 opj_free(color->jp2_pclr->entries);
1006 if(color->jp2_pclr->cmap) opj_free(color->jp2_pclr->cmap);
1008 opj_free(color->jp2_pclr); color->jp2_pclr = NULL;
1011 static void free_color_data(opj_jp2_color_t *color)
1015 jp2_free_pclr(color);
1019 if(color->jp2_cdef->info) opj_free(color->jp2_cdef->info);
1020 opj_free(color->jp2_cdef);
1022 if(color->icc_profile_buf) opj_free(color->icc_profile_buf);
1026 static void jp2_apply_pclr(opj_image_t *image, opj_jp2_color_t *color)
1028 opj_image_comp_t *old_comps, *new_comps;
1029 OPJ_BYTE *channel_size, *channel_sign;
1030 OPJ_UINT32 *entries;
1031 opj_jp2_cmap_comp_t *cmap;
1032 OPJ_INT32 *src, *dst;
1034 OPJ_UINT16 i, nr_channels, cmp, pcol;
1037 channel_size = color->jp2_pclr->channel_size;
1038 channel_sign = color->jp2_pclr->channel_sign;
1039 entries = color->jp2_pclr->entries;
1040 cmap = color->jp2_pclr->cmap;
1041 nr_channels = color->jp2_pclr->nr_channels;
1043 old_comps = image->comps;
1044 new_comps = (opj_image_comp_t*)
1045 opj_malloc(nr_channels * sizeof(opj_image_comp_t));
1047 for(i = 0; i < nr_channels; ++i) {
1048 pcol = cmap[i].pcol; cmp = cmap[i].cmp;
1050 new_comps[pcol] = old_comps[cmp];
1053 if(cmap[i].mtyp == 0){
1054 old_comps[cmp].data = NULL; continue;
1057 /* Palette mapping: */
1058 new_comps[pcol].data = (int*)
1059 opj_malloc(old_comps[cmp].w * old_comps[cmp].h * sizeof(int));
1060 new_comps[pcol].prec = channel_size[i];
1061 new_comps[pcol].sgnd = channel_sign[i];
1064 top_k = color->jp2_pclr->nr_entries - 1;
1066 for(i = 0; i < nr_channels; ++i) {
1068 if(cmap[i].mtyp == 0) continue;
1070 /* Palette mapping: */
1071 cmp = cmap[i].cmp; pcol = cmap[i].pcol;
1072 src = old_comps[cmp].data;
1073 dst = new_comps[pcol].data;
1074 max = new_comps[pcol].w * new_comps[pcol].h;
1076 for(j = 0; j < max; ++j)
1079 if((k = src[j]) < 0) k = 0; else if(k > top_k) k = top_k;
1082 dst[j] = entries[k * nr_channels + pcol];
1086 max = image->numcomps;
1087 for(i = 0; i < max; ++i) {
1088 if(old_comps[i].data) opj_free(old_comps[i].data);
1091 opj_free(old_comps);
1092 image->comps = new_comps;
1093 image->numcomps = nr_channels;
1095 jp2_free_pclr(color);
1100 static opj_bool jp2_read_pclr(opj_jp2_t *jp2, opj_cio_t *cio,
1101 opj_jp2_box_t *box, opj_jp2_color_t *color)
1103 opj_jp2_pclr_t *jp2_pclr;
1104 unsigned char *channel_size, *channel_sign;
1105 unsigned int *entries;
1106 unsigned short nr_entries, nr_channels;
1107 unsigned short i, j;
1110 OPJ_ARG_NOT_USED(box);
1111 OPJ_ARG_NOT_USED(jp2);
1113 /* Part 1, I.5.3.4: 'There shall be at most one Palette box inside
1114 * a JP2 Header box' :
1116 if(color->jp2_pclr) return OPJ_FALSE;
1118 nr_entries = (unsigned short)cio_read(cio, 2); /* NE */
1119 nr_channels = (unsigned short)cio_read(cio, 1);/* NPC */
1121 entries = (unsigned int*)
1122 opj_malloc(nr_channels * nr_entries * sizeof(unsigned int));
1123 channel_size = (unsigned char*)opj_malloc(nr_channels);
1124 channel_sign = (unsigned char*)opj_malloc(nr_channels);
1126 jp2_pclr = (opj_jp2_pclr_t*)opj_malloc(sizeof(opj_jp2_pclr_t));
1127 jp2_pclr->channel_sign = channel_sign;
1128 jp2_pclr->channel_size = channel_size;
1129 jp2_pclr->entries = entries;
1130 jp2_pclr->nr_entries = nr_entries;
1131 jp2_pclr->nr_channels = nr_channels;
1132 jp2_pclr->cmap = NULL;
1134 color->jp2_pclr = jp2_pclr;
1136 for(i = 0; i < nr_channels; ++i)
1138 uc = cio_read(cio, 1); /* Bi */
1139 channel_size[i] = (uc & 0x7f) + 1;
1140 channel_sign[i] = (uc & 0x80)?1:0;
1143 for(j = 0; j < nr_entries; ++j)
1145 for(i = 0; i < nr_channels; ++i)
1148 *entries++ = cio_read(cio, channel_size[i]>>3);
1153 }/* jp2_read_pclr() */
1156 * Reads a palette box.
1158 * @param jp2 the jpeg2000 file codec.
1159 * @param p_pclr_header_data pointer to actual data (already read from file)
1160 * @param p_pclr_header_size pointer that will hold the size of the PCLR header
1161 * @param p_manager the user event manager.
1163 * @return true if the bpc header is valid, fale else.
1165 opj_bool jp2_read_pclr_v2( opj_jp2_v2_t *jp2,
1166 unsigned char * p_pclr_header_data,
1167 OPJ_UINT32 p_pclr_header_size,
1168 opj_event_mgr_t * p_manager
1170 opj_jp2_pclr_t *jp2_pclr;
1171 OPJ_BYTE *channel_size, *channel_sign;
1172 OPJ_UINT32 *entries;
1173 OPJ_UINT16 nr_entries,nr_channels;
1178 assert(p_pclr_header_data != 00);
1180 assert(p_manager != 00);
1182 if(jp2->color.jp2_pclr)
1185 opj_read_bytes(p_pclr_header_data, &l_value , 2); /* NE */
1186 p_pclr_header_data += 2;
1187 nr_entries = (OPJ_UINT16) l_value;
1189 opj_read_bytes(p_pclr_header_data, &l_value , 1); /* NPC */
1190 ++p_pclr_header_data;
1191 nr_channels = (OPJ_UINT16) l_value;
1193 entries = (OPJ_UINT32*) opj_malloc(nr_channels * nr_entries * sizeof(OPJ_UINT32));
1194 channel_size = (OPJ_BYTE*) opj_malloc(nr_channels);
1195 channel_sign = (OPJ_BYTE*) opj_malloc(nr_channels);
1197 jp2_pclr = (opj_jp2_pclr_t*)opj_malloc(sizeof(opj_jp2_pclr_t));
1198 jp2_pclr->channel_sign = channel_sign;
1199 jp2_pclr->channel_size = channel_size;
1200 jp2_pclr->entries = entries;
1201 jp2_pclr->nr_entries = nr_entries;
1202 jp2_pclr->nr_channels = nr_channels;
1203 jp2_pclr->cmap = NULL;
1205 jp2->color.jp2_pclr = jp2_pclr;
1207 for(i = 0; i < nr_channels; ++i) {
1208 opj_read_bytes(p_pclr_header_data, &l_value , 1); /* Bi */
1209 ++p_pclr_header_data;
1211 channel_size[i] = (l_value & 0x7f) + 1;
1212 channel_sign[i] = (l_value & 0x80)? 1 : 0;
1215 for(j = 0; j < nr_entries; ++j) {
1216 for(i = 0; i < nr_channels; ++i) {
1217 /**entries++ = cio_read(cio, channel_size[i]>>3); */
1218 opj_read_bytes(p_pclr_header_data, &l_value , channel_size[i]>>3); /* Cji */
1219 p_pclr_header_data += channel_size[i]>>3;
1220 *entries = (OPJ_UINT32) l_value;
1229 static opj_bool jp2_read_cmap(opj_jp2_t *jp2, opj_cio_t *cio,
1230 opj_jp2_box_t *box, opj_jp2_color_t *color)
1232 opj_jp2_cmap_comp_t *cmap;
1233 unsigned short i, nr_channels;
1235 OPJ_ARG_NOT_USED(box);
1236 OPJ_ARG_NOT_USED(jp2);
1238 /* Need nr_channels: */
1239 if(color->jp2_pclr == NULL) return OPJ_FALSE;
1241 /* Part 1, I.5.3.5: 'There shall be at most one Component Mapping box
1242 * inside a JP2 Header box' :
1244 if(color->jp2_pclr->cmap) return OPJ_FALSE;
1246 nr_channels = color->jp2_pclr->nr_channels;
1247 cmap = (opj_jp2_cmap_comp_t*)
1248 opj_malloc(nr_channels * sizeof(opj_jp2_cmap_comp_t));
1250 for(i = 0; i < nr_channels; ++i)
1252 cmap[i].cmp = (unsigned short)cio_read(cio, 2);
1253 cmap[i].mtyp = cio_read(cio, 1);
1254 cmap[i].pcol = cio_read(cio, 1);
1257 color->jp2_pclr->cmap = cmap;
1261 }/* jp2_read_cmap() */
1264 * Reads the Component Mapping box.
1266 * @param p_cmap_header_data pointer to actual data (already read from file)
1267 * @param jp2 the jpeg2000 file codec.
1268 * @param p_cmap_header_size pointer that will hold the size of the color header
1269 * @param p_manager the user event manager.
1271 * @return true if the cdef header is valid, false else.
1273 static opj_bool jp2_read_cmap_v2( opj_jp2_v2_t * jp2,
1274 unsigned char * p_cmap_header_data,
1275 OPJ_UINT32 p_cmap_header_size,
1276 opj_event_mgr_t * p_manager
1279 opj_jp2_cmap_comp_t *cmap;
1280 OPJ_BYTE i, nr_channels;
1285 assert(p_cmap_header_data != 00);
1286 assert(p_manager != 00);
1288 /* Need nr_channels: */
1289 if(jp2->color.jp2_pclr == NULL) {
1290 opj_event_msg_v2(p_manager, EVT_ERROR, "Need to read a PCLR box before the CMAP box.\n");
1294 /* Part 1, I.5.3.5: 'There shall be at most one Component Mapping box
1295 * inside a JP2 Header box' :
1297 if(jp2->color.jp2_pclr->cmap) {
1298 opj_event_msg_v2(p_manager, EVT_ERROR, "Only one CMAP box is allowed.\n");
1302 nr_channels = jp2->color.jp2_pclr->nr_channels;
1303 cmap = (opj_jp2_cmap_comp_t*) opj_malloc(nr_channels * sizeof(opj_jp2_cmap_comp_t));
1305 for(i = 0; i < nr_channels; ++i) {
1306 opj_read_bytes(p_cmap_header_data, &l_value, 2); /* CMP^i */
1307 p_cmap_header_data +=2;
1308 cmap[i].cmp = (OPJ_UINT16) l_value;
1310 opj_read_bytes(p_cmap_header_data, &l_value, 1); /* MTYP^i */
1311 ++p_cmap_header_data;
1312 cmap[i].mtyp = (OPJ_BYTE) l_value;
1314 opj_read_bytes(p_cmap_header_data, &l_value, 1); /* PCOL^i */
1315 ++p_cmap_header_data;
1316 cmap[i].pcol = (OPJ_BYTE) l_value;
1319 jp2->color.jp2_pclr->cmap = cmap;
1324 static void jp2_apply_cdef(opj_image_t *image, opj_jp2_color_t *color)
1326 opj_jp2_cdef_info_t *info;
1327 OPJ_INT32 color_space;
1328 OPJ_UINT16 i, n, cn, typ, asoc, acn;
1330 color_space = image->color_space;
1331 info = color->jp2_cdef->info;
1332 n = color->jp2_cdef->n;
1334 for(i = 0; i < n; ++i)
1336 /* WATCH: acn = asoc - 1 ! */
1337 if((asoc = info[i].asoc) == 0) continue;
1339 cn = info[i].cn; typ = info[i].typ; acn = asoc - 1;
1343 opj_image_comp_t saved;
1345 memcpy(&saved, &image->comps[cn], sizeof(opj_image_comp_t));
1346 memcpy(&image->comps[cn], &image->comps[acn], sizeof(opj_image_comp_t));
1347 memcpy(&image->comps[acn], &saved, sizeof(opj_image_comp_t));
1349 info[i].asoc = cn + 1;
1350 info[acn].asoc = info[acn].cn + 1;
1354 if(color->jp2_cdef->info) opj_free(color->jp2_cdef->info);
1356 opj_free(color->jp2_cdef); color->jp2_cdef = NULL;
1358 }/* jp2_apply_cdef() */
1360 static opj_bool jp2_read_cdef(opj_jp2_t *jp2, opj_cio_t *cio,
1361 opj_jp2_box_t *box, opj_jp2_color_t *color)
1363 opj_jp2_cdef_info_t *info;
1364 unsigned short i, n;
1366 OPJ_ARG_NOT_USED(box);
1367 OPJ_ARG_NOT_USED(jp2);
1369 /* Part 1, I.5.3.6: 'The shall be at most one Channel Definition box
1370 * inside a JP2 Header box.'
1372 if(color->jp2_cdef) return OPJ_FALSE;
1374 if((n = (unsigned short)cio_read(cio, 2)) == 0) return OPJ_FALSE; /* szukw000: FIXME */
1376 info = (opj_jp2_cdef_info_t*)
1377 opj_malloc(n * sizeof(opj_jp2_cdef_info_t));
1379 color->jp2_cdef = (opj_jp2_cdef_t*)opj_malloc(sizeof(opj_jp2_cdef_t));
1380 color->jp2_cdef->info = info;
1381 color->jp2_cdef->n = n;
1383 for(i = 0; i < n; ++i)
1385 info[i].cn = (unsigned short)cio_read(cio, 2);
1386 info[i].typ = (unsigned short)cio_read(cio, 2);
1387 info[i].asoc = (unsigned short)cio_read(cio, 2);
1391 }/* jp2_read_cdef() */
1394 * Reads the Component Definition box.
1396 * @param p_cdef_header_data pointer to actual data (already read from file)
1397 * @param jp2 the jpeg2000 file codec.
1398 * @param p_cdef_header_size pointer that will hold the size of the color header
1399 * @param p_manager the user event manager.
1401 * @return true if the cdef header is valid, false else.
1403 static opj_bool jp2_read_cdef_v2( opj_jp2_v2_t * jp2,
1404 unsigned char * p_cdef_header_data,
1405 OPJ_UINT32 p_cdef_header_size,
1406 opj_event_mgr_t * p_manager
1409 opj_jp2_cdef_info_t *cdef_info;
1415 assert(p_cdef_header_data != 00);
1416 assert(p_manager != 00);
1418 /* Part 1, I.5.3.6: 'The shall be at most one Channel Definition box
1419 * inside a JP2 Header box.'*/
1420 if(jp2->color.jp2_cdef) return OPJ_FALSE;
1422 opj_read_bytes(p_cdef_header_data,&l_value ,2); /* N */
1423 p_cdef_header_data+= 2;
1425 if ( (OPJ_UINT16)l_value == 0){ /* szukw000: FIXME */
1426 opj_event_msg_v2(p_manager, EVT_ERROR, "Number of channel description is equal to zero in CDEF box.\n");
1430 cdef_info = (opj_jp2_cdef_info_t*) opj_malloc(l_value * sizeof(opj_jp2_cdef_info_t));
1432 jp2->color.jp2_cdef = (opj_jp2_cdef_t*)opj_malloc(sizeof(opj_jp2_cdef_t));
1433 jp2->color.jp2_cdef->info = cdef_info;
1434 jp2->color.jp2_cdef->n = (OPJ_UINT16) l_value;
1436 for(i = 0; i < jp2->color.jp2_cdef->n; ++i) {
1437 opj_read_bytes(p_cdef_header_data, &l_value, 2); /* Cn^i */
1438 p_cdef_header_data +=2;
1439 cdef_info[i].cn = (OPJ_UINT16) l_value;
1441 opj_read_bytes(p_cdef_header_data, &l_value, 2); /* Typ^i */
1442 p_cdef_header_data +=2;
1443 cdef_info[i].typ = (OPJ_UINT16) l_value;
1445 opj_read_bytes(p_cdef_header_data, &l_value, 2); /* Asoc^i */
1446 p_cdef_header_data +=2;
1447 cdef_info[i].asoc = (OPJ_UINT16) l_value;
1454 static opj_bool jp2_read_colr(opj_jp2_t *jp2, opj_cio_t *cio,
1455 opj_jp2_box_t *box, opj_jp2_color_t *color)
1458 opj_common_ptr cinfo;
1460 /* Part 1, I.5.3.3 : 'A conforming JP2 reader shall ignore all Colour
1461 * Specification boxes after the first.'
1463 if(color->jp2_has_colr) return OPJ_FALSE;
1467 jp2->meth = cio_read(cio, 1); /* METH */
1468 jp2->precedence = cio_read(cio, 1); /* PRECEDENCE */
1469 jp2->approx = cio_read(cio, 1); /* APPROX */
1473 jp2->enumcs = cio_read(cio, 4); /* EnumCS */
1479 skip_len = box->init_pos + box->length - cio_tell(cio);
1482 opj_event_msg(cinfo, EVT_ERROR, "Error with COLR box size\n");
1487 unsigned char *start;
1489 start = cio_getbp(cio);
1490 color->icc_profile_buf = (unsigned char*)opj_malloc(skip_len);
1491 color->icc_profile_len = skip_len;
1493 cio_skip(cio, box->init_pos + box->length - cio_tell(cio));
1495 memcpy(color->icc_profile_buf, start, skip_len);
1499 if (cio_tell(cio) - box->init_pos != box->length)
1501 opj_event_msg(cinfo, EVT_ERROR, "Error with COLR Box\n");
1504 color->jp2_has_colr = 1;
1507 }/* jp2_read_colr() */
1510 * Reads the Colour Specification box.
1512 * @param p_colr_header_data pointer to actual data (already read from file)
1513 * @param jp2 the jpeg2000 file codec.
1514 * @param p_colr_header_size pointer that will hold the size of the color header
1515 * @param p_colr_header_max_size maximum size of the header, any size bigger than this value should result the function to output false.
1516 * @param p_manager the user event manager.
1518 * @return true if the bpc header is valid, fale else.
1520 static opj_bool jp2_read_colr_v2( opj_jp2_v2_t * jp2,
1521 unsigned char * p_colr_header_data,
1522 OPJ_UINT32 p_colr_header_size,
1523 opj_event_mgr_t * p_manager
1530 assert(p_colr_header_data != 00);
1531 assert(p_manager != 00);
1533 if (p_colr_header_size < 3) {
1534 opj_event_msg_v2(p_manager, EVT_ERROR, "Bad COLR header box (bad size)\n");
1538 /* Part 1, I.5.3.3 : 'A conforming JP2 reader shall ignore all Colour
1539 * Specification boxes after the first.'
1541 if(jp2->color.jp2_has_colr) {
1542 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");
1543 p_colr_header_data += p_colr_header_size;
1547 opj_read_bytes(p_colr_header_data,&jp2->meth ,1); /* METH */
1548 ++p_colr_header_data;
1550 opj_read_bytes(p_colr_header_data,&jp2->precedence ,1); /* PRECEDENCE */
1551 ++p_colr_header_data;
1553 opj_read_bytes(p_colr_header_data,&jp2->approx ,1); /* APPROX */
1554 ++p_colr_header_data;
1556 if (jp2->meth == 1) {
1557 if (p_colr_header_size != 7) {
1558 opj_event_msg_v2(p_manager, EVT_ERROR, "Bad BPCC header box (bad size)\n");
1562 opj_read_bytes(p_colr_header_data,&jp2->enumcs ,4); /* EnumCS */
1564 else if (jp2->meth == 2) {
1566 int it_icc_value = 0;
1567 int icc_len = p_colr_header_size - 3;
1569 jp2->color.icc_profile_len = icc_len;
1570 jp2->color.icc_profile_buf = (unsigned char*) opj_malloc(icc_len);
1572 memset(jp2->color.icc_profile_buf, 0, icc_len * sizeof(unsigned char));
1574 for (it_icc_value = 0; it_icc_value < icc_len; ++it_icc_value)
1576 opj_read_bytes(p_colr_header_data,&l_value,1); /* icc values */
1577 ++p_colr_header_data;
1578 jp2->color.icc_profile_buf[it_icc_value] = (OPJ_BYTE) l_value;
1583 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);
1585 jp2->color.jp2_has_colr = 1;
1590 opj_bool jp2_read_jp2h(opj_jp2_t *jp2, opj_cio_t *cio, opj_jp2_color_t *color)
1595 opj_common_ptr cinfo = jp2->cinfo;
1597 jp2_read_boxhdr(cinfo, cio, &box);
1600 if (JP2_JP2H != box.type)
1602 if (box.type == JP2_JP2C)
1604 opj_event_msg(cinfo, EVT_ERROR, "Expected JP2H Marker\n");
1607 cio_skip(cio, box.length - 8);
1609 if(cio->bp >= cio->end) return OPJ_FALSE;
1611 jp2_read_boxhdr(cinfo, cio, &box);
1613 } while(JP2_JP2H != box.type);
1615 if (!jp2_read_ihdr(jp2, cio))
1617 jp2h_end = box.init_pos + box.length;
1619 if (jp2->bpc == 255)
1621 if (!jp2_read_bpcc(jp2, cio))
1624 jp2_read_boxhdr(cinfo, cio, &box);
1626 while(cio_tell(cio) < jp2h_end)
1628 if(box.type == JP2_COLR)
1630 if( !jp2_read_colr(jp2, cio, &box, color))
1632 cio_seek(cio, box.init_pos + 8);
1633 cio_skip(cio, box.length - 8);
1635 jp2_read_boxhdr(cinfo, cio, &box);
1638 if(box.type == JP2_CDEF && !jp2->ignore_pclr_cmap_cdef)
1640 if( !jp2_read_cdef(jp2, cio, &box, color))
1642 cio_seek(cio, box.init_pos + 8);
1643 cio_skip(cio, box.length - 8);
1645 jp2_read_boxhdr(cinfo, cio, &box);
1648 if(box.type == JP2_PCLR && !jp2->ignore_pclr_cmap_cdef)
1650 if( !jp2_read_pclr(jp2, cio, &box, color))
1652 cio_seek(cio, box.init_pos + 8);
1653 cio_skip(cio, box.length - 8);
1655 jp2_read_boxhdr(cinfo, cio, &box);
1658 if(box.type == JP2_CMAP && !jp2->ignore_pclr_cmap_cdef)
1660 if( !jp2_read_cmap(jp2, cio, &box, color))
1662 cio_seek(cio, box.init_pos + 8);
1663 cio_skip(cio, box.length - 8);
1665 jp2_read_boxhdr(cinfo, cio, &box);
1668 cio_seek(cio, box.init_pos + 8);
1669 cio_skip(cio, box.length - 8);
1670 jp2_read_boxhdr(cinfo, cio, &box);
1672 }/* while(cio_tell(cio) < box_end) */
1674 cio_seek(cio, jp2h_end);
1676 /* Part 1, I.5.3.3 : 'must contain at least one' */
1677 return (color->jp2_has_colr == 1);
1679 }/* jp2_read_jp2h() */
1681 opj_image_t* opj_jp2_decode(opj_jp2_t *jp2, opj_cio_t *cio,
1682 opj_codestream_info_t *cstr_info)
1684 opj_common_ptr cinfo;
1685 opj_image_t *image = NULL;
1686 opj_jp2_color_t color;
1692 memset(&color, 0, sizeof(opj_jp2_color_t));
1696 if(!jp2_read_struct(jp2, cio, &color))
1698 free_color_data(&color);
1699 opj_event_msg(cinfo, EVT_ERROR, "Failed to decode jp2 structure\n");
1704 image = j2k_decode(jp2->j2k, cio, cstr_info);
1708 free_color_data(&color);
1709 opj_event_msg(cinfo, EVT_ERROR, "Failed to decode J2K image\n");
1713 if (!jp2->ignore_pclr_cmap_cdef){
1715 /* Set Image Color Space */
1716 if (jp2->enumcs == 16)
1717 image->color_space = CLRSPC_SRGB;
1718 else if (jp2->enumcs == 17)
1719 image->color_space = CLRSPC_GRAY;
1720 else if (jp2->enumcs == 18)
1721 image->color_space = CLRSPC_SYCC;
1723 image->color_space = CLRSPC_UNKNOWN;
1727 jp2_apply_cdef(image, &color);
1731 /* Part 1, I.5.3.4: Either both or none : */
1732 if( !color.jp2_pclr->cmap)
1733 jp2_free_pclr(&color);
1735 jp2_apply_pclr(image, &color);
1737 if(color.icc_profile_buf)
1739 image->icc_profile_buf = color.icc_profile_buf;
1740 color.icc_profile_buf = NULL;
1741 image->icc_profile_len = color.icc_profile_len;
1747 }/* opj_jp2_decode() */
1749 opj_bool jp2_decode_v2( opj_jp2_v2_t *jp2,
1750 struct opj_stream_private *cio,
1751 opj_image_t* p_image,
1752 struct opj_event_mgr * p_manager)
1758 if( ! j2k_decode_v2(jp2->j2k, cio, p_image, p_manager) ) {
1759 opj_event_msg_v2(p_manager, EVT_ERROR, "Failed to decode the codestream in the JP2 file\n");
1763 /* Set Image Color Space */
1764 if (jp2->enumcs == 16)
1765 p_image->color_space = CLRSPC_SRGB;
1766 else if (jp2->enumcs == 17)
1767 p_image->color_space = CLRSPC_GRAY;
1768 else if (jp2->enumcs == 18)
1769 p_image->color_space = CLRSPC_SYCC;
1771 p_image->color_space = CLRSPC_UNKNOWN;
1773 /* Apply the color space if needed */
1774 if(jp2->color.jp2_cdef) {
1775 jp2_apply_cdef(p_image, &(jp2->color));
1778 if(jp2->color.jp2_pclr) {
1779 /* Part 1, I.5.3.4: Either both or none : */
1780 if( !jp2->color.jp2_pclr->cmap)
1781 jp2_free_pclr(&(jp2->color));
1783 jp2_apply_pclr(p_image, &(jp2->color));
1786 if(jp2->color.icc_profile_buf) {
1787 p_image->icc_profile_buf = jp2->color.icc_profile_buf;
1788 p_image->icc_profile_len = jp2->color.icc_profile_len;
1789 jp2->color.icc_profile_buf = NULL;
1796 void jp2_write_jp2h(opj_jp2_t *jp2, opj_cio_t *cio) {
1799 box.init_pos = cio_tell(cio);
1801 cio_write(cio, JP2_JP2H, 4); /* JP2H */
1803 jp2_write_ihdr(jp2, cio);
1805 if (jp2->bpc == 255) {
1806 jp2_write_bpcc(jp2, cio);
1808 jp2_write_colr(jp2, cio);
1810 box.length = cio_tell(cio) - box.init_pos;
1811 cio_seek(cio, box.init_pos);
1812 cio_write(cio, box.length, 4); /* L */
1813 cio_seek(cio, box.init_pos + box.length);
1817 * Writes the Jpeg2000 file Header box - JP2 Header box (warning, this is a super box).
1819 * @param cio the stream to write data to.
1820 * @param jp2 the jpeg2000 file codec.
1821 * @param p_manager user event manager.
1823 * @return true if writting was successful.
1825 opj_bool jp2_write_jp2h_v2( opj_jp2_v2_t *jp2,
1826 opj_stream_private_t *cio,
1827 opj_event_mgr_t * p_manager )
1829 opj_jp2_img_header_writer_handler_t l_writers [3];
1830 opj_jp2_img_header_writer_handler_t * l_current_writer;
1833 /* size of data for super box*/
1834 int l_jp2h_size = 8;
1835 opj_bool l_result = OPJ_TRUE;
1837 /* to store the data of the super box */
1838 unsigned char l_jp2h_data [8];
1843 assert(p_manager != 00);
1845 memset(l_writers,0,sizeof(l_writers));
1847 if (jp2->bpc == 255) {
1849 l_writers[0].handler = jp2_write_ihdr_v2;
1850 l_writers[1].handler = jp2_write_bpcc_v2;
1851 l_writers[2].handler = jp2_write_colr_v2;
1855 l_writers[0].handler = jp2_write_ihdr_v2;
1856 l_writers[1].handler = jp2_write_colr_v2;
1859 /* write box header */
1860 /* write JP2H type */
1861 opj_write_bytes(l_jp2h_data+4,JP2_JP2H,4);
1863 l_current_writer = l_writers;
1864 for (i=0;i<l_nb_pass;++i) {
1865 l_current_writer->m_data = l_current_writer->handler(jp2,&(l_current_writer->m_size));
1866 if (l_current_writer->m_data == 00) {
1867 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to hold JP2 Header data\n");
1868 l_result = OPJ_FALSE;
1872 l_jp2h_size += l_current_writer->m_size;
1877 l_current_writer = l_writers;
1878 for (i=0;i<l_nb_pass;++i) {
1879 if (l_current_writer->m_data != 00) {
1880 opj_free(l_current_writer->m_data );
1888 /* write super box size */
1889 opj_write_bytes(l_jp2h_data,l_jp2h_size,4);
1891 /* write super box data on stream */
1892 if (opj_stream_write_data(cio,l_jp2h_data,8,p_manager) != 8) {
1893 opj_event_msg(p_manager, EVT_ERROR, "Stream error while writting JP2 Header box\n");
1894 l_result = OPJ_FALSE;
1898 l_current_writer = l_writers;
1899 for (i=0;i<l_nb_pass;++i) {
1900 if (opj_stream_write_data(cio,l_current_writer->m_data,l_current_writer->m_size,p_manager) != l_current_writer->m_size) {
1901 opj_event_msg(p_manager, EVT_ERROR, "Stream error while writting JP2 Header box\n");
1902 l_result = OPJ_FALSE;
1909 l_current_writer = l_writers;
1912 for (i=0;i<l_nb_pass;++i) {
1913 if (l_current_writer->m_data != 00) {
1914 opj_free(l_current_writer->m_data );
1922 static void jp2_write_ftyp(opj_jp2_t *jp2, opj_cio_t *cio) {
1926 box.init_pos = cio_tell(cio);
1928 cio_write(cio, JP2_FTYP, 4); /* FTYP */
1930 cio_write(cio, jp2->brand, 4); /* BR */
1931 cio_write(cio, jp2->minversion, 4); /* MinV */
1933 for (i = 0; i < jp2->numcl; i++) {
1934 cio_write(cio, jp2->cl[i], 4); /* CL */
1937 box.length = cio_tell(cio) - box.init_pos;
1938 cio_seek(cio, box.init_pos);
1939 cio_write(cio, box.length, 4); /* L */
1940 cio_seek(cio, box.init_pos + box.length);
1944 * Writes a FTYP box - File type box
1946 * @param cio the stream to write data to.
1947 * @param jp2 the jpeg2000 file codec.
1948 * @param p_manager the user event manager.
1950 * @return true if writting was successful.
1952 opj_bool jp2_write_ftyp_v2( opj_jp2_v2_t *jp2,
1953 opj_stream_private_t *cio,
1954 opj_event_mgr_t * p_manager )
1957 unsigned int l_ftyp_size = 16 + 4 * jp2->numcl;
1958 unsigned char * l_ftyp_data, * l_current_data_ptr;
1964 assert(p_manager != 00);
1966 l_ftyp_data = (unsigned char *) opj_malloc(l_ftyp_size);
1968 if (l_ftyp_data == 00) {
1969 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to handle ftyp data\n");
1973 memset(l_ftyp_data,0,l_ftyp_size);
1975 l_current_data_ptr = l_ftyp_data;
1977 opj_write_bytes(l_current_data_ptr, l_ftyp_size,4); /* box size */
1978 l_current_data_ptr += 4;
1980 opj_write_bytes(l_current_data_ptr, JP2_FTYP,4); /* FTYP */
1981 l_current_data_ptr += 4;
1983 opj_write_bytes(l_current_data_ptr, jp2->brand,4); /* BR */
1984 l_current_data_ptr += 4;
1986 opj_write_bytes(l_current_data_ptr, jp2->minversion,4); /* MinV */
1987 l_current_data_ptr += 4;
1989 for (i = 0; i < jp2->numcl; i++) {
1990 opj_write_bytes(l_current_data_ptr, jp2->cl[i],4); /* CL */
1993 l_result = (opj_stream_write_data(cio,l_ftyp_data,l_ftyp_size,p_manager) == l_ftyp_size);
1996 opj_event_msg(p_manager, EVT_ERROR, "Error while writting ftyp data to stream\n");
1999 opj_free(l_ftyp_data);
2004 static opj_bool jp2_read_ftyp(opj_jp2_t *jp2, opj_cio_t *cio) {
2008 opj_common_ptr cinfo = jp2->cinfo;
2010 jp2_read_boxhdr(cinfo, cio, &box);
2012 if (JP2_FTYP != box.type) {
2013 opj_event_msg(cinfo, EVT_ERROR, "Expected FTYP Marker\n");
2017 jp2->brand = cio_read(cio, 4); /* BR */
2018 jp2->minversion = cio_read(cio, 4); /* MinV */
2019 jp2->numcl = (box.length - 16) / 4;
2020 jp2->cl = (unsigned int *) opj_malloc(jp2->numcl * sizeof(unsigned int));
2022 for (i = 0; i < (int)jp2->numcl; i++) {
2023 jp2->cl[i] = cio_read(cio, 4); /* CLi */
2026 if (cio_tell(cio) - box.init_pos != box.length) {
2027 opj_event_msg(cinfo, EVT_ERROR, "Error with FTYP Box\n");
2034 static int jp2_write_jp2c(opj_jp2_t *jp2, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info) {
2035 unsigned int j2k_codestream_offset, j2k_codestream_length;
2038 opj_j2k_t *j2k = jp2->j2k;
2040 box.init_pos = cio_tell(cio);
2042 cio_write(cio, JP2_JP2C, 4); /* JP2C */
2045 j2k_codestream_offset = cio_tell(cio);
2046 if(!j2k_encode(j2k, cio, image, cstr_info)) {
2047 opj_event_msg(j2k->cinfo, EVT_ERROR, "Failed to encode image\n");
2050 j2k_codestream_length = cio_tell(cio) - j2k_codestream_offset;
2052 jp2->j2k_codestream_offset = j2k_codestream_offset;
2053 jp2->j2k_codestream_length = j2k_codestream_length;
2055 box.length = 8 + jp2->j2k_codestream_length;
2056 cio_seek(cio, box.init_pos);
2057 cio_write(cio, box.length, 4); /* L */
2058 cio_seek(cio, box.init_pos + box.length);
2063 static opj_bool jp2_read_jp2c(opj_jp2_t *jp2, opj_cio_t *cio, unsigned int *j2k_codestream_length, unsigned int *j2k_codestream_offset) {
2066 opj_common_ptr cinfo = jp2->cinfo;
2068 jp2_read_boxhdr(cinfo, cio, &box);
2070 if(JP2_JP2C != box.type) {
2071 cio_skip(cio, box.length - 8);
2072 jp2_read_boxhdr(cinfo, cio, &box);
2074 } while(JP2_JP2C != box.type);
2076 *j2k_codestream_offset = cio_tell(cio);
2077 *j2k_codestream_length = box.length - 8;
2082 static void jp2_write_jp(opj_cio_t *cio) {
2085 box.init_pos = cio_tell(cio);
2087 cio_write(cio, JP2_JP, 4); /* JP2 signature */
2088 cio_write(cio, 0x0d0a870a, 4);
2090 box.length = cio_tell(cio) - box.init_pos;
2091 cio_seek(cio, box.init_pos);
2092 cio_write(cio, box.length, 4); /* L */
2093 cio_seek(cio, box.init_pos + box.length);
2097 * Writes a jpeg2000 file signature box.
2099 * @param cio the stream to write data to.
2100 * @param jp2 the jpeg2000 file codec.
2101 * @param p_manager the user event manager.
2103 * @return true if writting was successful.
2105 opj_bool jp2_write_jp_v2( opj_jp2_v2_t *jp2,
2106 opj_stream_private_t *cio,
2107 opj_event_mgr_t * p_manager )
2109 /* 12 bytes will be read */
2110 unsigned char l_signature_data [12];
2115 assert(p_manager != 00);
2117 /* write box length */
2118 opj_write_bytes(l_signature_data,12,4);
2119 /* writes box type */
2120 opj_write_bytes(l_signature_data+4,JP2_JP,4);
2121 /* writes magic number*/
2122 opj_write_bytes(l_signature_data+8,0x0d0a870a,4);
2124 if (opj_stream_write_data(cio,l_signature_data,12,p_manager) != 12) {
2131 static opj_bool jp2_read_jp(opj_jp2_t *jp2, opj_cio_t *cio) {
2134 opj_common_ptr cinfo = jp2->cinfo;
2136 jp2_read_boxhdr(cinfo, cio, &box);
2137 if (JP2_JP != box.type) {
2138 opj_event_msg(cinfo, EVT_ERROR, "Expected JP Marker\n");
2141 if (0x0d0a870a != cio_read(cio, 4)) {
2142 opj_event_msg(cinfo, EVT_ERROR, "Error with JP Marker\n");
2145 if (cio_tell(cio) - box.init_pos != box.length) {
2146 opj_event_msg(cinfo, EVT_ERROR, "Error with JP Box size\n");
2154 static opj_bool jp2_read_struct(opj_jp2_t *jp2, opj_cio_t *cio,
2155 opj_jp2_color_t *color) {
2156 if (!jp2_read_jp(jp2, cio))
2158 if (!jp2_read_ftyp(jp2, cio))
2160 if (!jp2_read_jp2h(jp2, cio, color))
2162 if (!jp2_read_jp2c(jp2, cio, &jp2->j2k_codestream_length, &jp2->j2k_codestream_offset))
2169 static int write_fidx( int offset_jp2c, int length_jp2c, int offset_idx, int length_idx, opj_cio_t *cio)
2173 lenp = cio_tell( cio);
2174 cio_skip( cio, 4); /* L [at the end] */
2175 cio_write( cio, JPIP_FIDX, 4); /* IPTR */
2177 write_prxy( offset_jp2c, length_jp2c, offset_idx, length_idx, cio);
2179 len = cio_tell( cio)-lenp;
2180 cio_seek( cio, lenp);
2181 cio_write( cio, len, 4); /* L */
2182 cio_seek( cio, lenp+len);
2187 static void write_prxy( int offset_jp2c, int length_jp2c, int offset_idx, int length_idx, opj_cio_t *cio)
2191 lenp = cio_tell( cio);
2192 cio_skip( cio, 4); /* L [at the end] */
2193 cio_write( cio, JPIP_PRXY, 4); /* IPTR */
2195 cio_write( cio, offset_jp2c, 8); /* OOFF */
2196 cio_write( cio, length_jp2c, 4); /* OBH part 1 */
2197 cio_write( cio, JP2_JP2C, 4); /* OBH part 2 */
2199 cio_write( cio, 1,1); /* NI */
2201 cio_write( cio, offset_idx, 8); /* IOFF */
2202 cio_write( cio, length_idx, 4); /* IBH part 1 */
2203 cio_write( cio, JPIP_CIDX, 4); /* IBH part 2 */
2205 len = cio_tell( cio)-lenp;
2206 cio_seek( cio, lenp);
2207 cio_write( cio, len, 4); /* L */
2208 cio_seek( cio, lenp+len);
2211 static void write_iptr( int offset, int length, opj_cio_t *cio)
2215 lenp = cio_tell( cio);
2216 cio_skip( cio, 4); /* L [at the end] */
2217 cio_write( cio, JPIP_IPTR, 4); /* IPTR */
2219 cio_write( cio, offset, 8);
2220 cio_write( cio, length, 8);
2222 len = cio_tell( cio)-lenp;
2223 cio_seek( cio, lenp);
2224 cio_write( cio, len, 4); /* L */
2225 cio_seek( cio, lenp+len);
2229 /* ----------------------------------------------------------------------- */
2230 /* JP2 decoder interface */
2231 /* ----------------------------------------------------------------------- */
2233 opj_jp2_t* jp2_create_decompress(opj_common_ptr cinfo) {
2234 opj_jp2_t *jp2 = (opj_jp2_t*) opj_calloc(1, sizeof(opj_jp2_t));
2237 /* create the J2K codec */
2238 jp2->j2k = j2k_create_decompress(cinfo);
2239 if(jp2->j2k == NULL) {
2240 jp2_destroy_decompress(jp2);
2247 void jp2_destroy_decompress(opj_jp2_t *jp2) {
2249 /* destroy the J2K codec */
2250 j2k_destroy_decompress(jp2->j2k);
2253 opj_free(jp2->comps);
2262 void jp2_setup_decoder(opj_jp2_t *jp2, opj_dparameters_t *parameters) {
2263 /* setup the J2K codec */
2264 j2k_setup_decoder(jp2->j2k, parameters);
2265 /* further JP2 initializations go here */
2266 jp2->ignore_pclr_cmap_cdef = parameters->flags & OPJ_DPARAMETERS_IGNORE_PCLR_CMAP_CDEF_FLAG;
2269 void jp2_setup_decoder_v2(opj_jp2_v2_t *jp2, opj_dparameters_t *parameters)
2271 /* setup the J2K codec */
2272 j2k_setup_decoder_v2(jp2->j2k, parameters);
2274 /* further JP2 initializations go here */
2275 jp2->color.jp2_has_colr = 0;
2279 /* ----------------------------------------------------------------------- */
2280 /* JP2 encoder interface */
2281 /* ----------------------------------------------------------------------- */
2283 opj_jp2_t* jp2_create_compress(opj_common_ptr cinfo) {
2284 opj_jp2_t *jp2 = (opj_jp2_t*)opj_malloc(sizeof(opj_jp2_t));
2287 /* create the J2K codec */
2288 jp2->j2k = j2k_create_compress(cinfo);
2289 if(jp2->j2k == NULL) {
2290 jp2_destroy_compress(jp2);
2297 void jp2_destroy_compress(opj_jp2_t *jp2) {
2299 /* destroy the J2K codec */
2300 j2k_destroy_compress(jp2->j2k);
2303 opj_free(jp2->comps);
2312 void jp2_setup_encoder( opj_jp2_v2_t *jp2,
2313 opj_cparameters_t *parameters,
2315 opj_event_mgr_t * p_manager) {
2319 if(!jp2 || !parameters || !image)
2322 /* setup the J2K codec */
2323 /* ------------------- */
2325 /* Check if number of components respects standard */
2326 if (image->numcomps < 1 || image->numcomps > 16384) {
2327 opj_event_msg_v2(p_manager, EVT_ERROR, "Invalid number of components specified while setting up JP2 encoder\n");
2331 j2k_setup_encoder_v2(jp2->j2k, parameters, image, p_manager );
2333 /* setup the JP2 codec */
2334 /* ------------------- */
2338 jp2->brand = JP2_JP2; /* BR */
2339 jp2->minversion = 0; /* MinV */
2341 jp2->cl = (unsigned int*) opj_malloc(jp2->numcl * sizeof(unsigned int));
2342 jp2->cl[0] = JP2_JP2; /* CL0 : JP2 */
2344 /* Image Header box */
2346 jp2->numcomps = image->numcomps; /* NC */
2347 jp2->comps = (opj_jp2_comps_t*) opj_malloc(jp2->numcomps * sizeof(opj_jp2_comps_t));
2348 jp2->h = image->y1 - image->y0; /* HEIGHT */
2349 jp2->w = image->x1 - image->x0; /* WIDTH */
2351 depth_0 = image->comps[0].prec - 1;
2352 sign = image->comps[0].sgnd;
2353 jp2->bpc = depth_0 + (sign << 7);
2354 for (i = 1; i < image->numcomps; i++) {
2355 int depth = image->comps[i].prec - 1;
2356 sign = image->comps[i].sgnd;
2357 if (depth_0 != depth)
2360 jp2->C = 7; /* C : Always 7 */
2361 jp2->UnkC = 0; /* UnkC, colorspace specified in colr box */
2362 jp2->IPR = 0; /* IPR, no intellectual property */
2364 /* BitsPerComponent box */
2365 for (i = 0; i < image->numcomps; i++) {
2366 jp2->comps[i].bpcc = image->comps[i].prec - 1 + (image->comps[i].sgnd << 7);
2369 /* Colour Specification box */
2370 if ((image->numcomps == 1 || image->numcomps == 3) && (jp2->bpc != 255)) {
2371 jp2->meth = 1; /* METH: Enumerated colourspace */
2373 jp2->meth = 2; /* METH: Restricted ICC profile */
2375 if (jp2->meth == 1) {
2376 if (image->color_space == 1)
2377 jp2->enumcs = 16; /* sRGB as defined by IEC 61966�2�1 */
2378 else if (image->color_space == 2)
2379 jp2->enumcs = 17; /* greyscale */
2380 else if (image->color_space == 3)
2381 jp2->enumcs = 18; /* YUV */
2383 jp2->enumcs = 0; /* PROFILE (??) */
2385 jp2->precedence = 0; /* PRECEDENCE */
2386 jp2->approx = 0; /* APPROX */
2388 // jp2->jpip_on = parameters->jpip_on;
2392 opj_bool opj_jp2_encode_v2( opj_jp2_v2_t *jp2,
2393 opj_stream_private_t *stream,
2394 opj_event_mgr_t * p_manager)
2396 return j2k_encode_v2(jp2->j2k, stream, p_manager);
2399 opj_bool opj_jp2_encode(opj_jp2_t *jp2, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info) {
2401 int pos_iptr, pos_cidx, pos_jp2c, len_jp2c, len_cidx, end_pos, pos_fidx, len_fidx;
2402 pos_jp2c = pos_iptr = -1; /* remove a warning */
2406 /* JPEG 2000 Signature box */
2409 jp2_write_ftyp(jp2, cio);
2410 /* JP2 Header box */
2411 jp2_write_jp2h(jp2, cio);
2414 pos_iptr = cio_tell( cio);
2415 cio_skip( cio, 24); /* IPTR further ! */
2417 pos_jp2c = cio_tell( cio);
2421 if(!(len_jp2c = jp2_write_jp2c( jp2, cio, image, cstr_info))){
2422 opj_event_msg(jp2->cinfo, EVT_ERROR, "Failed to encode image\n");
2427 pos_cidx = cio_tell( cio);
2429 len_cidx = write_cidx( pos_jp2c+8, cio, image, *cstr_info, len_jp2c-8);
2431 pos_fidx = cio_tell( cio);
2432 len_fidx = write_fidx( pos_jp2c, len_jp2c, pos_cidx, len_cidx, cio);
2434 end_pos = cio_tell( cio);
2436 cio_seek( cio, pos_iptr);
2437 write_iptr( pos_fidx, len_fidx, cio);
2438 cio_seek( cio, end_pos);
2445 * Ends the decompression procedures and possibiliy add data to be read after the
2448 opj_bool jp2_end_decompress(opj_jp2_v2_t *jp2, opj_stream_private_t *cio, opj_event_mgr_t * p_manager)
2453 assert(p_manager != 00);
2455 /* customization of the end encoding */
2456 jp2_setup_end_header_reading(jp2);
2459 if (! jp2_exec (jp2,jp2->m_procedure_list,cio,p_manager)) {
2463 return j2k_end_decompress(jp2->j2k, cio, p_manager);
2467 * Ends the compression procedures and possibility add data to be read after the
2470 opj_bool jp2_end_compress( opj_jp2_v2_t *jp2,
2471 opj_stream_private_t *cio,
2472 opj_event_mgr_t * p_manager)
2477 assert(p_manager != 00);
2479 /* customization of the end encoding */
2480 jp2_setup_end_header_writting(jp2);
2482 if (! j2k_end_compress(jp2->j2k,cio,p_manager)) {
2487 return jp2_exec(jp2,jp2->m_procedure_list,cio,p_manager);
2492 * Sets up the procedures to do on writing header after the codestream.
2493 * Developers wanting to extend the library can add their own writing procedures.
2495 void jp2_setup_end_header_writting (opj_jp2_v2_t *jp2)
2500 opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)jp2_write_jp2c );
2501 /* DEVELOPER CORNER, add your custom procedures */
2505 * Sets up the procedures to do on reading header after the codestream.
2506 * Developers wanting to extend the library can add their own writing procedures.
2508 void jp2_setup_end_header_reading (opj_jp2_v2_t *jp2)
2512 opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)jp2_read_header_procedure );
2513 /* DEVELOPER CORNER, add your custom procedures */
2517 * The default validation procedure without any extension.
2519 * @param jp2 the jpeg2000 codec to validate.
2520 * @param cio the input stream to validate.
2521 * @param p_manager the user event manager.
2523 * @return true if the parameters are correct.
2525 opj_bool jp2_default_validation ( opj_jp2_v2_t * jp2,
2526 opj_stream_private_t *cio,
2527 opj_event_mgr_t * p_manager )
2529 opj_bool l_is_valid = OPJ_TRUE;
2535 assert(p_manager != 00);
2537 /* JPEG2000 codec validation */
2540 /* STATE checking */
2541 /* make sure the state is at 0 */
2542 l_is_valid &= (jp2->jp2_state == JP2_STATE_NONE);
2544 /* make sure not reading a jp2h ???? WEIRD */
2545 l_is_valid &= (jp2->jp2_img_state == JP2_IMG_STATE_NONE);
2547 /* POINTER validation */
2548 /* make sure a j2k codec is present */
2549 l_is_valid &= (jp2->j2k != 00);
2551 /* make sure a procedure list is present */
2552 l_is_valid &= (jp2->m_procedure_list != 00);
2554 /* make sure a validation list is present */
2555 l_is_valid &= (jp2->m_validation_list != 00);
2557 /* PARAMETER VALIDATION */
2558 /* number of components */
2559 l_is_valid &= (jp2->numcl > 0);
2561 l_is_valid &= (jp2->h > 0);
2563 l_is_valid &= (jp2->w > 0);
2565 for (i = 0; i < jp2->numcomps; ++i) {
2566 l_is_valid &= (jp2->comps[i].bpcc > 0);
2570 l_is_valid &= ((jp2->meth > 0) && (jp2->meth < 3));
2572 /* stream validation */
2573 /* back and forth is needed */
2574 l_is_valid &= opj_stream_has_seek(cio);
2580 * Reads a jpeg2000 file header structure.
2582 * @param cio the stream to read data from.
2583 * @param jp2 the jpeg2000 file header structure.
2584 * @param p_manager the user event manager.
2586 * @return true if the box is valid.
2588 opj_bool jp2_read_header_procedure(
2590 opj_stream_private_t *cio,
2591 opj_event_mgr_t * p_manager)
2594 OPJ_UINT32 l_nb_bytes_read;
2595 const opj_jp2_header_handler_t * l_current_handler;
2596 OPJ_UINT32 l_last_data_size = BOX_SIZE;
2597 OPJ_UINT32 l_current_data_size;
2598 unsigned char * l_current_data = 00;
2603 assert(p_manager != 00);
2605 l_current_data = (unsigned char*)opj_malloc(l_last_data_size);
2607 if (l_current_data == 00) {
2608 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to handle jpeg2000 file header\n");
2611 memset(l_current_data, 0 , l_last_data_size);
2613 while (jp2_read_boxhdr_v2(&box,&l_nb_bytes_read,cio,p_manager)) {
2614 /* is it the codestream box ? */
2615 if (box.type == JP2_JP2C) {
2616 if (jp2->jp2_state & JP2_STATE_HEADER) {
2617 jp2->jp2_state |= JP2_STATE_CODESTREAM;
2618 opj_free(l_current_data);
2622 opj_event_msg_v2(p_manager, EVT_ERROR, "bad placed jpeg codestream\n");
2623 opj_free(l_current_data);
2627 else if (box.length == 0) {
2628 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot handle box of undefined sizes\n");
2629 opj_free(l_current_data);
2633 l_current_handler = jp2_find_handler(box.type);
2634 l_current_data_size = box.length - l_nb_bytes_read;
2636 if (l_current_handler != 00) {
2637 if (l_current_data_size > l_last_data_size) {
2638 l_current_data = (unsigned char*)opj_realloc(l_current_data,l_current_data_size);
2639 if (!l_current_data){
2640 opj_free(l_current_data);
2643 l_last_data_size = l_current_data_size;
2646 l_nb_bytes_read = opj_stream_read_data(cio,l_current_data,l_current_data_size,p_manager);
2647 if (l_nb_bytes_read != l_current_data_size) {
2648 opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with reading JPEG2000 box, stream error\n");
2652 if (! l_current_handler->handler(jp2,l_current_data,l_current_data_size,p_manager)) {
2653 opj_free(l_current_data);
2658 jp2->jp2_state |= JP2_STATE_UNKNOWN;
2659 if (opj_stream_skip(cio,l_current_data_size,p_manager) != l_current_data_size) {
2660 opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with skipping JPEG2000 box, stream error\n");
2661 opj_free(l_current_data);
2667 opj_free(l_current_data);
2673 * Excutes the given procedures on the given codec.
2675 * @param p_procedure_list the list of procedures to execute
2676 * @param jp2 the jpeg2000 file codec to execute the procedures on.
2677 * @param cio the stream to execute the procedures on.
2678 * @param p_manager the user manager.
2680 * @return true if all the procedures were successfully executed.
2684 opj_procedure_list_t * p_procedure_list,
2685 opj_stream_private_t *cio,
2686 opj_event_mgr_t * p_manager
2689 opj_bool (** l_procedure) (opj_jp2_v2_t * jp2, opj_stream_private_t *, opj_event_mgr_t *) = 00;
2690 opj_bool l_result = OPJ_TRUE;
2691 OPJ_UINT32 l_nb_proc, i;
2694 assert(p_procedure_list != 00);
2697 assert(p_manager != 00);
2699 l_nb_proc = opj_procedure_list_get_nb_procedures(p_procedure_list);
2700 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);
2702 for (i=0;i<l_nb_proc;++i) {
2703 l_result = l_result && (*l_procedure) (jp2,cio,p_manager);
2707 /* and clear the procedure list at the end. */
2708 opj_procedure_list_clear(p_procedure_list);
2713 * Starts a compression scheme, i.e. validates the codec parameters, writes the header.
2715 * @param jp2 the jpeg2000 file codec.
2716 * @param cio the stream object.
2718 * @return true if the codec is valid.
2720 opj_bool jp2_start_compress(opj_jp2_v2_t *jp2,
2721 struct opj_stream_private *cio,
2722 opj_image_t * p_image,
2723 struct opj_event_mgr * p_manager)
2728 assert(p_manager != 00);
2730 /* customization of the validation */
2731 jp2_setup_encoding_validation (jp2);
2733 /* validation of the parameters codec */
2734 if (! jp2_exec(jp2,jp2->m_validation_list,cio,p_manager)) {
2738 /* customization of the encoding */
2739 jp2_setup_header_writting(jp2);
2742 if (! jp2_exec (jp2,jp2->m_procedure_list,cio,p_manager)) {
2746 return j2k_start_compress(jp2->j2k,cio,p_image,p_manager);
2750 * Finds the execution function related to the given box id.
2752 * @param p_id the id of the handler to fetch.
2754 * @return the given handler or 00 if it could not be found.
2756 const opj_jp2_header_handler_t * jp2_find_handler (int p_id)
2758 OPJ_UINT32 i, l_handler_size = sizeof(jp2_header) / sizeof(opj_jp2_header_handler_t);
2760 for (i=0;i<l_handler_size;++i) {
2761 if (jp2_header[i].id == p_id) {
2762 return &jp2_header[i];
2769 * Finds the image execution function related to the given box id.
2771 * @param p_id the id of the handler to fetch.
2773 * @return the given handler or 00 if it could not be found.
2775 static const opj_jp2_header_handler_t * jp2_img_find_handler (
2779 OPJ_UINT32 i, l_handler_size = sizeof(jp2_img_header) / sizeof(opj_jp2_header_handler_t);
2780 for (i=0;i<l_handler_size;++i)
2782 if (jp2_img_header[i].id == p_id) {
2783 return &jp2_img_header[i];
2792 * Reads a jpeg2000 file signature box.
2794 * @param p_header_data the data contained in the signature box.
2795 * @param jp2 the jpeg2000 file codec.
2796 * @param p_header_size the size of the data contained in the signature box.
2797 * @param p_manager the user event manager.
2799 * @return true if the file signature box is valid.
2801 opj_bool jp2_read_jp_v2(
2803 unsigned char * p_header_data,
2804 unsigned int p_header_size,
2805 opj_event_mgr_t * p_manager
2808 unsigned int l_magic_number;
2811 assert(p_header_data != 00);
2813 assert(p_manager != 00);
2815 if (jp2->jp2_state != JP2_STATE_NONE) {
2816 opj_event_msg_v2(p_manager, EVT_ERROR, "The signature box must be the first box in the file.\n");
2820 /* assure length of data is correct (4 -> magic number) */
2821 if (p_header_size != 4) {
2822 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with JP signature Box size\n");
2826 /* rearrange data */
2827 opj_read_bytes(p_header_data,&l_magic_number,4);
2828 if (l_magic_number != 0x0d0a870a ) {
2829 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with JP Signature : bad magic number\n");
2833 jp2->jp2_state |= JP2_STATE_SIGNATURE;
2840 * Reads a a FTYP box - File type box
2842 * @param p_header_data the data contained in the FTYP box.
2843 * @param jp2 the jpeg2000 file codec.
2844 * @param p_header_size the size of the data contained in the FTYP box.
2845 * @param p_manager the user event manager.
2847 * @return true if the FTYP box is valid.
2849 opj_bool jp2_read_ftyp_v2(
2851 unsigned char * p_header_data,
2852 unsigned int p_header_size,
2853 opj_event_mgr_t * p_manager
2856 OPJ_UINT32 i, l_remaining_bytes;
2859 assert(p_header_data != 00);
2861 assert(p_manager != 00);
2863 if (jp2->jp2_state != JP2_STATE_SIGNATURE) {
2864 opj_event_msg_v2(p_manager, EVT_ERROR, "The ftyp box must be the second box in the file.\n");
2868 /* assure length of data is correct */
2869 if (p_header_size < 8) {
2870 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with FTYP signature Box size\n");
2874 opj_read_bytes(p_header_data,&jp2->brand,4); /* BR */
2877 opj_read_bytes(p_header_data,&jp2->minversion,4); /* MinV */
2880 l_remaining_bytes = p_header_size - 8;
2882 /* the number of remaining bytes should be a multiple of 4 */
2883 if ((l_remaining_bytes & 0x3) != 0) {
2884 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with FTYP signature Box size\n");
2889 jp2->numcl = l_remaining_bytes >> 2;
2891 jp2->cl = (unsigned int *) opj_malloc(jp2->numcl * sizeof(unsigned int));
2892 if (jp2->cl == 00) {
2893 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory with FTYP Box\n");
2896 memset(jp2->cl,0,jp2->numcl * sizeof(unsigned int));
2899 for (i = 0; i < jp2->numcl; ++i)
2901 opj_read_bytes(p_header_data,&jp2->cl[i],4); /* CLi */
2905 jp2->jp2_state |= JP2_STATE_FILE_TYPE;
2911 * Skips the Jpeg2000 Codestream Header box - JP2C Header box.
2913 * @param cio the stream to write data to.
2914 * @param jp2 the jpeg2000 file codec.
2915 * @param p_manager user event manager.
2917 * @return true if writting was successful.
2919 opj_bool jp2_skip_jp2c( opj_jp2_v2_t *jp2,
2920 struct opj_stream_private *stream,
2921 struct opj_event_mgr * p_manager )
2925 assert(stream != 00);
2926 assert(p_manager != 00);
2928 jp2->j2k_codestream_offset = opj_stream_tell(stream);
2930 if (opj_stream_skip(stream,8,p_manager) != 8) {
2938 * Reads the Jpeg2000 file Header box - JP2 Header box (warning, this is a super box).
2940 * @param p_header_data the data contained in the file header box.
2941 * @param jp2 the jpeg2000 file codec.
2942 * @param p_header_size the size of the data contained in the file header box.
2943 * @param p_manager the user event manager.
2945 * @return true if the JP2 Header box was successfully reconized.
2947 opj_bool jp2_read_jp2h_v2(
2949 unsigned char * p_header_data,
2950 unsigned int p_header_size,
2951 opj_event_mgr_t * p_manager
2954 OPJ_UINT32 l_box_size=0, l_current_data_size = 0;
2956 const opj_jp2_header_handler_t * l_current_handler;
2959 assert(p_header_data != 00);
2961 assert(p_manager != 00);
2963 /* make sure the box is well placed */
2964 if ((jp2->jp2_state & JP2_STATE_FILE_TYPE) != JP2_STATE_FILE_TYPE ) {
2965 opj_event_msg_v2(p_manager, EVT_ERROR, "The box must be the first box in the file.\n");
2969 jp2->jp2_img_state = JP2_IMG_STATE_NONE;
2971 /* iterate while remaining data */
2972 while (p_header_size > 0) {
2974 if (! jp2_read_boxhdr_char(&box,p_header_data,&l_box_size,p_header_size, p_manager)) {
2975 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream error while reading JP2 Header box\n");
2979 if (box.length > p_header_size) {
2980 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream error while reading JP2 Header box: box length is inconsistent.\n");
2984 l_current_handler = jp2_img_find_handler(box.type);
2985 l_current_data_size = box.length - l_box_size;
2986 p_header_data += l_box_size;
2988 if (l_current_handler != 00) {
2989 if (! l_current_handler->handler(jp2,p_header_data,l_current_data_size,p_manager)) {
2994 jp2->jp2_img_state |= JP2_IMG_STATE_UNKNOWN;
2997 p_header_data += l_current_data_size;
2998 p_header_size -= box.length;
3001 jp2->jp2_state |= JP2_STATE_HEADER;
3007 * Reads a box header. The box is the way data is packed inside a jpeg2000 file structure. Data is read from a character string
3009 * @param p_data the character string to read data from.
3010 * @param box the box structure to fill.
3011 * @param p_number_bytes_read pointer to an int that will store the number of bytes read from the stream (shoul usually be 2).
3012 * @param p_box_max_size the maximum number of bytes in the box.
3014 * @return true if the box is reconized, false otherwise
3016 static opj_bool jp2_read_boxhdr_char(
3019 OPJ_UINT32 * p_number_bytes_read,
3020 OPJ_UINT32 p_box_max_size,
3021 opj_event_mgr_t * p_manager
3027 assert(p_data != 00);
3029 assert(p_number_bytes_read != 00);
3030 assert(p_manager != 00);
3032 if (p_box_max_size < 8) {
3033 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot handle box of less than 8 bytes\n");
3037 /* process read data */
3038 opj_read_bytes(p_data, &l_value, 4);
3040 box->length = (OPJ_INT32)(l_value);
3042 opj_read_bytes(p_data, &l_value, 4);
3044 box->type = (OPJ_INT32)(l_value);
3046 *p_number_bytes_read = 8;
3048 /* do we have a "special very large box ?" */
3049 /* read then the XLBox */
3050 if (box->length == 1) {
3051 unsigned int l_xl_part_size;
3053 if (p_box_max_size < 16) {
3054 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot handle XL box of less than 16 bytes\n");
3058 opj_read_bytes(p_data,&l_xl_part_size, 4);
3060 *p_number_bytes_read += 4;
3062 if (l_xl_part_size != 0) {
3063 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot handle box sizes higher than 2^32\n");
3067 opj_read_bytes(p_data, &l_value, 4);
3068 *p_number_bytes_read += 4;
3069 box->length = (OPJ_INT32)(l_value);
3071 if (box->length == 0) {
3072 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot handle box of undefined sizes\n");
3076 else if (box->length == 0) {
3077 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot handle box of undefined sizes\n");
3086 * Reads a jpeg2000 file header structure.
3088 * @param cio the stream to read data from.
3089 * @param jp2 the jpeg2000 file header structure.
3090 * @param p_manager the user event manager.
3092 * @return true if the box is valid.
3094 opj_bool jp2_read_header( struct opj_stream_private *p_stream,
3096 opj_image_t** p_image,
3097 struct opj_event_mgr * p_manager
3102 assert(p_stream != 00);
3103 assert(p_manager != 00);
3105 /* customization of the validation */
3106 jp2_setup_decoding_validation (jp2);
3108 /* customization of the encoding */
3109 jp2_setup_header_reading(jp2);
3111 /* validation of the parameters codec */
3112 if (! jp2_exec(jp2,jp2->m_validation_list,p_stream,p_manager)) {
3117 if (! jp2_exec (jp2,jp2->m_procedure_list,p_stream,p_manager)) {
3121 return j2k_read_header( p_stream,
3128 * Sets up the validation ,i.e. adds the procedures to launch to make sure the codec parameters
3129 * are valid. Developers wanting to extend the library can add their own validation procedures.
3131 void jp2_setup_encoding_validation (opj_jp2_v2_t *jp2)
3136 opj_procedure_list_add_procedure(jp2->m_validation_list, (opj_procedure)jp2_default_validation);
3137 /* DEVELOPER CORNER, add your custom validation procedure */
3141 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
3142 * are valid. Developpers wanting to extend the library can add their own validation procedures.
3144 void jp2_setup_decoding_validation (opj_jp2_v2_t *jp2)
3148 /* DEVELOPER CORNER, add your custom validation procedure */
3152 * Sets up the procedures to do on writting header.
3153 * Developers wanting to extend the library can add their own writing procedures.
3155 void jp2_setup_header_writting (opj_jp2_v2_t *jp2)
3160 opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)jp2_write_jp_v2 );
3161 opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)jp2_write_ftyp_v2 );
3162 opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)jp2_write_jp2h_v2 );
3163 opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)jp2_skip_jp2c );
3165 /* DEVELOPER CORNER, insert your custom procedures */
3170 * Sets up the procedures to do on reading header.
3171 * Developpers wanting to extend the library can add their own writting procedures.
3173 void jp2_setup_header_reading (opj_jp2_v2_t *jp2)
3178 opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)jp2_read_header_procedure );
3179 /* DEVELOPER CORNER, add your custom procedures */
3184 * Reads a tile header.
3185 * @param p_j2k the jpeg2000 codec.
3186 * @param p_stream the stream to write data to.
3187 * @param p_manager the user event manager.
3189 opj_bool jp2_read_tile_header( opj_jp2_v2_t * p_jp2,
3190 OPJ_UINT32 * p_tile_index,
3191 OPJ_UINT32 * p_data_size,
3192 OPJ_INT32 * p_tile_x0, OPJ_INT32 * p_tile_y0,
3193 OPJ_INT32 * p_tile_x1, OPJ_INT32 * p_tile_y1,
3194 OPJ_UINT32 * p_nb_comps,
3196 opj_stream_private_t *p_stream,
3197 opj_event_mgr_t * p_manager )
3199 return j2k_read_tile_header(p_jp2->j2k,
3202 p_tile_x0, p_tile_y0,
3203 p_tile_x1, p_tile_y1,
3212 * @param p_j2k the jpeg2000 codec.
3213 * @param p_stream the stream to write data to.
3214 * @param p_manager the user event manager.
3216 opj_bool jp2_write_tile ( opj_jp2_v2_t *p_jp2,
3217 OPJ_UINT32 p_tile_index,
3219 OPJ_UINT32 p_data_size,
3220 struct opj_stream_private *p_stream,
3221 struct opj_event_mgr * p_manager )
3223 return j2k_write_tile (p_jp2->j2k,p_tile_index,p_data,p_data_size,p_stream,p_manager);
3228 * @param p_j2k the jpeg2000 codec.
3229 * @param p_stream the stream to write data to.
3230 * @param p_manager the user event manager.
3232 opj_bool jp2_decode_tile (
3233 opj_jp2_v2_t * p_jp2,
3234 OPJ_UINT32 p_tile_index,
3236 OPJ_UINT32 p_data_size,
3237 opj_stream_private_t *p_stream,
3238 opj_event_mgr_t * p_manager
3241 return j2k_decode_tile (p_jp2->j2k,p_tile_index,p_data,p_data_size,p_stream,p_manager);
3245 * Destroys a jpeg2000 file decompressor.
3247 * @param jp2 a jpeg2000 file decompressor.
3249 void jp2_destroy(opj_jp2_v2_t *jp2)
3252 /* destroy the J2K codec */
3253 j2k_destroy(jp2->j2k);
3257 opj_free(jp2->comps);
3266 if (jp2->color.icc_profile_buf) {
3267 opj_free(jp2->color.icc_profile_buf);
3268 jp2->color.icc_profile_buf = 00;
3271 if (jp2->color.jp2_cdef) {
3272 if (jp2->color.jp2_cdef->info) {
3273 opj_free(jp2->color.jp2_cdef->info);
3274 jp2->color.jp2_cdef->info = NULL;
3277 opj_free(jp2->color.jp2_cdef);
3278 jp2->color.jp2_cdef = 00;
3281 if (jp2->color.jp2_pclr) {
3282 if (jp2->color.jp2_pclr->cmap) {
3283 opj_free(jp2->color.jp2_pclr->cmap);
3284 jp2->color.jp2_pclr->cmap = NULL;
3286 if (jp2->color.jp2_pclr->channel_sign) {
3287 opj_free(jp2->color.jp2_pclr->channel_sign);
3288 jp2->color.jp2_pclr->channel_sign = NULL;
3290 if (jp2->color.jp2_pclr->channel_size) {
3291 opj_free(jp2->color.jp2_pclr->channel_size);
3292 jp2->color.jp2_pclr->channel_size = NULL;
3294 if (jp2->color.jp2_pclr->entries) {
3295 opj_free(jp2->color.jp2_pclr->entries);
3296 jp2->color.jp2_pclr->entries = NULL;
3299 opj_free(jp2->color.jp2_pclr);
3300 jp2->color.jp2_pclr = 00;
3303 if (jp2->m_validation_list) {
3304 opj_procedure_list_destroy(jp2->m_validation_list);
3305 jp2->m_validation_list = 00;
3308 if (jp2->m_procedure_list) {
3309 opj_procedure_list_destroy(jp2->m_procedure_list);
3310 jp2->m_procedure_list = 00;
3318 * Sets the given area to be decoded. This function should be called right after opj_read_header and before any tile header reading.
3320 * @param p_jp2 the jpeg2000 codec.
3321 * @param p_end_x the right position of the rectangle to decode (in image coordinates).
3322 * @param p_start_y the up position of the rectangle to decode (in image coordinates).
3323 * @param p_end_y the bottom position of the rectangle to decode (in image coordinates).
3324 * @param p_manager the user event manager
3326 * @return true if the area could be set.
3328 opj_bool jp2_set_decode_area( opj_jp2_v2_t *p_jp2,
3329 opj_image_t* p_image,
3330 OPJ_INT32 p_start_x, OPJ_INT32 p_start_y,
3331 OPJ_INT32 p_end_x, OPJ_INT32 p_end_y,
3332 struct opj_event_mgr * p_manager )
3334 return j2k_set_decode_area(p_jp2->j2k, p_image, p_start_x, p_start_y, p_end_x, p_end_y, p_manager);
3338 * Get the decoded tile.
3340 * @param jp2 the jpeg2000 codec.
3341 * @param p_stream input_stream
3342 * @param p_image output image. .
3343 * @param p_manager the user event manager
3344 * @param tile_index index of the tile we want decode
3346 * @return true if succeed.
3348 opj_bool jp2_get_tile( opj_jp2_v2_t *jp2,
3349 opj_stream_private_t *p_stream,
3350 opj_image_t* p_image,
3351 struct opj_event_mgr * p_manager,
3352 OPJ_UINT32 tile_index )
3357 opj_event_msg_v2(p_manager, EVT_WARNING, "JP2 box which are after the codestream will not be read by this function.\n");
3359 if (! j2k_get_tile(jp2->j2k, p_stream, p_image, p_manager, tile_index) ){
3360 opj_event_msg_v2(p_manager, EVT_ERROR, "Failed to decode the codestream in the JP2 file\n");
3364 /* Set Image Color Space */
3365 if (jp2->enumcs == 16)
3366 p_image->color_space = CLRSPC_SRGB;
3367 else if (jp2->enumcs == 17)
3368 p_image->color_space = CLRSPC_GRAY;
3369 else if (jp2->enumcs == 18)
3370 p_image->color_space = CLRSPC_SYCC;
3372 p_image->color_space = CLRSPC_UNKNOWN;
3374 /* Apply the color space if needed */
3375 if(jp2->color.jp2_cdef) {
3376 jp2_apply_cdef(p_image, &(jp2->color));
3379 if(jp2->color.jp2_pclr) {
3380 /* Part 1, I.5.3.4: Either both or none : */
3381 if( !jp2->color.jp2_pclr->cmap)
3382 jp2_free_pclr(&(jp2->color));
3384 jp2_apply_pclr(p_image, &(jp2->color));
3387 if(jp2->color.icc_profile_buf) {
3388 p_image->icc_profile_buf = jp2->color.icc_profile_buf;
3389 p_image->icc_profile_len = jp2->color.icc_profile_len;
3390 jp2->color.icc_profile_buf = NULL;
3398 /* ----------------------------------------------------------------------- */
3399 /* JP2 encoder interface */
3400 /* ----------------------------------------------------------------------- */
3402 opj_jp2_v2_t* jp2_create(opj_bool p_is_decoder)
3404 opj_jp2_v2_t *jp2 = (opj_jp2_v2_t*)opj_malloc(sizeof(opj_jp2_v2_t));
3406 memset(jp2,0,sizeof(opj_jp2_t));
3408 /* create the J2K codec */
3409 if (! p_is_decoder) {
3410 jp2->j2k = j2k_create_compress_v2();
3413 jp2->j2k = j2k_create_decompress_v2();
3416 if (jp2->j2k == 00) {
3421 /* Color structure */
3422 jp2->color.icc_profile_buf = NULL;
3423 jp2->color.icc_profile_len = 0;
3424 jp2->color.jp2_cdef = NULL;
3425 jp2->color.jp2_pclr = NULL;
3426 jp2->color.jp2_has_colr = 0;
3428 /* validation list creation */
3429 jp2->m_validation_list = opj_procedure_list_create();
3430 if (! jp2->m_validation_list) {
3435 /* execution list creation */
3436 jp2->m_procedure_list = opj_procedure_list_create();
3437 if (! jp2->m_procedure_list) {
3446 void jp2_dump(opj_jp2_v2_t* p_jp2, OPJ_INT32 flag, FILE* out_stream)
3449 assert(p_jp2 != 00);
3451 j2k_dump(p_jp2->j2k,
3456 opj_codestream_index_t* jp2_get_cstr_index(opj_jp2_v2_t* p_jp2)
3458 return j2k_get_cstr_index(p_jp2->j2k);
3461 opj_codestream_info_v2_t* jp2_get_cstr_info(opj_jp2_v2_t* p_jp2)
3463 return j2k_get_cstr_info(p_jp2->j2k);
3466 opj_bool jp2_set_decoded_resolution_factor(opj_jp2_v2_t *p_jp2, OPJ_UINT32 res_factor, opj_event_mgr_t * p_manager)
3468 return j2k_set_decoded_resolution_factor(p_jp2->j2k, res_factor, p_manager);