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
271 @param color Collector for profile, cdef and pclr data
274 static void opj_jp2_apply_pclr(opj_image_t *image, opj_jp2_color_t *color);
276 static void opj_jp2_free_pclr(opj_jp2_color_t *color);
279 * Collect palette data
281 * @param jp2 JP2 handle
282 * @param p_pclr_header_data FIXME DOC
283 * @param p_pclr_header_size FIXME DOC
286 * @return Returns true if successful, returns false otherwise
288 static OPJ_BOOL opj_jp2_read_pclr(opj_jp2_t *jp2,
289 OPJ_BYTE * p_pclr_header_data,
290 OPJ_UINT32 p_pclr_header_size,
291 opj_event_mgr_t * p_manager);
294 * Collect component mapping data
296 * @param jp2 JP2 handle
297 * @param p_cmap_header_data FIXME DOC
298 * @param p_cmap_header_size FIXME DOC
299 * @param p_manager FIXME DOC
301 * @return Returns true if successful, returns false otherwise
304 static OPJ_BOOL opj_jp2_read_cmap(opj_jp2_t * jp2,
305 OPJ_BYTE * p_cmap_header_data,
306 OPJ_UINT32 p_cmap_header_size,
307 opj_event_mgr_t * p_manager);
310 * Reads the Color Specification box.
312 * @param p_colr_header_data pointer to actual data (already read from file)
313 * @param jp2 the jpeg2000 file codec.
314 * @param p_colr_header_size the size of the color header
315 * @param p_manager the user event manager.
317 * @return true if the bpc header is valid, false else.
319 static OPJ_BOOL opj_jp2_read_colr(opj_jp2_t *jp2,
320 OPJ_BYTE * p_colr_header_data,
321 OPJ_UINT32 p_colr_header_size,
322 opj_event_mgr_t * p_manager);
329 * Sets up the procedures to do on writing header after the codestream.
330 * Developpers wanting to extend the library can add their own writing procedures.
332 static OPJ_BOOL opj_jp2_setup_end_header_writing(opj_jp2_t *jp2,
333 opj_event_mgr_t * p_manager);
336 * Sets up the procedures to do on reading header after the codestream.
337 * Developpers wanting to extend the library can add their own writing procedures.
339 static OPJ_BOOL opj_jp2_setup_end_header_reading(opj_jp2_t *jp2,
340 opj_event_mgr_t * p_manager);
343 * Reads a jpeg2000 file header structure.
345 * @param jp2 the jpeg2000 file header structure.
346 * @param stream the stream to read data from.
347 * @param p_manager the user event manager.
349 * @return true if the box is valid.
351 static OPJ_BOOL opj_jp2_read_header_procedure(opj_jp2_t *jp2,
352 opj_stream_private_t *stream,
353 opj_event_mgr_t * p_manager);
356 * Excutes the given procedures on the given codec.
358 * @param p_procedure_list the list of procedures to execute
359 * @param jp2 the jpeg2000 file codec to execute the procedures on.
360 * @param stream the stream to execute the procedures on.
361 * @param p_manager the user manager.
363 * @return true if all the procedures were successfully executed.
365 static OPJ_BOOL opj_jp2_exec(opj_jp2_t * jp2,
366 opj_procedure_list_t * p_procedure_list,
367 opj_stream_private_t *stream,
368 opj_event_mgr_t * p_manager);
371 * Reads a box header. The box is the way data is packed inside a jpeg2000 file structure.
373 * @param cio the input stream to read data from.
374 * @param box the box structure to fill.
375 * @param p_number_bytes_read pointer to an int that will store the number of bytes read from the stream (shoul usually be 2).
376 * @param p_manager user event manager.
378 * @return true if the box is recognized, false otherwise
380 static OPJ_BOOL opj_jp2_read_boxhdr(opj_jp2_box_t *box,
381 OPJ_UINT32 * p_number_bytes_read,
382 opj_stream_private_t *cio,
383 opj_event_mgr_t * p_manager);
386 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
387 * are valid. Developpers wanting to extend the library can add their own validation procedures.
389 static OPJ_BOOL opj_jp2_setup_encoding_validation(opj_jp2_t *jp2,
390 opj_event_mgr_t * p_manager);
393 * Sets up the procedures to do on writing header. Developpers wanting to extend the library can add their own writing procedures.
395 static OPJ_BOOL opj_jp2_setup_header_writing(opj_jp2_t *jp2,
396 opj_event_mgr_t * p_manager);
398 static OPJ_BOOL opj_jp2_default_validation(opj_jp2_t * jp2,
399 opj_stream_private_t *cio,
400 opj_event_mgr_t * p_manager);
403 * Finds the image execution function related to the given box id.
405 * @param p_id the id of the handler to fetch.
407 * @return the given handler or NULL if it could not be found.
409 static const opj_jp2_header_handler_t * opj_jp2_img_find_handler(
413 * Finds the execution function related to the given box id.
415 * @param p_id the id of the handler to fetch.
417 * @return the given handler or NULL if it could not be found.
419 static const opj_jp2_header_handler_t * opj_jp2_find_handler(OPJ_UINT32 p_id);
421 static const opj_jp2_header_handler_t jp2_header [] = {
422 {JP2_JP, opj_jp2_read_jp},
423 {JP2_FTYP, opj_jp2_read_ftyp},
424 {JP2_JP2H, opj_jp2_read_jp2h}
427 static const opj_jp2_header_handler_t jp2_img_header [] = {
428 {JP2_IHDR, opj_jp2_read_ihdr},
429 {JP2_COLR, opj_jp2_read_colr},
430 {JP2_BPCC, opj_jp2_read_bpcc},
431 {JP2_PCLR, opj_jp2_read_pclr},
432 {JP2_CMAP, opj_jp2_read_cmap},
433 {JP2_CDEF, opj_jp2_read_cdef}
438 * Reads a box header. The box is the way data is packed inside a jpeg2000 file structure. Data is read from a character string
440 * @param box the box structure to fill.
441 * @param p_data the character string to read data from.
442 * @param p_number_bytes_read pointer to an int that will store the number of bytes read from the stream (shoul usually be 2).
443 * @param p_box_max_size the maximum number of bytes in the box.
444 * @param p_manager FIXME DOC
446 * @return true if the box is recognized, false otherwise
448 static OPJ_BOOL opj_jp2_read_boxhdr_char(opj_jp2_box_t *box,
450 OPJ_UINT32 * p_number_bytes_read,
451 OPJ_UINT32 p_box_max_size,
452 opj_event_mgr_t * p_manager);
455 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
456 * are valid. Developpers wanting to extend the library can add their own validation procedures.
458 static OPJ_BOOL opj_jp2_setup_decoding_validation(opj_jp2_t *jp2,
459 opj_event_mgr_t * p_manager);
462 * Sets up the procedures to do on reading header.
463 * Developpers wanting to extend the library can add their own writing procedures.
465 static OPJ_BOOL opj_jp2_setup_header_reading(opj_jp2_t *jp2,
466 opj_event_mgr_t * p_manager);
468 /* ----------------------------------------------------------------------- */
469 static OPJ_BOOL opj_jp2_read_boxhdr(opj_jp2_box_t *box,
470 OPJ_UINT32 * p_number_bytes_read,
471 opj_stream_private_t *cio,
472 opj_event_mgr_t * p_manager)
474 /* read header from file */
475 OPJ_BYTE l_data_header [8];
480 assert(p_number_bytes_read != 00);
481 assert(p_manager != 00);
483 *p_number_bytes_read = (OPJ_UINT32)opj_stream_read_data(cio, l_data_header, 8,
485 if (*p_number_bytes_read != 8) {
489 /* process read data */
490 opj_read_bytes(l_data_header, &(box->length), 4);
491 opj_read_bytes(l_data_header + 4, &(box->type), 4);
493 if (box->length == 0) { /* last box */
494 const OPJ_OFF_T bleft = opj_stream_get_number_byte_left(cio);
495 if (bleft > (OPJ_OFF_T)(0xFFFFFFFFU - 8U)) {
496 opj_event_msg(p_manager, EVT_ERROR,
497 "Cannot handle box sizes higher than 2^32\n");
500 box->length = (OPJ_UINT32)bleft + 8U;
501 assert((OPJ_OFF_T)box->length == bleft + 8);
505 /* do we have a "special very large box ?" */
506 /* read then the XLBox */
507 if (box->length == 1) {
508 OPJ_UINT32 l_xl_part_size;
510 OPJ_UINT32 l_nb_bytes_read = (OPJ_UINT32)opj_stream_read_data(cio,
511 l_data_header, 8, p_manager);
512 if (l_nb_bytes_read != 8) {
513 if (l_nb_bytes_read > 0) {
514 *p_number_bytes_read += l_nb_bytes_read;
520 *p_number_bytes_read = 16;
521 opj_read_bytes(l_data_header, &l_xl_part_size, 4);
522 if (l_xl_part_size != 0) {
523 opj_event_msg(p_manager, EVT_ERROR,
524 "Cannot handle box sizes higher than 2^32\n");
527 opj_read_bytes(l_data_header + 4, &(box->length), 4);
533 static void jp2_write_url(opj_cio_t *cio, char *Idx_file)
538 box.init_pos = cio_tell(cio);
540 cio_write(cio, JP2_URL, 4); /* DBTL */
541 cio_write(cio, 0, 1); /* VERS */
542 cio_write(cio, 0, 3); /* FLAG */
545 for (i = 0; i < strlen(Idx_file); i++) {
546 cio_write(cio, Idx_file[i], 1);
550 box.length = cio_tell(cio) - box.init_pos;
551 cio_seek(cio, box.init_pos);
552 cio_write(cio, box.length, 4); /* L */
553 cio_seek(cio, box.init_pos + box.length);
557 static OPJ_BOOL opj_jp2_read_ihdr(opj_jp2_t *jp2,
558 OPJ_BYTE *p_image_header_data,
559 OPJ_UINT32 p_image_header_size,
560 opj_event_mgr_t * p_manager)
563 assert(p_image_header_data != 00);
565 assert(p_manager != 00);
567 if (jp2->comps != NULL) {
568 opj_event_msg(p_manager, EVT_WARNING,
569 "Ignoring ihdr box. First ihdr box already read\n");
573 if (p_image_header_size != 14) {
574 opj_event_msg(p_manager, EVT_ERROR, "Bad image header box (bad size)\n");
578 opj_read_bytes(p_image_header_data, &(jp2->h), 4); /* HEIGHT */
579 p_image_header_data += 4;
580 opj_read_bytes(p_image_header_data, &(jp2->w), 4); /* WIDTH */
581 p_image_header_data += 4;
582 opj_read_bytes(p_image_header_data, &(jp2->numcomps), 2); /* NC */
583 p_image_header_data += 2;
585 if ((jp2->numcomps - 1U) >=
586 16384U) { /* unsigned underflow is well defined: 1U <= jp2->numcomps <= 16384U */
587 opj_event_msg(p_manager, EVT_ERROR, "Invalid number of components (ihdr)\n");
591 /* allocate memory for components */
592 jp2->comps = (opj_jp2_comps_t*) opj_calloc(jp2->numcomps,
593 sizeof(opj_jp2_comps_t));
594 if (jp2->comps == 0) {
595 opj_event_msg(p_manager, EVT_ERROR,
596 "Not enough memory to handle image header (ihdr)\n");
600 opj_read_bytes(p_image_header_data, &(jp2->bpc), 1); /* BPC */
601 ++ p_image_header_data;
603 opj_read_bytes(p_image_header_data, &(jp2->C), 1); /* C */
604 ++ p_image_header_data;
606 /* Should be equal to 7 cf. chapter about image header box of the norm */
608 opj_event_msg(p_manager, EVT_INFO,
609 "JP2 IHDR box: compression type indicate that the file is not a conforming JP2 file (%d) \n",
613 opj_read_bytes(p_image_header_data, &(jp2->UnkC), 1); /* UnkC */
614 ++ p_image_header_data;
615 opj_read_bytes(p_image_header_data, &(jp2->IPR), 1); /* IPR */
616 ++ p_image_header_data;
621 static OPJ_BYTE * opj_jp2_write_ihdr(opj_jp2_t *jp2,
622 OPJ_UINT32 * p_nb_bytes_written
625 OPJ_BYTE * l_ihdr_data, * l_current_ihdr_ptr;
629 assert(p_nb_bytes_written != 00);
631 /* default image header is 22 bytes wide */
632 l_ihdr_data = (OPJ_BYTE *) opj_calloc(1, 22);
633 if (l_ihdr_data == 00) {
637 l_current_ihdr_ptr = l_ihdr_data;
639 opj_write_bytes(l_current_ihdr_ptr, 22, 4); /* write box size */
640 l_current_ihdr_ptr += 4;
642 opj_write_bytes(l_current_ihdr_ptr, JP2_IHDR, 4); /* IHDR */
643 l_current_ihdr_ptr += 4;
645 opj_write_bytes(l_current_ihdr_ptr, jp2->h, 4); /* HEIGHT */
646 l_current_ihdr_ptr += 4;
648 opj_write_bytes(l_current_ihdr_ptr, jp2->w, 4); /* WIDTH */
649 l_current_ihdr_ptr += 4;
651 opj_write_bytes(l_current_ihdr_ptr, jp2->numcomps, 2); /* NC */
652 l_current_ihdr_ptr += 2;
654 opj_write_bytes(l_current_ihdr_ptr, jp2->bpc, 1); /* BPC */
655 ++l_current_ihdr_ptr;
657 opj_write_bytes(l_current_ihdr_ptr, jp2->C, 1); /* C : Always 7 */
658 ++l_current_ihdr_ptr;
660 opj_write_bytes(l_current_ihdr_ptr, jp2->UnkC,
661 1); /* UnkC, colorspace unknown */
662 ++l_current_ihdr_ptr;
664 opj_write_bytes(l_current_ihdr_ptr, jp2->IPR,
665 1); /* IPR, no intellectual property */
666 ++l_current_ihdr_ptr;
668 *p_nb_bytes_written = 22;
673 static OPJ_BYTE * opj_jp2_write_bpcc(opj_jp2_t *jp2,
674 OPJ_UINT32 * p_nb_bytes_written
678 /* room for 8 bytes for box and 1 byte for each component */
679 OPJ_UINT32 l_bpcc_size;
680 OPJ_BYTE * l_bpcc_data, * l_current_bpcc_ptr;
684 assert(p_nb_bytes_written != 00);
685 l_bpcc_size = 8 + jp2->numcomps;
687 l_bpcc_data = (OPJ_BYTE *) opj_calloc(1, l_bpcc_size);
688 if (l_bpcc_data == 00) {
692 l_current_bpcc_ptr = l_bpcc_data;
694 opj_write_bytes(l_current_bpcc_ptr, l_bpcc_size,
695 4); /* write box size */
696 l_current_bpcc_ptr += 4;
698 opj_write_bytes(l_current_bpcc_ptr, JP2_BPCC, 4); /* BPCC */
699 l_current_bpcc_ptr += 4;
701 for (i = 0; i < jp2->numcomps; ++i) {
702 opj_write_bytes(l_current_bpcc_ptr, jp2->comps[i].bpcc,
703 1); /* write each component information */
704 ++l_current_bpcc_ptr;
707 *p_nb_bytes_written = l_bpcc_size;
712 static OPJ_BOOL opj_jp2_read_bpcc(opj_jp2_t *jp2,
713 OPJ_BYTE * p_bpc_header_data,
714 OPJ_UINT32 p_bpc_header_size,
715 opj_event_mgr_t * p_manager
721 assert(p_bpc_header_data != 00);
723 assert(p_manager != 00);
726 if (jp2->bpc != 255) {
727 opj_event_msg(p_manager, EVT_WARNING,
728 "A BPCC header box is available although BPC given by the IHDR box (%d) indicate components bit depth is constant\n",
732 /* and length is relevant */
733 if (p_bpc_header_size != jp2->numcomps) {
734 opj_event_msg(p_manager, EVT_ERROR, "Bad BPCC header box (bad size)\n");
738 /* read info for each component */
739 for (i = 0; i < jp2->numcomps; ++i) {
740 opj_read_bytes(p_bpc_header_data, &jp2->comps[i].bpcc,
741 1); /* read each BPCC component */
747 static OPJ_BYTE * opj_jp2_write_cdef(opj_jp2_t *jp2,
748 OPJ_UINT32 * p_nb_bytes_written)
750 /* room for 8 bytes for box, 2 for n */
751 OPJ_UINT32 l_cdef_size = 10;
752 OPJ_BYTE * l_cdef_data, * l_current_cdef_ptr;
758 assert(p_nb_bytes_written != 00);
759 assert(jp2->color.jp2_cdef != 00);
760 assert(jp2->color.jp2_cdef->info != 00);
761 assert(jp2->color.jp2_cdef->n > 0U);
763 l_cdef_size += 6U * jp2->color.jp2_cdef->n;
765 l_cdef_data = (OPJ_BYTE *) opj_malloc(l_cdef_size);
766 if (l_cdef_data == 00) {
770 l_current_cdef_ptr = l_cdef_data;
772 opj_write_bytes(l_current_cdef_ptr, l_cdef_size, 4); /* write box size */
773 l_current_cdef_ptr += 4;
775 opj_write_bytes(l_current_cdef_ptr, JP2_CDEF, 4); /* BPCC */
776 l_current_cdef_ptr += 4;
778 l_value = jp2->color.jp2_cdef->n;
779 opj_write_bytes(l_current_cdef_ptr, l_value, 2); /* N */
780 l_current_cdef_ptr += 2;
782 for (i = 0U; i < jp2->color.jp2_cdef->n; ++i) {
783 l_value = jp2->color.jp2_cdef->info[i].cn;
784 opj_write_bytes(l_current_cdef_ptr, l_value, 2); /* Cni */
785 l_current_cdef_ptr += 2;
786 l_value = jp2->color.jp2_cdef->info[i].typ;
787 opj_write_bytes(l_current_cdef_ptr, l_value, 2); /* Typi */
788 l_current_cdef_ptr += 2;
789 l_value = jp2->color.jp2_cdef->info[i].asoc;
790 opj_write_bytes(l_current_cdef_ptr, l_value, 2); /* Asoci */
791 l_current_cdef_ptr += 2;
793 *p_nb_bytes_written = l_cdef_size;
798 static OPJ_BYTE * opj_jp2_write_colr(opj_jp2_t *jp2,
799 OPJ_UINT32 * p_nb_bytes_written
802 /* room for 8 bytes for box 3 for common data and variable upon profile*/
803 OPJ_UINT32 l_colr_size = 11;
804 OPJ_BYTE * l_colr_data, * l_current_colr_ptr;
808 assert(p_nb_bytes_written != 00);
809 assert(jp2->meth == 1 || jp2->meth == 2);
813 l_colr_size += 4; /* EnumCS */
816 assert(jp2->color.icc_profile_len); /* ICC profile */
817 l_colr_size += jp2->color.icc_profile_len;
823 l_colr_data = (OPJ_BYTE *) opj_calloc(1, l_colr_size);
824 if (l_colr_data == 00) {
828 l_current_colr_ptr = l_colr_data;
830 opj_write_bytes(l_current_colr_ptr, l_colr_size,
831 4); /* write box size */
832 l_current_colr_ptr += 4;
834 opj_write_bytes(l_current_colr_ptr, JP2_COLR, 4); /* BPCC */
835 l_current_colr_ptr += 4;
837 opj_write_bytes(l_current_colr_ptr, jp2->meth, 1); /* METH */
838 ++l_current_colr_ptr;
840 opj_write_bytes(l_current_colr_ptr, jp2->precedence, 1); /* PRECEDENCE */
841 ++l_current_colr_ptr;
843 opj_write_bytes(l_current_colr_ptr, jp2->approx, 1); /* APPROX */
844 ++l_current_colr_ptr;
847 1) { /* Meth value is restricted to 1 or 2 (Table I.9 of part 1) */
848 opj_write_bytes(l_current_colr_ptr, jp2->enumcs, 4);
851 if (jp2->meth == 2) { /* ICC profile */
853 for (i = 0; i < jp2->color.icc_profile_len; ++i) {
854 opj_write_bytes(l_current_colr_ptr, jp2->color.icc_profile_buf[i], 1);
855 ++l_current_colr_ptr;
860 *p_nb_bytes_written = l_colr_size;
865 static void opj_jp2_free_pclr(opj_jp2_color_t *color)
867 opj_free(color->jp2_pclr->channel_sign);
868 opj_free(color->jp2_pclr->channel_size);
869 opj_free(color->jp2_pclr->entries);
871 if (color->jp2_pclr->cmap) {
872 opj_free(color->jp2_pclr->cmap);
875 opj_free(color->jp2_pclr);
876 color->jp2_pclr = NULL;
879 static OPJ_BOOL opj_jp2_check_color(opj_image_t *image, opj_jp2_color_t *color,
880 opj_event_mgr_t *p_manager)
884 /* testcase 4149.pdf.SIGSEGV.cf7.3501 */
885 if (color->jp2_cdef) {
886 opj_jp2_cdef_info_t *info = color->jp2_cdef->info;
887 OPJ_UINT16 n = color->jp2_cdef->n;
888 OPJ_UINT32 nr_channels =
889 image->numcomps; /* FIXME image->numcomps == jp2->numcomps before color is applied ??? */
891 /* cdef applies to cmap channels if any */
892 if (color->jp2_pclr && color->jp2_pclr->cmap) {
893 nr_channels = (OPJ_UINT32)color->jp2_pclr->nr_channels;
896 for (i = 0; i < n; i++) {
897 if (info[i].cn >= nr_channels) {
898 opj_event_msg(p_manager, EVT_ERROR, "Invalid component index %d (>= %d).\n",
899 info[i].cn, nr_channels);
902 if (info[i].asoc == 65535U) {
906 if (info[i].asoc > 0 && (OPJ_UINT32)(info[i].asoc - 1) >= nr_channels) {
907 opj_event_msg(p_manager, EVT_ERROR, "Invalid component index %d (>= %d).\n",
908 info[i].asoc - 1, nr_channels);
914 /* ISO 15444-1 states that if cdef is present, it shall contain a complete list of channel definitions. */
915 while (nr_channels > 0) {
916 for (i = 0; i < n; ++i) {
917 if ((OPJ_UINT32)info[i].cn == (nr_channels - 1U)) {
922 opj_event_msg(p_manager, EVT_ERROR, "Incomplete channel definitions.\n");
929 /* testcases 451.pdf.SIGSEGV.f4c.3723, 451.pdf.SIGSEGV.5b5.3723 and
930 66ea31acbb0f23a2bbc91f64d69a03f5_signal_sigsegv_13937c0_7030_5725.pdf */
931 if (color->jp2_pclr && color->jp2_pclr->cmap) {
932 OPJ_UINT16 nr_channels = color->jp2_pclr->nr_channels;
933 opj_jp2_cmap_comp_t *cmap = color->jp2_pclr->cmap;
934 OPJ_BOOL *pcol_usage, is_sane = OPJ_TRUE;
936 /* verify that all original components match an existing one */
937 for (i = 0; i < nr_channels; i++) {
938 if (cmap[i].cmp >= image->numcomps) {
939 opj_event_msg(p_manager, EVT_ERROR, "Invalid component index %d (>= %d).\n",
940 cmap[i].cmp, image->numcomps);
945 pcol_usage = (OPJ_BOOL *) opj_calloc(nr_channels, sizeof(OPJ_BOOL));
947 opj_event_msg(p_manager, EVT_ERROR, "Unexpected OOM.\n");
950 /* verify that no component is targeted more than once */
951 for (i = 0; i < nr_channels; i++) {
952 OPJ_UINT16 pcol = cmap[i].pcol;
953 assert(cmap[i].mtyp == 0 || cmap[i].mtyp == 1);
954 if (pcol >= nr_channels) {
955 opj_event_msg(p_manager, EVT_ERROR,
956 "Invalid component/palette index for direct mapping %d.\n", pcol);
958 } else if (pcol_usage[pcol] && cmap[i].mtyp == 1) {
959 opj_event_msg(p_manager, EVT_ERROR, "Component %d is mapped twice.\n", pcol);
961 } else if (cmap[i].mtyp == 0 && cmap[i].pcol != 0) {
962 /* I.5.3.5 PCOL: If the value of the MTYP field for this channel is 0, then
963 * the value of this field shall be 0. */
964 opj_event_msg(p_manager, EVT_ERROR, "Direct use at #%d however pcol=%d.\n", i,
968 pcol_usage[pcol] = OPJ_TRUE;
971 /* verify that all components are targeted at least once */
972 for (i = 0; i < nr_channels; i++) {
973 if (!pcol_usage[i] && cmap[i].mtyp != 0) {
974 opj_event_msg(p_manager, EVT_ERROR, "Component %d doesn't have a mapping.\n",
979 /* Issue 235/447 weird cmap */
980 if (1 && is_sane && (image->numcomps == 1U)) {
981 for (i = 0; i < nr_channels; i++) {
982 if (!pcol_usage[i]) {
984 opj_event_msg(p_manager, EVT_WARNING,
985 "Component mapping seems wrong. Trying to correct.\n", i);
991 for (i = 0; i < nr_channels; i++) {
993 cmap[i].pcol = (OPJ_BYTE) i;
997 opj_free(pcol_usage);
1007 static void opj_jp2_apply_pclr(opj_image_t *image, opj_jp2_color_t *color)
1009 opj_image_comp_t *old_comps, *new_comps;
1010 OPJ_BYTE *channel_size, *channel_sign;
1011 OPJ_UINT32 *entries;
1012 opj_jp2_cmap_comp_t *cmap;
1013 OPJ_INT32 *src, *dst;
1015 OPJ_UINT16 i, nr_channels, cmp, pcol;
1018 channel_size = color->jp2_pclr->channel_size;
1019 channel_sign = color->jp2_pclr->channel_sign;
1020 entries = color->jp2_pclr->entries;
1021 cmap = color->jp2_pclr->cmap;
1022 nr_channels = color->jp2_pclr->nr_channels;
1024 old_comps = image->comps;
1025 new_comps = (opj_image_comp_t*)
1026 opj_malloc(nr_channels * sizeof(opj_image_comp_t));
1028 /* FIXME no error code for opj_jp2_apply_pclr */
1029 /* FIXME event manager error callback */
1032 for (i = 0; i < nr_channels; ++i) {
1033 pcol = cmap[i].pcol;
1037 if (cmap[i].mtyp == 0) {
1039 new_comps[i] = old_comps[cmp];
1042 new_comps[pcol] = old_comps[cmp];
1045 /* Palette mapping: */
1046 new_comps[i].data = (OPJ_INT32*)
1047 opj_malloc(old_comps[cmp].w * old_comps[cmp].h * sizeof(OPJ_INT32));
1048 if (!new_comps[i].data) {
1049 opj_free(new_comps);
1051 /* FIXME no error code for opj_jp2_apply_pclr */
1052 /* FIXME event manager error callback */
1055 new_comps[i].prec = channel_size[i];
1056 new_comps[i].sgnd = channel_sign[i];
1059 top_k = color->jp2_pclr->nr_entries - 1;
1061 for (i = 0; i < nr_channels; ++i) {
1062 /* Palette mapping: */
1064 pcol = cmap[i].pcol;
1065 src = old_comps[cmp].data;
1067 max = new_comps[pcol].w * new_comps[pcol].h;
1070 if (cmap[i].mtyp == 0) {
1072 dst = new_comps[i].data;
1074 for (j = 0; j < max; ++j) {
1079 dst = new_comps[pcol].data;
1081 for (j = 0; j < max; ++j) {
1083 if ((k = src[j]) < 0) {
1085 } else if (k > top_k) {
1090 dst[j] = (OPJ_INT32)entries[k * nr_channels + pcol];
1095 max = image->numcomps;
1096 for (i = 0; i < max; ++i) {
1097 if (old_comps[i].data) {
1098 opj_free(old_comps[i].data);
1102 opj_free(old_comps);
1103 image->comps = new_comps;
1104 image->numcomps = nr_channels;
1106 opj_jp2_free_pclr(color);
1110 static OPJ_BOOL opj_jp2_read_pclr(opj_jp2_t *jp2,
1111 OPJ_BYTE * p_pclr_header_data,
1112 OPJ_UINT32 p_pclr_header_size,
1113 opj_event_mgr_t * p_manager
1116 opj_jp2_pclr_t *jp2_pclr;
1117 OPJ_BYTE *channel_size, *channel_sign;
1118 OPJ_UINT32 *entries;
1119 OPJ_UINT16 nr_entries, nr_channels;
1122 OPJ_BYTE *orig_header_data = p_pclr_header_data;
1125 assert(p_pclr_header_data != 00);
1127 assert(p_manager != 00);
1128 (void)p_pclr_header_size;
1130 if (jp2->color.jp2_pclr) {
1134 if (p_pclr_header_size < 3) {
1138 opj_read_bytes(p_pclr_header_data, &l_value, 2); /* NE */
1139 p_pclr_header_data += 2;
1140 nr_entries = (OPJ_UINT16) l_value;
1141 if ((nr_entries == 0U) || (nr_entries > 1024U)) {
1142 opj_event_msg(p_manager, EVT_ERROR, "Invalid PCLR box. Reports %d entries\n",
1147 opj_read_bytes(p_pclr_header_data, &l_value, 1); /* NPC */
1148 ++p_pclr_header_data;
1149 nr_channels = (OPJ_UINT16) l_value;
1150 if (nr_channels == 0U) {
1151 opj_event_msg(p_manager, EVT_ERROR,
1152 "Invalid PCLR box. Reports 0 palette columns\n");
1156 if (p_pclr_header_size < 3 + (OPJ_UINT32)nr_channels) {
1160 entries = (OPJ_UINT32*) opj_malloc((size_t)nr_channels * nr_entries * sizeof(
1165 channel_size = (OPJ_BYTE*) opj_malloc(nr_channels);
1166 if (!channel_size) {
1170 channel_sign = (OPJ_BYTE*) opj_malloc(nr_channels);
1171 if (!channel_sign) {
1173 opj_free(channel_size);
1177 jp2_pclr = (opj_jp2_pclr_t*)opj_malloc(sizeof(opj_jp2_pclr_t));
1180 opj_free(channel_size);
1181 opj_free(channel_sign);
1185 jp2_pclr->channel_sign = channel_sign;
1186 jp2_pclr->channel_size = channel_size;
1187 jp2_pclr->entries = entries;
1188 jp2_pclr->nr_entries = nr_entries;
1189 jp2_pclr->nr_channels = (OPJ_BYTE) l_value;
1190 jp2_pclr->cmap = NULL;
1192 jp2->color.jp2_pclr = jp2_pclr;
1194 for (i = 0; i < nr_channels; ++i) {
1195 opj_read_bytes(p_pclr_header_data, &l_value, 1); /* Bi */
1196 ++p_pclr_header_data;
1198 channel_size[i] = (OPJ_BYTE)((l_value & 0x7f) + 1);
1199 channel_sign[i] = (l_value & 0x80) ? 1 : 0;
1202 for (j = 0; j < nr_entries; ++j) {
1203 for (i = 0; i < nr_channels; ++i) {
1204 OPJ_UINT32 bytes_to_read = (OPJ_UINT32)((channel_size[i] + 7) >> 3);
1206 if (bytes_to_read > sizeof(OPJ_UINT32)) {
1207 bytes_to_read = sizeof(OPJ_UINT32);
1209 if ((ptrdiff_t)p_pclr_header_size < (ptrdiff_t)(p_pclr_header_data -
1210 orig_header_data) + (ptrdiff_t)bytes_to_read) {
1214 opj_read_bytes(p_pclr_header_data, &l_value, bytes_to_read); /* Cji */
1215 p_pclr_header_data += bytes_to_read;
1216 *entries = (OPJ_UINT32) l_value;
1224 static OPJ_BOOL opj_jp2_read_cmap(opj_jp2_t * jp2,
1225 OPJ_BYTE * p_cmap_header_data,
1226 OPJ_UINT32 p_cmap_header_size,
1227 opj_event_mgr_t * p_manager
1230 opj_jp2_cmap_comp_t *cmap;
1231 OPJ_BYTE i, nr_channels;
1236 assert(p_cmap_header_data != 00);
1237 assert(p_manager != 00);
1238 (void)p_cmap_header_size;
1240 /* Need nr_channels: */
1241 if (jp2->color.jp2_pclr == NULL) {
1242 opj_event_msg(p_manager, EVT_ERROR,
1243 "Need to read a PCLR box before the CMAP box.\n");
1247 /* Part 1, I.5.3.5: 'There shall be at most one Component Mapping box
1248 * inside a JP2 Header box' :
1250 if (jp2->color.jp2_pclr->cmap) {
1251 opj_event_msg(p_manager, EVT_ERROR, "Only one CMAP box is allowed.\n");
1255 nr_channels = jp2->color.jp2_pclr->nr_channels;
1256 if (p_cmap_header_size < (OPJ_UINT32)nr_channels * 4) {
1257 opj_event_msg(p_manager, EVT_ERROR, "Insufficient data for CMAP box.\n");
1261 cmap = (opj_jp2_cmap_comp_t*) opj_malloc(nr_channels * sizeof(
1262 opj_jp2_cmap_comp_t));
1268 for (i = 0; i < nr_channels; ++i) {
1269 opj_read_bytes(p_cmap_header_data, &l_value, 2); /* CMP^i */
1270 p_cmap_header_data += 2;
1271 cmap[i].cmp = (OPJ_UINT16) l_value;
1273 opj_read_bytes(p_cmap_header_data, &l_value, 1); /* MTYP^i */
1274 ++p_cmap_header_data;
1275 cmap[i].mtyp = (OPJ_BYTE) l_value;
1277 opj_read_bytes(p_cmap_header_data, &l_value, 1); /* PCOL^i */
1278 ++p_cmap_header_data;
1279 cmap[i].pcol = (OPJ_BYTE) l_value;
1282 jp2->color.jp2_pclr->cmap = cmap;
1287 static void opj_jp2_apply_cdef(opj_image_t *image, opj_jp2_color_t *color,
1288 opj_event_mgr_t *manager)
1290 opj_jp2_cdef_info_t *info;
1291 OPJ_UINT16 i, n, cn, asoc, acn;
1293 info = color->jp2_cdef->info;
1294 n = color->jp2_cdef->n;
1296 for (i = 0; i < n; ++i) {
1297 /* WATCH: acn = asoc - 1 ! */
1298 asoc = info[i].asoc;
1301 if (cn >= image->numcomps) {
1302 opj_event_msg(manager, EVT_WARNING, "opj_jp2_apply_cdef: cn=%d, numcomps=%d\n",
1303 cn, image->numcomps);
1306 if (asoc == 0 || asoc == 65535) {
1307 image->comps[cn].alpha = info[i].typ;
1311 acn = (OPJ_UINT16)(asoc - 1);
1312 if (acn >= image->numcomps) {
1313 opj_event_msg(manager, EVT_WARNING, "opj_jp2_apply_cdef: acn=%d, numcomps=%d\n",
1314 acn, image->numcomps);
1318 /* Swap only if color channel */
1319 if ((cn != acn) && (info[i].typ == 0)) {
1320 opj_image_comp_t saved;
1323 memcpy(&saved, &image->comps[cn], sizeof(opj_image_comp_t));
1324 memcpy(&image->comps[cn], &image->comps[acn], sizeof(opj_image_comp_t));
1325 memcpy(&image->comps[acn], &saved, sizeof(opj_image_comp_t));
1327 /* Swap channels in following channel definitions, don't bother with j <= i that are already processed */
1328 for (j = (OPJ_UINT16)(i + 1U); j < n ; ++j) {
1329 if (info[j].cn == cn) {
1331 } else if (info[j].cn == acn) {
1334 /* asoc is related to color index. Do not update. */
1338 image->comps[cn].alpha = info[i].typ;
1341 if (color->jp2_cdef->info) {
1342 opj_free(color->jp2_cdef->info);
1345 opj_free(color->jp2_cdef);
1346 color->jp2_cdef = NULL;
1348 }/* jp2_apply_cdef() */
1350 static OPJ_BOOL opj_jp2_read_cdef(opj_jp2_t * jp2,
1351 OPJ_BYTE * p_cdef_header_data,
1352 OPJ_UINT32 p_cdef_header_size,
1353 opj_event_mgr_t * p_manager
1356 opj_jp2_cdef_info_t *cdef_info;
1362 assert(p_cdef_header_data != 00);
1363 assert(p_manager != 00);
1364 (void)p_cdef_header_size;
1366 /* Part 1, I.5.3.6: 'The shall be at most one Channel Definition box
1367 * inside a JP2 Header box.'*/
1368 if (jp2->color.jp2_cdef) {
1372 if (p_cdef_header_size < 2) {
1373 opj_event_msg(p_manager, EVT_ERROR, "Insufficient data for CDEF box.\n");
1377 opj_read_bytes(p_cdef_header_data, &l_value, 2); /* N */
1378 p_cdef_header_data += 2;
1380 if ((OPJ_UINT16)l_value == 0) { /* szukw000: FIXME */
1381 opj_event_msg(p_manager, EVT_ERROR,
1382 "Number of channel description is equal to zero in CDEF box.\n");
1386 if (p_cdef_header_size < 2 + (OPJ_UINT32)(OPJ_UINT16)l_value * 6) {
1387 opj_event_msg(p_manager, EVT_ERROR, "Insufficient data for CDEF box.\n");
1391 cdef_info = (opj_jp2_cdef_info_t*) opj_malloc(l_value * sizeof(
1392 opj_jp2_cdef_info_t));
1397 jp2->color.jp2_cdef = (opj_jp2_cdef_t*)opj_malloc(sizeof(opj_jp2_cdef_t));
1398 if (!jp2->color.jp2_cdef) {
1399 opj_free(cdef_info);
1402 jp2->color.jp2_cdef->info = cdef_info;
1403 jp2->color.jp2_cdef->n = (OPJ_UINT16) l_value;
1405 for (i = 0; i < jp2->color.jp2_cdef->n; ++i) {
1406 opj_read_bytes(p_cdef_header_data, &l_value, 2); /* Cn^i */
1407 p_cdef_header_data += 2;
1408 cdef_info[i].cn = (OPJ_UINT16) l_value;
1410 opj_read_bytes(p_cdef_header_data, &l_value, 2); /* Typ^i */
1411 p_cdef_header_data += 2;
1412 cdef_info[i].typ = (OPJ_UINT16) l_value;
1414 opj_read_bytes(p_cdef_header_data, &l_value, 2); /* Asoc^i */
1415 p_cdef_header_data += 2;
1416 cdef_info[i].asoc = (OPJ_UINT16) l_value;
1422 static OPJ_BOOL opj_jp2_read_colr(opj_jp2_t *jp2,
1423 OPJ_BYTE * p_colr_header_data,
1424 OPJ_UINT32 p_colr_header_size,
1425 opj_event_mgr_t * p_manager
1432 assert(p_colr_header_data != 00);
1433 assert(p_manager != 00);
1435 if (p_colr_header_size < 3) {
1436 opj_event_msg(p_manager, EVT_ERROR, "Bad COLR header box (bad size)\n");
1440 /* Part 1, I.5.3.3 : 'A conforming JP2 reader shall ignore all Colour
1441 * Specification boxes after the first.'
1443 if (jp2->color.jp2_has_colr) {
1444 opj_event_msg(p_manager, EVT_INFO,
1445 "A conforming JP2 reader shall ignore all Colour Specification boxes after the first, so we ignore this one.\n");
1446 p_colr_header_data += p_colr_header_size;
1450 opj_read_bytes(p_colr_header_data, &jp2->meth, 1); /* METH */
1451 ++p_colr_header_data;
1453 opj_read_bytes(p_colr_header_data, &jp2->precedence, 1); /* PRECEDENCE */
1454 ++p_colr_header_data;
1456 opj_read_bytes(p_colr_header_data, &jp2->approx, 1); /* APPROX */
1457 ++p_colr_header_data;
1459 if (jp2->meth == 1) {
1460 if (p_colr_header_size < 7) {
1461 opj_event_msg(p_manager, EVT_ERROR, "Bad COLR header box (bad size: %d)\n",
1462 p_colr_header_size);
1465 if ((p_colr_header_size > 7) &&
1466 (jp2->enumcs != 14)) { /* handled below for CIELab) */
1467 /* testcase Altona_Technical_v20_x4.pdf */
1468 opj_event_msg(p_manager, EVT_WARNING, "Bad COLR header box (bad size: %d)\n",
1469 p_colr_header_size);
1472 opj_read_bytes(p_colr_header_data, &jp2->enumcs, 4); /* EnumCS */
1474 p_colr_header_data += 4;
1476 if (jp2->enumcs == 14) { /* CIELab */
1478 OPJ_UINT32 rl, ol, ra, oa, rb, ob, il;
1480 cielab = (OPJ_UINT32*)opj_malloc(9 * sizeof(OPJ_UINT32));
1481 if (cielab == NULL) {
1482 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory for cielab\n");
1485 cielab[0] = 14; /* enumcs */
1487 /* default values */
1488 rl = ra = rb = ol = oa = ob = 0;
1489 il = 0x00443530; /* D50 */
1490 cielab[1] = 0x44454600;/* DEF */
1492 if (p_colr_header_size == 35) {
1493 opj_read_bytes(p_colr_header_data, &rl, 4);
1494 p_colr_header_data += 4;
1495 opj_read_bytes(p_colr_header_data, &ol, 4);
1496 p_colr_header_data += 4;
1497 opj_read_bytes(p_colr_header_data, &ra, 4);
1498 p_colr_header_data += 4;
1499 opj_read_bytes(p_colr_header_data, &oa, 4);
1500 p_colr_header_data += 4;
1501 opj_read_bytes(p_colr_header_data, &rb, 4);
1502 p_colr_header_data += 4;
1503 opj_read_bytes(p_colr_header_data, &ob, 4);
1504 p_colr_header_data += 4;
1505 opj_read_bytes(p_colr_header_data, &il, 4);
1506 p_colr_header_data += 4;
1509 } else if (p_colr_header_size != 7) {
1510 opj_event_msg(p_manager, EVT_WARNING,
1511 "Bad COLR header box (CIELab, bad size: %d)\n", p_colr_header_size);
1521 jp2->color.icc_profile_buf = (OPJ_BYTE*)cielab;
1522 jp2->color.icc_profile_len = 0;
1524 jp2->color.jp2_has_colr = 1;
1525 } else if (jp2->meth == 2) {
1527 OPJ_INT32 it_icc_value = 0;
1528 OPJ_INT32 icc_len = (OPJ_INT32)p_colr_header_size - 3;
1530 jp2->color.icc_profile_len = (OPJ_UINT32)icc_len;
1531 jp2->color.icc_profile_buf = (OPJ_BYTE*) opj_calloc(1, (size_t)icc_len);
1532 if (!jp2->color.icc_profile_buf) {
1533 jp2->color.icc_profile_len = 0;
1537 for (it_icc_value = 0; it_icc_value < icc_len; ++it_icc_value) {
1538 opj_read_bytes(p_colr_header_data, &l_value, 1); /* icc values */
1539 ++p_colr_header_data;
1540 jp2->color.icc_profile_buf[it_icc_value] = (OPJ_BYTE) l_value;
1543 jp2->color.jp2_has_colr = 1;
1544 } else if (jp2->meth > 2) {
1545 /* ISO/IEC 15444-1:2004 (E), Table I.9 Legal METH values:
1546 conforming JP2 reader shall ignore the entire Colour Specification box.*/
1547 opj_event_msg(p_manager, EVT_INFO,
1548 "COLR BOX meth value is not a regular value (%d), "
1549 "so we will ignore the entire Colour Specification box. \n", jp2->meth);
1554 OPJ_BOOL opj_jp2_decode(opj_jp2_t *jp2,
1555 opj_stream_private_t *p_stream,
1556 opj_image_t* p_image,
1557 opj_event_mgr_t * p_manager)
1564 if (! opj_j2k_decode(jp2->j2k, p_stream, p_image, p_manager)) {
1565 opj_event_msg(p_manager, EVT_ERROR,
1566 "Failed to decode the codestream in the JP2 file\n");
1570 if (!jp2->ignore_pclr_cmap_cdef) {
1571 if (!opj_jp2_check_color(p_image, &(jp2->color), p_manager)) {
1575 /* Set Image Color Space */
1576 if (jp2->enumcs == 16) {
1577 p_image->color_space = OPJ_CLRSPC_SRGB;
1578 } else if (jp2->enumcs == 17) {
1579 p_image->color_space = OPJ_CLRSPC_GRAY;
1580 } else if (jp2->enumcs == 18) {
1581 p_image->color_space = OPJ_CLRSPC_SYCC;
1582 } else if (jp2->enumcs == 24) {
1583 p_image->color_space = OPJ_CLRSPC_EYCC;
1584 } else if (jp2->enumcs == 12) {
1585 p_image->color_space = OPJ_CLRSPC_CMYK;
1587 p_image->color_space = OPJ_CLRSPC_UNKNOWN;
1590 if (jp2->color.jp2_pclr) {
1591 /* Part 1, I.5.3.4: Either both or none : */
1592 if (!jp2->color.jp2_pclr->cmap) {
1593 opj_jp2_free_pclr(&(jp2->color));
1595 opj_jp2_apply_pclr(p_image, &(jp2->color));
1599 /* Apply the color space if needed */
1600 if (jp2->color.jp2_cdef) {
1601 opj_jp2_apply_cdef(p_image, &(jp2->color), p_manager);
1604 if (jp2->color.icc_profile_buf) {
1605 p_image->icc_profile_buf = jp2->color.icc_profile_buf;
1606 p_image->icc_profile_len = jp2->color.icc_profile_len;
1607 jp2->color.icc_profile_buf = NULL;
1614 static OPJ_BOOL opj_jp2_write_jp2h(opj_jp2_t *jp2,
1615 opj_stream_private_t *stream,
1616 opj_event_mgr_t * p_manager
1619 opj_jp2_img_header_writer_handler_t l_writers [4];
1620 opj_jp2_img_header_writer_handler_t * l_current_writer;
1622 OPJ_INT32 i, l_nb_pass;
1623 /* size of data for super box*/
1624 OPJ_UINT32 l_jp2h_size = 8;
1625 OPJ_BOOL l_result = OPJ_TRUE;
1627 /* to store the data of the super box */
1628 OPJ_BYTE l_jp2h_data [8];
1631 assert(stream != 00);
1633 assert(p_manager != 00);
1635 memset(l_writers, 0, sizeof(l_writers));
1637 if (jp2->bpc == 255) {
1639 l_writers[0].handler = opj_jp2_write_ihdr;
1640 l_writers[1].handler = opj_jp2_write_bpcc;
1641 l_writers[2].handler = opj_jp2_write_colr;
1644 l_writers[0].handler = opj_jp2_write_ihdr;
1645 l_writers[1].handler = opj_jp2_write_colr;
1648 if (jp2->color.jp2_cdef != NULL) {
1649 l_writers[l_nb_pass].handler = opj_jp2_write_cdef;
1653 /* write box header */
1654 /* write JP2H type */
1655 opj_write_bytes(l_jp2h_data + 4, JP2_JP2H, 4);
1657 l_current_writer = l_writers;
1658 for (i = 0; i < l_nb_pass; ++i) {
1659 l_current_writer->m_data = l_current_writer->handler(jp2,
1660 &(l_current_writer->m_size));
1661 if (l_current_writer->m_data == 00) {
1662 opj_event_msg(p_manager, EVT_ERROR,
1663 "Not enough memory to hold JP2 Header data\n");
1664 l_result = OPJ_FALSE;
1668 l_jp2h_size += l_current_writer->m_size;
1673 l_current_writer = l_writers;
1674 for (i = 0; i < l_nb_pass; ++i) {
1675 if (l_current_writer->m_data != 00) {
1676 opj_free(l_current_writer->m_data);
1684 /* write super box size */
1685 opj_write_bytes(l_jp2h_data, l_jp2h_size, 4);
1687 /* write super box data on stream */
1688 if (opj_stream_write_data(stream, l_jp2h_data, 8, p_manager) != 8) {
1689 opj_event_msg(p_manager, EVT_ERROR,
1690 "Stream error while writing JP2 Header box\n");
1691 l_result = OPJ_FALSE;
1695 l_current_writer = l_writers;
1696 for (i = 0; i < l_nb_pass; ++i) {
1697 if (opj_stream_write_data(stream, l_current_writer->m_data,
1698 l_current_writer->m_size, p_manager) != l_current_writer->m_size) {
1699 opj_event_msg(p_manager, EVT_ERROR,
1700 "Stream error while writing JP2 Header box\n");
1701 l_result = OPJ_FALSE;
1708 l_current_writer = l_writers;
1711 for (i = 0; i < l_nb_pass; ++i) {
1712 if (l_current_writer->m_data != 00) {
1713 opj_free(l_current_writer->m_data);
1721 static OPJ_BOOL opj_jp2_write_ftyp(opj_jp2_t *jp2,
1722 opj_stream_private_t *cio,
1723 opj_event_mgr_t * p_manager)
1726 OPJ_UINT32 l_ftyp_size;
1727 OPJ_BYTE * l_ftyp_data, * l_current_data_ptr;
1733 assert(p_manager != 00);
1734 l_ftyp_size = 16 + 4 * jp2->numcl;
1736 l_ftyp_data = (OPJ_BYTE *) opj_calloc(1, l_ftyp_size);
1738 if (l_ftyp_data == 00) {
1739 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to handle ftyp data\n");
1743 l_current_data_ptr = l_ftyp_data;
1745 opj_write_bytes(l_current_data_ptr, l_ftyp_size, 4); /* box size */
1746 l_current_data_ptr += 4;
1748 opj_write_bytes(l_current_data_ptr, JP2_FTYP, 4); /* FTYP */
1749 l_current_data_ptr += 4;
1751 opj_write_bytes(l_current_data_ptr, jp2->brand, 4); /* BR */
1752 l_current_data_ptr += 4;
1754 opj_write_bytes(l_current_data_ptr, jp2->minversion, 4); /* MinV */
1755 l_current_data_ptr += 4;
1757 for (i = 0; i < jp2->numcl; i++) {
1758 opj_write_bytes(l_current_data_ptr, jp2->cl[i], 4); /* CL */
1761 l_result = (opj_stream_write_data(cio, l_ftyp_data, l_ftyp_size,
1762 p_manager) == l_ftyp_size);
1764 opj_event_msg(p_manager, EVT_ERROR,
1765 "Error while writing ftyp data to stream\n");
1768 opj_free(l_ftyp_data);
1773 static OPJ_BOOL opj_jp2_write_jp2c(opj_jp2_t *jp2,
1774 opj_stream_private_t *cio,
1775 opj_event_mgr_t * p_manager)
1777 OPJ_OFF_T j2k_codestream_exit;
1778 OPJ_BYTE l_data_header [8];
1783 assert(p_manager != 00);
1784 assert(opj_stream_has_seek(cio));
1786 j2k_codestream_exit = opj_stream_tell(cio);
1787 opj_write_bytes(l_data_header,
1788 (OPJ_UINT32)(j2k_codestream_exit - jp2->j2k_codestream_offset),
1789 4); /* size of codestream */
1790 opj_write_bytes(l_data_header + 4, JP2_JP2C,
1793 if (! opj_stream_seek(cio, jp2->j2k_codestream_offset, p_manager)) {
1794 opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
1798 if (opj_stream_write_data(cio, l_data_header, 8, p_manager) != 8) {
1799 opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
1803 if (! opj_stream_seek(cio, j2k_codestream_exit, p_manager)) {
1804 opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
1811 static OPJ_BOOL opj_jp2_write_jp(opj_jp2_t *jp2,
1812 opj_stream_private_t *cio,
1813 opj_event_mgr_t * p_manager)
1815 /* 12 bytes will be read */
1816 OPJ_BYTE l_signature_data [12];
1821 assert(p_manager != 00);
1825 /* write box length */
1826 opj_write_bytes(l_signature_data, 12, 4);
1827 /* writes box type */
1828 opj_write_bytes(l_signature_data + 4, JP2_JP, 4);
1829 /* writes magic number*/
1830 opj_write_bytes(l_signature_data + 8, 0x0d0a870a, 4);
1832 if (opj_stream_write_data(cio, l_signature_data, 12, p_manager) != 12) {
1839 /* ----------------------------------------------------------------------- */
1840 /* JP2 decoder interface */
1841 /* ----------------------------------------------------------------------- */
1843 void opj_jp2_setup_decoder(opj_jp2_t *jp2, opj_dparameters_t *parameters)
1845 /* setup the J2K codec */
1846 opj_j2k_setup_decoder(jp2->j2k, parameters);
1848 /* further JP2 initializations go here */
1849 jp2->color.jp2_has_colr = 0;
1850 jp2->ignore_pclr_cmap_cdef = parameters->flags &
1851 OPJ_DPARAMETERS_IGNORE_PCLR_CMAP_CDEF_FLAG;
1854 OPJ_BOOL opj_jp2_set_threads(opj_jp2_t *jp2, OPJ_UINT32 num_threads)
1856 return opj_j2k_set_threads(jp2->j2k, num_threads);
1859 /* ----------------------------------------------------------------------- */
1860 /* JP2 encoder interface */
1861 /* ----------------------------------------------------------------------- */
1863 OPJ_BOOL opj_jp2_setup_encoder(opj_jp2_t *jp2,
1864 opj_cparameters_t *parameters,
1866 opj_event_mgr_t * p_manager)
1871 OPJ_UINT32 alpha_count;
1872 OPJ_UINT32 color_channels = 0U;
1873 OPJ_UINT32 alpha_channel = 0U;
1876 if (!jp2 || !parameters || !image) {
1880 /* setup the J2K codec */
1881 /* ------------------- */
1883 /* Check if number of components respects standard */
1884 if (image->numcomps < 1 || image->numcomps > 16384) {
1885 opj_event_msg(p_manager, EVT_ERROR,
1886 "Invalid number of components specified while setting up JP2 encoder\n");
1890 if (opj_j2k_setup_encoder(jp2->j2k, parameters, image,
1891 p_manager) == OPJ_FALSE) {
1895 /* setup the JP2 codec */
1896 /* ------------------- */
1900 jp2->brand = JP2_JP2; /* BR */
1901 jp2->minversion = 0; /* MinV */
1903 jp2->cl = (OPJ_UINT32*) opj_malloc(jp2->numcl * sizeof(OPJ_UINT32));
1905 opj_event_msg(p_manager, EVT_ERROR,
1906 "Not enough memory when setup the JP2 encoder\n");
1909 jp2->cl[0] = JP2_JP2; /* CL0 : JP2 */
1911 /* Image Header box */
1913 jp2->numcomps = image->numcomps; /* NC */
1914 jp2->comps = (opj_jp2_comps_t*) opj_malloc(jp2->numcomps * sizeof(
1917 opj_event_msg(p_manager, EVT_ERROR,
1918 "Not enough memory when setup the JP2 encoder\n");
1919 /* Memory of jp2->cl will be freed by opj_jp2_destroy */
1923 jp2->h = image->y1 - image->y0; /* HEIGHT */
1924 jp2->w = image->x1 - image->x0; /* WIDTH */
1926 depth_0 = image->comps[0].prec - 1;
1927 sign = image->comps[0].sgnd;
1928 jp2->bpc = depth_0 + (sign << 7);
1929 for (i = 1; i < image->numcomps; i++) {
1930 OPJ_UINT32 depth = image->comps[i].prec - 1;
1931 sign = image->comps[i].sgnd;
1932 if (depth_0 != depth) {
1936 jp2->C = 7; /* C : Always 7 */
1937 jp2->UnkC = 0; /* UnkC, colorspace specified in colr box */
1938 jp2->IPR = 0; /* IPR, no intellectual property */
1940 /* BitsPerComponent box */
1941 for (i = 0; i < image->numcomps; i++) {
1942 jp2->comps[i].bpcc = image->comps[i].prec - 1 + (image->comps[i].sgnd << 7);
1945 /* Colour Specification box */
1946 if (image->icc_profile_len) {
1951 if (image->color_space == 1) {
1952 jp2->enumcs = 16; /* sRGB as defined by IEC 61966-2-1 */
1953 } else if (image->color_space == 2) {
1954 jp2->enumcs = 17; /* greyscale */
1955 } else if (image->color_space == 3) {
1956 jp2->enumcs = 18; /* YUV */
1960 /* Channel Definition box */
1961 /* FIXME not provided by parameters */
1962 /* We try to do what we can... */
1964 for (i = 0; i < image->numcomps; i++) {
1965 if (image->comps[i].alpha != 0) {
1970 if (alpha_count == 1U) { /* no way to deal with more than 1 alpha channel */
1971 switch (jp2->enumcs) {
1983 if (alpha_count == 0U) {
1984 opj_event_msg(p_manager, EVT_WARNING,
1985 "Alpha channel specified but unknown enumcs. No cdef box will be created.\n");
1986 } else if (image->numcomps < (color_channels + 1)) {
1987 opj_event_msg(p_manager, EVT_WARNING,
1988 "Alpha channel specified but not enough image components for an automatic cdef box creation.\n");
1990 } else if ((OPJ_UINT32)alpha_channel < color_channels) {
1991 opj_event_msg(p_manager, EVT_WARNING,
1992 "Alpha channel position conflicts with color channel. No cdef box will be created.\n");
1995 } else if (alpha_count > 1) {
1996 opj_event_msg(p_manager, EVT_WARNING,
1997 "Multiple alpha channels specified. No cdef box will be created.\n");
1999 if (alpha_count == 1U) { /* if here, we know what we can do */
2000 jp2->color.jp2_cdef = (opj_jp2_cdef_t*)opj_malloc(sizeof(opj_jp2_cdef_t));
2001 if (!jp2->color.jp2_cdef) {
2002 opj_event_msg(p_manager, EVT_ERROR,
2003 "Not enough memory to setup the JP2 encoder\n");
2006 /* no memset needed, all values will be overwritten except if jp2->color.jp2_cdef->info allocation fails, */
2007 /* in which case jp2->color.jp2_cdef->info will be NULL => valid for destruction */
2008 jp2->color.jp2_cdef->info = (opj_jp2_cdef_info_t*) opj_malloc(
2009 image->numcomps * sizeof(opj_jp2_cdef_info_t));
2010 if (!jp2->color.jp2_cdef->info) {
2011 /* memory will be freed by opj_jp2_destroy */
2012 opj_event_msg(p_manager, EVT_ERROR,
2013 "Not enough memory to setup the JP2 encoder\n");
2016 jp2->color.jp2_cdef->n = (OPJ_UINT16)
2017 image->numcomps; /* cast is valid : image->numcomps [1,16384] */
2018 for (i = 0U; i < color_channels; i++) {
2019 jp2->color.jp2_cdef->info[i].cn = (OPJ_UINT16)
2020 i; /* cast is valid : image->numcomps [1,16384] */
2021 jp2->color.jp2_cdef->info[i].typ = 0U;
2022 jp2->color.jp2_cdef->info[i].asoc = (OPJ_UINT16)(i +
2023 1U); /* No overflow + cast is valid : image->numcomps [1,16384] */
2025 for (; i < image->numcomps; i++) {
2026 if (image->comps[i].alpha != 0) { /* we'll be here exactly once */
2027 jp2->color.jp2_cdef->info[i].cn = (OPJ_UINT16)
2028 i; /* cast is valid : image->numcomps [1,16384] */
2029 jp2->color.jp2_cdef->info[i].typ = 1U; /* Opacity channel */
2030 jp2->color.jp2_cdef->info[i].asoc =
2031 0U; /* Apply alpha channel to the whole image */
2033 /* Unknown channel */
2034 jp2->color.jp2_cdef->info[i].cn = (OPJ_UINT16)
2035 i; /* cast is valid : image->numcomps [1,16384] */
2036 jp2->color.jp2_cdef->info[i].typ = 65535U;
2037 jp2->color.jp2_cdef->info[i].asoc = 65535U;
2042 jp2->precedence = 0; /* PRECEDENCE */
2043 jp2->approx = 0; /* APPROX */
2045 jp2->jpip_on = parameters->jpip_on;
2050 OPJ_BOOL opj_jp2_encode(opj_jp2_t *jp2,
2051 opj_stream_private_t *stream,
2052 opj_event_mgr_t * p_manager)
2054 return opj_j2k_encode(jp2->j2k, stream, p_manager);
2057 OPJ_BOOL opj_jp2_end_decompress(opj_jp2_t *jp2,
2058 opj_stream_private_t *cio,
2059 opj_event_mgr_t * p_manager
2065 assert(p_manager != 00);
2067 /* customization of the end encoding */
2068 if (! opj_jp2_setup_end_header_reading(jp2, p_manager)) {
2073 if (! opj_jp2_exec(jp2, jp2->m_procedure_list, cio, p_manager)) {
2077 return opj_j2k_end_decompress(jp2->j2k, cio, p_manager);
2080 OPJ_BOOL opj_jp2_end_compress(opj_jp2_t *jp2,
2081 opj_stream_private_t *cio,
2082 opj_event_mgr_t * p_manager
2088 assert(p_manager != 00);
2090 /* customization of the end encoding */
2091 if (! opj_jp2_setup_end_header_writing(jp2, p_manager)) {
2095 if (! opj_j2k_end_compress(jp2->j2k, cio, p_manager)) {
2100 return opj_jp2_exec(jp2, jp2->m_procedure_list, cio, p_manager);
2103 static OPJ_BOOL opj_jp2_setup_end_header_writing(opj_jp2_t *jp2,
2104 opj_event_mgr_t * p_manager)
2108 assert(p_manager != 00);
2112 if (! opj_procedure_list_add_procedure(jp2->m_procedure_list,
2113 (opj_procedure)opj_jpip_write_iptr, p_manager)) {
2118 if (! opj_procedure_list_add_procedure(jp2->m_procedure_list,
2119 (opj_procedure)opj_jp2_write_jp2c, p_manager)) {
2122 /* DEVELOPER CORNER, add your custom procedures */
2125 if (! opj_procedure_list_add_procedure(jp2->m_procedure_list,
2126 (opj_procedure)opj_jpip_write_cidx, p_manager)) {
2129 if (! opj_procedure_list_add_procedure(jp2->m_procedure_list,
2130 (opj_procedure)opj_jpip_write_fidx, p_manager)) {
2138 static OPJ_BOOL opj_jp2_setup_end_header_reading(opj_jp2_t *jp2,
2139 opj_event_mgr_t * p_manager)
2143 assert(p_manager != 00);
2145 if (! opj_procedure_list_add_procedure(jp2->m_procedure_list,
2146 (opj_procedure)opj_jp2_read_header_procedure, p_manager)) {
2149 /* DEVELOPER CORNER, add your custom procedures */
2154 static OPJ_BOOL opj_jp2_default_validation(opj_jp2_t * jp2,
2155 opj_stream_private_t *cio,
2156 opj_event_mgr_t * p_manager
2159 OPJ_BOOL l_is_valid = OPJ_TRUE;
2165 assert(p_manager != 00);
2167 OPJ_UNUSED(p_manager);
2169 /* JPEG2000 codec validation */
2171 /* STATE checking */
2172 /* make sure the state is at 0 */
2173 l_is_valid &= (jp2->jp2_state == JP2_STATE_NONE);
2175 /* make sure not reading a jp2h ???? WEIRD */
2176 l_is_valid &= (jp2->jp2_img_state == JP2_IMG_STATE_NONE);
2178 /* POINTER validation */
2179 /* make sure a j2k codec is present */
2180 l_is_valid &= (jp2->j2k != 00);
2182 /* make sure a procedure list is present */
2183 l_is_valid &= (jp2->m_procedure_list != 00);
2185 /* make sure a validation list is present */
2186 l_is_valid &= (jp2->m_validation_list != 00);
2188 /* PARAMETER VALIDATION */
2189 /* number of components */
2190 l_is_valid &= (jp2->numcl > 0);
2192 l_is_valid &= (jp2->h > 0);
2194 l_is_valid &= (jp2->w > 0);
2196 for (i = 0; i < jp2->numcomps; ++i) {
2197 l_is_valid &= ((jp2->comps[i].bpcc & 0x7FU) <
2198 38U); /* 0 is valid, ignore sign for check */
2202 l_is_valid &= ((jp2->meth > 0) && (jp2->meth < 3));
2204 /* stream validation */
2205 /* back and forth is needed */
2206 l_is_valid &= opj_stream_has_seek(cio);
2211 static OPJ_BOOL opj_jp2_read_header_procedure(opj_jp2_t *jp2,
2212 opj_stream_private_t *stream,
2213 opj_event_mgr_t * p_manager
2217 OPJ_UINT32 l_nb_bytes_read;
2218 const opj_jp2_header_handler_t * l_current_handler;
2219 const opj_jp2_header_handler_t * l_current_handler_misplaced;
2220 OPJ_UINT32 l_last_data_size = OPJ_BOX_SIZE;
2221 OPJ_UINT32 l_current_data_size;
2222 OPJ_BYTE * l_current_data = 00;
2225 assert(stream != 00);
2227 assert(p_manager != 00);
2229 l_current_data = (OPJ_BYTE*)opj_calloc(1, l_last_data_size);
2231 if (l_current_data == 00) {
2232 opj_event_msg(p_manager, EVT_ERROR,
2233 "Not enough memory to handle jpeg2000 file header\n");
2237 while (opj_jp2_read_boxhdr(&box, &l_nb_bytes_read, stream, p_manager)) {
2238 /* is it the codestream box ? */
2239 if (box.type == JP2_JP2C) {
2240 if (jp2->jp2_state & JP2_STATE_HEADER) {
2241 jp2->jp2_state |= JP2_STATE_CODESTREAM;
2242 opj_free(l_current_data);
2245 opj_event_msg(p_manager, EVT_ERROR, "bad placed jpeg codestream\n");
2246 opj_free(l_current_data);
2249 } else if (box.length == 0) {
2250 opj_event_msg(p_manager, EVT_ERROR, "Cannot handle box of undefined sizes\n");
2251 opj_free(l_current_data);
2254 /* testcase 1851.pdf.SIGSEGV.ce9.948 */
2255 else if (box.length < l_nb_bytes_read) {
2256 opj_event_msg(p_manager, EVT_ERROR, "invalid box size %d (%x)\n", box.length,
2258 opj_free(l_current_data);
2262 l_current_handler = opj_jp2_find_handler(box.type);
2263 l_current_handler_misplaced = opj_jp2_img_find_handler(box.type);
2264 l_current_data_size = box.length - l_nb_bytes_read;
2266 if ((l_current_handler != 00) || (l_current_handler_misplaced != 00)) {
2267 if (l_current_handler == 00) {
2268 opj_event_msg(p_manager, EVT_WARNING,
2269 "Found a misplaced '%c%c%c%c' box outside jp2h box\n",
2270 (OPJ_BYTE)(box.type >> 24), (OPJ_BYTE)(box.type >> 16),
2271 (OPJ_BYTE)(box.type >> 8), (OPJ_BYTE)(box.type >> 0));
2272 if (jp2->jp2_state & JP2_STATE_HEADER) {
2273 /* read anyway, we already have jp2h */
2274 l_current_handler = l_current_handler_misplaced;
2276 opj_event_msg(p_manager, EVT_WARNING,
2277 "JPEG2000 Header box not read yet, '%c%c%c%c' box will be ignored\n",
2278 (OPJ_BYTE)(box.type >> 24), (OPJ_BYTE)(box.type >> 16),
2279 (OPJ_BYTE)(box.type >> 8), (OPJ_BYTE)(box.type >> 0));
2280 jp2->jp2_state |= JP2_STATE_UNKNOWN;
2281 if (opj_stream_skip(stream, l_current_data_size,
2282 p_manager) != l_current_data_size) {
2283 opj_event_msg(p_manager, EVT_ERROR,
2284 "Problem with skipping JPEG2000 box, stream error\n");
2285 opj_free(l_current_data);
2291 if ((OPJ_OFF_T)l_current_data_size > opj_stream_get_number_byte_left(stream)) {
2292 /* do not even try to malloc if we can't read */
2293 opj_event_msg(p_manager, EVT_ERROR,
2294 "Invalid box size %d for box '%c%c%c%c'. Need %d bytes, %d bytes remaining \n",
2295 box.length, (OPJ_BYTE)(box.type >> 24), (OPJ_BYTE)(box.type >> 16),
2296 (OPJ_BYTE)(box.type >> 8), (OPJ_BYTE)(box.type >> 0), l_current_data_size,
2297 (OPJ_UINT32)opj_stream_get_number_byte_left(stream));
2298 opj_free(l_current_data);
2301 if (l_current_data_size > l_last_data_size) {
2302 OPJ_BYTE* new_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,
2303 l_current_data_size);
2304 if (!new_current_data) {
2305 opj_free(l_current_data);
2306 opj_event_msg(p_manager, EVT_ERROR,
2307 "Not enough memory to handle jpeg2000 box\n");
2310 l_current_data = new_current_data;
2311 l_last_data_size = l_current_data_size;
2314 l_nb_bytes_read = (OPJ_UINT32)opj_stream_read_data(stream, l_current_data,
2315 l_current_data_size, p_manager);
2316 if (l_nb_bytes_read != l_current_data_size) {
2317 opj_event_msg(p_manager, EVT_ERROR,
2318 "Problem with reading JPEG2000 box, stream error\n");
2319 opj_free(l_current_data);
2323 if (! l_current_handler->handler(jp2, l_current_data, l_current_data_size,
2325 opj_free(l_current_data);
2329 if (!(jp2->jp2_state & JP2_STATE_SIGNATURE)) {
2330 opj_event_msg(p_manager, EVT_ERROR,
2331 "Malformed JP2 file format: first box must be JPEG 2000 signature box\n");
2332 opj_free(l_current_data);
2335 if (!(jp2->jp2_state & JP2_STATE_FILE_TYPE)) {
2336 opj_event_msg(p_manager, EVT_ERROR,
2337 "Malformed JP2 file format: second box must be file type box\n");
2338 opj_free(l_current_data);
2341 jp2->jp2_state |= JP2_STATE_UNKNOWN;
2342 if (opj_stream_skip(stream, l_current_data_size,
2343 p_manager) != l_current_data_size) {
2344 opj_event_msg(p_manager, EVT_ERROR,
2345 "Problem with skipping JPEG2000 box, stream error\n");
2346 opj_free(l_current_data);
2352 opj_free(l_current_data);
2358 * Excutes the given procedures on the given codec.
2360 * @param p_procedure_list the list of procedures to execute
2361 * @param jp2 the jpeg2000 file codec to execute the procedures on.
2362 * @param stream the stream to execute the procedures on.
2363 * @param p_manager the user manager.
2365 * @return true if all the procedures were successfully executed.
2367 static OPJ_BOOL opj_jp2_exec(opj_jp2_t * jp2,
2368 opj_procedure_list_t * p_procedure_list,
2369 opj_stream_private_t *stream,
2370 opj_event_mgr_t * p_manager
2374 OPJ_BOOL(** l_procedure)(opj_jp2_t * jp2, opj_stream_private_t *,
2375 opj_event_mgr_t *) = 00;
2376 OPJ_BOOL l_result = OPJ_TRUE;
2377 OPJ_UINT32 l_nb_proc, i;
2380 assert(p_procedure_list != 00);
2382 assert(stream != 00);
2383 assert(p_manager != 00);
2385 l_nb_proc = opj_procedure_list_get_nb_procedures(p_procedure_list);
2386 l_procedure = (OPJ_BOOL(**)(opj_jp2_t * jp2, opj_stream_private_t *,
2387 opj_event_mgr_t *)) opj_procedure_list_get_first_procedure(p_procedure_list);
2389 for (i = 0; i < l_nb_proc; ++i) {
2390 l_result = l_result && (*l_procedure)(jp2, stream, p_manager);
2394 /* and clear the procedure list at the end. */
2395 opj_procedure_list_clear(p_procedure_list);
2399 OPJ_BOOL opj_jp2_start_compress(opj_jp2_t *jp2,
2400 opj_stream_private_t *stream,
2401 opj_image_t * p_image,
2402 opj_event_mgr_t * p_manager
2407 assert(stream != 00);
2408 assert(p_manager != 00);
2410 /* customization of the validation */
2411 if (! opj_jp2_setup_encoding_validation(jp2, p_manager)) {
2415 /* validation of the parameters codec */
2416 if (! opj_jp2_exec(jp2, jp2->m_validation_list, stream, p_manager)) {
2420 /* customization of the encoding */
2421 if (! opj_jp2_setup_header_writing(jp2, p_manager)) {
2426 if (! opj_jp2_exec(jp2, jp2->m_procedure_list, stream, p_manager)) {
2430 return opj_j2k_start_compress(jp2->j2k, stream, p_image, p_manager);
2433 static const opj_jp2_header_handler_t * opj_jp2_find_handler(OPJ_UINT32 p_id)
2435 OPJ_UINT32 i, l_handler_size = sizeof(jp2_header) / sizeof(
2436 opj_jp2_header_handler_t);
2438 for (i = 0; i < l_handler_size; ++i) {
2439 if (jp2_header[i].id == p_id) {
2440 return &jp2_header[i];
2447 * Finds the image execution function related to the given box id.
2449 * @param p_id the id of the handler to fetch.
2451 * @return the given handler or 00 if it could not be found.
2453 static const opj_jp2_header_handler_t * opj_jp2_img_find_handler(
2456 OPJ_UINT32 i, l_handler_size = sizeof(jp2_img_header) / sizeof(
2457 opj_jp2_header_handler_t);
2458 for (i = 0; i < l_handler_size; ++i) {
2459 if (jp2_img_header[i].id == p_id) {
2460 return &jp2_img_header[i];
2468 * Reads a jpeg2000 file signature box.
2470 * @param p_header_data the data contained in the signature box.
2471 * @param jp2 the jpeg2000 file codec.
2472 * @param p_header_size the size of the data contained in the signature box.
2473 * @param p_manager the user event manager.
2475 * @return true if the file signature box is valid.
2477 static OPJ_BOOL opj_jp2_read_jp(opj_jp2_t *jp2,
2478 OPJ_BYTE * p_header_data,
2479 OPJ_UINT32 p_header_size,
2480 opj_event_mgr_t * p_manager
2484 OPJ_UINT32 l_magic_number;
2487 assert(p_header_data != 00);
2489 assert(p_manager != 00);
2491 if (jp2->jp2_state != JP2_STATE_NONE) {
2492 opj_event_msg(p_manager, EVT_ERROR,
2493 "The signature box must be the first box in the file.\n");
2497 /* assure length of data is correct (4 -> magic number) */
2498 if (p_header_size != 4) {
2499 opj_event_msg(p_manager, EVT_ERROR, "Error with JP signature Box size\n");
2503 /* rearrange data */
2504 opj_read_bytes(p_header_data, &l_magic_number, 4);
2505 if (l_magic_number != 0x0d0a870a) {
2506 opj_event_msg(p_manager, EVT_ERROR,
2507 "Error with JP Signature : bad magic number\n");
2511 jp2->jp2_state |= JP2_STATE_SIGNATURE;
2517 * Reads a a FTYP box - File type box
2519 * @param p_header_data the data contained in the FTYP box.
2520 * @param jp2 the jpeg2000 file codec.
2521 * @param p_header_size the size of the data contained in the FTYP box.
2522 * @param p_manager the user event manager.
2524 * @return true if the FTYP box is valid.
2526 static OPJ_BOOL opj_jp2_read_ftyp(opj_jp2_t *jp2,
2527 OPJ_BYTE * p_header_data,
2528 OPJ_UINT32 p_header_size,
2529 opj_event_mgr_t * p_manager
2532 OPJ_UINT32 i, l_remaining_bytes;
2535 assert(p_header_data != 00);
2537 assert(p_manager != 00);
2539 if (jp2->jp2_state != JP2_STATE_SIGNATURE) {
2540 opj_event_msg(p_manager, EVT_ERROR,
2541 "The ftyp box must be the second box in the file.\n");
2545 /* assure length of data is correct */
2546 if (p_header_size < 8) {
2547 opj_event_msg(p_manager, EVT_ERROR, "Error with FTYP signature Box size\n");
2551 opj_read_bytes(p_header_data, &jp2->brand, 4); /* BR */
2554 opj_read_bytes(p_header_data, &jp2->minversion, 4); /* MinV */
2557 l_remaining_bytes = p_header_size - 8;
2559 /* the number of remaining bytes should be a multiple of 4 */
2560 if ((l_remaining_bytes & 0x3) != 0) {
2561 opj_event_msg(p_manager, EVT_ERROR, "Error with FTYP signature Box size\n");
2566 jp2->numcl = l_remaining_bytes >> 2;
2568 jp2->cl = (OPJ_UINT32 *) opj_calloc(jp2->numcl, sizeof(OPJ_UINT32));
2569 if (jp2->cl == 00) {
2570 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory with FTYP Box\n");
2575 for (i = 0; i < jp2->numcl; ++i) {
2576 opj_read_bytes(p_header_data, &jp2->cl[i], 4); /* CLi */
2580 jp2->jp2_state |= JP2_STATE_FILE_TYPE;
2585 static OPJ_BOOL opj_jp2_skip_jp2c(opj_jp2_t *jp2,
2586 opj_stream_private_t *stream,
2587 opj_event_mgr_t * p_manager)
2591 assert(stream != 00);
2592 assert(p_manager != 00);
2594 jp2->j2k_codestream_offset = opj_stream_tell(stream);
2596 if (opj_stream_skip(stream, 8, p_manager) != 8) {
2603 static OPJ_BOOL opj_jpip_skip_iptr(opj_jp2_t *jp2,
2604 opj_stream_private_t *stream,
2605 opj_event_mgr_t * p_manager)
2609 assert(stream != 00);
2610 assert(p_manager != 00);
2612 jp2->jpip_iptr_offset = opj_stream_tell(stream);
2614 if (opj_stream_skip(stream, 24, p_manager) != 24) {
2622 * Reads the Jpeg2000 file Header box - JP2 Header box (warning, this is a super box).
2624 * @param p_header_data the data contained in the file header box.
2625 * @param jp2 the jpeg2000 file codec.
2626 * @param p_header_size the size of the data contained in the file header box.
2627 * @param p_manager the user event manager.
2629 * @return true if the JP2 Header box was successfully recognized.
2631 static OPJ_BOOL opj_jp2_read_jp2h(opj_jp2_t *jp2,
2632 OPJ_BYTE *p_header_data,
2633 OPJ_UINT32 p_header_size,
2634 opj_event_mgr_t * p_manager
2637 OPJ_UINT32 l_box_size = 0, l_current_data_size = 0;
2639 const opj_jp2_header_handler_t * l_current_handler;
2640 OPJ_BOOL l_has_ihdr = 0;
2643 assert(p_header_data != 00);
2645 assert(p_manager != 00);
2647 /* make sure the box is well placed */
2648 if ((jp2->jp2_state & JP2_STATE_FILE_TYPE) != JP2_STATE_FILE_TYPE) {
2649 opj_event_msg(p_manager, EVT_ERROR,
2650 "The box must be the first box in the file.\n");
2654 jp2->jp2_img_state = JP2_IMG_STATE_NONE;
2656 /* iterate while remaining data */
2657 while (p_header_size > 0) {
2659 if (! opj_jp2_read_boxhdr_char(&box, p_header_data, &l_box_size, p_header_size,
2661 opj_event_msg(p_manager, EVT_ERROR,
2662 "Stream error while reading JP2 Header box\n");
2666 if (box.length > p_header_size) {
2667 opj_event_msg(p_manager, EVT_ERROR,
2668 "Stream error while reading JP2 Header box: box length is inconsistent.\n");
2672 l_current_handler = opj_jp2_img_find_handler(box.type);
2673 l_current_data_size = box.length - l_box_size;
2674 p_header_data += l_box_size;
2676 if (l_current_handler != 00) {
2677 if (! l_current_handler->handler(jp2, p_header_data, l_current_data_size,
2682 jp2->jp2_img_state |= JP2_IMG_STATE_UNKNOWN;
2685 if (box.type == JP2_IHDR) {
2689 p_header_data += l_current_data_size;
2690 p_header_size -= box.length;
2693 if (l_has_ihdr == 0) {
2694 opj_event_msg(p_manager, EVT_ERROR,
2695 "Stream error while reading JP2 Header box: no 'ihdr' box.\n");
2699 jp2->jp2_state |= JP2_STATE_HEADER;
2704 static OPJ_BOOL opj_jp2_read_boxhdr_char(opj_jp2_box_t *box,
2706 OPJ_UINT32 * p_number_bytes_read,
2707 OPJ_UINT32 p_box_max_size,
2708 opj_event_mgr_t * p_manager
2714 assert(p_data != 00);
2716 assert(p_number_bytes_read != 00);
2717 assert(p_manager != 00);
2719 if (p_box_max_size < 8) {
2720 opj_event_msg(p_manager, EVT_ERROR, "Cannot handle box of less than 8 bytes\n");
2724 /* process read data */
2725 opj_read_bytes(p_data, &l_value, 4);
2727 box->length = (OPJ_UINT32)(l_value);
2729 opj_read_bytes(p_data, &l_value, 4);
2731 box->type = (OPJ_UINT32)(l_value);
2733 *p_number_bytes_read = 8;
2735 /* do we have a "special very large box ?" */
2736 /* read then the XLBox */
2737 if (box->length == 1) {
2738 OPJ_UINT32 l_xl_part_size;
2740 if (p_box_max_size < 16) {
2741 opj_event_msg(p_manager, EVT_ERROR,
2742 "Cannot handle XL box of less than 16 bytes\n");
2746 opj_read_bytes(p_data, &l_xl_part_size, 4);
2748 *p_number_bytes_read += 4;
2750 if (l_xl_part_size != 0) {
2751 opj_event_msg(p_manager, EVT_ERROR,
2752 "Cannot handle box sizes higher than 2^32\n");
2756 opj_read_bytes(p_data, &l_value, 4);
2757 *p_number_bytes_read += 4;
2758 box->length = (OPJ_UINT32)(l_value);
2760 if (box->length == 0) {
2761 opj_event_msg(p_manager, EVT_ERROR, "Cannot handle box of undefined sizes\n");
2764 } else if (box->length == 0) {
2765 opj_event_msg(p_manager, EVT_ERROR, "Cannot handle box of undefined sizes\n");
2768 if (box->length < *p_number_bytes_read) {
2769 opj_event_msg(p_manager, EVT_ERROR, "Box length is inconsistent.\n");
2775 OPJ_BOOL opj_jp2_read_header(opj_stream_private_t *p_stream,
2777 opj_image_t ** p_image,
2778 opj_event_mgr_t * p_manager
2783 assert(p_stream != 00);
2784 assert(p_manager != 00);
2786 /* customization of the validation */
2787 if (! opj_jp2_setup_decoding_validation(jp2, p_manager)) {
2791 /* customization of the encoding */
2792 if (! opj_jp2_setup_header_reading(jp2, p_manager)) {
2796 /* validation of the parameters codec */
2797 if (! opj_jp2_exec(jp2, jp2->m_validation_list, p_stream, p_manager)) {
2802 if (! opj_jp2_exec(jp2, jp2->m_procedure_list, p_stream, p_manager)) {
2806 return opj_j2k_read_header(p_stream,
2812 static OPJ_BOOL opj_jp2_setup_encoding_validation(opj_jp2_t *jp2,
2813 opj_event_mgr_t * p_manager)
2817 assert(p_manager != 00);
2819 if (! opj_procedure_list_add_procedure(jp2->m_validation_list,
2820 (opj_procedure)opj_jp2_default_validation, p_manager)) {
2823 /* DEVELOPER CORNER, add your custom validation procedure */
2828 static OPJ_BOOL opj_jp2_setup_decoding_validation(opj_jp2_t *jp2,
2829 opj_event_mgr_t * p_manager)
2833 assert(p_manager != 00);
2836 OPJ_UNUSED(p_manager);
2838 /* DEVELOPER CORNER, add your custom validation procedure */
2843 static OPJ_BOOL opj_jp2_setup_header_writing(opj_jp2_t *jp2,
2844 opj_event_mgr_t * p_manager)
2848 assert(p_manager != 00);
2850 if (! opj_procedure_list_add_procedure(jp2->m_procedure_list,
2851 (opj_procedure)opj_jp2_write_jp, p_manager)) {
2854 if (! opj_procedure_list_add_procedure(jp2->m_procedure_list,
2855 (opj_procedure)opj_jp2_write_ftyp, p_manager)) {
2858 if (! opj_procedure_list_add_procedure(jp2->m_procedure_list,
2859 (opj_procedure)opj_jp2_write_jp2h, p_manager)) {
2863 if (! opj_procedure_list_add_procedure(jp2->m_procedure_list,
2864 (opj_procedure)opj_jpip_skip_iptr, p_manager)) {
2868 if (! opj_procedure_list_add_procedure(jp2->m_procedure_list,
2869 (opj_procedure)opj_jp2_skip_jp2c, p_manager)) {
2873 /* DEVELOPER CORNER, insert your custom procedures */
2878 static OPJ_BOOL opj_jp2_setup_header_reading(opj_jp2_t *jp2,
2879 opj_event_mgr_t * p_manager)
2883 assert(p_manager != 00);
2885 if (! opj_procedure_list_add_procedure(jp2->m_procedure_list,
2886 (opj_procedure)opj_jp2_read_header_procedure, p_manager)) {
2890 /* DEVELOPER CORNER, add your custom procedures */
2895 OPJ_BOOL opj_jp2_read_tile_header(opj_jp2_t * p_jp2,
2896 OPJ_UINT32 * p_tile_index,
2897 OPJ_UINT32 * p_data_size,
2898 OPJ_INT32 * p_tile_x0,
2899 OPJ_INT32 * p_tile_y0,
2900 OPJ_INT32 * p_tile_x1,
2901 OPJ_INT32 * p_tile_y1,
2902 OPJ_UINT32 * p_nb_comps,
2904 opj_stream_private_t *p_stream,
2905 opj_event_mgr_t * p_manager
2908 return opj_j2k_read_tile_header(p_jp2->j2k,
2911 p_tile_x0, p_tile_y0,
2912 p_tile_x1, p_tile_y1,
2919 OPJ_BOOL opj_jp2_write_tile(opj_jp2_t *p_jp2,
2920 OPJ_UINT32 p_tile_index,
2922 OPJ_UINT32 p_data_size,
2923 opj_stream_private_t *p_stream,
2924 opj_event_mgr_t * p_manager
2928 return opj_j2k_write_tile(p_jp2->j2k, p_tile_index, p_data, p_data_size,
2929 p_stream, p_manager);
2932 OPJ_BOOL opj_jp2_decode_tile(opj_jp2_t * p_jp2,
2933 OPJ_UINT32 p_tile_index,
2935 OPJ_UINT32 p_data_size,
2936 opj_stream_private_t *p_stream,
2937 opj_event_mgr_t * p_manager
2940 return opj_j2k_decode_tile(p_jp2->j2k, p_tile_index, p_data, p_data_size,
2941 p_stream, p_manager);
2944 void opj_jp2_destroy(opj_jp2_t *jp2)
2947 /* destroy the J2K codec */
2948 opj_j2k_destroy(jp2->j2k);
2952 opj_free(jp2->comps);
2961 if (jp2->color.icc_profile_buf) {
2962 opj_free(jp2->color.icc_profile_buf);
2963 jp2->color.icc_profile_buf = 00;
2966 if (jp2->color.jp2_cdef) {
2967 if (jp2->color.jp2_cdef->info) {
2968 opj_free(jp2->color.jp2_cdef->info);
2969 jp2->color.jp2_cdef->info = NULL;
2972 opj_free(jp2->color.jp2_cdef);
2973 jp2->color.jp2_cdef = 00;
2976 if (jp2->color.jp2_pclr) {
2977 if (jp2->color.jp2_pclr->cmap) {
2978 opj_free(jp2->color.jp2_pclr->cmap);
2979 jp2->color.jp2_pclr->cmap = NULL;
2981 if (jp2->color.jp2_pclr->channel_sign) {
2982 opj_free(jp2->color.jp2_pclr->channel_sign);
2983 jp2->color.jp2_pclr->channel_sign = NULL;
2985 if (jp2->color.jp2_pclr->channel_size) {
2986 opj_free(jp2->color.jp2_pclr->channel_size);
2987 jp2->color.jp2_pclr->channel_size = NULL;
2989 if (jp2->color.jp2_pclr->entries) {
2990 opj_free(jp2->color.jp2_pclr->entries);
2991 jp2->color.jp2_pclr->entries = NULL;
2994 opj_free(jp2->color.jp2_pclr);
2995 jp2->color.jp2_pclr = 00;
2998 if (jp2->m_validation_list) {
2999 opj_procedure_list_destroy(jp2->m_validation_list);
3000 jp2->m_validation_list = 00;
3003 if (jp2->m_procedure_list) {
3004 opj_procedure_list_destroy(jp2->m_procedure_list);
3005 jp2->m_procedure_list = 00;
3012 OPJ_BOOL opj_jp2_set_decode_area(opj_jp2_t *p_jp2,
3013 opj_image_t* p_image,
3014 OPJ_INT32 p_start_x, OPJ_INT32 p_start_y,
3015 OPJ_INT32 p_end_x, OPJ_INT32 p_end_y,
3016 opj_event_mgr_t * p_manager
3019 return opj_j2k_set_decode_area(p_jp2->j2k, p_image, p_start_x, p_start_y,
3020 p_end_x, p_end_y, p_manager);
3023 OPJ_BOOL opj_jp2_get_tile(opj_jp2_t *p_jp2,
3024 opj_stream_private_t *p_stream,
3025 opj_image_t* p_image,
3026 opj_event_mgr_t * p_manager,
3027 OPJ_UINT32 tile_index
3034 opj_event_msg(p_manager, EVT_WARNING,
3035 "JP2 box which are after the codestream will not be read by this function.\n");
3037 if (! opj_j2k_get_tile(p_jp2->j2k, p_stream, p_image, p_manager, tile_index)) {
3038 opj_event_msg(p_manager, EVT_ERROR,
3039 "Failed to decode the codestream in the JP2 file\n");
3043 if (!opj_jp2_check_color(p_image, &(p_jp2->color), p_manager)) {
3047 /* Set Image Color Space */
3048 if (p_jp2->enumcs == 16) {
3049 p_image->color_space = OPJ_CLRSPC_SRGB;
3050 } else if (p_jp2->enumcs == 17) {
3051 p_image->color_space = OPJ_CLRSPC_GRAY;
3052 } else if (p_jp2->enumcs == 18) {
3053 p_image->color_space = OPJ_CLRSPC_SYCC;
3054 } else if (p_jp2->enumcs == 24) {
3055 p_image->color_space = OPJ_CLRSPC_EYCC;
3056 } else if (p_jp2->enumcs == 12) {
3057 p_image->color_space = OPJ_CLRSPC_CMYK;
3059 p_image->color_space = OPJ_CLRSPC_UNKNOWN;
3062 if (p_jp2->color.jp2_pclr) {
3063 /* Part 1, I.5.3.4: Either both or none : */
3064 if (!p_jp2->color.jp2_pclr->cmap) {
3065 opj_jp2_free_pclr(&(p_jp2->color));
3067 opj_jp2_apply_pclr(p_image, &(p_jp2->color));
3071 /* Apply the color space if needed */
3072 if (p_jp2->color.jp2_cdef) {
3073 opj_jp2_apply_cdef(p_image, &(p_jp2->color), p_manager);
3076 if (p_jp2->color.icc_profile_buf) {
3077 p_image->icc_profile_buf = p_jp2->color.icc_profile_buf;
3078 p_image->icc_profile_len = p_jp2->color.icc_profile_len;
3079 p_jp2->color.icc_profile_buf = NULL;
3085 /* ----------------------------------------------------------------------- */
3086 /* JP2 encoder interface */
3087 /* ----------------------------------------------------------------------- */
3089 opj_jp2_t* opj_jp2_create(OPJ_BOOL p_is_decoder)
3091 opj_jp2_t *jp2 = (opj_jp2_t*)opj_calloc(1, sizeof(opj_jp2_t));
3094 /* create the J2K codec */
3095 if (! p_is_decoder) {
3096 jp2->j2k = opj_j2k_create_compress();
3098 jp2->j2k = opj_j2k_create_decompress();
3101 if (jp2->j2k == 00) {
3102 opj_jp2_destroy(jp2);
3106 /* Color structure */
3107 jp2->color.icc_profile_buf = NULL;
3108 jp2->color.icc_profile_len = 0;
3109 jp2->color.jp2_cdef = NULL;
3110 jp2->color.jp2_pclr = NULL;
3111 jp2->color.jp2_has_colr = 0;
3113 /* validation list creation */
3114 jp2->m_validation_list = opj_procedure_list_create();
3115 if (! jp2->m_validation_list) {
3116 opj_jp2_destroy(jp2);
3120 /* execution list creation */
3121 jp2->m_procedure_list = opj_procedure_list_create();
3122 if (! jp2->m_procedure_list) {
3123 opj_jp2_destroy(jp2);
3131 void jp2_dump(opj_jp2_t* p_jp2, OPJ_INT32 flag, FILE* out_stream)
3134 assert(p_jp2 != 00);
3136 j2k_dump(p_jp2->j2k,
3141 opj_codestream_index_t* jp2_get_cstr_index(opj_jp2_t* p_jp2)
3143 return j2k_get_cstr_index(p_jp2->j2k);
3146 opj_codestream_info_v2_t* jp2_get_cstr_info(opj_jp2_t* p_jp2)
3148 return j2k_get_cstr_info(p_jp2->j2k);
3151 OPJ_BOOL opj_jp2_set_decoded_resolution_factor(opj_jp2_t *p_jp2,
3152 OPJ_UINT32 res_factor,
3153 opj_event_mgr_t * p_manager)
3155 return opj_j2k_set_decoded_resolution_factor(p_jp2->j2k, res_factor, p_manager);
3161 static OPJ_BOOL opj_jpip_write_iptr(opj_jp2_t *jp2,
3162 opj_stream_private_t *cio,
3163 opj_event_mgr_t * p_manager)
3165 OPJ_OFF_T j2k_codestream_exit;
3166 OPJ_BYTE l_data_header [24];
3171 assert(p_manager != 00);
3172 assert(opj_stream_has_seek(cio));
3174 j2k_codestream_exit = opj_stream_tell(cio);
3175 opj_write_bytes(l_data_header, 24, 4); /* size of iptr */
3176 opj_write_bytes(l_data_header + 4, JPIP_IPTR,
3179 opj_write_bytes(l_data_header + 4 + 4, 0, 8); /* offset */
3180 opj_write_bytes(l_data_header + 8 + 8, 0, 8); /* length */
3182 opj_write_double(l_data_header + 4 + 4, 0); /* offset */
3183 opj_write_double(l_data_header + 8 + 8, 0); /* length */
3186 if (! opj_stream_seek(cio, jp2->jpip_iptr_offset, p_manager)) {
3187 opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
3191 if (opj_stream_write_data(cio, l_data_header, 24, p_manager) != 24) {
3192 opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
3196 if (! opj_stream_seek(cio, j2k_codestream_exit, p_manager)) {
3197 opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
3204 static OPJ_BOOL opj_jpip_write_fidx(opj_jp2_t *jp2,
3205 opj_stream_private_t *cio,
3206 opj_event_mgr_t * p_manager)
3208 OPJ_OFF_T j2k_codestream_exit;
3209 OPJ_BYTE l_data_header [24];
3214 assert(p_manager != 00);
3215 assert(opj_stream_has_seek(cio));
3217 opj_write_bytes(l_data_header, 24, 4); /* size of iptr */
3218 opj_write_bytes(l_data_header + 4, JPIP_FIDX,
3220 opj_write_double(l_data_header + 4 + 4, 0); /* offset */
3221 opj_write_double(l_data_header + 8 + 8, 0); /* length */
3223 if (opj_stream_write_data(cio, l_data_header, 24, p_manager) != 24) {
3224 opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
3228 j2k_codestream_exit = opj_stream_tell(cio);
3229 if (! opj_stream_seek(cio, j2k_codestream_exit, p_manager)) {
3230 opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
3237 static OPJ_BOOL opj_jpip_write_cidx(opj_jp2_t *jp2,
3238 opj_stream_private_t *cio,
3239 opj_event_mgr_t * p_manager)
3241 OPJ_OFF_T j2k_codestream_exit;
3242 OPJ_BYTE l_data_header [24];
3247 assert(p_manager != 00);
3248 assert(opj_stream_has_seek(cio));
3250 j2k_codestream_exit = opj_stream_tell(cio);
3251 opj_write_bytes(l_data_header, 24, 4); /* size of iptr */
3252 opj_write_bytes(l_data_header + 4, JPIP_CIDX,
3255 opj_write_bytes(l_data_header + 4 + 4, 0, 8); /* offset */
3256 opj_write_bytes(l_data_header + 8 + 8, 0, 8); /* length */
3258 opj_write_double(l_data_header + 4 + 4, 0); /* offset */
3259 opj_write_double(l_data_header + 8 + 8, 0); /* length */
3262 if (! opj_stream_seek(cio, j2k_codestream_exit, p_manager)) {
3263 opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
3267 if (opj_stream_write_data(cio, l_data_header, 24, p_manager) != 24) {
3268 opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
3272 j2k_codestream_exit = opj_stream_tell(cio);
3273 if (! opj_stream_seek(cio, j2k_codestream_exit, p_manager)) {
3274 opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
3282 static void write_prxy(int offset_jp2c, int length_jp2c, int offset_idx,
3283 int length_idx, opj_stream_private_t *cio,
3284 opj_event_mgr_t * p_manager)
3286 OPJ_BYTE l_data_header [8];
3287 OPJ_OFF_T len, lenp;
3289 lenp = opj_stream_tell(cio);
3290 opj_stream_skip(cio, 4, p_manager); /* L [at the end] */
3291 opj_write_bytes(l_data_header, JPIP_PRXY, 4); /* IPTR */
3292 opj_stream_write_data(cio, l_data_header, 4, p_manager);
3294 opj_write_bytes(l_data_header, offset_jp2c, 8); /* OOFF */
3295 opj_stream_write_data(cio, l_data_header, 8, p_manager);
3296 opj_write_bytes(l_data_header, length_jp2c, 4); /* OBH part 1 */
3297 opj_write_bytes(l_data_header + 4, JP2_JP2C, 4); /* OBH part 2 */
3298 opj_stream_write_data(cio, l_data_header, 8, p_manager);
3300 opj_write_bytes(l_data_header, 1, 1); /* NI */
3301 opj_stream_write_data(cio, l_data_header, 1, p_manager);
3303 opj_write_bytes(l_data_header, offset_idx, 8); /* IOFF */
3304 opj_stream_write_data(cio, l_data_header, 8, p_manager);
3305 opj_write_bytes(l_data_header, length_idx, 4); /* IBH part 1 */
3306 opj_write_bytes(l_data_header + 4, JPIP_CIDX, 4); /* IBH part 2 */
3307 opj_stream_write_data(cio, l_data_header, 8, p_manager);
3309 len = opj_stream_tell(cio) - lenp;
3310 opj_stream_skip(cio, lenp, p_manager);
3311 opj_write_bytes(l_data_header, len, 4); /* L */
3312 opj_stream_write_data(cio, l_data_header, 4, p_manager);
3313 opj_stream_seek(cio, lenp + len, p_manager);
3319 static int write_fidx(int offset_jp2c, int length_jp2c, int offset_idx,
3320 int length_idx, opj_stream_private_t *cio,
3321 opj_event_mgr_t * p_manager)
3323 OPJ_BYTE l_data_header [4];
3324 OPJ_OFF_T len, lenp;
3326 lenp = opj_stream_tell(cio);
3327 opj_stream_skip(cio, 4, p_manager);
3328 opj_write_bytes(l_data_header, JPIP_FIDX, 4); /* FIDX */
3329 opj_stream_write_data(cio, l_data_header, 4, p_manager);
3331 write_prxy(offset_jp2c, length_jp2c, offset_idx, length_idx, cio, p_manager);
3333 len = opj_stream_tell(cio) - lenp;
3334 opj_stream_skip(cio, lenp, p_manager);
3335 opj_write_bytes(l_data_header, len, 4); /* L */
3336 opj_stream_write_data(cio, l_data_header, 4, p_manager);
3337 opj_stream_seek(cio, lenp + len, p_manager);
3342 #endif /* USE_JPIP */