2 * Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium
3 * Copyright (c) 2002-2007, Professor Benoit Macq
4 * Copyright (c) 2001-2003, David Janssens
5 * Copyright (c) 2002-2003, Yannick Verschueren
6 * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe
7 * Copyright (c) 2005, Herve Drolon, FreeImage Team
8 * Copyright (c) 2010-2011, Kaori Hagihara
9 * Copyright (c) 2008;2011-2012, Centre National d'Etudes Spatiales (CNES), France
10 * Copyright (c) 2012, CS Systemes d'Information, France
11 * All rights reserved.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
23 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
34 #include "opj_includes.h"
36 /** @defgroup JP2 JP2 - JPEG-2000 file format reader/writer */
39 #define OPJ_BOX_SIZE 1024
41 /** @name Local static functions */
44 /*static void jp2_write_url(opj_cio_t *cio, char *Idx_file);*/
47 * Reads a IHDR box - Image Header box
49 * @param p_image_header_data pointer to actual data (already read from file)
50 * @param jp2 the jpeg2000 file codec.
51 * @param p_image_header_size the size of the image header
52 * @param p_manager the user event manager.
54 * @return true if the image header is valid, false else.
56 static OPJ_BOOL opj_jp2_read_ihdr( opj_jp2_t *jp2,
57 OPJ_BYTE *p_image_header_data,
58 OPJ_UINT32 p_image_header_size,
59 opj_event_mgr_t * p_manager );
62 * Writes the Image Header box - Image Header box.
64 * @param jp2 jpeg2000 file codec.
65 * @param p_nb_bytes_written pointer to store the nb of bytes written by the function.
67 * @return the data being copied.
69 static OPJ_BYTE * opj_jp2_write_ihdr(opj_jp2_t *jp2,
70 OPJ_UINT32 * p_nb_bytes_written );
73 * Writes the Bit per Component box.
75 * @param jp2 jpeg2000 file codec.
76 * @param p_nb_bytes_written pointer to store the nb of bytes written by the function.
78 * @return the data being copied.
80 static OPJ_BYTE * opj_jp2_write_bpcc( opj_jp2_t *jp2,
81 OPJ_UINT32 * p_nb_bytes_written );
84 * Reads a Bit per Component box.
86 * @param p_bpc_header_data pointer to actual data (already read from file)
87 * @param jp2 the jpeg2000 file codec.
88 * @param p_bpc_header_size the size of the bpc header
89 * @param p_manager the user event manager.
91 * @return true if the bpc header is valid, fale else.
93 static OPJ_BOOL opj_jp2_read_bpcc( opj_jp2_t *jp2,
94 OPJ_BYTE * p_bpc_header_data,
95 OPJ_UINT32 p_bpc_header_size,
96 opj_event_mgr_t * p_manager );
98 static OPJ_BOOL opj_jp2_read_cdef( opj_jp2_t * jp2,
99 OPJ_BYTE * p_cdef_header_data,
100 OPJ_UINT32 p_cdef_header_size,
101 opj_event_mgr_t * p_manager );
103 static void opj_jp2_apply_cdef(opj_image_t *image, opj_jp2_color_t *color);
106 * Writes the Colour Specification box.
108 * @param jp2 jpeg2000 file codec.
109 * @param p_nb_bytes_written pointer to store the nb of bytes written by the function.
111 * @return the data being copied.
113 static OPJ_BYTE * opj_jp2_write_colr( opj_jp2_t *jp2,
114 OPJ_UINT32 * p_nb_bytes_written );
117 * Writes a FTYP box - File type box
119 * @param cio the stream to write data to.
120 * @param jp2 the jpeg2000 file codec.
121 * @param p_manager the user event manager.
123 * @return true if writing was successful.
125 static OPJ_BOOL opj_jp2_write_ftyp( opj_jp2_t *jp2,
126 opj_stream_private_t *cio,
127 opj_event_mgr_t * p_manager );
130 * Reads a a FTYP box - File type box
132 * @param p_header_data the data contained in the FTYP box.
133 * @param jp2 the jpeg2000 file codec.
134 * @param p_header_size the size of the data contained in the FTYP box.
135 * @param p_manager the user event manager.
137 * @return true if the FTYP box is valid.
139 static OPJ_BOOL opj_jp2_read_ftyp( opj_jp2_t *jp2,
140 OPJ_BYTE * p_header_data,
141 OPJ_UINT32 p_header_size,
142 opj_event_mgr_t * p_manager );
144 OPJ_BOOL opj_jp2_skip_jp2c( opj_jp2_t *jp2,
145 opj_stream_private_t *cio,
146 opj_event_mgr_t * p_manager );
149 * Reads the Jpeg2000 file Header box - JP2 Header box (warning, this is a super box).
151 * @param p_header_data the data contained in the file header box.
152 * @param jp2 the jpeg2000 file codec.
153 * @param p_header_size the size of the data contained in the file header box.
154 * @param p_manager the user event manager.
156 * @return true if the JP2 Header box was successfully reconized.
158 static OPJ_BOOL opj_jp2_read_jp2h( opj_jp2_t *jp2,
159 OPJ_BYTE *p_header_data,
160 OPJ_UINT32 p_header_size,
161 opj_event_mgr_t * p_manager );
164 * Writes the Jpeg2000 codestream Header box - JP2C Header box. This function must be called AFTER the coding has been done.
166 * @param cio the stream to write data to.
167 * @param jp2 the jpeg2000 file codec.
168 * @param p_manager user event manager.
170 * @return true if writing was successful.
172 static OPJ_BOOL opj_jp2_write_jp2c( opj_jp2_t *jp2,
173 opj_stream_private_t *cio,
174 opj_event_mgr_t * p_manager );
178 * Write index Finder box
179 * @param cio the stream to write to.
180 * @param jp2 the jpeg2000 file codec.
181 * @param p_manager user event manager.
183 static OPJ_BOOL opj_jpip_write_iptr( opj_jp2_t *jp2,
184 opj_stream_private_t *cio,
185 opj_event_mgr_t * p_manager );
188 * Write index Finder box
189 * @param cio the stream to write to.
190 * @param jp2 the jpeg2000 file codec.
191 * @param p_manager user event manager.
193 static OPJ_BOOL opj_jpip_write_cidx(opj_jp2_t *jp2,
194 opj_stream_private_t *cio,
195 opj_event_mgr_t * p_manager );
198 * Write file Index (superbox)
199 * @param cio the stream to write to.
200 * @param jp2 the jpeg2000 file codec.
201 * @param p_manager user event manager.
203 static OPJ_BOOL opj_jpip_write_fidx(opj_jp2_t *jp2,
204 opj_stream_private_t *cio,
205 opj_event_mgr_t * p_manager );
206 #endif /* USE_JPIP */
209 * Reads a jpeg2000 file signature box.
211 * @param p_header_data the data contained in the signature box.
212 * @param jp2 the jpeg2000 file codec.
213 * @param p_header_size the size of the data contained in the signature box.
214 * @param p_manager the user event manager.
216 * @return true if the file signature box is valid.
218 static OPJ_BOOL opj_jp2_read_jp(opj_jp2_t *jp2,
219 OPJ_BYTE * p_header_data,
220 OPJ_UINT32 p_header_size,
221 opj_event_mgr_t * p_manager);
224 * Writes a jpeg2000 file signature box.
226 * @param cio the stream to write data to.
227 * @param jp2 the jpeg2000 file codec.
228 * @param p_manager the user event manager.
230 * @return true if writing was successful.
232 static OPJ_BOOL opj_jp2_write_jp( opj_jp2_t *jp2,
233 opj_stream_private_t *cio,
234 opj_event_mgr_t * p_manager );
237 Apply collected palette data
238 @param color Collector for profile, cdef and pclr data
241 static void opj_jp2_apply_pclr(opj_image_t *image, opj_jp2_color_t *color);
243 static void opj_jp2_free_pclr(opj_jp2_color_t *color);
246 * Collect palette data
248 * @param jp2 JP2 handle
249 * @param p_pclr_header_data FIXME DOC
250 * @param p_pclr_header_size FIXME DOC
253 * @return Returns true if successful, returns false otherwise
255 static OPJ_BOOL opj_jp2_read_pclr( opj_jp2_t *jp2,
256 OPJ_BYTE * p_pclr_header_data,
257 OPJ_UINT32 p_pclr_header_size,
258 opj_event_mgr_t * p_manager );
261 * Collect component mapping data
263 * @param jp2 JP2 handle
264 * @param p_cmap_header_data FIXME DOC
265 * @param p_cmap_header_size FIXME DOC
266 * @param p_manager FIXME DOC
268 * @return Returns true if successful, returns false otherwise
271 static OPJ_BOOL opj_jp2_read_cmap( opj_jp2_t * jp2,
272 OPJ_BYTE * p_cmap_header_data,
273 OPJ_UINT32 p_cmap_header_size,
274 opj_event_mgr_t * p_manager );
277 * Reads the Color Specification box.
279 * @param p_colr_header_data pointer to actual data (already read from file)
280 * @param jp2 the jpeg2000 file codec.
281 * @param p_colr_header_size the size of the color header
282 * @param p_manager the user event manager.
284 * @return true if the bpc header is valid, fale else.
286 static OPJ_BOOL opj_jp2_read_colr( opj_jp2_t *jp2,
287 OPJ_BYTE * p_colr_header_data,
288 OPJ_UINT32 p_colr_header_size,
289 opj_event_mgr_t * p_manager );
296 * Sets up the procedures to do on writing header after the codestream.
297 * Developpers wanting to extend the library can add their own writing procedures.
299 static void opj_jp2_setup_end_header_writing (opj_jp2_t *jp2);
302 * Sets up the procedures to do on reading 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_reading (opj_jp2_t *jp2);
308 * Reads a jpeg2000 file header structure.
310 * @param jp2 the jpeg2000 file header structure.
311 * @param stream the stream to read data from.
312 * @param p_manager the user event manager.
314 * @return true if the box is valid.
316 static OPJ_BOOL opj_jp2_read_header_procedure( opj_jp2_t *jp2,
317 opj_stream_private_t *stream,
318 opj_event_mgr_t * p_manager );
321 * Excutes the given procedures on the given codec.
323 * @param p_procedure_list the list of procedures to execute
324 * @param jp2 the jpeg2000 file codec to execute the procedures on.
325 * @param stream the stream to execute the procedures on.
326 * @param p_manager the user manager.
328 * @return true if all the procedures were successfully executed.
330 static OPJ_BOOL opj_jp2_exec ( opj_jp2_t * jp2,
331 opj_procedure_list_t * p_procedure_list,
332 opj_stream_private_t *stream,
333 opj_event_mgr_t * p_manager );
336 * Reads a box header. The box is the way data is packed inside a jpeg2000 file structure.
338 * @param cio the input stream to read data from.
339 * @param box the box structure to fill.
340 * @param p_number_bytes_read pointer to an int that will store the number of bytes read from the stream (shoul usually be 2).
341 * @param p_manager user event manager.
343 * @return true if the box is reconized, false otherwise
345 static OPJ_BOOL opj_jp2_read_boxhdr(opj_jp2_box_t *box,
346 OPJ_UINT32 * p_number_bytes_read,
347 opj_stream_private_t *cio,
348 opj_event_mgr_t * p_manager);
351 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
352 * are valid. Developpers wanting to extend the library can add their own validation procedures.
354 static void opj_jp2_setup_encoding_validation (opj_jp2_t *jp2);
357 * Sets up the procedures to do on writing header. Developpers wanting to extend the library can add their own writing procedures.
359 static void opj_jp2_setup_header_writing (opj_jp2_t *jp2);
361 OPJ_BOOL opj_jp2_default_validation ( opj_jp2_t * jp2,
362 opj_stream_private_t *cio,
363 opj_event_mgr_t * p_manager );
366 * Finds the image execution function related to the given box id.
368 * @param p_id the id of the handler to fetch.
370 * @return the given handler or NULL if it could not be found.
372 static const opj_jp2_header_handler_t * opj_jp2_img_find_handler (OPJ_UINT32 p_id);
375 * Finds the execution function related to the given box id.
377 * @param p_id the id of the handler to fetch.
379 * @return the given handler or NULL if it could not be found.
381 static const opj_jp2_header_handler_t * opj_jp2_find_handler (OPJ_UINT32 p_id );
383 const opj_jp2_header_handler_t jp2_header [] =
385 {JP2_JP,opj_jp2_read_jp},
386 {JP2_FTYP,opj_jp2_read_ftyp},
387 {JP2_JP2H,opj_jp2_read_jp2h}
390 const opj_jp2_header_handler_t jp2_img_header [] =
392 {JP2_IHDR,opj_jp2_read_ihdr},
393 {JP2_COLR,opj_jp2_read_colr},
394 {JP2_BPCC,opj_jp2_read_bpcc},
395 {JP2_PCLR,opj_jp2_read_pclr},
396 {JP2_CMAP,opj_jp2_read_cmap},
397 {JP2_CDEF,opj_jp2_read_cdef}
402 * Reads a box header. The box is the way data is packed inside a jpeg2000 file structure. Data is read from a character string
404 * @param box the box structure to fill.
405 * @param p_data the character string to read data from.
406 * @param p_number_bytes_read pointer to an int that will store the number of bytes read from the stream (shoul usually be 2).
407 * @param p_box_max_size the maximum number of bytes in the box.
408 * @param p_manager FIXME DOC
410 * @return true if the box is reconized, false otherwise
412 static OPJ_BOOL opj_jp2_read_boxhdr_char( opj_jp2_box_t *box,
414 OPJ_UINT32 * p_number_bytes_read,
415 OPJ_UINT32 p_box_max_size,
416 opj_event_mgr_t * p_manager );
419 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
420 * are valid. Developpers wanting to extend the library can add their own validation procedures.
422 static void opj_jp2_setup_decoding_validation (opj_jp2_t *jp2);
425 * Sets up the procedures to do on reading header.
426 * Developpers wanting to extend the library can add their own writing procedures.
428 static void opj_jp2_setup_header_reading (opj_jp2_t *jp2);
430 /* ----------------------------------------------------------------------- */
431 OPJ_BOOL opj_jp2_read_boxhdr(opj_jp2_box_t *box,
432 OPJ_UINT32 * p_number_bytes_read,
433 opj_stream_private_t *cio,
434 opj_event_mgr_t * p_manager )
436 /* read header from file */
437 OPJ_BYTE l_data_header [8];
442 assert(p_number_bytes_read != 00);
443 assert(p_manager != 00);
445 *p_number_bytes_read = (OPJ_UINT32)opj_stream_read_data(cio,l_data_header,8,p_manager);
446 if (*p_number_bytes_read != 8) {
450 /* process read data */
451 opj_read_bytes(l_data_header,&(box->length), 4);
452 opj_read_bytes(l_data_header+4,&(box->type), 4);
454 if(box->length == 0)/* last box */
456 const OPJ_OFF_T bleft = opj_stream_get_number_byte_left(cio);
457 box->length = (OPJ_UINT32)bleft;
458 assert( (OPJ_OFF_T)box->length == bleft );
462 /* do we have a "special very large box ?" */
463 /* read then the XLBox */
464 if (box->length == 1) {
465 OPJ_UINT32 l_xl_part_size;
467 OPJ_UINT32 l_nb_bytes_read = (OPJ_UINT32)opj_stream_read_data(cio,l_data_header,8,p_manager);
468 if (l_nb_bytes_read != 8) {
469 if (l_nb_bytes_read > 0) {
470 *p_number_bytes_read += l_nb_bytes_read;
476 *p_number_bytes_read = 16;
477 opj_read_bytes(l_data_header,&l_xl_part_size, 4);
478 if (l_xl_part_size != 0) {
479 opj_event_msg(p_manager, EVT_ERROR, "Cannot handle box sizes higher than 2^32\n");
482 opj_read_bytes(l_data_header+4,&(box->length), 4);
488 static void jp2_write_url(opj_cio_t *cio, char *Idx_file) {
492 box.init_pos = cio_tell(cio);
494 cio_write(cio, JP2_URL, 4); /* DBTL */
495 cio_write(cio, 0, 1); /* VERS */
496 cio_write(cio, 0, 3); /* FLAG */
499 for (i = 0; i < strlen(Idx_file); i++) {
500 cio_write(cio, Idx_file[i], 1);
504 box.length = cio_tell(cio) - box.init_pos;
505 cio_seek(cio, box.init_pos);
506 cio_write(cio, box.length, 4); /* L */
507 cio_seek(cio, box.init_pos + box.length);
511 OPJ_BOOL opj_jp2_read_ihdr( opj_jp2_t *jp2,
512 OPJ_BYTE *p_image_header_data,
513 OPJ_UINT32 p_image_header_size,
514 opj_event_mgr_t * p_manager )
517 assert(p_image_header_data != 00);
519 assert(p_manager != 00);
521 if (p_image_header_size != 14) {
522 opj_event_msg(p_manager, EVT_ERROR, "Bad image header box (bad size)\n");
526 opj_read_bytes(p_image_header_data,&(jp2->h),4); /* HEIGHT */
527 p_image_header_data += 4;
528 opj_read_bytes(p_image_header_data,&(jp2->w),4); /* WIDTH */
529 p_image_header_data += 4;
530 opj_read_bytes(p_image_header_data,&(jp2->numcomps),2); /* NC */
531 p_image_header_data += 2;
533 /* allocate memory for components */
534 jp2->comps = (opj_jp2_comps_t*) opj_malloc(jp2->numcomps * sizeof(opj_jp2_comps_t));
535 if (jp2->comps == 0) {
536 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to handle image header (ihdr)\n");
539 memset(jp2->comps,0,jp2->numcomps * sizeof(opj_jp2_comps_t));
541 opj_read_bytes(p_image_header_data,&(jp2->bpc),1); /* BPC */
542 ++ p_image_header_data;
544 opj_read_bytes(p_image_header_data,&(jp2->C),1); /* C */
545 ++ p_image_header_data;
547 /* Should be equal to 7 cf. chapter about image header box of the norm */
549 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);
552 opj_read_bytes(p_image_header_data,&(jp2->UnkC),1); /* UnkC */
553 ++ p_image_header_data;
554 opj_read_bytes(p_image_header_data,&(jp2->IPR),1); /* IPR */
555 ++ p_image_header_data;
560 OPJ_BYTE * opj_jp2_write_ihdr(opj_jp2_t *jp2,
561 OPJ_UINT32 * p_nb_bytes_written
564 OPJ_BYTE * l_ihdr_data,* l_current_ihdr_ptr;
568 assert(p_nb_bytes_written != 00);
570 /* default image header is 22 bytes wide */
571 l_ihdr_data = (OPJ_BYTE *) opj_malloc(22);
572 if (l_ihdr_data == 00) {
575 memset(l_ihdr_data,0,22);
577 l_current_ihdr_ptr = l_ihdr_data;
579 opj_write_bytes(l_current_ihdr_ptr,22,4); /* write box size */
580 l_current_ihdr_ptr+=4;
582 opj_write_bytes(l_current_ihdr_ptr,JP2_IHDR, 4); /* IHDR */
583 l_current_ihdr_ptr+=4;
585 opj_write_bytes(l_current_ihdr_ptr,jp2->h, 4); /* HEIGHT */
586 l_current_ihdr_ptr+=4;
588 opj_write_bytes(l_current_ihdr_ptr, jp2->w, 4); /* WIDTH */
589 l_current_ihdr_ptr+=4;
591 opj_write_bytes(l_current_ihdr_ptr, jp2->numcomps, 2); /* NC */
592 l_current_ihdr_ptr+=2;
594 opj_write_bytes(l_current_ihdr_ptr, jp2->bpc, 1); /* BPC */
595 ++l_current_ihdr_ptr;
597 opj_write_bytes(l_current_ihdr_ptr, jp2->C, 1); /* C : Always 7 */
598 ++l_current_ihdr_ptr;
600 opj_write_bytes(l_current_ihdr_ptr, jp2->UnkC, 1); /* UnkC, colorspace unknown */
601 ++l_current_ihdr_ptr;
603 opj_write_bytes(l_current_ihdr_ptr, jp2->IPR, 1); /* IPR, no intellectual property */
604 ++l_current_ihdr_ptr;
606 *p_nb_bytes_written = 22;
611 OPJ_BYTE * opj_jp2_write_bpcc( opj_jp2_t *jp2,
612 OPJ_UINT32 * p_nb_bytes_written
616 /* room for 8 bytes for box and 1 byte for each component */
617 OPJ_INT32 l_bpcc_size = 8 + jp2->numcomps;
618 OPJ_BYTE * l_bpcc_data,* l_current_bpcc_ptr;
622 assert(p_nb_bytes_written != 00);
624 l_bpcc_data = (OPJ_BYTE *) opj_malloc(l_bpcc_size);
625 if (l_bpcc_data == 00) {
628 memset(l_bpcc_data,0,l_bpcc_size);
630 l_current_bpcc_ptr = l_bpcc_data;
632 opj_write_bytes(l_current_bpcc_ptr,l_bpcc_size,4); /* write box size */
633 l_current_bpcc_ptr += 4;
635 opj_write_bytes(l_current_bpcc_ptr,JP2_BPCC,4); /* BPCC */
636 l_current_bpcc_ptr += 4;
638 for (i = 0; i < jp2->numcomps; ++i) {
639 opj_write_bytes(l_current_bpcc_ptr, jp2->comps[i].bpcc, 1); /* write each component information */
640 ++l_current_bpcc_ptr;
643 *p_nb_bytes_written = l_bpcc_size;
648 OPJ_BOOL opj_jp2_read_bpcc( opj_jp2_t *jp2,
649 OPJ_BYTE * p_bpc_header_data,
650 OPJ_UINT32 p_bpc_header_size,
651 opj_event_mgr_t * p_manager
657 assert(p_bpc_header_data != 00);
659 assert(p_manager != 00);
662 if (jp2->bpc != 255 ){
663 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);
666 /* and length is relevant */
667 if (p_bpc_header_size != jp2->numcomps) {
668 opj_event_msg(p_manager, EVT_ERROR, "Bad BPCC header box (bad size)\n");
672 /* read info for each component */
673 for (i = 0; i < jp2->numcomps; ++i) {
674 opj_read_bytes(p_bpc_header_data,&jp2->comps[i].bpcc ,1); /* read each BPCC component */
681 OPJ_BYTE * opj_jp2_write_colr( opj_jp2_t *jp2,
682 OPJ_UINT32 * p_nb_bytes_written
685 /* room for 8 bytes for box 3 for common data and variable upon profile*/
686 OPJ_UINT32 l_colr_size = 11;
687 OPJ_BYTE * l_colr_data,* l_current_colr_ptr;
691 assert(p_nb_bytes_written != 00);
692 assert(jp2->meth == 1 || jp2->meth == 2);
696 l_colr_size += 4; /* EnumCS */
699 assert(jp2->color.icc_profile_len); /* ICC profile */
700 l_colr_size += jp2->color.icc_profile_len;
706 l_colr_data = (OPJ_BYTE *) opj_malloc(l_colr_size);
707 if (l_colr_data == 00) {
710 memset(l_colr_data,0,l_colr_size);
712 l_current_colr_ptr = l_colr_data;
714 opj_write_bytes(l_current_colr_ptr,l_colr_size,4); /* write box size */
715 l_current_colr_ptr += 4;
717 opj_write_bytes(l_current_colr_ptr,JP2_COLR,4); /* BPCC */
718 l_current_colr_ptr += 4;
720 opj_write_bytes(l_current_colr_ptr, jp2->meth,1); /* METH */
721 ++l_current_colr_ptr;
723 opj_write_bytes(l_current_colr_ptr, jp2->precedence,1); /* PRECEDENCE */
724 ++l_current_colr_ptr;
726 opj_write_bytes(l_current_colr_ptr, jp2->approx,1); /* APPROX */
727 ++l_current_colr_ptr;
729 if (jp2->meth == 1) { /* Meth value is restricted to 1 or 2 (Table I.9 of part 1) */
730 opj_write_bytes(l_current_colr_ptr, jp2->enumcs,4); } /* EnumCS */
732 if (jp2->meth == 2) { /* ICC profile */
734 for(i = 0; i < jp2->color.icc_profile_len; ++i) {
735 opj_write_bytes(l_current_colr_ptr, jp2->color.icc_profile_buf[i], 1);
736 ++l_current_colr_ptr;
741 *p_nb_bytes_written = l_colr_size;
746 void opj_jp2_free_pclr(opj_jp2_color_t *color)
748 opj_free(color->jp2_pclr->channel_sign);
749 opj_free(color->jp2_pclr->channel_size);
750 opj_free(color->jp2_pclr->entries);
752 if(color->jp2_pclr->cmap) opj_free(color->jp2_pclr->cmap);
754 opj_free(color->jp2_pclr); color->jp2_pclr = NULL;
757 static OPJ_BOOL opj_jp2_check_color(opj_image_t *image, opj_jp2_color_t *color, opj_event_mgr_t *p_manager)
761 /* testcase 4149.pdf.SIGSEGV.cf7.3501 */
762 if (color->jp2_cdef) {
763 opj_jp2_cdef_info_t *info = color->jp2_cdef->info;
764 OPJ_UINT16 n = color->jp2_cdef->n;
766 for (i = 0; i < n; i++) {
767 if (info[i].cn >= image->numcomps) {
768 opj_event_msg(p_manager, EVT_ERROR, "Invalid component index %d (>= %d).\n", info[i].cn, image->numcomps);
771 if (info[i].asoc > 0 && (OPJ_UINT32)(info[i].asoc - 1) >= image->numcomps) {
772 opj_event_msg(p_manager, EVT_ERROR, "Invalid component index %d (>= %d).\n", info[i].asoc - 1, image->numcomps);
778 /* testcases 451.pdf.SIGSEGV.f4c.3723, 451.pdf.SIGSEGV.5b5.3723 and
779 66ea31acbb0f23a2bbc91f64d69a03f5_signal_sigsegv_13937c0_7030_5725.pdf */
780 if (color->jp2_pclr && color->jp2_pclr->cmap) {
781 OPJ_UINT16 nr_channels = color->jp2_pclr->nr_channels;
782 opj_jp2_cmap_comp_t *cmap = color->jp2_pclr->cmap;
783 OPJ_BOOL *pcol_usage, is_sane = OPJ_TRUE;
785 /* verify that all original components match an existing one */
786 for (i = 0; i < nr_channels; i++) {
787 if (cmap[i].cmp >= image->numcomps) {
788 opj_event_msg(p_manager, EVT_ERROR, "Invalid component index %d (>= %d).\n", cmap[i].cmp, image->numcomps);
793 pcol_usage = opj_calloc(nr_channels, sizeof(OPJ_BOOL));
795 opj_event_msg(p_manager, EVT_ERROR, "Unexpected OOM.\n");
798 /* verify that no component is targeted more than once */
799 for (i = 0; i < nr_channels; i++) {
800 OPJ_UINT16 pcol = cmap[i].pcol;
801 if (pcol >= nr_channels) {
802 opj_event_msg(p_manager, EVT_ERROR, "Invalid component/palette index for direct mapping %d.\n", pcol);
805 else if (pcol_usage[pcol]) {
806 opj_event_msg(p_manager, EVT_ERROR, "Component %d is mapped twice.\n", pcol);
810 pcol_usage[pcol] = OPJ_TRUE;
812 /* verify that all components are targeted at least once */
813 for (i = 0; i < nr_channels; i++) {
814 if (!pcol_usage[i]) {
815 opj_event_msg(p_manager, EVT_ERROR, "Component %d doesn't have a mapping.\n", i);
819 opj_free(pcol_usage);
829 void opj_jp2_apply_pclr(opj_image_t *image, opj_jp2_color_t *color)
831 opj_image_comp_t *old_comps, *new_comps;
832 OPJ_BYTE *channel_size, *channel_sign;
834 opj_jp2_cmap_comp_t *cmap;
835 OPJ_INT32 *src, *dst;
837 OPJ_UINT16 i, nr_channels, cmp, pcol;
840 channel_size = color->jp2_pclr->channel_size;
841 channel_sign = color->jp2_pclr->channel_sign;
842 entries = color->jp2_pclr->entries;
843 cmap = color->jp2_pclr->cmap;
844 nr_channels = color->jp2_pclr->nr_channels;
846 old_comps = image->comps;
847 new_comps = (opj_image_comp_t*)
848 opj_malloc(nr_channels * sizeof(opj_image_comp_t));
850 for(i = 0; i < nr_channels; ++i) {
851 pcol = cmap[i].pcol; cmp = cmap[i].cmp;
853 new_comps[pcol] = old_comps[cmp];
856 if(cmap[i].mtyp == 0){
857 old_comps[cmp].data = NULL; continue;
860 /* Palette mapping: */
861 new_comps[pcol].data = (OPJ_INT32*)
862 opj_malloc(old_comps[cmp].w * old_comps[cmp].h * sizeof(OPJ_INT32));
863 new_comps[pcol].prec = channel_size[i];
864 new_comps[pcol].sgnd = channel_sign[i];
867 top_k = color->jp2_pclr->nr_entries - 1;
869 for(i = 0; i < nr_channels; ++i) {
871 if(cmap[i].mtyp == 0) continue;
873 /* Palette mapping: */
874 cmp = cmap[i].cmp; pcol = cmap[i].pcol;
875 src = old_comps[cmp].data;
876 dst = new_comps[pcol].data;
877 max = new_comps[pcol].w * new_comps[pcol].h;
879 for(j = 0; j < max; ++j)
882 if((k = src[j]) < 0) k = 0; else if(k > top_k) k = top_k;
885 dst[j] = entries[k * nr_channels + pcol];
889 max = image->numcomps;
890 for(i = 0; i < max; ++i) {
891 if(old_comps[i].data) opj_free(old_comps[i].data);
895 image->comps = new_comps;
896 image->numcomps = nr_channels;
898 opj_jp2_free_pclr(color);
902 OPJ_BOOL opj_jp2_read_pclr( opj_jp2_t *jp2,
903 OPJ_BYTE * p_pclr_header_data,
904 OPJ_UINT32 p_pclr_header_size,
905 opj_event_mgr_t * p_manager
908 opj_jp2_pclr_t *jp2_pclr;
909 OPJ_BYTE *channel_size, *channel_sign;
911 OPJ_UINT16 nr_entries,nr_channels;
914 OPJ_BYTE *orig_header_data = p_pclr_header_data;
917 assert(p_pclr_header_data != 00);
919 assert(p_manager != 00);
920 (void)p_pclr_header_size;
922 if(jp2->color.jp2_pclr)
925 if (p_pclr_header_size < 3)
928 opj_read_bytes(p_pclr_header_data, &l_value , 2); /* NE */
929 p_pclr_header_data += 2;
930 nr_entries = (OPJ_UINT16) l_value;
932 opj_read_bytes(p_pclr_header_data, &l_value , 1); /* NPC */
933 ++p_pclr_header_data;
934 nr_channels = (OPJ_UINT16) l_value;
936 if (p_pclr_header_size < 3 + (OPJ_UINT32)nr_channels || nr_channels == 0 || nr_entries >= (OPJ_UINT32)-1 / nr_channels)
939 entries = (OPJ_UINT32*) opj_malloc(nr_channels * nr_entries * sizeof(OPJ_UINT32));
942 channel_size = (OPJ_BYTE*) opj_malloc(nr_channels);
948 channel_sign = (OPJ_BYTE*) opj_malloc(nr_channels);
952 opj_free(channel_size);
956 jp2_pclr = (opj_jp2_pclr_t*)opj_malloc(sizeof(opj_jp2_pclr_t));
960 opj_free(channel_size);
961 opj_free(channel_sign);
965 jp2_pclr->channel_sign = channel_sign;
966 jp2_pclr->channel_size = channel_size;
967 jp2_pclr->entries = entries;
968 jp2_pclr->nr_entries = nr_entries;
969 jp2_pclr->nr_channels = (OPJ_BYTE) l_value;
970 jp2_pclr->cmap = NULL;
972 jp2->color.jp2_pclr = jp2_pclr;
974 for(i = 0; i < nr_channels; ++i) {
975 opj_read_bytes(p_pclr_header_data, &l_value , 1); /* Bi */
976 ++p_pclr_header_data;
978 channel_size[i] = (OPJ_BYTE)((l_value & 0x7f) + 1);
979 channel_sign[i] = (l_value & 0x80) ? 1 : 0;
982 for(j = 0; j < nr_entries; ++j) {
983 for(i = 0; i < nr_channels; ++i) {
984 OPJ_INT32 bytes_to_read = (channel_size[i]+7)>>3;
986 if (bytes_to_read > sizeof(OPJ_UINT32))
987 bytes_to_read = sizeof(OPJ_UINT32);
988 if ((ptrdiff_t)p_pclr_header_size < p_pclr_header_data - orig_header_data + bytes_to_read)
991 if (bytes_to_read > sizeof(OPJ_UINT32))
992 bytes_to_read = sizeof(OPJ_UINT32);
993 if ((ptrdiff_t)p_pclr_header_size < p_pclr_header_data - orig_header_data + bytes_to_read)
996 opj_read_bytes(p_pclr_header_data, &l_value , bytes_to_read); /* Cji */
997 p_pclr_header_data += bytes_to_read;
998 *entries = (OPJ_UINT32) l_value;
1006 OPJ_BOOL opj_jp2_read_cmap( opj_jp2_t * jp2,
1007 OPJ_BYTE * p_cmap_header_data,
1008 OPJ_UINT32 p_cmap_header_size,
1009 opj_event_mgr_t * p_manager
1012 opj_jp2_cmap_comp_t *cmap;
1013 OPJ_BYTE i, nr_channels;
1018 assert(p_cmap_header_data != 00);
1019 assert(p_manager != 00);
1020 (void)p_cmap_header_size;
1022 /* Need nr_channels: */
1023 if(jp2->color.jp2_pclr == NULL) {
1024 opj_event_msg(p_manager, EVT_ERROR, "Need to read a PCLR box before the CMAP box.\n");
1028 /* Part 1, I.5.3.5: 'There shall be at most one Component Mapping box
1029 * inside a JP2 Header box' :
1031 if(jp2->color.jp2_pclr->cmap) {
1032 opj_event_msg(p_manager, EVT_ERROR, "Only one CMAP box is allowed.\n");
1036 nr_channels = jp2->color.jp2_pclr->nr_channels;
1037 if (p_cmap_header_size < (OPJ_UINT32)nr_channels * 4) {
1038 opj_event_msg(p_manager, EVT_ERROR, "Insufficient data for CMAP box.\n");
1042 if (p_cmap_header_size < (OPJ_UINT32)nr_channels * 4) {
1043 opj_event_msg(p_manager, EVT_ERROR, "Insufficient data for CMAP box.\n");
1047 cmap = (opj_jp2_cmap_comp_t*) opj_malloc(nr_channels * sizeof(opj_jp2_cmap_comp_t));
1052 for(i = 0; i < nr_channels; ++i) {
1053 opj_read_bytes(p_cmap_header_data, &l_value, 2); /* CMP^i */
1054 p_cmap_header_data +=2;
1055 cmap[i].cmp = (OPJ_UINT16) l_value;
1057 opj_read_bytes(p_cmap_header_data, &l_value, 1); /* MTYP^i */
1058 ++p_cmap_header_data;
1059 cmap[i].mtyp = (OPJ_BYTE) l_value;
1061 opj_read_bytes(p_cmap_header_data, &l_value, 1); /* PCOL^i */
1062 ++p_cmap_header_data;
1063 cmap[i].pcol = (OPJ_BYTE) l_value;
1066 jp2->color.jp2_pclr->cmap = cmap;
1071 void opj_jp2_apply_cdef(opj_image_t *image, opj_jp2_color_t *color)
1073 opj_jp2_cdef_info_t *info;
1074 OPJ_UINT16 i, n, cn, asoc, acn;
1076 info = color->jp2_cdef->info;
1077 n = color->jp2_cdef->n;
1079 for(i = 0; i < n; ++i)
1081 /* WATCH: acn = asoc - 1 ! */
1082 asoc = info[i].asoc;
1083 if(asoc == 0 || asoc == 65535)
1085 if (i < image->numcomps)
1086 image->comps[i].alpha = info[i].typ;
1091 acn = (OPJ_INT16)(asoc - 1);
1092 if( cn >= image->numcomps || acn >= image->numcomps )
1094 fprintf(stderr, "cn=%d, acn=%d, numcomps=%d\n", cn, acn, image->numcomps);
1100 opj_image_comp_t saved;
1102 memcpy(&saved, &image->comps[cn], sizeof(opj_image_comp_t));
1103 memcpy(&image->comps[cn], &image->comps[acn], sizeof(opj_image_comp_t));
1104 memcpy(&image->comps[acn], &saved, sizeof(opj_image_comp_t));
1106 info[i].asoc = (OPJ_UINT16)(cn + 1);
1107 info[acn].asoc = (OPJ_UINT16)(info[acn].cn + 1);
1110 image->comps[cn].alpha = info[i].typ;
1113 if(color->jp2_cdef->info) opj_free(color->jp2_cdef->info);
1115 opj_free(color->jp2_cdef); color->jp2_cdef = NULL;
1117 }/* jp2_apply_cdef() */
1119 OPJ_BOOL opj_jp2_read_cdef( opj_jp2_t * jp2,
1120 OPJ_BYTE * p_cdef_header_data,
1121 OPJ_UINT32 p_cdef_header_size,
1122 opj_event_mgr_t * p_manager
1125 opj_jp2_cdef_info_t *cdef_info;
1131 assert(p_cdef_header_data != 00);
1132 assert(p_manager != 00);
1133 (void)p_cdef_header_size;
1135 /* Part 1, I.5.3.6: 'The shall be at most one Channel Definition box
1136 * inside a JP2 Header box.'*/
1137 if(jp2->color.jp2_cdef) return OPJ_FALSE;
1139 if (p_cdef_header_size < 2) {
1140 opj_event_msg(p_manager, EVT_ERROR, "Insufficient data for CDEF box.\n");
1144 if (p_cdef_header_size < 2) {
1145 opj_event_msg(p_manager, EVT_ERROR, "Insufficient data for CDEF box.\n");
1149 opj_read_bytes(p_cdef_header_data,&l_value ,2); /* N */
1150 p_cdef_header_data+= 2;
1152 if ( (OPJ_UINT16)l_value == 0){ /* szukw000: FIXME */
1153 opj_event_msg(p_manager, EVT_ERROR, "Number of channel description is equal to zero in CDEF box.\n");
1157 if (p_cdef_header_size < 2 + (OPJ_UINT32)(OPJ_UINT16)l_value * 6) {
1158 opj_event_msg(p_manager, EVT_ERROR, "Insufficient data for CDEF box.\n");
1162 if (p_cdef_header_size < 2 + (OPJ_UINT32)(OPJ_UINT16)l_value * 6) {
1163 opj_event_msg(p_manager, EVT_ERROR, "Insufficient data for CDEF box.\n");
1167 cdef_info = (opj_jp2_cdef_info_t*) opj_malloc(l_value * sizeof(opj_jp2_cdef_info_t));
1171 jp2->color.jp2_cdef = (opj_jp2_cdef_t*)opj_malloc(sizeof(opj_jp2_cdef_t));
1172 if(!jp2->color.jp2_cdef)
1174 opj_free(cdef_info);
1177 jp2->color.jp2_cdef->info = cdef_info;
1178 jp2->color.jp2_cdef->n = (OPJ_UINT16) l_value;
1180 for(i = 0; i < jp2->color.jp2_cdef->n; ++i) {
1181 opj_read_bytes(p_cdef_header_data, &l_value, 2); /* Cn^i */
1182 p_cdef_header_data +=2;
1183 cdef_info[i].cn = (OPJ_UINT16) l_value;
1185 opj_read_bytes(p_cdef_header_data, &l_value, 2); /* Typ^i */
1186 p_cdef_header_data +=2;
1187 cdef_info[i].typ = (OPJ_UINT16) l_value;
1189 opj_read_bytes(p_cdef_header_data, &l_value, 2); /* Asoc^i */
1190 p_cdef_header_data +=2;
1191 cdef_info[i].asoc = (OPJ_UINT16) l_value;
1197 OPJ_BOOL opj_jp2_read_colr( opj_jp2_t *jp2,
1198 OPJ_BYTE * p_colr_header_data,
1199 OPJ_UINT32 p_colr_header_size,
1200 opj_event_mgr_t * p_manager
1207 assert(p_colr_header_data != 00);
1208 assert(p_manager != 00);
1210 if (p_colr_header_size < 3) {
1211 opj_event_msg(p_manager, EVT_ERROR, "Bad COLR header box (bad size)\n");
1215 /* Part 1, I.5.3.3 : 'A conforming JP2 reader shall ignore all Colour
1216 * Specification boxes after the first.'
1218 if(jp2->color.jp2_has_colr) {
1219 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");
1220 p_colr_header_data += p_colr_header_size;
1224 opj_read_bytes(p_colr_header_data,&jp2->meth ,1); /* METH */
1225 ++p_colr_header_data;
1227 opj_read_bytes(p_colr_header_data,&jp2->precedence ,1); /* PRECEDENCE */
1228 ++p_colr_header_data;
1230 opj_read_bytes(p_colr_header_data,&jp2->approx ,1); /* APPROX */
1231 ++p_colr_header_data;
1233 if (jp2->meth == 1) {
1234 if (p_colr_header_size < 7) {
1235 opj_event_msg(p_manager, EVT_ERROR, "Bad COLR header box (bad size: %d)\n", p_colr_header_size);
1238 if (p_colr_header_size > 7) {
1239 /* testcase Altona_Technical_v20_x4.pdf */
1240 opj_event_msg(p_manager, EVT_WARNING, "Bad COLR header box (bad size: %d)\n", p_colr_header_size);
1243 opj_read_bytes(p_colr_header_data,&jp2->enumcs ,4); /* EnumCS */
1245 jp2->color.jp2_has_colr = 1;
1247 else if (jp2->meth == 2) {
1249 OPJ_INT32 it_icc_value = 0;
1250 OPJ_INT32 icc_len = p_colr_header_size - 3;
1252 jp2->color.icc_profile_len = icc_len;
1253 jp2->color.icc_profile_buf = (OPJ_BYTE*) opj_malloc(icc_len);
1254 if (!jp2->color.icc_profile_buf)
1256 jp2->color.icc_profile_len = 0;
1259 memset(jp2->color.icc_profile_buf, 0, icc_len * sizeof(OPJ_BYTE));
1261 for (it_icc_value = 0; it_icc_value < icc_len; ++it_icc_value)
1263 opj_read_bytes(p_colr_header_data,&l_value,1); /* icc values */
1264 ++p_colr_header_data;
1265 jp2->color.icc_profile_buf[it_icc_value] = (OPJ_BYTE) l_value;
1268 jp2->color.jp2_has_colr = 1;
1270 else if (jp2->meth > 2)
1272 /* ISO/IEC 15444-1:2004 (E), Table I.9 � Legal METH values:
1273 conforming JP2 reader shall ignore the entire Colour Specification box.*/
1274 opj_event_msg(p_manager, EVT_INFO, "COLR BOX meth value is not a regular value (%d), "
1275 "so we will ignore the entire Colour Specification box. \n", jp2->meth);
1280 OPJ_BOOL opj_jp2_decode(opj_jp2_t *jp2,
1281 opj_stream_private_t *p_stream,
1282 opj_image_t* p_image,
1283 opj_event_mgr_t * p_manager)
1289 if( ! opj_j2k_decode(jp2->j2k, p_stream, p_image, p_manager) ) {
1290 opj_event_msg(p_manager, EVT_ERROR, "Failed to decode the codestream in the JP2 file\n");
1294 if (!jp2->ignore_pclr_cmap_cdef){
1295 if (!opj_jp2_check_color(p_image, &(jp2->color), p_manager)) {
1299 /* Set Image Color Space */
1300 if (jp2->enumcs == 16)
1301 p_image->color_space = OPJ_CLRSPC_SRGB;
1302 else if (jp2->enumcs == 17)
1303 p_image->color_space = OPJ_CLRSPC_GRAY;
1304 else if (jp2->enumcs == 18)
1305 p_image->color_space = OPJ_CLRSPC_SYCC;
1306 else if (jp2->enumcs == 24)
1307 p_image->color_space = OPJ_CLRSPC_EYCC;
1309 p_image->color_space = OPJ_CLRSPC_UNKNOWN;
1311 /* Apply the color space if needed */
1312 if(jp2->color.jp2_cdef) {
1313 opj_jp2_apply_cdef(p_image, &(jp2->color));
1316 if(jp2->color.jp2_pclr) {
1317 /* Part 1, I.5.3.4: Either both or none : */
1318 if( !jp2->color.jp2_pclr->cmap)
1319 opj_jp2_free_pclr(&(jp2->color));
1321 opj_jp2_apply_pclr(p_image, &(jp2->color));
1324 if(jp2->color.icc_profile_buf) {
1325 p_image->icc_profile_buf = jp2->color.icc_profile_buf;
1326 p_image->icc_profile_len = jp2->color.icc_profile_len;
1327 jp2->color.icc_profile_buf = NULL;
1334 OPJ_BOOL opj_jp2_write_jp2h(opj_jp2_t *jp2,
1335 opj_stream_private_t *stream,
1336 opj_event_mgr_t * p_manager
1339 opj_jp2_img_header_writer_handler_t l_writers [3];
1340 opj_jp2_img_header_writer_handler_t * l_current_writer;
1342 OPJ_INT32 i, l_nb_pass;
1343 /* size of data for super box*/
1344 OPJ_INT32 l_jp2h_size = 8;
1345 OPJ_BOOL l_result = OPJ_TRUE;
1347 /* to store the data of the super box */
1348 OPJ_BYTE l_jp2h_data [8];
1351 assert(stream != 00);
1353 assert(p_manager != 00);
1355 memset(l_writers,0,sizeof(l_writers));
1357 if (jp2->bpc == 255) {
1359 l_writers[0].handler = opj_jp2_write_ihdr;
1360 l_writers[1].handler = opj_jp2_write_bpcc;
1361 l_writers[2].handler = opj_jp2_write_colr;
1365 l_writers[0].handler = opj_jp2_write_ihdr;
1366 l_writers[1].handler = opj_jp2_write_colr;
1369 /* write box header */
1370 /* write JP2H type */
1371 opj_write_bytes(l_jp2h_data+4,JP2_JP2H,4);
1373 l_current_writer = l_writers;
1374 for (i=0;i<l_nb_pass;++i) {
1375 l_current_writer->m_data = l_current_writer->handler(jp2,&(l_current_writer->m_size));
1376 if (l_current_writer->m_data == 00) {
1377 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to hold JP2 Header data\n");
1378 l_result = OPJ_FALSE;
1382 l_jp2h_size += l_current_writer->m_size;
1387 l_current_writer = l_writers;
1388 for (i=0;i<l_nb_pass;++i) {
1389 if (l_current_writer->m_data != 00) {
1390 opj_free(l_current_writer->m_data );
1398 /* write super box size */
1399 opj_write_bytes(l_jp2h_data,l_jp2h_size,4);
1401 /* write super box data on stream */
1402 if (opj_stream_write_data(stream,l_jp2h_data,8,p_manager) != 8) {
1403 opj_event_msg(p_manager, EVT_ERROR, "Stream error while writing JP2 Header box\n");
1404 l_result = OPJ_FALSE;
1408 l_current_writer = l_writers;
1409 for (i=0;i<l_nb_pass;++i) {
1410 if (opj_stream_write_data(stream,l_current_writer->m_data,l_current_writer->m_size,p_manager) != l_current_writer->m_size) {
1411 opj_event_msg(p_manager, EVT_ERROR, "Stream error while writing JP2 Header box\n");
1412 l_result = OPJ_FALSE;
1419 l_current_writer = l_writers;
1422 for (i=0;i<l_nb_pass;++i) {
1423 if (l_current_writer->m_data != 00) {
1424 opj_free(l_current_writer->m_data );
1432 OPJ_BOOL opj_jp2_write_ftyp(opj_jp2_t *jp2,
1433 opj_stream_private_t *cio,
1434 opj_event_mgr_t * p_manager )
1437 OPJ_UINT32 l_ftyp_size = 16 + 4 * jp2->numcl;
1438 OPJ_BYTE * l_ftyp_data, * l_current_data_ptr;
1444 assert(p_manager != 00);
1446 l_ftyp_data = (OPJ_BYTE *) opj_malloc(l_ftyp_size);
1448 if (l_ftyp_data == 00) {
1449 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to handle ftyp data\n");
1453 memset(l_ftyp_data,0,l_ftyp_size);
1455 l_current_data_ptr = l_ftyp_data;
1457 opj_write_bytes(l_current_data_ptr, l_ftyp_size,4); /* box size */
1458 l_current_data_ptr += 4;
1460 opj_write_bytes(l_current_data_ptr, JP2_FTYP,4); /* FTYP */
1461 l_current_data_ptr += 4;
1463 opj_write_bytes(l_current_data_ptr, jp2->brand,4); /* BR */
1464 l_current_data_ptr += 4;
1466 opj_write_bytes(l_current_data_ptr, jp2->minversion,4); /* MinV */
1467 l_current_data_ptr += 4;
1469 for (i = 0; i < jp2->numcl; i++) {
1470 opj_write_bytes(l_current_data_ptr, jp2->cl[i],4); /* CL */
1473 l_result = (opj_stream_write_data(cio,l_ftyp_data,l_ftyp_size,p_manager) == l_ftyp_size);
1476 opj_event_msg(p_manager, EVT_ERROR, "Error while writing ftyp data to stream\n");
1479 opj_free(l_ftyp_data);
1484 OPJ_BOOL opj_jp2_write_jp2c(opj_jp2_t *jp2,
1485 opj_stream_private_t *cio,
1486 opj_event_mgr_t * p_manager )
1488 OPJ_OFF_T j2k_codestream_exit;
1489 OPJ_BYTE l_data_header [8];
1494 assert(p_manager != 00);
1495 assert(opj_stream_has_seek(cio));
1497 j2k_codestream_exit = opj_stream_tell(cio);
1498 opj_write_bytes(l_data_header,
1499 (OPJ_UINT32) (j2k_codestream_exit - jp2->j2k_codestream_offset),
1500 4); /* size of codestream */
1501 opj_write_bytes(l_data_header + 4,JP2_JP2C,4); /* JP2C */
1503 if (! opj_stream_seek(cio,jp2->j2k_codestream_offset,p_manager)) {
1504 opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
1508 if (opj_stream_write_data(cio,l_data_header,8,p_manager) != 8) {
1509 opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
1513 if (! opj_stream_seek(cio,j2k_codestream_exit,p_manager)) {
1514 opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
1521 OPJ_BOOL opj_jp2_write_jp( opj_jp2_t *jp2,
1522 opj_stream_private_t *cio,
1523 opj_event_mgr_t * p_manager )
1525 /* 12 bytes will be read */
1526 OPJ_BYTE l_signature_data [12];
1531 assert(p_manager != 00);
1533 /* write box length */
1534 opj_write_bytes(l_signature_data,12,4);
1535 /* writes box type */
1536 opj_write_bytes(l_signature_data+4,JP2_JP,4);
1537 /* writes magic number*/
1538 opj_write_bytes(l_signature_data+8,0x0d0a870a,4);
1540 if (opj_stream_write_data(cio,l_signature_data,12,p_manager) != 12) {
1547 /* ----------------------------------------------------------------------- */
1548 /* JP2 decoder interface */
1549 /* ----------------------------------------------------------------------- */
1551 void opj_jp2_setup_decoder(opj_jp2_t *jp2, opj_dparameters_t *parameters)
1553 /* setup the J2K codec */
1554 opj_j2k_setup_decoder(jp2->j2k, parameters);
1556 /* further JP2 initializations go here */
1557 jp2->color.jp2_has_colr = 0;
1558 jp2->ignore_pclr_cmap_cdef = parameters->flags & OPJ_DPARAMETERS_IGNORE_PCLR_CMAP_CDEF_FLAG;
1561 /* ----------------------------------------------------------------------- */
1562 /* JP2 encoder interface */
1563 /* ----------------------------------------------------------------------- */
1565 void opj_jp2_setup_encoder( opj_jp2_t *jp2,
1566 opj_cparameters_t *parameters,
1568 opj_event_mgr_t * p_manager)
1571 OPJ_INT32 depth_0, sign;
1573 if(!jp2 || !parameters || !image)
1576 /* setup the J2K codec */
1577 /* ------------------- */
1579 /* Check if number of components respects standard */
1580 if (image->numcomps < 1 || image->numcomps > 16384) {
1581 opj_event_msg(p_manager, EVT_ERROR, "Invalid number of components specified while setting up JP2 encoder\n");
1585 opj_j2k_setup_encoder(jp2->j2k, parameters, image, p_manager );
1587 /* setup the JP2 codec */
1588 /* ------------------- */
1592 jp2->brand = JP2_JP2; /* BR */
1593 jp2->minversion = 0; /* MinV */
1595 jp2->cl = (OPJ_UINT32*) opj_malloc(jp2->numcl * sizeof(OPJ_UINT32));
1598 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory when setup the JP2 encoder\n");
1601 jp2->cl[0] = JP2_JP2; /* CL0 : JP2 */
1603 /* Image Header box */
1605 jp2->numcomps = image->numcomps; /* NC */
1606 jp2->comps = (opj_jp2_comps_t*) opj_malloc(jp2->numcomps * sizeof(opj_jp2_comps_t));
1609 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory when setup the JP2 encoder\n");
1613 jp2->h = image->y1 - image->y0; /* HEIGHT */
1614 jp2->w = image->x1 - image->x0; /* WIDTH */
1616 depth_0 = image->comps[0].prec - 1;
1617 sign = image->comps[0].sgnd;
1618 jp2->bpc = depth_0 + (sign << 7);
1619 for (i = 1; i < image->numcomps; i++) {
1620 OPJ_INT32 depth = image->comps[i].prec - 1;
1621 sign = image->comps[i].sgnd;
1622 if (depth_0 != depth)
1625 jp2->C = 7; /* C : Always 7 */
1626 jp2->UnkC = 0; /* UnkC, colorspace specified in colr box */
1627 jp2->IPR = 0; /* IPR, no intellectual property */
1629 /* BitsPerComponent box */
1630 for (i = 0; i < image->numcomps; i++) {
1631 jp2->comps[i].bpcc = image->comps[i].prec - 1 + (image->comps[i].sgnd << 7);
1634 /* Colour Specification box */
1635 if(image->icc_profile_len) {
1641 if (image->color_space == 1)
1642 jp2->enumcs = 16; /* sRGB as defined by IEC 61966-2-1 */
1643 else if (image->color_space == 2)
1644 jp2->enumcs = 17; /* greyscale */
1645 else if (image->color_space == 3)
1646 jp2->enumcs = 18; /* YUV */
1650 jp2->precedence = 0; /* PRECEDENCE */
1651 jp2->approx = 0; /* APPROX */
1653 jp2->jpip_on = parameters->jpip_on;
1656 OPJ_BOOL opj_jp2_encode(opj_jp2_t *jp2,
1657 opj_stream_private_t *stream,
1658 opj_event_mgr_t * p_manager)
1660 return opj_j2k_encode(jp2->j2k, stream, p_manager);
1663 OPJ_BOOL opj_jp2_end_decompress(opj_jp2_t *jp2,
1664 opj_stream_private_t *cio,
1665 opj_event_mgr_t * p_manager
1671 assert(p_manager != 00);
1673 /* customization of the end encoding */
1674 opj_jp2_setup_end_header_reading(jp2);
1677 if (! opj_jp2_exec (jp2,jp2->m_procedure_list,cio,p_manager)) {
1681 return opj_j2k_end_decompress(jp2->j2k, cio, p_manager);
1684 OPJ_BOOL opj_jp2_end_compress( opj_jp2_t *jp2,
1685 opj_stream_private_t *cio,
1686 opj_event_mgr_t * p_manager
1692 assert(p_manager != 00);
1694 /* customization of the end encoding */
1695 opj_jp2_setup_end_header_writing(jp2);
1697 if (! opj_j2k_end_compress(jp2->j2k,cio,p_manager)) {
1702 return opj_jp2_exec(jp2,jp2->m_procedure_list,cio,p_manager);
1705 void opj_jp2_setup_end_header_writing (opj_jp2_t *jp2)
1712 opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)opj_jpip_write_iptr );
1714 opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)opj_jp2_write_jp2c );
1715 /* DEVELOPER CORNER, add your custom procedures */
1719 opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)opj_jpip_write_cidx );
1720 opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)opj_jpip_write_fidx );
1725 void opj_jp2_setup_end_header_reading (opj_jp2_t *jp2)
1729 opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)opj_jp2_read_header_procedure );
1730 /* DEVELOPER CORNER, add your custom procedures */
1733 OPJ_BOOL opj_jp2_default_validation ( opj_jp2_t * jp2,
1734 opj_stream_private_t *cio,
1735 opj_event_mgr_t * p_manager
1738 OPJ_BOOL l_is_valid = OPJ_TRUE;
1744 assert(p_manager != 00);
1746 /* JPEG2000 codec validation */
1748 /* STATE checking */
1749 /* make sure the state is at 0 */
1750 l_is_valid &= (jp2->jp2_state == JP2_STATE_NONE);
1752 /* make sure not reading a jp2h ???? WEIRD */
1753 l_is_valid &= (jp2->jp2_img_state == JP2_IMG_STATE_NONE);
1755 /* POINTER validation */
1756 /* make sure a j2k codec is present */
1757 l_is_valid &= (jp2->j2k != 00);
1759 /* make sure a procedure list is present */
1760 l_is_valid &= (jp2->m_procedure_list != 00);
1762 /* make sure a validation list is present */
1763 l_is_valid &= (jp2->m_validation_list != 00);
1765 /* PARAMETER VALIDATION */
1766 /* number of components */
1767 l_is_valid &= (jp2->numcl > 0);
1769 l_is_valid &= (jp2->h > 0);
1771 l_is_valid &= (jp2->w > 0);
1773 for (i = 0; i < jp2->numcomps; ++i) {
1774 l_is_valid &= (jp2->comps[i].bpcc > 0);
1778 l_is_valid &= ((jp2->meth > 0) && (jp2->meth < 3));
1780 /* stream validation */
1781 /* back and forth is needed */
1782 l_is_valid &= opj_stream_has_seek(cio);
1787 OPJ_BOOL opj_jp2_read_header_procedure( opj_jp2_t *jp2,
1788 opj_stream_private_t *stream,
1789 opj_event_mgr_t * p_manager
1793 OPJ_UINT32 l_nb_bytes_read;
1794 const opj_jp2_header_handler_t * l_current_handler;
1795 OPJ_UINT32 l_last_data_size = OPJ_BOX_SIZE;
1796 OPJ_UINT32 l_current_data_size;
1797 OPJ_BYTE * l_current_data = 00;
1800 assert(stream != 00);
1802 assert(p_manager != 00);
1804 l_current_data = (OPJ_BYTE*)opj_malloc(l_last_data_size);
1806 if (l_current_data == 00) {
1807 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to handle jpeg2000 file header\n");
1810 memset(l_current_data, 0 , l_last_data_size);
1812 while (opj_jp2_read_boxhdr(&box,&l_nb_bytes_read,stream,p_manager)) {
1813 /* is it the codestream box ? */
1814 if (box.type == JP2_JP2C) {
1815 if (jp2->jp2_state & JP2_STATE_HEADER) {
1816 jp2->jp2_state |= JP2_STATE_CODESTREAM;
1817 opj_free(l_current_data);
1821 opj_event_msg(p_manager, EVT_ERROR, "bad placed jpeg codestream\n");
1822 opj_free(l_current_data);
1826 else if (box.length == 0) {
1827 opj_event_msg(p_manager, EVT_ERROR, "Cannot handle box of undefined sizes\n");
1828 opj_free(l_current_data);
1831 /* testcase 1851.pdf.SIGSEGV.ce9.948 */
1832 else if (box.length < l_nb_bytes_read) {
1833 opj_event_msg(p_manager, EVT_ERROR, "invalid box size %d (%x)\n", box.length, box.type);
1834 opj_free(l_current_data);
1838 l_current_handler = opj_jp2_find_handler(box.type);
1839 l_current_data_size = box.length - l_nb_bytes_read;
1841 if (l_current_handler != 00) {
1842 if (l_current_data_size > l_last_data_size) {
1843 OPJ_BYTE* new_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,l_current_data_size);
1844 if (!new_current_data) {
1845 opj_free(l_current_data);
1846 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to handle jpeg2000 box\n");
1849 l_current_data = new_current_data;
1850 l_last_data_size = l_current_data_size;
1853 l_nb_bytes_read = (OPJ_UINT32)opj_stream_read_data(stream,l_current_data,l_current_data_size,p_manager);
1854 if (l_nb_bytes_read != l_current_data_size) {
1855 opj_event_msg(p_manager, EVT_ERROR, "Problem with reading JPEG2000 box, stream error\n");
1856 opj_free(l_current_data);
1860 if (! l_current_handler->handler(jp2,l_current_data,l_current_data_size,p_manager)) {
1861 opj_free(l_current_data);
1866 jp2->jp2_state |= JP2_STATE_UNKNOWN;
1867 if (opj_stream_skip(stream,l_current_data_size,p_manager) != l_current_data_size) {
1868 opj_event_msg(p_manager, EVT_ERROR, "Problem with skipping JPEG2000 box, stream error\n");
1869 opj_free(l_current_data);
1875 opj_free(l_current_data);
1881 * Excutes the given procedures on the given codec.
1883 * @param p_procedure_list the list of procedures to execute
1884 * @param jp2 the jpeg2000 file codec to execute the procedures on.
1885 * @param stream the stream to execute the procedures on.
1886 * @param p_manager the user manager.
1888 * @return true if all the procedures were successfully executed.
1890 static OPJ_BOOL opj_jp2_exec ( opj_jp2_t * jp2,
1891 opj_procedure_list_t * p_procedure_list,
1892 opj_stream_private_t *stream,
1893 opj_event_mgr_t * p_manager
1897 OPJ_BOOL (** l_procedure) (opj_jp2_t * jp2, opj_stream_private_t *, opj_event_mgr_t *) = 00;
1898 OPJ_BOOL l_result = OPJ_TRUE;
1899 OPJ_UINT32 l_nb_proc, i;
1902 assert(p_procedure_list != 00);
1904 assert(stream != 00);
1905 assert(p_manager != 00);
1907 l_nb_proc = opj_procedure_list_get_nb_procedures(p_procedure_list);
1908 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);
1910 for (i=0;i<l_nb_proc;++i) {
1911 l_result = l_result && (*l_procedure) (jp2,stream,p_manager);
1915 /* and clear the procedure list at the end. */
1916 opj_procedure_list_clear(p_procedure_list);
1920 OPJ_BOOL opj_jp2_start_compress(opj_jp2_t *jp2,
1921 opj_stream_private_t *stream,
1922 opj_image_t * p_image,
1923 opj_event_mgr_t * p_manager
1928 assert(stream != 00);
1929 assert(p_manager != 00);
1931 /* customization of the validation */
1932 opj_jp2_setup_encoding_validation (jp2);
1934 /* validation of the parameters codec */
1935 if (! opj_jp2_exec(jp2,jp2->m_validation_list,stream,p_manager)) {
1939 /* customization of the encoding */
1940 opj_jp2_setup_header_writing(jp2);
1943 if (! opj_jp2_exec (jp2,jp2->m_procedure_list,stream,p_manager)) {
1947 return opj_j2k_start_compress(jp2->j2k,stream,p_image,p_manager);
1950 const opj_jp2_header_handler_t * opj_jp2_find_handler (OPJ_UINT32 p_id)
1952 OPJ_UINT32 i, l_handler_size = sizeof(jp2_header) / sizeof(opj_jp2_header_handler_t);
1954 for (i=0;i<l_handler_size;++i) {
1955 if (jp2_header[i].id == p_id) {
1956 return &jp2_header[i];
1963 * Finds the image execution function related to the given box id.
1965 * @param p_id the id of the handler to fetch.
1967 * @return the given handler or 00 if it could not be found.
1969 static const opj_jp2_header_handler_t * opj_jp2_img_find_handler (OPJ_UINT32 p_id)
1971 OPJ_UINT32 i, l_handler_size = sizeof(jp2_img_header) / sizeof(opj_jp2_header_handler_t);
1972 for (i=0;i<l_handler_size;++i)
1974 if (jp2_img_header[i].id == p_id) {
1975 return &jp2_img_header[i];
1983 * Reads a jpeg2000 file signature box.
1985 * @param p_header_data the data contained in the signature box.
1986 * @param jp2 the jpeg2000 file codec.
1987 * @param p_header_size the size of the data contained in the signature box.
1988 * @param p_manager the user event manager.
1990 * @return true if the file signature box is valid.
1992 static OPJ_BOOL opj_jp2_read_jp(opj_jp2_t *jp2,
1993 OPJ_BYTE * p_header_data,
1994 OPJ_UINT32 p_header_size,
1995 opj_event_mgr_t * p_manager
1999 OPJ_UINT32 l_magic_number;
2002 assert(p_header_data != 00);
2004 assert(p_manager != 00);
2006 if (jp2->jp2_state != JP2_STATE_NONE) {
2007 opj_event_msg(p_manager, EVT_ERROR, "The signature box must be the first box in the file.\n");
2011 /* assure length of data is correct (4 -> magic number) */
2012 if (p_header_size != 4) {
2013 opj_event_msg(p_manager, EVT_ERROR, "Error with JP signature Box size\n");
2017 /* rearrange data */
2018 opj_read_bytes(p_header_data,&l_magic_number,4);
2019 if (l_magic_number != 0x0d0a870a ) {
2020 opj_event_msg(p_manager, EVT_ERROR, "Error with JP Signature : bad magic number\n");
2024 jp2->jp2_state |= JP2_STATE_SIGNATURE;
2030 * Reads a a FTYP box - File type box
2032 * @param p_header_data the data contained in the FTYP box.
2033 * @param jp2 the jpeg2000 file codec.
2034 * @param p_header_size the size of the data contained in the FTYP box.
2035 * @param p_manager the user event manager.
2037 * @return true if the FTYP box is valid.
2039 static OPJ_BOOL opj_jp2_read_ftyp( opj_jp2_t *jp2,
2040 OPJ_BYTE * p_header_data,
2041 OPJ_UINT32 p_header_size,
2042 opj_event_mgr_t * p_manager
2045 OPJ_UINT32 i, l_remaining_bytes;
2048 assert(p_header_data != 00);
2050 assert(p_manager != 00);
2052 if (jp2->jp2_state != JP2_STATE_SIGNATURE) {
2053 opj_event_msg(p_manager, EVT_ERROR, "The ftyp box must be the second box in the file.\n");
2057 /* assure length of data is correct */
2058 if (p_header_size < 8) {
2059 opj_event_msg(p_manager, EVT_ERROR, "Error with FTYP signature Box size\n");
2063 opj_read_bytes(p_header_data,&jp2->brand,4); /* BR */
2066 opj_read_bytes(p_header_data,&jp2->minversion,4); /* MinV */
2069 l_remaining_bytes = p_header_size - 8;
2071 /* the number of remaining bytes should be a multiple of 4 */
2072 if ((l_remaining_bytes & 0x3) != 0) {
2073 opj_event_msg(p_manager, EVT_ERROR, "Error with FTYP signature Box size\n");
2078 jp2->numcl = l_remaining_bytes >> 2;
2080 jp2->cl = (OPJ_UINT32 *) opj_malloc(jp2->numcl * sizeof(OPJ_UINT32));
2081 if (jp2->cl == 00) {
2082 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory with FTYP Box\n");
2085 memset(jp2->cl,0,jp2->numcl * sizeof(OPJ_UINT32));
2088 for (i = 0; i < jp2->numcl; ++i)
2090 opj_read_bytes(p_header_data,&jp2->cl[i],4); /* CLi */
2094 jp2->jp2_state |= JP2_STATE_FILE_TYPE;
2099 OPJ_BOOL opj_jp2_skip_jp2c( opj_jp2_t *jp2,
2100 opj_stream_private_t *stream,
2101 opj_event_mgr_t * p_manager )
2105 assert(stream != 00);
2106 assert(p_manager != 00);
2108 jp2->j2k_codestream_offset = opj_stream_tell(stream);
2110 if (opj_stream_skip(stream,8,p_manager) != 8) {
2117 static OPJ_BOOL opj_jpip_skip_iptr( opj_jp2_t *jp2,
2118 opj_stream_private_t *stream,
2119 opj_event_mgr_t * p_manager )
2123 assert(stream != 00);
2124 assert(p_manager != 00);
2126 jp2->jpip_iptr_offset = opj_stream_tell(stream);
2128 if (opj_stream_skip(stream,24,p_manager) != 24) {
2136 * Reads the Jpeg2000 file Header box - JP2 Header box (warning, this is a super box).
2138 * @param p_header_data the data contained in the file header box.
2139 * @param jp2 the jpeg2000 file codec.
2140 * @param p_header_size the size of the data contained in the file header box.
2141 * @param p_manager the user event manager.
2143 * @return true if the JP2 Header box was successfully reconized.
2145 static OPJ_BOOL opj_jp2_read_jp2h( opj_jp2_t *jp2,
2146 OPJ_BYTE *p_header_data,
2147 OPJ_UINT32 p_header_size,
2148 opj_event_mgr_t * p_manager
2151 OPJ_UINT32 l_box_size=0, l_current_data_size = 0;
2153 const opj_jp2_header_handler_t * l_current_handler;
2156 assert(p_header_data != 00);
2158 assert(p_manager != 00);
2160 /* make sure the box is well placed */
2161 if ((jp2->jp2_state & JP2_STATE_FILE_TYPE) != JP2_STATE_FILE_TYPE ) {
2162 opj_event_msg(p_manager, EVT_ERROR, "The box must be the first box in the file.\n");
2166 jp2->jp2_img_state = JP2_IMG_STATE_NONE;
2168 /* iterate while remaining data */
2169 while (p_header_size > 0) {
2171 if (! opj_jp2_read_boxhdr_char(&box,p_header_data,&l_box_size,p_header_size, p_manager)) {
2172 opj_event_msg(p_manager, EVT_ERROR, "Stream error while reading JP2 Header box\n");
2176 if (box.length > p_header_size) {
2177 opj_event_msg(p_manager, EVT_ERROR, "Stream error while reading JP2 Header box: box length is inconsistent.\n");
2181 l_current_handler = opj_jp2_img_find_handler(box.type);
2182 l_current_data_size = box.length - l_box_size;
2183 p_header_data += l_box_size;
2185 if (l_current_handler != 00) {
2186 if (! l_current_handler->handler(jp2,p_header_data,l_current_data_size,p_manager)) {
2191 jp2->jp2_img_state |= JP2_IMG_STATE_UNKNOWN;
2194 p_header_data += l_current_data_size;
2195 p_header_size -= box.length;
2198 jp2->jp2_state |= JP2_STATE_HEADER;
2203 OPJ_BOOL opj_jp2_read_boxhdr_char( opj_jp2_box_t *box,
2205 OPJ_UINT32 * p_number_bytes_read,
2206 OPJ_UINT32 p_box_max_size,
2207 opj_event_mgr_t * p_manager
2213 assert(p_data != 00);
2215 assert(p_number_bytes_read != 00);
2216 assert(p_manager != 00);
2218 if (p_box_max_size < 8) {
2219 opj_event_msg(p_manager, EVT_ERROR, "Cannot handle box of less than 8 bytes\n");
2223 /* process read data */
2224 opj_read_bytes(p_data, &l_value, 4);
2226 box->length = (OPJ_INT32)(l_value);
2228 opj_read_bytes(p_data, &l_value, 4);
2230 box->type = (OPJ_INT32)(l_value);
2232 *p_number_bytes_read = 8;
2234 /* do we have a "special very large box ?" */
2235 /* read then the XLBox */
2236 if (box->length == 1) {
2237 OPJ_UINT32 l_xl_part_size;
2239 if (p_box_max_size < 16) {
2240 opj_event_msg(p_manager, EVT_ERROR, "Cannot handle XL box of less than 16 bytes\n");
2244 opj_read_bytes(p_data,&l_xl_part_size, 4);
2246 *p_number_bytes_read += 4;
2248 if (l_xl_part_size != 0) {
2249 opj_event_msg(p_manager, EVT_ERROR, "Cannot handle box sizes higher than 2^32\n");
2253 opj_read_bytes(p_data, &l_value, 4);
2254 *p_number_bytes_read += 4;
2255 box->length = (OPJ_INT32)(l_value);
2257 if (box->length == 0) {
2258 opj_event_msg(p_manager, EVT_ERROR, "Cannot handle box of undefined sizes\n");
2262 else if (box->length == 0) {
2263 opj_event_msg(p_manager, EVT_ERROR, "Cannot handle box of undefined sizes\n");
2270 OPJ_BOOL opj_jp2_read_header( opj_stream_private_t *p_stream,
2272 opj_image_t ** p_image,
2273 opj_event_mgr_t * p_manager
2278 assert(p_stream != 00);
2279 assert(p_manager != 00);
2281 /* customization of the validation */
2282 opj_jp2_setup_decoding_validation (jp2);
2284 /* customization of the encoding */
2285 opj_jp2_setup_header_reading(jp2);
2287 /* validation of the parameters codec */
2288 if (! opj_jp2_exec(jp2,jp2->m_validation_list,p_stream,p_manager)) {
2293 if (! opj_jp2_exec (jp2,jp2->m_procedure_list,p_stream,p_manager)) {
2297 return opj_j2k_read_header( p_stream,
2303 void opj_jp2_setup_encoding_validation (opj_jp2_t *jp2)
2308 opj_procedure_list_add_procedure(jp2->m_validation_list, (opj_procedure)opj_jp2_default_validation);
2309 /* DEVELOPER CORNER, add your custom validation procedure */
2312 void opj_jp2_setup_decoding_validation (opj_jp2_t *jp2)
2316 /* DEVELOPER CORNER, add your custom validation procedure */
2319 void opj_jp2_setup_header_writing (opj_jp2_t *jp2)
2324 opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)opj_jp2_write_jp );
2325 opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)opj_jp2_write_ftyp );
2326 opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)opj_jp2_write_jp2h );
2328 opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)opj_jpip_skip_iptr );
2329 opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)opj_jp2_skip_jp2c );
2331 /* DEVELOPER CORNER, insert your custom procedures */
2335 void opj_jp2_setup_header_reading (opj_jp2_t *jp2)
2340 opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)opj_jp2_read_header_procedure );
2341 /* DEVELOPER CORNER, add your custom procedures */
2344 OPJ_BOOL opj_jp2_read_tile_header ( opj_jp2_t * p_jp2,
2345 OPJ_UINT32 * p_tile_index,
2346 OPJ_UINT32 * p_data_size,
2347 OPJ_INT32 * p_tile_x0,
2348 OPJ_INT32 * p_tile_y0,
2349 OPJ_INT32 * p_tile_x1,
2350 OPJ_INT32 * p_tile_y1,
2351 OPJ_UINT32 * p_nb_comps,
2353 opj_stream_private_t *p_stream,
2354 opj_event_mgr_t * p_manager
2357 return opj_j2k_read_tile_header(p_jp2->j2k,
2360 p_tile_x0, p_tile_y0,
2361 p_tile_x1, p_tile_y1,
2368 OPJ_BOOL opj_jp2_write_tile ( opj_jp2_t *p_jp2,
2369 OPJ_UINT32 p_tile_index,
2371 OPJ_UINT32 p_data_size,
2372 opj_stream_private_t *p_stream,
2373 opj_event_mgr_t * p_manager
2377 return opj_j2k_write_tile (p_jp2->j2k,p_tile_index,p_data,p_data_size,p_stream,p_manager);
2380 OPJ_BOOL opj_jp2_decode_tile ( opj_jp2_t * p_jp2,
2381 OPJ_UINT32 p_tile_index,
2383 OPJ_UINT32 p_data_size,
2384 opj_stream_private_t *p_stream,
2385 opj_event_mgr_t * p_manager
2388 return opj_j2k_decode_tile (p_jp2->j2k,p_tile_index,p_data,p_data_size,p_stream,p_manager);
2391 void opj_jp2_destroy(opj_jp2_t *jp2)
2394 /* destroy the J2K codec */
2395 opj_j2k_destroy(jp2->j2k);
2399 opj_free(jp2->comps);
2408 if (jp2->color.icc_profile_buf) {
2409 opj_free(jp2->color.icc_profile_buf);
2410 jp2->color.icc_profile_buf = 00;
2413 if (jp2->color.jp2_cdef) {
2414 if (jp2->color.jp2_cdef->info) {
2415 opj_free(jp2->color.jp2_cdef->info);
2416 jp2->color.jp2_cdef->info = NULL;
2419 opj_free(jp2->color.jp2_cdef);
2420 jp2->color.jp2_cdef = 00;
2423 if (jp2->color.jp2_pclr) {
2424 if (jp2->color.jp2_pclr->cmap) {
2425 opj_free(jp2->color.jp2_pclr->cmap);
2426 jp2->color.jp2_pclr->cmap = NULL;
2428 if (jp2->color.jp2_pclr->channel_sign) {
2429 opj_free(jp2->color.jp2_pclr->channel_sign);
2430 jp2->color.jp2_pclr->channel_sign = NULL;
2432 if (jp2->color.jp2_pclr->channel_size) {
2433 opj_free(jp2->color.jp2_pclr->channel_size);
2434 jp2->color.jp2_pclr->channel_size = NULL;
2436 if (jp2->color.jp2_pclr->entries) {
2437 opj_free(jp2->color.jp2_pclr->entries);
2438 jp2->color.jp2_pclr->entries = NULL;
2441 opj_free(jp2->color.jp2_pclr);
2442 jp2->color.jp2_pclr = 00;
2445 if (jp2->m_validation_list) {
2446 opj_procedure_list_destroy(jp2->m_validation_list);
2447 jp2->m_validation_list = 00;
2450 if (jp2->m_procedure_list) {
2451 opj_procedure_list_destroy(jp2->m_procedure_list);
2452 jp2->m_procedure_list = 00;
2459 OPJ_BOOL opj_jp2_set_decode_area( opj_jp2_t *p_jp2,
2460 opj_image_t* p_image,
2461 OPJ_INT32 p_start_x, OPJ_INT32 p_start_y,
2462 OPJ_INT32 p_end_x, OPJ_INT32 p_end_y,
2463 opj_event_mgr_t * p_manager
2466 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);
2469 OPJ_BOOL opj_jp2_get_tile( opj_jp2_t *p_jp2,
2470 opj_stream_private_t *p_stream,
2471 opj_image_t* p_image,
2472 opj_event_mgr_t * p_manager,
2473 OPJ_UINT32 tile_index
2479 opj_event_msg(p_manager, EVT_WARNING, "JP2 box which are after the codestream will not be read by this function.\n");
2481 if (! opj_j2k_get_tile(p_jp2->j2k, p_stream, p_image, p_manager, tile_index) ){
2482 opj_event_msg(p_manager, EVT_ERROR, "Failed to decode the codestream in the JP2 file\n");
2486 if (!opj_jp2_check_color(p_image, &(p_jp2->color), p_manager)) {
2490 /* Set Image Color Space */
2491 if (p_jp2->enumcs == 16)
2492 p_image->color_space = OPJ_CLRSPC_SRGB;
2493 else if (p_jp2->enumcs == 17)
2494 p_image->color_space = OPJ_CLRSPC_GRAY;
2495 else if (p_jp2->enumcs == 18)
2496 p_image->color_space = OPJ_CLRSPC_SYCC;
2498 p_image->color_space = OPJ_CLRSPC_UNKNOWN;
2500 /* Apply the color space if needed */
2501 if(p_jp2->color.jp2_cdef) {
2502 opj_jp2_apply_cdef(p_image, &(p_jp2->color));
2505 if(p_jp2->color.jp2_pclr) {
2506 /* Part 1, I.5.3.4: Either both or none : */
2507 if( !p_jp2->color.jp2_pclr->cmap)
2508 opj_jp2_free_pclr(&(p_jp2->color));
2510 opj_jp2_apply_pclr(p_image, &(p_jp2->color));
2513 if(p_jp2->color.icc_profile_buf) {
2514 p_image->icc_profile_buf = p_jp2->color.icc_profile_buf;
2515 p_image->icc_profile_len = p_jp2->color.icc_profile_len;
2516 p_jp2->color.icc_profile_buf = NULL;
2522 /* ----------------------------------------------------------------------- */
2523 /* JP2 encoder interface */
2524 /* ----------------------------------------------------------------------- */
2526 opj_jp2_t* opj_jp2_create(OPJ_BOOL p_is_decoder)
2528 opj_jp2_t *jp2 = (opj_jp2_t*)opj_malloc(sizeof(opj_jp2_t));
2530 memset(jp2,0,sizeof(opj_jp2_t));
2532 /* create the J2K codec */
2533 if (! p_is_decoder) {
2534 jp2->j2k = opj_j2k_create_compress();
2537 jp2->j2k = opj_j2k_create_decompress();
2540 if (jp2->j2k == 00) {
2541 opj_jp2_destroy(jp2);
2545 /* Color structure */
2546 jp2->color.icc_profile_buf = NULL;
2547 jp2->color.icc_profile_len = 0;
2548 jp2->color.jp2_cdef = NULL;
2549 jp2->color.jp2_pclr = NULL;
2550 jp2->color.jp2_has_colr = 0;
2552 /* validation list creation */
2553 jp2->m_validation_list = opj_procedure_list_create();
2554 if (! jp2->m_validation_list) {
2555 opj_jp2_destroy(jp2);
2559 /* execution list creation */
2560 jp2->m_procedure_list = opj_procedure_list_create();
2561 if (! jp2->m_procedure_list) {
2562 opj_jp2_destroy(jp2);
2570 void jp2_dump(opj_jp2_t* p_jp2, OPJ_INT32 flag, FILE* out_stream)
2573 assert(p_jp2 != 00);
2575 j2k_dump(p_jp2->j2k,
2580 opj_codestream_index_t* jp2_get_cstr_index(opj_jp2_t* p_jp2)
2582 return j2k_get_cstr_index(p_jp2->j2k);
2585 opj_codestream_info_v2_t* jp2_get_cstr_info(opj_jp2_t* p_jp2)
2587 return j2k_get_cstr_info(p_jp2->j2k);
2590 OPJ_BOOL opj_jp2_set_decoded_resolution_factor(opj_jp2_t *p_jp2,
2591 OPJ_UINT32 res_factor,
2592 opj_event_mgr_t * p_manager)
2594 return opj_j2k_set_decoded_resolution_factor(p_jp2->j2k, res_factor, p_manager);
2600 static OPJ_BOOL opj_jpip_write_iptr(opj_jp2_t *jp2,
2601 opj_stream_private_t *cio,
2602 opj_event_mgr_t * p_manager )
2604 OPJ_OFF_T j2k_codestream_exit;
2605 OPJ_BYTE l_data_header [24];
2610 assert(p_manager != 00);
2611 assert(opj_stream_has_seek(cio));
2613 j2k_codestream_exit = opj_stream_tell(cio);
2614 opj_write_bytes(l_data_header, 24, 4); /* size of iptr */
2615 opj_write_bytes(l_data_header + 4,JPIP_IPTR,4); /* IPTR */
2617 opj_write_bytes(l_data_header + 4 + 4, 0, 8); /* offset */
2618 opj_write_bytes(l_data_header + 8 + 8, 0, 8); /* length */
2620 opj_write_double(l_data_header + 4 + 4, 0); /* offset */
2621 opj_write_double(l_data_header + 8 + 8, 0); /* length */
2624 if (! opj_stream_seek(cio,jp2->jpip_iptr_offset,p_manager)) {
2625 opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
2629 if (opj_stream_write_data(cio,l_data_header,24,p_manager) != 24) {
2630 opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
2634 if (! opj_stream_seek(cio,j2k_codestream_exit,p_manager)) {
2635 opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
2642 static OPJ_BOOL opj_jpip_write_fidx(opj_jp2_t *jp2,
2643 opj_stream_private_t *cio,
2644 opj_event_mgr_t * p_manager )
2646 OPJ_OFF_T j2k_codestream_exit;
2647 OPJ_BYTE l_data_header [24];
2652 assert(p_manager != 00);
2653 assert(opj_stream_has_seek(cio));
2655 opj_write_bytes(l_data_header, 24, 4); /* size of iptr */
2656 opj_write_bytes(l_data_header + 4,JPIP_FIDX,4); /* IPTR */
2657 opj_write_double(l_data_header + 4 + 4, 0); /* offset */
2658 opj_write_double(l_data_header + 8 + 8, 0); /* length */
2660 if (opj_stream_write_data(cio,l_data_header,24,p_manager) != 24) {
2661 opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
2665 j2k_codestream_exit = opj_stream_tell(cio);
2666 if (! opj_stream_seek(cio,j2k_codestream_exit,p_manager)) {
2667 opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
2674 static OPJ_BOOL opj_jpip_write_cidx(opj_jp2_t *jp2,
2675 opj_stream_private_t *cio,
2676 opj_event_mgr_t * p_manager )
2678 OPJ_OFF_T j2k_codestream_exit;
2679 OPJ_BYTE l_data_header [24];
2684 assert(p_manager != 00);
2685 assert(opj_stream_has_seek(cio));
2687 j2k_codestream_exit = opj_stream_tell(cio);
2688 opj_write_bytes(l_data_header, 24, 4); /* size of iptr */
2689 opj_write_bytes(l_data_header + 4,JPIP_CIDX,4); /* IPTR */
2691 opj_write_bytes(l_data_header + 4 + 4, 0, 8); /* offset */
2692 opj_write_bytes(l_data_header + 8 + 8, 0, 8); /* length */
2694 opj_write_double(l_data_header + 4 + 4, 0); /* offset */
2695 opj_write_double(l_data_header + 8 + 8, 0); /* length */
2698 if (! opj_stream_seek(cio,j2k_codestream_exit,p_manager)) {
2699 opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
2703 if (opj_stream_write_data(cio,l_data_header,24,p_manager) != 24) {
2704 opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
2708 j2k_codestream_exit = opj_stream_tell(cio);
2709 if (! opj_stream_seek(cio,j2k_codestream_exit,p_manager)) {
2710 opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
2718 static void write_prxy( int offset_jp2c, int length_jp2c, int offset_idx, int length_idx, opj_stream_private_t *cio,
2719 opj_event_mgr_t * p_manager )
2721 OPJ_BYTE l_data_header [8];
2722 OPJ_OFF_T len, lenp;
2724 lenp = opj_stream_tell(cio);
2725 opj_stream_skip(cio, 4, p_manager); /* L [at the end] */
2726 opj_write_bytes(l_data_header,JPIP_PRXY,4); /* IPTR */
2727 opj_stream_write_data(cio,l_data_header,4,p_manager);
2729 opj_write_bytes( l_data_header, offset_jp2c, 8); /* OOFF */
2730 opj_stream_write_data(cio,l_data_header,8,p_manager);
2731 opj_write_bytes( l_data_header, length_jp2c, 4); /* OBH part 1 */
2732 opj_write_bytes( l_data_header+4, JP2_JP2C, 4); /* OBH part 2 */
2733 opj_stream_write_data(cio,l_data_header,8,p_manager);
2735 opj_write_bytes( l_data_header, 1, 1);/* NI */
2736 opj_stream_write_data(cio,l_data_header,1,p_manager);
2738 opj_write_bytes( l_data_header, offset_idx, 8); /* IOFF */
2739 opj_stream_write_data(cio,l_data_header,8,p_manager);
2740 opj_write_bytes( l_data_header, length_idx, 4); /* IBH part 1 */
2741 opj_write_bytes( l_data_header+4, JPIP_CIDX, 4); /* IBH part 2 */
2742 opj_stream_write_data(cio,l_data_header,8,p_manager);
2744 len = opj_stream_tell(cio)-lenp;
2745 opj_stream_skip(cio, lenp, p_manager);
2746 opj_write_bytes(l_data_header,len,4);/* L */
2747 opj_stream_write_data(cio,l_data_header,4,p_manager);
2748 opj_stream_seek(cio, lenp+len,p_manager);
2754 static int write_fidx( int offset_jp2c, int length_jp2c, int offset_idx, int length_idx, opj_stream_private_t *cio,
2755 opj_event_mgr_t * p_manager )
2757 OPJ_BYTE l_data_header [4];
2758 OPJ_OFF_T len, lenp;
2760 lenp = opj_stream_tell(cio);
2761 opj_stream_skip(cio, 4, p_manager);
2762 opj_write_bytes(l_data_header,JPIP_FIDX,4); /* FIDX */
2763 opj_stream_write_data(cio,l_data_header,4,p_manager);
2765 write_prxy( offset_jp2c, length_jp2c, offset_idx, length_idx, cio,p_manager);
2767 len = opj_stream_tell(cio)-lenp;
2768 opj_stream_skip(cio, lenp, p_manager);
2769 opj_write_bytes(l_data_header,len,4);/* L */
2770 opj_stream_write_data(cio,l_data_header,4,p_manager);
2771 opj_stream_seek(cio, lenp+len,p_manager);
2776 #endif /* USE_JPIP */