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 /** @name Local static functions */
50 /*static void jp2_write_url(opj_cio_t *cio, char *Idx_file);*/
53 * Reads a IHDR box - Image Header box
55 * @param p_image_header_data pointer to actual data (already read from file)
56 * @param jp2 the jpeg2000 file codec.
57 * @param p_image_header_size the size of the image header
58 * @param p_manager the user event manager.
60 * @return true if the image header is valid, false else.
62 static OPJ_BOOL opj_jp2_read_ihdr( opj_jp2_t *jp2,
63 OPJ_BYTE *p_image_header_data,
64 OPJ_UINT32 p_image_header_size,
65 opj_event_mgr_t * p_manager );
68 * Writes the Image Header box - Image Header box.
70 * @param jp2 jpeg2000 file codec.
71 * @param p_nb_bytes_written pointer to store the nb of bytes written by the function.
73 * @return the data being copied.
75 static OPJ_BYTE * opj_jp2_write_ihdr(opj_jp2_t *jp2,
76 OPJ_UINT32 * p_nb_bytes_written );
79 * Writes the Bit per Component box.
81 * @param jp2 jpeg2000 file codec.
82 * @param p_nb_bytes_written pointer to store the nb of bytes written by the function.
84 * @return the data being copied.
86 static OPJ_BYTE * opj_jp2_write_bpcc( opj_jp2_t *jp2,
87 OPJ_UINT32 * p_nb_bytes_written );
90 * Reads a Bit per Component box.
92 * @param p_bpc_header_data pointer to actual data (already read from file)
93 * @param jp2 the jpeg2000 file codec.
94 * @param p_bpc_header_size the size of the bpc header
95 * @param p_manager the user event manager.
97 * @return true if the bpc header is valid, fale else.
99 static OPJ_BOOL opj_jp2_read_bpcc( opj_jp2_t *jp2,
100 OPJ_BYTE * p_bpc_header_data,
101 OPJ_UINT32 p_bpc_header_size,
102 opj_event_mgr_t * p_manager );
104 static OPJ_BOOL opj_jp2_read_cdef( opj_jp2_t * jp2,
105 OPJ_BYTE * p_cdef_header_data,
106 OPJ_UINT32 p_cdef_header_size,
107 opj_event_mgr_t * p_manager );
109 static void opj_jp2_apply_cdef(opj_image_t *image, opj_jp2_color_t *color);
112 * Writes the Colour Specification box.
114 * @param jp2 jpeg2000 file codec.
115 * @param p_nb_bytes_written pointer to store the nb of bytes written by the function.
117 * @return the data being copied.
119 static OPJ_BYTE * opj_jp2_write_colr( opj_jp2_t *jp2,
120 OPJ_UINT32 * p_nb_bytes_written );
123 * Writes a FTYP box - File type box
125 * @param cio the stream to write data to.
126 * @param jp2 the jpeg2000 file codec.
127 * @param p_manager the user event manager.
129 * @return true if writing was successful.
131 static OPJ_BOOL opj_jp2_write_ftyp( opj_jp2_t *jp2,
132 opj_stream_private_t *cio,
133 opj_event_mgr_t * p_manager );
136 * Reads a a FTYP box - File type box
138 * @param p_header_data the data contained in the FTYP box.
139 * @param jp2 the jpeg2000 file codec.
140 * @param p_header_size the size of the data contained in the FTYP box.
141 * @param p_manager the user event manager.
143 * @return true if the FTYP box is valid.
145 static OPJ_BOOL opj_jp2_read_ftyp( opj_jp2_t *jp2,
146 OPJ_BYTE * p_header_data,
147 OPJ_UINT32 p_header_size,
148 opj_event_mgr_t * p_manager );
150 OPJ_BOOL opj_jp2_skip_jp2c( opj_jp2_t *jp2,
151 opj_stream_private_t *cio,
152 opj_event_mgr_t * p_manager );
155 * Reads the Jpeg2000 file Header box - JP2 Header box (warning, this is a super box).
157 * @param p_header_data the data contained in the file header box.
158 * @param jp2 the jpeg2000 file codec.
159 * @param p_header_size the size of the data contained in the file header box.
160 * @param p_manager the user event manager.
162 * @return true if the JP2 Header box was successfully reconized.
164 static OPJ_BOOL opj_jp2_read_jp2h( opj_jp2_t *jp2,
165 OPJ_BYTE *p_header_data,
166 OPJ_UINT32 p_header_size,
167 opj_event_mgr_t * p_manager );
170 * Writes the Jpeg2000 codestream Header box - JP2C Header box. This function must be called AFTER the coding has been done.
172 * @param cio the stream to write data to.
173 * @param jp2 the jpeg2000 file codec.
174 * @param p_manager user event manager.
176 * @return true if writing was successful.
178 static OPJ_BOOL opj_jp2_write_jp2c( opj_jp2_t *jp2,
179 opj_stream_private_t *cio,
180 opj_event_mgr_t * p_manager );
184 * Write index Finder box
185 * @param cio the stream to write to.
186 * @param jp2 the jpeg2000 file codec.
187 * @param p_manager user event manager.
189 static OPJ_BOOL opj_jpip_write_iptr( opj_jp2_t *jp2,
190 opj_stream_private_t *cio,
191 opj_event_mgr_t * p_manager );
194 * Write index Finder box
195 * @param cio the stream to write to.
196 * @param jp2 the jpeg2000 file codec.
197 * @param p_manager user event manager.
199 static OPJ_BOOL opj_jpip_write_cidx(opj_jp2_t *jp2,
200 opj_stream_private_t *cio,
201 opj_event_mgr_t * p_manager );
204 * Write file Index (superbox)
205 * @param cio the stream to write to.
206 * @param jp2 the jpeg2000 file codec.
207 * @param p_manager user event manager.
209 static OPJ_BOOL opj_jpip_write_fidx(opj_jp2_t *jp2,
210 opj_stream_private_t *cio,
211 opj_event_mgr_t * p_manager );
212 #endif /* USE_JPIP */
215 * Reads a jpeg2000 file signature box.
217 * @param p_header_data the data contained in the signature box.
218 * @param jp2 the jpeg2000 file codec.
219 * @param p_header_size the size of the data contained in the signature box.
220 * @param p_manager the user event manager.
222 * @return true if the file signature box is valid.
224 static OPJ_BOOL opj_jp2_read_jp(opj_jp2_t *jp2,
225 OPJ_BYTE * p_header_data,
226 OPJ_UINT32 p_header_size,
227 opj_event_mgr_t * p_manager);
230 * Writes a jpeg2000 file signature box.
232 * @param cio the stream to write data to.
233 * @param jp2 the jpeg2000 file codec.
234 * @param p_manager the user event manager.
236 * @return true if writing was successful.
238 static OPJ_BOOL opj_jp2_write_jp( opj_jp2_t *jp2,
239 opj_stream_private_t *cio,
240 opj_event_mgr_t * p_manager );
243 Apply collected palette data
244 @param color Collector for profile, cdef and pclr data
247 static void opj_jp2_apply_pclr(opj_image_t *image, opj_jp2_color_t *color);
249 static void opj_jp2_free_pclr(opj_jp2_color_t *color);
252 * Collect palette data
254 * @param jp2 JP2 handle
255 * @param p_pclr_header_data FIXME DOC
256 * @param p_pclr_header_size FIXME DOC
259 * @return Returns true if successful, returns false otherwise
261 static OPJ_BOOL opj_jp2_read_pclr( opj_jp2_t *jp2,
262 OPJ_BYTE * p_pclr_header_data,
263 OPJ_UINT32 p_pclr_header_size,
264 opj_event_mgr_t * p_manager );
267 * Collect component mapping data
269 * @param jp2 JP2 handle
270 * @param p_cmap_header_data FIXME DOC
271 * @param p_cmap_header_size FIXME DOC
272 * @param p_manager FIXME DOC
274 * @return Returns true if successful, returns false otherwise
277 static OPJ_BOOL opj_jp2_read_cmap( opj_jp2_t * jp2,
278 OPJ_BYTE * p_cmap_header_data,
279 OPJ_UINT32 p_cmap_header_size,
280 opj_event_mgr_t * p_manager );
283 * Reads the Color Specification box.
285 * @param p_colr_header_data pointer to actual data (already read from file)
286 * @param jp2 the jpeg2000 file codec.
287 * @param p_colr_header_size the size of the color header
288 * @param p_manager the user event manager.
290 * @return true if the bpc header is valid, fale else.
292 static OPJ_BOOL opj_jp2_read_colr( opj_jp2_t *jp2,
293 OPJ_BYTE * p_colr_header_data,
294 OPJ_UINT32 p_colr_header_size,
295 opj_event_mgr_t * p_manager );
302 * Sets up the procedures to do on writing header after the codestream.
303 * Developpers wanting to extend the library can add their own writing procedures.
305 static void opj_jp2_setup_end_header_writing (opj_jp2_t *jp2);
308 * Sets up the procedures to do on reading header after the codestream.
309 * Developpers wanting to extend the library can add their own writing procedures.
311 static void opj_jp2_setup_end_header_reading (opj_jp2_t *jp2);
314 * Reads a jpeg2000 file header structure.
316 * @param jp2 the jpeg2000 file header structure.
317 * @param stream the stream to read data from.
318 * @param p_manager the user event manager.
320 * @return true if the box is valid.
322 static OPJ_BOOL opj_jp2_read_header_procedure( opj_jp2_t *jp2,
323 opj_stream_private_t *stream,
324 opj_event_mgr_t * p_manager );
327 * Excutes the given procedures on the given codec.
329 * @param p_procedure_list the list of procedures to execute
330 * @param jp2 the jpeg2000 file codec to execute the procedures on.
331 * @param stream the stream to execute the procedures on.
332 * @param p_manager the user manager.
334 * @return true if all the procedures were successfully executed.
336 static OPJ_BOOL opj_jp2_exec ( opj_jp2_t * jp2,
337 opj_procedure_list_t * p_procedure_list,
338 opj_stream_private_t *stream,
339 opj_event_mgr_t * p_manager );
342 * Reads a box header. The box is the way data is packed inside a jpeg2000 file structure.
344 * @param cio the input stream to read data from.
345 * @param box the box structure to fill.
346 * @param p_number_bytes_read pointer to an int that will store the number of bytes read from the stream (shoul usually be 2).
347 * @param p_manager user event manager.
349 * @return true if the box is reconized, false otherwise
351 static OPJ_BOOL opj_jp2_read_boxhdr(opj_jp2_box_t *box,
352 OPJ_UINT32 * p_number_bytes_read,
353 opj_stream_private_t *cio,
354 opj_event_mgr_t * p_manager);
357 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
358 * are valid. Developpers wanting to extend the library can add their own validation procedures.
360 static void opj_jp2_setup_encoding_validation (opj_jp2_t *jp2);
363 * Sets up the procedures to do on writing header. Developpers wanting to extend the library can add their own writing procedures.
365 static void opj_jp2_setup_header_writing (opj_jp2_t *jp2);
367 OPJ_BOOL opj_jp2_default_validation ( opj_jp2_t * jp2,
368 opj_stream_private_t *cio,
369 opj_event_mgr_t * p_manager );
372 * Finds the image execution function related to the given box id.
374 * @param p_id the id of the handler to fetch.
376 * @return the given handler or NULL if it could not be found.
378 static const opj_jp2_header_handler_t * opj_jp2_img_find_handler (OPJ_UINT32 p_id);
381 * Finds the execution function related to the given box id.
383 * @param p_id the id of the handler to fetch.
385 * @return the given handler or NULL if it could not be found.
387 static const opj_jp2_header_handler_t * opj_jp2_find_handler (OPJ_UINT32 p_id );
389 const opj_jp2_header_handler_t jp2_header [] =
391 {JP2_JP,opj_jp2_read_jp},
392 {JP2_FTYP,opj_jp2_read_ftyp},
393 {JP2_JP2H,opj_jp2_read_jp2h}
396 const opj_jp2_header_handler_t jp2_img_header [] =
398 {JP2_IHDR,opj_jp2_read_ihdr},
399 {JP2_COLR,opj_jp2_read_colr},
400 {JP2_BPCC,opj_jp2_read_bpcc},
401 {JP2_PCLR,opj_jp2_read_pclr},
402 {JP2_CMAP,opj_jp2_read_cmap},
403 {JP2_CDEF,opj_jp2_read_cdef}
408 * Reads a box header. The box is the way data is packed inside a jpeg2000 file structure. Data is read from a character string
410 * @param box the box structure to fill.
411 * @param p_data the character string to read data from.
412 * @param p_number_bytes_read pointer to an int that will store the number of bytes read from the stream (shoul usually be 2).
413 * @param p_box_max_size the maximum number of bytes in the box.
414 * @param p_manager FIXME DOC
416 * @return true if the box is reconized, false otherwise
418 static OPJ_BOOL opj_jp2_read_boxhdr_char( opj_jp2_box_t *box,
420 OPJ_UINT32 * p_number_bytes_read,
421 OPJ_UINT32 p_box_max_size,
422 opj_event_mgr_t * p_manager );
425 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
426 * are valid. Developpers wanting to extend the library can add their own validation procedures.
428 static void opj_jp2_setup_decoding_validation (opj_jp2_t *jp2);
431 * Sets up the procedures to do on reading header.
432 * Developpers wanting to extend the library can add their own writing procedures.
434 static void opj_jp2_setup_header_reading (opj_jp2_t *jp2);
436 /* ----------------------------------------------------------------------- */
437 OPJ_BOOL opj_jp2_read_boxhdr(opj_jp2_box_t *box,
438 OPJ_UINT32 * p_number_bytes_read,
439 opj_stream_private_t *cio,
440 opj_event_mgr_t * p_manager )
442 /* read header from file */
443 OPJ_BYTE l_data_header [8];
448 assert(p_number_bytes_read != 00);
449 assert(p_manager != 00);
451 *p_number_bytes_read = (OPJ_UINT32)opj_stream_read_data(cio,l_data_header,8,p_manager);
452 if (*p_number_bytes_read != 8) {
456 /* process read data */
457 opj_read_bytes(l_data_header,&(box->length), 4);
458 opj_read_bytes(l_data_header+4,&(box->type), 4);
460 if(box->length == 0)/* last box */
462 const OPJ_OFF_T bleft = opj_stream_get_number_byte_left(cio);
463 box->length = (OPJ_UINT32)bleft;
464 assert( (OPJ_OFF_T)box->length == bleft );
468 /* do we have a "special very large box ?" */
469 /* read then the XLBox */
470 if (box->length == 1) {
471 OPJ_UINT32 l_xl_part_size;
473 OPJ_UINT32 l_nb_bytes_read = (OPJ_UINT32)opj_stream_read_data(cio,l_data_header,8,p_manager);
474 if (l_nb_bytes_read != 8) {
475 if (l_nb_bytes_read > 0) {
476 *p_number_bytes_read += l_nb_bytes_read;
482 *p_number_bytes_read = 16;
483 opj_read_bytes(l_data_header,&l_xl_part_size, 4);
484 if (l_xl_part_size != 0) {
485 opj_event_msg(p_manager, EVT_ERROR, "Cannot handle box sizes higher than 2^32\n");
488 opj_read_bytes(l_data_header+4,&(box->length), 4);
494 static void jp2_write_url(opj_cio_t *cio, char *Idx_file) {
498 box.init_pos = cio_tell(cio);
500 cio_write(cio, JP2_URL, 4); /* DBTL */
501 cio_write(cio, 0, 1); /* VERS */
502 cio_write(cio, 0, 3); /* FLAG */
505 for (i = 0; i < strlen(Idx_file); i++) {
506 cio_write(cio, Idx_file[i], 1);
510 box.length = cio_tell(cio) - box.init_pos;
511 cio_seek(cio, box.init_pos);
512 cio_write(cio, box.length, 4); /* L */
513 cio_seek(cio, box.init_pos + box.length);
517 OPJ_BOOL opj_jp2_read_ihdr( opj_jp2_t *jp2,
518 OPJ_BYTE *p_image_header_data,
519 OPJ_UINT32 p_image_header_size,
520 opj_event_mgr_t * p_manager )
523 assert(p_image_header_data != 00);
525 assert(p_manager != 00);
527 if (p_image_header_size != 14) {
528 opj_event_msg(p_manager, EVT_ERROR, "Bad image header box (bad size)\n");
532 opj_read_bytes(p_image_header_data,&(jp2->h),4); /* HEIGHT */
533 p_image_header_data += 4;
534 opj_read_bytes(p_image_header_data,&(jp2->w),4); /* WIDTH */
535 p_image_header_data += 4;
536 opj_read_bytes(p_image_header_data,&(jp2->numcomps),2); /* NC */
537 p_image_header_data += 2;
539 /* allocate memory for components */
540 jp2->comps = (opj_jp2_comps_t*) opj_malloc(jp2->numcomps * sizeof(opj_jp2_comps_t));
541 if (jp2->comps == 0) {
542 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to handle image header (ihdr)\n");
545 memset(jp2->comps,0,jp2->numcomps * sizeof(opj_jp2_comps_t));
547 opj_read_bytes(p_image_header_data,&(jp2->bpc),1); /* BPC */
548 ++ p_image_header_data;
550 opj_read_bytes(p_image_header_data,&(jp2->C),1); /* C */
551 ++ p_image_header_data;
553 /* Should be equal to 7 cf. chapter about image header box of the norm */
555 opj_event_msg(p_manager, EVT_INFO, "JP2 IHDR box: compression type indicate that the file is not a conforming JP2 file (%d) \n", jp2->C);
558 opj_read_bytes(p_image_header_data,&(jp2->UnkC),1); /* UnkC */
559 ++ p_image_header_data;
560 opj_read_bytes(p_image_header_data,&(jp2->IPR),1); /* IPR */
561 ++ p_image_header_data;
566 OPJ_BYTE * opj_jp2_write_ihdr(opj_jp2_t *jp2,
567 OPJ_UINT32 * p_nb_bytes_written
570 OPJ_BYTE * l_ihdr_data,* l_current_ihdr_ptr;
574 assert(p_nb_bytes_written != 00);
576 /* default image header is 22 bytes wide */
577 l_ihdr_data = (OPJ_BYTE *) opj_malloc(22);
578 if (l_ihdr_data == 00) {
581 memset(l_ihdr_data,0,22);
583 l_current_ihdr_ptr = l_ihdr_data;
585 opj_write_bytes(l_current_ihdr_ptr,22,4); /* write box size */
586 l_current_ihdr_ptr+=4;
588 opj_write_bytes(l_current_ihdr_ptr,JP2_IHDR, 4); /* IHDR */
589 l_current_ihdr_ptr+=4;
591 opj_write_bytes(l_current_ihdr_ptr,jp2->h, 4); /* HEIGHT */
592 l_current_ihdr_ptr+=4;
594 opj_write_bytes(l_current_ihdr_ptr, jp2->w, 4); /* WIDTH */
595 l_current_ihdr_ptr+=4;
597 opj_write_bytes(l_current_ihdr_ptr, jp2->numcomps, 2); /* NC */
598 l_current_ihdr_ptr+=2;
600 opj_write_bytes(l_current_ihdr_ptr, jp2->bpc, 1); /* BPC */
601 ++l_current_ihdr_ptr;
603 opj_write_bytes(l_current_ihdr_ptr, jp2->C, 1); /* C : Always 7 */
604 ++l_current_ihdr_ptr;
606 opj_write_bytes(l_current_ihdr_ptr, jp2->UnkC, 1); /* UnkC, colorspace unknown */
607 ++l_current_ihdr_ptr;
609 opj_write_bytes(l_current_ihdr_ptr, jp2->IPR, 1); /* IPR, no intellectual property */
610 ++l_current_ihdr_ptr;
612 *p_nb_bytes_written = 22;
617 OPJ_BYTE * opj_jp2_write_bpcc( opj_jp2_t *jp2,
618 OPJ_UINT32 * p_nb_bytes_written
622 /* room for 8 bytes for box and 1 byte for each component */
623 OPJ_UINT32 l_bpcc_size = 8 + jp2->numcomps;
624 OPJ_BYTE * l_bpcc_data,* l_current_bpcc_ptr;
628 assert(p_nb_bytes_written != 00);
630 l_bpcc_data = (OPJ_BYTE *) opj_malloc(l_bpcc_size);
631 if (l_bpcc_data == 00) {
634 memset(l_bpcc_data,0,l_bpcc_size);
636 l_current_bpcc_ptr = l_bpcc_data;
638 opj_write_bytes(l_current_bpcc_ptr,l_bpcc_size,4); /* write box size */
639 l_current_bpcc_ptr += 4;
641 opj_write_bytes(l_current_bpcc_ptr,JP2_BPCC,4); /* BPCC */
642 l_current_bpcc_ptr += 4;
644 for (i = 0; i < jp2->numcomps; ++i) {
645 opj_write_bytes(l_current_bpcc_ptr, jp2->comps[i].bpcc, 1); /* write each component information */
646 ++l_current_bpcc_ptr;
649 *p_nb_bytes_written = l_bpcc_size;
654 OPJ_BOOL opj_jp2_read_bpcc( opj_jp2_t *jp2,
655 OPJ_BYTE * p_bpc_header_data,
656 OPJ_UINT32 p_bpc_header_size,
657 opj_event_mgr_t * p_manager
663 assert(p_bpc_header_data != 00);
665 assert(p_manager != 00);
668 if (jp2->bpc != 255 ){
669 opj_event_msg(p_manager, EVT_WARNING, "A BPCC header box is available although BPC given by the IHDR box (%d) indicate components bit depth is constant\n",jp2->bpc);
672 /* and length is relevant */
673 if (p_bpc_header_size != jp2->numcomps) {
674 opj_event_msg(p_manager, EVT_ERROR, "Bad BPCC header box (bad size)\n");
678 /* read info for each component */
679 for (i = 0; i < jp2->numcomps; ++i) {
680 opj_read_bytes(p_bpc_header_data,&jp2->comps[i].bpcc ,1); /* read each BPCC component */
687 OPJ_BYTE * opj_jp2_write_colr( opj_jp2_t *jp2,
688 OPJ_UINT32 * p_nb_bytes_written
691 /* room for 8 bytes for box 3 for common data and variable upon profile*/
692 OPJ_UINT32 l_colr_size = 11;
693 OPJ_BYTE * l_colr_data,* l_current_colr_ptr;
697 assert(p_nb_bytes_written != 00);
698 assert(jp2->meth == 1 || jp2->meth == 2);
702 l_colr_size += 4; /* EnumCS */
705 assert(jp2->color.icc_profile_len); /* ICC profile */
706 l_colr_size += jp2->color.icc_profile_len;
712 l_colr_data = (OPJ_BYTE *) opj_malloc(l_colr_size);
713 if (l_colr_data == 00) {
716 memset(l_colr_data,0,l_colr_size);
718 l_current_colr_ptr = l_colr_data;
720 opj_write_bytes(l_current_colr_ptr,l_colr_size,4); /* write box size */
721 l_current_colr_ptr += 4;
723 opj_write_bytes(l_current_colr_ptr,JP2_COLR,4); /* BPCC */
724 l_current_colr_ptr += 4;
726 opj_write_bytes(l_current_colr_ptr, jp2->meth,1); /* METH */
727 ++l_current_colr_ptr;
729 opj_write_bytes(l_current_colr_ptr, jp2->precedence,1); /* PRECEDENCE */
730 ++l_current_colr_ptr;
732 opj_write_bytes(l_current_colr_ptr, jp2->approx,1); /* APPROX */
733 ++l_current_colr_ptr;
735 if (jp2->meth == 1) { /* Meth value is restricted to 1 or 2 (Table I.9 of part 1) */
736 opj_write_bytes(l_current_colr_ptr, jp2->enumcs,4); } /* EnumCS */
738 if (jp2->meth == 2) { /* ICC profile */
740 for(i = 0; i < jp2->color.icc_profile_len; ++i) {
741 opj_write_bytes(l_current_colr_ptr, jp2->color.icc_profile_buf[i], 1);
742 ++l_current_colr_ptr;
747 *p_nb_bytes_written = l_colr_size;
752 void opj_jp2_free_pclr(opj_jp2_color_t *color)
754 opj_free(color->jp2_pclr->channel_sign);
755 opj_free(color->jp2_pclr->channel_size);
756 opj_free(color->jp2_pclr->entries);
758 if(color->jp2_pclr->cmap) opj_free(color->jp2_pclr->cmap);
760 opj_free(color->jp2_pclr); color->jp2_pclr = NULL;
763 static OPJ_BOOL opj_jp2_check_color(opj_image_t *image, opj_jp2_color_t *color, opj_event_mgr_t *p_manager)
767 /* testcase 4149.pdf.SIGSEGV.cf7.3501 */
768 if (color->jp2_cdef) {
769 opj_jp2_cdef_info_t *info = color->jp2_cdef->info;
770 OPJ_UINT16 n = color->jp2_cdef->n;
772 for (i = 0; i < n; i++) {
773 if (info[i].cn >= image->numcomps) {
774 opj_event_msg(p_manager, EVT_ERROR, "Invalid component index %d (>= %d).\n", info[i].cn, image->numcomps);
777 if (info[i].asoc > 0 && (OPJ_UINT32)(info[i].asoc - 1) >= image->numcomps) {
778 opj_event_msg(p_manager, EVT_ERROR, "Invalid component index %d (>= %d).\n", info[i].asoc - 1, image->numcomps);
784 /* testcases 451.pdf.SIGSEGV.f4c.3723, 451.pdf.SIGSEGV.5b5.3723 and
785 66ea31acbb0f23a2bbc91f64d69a03f5_signal_sigsegv_13937c0_7030_5725.pdf */
786 if (color->jp2_pclr && color->jp2_pclr->cmap) {
787 OPJ_UINT16 nr_channels = color->jp2_pclr->nr_channels;
788 opj_jp2_cmap_comp_t *cmap = color->jp2_pclr->cmap;
789 OPJ_BOOL *pcol_usage, is_sane = OPJ_TRUE;
791 /* verify that all original components match an existing one */
792 for (i = 0; i < nr_channels; i++) {
793 if (cmap[i].cmp >= image->numcomps) {
794 opj_event_msg(p_manager, EVT_ERROR, "Invalid component index %d (>= %d).\n", cmap[i].cmp, image->numcomps);
799 pcol_usage = opj_calloc(nr_channels, sizeof(OPJ_BOOL));
801 opj_event_msg(p_manager, EVT_ERROR, "Unexpected OOM.\n");
804 /* verify that no component is targeted more than once */
805 for (i = 0; i < nr_channels; i++) {
806 OPJ_UINT16 pcol = cmap[i].pcol;
807 assert(cmap[i].mtyp == 0 || cmap[i].mtyp == 1);
808 if (pcol >= nr_channels) {
809 opj_event_msg(p_manager, EVT_ERROR, "Invalid component/palette index for direct mapping %d.\n", pcol);
812 else if (pcol_usage[pcol] && cmap[i].mtyp == 1) {
813 opj_event_msg(p_manager, EVT_ERROR, "Component %d is mapped twice.\n", pcol);
816 else if (cmap[i].mtyp == 0 && cmap[i].pcol != 0) {
817 /* I.5.3.5 PCOL: If the value of the MTYP field for this channel is 0, then
818 * the value of this field shall be 0. */
819 opj_event_msg(p_manager, EVT_ERROR, "Direct use at #%d however pcol=%d.\n", i, pcol);
823 pcol_usage[pcol] = OPJ_TRUE;
825 /* verify that all components are targeted at least once */
826 for (i = 0; i < nr_channels; i++) {
827 if (!pcol_usage[i] && cmap[i].mtyp != 0) {
828 opj_event_msg(p_manager, EVT_ERROR, "Component %d doesn't have a mapping.\n", i);
832 opj_free(pcol_usage);
842 void opj_jp2_apply_pclr(opj_image_t *image, opj_jp2_color_t *color)
844 opj_image_comp_t *old_comps, *new_comps;
845 OPJ_BYTE *channel_size, *channel_sign;
847 opj_jp2_cmap_comp_t *cmap;
848 OPJ_INT32 *src, *dst;
850 OPJ_UINT16 i, nr_channels, cmp, pcol;
853 channel_size = color->jp2_pclr->channel_size;
854 channel_sign = color->jp2_pclr->channel_sign;
855 entries = color->jp2_pclr->entries;
856 cmap = color->jp2_pclr->cmap;
857 nr_channels = color->jp2_pclr->nr_channels;
859 old_comps = image->comps;
860 new_comps = (opj_image_comp_t*)
861 opj_malloc(nr_channels * sizeof(opj_image_comp_t));
863 for(i = 0; i < nr_channels; ++i) {
864 pcol = cmap[i].pcol; cmp = cmap[i].cmp;
867 if(cmap[i].mtyp == 0){
869 new_comps[i] = old_comps[cmp];
872 new_comps[pcol] = old_comps[cmp];
875 /* Palette mapping: */
876 new_comps[i].data = (OPJ_INT32*)
877 opj_malloc(old_comps[cmp].w * old_comps[cmp].h * sizeof(OPJ_INT32));
878 new_comps[i].prec = channel_size[i];
879 new_comps[i].sgnd = channel_sign[i];
882 top_k = color->jp2_pclr->nr_entries - 1;
884 for(i = 0; i < nr_channels; ++i) {
885 /* Palette mapping: */
886 cmp = cmap[i].cmp; pcol = cmap[i].pcol;
887 src = old_comps[cmp].data;
889 max = new_comps[pcol].w * new_comps[pcol].h;
892 if(cmap[i].mtyp == 0) {
894 dst = new_comps[i].data;
896 for(j = 0; j < max; ++j) {
902 dst = new_comps[pcol].data;
904 for(j = 0; j < max; ++j) {
906 if((k = src[j]) < 0) k = 0; else if(k > top_k) k = top_k;
909 dst[j] = (OPJ_INT32)entries[k * nr_channels + pcol];
914 max = image->numcomps;
915 for(i = 0; i < max; ++i) {
916 if(old_comps[i].data) opj_free(old_comps[i].data);
920 image->comps = new_comps;
921 image->numcomps = nr_channels;
923 opj_jp2_free_pclr(color);
927 OPJ_BOOL opj_jp2_read_pclr( opj_jp2_t *jp2,
928 OPJ_BYTE * p_pclr_header_data,
929 OPJ_UINT32 p_pclr_header_size,
930 opj_event_mgr_t * p_manager
933 opj_jp2_pclr_t *jp2_pclr;
934 OPJ_BYTE *channel_size, *channel_sign;
936 OPJ_UINT16 nr_entries,nr_channels;
939 OPJ_BYTE *orig_header_data = p_pclr_header_data;
942 assert(p_pclr_header_data != 00);
944 assert(p_manager != 00);
945 (void)p_pclr_header_size;
947 if(jp2->color.jp2_pclr)
950 if (p_pclr_header_size < 3)
953 opj_read_bytes(p_pclr_header_data, &l_value , 2); /* NE */
954 p_pclr_header_data += 2;
955 nr_entries = (OPJ_UINT16) l_value;
957 opj_read_bytes(p_pclr_header_data, &l_value , 1); /* NPC */
958 ++p_pclr_header_data;
959 nr_channels = (OPJ_UINT16) l_value;
961 if (p_pclr_header_size < 3 + (OPJ_UINT32)nr_channels || nr_channels == 0 || nr_entries >= (OPJ_UINT32)-1 / nr_channels)
964 entries = (OPJ_UINT32*) opj_malloc((size_t)nr_channels * nr_entries * sizeof(OPJ_UINT32));
967 channel_size = (OPJ_BYTE*) opj_malloc(nr_channels);
973 channel_sign = (OPJ_BYTE*) opj_malloc(nr_channels);
977 opj_free(channel_size);
981 jp2_pclr = (opj_jp2_pclr_t*)opj_malloc(sizeof(opj_jp2_pclr_t));
985 opj_free(channel_size);
986 opj_free(channel_sign);
990 jp2_pclr->channel_sign = channel_sign;
991 jp2_pclr->channel_size = channel_size;
992 jp2_pclr->entries = entries;
993 jp2_pclr->nr_entries = nr_entries;
994 jp2_pclr->nr_channels = (OPJ_BYTE) l_value;
995 jp2_pclr->cmap = NULL;
997 jp2->color.jp2_pclr = jp2_pclr;
999 for(i = 0; i < nr_channels; ++i) {
1000 opj_read_bytes(p_pclr_header_data, &l_value , 1); /* Bi */
1001 ++p_pclr_header_data;
1003 channel_size[i] = (OPJ_BYTE)((l_value & 0x7f) + 1);
1004 channel_sign[i] = (l_value & 0x80) ? 1 : 0;
1007 for(j = 0; j < nr_entries; ++j) {
1008 for(i = 0; i < nr_channels; ++i) {
1009 OPJ_UINT32 bytes_to_read = (OPJ_UINT32)((channel_size[i]+7)>>3);
1011 if (bytes_to_read > sizeof(OPJ_UINT32))
1012 bytes_to_read = sizeof(OPJ_UINT32);
1013 if ((ptrdiff_t)p_pclr_header_size < p_pclr_header_data - orig_header_data + (ptrdiff_t)bytes_to_read)
1016 opj_read_bytes(p_pclr_header_data, &l_value , bytes_to_read); /* Cji */
1017 p_pclr_header_data += bytes_to_read;
1018 *entries = (OPJ_UINT32) l_value;
1026 OPJ_BOOL opj_jp2_read_cmap( opj_jp2_t * jp2,
1027 OPJ_BYTE * p_cmap_header_data,
1028 OPJ_UINT32 p_cmap_header_size,
1029 opj_event_mgr_t * p_manager
1032 opj_jp2_cmap_comp_t *cmap;
1033 OPJ_BYTE i, nr_channels;
1038 assert(p_cmap_header_data != 00);
1039 assert(p_manager != 00);
1040 (void)p_cmap_header_size;
1042 /* Need nr_channels: */
1043 if(jp2->color.jp2_pclr == NULL) {
1044 opj_event_msg(p_manager, EVT_ERROR, "Need to read a PCLR box before the CMAP box.\n");
1048 /* Part 1, I.5.3.5: 'There shall be at most one Component Mapping box
1049 * inside a JP2 Header box' :
1051 if(jp2->color.jp2_pclr->cmap) {
1052 opj_event_msg(p_manager, EVT_ERROR, "Only one CMAP box is allowed.\n");
1056 nr_channels = jp2->color.jp2_pclr->nr_channels;
1057 if (p_cmap_header_size < (OPJ_UINT32)nr_channels * 4) {
1058 opj_event_msg(p_manager, EVT_ERROR, "Insufficient data for CMAP box.\n");
1062 cmap = (opj_jp2_cmap_comp_t*) opj_malloc(nr_channels * sizeof(opj_jp2_cmap_comp_t));
1067 for(i = 0; i < nr_channels; ++i) {
1068 opj_read_bytes(p_cmap_header_data, &l_value, 2); /* CMP^i */
1069 p_cmap_header_data +=2;
1070 cmap[i].cmp = (OPJ_UINT16) l_value;
1072 opj_read_bytes(p_cmap_header_data, &l_value, 1); /* MTYP^i */
1073 ++p_cmap_header_data;
1074 cmap[i].mtyp = (OPJ_BYTE) l_value;
1076 opj_read_bytes(p_cmap_header_data, &l_value, 1); /* PCOL^i */
1077 ++p_cmap_header_data;
1078 cmap[i].pcol = (OPJ_BYTE) l_value;
1081 jp2->color.jp2_pclr->cmap = cmap;
1086 void opj_jp2_apply_cdef(opj_image_t *image, opj_jp2_color_t *color)
1088 opj_jp2_cdef_info_t *info;
1089 OPJ_UINT16 i, n, cn, asoc, acn;
1091 info = color->jp2_cdef->info;
1092 n = color->jp2_cdef->n;
1094 for(i = 0; i < n; ++i)
1096 /* WATCH: acn = asoc - 1 ! */
1097 asoc = info[i].asoc;
1098 if(asoc == 0 || asoc == 65535)
1100 if (i < image->numcomps)
1101 image->comps[i].alpha = info[i].typ;
1106 acn = (OPJ_UINT16)(asoc - 1);
1107 if( cn >= image->numcomps || acn >= image->numcomps )
1109 fprintf(stderr, "cn=%d, acn=%d, numcomps=%d\n", cn, acn, image->numcomps);
1115 opj_image_comp_t saved;
1117 memcpy(&saved, &image->comps[cn], sizeof(opj_image_comp_t));
1118 memcpy(&image->comps[cn], &image->comps[acn], sizeof(opj_image_comp_t));
1119 memcpy(&image->comps[acn], &saved, sizeof(opj_image_comp_t));
1121 info[i].asoc = (OPJ_UINT16)(cn + 1);
1122 info[acn].asoc = (OPJ_UINT16)(info[acn].cn + 1);
1125 image->comps[cn].alpha = info[i].typ;
1128 if(color->jp2_cdef->info) opj_free(color->jp2_cdef->info);
1130 opj_free(color->jp2_cdef); color->jp2_cdef = NULL;
1132 }/* jp2_apply_cdef() */
1134 OPJ_BOOL opj_jp2_read_cdef( opj_jp2_t * jp2,
1135 OPJ_BYTE * p_cdef_header_data,
1136 OPJ_UINT32 p_cdef_header_size,
1137 opj_event_mgr_t * p_manager
1140 opj_jp2_cdef_info_t *cdef_info;
1146 assert(p_cdef_header_data != 00);
1147 assert(p_manager != 00);
1148 (void)p_cdef_header_size;
1150 /* Part 1, I.5.3.6: 'The shall be at most one Channel Definition box
1151 * inside a JP2 Header box.'*/
1152 if(jp2->color.jp2_cdef) return OPJ_FALSE;
1154 if (p_cdef_header_size < 2) {
1155 opj_event_msg(p_manager, EVT_ERROR, "Insufficient data for CDEF box.\n");
1159 opj_read_bytes(p_cdef_header_data,&l_value ,2); /* N */
1160 p_cdef_header_data+= 2;
1162 if ( (OPJ_UINT16)l_value == 0){ /* szukw000: FIXME */
1163 opj_event_msg(p_manager, EVT_ERROR, "Number of channel description is equal to zero in CDEF box.\n");
1167 if (p_cdef_header_size < 2 + (OPJ_UINT32)(OPJ_UINT16)l_value * 6) {
1168 opj_event_msg(p_manager, EVT_ERROR, "Insufficient data for CDEF box.\n");
1172 cdef_info = (opj_jp2_cdef_info_t*) opj_malloc(l_value * sizeof(opj_jp2_cdef_info_t));
1176 jp2->color.jp2_cdef = (opj_jp2_cdef_t*)opj_malloc(sizeof(opj_jp2_cdef_t));
1177 if(!jp2->color.jp2_cdef)
1179 opj_free(cdef_info);
1182 jp2->color.jp2_cdef->info = cdef_info;
1183 jp2->color.jp2_cdef->n = (OPJ_UINT16) l_value;
1185 for(i = 0; i < jp2->color.jp2_cdef->n; ++i) {
1186 opj_read_bytes(p_cdef_header_data, &l_value, 2); /* Cn^i */
1187 p_cdef_header_data +=2;
1188 cdef_info[i].cn = (OPJ_UINT16) l_value;
1190 opj_read_bytes(p_cdef_header_data, &l_value, 2); /* Typ^i */
1191 p_cdef_header_data +=2;
1192 cdef_info[i].typ = (OPJ_UINT16) l_value;
1194 opj_read_bytes(p_cdef_header_data, &l_value, 2); /* Asoc^i */
1195 p_cdef_header_data +=2;
1196 cdef_info[i].asoc = (OPJ_UINT16) l_value;
1202 OPJ_BOOL opj_jp2_read_colr( opj_jp2_t *jp2,
1203 OPJ_BYTE * p_colr_header_data,
1204 OPJ_UINT32 p_colr_header_size,
1205 opj_event_mgr_t * p_manager
1212 assert(p_colr_header_data != 00);
1213 assert(p_manager != 00);
1215 if (p_colr_header_size < 3) {
1216 opj_event_msg(p_manager, EVT_ERROR, "Bad COLR header box (bad size)\n");
1220 /* Part 1, I.5.3.3 : 'A conforming JP2 reader shall ignore all Colour
1221 * Specification boxes after the first.'
1223 if(jp2->color.jp2_has_colr) {
1224 opj_event_msg(p_manager, EVT_INFO, "A conforming JP2 reader shall ignore all Colour Specification boxes after the first, so we ignore this one.\n");
1225 p_colr_header_data += p_colr_header_size;
1229 opj_read_bytes(p_colr_header_data,&jp2->meth ,1); /* METH */
1230 ++p_colr_header_data;
1232 opj_read_bytes(p_colr_header_data,&jp2->precedence ,1); /* PRECEDENCE */
1233 ++p_colr_header_data;
1235 opj_read_bytes(p_colr_header_data,&jp2->approx ,1); /* APPROX */
1236 ++p_colr_header_data;
1238 if (jp2->meth == 1) {
1239 if (p_colr_header_size < 7) {
1240 opj_event_msg(p_manager, EVT_ERROR, "Bad COLR header box (bad size: %d)\n", p_colr_header_size);
1243 if (p_colr_header_size > 7) {
1244 /* testcase Altona_Technical_v20_x4.pdf */
1245 opj_event_msg(p_manager, EVT_WARNING, "Bad COLR header box (bad size: %d)\n", p_colr_header_size);
1248 opj_read_bytes(p_colr_header_data,&jp2->enumcs ,4); /* EnumCS */
1250 jp2->color.jp2_has_colr = 1;
1252 else if (jp2->meth == 2) {
1254 OPJ_INT32 it_icc_value = 0;
1255 OPJ_INT32 icc_len = (OPJ_INT32)p_colr_header_size - 3;
1257 jp2->color.icc_profile_len = (OPJ_UINT32)icc_len;
1258 jp2->color.icc_profile_buf = (OPJ_BYTE*) opj_malloc((size_t)icc_len);
1259 if (!jp2->color.icc_profile_buf)
1261 jp2->color.icc_profile_len = 0;
1264 memset(jp2->color.icc_profile_buf, 0, (size_t)icc_len * sizeof(OPJ_BYTE));
1266 for (it_icc_value = 0; it_icc_value < icc_len; ++it_icc_value)
1268 opj_read_bytes(p_colr_header_data,&l_value,1); /* icc values */
1269 ++p_colr_header_data;
1270 jp2->color.icc_profile_buf[it_icc_value] = (OPJ_BYTE) l_value;
1273 jp2->color.jp2_has_colr = 1;
1275 else if (jp2->meth > 2)
1277 /* ISO/IEC 15444-1:2004 (E), Table I.9 � Legal METH values:
1278 conforming JP2 reader shall ignore the entire Colour Specification box.*/
1279 opj_event_msg(p_manager, EVT_INFO, "COLR BOX meth value is not a regular value (%d), "
1280 "so we will ignore the entire Colour Specification box. \n", jp2->meth);
1285 OPJ_BOOL opj_jp2_decode(opj_jp2_t *jp2,
1286 opj_stream_private_t *p_stream,
1287 opj_image_t* p_image,
1288 opj_event_mgr_t * p_manager)
1294 if( ! opj_j2k_decode(jp2->j2k, p_stream, p_image, p_manager) ) {
1295 opj_event_msg(p_manager, EVT_ERROR, "Failed to decode the codestream in the JP2 file\n");
1299 if (!jp2->ignore_pclr_cmap_cdef){
1300 if (!opj_jp2_check_color(p_image, &(jp2->color), p_manager)) {
1304 /* Set Image Color Space */
1305 if (jp2->enumcs == 16)
1306 p_image->color_space = OPJ_CLRSPC_SRGB;
1307 else if (jp2->enumcs == 17)
1308 p_image->color_space = OPJ_CLRSPC_GRAY;
1309 else if (jp2->enumcs == 18)
1310 p_image->color_space = OPJ_CLRSPC_SYCC;
1311 else if (jp2->enumcs == 24)
1312 p_image->color_space = OPJ_CLRSPC_EYCC;
1314 p_image->color_space = OPJ_CLRSPC_UNKNOWN;
1316 /* Apply the color space if needed */
1317 if(jp2->color.jp2_cdef) {
1318 opj_jp2_apply_cdef(p_image, &(jp2->color));
1321 if(jp2->color.jp2_pclr) {
1322 /* Part 1, I.5.3.4: Either both or none : */
1323 if( !jp2->color.jp2_pclr->cmap)
1324 opj_jp2_free_pclr(&(jp2->color));
1326 opj_jp2_apply_pclr(p_image, &(jp2->color));
1329 if(jp2->color.icc_profile_buf) {
1330 p_image->icc_profile_buf = jp2->color.icc_profile_buf;
1331 p_image->icc_profile_len = jp2->color.icc_profile_len;
1332 jp2->color.icc_profile_buf = NULL;
1339 OPJ_BOOL opj_jp2_write_jp2h(opj_jp2_t *jp2,
1340 opj_stream_private_t *stream,
1341 opj_event_mgr_t * p_manager
1344 opj_jp2_img_header_writer_handler_t l_writers [3];
1345 opj_jp2_img_header_writer_handler_t * l_current_writer;
1347 OPJ_INT32 i, l_nb_pass;
1348 /* size of data for super box*/
1349 OPJ_UINT32 l_jp2h_size = 8;
1350 OPJ_BOOL l_result = OPJ_TRUE;
1352 /* to store the data of the super box */
1353 OPJ_BYTE l_jp2h_data [8];
1356 assert(stream != 00);
1358 assert(p_manager != 00);
1360 memset(l_writers,0,sizeof(l_writers));
1362 if (jp2->bpc == 255) {
1364 l_writers[0].handler = opj_jp2_write_ihdr;
1365 l_writers[1].handler = opj_jp2_write_bpcc;
1366 l_writers[2].handler = opj_jp2_write_colr;
1370 l_writers[0].handler = opj_jp2_write_ihdr;
1371 l_writers[1].handler = opj_jp2_write_colr;
1374 /* write box header */
1375 /* write JP2H type */
1376 opj_write_bytes(l_jp2h_data+4,JP2_JP2H,4);
1378 l_current_writer = l_writers;
1379 for (i=0;i<l_nb_pass;++i) {
1380 l_current_writer->m_data = l_current_writer->handler(jp2,&(l_current_writer->m_size));
1381 if (l_current_writer->m_data == 00) {
1382 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to hold JP2 Header data\n");
1383 l_result = OPJ_FALSE;
1387 l_jp2h_size += l_current_writer->m_size;
1392 l_current_writer = l_writers;
1393 for (i=0;i<l_nb_pass;++i) {
1394 if (l_current_writer->m_data != 00) {
1395 opj_free(l_current_writer->m_data );
1403 /* write super box size */
1404 opj_write_bytes(l_jp2h_data,l_jp2h_size,4);
1406 /* write super box data on stream */
1407 if (opj_stream_write_data(stream,l_jp2h_data,8,p_manager) != 8) {
1408 opj_event_msg(p_manager, EVT_ERROR, "Stream error while writing JP2 Header box\n");
1409 l_result = OPJ_FALSE;
1413 l_current_writer = l_writers;
1414 for (i=0;i<l_nb_pass;++i) {
1415 if (opj_stream_write_data(stream,l_current_writer->m_data,l_current_writer->m_size,p_manager) != l_current_writer->m_size) {
1416 opj_event_msg(p_manager, EVT_ERROR, "Stream error while writing JP2 Header box\n");
1417 l_result = OPJ_FALSE;
1424 l_current_writer = l_writers;
1427 for (i=0;i<l_nb_pass;++i) {
1428 if (l_current_writer->m_data != 00) {
1429 opj_free(l_current_writer->m_data );
1437 OPJ_BOOL opj_jp2_write_ftyp(opj_jp2_t *jp2,
1438 opj_stream_private_t *cio,
1439 opj_event_mgr_t * p_manager )
1442 OPJ_UINT32 l_ftyp_size = 16 + 4 * jp2->numcl;
1443 OPJ_BYTE * l_ftyp_data, * l_current_data_ptr;
1449 assert(p_manager != 00);
1451 l_ftyp_data = (OPJ_BYTE *) opj_malloc(l_ftyp_size);
1453 if (l_ftyp_data == 00) {
1454 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to handle ftyp data\n");
1458 memset(l_ftyp_data,0,l_ftyp_size);
1460 l_current_data_ptr = l_ftyp_data;
1462 opj_write_bytes(l_current_data_ptr, l_ftyp_size,4); /* box size */
1463 l_current_data_ptr += 4;
1465 opj_write_bytes(l_current_data_ptr, JP2_FTYP,4); /* FTYP */
1466 l_current_data_ptr += 4;
1468 opj_write_bytes(l_current_data_ptr, jp2->brand,4); /* BR */
1469 l_current_data_ptr += 4;
1471 opj_write_bytes(l_current_data_ptr, jp2->minversion,4); /* MinV */
1472 l_current_data_ptr += 4;
1474 for (i = 0; i < jp2->numcl; i++) {
1475 opj_write_bytes(l_current_data_ptr, jp2->cl[i],4); /* CL */
1478 l_result = (opj_stream_write_data(cio,l_ftyp_data,l_ftyp_size,p_manager) == l_ftyp_size);
1481 opj_event_msg(p_manager, EVT_ERROR, "Error while writing ftyp data to stream\n");
1484 opj_free(l_ftyp_data);
1489 OPJ_BOOL opj_jp2_write_jp2c(opj_jp2_t *jp2,
1490 opj_stream_private_t *cio,
1491 opj_event_mgr_t * p_manager )
1493 OPJ_OFF_T j2k_codestream_exit;
1494 OPJ_BYTE l_data_header [8];
1499 assert(p_manager != 00);
1500 assert(opj_stream_has_seek(cio));
1502 j2k_codestream_exit = opj_stream_tell(cio);
1503 opj_write_bytes(l_data_header,
1504 (OPJ_UINT32) (j2k_codestream_exit - jp2->j2k_codestream_offset),
1505 4); /* size of codestream */
1506 opj_write_bytes(l_data_header + 4,JP2_JP2C,4); /* JP2C */
1508 if (! opj_stream_seek(cio,jp2->j2k_codestream_offset,p_manager)) {
1509 opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
1513 if (opj_stream_write_data(cio,l_data_header,8,p_manager) != 8) {
1514 opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
1518 if (! opj_stream_seek(cio,j2k_codestream_exit,p_manager)) {
1519 opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
1526 OPJ_BOOL opj_jp2_write_jp( opj_jp2_t *jp2,
1527 opj_stream_private_t *cio,
1528 opj_event_mgr_t * p_manager )
1530 /* 12 bytes will be read */
1531 OPJ_BYTE l_signature_data [12];
1536 assert(p_manager != 00);
1538 /* write box length */
1539 opj_write_bytes(l_signature_data,12,4);
1540 /* writes box type */
1541 opj_write_bytes(l_signature_data+4,JP2_JP,4);
1542 /* writes magic number*/
1543 opj_write_bytes(l_signature_data+8,0x0d0a870a,4);
1545 if (opj_stream_write_data(cio,l_signature_data,12,p_manager) != 12) {
1552 /* ----------------------------------------------------------------------- */
1553 /* JP2 decoder interface */
1554 /* ----------------------------------------------------------------------- */
1556 void opj_jp2_setup_decoder(opj_jp2_t *jp2, opj_dparameters_t *parameters)
1558 /* setup the J2K codec */
1559 opj_j2k_setup_decoder(jp2->j2k, parameters);
1561 /* further JP2 initializations go here */
1562 jp2->color.jp2_has_colr = 0;
1563 jp2->ignore_pclr_cmap_cdef = parameters->flags & OPJ_DPARAMETERS_IGNORE_PCLR_CMAP_CDEF_FLAG;
1566 /* ----------------------------------------------------------------------- */
1567 /* JP2 encoder interface */
1568 /* ----------------------------------------------------------------------- */
1570 void opj_jp2_setup_encoder( opj_jp2_t *jp2,
1571 opj_cparameters_t *parameters,
1573 opj_event_mgr_t * p_manager)
1579 if(!jp2 || !parameters || !image)
1582 /* setup the J2K codec */
1583 /* ------------------- */
1585 /* Check if number of components respects standard */
1586 if (image->numcomps < 1 || image->numcomps > 16384) {
1587 opj_event_msg(p_manager, EVT_ERROR, "Invalid number of components specified while setting up JP2 encoder\n");
1591 opj_j2k_setup_encoder(jp2->j2k, parameters, image, p_manager );
1593 /* setup the JP2 codec */
1594 /* ------------------- */
1598 jp2->brand = JP2_JP2; /* BR */
1599 jp2->minversion = 0; /* MinV */
1601 jp2->cl = (OPJ_UINT32*) opj_malloc(jp2->numcl * sizeof(OPJ_UINT32));
1604 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory when setup the JP2 encoder\n");
1607 jp2->cl[0] = JP2_JP2; /* CL0 : JP2 */
1609 /* Image Header box */
1611 jp2->numcomps = image->numcomps; /* NC */
1612 jp2->comps = (opj_jp2_comps_t*) opj_malloc(jp2->numcomps * sizeof(opj_jp2_comps_t));
1615 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory when setup the JP2 encoder\n");
1619 jp2->h = image->y1 - image->y0; /* HEIGHT */
1620 jp2->w = image->x1 - image->x0; /* WIDTH */
1622 depth_0 = image->comps[0].prec - 1;
1623 sign = image->comps[0].sgnd;
1624 jp2->bpc = depth_0 + (sign << 7);
1625 for (i = 1; i < image->numcomps; i++) {
1626 OPJ_UINT32 depth = image->comps[i].prec - 1;
1627 sign = image->comps[i].sgnd;
1628 if (depth_0 != depth)
1631 jp2->C = 7; /* C : Always 7 */
1632 jp2->UnkC = 0; /* UnkC, colorspace specified in colr box */
1633 jp2->IPR = 0; /* IPR, no intellectual property */
1635 /* BitsPerComponent box */
1636 for (i = 0; i < image->numcomps; i++) {
1637 jp2->comps[i].bpcc = image->comps[i].prec - 1 + (image->comps[i].sgnd << 7);
1640 /* Colour Specification box */
1641 if(image->icc_profile_len) {
1647 if (image->color_space == 1)
1648 jp2->enumcs = 16; /* sRGB as defined by IEC 61966-2-1 */
1649 else if (image->color_space == 2)
1650 jp2->enumcs = 17; /* greyscale */
1651 else if (image->color_space == 3)
1652 jp2->enumcs = 18; /* YUV */
1656 jp2->precedence = 0; /* PRECEDENCE */
1657 jp2->approx = 0; /* APPROX */
1659 jp2->jpip_on = parameters->jpip_on;
1662 OPJ_BOOL opj_jp2_encode(opj_jp2_t *jp2,
1663 opj_stream_private_t *stream,
1664 opj_event_mgr_t * p_manager)
1666 return opj_j2k_encode(jp2->j2k, stream, p_manager);
1669 OPJ_BOOL opj_jp2_end_decompress(opj_jp2_t *jp2,
1670 opj_stream_private_t *cio,
1671 opj_event_mgr_t * p_manager
1677 assert(p_manager != 00);
1679 /* customization of the end encoding */
1680 opj_jp2_setup_end_header_reading(jp2);
1683 if (! opj_jp2_exec (jp2,jp2->m_procedure_list,cio,p_manager)) {
1687 return opj_j2k_end_decompress(jp2->j2k, cio, p_manager);
1690 OPJ_BOOL opj_jp2_end_compress( opj_jp2_t *jp2,
1691 opj_stream_private_t *cio,
1692 opj_event_mgr_t * p_manager
1698 assert(p_manager != 00);
1700 /* customization of the end encoding */
1701 opj_jp2_setup_end_header_writing(jp2);
1703 if (! opj_j2k_end_compress(jp2->j2k,cio,p_manager)) {
1708 return opj_jp2_exec(jp2,jp2->m_procedure_list,cio,p_manager);
1711 void opj_jp2_setup_end_header_writing (opj_jp2_t *jp2)
1718 opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)opj_jpip_write_iptr );
1720 opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)opj_jp2_write_jp2c );
1721 /* DEVELOPER CORNER, add your custom procedures */
1725 opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)opj_jpip_write_cidx );
1726 opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)opj_jpip_write_fidx );
1731 void opj_jp2_setup_end_header_reading (opj_jp2_t *jp2)
1735 opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)opj_jp2_read_header_procedure );
1736 /* DEVELOPER CORNER, add your custom procedures */
1739 OPJ_BOOL opj_jp2_default_validation ( opj_jp2_t * jp2,
1740 opj_stream_private_t *cio,
1741 opj_event_mgr_t * p_manager
1744 OPJ_BOOL l_is_valid = OPJ_TRUE;
1750 assert(p_manager != 00);
1752 /* JPEG2000 codec validation */
1754 /* STATE checking */
1755 /* make sure the state is at 0 */
1756 l_is_valid &= (jp2->jp2_state == JP2_STATE_NONE);
1758 /* make sure not reading a jp2h ???? WEIRD */
1759 l_is_valid &= (jp2->jp2_img_state == JP2_IMG_STATE_NONE);
1761 /* POINTER validation */
1762 /* make sure a j2k codec is present */
1763 l_is_valid &= (jp2->j2k != 00);
1765 /* make sure a procedure list is present */
1766 l_is_valid &= (jp2->m_procedure_list != 00);
1768 /* make sure a validation list is present */
1769 l_is_valid &= (jp2->m_validation_list != 00);
1771 /* PARAMETER VALIDATION */
1772 /* number of components */
1773 l_is_valid &= (jp2->numcl > 0);
1775 l_is_valid &= (jp2->h > 0);
1777 l_is_valid &= (jp2->w > 0);
1779 for (i = 0; i < jp2->numcomps; ++i) {
1780 l_is_valid &= (jp2->comps[i].bpcc > 0);
1784 l_is_valid &= ((jp2->meth > 0) && (jp2->meth < 3));
1786 /* stream validation */
1787 /* back and forth is needed */
1788 l_is_valid &= opj_stream_has_seek(cio);
1793 OPJ_BOOL opj_jp2_read_header_procedure( opj_jp2_t *jp2,
1794 opj_stream_private_t *stream,
1795 opj_event_mgr_t * p_manager
1799 OPJ_UINT32 l_nb_bytes_read;
1800 const opj_jp2_header_handler_t * l_current_handler;
1801 OPJ_UINT32 l_last_data_size = OPJ_BOX_SIZE;
1802 OPJ_UINT32 l_current_data_size;
1803 OPJ_BYTE * l_current_data = 00;
1806 assert(stream != 00);
1808 assert(p_manager != 00);
1810 l_current_data = (OPJ_BYTE*)opj_malloc(l_last_data_size);
1812 if (l_current_data == 00) {
1813 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to handle jpeg2000 file header\n");
1816 memset(l_current_data, 0 , l_last_data_size);
1818 while (opj_jp2_read_boxhdr(&box,&l_nb_bytes_read,stream,p_manager)) {
1819 /* is it the codestream box ? */
1820 if (box.type == JP2_JP2C) {
1821 if (jp2->jp2_state & JP2_STATE_HEADER) {
1822 jp2->jp2_state |= JP2_STATE_CODESTREAM;
1823 opj_free(l_current_data);
1827 opj_event_msg(p_manager, EVT_ERROR, "bad placed jpeg codestream\n");
1828 opj_free(l_current_data);
1832 else if (box.length == 0) {
1833 opj_event_msg(p_manager, EVT_ERROR, "Cannot handle box of undefined sizes\n");
1834 opj_free(l_current_data);
1837 /* testcase 1851.pdf.SIGSEGV.ce9.948 */
1838 else if (box.length < l_nb_bytes_read) {
1839 opj_event_msg(p_manager, EVT_ERROR, "invalid box size %d (%x)\n", box.length, box.type);
1840 opj_free(l_current_data);
1844 l_current_handler = opj_jp2_find_handler(box.type);
1845 l_current_data_size = box.length - l_nb_bytes_read;
1847 if (l_current_handler != 00) {
1848 if (l_current_data_size > l_last_data_size) {
1849 OPJ_BYTE* new_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,l_current_data_size);
1850 if (!new_current_data) {
1851 opj_free(l_current_data);
1852 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to handle jpeg2000 box\n");
1855 l_current_data = new_current_data;
1856 l_last_data_size = l_current_data_size;
1859 l_nb_bytes_read = (OPJ_UINT32)opj_stream_read_data(stream,l_current_data,l_current_data_size,p_manager);
1860 if (l_nb_bytes_read != l_current_data_size) {
1861 opj_event_msg(p_manager, EVT_ERROR, "Problem with reading JPEG2000 box, stream error\n");
1862 opj_free(l_current_data);
1866 if (! l_current_handler->handler(jp2,l_current_data,l_current_data_size,p_manager)) {
1867 opj_free(l_current_data);
1872 jp2->jp2_state |= JP2_STATE_UNKNOWN;
1873 if (opj_stream_skip(stream,l_current_data_size,p_manager) != l_current_data_size) {
1874 opj_event_msg(p_manager, EVT_ERROR, "Problem with skipping JPEG2000 box, stream error\n");
1875 opj_free(l_current_data);
1881 opj_free(l_current_data);
1887 * Excutes the given procedures on the given codec.
1889 * @param p_procedure_list the list of procedures to execute
1890 * @param jp2 the jpeg2000 file codec to execute the procedures on.
1891 * @param stream the stream to execute the procedures on.
1892 * @param p_manager the user manager.
1894 * @return true if all the procedures were successfully executed.
1896 static OPJ_BOOL opj_jp2_exec ( opj_jp2_t * jp2,
1897 opj_procedure_list_t * p_procedure_list,
1898 opj_stream_private_t *stream,
1899 opj_event_mgr_t * p_manager
1903 OPJ_BOOL (** l_procedure) (opj_jp2_t * jp2, opj_stream_private_t *, opj_event_mgr_t *) = 00;
1904 OPJ_BOOL l_result = OPJ_TRUE;
1905 OPJ_UINT32 l_nb_proc, i;
1908 assert(p_procedure_list != 00);
1910 assert(stream != 00);
1911 assert(p_manager != 00);
1913 l_nb_proc = opj_procedure_list_get_nb_procedures(p_procedure_list);
1914 l_procedure = (OPJ_BOOL (**) (opj_jp2_t * jp2, opj_stream_private_t *, opj_event_mgr_t *)) opj_procedure_list_get_first_procedure(p_procedure_list);
1916 for (i=0;i<l_nb_proc;++i) {
1917 l_result = l_result && (*l_procedure) (jp2,stream,p_manager);
1921 /* and clear the procedure list at the end. */
1922 opj_procedure_list_clear(p_procedure_list);
1926 OPJ_BOOL opj_jp2_start_compress(opj_jp2_t *jp2,
1927 opj_stream_private_t *stream,
1928 opj_image_t * p_image,
1929 opj_event_mgr_t * p_manager
1934 assert(stream != 00);
1935 assert(p_manager != 00);
1937 /* customization of the validation */
1938 opj_jp2_setup_encoding_validation (jp2);
1940 /* validation of the parameters codec */
1941 if (! opj_jp2_exec(jp2,jp2->m_validation_list,stream,p_manager)) {
1945 /* customization of the encoding */
1946 opj_jp2_setup_header_writing(jp2);
1949 if (! opj_jp2_exec (jp2,jp2->m_procedure_list,stream,p_manager)) {
1953 return opj_j2k_start_compress(jp2->j2k,stream,p_image,p_manager);
1956 const opj_jp2_header_handler_t * opj_jp2_find_handler (OPJ_UINT32 p_id)
1958 OPJ_UINT32 i, l_handler_size = sizeof(jp2_header) / sizeof(opj_jp2_header_handler_t);
1960 for (i=0;i<l_handler_size;++i) {
1961 if (jp2_header[i].id == p_id) {
1962 return &jp2_header[i];
1969 * Finds the image execution function related to the given box id.
1971 * @param p_id the id of the handler to fetch.
1973 * @return the given handler or 00 if it could not be found.
1975 static const opj_jp2_header_handler_t * opj_jp2_img_find_handler (OPJ_UINT32 p_id)
1977 OPJ_UINT32 i, l_handler_size = sizeof(jp2_img_header) / sizeof(opj_jp2_header_handler_t);
1978 for (i=0;i<l_handler_size;++i)
1980 if (jp2_img_header[i].id == p_id) {
1981 return &jp2_img_header[i];
1989 * Reads a jpeg2000 file signature box.
1991 * @param p_header_data the data contained in the signature box.
1992 * @param jp2 the jpeg2000 file codec.
1993 * @param p_header_size the size of the data contained in the signature box.
1994 * @param p_manager the user event manager.
1996 * @return true if the file signature box is valid.
1998 static OPJ_BOOL opj_jp2_read_jp(opj_jp2_t *jp2,
1999 OPJ_BYTE * p_header_data,
2000 OPJ_UINT32 p_header_size,
2001 opj_event_mgr_t * p_manager
2005 OPJ_UINT32 l_magic_number;
2008 assert(p_header_data != 00);
2010 assert(p_manager != 00);
2012 if (jp2->jp2_state != JP2_STATE_NONE) {
2013 opj_event_msg(p_manager, EVT_ERROR, "The signature box must be the first box in the file.\n");
2017 /* assure length of data is correct (4 -> magic number) */
2018 if (p_header_size != 4) {
2019 opj_event_msg(p_manager, EVT_ERROR, "Error with JP signature Box size\n");
2023 /* rearrange data */
2024 opj_read_bytes(p_header_data,&l_magic_number,4);
2025 if (l_magic_number != 0x0d0a870a ) {
2026 opj_event_msg(p_manager, EVT_ERROR, "Error with JP Signature : bad magic number\n");
2030 jp2->jp2_state |= JP2_STATE_SIGNATURE;
2036 * Reads a a FTYP box - File type box
2038 * @param p_header_data the data contained in the FTYP box.
2039 * @param jp2 the jpeg2000 file codec.
2040 * @param p_header_size the size of the data contained in the FTYP box.
2041 * @param p_manager the user event manager.
2043 * @return true if the FTYP box is valid.
2045 static OPJ_BOOL opj_jp2_read_ftyp( opj_jp2_t *jp2,
2046 OPJ_BYTE * p_header_data,
2047 OPJ_UINT32 p_header_size,
2048 opj_event_mgr_t * p_manager
2051 OPJ_UINT32 i, l_remaining_bytes;
2054 assert(p_header_data != 00);
2056 assert(p_manager != 00);
2058 if (jp2->jp2_state != JP2_STATE_SIGNATURE) {
2059 opj_event_msg(p_manager, EVT_ERROR, "The ftyp box must be the second box in the file.\n");
2063 /* assure length of data is correct */
2064 if (p_header_size < 8) {
2065 opj_event_msg(p_manager, EVT_ERROR, "Error with FTYP signature Box size\n");
2069 opj_read_bytes(p_header_data,&jp2->brand,4); /* BR */
2072 opj_read_bytes(p_header_data,&jp2->minversion,4); /* MinV */
2075 l_remaining_bytes = p_header_size - 8;
2077 /* the number of remaining bytes should be a multiple of 4 */
2078 if ((l_remaining_bytes & 0x3) != 0) {
2079 opj_event_msg(p_manager, EVT_ERROR, "Error with FTYP signature Box size\n");
2084 jp2->numcl = l_remaining_bytes >> 2;
2086 jp2->cl = (OPJ_UINT32 *) opj_malloc(jp2->numcl * sizeof(OPJ_UINT32));
2087 if (jp2->cl == 00) {
2088 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory with FTYP Box\n");
2091 memset(jp2->cl,0,jp2->numcl * sizeof(OPJ_UINT32));
2094 for (i = 0; i < jp2->numcl; ++i)
2096 opj_read_bytes(p_header_data,&jp2->cl[i],4); /* CLi */
2100 jp2->jp2_state |= JP2_STATE_FILE_TYPE;
2105 OPJ_BOOL opj_jp2_skip_jp2c( opj_jp2_t *jp2,
2106 opj_stream_private_t *stream,
2107 opj_event_mgr_t * p_manager )
2111 assert(stream != 00);
2112 assert(p_manager != 00);
2114 jp2->j2k_codestream_offset = opj_stream_tell(stream);
2116 if (opj_stream_skip(stream,8,p_manager) != 8) {
2123 static OPJ_BOOL opj_jpip_skip_iptr( opj_jp2_t *jp2,
2124 opj_stream_private_t *stream,
2125 opj_event_mgr_t * p_manager )
2129 assert(stream != 00);
2130 assert(p_manager != 00);
2132 jp2->jpip_iptr_offset = opj_stream_tell(stream);
2134 if (opj_stream_skip(stream,24,p_manager) != 24) {
2142 * Reads the Jpeg2000 file Header box - JP2 Header box (warning, this is a super box).
2144 * @param p_header_data the data contained in the file header box.
2145 * @param jp2 the jpeg2000 file codec.
2146 * @param p_header_size the size of the data contained in the file header box.
2147 * @param p_manager the user event manager.
2149 * @return true if the JP2 Header box was successfully reconized.
2151 static OPJ_BOOL opj_jp2_read_jp2h( opj_jp2_t *jp2,
2152 OPJ_BYTE *p_header_data,
2153 OPJ_UINT32 p_header_size,
2154 opj_event_mgr_t * p_manager
2157 OPJ_UINT32 l_box_size=0, l_current_data_size = 0;
2159 const opj_jp2_header_handler_t * l_current_handler;
2162 assert(p_header_data != 00);
2164 assert(p_manager != 00);
2166 /* make sure the box is well placed */
2167 if ((jp2->jp2_state & JP2_STATE_FILE_TYPE) != JP2_STATE_FILE_TYPE ) {
2168 opj_event_msg(p_manager, EVT_ERROR, "The box must be the first box in the file.\n");
2172 jp2->jp2_img_state = JP2_IMG_STATE_NONE;
2174 /* iterate while remaining data */
2175 while (p_header_size > 0) {
2177 if (! opj_jp2_read_boxhdr_char(&box,p_header_data,&l_box_size,p_header_size, p_manager)) {
2178 opj_event_msg(p_manager, EVT_ERROR, "Stream error while reading JP2 Header box\n");
2182 if (box.length > p_header_size) {
2183 opj_event_msg(p_manager, EVT_ERROR, "Stream error while reading JP2 Header box: box length is inconsistent.\n");
2187 l_current_handler = opj_jp2_img_find_handler(box.type);
2188 l_current_data_size = box.length - l_box_size;
2189 p_header_data += l_box_size;
2191 if (l_current_handler != 00) {
2192 if (! l_current_handler->handler(jp2,p_header_data,l_current_data_size,p_manager)) {
2197 jp2->jp2_img_state |= JP2_IMG_STATE_UNKNOWN;
2200 p_header_data += l_current_data_size;
2201 p_header_size -= box.length;
2204 jp2->jp2_state |= JP2_STATE_HEADER;
2209 OPJ_BOOL opj_jp2_read_boxhdr_char( opj_jp2_box_t *box,
2211 OPJ_UINT32 * p_number_bytes_read,
2212 OPJ_UINT32 p_box_max_size,
2213 opj_event_mgr_t * p_manager
2219 assert(p_data != 00);
2221 assert(p_number_bytes_read != 00);
2222 assert(p_manager != 00);
2224 if (p_box_max_size < 8) {
2225 opj_event_msg(p_manager, EVT_ERROR, "Cannot handle box of less than 8 bytes\n");
2229 /* process read data */
2230 opj_read_bytes(p_data, &l_value, 4);
2232 box->length = (OPJ_UINT32)(l_value);
2234 opj_read_bytes(p_data, &l_value, 4);
2236 box->type = (OPJ_UINT32)(l_value);
2238 *p_number_bytes_read = 8;
2240 /* do we have a "special very large box ?" */
2241 /* read then the XLBox */
2242 if (box->length == 1) {
2243 OPJ_UINT32 l_xl_part_size;
2245 if (p_box_max_size < 16) {
2246 opj_event_msg(p_manager, EVT_ERROR, "Cannot handle XL box of less than 16 bytes\n");
2250 opj_read_bytes(p_data,&l_xl_part_size, 4);
2252 *p_number_bytes_read += 4;
2254 if (l_xl_part_size != 0) {
2255 opj_event_msg(p_manager, EVT_ERROR, "Cannot handle box sizes higher than 2^32\n");
2259 opj_read_bytes(p_data, &l_value, 4);
2260 *p_number_bytes_read += 4;
2261 box->length = (OPJ_UINT32)(l_value);
2263 if (box->length == 0) {
2264 opj_event_msg(p_manager, EVT_ERROR, "Cannot handle box of undefined sizes\n");
2268 else if (box->length == 0) {
2269 opj_event_msg(p_manager, EVT_ERROR, "Cannot handle box of undefined sizes\n");
2276 OPJ_BOOL opj_jp2_read_header( opj_stream_private_t *p_stream,
2278 opj_image_t ** p_image,
2279 opj_event_mgr_t * p_manager
2284 assert(p_stream != 00);
2285 assert(p_manager != 00);
2287 /* customization of the validation */
2288 opj_jp2_setup_decoding_validation (jp2);
2290 /* customization of the encoding */
2291 opj_jp2_setup_header_reading(jp2);
2293 /* validation of the parameters codec */
2294 if (! opj_jp2_exec(jp2,jp2->m_validation_list,p_stream,p_manager)) {
2299 if (! opj_jp2_exec (jp2,jp2->m_procedure_list,p_stream,p_manager)) {
2303 return opj_j2k_read_header( p_stream,
2309 void opj_jp2_setup_encoding_validation (opj_jp2_t *jp2)
2314 opj_procedure_list_add_procedure(jp2->m_validation_list, (opj_procedure)opj_jp2_default_validation);
2315 /* DEVELOPER CORNER, add your custom validation procedure */
2318 void opj_jp2_setup_decoding_validation (opj_jp2_t *jp2)
2322 /* DEVELOPER CORNER, add your custom validation procedure */
2325 void opj_jp2_setup_header_writing (opj_jp2_t *jp2)
2330 opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)opj_jp2_write_jp );
2331 opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)opj_jp2_write_ftyp );
2332 opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)opj_jp2_write_jp2h );
2334 opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)opj_jpip_skip_iptr );
2335 opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)opj_jp2_skip_jp2c );
2337 /* DEVELOPER CORNER, insert your custom procedures */
2341 void opj_jp2_setup_header_reading (opj_jp2_t *jp2)
2346 opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)opj_jp2_read_header_procedure );
2347 /* DEVELOPER CORNER, add your custom procedures */
2350 OPJ_BOOL opj_jp2_read_tile_header ( opj_jp2_t * p_jp2,
2351 OPJ_UINT32 * p_tile_index,
2352 OPJ_UINT32 * p_data_size,
2353 OPJ_INT32 * p_tile_x0,
2354 OPJ_INT32 * p_tile_y0,
2355 OPJ_INT32 * p_tile_x1,
2356 OPJ_INT32 * p_tile_y1,
2357 OPJ_UINT32 * p_nb_comps,
2359 opj_stream_private_t *p_stream,
2360 opj_event_mgr_t * p_manager
2363 return opj_j2k_read_tile_header(p_jp2->j2k,
2366 p_tile_x0, p_tile_y0,
2367 p_tile_x1, p_tile_y1,
2374 OPJ_BOOL opj_jp2_write_tile ( opj_jp2_t *p_jp2,
2375 OPJ_UINT32 p_tile_index,
2377 OPJ_UINT32 p_data_size,
2378 opj_stream_private_t *p_stream,
2379 opj_event_mgr_t * p_manager
2383 return opj_j2k_write_tile (p_jp2->j2k,p_tile_index,p_data,p_data_size,p_stream,p_manager);
2386 OPJ_BOOL opj_jp2_decode_tile ( opj_jp2_t * p_jp2,
2387 OPJ_UINT32 p_tile_index,
2389 OPJ_UINT32 p_data_size,
2390 opj_stream_private_t *p_stream,
2391 opj_event_mgr_t * p_manager
2394 return opj_j2k_decode_tile (p_jp2->j2k,p_tile_index,p_data,p_data_size,p_stream,p_manager);
2397 void opj_jp2_destroy(opj_jp2_t *jp2)
2400 /* destroy the J2K codec */
2401 opj_j2k_destroy(jp2->j2k);
2405 opj_free(jp2->comps);
2414 if (jp2->color.icc_profile_buf) {
2415 opj_free(jp2->color.icc_profile_buf);
2416 jp2->color.icc_profile_buf = 00;
2419 if (jp2->color.jp2_cdef) {
2420 if (jp2->color.jp2_cdef->info) {
2421 opj_free(jp2->color.jp2_cdef->info);
2422 jp2->color.jp2_cdef->info = NULL;
2425 opj_free(jp2->color.jp2_cdef);
2426 jp2->color.jp2_cdef = 00;
2429 if (jp2->color.jp2_pclr) {
2430 if (jp2->color.jp2_pclr->cmap) {
2431 opj_free(jp2->color.jp2_pclr->cmap);
2432 jp2->color.jp2_pclr->cmap = NULL;
2434 if (jp2->color.jp2_pclr->channel_sign) {
2435 opj_free(jp2->color.jp2_pclr->channel_sign);
2436 jp2->color.jp2_pclr->channel_sign = NULL;
2438 if (jp2->color.jp2_pclr->channel_size) {
2439 opj_free(jp2->color.jp2_pclr->channel_size);
2440 jp2->color.jp2_pclr->channel_size = NULL;
2442 if (jp2->color.jp2_pclr->entries) {
2443 opj_free(jp2->color.jp2_pclr->entries);
2444 jp2->color.jp2_pclr->entries = NULL;
2447 opj_free(jp2->color.jp2_pclr);
2448 jp2->color.jp2_pclr = 00;
2451 if (jp2->m_validation_list) {
2452 opj_procedure_list_destroy(jp2->m_validation_list);
2453 jp2->m_validation_list = 00;
2456 if (jp2->m_procedure_list) {
2457 opj_procedure_list_destroy(jp2->m_procedure_list);
2458 jp2->m_procedure_list = 00;
2465 OPJ_BOOL opj_jp2_set_decode_area( opj_jp2_t *p_jp2,
2466 opj_image_t* p_image,
2467 OPJ_INT32 p_start_x, OPJ_INT32 p_start_y,
2468 OPJ_INT32 p_end_x, OPJ_INT32 p_end_y,
2469 opj_event_mgr_t * p_manager
2472 return opj_j2k_set_decode_area(p_jp2->j2k, p_image, p_start_x, p_start_y, p_end_x, p_end_y, p_manager);
2475 OPJ_BOOL opj_jp2_get_tile( opj_jp2_t *p_jp2,
2476 opj_stream_private_t *p_stream,
2477 opj_image_t* p_image,
2478 opj_event_mgr_t * p_manager,
2479 OPJ_UINT32 tile_index
2485 opj_event_msg(p_manager, EVT_WARNING, "JP2 box which are after the codestream will not be read by this function.\n");
2487 if (! opj_j2k_get_tile(p_jp2->j2k, p_stream, p_image, p_manager, tile_index) ){
2488 opj_event_msg(p_manager, EVT_ERROR, "Failed to decode the codestream in the JP2 file\n");
2492 if (!opj_jp2_check_color(p_image, &(p_jp2->color), p_manager)) {
2496 /* Set Image Color Space */
2497 if (p_jp2->enumcs == 16)
2498 p_image->color_space = OPJ_CLRSPC_SRGB;
2499 else if (p_jp2->enumcs == 17)
2500 p_image->color_space = OPJ_CLRSPC_GRAY;
2501 else if (p_jp2->enumcs == 18)
2502 p_image->color_space = OPJ_CLRSPC_SYCC;
2504 p_image->color_space = OPJ_CLRSPC_UNKNOWN;
2506 /* Apply the color space if needed */
2507 if(p_jp2->color.jp2_cdef) {
2508 opj_jp2_apply_cdef(p_image, &(p_jp2->color));
2511 if(p_jp2->color.jp2_pclr) {
2512 /* Part 1, I.5.3.4: Either both or none : */
2513 if( !p_jp2->color.jp2_pclr->cmap)
2514 opj_jp2_free_pclr(&(p_jp2->color));
2516 opj_jp2_apply_pclr(p_image, &(p_jp2->color));
2519 if(p_jp2->color.icc_profile_buf) {
2520 p_image->icc_profile_buf = p_jp2->color.icc_profile_buf;
2521 p_image->icc_profile_len = p_jp2->color.icc_profile_len;
2522 p_jp2->color.icc_profile_buf = NULL;
2528 /* ----------------------------------------------------------------------- */
2529 /* JP2 encoder interface */
2530 /* ----------------------------------------------------------------------- */
2532 opj_jp2_t* opj_jp2_create(OPJ_BOOL p_is_decoder)
2534 opj_jp2_t *jp2 = (opj_jp2_t*)opj_malloc(sizeof(opj_jp2_t));
2536 memset(jp2,0,sizeof(opj_jp2_t));
2538 /* create the J2K codec */
2539 if (! p_is_decoder) {
2540 jp2->j2k = opj_j2k_create_compress();
2543 jp2->j2k = opj_j2k_create_decompress();
2546 if (jp2->j2k == 00) {
2547 opj_jp2_destroy(jp2);
2551 /* Color structure */
2552 jp2->color.icc_profile_buf = NULL;
2553 jp2->color.icc_profile_len = 0;
2554 jp2->color.jp2_cdef = NULL;
2555 jp2->color.jp2_pclr = NULL;
2556 jp2->color.jp2_has_colr = 0;
2558 /* validation list creation */
2559 jp2->m_validation_list = opj_procedure_list_create();
2560 if (! jp2->m_validation_list) {
2561 opj_jp2_destroy(jp2);
2565 /* execution list creation */
2566 jp2->m_procedure_list = opj_procedure_list_create();
2567 if (! jp2->m_procedure_list) {
2568 opj_jp2_destroy(jp2);
2576 void jp2_dump(opj_jp2_t* p_jp2, OPJ_INT32 flag, FILE* out_stream)
2579 assert(p_jp2 != 00);
2581 j2k_dump(p_jp2->j2k,
2586 opj_codestream_index_t* jp2_get_cstr_index(opj_jp2_t* p_jp2)
2588 return j2k_get_cstr_index(p_jp2->j2k);
2591 opj_codestream_info_v2_t* jp2_get_cstr_info(opj_jp2_t* p_jp2)
2593 return j2k_get_cstr_info(p_jp2->j2k);
2596 OPJ_BOOL opj_jp2_set_decoded_resolution_factor(opj_jp2_t *p_jp2,
2597 OPJ_UINT32 res_factor,
2598 opj_event_mgr_t * p_manager)
2600 return opj_j2k_set_decoded_resolution_factor(p_jp2->j2k, res_factor, p_manager);
2606 static OPJ_BOOL opj_jpip_write_iptr(opj_jp2_t *jp2,
2607 opj_stream_private_t *cio,
2608 opj_event_mgr_t * p_manager )
2610 OPJ_OFF_T j2k_codestream_exit;
2611 OPJ_BYTE l_data_header [24];
2616 assert(p_manager != 00);
2617 assert(opj_stream_has_seek(cio));
2619 j2k_codestream_exit = opj_stream_tell(cio);
2620 opj_write_bytes(l_data_header, 24, 4); /* size of iptr */
2621 opj_write_bytes(l_data_header + 4,JPIP_IPTR,4); /* IPTR */
2623 opj_write_bytes(l_data_header + 4 + 4, 0, 8); /* offset */
2624 opj_write_bytes(l_data_header + 8 + 8, 0, 8); /* length */
2626 opj_write_double(l_data_header + 4 + 4, 0); /* offset */
2627 opj_write_double(l_data_header + 8 + 8, 0); /* length */
2630 if (! opj_stream_seek(cio,jp2->jpip_iptr_offset,p_manager)) {
2631 opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
2635 if (opj_stream_write_data(cio,l_data_header,24,p_manager) != 24) {
2636 opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
2640 if (! opj_stream_seek(cio,j2k_codestream_exit,p_manager)) {
2641 opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
2648 static OPJ_BOOL opj_jpip_write_fidx(opj_jp2_t *jp2,
2649 opj_stream_private_t *cio,
2650 opj_event_mgr_t * p_manager )
2652 OPJ_OFF_T j2k_codestream_exit;
2653 OPJ_BYTE l_data_header [24];
2658 assert(p_manager != 00);
2659 assert(opj_stream_has_seek(cio));
2661 opj_write_bytes(l_data_header, 24, 4); /* size of iptr */
2662 opj_write_bytes(l_data_header + 4,JPIP_FIDX,4); /* IPTR */
2663 opj_write_double(l_data_header + 4 + 4, 0); /* offset */
2664 opj_write_double(l_data_header + 8 + 8, 0); /* length */
2666 if (opj_stream_write_data(cio,l_data_header,24,p_manager) != 24) {
2667 opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
2671 j2k_codestream_exit = opj_stream_tell(cio);
2672 if (! opj_stream_seek(cio,j2k_codestream_exit,p_manager)) {
2673 opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
2680 static OPJ_BOOL opj_jpip_write_cidx(opj_jp2_t *jp2,
2681 opj_stream_private_t *cio,
2682 opj_event_mgr_t * p_manager )
2684 OPJ_OFF_T j2k_codestream_exit;
2685 OPJ_BYTE l_data_header [24];
2690 assert(p_manager != 00);
2691 assert(opj_stream_has_seek(cio));
2693 j2k_codestream_exit = opj_stream_tell(cio);
2694 opj_write_bytes(l_data_header, 24, 4); /* size of iptr */
2695 opj_write_bytes(l_data_header + 4,JPIP_CIDX,4); /* IPTR */
2697 opj_write_bytes(l_data_header + 4 + 4, 0, 8); /* offset */
2698 opj_write_bytes(l_data_header + 8 + 8, 0, 8); /* length */
2700 opj_write_double(l_data_header + 4 + 4, 0); /* offset */
2701 opj_write_double(l_data_header + 8 + 8, 0); /* length */
2704 if (! opj_stream_seek(cio,j2k_codestream_exit,p_manager)) {
2705 opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
2709 if (opj_stream_write_data(cio,l_data_header,24,p_manager) != 24) {
2710 opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
2714 j2k_codestream_exit = opj_stream_tell(cio);
2715 if (! opj_stream_seek(cio,j2k_codestream_exit,p_manager)) {
2716 opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
2724 static void write_prxy( int offset_jp2c, int length_jp2c, int offset_idx, int length_idx, opj_stream_private_t *cio,
2725 opj_event_mgr_t * p_manager )
2727 OPJ_BYTE l_data_header [8];
2728 OPJ_OFF_T len, lenp;
2730 lenp = opj_stream_tell(cio);
2731 opj_stream_skip(cio, 4, p_manager); /* L [at the end] */
2732 opj_write_bytes(l_data_header,JPIP_PRXY,4); /* IPTR */
2733 opj_stream_write_data(cio,l_data_header,4,p_manager);
2735 opj_write_bytes( l_data_header, offset_jp2c, 8); /* OOFF */
2736 opj_stream_write_data(cio,l_data_header,8,p_manager);
2737 opj_write_bytes( l_data_header, length_jp2c, 4); /* OBH part 1 */
2738 opj_write_bytes( l_data_header+4, JP2_JP2C, 4); /* OBH part 2 */
2739 opj_stream_write_data(cio,l_data_header,8,p_manager);
2741 opj_write_bytes( l_data_header, 1, 1);/* NI */
2742 opj_stream_write_data(cio,l_data_header,1,p_manager);
2744 opj_write_bytes( l_data_header, offset_idx, 8); /* IOFF */
2745 opj_stream_write_data(cio,l_data_header,8,p_manager);
2746 opj_write_bytes( l_data_header, length_idx, 4); /* IBH part 1 */
2747 opj_write_bytes( l_data_header+4, JPIP_CIDX, 4); /* IBH part 2 */
2748 opj_stream_write_data(cio,l_data_header,8,p_manager);
2750 len = opj_stream_tell(cio)-lenp;
2751 opj_stream_skip(cio, lenp, p_manager);
2752 opj_write_bytes(l_data_header,len,4);/* L */
2753 opj_stream_write_data(cio,l_data_header,4,p_manager);
2754 opj_stream_seek(cio, lenp+len,p_manager);
2760 static int write_fidx( int offset_jp2c, int length_jp2c, int offset_idx, int length_idx, opj_stream_private_t *cio,
2761 opj_event_mgr_t * p_manager )
2763 OPJ_BYTE l_data_header [4];
2764 OPJ_OFF_T len, lenp;
2766 lenp = opj_stream_tell(cio);
2767 opj_stream_skip(cio, 4, p_manager);
2768 opj_write_bytes(l_data_header,JPIP_FIDX,4); /* FIDX */
2769 opj_stream_write_data(cio,l_data_header,4,p_manager);
2771 write_prxy( offset_jp2c, length_jp2c, offset_idx, length_idx, cio,p_manager);
2773 len = opj_stream_tell(cio)-lenp;
2774 opj_stream_skip(cio, lenp, p_manager);
2775 opj_write_bytes(l_data_header,len,4);/* L */
2776 opj_stream_write_data(cio,l_data_header,4,p_manager);
2777 opj_stream_seek(cio, lenp+len,p_manager);
2782 #endif /* USE_JPIP */