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 Channel Definition 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_cdef( opj_jp2_t *jp2,
120 OPJ_UINT32 * p_nb_bytes_written );
123 * Writes the Colour Specification box.
125 * @param jp2 jpeg2000 file codec.
126 * @param p_nb_bytes_written pointer to store the nb of bytes written by the function.
128 * @return the data being copied.
130 static OPJ_BYTE * opj_jp2_write_colr( opj_jp2_t *jp2,
131 OPJ_UINT32 * p_nb_bytes_written );
134 * Writes a FTYP box - File type box
136 * @param cio the stream to write data to.
137 * @param jp2 the jpeg2000 file codec.
138 * @param p_manager the user event manager.
140 * @return true if writing was successful.
142 static OPJ_BOOL opj_jp2_write_ftyp( opj_jp2_t *jp2,
143 opj_stream_private_t *cio,
144 opj_event_mgr_t * p_manager );
147 * Reads a a FTYP box - File type box
149 * @param p_header_data the data contained in the FTYP box.
150 * @param jp2 the jpeg2000 file codec.
151 * @param p_header_size the size of the data contained in the FTYP box.
152 * @param p_manager the user event manager.
154 * @return true if the FTYP box is valid.
156 static OPJ_BOOL opj_jp2_read_ftyp( opj_jp2_t *jp2,
157 OPJ_BYTE * p_header_data,
158 OPJ_UINT32 p_header_size,
159 opj_event_mgr_t * p_manager );
161 OPJ_BOOL opj_jp2_skip_jp2c( opj_jp2_t *jp2,
162 opj_stream_private_t *cio,
163 opj_event_mgr_t * p_manager );
166 * Reads the Jpeg2000 file Header box - JP2 Header box (warning, this is a super box).
168 * @param p_header_data the data contained in the file header box.
169 * @param jp2 the jpeg2000 file codec.
170 * @param p_header_size the size of the data contained in the file header box.
171 * @param p_manager the user event manager.
173 * @return true if the JP2 Header box was successfully reconized.
175 static OPJ_BOOL opj_jp2_read_jp2h( opj_jp2_t *jp2,
176 OPJ_BYTE *p_header_data,
177 OPJ_UINT32 p_header_size,
178 opj_event_mgr_t * p_manager );
181 * Writes the Jpeg2000 codestream Header box - JP2C Header box. This function must be called AFTER the coding has been done.
183 * @param cio the stream to write data to.
184 * @param jp2 the jpeg2000 file codec.
185 * @param p_manager user event manager.
187 * @return true if writing was successful.
189 static OPJ_BOOL opj_jp2_write_jp2c( opj_jp2_t *jp2,
190 opj_stream_private_t *cio,
191 opj_event_mgr_t * p_manager );
195 * Write index Finder box
196 * @param cio the stream to write to.
197 * @param jp2 the jpeg2000 file codec.
198 * @param p_manager user event manager.
200 static OPJ_BOOL opj_jpip_write_iptr( opj_jp2_t *jp2,
201 opj_stream_private_t *cio,
202 opj_event_mgr_t * p_manager );
205 * Write index Finder box
206 * @param cio the stream to write to.
207 * @param jp2 the jpeg2000 file codec.
208 * @param p_manager user event manager.
210 static OPJ_BOOL opj_jpip_write_cidx(opj_jp2_t *jp2,
211 opj_stream_private_t *cio,
212 opj_event_mgr_t * p_manager );
215 * Write file Index (superbox)
216 * @param cio the stream to write to.
217 * @param jp2 the jpeg2000 file codec.
218 * @param p_manager user event manager.
220 static OPJ_BOOL opj_jpip_write_fidx(opj_jp2_t *jp2,
221 opj_stream_private_t *cio,
222 opj_event_mgr_t * p_manager );
223 #endif /* USE_JPIP */
226 * Reads a jpeg2000 file signature box.
228 * @param p_header_data the data contained in the signature box.
229 * @param jp2 the jpeg2000 file codec.
230 * @param p_header_size the size of the data contained in the signature box.
231 * @param p_manager the user event manager.
233 * @return true if the file signature box is valid.
235 static OPJ_BOOL opj_jp2_read_jp(opj_jp2_t *jp2,
236 OPJ_BYTE * p_header_data,
237 OPJ_UINT32 p_header_size,
238 opj_event_mgr_t * p_manager);
241 * Writes a jpeg2000 file signature box.
243 * @param cio the stream to write data to.
244 * @param jp2 the jpeg2000 file codec.
245 * @param p_manager the user event manager.
247 * @return true if writing was successful.
249 static OPJ_BOOL opj_jp2_write_jp( opj_jp2_t *jp2,
250 opj_stream_private_t *cio,
251 opj_event_mgr_t * p_manager );
254 Apply collected palette data
255 @param color Collector for profile, cdef and pclr data
258 static void opj_jp2_apply_pclr(opj_image_t *image, opj_jp2_color_t *color);
260 static void opj_jp2_free_pclr(opj_jp2_color_t *color);
263 * Collect palette data
265 * @param jp2 JP2 handle
266 * @param p_pclr_header_data FIXME DOC
267 * @param p_pclr_header_size FIXME DOC
270 * @return Returns true if successful, returns false otherwise
272 static OPJ_BOOL opj_jp2_read_pclr( opj_jp2_t *jp2,
273 OPJ_BYTE * p_pclr_header_data,
274 OPJ_UINT32 p_pclr_header_size,
275 opj_event_mgr_t * p_manager );
278 * Collect component mapping data
280 * @param jp2 JP2 handle
281 * @param p_cmap_header_data FIXME DOC
282 * @param p_cmap_header_size FIXME DOC
283 * @param p_manager FIXME DOC
285 * @return Returns true if successful, returns false otherwise
288 static OPJ_BOOL opj_jp2_read_cmap( opj_jp2_t * jp2,
289 OPJ_BYTE * p_cmap_header_data,
290 OPJ_UINT32 p_cmap_header_size,
291 opj_event_mgr_t * p_manager );
294 * Reads the Color Specification box.
296 * @param p_colr_header_data pointer to actual data (already read from file)
297 * @param jp2 the jpeg2000 file codec.
298 * @param p_colr_header_size the size of the color header
299 * @param p_manager the user event manager.
301 * @return true if the bpc header is valid, fale else.
303 static OPJ_BOOL opj_jp2_read_colr( opj_jp2_t *jp2,
304 OPJ_BYTE * p_colr_header_data,
305 OPJ_UINT32 p_colr_header_size,
306 opj_event_mgr_t * p_manager );
313 * Sets up the procedures to do on writing header after the codestream.
314 * Developpers wanting to extend the library can add their own writing procedures.
316 static void opj_jp2_setup_end_header_writing (opj_jp2_t *jp2);
319 * Sets up the procedures to do on reading header after the codestream.
320 * Developpers wanting to extend the library can add their own writing procedures.
322 static void opj_jp2_setup_end_header_reading (opj_jp2_t *jp2);
325 * Reads a jpeg2000 file header structure.
327 * @param jp2 the jpeg2000 file header structure.
328 * @param stream the stream to read data from.
329 * @param p_manager the user event manager.
331 * @return true if the box is valid.
333 static OPJ_BOOL opj_jp2_read_header_procedure( opj_jp2_t *jp2,
334 opj_stream_private_t *stream,
335 opj_event_mgr_t * p_manager );
338 * Excutes the given procedures on the given codec.
340 * @param p_procedure_list the list of procedures to execute
341 * @param jp2 the jpeg2000 file codec to execute the procedures on.
342 * @param stream the stream to execute the procedures on.
343 * @param p_manager the user manager.
345 * @return true if all the procedures were successfully executed.
347 static OPJ_BOOL opj_jp2_exec ( opj_jp2_t * jp2,
348 opj_procedure_list_t * p_procedure_list,
349 opj_stream_private_t *stream,
350 opj_event_mgr_t * p_manager );
353 * Reads a box header. The box is the way data is packed inside a jpeg2000 file structure.
355 * @param cio the input stream to read data from.
356 * @param box the box structure to fill.
357 * @param p_number_bytes_read pointer to an int that will store the number of bytes read from the stream (shoul usually be 2).
358 * @param p_manager user event manager.
360 * @return true if the box is reconized, false otherwise
362 static OPJ_BOOL opj_jp2_read_boxhdr(opj_jp2_box_t *box,
363 OPJ_UINT32 * p_number_bytes_read,
364 opj_stream_private_t *cio,
365 opj_event_mgr_t * p_manager);
368 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
369 * are valid. Developpers wanting to extend the library can add their own validation procedures.
371 static void opj_jp2_setup_encoding_validation (opj_jp2_t *jp2);
374 * Sets up the procedures to do on writing header. Developpers wanting to extend the library can add their own writing procedures.
376 static void opj_jp2_setup_header_writing (opj_jp2_t *jp2);
378 OPJ_BOOL opj_jp2_default_validation ( opj_jp2_t * jp2,
379 opj_stream_private_t *cio,
380 opj_event_mgr_t * p_manager );
383 * Finds the image execution function related to the given box id.
385 * @param p_id the id of the handler to fetch.
387 * @return the given handler or NULL if it could not be found.
389 static const opj_jp2_header_handler_t * opj_jp2_img_find_handler (OPJ_UINT32 p_id);
392 * Finds the execution function related to the given box id.
394 * @param p_id the id of the handler to fetch.
396 * @return the given handler or NULL if it could not be found.
398 static const opj_jp2_header_handler_t * opj_jp2_find_handler (OPJ_UINT32 p_id );
400 const opj_jp2_header_handler_t jp2_header [] =
402 {JP2_JP,opj_jp2_read_jp},
403 {JP2_FTYP,opj_jp2_read_ftyp},
404 {JP2_JP2H,opj_jp2_read_jp2h}
407 const opj_jp2_header_handler_t jp2_img_header [] =
409 {JP2_IHDR,opj_jp2_read_ihdr},
410 {JP2_COLR,opj_jp2_read_colr},
411 {JP2_BPCC,opj_jp2_read_bpcc},
412 {JP2_PCLR,opj_jp2_read_pclr},
413 {JP2_CMAP,opj_jp2_read_cmap},
414 {JP2_CDEF,opj_jp2_read_cdef}
419 * Reads a box header. The box is the way data is packed inside a jpeg2000 file structure. Data is read from a character string
421 * @param box the box structure to fill.
422 * @param p_data the character string to read data from.
423 * @param p_number_bytes_read pointer to an int that will store the number of bytes read from the stream (shoul usually be 2).
424 * @param p_box_max_size the maximum number of bytes in the box.
425 * @param p_manager FIXME DOC
427 * @return true if the box is reconized, false otherwise
429 static OPJ_BOOL opj_jp2_read_boxhdr_char( opj_jp2_box_t *box,
431 OPJ_UINT32 * p_number_bytes_read,
432 OPJ_UINT32 p_box_max_size,
433 opj_event_mgr_t * p_manager );
436 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
437 * are valid. Developpers wanting to extend the library can add their own validation procedures.
439 static void opj_jp2_setup_decoding_validation (opj_jp2_t *jp2);
442 * Sets up the procedures to do on reading header.
443 * Developpers wanting to extend the library can add their own writing procedures.
445 static void opj_jp2_setup_header_reading (opj_jp2_t *jp2);
447 /* ----------------------------------------------------------------------- */
448 OPJ_BOOL opj_jp2_read_boxhdr(opj_jp2_box_t *box,
449 OPJ_UINT32 * p_number_bytes_read,
450 opj_stream_private_t *cio,
451 opj_event_mgr_t * p_manager )
453 /* read header from file */
454 OPJ_BYTE l_data_header [8];
459 assert(p_number_bytes_read != 00);
460 assert(p_manager != 00);
462 *p_number_bytes_read = (OPJ_UINT32)opj_stream_read_data(cio,l_data_header,8,p_manager);
463 if (*p_number_bytes_read != 8) {
467 /* process read data */
468 opj_read_bytes(l_data_header,&(box->length), 4);
469 opj_read_bytes(l_data_header+4,&(box->type), 4);
471 if(box->length == 0)/* last box */
473 const OPJ_OFF_T bleft = opj_stream_get_number_byte_left(cio);
474 box->length = (OPJ_UINT32)bleft;
475 assert( (OPJ_OFF_T)box->length == bleft );
479 /* do we have a "special very large box ?" */
480 /* read then the XLBox */
481 if (box->length == 1) {
482 OPJ_UINT32 l_xl_part_size;
484 OPJ_UINT32 l_nb_bytes_read = (OPJ_UINT32)opj_stream_read_data(cio,l_data_header,8,p_manager);
485 if (l_nb_bytes_read != 8) {
486 if (l_nb_bytes_read > 0) {
487 *p_number_bytes_read += l_nb_bytes_read;
493 *p_number_bytes_read = 16;
494 opj_read_bytes(l_data_header,&l_xl_part_size, 4);
495 if (l_xl_part_size != 0) {
496 opj_event_msg(p_manager, EVT_ERROR, "Cannot handle box sizes higher than 2^32\n");
499 opj_read_bytes(l_data_header+4,&(box->length), 4);
505 static void jp2_write_url(opj_cio_t *cio, char *Idx_file) {
509 box.init_pos = cio_tell(cio);
511 cio_write(cio, JP2_URL, 4); /* DBTL */
512 cio_write(cio, 0, 1); /* VERS */
513 cio_write(cio, 0, 3); /* FLAG */
516 for (i = 0; i < strlen(Idx_file); i++) {
517 cio_write(cio, Idx_file[i], 1);
521 box.length = cio_tell(cio) - box.init_pos;
522 cio_seek(cio, box.init_pos);
523 cio_write(cio, box.length, 4); /* L */
524 cio_seek(cio, box.init_pos + box.length);
528 OPJ_BOOL opj_jp2_read_ihdr( opj_jp2_t *jp2,
529 OPJ_BYTE *p_image_header_data,
530 OPJ_UINT32 p_image_header_size,
531 opj_event_mgr_t * p_manager )
534 assert(p_image_header_data != 00);
536 assert(p_manager != 00);
538 if (p_image_header_size != 14) {
539 opj_event_msg(p_manager, EVT_ERROR, "Bad image header box (bad size)\n");
543 opj_read_bytes(p_image_header_data,&(jp2->h),4); /* HEIGHT */
544 p_image_header_data += 4;
545 opj_read_bytes(p_image_header_data,&(jp2->w),4); /* WIDTH */
546 p_image_header_data += 4;
547 opj_read_bytes(p_image_header_data,&(jp2->numcomps),2); /* NC */
548 p_image_header_data += 2;
550 /* allocate memory for components */
551 jp2->comps = (opj_jp2_comps_t*) opj_calloc(jp2->numcomps, sizeof(opj_jp2_comps_t));
552 if (jp2->comps == 0) {
553 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to handle image header (ihdr)\n");
557 opj_read_bytes(p_image_header_data,&(jp2->bpc),1); /* BPC */
558 ++ p_image_header_data;
560 opj_read_bytes(p_image_header_data,&(jp2->C),1); /* C */
561 ++ p_image_header_data;
563 /* Should be equal to 7 cf. chapter about image header box of the norm */
565 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);
568 opj_read_bytes(p_image_header_data,&(jp2->UnkC),1); /* UnkC */
569 ++ p_image_header_data;
570 opj_read_bytes(p_image_header_data,&(jp2->IPR),1); /* IPR */
571 ++ p_image_header_data;
576 OPJ_BYTE * opj_jp2_write_ihdr(opj_jp2_t *jp2,
577 OPJ_UINT32 * p_nb_bytes_written
580 OPJ_BYTE * l_ihdr_data,* l_current_ihdr_ptr;
584 assert(p_nb_bytes_written != 00);
586 /* default image header is 22 bytes wide */
587 l_ihdr_data = (OPJ_BYTE *) opj_calloc(1,22);
588 if (l_ihdr_data == 00) {
592 l_current_ihdr_ptr = l_ihdr_data;
594 opj_write_bytes(l_current_ihdr_ptr,22,4); /* write box size */
595 l_current_ihdr_ptr+=4;
597 opj_write_bytes(l_current_ihdr_ptr,JP2_IHDR, 4); /* IHDR */
598 l_current_ihdr_ptr+=4;
600 opj_write_bytes(l_current_ihdr_ptr,jp2->h, 4); /* HEIGHT */
601 l_current_ihdr_ptr+=4;
603 opj_write_bytes(l_current_ihdr_ptr, jp2->w, 4); /* WIDTH */
604 l_current_ihdr_ptr+=4;
606 opj_write_bytes(l_current_ihdr_ptr, jp2->numcomps, 2); /* NC */
607 l_current_ihdr_ptr+=2;
609 opj_write_bytes(l_current_ihdr_ptr, jp2->bpc, 1); /* BPC */
610 ++l_current_ihdr_ptr;
612 opj_write_bytes(l_current_ihdr_ptr, jp2->C, 1); /* C : Always 7 */
613 ++l_current_ihdr_ptr;
615 opj_write_bytes(l_current_ihdr_ptr, jp2->UnkC, 1); /* UnkC, colorspace unknown */
616 ++l_current_ihdr_ptr;
618 opj_write_bytes(l_current_ihdr_ptr, jp2->IPR, 1); /* IPR, no intellectual property */
619 ++l_current_ihdr_ptr;
621 *p_nb_bytes_written = 22;
626 OPJ_BYTE * opj_jp2_write_bpcc( opj_jp2_t *jp2,
627 OPJ_UINT32 * p_nb_bytes_written
631 /* room for 8 bytes for box and 1 byte for each component */
632 OPJ_UINT32 l_bpcc_size = 8 + jp2->numcomps;
633 OPJ_BYTE * l_bpcc_data,* l_current_bpcc_ptr;
637 assert(p_nb_bytes_written != 00);
639 l_bpcc_data = (OPJ_BYTE *) opj_calloc(1,l_bpcc_size);
640 if (l_bpcc_data == 00) {
644 l_current_bpcc_ptr = l_bpcc_data;
646 opj_write_bytes(l_current_bpcc_ptr,l_bpcc_size,4); /* write box size */
647 l_current_bpcc_ptr += 4;
649 opj_write_bytes(l_current_bpcc_ptr,JP2_BPCC,4); /* BPCC */
650 l_current_bpcc_ptr += 4;
652 for (i = 0; i < jp2->numcomps; ++i) {
653 opj_write_bytes(l_current_bpcc_ptr, jp2->comps[i].bpcc, 1); /* write each component information */
654 ++l_current_bpcc_ptr;
657 *p_nb_bytes_written = l_bpcc_size;
662 OPJ_BOOL opj_jp2_read_bpcc( opj_jp2_t *jp2,
663 OPJ_BYTE * p_bpc_header_data,
664 OPJ_UINT32 p_bpc_header_size,
665 opj_event_mgr_t * p_manager
671 assert(p_bpc_header_data != 00);
673 assert(p_manager != 00);
676 if (jp2->bpc != 255 ){
677 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);
680 /* and length is relevant */
681 if (p_bpc_header_size != jp2->numcomps) {
682 opj_event_msg(p_manager, EVT_ERROR, "Bad BPCC header box (bad size)\n");
686 /* read info for each component */
687 for (i = 0; i < jp2->numcomps; ++i) {
688 opj_read_bytes(p_bpc_header_data,&jp2->comps[i].bpcc ,1); /* read each BPCC component */
694 static OPJ_BYTE * opj_jp2_write_cdef(opj_jp2_t *jp2, OPJ_UINT32 * p_nb_bytes_written)
696 /* room for 8 bytes for box, 2 for n */
697 OPJ_UINT32 l_cdef_size = 10;
698 OPJ_BYTE * l_cdef_data,* l_current_cdef_ptr;
704 assert(p_nb_bytes_written != 00);
705 assert(jp2->color.jp2_cdef != 00);
706 assert(jp2->color.jp2_cdef->info != 00);
707 assert(jp2->color.jp2_cdef->n > 0U);
709 l_cdef_size += 6 * jp2->color.jp2_cdef->n;
711 l_cdef_data = (OPJ_BYTE *) opj_malloc(l_cdef_size);
712 if (l_cdef_data == 00) {
716 l_current_cdef_ptr = l_cdef_data;
718 opj_write_bytes(l_current_cdef_ptr,l_cdef_size,4); /* write box size */
719 l_current_cdef_ptr += 4;
721 opj_write_bytes(l_current_cdef_ptr,JP2_CDEF,4); /* BPCC */
722 l_current_cdef_ptr += 4;
724 l_value = jp2->color.jp2_cdef->n;
725 opj_write_bytes(l_current_cdef_ptr,l_value,2); /* N */
726 l_current_cdef_ptr += 2;
728 for (i = 0U; i < jp2->color.jp2_cdef->n; ++i) {
729 l_value = jp2->color.jp2_cdef->info[i].cn;
730 opj_write_bytes(l_current_cdef_ptr,l_value,2); /* Cni */
731 l_current_cdef_ptr += 2;
732 l_value = jp2->color.jp2_cdef->info[i].typ;
733 opj_write_bytes(l_current_cdef_ptr,l_value,2); /* Typi */
734 l_current_cdef_ptr += 2;
735 l_value = jp2->color.jp2_cdef->info[i].asoc;
736 opj_write_bytes(l_current_cdef_ptr,l_value,2); /* Asoci */
737 l_current_cdef_ptr += 2;
739 *p_nb_bytes_written = l_cdef_size;
744 OPJ_BYTE * opj_jp2_write_colr( opj_jp2_t *jp2,
745 OPJ_UINT32 * p_nb_bytes_written
748 /* room for 8 bytes for box 3 for common data and variable upon profile*/
749 OPJ_UINT32 l_colr_size = 11;
750 OPJ_BYTE * l_colr_data,* l_current_colr_ptr;
754 assert(p_nb_bytes_written != 00);
755 assert(jp2->meth == 1 || jp2->meth == 2);
759 l_colr_size += 4; /* EnumCS */
762 assert(jp2->color.icc_profile_len); /* ICC profile */
763 l_colr_size += jp2->color.icc_profile_len;
769 l_colr_data = (OPJ_BYTE *) opj_calloc(1,l_colr_size);
770 if (l_colr_data == 00) {
774 l_current_colr_ptr = l_colr_data;
776 opj_write_bytes(l_current_colr_ptr,l_colr_size,4); /* write box size */
777 l_current_colr_ptr += 4;
779 opj_write_bytes(l_current_colr_ptr,JP2_COLR,4); /* BPCC */
780 l_current_colr_ptr += 4;
782 opj_write_bytes(l_current_colr_ptr, jp2->meth,1); /* METH */
783 ++l_current_colr_ptr;
785 opj_write_bytes(l_current_colr_ptr, jp2->precedence,1); /* PRECEDENCE */
786 ++l_current_colr_ptr;
788 opj_write_bytes(l_current_colr_ptr, jp2->approx,1); /* APPROX */
789 ++l_current_colr_ptr;
791 if (jp2->meth == 1) { /* Meth value is restricted to 1 or 2 (Table I.9 of part 1) */
792 opj_write_bytes(l_current_colr_ptr, jp2->enumcs,4); } /* EnumCS */
794 if (jp2->meth == 2) { /* ICC profile */
796 for(i = 0; i < jp2->color.icc_profile_len; ++i) {
797 opj_write_bytes(l_current_colr_ptr, jp2->color.icc_profile_buf[i], 1);
798 ++l_current_colr_ptr;
803 *p_nb_bytes_written = l_colr_size;
808 void opj_jp2_free_pclr(opj_jp2_color_t *color)
810 opj_free(color->jp2_pclr->channel_sign);
811 opj_free(color->jp2_pclr->channel_size);
812 opj_free(color->jp2_pclr->entries);
814 if(color->jp2_pclr->cmap) opj_free(color->jp2_pclr->cmap);
816 opj_free(color->jp2_pclr); color->jp2_pclr = NULL;
819 static OPJ_BOOL opj_jp2_check_color(opj_image_t *image, opj_jp2_color_t *color, opj_event_mgr_t *p_manager)
823 /* testcase 4149.pdf.SIGSEGV.cf7.3501 */
824 if (color->jp2_cdef) {
825 opj_jp2_cdef_info_t *info = color->jp2_cdef->info;
826 OPJ_UINT16 n = color->jp2_cdef->n;
827 OPJ_UINT32 nr_channels = image->numcomps; /* FIXME image->numcomps == jp2->numcomps before color is applied ??? */
829 /* cdef applies to cmap channels if any */
830 if (color->jp2_pclr && color->jp2_pclr->cmap) {
831 nr_channels = (OPJ_UINT32)color->jp2_pclr->nr_channels;
834 for (i = 0; i < n; i++) {
835 if (info[i].cn >= nr_channels) {
836 opj_event_msg(p_manager, EVT_ERROR, "Invalid component index %d (>= %d).\n", info[i].cn, nr_channels);
839 if (info[i].asoc > 0 && (OPJ_UINT32)(info[i].asoc - 1) >= nr_channels) {
840 opj_event_msg(p_manager, EVT_ERROR, "Invalid component index %d (>= %d).\n", info[i].asoc - 1, nr_channels);
846 /* ISO 15444-1 states that if cdef is present, it shall contain a complete list of channel definitions. */
847 while (nr_channels > 0)
849 for(i = 0; i < n; ++i) {
850 if ((OPJ_UINT32)info[i].cn == (nr_channels - 1U)) {
855 opj_event_msg(p_manager, EVT_ERROR, "Incomplete channel definitions.\n");
862 /* testcases 451.pdf.SIGSEGV.f4c.3723, 451.pdf.SIGSEGV.5b5.3723 and
863 66ea31acbb0f23a2bbc91f64d69a03f5_signal_sigsegv_13937c0_7030_5725.pdf */
864 if (color->jp2_pclr && color->jp2_pclr->cmap) {
865 OPJ_UINT16 nr_channels = color->jp2_pclr->nr_channels;
866 opj_jp2_cmap_comp_t *cmap = color->jp2_pclr->cmap;
867 OPJ_BOOL *pcol_usage, is_sane = OPJ_TRUE;
869 /* verify that all original components match an existing one */
870 for (i = 0; i < nr_channels; i++) {
871 if (cmap[i].cmp >= image->numcomps) {
872 opj_event_msg(p_manager, EVT_ERROR, "Invalid component index %d (>= %d).\n", cmap[i].cmp, image->numcomps);
877 pcol_usage = opj_calloc(nr_channels, sizeof(OPJ_BOOL));
879 opj_event_msg(p_manager, EVT_ERROR, "Unexpected OOM.\n");
882 /* verify that no component is targeted more than once */
883 for (i = 0; i < nr_channels; i++) {
884 OPJ_UINT16 pcol = cmap[i].pcol;
885 assert(cmap[i].mtyp == 0 || cmap[i].mtyp == 1);
886 if (pcol >= nr_channels) {
887 opj_event_msg(p_manager, EVT_ERROR, "Invalid component/palette index for direct mapping %d.\n", pcol);
890 else if (pcol_usage[pcol] && cmap[i].mtyp == 1) {
891 opj_event_msg(p_manager, EVT_ERROR, "Component %d is mapped twice.\n", pcol);
894 else if (cmap[i].mtyp == 0 && cmap[i].pcol != 0) {
895 /* I.5.3.5 PCOL: If the value of the MTYP field for this channel is 0, then
896 * the value of this field shall be 0. */
897 opj_event_msg(p_manager, EVT_ERROR, "Direct use at #%d however pcol=%d.\n", i, pcol);
901 pcol_usage[pcol] = OPJ_TRUE;
903 /* verify that all components are targeted at least once */
904 for (i = 0; i < nr_channels; i++) {
905 if (!pcol_usage[i] && cmap[i].mtyp != 0) {
906 opj_event_msg(p_manager, EVT_ERROR, "Component %d doesn't have a mapping.\n", i);
910 opj_free(pcol_usage);
920 void opj_jp2_apply_pclr(opj_image_t *image, opj_jp2_color_t *color)
922 opj_image_comp_t *old_comps, *new_comps;
923 OPJ_BYTE *channel_size, *channel_sign;
925 opj_jp2_cmap_comp_t *cmap;
926 OPJ_INT32 *src, *dst;
928 OPJ_UINT16 i, nr_channels, cmp, pcol;
931 channel_size = color->jp2_pclr->channel_size;
932 channel_sign = color->jp2_pclr->channel_sign;
933 entries = color->jp2_pclr->entries;
934 cmap = color->jp2_pclr->cmap;
935 nr_channels = color->jp2_pclr->nr_channels;
937 old_comps = image->comps;
938 new_comps = (opj_image_comp_t*)
939 opj_malloc(nr_channels * sizeof(opj_image_comp_t));
941 /* FIXME no error code for opj_jp2_apply_pclr */
942 /* FIXME event manager error callback */
945 for(i = 0; i < nr_channels; ++i) {
946 pcol = cmap[i].pcol; cmp = cmap[i].cmp;
949 if(cmap[i].mtyp == 0){
951 new_comps[i] = old_comps[cmp];
954 new_comps[pcol] = old_comps[cmp];
957 /* Palette mapping: */
958 new_comps[i].data = (OPJ_INT32*)
959 opj_malloc(old_comps[cmp].w * old_comps[cmp].h * sizeof(OPJ_INT32));
960 if (!new_comps[i].data) {
963 /* FIXME no error code for opj_jp2_apply_pclr */
964 /* FIXME event manager error callback */
967 new_comps[i].prec = channel_size[i];
968 new_comps[i].sgnd = channel_sign[i];
971 top_k = color->jp2_pclr->nr_entries - 1;
973 for(i = 0; i < nr_channels; ++i) {
974 /* Palette mapping: */
975 cmp = cmap[i].cmp; pcol = cmap[i].pcol;
976 src = old_comps[cmp].data;
978 max = new_comps[pcol].w * new_comps[pcol].h;
981 if(cmap[i].mtyp == 0) {
983 dst = new_comps[i].data;
985 for(j = 0; j < max; ++j) {
991 dst = new_comps[pcol].data;
993 for(j = 0; j < max; ++j) {
995 if((k = src[j]) < 0) k = 0; else if(k > top_k) k = top_k;
998 dst[j] = (OPJ_INT32)entries[k * nr_channels + pcol];
1003 max = image->numcomps;
1004 for(i = 0; i < max; ++i) {
1005 if(old_comps[i].data) opj_free(old_comps[i].data);
1008 opj_free(old_comps);
1009 image->comps = new_comps;
1010 image->numcomps = nr_channels;
1012 opj_jp2_free_pclr(color);
1016 OPJ_BOOL opj_jp2_read_pclr( opj_jp2_t *jp2,
1017 OPJ_BYTE * p_pclr_header_data,
1018 OPJ_UINT32 p_pclr_header_size,
1019 opj_event_mgr_t * p_manager
1022 opj_jp2_pclr_t *jp2_pclr;
1023 OPJ_BYTE *channel_size, *channel_sign;
1024 OPJ_UINT32 *entries;
1025 OPJ_UINT16 nr_entries,nr_channels;
1028 OPJ_BYTE *orig_header_data = p_pclr_header_data;
1031 assert(p_pclr_header_data != 00);
1033 assert(p_manager != 00);
1034 (void)p_pclr_header_size;
1036 if(jp2->color.jp2_pclr)
1039 if (p_pclr_header_size < 3)
1042 opj_read_bytes(p_pclr_header_data, &l_value , 2); /* NE */
1043 p_pclr_header_data += 2;
1044 nr_entries = (OPJ_UINT16) l_value;
1045 if ((nr_entries == 0U) || (nr_entries > 1024U)) {
1046 opj_event_msg(p_manager, EVT_ERROR, "Invalid PCLR box. Reports %d entries\n", (int)nr_entries);
1050 opj_read_bytes(p_pclr_header_data, &l_value , 1); /* NPC */
1051 ++p_pclr_header_data;
1052 nr_channels = (OPJ_UINT16) l_value;
1053 if (nr_channels == 0U) {
1054 opj_event_msg(p_manager, EVT_ERROR, "Invalid PCLR box. Reports 0 palette columns\n");
1058 if (p_pclr_header_size < 3 + (OPJ_UINT32)nr_channels)
1061 entries = (OPJ_UINT32*) opj_malloc((size_t)nr_channels * nr_entries * sizeof(OPJ_UINT32));
1064 channel_size = (OPJ_BYTE*) opj_malloc(nr_channels);
1070 channel_sign = (OPJ_BYTE*) opj_malloc(nr_channels);
1074 opj_free(channel_size);
1078 jp2_pclr = (opj_jp2_pclr_t*)opj_malloc(sizeof(opj_jp2_pclr_t));
1082 opj_free(channel_size);
1083 opj_free(channel_sign);
1087 jp2_pclr->channel_sign = channel_sign;
1088 jp2_pclr->channel_size = channel_size;
1089 jp2_pclr->entries = entries;
1090 jp2_pclr->nr_entries = nr_entries;
1091 jp2_pclr->nr_channels = (OPJ_BYTE) l_value;
1092 jp2_pclr->cmap = NULL;
1094 jp2->color.jp2_pclr = jp2_pclr;
1096 for(i = 0; i < nr_channels; ++i) {
1097 opj_read_bytes(p_pclr_header_data, &l_value , 1); /* Bi */
1098 ++p_pclr_header_data;
1100 channel_size[i] = (OPJ_BYTE)((l_value & 0x7f) + 1);
1101 channel_sign[i] = (l_value & 0x80) ? 1 : 0;
1104 for(j = 0; j < nr_entries; ++j) {
1105 for(i = 0; i < nr_channels; ++i) {
1106 OPJ_UINT32 bytes_to_read = (OPJ_UINT32)((channel_size[i]+7)>>3);
1108 if (bytes_to_read > sizeof(OPJ_UINT32))
1109 bytes_to_read = sizeof(OPJ_UINT32);
1110 if ((ptrdiff_t)p_pclr_header_size < (ptrdiff_t)(p_pclr_header_data - orig_header_data) + (ptrdiff_t)bytes_to_read)
1113 opj_read_bytes(p_pclr_header_data, &l_value , bytes_to_read); /* Cji */
1114 p_pclr_header_data += bytes_to_read;
1115 *entries = (OPJ_UINT32) l_value;
1123 OPJ_BOOL opj_jp2_read_cmap( opj_jp2_t * jp2,
1124 OPJ_BYTE * p_cmap_header_data,
1125 OPJ_UINT32 p_cmap_header_size,
1126 opj_event_mgr_t * p_manager
1129 opj_jp2_cmap_comp_t *cmap;
1130 OPJ_BYTE i, nr_channels;
1135 assert(p_cmap_header_data != 00);
1136 assert(p_manager != 00);
1137 (void)p_cmap_header_size;
1139 /* Need nr_channels: */
1140 if(jp2->color.jp2_pclr == NULL) {
1141 opj_event_msg(p_manager, EVT_ERROR, "Need to read a PCLR box before the CMAP box.\n");
1145 /* Part 1, I.5.3.5: 'There shall be at most one Component Mapping box
1146 * inside a JP2 Header box' :
1148 if(jp2->color.jp2_pclr->cmap) {
1149 opj_event_msg(p_manager, EVT_ERROR, "Only one CMAP box is allowed.\n");
1153 nr_channels = jp2->color.jp2_pclr->nr_channels;
1154 if (p_cmap_header_size < (OPJ_UINT32)nr_channels * 4) {
1155 opj_event_msg(p_manager, EVT_ERROR, "Insufficient data for CMAP box.\n");
1159 cmap = (opj_jp2_cmap_comp_t*) opj_malloc(nr_channels * sizeof(opj_jp2_cmap_comp_t));
1164 for(i = 0; i < nr_channels; ++i) {
1165 opj_read_bytes(p_cmap_header_data, &l_value, 2); /* CMP^i */
1166 p_cmap_header_data +=2;
1167 cmap[i].cmp = (OPJ_UINT16) l_value;
1169 opj_read_bytes(p_cmap_header_data, &l_value, 1); /* MTYP^i */
1170 ++p_cmap_header_data;
1171 cmap[i].mtyp = (OPJ_BYTE) l_value;
1173 opj_read_bytes(p_cmap_header_data, &l_value, 1); /* PCOL^i */
1174 ++p_cmap_header_data;
1175 cmap[i].pcol = (OPJ_BYTE) l_value;
1178 jp2->color.jp2_pclr->cmap = cmap;
1183 void opj_jp2_apply_cdef(opj_image_t *image, opj_jp2_color_t *color)
1185 opj_jp2_cdef_info_t *info;
1186 OPJ_UINT16 i, n, cn, asoc, acn;
1188 info = color->jp2_cdef->info;
1189 n = color->jp2_cdef->n;
1191 for(i = 0; i < n; ++i)
1193 /* WATCH: acn = asoc - 1 ! */
1194 asoc = info[i].asoc;
1197 if( cn >= image->numcomps)
1199 fprintf(stderr, "cn=%d, numcomps=%d\n", cn, image->numcomps);
1202 if(asoc == 0 || asoc == 65535)
1204 image->comps[cn].alpha = info[i].typ;
1208 acn = (OPJ_UINT16)(asoc - 1);
1209 if( acn >= image->numcomps )
1211 fprintf(stderr, "acn=%d, numcomps=%d\n", acn, image->numcomps);
1215 /* Swap only if color channel */
1216 if((cn != acn) && (info[i].typ == 0))
1218 opj_image_comp_t saved;
1221 memcpy(&saved, &image->comps[cn], sizeof(opj_image_comp_t));
1222 memcpy(&image->comps[cn], &image->comps[acn], sizeof(opj_image_comp_t));
1223 memcpy(&image->comps[acn], &saved, sizeof(opj_image_comp_t));
1225 /* Swap channels in following channel definitions, don't bother with j <= i that are already processed */
1226 for (j = i + 1; j < n ; ++j)
1228 if (info[j].cn == cn) {
1231 else if (info[j].cn == acn) {
1234 /* asoc is related to color index. Do not update. */
1238 image->comps[cn].alpha = info[i].typ;
1241 if(color->jp2_cdef->info) opj_free(color->jp2_cdef->info);
1243 opj_free(color->jp2_cdef); color->jp2_cdef = NULL;
1245 }/* jp2_apply_cdef() */
1247 OPJ_BOOL opj_jp2_read_cdef( opj_jp2_t * jp2,
1248 OPJ_BYTE * p_cdef_header_data,
1249 OPJ_UINT32 p_cdef_header_size,
1250 opj_event_mgr_t * p_manager
1253 opj_jp2_cdef_info_t *cdef_info;
1259 assert(p_cdef_header_data != 00);
1260 assert(p_manager != 00);
1261 (void)p_cdef_header_size;
1263 /* Part 1, I.5.3.6: 'The shall be at most one Channel Definition box
1264 * inside a JP2 Header box.'*/
1265 if(jp2->color.jp2_cdef) return OPJ_FALSE;
1267 if (p_cdef_header_size < 2) {
1268 opj_event_msg(p_manager, EVT_ERROR, "Insufficient data for CDEF box.\n");
1272 opj_read_bytes(p_cdef_header_data,&l_value ,2); /* N */
1273 p_cdef_header_data+= 2;
1275 if ( (OPJ_UINT16)l_value == 0){ /* szukw000: FIXME */
1276 opj_event_msg(p_manager, EVT_ERROR, "Number of channel description is equal to zero in CDEF box.\n");
1280 if (p_cdef_header_size < 2 + (OPJ_UINT32)(OPJ_UINT16)l_value * 6) {
1281 opj_event_msg(p_manager, EVT_ERROR, "Insufficient data for CDEF box.\n");
1285 cdef_info = (opj_jp2_cdef_info_t*) opj_malloc(l_value * sizeof(opj_jp2_cdef_info_t));
1289 jp2->color.jp2_cdef = (opj_jp2_cdef_t*)opj_malloc(sizeof(opj_jp2_cdef_t));
1290 if(!jp2->color.jp2_cdef)
1292 opj_free(cdef_info);
1295 jp2->color.jp2_cdef->info = cdef_info;
1296 jp2->color.jp2_cdef->n = (OPJ_UINT16) l_value;
1298 for(i = 0; i < jp2->color.jp2_cdef->n; ++i) {
1299 opj_read_bytes(p_cdef_header_data, &l_value, 2); /* Cn^i */
1300 p_cdef_header_data +=2;
1301 cdef_info[i].cn = (OPJ_UINT16) l_value;
1303 opj_read_bytes(p_cdef_header_data, &l_value, 2); /* Typ^i */
1304 p_cdef_header_data +=2;
1305 cdef_info[i].typ = (OPJ_UINT16) l_value;
1307 opj_read_bytes(p_cdef_header_data, &l_value, 2); /* Asoc^i */
1308 p_cdef_header_data +=2;
1309 cdef_info[i].asoc = (OPJ_UINT16) l_value;
1315 OPJ_BOOL opj_jp2_read_colr( opj_jp2_t *jp2,
1316 OPJ_BYTE * p_colr_header_data,
1317 OPJ_UINT32 p_colr_header_size,
1318 opj_event_mgr_t * p_manager
1325 assert(p_colr_header_data != 00);
1326 assert(p_manager != 00);
1328 if (p_colr_header_size < 3) {
1329 opj_event_msg(p_manager, EVT_ERROR, "Bad COLR header box (bad size)\n");
1333 /* Part 1, I.5.3.3 : 'A conforming JP2 reader shall ignore all Colour
1334 * Specification boxes after the first.'
1336 if(jp2->color.jp2_has_colr) {
1337 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");
1338 p_colr_header_data += p_colr_header_size;
1342 opj_read_bytes(p_colr_header_data,&jp2->meth ,1); /* METH */
1343 ++p_colr_header_data;
1345 opj_read_bytes(p_colr_header_data,&jp2->precedence ,1); /* PRECEDENCE */
1346 ++p_colr_header_data;
1348 opj_read_bytes(p_colr_header_data,&jp2->approx ,1); /* APPROX */
1349 ++p_colr_header_data;
1351 if (jp2->meth == 1) {
1352 if (p_colr_header_size < 7) {
1353 opj_event_msg(p_manager, EVT_ERROR, "Bad COLR header box (bad size: %d)\n", p_colr_header_size);
1356 if (p_colr_header_size > 7) {
1357 /* testcase Altona_Technical_v20_x4.pdf */
1358 opj_event_msg(p_manager, EVT_WARNING, "Bad COLR header box (bad size: %d)\n", p_colr_header_size);
1361 opj_read_bytes(p_colr_header_data,&jp2->enumcs ,4); /* EnumCS */
1363 jp2->color.jp2_has_colr = 1;
1365 else if (jp2->meth == 2) {
1367 OPJ_INT32 it_icc_value = 0;
1368 OPJ_INT32 icc_len = (OPJ_INT32)p_colr_header_size - 3;
1370 jp2->color.icc_profile_len = (OPJ_UINT32)icc_len;
1371 jp2->color.icc_profile_buf = (OPJ_BYTE*) opj_calloc(1,(size_t)icc_len);
1372 if (!jp2->color.icc_profile_buf)
1374 jp2->color.icc_profile_len = 0;
1378 for (it_icc_value = 0; it_icc_value < icc_len; ++it_icc_value)
1380 opj_read_bytes(p_colr_header_data,&l_value,1); /* icc values */
1381 ++p_colr_header_data;
1382 jp2->color.icc_profile_buf[it_icc_value] = (OPJ_BYTE) l_value;
1385 jp2->color.jp2_has_colr = 1;
1387 else if (jp2->meth > 2)
1389 /* ISO/IEC 15444-1:2004 (E), Table I.9 Legal METH values:
1390 conforming JP2 reader shall ignore the entire Colour Specification box.*/
1391 opj_event_msg(p_manager, EVT_INFO, "COLR BOX meth value is not a regular value (%d), "
1392 "so we will ignore the entire Colour Specification box. \n", jp2->meth);
1397 OPJ_BOOL opj_jp2_decode(opj_jp2_t *jp2,
1398 opj_stream_private_t *p_stream,
1399 opj_image_t* p_image,
1400 opj_event_mgr_t * p_manager)
1406 if( ! opj_j2k_decode(jp2->j2k, p_stream, p_image, p_manager) ) {
1407 opj_event_msg(p_manager, EVT_ERROR, "Failed to decode the codestream in the JP2 file\n");
1411 if (!jp2->ignore_pclr_cmap_cdef){
1412 if (!opj_jp2_check_color(p_image, &(jp2->color), p_manager)) {
1416 /* Set Image Color Space */
1417 if (jp2->enumcs == 16)
1418 p_image->color_space = OPJ_CLRSPC_SRGB;
1419 else if (jp2->enumcs == 17)
1420 p_image->color_space = OPJ_CLRSPC_GRAY;
1421 else if (jp2->enumcs == 18)
1422 p_image->color_space = OPJ_CLRSPC_SYCC;
1423 else if (jp2->enumcs == 24)
1424 p_image->color_space = OPJ_CLRSPC_EYCC;
1426 p_image->color_space = OPJ_CLRSPC_UNKNOWN;
1428 if(jp2->color.jp2_pclr) {
1429 /* Part 1, I.5.3.4: Either both or none : */
1430 if( !jp2->color.jp2_pclr->cmap)
1431 opj_jp2_free_pclr(&(jp2->color));
1433 opj_jp2_apply_pclr(p_image, &(jp2->color));
1436 /* Apply the color space if needed */
1437 if(jp2->color.jp2_cdef) {
1438 opj_jp2_apply_cdef(p_image, &(jp2->color));
1441 if(jp2->color.icc_profile_buf) {
1442 p_image->icc_profile_buf = jp2->color.icc_profile_buf;
1443 p_image->icc_profile_len = jp2->color.icc_profile_len;
1444 jp2->color.icc_profile_buf = NULL;
1451 OPJ_BOOL opj_jp2_write_jp2h(opj_jp2_t *jp2,
1452 opj_stream_private_t *stream,
1453 opj_event_mgr_t * p_manager
1456 opj_jp2_img_header_writer_handler_t l_writers [4];
1457 opj_jp2_img_header_writer_handler_t * l_current_writer;
1459 OPJ_INT32 i, l_nb_pass;
1460 /* size of data for super box*/
1461 OPJ_UINT32 l_jp2h_size = 8;
1462 OPJ_BOOL l_result = OPJ_TRUE;
1464 /* to store the data of the super box */
1465 OPJ_BYTE l_jp2h_data [8];
1468 assert(stream != 00);
1470 assert(p_manager != 00);
1472 memset(l_writers,0,sizeof(l_writers));
1474 if (jp2->bpc == 255) {
1476 l_writers[0].handler = opj_jp2_write_ihdr;
1477 l_writers[1].handler = opj_jp2_write_bpcc;
1478 l_writers[2].handler = opj_jp2_write_colr;
1482 l_writers[0].handler = opj_jp2_write_ihdr;
1483 l_writers[1].handler = opj_jp2_write_colr;
1486 if (jp2->color.jp2_cdef != NULL) {
1487 l_writers[l_nb_pass].handler = opj_jp2_write_cdef;
1491 /* write box header */
1492 /* write JP2H type */
1493 opj_write_bytes(l_jp2h_data+4,JP2_JP2H,4);
1495 l_current_writer = l_writers;
1496 for (i=0;i<l_nb_pass;++i) {
1497 l_current_writer->m_data = l_current_writer->handler(jp2,&(l_current_writer->m_size));
1498 if (l_current_writer->m_data == 00) {
1499 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to hold JP2 Header data\n");
1500 l_result = OPJ_FALSE;
1504 l_jp2h_size += l_current_writer->m_size;
1509 l_current_writer = l_writers;
1510 for (i=0;i<l_nb_pass;++i) {
1511 if (l_current_writer->m_data != 00) {
1512 opj_free(l_current_writer->m_data );
1520 /* write super box size */
1521 opj_write_bytes(l_jp2h_data,l_jp2h_size,4);
1523 /* write super box data on stream */
1524 if (opj_stream_write_data(stream,l_jp2h_data,8,p_manager) != 8) {
1525 opj_event_msg(p_manager, EVT_ERROR, "Stream error while writing JP2 Header box\n");
1526 l_result = OPJ_FALSE;
1530 l_current_writer = l_writers;
1531 for (i=0;i<l_nb_pass;++i) {
1532 if (opj_stream_write_data(stream,l_current_writer->m_data,l_current_writer->m_size,p_manager) != l_current_writer->m_size) {
1533 opj_event_msg(p_manager, EVT_ERROR, "Stream error while writing JP2 Header box\n");
1534 l_result = OPJ_FALSE;
1541 l_current_writer = l_writers;
1544 for (i=0;i<l_nb_pass;++i) {
1545 if (l_current_writer->m_data != 00) {
1546 opj_free(l_current_writer->m_data );
1554 OPJ_BOOL opj_jp2_write_ftyp(opj_jp2_t *jp2,
1555 opj_stream_private_t *cio,
1556 opj_event_mgr_t * p_manager )
1559 OPJ_UINT32 l_ftyp_size = 16 + 4 * jp2->numcl;
1560 OPJ_BYTE * l_ftyp_data, * l_current_data_ptr;
1566 assert(p_manager != 00);
1568 l_ftyp_data = (OPJ_BYTE *) opj_calloc(1,l_ftyp_size);
1570 if (l_ftyp_data == 00) {
1571 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to handle ftyp data\n");
1575 l_current_data_ptr = l_ftyp_data;
1577 opj_write_bytes(l_current_data_ptr, l_ftyp_size,4); /* box size */
1578 l_current_data_ptr += 4;
1580 opj_write_bytes(l_current_data_ptr, JP2_FTYP,4); /* FTYP */
1581 l_current_data_ptr += 4;
1583 opj_write_bytes(l_current_data_ptr, jp2->brand,4); /* BR */
1584 l_current_data_ptr += 4;
1586 opj_write_bytes(l_current_data_ptr, jp2->minversion,4); /* MinV */
1587 l_current_data_ptr += 4;
1589 for (i = 0; i < jp2->numcl; i++) {
1590 opj_write_bytes(l_current_data_ptr, jp2->cl[i],4); /* CL */
1593 l_result = (opj_stream_write_data(cio,l_ftyp_data,l_ftyp_size,p_manager) == l_ftyp_size);
1596 opj_event_msg(p_manager, EVT_ERROR, "Error while writing ftyp data to stream\n");
1599 opj_free(l_ftyp_data);
1604 OPJ_BOOL opj_jp2_write_jp2c(opj_jp2_t *jp2,
1605 opj_stream_private_t *cio,
1606 opj_event_mgr_t * p_manager )
1608 OPJ_OFF_T j2k_codestream_exit;
1609 OPJ_BYTE l_data_header [8];
1614 assert(p_manager != 00);
1615 assert(opj_stream_has_seek(cio));
1617 j2k_codestream_exit = opj_stream_tell(cio);
1618 opj_write_bytes(l_data_header,
1619 (OPJ_UINT32) (j2k_codestream_exit - jp2->j2k_codestream_offset),
1620 4); /* size of codestream */
1621 opj_write_bytes(l_data_header + 4,JP2_JP2C,4); /* JP2C */
1623 if (! opj_stream_seek(cio,jp2->j2k_codestream_offset,p_manager)) {
1624 opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
1628 if (opj_stream_write_data(cio,l_data_header,8,p_manager) != 8) {
1629 opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
1633 if (! opj_stream_seek(cio,j2k_codestream_exit,p_manager)) {
1634 opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
1641 OPJ_BOOL opj_jp2_write_jp( opj_jp2_t *jp2,
1642 opj_stream_private_t *cio,
1643 opj_event_mgr_t * p_manager )
1645 /* 12 bytes will be read */
1646 OPJ_BYTE l_signature_data [12];
1651 assert(p_manager != 00);
1653 /* write box length */
1654 opj_write_bytes(l_signature_data,12,4);
1655 /* writes box type */
1656 opj_write_bytes(l_signature_data+4,JP2_JP,4);
1657 /* writes magic number*/
1658 opj_write_bytes(l_signature_data+8,0x0d0a870a,4);
1660 if (opj_stream_write_data(cio,l_signature_data,12,p_manager) != 12) {
1667 /* ----------------------------------------------------------------------- */
1668 /* JP2 decoder interface */
1669 /* ----------------------------------------------------------------------- */
1671 void opj_jp2_setup_decoder(opj_jp2_t *jp2, opj_dparameters_t *parameters)
1673 /* setup the J2K codec */
1674 opj_j2k_setup_decoder(jp2->j2k, parameters);
1676 /* further JP2 initializations go here */
1677 jp2->color.jp2_has_colr = 0;
1678 jp2->ignore_pclr_cmap_cdef = parameters->flags & OPJ_DPARAMETERS_IGNORE_PCLR_CMAP_CDEF_FLAG;
1681 /* ----------------------------------------------------------------------- */
1682 /* JP2 encoder interface */
1683 /* ----------------------------------------------------------------------- */
1685 OPJ_BOOL opj_jp2_setup_encoder( opj_jp2_t *jp2,
1686 opj_cparameters_t *parameters,
1688 opj_event_mgr_t * p_manager)
1693 OPJ_UINT32 alpha_count;
1694 OPJ_UINT32 color_channels = 0U;
1695 OPJ_UINT32 alpha_channel = 0U;
1698 if(!jp2 || !parameters || !image)
1701 /* setup the J2K codec */
1702 /* ------------------- */
1704 /* Check if number of components respects standard */
1705 if (image->numcomps < 1 || image->numcomps > 16384) {
1706 opj_event_msg(p_manager, EVT_ERROR, "Invalid number of components specified while setting up JP2 encoder\n");
1710 if (opj_j2k_setup_encoder(jp2->j2k, parameters, image, p_manager ) == OPJ_FALSE) {
1714 /* setup the JP2 codec */
1715 /* ------------------- */
1719 jp2->brand = JP2_JP2; /* BR */
1720 jp2->minversion = 0; /* MinV */
1722 jp2->cl = (OPJ_UINT32*) opj_malloc(jp2->numcl * sizeof(OPJ_UINT32));
1725 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory when setup the JP2 encoder\n");
1728 jp2->cl[0] = JP2_JP2; /* CL0 : JP2 */
1730 /* Image Header box */
1732 jp2->numcomps = image->numcomps; /* NC */
1733 jp2->comps = (opj_jp2_comps_t*) opj_malloc(jp2->numcomps * sizeof(opj_jp2_comps_t));
1736 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory when setup the JP2 encoder\n");
1737 /* Memory of jp2->cl will be freed by opj_jp2_destroy */
1741 jp2->h = image->y1 - image->y0; /* HEIGHT */
1742 jp2->w = image->x1 - image->x0; /* WIDTH */
1744 depth_0 = image->comps[0].prec - 1;
1745 sign = image->comps[0].sgnd;
1746 jp2->bpc = depth_0 + (sign << 7);
1747 for (i = 1; i < image->numcomps; i++) {
1748 OPJ_UINT32 depth = image->comps[i].prec - 1;
1749 sign = image->comps[i].sgnd;
1750 if (depth_0 != depth)
1753 jp2->C = 7; /* C : Always 7 */
1754 jp2->UnkC = 0; /* UnkC, colorspace specified in colr box */
1755 jp2->IPR = 0; /* IPR, no intellectual property */
1757 /* BitsPerComponent box */
1758 for (i = 0; i < image->numcomps; i++) {
1759 jp2->comps[i].bpcc = image->comps[i].prec - 1 + (image->comps[i].sgnd << 7);
1762 /* Colour Specification box */
1763 if(image->icc_profile_len) {
1769 if (image->color_space == 1)
1770 jp2->enumcs = 16; /* sRGB as defined by IEC 61966-2-1 */
1771 else if (image->color_space == 2)
1772 jp2->enumcs = 17; /* greyscale */
1773 else if (image->color_space == 3)
1774 jp2->enumcs = 18; /* YUV */
1777 /* Channel Definition box */
1778 /* FIXME not provided by parameters */
1779 /* We try to do what we can... */
1781 for (i = 0; i < image->numcomps; i++) {
1782 if (image->comps[i].alpha != 0) {
1787 if (alpha_count == 1U) { /* no way to deal with more than 1 alpha channel */
1788 switch (jp2->enumcs) {
1800 if (alpha_count == 0U) {
1801 opj_event_msg(p_manager, EVT_WARNING, "Alpha channel specified but unknown enumcs. No cdef box will be created.\n");
1802 } else if (image->numcomps < (color_channels+1)) {
1803 opj_event_msg(p_manager, EVT_WARNING, "Alpha channel specified but not enough image components for an automatic cdef box creation.\n");
1805 } else if ((OPJ_UINT32)alpha_channel < color_channels) {
1806 opj_event_msg(p_manager, EVT_WARNING, "Alpha channel position conflicts with color channel. No cdef box will be created.\n");
1809 } else if (alpha_count > 1) {
1810 opj_event_msg(p_manager, EVT_WARNING, "Multiple alpha channels specified. No cdef box will be created.\n");
1812 if (alpha_count == 1U) { /* if here, we know what we can do */
1813 jp2->color.jp2_cdef = (opj_jp2_cdef_t*)opj_malloc(sizeof(opj_jp2_cdef_t));
1814 if(!jp2->color.jp2_cdef) {
1815 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to setup the JP2 encoder\n");
1818 /* no memset needed, all values will be overwritten except if jp2->color.jp2_cdef->info allocation fails, */
1819 /* in which case jp2->color.jp2_cdef->info will be NULL => valid for destruction */
1820 jp2->color.jp2_cdef->info = (opj_jp2_cdef_info_t*) opj_malloc(image->numcomps * sizeof(opj_jp2_cdef_info_t));
1821 if (!jp2->color.jp2_cdef->info) {
1822 /* memory will be freed by opj_jp2_destroy */
1823 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to setup the JP2 encoder\n");
1826 jp2->color.jp2_cdef->n = (OPJ_UINT16) image->numcomps; /* cast is valid : image->numcomps [1,16384] */
1827 for (i = 0U; i < color_channels; i++) {
1828 jp2->color.jp2_cdef->info[i].cn = (OPJ_UINT16)i; /* cast is valid : image->numcomps [1,16384] */
1829 jp2->color.jp2_cdef->info[i].typ = 0U;
1830 jp2->color.jp2_cdef->info[i].asoc = (OPJ_UINT16)(i+1U); /* No overflow + cast is valid : image->numcomps [1,16384] */
1832 for (; i < image->numcomps; i++) {
1833 if (image->comps[i].alpha != 0) { /* we'll be here exactly once */
1834 jp2->color.jp2_cdef->info[i].cn = (OPJ_UINT16)i; /* cast is valid : image->numcomps [1,16384] */
1835 jp2->color.jp2_cdef->info[i].typ = 1U; /* Opacity channel */
1836 jp2->color.jp2_cdef->info[i].asoc = 0U; /* Apply alpha channel to the whole image */
1838 /* Unknown channel */
1839 jp2->color.jp2_cdef->info[i].cn = (OPJ_UINT16)i; /* cast is valid : image->numcomps [1,16384] */
1840 jp2->color.jp2_cdef->info[i].typ = 65535U;
1841 jp2->color.jp2_cdef->info[i].asoc = 65535U;
1846 jp2->precedence = 0; /* PRECEDENCE */
1847 jp2->approx = 0; /* APPROX */
1849 jp2->jpip_on = parameters->jpip_on;
1854 OPJ_BOOL opj_jp2_encode(opj_jp2_t *jp2,
1855 opj_stream_private_t *stream,
1856 opj_event_mgr_t * p_manager)
1858 return opj_j2k_encode(jp2->j2k, stream, p_manager);
1861 OPJ_BOOL opj_jp2_end_decompress(opj_jp2_t *jp2,
1862 opj_stream_private_t *cio,
1863 opj_event_mgr_t * p_manager
1869 assert(p_manager != 00);
1871 /* customization of the end encoding */
1872 opj_jp2_setup_end_header_reading(jp2);
1875 if (! opj_jp2_exec (jp2,jp2->m_procedure_list,cio,p_manager)) {
1879 return opj_j2k_end_decompress(jp2->j2k, cio, p_manager);
1882 OPJ_BOOL opj_jp2_end_compress( opj_jp2_t *jp2,
1883 opj_stream_private_t *cio,
1884 opj_event_mgr_t * p_manager
1890 assert(p_manager != 00);
1892 /* customization of the end encoding */
1893 opj_jp2_setup_end_header_writing(jp2);
1895 if (! opj_j2k_end_compress(jp2->j2k,cio,p_manager)) {
1900 return opj_jp2_exec(jp2,jp2->m_procedure_list,cio,p_manager);
1903 void opj_jp2_setup_end_header_writing (opj_jp2_t *jp2)
1910 opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)opj_jpip_write_iptr );
1912 opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)opj_jp2_write_jp2c );
1913 /* DEVELOPER CORNER, add your custom procedures */
1917 opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)opj_jpip_write_cidx );
1918 opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)opj_jpip_write_fidx );
1923 void opj_jp2_setup_end_header_reading (opj_jp2_t *jp2)
1927 opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)opj_jp2_read_header_procedure );
1928 /* DEVELOPER CORNER, add your custom procedures */
1931 OPJ_BOOL opj_jp2_default_validation ( opj_jp2_t * jp2,
1932 opj_stream_private_t *cio,
1933 opj_event_mgr_t * p_manager
1936 OPJ_BOOL l_is_valid = OPJ_TRUE;
1942 assert(p_manager != 00);
1944 /* JPEG2000 codec validation */
1946 /* STATE checking */
1947 /* make sure the state is at 0 */
1948 l_is_valid &= (jp2->jp2_state == JP2_STATE_NONE);
1950 /* make sure not reading a jp2h ???? WEIRD */
1951 l_is_valid &= (jp2->jp2_img_state == JP2_IMG_STATE_NONE);
1953 /* POINTER validation */
1954 /* make sure a j2k codec is present */
1955 l_is_valid &= (jp2->j2k != 00);
1957 /* make sure a procedure list is present */
1958 l_is_valid &= (jp2->m_procedure_list != 00);
1960 /* make sure a validation list is present */
1961 l_is_valid &= (jp2->m_validation_list != 00);
1963 /* PARAMETER VALIDATION */
1964 /* number of components */
1965 l_is_valid &= (jp2->numcl > 0);
1967 l_is_valid &= (jp2->h > 0);
1969 l_is_valid &= (jp2->w > 0);
1971 for (i = 0; i < jp2->numcomps; ++i) {
1972 l_is_valid &= (jp2->comps[i].bpcc > 0);
1976 l_is_valid &= ((jp2->meth > 0) && (jp2->meth < 3));
1978 /* stream validation */
1979 /* back and forth is needed */
1980 l_is_valid &= opj_stream_has_seek(cio);
1985 OPJ_BOOL opj_jp2_read_header_procedure( opj_jp2_t *jp2,
1986 opj_stream_private_t *stream,
1987 opj_event_mgr_t * p_manager
1991 OPJ_UINT32 l_nb_bytes_read;
1992 const opj_jp2_header_handler_t * l_current_handler;
1993 OPJ_UINT32 l_last_data_size = OPJ_BOX_SIZE;
1994 OPJ_UINT32 l_current_data_size;
1995 OPJ_BYTE * l_current_data = 00;
1998 assert(stream != 00);
2000 assert(p_manager != 00);
2002 l_current_data = (OPJ_BYTE*)opj_calloc(1,l_last_data_size);
2004 if (l_current_data == 00) {
2005 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to handle jpeg2000 file header\n");
2009 while (opj_jp2_read_boxhdr(&box,&l_nb_bytes_read,stream,p_manager)) {
2010 /* is it the codestream box ? */
2011 if (box.type == JP2_JP2C) {
2012 if (jp2->jp2_state & JP2_STATE_HEADER) {
2013 jp2->jp2_state |= JP2_STATE_CODESTREAM;
2014 opj_free(l_current_data);
2018 opj_event_msg(p_manager, EVT_ERROR, "bad placed jpeg codestream\n");
2019 opj_free(l_current_data);
2023 else if (box.length == 0) {
2024 opj_event_msg(p_manager, EVT_ERROR, "Cannot handle box of undefined sizes\n");
2025 opj_free(l_current_data);
2028 /* testcase 1851.pdf.SIGSEGV.ce9.948 */
2029 else if (box.length < l_nb_bytes_read) {
2030 opj_event_msg(p_manager, EVT_ERROR, "invalid box size %d (%x)\n", box.length, box.type);
2031 opj_free(l_current_data);
2035 l_current_handler = opj_jp2_find_handler(box.type);
2036 l_current_data_size = box.length - l_nb_bytes_read;
2038 if (l_current_handler != 00) {
2039 if ((OPJ_OFF_T)l_current_data_size > opj_stream_get_number_byte_left(stream)) {
2040 /* do not even try to malloc if we can't read */
2041 opj_event_msg(p_manager, EVT_ERROR, "Invalid box size %d for box '%c%c%c%c'. Need %d bytes, %d bytes remaining \n", box.length, (OPJ_BYTE)(box.type>>24), (OPJ_BYTE)(box.type>>16), (OPJ_BYTE)(box.type>>8), (OPJ_BYTE)(box.type>>0), l_current_data_size, (OPJ_UINT32)opj_stream_get_number_byte_left(stream));
2042 opj_free(l_current_data);
2045 if (l_current_data_size > l_last_data_size) {
2046 OPJ_BYTE* new_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,l_current_data_size);
2047 if (!new_current_data) {
2048 opj_free(l_current_data);
2049 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to handle jpeg2000 box\n");
2052 l_current_data = new_current_data;
2053 l_last_data_size = l_current_data_size;
2056 l_nb_bytes_read = (OPJ_UINT32)opj_stream_read_data(stream,l_current_data,l_current_data_size,p_manager);
2057 if (l_nb_bytes_read != l_current_data_size) {
2058 opj_event_msg(p_manager, EVT_ERROR, "Problem with reading JPEG2000 box, stream error\n");
2059 opj_free(l_current_data);
2063 if (! l_current_handler->handler(jp2,l_current_data,l_current_data_size,p_manager)) {
2064 opj_free(l_current_data);
2069 jp2->jp2_state |= JP2_STATE_UNKNOWN;
2070 if (opj_stream_skip(stream,l_current_data_size,p_manager) != l_current_data_size) {
2071 opj_event_msg(p_manager, EVT_ERROR, "Problem with skipping JPEG2000 box, stream error\n");
2072 opj_free(l_current_data);
2078 opj_free(l_current_data);
2084 * Excutes the given procedures on the given codec.
2086 * @param p_procedure_list the list of procedures to execute
2087 * @param jp2 the jpeg2000 file codec to execute the procedures on.
2088 * @param stream the stream to execute the procedures on.
2089 * @param p_manager the user manager.
2091 * @return true if all the procedures were successfully executed.
2093 static OPJ_BOOL opj_jp2_exec ( opj_jp2_t * jp2,
2094 opj_procedure_list_t * p_procedure_list,
2095 opj_stream_private_t *stream,
2096 opj_event_mgr_t * p_manager
2100 OPJ_BOOL (** l_procedure) (opj_jp2_t * jp2, opj_stream_private_t *, opj_event_mgr_t *) = 00;
2101 OPJ_BOOL l_result = OPJ_TRUE;
2102 OPJ_UINT32 l_nb_proc, i;
2105 assert(p_procedure_list != 00);
2107 assert(stream != 00);
2108 assert(p_manager != 00);
2110 l_nb_proc = opj_procedure_list_get_nb_procedures(p_procedure_list);
2111 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);
2113 for (i=0;i<l_nb_proc;++i) {
2114 l_result = l_result && (*l_procedure) (jp2,stream,p_manager);
2118 /* and clear the procedure list at the end. */
2119 opj_procedure_list_clear(p_procedure_list);
2123 OPJ_BOOL opj_jp2_start_compress(opj_jp2_t *jp2,
2124 opj_stream_private_t *stream,
2125 opj_image_t * p_image,
2126 opj_event_mgr_t * p_manager
2131 assert(stream != 00);
2132 assert(p_manager != 00);
2134 /* customization of the validation */
2135 opj_jp2_setup_encoding_validation (jp2);
2137 /* validation of the parameters codec */
2138 if (! opj_jp2_exec(jp2,jp2->m_validation_list,stream,p_manager)) {
2142 /* customization of the encoding */
2143 opj_jp2_setup_header_writing(jp2);
2146 if (! opj_jp2_exec (jp2,jp2->m_procedure_list,stream,p_manager)) {
2150 return opj_j2k_start_compress(jp2->j2k,stream,p_image,p_manager);
2153 const opj_jp2_header_handler_t * opj_jp2_find_handler (OPJ_UINT32 p_id)
2155 OPJ_UINT32 i, l_handler_size = sizeof(jp2_header) / sizeof(opj_jp2_header_handler_t);
2157 for (i=0;i<l_handler_size;++i) {
2158 if (jp2_header[i].id == p_id) {
2159 return &jp2_header[i];
2166 * Finds the image execution function related to the given box id.
2168 * @param p_id the id of the handler to fetch.
2170 * @return the given handler or 00 if it could not be found.
2172 static const opj_jp2_header_handler_t * opj_jp2_img_find_handler (OPJ_UINT32 p_id)
2174 OPJ_UINT32 i, l_handler_size = sizeof(jp2_img_header) / sizeof(opj_jp2_header_handler_t);
2175 for (i=0;i<l_handler_size;++i)
2177 if (jp2_img_header[i].id == p_id) {
2178 return &jp2_img_header[i];
2186 * Reads a jpeg2000 file signature box.
2188 * @param p_header_data the data contained in the signature box.
2189 * @param jp2 the jpeg2000 file codec.
2190 * @param p_header_size the size of the data contained in the signature box.
2191 * @param p_manager the user event manager.
2193 * @return true if the file signature box is valid.
2195 static OPJ_BOOL opj_jp2_read_jp(opj_jp2_t *jp2,
2196 OPJ_BYTE * p_header_data,
2197 OPJ_UINT32 p_header_size,
2198 opj_event_mgr_t * p_manager
2202 OPJ_UINT32 l_magic_number;
2205 assert(p_header_data != 00);
2207 assert(p_manager != 00);
2209 if (jp2->jp2_state != JP2_STATE_NONE) {
2210 opj_event_msg(p_manager, EVT_ERROR, "The signature box must be the first box in the file.\n");
2214 /* assure length of data is correct (4 -> magic number) */
2215 if (p_header_size != 4) {
2216 opj_event_msg(p_manager, EVT_ERROR, "Error with JP signature Box size\n");
2220 /* rearrange data */
2221 opj_read_bytes(p_header_data,&l_magic_number,4);
2222 if (l_magic_number != 0x0d0a870a ) {
2223 opj_event_msg(p_manager, EVT_ERROR, "Error with JP Signature : bad magic number\n");
2227 jp2->jp2_state |= JP2_STATE_SIGNATURE;
2233 * Reads a a FTYP box - File type box
2235 * @param p_header_data the data contained in the FTYP box.
2236 * @param jp2 the jpeg2000 file codec.
2237 * @param p_header_size the size of the data contained in the FTYP box.
2238 * @param p_manager the user event manager.
2240 * @return true if the FTYP box is valid.
2242 static OPJ_BOOL opj_jp2_read_ftyp( opj_jp2_t *jp2,
2243 OPJ_BYTE * p_header_data,
2244 OPJ_UINT32 p_header_size,
2245 opj_event_mgr_t * p_manager
2248 OPJ_UINT32 i, l_remaining_bytes;
2251 assert(p_header_data != 00);
2253 assert(p_manager != 00);
2255 if (jp2->jp2_state != JP2_STATE_SIGNATURE) {
2256 opj_event_msg(p_manager, EVT_ERROR, "The ftyp box must be the second box in the file.\n");
2260 /* assure length of data is correct */
2261 if (p_header_size < 8) {
2262 opj_event_msg(p_manager, EVT_ERROR, "Error with FTYP signature Box size\n");
2266 opj_read_bytes(p_header_data,&jp2->brand,4); /* BR */
2269 opj_read_bytes(p_header_data,&jp2->minversion,4); /* MinV */
2272 l_remaining_bytes = p_header_size - 8;
2274 /* the number of remaining bytes should be a multiple of 4 */
2275 if ((l_remaining_bytes & 0x3) != 0) {
2276 opj_event_msg(p_manager, EVT_ERROR, "Error with FTYP signature Box size\n");
2281 jp2->numcl = l_remaining_bytes >> 2;
2283 jp2->cl = (OPJ_UINT32 *) opj_calloc(jp2->numcl, sizeof(OPJ_UINT32));
2284 if (jp2->cl == 00) {
2285 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory with FTYP Box\n");
2290 for (i = 0; i < jp2->numcl; ++i)
2292 opj_read_bytes(p_header_data,&jp2->cl[i],4); /* CLi */
2296 jp2->jp2_state |= JP2_STATE_FILE_TYPE;
2301 OPJ_BOOL opj_jp2_skip_jp2c( opj_jp2_t *jp2,
2302 opj_stream_private_t *stream,
2303 opj_event_mgr_t * p_manager )
2307 assert(stream != 00);
2308 assert(p_manager != 00);
2310 jp2->j2k_codestream_offset = opj_stream_tell(stream);
2312 if (opj_stream_skip(stream,8,p_manager) != 8) {
2319 static OPJ_BOOL opj_jpip_skip_iptr( opj_jp2_t *jp2,
2320 opj_stream_private_t *stream,
2321 opj_event_mgr_t * p_manager )
2325 assert(stream != 00);
2326 assert(p_manager != 00);
2328 jp2->jpip_iptr_offset = opj_stream_tell(stream);
2330 if (opj_stream_skip(stream,24,p_manager) != 24) {
2338 * Reads the Jpeg2000 file Header box - JP2 Header box (warning, this is a super box).
2340 * @param p_header_data the data contained in the file header box.
2341 * @param jp2 the jpeg2000 file codec.
2342 * @param p_header_size the size of the data contained in the file header box.
2343 * @param p_manager the user event manager.
2345 * @return true if the JP2 Header box was successfully reconized.
2347 static OPJ_BOOL opj_jp2_read_jp2h( opj_jp2_t *jp2,
2348 OPJ_BYTE *p_header_data,
2349 OPJ_UINT32 p_header_size,
2350 opj_event_mgr_t * p_manager
2353 OPJ_UINT32 l_box_size=0, l_current_data_size = 0;
2355 const opj_jp2_header_handler_t * l_current_handler;
2356 OPJ_BOOL l_has_ihdr = 0;
2359 assert(p_header_data != 00);
2361 assert(p_manager != 00);
2363 /* make sure the box is well placed */
2364 if ((jp2->jp2_state & JP2_STATE_FILE_TYPE) != JP2_STATE_FILE_TYPE ) {
2365 opj_event_msg(p_manager, EVT_ERROR, "The box must be the first box in the file.\n");
2369 jp2->jp2_img_state = JP2_IMG_STATE_NONE;
2371 /* iterate while remaining data */
2372 while (p_header_size > 0) {
2374 if (! opj_jp2_read_boxhdr_char(&box,p_header_data,&l_box_size,p_header_size, p_manager)) {
2375 opj_event_msg(p_manager, EVT_ERROR, "Stream error while reading JP2 Header box\n");
2379 if (box.length > p_header_size) {
2380 opj_event_msg(p_manager, EVT_ERROR, "Stream error while reading JP2 Header box: box length is inconsistent.\n");
2384 l_current_handler = opj_jp2_img_find_handler(box.type);
2385 l_current_data_size = box.length - l_box_size;
2386 p_header_data += l_box_size;
2388 if (l_current_handler != 00) {
2389 if (! l_current_handler->handler(jp2,p_header_data,l_current_data_size,p_manager)) {
2394 jp2->jp2_img_state |= JP2_IMG_STATE_UNKNOWN;
2397 if (box.type == JP2_IHDR) {
2401 p_header_data += l_current_data_size;
2402 p_header_size -= box.length;
2405 if (l_has_ihdr == 0) {
2406 opj_event_msg(p_manager, EVT_ERROR, "Stream error while reading JP2 Header box: no 'ihdr' box.\n");
2410 jp2->jp2_state |= JP2_STATE_HEADER;
2415 OPJ_BOOL opj_jp2_read_boxhdr_char( opj_jp2_box_t *box,
2417 OPJ_UINT32 * p_number_bytes_read,
2418 OPJ_UINT32 p_box_max_size,
2419 opj_event_mgr_t * p_manager
2425 assert(p_data != 00);
2427 assert(p_number_bytes_read != 00);
2428 assert(p_manager != 00);
2430 if (p_box_max_size < 8) {
2431 opj_event_msg(p_manager, EVT_ERROR, "Cannot handle box of less than 8 bytes\n");
2435 /* process read data */
2436 opj_read_bytes(p_data, &l_value, 4);
2438 box->length = (OPJ_UINT32)(l_value);
2440 opj_read_bytes(p_data, &l_value, 4);
2442 box->type = (OPJ_UINT32)(l_value);
2444 *p_number_bytes_read = 8;
2446 /* do we have a "special very large box ?" */
2447 /* read then the XLBox */
2448 if (box->length == 1) {
2449 OPJ_UINT32 l_xl_part_size;
2451 if (p_box_max_size < 16) {
2452 opj_event_msg(p_manager, EVT_ERROR, "Cannot handle XL box of less than 16 bytes\n");
2456 opj_read_bytes(p_data,&l_xl_part_size, 4);
2458 *p_number_bytes_read += 4;
2460 if (l_xl_part_size != 0) {
2461 opj_event_msg(p_manager, EVT_ERROR, "Cannot handle box sizes higher than 2^32\n");
2465 opj_read_bytes(p_data, &l_value, 4);
2466 *p_number_bytes_read += 4;
2467 box->length = (OPJ_UINT32)(l_value);
2469 if (box->length == 0) {
2470 opj_event_msg(p_manager, EVT_ERROR, "Cannot handle box of undefined sizes\n");
2474 else if (box->length == 0) {
2475 opj_event_msg(p_manager, EVT_ERROR, "Cannot handle box of undefined sizes\n");
2478 if (box->length < *p_number_bytes_read) {
2479 opj_event_msg(p_manager, EVT_ERROR, "Box length is inconsistent.\n");
2485 OPJ_BOOL opj_jp2_read_header( opj_stream_private_t *p_stream,
2487 opj_image_t ** p_image,
2488 opj_event_mgr_t * p_manager
2493 assert(p_stream != 00);
2494 assert(p_manager != 00);
2496 /* customization of the validation */
2497 opj_jp2_setup_decoding_validation (jp2);
2499 /* customization of the encoding */
2500 opj_jp2_setup_header_reading(jp2);
2502 /* validation of the parameters codec */
2503 if (! opj_jp2_exec(jp2,jp2->m_validation_list,p_stream,p_manager)) {
2508 if (! opj_jp2_exec (jp2,jp2->m_procedure_list,p_stream,p_manager)) {
2512 return opj_j2k_read_header( p_stream,
2518 void opj_jp2_setup_encoding_validation (opj_jp2_t *jp2)
2523 opj_procedure_list_add_procedure(jp2->m_validation_list, (opj_procedure)opj_jp2_default_validation);
2524 /* DEVELOPER CORNER, add your custom validation procedure */
2527 void opj_jp2_setup_decoding_validation (opj_jp2_t *jp2)
2531 /* DEVELOPER CORNER, add your custom validation procedure */
2534 void opj_jp2_setup_header_writing (opj_jp2_t *jp2)
2539 opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)opj_jp2_write_jp );
2540 opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)opj_jp2_write_ftyp );
2541 opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)opj_jp2_write_jp2h );
2543 opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)opj_jpip_skip_iptr );
2544 opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)opj_jp2_skip_jp2c );
2546 /* DEVELOPER CORNER, insert your custom procedures */
2550 void opj_jp2_setup_header_reading (opj_jp2_t *jp2)
2555 opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)opj_jp2_read_header_procedure );
2556 /* DEVELOPER CORNER, add your custom procedures */
2559 OPJ_BOOL opj_jp2_read_tile_header ( opj_jp2_t * p_jp2,
2560 OPJ_UINT32 * p_tile_index,
2561 OPJ_UINT32 * p_data_size,
2562 OPJ_INT32 * p_tile_x0,
2563 OPJ_INT32 * p_tile_y0,
2564 OPJ_INT32 * p_tile_x1,
2565 OPJ_INT32 * p_tile_y1,
2566 OPJ_UINT32 * p_nb_comps,
2568 opj_stream_private_t *p_stream,
2569 opj_event_mgr_t * p_manager
2572 return opj_j2k_read_tile_header(p_jp2->j2k,
2575 p_tile_x0, p_tile_y0,
2576 p_tile_x1, p_tile_y1,
2583 OPJ_BOOL opj_jp2_write_tile ( opj_jp2_t *p_jp2,
2584 OPJ_UINT32 p_tile_index,
2586 OPJ_UINT32 p_data_size,
2587 opj_stream_private_t *p_stream,
2588 opj_event_mgr_t * p_manager
2592 return opj_j2k_write_tile (p_jp2->j2k,p_tile_index,p_data,p_data_size,p_stream,p_manager);
2595 OPJ_BOOL opj_jp2_decode_tile ( opj_jp2_t * p_jp2,
2596 OPJ_UINT32 p_tile_index,
2598 OPJ_UINT32 p_data_size,
2599 opj_stream_private_t *p_stream,
2600 opj_event_mgr_t * p_manager
2603 return opj_j2k_decode_tile (p_jp2->j2k,p_tile_index,p_data,p_data_size,p_stream,p_manager);
2606 void opj_jp2_destroy(opj_jp2_t *jp2)
2609 /* destroy the J2K codec */
2610 opj_j2k_destroy(jp2->j2k);
2614 opj_free(jp2->comps);
2623 if (jp2->color.icc_profile_buf) {
2624 opj_free(jp2->color.icc_profile_buf);
2625 jp2->color.icc_profile_buf = 00;
2628 if (jp2->color.jp2_cdef) {
2629 if (jp2->color.jp2_cdef->info) {
2630 opj_free(jp2->color.jp2_cdef->info);
2631 jp2->color.jp2_cdef->info = NULL;
2634 opj_free(jp2->color.jp2_cdef);
2635 jp2->color.jp2_cdef = 00;
2638 if (jp2->color.jp2_pclr) {
2639 if (jp2->color.jp2_pclr->cmap) {
2640 opj_free(jp2->color.jp2_pclr->cmap);
2641 jp2->color.jp2_pclr->cmap = NULL;
2643 if (jp2->color.jp2_pclr->channel_sign) {
2644 opj_free(jp2->color.jp2_pclr->channel_sign);
2645 jp2->color.jp2_pclr->channel_sign = NULL;
2647 if (jp2->color.jp2_pclr->channel_size) {
2648 opj_free(jp2->color.jp2_pclr->channel_size);
2649 jp2->color.jp2_pclr->channel_size = NULL;
2651 if (jp2->color.jp2_pclr->entries) {
2652 opj_free(jp2->color.jp2_pclr->entries);
2653 jp2->color.jp2_pclr->entries = NULL;
2656 opj_free(jp2->color.jp2_pclr);
2657 jp2->color.jp2_pclr = 00;
2660 if (jp2->m_validation_list) {
2661 opj_procedure_list_destroy(jp2->m_validation_list);
2662 jp2->m_validation_list = 00;
2665 if (jp2->m_procedure_list) {
2666 opj_procedure_list_destroy(jp2->m_procedure_list);
2667 jp2->m_procedure_list = 00;
2674 OPJ_BOOL opj_jp2_set_decode_area( opj_jp2_t *p_jp2,
2675 opj_image_t* p_image,
2676 OPJ_INT32 p_start_x, OPJ_INT32 p_start_y,
2677 OPJ_INT32 p_end_x, OPJ_INT32 p_end_y,
2678 opj_event_mgr_t * p_manager
2681 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);
2684 OPJ_BOOL opj_jp2_get_tile( opj_jp2_t *p_jp2,
2685 opj_stream_private_t *p_stream,
2686 opj_image_t* p_image,
2687 opj_event_mgr_t * p_manager,
2688 OPJ_UINT32 tile_index
2694 opj_event_msg(p_manager, EVT_WARNING, "JP2 box which are after the codestream will not be read by this function.\n");
2696 if (! opj_j2k_get_tile(p_jp2->j2k, p_stream, p_image, p_manager, tile_index) ){
2697 opj_event_msg(p_manager, EVT_ERROR, "Failed to decode the codestream in the JP2 file\n");
2701 if (!opj_jp2_check_color(p_image, &(p_jp2->color), p_manager)) {
2705 /* Set Image Color Space */
2706 if (p_jp2->enumcs == 16)
2707 p_image->color_space = OPJ_CLRSPC_SRGB;
2708 else if (p_jp2->enumcs == 17)
2709 p_image->color_space = OPJ_CLRSPC_GRAY;
2710 else if (p_jp2->enumcs == 18)
2711 p_image->color_space = OPJ_CLRSPC_SYCC;
2713 p_image->color_space = OPJ_CLRSPC_UNKNOWN;
2715 if(p_jp2->color.jp2_pclr) {
2716 /* Part 1, I.5.3.4: Either both or none : */
2717 if( !p_jp2->color.jp2_pclr->cmap)
2718 opj_jp2_free_pclr(&(p_jp2->color));
2720 opj_jp2_apply_pclr(p_image, &(p_jp2->color));
2723 /* Apply the color space if needed */
2724 if(p_jp2->color.jp2_cdef) {
2725 opj_jp2_apply_cdef(p_image, &(p_jp2->color));
2728 if(p_jp2->color.icc_profile_buf) {
2729 p_image->icc_profile_buf = p_jp2->color.icc_profile_buf;
2730 p_image->icc_profile_len = p_jp2->color.icc_profile_len;
2731 p_jp2->color.icc_profile_buf = NULL;
2737 /* ----------------------------------------------------------------------- */
2738 /* JP2 encoder interface */
2739 /* ----------------------------------------------------------------------- */
2741 opj_jp2_t* opj_jp2_create(OPJ_BOOL p_is_decoder)
2743 opj_jp2_t *jp2 = (opj_jp2_t*)opj_calloc(1,sizeof(opj_jp2_t));
2746 /* create the J2K codec */
2747 if (! p_is_decoder) {
2748 jp2->j2k = opj_j2k_create_compress();
2751 jp2->j2k = opj_j2k_create_decompress();
2754 if (jp2->j2k == 00) {
2755 opj_jp2_destroy(jp2);
2759 /* Color structure */
2760 jp2->color.icc_profile_buf = NULL;
2761 jp2->color.icc_profile_len = 0;
2762 jp2->color.jp2_cdef = NULL;
2763 jp2->color.jp2_pclr = NULL;
2764 jp2->color.jp2_has_colr = 0;
2766 /* validation list creation */
2767 jp2->m_validation_list = opj_procedure_list_create();
2768 if (! jp2->m_validation_list) {
2769 opj_jp2_destroy(jp2);
2773 /* execution list creation */
2774 jp2->m_procedure_list = opj_procedure_list_create();
2775 if (! jp2->m_procedure_list) {
2776 opj_jp2_destroy(jp2);
2784 void jp2_dump(opj_jp2_t* p_jp2, OPJ_INT32 flag, FILE* out_stream)
2787 assert(p_jp2 != 00);
2789 j2k_dump(p_jp2->j2k,
2794 opj_codestream_index_t* jp2_get_cstr_index(opj_jp2_t* p_jp2)
2796 return j2k_get_cstr_index(p_jp2->j2k);
2799 opj_codestream_info_v2_t* jp2_get_cstr_info(opj_jp2_t* p_jp2)
2801 return j2k_get_cstr_info(p_jp2->j2k);
2804 OPJ_BOOL opj_jp2_set_decoded_resolution_factor(opj_jp2_t *p_jp2,
2805 OPJ_UINT32 res_factor,
2806 opj_event_mgr_t * p_manager)
2808 return opj_j2k_set_decoded_resolution_factor(p_jp2->j2k, res_factor, p_manager);
2814 static OPJ_BOOL opj_jpip_write_iptr(opj_jp2_t *jp2,
2815 opj_stream_private_t *cio,
2816 opj_event_mgr_t * p_manager )
2818 OPJ_OFF_T j2k_codestream_exit;
2819 OPJ_BYTE l_data_header [24];
2824 assert(p_manager != 00);
2825 assert(opj_stream_has_seek(cio));
2827 j2k_codestream_exit = opj_stream_tell(cio);
2828 opj_write_bytes(l_data_header, 24, 4); /* size of iptr */
2829 opj_write_bytes(l_data_header + 4,JPIP_IPTR,4); /* IPTR */
2831 opj_write_bytes(l_data_header + 4 + 4, 0, 8); /* offset */
2832 opj_write_bytes(l_data_header + 8 + 8, 0, 8); /* length */
2834 opj_write_double(l_data_header + 4 + 4, 0); /* offset */
2835 opj_write_double(l_data_header + 8 + 8, 0); /* length */
2838 if (! opj_stream_seek(cio,jp2->jpip_iptr_offset,p_manager)) {
2839 opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
2843 if (opj_stream_write_data(cio,l_data_header,24,p_manager) != 24) {
2844 opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
2848 if (! opj_stream_seek(cio,j2k_codestream_exit,p_manager)) {
2849 opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
2856 static OPJ_BOOL opj_jpip_write_fidx(opj_jp2_t *jp2,
2857 opj_stream_private_t *cio,
2858 opj_event_mgr_t * p_manager )
2860 OPJ_OFF_T j2k_codestream_exit;
2861 OPJ_BYTE l_data_header [24];
2866 assert(p_manager != 00);
2867 assert(opj_stream_has_seek(cio));
2869 opj_write_bytes(l_data_header, 24, 4); /* size of iptr */
2870 opj_write_bytes(l_data_header + 4,JPIP_FIDX,4); /* IPTR */
2871 opj_write_double(l_data_header + 4 + 4, 0); /* offset */
2872 opj_write_double(l_data_header + 8 + 8, 0); /* length */
2874 if (opj_stream_write_data(cio,l_data_header,24,p_manager) != 24) {
2875 opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
2879 j2k_codestream_exit = opj_stream_tell(cio);
2880 if (! opj_stream_seek(cio,j2k_codestream_exit,p_manager)) {
2881 opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
2888 static OPJ_BOOL opj_jpip_write_cidx(opj_jp2_t *jp2,
2889 opj_stream_private_t *cio,
2890 opj_event_mgr_t * p_manager )
2892 OPJ_OFF_T j2k_codestream_exit;
2893 OPJ_BYTE l_data_header [24];
2898 assert(p_manager != 00);
2899 assert(opj_stream_has_seek(cio));
2901 j2k_codestream_exit = opj_stream_tell(cio);
2902 opj_write_bytes(l_data_header, 24, 4); /* size of iptr */
2903 opj_write_bytes(l_data_header + 4,JPIP_CIDX,4); /* IPTR */
2905 opj_write_bytes(l_data_header + 4 + 4, 0, 8); /* offset */
2906 opj_write_bytes(l_data_header + 8 + 8, 0, 8); /* length */
2908 opj_write_double(l_data_header + 4 + 4, 0); /* offset */
2909 opj_write_double(l_data_header + 8 + 8, 0); /* length */
2912 if (! opj_stream_seek(cio,j2k_codestream_exit,p_manager)) {
2913 opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
2917 if (opj_stream_write_data(cio,l_data_header,24,p_manager) != 24) {
2918 opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
2922 j2k_codestream_exit = opj_stream_tell(cio);
2923 if (! opj_stream_seek(cio,j2k_codestream_exit,p_manager)) {
2924 opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
2932 static void write_prxy( int offset_jp2c, int length_jp2c, int offset_idx, int length_idx, opj_stream_private_t *cio,
2933 opj_event_mgr_t * p_manager )
2935 OPJ_BYTE l_data_header [8];
2936 OPJ_OFF_T len, lenp;
2938 lenp = opj_stream_tell(cio);
2939 opj_stream_skip(cio, 4, p_manager); /* L [at the end] */
2940 opj_write_bytes(l_data_header,JPIP_PRXY,4); /* IPTR */
2941 opj_stream_write_data(cio,l_data_header,4,p_manager);
2943 opj_write_bytes( l_data_header, offset_jp2c, 8); /* OOFF */
2944 opj_stream_write_data(cio,l_data_header,8,p_manager);
2945 opj_write_bytes( l_data_header, length_jp2c, 4); /* OBH part 1 */
2946 opj_write_bytes( l_data_header+4, JP2_JP2C, 4); /* OBH part 2 */
2947 opj_stream_write_data(cio,l_data_header,8,p_manager);
2949 opj_write_bytes( l_data_header, 1, 1);/* NI */
2950 opj_stream_write_data(cio,l_data_header,1,p_manager);
2952 opj_write_bytes( l_data_header, offset_idx, 8); /* IOFF */
2953 opj_stream_write_data(cio,l_data_header,8,p_manager);
2954 opj_write_bytes( l_data_header, length_idx, 4); /* IBH part 1 */
2955 opj_write_bytes( l_data_header+4, JPIP_CIDX, 4); /* IBH part 2 */
2956 opj_stream_write_data(cio,l_data_header,8,p_manager);
2958 len = opj_stream_tell(cio)-lenp;
2959 opj_stream_skip(cio, lenp, p_manager);
2960 opj_write_bytes(l_data_header,len,4);/* L */
2961 opj_stream_write_data(cio,l_data_header,4,p_manager);
2962 opj_stream_seek(cio, lenp+len,p_manager);
2968 static int write_fidx( int offset_jp2c, int length_jp2c, int offset_idx, int length_idx, opj_stream_private_t *cio,
2969 opj_event_mgr_t * p_manager )
2971 OPJ_BYTE l_data_header [4];
2972 OPJ_OFF_T len, lenp;
2974 lenp = opj_stream_tell(cio);
2975 opj_stream_skip(cio, 4, p_manager);
2976 opj_write_bytes(l_data_header,JPIP_FIDX,4); /* FIDX */
2977 opj_stream_write_data(cio,l_data_header,4,p_manager);
2979 write_prxy( offset_jp2c, length_jp2c, offset_idx, length_idx, cio,p_manager);
2981 len = opj_stream_tell(cio)-lenp;
2982 opj_stream_skip(cio, lenp, p_manager);
2983 opj_write_bytes(l_data_header,len,4);/* L */
2984 opj_stream_write_data(cio,l_data_header,4,p_manager);
2985 opj_stream_seek(cio, lenp+len,p_manager);
2990 #endif /* USE_JPIP */