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 *stream,
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 * Developers 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 * Developers 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. Developers 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. Developers 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. Developers 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 * Developers 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->h < 1 || jp2->w < 1 || jp2->numcomps < 1) {
590 opj_event_msg(p_manager, EVT_ERROR,
591 "Wrong values for: w(%d) h(%d) numcomps(%d) (ihdr)\n",
592 jp2->w, jp2->h, jp2->numcomps);
595 if ((jp2->numcomps - 1U) >=
596 16384U) { /* unsigned underflow is well defined: 1U <= jp2->numcomps <= 16384U */
597 opj_event_msg(p_manager, EVT_ERROR, "Invalid number of components (ihdr)\n");
601 /* allocate memory for components */
602 jp2->comps = (opj_jp2_comps_t*) opj_calloc(jp2->numcomps,
603 sizeof(opj_jp2_comps_t));
604 if (jp2->comps == 0) {
605 opj_event_msg(p_manager, EVT_ERROR,
606 "Not enough memory to handle image header (ihdr)\n");
610 opj_read_bytes(p_image_header_data, &(jp2->bpc), 1); /* BPC */
611 ++ p_image_header_data;
613 opj_read_bytes(p_image_header_data, &(jp2->C), 1); /* C */
614 ++ p_image_header_data;
616 /* Should be equal to 7 cf. chapter about image header box of the norm */
618 opj_event_msg(p_manager, EVT_INFO,
619 "JP2 IHDR box: compression type indicate that the file is not a conforming JP2 file (%d) \n",
623 opj_read_bytes(p_image_header_data, &(jp2->UnkC), 1); /* UnkC */
624 ++ p_image_header_data;
625 opj_read_bytes(p_image_header_data, &(jp2->IPR), 1); /* IPR */
626 ++ p_image_header_data;
628 jp2->j2k->m_cp.allow_different_bit_depth_sign = (jp2->bpc == 255);
629 jp2->j2k->ihdr_w = jp2->w;
630 jp2->j2k->ihdr_h = jp2->h;
636 static OPJ_BYTE * opj_jp2_write_ihdr(opj_jp2_t *jp2,
637 OPJ_UINT32 * p_nb_bytes_written
640 OPJ_BYTE * l_ihdr_data, * l_current_ihdr_ptr;
644 assert(p_nb_bytes_written != 00);
646 /* default image header is 22 bytes wide */
647 l_ihdr_data = (OPJ_BYTE *) opj_calloc(1, 22);
648 if (l_ihdr_data == 00) {
652 l_current_ihdr_ptr = l_ihdr_data;
654 opj_write_bytes(l_current_ihdr_ptr, 22, 4); /* write box size */
655 l_current_ihdr_ptr += 4;
657 opj_write_bytes(l_current_ihdr_ptr, JP2_IHDR, 4); /* IHDR */
658 l_current_ihdr_ptr += 4;
660 opj_write_bytes(l_current_ihdr_ptr, jp2->h, 4); /* HEIGHT */
661 l_current_ihdr_ptr += 4;
663 opj_write_bytes(l_current_ihdr_ptr, jp2->w, 4); /* WIDTH */
664 l_current_ihdr_ptr += 4;
666 opj_write_bytes(l_current_ihdr_ptr, jp2->numcomps, 2); /* NC */
667 l_current_ihdr_ptr += 2;
669 opj_write_bytes(l_current_ihdr_ptr, jp2->bpc, 1); /* BPC */
670 ++l_current_ihdr_ptr;
672 opj_write_bytes(l_current_ihdr_ptr, jp2->C, 1); /* C : Always 7 */
673 ++l_current_ihdr_ptr;
675 opj_write_bytes(l_current_ihdr_ptr, jp2->UnkC,
676 1); /* UnkC, colorspace unknown */
677 ++l_current_ihdr_ptr;
679 opj_write_bytes(l_current_ihdr_ptr, jp2->IPR,
680 1); /* IPR, no intellectual property */
681 ++l_current_ihdr_ptr;
683 *p_nb_bytes_written = 22;
688 static OPJ_BYTE * opj_jp2_write_bpcc(opj_jp2_t *jp2,
689 OPJ_UINT32 * p_nb_bytes_written
693 /* room for 8 bytes for box and 1 byte for each component */
694 OPJ_UINT32 l_bpcc_size;
695 OPJ_BYTE * l_bpcc_data, * l_current_bpcc_ptr;
699 assert(p_nb_bytes_written != 00);
700 l_bpcc_size = 8 + jp2->numcomps;
702 l_bpcc_data = (OPJ_BYTE *) opj_calloc(1, l_bpcc_size);
703 if (l_bpcc_data == 00) {
707 l_current_bpcc_ptr = l_bpcc_data;
709 opj_write_bytes(l_current_bpcc_ptr, l_bpcc_size,
710 4); /* write box size */
711 l_current_bpcc_ptr += 4;
713 opj_write_bytes(l_current_bpcc_ptr, JP2_BPCC, 4); /* BPCC */
714 l_current_bpcc_ptr += 4;
716 for (i = 0; i < jp2->numcomps; ++i) {
717 opj_write_bytes(l_current_bpcc_ptr, jp2->comps[i].bpcc,
718 1); /* write each component information */
719 ++l_current_bpcc_ptr;
722 *p_nb_bytes_written = l_bpcc_size;
727 static OPJ_BOOL opj_jp2_read_bpcc(opj_jp2_t *jp2,
728 OPJ_BYTE * p_bpc_header_data,
729 OPJ_UINT32 p_bpc_header_size,
730 opj_event_mgr_t * p_manager
736 assert(p_bpc_header_data != 00);
738 assert(p_manager != 00);
741 if (jp2->bpc != 255) {
742 opj_event_msg(p_manager, EVT_WARNING,
743 "A BPCC header box is available although BPC given by the IHDR box (%d) indicate components bit depth is constant\n",
747 /* and length is relevant */
748 if (p_bpc_header_size != jp2->numcomps) {
749 opj_event_msg(p_manager, EVT_ERROR, "Bad BPCC header box (bad size)\n");
753 /* read info for each component */
754 for (i = 0; i < jp2->numcomps; ++i) {
755 opj_read_bytes(p_bpc_header_data, &jp2->comps[i].bpcc,
756 1); /* read each BPCC component */
762 static OPJ_BYTE * opj_jp2_write_cdef(opj_jp2_t *jp2,
763 OPJ_UINT32 * p_nb_bytes_written)
765 /* room for 8 bytes for box, 2 for n */
766 OPJ_UINT32 l_cdef_size = 10;
767 OPJ_BYTE * l_cdef_data, * l_current_cdef_ptr;
773 assert(p_nb_bytes_written != 00);
774 assert(jp2->color.jp2_cdef != 00);
775 assert(jp2->color.jp2_cdef->info != 00);
776 assert(jp2->color.jp2_cdef->n > 0U);
778 l_cdef_size += 6U * jp2->color.jp2_cdef->n;
780 l_cdef_data = (OPJ_BYTE *) opj_malloc(l_cdef_size);
781 if (l_cdef_data == 00) {
785 l_current_cdef_ptr = l_cdef_data;
787 opj_write_bytes(l_current_cdef_ptr, l_cdef_size, 4); /* write box size */
788 l_current_cdef_ptr += 4;
790 opj_write_bytes(l_current_cdef_ptr, JP2_CDEF, 4); /* BPCC */
791 l_current_cdef_ptr += 4;
793 l_value = jp2->color.jp2_cdef->n;
794 opj_write_bytes(l_current_cdef_ptr, l_value, 2); /* N */
795 l_current_cdef_ptr += 2;
797 for (i = 0U; i < jp2->color.jp2_cdef->n; ++i) {
798 l_value = jp2->color.jp2_cdef->info[i].cn;
799 opj_write_bytes(l_current_cdef_ptr, l_value, 2); /* Cni */
800 l_current_cdef_ptr += 2;
801 l_value = jp2->color.jp2_cdef->info[i].typ;
802 opj_write_bytes(l_current_cdef_ptr, l_value, 2); /* Typi */
803 l_current_cdef_ptr += 2;
804 l_value = jp2->color.jp2_cdef->info[i].asoc;
805 opj_write_bytes(l_current_cdef_ptr, l_value, 2); /* Asoci */
806 l_current_cdef_ptr += 2;
808 *p_nb_bytes_written = l_cdef_size;
813 static OPJ_BYTE * opj_jp2_write_colr(opj_jp2_t *jp2,
814 OPJ_UINT32 * p_nb_bytes_written
817 /* room for 8 bytes for box 3 for common data and variable upon profile*/
818 OPJ_UINT32 l_colr_size = 11;
819 OPJ_BYTE * l_colr_data, * l_current_colr_ptr;
823 assert(p_nb_bytes_written != 00);
824 assert(jp2->meth == 1 || jp2->meth == 2);
828 l_colr_size += 4; /* EnumCS */
831 assert(jp2->color.icc_profile_len); /* ICC profile */
832 l_colr_size += jp2->color.icc_profile_len;
838 l_colr_data = (OPJ_BYTE *) opj_calloc(1, l_colr_size);
839 if (l_colr_data == 00) {
843 l_current_colr_ptr = l_colr_data;
845 opj_write_bytes(l_current_colr_ptr, l_colr_size,
846 4); /* write box size */
847 l_current_colr_ptr += 4;
849 opj_write_bytes(l_current_colr_ptr, JP2_COLR, 4); /* BPCC */
850 l_current_colr_ptr += 4;
852 opj_write_bytes(l_current_colr_ptr, jp2->meth, 1); /* METH */
853 ++l_current_colr_ptr;
855 opj_write_bytes(l_current_colr_ptr, jp2->precedence, 1); /* PRECEDENCE */
856 ++l_current_colr_ptr;
858 opj_write_bytes(l_current_colr_ptr, jp2->approx, 1); /* APPROX */
859 ++l_current_colr_ptr;
862 1) { /* Meth value is restricted to 1 or 2 (Table I.9 of part 1) */
863 opj_write_bytes(l_current_colr_ptr, jp2->enumcs, 4);
866 if (jp2->meth == 2) { /* ICC profile */
868 for (i = 0; i < jp2->color.icc_profile_len; ++i) {
869 opj_write_bytes(l_current_colr_ptr, jp2->color.icc_profile_buf[i], 1);
870 ++l_current_colr_ptr;
875 *p_nb_bytes_written = l_colr_size;
880 static void opj_jp2_free_pclr(opj_jp2_color_t *color)
882 opj_free(color->jp2_pclr->channel_sign);
883 opj_free(color->jp2_pclr->channel_size);
884 opj_free(color->jp2_pclr->entries);
886 if (color->jp2_pclr->cmap) {
887 opj_free(color->jp2_pclr->cmap);
890 opj_free(color->jp2_pclr);
891 color->jp2_pclr = NULL;
894 static OPJ_BOOL opj_jp2_check_color(opj_image_t *image, opj_jp2_color_t *color,
895 opj_event_mgr_t *p_manager)
899 /* testcase 4149.pdf.SIGSEGV.cf7.3501 */
900 if (color->jp2_cdef) {
901 opj_jp2_cdef_info_t *info = color->jp2_cdef->info;
902 OPJ_UINT16 n = color->jp2_cdef->n;
903 OPJ_UINT32 nr_channels =
904 image->numcomps; /* FIXME image->numcomps == jp2->numcomps before color is applied ??? */
906 /* cdef applies to cmap channels if any */
907 if (color->jp2_pclr && color->jp2_pclr->cmap) {
908 nr_channels = (OPJ_UINT32)color->jp2_pclr->nr_channels;
911 for (i = 0; i < n; i++) {
912 if (info[i].cn >= nr_channels) {
913 opj_event_msg(p_manager, EVT_ERROR, "Invalid component index %d (>= %d).\n",
914 info[i].cn, nr_channels);
917 if (info[i].asoc == 65535U) {
921 if (info[i].asoc > 0 && (OPJ_UINT32)(info[i].asoc - 1) >= nr_channels) {
922 opj_event_msg(p_manager, EVT_ERROR, "Invalid component index %d (>= %d).\n",
923 info[i].asoc - 1, nr_channels);
929 /* ISO 15444-1 states that if cdef is present, it shall contain a complete list of channel definitions. */
930 while (nr_channels > 0) {
931 for (i = 0; i < n; ++i) {
932 if ((OPJ_UINT32)info[i].cn == (nr_channels - 1U)) {
937 opj_event_msg(p_manager, EVT_ERROR, "Incomplete channel definitions.\n");
944 /* testcases 451.pdf.SIGSEGV.f4c.3723, 451.pdf.SIGSEGV.5b5.3723 and
945 66ea31acbb0f23a2bbc91f64d69a03f5_signal_sigsegv_13937c0_7030_5725.pdf */
946 if (color->jp2_pclr && color->jp2_pclr->cmap) {
947 OPJ_UINT16 nr_channels = color->jp2_pclr->nr_channels;
948 opj_jp2_cmap_comp_t *cmap = color->jp2_pclr->cmap;
949 OPJ_BOOL *pcol_usage, is_sane = OPJ_TRUE;
951 /* verify that all original components match an existing one */
952 for (i = 0; i < nr_channels; i++) {
953 if (cmap[i].cmp >= image->numcomps) {
954 opj_event_msg(p_manager, EVT_ERROR, "Invalid component index %d (>= %d).\n",
955 cmap[i].cmp, image->numcomps);
960 pcol_usage = (OPJ_BOOL *) opj_calloc(nr_channels, sizeof(OPJ_BOOL));
962 opj_event_msg(p_manager, EVT_ERROR, "Unexpected OOM.\n");
965 /* verify that no component is targeted more than once */
966 for (i = 0; i < nr_channels; i++) {
967 OPJ_BYTE mtyp = cmap[i].mtyp;
968 OPJ_BYTE pcol = cmap[i].pcol;
969 /* See ISO 15444-1 Table I.14 – MTYPi field values */
970 if (mtyp != 0 && mtyp != 1) {
971 opj_event_msg(p_manager, EVT_ERROR,
972 "Invalid value for cmap[%d].mtyp = %d.\n", i,
975 } else if (pcol >= nr_channels) {
976 opj_event_msg(p_manager, EVT_ERROR,
977 "Invalid component/palette index for direct mapping %d.\n", pcol);
979 } else if (pcol_usage[pcol] && mtyp == 1) {
980 opj_event_msg(p_manager, EVT_ERROR, "Component %d is mapped twice.\n", pcol);
982 } else if (mtyp == 0 && pcol != 0) {
983 /* I.5.3.5 PCOL: If the value of the MTYP field for this channel is 0, then
984 * the value of this field shall be 0. */
985 opj_event_msg(p_manager, EVT_ERROR, "Direct use at #%d however pcol=%d.\n", i,
988 } else if (mtyp == 1 && pcol != i) {
989 /* OpenJPEG implementation limitation. See assert(i == pcol); */
990 /* in opj_jp2_apply_pclr() */
991 opj_event_msg(p_manager, EVT_ERROR,
992 "Implementation limitation: for palette mapping, "
993 "pcol[%d] should be equal to %d, but is equal "
994 "to %d.\n", i, i, pcol);
997 pcol_usage[pcol] = OPJ_TRUE;
1000 /* verify that all components are targeted at least once */
1001 for (i = 0; i < nr_channels; i++) {
1002 if (!pcol_usage[i] && cmap[i].mtyp != 0) {
1003 opj_event_msg(p_manager, EVT_ERROR, "Component %d doesn't have a mapping.\n",
1005 is_sane = OPJ_FALSE;
1008 /* Issue 235/447 weird cmap */
1009 if (1 && is_sane && (image->numcomps == 1U)) {
1010 for (i = 0; i < nr_channels; i++) {
1011 if (!pcol_usage[i]) {
1013 opj_event_msg(p_manager, EVT_WARNING,
1014 "Component mapping seems wrong. Trying to correct.\n");
1020 for (i = 0; i < nr_channels; i++) {
1022 cmap[i].pcol = (OPJ_BYTE) i;
1026 opj_free(pcol_usage);
1036 static OPJ_BOOL opj_jp2_apply_pclr(opj_image_t *image,
1037 opj_jp2_color_t *color,
1038 opj_event_mgr_t * p_manager)
1040 opj_image_comp_t *old_comps, *new_comps;
1041 OPJ_BYTE *channel_size, *channel_sign;
1042 OPJ_UINT32 *entries;
1043 opj_jp2_cmap_comp_t *cmap;
1044 OPJ_INT32 *src, *dst;
1046 OPJ_UINT16 i, nr_channels, cmp, pcol;
1049 channel_size = color->jp2_pclr->channel_size;
1050 channel_sign = color->jp2_pclr->channel_sign;
1051 entries = color->jp2_pclr->entries;
1052 cmap = color->jp2_pclr->cmap;
1053 nr_channels = color->jp2_pclr->nr_channels;
1055 for (i = 0; i < nr_channels; ++i) {
1056 /* Palette mapping: */
1058 if (image->comps[cmp].data == NULL) {
1059 opj_event_msg(p_manager, EVT_ERROR,
1060 "image->comps[%d].data == NULL in opj_jp2_apply_pclr().\n", i);
1065 old_comps = image->comps;
1066 new_comps = (opj_image_comp_t*)
1067 opj_malloc(nr_channels * sizeof(opj_image_comp_t));
1069 opj_event_msg(p_manager, EVT_ERROR,
1070 "Memory allocation failure in opj_jp2_apply_pclr().\n");
1073 for (i = 0; i < nr_channels; ++i) {
1074 pcol = cmap[i].pcol;
1078 if (cmap[i].mtyp == 0) {
1080 new_comps[i] = old_comps[cmp];
1083 new_comps[pcol] = old_comps[cmp];
1086 /* Palette mapping: */
1087 new_comps[i].data = (OPJ_INT32*)
1088 opj_image_data_alloc(sizeof(OPJ_INT32) * old_comps[cmp].w * old_comps[cmp].h);
1089 if (!new_comps[i].data) {
1092 opj_image_data_free(new_comps[i].data);
1094 opj_free(new_comps);
1095 opj_event_msg(p_manager, EVT_ERROR,
1096 "Memory allocation failure in opj_jp2_apply_pclr().\n");
1099 new_comps[i].prec = channel_size[i];
1100 new_comps[i].sgnd = channel_sign[i];
1103 top_k = color->jp2_pclr->nr_entries - 1;
1105 for (i = 0; i < nr_channels; ++i) {
1106 /* Palette mapping: */
1108 pcol = cmap[i].pcol;
1109 src = old_comps[cmp].data;
1110 assert(src); /* verified above */
1111 max = new_comps[i].w * new_comps[i].h;
1114 if (cmap[i].mtyp == 0) {
1115 dst = new_comps[i].data;
1117 for (j = 0; j < max; ++j) {
1122 dst = new_comps[pcol].data;
1124 for (j = 0; j < max; ++j) {
1126 if ((k = src[j]) < 0) {
1128 } else if (k > top_k) {
1133 dst[j] = (OPJ_INT32)entries[k * nr_channels + pcol];
1138 max = image->numcomps;
1139 for (j = 0; j < max; ++j) {
1140 if (old_comps[j].data) {
1141 opj_image_data_free(old_comps[j].data);
1145 opj_free(old_comps);
1146 image->comps = new_comps;
1147 image->numcomps = nr_channels;
1152 static OPJ_BOOL opj_jp2_read_pclr(opj_jp2_t *jp2,
1153 OPJ_BYTE * p_pclr_header_data,
1154 OPJ_UINT32 p_pclr_header_size,
1155 opj_event_mgr_t * p_manager
1158 opj_jp2_pclr_t *jp2_pclr;
1159 OPJ_BYTE *channel_size, *channel_sign;
1160 OPJ_UINT32 *entries;
1161 OPJ_UINT16 nr_entries, nr_channels;
1164 OPJ_BYTE *orig_header_data = p_pclr_header_data;
1167 assert(p_pclr_header_data != 00);
1169 assert(p_manager != 00);
1170 (void)p_pclr_header_size;
1172 if (jp2->color.jp2_pclr) {
1176 if (p_pclr_header_size < 3) {
1180 opj_read_bytes(p_pclr_header_data, &l_value, 2); /* NE */
1181 p_pclr_header_data += 2;
1182 nr_entries = (OPJ_UINT16) l_value;
1183 if ((nr_entries == 0U) || (nr_entries > 1024U)) {
1184 opj_event_msg(p_manager, EVT_ERROR, "Invalid PCLR box. Reports %d entries\n",
1189 opj_read_bytes(p_pclr_header_data, &l_value, 1); /* NPC */
1190 ++p_pclr_header_data;
1191 nr_channels = (OPJ_UINT16) l_value;
1192 if (nr_channels == 0U) {
1193 opj_event_msg(p_manager, EVT_ERROR,
1194 "Invalid PCLR box. Reports 0 palette columns\n");
1198 if (p_pclr_header_size < 3 + (OPJ_UINT32)nr_channels) {
1202 entries = (OPJ_UINT32*) opj_malloc(sizeof(OPJ_UINT32) * nr_channels *
1207 channel_size = (OPJ_BYTE*) opj_malloc(nr_channels);
1208 if (!channel_size) {
1212 channel_sign = (OPJ_BYTE*) opj_malloc(nr_channels);
1213 if (!channel_sign) {
1215 opj_free(channel_size);
1219 jp2_pclr = (opj_jp2_pclr_t*)opj_malloc(sizeof(opj_jp2_pclr_t));
1222 opj_free(channel_size);
1223 opj_free(channel_sign);
1227 jp2_pclr->channel_sign = channel_sign;
1228 jp2_pclr->channel_size = channel_size;
1229 jp2_pclr->entries = entries;
1230 jp2_pclr->nr_entries = nr_entries;
1231 jp2_pclr->nr_channels = (OPJ_BYTE) l_value;
1232 jp2_pclr->cmap = NULL;
1234 jp2->color.jp2_pclr = jp2_pclr;
1236 for (i = 0; i < nr_channels; ++i) {
1237 opj_read_bytes(p_pclr_header_data, &l_value, 1); /* Bi */
1238 ++p_pclr_header_data;
1240 channel_size[i] = (OPJ_BYTE)((l_value & 0x7f) + 1);
1241 channel_sign[i] = (l_value & 0x80) ? 1 : 0;
1244 for (j = 0; j < nr_entries; ++j) {
1245 for (i = 0; i < nr_channels; ++i) {
1246 OPJ_UINT32 bytes_to_read = (OPJ_UINT32)((channel_size[i] + 7) >> 3);
1248 if (bytes_to_read > sizeof(OPJ_UINT32)) {
1249 bytes_to_read = sizeof(OPJ_UINT32);
1251 if ((ptrdiff_t)p_pclr_header_size < (ptrdiff_t)(p_pclr_header_data -
1252 orig_header_data) + (ptrdiff_t)bytes_to_read) {
1256 opj_read_bytes(p_pclr_header_data, &l_value, bytes_to_read); /* Cji */
1257 p_pclr_header_data += bytes_to_read;
1258 *entries = (OPJ_UINT32) l_value;
1266 static OPJ_BOOL opj_jp2_read_cmap(opj_jp2_t * jp2,
1267 OPJ_BYTE * p_cmap_header_data,
1268 OPJ_UINT32 p_cmap_header_size,
1269 opj_event_mgr_t * p_manager
1272 opj_jp2_cmap_comp_t *cmap;
1273 OPJ_BYTE i, nr_channels;
1278 assert(p_cmap_header_data != 00);
1279 assert(p_manager != 00);
1280 (void)p_cmap_header_size;
1282 /* Need nr_channels: */
1283 if (jp2->color.jp2_pclr == NULL) {
1284 opj_event_msg(p_manager, EVT_ERROR,
1285 "Need to read a PCLR box before the CMAP box.\n");
1289 /* Part 1, I.5.3.5: 'There shall be at most one Component Mapping box
1290 * inside a JP2 Header box' :
1292 if (jp2->color.jp2_pclr->cmap) {
1293 opj_event_msg(p_manager, EVT_ERROR, "Only one CMAP box is allowed.\n");
1297 nr_channels = jp2->color.jp2_pclr->nr_channels;
1298 if (p_cmap_header_size < (OPJ_UINT32)nr_channels * 4) {
1299 opj_event_msg(p_manager, EVT_ERROR, "Insufficient data for CMAP box.\n");
1303 cmap = (opj_jp2_cmap_comp_t*) opj_malloc(nr_channels * sizeof(
1304 opj_jp2_cmap_comp_t));
1310 for (i = 0; i < nr_channels; ++i) {
1311 opj_read_bytes(p_cmap_header_data, &l_value, 2); /* CMP^i */
1312 p_cmap_header_data += 2;
1313 cmap[i].cmp = (OPJ_UINT16) l_value;
1315 opj_read_bytes(p_cmap_header_data, &l_value, 1); /* MTYP^i */
1316 ++p_cmap_header_data;
1317 cmap[i].mtyp = (OPJ_BYTE) l_value;
1319 opj_read_bytes(p_cmap_header_data, &l_value, 1); /* PCOL^i */
1320 ++p_cmap_header_data;
1321 cmap[i].pcol = (OPJ_BYTE) l_value;
1324 jp2->color.jp2_pclr->cmap = cmap;
1329 static void opj_jp2_apply_cdef(opj_image_t *image, opj_jp2_color_t *color,
1330 opj_event_mgr_t *manager)
1332 opj_jp2_cdef_info_t *info;
1333 OPJ_UINT16 i, n, cn, asoc, acn;
1335 info = color->jp2_cdef->info;
1336 n = color->jp2_cdef->n;
1338 for (i = 0; i < n; ++i) {
1339 /* WATCH: acn = asoc - 1 ! */
1340 asoc = info[i].asoc;
1343 if (cn >= image->numcomps) {
1344 opj_event_msg(manager, EVT_WARNING, "opj_jp2_apply_cdef: cn=%d, numcomps=%d\n",
1345 cn, image->numcomps);
1348 if (asoc == 0 || asoc == 65535) {
1349 image->comps[cn].alpha = info[i].typ;
1353 acn = (OPJ_UINT16)(asoc - 1);
1354 if (acn >= image->numcomps) {
1355 opj_event_msg(manager, EVT_WARNING, "opj_jp2_apply_cdef: acn=%d, numcomps=%d\n",
1356 acn, image->numcomps);
1360 /* Swap only if color channel */
1361 if ((cn != acn) && (info[i].typ == 0)) {
1362 opj_image_comp_t saved;
1365 memcpy(&saved, &image->comps[cn], sizeof(opj_image_comp_t));
1366 memcpy(&image->comps[cn], &image->comps[acn], sizeof(opj_image_comp_t));
1367 memcpy(&image->comps[acn], &saved, sizeof(opj_image_comp_t));
1369 /* Swap channels in following channel definitions, don't bother with j <= i that are already processed */
1370 for (j = (OPJ_UINT16)(i + 1U); j < n ; ++j) {
1371 if (info[j].cn == cn) {
1373 } else if (info[j].cn == acn) {
1376 /* asoc is related to color index. Do not update. */
1380 image->comps[cn].alpha = info[i].typ;
1383 if (color->jp2_cdef->info) {
1384 opj_free(color->jp2_cdef->info);
1387 opj_free(color->jp2_cdef);
1388 color->jp2_cdef = NULL;
1390 }/* jp2_apply_cdef() */
1392 static OPJ_BOOL opj_jp2_read_cdef(opj_jp2_t * jp2,
1393 OPJ_BYTE * p_cdef_header_data,
1394 OPJ_UINT32 p_cdef_header_size,
1395 opj_event_mgr_t * p_manager
1398 opj_jp2_cdef_info_t *cdef_info;
1404 assert(p_cdef_header_data != 00);
1405 assert(p_manager != 00);
1406 (void)p_cdef_header_size;
1408 /* Part 1, I.5.3.6: 'The shall be at most one Channel Definition box
1409 * inside a JP2 Header box.'*/
1410 if (jp2->color.jp2_cdef) {
1414 if (p_cdef_header_size < 2) {
1415 opj_event_msg(p_manager, EVT_ERROR, "Insufficient data for CDEF box.\n");
1419 opj_read_bytes(p_cdef_header_data, &l_value, 2); /* N */
1420 p_cdef_header_data += 2;
1422 if ((OPJ_UINT16)l_value == 0) { /* szukw000: FIXME */
1423 opj_event_msg(p_manager, EVT_ERROR,
1424 "Number of channel description is equal to zero in CDEF box.\n");
1428 if (p_cdef_header_size < 2 + (OPJ_UINT32)(OPJ_UINT16)l_value * 6) {
1429 opj_event_msg(p_manager, EVT_ERROR, "Insufficient data for CDEF box.\n");
1433 cdef_info = (opj_jp2_cdef_info_t*) opj_malloc(l_value * sizeof(
1434 opj_jp2_cdef_info_t));
1439 jp2->color.jp2_cdef = (opj_jp2_cdef_t*)opj_malloc(sizeof(opj_jp2_cdef_t));
1440 if (!jp2->color.jp2_cdef) {
1441 opj_free(cdef_info);
1444 jp2->color.jp2_cdef->info = cdef_info;
1445 jp2->color.jp2_cdef->n = (OPJ_UINT16) l_value;
1447 for (i = 0; i < jp2->color.jp2_cdef->n; ++i) {
1448 opj_read_bytes(p_cdef_header_data, &l_value, 2); /* Cn^i */
1449 p_cdef_header_data += 2;
1450 cdef_info[i].cn = (OPJ_UINT16) l_value;
1452 opj_read_bytes(p_cdef_header_data, &l_value, 2); /* Typ^i */
1453 p_cdef_header_data += 2;
1454 cdef_info[i].typ = (OPJ_UINT16) l_value;
1456 opj_read_bytes(p_cdef_header_data, &l_value, 2); /* Asoc^i */
1457 p_cdef_header_data += 2;
1458 cdef_info[i].asoc = (OPJ_UINT16) l_value;
1464 static OPJ_BOOL opj_jp2_read_colr(opj_jp2_t *jp2,
1465 OPJ_BYTE * p_colr_header_data,
1466 OPJ_UINT32 p_colr_header_size,
1467 opj_event_mgr_t * p_manager
1474 assert(p_colr_header_data != 00);
1475 assert(p_manager != 00);
1477 if (p_colr_header_size < 3) {
1478 opj_event_msg(p_manager, EVT_ERROR, "Bad COLR header box (bad size)\n");
1482 /* Part 1, I.5.3.3 : 'A conforming JP2 reader shall ignore all Colour
1483 * Specification boxes after the first.'
1485 if (jp2->color.jp2_has_colr) {
1486 opj_event_msg(p_manager, EVT_INFO,
1487 "A conforming JP2 reader shall ignore all Colour Specification boxes after the first, so we ignore this one.\n");
1488 p_colr_header_data += p_colr_header_size;
1492 opj_read_bytes(p_colr_header_data, &jp2->meth, 1); /* METH */
1493 ++p_colr_header_data;
1495 opj_read_bytes(p_colr_header_data, &jp2->precedence, 1); /* PRECEDENCE */
1496 ++p_colr_header_data;
1498 opj_read_bytes(p_colr_header_data, &jp2->approx, 1); /* APPROX */
1499 ++p_colr_header_data;
1501 if (jp2->meth == 1) {
1502 if (p_colr_header_size < 7) {
1503 opj_event_msg(p_manager, EVT_ERROR, "Bad COLR header box (bad size: %d)\n",
1504 p_colr_header_size);
1507 if ((p_colr_header_size > 7) &&
1508 (jp2->enumcs != 14)) { /* handled below for CIELab) */
1509 /* testcase Altona_Technical_v20_x4.pdf */
1510 opj_event_msg(p_manager, EVT_WARNING, "Bad COLR header box (bad size: %d)\n",
1511 p_colr_header_size);
1514 opj_read_bytes(p_colr_header_data, &jp2->enumcs, 4); /* EnumCS */
1516 p_colr_header_data += 4;
1518 if (jp2->enumcs == 14) { /* CIELab */
1520 OPJ_UINT32 rl, ol, ra, oa, rb, ob, il;
1522 cielab = (OPJ_UINT32*)opj_malloc(9 * sizeof(OPJ_UINT32));
1523 if (cielab == NULL) {
1524 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory for cielab\n");
1527 cielab[0] = 14; /* enumcs */
1529 /* default values */
1530 rl = ra = rb = ol = oa = ob = 0;
1531 il = 0x00443530; /* D50 */
1532 cielab[1] = 0x44454600;/* DEF */
1534 if (p_colr_header_size == 35) {
1535 opj_read_bytes(p_colr_header_data, &rl, 4);
1536 p_colr_header_data += 4;
1537 opj_read_bytes(p_colr_header_data, &ol, 4);
1538 p_colr_header_data += 4;
1539 opj_read_bytes(p_colr_header_data, &ra, 4);
1540 p_colr_header_data += 4;
1541 opj_read_bytes(p_colr_header_data, &oa, 4);
1542 p_colr_header_data += 4;
1543 opj_read_bytes(p_colr_header_data, &rb, 4);
1544 p_colr_header_data += 4;
1545 opj_read_bytes(p_colr_header_data, &ob, 4);
1546 p_colr_header_data += 4;
1547 opj_read_bytes(p_colr_header_data, &il, 4);
1548 p_colr_header_data += 4;
1551 } else if (p_colr_header_size != 7) {
1552 opj_event_msg(p_manager, EVT_WARNING,
1553 "Bad COLR header box (CIELab, bad size: %d)\n", p_colr_header_size);
1563 jp2->color.icc_profile_buf = (OPJ_BYTE*)cielab;
1564 jp2->color.icc_profile_len = 0;
1566 jp2->color.jp2_has_colr = 1;
1567 } else if (jp2->meth == 2) {
1569 OPJ_INT32 it_icc_value = 0;
1570 OPJ_INT32 icc_len = (OPJ_INT32)p_colr_header_size - 3;
1572 jp2->color.icc_profile_len = (OPJ_UINT32)icc_len;
1573 jp2->color.icc_profile_buf = (OPJ_BYTE*) opj_calloc(1, (size_t)icc_len);
1574 if (!jp2->color.icc_profile_buf) {
1575 jp2->color.icc_profile_len = 0;
1579 for (it_icc_value = 0; it_icc_value < icc_len; ++it_icc_value) {
1580 opj_read_bytes(p_colr_header_data, &l_value, 1); /* icc values */
1581 ++p_colr_header_data;
1582 jp2->color.icc_profile_buf[it_icc_value] = (OPJ_BYTE) l_value;
1585 jp2->color.jp2_has_colr = 1;
1586 } else if (jp2->meth > 2) {
1587 /* ISO/IEC 15444-1:2004 (E), Table I.9 Legal METH values:
1588 conforming JP2 reader shall ignore the entire Colour Specification box.*/
1589 opj_event_msg(p_manager, EVT_INFO,
1590 "COLR BOX meth value is not a regular value (%d), "
1591 "so we will ignore the entire Colour Specification box. \n", jp2->meth);
1597 static OPJ_BOOL opj_jp2_apply_color_postprocessing(opj_jp2_t *jp2,
1598 opj_image_t* p_image,
1599 opj_event_mgr_t * p_manager)
1601 if (jp2->j2k->m_specific_param.m_decoder.m_numcomps_to_decode) {
1602 /* Bypass all JP2 component transforms */
1606 if (!jp2->ignore_pclr_cmap_cdef) {
1607 if (!opj_jp2_check_color(p_image, &(jp2->color), p_manager)) {
1611 if (jp2->color.jp2_pclr) {
1612 /* Part 1, I.5.3.4: Either both or none : */
1613 if (!jp2->color.jp2_pclr->cmap) {
1614 opj_jp2_free_pclr(&(jp2->color));
1616 if (!opj_jp2_apply_pclr(p_image, &(jp2->color), p_manager)) {
1622 /* Apply the color space if needed */
1623 if (jp2->color.jp2_cdef) {
1624 opj_jp2_apply_cdef(p_image, &(jp2->color), p_manager);
1631 OPJ_BOOL opj_jp2_decode(opj_jp2_t *jp2,
1632 opj_stream_private_t *p_stream,
1633 opj_image_t* p_image,
1634 opj_event_mgr_t * p_manager)
1641 if (! opj_j2k_decode(jp2->j2k, p_stream, p_image, p_manager)) {
1642 opj_event_msg(p_manager, EVT_ERROR,
1643 "Failed to decode the codestream in the JP2 file\n");
1647 return opj_jp2_apply_color_postprocessing(jp2, p_image, p_manager);
1650 static OPJ_BOOL opj_jp2_write_jp2h(opj_jp2_t *jp2,
1651 opj_stream_private_t *stream,
1652 opj_event_mgr_t * p_manager
1655 opj_jp2_img_header_writer_handler_t l_writers [4];
1656 opj_jp2_img_header_writer_handler_t * l_current_writer;
1658 OPJ_INT32 i, l_nb_pass;
1659 /* size of data for super box*/
1660 OPJ_UINT32 l_jp2h_size = 8;
1661 OPJ_BOOL l_result = OPJ_TRUE;
1663 /* to store the data of the super box */
1664 OPJ_BYTE l_jp2h_data [8];
1667 assert(stream != 00);
1669 assert(p_manager != 00);
1671 memset(l_writers, 0, sizeof(l_writers));
1673 if (jp2->bpc == 255) {
1675 l_writers[0].handler = opj_jp2_write_ihdr;
1676 l_writers[1].handler = opj_jp2_write_bpcc;
1677 l_writers[2].handler = opj_jp2_write_colr;
1680 l_writers[0].handler = opj_jp2_write_ihdr;
1681 l_writers[1].handler = opj_jp2_write_colr;
1684 if (jp2->color.jp2_cdef != NULL) {
1685 l_writers[l_nb_pass].handler = opj_jp2_write_cdef;
1689 /* write box header */
1690 /* write JP2H type */
1691 opj_write_bytes(l_jp2h_data + 4, JP2_JP2H, 4);
1693 l_current_writer = l_writers;
1694 for (i = 0; i < l_nb_pass; ++i) {
1695 l_current_writer->m_data = l_current_writer->handler(jp2,
1696 &(l_current_writer->m_size));
1697 if (l_current_writer->m_data == 00) {
1698 opj_event_msg(p_manager, EVT_ERROR,
1699 "Not enough memory to hold JP2 Header data\n");
1700 l_result = OPJ_FALSE;
1704 l_jp2h_size += l_current_writer->m_size;
1709 l_current_writer = l_writers;
1710 for (i = 0; i < l_nb_pass; ++i) {
1711 if (l_current_writer->m_data != 00) {
1712 opj_free(l_current_writer->m_data);
1720 /* write super box size */
1721 opj_write_bytes(l_jp2h_data, l_jp2h_size, 4);
1723 /* write super box data on stream */
1724 if (opj_stream_write_data(stream, l_jp2h_data, 8, p_manager) != 8) {
1725 opj_event_msg(p_manager, EVT_ERROR,
1726 "Stream error while writing JP2 Header box\n");
1727 l_result = OPJ_FALSE;
1731 l_current_writer = l_writers;
1732 for (i = 0; i < l_nb_pass; ++i) {
1733 if (opj_stream_write_data(stream, l_current_writer->m_data,
1734 l_current_writer->m_size, p_manager) != l_current_writer->m_size) {
1735 opj_event_msg(p_manager, EVT_ERROR,
1736 "Stream error while writing JP2 Header box\n");
1737 l_result = OPJ_FALSE;
1744 l_current_writer = l_writers;
1747 for (i = 0; i < l_nb_pass; ++i) {
1748 if (l_current_writer->m_data != 00) {
1749 opj_free(l_current_writer->m_data);
1757 static OPJ_BOOL opj_jp2_write_ftyp(opj_jp2_t *jp2,
1758 opj_stream_private_t *cio,
1759 opj_event_mgr_t * p_manager)
1762 OPJ_UINT32 l_ftyp_size;
1763 OPJ_BYTE * l_ftyp_data, * l_current_data_ptr;
1769 assert(p_manager != 00);
1770 l_ftyp_size = 16 + 4 * jp2->numcl;
1772 l_ftyp_data = (OPJ_BYTE *) opj_calloc(1, l_ftyp_size);
1774 if (l_ftyp_data == 00) {
1775 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to handle ftyp data\n");
1779 l_current_data_ptr = l_ftyp_data;
1781 opj_write_bytes(l_current_data_ptr, l_ftyp_size, 4); /* box size */
1782 l_current_data_ptr += 4;
1784 opj_write_bytes(l_current_data_ptr, JP2_FTYP, 4); /* FTYP */
1785 l_current_data_ptr += 4;
1787 opj_write_bytes(l_current_data_ptr, jp2->brand, 4); /* BR */
1788 l_current_data_ptr += 4;
1790 opj_write_bytes(l_current_data_ptr, jp2->minversion, 4); /* MinV */
1791 l_current_data_ptr += 4;
1793 for (i = 0; i < jp2->numcl; i++) {
1794 opj_write_bytes(l_current_data_ptr, jp2->cl[i], 4); /* CL */
1797 l_result = (opj_stream_write_data(cio, l_ftyp_data, l_ftyp_size,
1798 p_manager) == l_ftyp_size);
1800 opj_event_msg(p_manager, EVT_ERROR,
1801 "Error while writing ftyp data to stream\n");
1804 opj_free(l_ftyp_data);
1809 static OPJ_BOOL opj_jp2_write_jp2c(opj_jp2_t *jp2,
1810 opj_stream_private_t *cio,
1811 opj_event_mgr_t * p_manager)
1813 OPJ_OFF_T j2k_codestream_exit;
1814 OPJ_BYTE l_data_header [8];
1819 assert(p_manager != 00);
1820 assert(opj_stream_has_seek(cio));
1822 j2k_codestream_exit = opj_stream_tell(cio);
1823 opj_write_bytes(l_data_header,
1824 (OPJ_UINT32)(j2k_codestream_exit - jp2->j2k_codestream_offset),
1825 4); /* size of codestream */
1826 opj_write_bytes(l_data_header + 4, JP2_JP2C,
1829 if (! opj_stream_seek(cio, jp2->j2k_codestream_offset, p_manager)) {
1830 opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
1834 if (opj_stream_write_data(cio, l_data_header, 8, p_manager) != 8) {
1835 opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
1839 if (! opj_stream_seek(cio, j2k_codestream_exit, p_manager)) {
1840 opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
1847 static OPJ_BOOL opj_jp2_write_jp(opj_jp2_t *jp2,
1848 opj_stream_private_t *cio,
1849 opj_event_mgr_t * p_manager)
1851 /* 12 bytes will be read */
1852 OPJ_BYTE l_signature_data [12];
1857 assert(p_manager != 00);
1861 /* write box length */
1862 opj_write_bytes(l_signature_data, 12, 4);
1863 /* writes box type */
1864 opj_write_bytes(l_signature_data + 4, JP2_JP, 4);
1865 /* writes magic number*/
1866 opj_write_bytes(l_signature_data + 8, 0x0d0a870a, 4);
1868 if (opj_stream_write_data(cio, l_signature_data, 12, p_manager) != 12) {
1875 /* ----------------------------------------------------------------------- */
1876 /* JP2 decoder interface */
1877 /* ----------------------------------------------------------------------- */
1879 void opj_jp2_setup_decoder(opj_jp2_t *jp2, opj_dparameters_t *parameters)
1881 /* setup the J2K codec */
1882 opj_j2k_setup_decoder(jp2->j2k, parameters);
1884 /* further JP2 initializations go here */
1885 jp2->color.jp2_has_colr = 0;
1886 jp2->ignore_pclr_cmap_cdef = parameters->flags &
1887 OPJ_DPARAMETERS_IGNORE_PCLR_CMAP_CDEF_FLAG;
1890 void opj_jp2_decoder_set_strict_mode(opj_jp2_t *jp2, OPJ_BOOL strict)
1892 opj_j2k_decoder_set_strict_mode(jp2->j2k, strict);
1895 OPJ_BOOL opj_jp2_set_threads(opj_jp2_t *jp2, OPJ_UINT32 num_threads)
1897 return opj_j2k_set_threads(jp2->j2k, num_threads);
1900 /* ----------------------------------------------------------------------- */
1901 /* JP2 encoder interface */
1902 /* ----------------------------------------------------------------------- */
1904 OPJ_BOOL opj_jp2_setup_encoder(opj_jp2_t *jp2,
1905 opj_cparameters_t *parameters,
1907 opj_event_mgr_t * p_manager)
1912 OPJ_UINT32 alpha_count;
1913 OPJ_UINT32 color_channels = 0U;
1914 OPJ_UINT32 alpha_channel = 0U;
1917 if (!jp2 || !parameters || !image) {
1921 /* setup the J2K codec */
1922 /* ------------------- */
1924 /* Check if number of components respects standard */
1925 if (image->numcomps < 1 || image->numcomps > 16384) {
1926 opj_event_msg(p_manager, EVT_ERROR,
1927 "Invalid number of components specified while setting up JP2 encoder\n");
1931 if (opj_j2k_setup_encoder(jp2->j2k, parameters, image,
1932 p_manager) == OPJ_FALSE) {
1936 /* setup the JP2 codec */
1937 /* ------------------- */
1941 jp2->brand = JP2_JP2; /* BR */
1942 jp2->minversion = 0; /* MinV */
1944 jp2->cl = (OPJ_UINT32*) opj_malloc(jp2->numcl * sizeof(OPJ_UINT32));
1946 opj_event_msg(p_manager, EVT_ERROR,
1947 "Not enough memory when setup the JP2 encoder\n");
1950 jp2->cl[0] = JP2_JP2; /* CL0 : JP2 */
1952 /* Image Header box */
1954 jp2->numcomps = image->numcomps; /* NC */
1955 jp2->comps = (opj_jp2_comps_t*) opj_malloc(jp2->numcomps * sizeof(
1958 opj_event_msg(p_manager, EVT_ERROR,
1959 "Not enough memory when setup the JP2 encoder\n");
1960 /* Memory of jp2->cl will be freed by opj_jp2_destroy */
1964 jp2->h = image->y1 - image->y0; /* HEIGHT */
1965 jp2->w = image->x1 - image->x0; /* WIDTH */
1967 depth_0 = image->comps[0].prec - 1;
1968 sign = image->comps[0].sgnd;
1969 jp2->bpc = depth_0 + (sign << 7);
1970 for (i = 1; i < image->numcomps; i++) {
1971 OPJ_UINT32 depth = image->comps[i].prec - 1;
1972 sign = image->comps[i].sgnd;
1973 if (depth_0 != depth) {
1977 jp2->C = 7; /* C : Always 7 */
1978 jp2->UnkC = 0; /* UnkC, colorspace specified in colr box */
1979 jp2->IPR = 0; /* IPR, no intellectual property */
1981 /* BitsPerComponent box */
1982 for (i = 0; i < image->numcomps; i++) {
1983 jp2->comps[i].bpcc = image->comps[i].prec - 1 + (image->comps[i].sgnd << 7);
1986 /* Colour Specification box */
1987 if (image->icc_profile_len) {
1992 if (image->color_space == 1) {
1993 jp2->enumcs = 16; /* sRGB as defined by IEC 61966-2-1 */
1994 } else if (image->color_space == 2) {
1995 jp2->enumcs = 17; /* greyscale */
1996 } else if (image->color_space == 3) {
1997 jp2->enumcs = 18; /* YUV */
2001 /* Channel Definition box */
2002 /* FIXME not provided by parameters */
2003 /* We try to do what we can... */
2005 for (i = 0; i < image->numcomps; i++) {
2006 if (image->comps[i].alpha != 0) {
2011 if (alpha_count == 1U) { /* no way to deal with more than 1 alpha channel */
2012 switch (jp2->enumcs) {
2024 if (alpha_count == 0U) {
2025 opj_event_msg(p_manager, EVT_WARNING,
2026 "Alpha channel specified but unknown enumcs. No cdef box will be created.\n");
2027 } else if (image->numcomps < (color_channels + 1)) {
2028 opj_event_msg(p_manager, EVT_WARNING,
2029 "Alpha channel specified but not enough image components for an automatic cdef box creation.\n");
2031 } else if ((OPJ_UINT32)alpha_channel < color_channels) {
2032 opj_event_msg(p_manager, EVT_WARNING,
2033 "Alpha channel position conflicts with color channel. No cdef box will be created.\n");
2036 } else if (alpha_count > 1) {
2037 opj_event_msg(p_manager, EVT_WARNING,
2038 "Multiple alpha channels specified. No cdef box will be created.\n");
2040 if (alpha_count == 1U) { /* if here, we know what we can do */
2041 jp2->color.jp2_cdef = (opj_jp2_cdef_t*)opj_malloc(sizeof(opj_jp2_cdef_t));
2042 if (!jp2->color.jp2_cdef) {
2043 opj_event_msg(p_manager, EVT_ERROR,
2044 "Not enough memory to setup the JP2 encoder\n");
2047 /* no memset needed, all values will be overwritten except if jp2->color.jp2_cdef->info allocation fails, */
2048 /* in which case jp2->color.jp2_cdef->info will be NULL => valid for destruction */
2049 jp2->color.jp2_cdef->info = (opj_jp2_cdef_info_t*) opj_malloc(
2050 image->numcomps * sizeof(opj_jp2_cdef_info_t));
2051 if (!jp2->color.jp2_cdef->info) {
2052 /* memory will be freed by opj_jp2_destroy */
2053 opj_event_msg(p_manager, EVT_ERROR,
2054 "Not enough memory to setup the JP2 encoder\n");
2057 jp2->color.jp2_cdef->n = (OPJ_UINT16)
2058 image->numcomps; /* cast is valid : image->numcomps [1,16384] */
2059 for (i = 0U; i < color_channels; i++) {
2060 jp2->color.jp2_cdef->info[i].cn = (OPJ_UINT16)
2061 i; /* cast is valid : image->numcomps [1,16384] */
2062 jp2->color.jp2_cdef->info[i].typ = 0U;
2063 jp2->color.jp2_cdef->info[i].asoc = (OPJ_UINT16)(i +
2064 1U); /* No overflow + cast is valid : image->numcomps [1,16384] */
2066 for (; i < image->numcomps; i++) {
2067 if (image->comps[i].alpha != 0) { /* we'll be here exactly once */
2068 jp2->color.jp2_cdef->info[i].cn = (OPJ_UINT16)
2069 i; /* cast is valid : image->numcomps [1,16384] */
2070 jp2->color.jp2_cdef->info[i].typ = 1U; /* Opacity channel */
2071 jp2->color.jp2_cdef->info[i].asoc =
2072 0U; /* Apply alpha channel to the whole image */
2074 /* Unknown channel */
2075 jp2->color.jp2_cdef->info[i].cn = (OPJ_UINT16)
2076 i; /* cast is valid : image->numcomps [1,16384] */
2077 jp2->color.jp2_cdef->info[i].typ = 65535U;
2078 jp2->color.jp2_cdef->info[i].asoc = 65535U;
2083 jp2->precedence = 0; /* PRECEDENCE */
2084 jp2->approx = 0; /* APPROX */
2086 jp2->jpip_on = parameters->jpip_on;
2091 OPJ_BOOL opj_jp2_encode(opj_jp2_t *jp2,
2092 opj_stream_private_t *stream,
2093 opj_event_mgr_t * p_manager)
2095 return opj_j2k_encode(jp2->j2k, stream, p_manager);
2098 OPJ_BOOL opj_jp2_end_decompress(opj_jp2_t *jp2,
2099 opj_stream_private_t *cio,
2100 opj_event_mgr_t * p_manager
2106 assert(p_manager != 00);
2108 /* customization of the end encoding */
2109 if (! opj_jp2_setup_end_header_reading(jp2, p_manager)) {
2114 if (! opj_jp2_exec(jp2, jp2->m_procedure_list, cio, p_manager)) {
2118 return opj_j2k_end_decompress(jp2->j2k, cio, p_manager);
2121 OPJ_BOOL opj_jp2_end_compress(opj_jp2_t *jp2,
2122 opj_stream_private_t *cio,
2123 opj_event_mgr_t * p_manager
2129 assert(p_manager != 00);
2131 /* customization of the end encoding */
2132 if (! opj_jp2_setup_end_header_writing(jp2, p_manager)) {
2136 if (! opj_j2k_end_compress(jp2->j2k, cio, p_manager)) {
2141 return opj_jp2_exec(jp2, jp2->m_procedure_list, cio, p_manager);
2144 static OPJ_BOOL opj_jp2_setup_end_header_writing(opj_jp2_t *jp2,
2145 opj_event_mgr_t * p_manager)
2149 assert(p_manager != 00);
2153 if (! opj_procedure_list_add_procedure(jp2->m_procedure_list,
2154 (opj_procedure)opj_jpip_write_iptr, p_manager)) {
2159 if (! opj_procedure_list_add_procedure(jp2->m_procedure_list,
2160 (opj_procedure)opj_jp2_write_jp2c, p_manager)) {
2163 /* DEVELOPER CORNER, add your custom procedures */
2166 if (! opj_procedure_list_add_procedure(jp2->m_procedure_list,
2167 (opj_procedure)opj_jpip_write_cidx, p_manager)) {
2170 if (! opj_procedure_list_add_procedure(jp2->m_procedure_list,
2171 (opj_procedure)opj_jpip_write_fidx, p_manager)) {
2179 static OPJ_BOOL opj_jp2_setup_end_header_reading(opj_jp2_t *jp2,
2180 opj_event_mgr_t * p_manager)
2184 assert(p_manager != 00);
2186 if (! opj_procedure_list_add_procedure(jp2->m_procedure_list,
2187 (opj_procedure)opj_jp2_read_header_procedure, p_manager)) {
2190 /* DEVELOPER CORNER, add your custom procedures */
2195 static OPJ_BOOL opj_jp2_default_validation(opj_jp2_t * jp2,
2196 opj_stream_private_t *cio,
2197 opj_event_mgr_t * p_manager
2200 OPJ_BOOL l_is_valid = OPJ_TRUE;
2206 assert(p_manager != 00);
2208 OPJ_UNUSED(p_manager);
2210 /* JPEG2000 codec validation */
2212 /* STATE checking */
2213 /* make sure the state is at 0 */
2214 l_is_valid &= (jp2->jp2_state == JP2_STATE_NONE);
2216 /* make sure not reading a jp2h ???? WEIRD */
2217 l_is_valid &= (jp2->jp2_img_state == JP2_IMG_STATE_NONE);
2219 /* POINTER validation */
2220 /* make sure a j2k codec is present */
2221 l_is_valid &= (jp2->j2k != 00);
2223 /* make sure a procedure list is present */
2224 l_is_valid &= (jp2->m_procedure_list != 00);
2226 /* make sure a validation list is present */
2227 l_is_valid &= (jp2->m_validation_list != 00);
2229 /* PARAMETER VALIDATION */
2230 /* number of components */
2231 l_is_valid &= (jp2->numcl > 0);
2233 l_is_valid &= (jp2->h > 0);
2235 l_is_valid &= (jp2->w > 0);
2237 for (i = 0; i < jp2->numcomps; ++i) {
2238 l_is_valid &= ((jp2->comps[i].bpcc & 0x7FU) <
2239 38U); /* 0 is valid, ignore sign for check */
2243 l_is_valid &= ((jp2->meth > 0) && (jp2->meth < 3));
2245 /* stream validation */
2246 /* back and forth is needed */
2247 l_is_valid &= opj_stream_has_seek(cio);
2252 static OPJ_BOOL opj_jp2_read_header_procedure(opj_jp2_t *jp2,
2253 opj_stream_private_t *stream,
2254 opj_event_mgr_t * p_manager
2258 OPJ_UINT32 l_nb_bytes_read;
2259 const opj_jp2_header_handler_t * l_current_handler;
2260 const opj_jp2_header_handler_t * l_current_handler_misplaced;
2261 OPJ_UINT32 l_last_data_size = OPJ_BOX_SIZE;
2262 OPJ_UINT32 l_current_data_size;
2263 OPJ_BYTE * l_current_data = 00;
2266 assert(stream != 00);
2268 assert(p_manager != 00);
2270 l_current_data = (OPJ_BYTE*)opj_calloc(1, l_last_data_size);
2272 if (l_current_data == 00) {
2273 opj_event_msg(p_manager, EVT_ERROR,
2274 "Not enough memory to handle jpeg2000 file header\n");
2278 while (opj_jp2_read_boxhdr(&box, &l_nb_bytes_read, stream, p_manager)) {
2279 /* is it the codestream box ? */
2280 if (box.type == JP2_JP2C) {
2281 if (jp2->jp2_state & JP2_STATE_HEADER) {
2282 jp2->jp2_state |= JP2_STATE_CODESTREAM;
2283 opj_free(l_current_data);
2286 opj_event_msg(p_manager, EVT_ERROR, "bad placed jpeg codestream\n");
2287 opj_free(l_current_data);
2290 } else if (box.length == 0) {
2291 opj_event_msg(p_manager, EVT_ERROR, "Cannot handle box of undefined sizes\n");
2292 opj_free(l_current_data);
2295 /* testcase 1851.pdf.SIGSEGV.ce9.948 */
2296 else if (box.length < l_nb_bytes_read) {
2297 opj_event_msg(p_manager, EVT_ERROR, "invalid box size %d (%x)\n", box.length,
2299 opj_free(l_current_data);
2303 l_current_handler = opj_jp2_find_handler(box.type);
2304 l_current_handler_misplaced = opj_jp2_img_find_handler(box.type);
2305 l_current_data_size = box.length - l_nb_bytes_read;
2307 if ((l_current_handler != 00) || (l_current_handler_misplaced != 00)) {
2308 if (l_current_handler == 00) {
2309 opj_event_msg(p_manager, EVT_WARNING,
2310 "Found a misplaced '%c%c%c%c' box outside jp2h box\n",
2311 (OPJ_BYTE)(box.type >> 24), (OPJ_BYTE)(box.type >> 16),
2312 (OPJ_BYTE)(box.type >> 8), (OPJ_BYTE)(box.type >> 0));
2313 if (jp2->jp2_state & JP2_STATE_HEADER) {
2314 /* read anyway, we already have jp2h */
2315 l_current_handler = l_current_handler_misplaced;
2317 opj_event_msg(p_manager, EVT_WARNING,
2318 "JPEG2000 Header box not read yet, '%c%c%c%c' box will be ignored\n",
2319 (OPJ_BYTE)(box.type >> 24), (OPJ_BYTE)(box.type >> 16),
2320 (OPJ_BYTE)(box.type >> 8), (OPJ_BYTE)(box.type >> 0));
2321 jp2->jp2_state |= JP2_STATE_UNKNOWN;
2322 if (opj_stream_skip(stream, l_current_data_size,
2323 p_manager) != l_current_data_size) {
2324 opj_event_msg(p_manager, EVT_ERROR,
2325 "Problem with skipping JPEG2000 box, stream error\n");
2326 opj_free(l_current_data);
2332 if ((OPJ_OFF_T)l_current_data_size > opj_stream_get_number_byte_left(stream)) {
2333 /* do not even try to malloc if we can't read */
2334 opj_event_msg(p_manager, EVT_ERROR,
2335 "Invalid box size %d for box '%c%c%c%c'. Need %d bytes, %d bytes remaining \n",
2336 box.length, (OPJ_BYTE)(box.type >> 24), (OPJ_BYTE)(box.type >> 16),
2337 (OPJ_BYTE)(box.type >> 8), (OPJ_BYTE)(box.type >> 0), l_current_data_size,
2338 (OPJ_UINT32)opj_stream_get_number_byte_left(stream));
2339 opj_free(l_current_data);
2342 if (l_current_data_size > l_last_data_size) {
2343 OPJ_BYTE* new_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,
2344 l_current_data_size);
2345 if (!new_current_data) {
2346 opj_free(l_current_data);
2347 opj_event_msg(p_manager, EVT_ERROR,
2348 "Not enough memory to handle jpeg2000 box\n");
2351 l_current_data = new_current_data;
2352 l_last_data_size = l_current_data_size;
2355 l_nb_bytes_read = (OPJ_UINT32)opj_stream_read_data(stream, l_current_data,
2356 l_current_data_size, p_manager);
2357 if (l_nb_bytes_read != l_current_data_size) {
2358 opj_event_msg(p_manager, EVT_ERROR,
2359 "Problem with reading JPEG2000 box, stream error\n");
2360 opj_free(l_current_data);
2364 if (! l_current_handler->handler(jp2, l_current_data, l_current_data_size,
2366 opj_free(l_current_data);
2370 if (!(jp2->jp2_state & JP2_STATE_SIGNATURE)) {
2371 opj_event_msg(p_manager, EVT_ERROR,
2372 "Malformed JP2 file format: first box must be JPEG 2000 signature box\n");
2373 opj_free(l_current_data);
2376 if (!(jp2->jp2_state & JP2_STATE_FILE_TYPE)) {
2377 opj_event_msg(p_manager, EVT_ERROR,
2378 "Malformed JP2 file format: second box must be file type box\n");
2379 opj_free(l_current_data);
2382 jp2->jp2_state |= JP2_STATE_UNKNOWN;
2383 if (opj_stream_skip(stream, l_current_data_size,
2384 p_manager) != l_current_data_size) {
2385 if (jp2->jp2_state & JP2_STATE_CODESTREAM) {
2386 /* If we already read the codestream, do not error out */
2387 /* Needed for data/input/nonregression/issue254.jp2 */
2388 opj_event_msg(p_manager, EVT_WARNING,
2389 "Problem with skipping JPEG2000 box, stream error\n");
2390 opj_free(l_current_data);
2393 opj_event_msg(p_manager, EVT_ERROR,
2394 "Problem with skipping JPEG2000 box, stream error\n");
2395 opj_free(l_current_data);
2402 opj_free(l_current_data);
2408 * Executes the given procedures on the given codec.
2410 * @param p_procedure_list the list of procedures to execute
2411 * @param jp2 the jpeg2000 file codec to execute the procedures on.
2412 * @param stream the stream to execute the procedures on.
2413 * @param p_manager the user manager.
2415 * @return true if all the procedures were successfully executed.
2417 static OPJ_BOOL opj_jp2_exec(opj_jp2_t * jp2,
2418 opj_procedure_list_t * p_procedure_list,
2419 opj_stream_private_t *stream,
2420 opj_event_mgr_t * p_manager
2424 OPJ_BOOL(** l_procedure)(opj_jp2_t * jp2, opj_stream_private_t *,
2425 opj_event_mgr_t *) = 00;
2426 OPJ_BOOL l_result = OPJ_TRUE;
2427 OPJ_UINT32 l_nb_proc, i;
2430 assert(p_procedure_list != 00);
2432 assert(stream != 00);
2433 assert(p_manager != 00);
2435 l_nb_proc = opj_procedure_list_get_nb_procedures(p_procedure_list);
2436 l_procedure = (OPJ_BOOL(**)(opj_jp2_t * jp2, opj_stream_private_t *,
2437 opj_event_mgr_t *)) opj_procedure_list_get_first_procedure(p_procedure_list);
2439 for (i = 0; i < l_nb_proc; ++i) {
2440 l_result = l_result && (*l_procedure)(jp2, stream, p_manager);
2444 /* and clear the procedure list at the end. */
2445 opj_procedure_list_clear(p_procedure_list);
2449 OPJ_BOOL opj_jp2_start_compress(opj_jp2_t *jp2,
2450 opj_stream_private_t *stream,
2451 opj_image_t * p_image,
2452 opj_event_mgr_t * p_manager
2457 assert(stream != 00);
2458 assert(p_manager != 00);
2460 /* customization of the validation */
2461 if (! opj_jp2_setup_encoding_validation(jp2, p_manager)) {
2465 /* validation of the parameters codec */
2466 if (! opj_jp2_exec(jp2, jp2->m_validation_list, stream, p_manager)) {
2470 /* customization of the encoding */
2471 if (! opj_jp2_setup_header_writing(jp2, p_manager)) {
2476 if (! opj_jp2_exec(jp2, jp2->m_procedure_list, stream, p_manager)) {
2480 return opj_j2k_start_compress(jp2->j2k, stream, p_image, p_manager);
2483 static const opj_jp2_header_handler_t * opj_jp2_find_handler(OPJ_UINT32 p_id)
2485 OPJ_UINT32 i, l_handler_size = sizeof(jp2_header) / sizeof(
2486 opj_jp2_header_handler_t);
2488 for (i = 0; i < l_handler_size; ++i) {
2489 if (jp2_header[i].id == p_id) {
2490 return &jp2_header[i];
2497 * Finds the image execution function related to the given box id.
2499 * @param p_id the id of the handler to fetch.
2501 * @return the given handler or 00 if it could not be found.
2503 static const opj_jp2_header_handler_t * opj_jp2_img_find_handler(
2506 OPJ_UINT32 i, l_handler_size = sizeof(jp2_img_header) / sizeof(
2507 opj_jp2_header_handler_t);
2508 for (i = 0; i < l_handler_size; ++i) {
2509 if (jp2_img_header[i].id == p_id) {
2510 return &jp2_img_header[i];
2518 * Reads a jpeg2000 file signature box.
2520 * @param p_header_data the data contained in the signature box.
2521 * @param jp2 the jpeg2000 file codec.
2522 * @param p_header_size the size of the data contained in the signature box.
2523 * @param p_manager the user event manager.
2525 * @return true if the file signature box is valid.
2527 static OPJ_BOOL opj_jp2_read_jp(opj_jp2_t *jp2,
2528 OPJ_BYTE * p_header_data,
2529 OPJ_UINT32 p_header_size,
2530 opj_event_mgr_t * p_manager
2534 OPJ_UINT32 l_magic_number;
2537 assert(p_header_data != 00);
2539 assert(p_manager != 00);
2541 if (jp2->jp2_state != JP2_STATE_NONE) {
2542 opj_event_msg(p_manager, EVT_ERROR,
2543 "The signature box must be the first box in the file.\n");
2547 /* assure length of data is correct (4 -> magic number) */
2548 if (p_header_size != 4) {
2549 opj_event_msg(p_manager, EVT_ERROR, "Error with JP signature Box size\n");
2553 /* rearrange data */
2554 opj_read_bytes(p_header_data, &l_magic_number, 4);
2555 if (l_magic_number != 0x0d0a870a) {
2556 opj_event_msg(p_manager, EVT_ERROR,
2557 "Error with JP Signature : bad magic number\n");
2561 jp2->jp2_state |= JP2_STATE_SIGNATURE;
2567 * Reads a a FTYP box - File type box
2569 * @param p_header_data the data contained in the FTYP box.
2570 * @param jp2 the jpeg2000 file codec.
2571 * @param p_header_size the size of the data contained in the FTYP box.
2572 * @param p_manager the user event manager.
2574 * @return true if the FTYP box is valid.
2576 static OPJ_BOOL opj_jp2_read_ftyp(opj_jp2_t *jp2,
2577 OPJ_BYTE * p_header_data,
2578 OPJ_UINT32 p_header_size,
2579 opj_event_mgr_t * p_manager
2582 OPJ_UINT32 i, l_remaining_bytes;
2585 assert(p_header_data != 00);
2587 assert(p_manager != 00);
2589 if (jp2->jp2_state != JP2_STATE_SIGNATURE) {
2590 opj_event_msg(p_manager, EVT_ERROR,
2591 "The ftyp box must be the second box in the file.\n");
2595 /* assure length of data is correct */
2596 if (p_header_size < 8) {
2597 opj_event_msg(p_manager, EVT_ERROR, "Error with FTYP signature Box size\n");
2601 opj_read_bytes(p_header_data, &jp2->brand, 4); /* BR */
2604 opj_read_bytes(p_header_data, &jp2->minversion, 4); /* MinV */
2607 l_remaining_bytes = p_header_size - 8;
2609 /* the number of remaining bytes should be a multiple of 4 */
2610 if ((l_remaining_bytes & 0x3) != 0) {
2611 opj_event_msg(p_manager, EVT_ERROR, "Error with FTYP signature Box size\n");
2616 jp2->numcl = l_remaining_bytes >> 2;
2618 jp2->cl = (OPJ_UINT32 *) opj_calloc(jp2->numcl, sizeof(OPJ_UINT32));
2619 if (jp2->cl == 00) {
2620 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory with FTYP Box\n");
2625 for (i = 0; i < jp2->numcl; ++i) {
2626 opj_read_bytes(p_header_data, &jp2->cl[i], 4); /* CLi */
2630 jp2->jp2_state |= JP2_STATE_FILE_TYPE;
2635 static OPJ_BOOL opj_jp2_skip_jp2c(opj_jp2_t *jp2,
2636 opj_stream_private_t *stream,
2637 opj_event_mgr_t * p_manager)
2641 assert(stream != 00);
2642 assert(p_manager != 00);
2644 jp2->j2k_codestream_offset = opj_stream_tell(stream);
2646 if (opj_stream_skip(stream, 8, p_manager) != 8) {
2653 static OPJ_BOOL opj_jpip_skip_iptr(opj_jp2_t *jp2,
2654 opj_stream_private_t *stream,
2655 opj_event_mgr_t * p_manager)
2659 assert(stream != 00);
2660 assert(p_manager != 00);
2662 jp2->jpip_iptr_offset = opj_stream_tell(stream);
2664 if (opj_stream_skip(stream, 24, p_manager) != 24) {
2672 * Reads the Jpeg2000 file Header box - JP2 Header box (warning, this is a super box).
2674 * @param p_header_data the data contained in the file header box.
2675 * @param jp2 the jpeg2000 file codec.
2676 * @param p_header_size the size of the data contained in the file header box.
2677 * @param p_manager the user event manager.
2679 * @return true if the JP2 Header box was successfully recognized.
2681 static OPJ_BOOL opj_jp2_read_jp2h(opj_jp2_t *jp2,
2682 OPJ_BYTE *p_header_data,
2683 OPJ_UINT32 p_header_size,
2684 opj_event_mgr_t * p_manager
2687 OPJ_UINT32 l_box_size = 0, l_current_data_size = 0;
2689 const opj_jp2_header_handler_t * l_current_handler;
2690 OPJ_BOOL l_has_ihdr = 0;
2693 assert(p_header_data != 00);
2695 assert(p_manager != 00);
2697 /* make sure the box is well placed */
2698 if ((jp2->jp2_state & JP2_STATE_FILE_TYPE) != JP2_STATE_FILE_TYPE) {
2699 opj_event_msg(p_manager, EVT_ERROR,
2700 "The box must be the first box in the file.\n");
2704 jp2->jp2_img_state = JP2_IMG_STATE_NONE;
2706 /* iterate while remaining data */
2707 while (p_header_size > 0) {
2709 if (! opj_jp2_read_boxhdr_char(&box, p_header_data, &l_box_size, p_header_size,
2711 opj_event_msg(p_manager, EVT_ERROR,
2712 "Stream error while reading JP2 Header box\n");
2716 if (box.length > p_header_size) {
2717 opj_event_msg(p_manager, EVT_ERROR,
2718 "Stream error while reading JP2 Header box: box length is inconsistent.\n");
2722 l_current_handler = opj_jp2_img_find_handler(box.type);
2723 l_current_data_size = box.length - l_box_size;
2724 p_header_data += l_box_size;
2726 if (l_current_handler != 00) {
2727 if (! l_current_handler->handler(jp2, p_header_data, l_current_data_size,
2732 jp2->jp2_img_state |= JP2_IMG_STATE_UNKNOWN;
2735 if (box.type == JP2_IHDR) {
2739 p_header_data += l_current_data_size;
2740 p_header_size -= box.length;
2743 if (l_has_ihdr == 0) {
2744 opj_event_msg(p_manager, EVT_ERROR,
2745 "Stream error while reading JP2 Header box: no 'ihdr' box.\n");
2749 jp2->jp2_state |= JP2_STATE_HEADER;
2755 static OPJ_BOOL opj_jp2_read_boxhdr_char(opj_jp2_box_t *box,
2757 OPJ_UINT32 * p_number_bytes_read,
2758 OPJ_UINT32 p_box_max_size,
2759 opj_event_mgr_t * p_manager
2765 assert(p_data != 00);
2767 assert(p_number_bytes_read != 00);
2768 assert(p_manager != 00);
2770 if (p_box_max_size < 8) {
2771 opj_event_msg(p_manager, EVT_ERROR, "Cannot handle box of less than 8 bytes\n");
2775 /* process read data */
2776 opj_read_bytes(p_data, &l_value, 4);
2778 box->length = (OPJ_UINT32)(l_value);
2780 opj_read_bytes(p_data, &l_value, 4);
2782 box->type = (OPJ_UINT32)(l_value);
2784 *p_number_bytes_read = 8;
2786 /* do we have a "special very large box ?" */
2787 /* read then the XLBox */
2788 if (box->length == 1) {
2789 OPJ_UINT32 l_xl_part_size;
2791 if (p_box_max_size < 16) {
2792 opj_event_msg(p_manager, EVT_ERROR,
2793 "Cannot handle XL box of less than 16 bytes\n");
2797 opj_read_bytes(p_data, &l_xl_part_size, 4);
2799 *p_number_bytes_read += 4;
2801 if (l_xl_part_size != 0) {
2802 opj_event_msg(p_manager, EVT_ERROR,
2803 "Cannot handle box sizes higher than 2^32\n");
2807 opj_read_bytes(p_data, &l_value, 4);
2808 *p_number_bytes_read += 4;
2809 box->length = (OPJ_UINT32)(l_value);
2811 if (box->length == 0) {
2812 opj_event_msg(p_manager, EVT_ERROR, "Cannot handle box of undefined sizes\n");
2815 } else if (box->length == 0) {
2816 opj_event_msg(p_manager, EVT_ERROR, "Cannot handle box of undefined sizes\n");
2819 if (box->length < *p_number_bytes_read) {
2820 opj_event_msg(p_manager, EVT_ERROR, "Box length is inconsistent.\n");
2826 OPJ_BOOL opj_jp2_read_header(opj_stream_private_t *p_stream,
2828 opj_image_t ** p_image,
2829 opj_event_mgr_t * p_manager
2836 assert(p_stream != 00);
2837 assert(p_manager != 00);
2839 /* customization of the validation */
2840 if (! opj_jp2_setup_decoding_validation(jp2, p_manager)) {
2844 /* customization of the encoding */
2845 if (! opj_jp2_setup_header_reading(jp2, p_manager)) {
2849 /* validation of the parameters codec */
2850 if (! opj_jp2_exec(jp2, jp2->m_validation_list, p_stream, p_manager)) {
2855 if (! opj_jp2_exec(jp2, jp2->m_procedure_list, p_stream, p_manager)) {
2858 if (jp2->has_jp2h == 0) {
2859 opj_event_msg(p_manager, EVT_ERROR, "JP2H box missing. Required.\n");
2862 if (jp2->has_ihdr == 0) {
2863 opj_event_msg(p_manager, EVT_ERROR, "IHDR box_missing. Required.\n");
2867 ret = opj_j2k_read_header(p_stream,
2872 if (p_image && *p_image) {
2873 /* Set Image Color Space */
2874 if (jp2->enumcs == 16) {
2875 (*p_image)->color_space = OPJ_CLRSPC_SRGB;
2876 } else if (jp2->enumcs == 17) {
2877 (*p_image)->color_space = OPJ_CLRSPC_GRAY;
2878 } else if (jp2->enumcs == 18) {
2879 (*p_image)->color_space = OPJ_CLRSPC_SYCC;
2880 } else if (jp2->enumcs == 24) {
2881 (*p_image)->color_space = OPJ_CLRSPC_EYCC;
2882 } else if (jp2->enumcs == 12) {
2883 (*p_image)->color_space = OPJ_CLRSPC_CMYK;
2885 (*p_image)->color_space = OPJ_CLRSPC_UNKNOWN;
2888 if (jp2->color.icc_profile_buf) {
2889 (*p_image)->icc_profile_buf = jp2->color.icc_profile_buf;
2890 (*p_image)->icc_profile_len = jp2->color.icc_profile_len;
2891 jp2->color.icc_profile_buf = NULL;
2897 static OPJ_BOOL opj_jp2_setup_encoding_validation(opj_jp2_t *jp2,
2898 opj_event_mgr_t * p_manager)
2902 assert(p_manager != 00);
2904 if (! opj_procedure_list_add_procedure(jp2->m_validation_list,
2905 (opj_procedure)opj_jp2_default_validation, p_manager)) {
2908 /* DEVELOPER CORNER, add your custom validation procedure */
2913 static OPJ_BOOL opj_jp2_setup_decoding_validation(opj_jp2_t *jp2,
2914 opj_event_mgr_t * p_manager)
2918 assert(p_manager != 00);
2921 OPJ_UNUSED(p_manager);
2923 /* DEVELOPER CORNER, add your custom validation procedure */
2928 static OPJ_BOOL opj_jp2_setup_header_writing(opj_jp2_t *jp2,
2929 opj_event_mgr_t * p_manager)
2933 assert(p_manager != 00);
2935 if (! opj_procedure_list_add_procedure(jp2->m_procedure_list,
2936 (opj_procedure)opj_jp2_write_jp, p_manager)) {
2939 if (! opj_procedure_list_add_procedure(jp2->m_procedure_list,
2940 (opj_procedure)opj_jp2_write_ftyp, p_manager)) {
2943 if (! opj_procedure_list_add_procedure(jp2->m_procedure_list,
2944 (opj_procedure)opj_jp2_write_jp2h, p_manager)) {
2948 if (! opj_procedure_list_add_procedure(jp2->m_procedure_list,
2949 (opj_procedure)opj_jpip_skip_iptr, p_manager)) {
2953 if (! opj_procedure_list_add_procedure(jp2->m_procedure_list,
2954 (opj_procedure)opj_jp2_skip_jp2c, p_manager)) {
2958 /* DEVELOPER CORNER, insert your custom procedures */
2963 static OPJ_BOOL opj_jp2_setup_header_reading(opj_jp2_t *jp2,
2964 opj_event_mgr_t * p_manager)
2968 assert(p_manager != 00);
2970 if (! opj_procedure_list_add_procedure(jp2->m_procedure_list,
2971 (opj_procedure)opj_jp2_read_header_procedure, p_manager)) {
2975 /* DEVELOPER CORNER, add your custom procedures */
2980 OPJ_BOOL opj_jp2_read_tile_header(opj_jp2_t * p_jp2,
2981 OPJ_UINT32 * p_tile_index,
2982 OPJ_UINT32 * p_data_size,
2983 OPJ_INT32 * p_tile_x0,
2984 OPJ_INT32 * p_tile_y0,
2985 OPJ_INT32 * p_tile_x1,
2986 OPJ_INT32 * p_tile_y1,
2987 OPJ_UINT32 * p_nb_comps,
2989 opj_stream_private_t *p_stream,
2990 opj_event_mgr_t * p_manager
2993 return opj_j2k_read_tile_header(p_jp2->j2k,
2996 p_tile_x0, p_tile_y0,
2997 p_tile_x1, p_tile_y1,
3004 OPJ_BOOL opj_jp2_write_tile(opj_jp2_t *p_jp2,
3005 OPJ_UINT32 p_tile_index,
3007 OPJ_UINT32 p_data_size,
3008 opj_stream_private_t *p_stream,
3009 opj_event_mgr_t * p_manager
3013 return opj_j2k_write_tile(p_jp2->j2k, p_tile_index, p_data, p_data_size,
3014 p_stream, p_manager);
3017 OPJ_BOOL opj_jp2_decode_tile(opj_jp2_t * p_jp2,
3018 OPJ_UINT32 p_tile_index,
3020 OPJ_UINT32 p_data_size,
3021 opj_stream_private_t *p_stream,
3022 opj_event_mgr_t * p_manager
3025 return opj_j2k_decode_tile(p_jp2->j2k, p_tile_index, p_data, p_data_size,
3026 p_stream, p_manager);
3029 void opj_jp2_destroy(opj_jp2_t *jp2)
3032 /* destroy the J2K codec */
3033 opj_j2k_destroy(jp2->j2k);
3037 opj_free(jp2->comps);
3046 if (jp2->color.icc_profile_buf) {
3047 opj_free(jp2->color.icc_profile_buf);
3048 jp2->color.icc_profile_buf = 00;
3051 if (jp2->color.jp2_cdef) {
3052 if (jp2->color.jp2_cdef->info) {
3053 opj_free(jp2->color.jp2_cdef->info);
3054 jp2->color.jp2_cdef->info = NULL;
3057 opj_free(jp2->color.jp2_cdef);
3058 jp2->color.jp2_cdef = 00;
3061 if (jp2->color.jp2_pclr) {
3062 if (jp2->color.jp2_pclr->cmap) {
3063 opj_free(jp2->color.jp2_pclr->cmap);
3064 jp2->color.jp2_pclr->cmap = NULL;
3066 if (jp2->color.jp2_pclr->channel_sign) {
3067 opj_free(jp2->color.jp2_pclr->channel_sign);
3068 jp2->color.jp2_pclr->channel_sign = NULL;
3070 if (jp2->color.jp2_pclr->channel_size) {
3071 opj_free(jp2->color.jp2_pclr->channel_size);
3072 jp2->color.jp2_pclr->channel_size = NULL;
3074 if (jp2->color.jp2_pclr->entries) {
3075 opj_free(jp2->color.jp2_pclr->entries);
3076 jp2->color.jp2_pclr->entries = NULL;
3079 opj_free(jp2->color.jp2_pclr);
3080 jp2->color.jp2_pclr = 00;
3083 if (jp2->m_validation_list) {
3084 opj_procedure_list_destroy(jp2->m_validation_list);
3085 jp2->m_validation_list = 00;
3088 if (jp2->m_procedure_list) {
3089 opj_procedure_list_destroy(jp2->m_procedure_list);
3090 jp2->m_procedure_list = 00;
3097 OPJ_BOOL opj_jp2_set_decoded_components(opj_jp2_t *p_jp2,
3098 OPJ_UINT32 numcomps,
3099 const OPJ_UINT32* comps_indices,
3100 opj_event_mgr_t * p_manager)
3102 return opj_j2k_set_decoded_components(p_jp2->j2k,
3103 numcomps, comps_indices,
3107 OPJ_BOOL opj_jp2_set_decode_area(opj_jp2_t *p_jp2,
3108 opj_image_t* p_image,
3109 OPJ_INT32 p_start_x, OPJ_INT32 p_start_y,
3110 OPJ_INT32 p_end_x, OPJ_INT32 p_end_y,
3111 opj_event_mgr_t * p_manager
3114 return opj_j2k_set_decode_area(p_jp2->j2k, p_image, p_start_x, p_start_y,
3115 p_end_x, p_end_y, p_manager);
3118 OPJ_BOOL opj_jp2_get_tile(opj_jp2_t *p_jp2,
3119 opj_stream_private_t *p_stream,
3120 opj_image_t* p_image,
3121 opj_event_mgr_t * p_manager,
3122 OPJ_UINT32 tile_index
3129 opj_event_msg(p_manager, EVT_WARNING,
3130 "JP2 box which are after the codestream will not be read by this function.\n");
3132 if (! opj_j2k_get_tile(p_jp2->j2k, p_stream, p_image, p_manager, tile_index)) {
3133 opj_event_msg(p_manager, EVT_ERROR,
3134 "Failed to decode the codestream in the JP2 file\n");
3138 return opj_jp2_apply_color_postprocessing(p_jp2, p_image, p_manager);
3141 /* ----------------------------------------------------------------------- */
3142 /* JP2 encoder interface */
3143 /* ----------------------------------------------------------------------- */
3145 opj_jp2_t* opj_jp2_create(OPJ_BOOL p_is_decoder)
3147 opj_jp2_t *jp2 = (opj_jp2_t*)opj_calloc(1, sizeof(opj_jp2_t));
3150 /* create the J2K codec */
3151 if (! p_is_decoder) {
3152 jp2->j2k = opj_j2k_create_compress();
3154 jp2->j2k = opj_j2k_create_decompress();
3157 if (jp2->j2k == 00) {
3158 opj_jp2_destroy(jp2);
3162 /* Color structure */
3163 jp2->color.icc_profile_buf = NULL;
3164 jp2->color.icc_profile_len = 0;
3165 jp2->color.jp2_cdef = NULL;
3166 jp2->color.jp2_pclr = NULL;
3167 jp2->color.jp2_has_colr = 0;
3169 /* validation list creation */
3170 jp2->m_validation_list = opj_procedure_list_create();
3171 if (! jp2->m_validation_list) {
3172 opj_jp2_destroy(jp2);
3176 /* execution list creation */
3177 jp2->m_procedure_list = opj_procedure_list_create();
3178 if (! jp2->m_procedure_list) {
3179 opj_jp2_destroy(jp2);
3187 void jp2_dump(opj_jp2_t* p_jp2, OPJ_INT32 flag, FILE* out_stream)
3190 assert(p_jp2 != 00);
3192 j2k_dump(p_jp2->j2k,
3197 opj_codestream_index_t* jp2_get_cstr_index(opj_jp2_t* p_jp2)
3199 return j2k_get_cstr_index(p_jp2->j2k);
3202 opj_codestream_info_v2_t* jp2_get_cstr_info(opj_jp2_t* p_jp2)
3204 return j2k_get_cstr_info(p_jp2->j2k);
3207 OPJ_BOOL opj_jp2_set_decoded_resolution_factor(opj_jp2_t *p_jp2,
3208 OPJ_UINT32 res_factor,
3209 opj_event_mgr_t * p_manager)
3211 return opj_j2k_set_decoded_resolution_factor(p_jp2->j2k, res_factor, p_manager);
3214 /* ----------------------------------------------------------------------- */
3216 OPJ_BOOL opj_jp2_encoder_set_extra_options(
3218 const char* const* p_options,
3219 opj_event_mgr_t * p_manager)
3221 return opj_j2k_encoder_set_extra_options(p_jp2->j2k, p_options, p_manager);
3224 /* ----------------------------------------------------------------------- */
3229 static OPJ_BOOL opj_jpip_write_iptr(opj_jp2_t *jp2,
3230 opj_stream_private_t *cio,
3231 opj_event_mgr_t * p_manager)
3233 OPJ_OFF_T j2k_codestream_exit;
3234 OPJ_BYTE l_data_header [24];
3239 assert(p_manager != 00);
3240 assert(opj_stream_has_seek(cio));
3242 j2k_codestream_exit = opj_stream_tell(cio);
3243 opj_write_bytes(l_data_header, 24, 4); /* size of iptr */
3244 opj_write_bytes(l_data_header + 4, JPIP_IPTR,
3247 opj_write_bytes(l_data_header + 4 + 4, 0, 8); /* offset */
3248 opj_write_bytes(l_data_header + 8 + 8, 0, 8); /* length */
3250 opj_write_double(l_data_header + 4 + 4, 0); /* offset */
3251 opj_write_double(l_data_header + 8 + 8, 0); /* length */
3254 if (! opj_stream_seek(cio, jp2->jpip_iptr_offset, p_manager)) {
3255 opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
3259 if (opj_stream_write_data(cio, l_data_header, 24, p_manager) != 24) {
3260 opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
3264 if (! opj_stream_seek(cio, j2k_codestream_exit, p_manager)) {
3265 opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
3272 static OPJ_BOOL opj_jpip_write_fidx(opj_jp2_t *jp2,
3273 opj_stream_private_t *cio,
3274 opj_event_mgr_t * p_manager)
3276 OPJ_OFF_T j2k_codestream_exit;
3277 OPJ_BYTE l_data_header [24];
3284 assert(p_manager != 00);
3285 assert(opj_stream_has_seek(cio));
3287 opj_write_bytes(l_data_header, 24, 4); /* size of iptr */
3288 opj_write_bytes(l_data_header + 4, JPIP_FIDX,
3290 opj_write_double(l_data_header + 4 + 4, 0); /* offset */
3291 opj_write_double(l_data_header + 8 + 8, 0); /* length */
3293 if (opj_stream_write_data(cio, l_data_header, 24, p_manager) != 24) {
3294 opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
3298 j2k_codestream_exit = opj_stream_tell(cio);
3299 if (! opj_stream_seek(cio, j2k_codestream_exit, p_manager)) {
3300 opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
3307 static OPJ_BOOL opj_jpip_write_cidx(opj_jp2_t *jp2,
3308 opj_stream_private_t *cio,
3309 opj_event_mgr_t * p_manager)
3311 OPJ_OFF_T j2k_codestream_exit;
3312 OPJ_BYTE l_data_header [24];
3319 assert(p_manager != 00);
3320 assert(opj_stream_has_seek(cio));
3322 j2k_codestream_exit = opj_stream_tell(cio);
3323 opj_write_bytes(l_data_header, 24, 4); /* size of iptr */
3324 opj_write_bytes(l_data_header + 4, JPIP_CIDX,
3327 opj_write_bytes(l_data_header + 4 + 4, 0, 8); /* offset */
3328 opj_write_bytes(l_data_header + 8 + 8, 0, 8); /* length */
3330 opj_write_double(l_data_header + 4 + 4, 0); /* offset */
3331 opj_write_double(l_data_header + 8 + 8, 0); /* length */
3334 if (! opj_stream_seek(cio, j2k_codestream_exit, p_manager)) {
3335 opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
3339 if (opj_stream_write_data(cio, l_data_header, 24, p_manager) != 24) {
3340 opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
3344 j2k_codestream_exit = opj_stream_tell(cio);
3345 if (! opj_stream_seek(cio, j2k_codestream_exit, p_manager)) {
3346 opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
3354 static void write_prxy(int offset_jp2c, int length_jp2c, int offset_idx,
3355 int length_idx, opj_stream_private_t *cio,
3356 opj_event_mgr_t * p_manager)
3358 OPJ_BYTE l_data_header [8];
3359 OPJ_OFF_T len, lenp;
3361 lenp = opj_stream_tell(cio);
3362 opj_stream_skip(cio, 4, p_manager); /* L [at the end] */
3363 opj_write_bytes(l_data_header, JPIP_PRXY, 4); /* IPTR */
3364 opj_stream_write_data(cio, l_data_header, 4, p_manager);
3366 opj_write_bytes(l_data_header, offset_jp2c, 8); /* OOFF */
3367 opj_stream_write_data(cio, l_data_header, 8, p_manager);
3368 opj_write_bytes(l_data_header, length_jp2c, 4); /* OBH part 1 */
3369 opj_write_bytes(l_data_header + 4, JP2_JP2C, 4); /* OBH part 2 */
3370 opj_stream_write_data(cio, l_data_header, 8, p_manager);
3372 opj_write_bytes(l_data_header, 1, 1); /* NI */
3373 opj_stream_write_data(cio, l_data_header, 1, p_manager);
3375 opj_write_bytes(l_data_header, offset_idx, 8); /* IOFF */
3376 opj_stream_write_data(cio, l_data_header, 8, p_manager);
3377 opj_write_bytes(l_data_header, length_idx, 4); /* IBH part 1 */
3378 opj_write_bytes(l_data_header + 4, JPIP_CIDX, 4); /* IBH part 2 */
3379 opj_stream_write_data(cio, l_data_header, 8, p_manager);
3381 len = opj_stream_tell(cio) - lenp;
3382 opj_stream_skip(cio, lenp, p_manager);
3383 opj_write_bytes(l_data_header, len, 4); /* L */
3384 opj_stream_write_data(cio, l_data_header, 4, p_manager);
3385 opj_stream_seek(cio, lenp + len, p_manager);
3391 static int write_fidx(int offset_jp2c, int length_jp2c, int offset_idx,
3392 int length_idx, opj_stream_private_t *cio,
3393 opj_event_mgr_t * p_manager)
3395 OPJ_BYTE l_data_header [4];
3396 OPJ_OFF_T len, lenp;
3398 lenp = opj_stream_tell(cio);
3399 opj_stream_skip(cio, 4, p_manager);
3400 opj_write_bytes(l_data_header, JPIP_FIDX, 4); /* FIDX */
3401 opj_stream_write_data(cio, l_data_header, 4, p_manager);
3403 write_prxy(offset_jp2c, length_jp2c, offset_idx, length_idx, cio, p_manager);
3405 len = opj_stream_tell(cio) - lenp;
3406 opj_stream_skip(cio, lenp, p_manager);
3407 opj_write_bytes(l_data_header, len, 4); /* L */
3408 opj_stream_write_data(cio, l_data_header, 4, p_manager);
3409 opj_stream_seek(cio, lenp + len, p_manager);
3414 #endif /* USE_JPIP */