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 static void opj_jp2_apply_cdef(opj_image_t *image, opj_jp2_color_t *color);
105 * Writes the Colour Specification box.
107 * @param jp2 jpeg2000 file codec.
108 * @param p_nb_bytes_written pointer to store the nb of bytes written by the function.
110 * @return the data being copied.
112 static OPJ_BYTE * opj_jp2_write_colr( opj_jp2_v2_t *jp2,
113 OPJ_UINT32 * p_nb_bytes_written );
116 * Writes a FTYP box - File type box
118 * @param cio the stream to write data to.
119 * @param jp2 the jpeg2000 file codec.
120 * @param p_manager the user event manager.
122 * @return true if writting was successful.
124 static opj_bool opj_jp2_write_ftyp( opj_jp2_v2_t *jp2,
125 opj_stream_private_t *cio,
126 opj_event_mgr_t * p_manager );
129 * Reads a a FTYP box - File type box
131 * @param p_header_data the data contained in the FTYP box.
132 * @param jp2 the jpeg2000 file codec.
133 * @param p_header_size the size of the data contained in the FTYP box.
134 * @param p_manager the user event manager.
136 * @return true if the FTYP box is valid.
138 static opj_bool opj_jp2_read_ftyp( opj_jp2_v2_t *jp2,
139 OPJ_BYTE * p_header_data,
140 OPJ_UINT32 p_header_size,
141 opj_event_mgr_t * p_manager );
144 * Skips the Jpeg2000 Codestream Header box - JP2C Header box.
146 * @param cio the stream to write data to.
147 * @param jp2 the jpeg2000 file codec.
148 * @param p_manager user event manager.
150 * @return true if writting was successful.
152 opj_bool opj_jp2_skip_jp2c( opj_jp2_v2_t *jp2,
153 opj_stream_private_t *cio,
154 opj_event_mgr_t * p_manager );
157 * Reads the Jpeg2000 file Header box - JP2 Header box (warning, this is a super box).
159 * @param p_header_data the data contained in the file header box.
160 * @param jp2 the jpeg2000 file codec.
161 * @param p_header_size the size of the data contained in the file header box.
162 * @param p_manager the user event manager.
164 * @return true if the JP2 Header box was successfully reconized.
166 static opj_bool opj_jp2_read_jp2h( opj_jp2_v2_t *jp2,
167 OPJ_BYTE *p_header_data,
168 OPJ_UINT32 p_header_size,
169 opj_event_mgr_t * p_manager );
172 * Writes the Jpeg2000 codestream Header box - JP2C Header box. This function must be called AFTER the coding has been done.
174 * @param cio the stream to write data to.
175 * @param jp2 the jpeg2000 file codec.
176 * @param p_manager user event manager.
178 * @return true if writting was successful.
180 static opj_bool opj_jp2_write_jp2c( opj_jp2_v2_t *jp2,
181 opj_stream_private_t *cio,
182 opj_event_mgr_t * p_manager );
185 * Reads a jpeg2000 file signature box.
187 * @param p_header_data the data contained in the signature box.
188 * @param jp2 the jpeg2000 file codec.
189 * @param p_header_size the size of the data contained in the signature box.
190 * @param p_manager the user event manager.
192 * @return true if the file signature box is valid.
194 static opj_bool opj_jp2_read_jp(opj_jp2_v2_t *jp2,
195 OPJ_BYTE * p_header_data,
196 OPJ_UINT32 p_header_size,
197 opj_event_mgr_t * p_manager);
200 * Writes a jpeg2000 file signature box.
202 * @param cio the stream to write data to.
203 * @param jp2 the jpeg2000 file codec.
204 * @param p_manager the user event manager.
206 * @return true if writting was successful.
208 static opj_bool opj_jp2_write_jp( opj_jp2_v2_t *jp2,
209 opj_stream_private_t *cio,
210 opj_event_mgr_t * p_manager );
213 Apply collected palette data
214 @param color Collector for profile, cdef and pclr data
217 static void opj_jp2_apply_pclr(opj_image_t *image, opj_jp2_color_t *color);
219 static void opj_jp2_free_pclr(opj_jp2_color_t *color);
223 @param jp2 JP2 handle
224 @param cio Input buffer stream
226 @param color Collector for profile, cdef and pclr data
227 @return Returns true if successful, returns false otherwise
229 static opj_bool opj_jp2_read_pclr( opj_jp2_v2_t *jp2,
230 OPJ_BYTE * p_pclr_header_data,
231 OPJ_UINT32 p_pclr_header_size,
232 opj_event_mgr_t * p_manager );
235 Collect component mapping data
236 @param jp2 JP2 handle
237 @param cio Input buffer stream
239 @param color Collector for profile, cdef and pclr data
240 @return Returns true if successful, returns false otherwise
243 static opj_bool opj_jp2_read_cmap( opj_jp2_v2_t * jp2,
244 OPJ_BYTE * p_cmap_header_data,
245 OPJ_UINT32 p_cmap_header_size,
246 opj_event_mgr_t * p_manager );
249 * Reads the Color Specification box.
251 * @param p_colr_header_data pointer to actual data (already read from file)
252 * @param jp2 the jpeg2000 file codec.
253 * @param p_colr_header_size the size of the color header
254 * @param p_manager the user event manager.
256 * @return true if the bpc header is valid, fale else.
258 static opj_bool opj_jp2_read_colr( opj_jp2_v2_t *jp2,
259 OPJ_BYTE * p_colr_header_data,
260 OPJ_UINT32 p_colr_header_size,
261 opj_event_mgr_t * p_manager );
264 Write file Index (superbox)
265 @param[in] offset_jp2c offset of jp2c box
266 @param[in] length_jp2c length of jp2c box
267 @param[in] offset_idx offset of cidx box
268 @param[in] length_idx length of cidx box
269 @param[in] cio file output handle
270 @return length of fidx box
272 static int write_fidx( int offset_jp2c, int length_jp2c, int offset_idx, int length_idx, opj_cio_t *cio);
274 Write index Finder box
275 @param[in] offset offset of fidx box
276 @param[in] length length of fidx box
277 @param[in] cio file output handle
279 static void write_iptr( int offset, int length, opj_cio_t *cio);
283 @param[in] offset_jp2c offset of jp2c box
284 @param[in] length_jp2c length of jp2c box
285 @param[in] offset_idx offset of cidx box
286 @param[in] length_idx length of cidx box
287 @param[in] cio file output handle
289 static void write_prxy( int offset_jp2c, int length_jp2c, int offset_idx, int length_idx, opj_cio_t *cio);
295 * Sets up the procedures to do on writting header after the codestream.
296 * Developpers wanting to extend the library can add their own writting procedures.
298 static void opj_jp2_setup_end_header_writting (opj_jp2_v2_t *jp2);
302 * Sets up the procedures to do on reading header after the codestream.
303 * Developpers wanting to extend the library can add their own writting procedures.
305 static void opj_jp2_setup_end_header_reading (opj_jp2_v2_t *jp2);
308 * Reads a jpeg2000 file header structure.
310 * @param cio the stream to read data from.
311 * @param jp2 the jpeg2000 file header structure.
312 * @param p_manager the user event manager.
314 * @return true if the box is valid.
316 static opj_bool opj_jp2_read_header_procedure( opj_jp2_v2_t *jp2,
317 opj_stream_private_t *stream,
318 opj_event_mgr_t * p_manager );
321 * Excutes the given procedures on the given codec.
323 * @param p_procedure_list the list of procedures to execute
324 * @param jp2 the jpeg2000 file codec to execute the procedures on.
325 * @param stream the stream to execute the procedures on.
326 * @param p_manager the user manager.
328 * @return true if all the procedures were successfully executed.
330 static opj_bool opj_jp2_exec ( opj_jp2_v2_t * jp2,
331 opj_procedure_list_t * p_procedure_list,
332 opj_stream_private_t *stream,
333 opj_event_mgr_t * p_manager );
336 * Reads a box header. The box is the way data is packed inside a jpeg2000 file structure.
338 * @param cio the input stream to read data from.
339 * @param box the box structure to fill.
340 * @param p_number_bytes_read pointer to an int that will store the number of bytes read from the stream (shoul usually be 2).
341 * @param p_manager user event manager.
343 * @return true if the box is reconized, false otherwise
345 static opj_bool opj_jp2_read_boxhdr(opj_jp2_box_t *box,
346 OPJ_UINT32 * p_number_bytes_read,
347 opj_stream_private_t *cio,
348 opj_event_mgr_t * p_manager);
351 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
352 * are valid. Developpers wanting to extend the library can add their own validation procedures.
354 static void opj_jp2_setup_encoding_validation (opj_jp2_v2_t *jp2);
358 * Sets up the procedures to do on writting header. Developpers wanting to extend the library can add their own writting procedures.
360 static void opj_jp2_setup_header_writting (opj_jp2_v2_t *jp2);
363 * The default validation procedure without any extension.
365 * @param jp2 the jpeg2000 codec to validate.
366 * @param cio the input stream to validate.
367 * @param p_manager the user event manager.
369 * @return true if the parameters are correct.
371 opj_bool opj_jp2_default_validation ( opj_jp2_v2_t * jp2,
372 opj_stream_private_t *cio,
373 opj_event_mgr_t * p_manager );
376 * Finds the image execution function related to the given box id.
378 * @param p_id the id of the handler to fetch.
380 * @return the given handler or NULL if it could not be found.
382 static const opj_jp2_header_handler_t * opj_jp2_img_find_handler (OPJ_UINT32 p_id);
385 * Finds the execution function related to the given box id.
387 * @param p_id the id of the handler to fetch.
389 * @return the given handler or NULL if it could not be found.
391 static const opj_jp2_header_handler_t * opj_jp2_find_handler (OPJ_UINT32 p_id );
393 const opj_jp2_header_handler_t jp2_header [] =
395 {JP2_JP,opj_jp2_read_jp},
396 {JP2_FTYP,opj_jp2_read_ftyp},
397 {JP2_JP2H,opj_jp2_read_jp2h}
400 const opj_jp2_header_handler_t jp2_img_header [] =
402 {JP2_IHDR,opj_jp2_read_ihdr},
403 {JP2_COLR,opj_jp2_read_colr},
404 {JP2_BPCC,opj_jp2_read_bpcc},
405 {JP2_PCLR,opj_jp2_read_pclr},
406 {JP2_CMAP,opj_jp2_read_cmap},
407 {JP2_CDEF,opj_jp2_read_cdef}
412 * Reads a box header. The box is the way data is packed inside a jpeg2000 file structure. Data is read from a character string
414 * @param p_data the character string to read data from.
415 * @param box the box structure to fill.
416 * @param p_number_bytes_read pointer to an int that will store the number of bytes read from the stream (shoul usually be 2).
417 * @param p_box_max_size the maximum number of bytes in the box.
419 * @return true if the box is reconized, false otherwise
421 static opj_bool opj_jp2_read_boxhdr_char( opj_jp2_box_t *box,
423 OPJ_UINT32 * p_number_bytes_read,
424 OPJ_UINT32 p_box_max_size,
425 opj_event_mgr_t * p_manager );
428 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
429 * are valid. Developpers wanting to extend the library can add their own validation procedures.
431 static void opj_jp2_setup_decoding_validation (opj_jp2_v2_t *jp2);
434 * Sets up the procedures to do on reading header.
435 * Developpers wanting to extend the library can add their own writting procedures.
437 static void opj_jp2_setup_header_reading (opj_jp2_v2_t *jp2);
441 /* ----------------------------------------------------------------------- */
444 * Reads a box header. The box is the way data is packed inside a jpeg2000 file structure.
446 * @param cio the input stream to read data from.
447 * @param box the box structure to fill.
448 * @param p_number_bytes_read pointer to an int that will store the number of bytes read from the stream (should usually be 8).
449 * @param p_manager user event manager.
451 * @return true if the box is reconized, false otherwise
453 opj_bool opj_jp2_read_boxhdr(opj_jp2_box_t *box,
454 OPJ_UINT32 * p_number_bytes_read,
455 opj_stream_private_t *cio,
456 opj_event_mgr_t * p_manager
459 /* read header from file */
460 unsigned char l_data_header [8];
465 assert(p_number_bytes_read != 00);
466 assert(p_manager != 00);
468 *p_number_bytes_read = opj_stream_read_data(cio,l_data_header,8,p_manager);
469 if (*p_number_bytes_read != 8) {
473 /* process read data */
474 opj_read_bytes(l_data_header,&(box->length), 4);
475 opj_read_bytes(l_data_header+4,&(box->type), 4);
477 /* do we have a "special very large box ?" */
478 /* read then the XLBox */
479 if (box->length == 1) {
480 OPJ_UINT32 l_xl_part_size;
482 OPJ_UINT32 l_nb_bytes_read = opj_stream_read_data(cio,l_data_header,8,p_manager);
483 if (l_nb_bytes_read != 8) {
484 if (l_nb_bytes_read > 0) {
485 *p_number_bytes_read += l_nb_bytes_read;
491 opj_read_bytes(l_data_header,&l_xl_part_size, 4);
492 if (l_xl_part_size != 0) {
493 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot handle box sizes higher than 2^32\n");
496 opj_read_bytes(l_data_header,&(box->length), 4);
502 static void jp2_write_url(opj_cio_t *cio, char *Idx_file) {
506 box.init_pos = cio_tell(cio);
508 cio_write(cio, JP2_URL, 4); /* DBTL */
509 cio_write(cio, 0, 1); /* VERS */
510 cio_write(cio, 0, 3); /* FLAG */
513 for (i = 0; i < strlen(Idx_file); i++) {
514 cio_write(cio, Idx_file[i], 1);
518 box.length = cio_tell(cio) - box.init_pos;
519 cio_seek(cio, box.init_pos);
520 cio_write(cio, box.length, 4); /* L */
521 cio_seek(cio, box.init_pos + box.length);
527 * Reads a IHDR box - Image Header box
529 * @param jp2 the jpeg2000 file codec.
530 * @param p_image_header_data pointer to actual data (already read from file)
531 * @param p_image_header_size the size of the image header
532 * @param p_manager the user event manager.
534 * @return true if the image header is valid, fale else.
536 opj_bool opj_jp2_read_ihdr( opj_jp2_v2_t *jp2,
537 OPJ_BYTE *p_image_header_data,
538 OPJ_UINT32 p_image_header_size,
539 opj_event_mgr_t * p_manager )
542 assert(p_image_header_data != 00);
544 assert(p_manager != 00);
546 if (p_image_header_size != 14) {
547 opj_event_msg_v2(p_manager, EVT_ERROR, "Bad image header box (bad size)\n");
551 opj_read_bytes(p_image_header_data,&(jp2->h),4); /* HEIGHT */
552 p_image_header_data += 4;
553 opj_read_bytes(p_image_header_data,&(jp2->w),4); /* WIDTH */
554 p_image_header_data += 4;
555 opj_read_bytes(p_image_header_data,&(jp2->numcomps),2); /* NC */
556 p_image_header_data += 2;
558 /* allocate memory for components */
559 jp2->comps = (opj_jp2_comps_t*) opj_malloc(jp2->numcomps * sizeof(opj_jp2_comps_t));
560 if (jp2->comps == 0) {
561 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to handle image header (ihdr)\n");
564 memset(jp2->comps,0,jp2->numcomps * sizeof(opj_jp2_comps_t));
566 opj_read_bytes(p_image_header_data,&(jp2->bpc),1); /* BPC */
567 ++ p_image_header_data;
569 /* if equal to 0 then need a BPC box (cf. chapter about image header box of the norm) */
570 /*if (jp2->bpc == 0){
571 indicate with a flag that we will wait a BPC box
574 opj_read_bytes(p_image_header_data,&(jp2->C),1); /* C */
575 ++ p_image_header_data;
577 /* Should be equal to 7 cf. chapter about image header box of the norm */
579 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);
582 opj_read_bytes(p_image_header_data,&(jp2->UnkC),1); /* UnkC */
583 ++ p_image_header_data;
584 opj_read_bytes(p_image_header_data,&(jp2->IPR),1); /* IPR */
585 ++ p_image_header_data;
591 * Writes the Image Header box - Image Header box.
593 * @param jp2 jpeg2000 file codec.
594 * @param p_nb_bytes_written pointer to store the nb of bytes written by the function.
596 * @return the data being copied.
598 OPJ_BYTE * opj_jp2_write_ihdr(opj_jp2_v2_t *jp2,
599 OPJ_UINT32 * p_nb_bytes_written
602 unsigned char * l_ihdr_data,* l_current_ihdr_ptr;
606 assert(p_nb_bytes_written != 00);
608 /* default image header is 22 bytes wide */
609 l_ihdr_data = (unsigned char *) opj_malloc(22);
610 if (l_ihdr_data == 00) {
613 memset(l_ihdr_data,0,22);
615 l_current_ihdr_ptr = l_ihdr_data;
617 opj_write_bytes(l_current_ihdr_ptr,22,4); /* write box size */
618 l_current_ihdr_ptr+=4;
620 opj_write_bytes(l_current_ihdr_ptr,JP2_IHDR, 4); /* IHDR */
621 l_current_ihdr_ptr+=4;
623 opj_write_bytes(l_current_ihdr_ptr,jp2->h, 4); /* HEIGHT */
624 l_current_ihdr_ptr+=4;
626 opj_write_bytes(l_current_ihdr_ptr, jp2->w, 4); /* WIDTH */
627 l_current_ihdr_ptr+=4;
629 opj_write_bytes(l_current_ihdr_ptr, jp2->numcomps, 2); /* NC */
630 l_current_ihdr_ptr+=2;
632 opj_write_bytes(l_current_ihdr_ptr, jp2->bpc, 1); /* BPC */
633 ++l_current_ihdr_ptr;
635 opj_write_bytes(l_current_ihdr_ptr, jp2->C, 1); /* C : Always 7 */
636 ++l_current_ihdr_ptr;
638 opj_write_bytes(l_current_ihdr_ptr, jp2->UnkC, 1); /* UnkC, colorspace unknown */
639 ++l_current_ihdr_ptr;
641 opj_write_bytes(l_current_ihdr_ptr, jp2->IPR, 1); /* IPR, no intellectual property */
642 ++l_current_ihdr_ptr;
644 *p_nb_bytes_written = 22;
650 * Writes the Bit per Component box.
652 * @param jp2 jpeg2000 file codec.
653 * @param p_nb_bytes_written pointer to store the nb of bytes written by the function.
655 * @return the data being copied.
657 OPJ_BYTE * opj_jp2_write_bpcc( opj_jp2_v2_t *jp2,
658 OPJ_UINT32 * p_nb_bytes_written
662 /* room for 8 bytes for box and 1 byte for each component */
663 int l_bpcc_size = 8 + jp2->numcomps;
664 unsigned char * l_bpcc_data,* l_current_bpcc_ptr;
668 assert(p_nb_bytes_written != 00);
670 l_bpcc_data = (unsigned char *) opj_malloc(l_bpcc_size);
671 if (l_bpcc_data == 00) {
674 memset(l_bpcc_data,0,l_bpcc_size);
676 l_current_bpcc_ptr = l_bpcc_data;
678 opj_write_bytes(l_current_bpcc_ptr,l_bpcc_size,4); /* write box size */
679 l_current_bpcc_ptr += 4;
681 opj_write_bytes(l_current_bpcc_ptr,JP2_BPCC,4); /* BPCC */
682 l_current_bpcc_ptr += 4;
684 for (i = 0; i < jp2->numcomps; ++i) {
685 opj_write_bytes(l_current_bpcc_ptr, jp2->comps[i].bpcc, 1); /* write each component information */
686 ++l_current_bpcc_ptr;
689 *p_nb_bytes_written = l_bpcc_size;
696 * Reads a Bit per Component box.
698 * @param jp2 the jpeg2000 file codec.
699 * @param p_bpc_header_data pointer to actual data (already read from file)
700 * @param p_bpc_header_size pointer that will hold the size of the bpc header
701 * @param p_manager the user event manager.
703 * @return true if the bpc header is valid, false otherwise.
705 opj_bool opj_jp2_read_bpcc( opj_jp2_v2_t *jp2,
706 OPJ_BYTE * p_bpc_header_data,
707 OPJ_UINT32 p_bpc_header_size,
708 opj_event_mgr_t * p_manager
714 assert(p_bpc_header_data != 00);
716 assert(p_manager != 00);
719 /*if (jp2->bpc != 0 ){
720 opj_event_msg_v2(p_manager, EVT_WARNING, "A BPCC header box is available although BPC is different to zero (%d)\n",jp2->bpc);
723 /* and length is relevant */
724 if (p_bpc_header_size != jp2->numcomps) {
725 opj_event_msg_v2(p_manager, EVT_ERROR, "Bad BPCC header box (bad size)\n");
729 /* read info for each component */
730 for (i = 0; i < jp2->numcomps; ++i) {
731 opj_read_bytes(p_bpc_header_data,&jp2->comps[i].bpcc ,1); /* read each BPCC component */
739 * Writes the Colour Specification box.
741 * @param jp2 jpeg2000 file codec.
742 * @param p_nb_bytes_written pointer to store the nb of bytes written by the function.
744 * @return the data being copied.
746 OPJ_BYTE * opj_jp2_write_colr( opj_jp2_v2_t *jp2,
747 OPJ_UINT32 * p_nb_bytes_written
750 /* room for 8 bytes for box 3 for common data and variable upon profile*/
751 unsigned int l_colr_size = 11;
752 unsigned char * l_colr_data,* l_current_colr_ptr;
756 assert(p_nb_bytes_written != 00);
769 l_colr_data = (unsigned char *) opj_malloc(l_colr_size);
770 if (l_colr_data == 00) {
773 memset(l_colr_data,0,l_colr_size);
775 l_current_colr_ptr = l_colr_data;
777 opj_write_bytes(l_current_colr_ptr,l_colr_size,4); /* write box size */
778 l_current_colr_ptr += 4;
780 opj_write_bytes(l_current_colr_ptr,JP2_COLR,4); /* BPCC */
781 l_current_colr_ptr += 4;
783 opj_write_bytes(l_current_colr_ptr, jp2->meth,1); /* METH */
784 ++l_current_colr_ptr;
786 opj_write_bytes(l_current_colr_ptr, jp2->precedence,1); /* PRECEDENCE */
787 ++l_current_colr_ptr;
789 opj_write_bytes(l_current_colr_ptr, jp2->approx,1); /* APPROX */
790 ++l_current_colr_ptr;
792 if (jp2->meth == 1) {
793 opj_write_bytes(l_current_colr_ptr, jp2->enumcs,4); /* EnumCS */
796 opj_write_bytes(l_current_colr_ptr, 0, 1); /* PROFILE (??) */
799 *p_nb_bytes_written = l_colr_size;
804 void opj_jp2_free_pclr(opj_jp2_color_t *color)
806 opj_free(color->jp2_pclr->channel_sign);
807 opj_free(color->jp2_pclr->channel_size);
808 opj_free(color->jp2_pclr->entries);
810 if(color->jp2_pclr->cmap) opj_free(color->jp2_pclr->cmap);
812 opj_free(color->jp2_pclr); color->jp2_pclr = NULL;
815 static void free_color_data(opj_jp2_color_t *color)
819 jp2_free_pclr(color);
823 if(color->jp2_cdef->info) opj_free(color->jp2_cdef->info);
824 opj_free(color->jp2_cdef);
826 if(color->icc_profile_buf) opj_free(color->icc_profile_buf);
830 void opj_jp2_apply_pclr(opj_image_t *image, opj_jp2_color_t *color)
832 opj_image_comp_t *old_comps, *new_comps;
833 OPJ_BYTE *channel_size, *channel_sign;
835 opj_jp2_cmap_comp_t *cmap;
836 OPJ_INT32 *src, *dst;
838 OPJ_UINT16 i, nr_channels, cmp, pcol;
841 channel_size = color->jp2_pclr->channel_size;
842 channel_sign = color->jp2_pclr->channel_sign;
843 entries = color->jp2_pclr->entries;
844 cmap = color->jp2_pclr->cmap;
845 nr_channels = color->jp2_pclr->nr_channels;
847 old_comps = image->comps;
848 new_comps = (opj_image_comp_t*)
849 opj_malloc(nr_channels * sizeof(opj_image_comp_t));
851 for(i = 0; i < nr_channels; ++i) {
852 pcol = cmap[i].pcol; cmp = cmap[i].cmp;
854 new_comps[pcol] = old_comps[cmp];
857 if(cmap[i].mtyp == 0){
858 old_comps[cmp].data = NULL; continue;
861 /* Palette mapping: */
862 new_comps[pcol].data = (int*)
863 opj_malloc(old_comps[cmp].w * old_comps[cmp].h * sizeof(int));
864 new_comps[pcol].prec = channel_size[i];
865 new_comps[pcol].sgnd = channel_sign[i];
868 top_k = color->jp2_pclr->nr_entries - 1;
870 for(i = 0; i < nr_channels; ++i) {
872 if(cmap[i].mtyp == 0) continue;
874 /* Palette mapping: */
875 cmp = cmap[i].cmp; pcol = cmap[i].pcol;
876 src = old_comps[cmp].data;
877 dst = new_comps[pcol].data;
878 max = new_comps[pcol].w * new_comps[pcol].h;
880 for(j = 0; j < max; ++j)
883 if((k = src[j]) < 0) k = 0; else if(k > top_k) k = top_k;
886 dst[j] = entries[k * nr_channels + pcol];
890 max = image->numcomps;
891 for(i = 0; i < max; ++i) {
892 if(old_comps[i].data) opj_free(old_comps[i].data);
896 image->comps = new_comps;
897 image->numcomps = nr_channels;
899 opj_jp2_free_pclr(color);
904 * Reads a palette box.
906 * @param jp2 the jpeg2000 file codec.
907 * @param p_pclr_header_data pointer to actual data (already read from file)
908 * @param p_pclr_header_size pointer that will hold the size of the PCLR header
909 * @param p_manager the user event manager.
911 * @return true if the bpc header is valid, fale else.
913 opj_bool opj_jp2_read_pclr( opj_jp2_v2_t *jp2,
914 OPJ_BYTE * p_pclr_header_data,
915 OPJ_UINT32 p_pclr_header_size,
916 opj_event_mgr_t * p_manager
919 opj_jp2_pclr_t *jp2_pclr;
920 OPJ_BYTE *channel_size, *channel_sign;
922 OPJ_UINT16 nr_entries,nr_channels;
927 assert(p_pclr_header_data != 00);
929 assert(p_manager != 00);
930 (void)p_pclr_header_size;
932 if(jp2->color.jp2_pclr)
935 opj_read_bytes(p_pclr_header_data, &l_value , 2); /* NE */
936 p_pclr_header_data += 2;
937 nr_entries = (OPJ_UINT16) l_value;
939 opj_read_bytes(p_pclr_header_data, &l_value , 1); /* NPC */
940 ++p_pclr_header_data;
941 nr_channels = (OPJ_UINT16) l_value;
943 entries = (OPJ_UINT32*) opj_malloc(nr_channels * nr_entries * sizeof(OPJ_UINT32));
944 channel_size = (OPJ_BYTE*) opj_malloc(nr_channels);
945 channel_sign = (OPJ_BYTE*) opj_malloc(nr_channels);
947 jp2_pclr = (opj_jp2_pclr_t*)opj_malloc(sizeof(opj_jp2_pclr_t));
948 jp2_pclr->channel_sign = channel_sign;
949 jp2_pclr->channel_size = channel_size;
950 jp2_pclr->entries = entries;
951 jp2_pclr->nr_entries = nr_entries;
952 jp2_pclr->nr_channels = (OPJ_BYTE) l_value;
953 jp2_pclr->cmap = NULL;
955 jp2->color.jp2_pclr = jp2_pclr;
957 for(i = 0; i < nr_channels; ++i) {
958 opj_read_bytes(p_pclr_header_data, &l_value , 1); /* Bi */
959 ++p_pclr_header_data;
961 channel_size[i] = (l_value & 0x7f) + 1;
962 channel_sign[i] = (l_value & 0x80)? 1 : 0;
965 for(j = 0; j < nr_entries; ++j) {
966 for(i = 0; i < nr_channels; ++i) {
967 int bytes_to_read = (channel_size[i]+7)>>3;
969 opj_read_bytes(p_pclr_header_data, &l_value , bytes_to_read); /* Cji */
970 p_pclr_header_data += bytes_to_read;
971 *entries = (OPJ_UINT32) l_value;
980 * Reads the Component Mapping box.
982 * @param p_cmap_header_data pointer to actual data (already read from file)
983 * @param jp2 the jpeg2000 file codec.
984 * @param p_cmap_header_size pointer that will hold the size of the color header
985 * @param p_manager the user event manager.
987 * @return true if the cdef header is valid, false else.
989 opj_bool opj_jp2_read_cmap( opj_jp2_v2_t * jp2,
990 OPJ_BYTE * p_cmap_header_data,
991 OPJ_UINT32 p_cmap_header_size,
992 opj_event_mgr_t * p_manager
995 opj_jp2_cmap_comp_t *cmap;
996 OPJ_BYTE i, nr_channels;
1001 assert(p_cmap_header_data != 00);
1002 assert(p_manager != 00);
1003 (void)p_cmap_header_size;
1005 /* Need nr_channels: */
1006 if(jp2->color.jp2_pclr == NULL) {
1007 opj_event_msg_v2(p_manager, EVT_ERROR, "Need to read a PCLR box before the CMAP box.\n");
1011 /* Part 1, I.5.3.5: 'There shall be at most one Component Mapping box
1012 * inside a JP2 Header box' :
1014 if(jp2->color.jp2_pclr->cmap) {
1015 opj_event_msg_v2(p_manager, EVT_ERROR, "Only one CMAP box is allowed.\n");
1019 nr_channels = jp2->color.jp2_pclr->nr_channels;
1020 cmap = (opj_jp2_cmap_comp_t*) opj_malloc(nr_channels * sizeof(opj_jp2_cmap_comp_t));
1022 for(i = 0; i < nr_channels; ++i) {
1023 opj_read_bytes(p_cmap_header_data, &l_value, 2); /* CMP^i */
1024 p_cmap_header_data +=2;
1025 cmap[i].cmp = (OPJ_UINT16) l_value;
1027 opj_read_bytes(p_cmap_header_data, &l_value, 1); /* MTYP^i */
1028 ++p_cmap_header_data;
1029 cmap[i].mtyp = (OPJ_BYTE) l_value;
1031 opj_read_bytes(p_cmap_header_data, &l_value, 1); /* PCOL^i */
1032 ++p_cmap_header_data;
1033 cmap[i].pcol = (OPJ_BYTE) l_value;
1036 jp2->color.jp2_pclr->cmap = cmap;
1041 void opj_jp2_apply_cdef(opj_image_t *image, opj_jp2_color_t *color)
1043 opj_jp2_cdef_info_t *info;
1044 OPJ_INT32 color_space;
1045 OPJ_UINT16 i, n, cn, typ, asoc, acn;
1047 color_space = image->color_space;
1048 info = color->jp2_cdef->info;
1049 n = color->jp2_cdef->n;
1051 for(i = 0; i < n; ++i)
1053 /* WATCH: acn = asoc - 1 ! */
1054 if((asoc = info[i].asoc) == 0) continue;
1056 cn = info[i].cn; typ = info[i].typ; acn = asoc - 1;
1060 opj_image_comp_t saved;
1062 memcpy(&saved, &image->comps[cn], sizeof(opj_image_comp_t));
1063 memcpy(&image->comps[cn], &image->comps[acn], sizeof(opj_image_comp_t));
1064 memcpy(&image->comps[acn], &saved, sizeof(opj_image_comp_t));
1066 info[i].asoc = cn + 1;
1067 info[acn].asoc = info[acn].cn + 1;
1071 if(color->jp2_cdef->info) opj_free(color->jp2_cdef->info);
1073 opj_free(color->jp2_cdef); color->jp2_cdef = NULL;
1075 }/* jp2_apply_cdef() */
1078 * Reads the Component Definition box.
1080 * @param p_cdef_header_data pointer to actual data (already read from file)
1081 * @param jp2 the jpeg2000 file codec.
1082 * @param p_cdef_header_size pointer that will hold the size of the color header
1083 * @param p_manager the user event manager.
1085 * @return true if the cdef header is valid, false else.
1087 opj_bool opj_jp2_read_cdef( opj_jp2_v2_t * jp2,
1088 OPJ_BYTE * p_cdef_header_data,
1089 OPJ_UINT32 p_cdef_header_size,
1090 opj_event_mgr_t * p_manager
1093 opj_jp2_cdef_info_t *cdef_info;
1099 assert(p_cdef_header_data != 00);
1100 assert(p_manager != 00);
1101 (void)p_cdef_header_size;
1103 /* Part 1, I.5.3.6: 'The shall be at most one Channel Definition box
1104 * inside a JP2 Header box.'*/
1105 if(jp2->color.jp2_cdef) return OPJ_FALSE;
1107 opj_read_bytes(p_cdef_header_data,&l_value ,2); /* N */
1108 p_cdef_header_data+= 2;
1110 if ( (OPJ_UINT16)l_value == 0){ /* szukw000: FIXME */
1111 opj_event_msg_v2(p_manager, EVT_ERROR, "Number of channel description is equal to zero in CDEF box.\n");
1115 cdef_info = (opj_jp2_cdef_info_t*) opj_malloc(l_value * sizeof(opj_jp2_cdef_info_t));
1117 jp2->color.jp2_cdef = (opj_jp2_cdef_t*)opj_malloc(sizeof(opj_jp2_cdef_t));
1118 jp2->color.jp2_cdef->info = cdef_info;
1119 jp2->color.jp2_cdef->n = (OPJ_UINT16) l_value;
1121 for(i = 0; i < jp2->color.jp2_cdef->n; ++i) {
1122 opj_read_bytes(p_cdef_header_data, &l_value, 2); /* Cn^i */
1123 p_cdef_header_data +=2;
1124 cdef_info[i].cn = (OPJ_UINT16) l_value;
1126 opj_read_bytes(p_cdef_header_data, &l_value, 2); /* Typ^i */
1127 p_cdef_header_data +=2;
1128 cdef_info[i].typ = (OPJ_UINT16) l_value;
1130 opj_read_bytes(p_cdef_header_data, &l_value, 2); /* Asoc^i */
1131 p_cdef_header_data +=2;
1132 cdef_info[i].asoc = (OPJ_UINT16) l_value;
1139 * Reads the Colour Specification box.
1141 * @param p_colr_header_data pointer to actual data (already read from file)
1142 * @param jp2 the jpeg2000 file codec.
1143 * @param p_colr_header_size pointer that will hold the size of the color header
1144 * @param p_colr_header_max_size maximum size of the header, any size bigger than this value should result the function to output false.
1145 * @param p_manager the user event manager.
1147 * @return true if the bpc header is valid, fale else.
1149 opj_bool opj_jp2_read_colr( opj_jp2_v2_t *jp2,
1150 OPJ_BYTE * p_colr_header_data,
1151 OPJ_UINT32 p_colr_header_size,
1152 opj_event_mgr_t * p_manager
1159 assert(p_colr_header_data != 00);
1160 assert(p_manager != 00);
1162 if (p_colr_header_size < 3) {
1163 opj_event_msg_v2(p_manager, EVT_ERROR, "Bad COLR header box (bad size)\n");
1167 /* Part 1, I.5.3.3 : 'A conforming JP2 reader shall ignore all Colour
1168 * Specification boxes after the first.'
1170 if(jp2->color.jp2_has_colr) {
1171 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");
1172 p_colr_header_data += p_colr_header_size;
1176 opj_read_bytes(p_colr_header_data,&jp2->meth ,1); /* METH */
1177 ++p_colr_header_data;
1179 opj_read_bytes(p_colr_header_data,&jp2->precedence ,1); /* PRECEDENCE */
1180 ++p_colr_header_data;
1182 opj_read_bytes(p_colr_header_data,&jp2->approx ,1); /* APPROX */
1183 ++p_colr_header_data;
1185 if (jp2->meth == 1) {
1186 if (p_colr_header_size != 7) {
1187 opj_event_msg_v2(p_manager, EVT_ERROR, "Bad BPCC header box (bad size)\n");
1191 opj_read_bytes(p_colr_header_data,&jp2->enumcs ,4); /* EnumCS */
1193 else if (jp2->meth == 2) {
1195 int it_icc_value = 0;
1196 int icc_len = p_colr_header_size - 3;
1198 jp2->color.icc_profile_len = icc_len;
1199 jp2->color.icc_profile_buf = (unsigned char*) opj_malloc(icc_len);
1201 memset(jp2->color.icc_profile_buf, 0, icc_len * sizeof(unsigned char));
1203 for (it_icc_value = 0; it_icc_value < icc_len; ++it_icc_value)
1205 opj_read_bytes(p_colr_header_data,&l_value,1); /* icc values */
1206 ++p_colr_header_data;
1207 jp2->color.icc_profile_buf[it_icc_value] = (OPJ_BYTE) l_value;
1212 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);
1214 jp2->color.jp2_has_colr = 1;
1220 opj_bool opj_jp2_decode(opj_jp2_v2_t *jp2,
1221 opj_stream_private_t *p_stream,
1222 opj_image_t* p_image,
1223 opj_event_mgr_t * p_manager)
1229 if( ! opj_j2k_decode(jp2->j2k, p_stream, p_image, p_manager) ) {
1230 opj_event_msg_v2(p_manager, EVT_ERROR, "Failed to decode the codestream in the JP2 file\n");
1234 if (!jp2->ignore_pclr_cmap_cdef){
1236 /* Set Image Color Space */
1237 if (jp2->enumcs == 16)
1238 p_image->color_space = CLRSPC_SRGB;
1239 else if (jp2->enumcs == 17)
1240 p_image->color_space = CLRSPC_GRAY;
1241 else if (jp2->enumcs == 18)
1242 p_image->color_space = CLRSPC_SYCC;
1244 p_image->color_space = CLRSPC_UNKNOWN;
1246 /* Apply the color space if needed */
1247 if(jp2->color.jp2_cdef) {
1248 opj_jp2_apply_cdef(p_image, &(jp2->color));
1251 if(jp2->color.jp2_pclr) {
1252 /* Part 1, I.5.3.4: Either both or none : */
1253 if( !jp2->color.jp2_pclr->cmap)
1254 opj_jp2_free_pclr(&(jp2->color));
1256 opj_jp2_apply_pclr(p_image, &(jp2->color));
1259 if(jp2->color.icc_profile_buf) {
1260 p_image->icc_profile_buf = jp2->color.icc_profile_buf;
1261 p_image->icc_profile_len = jp2->color.icc_profile_len;
1262 jp2->color.icc_profile_buf = NULL;
1271 * Writes the Jpeg2000 file Header box - JP2 Header box (warning, this is a super box).
1273 * @param cio the stream to write data to.
1274 * @param jp2 the jpeg2000 file codec.
1275 * @param p_manager user event manager.
1277 * @return true if writting was successful.
1279 opj_bool opj_jp2_write_jp2h(opj_jp2_v2_t *jp2,
1280 opj_stream_private_t *stream,
1281 opj_event_mgr_t * p_manager
1284 opj_jp2_img_header_writer_handler_t l_writers [3];
1285 opj_jp2_img_header_writer_handler_t * l_current_writer;
1288 /* size of data for super box*/
1289 int l_jp2h_size = 8;
1290 opj_bool l_result = OPJ_TRUE;
1292 /* to store the data of the super box */
1293 unsigned char l_jp2h_data [8];
1296 assert(stream != 00);
1298 assert(p_manager != 00);
1300 memset(l_writers,0,sizeof(l_writers));
1302 if (jp2->bpc == 255) {
1304 l_writers[0].handler = opj_jp2_write_ihdr;
1305 l_writers[1].handler = opj_jp2_write_bpcc;
1306 l_writers[2].handler = opj_jp2_write_colr;
1310 l_writers[0].handler = opj_jp2_write_ihdr;
1311 l_writers[1].handler = opj_jp2_write_colr;
1314 /* write box header */
1315 /* write JP2H type */
1316 opj_write_bytes(l_jp2h_data+4,JP2_JP2H,4);
1318 l_current_writer = l_writers;
1319 for (i=0;i<l_nb_pass;++i) {
1320 l_current_writer->m_data = l_current_writer->handler(jp2,&(l_current_writer->m_size));
1321 if (l_current_writer->m_data == 00) {
1322 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to hold JP2 Header data\n");
1323 l_result = OPJ_FALSE;
1327 l_jp2h_size += l_current_writer->m_size;
1332 l_current_writer = l_writers;
1333 for (i=0;i<l_nb_pass;++i) {
1334 if (l_current_writer->m_data != 00) {
1335 opj_free(l_current_writer->m_data );
1343 /* write super box size */
1344 opj_write_bytes(l_jp2h_data,l_jp2h_size,4);
1346 /* write super box data on stream */
1347 if (opj_stream_write_data(stream,l_jp2h_data,8,p_manager) != 8) {
1348 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream error while writing JP2 Header box\n");
1349 l_result = OPJ_FALSE;
1353 l_current_writer = l_writers;
1354 for (i=0;i<l_nb_pass;++i) {
1355 if (opj_stream_write_data(stream,l_current_writer->m_data,l_current_writer->m_size,p_manager) != l_current_writer->m_size) {
1356 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream error while writting JP2 Header box\n");
1357 l_result = OPJ_FALSE;
1364 l_current_writer = l_writers;
1367 for (i=0;i<l_nb_pass;++i) {
1368 if (l_current_writer->m_data != 00) {
1369 opj_free(l_current_writer->m_data );
1378 * Writes a FTYP box - File type box
1380 * @param cio the stream to write data to.
1381 * @param jp2 the jpeg2000 file codec.
1382 * @param p_manager the user event manager.
1384 * @return true if writting was successful.
1386 opj_bool opj_jp2_write_ftyp(opj_jp2_v2_t *jp2,
1387 opj_stream_private_t *cio,
1388 opj_event_mgr_t * p_manager )
1391 unsigned int l_ftyp_size = 16 + 4 * jp2->numcl;
1392 unsigned char * l_ftyp_data, * l_current_data_ptr;
1398 assert(p_manager != 00);
1400 l_ftyp_data = (unsigned char *) opj_malloc(l_ftyp_size);
1402 if (l_ftyp_data == 00) {
1403 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to handle ftyp data\n");
1407 memset(l_ftyp_data,0,l_ftyp_size);
1409 l_current_data_ptr = l_ftyp_data;
1411 opj_write_bytes(l_current_data_ptr, l_ftyp_size,4); /* box size */
1412 l_current_data_ptr += 4;
1414 opj_write_bytes(l_current_data_ptr, JP2_FTYP,4); /* FTYP */
1415 l_current_data_ptr += 4;
1417 opj_write_bytes(l_current_data_ptr, jp2->brand,4); /* BR */
1418 l_current_data_ptr += 4;
1420 opj_write_bytes(l_current_data_ptr, jp2->minversion,4); /* MinV */
1421 l_current_data_ptr += 4;
1423 for (i = 0; i < jp2->numcl; i++) {
1424 opj_write_bytes(l_current_data_ptr, jp2->cl[i],4); /* CL */
1427 l_result = (opj_stream_write_data(cio,l_ftyp_data,l_ftyp_size,p_manager) == l_ftyp_size);
1430 opj_event_msg_v2(p_manager, EVT_ERROR, "Error while writting ftyp data to stream\n");
1433 opj_free(l_ftyp_data);
1439 * Writes the Jpeg2000 codestream Header box - JP2C Header box.
1441 * @param cio the stream to write data to.
1442 * @param jp2 the jpeg2000 file codec.
1443 * @param p_manager user event manager.
1445 * @return true if writting was successful.
1447 opj_bool opj_jp2_write_jp2c(opj_jp2_v2_t *jp2,
1448 opj_stream_private_t *cio,
1449 opj_event_mgr_t * p_manager )
1451 OPJ_OFF_T j2k_codestream_exit;
1452 OPJ_BYTE l_data_header [8];
1457 assert(p_manager != 00);
1458 assert(opj_stream_has_seek(cio));
1460 j2k_codestream_exit = opj_stream_tell(cio);
1461 opj_write_bytes(l_data_header,
1462 (OPJ_UINT32) (j2k_codestream_exit - jp2->j2k_codestream_offset),
1463 4); /* size of codestream */
1464 opj_write_bytes(l_data_header + 4,JP2_JP2C,4); /* JP2C */
1466 if (! opj_stream_seek(cio,jp2->j2k_codestream_offset,p_manager)) {
1467 opj_event_msg_v2(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
1471 if (opj_stream_write_data(cio,l_data_header,8,p_manager) != 8) {
1472 opj_event_msg_v2(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
1476 if (! opj_stream_seek(cio,j2k_codestream_exit,p_manager)) {
1477 opj_event_msg_v2(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
1485 * Writes a jpeg2000 file signature box.
1487 * @param cio the stream to write data to.
1488 * @param jp2 the jpeg2000 file codec.
1489 * @param p_manager the user event manager.
1491 * @return true if writting was successful.
1493 opj_bool opj_jp2_write_jp( opj_jp2_v2_t *jp2,
1494 opj_stream_private_t *cio,
1495 opj_event_mgr_t * p_manager )
1497 /* 12 bytes will be read */
1498 unsigned char l_signature_data [12];
1503 assert(p_manager != 00);
1505 /* write box length */
1506 opj_write_bytes(l_signature_data,12,4);
1507 /* writes box type */
1508 opj_write_bytes(l_signature_data+4,JP2_JP,4);
1509 /* writes magic number*/
1510 opj_write_bytes(l_signature_data+8,0x0d0a870a,4);
1512 if (opj_stream_write_data(cio,l_signature_data,12,p_manager) != 12) {
1519 static int write_fidx( int offset_jp2c, int length_jp2c, int offset_idx, int length_idx, opj_cio_t *cio)
1523 lenp = cio_tell( cio);
1524 cio_skip( cio, 4); /* L [at the end] */
1525 cio_write( cio, JPIP_FIDX, 4); /* IPTR */
1527 write_prxy( offset_jp2c, length_jp2c, offset_idx, length_idx, cio);
1529 len = cio_tell( cio)-lenp;
1530 cio_seek( cio, lenp);
1531 cio_write( cio, len, 4); /* L */
1532 cio_seek( cio, lenp+len);
1537 static void write_prxy( int offset_jp2c, int length_jp2c, int offset_idx, int length_idx, opj_cio_t *cio)
1541 lenp = cio_tell( cio);
1542 cio_skip( cio, 4); /* L [at the end] */
1543 cio_write( cio, JPIP_PRXY, 4); /* IPTR */
1545 cio_write( cio, offset_jp2c, 8); /* OOFF */
1546 cio_write( cio, length_jp2c, 4); /* OBH part 1 */
1547 cio_write( cio, JP2_JP2C, 4); /* OBH part 2 */
1549 cio_write( cio, 1,1); /* NI */
1551 cio_write( cio, offset_idx, 8); /* IOFF */
1552 cio_write( cio, length_idx, 4); /* IBH part 1 */
1553 cio_write( cio, JPIP_CIDX, 4); /* IBH part 2 */
1555 len = cio_tell( cio)-lenp;
1556 cio_seek( cio, lenp);
1557 cio_write( cio, len, 4); /* L */
1558 cio_seek( cio, lenp+len);
1561 static void write_iptr( int offset, int length, opj_cio_t *cio)
1565 lenp = cio_tell( cio);
1566 cio_skip( cio, 4); /* L [at the end] */
1567 cio_write( cio, JPIP_IPTR, 4); /* IPTR */
1569 cio_write( cio, offset, 8);
1570 cio_write( cio, length, 8);
1572 len = cio_tell( cio)-lenp;
1573 cio_seek( cio, lenp);
1574 cio_write( cio, len, 4); /* L */
1575 cio_seek( cio, lenp+len);
1579 /* ----------------------------------------------------------------------- */
1580 /* JP2 decoder interface */
1581 /* ----------------------------------------------------------------------- */
1584 void opj_jp2_setup_decoder(opj_jp2_v2_t *jp2, opj_dparameters_t *parameters)
1586 /* setup the J2K codec */
1587 opj_j2k_setup_decoder(jp2->j2k, parameters);
1589 /* further JP2 initializations go here */
1590 jp2->color.jp2_has_colr = 0;
1591 jp2->ignore_pclr_cmap_cdef = parameters->flags & OPJ_DPARAMETERS_IGNORE_PCLR_CMAP_CDEF_FLAG;
1595 /* ----------------------------------------------------------------------- */
1596 /* JP2 encoder interface */
1597 /* ----------------------------------------------------------------------- */
1599 void opj_jp2_setup_encoder( opj_jp2_v2_t *jp2,
1600 opj_cparameters_t *parameters,
1602 opj_event_mgr_t * p_manager)
1607 if(!jp2 || !parameters || !image)
1610 /* setup the J2K codec */
1611 /* ------------------- */
1613 /* Check if number of components respects standard */
1614 if (image->numcomps < 1 || image->numcomps > 16384) {
1615 opj_event_msg_v2(p_manager, EVT_ERROR, "Invalid number of components specified while setting up JP2 encoder\n");
1619 j2k_setup_encoder_v2(jp2->j2k, parameters, image, p_manager );
1621 /* setup the JP2 codec */
1622 /* ------------------- */
1626 jp2->brand = JP2_JP2; /* BR */
1627 jp2->minversion = 0; /* MinV */
1629 jp2->cl = (unsigned int*) opj_malloc(jp2->numcl * sizeof(unsigned int));
1630 jp2->cl[0] = JP2_JP2; /* CL0 : JP2 */
1632 /* Image Header box */
1634 jp2->numcomps = image->numcomps; /* NC */
1635 jp2->comps = (opj_jp2_comps_t*) opj_malloc(jp2->numcomps * sizeof(opj_jp2_comps_t));
1636 jp2->h = image->y1 - image->y0; /* HEIGHT */
1637 jp2->w = image->x1 - image->x0; /* WIDTH */
1639 depth_0 = image->comps[0].prec - 1;
1640 sign = image->comps[0].sgnd;
1641 jp2->bpc = depth_0 + (sign << 7);
1642 for (i = 1; i < image->numcomps; i++) {
1643 int depth = image->comps[i].prec - 1;
1644 sign = image->comps[i].sgnd;
1645 if (depth_0 != depth)
1648 jp2->C = 7; /* C : Always 7 */
1649 jp2->UnkC = 0; /* UnkC, colorspace specified in colr box */
1650 jp2->IPR = 0; /* IPR, no intellectual property */
1652 /* BitsPerComponent box */
1653 for (i = 0; i < image->numcomps; i++) {
1654 jp2->comps[i].bpcc = image->comps[i].prec - 1 + (image->comps[i].sgnd << 7);
1657 /* Colour Specification box */
1658 if ((image->numcomps == 1 || image->numcomps == 3) && (jp2->bpc != 255)) {
1659 jp2->meth = 1; /* METH: Enumerated colourspace */
1661 jp2->meth = 2; /* METH: Restricted ICC profile */
1663 if (jp2->meth == 1) {
1664 if (image->color_space == 1)
1665 jp2->enumcs = 16; /* sRGB as defined by IEC 61966�2�1 */
1666 else if (image->color_space == 2)
1667 jp2->enumcs = 17; /* greyscale */
1668 else if (image->color_space == 3)
1669 jp2->enumcs = 18; /* YUV */
1671 jp2->enumcs = 0; /* PROFILE (??) */
1673 jp2->precedence = 0; /* PRECEDENCE */
1674 jp2->approx = 0; /* APPROX */
1676 // jp2->jpip_on = parameters->jpip_on;
1680 opj_bool opj_jp2_encode(opj_jp2_v2_t *jp2,
1681 opj_stream_private_t *stream,
1682 opj_event_mgr_t * p_manager)
1684 return j2k_encode_v2(jp2->j2k, stream, p_manager);
1689 * Ends the decompression procedures and possibiliy add data to be read after the
1692 opj_bool opj_jp2_end_decompress(opj_jp2_v2_t *jp2,
1693 opj_stream_private_t *cio,
1694 opj_event_mgr_t * p_manager
1700 assert(p_manager != 00);
1702 /* customization of the end encoding */
1703 opj_jp2_setup_end_header_reading(jp2);
1706 if (! opj_jp2_exec (jp2,jp2->m_procedure_list,cio,p_manager)) {
1710 return j2k_end_decompress(jp2->j2k, cio, p_manager);
1714 * Ends the compression procedures and possibility add data to be read after the
1717 opj_bool opj_jp2_end_compress( opj_jp2_v2_t *jp2,
1718 opj_stream_private_t *cio,
1719 opj_event_mgr_t * p_manager
1725 assert(p_manager != 00);
1727 /* customization of the end encoding */
1728 opj_jp2_setup_end_header_writting(jp2);
1730 if (! j2k_end_compress(jp2->j2k,cio,p_manager)) {
1735 return opj_jp2_exec(jp2,jp2->m_procedure_list,cio,p_manager);
1740 * Sets up the procedures to do on writing header after the codestream.
1741 * Developers wanting to extend the library can add their own writing procedures.
1743 void opj_jp2_setup_end_header_writting (opj_jp2_v2_t *jp2)
1748 opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)opj_jp2_write_jp2c );
1749 /* DEVELOPER CORNER, add your custom procedures */
1753 * Sets up the procedures to do on reading header after the codestream.
1754 * Developers wanting to extend the library can add their own writing procedures.
1756 void opj_jp2_setup_end_header_reading (opj_jp2_v2_t *jp2)
1760 opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)opj_jp2_read_header_procedure );
1761 /* DEVELOPER CORNER, add your custom procedures */
1765 * The default validation procedure without any extension.
1767 * @param jp2 the jpeg2000 codec to validate.
1768 * @param cio the input stream to validate.
1769 * @param p_manager the user event manager.
1771 * @return true if the parameters are correct.
1773 opj_bool opj_jp2_default_validation ( opj_jp2_v2_t * jp2,
1774 opj_stream_private_t *cio,
1775 opj_event_mgr_t * p_manager
1778 opj_bool l_is_valid = OPJ_TRUE;
1784 assert(p_manager != 00);
1786 /* JPEG2000 codec validation */
1789 /* STATE checking */
1790 /* make sure the state is at 0 */
1791 l_is_valid &= (jp2->jp2_state == JP2_STATE_NONE);
1793 /* make sure not reading a jp2h ???? WEIRD */
1794 l_is_valid &= (jp2->jp2_img_state == JP2_IMG_STATE_NONE);
1796 /* POINTER validation */
1797 /* make sure a j2k codec is present */
1798 l_is_valid &= (jp2->j2k != 00);
1800 /* make sure a procedure list is present */
1801 l_is_valid &= (jp2->m_procedure_list != 00);
1803 /* make sure a validation list is present */
1804 l_is_valid &= (jp2->m_validation_list != 00);
1806 /* PARAMETER VALIDATION */
1807 /* number of components */
1808 l_is_valid &= (jp2->numcl > 0);
1810 l_is_valid &= (jp2->h > 0);
1812 l_is_valid &= (jp2->w > 0);
1814 for (i = 0; i < jp2->numcomps; ++i) {
1815 l_is_valid &= (jp2->comps[i].bpcc > 0);
1819 l_is_valid &= ((jp2->meth > 0) && (jp2->meth < 3));
1821 /* stream validation */
1822 /* back and forth is needed */
1823 l_is_valid &= opj_stream_has_seek(cio);
1829 * Reads a jpeg2000 file header structure.
1831 * @param stream the stream to read data from.
1832 * @param jp2 the jpeg2000 file header structure.
1833 * @param p_manager the user event manager.
1835 * @return true if the box is valid.
1837 static opj_bool opj_jp2_read_header_procedure( opj_jp2_v2_t *jp2,
1838 opj_stream_private_t *stream,
1839 opj_event_mgr_t * p_manager
1843 OPJ_UINT32 l_nb_bytes_read;
1844 const opj_jp2_header_handler_t * l_current_handler;
1845 OPJ_UINT32 l_last_data_size = BOX_SIZE;
1846 OPJ_UINT32 l_current_data_size;
1847 unsigned char * l_current_data = 00;
1850 assert(stream != 00);
1852 assert(p_manager != 00);
1854 l_current_data = (unsigned char*)opj_malloc(l_last_data_size);
1856 if (l_current_data == 00) {
1857 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to handle jpeg2000 file header\n");
1860 memset(l_current_data, 0 , l_last_data_size);
1862 while (opj_jp2_read_boxhdr(&box,&l_nb_bytes_read,stream,p_manager)) {
1863 /* is it the codestream box ? */
1864 if (box.type == JP2_JP2C) {
1865 if (jp2->jp2_state & JP2_STATE_HEADER) {
1866 jp2->jp2_state |= JP2_STATE_CODESTREAM;
1867 opj_free(l_current_data);
1871 opj_event_msg_v2(p_manager, EVT_ERROR, "bad placed jpeg codestream\n");
1872 opj_free(l_current_data);
1876 else if (box.length == 0) {
1877 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot handle box of undefined sizes\n");
1878 opj_free(l_current_data);
1882 l_current_handler = opj_jp2_find_handler(box.type);
1883 l_current_data_size = box.length - l_nb_bytes_read;
1885 if (l_current_handler != 00) {
1886 if (l_current_data_size > l_last_data_size) {
1887 l_current_data = (unsigned char*)opj_realloc(l_current_data,l_current_data_size);
1888 if (!l_current_data){
1889 opj_free(l_current_data);
1892 l_last_data_size = l_current_data_size;
1895 l_nb_bytes_read = opj_stream_read_data(stream,l_current_data,l_current_data_size,p_manager);
1896 if (l_nb_bytes_read != l_current_data_size) {
1897 opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with reading JPEG2000 box, stream error\n");
1901 if (! l_current_handler->handler(jp2,l_current_data,l_current_data_size,p_manager)) {
1902 opj_free(l_current_data);
1907 jp2->jp2_state |= JP2_STATE_UNKNOWN;
1908 if (opj_stream_skip(stream,l_current_data_size,p_manager) != l_current_data_size) {
1909 opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with skipping JPEG2000 box, stream error\n");
1910 opj_free(l_current_data);
1916 opj_free(l_current_data);
1922 * Excutes the given procedures on the given codec.
1924 * @param p_procedure_list the list of procedures to execute
1925 * @param jp2 the jpeg2000 file codec to execute the procedures on.
1926 * @param stream the stream to execute the procedures on.
1927 * @param p_manager the user manager.
1929 * @return true if all the procedures were successfully executed.
1931 static opj_bool opj_jp2_exec ( opj_jp2_v2_t * jp2,
1932 opj_procedure_list_t * p_procedure_list,
1933 opj_stream_private_t *stream,
1934 opj_event_mgr_t * p_manager
1938 opj_bool (** l_procedure) (opj_jp2_v2_t * jp2, opj_stream_private_t *, opj_event_mgr_t *) = 00;
1939 opj_bool l_result = OPJ_TRUE;
1940 OPJ_UINT32 l_nb_proc, i;
1943 assert(p_procedure_list != 00);
1945 assert(stream != 00);
1946 assert(p_manager != 00);
1948 l_nb_proc = opj_procedure_list_get_nb_procedures(p_procedure_list);
1949 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);
1951 for (i=0;i<l_nb_proc;++i) {
1952 l_result = l_result && (*l_procedure) (jp2,stream,p_manager);
1956 /* and clear the procedure list at the end. */
1957 opj_procedure_list_clear(p_procedure_list);
1962 * Starts a compression scheme, i.e. validates the codec parameters, writes the header.
1964 * @param jp2 the jpeg2000 file codec.
1965 * @param cio the stream object.
1967 * @return true if the codec is valid.
1969 opj_bool opj_jp2_start_compress(opj_jp2_v2_t *jp2,
1970 opj_stream_private_t *stream,
1971 opj_image_t * p_image,
1972 opj_event_mgr_t * p_manager
1977 assert(stream != 00);
1978 assert(p_manager != 00);
1980 /* customization of the validation */
1981 opj_jp2_setup_encoding_validation (jp2);
1983 /* validation of the parameters codec */
1984 if (! opj_jp2_exec(jp2,jp2->m_validation_list,stream,p_manager)) {
1988 /* customization of the encoding */
1989 opj_jp2_setup_header_writting(jp2);
1992 if (! opj_jp2_exec (jp2,jp2->m_procedure_list,stream,p_manager)) {
1996 return j2k_start_compress(jp2->j2k,stream,p_image,p_manager);
2000 * Finds the execution function related to the given box id.
2002 * @param p_id the id of the handler to fetch.
2004 * @return the given handler or 00 if it could not be found.
2006 const opj_jp2_header_handler_t * opj_jp2_find_handler (OPJ_UINT32 p_id)
2008 OPJ_UINT32 i, l_handler_size = sizeof(jp2_header) / sizeof(opj_jp2_header_handler_t);
2010 for (i=0;i<l_handler_size;++i) {
2011 if (jp2_header[i].id == p_id) {
2012 return &jp2_header[i];
2019 * Finds the image execution function related to the given box id.
2021 * @param p_id the id of the handler to fetch.
2023 * @return the given handler or 00 if it could not be found.
2025 static const opj_jp2_header_handler_t * opj_jp2_img_find_handler (OPJ_UINT32 p_id)
2027 OPJ_UINT32 i, l_handler_size = sizeof(jp2_img_header) / sizeof(opj_jp2_header_handler_t);
2028 for (i=0;i<l_handler_size;++i)
2030 if (jp2_img_header[i].id == p_id) {
2031 return &jp2_img_header[i];
2040 * Reads a jpeg2000 file signature box.
2042 * @param p_header_data the data contained in the signature box.
2043 * @param jp2 the jpeg2000 file codec.
2044 * @param p_header_size the size of the data contained in the signature box.
2045 * @param p_manager the user event manager.
2047 * @return true if the file signature box is valid.
2049 static opj_bool opj_jp2_read_jp(opj_jp2_v2_t *jp2,
2050 OPJ_BYTE * p_header_data,
2051 OPJ_UINT32 p_header_size,
2052 opj_event_mgr_t * p_manager
2056 unsigned int l_magic_number;
2059 assert(p_header_data != 00);
2061 assert(p_manager != 00);
2063 if (jp2->jp2_state != JP2_STATE_NONE) {
2064 opj_event_msg_v2(p_manager, EVT_ERROR, "The signature box must be the first box in the file.\n");
2068 /* assure length of data is correct (4 -> magic number) */
2069 if (p_header_size != 4) {
2070 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with JP signature Box size\n");
2074 /* rearrange data */
2075 opj_read_bytes(p_header_data,&l_magic_number,4);
2076 if (l_magic_number != 0x0d0a870a ) {
2077 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with JP Signature : bad magic number\n");
2081 jp2->jp2_state |= JP2_STATE_SIGNATURE;
2088 * Reads a a FTYP box - File type box
2090 * @param p_header_data the data contained in the FTYP box.
2091 * @param jp2 the jpeg2000 file codec.
2092 * @param p_header_size the size of the data contained in the FTYP box.
2093 * @param p_manager the user event manager.
2095 * @return true if the FTYP box is valid.
2097 static opj_bool opj_jp2_read_ftyp( opj_jp2_v2_t *jp2,
2098 OPJ_BYTE * p_header_data,
2099 OPJ_UINT32 p_header_size,
2100 opj_event_mgr_t * p_manager
2103 OPJ_UINT32 i, l_remaining_bytes;
2106 assert(p_header_data != 00);
2108 assert(p_manager != 00);
2110 if (jp2->jp2_state != JP2_STATE_SIGNATURE) {
2111 opj_event_msg_v2(p_manager, EVT_ERROR, "The ftyp box must be the second box in the file.\n");
2115 /* assure length of data is correct */
2116 if (p_header_size < 8) {
2117 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with FTYP signature Box size\n");
2121 opj_read_bytes(p_header_data,&jp2->brand,4); /* BR */
2124 opj_read_bytes(p_header_data,&jp2->minversion,4); /* MinV */
2127 l_remaining_bytes = p_header_size - 8;
2129 /* the number of remaining bytes should be a multiple of 4 */
2130 if ((l_remaining_bytes & 0x3) != 0) {
2131 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with FTYP signature Box size\n");
2136 jp2->numcl = l_remaining_bytes >> 2;
2138 jp2->cl = (unsigned int *) opj_malloc(jp2->numcl * sizeof(unsigned int));
2139 if (jp2->cl == 00) {
2140 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory with FTYP Box\n");
2143 memset(jp2->cl,0,jp2->numcl * sizeof(unsigned int));
2146 for (i = 0; i < jp2->numcl; ++i)
2148 opj_read_bytes(p_header_data,&jp2->cl[i],4); /* CLi */
2152 jp2->jp2_state |= JP2_STATE_FILE_TYPE;
2158 * Skips the Jpeg2000 Codestream Header box - JP2C Header box.
2160 * @param cio the stream to write data to.
2161 * @param jp2 the jpeg2000 file codec.
2162 * @param p_manager user event manager.
2164 * @return true if writting was successful.
2166 opj_bool opj_jp2_skip_jp2c( opj_jp2_v2_t *jp2,
2167 opj_stream_private_t *stream,
2168 opj_event_mgr_t * p_manager )
2172 assert(stream != 00);
2173 assert(p_manager != 00);
2175 jp2->j2k_codestream_offset = opj_stream_tell(stream);
2177 if (opj_stream_skip(stream,8,p_manager) != 8) {
2185 * Reads the Jpeg2000 file Header box - JP2 Header box (warning, this is a super box).
2187 * @param p_header_data the data contained in the file header box.
2188 * @param jp2 the jpeg2000 file codec.
2189 * @param p_header_size the size of the data contained in the file header box.
2190 * @param p_manager the user event manager.
2192 * @return true if the JP2 Header box was successfully reconized.
2194 static opj_bool opj_jp2_read_jp2h( opj_jp2_v2_t *jp2,
2195 OPJ_BYTE *p_header_data,
2196 OPJ_UINT32 p_header_size,
2197 opj_event_mgr_t * p_manager
2200 OPJ_UINT32 l_box_size=0, l_current_data_size = 0;
2202 const opj_jp2_header_handler_t * l_current_handler;
2205 assert(p_header_data != 00);
2207 assert(p_manager != 00);
2209 /* make sure the box is well placed */
2210 if ((jp2->jp2_state & JP2_STATE_FILE_TYPE) != JP2_STATE_FILE_TYPE ) {
2211 opj_event_msg_v2(p_manager, EVT_ERROR, "The box must be the first box in the file.\n");
2215 jp2->jp2_img_state = JP2_IMG_STATE_NONE;
2217 /* iterate while remaining data */
2218 while (p_header_size > 0) {
2220 if (! opj_jp2_read_boxhdr_char(&box,p_header_data,&l_box_size,p_header_size, p_manager)) {
2221 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream error while reading JP2 Header box\n");
2225 if (box.length > p_header_size) {
2226 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream error while reading JP2 Header box: box length is inconsistent.\n");
2230 l_current_handler = opj_jp2_img_find_handler(box.type);
2231 l_current_data_size = box.length - l_box_size;
2232 p_header_data += l_box_size;
2234 if (l_current_handler != 00) {
2235 if (! l_current_handler->handler(jp2,p_header_data,l_current_data_size,p_manager)) {
2240 jp2->jp2_img_state |= JP2_IMG_STATE_UNKNOWN;
2243 p_header_data += l_current_data_size;
2244 p_header_size -= box.length;
2247 jp2->jp2_state |= JP2_STATE_HEADER;
2253 * Reads a box header. The box is the way data is packed inside a jpeg2000 file structure. Data is read from a character string
2255 * @param p_data the character string to read data from.
2256 * @param box the box structure to fill.
2257 * @param p_number_bytes_read pointer to an int that will store the number of bytes read from the stream (shoul usually be 2).
2258 * @param p_box_max_size the maximum number of bytes in the box.
2260 * @return true if the box is reconized, false otherwise
2262 opj_bool opj_jp2_read_boxhdr_char( opj_jp2_box_t *box,
2264 OPJ_UINT32 * p_number_bytes_read,
2265 OPJ_UINT32 p_box_max_size,
2266 opj_event_mgr_t * p_manager
2272 assert(p_data != 00);
2274 assert(p_number_bytes_read != 00);
2275 assert(p_manager != 00);
2277 if (p_box_max_size < 8) {
2278 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot handle box of less than 8 bytes\n");
2282 /* process read data */
2283 opj_read_bytes(p_data, &l_value, 4);
2285 box->length = (OPJ_INT32)(l_value);
2287 opj_read_bytes(p_data, &l_value, 4);
2289 box->type = (OPJ_INT32)(l_value);
2291 *p_number_bytes_read = 8;
2293 /* do we have a "special very large box ?" */
2294 /* read then the XLBox */
2295 if (box->length == 1) {
2296 unsigned int l_xl_part_size;
2298 if (p_box_max_size < 16) {
2299 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot handle XL box of less than 16 bytes\n");
2303 opj_read_bytes(p_data,&l_xl_part_size, 4);
2305 *p_number_bytes_read += 4;
2307 if (l_xl_part_size != 0) {
2308 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot handle box sizes higher than 2^32\n");
2312 opj_read_bytes(p_data, &l_value, 4);
2313 *p_number_bytes_read += 4;
2314 box->length = (OPJ_INT32)(l_value);
2316 if (box->length == 0) {
2317 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot handle box of undefined sizes\n");
2321 else if (box->length == 0) {
2322 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot handle box of undefined sizes\n");
2331 * Reads a jpeg2000 file header structure.
2333 * @param cio the stream to read data from.
2334 * @param jp2 the jpeg2000 file header structure.
2335 * @param p_manager the user event manager.
2337 * @return true if the box is valid.
2339 opj_bool opj_jp2_read_header( opj_stream_private_t *p_stream,
2341 opj_image_t ** p_image,
2342 opj_event_mgr_t * p_manager
2347 assert(p_stream != 00);
2348 assert(p_manager != 00);
2350 /* customization of the validation */
2351 opj_jp2_setup_decoding_validation (jp2);
2353 /* customization of the encoding */
2354 opj_jp2_setup_header_reading(jp2);
2356 /* validation of the parameters codec */
2357 if (! opj_jp2_exec(jp2,jp2->m_validation_list,p_stream,p_manager)) {
2362 if (! opj_jp2_exec (jp2,jp2->m_procedure_list,p_stream,p_manager)) {
2366 return j2k_read_header( p_stream,
2373 * Sets up the validation ,i.e. adds the procedures to launch to make sure the codec parameters
2374 * are valid. Developers wanting to extend the library can add their own validation procedures.
2376 void opj_jp2_setup_encoding_validation (opj_jp2_v2_t *jp2)
2381 opj_procedure_list_add_procedure(jp2->m_validation_list, (opj_procedure)opj_jp2_default_validation);
2382 /* DEVELOPER CORNER, add your custom validation procedure */
2386 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
2387 * are valid. Developpers wanting to extend the library can add their own validation procedures.
2389 void opj_jp2_setup_decoding_validation (opj_jp2_v2_t *jp2)
2393 /* DEVELOPER CORNER, add your custom validation procedure */
2397 * Sets up the procedures to do on writting header.
2398 * Developers wanting to extend the library can add their own writing procedures.
2400 void opj_jp2_setup_header_writting (opj_jp2_v2_t *jp2)
2405 opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)opj_jp2_write_jp );
2406 opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)opj_jp2_write_ftyp );
2407 opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)opj_jp2_write_jp2h );
2408 opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)opj_jp2_skip_jp2c );
2410 /* DEVELOPER CORNER, insert your custom procedures */
2415 * Sets up the procedures to do on reading header.
2416 * Developpers wanting to extend the library can add their own writting procedures.
2418 void opj_jp2_setup_header_reading (opj_jp2_v2_t *jp2)
2423 opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)opj_jp2_read_header_procedure );
2424 /* DEVELOPER CORNER, add your custom procedures */
2429 * Reads a tile header.
2430 * @param p_j2k the jpeg2000 codec.
2431 * @param p_stream the stream to write data to.
2432 * @param p_manager the user event manager.
2434 opj_bool opj_jp2_read_tile_header ( opj_jp2_v2_t * p_jp2,
2435 OPJ_UINT32 * p_tile_index,
2436 OPJ_UINT32 * p_data_size,
2437 OPJ_INT32 * p_tile_x0,
2438 OPJ_INT32 * p_tile_y0,
2439 OPJ_INT32 * p_tile_x1,
2440 OPJ_INT32 * p_tile_y1,
2441 OPJ_UINT32 * p_nb_comps,
2443 opj_stream_private_t *p_stream,
2444 opj_event_mgr_t * p_manager
2447 return j2k_read_tile_header(p_jp2->j2k,
2450 p_tile_x0, p_tile_y0,
2451 p_tile_x1, p_tile_y1,
2460 * @param p_j2k the jpeg2000 codec.
2461 * @param p_stream the stream to write data to.
2462 * @param p_manager the user event manager.
2464 opj_bool opj_jp2_write_tile ( opj_jp2_v2_t *p_jp2,
2465 OPJ_UINT32 p_tile_index,
2467 OPJ_UINT32 p_data_size,
2468 opj_stream_private_t *p_stream,
2469 opj_event_mgr_t * p_manager
2473 return j2k_write_tile (p_jp2->j2k,p_tile_index,p_data,p_data_size,p_stream,p_manager);
2478 * @param p_j2k the jpeg2000 codec.
2479 * @param p_stream the stream to write data to.
2480 * @param p_manager the user event manager.
2482 opj_bool opj_jp2_decode_tile ( opj_jp2_v2_t * p_jp2,
2483 OPJ_UINT32 p_tile_index,
2485 OPJ_UINT32 p_data_size,
2486 opj_stream_private_t *p_stream,
2487 opj_event_mgr_t * p_manager
2490 return j2k_decode_tile (p_jp2->j2k,p_tile_index,p_data,p_data_size,p_stream,p_manager);
2494 * Destroys a jpeg2000 file decompressor.
2496 * @param jp2 a jpeg2000 file decompressor.
2498 void opj_jp2_destroy(opj_jp2_v2_t *jp2)
2501 /* destroy the J2K codec */
2502 j2k_destroy(jp2->j2k);
2506 opj_free(jp2->comps);
2515 if (jp2->color.icc_profile_buf) {
2516 opj_free(jp2->color.icc_profile_buf);
2517 jp2->color.icc_profile_buf = 00;
2520 if (jp2->color.jp2_cdef) {
2521 if (jp2->color.jp2_cdef->info) {
2522 opj_free(jp2->color.jp2_cdef->info);
2523 jp2->color.jp2_cdef->info = NULL;
2526 opj_free(jp2->color.jp2_cdef);
2527 jp2->color.jp2_cdef = 00;
2530 if (jp2->color.jp2_pclr) {
2531 if (jp2->color.jp2_pclr->cmap) {
2532 opj_free(jp2->color.jp2_pclr->cmap);
2533 jp2->color.jp2_pclr->cmap = NULL;
2535 if (jp2->color.jp2_pclr->channel_sign) {
2536 opj_free(jp2->color.jp2_pclr->channel_sign);
2537 jp2->color.jp2_pclr->channel_sign = NULL;
2539 if (jp2->color.jp2_pclr->channel_size) {
2540 opj_free(jp2->color.jp2_pclr->channel_size);
2541 jp2->color.jp2_pclr->channel_size = NULL;
2543 if (jp2->color.jp2_pclr->entries) {
2544 opj_free(jp2->color.jp2_pclr->entries);
2545 jp2->color.jp2_pclr->entries = NULL;
2548 opj_free(jp2->color.jp2_pclr);
2549 jp2->color.jp2_pclr = 00;
2552 if (jp2->m_validation_list) {
2553 opj_procedure_list_destroy(jp2->m_validation_list);
2554 jp2->m_validation_list = 00;
2557 if (jp2->m_procedure_list) {
2558 opj_procedure_list_destroy(jp2->m_procedure_list);
2559 jp2->m_procedure_list = 00;
2567 * Sets the given area to be decoded. This function should be called right after opj_read_header and before any tile header reading.
2569 * @param p_jp2 the jpeg2000 codec.
2570 * @param p_end_x the right position of the rectangle to decode (in image coordinates).
2571 * @param p_start_y the up position of the rectangle to decode (in image coordinates).
2572 * @param p_end_y the bottom position of the rectangle to decode (in image coordinates).
2573 * @param p_manager the user event manager
2575 * @return true if the area could be set.
2577 opj_bool opj_jp2_set_decode_area( opj_jp2_v2_t *p_jp2,
2578 opj_image_t* p_image,
2579 OPJ_INT32 p_start_x, OPJ_INT32 p_start_y,
2580 OPJ_INT32 p_end_x, OPJ_INT32 p_end_y,
2581 opj_event_mgr_t * p_manager
2584 return j2k_set_decode_area(p_jp2->j2k, p_image, p_start_x, p_start_y, p_end_x, p_end_y, p_manager);
2588 * Get the decoded tile.
2590 * @param jp2 the jpeg2000 codec.
2591 * @param p_stream input_stream
2592 * @param p_image output image. .
2593 * @param p_manager the user event manager
2594 * @param tile_index index of the tile we want decode
2596 * @return true if succeed.
2598 opj_bool opj_jp2_get_tile( opj_jp2_v2_t *p_jp2,
2599 opj_stream_private_t *p_stream,
2600 opj_image_t* p_image,
2601 opj_event_mgr_t * p_manager,
2602 OPJ_UINT32 tile_index
2608 opj_event_msg_v2(p_manager, EVT_WARNING, "JP2 box which are after the codestream will not be read by this function.\n");
2610 if (! j2k_get_tile(p_jp2->j2k, p_stream, p_image, p_manager, tile_index) ){
2611 opj_event_msg_v2(p_manager, EVT_ERROR, "Failed to decode the codestream in the JP2 file\n");
2615 /* Set Image Color Space */
2616 if (p_jp2->enumcs == 16)
2617 p_image->color_space = CLRSPC_SRGB;
2618 else if (p_jp2->enumcs == 17)
2619 p_image->color_space = CLRSPC_GRAY;
2620 else if (p_jp2->enumcs == 18)
2621 p_image->color_space = CLRSPC_SYCC;
2623 p_image->color_space = CLRSPC_UNKNOWN;
2625 /* Apply the color space if needed */
2626 if(p_jp2->color.jp2_cdef) {
2627 opj_jp2_apply_cdef(p_image, &(p_jp2->color));
2630 if(p_jp2->color.jp2_pclr) {
2631 /* Part 1, I.5.3.4: Either both or none : */
2632 if( !p_jp2->color.jp2_pclr->cmap)
2633 opj_jp2_free_pclr(&(p_jp2->color));
2635 opj_jp2_apply_pclr(p_image, &(p_jp2->color));
2638 if(p_jp2->color.icc_profile_buf) {
2639 p_image->icc_profile_buf = p_jp2->color.icc_profile_buf;
2640 p_image->icc_profile_len = p_jp2->color.icc_profile_len;
2641 p_jp2->color.icc_profile_buf = NULL;
2649 /* ----------------------------------------------------------------------- */
2650 /* JP2 encoder interface */
2651 /* ----------------------------------------------------------------------- */
2653 opj_jp2_v2_t* opj_jp2_create(opj_bool p_is_decoder)
2655 opj_jp2_v2_t *jp2 = (opj_jp2_v2_t*)opj_malloc(sizeof(opj_jp2_v2_t));
2657 memset(jp2,0,sizeof(opj_jp2_v2_t));
2659 /* create the J2K codec */
2660 if (! p_is_decoder) {
2661 jp2->j2k = j2k_create_compress_v2();
2664 jp2->j2k = opj_j2k_create_decompress();
2667 if (jp2->j2k == 00) {
2668 opj_jp2_destroy(jp2);
2672 /* Color structure */
2673 jp2->color.icc_profile_buf = NULL;
2674 jp2->color.icc_profile_len = 0;
2675 jp2->color.jp2_cdef = NULL;
2676 jp2->color.jp2_pclr = NULL;
2677 jp2->color.jp2_has_colr = 0;
2679 /* validation list creation */
2680 jp2->m_validation_list = opj_procedure_list_create();
2681 if (! jp2->m_validation_list) {
2682 opj_jp2_destroy(jp2);
2686 /* execution list creation */
2687 jp2->m_procedure_list = opj_procedure_list_create();
2688 if (! jp2->m_procedure_list) {
2689 opj_jp2_destroy(jp2);
2697 void jp2_dump(opj_jp2_v2_t* p_jp2, OPJ_INT32 flag, FILE* out_stream)
2700 assert(p_jp2 != 00);
2702 j2k_dump(p_jp2->j2k,
2707 opj_codestream_index_t* jp2_get_cstr_index(opj_jp2_v2_t* p_jp2)
2709 return j2k_get_cstr_index(p_jp2->j2k);
2712 opj_codestream_info_v2_t* jp2_get_cstr_info(opj_jp2_v2_t* p_jp2)
2714 return j2k_get_cstr_info(p_jp2->j2k);
2717 opj_bool opj_jp2_set_decoded_resolution_factor(opj_jp2_v2_t *p_jp2,
2718 OPJ_UINT32 res_factor,
2719 opj_event_mgr_t * p_manager)
2721 return j2k_set_decoded_resolution_factor(p_jp2->j2k, res_factor, p_manager);