2 * The copyright in this software is being made available under the 2-clauses
3 * BSD License, included below. This software may be subject to other third
4 * party and contributor rights, including patent rights, and no such rights
5 * are granted under this license.
7 * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium
8 * Copyright (c) 2002-2014, Professor Benoit Macq
9 * Copyright (c) 2001-2003, David Janssens
10 * Copyright (c) 2002-2003, Yannick Verschueren
11 * Copyright (c) 2003-2007, Francois-Olivier Devaux
12 * Copyright (c) 2003-2014, Antonin Descampe
13 * Copyright (c) 2005, Herve Drolon, FreeImage Team
14 * Copyright (c) 2010-2011, Kaori Hagihara
15 * Copyright (c) 2008, 2011-2012, Centre National d'Etudes Spatiales (CNES), FR
16 * Copyright (c) 2012, CS Systemes d'Information, France
17 * All rights reserved.
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following conditions
22 * 1. Redistributions of source code must retain the above copyright
23 * notice, this list of conditions and the following disclaimer.
24 * 2. Redistributions in binary form must reproduce the above copyright
25 * notice, this list of conditions and the following disclaimer in the
26 * documentation and/or other materials provided with the distribution.
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
29 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
32 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38 * POSSIBILITY OF SUCH DAMAGE.
40 #include "opj_includes.h"
42 /** @defgroup JP2 JP2 - JPEG-2000 file format reader/writer */
45 #define OPJ_BOX_SIZE 1024
47 #define OPJ_UNUSED(x) (void)x
49 /** @name Local static functions */
52 /*static void jp2_write_url(opj_cio_t *cio, char *Idx_file);*/
55 * Reads a IHDR box - Image Header box
57 * @param p_image_header_data pointer to actual data (already read from file)
58 * @param jp2 the jpeg2000 file codec.
59 * @param p_image_header_size the size of the image header
60 * @param p_manager the user event manager.
62 * @return true if the image header is valid, false else.
64 static OPJ_BOOL opj_jp2_read_ihdr(opj_jp2_t *jp2,
65 OPJ_BYTE *p_image_header_data,
66 OPJ_UINT32 p_image_header_size,
67 opj_event_mgr_t * p_manager);
70 * Writes the Image Header box - Image Header box.
72 * @param jp2 jpeg2000 file codec.
73 * @param p_nb_bytes_written pointer to store the nb of bytes written by the function.
75 * @return the data being copied.
77 static OPJ_BYTE * opj_jp2_write_ihdr(opj_jp2_t *jp2,
78 OPJ_UINT32 * p_nb_bytes_written);
81 * Writes the Bit per Component box.
83 * @param jp2 jpeg2000 file codec.
84 * @param p_nb_bytes_written pointer to store the nb of bytes written by the function.
86 * @return the data being copied.
88 static OPJ_BYTE * opj_jp2_write_bpcc(opj_jp2_t *jp2,
89 OPJ_UINT32 * p_nb_bytes_written);
92 * Reads a Bit per Component box.
94 * @param p_bpc_header_data pointer to actual data (already read from file)
95 * @param jp2 the jpeg2000 file codec.
96 * @param p_bpc_header_size the size of the bpc header
97 * @param p_manager the user event manager.
99 * @return true if the bpc header is valid, false else.
101 static OPJ_BOOL opj_jp2_read_bpcc(opj_jp2_t *jp2,
102 OPJ_BYTE * p_bpc_header_data,
103 OPJ_UINT32 p_bpc_header_size,
104 opj_event_mgr_t * p_manager);
106 static OPJ_BOOL opj_jp2_read_cdef(opj_jp2_t * jp2,
107 OPJ_BYTE * p_cdef_header_data,
108 OPJ_UINT32 p_cdef_header_size,
109 opj_event_mgr_t * p_manager);
111 static void opj_jp2_apply_cdef(opj_image_t *image, opj_jp2_color_t *color,
115 * Writes the Channel Definition box.
117 * @param jp2 jpeg2000 file codec.
118 * @param p_nb_bytes_written pointer to store the nb of bytes written by the function.
120 * @return the data being copied.
122 static OPJ_BYTE * opj_jp2_write_cdef(opj_jp2_t *jp2,
123 OPJ_UINT32 * p_nb_bytes_written);
126 * Writes the Colour Specification box.
128 * @param jp2 jpeg2000 file codec.
129 * @param p_nb_bytes_written pointer to store the nb of bytes written by the function.
131 * @return the data being copied.
133 static OPJ_BYTE * opj_jp2_write_colr(opj_jp2_t *jp2,
134 OPJ_UINT32 * p_nb_bytes_written);
137 * Writes a FTYP box - File type box
139 * @param cio the stream to write data to.
140 * @param jp2 the jpeg2000 file codec.
141 * @param p_manager the user event manager.
143 * @return true if writing was successful.
145 static OPJ_BOOL opj_jp2_write_ftyp(opj_jp2_t *jp2,
146 opj_stream_private_t *cio,
147 opj_event_mgr_t * p_manager);
150 * Reads a a FTYP box - File type box
152 * @param p_header_data the data contained in the FTYP box.
153 * @param jp2 the jpeg2000 file codec.
154 * @param p_header_size the size of the data contained in the FTYP box.
155 * @param p_manager the user event manager.
157 * @return true if the FTYP box is valid.
159 static OPJ_BOOL opj_jp2_read_ftyp(opj_jp2_t *jp2,
160 OPJ_BYTE * p_header_data,
161 OPJ_UINT32 p_header_size,
162 opj_event_mgr_t * p_manager);
164 static OPJ_BOOL opj_jp2_skip_jp2c(opj_jp2_t *jp2,
165 opj_stream_private_t *cio,
166 opj_event_mgr_t * p_manager);
169 * Reads the Jpeg2000 file Header box - JP2 Header box (warning, this is a super box).
171 * @param p_header_data the data contained in the file header box.
172 * @param jp2 the jpeg2000 file codec.
173 * @param p_header_size the size of the data contained in the file header box.
174 * @param p_manager the user event manager.
176 * @return true if the JP2 Header box was successfully recognized.
178 static OPJ_BOOL opj_jp2_read_jp2h(opj_jp2_t *jp2,
179 OPJ_BYTE *p_header_data,
180 OPJ_UINT32 p_header_size,
181 opj_event_mgr_t * p_manager);
184 * Writes the Jpeg2000 file Header box - JP2 Header box (warning, this is a super box).
186 * @param jp2 the jpeg2000 file codec.
187 * @param stream the stream to write data to.
188 * @param p_manager user event manager.
190 * @return true if writing was successful.
192 static OPJ_BOOL opj_jp2_write_jp2h(opj_jp2_t *jp2,
193 opj_stream_private_t *stream,
194 opj_event_mgr_t * p_manager);
197 * Writes the Jpeg2000 codestream Header box - JP2C Header box. This function must be called AFTER the coding has been done.
199 * @param cio the stream to write data to.
200 * @param jp2 the jpeg2000 file codec.
201 * @param p_manager user event manager.
203 * @return true if writing was successful.
205 static OPJ_BOOL opj_jp2_write_jp2c(opj_jp2_t *jp2,
206 opj_stream_private_t *cio,
207 opj_event_mgr_t * p_manager);
211 * Write index Finder box
212 * @param cio the stream to write to.
213 * @param jp2 the jpeg2000 file codec.
214 * @param p_manager user event manager.
216 static OPJ_BOOL opj_jpip_write_iptr(opj_jp2_t *jp2,
217 opj_stream_private_t *cio,
218 opj_event_mgr_t * p_manager);
221 * Write index Finder box
222 * @param cio the stream to write to.
223 * @param jp2 the jpeg2000 file codec.
224 * @param p_manager user event manager.
226 static OPJ_BOOL opj_jpip_write_cidx(opj_jp2_t *jp2,
227 opj_stream_private_t *cio,
228 opj_event_mgr_t * p_manager);
231 * Write file Index (superbox)
232 * @param cio the stream to write to.
233 * @param jp2 the jpeg2000 file codec.
234 * @param p_manager user event manager.
236 static OPJ_BOOL opj_jpip_write_fidx(opj_jp2_t *jp2,
237 opj_stream_private_t *cio,
238 opj_event_mgr_t * p_manager);
239 #endif /* USE_JPIP */
242 * Reads a jpeg2000 file signature box.
244 * @param p_header_data the data contained in the signature box.
245 * @param jp2 the jpeg2000 file codec.
246 * @param p_header_size the size of the data contained in the signature box.
247 * @param p_manager the user event manager.
249 * @return true if the file signature box is valid.
251 static OPJ_BOOL opj_jp2_read_jp(opj_jp2_t *jp2,
252 OPJ_BYTE * p_header_data,
253 OPJ_UINT32 p_header_size,
254 opj_event_mgr_t * p_manager);
257 * Writes a jpeg2000 file signature box.
259 * @param cio the stream to write data to.
260 * @param jp2 the jpeg2000 file codec.
261 * @param p_manager the user event manager.
263 * @return true if writing was successful.
265 static OPJ_BOOL opj_jp2_write_jp(opj_jp2_t *jp2,
266 opj_stream_private_t *cio,
267 opj_event_mgr_t * p_manager);
270 Apply collected palette data
272 @param color Collector for profile, cdef and pclr data.
273 @param p_manager the user event manager.
274 @return true in case of success
276 static OPJ_BOOL opj_jp2_apply_pclr(opj_image_t *image,
277 opj_jp2_color_t *color,
278 opj_event_mgr_t * p_manager);
280 static void opj_jp2_free_pclr(opj_jp2_color_t *color);
283 * Collect palette data
285 * @param jp2 JP2 handle
286 * @param p_pclr_header_data FIXME DOC
287 * @param p_pclr_header_size FIXME DOC
290 * @return Returns true if successful, returns false otherwise
292 static OPJ_BOOL opj_jp2_read_pclr(opj_jp2_t *jp2,
293 OPJ_BYTE * p_pclr_header_data,
294 OPJ_UINT32 p_pclr_header_size,
295 opj_event_mgr_t * p_manager);
298 * Collect component mapping data
300 * @param jp2 JP2 handle
301 * @param p_cmap_header_data FIXME DOC
302 * @param p_cmap_header_size FIXME DOC
303 * @param p_manager FIXME DOC
305 * @return Returns true if successful, returns false otherwise
308 static OPJ_BOOL opj_jp2_read_cmap(opj_jp2_t * jp2,
309 OPJ_BYTE * p_cmap_header_data,
310 OPJ_UINT32 p_cmap_header_size,
311 opj_event_mgr_t * p_manager);
314 * Reads the Color Specification box.
316 * @param p_colr_header_data pointer to actual data (already read from file)
317 * @param jp2 the jpeg2000 file codec.
318 * @param p_colr_header_size the size of the color header
319 * @param p_manager the user event manager.
321 * @return true if the bpc header is valid, false else.
323 static OPJ_BOOL opj_jp2_read_colr(opj_jp2_t *jp2,
324 OPJ_BYTE * p_colr_header_data,
325 OPJ_UINT32 p_colr_header_size,
326 opj_event_mgr_t * p_manager);
333 * Sets up the procedures to do on writing header after the codestream.
334 * Developpers wanting to extend the library can add their own writing procedures.
336 static OPJ_BOOL opj_jp2_setup_end_header_writing(opj_jp2_t *jp2,
337 opj_event_mgr_t * p_manager);
340 * Sets up the procedures to do on reading header after the codestream.
341 * Developpers wanting to extend the library can add their own writing procedures.
343 static OPJ_BOOL opj_jp2_setup_end_header_reading(opj_jp2_t *jp2,
344 opj_event_mgr_t * p_manager);
347 * Reads a jpeg2000 file header structure.
349 * @param jp2 the jpeg2000 file header structure.
350 * @param stream the stream to read data from.
351 * @param p_manager the user event manager.
353 * @return true if the box is valid.
355 static OPJ_BOOL opj_jp2_read_header_procedure(opj_jp2_t *jp2,
356 opj_stream_private_t *stream,
357 opj_event_mgr_t * p_manager);
360 * Executes the given procedures on the given codec.
362 * @param p_procedure_list the list of procedures to execute
363 * @param jp2 the jpeg2000 file codec to execute the procedures on.
364 * @param stream the stream to execute the procedures on.
365 * @param p_manager the user manager.
367 * @return true if all the procedures were successfully executed.
369 static OPJ_BOOL opj_jp2_exec(opj_jp2_t * jp2,
370 opj_procedure_list_t * p_procedure_list,
371 opj_stream_private_t *stream,
372 opj_event_mgr_t * p_manager);
375 * Reads a box header. The box is the way data is packed inside a jpeg2000 file structure.
377 * @param cio the input stream to read data from.
378 * @param box the box structure to fill.
379 * @param p_number_bytes_read pointer to an int that will store the number of bytes read from the stream (shoul usually be 2).
380 * @param p_manager user event manager.
382 * @return true if the box is recognized, false otherwise
384 static OPJ_BOOL opj_jp2_read_boxhdr(opj_jp2_box_t *box,
385 OPJ_UINT32 * p_number_bytes_read,
386 opj_stream_private_t *cio,
387 opj_event_mgr_t * p_manager);
390 * Sets up the validation ,i.e. adds the procedures to launch to make sure the codec parameters
391 * are valid. Developpers wanting to extend the library can add their own validation procedures.
393 static OPJ_BOOL opj_jp2_setup_encoding_validation(opj_jp2_t *jp2,
394 opj_event_mgr_t * p_manager);
397 * Sets up the procedures to do on writing header. Developpers wanting to extend the library can add their own writing procedures.
399 static OPJ_BOOL opj_jp2_setup_header_writing(opj_jp2_t *jp2,
400 opj_event_mgr_t * p_manager);
402 static OPJ_BOOL opj_jp2_default_validation(opj_jp2_t * jp2,
403 opj_stream_private_t *cio,
404 opj_event_mgr_t * p_manager);
407 * Finds the image execution function related to the given box id.
409 * @param p_id the id of the handler to fetch.
411 * @return the given handler or NULL if it could not be found.
413 static const opj_jp2_header_handler_t * opj_jp2_img_find_handler(
417 * Finds the execution function related to the given box id.
419 * @param p_id the id of the handler to fetch.
421 * @return the given handler or NULL if it could not be found.
423 static const opj_jp2_header_handler_t * opj_jp2_find_handler(OPJ_UINT32 p_id);
425 static const opj_jp2_header_handler_t jp2_header [] = {
426 {JP2_JP, opj_jp2_read_jp},
427 {JP2_FTYP, opj_jp2_read_ftyp},
428 {JP2_JP2H, opj_jp2_read_jp2h}
431 static const opj_jp2_header_handler_t jp2_img_header [] = {
432 {JP2_IHDR, opj_jp2_read_ihdr},
433 {JP2_COLR, opj_jp2_read_colr},
434 {JP2_BPCC, opj_jp2_read_bpcc},
435 {JP2_PCLR, opj_jp2_read_pclr},
436 {JP2_CMAP, opj_jp2_read_cmap},
437 {JP2_CDEF, opj_jp2_read_cdef}
442 * Reads a box header. The box is the way data is packed inside a jpeg2000 file structure. Data is read from a character string
444 * @param box the box structure to fill.
445 * @param p_data the character string to read data from.
446 * @param p_number_bytes_read pointer to an int that will store the number of bytes read from the stream (shoul usually be 2).
447 * @param p_box_max_size the maximum number of bytes in the box.
448 * @param p_manager FIXME DOC
450 * @return true if the box is recognized, false otherwise
452 static OPJ_BOOL opj_jp2_read_boxhdr_char(opj_jp2_box_t *box,
454 OPJ_UINT32 * p_number_bytes_read,
455 OPJ_UINT32 p_box_max_size,
456 opj_event_mgr_t * p_manager);
459 * Sets up the validation ,i.e. adds the procedures to launch to make sure the codec parameters
460 * are valid. Developpers wanting to extend the library can add their own validation procedures.
462 static OPJ_BOOL opj_jp2_setup_decoding_validation(opj_jp2_t *jp2,
463 opj_event_mgr_t * p_manager);
466 * Sets up the procedures to do on reading header.
467 * Developpers wanting to extend the library can add their own writing procedures.
469 static OPJ_BOOL opj_jp2_setup_header_reading(opj_jp2_t *jp2,
470 opj_event_mgr_t * p_manager);
472 /* ----------------------------------------------------------------------- */
473 static OPJ_BOOL opj_jp2_read_boxhdr(opj_jp2_box_t *box,
474 OPJ_UINT32 * p_number_bytes_read,
475 opj_stream_private_t *cio,
476 opj_event_mgr_t * p_manager)
478 /* read header from file */
479 OPJ_BYTE l_data_header [8];
484 assert(p_number_bytes_read != 00);
485 assert(p_manager != 00);
487 *p_number_bytes_read = (OPJ_UINT32)opj_stream_read_data(cio, l_data_header, 8,
489 if (*p_number_bytes_read != 8) {
493 /* process read data */
494 opj_read_bytes(l_data_header, &(box->length), 4);
495 opj_read_bytes(l_data_header + 4, &(box->type), 4);
497 if (box->length == 0) { /* last box */
498 const OPJ_OFF_T bleft = opj_stream_get_number_byte_left(cio);
499 if (bleft > (OPJ_OFF_T)(0xFFFFFFFFU - 8U)) {
500 opj_event_msg(p_manager, EVT_ERROR,
501 "Cannot handle box sizes higher than 2^32\n");
504 box->length = (OPJ_UINT32)bleft + 8U;
505 assert((OPJ_OFF_T)box->length == bleft + 8);
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_UINT32)opj_stream_read_data(cio,
515 l_data_header, 8, p_manager);
516 if (l_nb_bytes_read != 8) {
517 if (l_nb_bytes_read > 0) {
518 *p_number_bytes_read += l_nb_bytes_read;
524 *p_number_bytes_read = 16;
525 opj_read_bytes(l_data_header, &l_xl_part_size, 4);
526 if (l_xl_part_size != 0) {
527 opj_event_msg(p_manager, EVT_ERROR,
528 "Cannot handle box sizes higher than 2^32\n");
531 opj_read_bytes(l_data_header + 4, &(box->length), 4);
537 static void jp2_write_url(opj_cio_t *cio, char *Idx_file)
542 box.init_pos = cio_tell(cio);
544 cio_write(cio, JP2_URL, 4); /* DBTL */
545 cio_write(cio, 0, 1); /* VERS */
546 cio_write(cio, 0, 3); /* FLAG */
549 for (i = 0; i < strlen(Idx_file); i++) {
550 cio_write(cio, Idx_file[i], 1);
554 box.length = cio_tell(cio) - box.init_pos;
555 cio_seek(cio, box.init_pos);
556 cio_write(cio, box.length, 4); /* L */
557 cio_seek(cio, box.init_pos + box.length);
561 static OPJ_BOOL opj_jp2_read_ihdr(opj_jp2_t *jp2,
562 OPJ_BYTE *p_image_header_data,
563 OPJ_UINT32 p_image_header_size,
564 opj_event_mgr_t * p_manager)
567 assert(p_image_header_data != 00);
569 assert(p_manager != 00);
571 if (jp2->comps != NULL) {
572 opj_event_msg(p_manager, EVT_WARNING,
573 "Ignoring ihdr box. First ihdr box already read\n");
577 if (p_image_header_size != 14) {
578 opj_event_msg(p_manager, EVT_ERROR, "Bad image header box (bad size)\n");
582 opj_read_bytes(p_image_header_data, &(jp2->h), 4); /* HEIGHT */
583 p_image_header_data += 4;
584 opj_read_bytes(p_image_header_data, &(jp2->w), 4); /* WIDTH */
585 p_image_header_data += 4;
586 opj_read_bytes(p_image_header_data, &(jp2->numcomps), 2); /* NC */
587 p_image_header_data += 2;
589 if ((jp2->numcomps - 1U) >=
590 16384U) { /* unsigned underflow is well defined: 1U <= jp2->numcomps <= 16384U */
591 opj_event_msg(p_manager, EVT_ERROR, "Invalid number of components (ihdr)\n");
595 /* allocate memory for components */
596 jp2->comps = (opj_jp2_comps_t*) opj_calloc(jp2->numcomps,
597 sizeof(opj_jp2_comps_t));
598 if (jp2->comps == 0) {
599 opj_event_msg(p_manager, EVT_ERROR,
600 "Not enough memory to handle image header (ihdr)\n");
604 opj_read_bytes(p_image_header_data, &(jp2->bpc), 1); /* BPC */
605 ++ p_image_header_data;
607 opj_read_bytes(p_image_header_data, &(jp2->C), 1); /* C */
608 ++ p_image_header_data;
610 /* Should be equal to 7 cf. chapter about image header box of the norm */
612 opj_event_msg(p_manager, EVT_INFO,
613 "JP2 IHDR box: compression type indicate that the file is not a conforming JP2 file (%d) \n",
617 opj_read_bytes(p_image_header_data, &(jp2->UnkC), 1); /* UnkC */
618 ++ p_image_header_data;
619 opj_read_bytes(p_image_header_data, &(jp2->IPR), 1); /* IPR */
620 ++ p_image_header_data;
622 jp2->j2k->m_cp.allow_different_bit_depth_sign = (jp2->bpc == 255);
623 jp2->j2k->ihdr_w = jp2->w;
624 jp2->j2k->ihdr_h = jp2->h;
630 static OPJ_BYTE * opj_jp2_write_ihdr(opj_jp2_t *jp2,
631 OPJ_UINT32 * p_nb_bytes_written
634 OPJ_BYTE * l_ihdr_data, * l_current_ihdr_ptr;
638 assert(p_nb_bytes_written != 00);
640 /* default image header is 22 bytes wide */
641 l_ihdr_data = (OPJ_BYTE *) opj_calloc(1, 22);
642 if (l_ihdr_data == 00) {
646 l_current_ihdr_ptr = l_ihdr_data;
648 opj_write_bytes(l_current_ihdr_ptr, 22, 4); /* write box size */
649 l_current_ihdr_ptr += 4;
651 opj_write_bytes(l_current_ihdr_ptr, JP2_IHDR, 4); /* IHDR */
652 l_current_ihdr_ptr += 4;
654 opj_write_bytes(l_current_ihdr_ptr, jp2->h, 4); /* HEIGHT */
655 l_current_ihdr_ptr += 4;
657 opj_write_bytes(l_current_ihdr_ptr, jp2->w, 4); /* WIDTH */
658 l_current_ihdr_ptr += 4;
660 opj_write_bytes(l_current_ihdr_ptr, jp2->numcomps, 2); /* NC */
661 l_current_ihdr_ptr += 2;
663 opj_write_bytes(l_current_ihdr_ptr, jp2->bpc, 1); /* BPC */
664 ++l_current_ihdr_ptr;
666 opj_write_bytes(l_current_ihdr_ptr, jp2->C, 1); /* C : Always 7 */
667 ++l_current_ihdr_ptr;
669 opj_write_bytes(l_current_ihdr_ptr, jp2->UnkC,
670 1); /* UnkC, colorspace unknown */
671 ++l_current_ihdr_ptr;
673 opj_write_bytes(l_current_ihdr_ptr, jp2->IPR,
674 1); /* IPR, no intellectual property */
675 ++l_current_ihdr_ptr;
677 *p_nb_bytes_written = 22;
682 static OPJ_BYTE * opj_jp2_write_bpcc(opj_jp2_t *jp2,
683 OPJ_UINT32 * p_nb_bytes_written
687 /* room for 8 bytes for box and 1 byte for each component */
688 OPJ_UINT32 l_bpcc_size;
689 OPJ_BYTE * l_bpcc_data, * l_current_bpcc_ptr;
693 assert(p_nb_bytes_written != 00);
694 l_bpcc_size = 8 + jp2->numcomps;
696 l_bpcc_data = (OPJ_BYTE *) opj_calloc(1, l_bpcc_size);
697 if (l_bpcc_data == 00) {
701 l_current_bpcc_ptr = l_bpcc_data;
703 opj_write_bytes(l_current_bpcc_ptr, l_bpcc_size,
704 4); /* write box size */
705 l_current_bpcc_ptr += 4;
707 opj_write_bytes(l_current_bpcc_ptr, JP2_BPCC, 4); /* BPCC */
708 l_current_bpcc_ptr += 4;
710 for (i = 0; i < jp2->numcomps; ++i) {
711 opj_write_bytes(l_current_bpcc_ptr, jp2->comps[i].bpcc,
712 1); /* write each component information */
713 ++l_current_bpcc_ptr;
716 *p_nb_bytes_written = l_bpcc_size;
721 static OPJ_BOOL opj_jp2_read_bpcc(opj_jp2_t *jp2,
722 OPJ_BYTE * p_bpc_header_data,
723 OPJ_UINT32 p_bpc_header_size,
724 opj_event_mgr_t * p_manager
730 assert(p_bpc_header_data != 00);
732 assert(p_manager != 00);
735 if (jp2->bpc != 255) {
736 opj_event_msg(p_manager, EVT_WARNING,
737 "A BPCC header box is available although BPC given by the IHDR box (%d) indicate components bit depth is constant\n",
741 /* and length is relevant */
742 if (p_bpc_header_size != jp2->numcomps) {
743 opj_event_msg(p_manager, EVT_ERROR, "Bad BPCC header box (bad size)\n");
747 /* read info for each component */
748 for (i = 0; i < jp2->numcomps; ++i) {
749 opj_read_bytes(p_bpc_header_data, &jp2->comps[i].bpcc,
750 1); /* read each BPCC component */
756 static OPJ_BYTE * opj_jp2_write_cdef(opj_jp2_t *jp2,
757 OPJ_UINT32 * p_nb_bytes_written)
759 /* room for 8 bytes for box, 2 for n */
760 OPJ_UINT32 l_cdef_size = 10;
761 OPJ_BYTE * l_cdef_data, * l_current_cdef_ptr;
767 assert(p_nb_bytes_written != 00);
768 assert(jp2->color.jp2_cdef != 00);
769 assert(jp2->color.jp2_cdef->info != 00);
770 assert(jp2->color.jp2_cdef->n > 0U);
772 l_cdef_size += 6U * jp2->color.jp2_cdef->n;
774 l_cdef_data = (OPJ_BYTE *) opj_malloc(l_cdef_size);
775 if (l_cdef_data == 00) {
779 l_current_cdef_ptr = l_cdef_data;
781 opj_write_bytes(l_current_cdef_ptr, l_cdef_size, 4); /* write box size */
782 l_current_cdef_ptr += 4;
784 opj_write_bytes(l_current_cdef_ptr, JP2_CDEF, 4); /* BPCC */
785 l_current_cdef_ptr += 4;
787 l_value = jp2->color.jp2_cdef->n;
788 opj_write_bytes(l_current_cdef_ptr, l_value, 2); /* N */
789 l_current_cdef_ptr += 2;
791 for (i = 0U; i < jp2->color.jp2_cdef->n; ++i) {
792 l_value = jp2->color.jp2_cdef->info[i].cn;
793 opj_write_bytes(l_current_cdef_ptr, l_value, 2); /* Cni */
794 l_current_cdef_ptr += 2;
795 l_value = jp2->color.jp2_cdef->info[i].typ;
796 opj_write_bytes(l_current_cdef_ptr, l_value, 2); /* Typi */
797 l_current_cdef_ptr += 2;
798 l_value = jp2->color.jp2_cdef->info[i].asoc;
799 opj_write_bytes(l_current_cdef_ptr, l_value, 2); /* Asoci */
800 l_current_cdef_ptr += 2;
802 *p_nb_bytes_written = l_cdef_size;
807 static OPJ_BYTE * opj_jp2_write_colr(opj_jp2_t *jp2,
808 OPJ_UINT32 * p_nb_bytes_written
811 /* room for 8 bytes for box 3 for common data and variable upon profile*/
812 OPJ_UINT32 l_colr_size = 11;
813 OPJ_BYTE * l_colr_data, * l_current_colr_ptr;
817 assert(p_nb_bytes_written != 00);
818 assert(jp2->meth == 1 || jp2->meth == 2);
822 l_colr_size += 4; /* EnumCS */
825 assert(jp2->color.icc_profile_len); /* ICC profile */
826 l_colr_size += jp2->color.icc_profile_len;
832 l_colr_data = (OPJ_BYTE *) opj_calloc(1, l_colr_size);
833 if (l_colr_data == 00) {
837 l_current_colr_ptr = l_colr_data;
839 opj_write_bytes(l_current_colr_ptr, l_colr_size,
840 4); /* write box size */
841 l_current_colr_ptr += 4;
843 opj_write_bytes(l_current_colr_ptr, JP2_COLR, 4); /* BPCC */
844 l_current_colr_ptr += 4;
846 opj_write_bytes(l_current_colr_ptr, jp2->meth, 1); /* METH */
847 ++l_current_colr_ptr;
849 opj_write_bytes(l_current_colr_ptr, jp2->precedence, 1); /* PRECEDENCE */
850 ++l_current_colr_ptr;
852 opj_write_bytes(l_current_colr_ptr, jp2->approx, 1); /* APPROX */
853 ++l_current_colr_ptr;
856 1) { /* Meth value is restricted to 1 or 2 (Table I.9 of part 1) */
857 opj_write_bytes(l_current_colr_ptr, jp2->enumcs, 4);
860 if (jp2->meth == 2) { /* ICC profile */
862 for (i = 0; i < jp2->color.icc_profile_len; ++i) {
863 opj_write_bytes(l_current_colr_ptr, jp2->color.icc_profile_buf[i], 1);
864 ++l_current_colr_ptr;
869 *p_nb_bytes_written = l_colr_size;
874 static void opj_jp2_free_pclr(opj_jp2_color_t *color)
876 opj_free(color->jp2_pclr->channel_sign);
877 opj_free(color->jp2_pclr->channel_size);
878 opj_free(color->jp2_pclr->entries);
880 if (color->jp2_pclr->cmap) {
881 opj_free(color->jp2_pclr->cmap);
884 opj_free(color->jp2_pclr);
885 color->jp2_pclr = NULL;
888 static OPJ_BOOL opj_jp2_check_color(opj_image_t *image, opj_jp2_color_t *color,
889 opj_event_mgr_t *p_manager)
893 /* testcase 4149.pdf.SIGSEGV.cf7.3501 */
894 if (color->jp2_cdef) {
895 opj_jp2_cdef_info_t *info = color->jp2_cdef->info;
896 OPJ_UINT16 n = color->jp2_cdef->n;
897 OPJ_UINT32 nr_channels =
898 image->numcomps; /* FIXME image->numcomps == jp2->numcomps before color is applied ??? */
900 /* cdef applies to cmap channels if any */
901 if (color->jp2_pclr && color->jp2_pclr->cmap) {
902 nr_channels = (OPJ_UINT32)color->jp2_pclr->nr_channels;
905 for (i = 0; i < n; i++) {
906 if (info[i].cn >= nr_channels) {
907 opj_event_msg(p_manager, EVT_ERROR, "Invalid component index %d (>= %d).\n",
908 info[i].cn, nr_channels);
911 if (info[i].asoc == 65535U) {
915 if (info[i].asoc > 0 && (OPJ_UINT32)(info[i].asoc - 1) >= nr_channels) {
916 opj_event_msg(p_manager, EVT_ERROR, "Invalid component index %d (>= %d).\n",
917 info[i].asoc - 1, nr_channels);
923 /* ISO 15444-1 states that if cdef is present, it shall contain a complete list of channel definitions. */
924 while (nr_channels > 0) {
925 for (i = 0; i < n; ++i) {
926 if ((OPJ_UINT32)info[i].cn == (nr_channels - 1U)) {
931 opj_event_msg(p_manager, EVT_ERROR, "Incomplete channel definitions.\n");
938 /* testcases 451.pdf.SIGSEGV.f4c.3723, 451.pdf.SIGSEGV.5b5.3723 and
939 66ea31acbb0f23a2bbc91f64d69a03f5_signal_sigsegv_13937c0_7030_5725.pdf */
940 if (color->jp2_pclr && color->jp2_pclr->cmap) {
941 OPJ_UINT16 nr_channels = color->jp2_pclr->nr_channels;
942 opj_jp2_cmap_comp_t *cmap = color->jp2_pclr->cmap;
943 OPJ_BOOL *pcol_usage, is_sane = OPJ_TRUE;
945 /* verify that all original components match an existing one */
946 for (i = 0; i < nr_channels; i++) {
947 if (cmap[i].cmp >= image->numcomps) {
948 opj_event_msg(p_manager, EVT_ERROR, "Invalid component index %d (>= %d).\n",
949 cmap[i].cmp, image->numcomps);
954 pcol_usage = (OPJ_BOOL *) opj_calloc(nr_channels, sizeof(OPJ_BOOL));
956 opj_event_msg(p_manager, EVT_ERROR, "Unexpected OOM.\n");
959 /* verify that no component is targeted more than once */
960 for (i = 0; i < nr_channels; i++) {
961 OPJ_BYTE mtyp = cmap[i].mtyp;
962 OPJ_BYTE pcol = cmap[i].pcol;
963 /* See ISO 15444-1 Table I.14 – MTYPi field values */
964 if (mtyp != 0 && mtyp != 1) {
965 opj_event_msg(p_manager, EVT_ERROR,
966 "Invalid value for cmap[%d].mtyp = %d.\n", i,
969 } else if (pcol >= nr_channels) {
970 opj_event_msg(p_manager, EVT_ERROR,
971 "Invalid component/palette index for direct mapping %d.\n", pcol);
973 } else if (pcol_usage[pcol] && mtyp == 1) {
974 opj_event_msg(p_manager, EVT_ERROR, "Component %d is mapped twice.\n", pcol);
976 } else if (mtyp == 0 && pcol != 0) {
977 /* I.5.3.5 PCOL: If the value of the MTYP field for this channel is 0, then
978 * the value of this field shall be 0. */
979 opj_event_msg(p_manager, EVT_ERROR, "Direct use at #%d however pcol=%d.\n", i,
982 } else if (mtyp == 1 && pcol != i) {
983 /* OpenJPEG implementation limitation. See assert(i == pcol); */
984 /* in opj_jp2_apply_pclr() */
985 opj_event_msg(p_manager, EVT_ERROR,
986 "Implementation limitation: for palette mapping, "
987 "pcol[%d] should be equal to %d, but is equal "
988 "to %d.\n", i, i, pcol);
991 pcol_usage[pcol] = OPJ_TRUE;
994 /* verify that all components are targeted at least once */
995 for (i = 0; i < nr_channels; i++) {
996 if (!pcol_usage[i] && cmap[i].mtyp != 0) {
997 opj_event_msg(p_manager, EVT_ERROR, "Component %d doesn't have a mapping.\n",
1002 /* Issue 235/447 weird cmap */
1003 if (1 && is_sane && (image->numcomps == 1U)) {
1004 for (i = 0; i < nr_channels; i++) {
1005 if (!pcol_usage[i]) {
1007 opj_event_msg(p_manager, EVT_WARNING,
1008 "Component mapping seems wrong. Trying to correct.\n", i);
1014 for (i = 0; i < nr_channels; i++) {
1016 cmap[i].pcol = (OPJ_BYTE) i;
1020 opj_free(pcol_usage);
1030 static OPJ_BOOL opj_jp2_apply_pclr(opj_image_t *image,
1031 opj_jp2_color_t *color,
1032 opj_event_mgr_t * p_manager)
1034 opj_image_comp_t *old_comps, *new_comps;
1035 OPJ_BYTE *channel_size, *channel_sign;
1036 OPJ_UINT32 *entries;
1037 opj_jp2_cmap_comp_t *cmap;
1038 OPJ_INT32 *src, *dst;
1040 OPJ_UINT16 i, nr_channels, cmp, pcol;
1043 channel_size = color->jp2_pclr->channel_size;
1044 channel_sign = color->jp2_pclr->channel_sign;
1045 entries = color->jp2_pclr->entries;
1046 cmap = color->jp2_pclr->cmap;
1047 nr_channels = color->jp2_pclr->nr_channels;
1049 for (i = 0; i < nr_channels; ++i) {
1050 /* Palette mapping: */
1052 if (image->comps[cmp].data == NULL) {
1053 opj_event_msg(p_manager, EVT_ERROR,
1054 "image->comps[%d].data == NULL in opj_jp2_apply_pclr().\n", i);
1059 old_comps = image->comps;
1060 new_comps = (opj_image_comp_t*)
1061 opj_malloc(nr_channels * sizeof(opj_image_comp_t));
1063 opj_event_msg(p_manager, EVT_ERROR,
1064 "Memory allocation failure in opj_jp2_apply_pclr().\n");
1067 for (i = 0; i < nr_channels; ++i) {
1068 pcol = cmap[i].pcol;
1072 if (cmap[i].mtyp == 0) {
1074 new_comps[i] = old_comps[cmp];
1077 new_comps[pcol] = old_comps[cmp];
1080 /* Palette mapping: */
1081 new_comps[i].data = (OPJ_INT32*)
1082 opj_image_data_alloc(old_comps[cmp].w * old_comps[cmp].h * sizeof(OPJ_INT32));
1083 if (!new_comps[i].data) {
1086 opj_image_data_free(new_comps[i].data);
1088 opj_free(new_comps);
1089 opj_event_msg(p_manager, EVT_ERROR,
1090 "Memory allocation failure in opj_jp2_apply_pclr().\n");
1093 new_comps[i].prec = channel_size[i];
1094 new_comps[i].sgnd = channel_sign[i];
1097 top_k = color->jp2_pclr->nr_entries - 1;
1099 for (i = 0; i < nr_channels; ++i) {
1100 /* Palette mapping: */
1102 pcol = cmap[i].pcol;
1103 src = old_comps[cmp].data;
1104 assert(src); /* verified above */
1105 max = new_comps[pcol].w * new_comps[pcol].h;
1108 if (cmap[i].mtyp == 0) {
1110 dst = new_comps[i].data;
1112 for (j = 0; j < max; ++j) {
1117 dst = new_comps[pcol].data;
1119 for (j = 0; j < max; ++j) {
1121 if ((k = src[j]) < 0) {
1123 } else if (k > top_k) {
1128 dst[j] = (OPJ_INT32)entries[k * nr_channels + pcol];
1133 max = image->numcomps;
1134 for (i = 0; i < max; ++i) {
1135 if (old_comps[i].data) {
1136 opj_image_data_free(old_comps[i].data);
1140 opj_free(old_comps);
1141 image->comps = new_comps;
1142 image->numcomps = nr_channels;
1147 static OPJ_BOOL opj_jp2_read_pclr(opj_jp2_t *jp2,
1148 OPJ_BYTE * p_pclr_header_data,
1149 OPJ_UINT32 p_pclr_header_size,
1150 opj_event_mgr_t * p_manager
1153 opj_jp2_pclr_t *jp2_pclr;
1154 OPJ_BYTE *channel_size, *channel_sign;
1155 OPJ_UINT32 *entries;
1156 OPJ_UINT16 nr_entries, nr_channels;
1159 OPJ_BYTE *orig_header_data = p_pclr_header_data;
1162 assert(p_pclr_header_data != 00);
1164 assert(p_manager != 00);
1165 (void)p_pclr_header_size;
1167 if (jp2->color.jp2_pclr) {
1171 if (p_pclr_header_size < 3) {
1175 opj_read_bytes(p_pclr_header_data, &l_value, 2); /* NE */
1176 p_pclr_header_data += 2;
1177 nr_entries = (OPJ_UINT16) l_value;
1178 if ((nr_entries == 0U) || (nr_entries > 1024U)) {
1179 opj_event_msg(p_manager, EVT_ERROR, "Invalid PCLR box. Reports %d entries\n",
1184 opj_read_bytes(p_pclr_header_data, &l_value, 1); /* NPC */
1185 ++p_pclr_header_data;
1186 nr_channels = (OPJ_UINT16) l_value;
1187 if (nr_channels == 0U) {
1188 opj_event_msg(p_manager, EVT_ERROR,
1189 "Invalid PCLR box. Reports 0 palette columns\n");
1193 if (p_pclr_header_size < 3 + (OPJ_UINT32)nr_channels) {
1197 entries = (OPJ_UINT32*) opj_malloc((size_t)nr_channels * nr_entries * sizeof(
1202 channel_size = (OPJ_BYTE*) opj_malloc(nr_channels);
1203 if (!channel_size) {
1207 channel_sign = (OPJ_BYTE*) opj_malloc(nr_channels);
1208 if (!channel_sign) {
1210 opj_free(channel_size);
1214 jp2_pclr = (opj_jp2_pclr_t*)opj_malloc(sizeof(opj_jp2_pclr_t));
1217 opj_free(channel_size);
1218 opj_free(channel_sign);
1222 jp2_pclr->channel_sign = channel_sign;
1223 jp2_pclr->channel_size = channel_size;
1224 jp2_pclr->entries = entries;
1225 jp2_pclr->nr_entries = nr_entries;
1226 jp2_pclr->nr_channels = (OPJ_BYTE) l_value;
1227 jp2_pclr->cmap = NULL;
1229 jp2->color.jp2_pclr = jp2_pclr;
1231 for (i = 0; i < nr_channels; ++i) {
1232 opj_read_bytes(p_pclr_header_data, &l_value, 1); /* Bi */
1233 ++p_pclr_header_data;
1235 channel_size[i] = (OPJ_BYTE)((l_value & 0x7f) + 1);
1236 channel_sign[i] = (l_value & 0x80) ? 1 : 0;
1239 for (j = 0; j < nr_entries; ++j) {
1240 for (i = 0; i < nr_channels; ++i) {
1241 OPJ_UINT32 bytes_to_read = (OPJ_UINT32)((channel_size[i] + 7) >> 3);
1243 if (bytes_to_read > sizeof(OPJ_UINT32)) {
1244 bytes_to_read = sizeof(OPJ_UINT32);
1246 if ((ptrdiff_t)p_pclr_header_size < (ptrdiff_t)(p_pclr_header_data -
1247 orig_header_data) + (ptrdiff_t)bytes_to_read) {
1251 opj_read_bytes(p_pclr_header_data, &l_value, bytes_to_read); /* Cji */
1252 p_pclr_header_data += bytes_to_read;
1253 *entries = (OPJ_UINT32) l_value;
1261 static OPJ_BOOL opj_jp2_read_cmap(opj_jp2_t * jp2,
1262 OPJ_BYTE * p_cmap_header_data,
1263 OPJ_UINT32 p_cmap_header_size,
1264 opj_event_mgr_t * p_manager
1267 opj_jp2_cmap_comp_t *cmap;
1268 OPJ_BYTE i, nr_channels;
1273 assert(p_cmap_header_data != 00);
1274 assert(p_manager != 00);
1275 (void)p_cmap_header_size;
1277 /* Need nr_channels: */
1278 if (jp2->color.jp2_pclr == NULL) {
1279 opj_event_msg(p_manager, EVT_ERROR,
1280 "Need to read a PCLR box before the CMAP box.\n");
1284 /* Part 1, I.5.3.5: 'There shall be at most one Component Mapping box
1285 * inside a JP2 Header box' :
1287 if (jp2->color.jp2_pclr->cmap) {
1288 opj_event_msg(p_manager, EVT_ERROR, "Only one CMAP box is allowed.\n");
1292 nr_channels = jp2->color.jp2_pclr->nr_channels;
1293 if (p_cmap_header_size < (OPJ_UINT32)nr_channels * 4) {
1294 opj_event_msg(p_manager, EVT_ERROR, "Insufficient data for CMAP box.\n");
1298 cmap = (opj_jp2_cmap_comp_t*) opj_malloc(nr_channels * sizeof(
1299 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 opj_jp2_apply_cdef(opj_image_t *image, opj_jp2_color_t *color,
1325 opj_event_mgr_t *manager)
1327 opj_jp2_cdef_info_t *info;
1328 OPJ_UINT16 i, n, cn, asoc, acn;
1330 info = color->jp2_cdef->info;
1331 n = color->jp2_cdef->n;
1333 for (i = 0; i < n; ++i) {
1334 /* WATCH: acn = asoc - 1 ! */
1335 asoc = info[i].asoc;
1338 if (cn >= image->numcomps) {
1339 opj_event_msg(manager, EVT_WARNING, "opj_jp2_apply_cdef: cn=%d, numcomps=%d\n",
1340 cn, image->numcomps);
1343 if (asoc == 0 || asoc == 65535) {
1344 image->comps[cn].alpha = info[i].typ;
1348 acn = (OPJ_UINT16)(asoc - 1);
1349 if (acn >= image->numcomps) {
1350 opj_event_msg(manager, EVT_WARNING, "opj_jp2_apply_cdef: acn=%d, numcomps=%d\n",
1351 acn, image->numcomps);
1355 /* Swap only if color channel */
1356 if ((cn != acn) && (info[i].typ == 0)) {
1357 opj_image_comp_t saved;
1360 memcpy(&saved, &image->comps[cn], sizeof(opj_image_comp_t));
1361 memcpy(&image->comps[cn], &image->comps[acn], sizeof(opj_image_comp_t));
1362 memcpy(&image->comps[acn], &saved, sizeof(opj_image_comp_t));
1364 /* Swap channels in following channel definitions, don't bother with j <= i that are already processed */
1365 for (j = (OPJ_UINT16)(i + 1U); j < n ; ++j) {
1366 if (info[j].cn == cn) {
1368 } else if (info[j].cn == acn) {
1371 /* asoc is related to color index. Do not update. */
1375 image->comps[cn].alpha = info[i].typ;
1378 if (color->jp2_cdef->info) {
1379 opj_free(color->jp2_cdef->info);
1382 opj_free(color->jp2_cdef);
1383 color->jp2_cdef = NULL;
1385 }/* jp2_apply_cdef() */
1387 static OPJ_BOOL opj_jp2_read_cdef(opj_jp2_t * jp2,
1388 OPJ_BYTE * p_cdef_header_data,
1389 OPJ_UINT32 p_cdef_header_size,
1390 opj_event_mgr_t * p_manager
1393 opj_jp2_cdef_info_t *cdef_info;
1399 assert(p_cdef_header_data != 00);
1400 assert(p_manager != 00);
1401 (void)p_cdef_header_size;
1403 /* Part 1, I.5.3.6: 'The shall be at most one Channel Definition box
1404 * inside a JP2 Header box.'*/
1405 if (jp2->color.jp2_cdef) {
1409 if (p_cdef_header_size < 2) {
1410 opj_event_msg(p_manager, EVT_ERROR, "Insufficient data for CDEF box.\n");
1414 opj_read_bytes(p_cdef_header_data, &l_value, 2); /* N */
1415 p_cdef_header_data += 2;
1417 if ((OPJ_UINT16)l_value == 0) { /* szukw000: FIXME */
1418 opj_event_msg(p_manager, EVT_ERROR,
1419 "Number of channel description is equal to zero in CDEF box.\n");
1423 if (p_cdef_header_size < 2 + (OPJ_UINT32)(OPJ_UINT16)l_value * 6) {
1424 opj_event_msg(p_manager, EVT_ERROR, "Insufficient data for CDEF box.\n");
1428 cdef_info = (opj_jp2_cdef_info_t*) opj_malloc(l_value * sizeof(
1429 opj_jp2_cdef_info_t));
1434 jp2->color.jp2_cdef = (opj_jp2_cdef_t*)opj_malloc(sizeof(opj_jp2_cdef_t));
1435 if (!jp2->color.jp2_cdef) {
1436 opj_free(cdef_info);
1439 jp2->color.jp2_cdef->info = cdef_info;
1440 jp2->color.jp2_cdef->n = (OPJ_UINT16) l_value;
1442 for (i = 0; i < jp2->color.jp2_cdef->n; ++i) {
1443 opj_read_bytes(p_cdef_header_data, &l_value, 2); /* Cn^i */
1444 p_cdef_header_data += 2;
1445 cdef_info[i].cn = (OPJ_UINT16) l_value;
1447 opj_read_bytes(p_cdef_header_data, &l_value, 2); /* Typ^i */
1448 p_cdef_header_data += 2;
1449 cdef_info[i].typ = (OPJ_UINT16) l_value;
1451 opj_read_bytes(p_cdef_header_data, &l_value, 2); /* Asoc^i */
1452 p_cdef_header_data += 2;
1453 cdef_info[i].asoc = (OPJ_UINT16) l_value;
1459 static OPJ_BOOL opj_jp2_read_colr(opj_jp2_t *jp2,
1460 OPJ_BYTE * p_colr_header_data,
1461 OPJ_UINT32 p_colr_header_size,
1462 opj_event_mgr_t * p_manager
1469 assert(p_colr_header_data != 00);
1470 assert(p_manager != 00);
1472 if (p_colr_header_size < 3) {
1473 opj_event_msg(p_manager, EVT_ERROR, "Bad COLR header box (bad size)\n");
1477 /* Part 1, I.5.3.3 : 'A conforming JP2 reader shall ignore all Colour
1478 * Specification boxes after the first.'
1480 if (jp2->color.jp2_has_colr) {
1481 opj_event_msg(p_manager, EVT_INFO,
1482 "A conforming JP2 reader shall ignore all Colour Specification boxes after the first, so we ignore this one.\n");
1483 p_colr_header_data += p_colr_header_size;
1487 opj_read_bytes(p_colr_header_data, &jp2->meth, 1); /* METH */
1488 ++p_colr_header_data;
1490 opj_read_bytes(p_colr_header_data, &jp2->precedence, 1); /* PRECEDENCE */
1491 ++p_colr_header_data;
1493 opj_read_bytes(p_colr_header_data, &jp2->approx, 1); /* APPROX */
1494 ++p_colr_header_data;
1496 if (jp2->meth == 1) {
1497 if (p_colr_header_size < 7) {
1498 opj_event_msg(p_manager, EVT_ERROR, "Bad COLR header box (bad size: %d)\n",
1499 p_colr_header_size);
1502 if ((p_colr_header_size > 7) &&
1503 (jp2->enumcs != 14)) { /* handled below for CIELab) */
1504 /* testcase Altona_Technical_v20_x4.pdf */
1505 opj_event_msg(p_manager, EVT_WARNING, "Bad COLR header box (bad size: %d)\n",
1506 p_colr_header_size);
1509 opj_read_bytes(p_colr_header_data, &jp2->enumcs, 4); /* EnumCS */
1511 p_colr_header_data += 4;
1513 if (jp2->enumcs == 14) { /* CIELab */
1515 OPJ_UINT32 rl, ol, ra, oa, rb, ob, il;
1517 cielab = (OPJ_UINT32*)opj_malloc(9 * sizeof(OPJ_UINT32));
1518 if (cielab == NULL) {
1519 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory for cielab\n");
1522 cielab[0] = 14; /* enumcs */
1524 /* default values */
1525 rl = ra = rb = ol = oa = ob = 0;
1526 il = 0x00443530; /* D50 */
1527 cielab[1] = 0x44454600;/* DEF */
1529 if (p_colr_header_size == 35) {
1530 opj_read_bytes(p_colr_header_data, &rl, 4);
1531 p_colr_header_data += 4;
1532 opj_read_bytes(p_colr_header_data, &ol, 4);
1533 p_colr_header_data += 4;
1534 opj_read_bytes(p_colr_header_data, &ra, 4);
1535 p_colr_header_data += 4;
1536 opj_read_bytes(p_colr_header_data, &oa, 4);
1537 p_colr_header_data += 4;
1538 opj_read_bytes(p_colr_header_data, &rb, 4);
1539 p_colr_header_data += 4;
1540 opj_read_bytes(p_colr_header_data, &ob, 4);
1541 p_colr_header_data += 4;
1542 opj_read_bytes(p_colr_header_data, &il, 4);
1543 p_colr_header_data += 4;
1546 } else if (p_colr_header_size != 7) {
1547 opj_event_msg(p_manager, EVT_WARNING,
1548 "Bad COLR header box (CIELab, bad size: %d)\n", p_colr_header_size);
1558 jp2->color.icc_profile_buf = (OPJ_BYTE*)cielab;
1559 jp2->color.icc_profile_len = 0;
1561 jp2->color.jp2_has_colr = 1;
1562 } else if (jp2->meth == 2) {
1564 OPJ_INT32 it_icc_value = 0;
1565 OPJ_INT32 icc_len = (OPJ_INT32)p_colr_header_size - 3;
1567 jp2->color.icc_profile_len = (OPJ_UINT32)icc_len;
1568 jp2->color.icc_profile_buf = (OPJ_BYTE*) opj_calloc(1, (size_t)icc_len);
1569 if (!jp2->color.icc_profile_buf) {
1570 jp2->color.icc_profile_len = 0;
1574 for (it_icc_value = 0; it_icc_value < icc_len; ++it_icc_value) {
1575 opj_read_bytes(p_colr_header_data, &l_value, 1); /* icc values */
1576 ++p_colr_header_data;
1577 jp2->color.icc_profile_buf[it_icc_value] = (OPJ_BYTE) l_value;
1580 jp2->color.jp2_has_colr = 1;
1581 } else if (jp2->meth > 2) {
1582 /* ISO/IEC 15444-1:2004 (E), Table I.9 Legal METH values:
1583 conforming JP2 reader shall ignore the entire Colour Specification box.*/
1584 opj_event_msg(p_manager, EVT_INFO,
1585 "COLR BOX meth value is not a regular value (%d), "
1586 "so we will ignore the entire Colour Specification box. \n", jp2->meth);
1588 if (jp2->color.jp2_has_colr) {
1589 jp2->j2k->enumcs = jp2->enumcs;
1594 OPJ_BOOL opj_jp2_decode(opj_jp2_t *jp2,
1595 opj_stream_private_t *p_stream,
1596 opj_image_t* p_image,
1597 opj_event_mgr_t * p_manager)
1604 if (! opj_j2k_decode(jp2->j2k, p_stream, p_image, p_manager)) {
1605 opj_event_msg(p_manager, EVT_ERROR,
1606 "Failed to decode the codestream in the JP2 file\n");
1610 if (!jp2->ignore_pclr_cmap_cdef) {
1611 if (!opj_jp2_check_color(p_image, &(jp2->color), p_manager)) {
1615 /* Set Image Color Space */
1616 if (jp2->enumcs == 16) {
1617 p_image->color_space = OPJ_CLRSPC_SRGB;
1618 } else if (jp2->enumcs == 17) {
1619 p_image->color_space = OPJ_CLRSPC_GRAY;
1620 } else if (jp2->enumcs == 18) {
1621 p_image->color_space = OPJ_CLRSPC_SYCC;
1622 } else if (jp2->enumcs == 24) {
1623 p_image->color_space = OPJ_CLRSPC_EYCC;
1624 } else if (jp2->enumcs == 12) {
1625 p_image->color_space = OPJ_CLRSPC_CMYK;
1627 p_image->color_space = OPJ_CLRSPC_UNKNOWN;
1630 if (jp2->color.jp2_pclr) {
1631 /* Part 1, I.5.3.4: Either both or none : */
1632 if (!jp2->color.jp2_pclr->cmap) {
1633 opj_jp2_free_pclr(&(jp2->color));
1635 if (!opj_jp2_apply_pclr(p_image, &(jp2->color), p_manager)) {
1641 /* Apply the color space if needed */
1642 if (jp2->color.jp2_cdef) {
1643 opj_jp2_apply_cdef(p_image, &(jp2->color), p_manager);
1646 if (jp2->color.icc_profile_buf) {
1647 p_image->icc_profile_buf = jp2->color.icc_profile_buf;
1648 p_image->icc_profile_len = jp2->color.icc_profile_len;
1649 jp2->color.icc_profile_buf = NULL;
1656 static OPJ_BOOL opj_jp2_write_jp2h(opj_jp2_t *jp2,
1657 opj_stream_private_t *stream,
1658 opj_event_mgr_t * p_manager
1661 opj_jp2_img_header_writer_handler_t l_writers [4];
1662 opj_jp2_img_header_writer_handler_t * l_current_writer;
1664 OPJ_INT32 i, l_nb_pass;
1665 /* size of data for super box*/
1666 OPJ_UINT32 l_jp2h_size = 8;
1667 OPJ_BOOL l_result = OPJ_TRUE;
1669 /* to store the data of the super box */
1670 OPJ_BYTE l_jp2h_data [8];
1673 assert(stream != 00);
1675 assert(p_manager != 00);
1677 memset(l_writers, 0, sizeof(l_writers));
1679 if (jp2->bpc == 255) {
1681 l_writers[0].handler = opj_jp2_write_ihdr;
1682 l_writers[1].handler = opj_jp2_write_bpcc;
1683 l_writers[2].handler = opj_jp2_write_colr;
1686 l_writers[0].handler = opj_jp2_write_ihdr;
1687 l_writers[1].handler = opj_jp2_write_colr;
1690 if (jp2->color.jp2_cdef != NULL) {
1691 l_writers[l_nb_pass].handler = opj_jp2_write_cdef;
1695 /* write box header */
1696 /* write JP2H type */
1697 opj_write_bytes(l_jp2h_data + 4, JP2_JP2H, 4);
1699 l_current_writer = l_writers;
1700 for (i = 0; i < l_nb_pass; ++i) {
1701 l_current_writer->m_data = l_current_writer->handler(jp2,
1702 &(l_current_writer->m_size));
1703 if (l_current_writer->m_data == 00) {
1704 opj_event_msg(p_manager, EVT_ERROR,
1705 "Not enough memory to hold JP2 Header data\n");
1706 l_result = OPJ_FALSE;
1710 l_jp2h_size += l_current_writer->m_size;
1715 l_current_writer = l_writers;
1716 for (i = 0; i < l_nb_pass; ++i) {
1717 if (l_current_writer->m_data != 00) {
1718 opj_free(l_current_writer->m_data);
1726 /* write super box size */
1727 opj_write_bytes(l_jp2h_data, l_jp2h_size, 4);
1729 /* write super box data on stream */
1730 if (opj_stream_write_data(stream, l_jp2h_data, 8, p_manager) != 8) {
1731 opj_event_msg(p_manager, EVT_ERROR,
1732 "Stream error while writing JP2 Header box\n");
1733 l_result = OPJ_FALSE;
1737 l_current_writer = l_writers;
1738 for (i = 0; i < l_nb_pass; ++i) {
1739 if (opj_stream_write_data(stream, l_current_writer->m_data,
1740 l_current_writer->m_size, p_manager) != l_current_writer->m_size) {
1741 opj_event_msg(p_manager, EVT_ERROR,
1742 "Stream error while writing JP2 Header box\n");
1743 l_result = OPJ_FALSE;
1750 l_current_writer = l_writers;
1753 for (i = 0; i < l_nb_pass; ++i) {
1754 if (l_current_writer->m_data != 00) {
1755 opj_free(l_current_writer->m_data);
1763 static OPJ_BOOL opj_jp2_write_ftyp(opj_jp2_t *jp2,
1764 opj_stream_private_t *cio,
1765 opj_event_mgr_t * p_manager)
1768 OPJ_UINT32 l_ftyp_size;
1769 OPJ_BYTE * l_ftyp_data, * l_current_data_ptr;
1775 assert(p_manager != 00);
1776 l_ftyp_size = 16 + 4 * jp2->numcl;
1778 l_ftyp_data = (OPJ_BYTE *) opj_calloc(1, l_ftyp_size);
1780 if (l_ftyp_data == 00) {
1781 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to handle ftyp data\n");
1785 l_current_data_ptr = l_ftyp_data;
1787 opj_write_bytes(l_current_data_ptr, l_ftyp_size, 4); /* box size */
1788 l_current_data_ptr += 4;
1790 opj_write_bytes(l_current_data_ptr, JP2_FTYP, 4); /* FTYP */
1791 l_current_data_ptr += 4;
1793 opj_write_bytes(l_current_data_ptr, jp2->brand, 4); /* BR */
1794 l_current_data_ptr += 4;
1796 opj_write_bytes(l_current_data_ptr, jp2->minversion, 4); /* MinV */
1797 l_current_data_ptr += 4;
1799 for (i = 0; i < jp2->numcl; i++) {
1800 opj_write_bytes(l_current_data_ptr, jp2->cl[i], 4); /* CL */
1803 l_result = (opj_stream_write_data(cio, l_ftyp_data, l_ftyp_size,
1804 p_manager) == l_ftyp_size);
1806 opj_event_msg(p_manager, EVT_ERROR,
1807 "Error while writing ftyp data to stream\n");
1810 opj_free(l_ftyp_data);
1815 static OPJ_BOOL opj_jp2_write_jp2c(opj_jp2_t *jp2,
1816 opj_stream_private_t *cio,
1817 opj_event_mgr_t * p_manager)
1819 OPJ_OFF_T j2k_codestream_exit;
1820 OPJ_BYTE l_data_header [8];
1825 assert(p_manager != 00);
1826 assert(opj_stream_has_seek(cio));
1828 j2k_codestream_exit = opj_stream_tell(cio);
1829 opj_write_bytes(l_data_header,
1830 (OPJ_UINT32)(j2k_codestream_exit - jp2->j2k_codestream_offset),
1831 4); /* size of codestream */
1832 opj_write_bytes(l_data_header + 4, JP2_JP2C,
1835 if (! opj_stream_seek(cio, jp2->j2k_codestream_offset, p_manager)) {
1836 opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
1840 if (opj_stream_write_data(cio, l_data_header, 8, p_manager) != 8) {
1841 opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
1845 if (! opj_stream_seek(cio, j2k_codestream_exit, p_manager)) {
1846 opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
1853 static OPJ_BOOL opj_jp2_write_jp(opj_jp2_t *jp2,
1854 opj_stream_private_t *cio,
1855 opj_event_mgr_t * p_manager)
1857 /* 12 bytes will be read */
1858 OPJ_BYTE l_signature_data [12];
1863 assert(p_manager != 00);
1867 /* write box length */
1868 opj_write_bytes(l_signature_data, 12, 4);
1869 /* writes box type */
1870 opj_write_bytes(l_signature_data + 4, JP2_JP, 4);
1871 /* writes magic number*/
1872 opj_write_bytes(l_signature_data + 8, 0x0d0a870a, 4);
1874 if (opj_stream_write_data(cio, l_signature_data, 12, p_manager) != 12) {
1881 /* ----------------------------------------------------------------------- */
1882 /* JP2 decoder interface */
1883 /* ----------------------------------------------------------------------- */
1885 void opj_jp2_setup_decoder(opj_jp2_t *jp2, opj_dparameters_t *parameters)
1887 /* setup the J2K codec */
1888 opj_j2k_setup_decoder(jp2->j2k, parameters);
1890 /* further JP2 initializations go here */
1891 jp2->color.jp2_has_colr = 0;
1892 jp2->ignore_pclr_cmap_cdef = parameters->flags &
1893 OPJ_DPARAMETERS_IGNORE_PCLR_CMAP_CDEF_FLAG;
1896 OPJ_BOOL opj_jp2_set_threads(opj_jp2_t *jp2, OPJ_UINT32 num_threads)
1898 return opj_j2k_set_threads(jp2->j2k, num_threads);
1901 /* ----------------------------------------------------------------------- */
1902 /* JP2 encoder interface */
1903 /* ----------------------------------------------------------------------- */
1905 OPJ_BOOL opj_jp2_setup_encoder(opj_jp2_t *jp2,
1906 opj_cparameters_t *parameters,
1908 opj_event_mgr_t * p_manager)
1913 OPJ_UINT32 alpha_count;
1914 OPJ_UINT32 color_channels = 0U;
1915 OPJ_UINT32 alpha_channel = 0U;
1918 if (!jp2 || !parameters || !image) {
1922 /* setup the J2K codec */
1923 /* ------------------- */
1925 /* Check if number of components respects standard */
1926 if (image->numcomps < 1 || image->numcomps > 16384) {
1927 opj_event_msg(p_manager, EVT_ERROR,
1928 "Invalid number of components specified while setting up JP2 encoder\n");
1932 if (opj_j2k_setup_encoder(jp2->j2k, parameters, image,
1933 p_manager) == OPJ_FALSE) {
1937 /* setup the JP2 codec */
1938 /* ------------------- */
1942 jp2->brand = JP2_JP2; /* BR */
1943 jp2->minversion = 0; /* MinV */
1945 jp2->cl = (OPJ_UINT32*) opj_malloc(jp2->numcl * sizeof(OPJ_UINT32));
1947 opj_event_msg(p_manager, EVT_ERROR,
1948 "Not enough memory when setup the JP2 encoder\n");
1951 jp2->cl[0] = JP2_JP2; /* CL0 : JP2 */
1953 /* Image Header box */
1955 jp2->numcomps = image->numcomps; /* NC */
1956 jp2->comps = (opj_jp2_comps_t*) opj_malloc(jp2->numcomps * sizeof(
1959 opj_event_msg(p_manager, EVT_ERROR,
1960 "Not enough memory when setup the JP2 encoder\n");
1961 /* Memory of jp2->cl will be freed by opj_jp2_destroy */
1965 jp2->h = image->y1 - image->y0; /* HEIGHT */
1966 jp2->w = image->x1 - image->x0; /* WIDTH */
1968 depth_0 = image->comps[0].prec - 1;
1969 sign = image->comps[0].sgnd;
1970 jp2->bpc = depth_0 + (sign << 7);
1971 for (i = 1; i < image->numcomps; i++) {
1972 OPJ_UINT32 depth = image->comps[i].prec - 1;
1973 sign = image->comps[i].sgnd;
1974 if (depth_0 != depth) {
1978 jp2->C = 7; /* C : Always 7 */
1979 jp2->UnkC = 0; /* UnkC, colorspace specified in colr box */
1980 jp2->IPR = 0; /* IPR, no intellectual property */
1982 /* BitsPerComponent box */
1983 for (i = 0; i < image->numcomps; i++) {
1984 jp2->comps[i].bpcc = image->comps[i].prec - 1 + (image->comps[i].sgnd << 7);
1987 /* Colour Specification box */
1988 if (image->icc_profile_len) {
1993 if (image->color_space == 1) {
1994 jp2->enumcs = 16; /* sRGB as defined by IEC 61966-2-1 */
1995 } else if (image->color_space == 2) {
1996 jp2->enumcs = 17; /* greyscale */
1997 } else if (image->color_space == 3) {
1998 jp2->enumcs = 18; /* YUV */
2002 /* Channel Definition box */
2003 /* FIXME not provided by parameters */
2004 /* We try to do what we can... */
2006 for (i = 0; i < image->numcomps; i++) {
2007 if (image->comps[i].alpha != 0) {
2012 if (alpha_count == 1U) { /* no way to deal with more than 1 alpha channel */
2013 switch (jp2->enumcs) {
2025 if (alpha_count == 0U) {
2026 opj_event_msg(p_manager, EVT_WARNING,
2027 "Alpha channel specified but unknown enumcs. No cdef box will be created.\n");
2028 } else if (image->numcomps < (color_channels + 1)) {
2029 opj_event_msg(p_manager, EVT_WARNING,
2030 "Alpha channel specified but not enough image components for an automatic cdef box creation.\n");
2032 } else if ((OPJ_UINT32)alpha_channel < color_channels) {
2033 opj_event_msg(p_manager, EVT_WARNING,
2034 "Alpha channel position conflicts with color channel. No cdef box will be created.\n");
2037 } else if (alpha_count > 1) {
2038 opj_event_msg(p_manager, EVT_WARNING,
2039 "Multiple alpha channels specified. No cdef box will be created.\n");
2041 if (alpha_count == 1U) { /* if here, we know what we can do */
2042 jp2->color.jp2_cdef = (opj_jp2_cdef_t*)opj_malloc(sizeof(opj_jp2_cdef_t));
2043 if (!jp2->color.jp2_cdef) {
2044 opj_event_msg(p_manager, EVT_ERROR,
2045 "Not enough memory to setup the JP2 encoder\n");
2048 /* no memset needed, all values will be overwritten except if jp2->color.jp2_cdef->info allocation fails, */
2049 /* in which case jp2->color.jp2_cdef->info will be NULL => valid for destruction */
2050 jp2->color.jp2_cdef->info = (opj_jp2_cdef_info_t*) opj_malloc(
2051 image->numcomps * sizeof(opj_jp2_cdef_info_t));
2052 if (!jp2->color.jp2_cdef->info) {
2053 /* memory will be freed by opj_jp2_destroy */
2054 opj_event_msg(p_manager, EVT_ERROR,
2055 "Not enough memory to setup the JP2 encoder\n");
2058 jp2->color.jp2_cdef->n = (OPJ_UINT16)
2059 image->numcomps; /* cast is valid : image->numcomps [1,16384] */
2060 for (i = 0U; i < color_channels; i++) {
2061 jp2->color.jp2_cdef->info[i].cn = (OPJ_UINT16)
2062 i; /* cast is valid : image->numcomps [1,16384] */
2063 jp2->color.jp2_cdef->info[i].typ = 0U;
2064 jp2->color.jp2_cdef->info[i].asoc = (OPJ_UINT16)(i +
2065 1U); /* No overflow + cast is valid : image->numcomps [1,16384] */
2067 for (; i < image->numcomps; i++) {
2068 if (image->comps[i].alpha != 0) { /* we'll be here exactly once */
2069 jp2->color.jp2_cdef->info[i].cn = (OPJ_UINT16)
2070 i; /* cast is valid : image->numcomps [1,16384] */
2071 jp2->color.jp2_cdef->info[i].typ = 1U; /* Opacity channel */
2072 jp2->color.jp2_cdef->info[i].asoc =
2073 0U; /* Apply alpha channel to the whole image */
2075 /* Unknown channel */
2076 jp2->color.jp2_cdef->info[i].cn = (OPJ_UINT16)
2077 i; /* cast is valid : image->numcomps [1,16384] */
2078 jp2->color.jp2_cdef->info[i].typ = 65535U;
2079 jp2->color.jp2_cdef->info[i].asoc = 65535U;
2084 jp2->precedence = 0; /* PRECEDENCE */
2085 jp2->approx = 0; /* APPROX */
2087 jp2->jpip_on = parameters->jpip_on;
2092 OPJ_BOOL opj_jp2_encode(opj_jp2_t *jp2,
2093 opj_stream_private_t *stream,
2094 opj_event_mgr_t * p_manager)
2096 return opj_j2k_encode(jp2->j2k, stream, p_manager);
2099 OPJ_BOOL opj_jp2_end_decompress(opj_jp2_t *jp2,
2100 opj_stream_private_t *cio,
2101 opj_event_mgr_t * p_manager
2107 assert(p_manager != 00);
2109 /* customization of the end encoding */
2110 if (! opj_jp2_setup_end_header_reading(jp2, p_manager)) {
2115 if (! opj_jp2_exec(jp2, jp2->m_procedure_list, cio, p_manager)) {
2119 return opj_j2k_end_decompress(jp2->j2k, cio, p_manager);
2122 OPJ_BOOL opj_jp2_end_compress(opj_jp2_t *jp2,
2123 opj_stream_private_t *cio,
2124 opj_event_mgr_t * p_manager
2130 assert(p_manager != 00);
2132 /* customization of the end encoding */
2133 if (! opj_jp2_setup_end_header_writing(jp2, p_manager)) {
2137 if (! opj_j2k_end_compress(jp2->j2k, cio, p_manager)) {
2142 return opj_jp2_exec(jp2, jp2->m_procedure_list, cio, p_manager);
2145 static OPJ_BOOL opj_jp2_setup_end_header_writing(opj_jp2_t *jp2,
2146 opj_event_mgr_t * p_manager)
2150 assert(p_manager != 00);
2154 if (! opj_procedure_list_add_procedure(jp2->m_procedure_list,
2155 (opj_procedure)opj_jpip_write_iptr, p_manager)) {
2160 if (! opj_procedure_list_add_procedure(jp2->m_procedure_list,
2161 (opj_procedure)opj_jp2_write_jp2c, p_manager)) {
2164 /* DEVELOPER CORNER, add your custom procedures */
2167 if (! opj_procedure_list_add_procedure(jp2->m_procedure_list,
2168 (opj_procedure)opj_jpip_write_cidx, p_manager)) {
2171 if (! opj_procedure_list_add_procedure(jp2->m_procedure_list,
2172 (opj_procedure)opj_jpip_write_fidx, p_manager)) {
2180 static OPJ_BOOL opj_jp2_setup_end_header_reading(opj_jp2_t *jp2,
2181 opj_event_mgr_t * p_manager)
2185 assert(p_manager != 00);
2187 if (! opj_procedure_list_add_procedure(jp2->m_procedure_list,
2188 (opj_procedure)opj_jp2_read_header_procedure, p_manager)) {
2191 /* DEVELOPER CORNER, add your custom procedures */
2196 static OPJ_BOOL opj_jp2_default_validation(opj_jp2_t * jp2,
2197 opj_stream_private_t *cio,
2198 opj_event_mgr_t * p_manager
2201 OPJ_BOOL l_is_valid = OPJ_TRUE;
2207 assert(p_manager != 00);
2209 OPJ_UNUSED(p_manager);
2211 /* JPEG2000 codec validation */
2213 /* STATE checking */
2214 /* make sure the state is at 0 */
2215 l_is_valid &= (jp2->jp2_state == JP2_STATE_NONE);
2217 /* make sure not reading a jp2h ???? WEIRD */
2218 l_is_valid &= (jp2->jp2_img_state == JP2_IMG_STATE_NONE);
2220 /* POINTER validation */
2221 /* make sure a j2k codec is present */
2222 l_is_valid &= (jp2->j2k != 00);
2224 /* make sure a procedure list is present */
2225 l_is_valid &= (jp2->m_procedure_list != 00);
2227 /* make sure a validation list is present */
2228 l_is_valid &= (jp2->m_validation_list != 00);
2230 /* PARAMETER VALIDATION */
2231 /* number of components */
2232 l_is_valid &= (jp2->numcl > 0);
2234 l_is_valid &= (jp2->h > 0);
2236 l_is_valid &= (jp2->w > 0);
2238 for (i = 0; i < jp2->numcomps; ++i) {
2239 l_is_valid &= ((jp2->comps[i].bpcc & 0x7FU) <
2240 38U); /* 0 is valid, ignore sign for check */
2244 l_is_valid &= ((jp2->meth > 0) && (jp2->meth < 3));
2246 /* stream validation */
2247 /* back and forth is needed */
2248 l_is_valid &= opj_stream_has_seek(cio);
2253 static OPJ_BOOL opj_jp2_read_header_procedure(opj_jp2_t *jp2,
2254 opj_stream_private_t *stream,
2255 opj_event_mgr_t * p_manager
2259 OPJ_UINT32 l_nb_bytes_read;
2260 const opj_jp2_header_handler_t * l_current_handler;
2261 const opj_jp2_header_handler_t * l_current_handler_misplaced;
2262 OPJ_UINT32 l_last_data_size = OPJ_BOX_SIZE;
2263 OPJ_UINT32 l_current_data_size;
2264 OPJ_BYTE * l_current_data = 00;
2267 assert(stream != 00);
2269 assert(p_manager != 00);
2271 l_current_data = (OPJ_BYTE*)opj_calloc(1, l_last_data_size);
2273 if (l_current_data == 00) {
2274 opj_event_msg(p_manager, EVT_ERROR,
2275 "Not enough memory to handle jpeg2000 file header\n");
2279 while (opj_jp2_read_boxhdr(&box, &l_nb_bytes_read, stream, p_manager)) {
2280 /* is it the codestream box ? */
2281 if (box.type == JP2_JP2C) {
2282 if (jp2->jp2_state & JP2_STATE_HEADER) {
2283 jp2->jp2_state |= JP2_STATE_CODESTREAM;
2284 opj_free(l_current_data);
2287 opj_event_msg(p_manager, EVT_ERROR, "bad placed jpeg codestream\n");
2288 opj_free(l_current_data);
2291 } else if (box.length == 0) {
2292 opj_event_msg(p_manager, EVT_ERROR, "Cannot handle box of undefined sizes\n");
2293 opj_free(l_current_data);
2296 /* testcase 1851.pdf.SIGSEGV.ce9.948 */
2297 else if (box.length < l_nb_bytes_read) {
2298 opj_event_msg(p_manager, EVT_ERROR, "invalid box size %d (%x)\n", box.length,
2300 opj_free(l_current_data);
2304 l_current_handler = opj_jp2_find_handler(box.type);
2305 l_current_handler_misplaced = opj_jp2_img_find_handler(box.type);
2306 l_current_data_size = box.length - l_nb_bytes_read;
2308 if ((l_current_handler != 00) || (l_current_handler_misplaced != 00)) {
2309 if (l_current_handler == 00) {
2310 opj_event_msg(p_manager, EVT_WARNING,
2311 "Found a misplaced '%c%c%c%c' box outside jp2h box\n",
2312 (OPJ_BYTE)(box.type >> 24), (OPJ_BYTE)(box.type >> 16),
2313 (OPJ_BYTE)(box.type >> 8), (OPJ_BYTE)(box.type >> 0));
2314 if (jp2->jp2_state & JP2_STATE_HEADER) {
2315 /* read anyway, we already have jp2h */
2316 l_current_handler = l_current_handler_misplaced;
2318 opj_event_msg(p_manager, EVT_WARNING,
2319 "JPEG2000 Header box not read yet, '%c%c%c%c' box will be ignored\n",
2320 (OPJ_BYTE)(box.type >> 24), (OPJ_BYTE)(box.type >> 16),
2321 (OPJ_BYTE)(box.type >> 8), (OPJ_BYTE)(box.type >> 0));
2322 jp2->jp2_state |= JP2_STATE_UNKNOWN;
2323 if (opj_stream_skip(stream, l_current_data_size,
2324 p_manager) != l_current_data_size) {
2325 opj_event_msg(p_manager, EVT_ERROR,
2326 "Problem with skipping JPEG2000 box, stream error\n");
2327 opj_free(l_current_data);
2333 if ((OPJ_OFF_T)l_current_data_size > opj_stream_get_number_byte_left(stream)) {
2334 /* do not even try to malloc if we can't read */
2335 opj_event_msg(p_manager, EVT_ERROR,
2336 "Invalid box size %d for box '%c%c%c%c'. Need %d bytes, %d bytes remaining \n",
2337 box.length, (OPJ_BYTE)(box.type >> 24), (OPJ_BYTE)(box.type >> 16),
2338 (OPJ_BYTE)(box.type >> 8), (OPJ_BYTE)(box.type >> 0), l_current_data_size,
2339 (OPJ_UINT32)opj_stream_get_number_byte_left(stream));
2340 opj_free(l_current_data);
2343 if (l_current_data_size > l_last_data_size) {
2344 OPJ_BYTE* new_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,
2345 l_current_data_size);
2346 if (!new_current_data) {
2347 opj_free(l_current_data);
2348 opj_event_msg(p_manager, EVT_ERROR,
2349 "Not enough memory to handle jpeg2000 box\n");
2352 l_current_data = new_current_data;
2353 l_last_data_size = l_current_data_size;
2356 l_nb_bytes_read = (OPJ_UINT32)opj_stream_read_data(stream, l_current_data,
2357 l_current_data_size, p_manager);
2358 if (l_nb_bytes_read != l_current_data_size) {
2359 opj_event_msg(p_manager, EVT_ERROR,
2360 "Problem with reading JPEG2000 box, stream error\n");
2361 opj_free(l_current_data);
2365 if (! l_current_handler->handler(jp2, l_current_data, l_current_data_size,
2367 opj_free(l_current_data);
2371 if (!(jp2->jp2_state & JP2_STATE_SIGNATURE)) {
2372 opj_event_msg(p_manager, EVT_ERROR,
2373 "Malformed JP2 file format: first box must be JPEG 2000 signature box\n");
2374 opj_free(l_current_data);
2377 if (!(jp2->jp2_state & JP2_STATE_FILE_TYPE)) {
2378 opj_event_msg(p_manager, EVT_ERROR,
2379 "Malformed JP2 file format: second box must be file type box\n");
2380 opj_free(l_current_data);
2383 jp2->jp2_state |= JP2_STATE_UNKNOWN;
2384 if (opj_stream_skip(stream, l_current_data_size,
2385 p_manager) != l_current_data_size) {
2386 if (jp2->jp2_state & JP2_STATE_CODESTREAM) {
2387 /* If we already read the codestream, do not error out */
2388 /* Needed for data/input/nonregression/issue254.jp2 */
2389 opj_event_msg(p_manager, EVT_WARNING,
2390 "Problem with skipping JPEG2000 box, stream error\n");
2391 opj_free(l_current_data);
2394 opj_event_msg(p_manager, EVT_ERROR,
2395 "Problem with skipping JPEG2000 box, stream error\n");
2396 opj_free(l_current_data);
2403 opj_free(l_current_data);
2409 * Executes the given procedures on the given codec.
2411 * @param p_procedure_list the list of procedures to execute
2412 * @param jp2 the jpeg2000 file codec to execute the procedures on.
2413 * @param stream the stream to execute the procedures on.
2414 * @param p_manager the user manager.
2416 * @return true if all the procedures were successfully executed.
2418 static OPJ_BOOL opj_jp2_exec(opj_jp2_t * jp2,
2419 opj_procedure_list_t * p_procedure_list,
2420 opj_stream_private_t *stream,
2421 opj_event_mgr_t * p_manager
2425 OPJ_BOOL(** l_procedure)(opj_jp2_t * jp2, opj_stream_private_t *,
2426 opj_event_mgr_t *) = 00;
2427 OPJ_BOOL l_result = OPJ_TRUE;
2428 OPJ_UINT32 l_nb_proc, i;
2431 assert(p_procedure_list != 00);
2433 assert(stream != 00);
2434 assert(p_manager != 00);
2436 l_nb_proc = opj_procedure_list_get_nb_procedures(p_procedure_list);
2437 l_procedure = (OPJ_BOOL(**)(opj_jp2_t * jp2, opj_stream_private_t *,
2438 opj_event_mgr_t *)) opj_procedure_list_get_first_procedure(p_procedure_list);
2440 for (i = 0; i < l_nb_proc; ++i) {
2441 l_result = l_result && (*l_procedure)(jp2, stream, p_manager);
2445 /* and clear the procedure list at the end. */
2446 opj_procedure_list_clear(p_procedure_list);
2450 OPJ_BOOL opj_jp2_start_compress(opj_jp2_t *jp2,
2451 opj_stream_private_t *stream,
2452 opj_image_t * p_image,
2453 opj_event_mgr_t * p_manager
2458 assert(stream != 00);
2459 assert(p_manager != 00);
2461 /* customization of the validation */
2462 if (! opj_jp2_setup_encoding_validation(jp2, p_manager)) {
2466 /* validation of the parameters codec */
2467 if (! opj_jp2_exec(jp2, jp2->m_validation_list, stream, p_manager)) {
2471 /* customization of the encoding */
2472 if (! opj_jp2_setup_header_writing(jp2, p_manager)) {
2477 if (! opj_jp2_exec(jp2, jp2->m_procedure_list, stream, p_manager)) {
2481 return opj_j2k_start_compress(jp2->j2k, stream, p_image, p_manager);
2484 static const opj_jp2_header_handler_t * opj_jp2_find_handler(OPJ_UINT32 p_id)
2486 OPJ_UINT32 i, l_handler_size = sizeof(jp2_header) / sizeof(
2487 opj_jp2_header_handler_t);
2489 for (i = 0; i < l_handler_size; ++i) {
2490 if (jp2_header[i].id == p_id) {
2491 return &jp2_header[i];
2498 * Finds the image execution function related to the given box id.
2500 * @param p_id the id of the handler to fetch.
2502 * @return the given handler or 00 if it could not be found.
2504 static const opj_jp2_header_handler_t * opj_jp2_img_find_handler(
2507 OPJ_UINT32 i, l_handler_size = sizeof(jp2_img_header) / sizeof(
2508 opj_jp2_header_handler_t);
2509 for (i = 0; i < l_handler_size; ++i) {
2510 if (jp2_img_header[i].id == p_id) {
2511 return &jp2_img_header[i];
2519 * Reads a jpeg2000 file signature box.
2521 * @param p_header_data the data contained in the signature box.
2522 * @param jp2 the jpeg2000 file codec.
2523 * @param p_header_size the size of the data contained in the signature box.
2524 * @param p_manager the user event manager.
2526 * @return true if the file signature box is valid.
2528 static OPJ_BOOL opj_jp2_read_jp(opj_jp2_t *jp2,
2529 OPJ_BYTE * p_header_data,
2530 OPJ_UINT32 p_header_size,
2531 opj_event_mgr_t * p_manager
2535 OPJ_UINT32 l_magic_number;
2538 assert(p_header_data != 00);
2540 assert(p_manager != 00);
2542 if (jp2->jp2_state != JP2_STATE_NONE) {
2543 opj_event_msg(p_manager, EVT_ERROR,
2544 "The signature box must be the first box in the file.\n");
2548 /* assure length of data is correct (4 -> magic number) */
2549 if (p_header_size != 4) {
2550 opj_event_msg(p_manager, EVT_ERROR, "Error with JP signature Box size\n");
2554 /* rearrange data */
2555 opj_read_bytes(p_header_data, &l_magic_number, 4);
2556 if (l_magic_number != 0x0d0a870a) {
2557 opj_event_msg(p_manager, EVT_ERROR,
2558 "Error with JP Signature : bad magic number\n");
2562 jp2->jp2_state |= JP2_STATE_SIGNATURE;
2568 * Reads a a FTYP box - File type box
2570 * @param p_header_data the data contained in the FTYP box.
2571 * @param jp2 the jpeg2000 file codec.
2572 * @param p_header_size the size of the data contained in the FTYP box.
2573 * @param p_manager the user event manager.
2575 * @return true if the FTYP box is valid.
2577 static OPJ_BOOL opj_jp2_read_ftyp(opj_jp2_t *jp2,
2578 OPJ_BYTE * p_header_data,
2579 OPJ_UINT32 p_header_size,
2580 opj_event_mgr_t * p_manager
2583 OPJ_UINT32 i, l_remaining_bytes;
2586 assert(p_header_data != 00);
2588 assert(p_manager != 00);
2590 if (jp2->jp2_state != JP2_STATE_SIGNATURE) {
2591 opj_event_msg(p_manager, EVT_ERROR,
2592 "The ftyp box must be the second box in the file.\n");
2596 /* assure length of data is correct */
2597 if (p_header_size < 8) {
2598 opj_event_msg(p_manager, EVT_ERROR, "Error with FTYP signature Box size\n");
2602 opj_read_bytes(p_header_data, &jp2->brand, 4); /* BR */
2605 opj_read_bytes(p_header_data, &jp2->minversion, 4); /* MinV */
2608 l_remaining_bytes = p_header_size - 8;
2610 /* the number of remaining bytes should be a multiple of 4 */
2611 if ((l_remaining_bytes & 0x3) != 0) {
2612 opj_event_msg(p_manager, EVT_ERROR, "Error with FTYP signature Box size\n");
2617 jp2->numcl = l_remaining_bytes >> 2;
2619 jp2->cl = (OPJ_UINT32 *) opj_calloc(jp2->numcl, sizeof(OPJ_UINT32));
2620 if (jp2->cl == 00) {
2621 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory with FTYP Box\n");
2626 for (i = 0; i < jp2->numcl; ++i) {
2627 opj_read_bytes(p_header_data, &jp2->cl[i], 4); /* CLi */
2631 jp2->jp2_state |= JP2_STATE_FILE_TYPE;
2636 static OPJ_BOOL opj_jp2_skip_jp2c(opj_jp2_t *jp2,
2637 opj_stream_private_t *stream,
2638 opj_event_mgr_t * p_manager)
2642 assert(stream != 00);
2643 assert(p_manager != 00);
2645 jp2->j2k_codestream_offset = opj_stream_tell(stream);
2647 if (opj_stream_skip(stream, 8, p_manager) != 8) {
2654 static OPJ_BOOL opj_jpip_skip_iptr(opj_jp2_t *jp2,
2655 opj_stream_private_t *stream,
2656 opj_event_mgr_t * p_manager)
2660 assert(stream != 00);
2661 assert(p_manager != 00);
2663 jp2->jpip_iptr_offset = opj_stream_tell(stream);
2665 if (opj_stream_skip(stream, 24, p_manager) != 24) {
2673 * Reads the Jpeg2000 file Header box - JP2 Header box (warning, this is a super box).
2675 * @param p_header_data the data contained in the file header box.
2676 * @param jp2 the jpeg2000 file codec.
2677 * @param p_header_size the size of the data contained in the file header box.
2678 * @param p_manager the user event manager.
2680 * @return true if the JP2 Header box was successfully recognized.
2682 static OPJ_BOOL opj_jp2_read_jp2h(opj_jp2_t *jp2,
2683 OPJ_BYTE *p_header_data,
2684 OPJ_UINT32 p_header_size,
2685 opj_event_mgr_t * p_manager
2688 OPJ_UINT32 l_box_size = 0, l_current_data_size = 0;
2690 const opj_jp2_header_handler_t * l_current_handler;
2691 OPJ_BOOL l_has_ihdr = 0;
2694 assert(p_header_data != 00);
2696 assert(p_manager != 00);
2698 /* make sure the box is well placed */
2699 if ((jp2->jp2_state & JP2_STATE_FILE_TYPE) != JP2_STATE_FILE_TYPE) {
2700 opj_event_msg(p_manager, EVT_ERROR,
2701 "The box must be the first box in the file.\n");
2705 jp2->jp2_img_state = JP2_IMG_STATE_NONE;
2707 /* iterate while remaining data */
2708 while (p_header_size > 0) {
2710 if (! opj_jp2_read_boxhdr_char(&box, p_header_data, &l_box_size, p_header_size,
2712 opj_event_msg(p_manager, EVT_ERROR,
2713 "Stream error while reading JP2 Header box\n");
2717 if (box.length > p_header_size) {
2718 opj_event_msg(p_manager, EVT_ERROR,
2719 "Stream error while reading JP2 Header box: box length is inconsistent.\n");
2723 l_current_handler = opj_jp2_img_find_handler(box.type);
2724 l_current_data_size = box.length - l_box_size;
2725 p_header_data += l_box_size;
2727 if (l_current_handler != 00) {
2728 if (! l_current_handler->handler(jp2, p_header_data, l_current_data_size,
2733 jp2->jp2_img_state |= JP2_IMG_STATE_UNKNOWN;
2736 if (box.type == JP2_IHDR) {
2740 p_header_data += l_current_data_size;
2741 p_header_size -= box.length;
2744 if (l_has_ihdr == 0) {
2745 opj_event_msg(p_manager, EVT_ERROR,
2746 "Stream error while reading JP2 Header box: no 'ihdr' box.\n");
2750 jp2->jp2_state |= JP2_STATE_HEADER;
2756 static OPJ_BOOL opj_jp2_read_boxhdr_char(opj_jp2_box_t *box,
2758 OPJ_UINT32 * p_number_bytes_read,
2759 OPJ_UINT32 p_box_max_size,
2760 opj_event_mgr_t * p_manager
2766 assert(p_data != 00);
2768 assert(p_number_bytes_read != 00);
2769 assert(p_manager != 00);
2771 if (p_box_max_size < 8) {
2772 opj_event_msg(p_manager, EVT_ERROR, "Cannot handle box of less than 8 bytes\n");
2776 /* process read data */
2777 opj_read_bytes(p_data, &l_value, 4);
2779 box->length = (OPJ_UINT32)(l_value);
2781 opj_read_bytes(p_data, &l_value, 4);
2783 box->type = (OPJ_UINT32)(l_value);
2785 *p_number_bytes_read = 8;
2787 /* do we have a "special very large box ?" */
2788 /* read then the XLBox */
2789 if (box->length == 1) {
2790 OPJ_UINT32 l_xl_part_size;
2792 if (p_box_max_size < 16) {
2793 opj_event_msg(p_manager, EVT_ERROR,
2794 "Cannot handle XL box of less than 16 bytes\n");
2798 opj_read_bytes(p_data, &l_xl_part_size, 4);
2800 *p_number_bytes_read += 4;
2802 if (l_xl_part_size != 0) {
2803 opj_event_msg(p_manager, EVT_ERROR,
2804 "Cannot handle box sizes higher than 2^32\n");
2808 opj_read_bytes(p_data, &l_value, 4);
2809 *p_number_bytes_read += 4;
2810 box->length = (OPJ_UINT32)(l_value);
2812 if (box->length == 0) {
2813 opj_event_msg(p_manager, EVT_ERROR, "Cannot handle box of undefined sizes\n");
2816 } else if (box->length == 0) {
2817 opj_event_msg(p_manager, EVT_ERROR, "Cannot handle box of undefined sizes\n");
2820 if (box->length < *p_number_bytes_read) {
2821 opj_event_msg(p_manager, EVT_ERROR, "Box length is inconsistent.\n");
2827 OPJ_BOOL opj_jp2_read_header(opj_stream_private_t *p_stream,
2829 opj_image_t ** p_image,
2830 opj_event_mgr_t * p_manager
2835 assert(p_stream != 00);
2836 assert(p_manager != 00);
2838 /* customization of the validation */
2839 if (! opj_jp2_setup_decoding_validation(jp2, p_manager)) {
2843 /* customization of the encoding */
2844 if (! opj_jp2_setup_header_reading(jp2, p_manager)) {
2848 /* validation of the parameters codec */
2849 if (! opj_jp2_exec(jp2, jp2->m_validation_list, p_stream, p_manager)) {
2854 if (! opj_jp2_exec(jp2, jp2->m_procedure_list, p_stream, p_manager)) {
2857 if (jp2->has_jp2h == 0) {
2858 opj_event_msg(p_manager, EVT_ERROR, "JP2H box missing. Required.\n");
2861 if (jp2->has_ihdr == 0) {
2862 opj_event_msg(p_manager, EVT_ERROR, "IHDR box_missing. Required.\n");
2866 return opj_j2k_read_header(p_stream,
2872 static OPJ_BOOL opj_jp2_setup_encoding_validation(opj_jp2_t *jp2,
2873 opj_event_mgr_t * p_manager)
2877 assert(p_manager != 00);
2879 if (! opj_procedure_list_add_procedure(jp2->m_validation_list,
2880 (opj_procedure)opj_jp2_default_validation, p_manager)) {
2883 /* DEVELOPER CORNER, add your custom validation procedure */
2888 static OPJ_BOOL opj_jp2_setup_decoding_validation(opj_jp2_t *jp2,
2889 opj_event_mgr_t * p_manager)
2893 assert(p_manager != 00);
2896 OPJ_UNUSED(p_manager);
2898 /* DEVELOPER CORNER, add your custom validation procedure */
2903 static OPJ_BOOL opj_jp2_setup_header_writing(opj_jp2_t *jp2,
2904 opj_event_mgr_t * p_manager)
2908 assert(p_manager != 00);
2910 if (! opj_procedure_list_add_procedure(jp2->m_procedure_list,
2911 (opj_procedure)opj_jp2_write_jp, p_manager)) {
2914 if (! opj_procedure_list_add_procedure(jp2->m_procedure_list,
2915 (opj_procedure)opj_jp2_write_ftyp, p_manager)) {
2918 if (! opj_procedure_list_add_procedure(jp2->m_procedure_list,
2919 (opj_procedure)opj_jp2_write_jp2h, p_manager)) {
2923 if (! opj_procedure_list_add_procedure(jp2->m_procedure_list,
2924 (opj_procedure)opj_jpip_skip_iptr, p_manager)) {
2928 if (! opj_procedure_list_add_procedure(jp2->m_procedure_list,
2929 (opj_procedure)opj_jp2_skip_jp2c, p_manager)) {
2933 /* DEVELOPER CORNER, insert your custom procedures */
2938 static OPJ_BOOL opj_jp2_setup_header_reading(opj_jp2_t *jp2,
2939 opj_event_mgr_t * p_manager)
2943 assert(p_manager != 00);
2945 if (! opj_procedure_list_add_procedure(jp2->m_procedure_list,
2946 (opj_procedure)opj_jp2_read_header_procedure, p_manager)) {
2950 /* DEVELOPER CORNER, add your custom procedures */
2955 OPJ_BOOL opj_jp2_read_tile_header(opj_jp2_t * p_jp2,
2956 OPJ_UINT32 * p_tile_index,
2957 OPJ_UINT32 * p_data_size,
2958 OPJ_INT32 * p_tile_x0,
2959 OPJ_INT32 * p_tile_y0,
2960 OPJ_INT32 * p_tile_x1,
2961 OPJ_INT32 * p_tile_y1,
2962 OPJ_UINT32 * p_nb_comps,
2964 opj_stream_private_t *p_stream,
2965 opj_event_mgr_t * p_manager
2968 return opj_j2k_read_tile_header(p_jp2->j2k,
2971 p_tile_x0, p_tile_y0,
2972 p_tile_x1, p_tile_y1,
2979 OPJ_BOOL opj_jp2_write_tile(opj_jp2_t *p_jp2,
2980 OPJ_UINT32 p_tile_index,
2982 OPJ_UINT32 p_data_size,
2983 opj_stream_private_t *p_stream,
2984 opj_event_mgr_t * p_manager
2988 return opj_j2k_write_tile(p_jp2->j2k, p_tile_index, p_data, p_data_size,
2989 p_stream, p_manager);
2992 OPJ_BOOL opj_jp2_decode_tile(opj_jp2_t * p_jp2,
2993 OPJ_UINT32 p_tile_index,
2995 OPJ_UINT32 p_data_size,
2996 opj_stream_private_t *p_stream,
2997 opj_event_mgr_t * p_manager
3000 return opj_j2k_decode_tile(p_jp2->j2k, p_tile_index, p_data, p_data_size,
3001 p_stream, p_manager);
3004 void opj_jp2_destroy(opj_jp2_t *jp2)
3007 /* destroy the J2K codec */
3008 opj_j2k_destroy(jp2->j2k);
3012 opj_free(jp2->comps);
3021 if (jp2->color.icc_profile_buf) {
3022 opj_free(jp2->color.icc_profile_buf);
3023 jp2->color.icc_profile_buf = 00;
3026 if (jp2->color.jp2_cdef) {
3027 if (jp2->color.jp2_cdef->info) {
3028 opj_free(jp2->color.jp2_cdef->info);
3029 jp2->color.jp2_cdef->info = NULL;
3032 opj_free(jp2->color.jp2_cdef);
3033 jp2->color.jp2_cdef = 00;
3036 if (jp2->color.jp2_pclr) {
3037 if (jp2->color.jp2_pclr->cmap) {
3038 opj_free(jp2->color.jp2_pclr->cmap);
3039 jp2->color.jp2_pclr->cmap = NULL;
3041 if (jp2->color.jp2_pclr->channel_sign) {
3042 opj_free(jp2->color.jp2_pclr->channel_sign);
3043 jp2->color.jp2_pclr->channel_sign = NULL;
3045 if (jp2->color.jp2_pclr->channel_size) {
3046 opj_free(jp2->color.jp2_pclr->channel_size);
3047 jp2->color.jp2_pclr->channel_size = NULL;
3049 if (jp2->color.jp2_pclr->entries) {
3050 opj_free(jp2->color.jp2_pclr->entries);
3051 jp2->color.jp2_pclr->entries = NULL;
3054 opj_free(jp2->color.jp2_pclr);
3055 jp2->color.jp2_pclr = 00;
3058 if (jp2->m_validation_list) {
3059 opj_procedure_list_destroy(jp2->m_validation_list);
3060 jp2->m_validation_list = 00;
3063 if (jp2->m_procedure_list) {
3064 opj_procedure_list_destroy(jp2->m_procedure_list);
3065 jp2->m_procedure_list = 00;
3072 OPJ_BOOL opj_jp2_set_decode_area(opj_jp2_t *p_jp2,
3073 opj_image_t* p_image,
3074 OPJ_INT32 p_start_x, OPJ_INT32 p_start_y,
3075 OPJ_INT32 p_end_x, OPJ_INT32 p_end_y,
3076 opj_event_mgr_t * p_manager
3079 return opj_j2k_set_decode_area(p_jp2->j2k, p_image, p_start_x, p_start_y,
3080 p_end_x, p_end_y, p_manager);
3083 OPJ_BOOL opj_jp2_get_tile(opj_jp2_t *p_jp2,
3084 opj_stream_private_t *p_stream,
3085 opj_image_t* p_image,
3086 opj_event_mgr_t * p_manager,
3087 OPJ_UINT32 tile_index
3094 opj_event_msg(p_manager, EVT_WARNING,
3095 "JP2 box which are after the codestream will not be read by this function.\n");
3097 if (! opj_j2k_get_tile(p_jp2->j2k, p_stream, p_image, p_manager, tile_index)) {
3098 opj_event_msg(p_manager, EVT_ERROR,
3099 "Failed to decode the codestream in the JP2 file\n");
3103 if (!opj_jp2_check_color(p_image, &(p_jp2->color), p_manager)) {
3107 /* Set Image Color Space */
3108 if (p_jp2->enumcs == 16) {
3109 p_image->color_space = OPJ_CLRSPC_SRGB;
3110 } else if (p_jp2->enumcs == 17) {
3111 p_image->color_space = OPJ_CLRSPC_GRAY;
3112 } else if (p_jp2->enumcs == 18) {
3113 p_image->color_space = OPJ_CLRSPC_SYCC;
3114 } else if (p_jp2->enumcs == 24) {
3115 p_image->color_space = OPJ_CLRSPC_EYCC;
3116 } else if (p_jp2->enumcs == 12) {
3117 p_image->color_space = OPJ_CLRSPC_CMYK;
3119 p_image->color_space = OPJ_CLRSPC_UNKNOWN;
3122 if (p_jp2->color.jp2_pclr) {
3123 /* Part 1, I.5.3.4: Either both or none : */
3124 if (!p_jp2->color.jp2_pclr->cmap) {
3125 opj_jp2_free_pclr(&(p_jp2->color));
3127 if (!opj_jp2_apply_pclr(p_image, &(p_jp2->color), p_manager)) {
3133 /* Apply the color space if needed */
3134 if (p_jp2->color.jp2_cdef) {
3135 opj_jp2_apply_cdef(p_image, &(p_jp2->color), p_manager);
3138 if (p_jp2->color.icc_profile_buf) {
3139 p_image->icc_profile_buf = p_jp2->color.icc_profile_buf;
3140 p_image->icc_profile_len = p_jp2->color.icc_profile_len;
3141 p_jp2->color.icc_profile_buf = NULL;
3147 /* ----------------------------------------------------------------------- */
3148 /* JP2 encoder interface */
3149 /* ----------------------------------------------------------------------- */
3151 opj_jp2_t* opj_jp2_create(OPJ_BOOL p_is_decoder)
3153 opj_jp2_t *jp2 = (opj_jp2_t*)opj_calloc(1, sizeof(opj_jp2_t));
3156 /* create the J2K codec */
3157 if (! p_is_decoder) {
3158 jp2->j2k = opj_j2k_create_compress();
3160 jp2->j2k = opj_j2k_create_decompress();
3163 if (jp2->j2k == 00) {
3164 opj_jp2_destroy(jp2);
3168 /* Color structure */
3169 jp2->color.icc_profile_buf = NULL;
3170 jp2->color.icc_profile_len = 0;
3171 jp2->color.jp2_cdef = NULL;
3172 jp2->color.jp2_pclr = NULL;
3173 jp2->color.jp2_has_colr = 0;
3175 /* validation list creation */
3176 jp2->m_validation_list = opj_procedure_list_create();
3177 if (! jp2->m_validation_list) {
3178 opj_jp2_destroy(jp2);
3182 /* execution list creation */
3183 jp2->m_procedure_list = opj_procedure_list_create();
3184 if (! jp2->m_procedure_list) {
3185 opj_jp2_destroy(jp2);
3193 void jp2_dump(opj_jp2_t* p_jp2, OPJ_INT32 flag, FILE* out_stream)
3196 assert(p_jp2 != 00);
3198 j2k_dump(p_jp2->j2k,
3203 opj_codestream_index_t* jp2_get_cstr_index(opj_jp2_t* p_jp2)
3205 return j2k_get_cstr_index(p_jp2->j2k);
3208 opj_codestream_info_v2_t* jp2_get_cstr_info(opj_jp2_t* p_jp2)
3210 return j2k_get_cstr_info(p_jp2->j2k);
3213 OPJ_BOOL opj_jp2_set_decoded_resolution_factor(opj_jp2_t *p_jp2,
3214 OPJ_UINT32 res_factor,
3215 opj_event_mgr_t * p_manager)
3217 return opj_j2k_set_decoded_resolution_factor(p_jp2->j2k, res_factor, p_manager);
3223 static OPJ_BOOL opj_jpip_write_iptr(opj_jp2_t *jp2,
3224 opj_stream_private_t *cio,
3225 opj_event_mgr_t * p_manager)
3227 OPJ_OFF_T j2k_codestream_exit;
3228 OPJ_BYTE l_data_header [24];
3233 assert(p_manager != 00);
3234 assert(opj_stream_has_seek(cio));
3236 j2k_codestream_exit = opj_stream_tell(cio);
3237 opj_write_bytes(l_data_header, 24, 4); /* size of iptr */
3238 opj_write_bytes(l_data_header + 4, JPIP_IPTR,
3241 opj_write_bytes(l_data_header + 4 + 4, 0, 8); /* offset */
3242 opj_write_bytes(l_data_header + 8 + 8, 0, 8); /* length */
3244 opj_write_double(l_data_header + 4 + 4, 0); /* offset */
3245 opj_write_double(l_data_header + 8 + 8, 0); /* length */
3248 if (! opj_stream_seek(cio, jp2->jpip_iptr_offset, p_manager)) {
3249 opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
3253 if (opj_stream_write_data(cio, l_data_header, 24, p_manager) != 24) {
3254 opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
3258 if (! opj_stream_seek(cio, j2k_codestream_exit, p_manager)) {
3259 opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
3266 static OPJ_BOOL opj_jpip_write_fidx(opj_jp2_t *jp2,
3267 opj_stream_private_t *cio,
3268 opj_event_mgr_t * p_manager)
3270 OPJ_OFF_T j2k_codestream_exit;
3271 OPJ_BYTE l_data_header [24];
3278 assert(p_manager != 00);
3279 assert(opj_stream_has_seek(cio));
3281 opj_write_bytes(l_data_header, 24, 4); /* size of iptr */
3282 opj_write_bytes(l_data_header + 4, JPIP_FIDX,
3284 opj_write_double(l_data_header + 4 + 4, 0); /* offset */
3285 opj_write_double(l_data_header + 8 + 8, 0); /* length */
3287 if (opj_stream_write_data(cio, l_data_header, 24, p_manager) != 24) {
3288 opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
3292 j2k_codestream_exit = opj_stream_tell(cio);
3293 if (! opj_stream_seek(cio, j2k_codestream_exit, p_manager)) {
3294 opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
3301 static OPJ_BOOL opj_jpip_write_cidx(opj_jp2_t *jp2,
3302 opj_stream_private_t *cio,
3303 opj_event_mgr_t * p_manager)
3305 OPJ_OFF_T j2k_codestream_exit;
3306 OPJ_BYTE l_data_header [24];
3313 assert(p_manager != 00);
3314 assert(opj_stream_has_seek(cio));
3316 j2k_codestream_exit = opj_stream_tell(cio);
3317 opj_write_bytes(l_data_header, 24, 4); /* size of iptr */
3318 opj_write_bytes(l_data_header + 4, JPIP_CIDX,
3321 opj_write_bytes(l_data_header + 4 + 4, 0, 8); /* offset */
3322 opj_write_bytes(l_data_header + 8 + 8, 0, 8); /* length */
3324 opj_write_double(l_data_header + 4 + 4, 0); /* offset */
3325 opj_write_double(l_data_header + 8 + 8, 0); /* length */
3328 if (! opj_stream_seek(cio, j2k_codestream_exit, p_manager)) {
3329 opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
3333 if (opj_stream_write_data(cio, l_data_header, 24, p_manager) != 24) {
3334 opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
3338 j2k_codestream_exit = opj_stream_tell(cio);
3339 if (! opj_stream_seek(cio, j2k_codestream_exit, p_manager)) {
3340 opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
3348 static void write_prxy(int offset_jp2c, int length_jp2c, int offset_idx,
3349 int length_idx, opj_stream_private_t *cio,
3350 opj_event_mgr_t * p_manager)
3352 OPJ_BYTE l_data_header [8];
3353 OPJ_OFF_T len, lenp;
3355 lenp = opj_stream_tell(cio);
3356 opj_stream_skip(cio, 4, p_manager); /* L [at the end] */
3357 opj_write_bytes(l_data_header, JPIP_PRXY, 4); /* IPTR */
3358 opj_stream_write_data(cio, l_data_header, 4, p_manager);
3360 opj_write_bytes(l_data_header, offset_jp2c, 8); /* OOFF */
3361 opj_stream_write_data(cio, l_data_header, 8, p_manager);
3362 opj_write_bytes(l_data_header, length_jp2c, 4); /* OBH part 1 */
3363 opj_write_bytes(l_data_header + 4, JP2_JP2C, 4); /* OBH part 2 */
3364 opj_stream_write_data(cio, l_data_header, 8, p_manager);
3366 opj_write_bytes(l_data_header, 1, 1); /* NI */
3367 opj_stream_write_data(cio, l_data_header, 1, p_manager);
3369 opj_write_bytes(l_data_header, offset_idx, 8); /* IOFF */
3370 opj_stream_write_data(cio, l_data_header, 8, p_manager);
3371 opj_write_bytes(l_data_header, length_idx, 4); /* IBH part 1 */
3372 opj_write_bytes(l_data_header + 4, JPIP_CIDX, 4); /* IBH part 2 */
3373 opj_stream_write_data(cio, l_data_header, 8, p_manager);
3375 len = opj_stream_tell(cio) - lenp;
3376 opj_stream_skip(cio, lenp, p_manager);
3377 opj_write_bytes(l_data_header, len, 4); /* L */
3378 opj_stream_write_data(cio, l_data_header, 4, p_manager);
3379 opj_stream_seek(cio, lenp + len, p_manager);
3385 static int write_fidx(int offset_jp2c, int length_jp2c, int offset_idx,
3386 int length_idx, opj_stream_private_t *cio,
3387 opj_event_mgr_t * p_manager)
3389 OPJ_BYTE l_data_header [4];
3390 OPJ_OFF_T len, lenp;
3392 lenp = opj_stream_tell(cio);
3393 opj_stream_skip(cio, 4, p_manager);
3394 opj_write_bytes(l_data_header, JPIP_FIDX, 4); /* FIDX */
3395 opj_stream_write_data(cio, l_data_header, 4, p_manager);
3397 write_prxy(offset_jp2c, length_jp2c, offset_idx, length_idx, cio, p_manager);
3399 len = opj_stream_tell(cio) - lenp;
3400 opj_stream_skip(cio, lenp, p_manager);
3401 opj_write_bytes(l_data_header, len, 4); /* L */
3402 opj_stream_write_data(cio, l_data_header, 4, p_manager);
3403 opj_stream_seek(cio, lenp + len, p_manager);
3408 #endif /* USE_JPIP */