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, false 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, opj_event_mgr_t *);
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 static 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 recognized.
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 file Header box - JP2 Header box (warning, this is a super box).
183 * @param jp2 the jpeg2000 file codec.
184 * @param stream the stream to write data to.
185 * @param p_manager user event manager.
187 * @return true if writing was successful.
189 static OPJ_BOOL opj_jp2_write_jp2h(opj_jp2_t *jp2,
190 opj_stream_private_t *stream,
191 opj_event_mgr_t * p_manager );
194 * Writes the Jpeg2000 codestream Header box - JP2C Header box. This function must be called AFTER the coding has been done.
196 * @param cio the stream to write data to.
197 * @param jp2 the jpeg2000 file codec.
198 * @param p_manager user event manager.
200 * @return true if writing was successful.
202 static OPJ_BOOL opj_jp2_write_jp2c( opj_jp2_t *jp2,
203 opj_stream_private_t *cio,
204 opj_event_mgr_t * p_manager );
208 * Write index Finder box
209 * @param cio the stream to write to.
210 * @param jp2 the jpeg2000 file codec.
211 * @param p_manager user event manager.
213 static OPJ_BOOL opj_jpip_write_iptr( opj_jp2_t *jp2,
214 opj_stream_private_t *cio,
215 opj_event_mgr_t * p_manager );
218 * Write index Finder box
219 * @param cio the stream to write to.
220 * @param jp2 the jpeg2000 file codec.
221 * @param p_manager user event manager.
223 static OPJ_BOOL opj_jpip_write_cidx(opj_jp2_t *jp2,
224 opj_stream_private_t *cio,
225 opj_event_mgr_t * p_manager );
228 * Write file Index (superbox)
229 * @param cio the stream to write to.
230 * @param jp2 the jpeg2000 file codec.
231 * @param p_manager user event manager.
233 static OPJ_BOOL opj_jpip_write_fidx(opj_jp2_t *jp2,
234 opj_stream_private_t *cio,
235 opj_event_mgr_t * p_manager );
236 #endif /* USE_JPIP */
239 * Reads a jpeg2000 file signature box.
241 * @param p_header_data the data contained in the signature box.
242 * @param jp2 the jpeg2000 file codec.
243 * @param p_header_size the size of the data contained in the signature box.
244 * @param p_manager the user event manager.
246 * @return true if the file signature box is valid.
248 static OPJ_BOOL opj_jp2_read_jp(opj_jp2_t *jp2,
249 OPJ_BYTE * p_header_data,
250 OPJ_UINT32 p_header_size,
251 opj_event_mgr_t * p_manager);
254 * Writes a jpeg2000 file signature box.
256 * @param cio the stream to write data to.
257 * @param jp2 the jpeg2000 file codec.
258 * @param p_manager the user event manager.
260 * @return true if writing was successful.
262 static OPJ_BOOL opj_jp2_write_jp( opj_jp2_t *jp2,
263 opj_stream_private_t *cio,
264 opj_event_mgr_t * p_manager );
267 Apply collected palette data
268 @param color Collector for profile, cdef and pclr data
271 static void opj_jp2_apply_pclr(opj_image_t *image, opj_jp2_color_t *color);
273 static void opj_jp2_free_pclr(opj_jp2_color_t *color);
276 * Collect palette data
278 * @param jp2 JP2 handle
279 * @param p_pclr_header_data FIXME DOC
280 * @param p_pclr_header_size FIXME DOC
283 * @return Returns true if successful, returns false otherwise
285 static OPJ_BOOL opj_jp2_read_pclr( opj_jp2_t *jp2,
286 OPJ_BYTE * p_pclr_header_data,
287 OPJ_UINT32 p_pclr_header_size,
288 opj_event_mgr_t * p_manager );
291 * Collect component mapping data
293 * @param jp2 JP2 handle
294 * @param p_cmap_header_data FIXME DOC
295 * @param p_cmap_header_size FIXME DOC
296 * @param p_manager FIXME DOC
298 * @return Returns true if successful, returns false otherwise
301 static OPJ_BOOL opj_jp2_read_cmap( opj_jp2_t * jp2,
302 OPJ_BYTE * p_cmap_header_data,
303 OPJ_UINT32 p_cmap_header_size,
304 opj_event_mgr_t * p_manager );
307 * Reads the Color Specification box.
309 * @param p_colr_header_data pointer to actual data (already read from file)
310 * @param jp2 the jpeg2000 file codec.
311 * @param p_colr_header_size the size of the color header
312 * @param p_manager the user event manager.
314 * @return true if the bpc header is valid, false else.
316 static OPJ_BOOL opj_jp2_read_colr( opj_jp2_t *jp2,
317 OPJ_BYTE * p_colr_header_data,
318 OPJ_UINT32 p_colr_header_size,
319 opj_event_mgr_t * p_manager );
326 * Sets up the procedures to do on writing header after the codestream.
327 * Developpers wanting to extend the library can add their own writing procedures.
329 static OPJ_BOOL opj_jp2_setup_end_header_writing (opj_jp2_t *jp2, opj_event_mgr_t * p_manager);
332 * Sets up the procedures to do on reading header after the codestream.
333 * Developpers wanting to extend the library can add their own writing procedures.
335 static OPJ_BOOL opj_jp2_setup_end_header_reading (opj_jp2_t *jp2, opj_event_mgr_t * p_manager);
338 * Reads a jpeg2000 file header structure.
340 * @param jp2 the jpeg2000 file header structure.
341 * @param stream the stream to read data from.
342 * @param p_manager the user event manager.
344 * @return true if the box is valid.
346 static OPJ_BOOL opj_jp2_read_header_procedure( opj_jp2_t *jp2,
347 opj_stream_private_t *stream,
348 opj_event_mgr_t * p_manager );
351 * Excutes the given procedures on the given codec.
353 * @param p_procedure_list the list of procedures to execute
354 * @param jp2 the jpeg2000 file codec to execute the procedures on.
355 * @param stream the stream to execute the procedures on.
356 * @param p_manager the user manager.
358 * @return true if all the procedures were successfully executed.
360 static OPJ_BOOL opj_jp2_exec ( opj_jp2_t * jp2,
361 opj_procedure_list_t * p_procedure_list,
362 opj_stream_private_t *stream,
363 opj_event_mgr_t * p_manager );
366 * Reads a box header. The box is the way data is packed inside a jpeg2000 file structure.
368 * @param cio the input stream to read data from.
369 * @param box the box structure to fill.
370 * @param p_number_bytes_read pointer to an int that will store the number of bytes read from the stream (shoul usually be 2).
371 * @param p_manager user event manager.
373 * @return true if the box is recognized, false otherwise
375 static OPJ_BOOL opj_jp2_read_boxhdr(opj_jp2_box_t *box,
376 OPJ_UINT32 * p_number_bytes_read,
377 opj_stream_private_t *cio,
378 opj_event_mgr_t * p_manager);
381 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
382 * are valid. Developpers wanting to extend the library can add their own validation procedures.
384 static OPJ_BOOL opj_jp2_setup_encoding_validation (opj_jp2_t *jp2, opj_event_mgr_t * p_manager);
387 * Sets up the procedures to do on writing header. Developpers wanting to extend the library can add their own writing procedures.
389 static OPJ_BOOL opj_jp2_setup_header_writing (opj_jp2_t *jp2, opj_event_mgr_t * p_manager);
391 static OPJ_BOOL opj_jp2_default_validation ( opj_jp2_t * jp2,
392 opj_stream_private_t *cio,
393 opj_event_mgr_t * p_manager );
396 * Finds the image execution function related to the given box id.
398 * @param p_id the id of the handler to fetch.
400 * @return the given handler or NULL if it could not be found.
402 static const opj_jp2_header_handler_t * opj_jp2_img_find_handler (OPJ_UINT32 p_id);
405 * Finds the execution function related to the given box id.
407 * @param p_id the id of the handler to fetch.
409 * @return the given handler or NULL if it could not be found.
411 static const opj_jp2_header_handler_t * opj_jp2_find_handler (OPJ_UINT32 p_id );
413 static const opj_jp2_header_handler_t jp2_header [] =
415 {JP2_JP,opj_jp2_read_jp},
416 {JP2_FTYP,opj_jp2_read_ftyp},
417 {JP2_JP2H,opj_jp2_read_jp2h}
420 static const opj_jp2_header_handler_t jp2_img_header [] =
422 {JP2_IHDR,opj_jp2_read_ihdr},
423 {JP2_COLR,opj_jp2_read_colr},
424 {JP2_BPCC,opj_jp2_read_bpcc},
425 {JP2_PCLR,opj_jp2_read_pclr},
426 {JP2_CMAP,opj_jp2_read_cmap},
427 {JP2_CDEF,opj_jp2_read_cdef}
432 * Reads a box header. The box is the way data is packed inside a jpeg2000 file structure. Data is read from a character string
434 * @param box the box structure to fill.
435 * @param p_data the character string to read data from.
436 * @param p_number_bytes_read pointer to an int that will store the number of bytes read from the stream (shoul usually be 2).
437 * @param p_box_max_size the maximum number of bytes in the box.
438 * @param p_manager FIXME DOC
440 * @return true if the box is recognized, false otherwise
442 static OPJ_BOOL opj_jp2_read_boxhdr_char( opj_jp2_box_t *box,
444 OPJ_UINT32 * p_number_bytes_read,
445 OPJ_UINT32 p_box_max_size,
446 opj_event_mgr_t * p_manager );
449 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
450 * are valid. Developpers wanting to extend the library can add their own validation procedures.
452 static OPJ_BOOL opj_jp2_setup_decoding_validation (opj_jp2_t *jp2, opj_event_mgr_t * p_manager);
455 * Sets up the procedures to do on reading header.
456 * Developpers wanting to extend the library can add their own writing procedures.
458 static OPJ_BOOL opj_jp2_setup_header_reading (opj_jp2_t *jp2, opj_event_mgr_t * p_manager);
460 /* ----------------------------------------------------------------------- */
461 static OPJ_BOOL opj_jp2_read_boxhdr(opj_jp2_box_t *box,
462 OPJ_UINT32 * p_number_bytes_read,
463 opj_stream_private_t *cio,
464 opj_event_mgr_t * p_manager )
466 /* read header from file */
467 OPJ_BYTE l_data_header [8];
472 assert(p_number_bytes_read != 00);
473 assert(p_manager != 00);
475 *p_number_bytes_read = (OPJ_UINT32)opj_stream_read_data(cio,l_data_header,8,p_manager);
476 if (*p_number_bytes_read != 8) {
480 /* process read data */
481 opj_read_bytes(l_data_header,&(box->length), 4);
482 opj_read_bytes(l_data_header+4,&(box->type), 4);
484 if(box->length == 0)/* last box */
486 const OPJ_OFF_T bleft = opj_stream_get_number_byte_left(cio);
487 if (bleft > (OPJ_OFF_T)(0xFFFFFFFFU - 8U)) {
488 opj_event_msg(p_manager, EVT_ERROR, "Cannot handle box sizes higher than 2^32\n");
491 box->length = (OPJ_UINT32)bleft + 8U;
492 assert( (OPJ_OFF_T)box->length == bleft + 8 );
496 /* do we have a "special very large box ?" */
497 /* read then the XLBox */
498 if (box->length == 1) {
499 OPJ_UINT32 l_xl_part_size;
501 OPJ_UINT32 l_nb_bytes_read = (OPJ_UINT32)opj_stream_read_data(cio,l_data_header,8,p_manager);
502 if (l_nb_bytes_read != 8) {
503 if (l_nb_bytes_read > 0) {
504 *p_number_bytes_read += l_nb_bytes_read;
510 *p_number_bytes_read = 16;
511 opj_read_bytes(l_data_header,&l_xl_part_size, 4);
512 if (l_xl_part_size != 0) {
513 opj_event_msg(p_manager, EVT_ERROR, "Cannot handle box sizes higher than 2^32\n");
516 opj_read_bytes(l_data_header+4,&(box->length), 4);
522 static void jp2_write_url(opj_cio_t *cio, char *Idx_file) {
526 box.init_pos = cio_tell(cio);
528 cio_write(cio, JP2_URL, 4); /* DBTL */
529 cio_write(cio, 0, 1); /* VERS */
530 cio_write(cio, 0, 3); /* FLAG */
533 for (i = 0; i < strlen(Idx_file); i++) {
534 cio_write(cio, Idx_file[i], 1);
538 box.length = cio_tell(cio) - box.init_pos;
539 cio_seek(cio, box.init_pos);
540 cio_write(cio, box.length, 4); /* L */
541 cio_seek(cio, box.init_pos + box.length);
545 static OPJ_BOOL opj_jp2_read_ihdr( opj_jp2_t *jp2,
546 OPJ_BYTE *p_image_header_data,
547 OPJ_UINT32 p_image_header_size,
548 opj_event_mgr_t * p_manager )
551 assert(p_image_header_data != 00);
553 assert(p_manager != 00);
555 if (jp2->comps != NULL) {
556 opj_event_msg(p_manager, EVT_WARNING, "Ignoring ihdr box. First ihdr box already read\n");
560 if (p_image_header_size != 14) {
561 opj_event_msg(p_manager, EVT_ERROR, "Bad image header box (bad size)\n");
565 opj_read_bytes(p_image_header_data,&(jp2->h),4); /* HEIGHT */
566 p_image_header_data += 4;
567 opj_read_bytes(p_image_header_data,&(jp2->w),4); /* WIDTH */
568 p_image_header_data += 4;
569 opj_read_bytes(p_image_header_data,&(jp2->numcomps),2); /* NC */
570 p_image_header_data += 2;
572 if ((jp2->numcomps - 1U) >= 16384U) { /* unsigned underflow is well defined: 1U <= jp2->numcomps <= 16384U */
573 opj_event_msg(p_manager, EVT_ERROR, "Invalid number of components (ihdr)\n");
577 /* allocate memory for components */
578 jp2->comps = (opj_jp2_comps_t*) opj_calloc(jp2->numcomps, sizeof(opj_jp2_comps_t));
579 if (jp2->comps == 0) {
580 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to handle image header (ihdr)\n");
584 opj_read_bytes(p_image_header_data,&(jp2->bpc),1); /* BPC */
585 ++ p_image_header_data;
587 opj_read_bytes(p_image_header_data,&(jp2->C),1); /* C */
588 ++ p_image_header_data;
590 /* Should be equal to 7 cf. chapter about image header box of the norm */
592 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);
595 opj_read_bytes(p_image_header_data,&(jp2->UnkC),1); /* UnkC */
596 ++ p_image_header_data;
597 opj_read_bytes(p_image_header_data,&(jp2->IPR),1); /* IPR */
598 ++ p_image_header_data;
603 static OPJ_BYTE * opj_jp2_write_ihdr(opj_jp2_t *jp2,
604 OPJ_UINT32 * p_nb_bytes_written
607 OPJ_BYTE * l_ihdr_data,* l_current_ihdr_ptr;
611 assert(p_nb_bytes_written != 00);
613 /* default image header is 22 bytes wide */
614 l_ihdr_data = (OPJ_BYTE *) opj_calloc(1,22);
615 if (l_ihdr_data == 00) {
619 l_current_ihdr_ptr = l_ihdr_data;
621 opj_write_bytes(l_current_ihdr_ptr,22,4); /* write box size */
622 l_current_ihdr_ptr+=4;
624 opj_write_bytes(l_current_ihdr_ptr,JP2_IHDR, 4); /* IHDR */
625 l_current_ihdr_ptr+=4;
627 opj_write_bytes(l_current_ihdr_ptr,jp2->h, 4); /* HEIGHT */
628 l_current_ihdr_ptr+=4;
630 opj_write_bytes(l_current_ihdr_ptr, jp2->w, 4); /* WIDTH */
631 l_current_ihdr_ptr+=4;
633 opj_write_bytes(l_current_ihdr_ptr, jp2->numcomps, 2); /* NC */
634 l_current_ihdr_ptr+=2;
636 opj_write_bytes(l_current_ihdr_ptr, jp2->bpc, 1); /* BPC */
637 ++l_current_ihdr_ptr;
639 opj_write_bytes(l_current_ihdr_ptr, jp2->C, 1); /* C : Always 7 */
640 ++l_current_ihdr_ptr;
642 opj_write_bytes(l_current_ihdr_ptr, jp2->UnkC, 1); /* UnkC, colorspace unknown */
643 ++l_current_ihdr_ptr;
645 opj_write_bytes(l_current_ihdr_ptr, jp2->IPR, 1); /* IPR, no intellectual property */
646 ++l_current_ihdr_ptr;
648 *p_nb_bytes_written = 22;
653 static OPJ_BYTE * opj_jp2_write_bpcc( opj_jp2_t *jp2,
654 OPJ_UINT32 * p_nb_bytes_written
658 /* room for 8 bytes for box and 1 byte for each component */
659 OPJ_UINT32 l_bpcc_size;
660 OPJ_BYTE * l_bpcc_data,* l_current_bpcc_ptr;
664 assert(p_nb_bytes_written != 00);
665 l_bpcc_size = 8 + jp2->numcomps;
667 l_bpcc_data = (OPJ_BYTE *) opj_calloc(1,l_bpcc_size);
668 if (l_bpcc_data == 00) {
672 l_current_bpcc_ptr = l_bpcc_data;
674 opj_write_bytes(l_current_bpcc_ptr,l_bpcc_size,4); /* write box size */
675 l_current_bpcc_ptr += 4;
677 opj_write_bytes(l_current_bpcc_ptr,JP2_BPCC,4); /* BPCC */
678 l_current_bpcc_ptr += 4;
680 for (i = 0; i < jp2->numcomps; ++i) {
681 opj_write_bytes(l_current_bpcc_ptr, jp2->comps[i].bpcc, 1); /* write each component information */
682 ++l_current_bpcc_ptr;
685 *p_nb_bytes_written = l_bpcc_size;
690 static OPJ_BOOL opj_jp2_read_bpcc( opj_jp2_t *jp2,
691 OPJ_BYTE * p_bpc_header_data,
692 OPJ_UINT32 p_bpc_header_size,
693 opj_event_mgr_t * p_manager
699 assert(p_bpc_header_data != 00);
701 assert(p_manager != 00);
704 if (jp2->bpc != 255 ){
705 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);
708 /* and length is relevant */
709 if (p_bpc_header_size != jp2->numcomps) {
710 opj_event_msg(p_manager, EVT_ERROR, "Bad BPCC header box (bad size)\n");
714 /* read info for each component */
715 for (i = 0; i < jp2->numcomps; ++i) {
716 opj_read_bytes(p_bpc_header_data,&jp2->comps[i].bpcc ,1); /* read each BPCC component */
722 static OPJ_BYTE * opj_jp2_write_cdef(opj_jp2_t *jp2, OPJ_UINT32 * p_nb_bytes_written)
724 /* room for 8 bytes for box, 2 for n */
725 OPJ_UINT32 l_cdef_size = 10;
726 OPJ_BYTE * l_cdef_data,* l_current_cdef_ptr;
732 assert(p_nb_bytes_written != 00);
733 assert(jp2->color.jp2_cdef != 00);
734 assert(jp2->color.jp2_cdef->info != 00);
735 assert(jp2->color.jp2_cdef->n > 0U);
737 l_cdef_size += 6U * jp2->color.jp2_cdef->n;
739 l_cdef_data = (OPJ_BYTE *) opj_malloc(l_cdef_size);
740 if (l_cdef_data == 00) {
744 l_current_cdef_ptr = l_cdef_data;
746 opj_write_bytes(l_current_cdef_ptr,l_cdef_size,4); /* write box size */
747 l_current_cdef_ptr += 4;
749 opj_write_bytes(l_current_cdef_ptr,JP2_CDEF,4); /* BPCC */
750 l_current_cdef_ptr += 4;
752 l_value = jp2->color.jp2_cdef->n;
753 opj_write_bytes(l_current_cdef_ptr,l_value,2); /* N */
754 l_current_cdef_ptr += 2;
756 for (i = 0U; i < jp2->color.jp2_cdef->n; ++i) {
757 l_value = jp2->color.jp2_cdef->info[i].cn;
758 opj_write_bytes(l_current_cdef_ptr,l_value,2); /* Cni */
759 l_current_cdef_ptr += 2;
760 l_value = jp2->color.jp2_cdef->info[i].typ;
761 opj_write_bytes(l_current_cdef_ptr,l_value,2); /* Typi */
762 l_current_cdef_ptr += 2;
763 l_value = jp2->color.jp2_cdef->info[i].asoc;
764 opj_write_bytes(l_current_cdef_ptr,l_value,2); /* Asoci */
765 l_current_cdef_ptr += 2;
767 *p_nb_bytes_written = l_cdef_size;
772 static OPJ_BYTE * opj_jp2_write_colr( opj_jp2_t *jp2,
773 OPJ_UINT32 * p_nb_bytes_written
776 /* room for 8 bytes for box 3 for common data and variable upon profile*/
777 OPJ_UINT32 l_colr_size = 11;
778 OPJ_BYTE * l_colr_data,* l_current_colr_ptr;
782 assert(p_nb_bytes_written != 00);
783 assert(jp2->meth == 1 || jp2->meth == 2);
787 l_colr_size += 4; /* EnumCS */
790 assert(jp2->color.icc_profile_len); /* ICC profile */
791 l_colr_size += jp2->color.icc_profile_len;
797 l_colr_data = (OPJ_BYTE *) opj_calloc(1,l_colr_size);
798 if (l_colr_data == 00) {
802 l_current_colr_ptr = l_colr_data;
804 opj_write_bytes(l_current_colr_ptr,l_colr_size,4); /* write box size */
805 l_current_colr_ptr += 4;
807 opj_write_bytes(l_current_colr_ptr,JP2_COLR,4); /* BPCC */
808 l_current_colr_ptr += 4;
810 opj_write_bytes(l_current_colr_ptr, jp2->meth,1); /* METH */
811 ++l_current_colr_ptr;
813 opj_write_bytes(l_current_colr_ptr, jp2->precedence,1); /* PRECEDENCE */
814 ++l_current_colr_ptr;
816 opj_write_bytes(l_current_colr_ptr, jp2->approx,1); /* APPROX */
817 ++l_current_colr_ptr;
819 if (jp2->meth == 1) { /* Meth value is restricted to 1 or 2 (Table I.9 of part 1) */
820 opj_write_bytes(l_current_colr_ptr, jp2->enumcs,4); } /* EnumCS */
822 if (jp2->meth == 2) { /* ICC profile */
824 for(i = 0; i < jp2->color.icc_profile_len; ++i) {
825 opj_write_bytes(l_current_colr_ptr, jp2->color.icc_profile_buf[i], 1);
826 ++l_current_colr_ptr;
831 *p_nb_bytes_written = l_colr_size;
836 static void opj_jp2_free_pclr(opj_jp2_color_t *color)
838 opj_free(color->jp2_pclr->channel_sign);
839 opj_free(color->jp2_pclr->channel_size);
840 opj_free(color->jp2_pclr->entries);
842 if(color->jp2_pclr->cmap) opj_free(color->jp2_pclr->cmap);
844 opj_free(color->jp2_pclr); color->jp2_pclr = NULL;
847 static OPJ_BOOL opj_jp2_check_color(opj_image_t *image, opj_jp2_color_t *color, opj_event_mgr_t *p_manager)
851 /* testcase 4149.pdf.SIGSEGV.cf7.3501 */
852 if (color->jp2_cdef) {
853 opj_jp2_cdef_info_t *info = color->jp2_cdef->info;
854 OPJ_UINT16 n = color->jp2_cdef->n;
855 OPJ_UINT32 nr_channels = image->numcomps; /* FIXME image->numcomps == jp2->numcomps before color is applied ??? */
857 /* cdef applies to cmap channels if any */
858 if (color->jp2_pclr && color->jp2_pclr->cmap) {
859 nr_channels = (OPJ_UINT32)color->jp2_pclr->nr_channels;
862 for (i = 0; i < n; i++) {
863 if (info[i].cn >= nr_channels) {
864 opj_event_msg(p_manager, EVT_ERROR, "Invalid component index %d (>= %d).\n", info[i].cn, nr_channels);
867 if (info[i].asoc == 65535U) continue;
869 if (info[i].asoc > 0 && (OPJ_UINT32)(info[i].asoc - 1) >= nr_channels) {
870 opj_event_msg(p_manager, EVT_ERROR, "Invalid component index %d (>= %d).\n", info[i].asoc - 1, nr_channels);
876 /* ISO 15444-1 states that if cdef is present, it shall contain a complete list of channel definitions. */
877 while (nr_channels > 0)
879 for(i = 0; i < n; ++i) {
880 if ((OPJ_UINT32)info[i].cn == (nr_channels - 1U)) {
885 opj_event_msg(p_manager, EVT_ERROR, "Incomplete channel definitions.\n");
892 /* testcases 451.pdf.SIGSEGV.f4c.3723, 451.pdf.SIGSEGV.5b5.3723 and
893 66ea31acbb0f23a2bbc91f64d69a03f5_signal_sigsegv_13937c0_7030_5725.pdf */
894 if (color->jp2_pclr && color->jp2_pclr->cmap) {
895 OPJ_UINT16 nr_channels = color->jp2_pclr->nr_channels;
896 opj_jp2_cmap_comp_t *cmap = color->jp2_pclr->cmap;
897 OPJ_BOOL *pcol_usage, is_sane = OPJ_TRUE;
899 /* verify that all original components match an existing one */
900 for (i = 0; i < nr_channels; i++) {
901 if (cmap[i].cmp >= image->numcomps) {
902 opj_event_msg(p_manager, EVT_ERROR, "Invalid component index %d (>= %d).\n", cmap[i].cmp, image->numcomps);
907 pcol_usage = (OPJ_BOOL *) opj_calloc(nr_channels, sizeof(OPJ_BOOL));
909 opj_event_msg(p_manager, EVT_ERROR, "Unexpected OOM.\n");
912 /* verify that no component is targeted more than once */
913 for (i = 0; i < nr_channels; i++) {
914 OPJ_UINT16 pcol = cmap[i].pcol;
915 assert(cmap[i].mtyp == 0 || cmap[i].mtyp == 1);
916 if (pcol >= nr_channels) {
917 opj_event_msg(p_manager, EVT_ERROR, "Invalid component/palette index for direct mapping %d.\n", pcol);
920 else if (pcol_usage[pcol] && cmap[i].mtyp == 1) {
921 opj_event_msg(p_manager, EVT_ERROR, "Component %d is mapped twice.\n", pcol);
924 else if (cmap[i].mtyp == 0 && cmap[i].pcol != 0) {
925 /* I.5.3.5 PCOL: If the value of the MTYP field for this channel is 0, then
926 * the value of this field shall be 0. */
927 opj_event_msg(p_manager, EVT_ERROR, "Direct use at #%d however pcol=%d.\n", i, pcol);
931 pcol_usage[pcol] = OPJ_TRUE;
933 /* verify that all components are targeted at least once */
934 for (i = 0; i < nr_channels; i++) {
935 if (!pcol_usage[i] && cmap[i].mtyp != 0) {
936 opj_event_msg(p_manager, EVT_ERROR, "Component %d doesn't have a mapping.\n", i);
940 /* Issue 235/447 weird cmap */
941 if (1 && is_sane && (image->numcomps==1U)) {
942 for (i = 0; i < nr_channels; i++) {
943 if (!pcol_usage[i]) {
945 opj_event_msg(p_manager, EVT_WARNING, "Component mapping seems wrong. Trying to correct.\n", i);
951 for (i = 0; i < nr_channels; i++) {
953 cmap[i].pcol = (OPJ_BYTE) i;
957 opj_free(pcol_usage);
967 static void opj_jp2_apply_pclr(opj_image_t *image, opj_jp2_color_t *color)
969 opj_image_comp_t *old_comps, *new_comps;
970 OPJ_BYTE *channel_size, *channel_sign;
972 opj_jp2_cmap_comp_t *cmap;
973 OPJ_INT32 *src, *dst;
975 OPJ_UINT16 i, nr_channels, cmp, pcol;
978 channel_size = color->jp2_pclr->channel_size;
979 channel_sign = color->jp2_pclr->channel_sign;
980 entries = color->jp2_pclr->entries;
981 cmap = color->jp2_pclr->cmap;
982 nr_channels = color->jp2_pclr->nr_channels;
984 old_comps = image->comps;
985 new_comps = (opj_image_comp_t*)
986 opj_malloc(nr_channels * sizeof(opj_image_comp_t));
988 /* FIXME no error code for opj_jp2_apply_pclr */
989 /* FIXME event manager error callback */
992 for(i = 0; i < nr_channels; ++i) {
993 pcol = cmap[i].pcol; cmp = cmap[i].cmp;
996 if(cmap[i].mtyp == 0){
998 new_comps[i] = old_comps[cmp];
1000 assert( i == pcol );
1001 new_comps[pcol] = old_comps[cmp];
1004 /* Palette mapping: */
1005 new_comps[i].data = (OPJ_INT32*)
1006 opj_malloc(old_comps[cmp].w * old_comps[cmp].h * sizeof(OPJ_INT32));
1007 if (!new_comps[i].data) {
1008 opj_free(new_comps);
1010 /* FIXME no error code for opj_jp2_apply_pclr */
1011 /* FIXME event manager error callback */
1014 new_comps[i].prec = channel_size[i];
1015 new_comps[i].sgnd = channel_sign[i];
1018 top_k = color->jp2_pclr->nr_entries - 1;
1020 for(i = 0; i < nr_channels; ++i) {
1021 /* Palette mapping: */
1022 cmp = cmap[i].cmp; pcol = cmap[i].pcol;
1023 src = old_comps[cmp].data;
1025 max = new_comps[pcol].w * new_comps[pcol].h;
1028 if(cmap[i].mtyp == 0) {
1030 dst = new_comps[i].data;
1032 for(j = 0; j < max; ++j) {
1037 assert( i == pcol );
1038 dst = new_comps[pcol].data;
1040 for(j = 0; j < max; ++j) {
1042 if((k = src[j]) < 0) k = 0; else if(k > top_k) k = top_k;
1045 dst[j] = (OPJ_INT32)entries[k * nr_channels + pcol];
1050 max = image->numcomps;
1051 for(i = 0; i < max; ++i) {
1052 if(old_comps[i].data) opj_free(old_comps[i].data);
1055 opj_free(old_comps);
1056 image->comps = new_comps;
1057 image->numcomps = nr_channels;
1059 opj_jp2_free_pclr(color);
1063 static OPJ_BOOL opj_jp2_read_pclr( opj_jp2_t *jp2,
1064 OPJ_BYTE * p_pclr_header_data,
1065 OPJ_UINT32 p_pclr_header_size,
1066 opj_event_mgr_t * p_manager
1069 opj_jp2_pclr_t *jp2_pclr;
1070 OPJ_BYTE *channel_size, *channel_sign;
1071 OPJ_UINT32 *entries;
1072 OPJ_UINT16 nr_entries,nr_channels;
1075 OPJ_BYTE *orig_header_data = p_pclr_header_data;
1078 assert(p_pclr_header_data != 00);
1080 assert(p_manager != 00);
1081 (void)p_pclr_header_size;
1083 if(jp2->color.jp2_pclr)
1086 if (p_pclr_header_size < 3)
1089 opj_read_bytes(p_pclr_header_data, &l_value , 2); /* NE */
1090 p_pclr_header_data += 2;
1091 nr_entries = (OPJ_UINT16) l_value;
1092 if ((nr_entries == 0U) || (nr_entries > 1024U)) {
1093 opj_event_msg(p_manager, EVT_ERROR, "Invalid PCLR box. Reports %d entries\n", (int)nr_entries);
1097 opj_read_bytes(p_pclr_header_data, &l_value , 1); /* NPC */
1098 ++p_pclr_header_data;
1099 nr_channels = (OPJ_UINT16) l_value;
1100 if (nr_channels == 0U) {
1101 opj_event_msg(p_manager, EVT_ERROR, "Invalid PCLR box. Reports 0 palette columns\n");
1105 if (p_pclr_header_size < 3 + (OPJ_UINT32)nr_channels)
1108 entries = (OPJ_UINT32*) opj_malloc((size_t)nr_channels * nr_entries * sizeof(OPJ_UINT32));
1111 channel_size = (OPJ_BYTE*) opj_malloc(nr_channels);
1117 channel_sign = (OPJ_BYTE*) opj_malloc(nr_channels);
1121 opj_free(channel_size);
1125 jp2_pclr = (opj_jp2_pclr_t*)opj_malloc(sizeof(opj_jp2_pclr_t));
1129 opj_free(channel_size);
1130 opj_free(channel_sign);
1134 jp2_pclr->channel_sign = channel_sign;
1135 jp2_pclr->channel_size = channel_size;
1136 jp2_pclr->entries = entries;
1137 jp2_pclr->nr_entries = nr_entries;
1138 jp2_pclr->nr_channels = (OPJ_BYTE) l_value;
1139 jp2_pclr->cmap = NULL;
1141 jp2->color.jp2_pclr = jp2_pclr;
1143 for(i = 0; i < nr_channels; ++i) {
1144 opj_read_bytes(p_pclr_header_data, &l_value , 1); /* Bi */
1145 ++p_pclr_header_data;
1147 channel_size[i] = (OPJ_BYTE)((l_value & 0x7f) + 1);
1148 channel_sign[i] = (l_value & 0x80) ? 1 : 0;
1151 for(j = 0; j < nr_entries; ++j) {
1152 for(i = 0; i < nr_channels; ++i) {
1153 OPJ_UINT32 bytes_to_read = (OPJ_UINT32)((channel_size[i]+7)>>3);
1155 if (bytes_to_read > sizeof(OPJ_UINT32))
1156 bytes_to_read = sizeof(OPJ_UINT32);
1157 if ((ptrdiff_t)p_pclr_header_size < (ptrdiff_t)(p_pclr_header_data - orig_header_data) + (ptrdiff_t)bytes_to_read)
1160 opj_read_bytes(p_pclr_header_data, &l_value , bytes_to_read); /* Cji */
1161 p_pclr_header_data += bytes_to_read;
1162 *entries = (OPJ_UINT32) l_value;
1170 static OPJ_BOOL opj_jp2_read_cmap( opj_jp2_t * jp2,
1171 OPJ_BYTE * p_cmap_header_data,
1172 OPJ_UINT32 p_cmap_header_size,
1173 opj_event_mgr_t * p_manager
1176 opj_jp2_cmap_comp_t *cmap;
1177 OPJ_BYTE i, nr_channels;
1182 assert(p_cmap_header_data != 00);
1183 assert(p_manager != 00);
1184 (void)p_cmap_header_size;
1186 /* Need nr_channels: */
1187 if(jp2->color.jp2_pclr == NULL) {
1188 opj_event_msg(p_manager, EVT_ERROR, "Need to read a PCLR box before the CMAP box.\n");
1192 /* Part 1, I.5.3.5: 'There shall be at most one Component Mapping box
1193 * inside a JP2 Header box' :
1195 if(jp2->color.jp2_pclr->cmap) {
1196 opj_event_msg(p_manager, EVT_ERROR, "Only one CMAP box is allowed.\n");
1200 nr_channels = jp2->color.jp2_pclr->nr_channels;
1201 if (p_cmap_header_size < (OPJ_UINT32)nr_channels * 4) {
1202 opj_event_msg(p_manager, EVT_ERROR, "Insufficient data for CMAP box.\n");
1206 cmap = (opj_jp2_cmap_comp_t*) opj_malloc(nr_channels * sizeof(opj_jp2_cmap_comp_t));
1211 for(i = 0; i < nr_channels; ++i) {
1212 opj_read_bytes(p_cmap_header_data, &l_value, 2); /* CMP^i */
1213 p_cmap_header_data +=2;
1214 cmap[i].cmp = (OPJ_UINT16) l_value;
1216 opj_read_bytes(p_cmap_header_data, &l_value, 1); /* MTYP^i */
1217 ++p_cmap_header_data;
1218 cmap[i].mtyp = (OPJ_BYTE) l_value;
1220 opj_read_bytes(p_cmap_header_data, &l_value, 1); /* PCOL^i */
1221 ++p_cmap_header_data;
1222 cmap[i].pcol = (OPJ_BYTE) l_value;
1225 jp2->color.jp2_pclr->cmap = cmap;
1230 static void opj_jp2_apply_cdef(opj_image_t *image, opj_jp2_color_t *color, opj_event_mgr_t *manager)
1232 opj_jp2_cdef_info_t *info;
1233 OPJ_UINT16 i, n, cn, asoc, acn;
1235 info = color->jp2_cdef->info;
1236 n = color->jp2_cdef->n;
1238 for(i = 0; i < n; ++i)
1240 /* WATCH: acn = asoc - 1 ! */
1241 asoc = info[i].asoc;
1244 if( cn >= image->numcomps)
1246 opj_event_msg(manager, EVT_WARNING, "opj_jp2_apply_cdef: cn=%d, numcomps=%d\n", cn, image->numcomps);
1249 if(asoc == 0 || asoc == 65535)
1251 image->comps[cn].alpha = info[i].typ;
1255 acn = (OPJ_UINT16)(asoc - 1);
1256 if( acn >= image->numcomps )
1258 opj_event_msg(manager, EVT_WARNING, "opj_jp2_apply_cdef: acn=%d, numcomps=%d\n", acn, image->numcomps);
1262 /* Swap only if color channel */
1263 if((cn != acn) && (info[i].typ == 0))
1265 opj_image_comp_t saved;
1268 memcpy(&saved, &image->comps[cn], sizeof(opj_image_comp_t));
1269 memcpy(&image->comps[cn], &image->comps[acn], sizeof(opj_image_comp_t));
1270 memcpy(&image->comps[acn], &saved, sizeof(opj_image_comp_t));
1272 /* Swap channels in following channel definitions, don't bother with j <= i that are already processed */
1273 for (j = (OPJ_UINT16)(i + 1U); j < n ; ++j)
1275 if (info[j].cn == cn) {
1278 else if (info[j].cn == acn) {
1281 /* asoc is related to color index. Do not update. */
1285 image->comps[cn].alpha = info[i].typ;
1288 if(color->jp2_cdef->info) opj_free(color->jp2_cdef->info);
1290 opj_free(color->jp2_cdef); color->jp2_cdef = NULL;
1292 }/* jp2_apply_cdef() */
1294 static OPJ_BOOL opj_jp2_read_cdef( opj_jp2_t * jp2,
1295 OPJ_BYTE * p_cdef_header_data,
1296 OPJ_UINT32 p_cdef_header_size,
1297 opj_event_mgr_t * p_manager
1300 opj_jp2_cdef_info_t *cdef_info;
1306 assert(p_cdef_header_data != 00);
1307 assert(p_manager != 00);
1308 (void)p_cdef_header_size;
1310 /* Part 1, I.5.3.6: 'The shall be at most one Channel Definition box
1311 * inside a JP2 Header box.'*/
1312 if(jp2->color.jp2_cdef) return OPJ_FALSE;
1314 if (p_cdef_header_size < 2) {
1315 opj_event_msg(p_manager, EVT_ERROR, "Insufficient data for CDEF box.\n");
1319 opj_read_bytes(p_cdef_header_data,&l_value ,2); /* N */
1320 p_cdef_header_data+= 2;
1322 if ( (OPJ_UINT16)l_value == 0){ /* szukw000: FIXME */
1323 opj_event_msg(p_manager, EVT_ERROR, "Number of channel description is equal to zero in CDEF box.\n");
1327 if (p_cdef_header_size < 2 + (OPJ_UINT32)(OPJ_UINT16)l_value * 6) {
1328 opj_event_msg(p_manager, EVT_ERROR, "Insufficient data for CDEF box.\n");
1332 cdef_info = (opj_jp2_cdef_info_t*) opj_malloc(l_value * sizeof(opj_jp2_cdef_info_t));
1336 jp2->color.jp2_cdef = (opj_jp2_cdef_t*)opj_malloc(sizeof(opj_jp2_cdef_t));
1337 if(!jp2->color.jp2_cdef)
1339 opj_free(cdef_info);
1342 jp2->color.jp2_cdef->info = cdef_info;
1343 jp2->color.jp2_cdef->n = (OPJ_UINT16) l_value;
1345 for(i = 0; i < jp2->color.jp2_cdef->n; ++i) {
1346 opj_read_bytes(p_cdef_header_data, &l_value, 2); /* Cn^i */
1347 p_cdef_header_data +=2;
1348 cdef_info[i].cn = (OPJ_UINT16) l_value;
1350 opj_read_bytes(p_cdef_header_data, &l_value, 2); /* Typ^i */
1351 p_cdef_header_data +=2;
1352 cdef_info[i].typ = (OPJ_UINT16) l_value;
1354 opj_read_bytes(p_cdef_header_data, &l_value, 2); /* Asoc^i */
1355 p_cdef_header_data +=2;
1356 cdef_info[i].asoc = (OPJ_UINT16) l_value;
1362 static OPJ_BOOL opj_jp2_read_colr( opj_jp2_t *jp2,
1363 OPJ_BYTE * p_colr_header_data,
1364 OPJ_UINT32 p_colr_header_size,
1365 opj_event_mgr_t * p_manager
1372 assert(p_colr_header_data != 00);
1373 assert(p_manager != 00);
1375 if (p_colr_header_size < 3) {
1376 opj_event_msg(p_manager, EVT_ERROR, "Bad COLR header box (bad size)\n");
1380 /* Part 1, I.5.3.3 : 'A conforming JP2 reader shall ignore all Colour
1381 * Specification boxes after the first.'
1383 if(jp2->color.jp2_has_colr) {
1384 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");
1385 p_colr_header_data += p_colr_header_size;
1389 opj_read_bytes(p_colr_header_data,&jp2->meth ,1); /* METH */
1390 ++p_colr_header_data;
1392 opj_read_bytes(p_colr_header_data,&jp2->precedence ,1); /* PRECEDENCE */
1393 ++p_colr_header_data;
1395 opj_read_bytes(p_colr_header_data,&jp2->approx ,1); /* APPROX */
1396 ++p_colr_header_data;
1398 if (jp2->meth == 1) {
1399 if (p_colr_header_size < 7) {
1400 opj_event_msg(p_manager, EVT_ERROR, "Bad COLR header box (bad size: %d)\n", p_colr_header_size);
1403 if ((p_colr_header_size > 7) && (jp2->enumcs != 14)) { /* handled below for CIELab) */
1404 /* testcase Altona_Technical_v20_x4.pdf */
1405 opj_event_msg(p_manager, EVT_WARNING, "Bad COLR header box (bad size: %d)\n", p_colr_header_size);
1408 opj_read_bytes(p_colr_header_data,&jp2->enumcs ,4); /* EnumCS */
1410 p_colr_header_data += 4;
1412 if(jp2->enumcs == 14)/* CIELab */
1415 OPJ_UINT32 rl, ol, ra, oa, rb, ob, il;
1417 cielab = (OPJ_UINT32*)opj_malloc(9 * sizeof(OPJ_UINT32));
1419 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory for cielab\n");
1422 cielab[0] = 14; /* enumcs */
1424 /* default values */
1425 rl = ra = rb = ol = oa = ob = 0;
1426 il = 0x00443530; /* D50 */
1427 cielab[1] = 0x44454600;/* DEF */
1429 if(p_colr_header_size == 35)
1431 opj_read_bytes(p_colr_header_data, &rl, 4);
1432 p_colr_header_data += 4;
1433 opj_read_bytes(p_colr_header_data, &ol, 4);
1434 p_colr_header_data += 4;
1435 opj_read_bytes(p_colr_header_data, &ra, 4);
1436 p_colr_header_data += 4;
1437 opj_read_bytes(p_colr_header_data, &oa, 4);
1438 p_colr_header_data += 4;
1439 opj_read_bytes(p_colr_header_data, &rb, 4);
1440 p_colr_header_data += 4;
1441 opj_read_bytes(p_colr_header_data, &ob, 4);
1442 p_colr_header_data += 4;
1443 opj_read_bytes(p_colr_header_data, &il, 4);
1444 p_colr_header_data += 4;
1448 else if(p_colr_header_size != 7)
1450 opj_event_msg(p_manager, EVT_WARNING, "Bad COLR header box (CIELab, bad size: %d)\n", p_colr_header_size);
1452 cielab[2] = rl; cielab[4] = ra; cielab[6] = rb;
1453 cielab[3] = ol; cielab[5] = oa; cielab[7] = ob;
1456 jp2->color.icc_profile_buf = (OPJ_BYTE*)cielab;
1457 jp2->color.icc_profile_len = 0;
1459 jp2->color.jp2_has_colr = 1;
1461 else if (jp2->meth == 2) {
1463 OPJ_INT32 it_icc_value = 0;
1464 OPJ_INT32 icc_len = (OPJ_INT32)p_colr_header_size - 3;
1466 jp2->color.icc_profile_len = (OPJ_UINT32)icc_len;
1467 jp2->color.icc_profile_buf = (OPJ_BYTE*) opj_calloc(1,(size_t)icc_len);
1468 if (!jp2->color.icc_profile_buf)
1470 jp2->color.icc_profile_len = 0;
1474 for (it_icc_value = 0; it_icc_value < icc_len; ++it_icc_value)
1476 opj_read_bytes(p_colr_header_data,&l_value,1); /* icc values */
1477 ++p_colr_header_data;
1478 jp2->color.icc_profile_buf[it_icc_value] = (OPJ_BYTE) l_value;
1481 jp2->color.jp2_has_colr = 1;
1483 else if (jp2->meth > 2)
1485 /* ISO/IEC 15444-1:2004 (E), Table I.9 Legal METH values:
1486 conforming JP2 reader shall ignore the entire Colour Specification box.*/
1487 opj_event_msg(p_manager, EVT_INFO, "COLR BOX meth value is not a regular value (%d), "
1488 "so we will ignore the entire Colour Specification box. \n", jp2->meth);
1493 OPJ_BOOL opj_jp2_decode(opj_jp2_t *jp2,
1494 opj_stream_private_t *p_stream,
1495 opj_image_t* p_image,
1496 opj_event_mgr_t * p_manager)
1502 if( ! opj_j2k_decode(jp2->j2k, p_stream, p_image, p_manager) ) {
1503 opj_event_msg(p_manager, EVT_ERROR, "Failed to decode the codestream in the JP2 file\n");
1507 if (!jp2->ignore_pclr_cmap_cdef){
1508 if (!opj_jp2_check_color(p_image, &(jp2->color), p_manager)) {
1512 /* Set Image Color Space */
1513 if (jp2->enumcs == 16)
1514 p_image->color_space = OPJ_CLRSPC_SRGB;
1515 else if (jp2->enumcs == 17)
1516 p_image->color_space = OPJ_CLRSPC_GRAY;
1517 else if (jp2->enumcs == 18)
1518 p_image->color_space = OPJ_CLRSPC_SYCC;
1519 else if (jp2->enumcs == 24)
1520 p_image->color_space = OPJ_CLRSPC_EYCC;
1521 else if (jp2->enumcs == 12)
1522 p_image->color_space = OPJ_CLRSPC_CMYK;
1524 p_image->color_space = OPJ_CLRSPC_UNKNOWN;
1526 if(jp2->color.jp2_pclr) {
1527 /* Part 1, I.5.3.4: Either both or none : */
1528 if( !jp2->color.jp2_pclr->cmap)
1529 opj_jp2_free_pclr(&(jp2->color));
1531 opj_jp2_apply_pclr(p_image, &(jp2->color));
1534 /* Apply the color space if needed */
1535 if(jp2->color.jp2_cdef) {
1536 opj_jp2_apply_cdef(p_image, &(jp2->color), p_manager);
1539 if(jp2->color.icc_profile_buf) {
1540 p_image->icc_profile_buf = jp2->color.icc_profile_buf;
1541 p_image->icc_profile_len = jp2->color.icc_profile_len;
1542 jp2->color.icc_profile_buf = NULL;
1549 static OPJ_BOOL opj_jp2_write_jp2h(opj_jp2_t *jp2,
1550 opj_stream_private_t *stream,
1551 opj_event_mgr_t * p_manager
1554 opj_jp2_img_header_writer_handler_t l_writers [4];
1555 opj_jp2_img_header_writer_handler_t * l_current_writer;
1557 OPJ_INT32 i, l_nb_pass;
1558 /* size of data for super box*/
1559 OPJ_UINT32 l_jp2h_size = 8;
1560 OPJ_BOOL l_result = OPJ_TRUE;
1562 /* to store the data of the super box */
1563 OPJ_BYTE l_jp2h_data [8];
1566 assert(stream != 00);
1568 assert(p_manager != 00);
1570 memset(l_writers,0,sizeof(l_writers));
1572 if (jp2->bpc == 255) {
1574 l_writers[0].handler = opj_jp2_write_ihdr;
1575 l_writers[1].handler = opj_jp2_write_bpcc;
1576 l_writers[2].handler = opj_jp2_write_colr;
1580 l_writers[0].handler = opj_jp2_write_ihdr;
1581 l_writers[1].handler = opj_jp2_write_colr;
1584 if (jp2->color.jp2_cdef != NULL) {
1585 l_writers[l_nb_pass].handler = opj_jp2_write_cdef;
1589 /* write box header */
1590 /* write JP2H type */
1591 opj_write_bytes(l_jp2h_data+4,JP2_JP2H,4);
1593 l_current_writer = l_writers;
1594 for (i=0;i<l_nb_pass;++i) {
1595 l_current_writer->m_data = l_current_writer->handler(jp2,&(l_current_writer->m_size));
1596 if (l_current_writer->m_data == 00) {
1597 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to hold JP2 Header data\n");
1598 l_result = OPJ_FALSE;
1602 l_jp2h_size += l_current_writer->m_size;
1607 l_current_writer = l_writers;
1608 for (i=0;i<l_nb_pass;++i) {
1609 if (l_current_writer->m_data != 00) {
1610 opj_free(l_current_writer->m_data );
1618 /* write super box size */
1619 opj_write_bytes(l_jp2h_data,l_jp2h_size,4);
1621 /* write super box data on stream */
1622 if (opj_stream_write_data(stream,l_jp2h_data,8,p_manager) != 8) {
1623 opj_event_msg(p_manager, EVT_ERROR, "Stream error while writing JP2 Header box\n");
1624 l_result = OPJ_FALSE;
1628 l_current_writer = l_writers;
1629 for (i=0;i<l_nb_pass;++i) {
1630 if (opj_stream_write_data(stream,l_current_writer->m_data,l_current_writer->m_size,p_manager) != l_current_writer->m_size) {
1631 opj_event_msg(p_manager, EVT_ERROR, "Stream error while writing JP2 Header box\n");
1632 l_result = OPJ_FALSE;
1639 l_current_writer = l_writers;
1642 for (i=0;i<l_nb_pass;++i) {
1643 if (l_current_writer->m_data != 00) {
1644 opj_free(l_current_writer->m_data );
1652 static OPJ_BOOL opj_jp2_write_ftyp(opj_jp2_t *jp2,
1653 opj_stream_private_t *cio,
1654 opj_event_mgr_t * p_manager )
1657 OPJ_UINT32 l_ftyp_size;
1658 OPJ_BYTE * l_ftyp_data, * l_current_data_ptr;
1664 assert(p_manager != 00);
1665 l_ftyp_size = 16 + 4 * jp2->numcl;
1667 l_ftyp_data = (OPJ_BYTE *) opj_calloc(1,l_ftyp_size);
1669 if (l_ftyp_data == 00) {
1670 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to handle ftyp data\n");
1674 l_current_data_ptr = l_ftyp_data;
1676 opj_write_bytes(l_current_data_ptr, l_ftyp_size,4); /* box size */
1677 l_current_data_ptr += 4;
1679 opj_write_bytes(l_current_data_ptr, JP2_FTYP,4); /* FTYP */
1680 l_current_data_ptr += 4;
1682 opj_write_bytes(l_current_data_ptr, jp2->brand,4); /* BR */
1683 l_current_data_ptr += 4;
1685 opj_write_bytes(l_current_data_ptr, jp2->minversion,4); /* MinV */
1686 l_current_data_ptr += 4;
1688 for (i = 0; i < jp2->numcl; i++) {
1689 opj_write_bytes(l_current_data_ptr, jp2->cl[i],4); /* CL */
1692 l_result = (opj_stream_write_data(cio,l_ftyp_data,l_ftyp_size,p_manager) == l_ftyp_size);
1695 opj_event_msg(p_manager, EVT_ERROR, "Error while writing ftyp data to stream\n");
1698 opj_free(l_ftyp_data);
1703 static OPJ_BOOL opj_jp2_write_jp2c(opj_jp2_t *jp2,
1704 opj_stream_private_t *cio,
1705 opj_event_mgr_t * p_manager )
1707 OPJ_OFF_T j2k_codestream_exit;
1708 OPJ_BYTE l_data_header [8];
1713 assert(p_manager != 00);
1714 assert(opj_stream_has_seek(cio));
1716 j2k_codestream_exit = opj_stream_tell(cio);
1717 opj_write_bytes(l_data_header,
1718 (OPJ_UINT32) (j2k_codestream_exit - jp2->j2k_codestream_offset),
1719 4); /* size of codestream */
1720 opj_write_bytes(l_data_header + 4,JP2_JP2C,4); /* JP2C */
1722 if (! opj_stream_seek(cio,jp2->j2k_codestream_offset,p_manager)) {
1723 opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
1727 if (opj_stream_write_data(cio,l_data_header,8,p_manager) != 8) {
1728 opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
1732 if (! opj_stream_seek(cio,j2k_codestream_exit,p_manager)) {
1733 opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
1740 static OPJ_BOOL opj_jp2_write_jp( opj_jp2_t *jp2,
1741 opj_stream_private_t *cio,
1742 opj_event_mgr_t * p_manager )
1744 /* 12 bytes will be read */
1745 OPJ_BYTE l_signature_data [12];
1750 assert(p_manager != 00);
1752 /* write box length */
1753 opj_write_bytes(l_signature_data,12,4);
1754 /* writes box type */
1755 opj_write_bytes(l_signature_data+4,JP2_JP,4);
1756 /* writes magic number*/
1757 opj_write_bytes(l_signature_data+8,0x0d0a870a,4);
1759 if (opj_stream_write_data(cio,l_signature_data,12,p_manager) != 12) {
1766 /* ----------------------------------------------------------------------- */
1767 /* JP2 decoder interface */
1768 /* ----------------------------------------------------------------------- */
1770 void opj_jp2_setup_decoder(opj_jp2_t *jp2, opj_dparameters_t *parameters)
1772 /* setup the J2K codec */
1773 opj_j2k_setup_decoder(jp2->j2k, parameters);
1775 /* further JP2 initializations go here */
1776 jp2->color.jp2_has_colr = 0;
1777 jp2->ignore_pclr_cmap_cdef = parameters->flags & OPJ_DPARAMETERS_IGNORE_PCLR_CMAP_CDEF_FLAG;
1780 /* ----------------------------------------------------------------------- */
1781 /* JP2 encoder interface */
1782 /* ----------------------------------------------------------------------- */
1784 OPJ_BOOL opj_jp2_setup_encoder( opj_jp2_t *jp2,
1785 opj_cparameters_t *parameters,
1787 opj_event_mgr_t * p_manager)
1792 OPJ_UINT32 alpha_count;
1793 OPJ_UINT32 color_channels = 0U;
1794 OPJ_UINT32 alpha_channel = 0U;
1797 if(!jp2 || !parameters || !image)
1800 /* setup the J2K codec */
1801 /* ------------------- */
1803 /* Check if number of components respects standard */
1804 if (image->numcomps < 1 || image->numcomps > 16384) {
1805 opj_event_msg(p_manager, EVT_ERROR, "Invalid number of components specified while setting up JP2 encoder\n");
1809 if (opj_j2k_setup_encoder(jp2->j2k, parameters, image, p_manager ) == OPJ_FALSE) {
1813 /* setup the JP2 codec */
1814 /* ------------------- */
1818 jp2->brand = JP2_JP2; /* BR */
1819 jp2->minversion = 0; /* MinV */
1821 jp2->cl = (OPJ_UINT32*) opj_malloc(jp2->numcl * sizeof(OPJ_UINT32));
1823 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory when setup the JP2 encoder\n");
1826 jp2->cl[0] = JP2_JP2; /* CL0 : JP2 */
1828 /* Image Header box */
1830 jp2->numcomps = image->numcomps; /* NC */
1831 jp2->comps = (opj_jp2_comps_t*) opj_malloc(jp2->numcomps * sizeof(opj_jp2_comps_t));
1833 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory when setup the JP2 encoder\n");
1834 /* Memory of jp2->cl will be freed by opj_jp2_destroy */
1838 jp2->h = image->y1 - image->y0; /* HEIGHT */
1839 jp2->w = image->x1 - image->x0; /* WIDTH */
1841 depth_0 = image->comps[0].prec - 1;
1842 sign = image->comps[0].sgnd;
1843 jp2->bpc = depth_0 + (sign << 7);
1844 for (i = 1; i < image->numcomps; i++) {
1845 OPJ_UINT32 depth = image->comps[i].prec - 1;
1846 sign = image->comps[i].sgnd;
1847 if (depth_0 != depth)
1850 jp2->C = 7; /* C : Always 7 */
1851 jp2->UnkC = 0; /* UnkC, colorspace specified in colr box */
1852 jp2->IPR = 0; /* IPR, no intellectual property */
1854 /* BitsPerComponent box */
1855 for (i = 0; i < image->numcomps; i++) {
1856 jp2->comps[i].bpcc = image->comps[i].prec - 1 + (image->comps[i].sgnd << 7);
1859 /* Colour Specification box */
1860 if(image->icc_profile_len) {
1866 if (image->color_space == 1)
1867 jp2->enumcs = 16; /* sRGB as defined by IEC 61966-2-1 */
1868 else if (image->color_space == 2)
1869 jp2->enumcs = 17; /* greyscale */
1870 else if (image->color_space == 3)
1871 jp2->enumcs = 18; /* YUV */
1874 /* Channel Definition box */
1875 /* FIXME not provided by parameters */
1876 /* We try to do what we can... */
1878 for (i = 0; i < image->numcomps; i++) {
1879 if (image->comps[i].alpha != 0) {
1884 if (alpha_count == 1U) { /* no way to deal with more than 1 alpha channel */
1885 switch (jp2->enumcs) {
1897 if (alpha_count == 0U) {
1898 opj_event_msg(p_manager, EVT_WARNING, "Alpha channel specified but unknown enumcs. No cdef box will be created.\n");
1899 } else if (image->numcomps < (color_channels+1)) {
1900 opj_event_msg(p_manager, EVT_WARNING, "Alpha channel specified but not enough image components for an automatic cdef box creation.\n");
1902 } else if ((OPJ_UINT32)alpha_channel < color_channels) {
1903 opj_event_msg(p_manager, EVT_WARNING, "Alpha channel position conflicts with color channel. No cdef box will be created.\n");
1906 } else if (alpha_count > 1) {
1907 opj_event_msg(p_manager, EVT_WARNING, "Multiple alpha channels specified. No cdef box will be created.\n");
1909 if (alpha_count == 1U) { /* if here, we know what we can do */
1910 jp2->color.jp2_cdef = (opj_jp2_cdef_t*)opj_malloc(sizeof(opj_jp2_cdef_t));
1911 if(!jp2->color.jp2_cdef) {
1912 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to setup the JP2 encoder\n");
1915 /* no memset needed, all values will be overwritten except if jp2->color.jp2_cdef->info allocation fails, */
1916 /* in which case jp2->color.jp2_cdef->info will be NULL => valid for destruction */
1917 jp2->color.jp2_cdef->info = (opj_jp2_cdef_info_t*) opj_malloc(image->numcomps * sizeof(opj_jp2_cdef_info_t));
1918 if (!jp2->color.jp2_cdef->info) {
1919 /* memory will be freed by opj_jp2_destroy */
1920 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to setup the JP2 encoder\n");
1923 jp2->color.jp2_cdef->n = (OPJ_UINT16) image->numcomps; /* cast is valid : image->numcomps [1,16384] */
1924 for (i = 0U; i < color_channels; i++) {
1925 jp2->color.jp2_cdef->info[i].cn = (OPJ_UINT16)i; /* cast is valid : image->numcomps [1,16384] */
1926 jp2->color.jp2_cdef->info[i].typ = 0U;
1927 jp2->color.jp2_cdef->info[i].asoc = (OPJ_UINT16)(i+1U); /* No overflow + cast is valid : image->numcomps [1,16384] */
1929 for (; i < image->numcomps; i++) {
1930 if (image->comps[i].alpha != 0) { /* we'll be here exactly once */
1931 jp2->color.jp2_cdef->info[i].cn = (OPJ_UINT16)i; /* cast is valid : image->numcomps [1,16384] */
1932 jp2->color.jp2_cdef->info[i].typ = 1U; /* Opacity channel */
1933 jp2->color.jp2_cdef->info[i].asoc = 0U; /* Apply alpha channel to the whole image */
1935 /* Unknown channel */
1936 jp2->color.jp2_cdef->info[i].cn = (OPJ_UINT16)i; /* cast is valid : image->numcomps [1,16384] */
1937 jp2->color.jp2_cdef->info[i].typ = 65535U;
1938 jp2->color.jp2_cdef->info[i].asoc = 65535U;
1943 jp2->precedence = 0; /* PRECEDENCE */
1944 jp2->approx = 0; /* APPROX */
1946 jp2->jpip_on = parameters->jpip_on;
1951 OPJ_BOOL opj_jp2_encode(opj_jp2_t *jp2,
1952 opj_stream_private_t *stream,
1953 opj_event_mgr_t * p_manager)
1955 return opj_j2k_encode(jp2->j2k, stream, p_manager);
1958 OPJ_BOOL opj_jp2_end_decompress(opj_jp2_t *jp2,
1959 opj_stream_private_t *cio,
1960 opj_event_mgr_t * p_manager
1966 assert(p_manager != 00);
1968 /* customization of the end encoding */
1969 if (! opj_jp2_setup_end_header_reading(jp2, p_manager)) {
1974 if (! opj_jp2_exec (jp2,jp2->m_procedure_list,cio,p_manager)) {
1978 return opj_j2k_end_decompress(jp2->j2k, cio, p_manager);
1981 OPJ_BOOL opj_jp2_end_compress( opj_jp2_t *jp2,
1982 opj_stream_private_t *cio,
1983 opj_event_mgr_t * p_manager
1989 assert(p_manager != 00);
1991 /* customization of the end encoding */
1992 if (! opj_jp2_setup_end_header_writing(jp2, p_manager)) {
1996 if (! opj_j2k_end_compress(jp2->j2k,cio,p_manager)) {
2001 return opj_jp2_exec(jp2,jp2->m_procedure_list,cio,p_manager);
2004 static OPJ_BOOL opj_jp2_setup_end_header_writing (opj_jp2_t *jp2, opj_event_mgr_t * p_manager)
2008 assert(p_manager != 00);
2011 if( jp2->jpip_on ) {
2012 if (! opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)opj_jpip_write_iptr, p_manager)) {
2017 if (! opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)opj_jp2_write_jp2c, p_manager)) {
2020 /* DEVELOPER CORNER, add your custom procedures */
2024 if (! opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)opj_jpip_write_cidx, p_manager)) {
2027 if (! opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)opj_jpip_write_fidx, p_manager)) {
2035 static OPJ_BOOL opj_jp2_setup_end_header_reading (opj_jp2_t *jp2, opj_event_mgr_t * p_manager)
2039 assert(p_manager != 00);
2041 if (! opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)opj_jp2_read_header_procedure, p_manager)) {
2044 /* DEVELOPER CORNER, add your custom procedures */
2049 static OPJ_BOOL opj_jp2_default_validation ( opj_jp2_t * jp2,
2050 opj_stream_private_t *cio,
2051 opj_event_mgr_t * p_manager
2054 OPJ_BOOL l_is_valid = OPJ_TRUE;
2060 assert(p_manager != 00);
2062 /* JPEG2000 codec validation */
2064 /* STATE checking */
2065 /* make sure the state is at 0 */
2066 l_is_valid &= (jp2->jp2_state == JP2_STATE_NONE);
2068 /* make sure not reading a jp2h ???? WEIRD */
2069 l_is_valid &= (jp2->jp2_img_state == JP2_IMG_STATE_NONE);
2071 /* POINTER validation */
2072 /* make sure a j2k codec is present */
2073 l_is_valid &= (jp2->j2k != 00);
2075 /* make sure a procedure list is present */
2076 l_is_valid &= (jp2->m_procedure_list != 00);
2078 /* make sure a validation list is present */
2079 l_is_valid &= (jp2->m_validation_list != 00);
2081 /* PARAMETER VALIDATION */
2082 /* number of components */
2083 l_is_valid &= (jp2->numcl > 0);
2085 l_is_valid &= (jp2->h > 0);
2087 l_is_valid &= (jp2->w > 0);
2089 for (i = 0; i < jp2->numcomps; ++i) {
2090 l_is_valid &= ((jp2->comps[i].bpcc & 0x7FU) < 38U); /* 0 is valid, ignore sign for check */
2094 l_is_valid &= ((jp2->meth > 0) && (jp2->meth < 3));
2096 /* stream validation */
2097 /* back and forth is needed */
2098 l_is_valid &= opj_stream_has_seek(cio);
2103 static OPJ_BOOL opj_jp2_read_header_procedure( opj_jp2_t *jp2,
2104 opj_stream_private_t *stream,
2105 opj_event_mgr_t * p_manager
2109 OPJ_UINT32 l_nb_bytes_read;
2110 const opj_jp2_header_handler_t * l_current_handler;
2111 const opj_jp2_header_handler_t * l_current_handler_misplaced;
2112 OPJ_UINT32 l_last_data_size = OPJ_BOX_SIZE;
2113 OPJ_UINT32 l_current_data_size;
2114 OPJ_BYTE * l_current_data = 00;
2117 assert(stream != 00);
2119 assert(p_manager != 00);
2121 l_current_data = (OPJ_BYTE*)opj_calloc(1,l_last_data_size);
2123 if (l_current_data == 00) {
2124 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to handle jpeg2000 file header\n");
2128 while (opj_jp2_read_boxhdr(&box,&l_nb_bytes_read,stream,p_manager)) {
2129 /* is it the codestream box ? */
2130 if (box.type == JP2_JP2C) {
2131 if (jp2->jp2_state & JP2_STATE_HEADER) {
2132 jp2->jp2_state |= JP2_STATE_CODESTREAM;
2133 opj_free(l_current_data);
2137 opj_event_msg(p_manager, EVT_ERROR, "bad placed jpeg codestream\n");
2138 opj_free(l_current_data);
2142 else if (box.length == 0) {
2143 opj_event_msg(p_manager, EVT_ERROR, "Cannot handle box of undefined sizes\n");
2144 opj_free(l_current_data);
2147 /* testcase 1851.pdf.SIGSEGV.ce9.948 */
2148 else if (box.length < l_nb_bytes_read) {
2149 opj_event_msg(p_manager, EVT_ERROR, "invalid box size %d (%x)\n", box.length, box.type);
2150 opj_free(l_current_data);
2154 l_current_handler = opj_jp2_find_handler(box.type);
2155 l_current_handler_misplaced = opj_jp2_img_find_handler(box.type);
2156 l_current_data_size = box.length - l_nb_bytes_read;
2158 if ((l_current_handler != 00) || (l_current_handler_misplaced != 00)) {
2159 if (l_current_handler == 00) {
2160 opj_event_msg(p_manager, EVT_WARNING, "Found a misplaced '%c%c%c%c' box outside jp2h box\n", (OPJ_BYTE)(box.type>>24), (OPJ_BYTE)(box.type>>16), (OPJ_BYTE)(box.type>>8), (OPJ_BYTE)(box.type>>0));
2161 if (jp2->jp2_state & JP2_STATE_HEADER) {
2162 /* read anyway, we already have jp2h */
2163 l_current_handler = l_current_handler_misplaced;
2165 opj_event_msg(p_manager, EVT_WARNING, "JPEG2000 Header box not read yet, '%c%c%c%c' box will be ignored\n", (OPJ_BYTE)(box.type>>24), (OPJ_BYTE)(box.type>>16), (OPJ_BYTE)(box.type>>8), (OPJ_BYTE)(box.type>>0));
2166 jp2->jp2_state |= JP2_STATE_UNKNOWN;
2167 if (opj_stream_skip(stream,l_current_data_size,p_manager) != l_current_data_size) {
2168 opj_event_msg(p_manager, EVT_ERROR, "Problem with skipping JPEG2000 box, stream error\n");
2169 opj_free(l_current_data);
2175 if ((OPJ_OFF_T)l_current_data_size > opj_stream_get_number_byte_left(stream)) {
2176 /* do not even try to malloc if we can't read */
2177 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));
2178 opj_free(l_current_data);
2181 if (l_current_data_size > l_last_data_size) {
2182 OPJ_BYTE* new_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,l_current_data_size);
2183 if (!new_current_data) {
2184 opj_free(l_current_data);
2185 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to handle jpeg2000 box\n");
2188 l_current_data = new_current_data;
2189 l_last_data_size = l_current_data_size;
2192 l_nb_bytes_read = (OPJ_UINT32)opj_stream_read_data(stream,l_current_data,l_current_data_size,p_manager);
2193 if (l_nb_bytes_read != l_current_data_size) {
2194 opj_event_msg(p_manager, EVT_ERROR, "Problem with reading JPEG2000 box, stream error\n");
2195 opj_free(l_current_data);
2199 if (! l_current_handler->handler(jp2,l_current_data,l_current_data_size,p_manager)) {
2200 opj_free(l_current_data);
2205 if (!(jp2->jp2_state & JP2_STATE_SIGNATURE)) {
2206 opj_event_msg(p_manager, EVT_ERROR, "Malformed JP2 file format: first box must be JPEG 2000 signature box\n");
2207 opj_free(l_current_data);
2210 if (!(jp2->jp2_state & JP2_STATE_FILE_TYPE)) {
2211 opj_event_msg(p_manager, EVT_ERROR, "Malformed JP2 file format: second box must be file type box\n");
2212 opj_free(l_current_data);
2215 jp2->jp2_state |= JP2_STATE_UNKNOWN;
2216 if (opj_stream_skip(stream,l_current_data_size,p_manager) != l_current_data_size) {
2217 opj_event_msg(p_manager, EVT_ERROR, "Problem with skipping JPEG2000 box, stream error\n");
2218 opj_free(l_current_data);
2224 opj_free(l_current_data);
2230 * Excutes the given procedures on the given codec.
2232 * @param p_procedure_list the list of procedures to execute
2233 * @param jp2 the jpeg2000 file codec to execute the procedures on.
2234 * @param stream the stream to execute the procedures on.
2235 * @param p_manager the user manager.
2237 * @return true if all the procedures were successfully executed.
2239 static OPJ_BOOL opj_jp2_exec ( opj_jp2_t * jp2,
2240 opj_procedure_list_t * p_procedure_list,
2241 opj_stream_private_t *stream,
2242 opj_event_mgr_t * p_manager
2246 OPJ_BOOL (** l_procedure) (opj_jp2_t * jp2, opj_stream_private_t *, opj_event_mgr_t *) = 00;
2247 OPJ_BOOL l_result = OPJ_TRUE;
2248 OPJ_UINT32 l_nb_proc, i;
2251 assert(p_procedure_list != 00);
2253 assert(stream != 00);
2254 assert(p_manager != 00);
2256 l_nb_proc = opj_procedure_list_get_nb_procedures(p_procedure_list);
2257 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);
2259 for (i=0;i<l_nb_proc;++i) {
2260 l_result = l_result && (*l_procedure) (jp2,stream,p_manager);
2264 /* and clear the procedure list at the end. */
2265 opj_procedure_list_clear(p_procedure_list);
2269 OPJ_BOOL opj_jp2_start_compress(opj_jp2_t *jp2,
2270 opj_stream_private_t *stream,
2271 opj_image_t * p_image,
2272 opj_event_mgr_t * p_manager
2277 assert(stream != 00);
2278 assert(p_manager != 00);
2280 /* customization of the validation */
2281 if (! opj_jp2_setup_encoding_validation (jp2, p_manager)) {
2285 /* validation of the parameters codec */
2286 if (! opj_jp2_exec(jp2,jp2->m_validation_list,stream,p_manager)) {
2290 /* customization of the encoding */
2291 if (! opj_jp2_setup_header_writing(jp2, p_manager)) {
2296 if (! opj_jp2_exec (jp2,jp2->m_procedure_list,stream,p_manager)) {
2300 return opj_j2k_start_compress(jp2->j2k,stream,p_image,p_manager);
2303 static const opj_jp2_header_handler_t * opj_jp2_find_handler (OPJ_UINT32 p_id)
2305 OPJ_UINT32 i, l_handler_size = sizeof(jp2_header) / sizeof(opj_jp2_header_handler_t);
2307 for (i=0;i<l_handler_size;++i) {
2308 if (jp2_header[i].id == p_id) {
2309 return &jp2_header[i];
2316 * Finds the image execution function related to the given box id.
2318 * @param p_id the id of the handler to fetch.
2320 * @return the given handler or 00 if it could not be found.
2322 static const opj_jp2_header_handler_t * opj_jp2_img_find_handler (OPJ_UINT32 p_id)
2324 OPJ_UINT32 i, l_handler_size = sizeof(jp2_img_header) / sizeof(opj_jp2_header_handler_t);
2325 for (i=0;i<l_handler_size;++i)
2327 if (jp2_img_header[i].id == p_id) {
2328 return &jp2_img_header[i];
2336 * Reads a jpeg2000 file signature box.
2338 * @param p_header_data the data contained in the signature box.
2339 * @param jp2 the jpeg2000 file codec.
2340 * @param p_header_size the size of the data contained in the signature box.
2341 * @param p_manager the user event manager.
2343 * @return true if the file signature box is valid.
2345 static OPJ_BOOL opj_jp2_read_jp(opj_jp2_t *jp2,
2346 OPJ_BYTE * p_header_data,
2347 OPJ_UINT32 p_header_size,
2348 opj_event_mgr_t * p_manager
2352 OPJ_UINT32 l_magic_number;
2355 assert(p_header_data != 00);
2357 assert(p_manager != 00);
2359 if (jp2->jp2_state != JP2_STATE_NONE) {
2360 opj_event_msg(p_manager, EVT_ERROR, "The signature box must be the first box in the file.\n");
2364 /* assure length of data is correct (4 -> magic number) */
2365 if (p_header_size != 4) {
2366 opj_event_msg(p_manager, EVT_ERROR, "Error with JP signature Box size\n");
2370 /* rearrange data */
2371 opj_read_bytes(p_header_data,&l_magic_number,4);
2372 if (l_magic_number != 0x0d0a870a ) {
2373 opj_event_msg(p_manager, EVT_ERROR, "Error with JP Signature : bad magic number\n");
2377 jp2->jp2_state |= JP2_STATE_SIGNATURE;
2383 * Reads a a FTYP box - File type box
2385 * @param p_header_data the data contained in the FTYP box.
2386 * @param jp2 the jpeg2000 file codec.
2387 * @param p_header_size the size of the data contained in the FTYP box.
2388 * @param p_manager the user event manager.
2390 * @return true if the FTYP box is valid.
2392 static OPJ_BOOL opj_jp2_read_ftyp( opj_jp2_t *jp2,
2393 OPJ_BYTE * p_header_data,
2394 OPJ_UINT32 p_header_size,
2395 opj_event_mgr_t * p_manager
2398 OPJ_UINT32 i, l_remaining_bytes;
2401 assert(p_header_data != 00);
2403 assert(p_manager != 00);
2405 if (jp2->jp2_state != JP2_STATE_SIGNATURE) {
2406 opj_event_msg(p_manager, EVT_ERROR, "The ftyp box must be the second box in the file.\n");
2410 /* assure length of data is correct */
2411 if (p_header_size < 8) {
2412 opj_event_msg(p_manager, EVT_ERROR, "Error with FTYP signature Box size\n");
2416 opj_read_bytes(p_header_data,&jp2->brand,4); /* BR */
2419 opj_read_bytes(p_header_data,&jp2->minversion,4); /* MinV */
2422 l_remaining_bytes = p_header_size - 8;
2424 /* the number of remaining bytes should be a multiple of 4 */
2425 if ((l_remaining_bytes & 0x3) != 0) {
2426 opj_event_msg(p_manager, EVT_ERROR, "Error with FTYP signature Box size\n");
2431 jp2->numcl = l_remaining_bytes >> 2;
2433 jp2->cl = (OPJ_UINT32 *) opj_calloc(jp2->numcl, sizeof(OPJ_UINT32));
2434 if (jp2->cl == 00) {
2435 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory with FTYP Box\n");
2440 for (i = 0; i < jp2->numcl; ++i)
2442 opj_read_bytes(p_header_data,&jp2->cl[i],4); /* CLi */
2446 jp2->jp2_state |= JP2_STATE_FILE_TYPE;
2451 static OPJ_BOOL opj_jp2_skip_jp2c( opj_jp2_t *jp2,
2452 opj_stream_private_t *stream,
2453 opj_event_mgr_t * p_manager )
2457 assert(stream != 00);
2458 assert(p_manager != 00);
2460 jp2->j2k_codestream_offset = opj_stream_tell(stream);
2462 if (opj_stream_skip(stream,8,p_manager) != 8) {
2469 static OPJ_BOOL opj_jpip_skip_iptr( opj_jp2_t *jp2,
2470 opj_stream_private_t *stream,
2471 opj_event_mgr_t * p_manager )
2475 assert(stream != 00);
2476 assert(p_manager != 00);
2478 jp2->jpip_iptr_offset = opj_stream_tell(stream);
2480 if (opj_stream_skip(stream,24,p_manager) != 24) {
2488 * Reads the Jpeg2000 file Header box - JP2 Header box (warning, this is a super box).
2490 * @param p_header_data the data contained in the file header box.
2491 * @param jp2 the jpeg2000 file codec.
2492 * @param p_header_size the size of the data contained in the file header box.
2493 * @param p_manager the user event manager.
2495 * @return true if the JP2 Header box was successfully recognized.
2497 static OPJ_BOOL opj_jp2_read_jp2h( opj_jp2_t *jp2,
2498 OPJ_BYTE *p_header_data,
2499 OPJ_UINT32 p_header_size,
2500 opj_event_mgr_t * p_manager
2503 OPJ_UINT32 l_box_size=0, l_current_data_size = 0;
2505 const opj_jp2_header_handler_t * l_current_handler;
2506 OPJ_BOOL l_has_ihdr = 0;
2509 assert(p_header_data != 00);
2511 assert(p_manager != 00);
2513 /* make sure the box is well placed */
2514 if ((jp2->jp2_state & JP2_STATE_FILE_TYPE) != JP2_STATE_FILE_TYPE ) {
2515 opj_event_msg(p_manager, EVT_ERROR, "The box must be the first box in the file.\n");
2519 jp2->jp2_img_state = JP2_IMG_STATE_NONE;
2521 /* iterate while remaining data */
2522 while (p_header_size > 0) {
2524 if (! opj_jp2_read_boxhdr_char(&box,p_header_data,&l_box_size,p_header_size, p_manager)) {
2525 opj_event_msg(p_manager, EVT_ERROR, "Stream error while reading JP2 Header box\n");
2529 if (box.length > p_header_size) {
2530 opj_event_msg(p_manager, EVT_ERROR, "Stream error while reading JP2 Header box: box length is inconsistent.\n");
2534 l_current_handler = opj_jp2_img_find_handler(box.type);
2535 l_current_data_size = box.length - l_box_size;
2536 p_header_data += l_box_size;
2538 if (l_current_handler != 00) {
2539 if (! l_current_handler->handler(jp2,p_header_data,l_current_data_size,p_manager)) {
2544 jp2->jp2_img_state |= JP2_IMG_STATE_UNKNOWN;
2547 if (box.type == JP2_IHDR) {
2551 p_header_data += l_current_data_size;
2552 p_header_size -= box.length;
2555 if (l_has_ihdr == 0) {
2556 opj_event_msg(p_manager, EVT_ERROR, "Stream error while reading JP2 Header box: no 'ihdr' box.\n");
2560 jp2->jp2_state |= JP2_STATE_HEADER;
2565 static OPJ_BOOL opj_jp2_read_boxhdr_char( opj_jp2_box_t *box,
2567 OPJ_UINT32 * p_number_bytes_read,
2568 OPJ_UINT32 p_box_max_size,
2569 opj_event_mgr_t * p_manager
2575 assert(p_data != 00);
2577 assert(p_number_bytes_read != 00);
2578 assert(p_manager != 00);
2580 if (p_box_max_size < 8) {
2581 opj_event_msg(p_manager, EVT_ERROR, "Cannot handle box of less than 8 bytes\n");
2585 /* process read data */
2586 opj_read_bytes(p_data, &l_value, 4);
2588 box->length = (OPJ_UINT32)(l_value);
2590 opj_read_bytes(p_data, &l_value, 4);
2592 box->type = (OPJ_UINT32)(l_value);
2594 *p_number_bytes_read = 8;
2596 /* do we have a "special very large box ?" */
2597 /* read then the XLBox */
2598 if (box->length == 1) {
2599 OPJ_UINT32 l_xl_part_size;
2601 if (p_box_max_size < 16) {
2602 opj_event_msg(p_manager, EVT_ERROR, "Cannot handle XL box of less than 16 bytes\n");
2606 opj_read_bytes(p_data,&l_xl_part_size, 4);
2608 *p_number_bytes_read += 4;
2610 if (l_xl_part_size != 0) {
2611 opj_event_msg(p_manager, EVT_ERROR, "Cannot handle box sizes higher than 2^32\n");
2615 opj_read_bytes(p_data, &l_value, 4);
2616 *p_number_bytes_read += 4;
2617 box->length = (OPJ_UINT32)(l_value);
2619 if (box->length == 0) {
2620 opj_event_msg(p_manager, EVT_ERROR, "Cannot handle box of undefined sizes\n");
2624 else if (box->length == 0) {
2625 opj_event_msg(p_manager, EVT_ERROR, "Cannot handle box of undefined sizes\n");
2628 if (box->length < *p_number_bytes_read) {
2629 opj_event_msg(p_manager, EVT_ERROR, "Box length is inconsistent.\n");
2635 OPJ_BOOL opj_jp2_read_header( opj_stream_private_t *p_stream,
2637 opj_image_t ** p_image,
2638 opj_event_mgr_t * p_manager
2643 assert(p_stream != 00);
2644 assert(p_manager != 00);
2646 /* customization of the validation */
2647 if (! opj_jp2_setup_decoding_validation (jp2, p_manager)) {
2651 /* customization of the encoding */
2652 if (! opj_jp2_setup_header_reading(jp2, p_manager)) {
2656 /* validation of the parameters codec */
2657 if (! opj_jp2_exec(jp2,jp2->m_validation_list,p_stream,p_manager)) {
2662 if (! opj_jp2_exec (jp2,jp2->m_procedure_list,p_stream,p_manager)) {
2666 return opj_j2k_read_header( p_stream,
2672 static OPJ_BOOL opj_jp2_setup_encoding_validation (opj_jp2_t *jp2, opj_event_mgr_t * p_manager)
2676 assert(p_manager != 00);
2678 if (! opj_procedure_list_add_procedure(jp2->m_validation_list, (opj_procedure)opj_jp2_default_validation, p_manager)) {
2681 /* DEVELOPER CORNER, add your custom validation procedure */
2686 static OPJ_BOOL opj_jp2_setup_decoding_validation (opj_jp2_t *jp2, opj_event_mgr_t * p_manager)
2690 assert(p_manager != 00);
2692 /* DEVELOPER CORNER, add your custom validation procedure */
2697 static OPJ_BOOL opj_jp2_setup_header_writing (opj_jp2_t *jp2, opj_event_mgr_t * p_manager)
2701 assert(p_manager != 00);
2703 if (! opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)opj_jp2_write_jp, p_manager)) {
2706 if (! opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)opj_jp2_write_ftyp, p_manager)) {
2709 if (! opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)opj_jp2_write_jp2h, p_manager)) {
2712 if( jp2->jpip_on ) {
2713 if (! opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)opj_jpip_skip_iptr, p_manager)) {
2717 if (! opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)opj_jp2_skip_jp2c,p_manager)) {
2721 /* DEVELOPER CORNER, insert your custom procedures */
2726 static OPJ_BOOL opj_jp2_setup_header_reading (opj_jp2_t *jp2, opj_event_mgr_t * p_manager)
2730 assert(p_manager != 00);
2732 if (! opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)opj_jp2_read_header_procedure, p_manager)) {
2736 /* DEVELOPER CORNER, add your custom procedures */
2741 OPJ_BOOL opj_jp2_read_tile_header ( opj_jp2_t * p_jp2,
2742 OPJ_UINT32 * p_tile_index,
2743 OPJ_UINT32 * p_data_size,
2744 OPJ_INT32 * p_tile_x0,
2745 OPJ_INT32 * p_tile_y0,
2746 OPJ_INT32 * p_tile_x1,
2747 OPJ_INT32 * p_tile_y1,
2748 OPJ_UINT32 * p_nb_comps,
2750 opj_stream_private_t *p_stream,
2751 opj_event_mgr_t * p_manager
2754 return opj_j2k_read_tile_header(p_jp2->j2k,
2757 p_tile_x0, p_tile_y0,
2758 p_tile_x1, p_tile_y1,
2765 OPJ_BOOL opj_jp2_write_tile ( opj_jp2_t *p_jp2,
2766 OPJ_UINT32 p_tile_index,
2768 OPJ_UINT32 p_data_size,
2769 opj_stream_private_t *p_stream,
2770 opj_event_mgr_t * p_manager
2774 return opj_j2k_write_tile (p_jp2->j2k,p_tile_index,p_data,p_data_size,p_stream,p_manager);
2777 OPJ_BOOL opj_jp2_decode_tile ( opj_jp2_t * p_jp2,
2778 OPJ_UINT32 p_tile_index,
2780 OPJ_UINT32 p_data_size,
2781 opj_stream_private_t *p_stream,
2782 opj_event_mgr_t * p_manager
2785 return opj_j2k_decode_tile (p_jp2->j2k,p_tile_index,p_data,p_data_size,p_stream,p_manager);
2788 void opj_jp2_destroy(opj_jp2_t *jp2)
2791 /* destroy the J2K codec */
2792 opj_j2k_destroy(jp2->j2k);
2796 opj_free(jp2->comps);
2805 if (jp2->color.icc_profile_buf) {
2806 opj_free(jp2->color.icc_profile_buf);
2807 jp2->color.icc_profile_buf = 00;
2810 if (jp2->color.jp2_cdef) {
2811 if (jp2->color.jp2_cdef->info) {
2812 opj_free(jp2->color.jp2_cdef->info);
2813 jp2->color.jp2_cdef->info = NULL;
2816 opj_free(jp2->color.jp2_cdef);
2817 jp2->color.jp2_cdef = 00;
2820 if (jp2->color.jp2_pclr) {
2821 if (jp2->color.jp2_pclr->cmap) {
2822 opj_free(jp2->color.jp2_pclr->cmap);
2823 jp2->color.jp2_pclr->cmap = NULL;
2825 if (jp2->color.jp2_pclr->channel_sign) {
2826 opj_free(jp2->color.jp2_pclr->channel_sign);
2827 jp2->color.jp2_pclr->channel_sign = NULL;
2829 if (jp2->color.jp2_pclr->channel_size) {
2830 opj_free(jp2->color.jp2_pclr->channel_size);
2831 jp2->color.jp2_pclr->channel_size = NULL;
2833 if (jp2->color.jp2_pclr->entries) {
2834 opj_free(jp2->color.jp2_pclr->entries);
2835 jp2->color.jp2_pclr->entries = NULL;
2838 opj_free(jp2->color.jp2_pclr);
2839 jp2->color.jp2_pclr = 00;
2842 if (jp2->m_validation_list) {
2843 opj_procedure_list_destroy(jp2->m_validation_list);
2844 jp2->m_validation_list = 00;
2847 if (jp2->m_procedure_list) {
2848 opj_procedure_list_destroy(jp2->m_procedure_list);
2849 jp2->m_procedure_list = 00;
2856 OPJ_BOOL opj_jp2_set_decode_area( opj_jp2_t *p_jp2,
2857 opj_image_t* p_image,
2858 OPJ_INT32 p_start_x, OPJ_INT32 p_start_y,
2859 OPJ_INT32 p_end_x, OPJ_INT32 p_end_y,
2860 opj_event_mgr_t * p_manager
2863 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);
2866 OPJ_BOOL opj_jp2_get_tile( opj_jp2_t *p_jp2,
2867 opj_stream_private_t *p_stream,
2868 opj_image_t* p_image,
2869 opj_event_mgr_t * p_manager,
2870 OPJ_UINT32 tile_index
2876 opj_event_msg(p_manager, EVT_WARNING, "JP2 box which are after the codestream will not be read by this function.\n");
2878 if (! opj_j2k_get_tile(p_jp2->j2k, p_stream, p_image, p_manager, tile_index) ){
2879 opj_event_msg(p_manager, EVT_ERROR, "Failed to decode the codestream in the JP2 file\n");
2883 if (!opj_jp2_check_color(p_image, &(p_jp2->color), p_manager)) {
2887 /* Set Image Color Space */
2888 if (p_jp2->enumcs == 16)
2889 p_image->color_space = OPJ_CLRSPC_SRGB;
2890 else if (p_jp2->enumcs == 17)
2891 p_image->color_space = OPJ_CLRSPC_GRAY;
2892 else if (p_jp2->enumcs == 18)
2893 p_image->color_space = OPJ_CLRSPC_SYCC;
2894 else if (p_jp2->enumcs == 24)
2895 p_image->color_space = OPJ_CLRSPC_EYCC;
2896 else if (p_jp2->enumcs == 12)
2897 p_image->color_space = OPJ_CLRSPC_CMYK;
2899 p_image->color_space = OPJ_CLRSPC_UNKNOWN;
2901 if(p_jp2->color.jp2_pclr) {
2902 /* Part 1, I.5.3.4: Either both or none : */
2903 if( !p_jp2->color.jp2_pclr->cmap)
2904 opj_jp2_free_pclr(&(p_jp2->color));
2906 opj_jp2_apply_pclr(p_image, &(p_jp2->color));
2909 /* Apply the color space if needed */
2910 if(p_jp2->color.jp2_cdef) {
2911 opj_jp2_apply_cdef(p_image, &(p_jp2->color), p_manager);
2914 if(p_jp2->color.icc_profile_buf) {
2915 p_image->icc_profile_buf = p_jp2->color.icc_profile_buf;
2916 p_image->icc_profile_len = p_jp2->color.icc_profile_len;
2917 p_jp2->color.icc_profile_buf = NULL;
2923 /* ----------------------------------------------------------------------- */
2924 /* JP2 encoder interface */
2925 /* ----------------------------------------------------------------------- */
2927 opj_jp2_t* opj_jp2_create(OPJ_BOOL p_is_decoder)
2929 opj_jp2_t *jp2 = (opj_jp2_t*)opj_calloc(1,sizeof(opj_jp2_t));
2932 /* create the J2K codec */
2933 if (! p_is_decoder) {
2934 jp2->j2k = opj_j2k_create_compress();
2937 jp2->j2k = opj_j2k_create_decompress();
2940 if (jp2->j2k == 00) {
2941 opj_jp2_destroy(jp2);
2945 /* Color structure */
2946 jp2->color.icc_profile_buf = NULL;
2947 jp2->color.icc_profile_len = 0;
2948 jp2->color.jp2_cdef = NULL;
2949 jp2->color.jp2_pclr = NULL;
2950 jp2->color.jp2_has_colr = 0;
2952 /* validation list creation */
2953 jp2->m_validation_list = opj_procedure_list_create();
2954 if (! jp2->m_validation_list) {
2955 opj_jp2_destroy(jp2);
2959 /* execution list creation */
2960 jp2->m_procedure_list = opj_procedure_list_create();
2961 if (! jp2->m_procedure_list) {
2962 opj_jp2_destroy(jp2);
2970 void jp2_dump(opj_jp2_t* p_jp2, OPJ_INT32 flag, FILE* out_stream)
2973 assert(p_jp2 != 00);
2975 j2k_dump(p_jp2->j2k,
2980 opj_codestream_index_t* jp2_get_cstr_index(opj_jp2_t* p_jp2)
2982 return j2k_get_cstr_index(p_jp2->j2k);
2985 opj_codestream_info_v2_t* jp2_get_cstr_info(opj_jp2_t* p_jp2)
2987 return j2k_get_cstr_info(p_jp2->j2k);
2990 OPJ_BOOL opj_jp2_set_decoded_resolution_factor(opj_jp2_t *p_jp2,
2991 OPJ_UINT32 res_factor,
2992 opj_event_mgr_t * p_manager)
2994 return opj_j2k_set_decoded_resolution_factor(p_jp2->j2k, res_factor, p_manager);
3000 static OPJ_BOOL opj_jpip_write_iptr(opj_jp2_t *jp2,
3001 opj_stream_private_t *cio,
3002 opj_event_mgr_t * p_manager )
3004 OPJ_OFF_T j2k_codestream_exit;
3005 OPJ_BYTE l_data_header [24];
3010 assert(p_manager != 00);
3011 assert(opj_stream_has_seek(cio));
3013 j2k_codestream_exit = opj_stream_tell(cio);
3014 opj_write_bytes(l_data_header, 24, 4); /* size of iptr */
3015 opj_write_bytes(l_data_header + 4,JPIP_IPTR,4); /* IPTR */
3017 opj_write_bytes(l_data_header + 4 + 4, 0, 8); /* offset */
3018 opj_write_bytes(l_data_header + 8 + 8, 0, 8); /* length */
3020 opj_write_double(l_data_header + 4 + 4, 0); /* offset */
3021 opj_write_double(l_data_header + 8 + 8, 0); /* length */
3024 if (! opj_stream_seek(cio,jp2->jpip_iptr_offset,p_manager)) {
3025 opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
3029 if (opj_stream_write_data(cio,l_data_header,24,p_manager) != 24) {
3030 opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
3034 if (! opj_stream_seek(cio,j2k_codestream_exit,p_manager)) {
3035 opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
3042 static OPJ_BOOL opj_jpip_write_fidx(opj_jp2_t *jp2,
3043 opj_stream_private_t *cio,
3044 opj_event_mgr_t * p_manager )
3046 OPJ_OFF_T j2k_codestream_exit;
3047 OPJ_BYTE l_data_header [24];
3052 assert(p_manager != 00);
3053 assert(opj_stream_has_seek(cio));
3055 opj_write_bytes(l_data_header, 24, 4); /* size of iptr */
3056 opj_write_bytes(l_data_header + 4,JPIP_FIDX,4); /* IPTR */
3057 opj_write_double(l_data_header + 4 + 4, 0); /* offset */
3058 opj_write_double(l_data_header + 8 + 8, 0); /* length */
3060 if (opj_stream_write_data(cio,l_data_header,24,p_manager) != 24) {
3061 opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
3065 j2k_codestream_exit = opj_stream_tell(cio);
3066 if (! opj_stream_seek(cio,j2k_codestream_exit,p_manager)) {
3067 opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
3074 static OPJ_BOOL opj_jpip_write_cidx(opj_jp2_t *jp2,
3075 opj_stream_private_t *cio,
3076 opj_event_mgr_t * p_manager )
3078 OPJ_OFF_T j2k_codestream_exit;
3079 OPJ_BYTE l_data_header [24];
3084 assert(p_manager != 00);
3085 assert(opj_stream_has_seek(cio));
3087 j2k_codestream_exit = opj_stream_tell(cio);
3088 opj_write_bytes(l_data_header, 24, 4); /* size of iptr */
3089 opj_write_bytes(l_data_header + 4,JPIP_CIDX,4); /* IPTR */
3091 opj_write_bytes(l_data_header + 4 + 4, 0, 8); /* offset */
3092 opj_write_bytes(l_data_header + 8 + 8, 0, 8); /* length */
3094 opj_write_double(l_data_header + 4 + 4, 0); /* offset */
3095 opj_write_double(l_data_header + 8 + 8, 0); /* length */
3098 if (! opj_stream_seek(cio,j2k_codestream_exit,p_manager)) {
3099 opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
3103 if (opj_stream_write_data(cio,l_data_header,24,p_manager) != 24) {
3104 opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
3108 j2k_codestream_exit = opj_stream_tell(cio);
3109 if (! opj_stream_seek(cio,j2k_codestream_exit,p_manager)) {
3110 opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
3118 static void write_prxy( int offset_jp2c, int length_jp2c, int offset_idx, int length_idx, opj_stream_private_t *cio,
3119 opj_event_mgr_t * p_manager )
3121 OPJ_BYTE l_data_header [8];
3122 OPJ_OFF_T len, lenp;
3124 lenp = opj_stream_tell(cio);
3125 opj_stream_skip(cio, 4, p_manager); /* L [at the end] */
3126 opj_write_bytes(l_data_header,JPIP_PRXY,4); /* IPTR */
3127 opj_stream_write_data(cio,l_data_header,4,p_manager);
3129 opj_write_bytes( l_data_header, offset_jp2c, 8); /* OOFF */
3130 opj_stream_write_data(cio,l_data_header,8,p_manager);
3131 opj_write_bytes( l_data_header, length_jp2c, 4); /* OBH part 1 */
3132 opj_write_bytes( l_data_header+4, JP2_JP2C, 4); /* OBH part 2 */
3133 opj_stream_write_data(cio,l_data_header,8,p_manager);
3135 opj_write_bytes( l_data_header, 1, 1);/* NI */
3136 opj_stream_write_data(cio,l_data_header,1,p_manager);
3138 opj_write_bytes( l_data_header, offset_idx, 8); /* IOFF */
3139 opj_stream_write_data(cio,l_data_header,8,p_manager);
3140 opj_write_bytes( l_data_header, length_idx, 4); /* IBH part 1 */
3141 opj_write_bytes( l_data_header+4, JPIP_CIDX, 4); /* IBH part 2 */
3142 opj_stream_write_data(cio,l_data_header,8,p_manager);
3144 len = opj_stream_tell(cio)-lenp;
3145 opj_stream_skip(cio, lenp, p_manager);
3146 opj_write_bytes(l_data_header,len,4);/* L */
3147 opj_stream_write_data(cio,l_data_header,4,p_manager);
3148 opj_stream_seek(cio, lenp+len,p_manager);
3154 static int write_fidx( int offset_jp2c, int length_jp2c, int offset_idx, int length_idx, opj_stream_private_t *cio,
3155 opj_event_mgr_t * p_manager )
3157 OPJ_BYTE l_data_header [4];
3158 OPJ_OFF_T len, lenp;
3160 lenp = opj_stream_tell(cio);
3161 opj_stream_skip(cio, 4, p_manager);
3162 opj_write_bytes(l_data_header,JPIP_FIDX,4); /* FIDX */
3163 opj_stream_write_data(cio,l_data_header,4,p_manager);
3165 write_prxy( offset_jp2c, length_jp2c, offset_idx, length_idx, cio,p_manager);
3167 len = opj_stream_tell(cio)-lenp;
3168 opj_stream_skip(cio, lenp, p_manager);
3169 opj_write_bytes(l_data_header,len,4);/* L */
3170 opj_stream_write_data(cio,l_data_header,4,p_manager);
3171 opj_stream_seek(cio, lenp+len,p_manager);
3176 #endif /* USE_JPIP */