2 * Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium
3 * Copyright (c) 2002-2007, Professor Benoit Macq
4 * Copyright (c) 2001-2003, David Janssens
5 * Copyright (c) 2002-2003, Yannick Verschueren
6 * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe
7 * Copyright (c) 2005, Herve Drolon, FreeImage Team
8 * Copyright (c) 2010-2011, Kaori Hagihara
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
32 #include "opj_includes.h"
34 /** @defgroup JP2 JP2 - JPEG-2000 file format reader/writer */
39 /** @name Local static functions */
43 /*static void jp2_write_url(opj_cio_t *cio, char *Idx_file);*/
46 * Reads a IHDR box - Image Header box
48 * @param p_image_header_data pointer to actual data (already read from file)
49 * @param jp2 the jpeg2000 file codec.
50 * @param p_image_header_size the size of the image header
51 * @param p_manager the user event manager.
53 * @return true if the image header is valid, false else.
55 static opj_bool opj_jp2_read_ihdr( opj_jp2_v2_t *jp2,
56 OPJ_BYTE *p_image_header_data,
57 OPJ_UINT32 p_image_header_size,
58 opj_event_mgr_t * p_manager );
61 * Writes the Image Header box - Image Header box.
63 * @param jp2 jpeg2000 file codec.
64 * @param p_nb_bytes_written pointer to store the nb of bytes written by the function.
66 * @return the data being copied.
68 static OPJ_BYTE * opj_jp2_write_ihdr(opj_jp2_v2_t *jp2,
69 OPJ_UINT32 * p_nb_bytes_written );
72 * Writes the Bit per Component box.
74 * @param jp2 jpeg2000 file codec.
75 * @param p_nb_bytes_written pointer to store the nb of bytes written by the function.
77 * @return the data being copied.
79 static OPJ_BYTE * opj_jp2_write_bpcc( opj_jp2_v2_t *jp2,
80 OPJ_UINT32 * p_nb_bytes_written );
83 * Reads a Bit per Component box.
85 * @param p_bpc_header_data pointer to actual data (already read from file)
86 * @param jp2 the jpeg2000 file codec.
87 * @param p_bpc_header_size the size of the bpc header
88 * @param p_manager the user event manager.
90 * @return true if the bpc header is valid, fale else.
92 static opj_bool opj_jp2_read_bpcc( opj_jp2_v2_t *jp2,
93 OPJ_BYTE * p_bpc_header_data,
94 OPJ_UINT32 p_bpc_header_size,
95 opj_event_mgr_t * p_manager );
97 static opj_bool opj_jp2_read_cdef( opj_jp2_v2_t * jp2,
98 OPJ_BYTE * p_cdef_header_data,
99 OPJ_UINT32 p_cdef_header_size,
100 opj_event_mgr_t * p_manager );
102 * Writes the Colour Specification box.
104 * @param jp2 jpeg2000 file codec.
105 * @param p_nb_bytes_written pointer to store the nb of bytes written by the function.
107 * @return the data being copied.
109 static OPJ_BYTE * opj_jp2_write_colr( opj_jp2_v2_t *jp2,
110 OPJ_UINT32 * p_nb_bytes_written );
113 * Writes a FTYP box - File type box
115 * @param cio the stream to write data to.
116 * @param jp2 the jpeg2000 file codec.
117 * @param p_manager the user event manager.
119 * @return true if writting was successful.
121 static opj_bool opj_jp2_write_ftyp( opj_jp2_v2_t *jp2,
122 opj_stream_private_t *cio,
123 opj_event_mgr_t * p_manager );
126 * Reads a a FTYP box - File type box
128 * @param p_header_data the data contained in the FTYP box.
129 * @param jp2 the jpeg2000 file codec.
130 * @param p_header_size the size of the data contained in the FTYP box.
131 * @param p_manager the user event manager.
133 * @return true if the FTYP box is valid.
135 static opj_bool opj_jp2_read_ftyp( opj_jp2_v2_t *jp2,
136 OPJ_BYTE * p_header_data,
137 OPJ_UINT32 p_header_size,
138 opj_event_mgr_t * p_manager );
141 * Skips the Jpeg2000 Codestream Header box - JP2C Header box.
143 * @param cio the stream to write data to.
144 * @param jp2 the jpeg2000 file codec.
145 * @param p_manager user event manager.
147 * @return true if writting was successful.
149 opj_bool opj_jp2_skip_jp2c( opj_jp2_v2_t *jp2,
150 opj_stream_private_t *cio,
151 opj_event_mgr_t * p_manager );
154 * Reads the Jpeg2000 file Header box - JP2 Header box (warning, this is a super box).
156 * @param p_header_data the data contained in the file header box.
157 * @param jp2 the jpeg2000 file codec.
158 * @param p_header_size the size of the data contained in the file header box.
159 * @param p_manager the user event manager.
161 * @return true if the JP2 Header box was successfully reconized.
163 static opj_bool opj_jp2_read_jp2h( opj_jp2_v2_t *jp2,
164 OPJ_BYTE *p_header_data,
165 OPJ_UINT32 p_header_size,
166 opj_event_mgr_t * p_manager );
169 * Writes the Jpeg2000 codestream Header box - JP2C Header box. This function must be called AFTER the coding has been done.
171 * @param cio the stream to write data to.
172 * @param jp2 the jpeg2000 file codec.
173 * @param p_manager user event manager.
175 * @return true if writting was successful.
177 static opj_bool opj_jp2_write_jp2c( opj_jp2_v2_t *jp2,
178 opj_stream_private_t *cio,
179 opj_event_mgr_t * p_manager );
182 * Reads a jpeg2000 file signature box.
184 * @param p_header_data the data contained in the signature box.
185 * @param jp2 the jpeg2000 file codec.
186 * @param p_header_size the size of the data contained in the signature box.
187 * @param p_manager the user event manager.
189 * @return true if the file signature box is valid.
191 static opj_bool opj_jp2_read_jp(opj_jp2_v2_t *jp2,
192 OPJ_BYTE * p_header_data,
193 OPJ_UINT32 p_header_size,
194 opj_event_mgr_t * p_manager);
197 * Writes a jpeg2000 file signature box.
199 * @param cio the stream to write data to.
200 * @param jp2 the jpeg2000 file codec.
201 * @param p_manager the user event manager.
203 * @return true if writting was successful.
205 static opj_bool opj_jp2_write_jp( opj_jp2_v2_t *jp2,
206 opj_stream_private_t *cio,
207 opj_event_mgr_t * p_manager );
210 Apply collected palette data
211 @param color Collector for profile, cdef and pclr data
214 static void jp2_apply_pclr(opj_image_t *image, opj_jp2_color_t *color);
217 @param jp2 JP2 handle
218 @param cio Input buffer stream
220 @param color Collector for profile, cdef and pclr data
221 @return Returns true if successful, returns false otherwise
223 static opj_bool opj_jp2_read_pclr( opj_jp2_v2_t *jp2,
224 OPJ_BYTE * p_pclr_header_data,
225 OPJ_UINT32 p_pclr_header_size,
226 opj_event_mgr_t * p_manager );
229 Collect component mapping data
230 @param jp2 JP2 handle
231 @param cio Input buffer stream
233 @param color Collector for profile, cdef and pclr data
234 @return Returns true if successful, returns false otherwise
237 static opj_bool opj_jp2_read_cmap( opj_jp2_v2_t * jp2,
238 OPJ_BYTE * p_cmap_header_data,
239 OPJ_UINT32 p_cmap_header_size,
240 opj_event_mgr_t * p_manager );
243 * Reads the Color Specification box.
245 * @param p_colr_header_data pointer to actual data (already read from file)
246 * @param jp2 the jpeg2000 file codec.
247 * @param p_colr_header_size the size of the color header
248 * @param p_manager the user event manager.
250 * @return true if the bpc header is valid, fale else.
252 static opj_bool opj_jp2_read_colr( opj_jp2_v2_t *jp2,
253 OPJ_BYTE * p_colr_header_data,
254 OPJ_UINT32 p_colr_header_size,
255 opj_event_mgr_t * p_manager );
258 Write file Index (superbox)
259 @param[in] offset_jp2c offset of jp2c box
260 @param[in] length_jp2c length of jp2c box
261 @param[in] offset_idx offset of cidx box
262 @param[in] length_idx length of cidx box
263 @param[in] cio file output handle
264 @return length of fidx box
266 static int write_fidx( int offset_jp2c, int length_jp2c, int offset_idx, int length_idx, opj_cio_t *cio);
268 Write index Finder box
269 @param[in] offset offset of fidx box
270 @param[in] length length of fidx box
271 @param[in] cio file output handle
273 static void write_iptr( int offset, int length, opj_cio_t *cio);
277 @param[in] offset_jp2c offset of jp2c box
278 @param[in] length_jp2c length of jp2c box
279 @param[in] offset_idx offset of cidx box
280 @param[in] length_idx length of cidx box
281 @param[in] cio file output handle
283 static void write_prxy( int offset_jp2c, int length_jp2c, int offset_idx, int length_idx, opj_cio_t *cio);
289 * Sets up the procedures to do on writting header after the codestream.
290 * Developpers wanting to extend the library can add their own writting procedures.
292 static void opj_jp2_setup_end_header_writting (opj_jp2_v2_t *jp2);
296 * Sets up the procedures to do on reading header after the codestream.
297 * Developpers wanting to extend the library can add their own writting procedures.
299 static void opj_jp2_setup_end_header_reading (opj_jp2_v2_t *jp2);
302 * Reads a jpeg2000 file header structure.
304 * @param cio the stream to read data from.
305 * @param jp2 the jpeg2000 file header structure.
306 * @param p_manager the user event manager.
308 * @return true if the box is valid.
310 static opj_bool opj_jp2_read_header_procedure( opj_jp2_v2_t *jp2,
311 opj_stream_private_t *stream,
312 opj_event_mgr_t * p_manager );
315 * Excutes the given procedures on the given codec.
317 * @param p_procedure_list the list of procedures to execute
318 * @param jp2 the jpeg2000 file codec to execute the procedures on.
319 * @param stream the stream to execute the procedures on.
320 * @param p_manager the user manager.
322 * @return true if all the procedures were successfully executed.
324 static opj_bool opj_jp2_exec ( opj_jp2_v2_t * jp2,
325 opj_procedure_list_t * p_procedure_list,
326 opj_stream_private_t *stream,
327 opj_event_mgr_t * p_manager );
330 * Reads a box header. The box is the way data is packed inside a jpeg2000 file structure.
332 * @param cio the input stream to read data from.
333 * @param box the box structure to fill.
334 * @param p_number_bytes_read pointer to an int that will store the number of bytes read from the stream (shoul usually be 2).
335 * @param p_manager user event manager.
337 * @return true if the box is reconized, false otherwise
339 static opj_bool opj_jp2_read_boxhdr(opj_jp2_box_t *box,
340 OPJ_UINT32 * p_number_bytes_read,
341 opj_stream_private_t *cio,
342 opj_event_mgr_t * p_manager);
345 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
346 * are valid. Developpers wanting to extend the library can add their own validation procedures.
348 static void opj_jp2_setup_encoding_validation (opj_jp2_v2_t *jp2);
352 * Sets up the procedures to do on writting header. Developpers wanting to extend the library can add their own writting procedures.
354 static void opj_jp2_setup_header_writting (opj_jp2_v2_t *jp2);
357 * The default validation procedure without any extension.
359 * @param jp2 the jpeg2000 codec to validate.
360 * @param cio the input stream to validate.
361 * @param p_manager the user event manager.
363 * @return true if the parameters are correct.
365 opj_bool opj_jp2_default_validation ( opj_jp2_v2_t * jp2,
366 opj_stream_private_t *cio,
367 opj_event_mgr_t * p_manager );
370 * Finds the image execution function related to the given box id.
372 * @param p_id the id of the handler to fetch.
374 * @return the given handler or NULL if it could not be found.
376 static const opj_jp2_header_handler_t * opj_jp2_img_find_handler (OPJ_UINT32 p_id);
379 * Finds the execution function related to the given box id.
381 * @param p_id the id of the handler to fetch.
383 * @return the given handler or NULL if it could not be found.
385 static const opj_jp2_header_handler_t * opj_jp2_find_handler (OPJ_UINT32 p_id );
387 const opj_jp2_header_handler_t jp2_header [] =
389 {JP2_JP,opj_jp2_read_jp},
390 {JP2_FTYP,opj_jp2_read_ftyp},
391 {JP2_JP2H,opj_jp2_read_jp2h}
394 const opj_jp2_header_handler_t jp2_img_header [] =
396 {JP2_IHDR,opj_jp2_read_ihdr},
397 {JP2_COLR,opj_jp2_read_colr},
398 {JP2_BPCC,opj_jp2_read_bpcc},
399 {JP2_PCLR,opj_jp2_read_pclr},
400 {JP2_CMAP,opj_jp2_read_cmap},
401 {JP2_CDEF,opj_jp2_read_cdef}
406 * Reads a box header. The box is the way data is packed inside a jpeg2000 file structure. Data is read from a character string
408 * @param p_data the character string to read data from.
409 * @param box the box structure to fill.
410 * @param p_number_bytes_read pointer to an int that will store the number of bytes read from the stream (shoul usually be 2).
411 * @param p_box_max_size the maximum number of bytes in the box.
413 * @return true if the box is reconized, false otherwise
415 static opj_bool opj_jp2_read_boxhdr_char( opj_jp2_box_t *box,
417 OPJ_UINT32 * p_number_bytes_read,
418 OPJ_UINT32 p_box_max_size,
419 opj_event_mgr_t * p_manager );
422 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
423 * are valid. Developpers wanting to extend the library can add their own validation procedures.
425 static void opj_jp2_setup_decoding_validation (opj_jp2_v2_t *jp2);
428 * Sets up the procedures to do on reading header.
429 * Developpers wanting to extend the library can add their own writting procedures.
431 static void opj_jp2_setup_header_reading (opj_jp2_v2_t *jp2);
435 /* ----------------------------------------------------------------------- */
438 * Reads a box header. The box is the way data is packed inside a jpeg2000 file structure.
440 * @param cio the input stream to read data from.
441 * @param box the box structure to fill.
442 * @param p_number_bytes_read pointer to an int that will store the number of bytes read from the stream (should usually be 8).
443 * @param p_manager user event manager.
445 * @return true if the box is reconized, false otherwise
447 opj_bool opj_jp2_read_boxhdr(opj_jp2_box_t *box,
448 OPJ_UINT32 * p_number_bytes_read,
449 opj_stream_private_t *cio,
450 opj_event_mgr_t * p_manager
453 /* read header from file */
454 unsigned char l_data_header [8];
459 assert(p_number_bytes_read != 00);
460 assert(p_manager != 00);
462 *p_number_bytes_read = 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 /* do we have a "special very large box ?" */
472 /* read then the XLBox */
473 if (box->length == 1) {
474 OPJ_UINT32 l_xl_part_size;
476 OPJ_UINT32 l_nb_bytes_read = opj_stream_read_data(cio,l_data_header,8,p_manager);
477 if (l_nb_bytes_read != 8) {
478 if (l_nb_bytes_read > 0) {
479 *p_number_bytes_read += l_nb_bytes_read;
485 opj_read_bytes(l_data_header,&l_xl_part_size, 4);
486 if (l_xl_part_size != 0) {
487 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot handle box sizes higher than 2^32\n");
490 opj_read_bytes(l_data_header,&(box->length), 4);
496 static void jp2_write_url(opj_cio_t *cio, char *Idx_file) {
500 box.init_pos = cio_tell(cio);
502 cio_write(cio, JP2_URL, 4); /* DBTL */
503 cio_write(cio, 0, 1); /* VERS */
504 cio_write(cio, 0, 3); /* FLAG */
507 for (i = 0; i < strlen(Idx_file); i++) {
508 cio_write(cio, Idx_file[i], 1);
512 box.length = cio_tell(cio) - box.init_pos;
513 cio_seek(cio, box.init_pos);
514 cio_write(cio, box.length, 4); /* L */
515 cio_seek(cio, box.init_pos + box.length);
521 * Reads a IHDR box - Image Header box
523 * @param jp2 the jpeg2000 file codec.
524 * @param p_image_header_data pointer to actual data (already read from file)
525 * @param p_image_header_size the size of the image header
526 * @param p_manager the user event manager.
528 * @return true if the image header is valid, fale else.
530 opj_bool opj_jp2_read_ihdr( opj_jp2_v2_t *jp2,
531 OPJ_BYTE *p_image_header_data,
532 OPJ_UINT32 p_image_header_size,
533 opj_event_mgr_t * p_manager )
536 assert(p_image_header_data != 00);
538 assert(p_manager != 00);
540 if (p_image_header_size != 14) {
541 opj_event_msg_v2(p_manager, EVT_ERROR, "Bad image header box (bad size)\n");
545 opj_read_bytes(p_image_header_data,&(jp2->h),4); /* HEIGHT */
546 p_image_header_data += 4;
547 opj_read_bytes(p_image_header_data,&(jp2->w),4); /* WIDTH */
548 p_image_header_data += 4;
549 opj_read_bytes(p_image_header_data,&(jp2->numcomps),2); /* NC */
550 p_image_header_data += 2;
552 /* allocate memory for components */
553 jp2->comps = (opj_jp2_comps_t*) opj_malloc(jp2->numcomps * sizeof(opj_jp2_comps_t));
554 if (jp2->comps == 0) {
555 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to handle image header (ihdr)\n");
558 memset(jp2->comps,0,jp2->numcomps * sizeof(opj_jp2_comps_t));
560 opj_read_bytes(p_image_header_data,&(jp2->bpc),1); /* BPC */
561 ++ p_image_header_data;
563 /* if equal to 0 then need a BPC box (cf. chapter about image header box of the norm) */
564 /*if (jp2->bpc == 0){
565 indicate with a flag that we will wait a BPC box
568 opj_read_bytes(p_image_header_data,&(jp2->C),1); /* C */
569 ++ p_image_header_data;
571 /* Should be equal to 7 cf. chapter about image header box of the norm */
573 opj_event_msg_v2(p_manager, EVT_INFO, "JP2 IHDR box: compression type indicate that the file is not a conforming JP2 file (%d) \n", jp2->C);
576 opj_read_bytes(p_image_header_data,&(jp2->UnkC),1); /* UnkC */
577 ++ p_image_header_data;
578 opj_read_bytes(p_image_header_data,&(jp2->IPR),1); /* IPR */
579 ++ p_image_header_data;
585 * Writes the Image Header box - Image Header box.
587 * @param jp2 jpeg2000 file codec.
588 * @param p_nb_bytes_written pointer to store the nb of bytes written by the function.
590 * @return the data being copied.
592 OPJ_BYTE * opj_jp2_write_ihdr(opj_jp2_v2_t *jp2,
593 OPJ_UINT32 * p_nb_bytes_written
596 unsigned char * l_ihdr_data,* l_current_ihdr_ptr;
600 assert(p_nb_bytes_written != 00);
602 /* default image header is 22 bytes wide */
603 l_ihdr_data = (unsigned char *) opj_malloc(22);
604 if (l_ihdr_data == 00) {
607 memset(l_ihdr_data,0,22);
609 l_current_ihdr_ptr = l_ihdr_data;
611 opj_write_bytes(l_current_ihdr_ptr,22,4); /* write box size */
612 l_current_ihdr_ptr+=4;
614 opj_write_bytes(l_current_ihdr_ptr,JP2_IHDR, 4); /* IHDR */
615 l_current_ihdr_ptr+=4;
617 opj_write_bytes(l_current_ihdr_ptr,jp2->h, 4); /* HEIGHT */
618 l_current_ihdr_ptr+=4;
620 opj_write_bytes(l_current_ihdr_ptr, jp2->w, 4); /* WIDTH */
621 l_current_ihdr_ptr+=4;
623 opj_write_bytes(l_current_ihdr_ptr, jp2->numcomps, 2); /* NC */
624 l_current_ihdr_ptr+=2;
626 opj_write_bytes(l_current_ihdr_ptr, jp2->bpc, 1); /* BPC */
627 ++l_current_ihdr_ptr;
629 opj_write_bytes(l_current_ihdr_ptr, jp2->C, 1); /* C : Always 7 */
630 ++l_current_ihdr_ptr;
632 opj_write_bytes(l_current_ihdr_ptr, jp2->UnkC, 1); /* UnkC, colorspace unknown */
633 ++l_current_ihdr_ptr;
635 opj_write_bytes(l_current_ihdr_ptr, jp2->IPR, 1); /* IPR, no intellectual property */
636 ++l_current_ihdr_ptr;
638 *p_nb_bytes_written = 22;
644 * Writes the Bit per Component box.
646 * @param jp2 jpeg2000 file codec.
647 * @param p_nb_bytes_written pointer to store the nb of bytes written by the function.
649 * @return the data being copied.
651 OPJ_BYTE * opj_jp2_write_bpcc( opj_jp2_v2_t *jp2,
652 OPJ_UINT32 * p_nb_bytes_written
656 /* room for 8 bytes for box and 1 byte for each component */
657 int l_bpcc_size = 8 + jp2->numcomps;
658 unsigned char * l_bpcc_data,* l_current_bpcc_ptr;
662 assert(p_nb_bytes_written != 00);
664 l_bpcc_data = (unsigned char *) opj_malloc(l_bpcc_size);
665 if (l_bpcc_data == 00) {
668 memset(l_bpcc_data,0,l_bpcc_size);
670 l_current_bpcc_ptr = l_bpcc_data;
672 opj_write_bytes(l_current_bpcc_ptr,l_bpcc_size,4); /* write box size */
673 l_current_bpcc_ptr += 4;
675 opj_write_bytes(l_current_bpcc_ptr,JP2_BPCC,4); /* BPCC */
676 l_current_bpcc_ptr += 4;
678 for (i = 0; i < jp2->numcomps; ++i) {
679 opj_write_bytes(l_current_bpcc_ptr, jp2->comps[i].bpcc, 1); /* write each component information */
680 ++l_current_bpcc_ptr;
683 *p_nb_bytes_written = l_bpcc_size;
690 * Reads a Bit per Component box.
692 * @param jp2 the jpeg2000 file codec.
693 * @param p_bpc_header_data pointer to actual data (already read from file)
694 * @param p_bpc_header_size pointer that will hold the size of the bpc header
695 * @param p_manager the user event manager.
697 * @return true if the bpc header is valid, false otherwise.
699 opj_bool opj_jp2_read_bpcc( opj_jp2_v2_t *jp2,
700 OPJ_BYTE * p_bpc_header_data,
701 OPJ_UINT32 p_bpc_header_size,
702 opj_event_mgr_t * p_manager
708 assert(p_bpc_header_data != 00);
710 assert(p_manager != 00);
713 /*if (jp2->bpc != 0 ){
714 opj_event_msg_v2(p_manager, EVT_WARNING, "A BPCC header box is available although BPC is different to zero (%d)\n",jp2->bpc);
717 /* and length is relevant */
718 if (p_bpc_header_size != jp2->numcomps) {
719 opj_event_msg_v2(p_manager, EVT_ERROR, "Bad BPCC header box (bad size)\n");
723 /* read info for each component */
724 for (i = 0; i < jp2->numcomps; ++i) {
725 opj_read_bytes(p_bpc_header_data,&jp2->comps[i].bpcc ,1); /* read each BPCC component */
733 * Writes the Colour Specification box.
735 * @param jp2 jpeg2000 file codec.
736 * @param p_nb_bytes_written pointer to store the nb of bytes written by the function.
738 * @return the data being copied.
740 OPJ_BYTE * opj_jp2_write_colr( opj_jp2_v2_t *jp2,
741 OPJ_UINT32 * p_nb_bytes_written
744 /* room for 8 bytes for box 3 for common data and variable upon profile*/
745 unsigned int l_colr_size = 11;
746 unsigned char * l_colr_data,* l_current_colr_ptr;
750 assert(p_nb_bytes_written != 00);
763 l_colr_data = (unsigned char *) opj_malloc(l_colr_size);
764 if (l_colr_data == 00) {
767 memset(l_colr_data,0,l_colr_size);
769 l_current_colr_ptr = l_colr_data;
771 opj_write_bytes(l_current_colr_ptr,l_colr_size,4); /* write box size */
772 l_current_colr_ptr += 4;
774 opj_write_bytes(l_current_colr_ptr,JP2_COLR,4); /* BPCC */
775 l_current_colr_ptr += 4;
777 opj_write_bytes(l_current_colr_ptr, jp2->meth,1); /* METH */
778 ++l_current_colr_ptr;
780 opj_write_bytes(l_current_colr_ptr, jp2->precedence,1); /* PRECEDENCE */
781 ++l_current_colr_ptr;
783 opj_write_bytes(l_current_colr_ptr, jp2->approx,1); /* APPROX */
784 ++l_current_colr_ptr;
786 if (jp2->meth == 1) {
787 opj_write_bytes(l_current_colr_ptr, jp2->enumcs,4); /* EnumCS */
790 opj_write_bytes(l_current_colr_ptr, 0, 1); /* PROFILE (??) */
793 *p_nb_bytes_written = l_colr_size;
798 static void jp2_free_pclr(opj_jp2_color_t *color)
800 opj_free(color->jp2_pclr->channel_sign);
801 opj_free(color->jp2_pclr->channel_size);
802 opj_free(color->jp2_pclr->entries);
804 if(color->jp2_pclr->cmap) opj_free(color->jp2_pclr->cmap);
806 opj_free(color->jp2_pclr); color->jp2_pclr = NULL;
809 static void free_color_data(opj_jp2_color_t *color)
813 jp2_free_pclr(color);
817 if(color->jp2_cdef->info) opj_free(color->jp2_cdef->info);
818 opj_free(color->jp2_cdef);
820 if(color->icc_profile_buf) opj_free(color->icc_profile_buf);
824 static void jp2_apply_pclr(opj_image_t *image, opj_jp2_color_t *color)
826 opj_image_comp_t *old_comps, *new_comps;
827 OPJ_BYTE *channel_size, *channel_sign;
829 opj_jp2_cmap_comp_t *cmap;
830 OPJ_INT32 *src, *dst;
832 OPJ_UINT16 i, nr_channels, cmp, pcol;
835 channel_size = color->jp2_pclr->channel_size;
836 channel_sign = color->jp2_pclr->channel_sign;
837 entries = color->jp2_pclr->entries;
838 cmap = color->jp2_pclr->cmap;
839 nr_channels = color->jp2_pclr->nr_channels;
841 old_comps = image->comps;
842 new_comps = (opj_image_comp_t*)
843 opj_malloc(nr_channels * sizeof(opj_image_comp_t));
845 for(i = 0; i < nr_channels; ++i) {
846 pcol = cmap[i].pcol; cmp = cmap[i].cmp;
848 new_comps[pcol] = old_comps[cmp];
851 if(cmap[i].mtyp == 0){
852 old_comps[cmp].data = NULL; continue;
855 /* Palette mapping: */
856 new_comps[pcol].data = (int*)
857 opj_malloc(old_comps[cmp].w * old_comps[cmp].h * sizeof(int));
858 new_comps[pcol].prec = channel_size[i];
859 new_comps[pcol].sgnd = channel_sign[i];
862 top_k = color->jp2_pclr->nr_entries - 1;
864 for(i = 0; i < nr_channels; ++i) {
866 if(cmap[i].mtyp == 0) continue;
868 /* Palette mapping: */
869 cmp = cmap[i].cmp; pcol = cmap[i].pcol;
870 src = old_comps[cmp].data;
871 dst = new_comps[pcol].data;
872 max = new_comps[pcol].w * new_comps[pcol].h;
874 for(j = 0; j < max; ++j)
877 if((k = src[j]) < 0) k = 0; else if(k > top_k) k = top_k;
880 dst[j] = entries[k * nr_channels + pcol];
884 max = image->numcomps;
885 for(i = 0; i < max; ++i) {
886 if(old_comps[i].data) opj_free(old_comps[i].data);
890 image->comps = new_comps;
891 image->numcomps = nr_channels;
893 jp2_free_pclr(color);
898 * Reads a palette box.
900 * @param jp2 the jpeg2000 file codec.
901 * @param p_pclr_header_data pointer to actual data (already read from file)
902 * @param p_pclr_header_size pointer that will hold the size of the PCLR header
903 * @param p_manager the user event manager.
905 * @return true if the bpc header is valid, fale else.
907 opj_bool opj_jp2_read_pclr( opj_jp2_v2_t *jp2,
908 OPJ_BYTE * p_pclr_header_data,
909 OPJ_UINT32 p_pclr_header_size,
910 opj_event_mgr_t * p_manager
913 opj_jp2_pclr_t *jp2_pclr;
914 OPJ_BYTE *channel_size, *channel_sign;
916 OPJ_UINT16 nr_entries,nr_channels;
921 assert(p_pclr_header_data != 00);
923 assert(p_manager != 00);
924 (void)p_pclr_header_size;
926 if(jp2->color.jp2_pclr)
929 opj_read_bytes(p_pclr_header_data, &l_value , 2); /* NE */
930 p_pclr_header_data += 2;
931 nr_entries = (OPJ_UINT16) l_value;
933 opj_read_bytes(p_pclr_header_data, &l_value , 1); /* NPC */
934 ++p_pclr_header_data;
935 nr_channels = (OPJ_UINT16) l_value;
937 entries = (OPJ_UINT32*) opj_malloc(nr_channels * nr_entries * sizeof(OPJ_UINT32));
938 channel_size = (OPJ_BYTE*) opj_malloc(nr_channels);
939 channel_sign = (OPJ_BYTE*) opj_malloc(nr_channels);
941 jp2_pclr = (opj_jp2_pclr_t*)opj_malloc(sizeof(opj_jp2_pclr_t));
942 jp2_pclr->channel_sign = channel_sign;
943 jp2_pclr->channel_size = channel_size;
944 jp2_pclr->entries = entries;
945 jp2_pclr->nr_entries = nr_entries;
946 jp2_pclr->nr_channels = (OPJ_BYTE) l_value;
947 jp2_pclr->cmap = NULL;
949 jp2->color.jp2_pclr = jp2_pclr;
951 for(i = 0; i < nr_channels; ++i) {
952 opj_read_bytes(p_pclr_header_data, &l_value , 1); /* Bi */
953 ++p_pclr_header_data;
955 channel_size[i] = (l_value & 0x7f) + 1;
956 channel_sign[i] = (l_value & 0x80)? 1 : 0;
959 for(j = 0; j < nr_entries; ++j) {
960 for(i = 0; i < nr_channels; ++i) {
961 int bytes_to_read = (channel_size[i]+7)>>3;
963 opj_read_bytes(p_pclr_header_data, &l_value , bytes_to_read); /* Cji */
964 p_pclr_header_data += bytes_to_read;
965 *entries = (OPJ_UINT32) l_value;
974 * Reads the Component Mapping box.
976 * @param p_cmap_header_data pointer to actual data (already read from file)
977 * @param jp2 the jpeg2000 file codec.
978 * @param p_cmap_header_size pointer that will hold the size of the color header
979 * @param p_manager the user event manager.
981 * @return true if the cdef header is valid, false else.
983 opj_bool opj_jp2_read_cmap( opj_jp2_v2_t * jp2,
984 OPJ_BYTE * p_cmap_header_data,
985 OPJ_UINT32 p_cmap_header_size,
986 opj_event_mgr_t * p_manager
989 opj_jp2_cmap_comp_t *cmap;
990 OPJ_BYTE i, nr_channels;
995 assert(p_cmap_header_data != 00);
996 assert(p_manager != 00);
997 (void)p_cmap_header_size;
999 /* Need nr_channels: */
1000 if(jp2->color.jp2_pclr == NULL) {
1001 opj_event_msg_v2(p_manager, EVT_ERROR, "Need to read a PCLR box before the CMAP box.\n");
1005 /* Part 1, I.5.3.5: 'There shall be at most one Component Mapping box
1006 * inside a JP2 Header box' :
1008 if(jp2->color.jp2_pclr->cmap) {
1009 opj_event_msg_v2(p_manager, EVT_ERROR, "Only one CMAP box is allowed.\n");
1013 nr_channels = jp2->color.jp2_pclr->nr_channels;
1014 cmap = (opj_jp2_cmap_comp_t*) opj_malloc(nr_channels * sizeof(opj_jp2_cmap_comp_t));
1016 for(i = 0; i < nr_channels; ++i) {
1017 opj_read_bytes(p_cmap_header_data, &l_value, 2); /* CMP^i */
1018 p_cmap_header_data +=2;
1019 cmap[i].cmp = (OPJ_UINT16) l_value;
1021 opj_read_bytes(p_cmap_header_data, &l_value, 1); /* MTYP^i */
1022 ++p_cmap_header_data;
1023 cmap[i].mtyp = (OPJ_BYTE) l_value;
1025 opj_read_bytes(p_cmap_header_data, &l_value, 1); /* PCOL^i */
1026 ++p_cmap_header_data;
1027 cmap[i].pcol = (OPJ_BYTE) l_value;
1030 jp2->color.jp2_pclr->cmap = cmap;
1035 static void jp2_apply_cdef(opj_image_t *image, opj_jp2_color_t *color)
1037 opj_jp2_cdef_info_t *info;
1038 OPJ_INT32 color_space;
1039 OPJ_UINT16 i, n, cn, typ, asoc, acn;
1041 color_space = image->color_space;
1042 info = color->jp2_cdef->info;
1043 n = color->jp2_cdef->n;
1045 for(i = 0; i < n; ++i)
1047 /* WATCH: acn = asoc - 1 ! */
1048 if((asoc = info[i].asoc) == 0) continue;
1050 cn = info[i].cn; typ = info[i].typ; acn = asoc - 1;
1054 opj_image_comp_t saved;
1056 memcpy(&saved, &image->comps[cn], sizeof(opj_image_comp_t));
1057 memcpy(&image->comps[cn], &image->comps[acn], sizeof(opj_image_comp_t));
1058 memcpy(&image->comps[acn], &saved, sizeof(opj_image_comp_t));
1060 info[i].asoc = cn + 1;
1061 info[acn].asoc = info[acn].cn + 1;
1065 if(color->jp2_cdef->info) opj_free(color->jp2_cdef->info);
1067 opj_free(color->jp2_cdef); color->jp2_cdef = NULL;
1069 }/* jp2_apply_cdef() */
1072 * Reads the Component Definition box.
1074 * @param p_cdef_header_data pointer to actual data (already read from file)
1075 * @param jp2 the jpeg2000 file codec.
1076 * @param p_cdef_header_size pointer that will hold the size of the color header
1077 * @param p_manager the user event manager.
1079 * @return true if the cdef header is valid, false else.
1081 opj_bool opj_jp2_read_cdef( opj_jp2_v2_t * jp2,
1082 OPJ_BYTE * p_cdef_header_data,
1083 OPJ_UINT32 p_cdef_header_size,
1084 opj_event_mgr_t * p_manager
1087 opj_jp2_cdef_info_t *cdef_info;
1093 assert(p_cdef_header_data != 00);
1094 assert(p_manager != 00);
1095 (void)p_cdef_header_size;
1097 /* Part 1, I.5.3.6: 'The shall be at most one Channel Definition box
1098 * inside a JP2 Header box.'*/
1099 if(jp2->color.jp2_cdef) return OPJ_FALSE;
1101 opj_read_bytes(p_cdef_header_data,&l_value ,2); /* N */
1102 p_cdef_header_data+= 2;
1104 if ( (OPJ_UINT16)l_value == 0){ /* szukw000: FIXME */
1105 opj_event_msg_v2(p_manager, EVT_ERROR, "Number of channel description is equal to zero in CDEF box.\n");
1109 cdef_info = (opj_jp2_cdef_info_t*) opj_malloc(l_value * sizeof(opj_jp2_cdef_info_t));
1111 jp2->color.jp2_cdef = (opj_jp2_cdef_t*)opj_malloc(sizeof(opj_jp2_cdef_t));
1112 jp2->color.jp2_cdef->info = cdef_info;
1113 jp2->color.jp2_cdef->n = (OPJ_UINT16) l_value;
1115 for(i = 0; i < jp2->color.jp2_cdef->n; ++i) {
1116 opj_read_bytes(p_cdef_header_data, &l_value, 2); /* Cn^i */
1117 p_cdef_header_data +=2;
1118 cdef_info[i].cn = (OPJ_UINT16) l_value;
1120 opj_read_bytes(p_cdef_header_data, &l_value, 2); /* Typ^i */
1121 p_cdef_header_data +=2;
1122 cdef_info[i].typ = (OPJ_UINT16) l_value;
1124 opj_read_bytes(p_cdef_header_data, &l_value, 2); /* Asoc^i */
1125 p_cdef_header_data +=2;
1126 cdef_info[i].asoc = (OPJ_UINT16) l_value;
1133 * Reads the Colour Specification box.
1135 * @param p_colr_header_data pointer to actual data (already read from file)
1136 * @param jp2 the jpeg2000 file codec.
1137 * @param p_colr_header_size pointer that will hold the size of the color header
1138 * @param p_colr_header_max_size maximum size of the header, any size bigger than this value should result the function to output false.
1139 * @param p_manager the user event manager.
1141 * @return true if the bpc header is valid, fale else.
1143 opj_bool opj_jp2_read_colr( opj_jp2_v2_t *jp2,
1144 OPJ_BYTE * p_colr_header_data,
1145 OPJ_UINT32 p_colr_header_size,
1146 opj_event_mgr_t * p_manager
1153 assert(p_colr_header_data != 00);
1154 assert(p_manager != 00);
1156 if (p_colr_header_size < 3) {
1157 opj_event_msg_v2(p_manager, EVT_ERROR, "Bad COLR header box (bad size)\n");
1161 /* Part 1, I.5.3.3 : 'A conforming JP2 reader shall ignore all Colour
1162 * Specification boxes after the first.'
1164 if(jp2->color.jp2_has_colr) {
1165 opj_event_msg_v2(p_manager, EVT_INFO, "A conforming JP2 reader shall ignore all Colour Specification boxes after the first, so we ignore this one.\n");
1166 p_colr_header_data += p_colr_header_size;
1170 opj_read_bytes(p_colr_header_data,&jp2->meth ,1); /* METH */
1171 ++p_colr_header_data;
1173 opj_read_bytes(p_colr_header_data,&jp2->precedence ,1); /* PRECEDENCE */
1174 ++p_colr_header_data;
1176 opj_read_bytes(p_colr_header_data,&jp2->approx ,1); /* APPROX */
1177 ++p_colr_header_data;
1179 if (jp2->meth == 1) {
1180 if (p_colr_header_size != 7) {
1181 opj_event_msg_v2(p_manager, EVT_ERROR, "Bad BPCC header box (bad size)\n");
1185 opj_read_bytes(p_colr_header_data,&jp2->enumcs ,4); /* EnumCS */
1187 else if (jp2->meth == 2) {
1189 int it_icc_value = 0;
1190 int icc_len = p_colr_header_size - 3;
1192 jp2->color.icc_profile_len = icc_len;
1193 jp2->color.icc_profile_buf = (unsigned char*) opj_malloc(icc_len);
1195 memset(jp2->color.icc_profile_buf, 0, icc_len * sizeof(unsigned char));
1197 for (it_icc_value = 0; it_icc_value < icc_len; ++it_icc_value)
1199 opj_read_bytes(p_colr_header_data,&l_value,1); /* icc values */
1200 ++p_colr_header_data;
1201 jp2->color.icc_profile_buf[it_icc_value] = (OPJ_BYTE) l_value;
1206 opj_event_msg_v2(p_manager, EVT_INFO, "COLR BOX meth value is not a regular value (%d), so we will skip the fields following the approx field.\n", jp2->meth);
1208 jp2->color.jp2_has_colr = 1;
1214 opj_bool opj_jp2_decode(opj_jp2_v2_t *jp2,
1215 opj_stream_private_t *p_stream,
1216 opj_image_t* p_image,
1217 opj_event_mgr_t * p_manager)
1223 if( ! opj_j2k_decode(jp2->j2k, p_stream, p_image, p_manager) ) {
1224 opj_event_msg_v2(p_manager, EVT_ERROR, "Failed to decode the codestream in the JP2 file\n");
1228 if (!jp2->ignore_pclr_cmap_cdef){
1230 /* Set Image Color Space */
1231 if (jp2->enumcs == 16)
1232 p_image->color_space = CLRSPC_SRGB;
1233 else if (jp2->enumcs == 17)
1234 p_image->color_space = CLRSPC_GRAY;
1235 else if (jp2->enumcs == 18)
1236 p_image->color_space = CLRSPC_SYCC;
1238 p_image->color_space = CLRSPC_UNKNOWN;
1240 /* Apply the color space if needed */
1241 if(jp2->color.jp2_cdef) {
1242 jp2_apply_cdef(p_image, &(jp2->color));
1245 if(jp2->color.jp2_pclr) {
1246 /* Part 1, I.5.3.4: Either both or none : */
1247 if( !jp2->color.jp2_pclr->cmap)
1248 jp2_free_pclr(&(jp2->color));
1250 jp2_apply_pclr(p_image, &(jp2->color));
1253 if(jp2->color.icc_profile_buf) {
1254 p_image->icc_profile_buf = jp2->color.icc_profile_buf;
1255 p_image->icc_profile_len = jp2->color.icc_profile_len;
1256 jp2->color.icc_profile_buf = NULL;
1265 * Writes the Jpeg2000 file Header box - JP2 Header box (warning, this is a super box).
1267 * @param cio the stream to write data to.
1268 * @param jp2 the jpeg2000 file codec.
1269 * @param p_manager user event manager.
1271 * @return true if writting was successful.
1273 opj_bool opj_jp2_write_jp2h(opj_jp2_v2_t *jp2,
1274 opj_stream_private_t *stream,
1275 opj_event_mgr_t * p_manager
1278 opj_jp2_img_header_writer_handler_t l_writers [3];
1279 opj_jp2_img_header_writer_handler_t * l_current_writer;
1282 /* size of data for super box*/
1283 int l_jp2h_size = 8;
1284 opj_bool l_result = OPJ_TRUE;
1286 /* to store the data of the super box */
1287 unsigned char l_jp2h_data [8];
1290 assert(stream != 00);
1292 assert(p_manager != 00);
1294 memset(l_writers,0,sizeof(l_writers));
1296 if (jp2->bpc == 255) {
1298 l_writers[0].handler = opj_jp2_write_ihdr;
1299 l_writers[1].handler = opj_jp2_write_bpcc;
1300 l_writers[2].handler = opj_jp2_write_colr;
1304 l_writers[0].handler = opj_jp2_write_ihdr;
1305 l_writers[1].handler = opj_jp2_write_colr;
1308 /* write box header */
1309 /* write JP2H type */
1310 opj_write_bytes(l_jp2h_data+4,JP2_JP2H,4);
1312 l_current_writer = l_writers;
1313 for (i=0;i<l_nb_pass;++i) {
1314 l_current_writer->m_data = l_current_writer->handler(jp2,&(l_current_writer->m_size));
1315 if (l_current_writer->m_data == 00) {
1316 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to hold JP2 Header data\n");
1317 l_result = OPJ_FALSE;
1321 l_jp2h_size += l_current_writer->m_size;
1326 l_current_writer = l_writers;
1327 for (i=0;i<l_nb_pass;++i) {
1328 if (l_current_writer->m_data != 00) {
1329 opj_free(l_current_writer->m_data );
1337 /* write super box size */
1338 opj_write_bytes(l_jp2h_data,l_jp2h_size,4);
1340 /* write super box data on stream */
1341 if (opj_stream_write_data(stream,l_jp2h_data,8,p_manager) != 8) {
1342 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream error while writing JP2 Header box\n");
1343 l_result = OPJ_FALSE;
1347 l_current_writer = l_writers;
1348 for (i=0;i<l_nb_pass;++i) {
1349 if (opj_stream_write_data(stream,l_current_writer->m_data,l_current_writer->m_size,p_manager) != l_current_writer->m_size) {
1350 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream error while writting JP2 Header box\n");
1351 l_result = OPJ_FALSE;
1358 l_current_writer = l_writers;
1361 for (i=0;i<l_nb_pass;++i) {
1362 if (l_current_writer->m_data != 00) {
1363 opj_free(l_current_writer->m_data );
1372 * Writes a FTYP box - File type box
1374 * @param cio the stream to write data to.
1375 * @param jp2 the jpeg2000 file codec.
1376 * @param p_manager the user event manager.
1378 * @return true if writting was successful.
1380 opj_bool opj_jp2_write_ftyp(opj_jp2_v2_t *jp2,
1381 opj_stream_private_t *cio,
1382 opj_event_mgr_t * p_manager )
1385 unsigned int l_ftyp_size = 16 + 4 * jp2->numcl;
1386 unsigned char * l_ftyp_data, * l_current_data_ptr;
1392 assert(p_manager != 00);
1394 l_ftyp_data = (unsigned char *) opj_malloc(l_ftyp_size);
1396 if (l_ftyp_data == 00) {
1397 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to handle ftyp data\n");
1401 memset(l_ftyp_data,0,l_ftyp_size);
1403 l_current_data_ptr = l_ftyp_data;
1405 opj_write_bytes(l_current_data_ptr, l_ftyp_size,4); /* box size */
1406 l_current_data_ptr += 4;
1408 opj_write_bytes(l_current_data_ptr, JP2_FTYP,4); /* FTYP */
1409 l_current_data_ptr += 4;
1411 opj_write_bytes(l_current_data_ptr, jp2->brand,4); /* BR */
1412 l_current_data_ptr += 4;
1414 opj_write_bytes(l_current_data_ptr, jp2->minversion,4); /* MinV */
1415 l_current_data_ptr += 4;
1417 for (i = 0; i < jp2->numcl; i++) {
1418 opj_write_bytes(l_current_data_ptr, jp2->cl[i],4); /* CL */
1421 l_result = (opj_stream_write_data(cio,l_ftyp_data,l_ftyp_size,p_manager) == l_ftyp_size);
1424 opj_event_msg_v2(p_manager, EVT_ERROR, "Error while writting ftyp data to stream\n");
1427 opj_free(l_ftyp_data);
1433 * Writes the Jpeg2000 codestream Header box - JP2C Header box.
1435 * @param cio the stream to write data to.
1436 * @param jp2 the jpeg2000 file codec.
1437 * @param p_manager user event manager.
1439 * @return true if writting was successful.
1441 opj_bool opj_jp2_write_jp2c(opj_jp2_v2_t *jp2,
1442 opj_stream_private_t *cio,
1443 opj_event_mgr_t * p_manager )
1445 OPJ_OFF_T j2k_codestream_exit;
1446 OPJ_BYTE l_data_header [8];
1451 assert(p_manager != 00);
1452 assert(opj_stream_has_seek(cio));
1454 j2k_codestream_exit = opj_stream_tell(cio);
1455 opj_write_bytes(l_data_header,
1456 (OPJ_UINT32) (j2k_codestream_exit - jp2->j2k_codestream_offset),
1457 4); /* size of codestream */
1458 opj_write_bytes(l_data_header + 4,JP2_JP2C,4); /* JP2C */
1460 if (! opj_stream_seek(cio,jp2->j2k_codestream_offset,p_manager)) {
1461 opj_event_msg_v2(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
1465 if (opj_stream_write_data(cio,l_data_header,8,p_manager) != 8) {
1466 opj_event_msg_v2(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
1470 if (! opj_stream_seek(cio,j2k_codestream_exit,p_manager)) {
1471 opj_event_msg_v2(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
1479 * Writes a jpeg2000 file signature box.
1481 * @param cio the stream to write data to.
1482 * @param jp2 the jpeg2000 file codec.
1483 * @param p_manager the user event manager.
1485 * @return true if writting was successful.
1487 opj_bool opj_jp2_write_jp( opj_jp2_v2_t *jp2,
1488 opj_stream_private_t *cio,
1489 opj_event_mgr_t * p_manager )
1491 /* 12 bytes will be read */
1492 unsigned char l_signature_data [12];
1497 assert(p_manager != 00);
1499 /* write box length */
1500 opj_write_bytes(l_signature_data,12,4);
1501 /* writes box type */
1502 opj_write_bytes(l_signature_data+4,JP2_JP,4);
1503 /* writes magic number*/
1504 opj_write_bytes(l_signature_data+8,0x0d0a870a,4);
1506 if (opj_stream_write_data(cio,l_signature_data,12,p_manager) != 12) {
1513 static int write_fidx( int offset_jp2c, int length_jp2c, int offset_idx, int length_idx, opj_cio_t *cio)
1517 lenp = cio_tell( cio);
1518 cio_skip( cio, 4); /* L [at the end] */
1519 cio_write( cio, JPIP_FIDX, 4); /* IPTR */
1521 write_prxy( offset_jp2c, length_jp2c, offset_idx, length_idx, cio);
1523 len = cio_tell( cio)-lenp;
1524 cio_seek( cio, lenp);
1525 cio_write( cio, len, 4); /* L */
1526 cio_seek( cio, lenp+len);
1531 static void write_prxy( int offset_jp2c, int length_jp2c, int offset_idx, int length_idx, opj_cio_t *cio)
1535 lenp = cio_tell( cio);
1536 cio_skip( cio, 4); /* L [at the end] */
1537 cio_write( cio, JPIP_PRXY, 4); /* IPTR */
1539 cio_write( cio, offset_jp2c, 8); /* OOFF */
1540 cio_write( cio, length_jp2c, 4); /* OBH part 1 */
1541 cio_write( cio, JP2_JP2C, 4); /* OBH part 2 */
1543 cio_write( cio, 1,1); /* NI */
1545 cio_write( cio, offset_idx, 8); /* IOFF */
1546 cio_write( cio, length_idx, 4); /* IBH part 1 */
1547 cio_write( cio, JPIP_CIDX, 4); /* IBH part 2 */
1549 len = cio_tell( cio)-lenp;
1550 cio_seek( cio, lenp);
1551 cio_write( cio, len, 4); /* L */
1552 cio_seek( cio, lenp+len);
1555 static void write_iptr( int offset, int length, opj_cio_t *cio)
1559 lenp = cio_tell( cio);
1560 cio_skip( cio, 4); /* L [at the end] */
1561 cio_write( cio, JPIP_IPTR, 4); /* IPTR */
1563 cio_write( cio, offset, 8);
1564 cio_write( cio, length, 8);
1566 len = cio_tell( cio)-lenp;
1567 cio_seek( cio, lenp);
1568 cio_write( cio, len, 4); /* L */
1569 cio_seek( cio, lenp+len);
1573 /* ----------------------------------------------------------------------- */
1574 /* JP2 decoder interface */
1575 /* ----------------------------------------------------------------------- */
1578 void opj_jp2_setup_decoder(opj_jp2_v2_t *jp2, opj_dparameters_t *parameters)
1580 /* setup the J2K codec */
1581 opj_j2k_setup_decoder(jp2->j2k, parameters);
1583 /* further JP2 initializations go here */
1584 jp2->color.jp2_has_colr = 0;
1585 jp2->ignore_pclr_cmap_cdef = parameters->flags & OPJ_DPARAMETERS_IGNORE_PCLR_CMAP_CDEF_FLAG;
1589 /* ----------------------------------------------------------------------- */
1590 /* JP2 encoder interface */
1591 /* ----------------------------------------------------------------------- */
1593 void opj_jp2_setup_encoder( opj_jp2_v2_t *jp2,
1594 opj_cparameters_t *parameters,
1596 opj_event_mgr_t * p_manager)
1601 if(!jp2 || !parameters || !image)
1604 /* setup the J2K codec */
1605 /* ------------------- */
1607 /* Check if number of components respects standard */
1608 if (image->numcomps < 1 || image->numcomps > 16384) {
1609 opj_event_msg_v2(p_manager, EVT_ERROR, "Invalid number of components specified while setting up JP2 encoder\n");
1613 j2k_setup_encoder_v2(jp2->j2k, parameters, image, p_manager );
1615 /* setup the JP2 codec */
1616 /* ------------------- */
1620 jp2->brand = JP2_JP2; /* BR */
1621 jp2->minversion = 0; /* MinV */
1623 jp2->cl = (unsigned int*) opj_malloc(jp2->numcl * sizeof(unsigned int));
1624 jp2->cl[0] = JP2_JP2; /* CL0 : JP2 */
1626 /* Image Header box */
1628 jp2->numcomps = image->numcomps; /* NC */
1629 jp2->comps = (opj_jp2_comps_t*) opj_malloc(jp2->numcomps * sizeof(opj_jp2_comps_t));
1630 jp2->h = image->y1 - image->y0; /* HEIGHT */
1631 jp2->w = image->x1 - image->x0; /* WIDTH */
1633 depth_0 = image->comps[0].prec - 1;
1634 sign = image->comps[0].sgnd;
1635 jp2->bpc = depth_0 + (sign << 7);
1636 for (i = 1; i < image->numcomps; i++) {
1637 int depth = image->comps[i].prec - 1;
1638 sign = image->comps[i].sgnd;
1639 if (depth_0 != depth)
1642 jp2->C = 7; /* C : Always 7 */
1643 jp2->UnkC = 0; /* UnkC, colorspace specified in colr box */
1644 jp2->IPR = 0; /* IPR, no intellectual property */
1646 /* BitsPerComponent box */
1647 for (i = 0; i < image->numcomps; i++) {
1648 jp2->comps[i].bpcc = image->comps[i].prec - 1 + (image->comps[i].sgnd << 7);
1651 /* Colour Specification box */
1652 if ((image->numcomps == 1 || image->numcomps == 3) && (jp2->bpc != 255)) {
1653 jp2->meth = 1; /* METH: Enumerated colourspace */
1655 jp2->meth = 2; /* METH: Restricted ICC profile */
1657 if (jp2->meth == 1) {
1658 if (image->color_space == 1)
1659 jp2->enumcs = 16; /* sRGB as defined by IEC 61966�2�1 */
1660 else if (image->color_space == 2)
1661 jp2->enumcs = 17; /* greyscale */
1662 else if (image->color_space == 3)
1663 jp2->enumcs = 18; /* YUV */
1665 jp2->enumcs = 0; /* PROFILE (??) */
1667 jp2->precedence = 0; /* PRECEDENCE */
1668 jp2->approx = 0; /* APPROX */
1670 // jp2->jpip_on = parameters->jpip_on;
1674 opj_bool opj_jp2_encode(opj_jp2_v2_t *jp2,
1675 opj_stream_private_t *stream,
1676 opj_event_mgr_t * p_manager)
1678 return j2k_encode_v2(jp2->j2k, stream, p_manager);
1683 * Ends the decompression procedures and possibiliy add data to be read after the
1686 opj_bool opj_jp2_end_decompress(opj_jp2_v2_t *jp2,
1687 opj_stream_private_t *cio,
1688 opj_event_mgr_t * p_manager
1694 assert(p_manager != 00);
1696 /* customization of the end encoding */
1697 opj_jp2_setup_end_header_reading(jp2);
1700 if (! opj_jp2_exec (jp2,jp2->m_procedure_list,cio,p_manager)) {
1704 return j2k_end_decompress(jp2->j2k, cio, p_manager);
1708 * Ends the compression procedures and possibility add data to be read after the
1711 opj_bool opj_jp2_end_compress( opj_jp2_v2_t *jp2,
1712 opj_stream_private_t *cio,
1713 opj_event_mgr_t * p_manager
1719 assert(p_manager != 00);
1721 /* customization of the end encoding */
1722 opj_jp2_setup_end_header_writting(jp2);
1724 if (! j2k_end_compress(jp2->j2k,cio,p_manager)) {
1729 return opj_jp2_exec(jp2,jp2->m_procedure_list,cio,p_manager);
1734 * Sets up the procedures to do on writing header after the codestream.
1735 * Developers wanting to extend the library can add their own writing procedures.
1737 void opj_jp2_setup_end_header_writting (opj_jp2_v2_t *jp2)
1742 opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)opj_jp2_write_jp2c );
1743 /* DEVELOPER CORNER, add your custom procedures */
1747 * Sets up the procedures to do on reading header after the codestream.
1748 * Developers wanting to extend the library can add their own writing procedures.
1750 void opj_jp2_setup_end_header_reading (opj_jp2_v2_t *jp2)
1754 opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)opj_jp2_read_header_procedure );
1755 /* DEVELOPER CORNER, add your custom procedures */
1759 * The default validation procedure without any extension.
1761 * @param jp2 the jpeg2000 codec to validate.
1762 * @param cio the input stream to validate.
1763 * @param p_manager the user event manager.
1765 * @return true if the parameters are correct.
1767 opj_bool opj_jp2_default_validation ( opj_jp2_v2_t * jp2,
1768 opj_stream_private_t *cio,
1769 opj_event_mgr_t * p_manager
1772 opj_bool l_is_valid = OPJ_TRUE;
1778 assert(p_manager != 00);
1780 /* JPEG2000 codec validation */
1783 /* STATE checking */
1784 /* make sure the state is at 0 */
1785 l_is_valid &= (jp2->jp2_state == JP2_STATE_NONE);
1787 /* make sure not reading a jp2h ???? WEIRD */
1788 l_is_valid &= (jp2->jp2_img_state == JP2_IMG_STATE_NONE);
1790 /* POINTER validation */
1791 /* make sure a j2k codec is present */
1792 l_is_valid &= (jp2->j2k != 00);
1794 /* make sure a procedure list is present */
1795 l_is_valid &= (jp2->m_procedure_list != 00);
1797 /* make sure a validation list is present */
1798 l_is_valid &= (jp2->m_validation_list != 00);
1800 /* PARAMETER VALIDATION */
1801 /* number of components */
1802 l_is_valid &= (jp2->numcl > 0);
1804 l_is_valid &= (jp2->h > 0);
1806 l_is_valid &= (jp2->w > 0);
1808 for (i = 0; i < jp2->numcomps; ++i) {
1809 l_is_valid &= (jp2->comps[i].bpcc > 0);
1813 l_is_valid &= ((jp2->meth > 0) && (jp2->meth < 3));
1815 /* stream validation */
1816 /* back and forth is needed */
1817 l_is_valid &= opj_stream_has_seek(cio);
1823 * Reads a jpeg2000 file header structure.
1825 * @param stream the stream to read data from.
1826 * @param jp2 the jpeg2000 file header structure.
1827 * @param p_manager the user event manager.
1829 * @return true if the box is valid.
1831 static opj_bool opj_jp2_read_header_procedure( opj_jp2_v2_t *jp2,
1832 opj_stream_private_t *stream,
1833 opj_event_mgr_t * p_manager
1837 OPJ_UINT32 l_nb_bytes_read;
1838 const opj_jp2_header_handler_t * l_current_handler;
1839 OPJ_UINT32 l_last_data_size = BOX_SIZE;
1840 OPJ_UINT32 l_current_data_size;
1841 unsigned char * l_current_data = 00;
1844 assert(stream != 00);
1846 assert(p_manager != 00);
1848 l_current_data = (unsigned char*)opj_malloc(l_last_data_size);
1850 if (l_current_data == 00) {
1851 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to handle jpeg2000 file header\n");
1854 memset(l_current_data, 0 , l_last_data_size);
1856 while (opj_jp2_read_boxhdr(&box,&l_nb_bytes_read,stream,p_manager)) {
1857 /* is it the codestream box ? */
1858 if (box.type == JP2_JP2C) {
1859 if (jp2->jp2_state & JP2_STATE_HEADER) {
1860 jp2->jp2_state |= JP2_STATE_CODESTREAM;
1861 opj_free(l_current_data);
1865 opj_event_msg_v2(p_manager, EVT_ERROR, "bad placed jpeg codestream\n");
1866 opj_free(l_current_data);
1870 else if (box.length == 0) {
1871 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot handle box of undefined sizes\n");
1872 opj_free(l_current_data);
1876 l_current_handler = opj_jp2_find_handler(box.type);
1877 l_current_data_size = box.length - l_nb_bytes_read;
1879 if (l_current_handler != 00) {
1880 if (l_current_data_size > l_last_data_size) {
1881 l_current_data = (unsigned char*)opj_realloc(l_current_data,l_current_data_size);
1882 if (!l_current_data){
1883 opj_free(l_current_data);
1886 l_last_data_size = l_current_data_size;
1889 l_nb_bytes_read = opj_stream_read_data(stream,l_current_data,l_current_data_size,p_manager);
1890 if (l_nb_bytes_read != l_current_data_size) {
1891 opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with reading JPEG2000 box, stream error\n");
1895 if (! l_current_handler->handler(jp2,l_current_data,l_current_data_size,p_manager)) {
1896 opj_free(l_current_data);
1901 jp2->jp2_state |= JP2_STATE_UNKNOWN;
1902 if (opj_stream_skip(stream,l_current_data_size,p_manager) != l_current_data_size) {
1903 opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with skipping JPEG2000 box, stream error\n");
1904 opj_free(l_current_data);
1910 opj_free(l_current_data);
1916 * Excutes the given procedures on the given codec.
1918 * @param p_procedure_list the list of procedures to execute
1919 * @param jp2 the jpeg2000 file codec to execute the procedures on.
1920 * @param stream the stream to execute the procedures on.
1921 * @param p_manager the user manager.
1923 * @return true if all the procedures were successfully executed.
1925 static opj_bool opj_jp2_exec ( opj_jp2_v2_t * jp2,
1926 opj_procedure_list_t * p_procedure_list,
1927 opj_stream_private_t *stream,
1928 opj_event_mgr_t * p_manager
1932 opj_bool (** l_procedure) (opj_jp2_v2_t * jp2, opj_stream_private_t *, opj_event_mgr_t *) = 00;
1933 opj_bool l_result = OPJ_TRUE;
1934 OPJ_UINT32 l_nb_proc, i;
1937 assert(p_procedure_list != 00);
1939 assert(stream != 00);
1940 assert(p_manager != 00);
1942 l_nb_proc = opj_procedure_list_get_nb_procedures(p_procedure_list);
1943 l_procedure = (opj_bool (**) (opj_jp2_v2_t * jp2, opj_stream_private_t *, opj_event_mgr_t *)) opj_procedure_list_get_first_procedure(p_procedure_list);
1945 for (i=0;i<l_nb_proc;++i) {
1946 l_result = l_result && (*l_procedure) (jp2,stream,p_manager);
1950 /* and clear the procedure list at the end. */
1951 opj_procedure_list_clear(p_procedure_list);
1956 * Starts a compression scheme, i.e. validates the codec parameters, writes the header.
1958 * @param jp2 the jpeg2000 file codec.
1959 * @param cio the stream object.
1961 * @return true if the codec is valid.
1963 opj_bool opj_jp2_start_compress(opj_jp2_v2_t *jp2,
1964 opj_stream_private_t *stream,
1965 opj_image_t * p_image,
1966 opj_event_mgr_t * p_manager
1971 assert(stream != 00);
1972 assert(p_manager != 00);
1974 /* customization of the validation */
1975 opj_jp2_setup_encoding_validation (jp2);
1977 /* validation of the parameters codec */
1978 if (! opj_jp2_exec(jp2,jp2->m_validation_list,stream,p_manager)) {
1982 /* customization of the encoding */
1983 opj_jp2_setup_header_writting(jp2);
1986 if (! opj_jp2_exec (jp2,jp2->m_procedure_list,stream,p_manager)) {
1990 return j2k_start_compress(jp2->j2k,stream,p_image,p_manager);
1994 * Finds the execution function related to the given box id.
1996 * @param p_id the id of the handler to fetch.
1998 * @return the given handler or 00 if it could not be found.
2000 const opj_jp2_header_handler_t * opj_jp2_find_handler (OPJ_UINT32 p_id)
2002 OPJ_UINT32 i, l_handler_size = sizeof(jp2_header) / sizeof(opj_jp2_header_handler_t);
2004 for (i=0;i<l_handler_size;++i) {
2005 if (jp2_header[i].id == p_id) {
2006 return &jp2_header[i];
2013 * Finds the image execution function related to the given box id.
2015 * @param p_id the id of the handler to fetch.
2017 * @return the given handler or 00 if it could not be found.
2019 static const opj_jp2_header_handler_t * opj_jp2_img_find_handler (OPJ_UINT32 p_id)
2021 OPJ_UINT32 i, l_handler_size = sizeof(jp2_img_header) / sizeof(opj_jp2_header_handler_t);
2022 for (i=0;i<l_handler_size;++i)
2024 if (jp2_img_header[i].id == p_id) {
2025 return &jp2_img_header[i];
2034 * Reads a jpeg2000 file signature box.
2036 * @param p_header_data the data contained in the signature box.
2037 * @param jp2 the jpeg2000 file codec.
2038 * @param p_header_size the size of the data contained in the signature box.
2039 * @param p_manager the user event manager.
2041 * @return true if the file signature box is valid.
2043 static opj_bool opj_jp2_read_jp(opj_jp2_v2_t *jp2,
2044 OPJ_BYTE * p_header_data,
2045 OPJ_UINT32 p_header_size,
2046 opj_event_mgr_t * p_manager
2050 unsigned int l_magic_number;
2053 assert(p_header_data != 00);
2055 assert(p_manager != 00);
2057 if (jp2->jp2_state != JP2_STATE_NONE) {
2058 opj_event_msg_v2(p_manager, EVT_ERROR, "The signature box must be the first box in the file.\n");
2062 /* assure length of data is correct (4 -> magic number) */
2063 if (p_header_size != 4) {
2064 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with JP signature Box size\n");
2068 /* rearrange data */
2069 opj_read_bytes(p_header_data,&l_magic_number,4);
2070 if (l_magic_number != 0x0d0a870a ) {
2071 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with JP Signature : bad magic number\n");
2075 jp2->jp2_state |= JP2_STATE_SIGNATURE;
2082 * Reads a a FTYP box - File type box
2084 * @param p_header_data the data contained in the FTYP box.
2085 * @param jp2 the jpeg2000 file codec.
2086 * @param p_header_size the size of the data contained in the FTYP box.
2087 * @param p_manager the user event manager.
2089 * @return true if the FTYP box is valid.
2091 static opj_bool opj_jp2_read_ftyp( opj_jp2_v2_t *jp2,
2092 OPJ_BYTE * p_header_data,
2093 OPJ_UINT32 p_header_size,
2094 opj_event_mgr_t * p_manager
2097 OPJ_UINT32 i, l_remaining_bytes;
2100 assert(p_header_data != 00);
2102 assert(p_manager != 00);
2104 if (jp2->jp2_state != JP2_STATE_SIGNATURE) {
2105 opj_event_msg_v2(p_manager, EVT_ERROR, "The ftyp box must be the second box in the file.\n");
2109 /* assure length of data is correct */
2110 if (p_header_size < 8) {
2111 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with FTYP signature Box size\n");
2115 opj_read_bytes(p_header_data,&jp2->brand,4); /* BR */
2118 opj_read_bytes(p_header_data,&jp2->minversion,4); /* MinV */
2121 l_remaining_bytes = p_header_size - 8;
2123 /* the number of remaining bytes should be a multiple of 4 */
2124 if ((l_remaining_bytes & 0x3) != 0) {
2125 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with FTYP signature Box size\n");
2130 jp2->numcl = l_remaining_bytes >> 2;
2132 jp2->cl = (unsigned int *) opj_malloc(jp2->numcl * sizeof(unsigned int));
2133 if (jp2->cl == 00) {
2134 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory with FTYP Box\n");
2137 memset(jp2->cl,0,jp2->numcl * sizeof(unsigned int));
2140 for (i = 0; i < jp2->numcl; ++i)
2142 opj_read_bytes(p_header_data,&jp2->cl[i],4); /* CLi */
2146 jp2->jp2_state |= JP2_STATE_FILE_TYPE;
2152 * Skips the Jpeg2000 Codestream Header box - JP2C Header box.
2154 * @param cio the stream to write data to.
2155 * @param jp2 the jpeg2000 file codec.
2156 * @param p_manager user event manager.
2158 * @return true if writting was successful.
2160 opj_bool opj_jp2_skip_jp2c( opj_jp2_v2_t *jp2,
2161 opj_stream_private_t *stream,
2162 opj_event_mgr_t * p_manager )
2166 assert(stream != 00);
2167 assert(p_manager != 00);
2169 jp2->j2k_codestream_offset = opj_stream_tell(stream);
2171 if (opj_stream_skip(stream,8,p_manager) != 8) {
2179 * Reads the Jpeg2000 file Header box - JP2 Header box (warning, this is a super box).
2181 * @param p_header_data the data contained in the file header box.
2182 * @param jp2 the jpeg2000 file codec.
2183 * @param p_header_size the size of the data contained in the file header box.
2184 * @param p_manager the user event manager.
2186 * @return true if the JP2 Header box was successfully reconized.
2188 static opj_bool opj_jp2_read_jp2h( opj_jp2_v2_t *jp2,
2189 OPJ_BYTE *p_header_data,
2190 OPJ_UINT32 p_header_size,
2191 opj_event_mgr_t * p_manager
2194 OPJ_UINT32 l_box_size=0, l_current_data_size = 0;
2196 const opj_jp2_header_handler_t * l_current_handler;
2199 assert(p_header_data != 00);
2201 assert(p_manager != 00);
2203 /* make sure the box is well placed */
2204 if ((jp2->jp2_state & JP2_STATE_FILE_TYPE) != JP2_STATE_FILE_TYPE ) {
2205 opj_event_msg_v2(p_manager, EVT_ERROR, "The box must be the first box in the file.\n");
2209 jp2->jp2_img_state = JP2_IMG_STATE_NONE;
2211 /* iterate while remaining data */
2212 while (p_header_size > 0) {
2214 if (! opj_jp2_read_boxhdr_char(&box,p_header_data,&l_box_size,p_header_size, p_manager)) {
2215 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream error while reading JP2 Header box\n");
2219 if (box.length > p_header_size) {
2220 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream error while reading JP2 Header box: box length is inconsistent.\n");
2224 l_current_handler = opj_jp2_img_find_handler(box.type);
2225 l_current_data_size = box.length - l_box_size;
2226 p_header_data += l_box_size;
2228 if (l_current_handler != 00) {
2229 if (! l_current_handler->handler(jp2,p_header_data,l_current_data_size,p_manager)) {
2234 jp2->jp2_img_state |= JP2_IMG_STATE_UNKNOWN;
2237 p_header_data += l_current_data_size;
2238 p_header_size -= box.length;
2241 jp2->jp2_state |= JP2_STATE_HEADER;
2247 * Reads a box header. The box is the way data is packed inside a jpeg2000 file structure. Data is read from a character string
2249 * @param p_data the character string to read data from.
2250 * @param box the box structure to fill.
2251 * @param p_number_bytes_read pointer to an int that will store the number of bytes read from the stream (shoul usually be 2).
2252 * @param p_box_max_size the maximum number of bytes in the box.
2254 * @return true if the box is reconized, false otherwise
2256 opj_bool opj_jp2_read_boxhdr_char( opj_jp2_box_t *box,
2258 OPJ_UINT32 * p_number_bytes_read,
2259 OPJ_UINT32 p_box_max_size,
2260 opj_event_mgr_t * p_manager
2266 assert(p_data != 00);
2268 assert(p_number_bytes_read != 00);
2269 assert(p_manager != 00);
2271 if (p_box_max_size < 8) {
2272 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot handle box of less than 8 bytes\n");
2276 /* process read data */
2277 opj_read_bytes(p_data, &l_value, 4);
2279 box->length = (OPJ_INT32)(l_value);
2281 opj_read_bytes(p_data, &l_value, 4);
2283 box->type = (OPJ_INT32)(l_value);
2285 *p_number_bytes_read = 8;
2287 /* do we have a "special very large box ?" */
2288 /* read then the XLBox */
2289 if (box->length == 1) {
2290 unsigned int l_xl_part_size;
2292 if (p_box_max_size < 16) {
2293 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot handle XL box of less than 16 bytes\n");
2297 opj_read_bytes(p_data,&l_xl_part_size, 4);
2299 *p_number_bytes_read += 4;
2301 if (l_xl_part_size != 0) {
2302 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot handle box sizes higher than 2^32\n");
2306 opj_read_bytes(p_data, &l_value, 4);
2307 *p_number_bytes_read += 4;
2308 box->length = (OPJ_INT32)(l_value);
2310 if (box->length == 0) {
2311 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot handle box of undefined sizes\n");
2315 else if (box->length == 0) {
2316 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot handle box of undefined sizes\n");
2325 * Reads a jpeg2000 file header structure.
2327 * @param cio the stream to read data from.
2328 * @param jp2 the jpeg2000 file header structure.
2329 * @param p_manager the user event manager.
2331 * @return true if the box is valid.
2333 opj_bool opj_jp2_read_header( opj_stream_private_t *p_stream,
2335 opj_image_t ** p_image,
2336 opj_event_mgr_t * p_manager
2341 assert(p_stream != 00);
2342 assert(p_manager != 00);
2344 /* customization of the validation */
2345 opj_jp2_setup_decoding_validation (jp2);
2347 /* customization of the encoding */
2348 opj_jp2_setup_header_reading(jp2);
2350 /* validation of the parameters codec */
2351 if (! opj_jp2_exec(jp2,jp2->m_validation_list,p_stream,p_manager)) {
2356 if (! opj_jp2_exec (jp2,jp2->m_procedure_list,p_stream,p_manager)) {
2360 return j2k_read_header( p_stream,
2367 * Sets up the validation ,i.e. adds the procedures to launch to make sure the codec parameters
2368 * are valid. Developers wanting to extend the library can add their own validation procedures.
2370 void opj_jp2_setup_encoding_validation (opj_jp2_v2_t *jp2)
2375 opj_procedure_list_add_procedure(jp2->m_validation_list, (opj_procedure)opj_jp2_default_validation);
2376 /* DEVELOPER CORNER, add your custom validation procedure */
2380 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
2381 * are valid. Developpers wanting to extend the library can add their own validation procedures.
2383 void opj_jp2_setup_decoding_validation (opj_jp2_v2_t *jp2)
2387 /* DEVELOPER CORNER, add your custom validation procedure */
2391 * Sets up the procedures to do on writting header.
2392 * Developers wanting to extend the library can add their own writing procedures.
2394 void opj_jp2_setup_header_writting (opj_jp2_v2_t *jp2)
2399 opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)opj_jp2_write_jp );
2400 opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)opj_jp2_write_ftyp );
2401 opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)opj_jp2_write_jp2h );
2402 opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)opj_jp2_skip_jp2c );
2404 /* DEVELOPER CORNER, insert your custom procedures */
2409 * Sets up the procedures to do on reading header.
2410 * Developpers wanting to extend the library can add their own writting procedures.
2412 void opj_jp2_setup_header_reading (opj_jp2_v2_t *jp2)
2417 opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)opj_jp2_read_header_procedure );
2418 /* DEVELOPER CORNER, add your custom procedures */
2423 * Reads a tile header.
2424 * @param p_j2k the jpeg2000 codec.
2425 * @param p_stream the stream to write data to.
2426 * @param p_manager the user event manager.
2428 opj_bool opj_jp2_read_tile_header ( opj_jp2_v2_t * p_jp2,
2429 OPJ_UINT32 * p_tile_index,
2430 OPJ_UINT32 * p_data_size,
2431 OPJ_INT32 * p_tile_x0,
2432 OPJ_INT32 * p_tile_y0,
2433 OPJ_INT32 * p_tile_x1,
2434 OPJ_INT32 * p_tile_y1,
2435 OPJ_UINT32 * p_nb_comps,
2437 opj_stream_private_t *p_stream,
2438 opj_event_mgr_t * p_manager
2441 return j2k_read_tile_header(p_jp2->j2k,
2444 p_tile_x0, p_tile_y0,
2445 p_tile_x1, p_tile_y1,
2454 * @param p_j2k the jpeg2000 codec.
2455 * @param p_stream the stream to write data to.
2456 * @param p_manager the user event manager.
2458 opj_bool opj_jp2_write_tile ( opj_jp2_v2_t *p_jp2,
2459 OPJ_UINT32 p_tile_index,
2461 OPJ_UINT32 p_data_size,
2462 opj_stream_private_t *p_stream,
2463 opj_event_mgr_t * p_manager
2467 return j2k_write_tile (p_jp2->j2k,p_tile_index,p_data,p_data_size,p_stream,p_manager);
2472 * @param p_j2k the jpeg2000 codec.
2473 * @param p_stream the stream to write data to.
2474 * @param p_manager the user event manager.
2476 opj_bool opj_jp2_decode_tile ( opj_jp2_v2_t * p_jp2,
2477 OPJ_UINT32 p_tile_index,
2479 OPJ_UINT32 p_data_size,
2480 opj_stream_private_t *p_stream,
2481 opj_event_mgr_t * p_manager
2484 return j2k_decode_tile (p_jp2->j2k,p_tile_index,p_data,p_data_size,p_stream,p_manager);
2488 * Destroys a jpeg2000 file decompressor.
2490 * @param jp2 a jpeg2000 file decompressor.
2492 void opj_jp2_destroy(opj_jp2_v2_t *jp2)
2495 /* destroy the J2K codec */
2496 j2k_destroy(jp2->j2k);
2500 opj_free(jp2->comps);
2509 if (jp2->color.icc_profile_buf) {
2510 opj_free(jp2->color.icc_profile_buf);
2511 jp2->color.icc_profile_buf = 00;
2514 if (jp2->color.jp2_cdef) {
2515 if (jp2->color.jp2_cdef->info) {
2516 opj_free(jp2->color.jp2_cdef->info);
2517 jp2->color.jp2_cdef->info = NULL;
2520 opj_free(jp2->color.jp2_cdef);
2521 jp2->color.jp2_cdef = 00;
2524 if (jp2->color.jp2_pclr) {
2525 if (jp2->color.jp2_pclr->cmap) {
2526 opj_free(jp2->color.jp2_pclr->cmap);
2527 jp2->color.jp2_pclr->cmap = NULL;
2529 if (jp2->color.jp2_pclr->channel_sign) {
2530 opj_free(jp2->color.jp2_pclr->channel_sign);
2531 jp2->color.jp2_pclr->channel_sign = NULL;
2533 if (jp2->color.jp2_pclr->channel_size) {
2534 opj_free(jp2->color.jp2_pclr->channel_size);
2535 jp2->color.jp2_pclr->channel_size = NULL;
2537 if (jp2->color.jp2_pclr->entries) {
2538 opj_free(jp2->color.jp2_pclr->entries);
2539 jp2->color.jp2_pclr->entries = NULL;
2542 opj_free(jp2->color.jp2_pclr);
2543 jp2->color.jp2_pclr = 00;
2546 if (jp2->m_validation_list) {
2547 opj_procedure_list_destroy(jp2->m_validation_list);
2548 jp2->m_validation_list = 00;
2551 if (jp2->m_procedure_list) {
2552 opj_procedure_list_destroy(jp2->m_procedure_list);
2553 jp2->m_procedure_list = 00;
2561 * Sets the given area to be decoded. This function should be called right after opj_read_header and before any tile header reading.
2563 * @param p_jp2 the jpeg2000 codec.
2564 * @param p_end_x the right position of the rectangle to decode (in image coordinates).
2565 * @param p_start_y the up position of the rectangle to decode (in image coordinates).
2566 * @param p_end_y the bottom position of the rectangle to decode (in image coordinates).
2567 * @param p_manager the user event manager
2569 * @return true if the area could be set.
2571 opj_bool opj_jp2_set_decode_area( opj_jp2_v2_t *p_jp2,
2572 opj_image_t* p_image,
2573 OPJ_INT32 p_start_x, OPJ_INT32 p_start_y,
2574 OPJ_INT32 p_end_x, OPJ_INT32 p_end_y,
2575 opj_event_mgr_t * p_manager
2578 return j2k_set_decode_area(p_jp2->j2k, p_image, p_start_x, p_start_y, p_end_x, p_end_y, p_manager);
2582 * Get the decoded tile.
2584 * @param jp2 the jpeg2000 codec.
2585 * @param p_stream input_stream
2586 * @param p_image output image. .
2587 * @param p_manager the user event manager
2588 * @param tile_index index of the tile we want decode
2590 * @return true if succeed.
2592 opj_bool opj_jp2_get_tile( opj_jp2_v2_t *p_jp2,
2593 opj_stream_private_t *p_stream,
2594 opj_image_t* p_image,
2595 opj_event_mgr_t * p_manager,
2596 OPJ_UINT32 tile_index
2602 opj_event_msg_v2(p_manager, EVT_WARNING, "JP2 box which are after the codestream will not be read by this function.\n");
2604 if (! j2k_get_tile(p_jp2->j2k, p_stream, p_image, p_manager, tile_index) ){
2605 opj_event_msg_v2(p_manager, EVT_ERROR, "Failed to decode the codestream in the JP2 file\n");
2609 /* Set Image Color Space */
2610 if (p_jp2->enumcs == 16)
2611 p_image->color_space = CLRSPC_SRGB;
2612 else if (p_jp2->enumcs == 17)
2613 p_image->color_space = CLRSPC_GRAY;
2614 else if (p_jp2->enumcs == 18)
2615 p_image->color_space = CLRSPC_SYCC;
2617 p_image->color_space = CLRSPC_UNKNOWN;
2619 /* Apply the color space if needed */
2620 if(p_jp2->color.jp2_cdef) {
2621 jp2_apply_cdef(p_image, &(p_jp2->color));
2624 if(p_jp2->color.jp2_pclr) {
2625 /* Part 1, I.5.3.4: Either both or none : */
2626 if( !p_jp2->color.jp2_pclr->cmap)
2627 jp2_free_pclr(&(p_jp2->color));
2629 jp2_apply_pclr(p_image, &(p_jp2->color));
2632 if(p_jp2->color.icc_profile_buf) {
2633 p_image->icc_profile_buf = p_jp2->color.icc_profile_buf;
2634 p_image->icc_profile_len = p_jp2->color.icc_profile_len;
2635 p_jp2->color.icc_profile_buf = NULL;
2643 /* ----------------------------------------------------------------------- */
2644 /* JP2 encoder interface */
2645 /* ----------------------------------------------------------------------- */
2647 opj_jp2_v2_t* opj_jp2_create(opj_bool p_is_decoder)
2649 opj_jp2_v2_t *jp2 = (opj_jp2_v2_t*)opj_malloc(sizeof(opj_jp2_v2_t));
2651 memset(jp2,0,sizeof(opj_jp2_v2_t));
2653 /* create the J2K codec */
2654 if (! p_is_decoder) {
2655 jp2->j2k = j2k_create_compress_v2();
2658 jp2->j2k = opj_j2k_create_decompress();
2661 if (jp2->j2k == 00) {
2662 opj_jp2_destroy(jp2);
2666 /* Color structure */
2667 jp2->color.icc_profile_buf = NULL;
2668 jp2->color.icc_profile_len = 0;
2669 jp2->color.jp2_cdef = NULL;
2670 jp2->color.jp2_pclr = NULL;
2671 jp2->color.jp2_has_colr = 0;
2673 /* validation list creation */
2674 jp2->m_validation_list = opj_procedure_list_create();
2675 if (! jp2->m_validation_list) {
2676 opj_jp2_destroy(jp2);
2680 /* execution list creation */
2681 jp2->m_procedure_list = opj_procedure_list_create();
2682 if (! jp2->m_procedure_list) {
2683 opj_jp2_destroy(jp2);
2691 void jp2_dump(opj_jp2_v2_t* p_jp2, OPJ_INT32 flag, FILE* out_stream)
2694 assert(p_jp2 != 00);
2696 j2k_dump(p_jp2->j2k,
2701 opj_codestream_index_t* jp2_get_cstr_index(opj_jp2_v2_t* p_jp2)
2703 return j2k_get_cstr_index(p_jp2->j2k);
2706 opj_codestream_info_v2_t* jp2_get_cstr_info(opj_jp2_v2_t* p_jp2)
2708 return j2k_get_cstr_info(p_jp2->j2k);
2711 opj_bool opj_jp2_set_decoded_resolution_factor(opj_jp2_v2_t *p_jp2,
2712 OPJ_UINT32 res_factor,
2713 opj_event_mgr_t * p_manager)
2715 return j2k_set_decoded_resolution_factor(p_jp2->j2k, res_factor, p_manager);