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 */
44 @param cinfo Codec context info
45 @param cio Input stream
47 @return Returns true if successful, returns false otherwise
49 static opj_bool jp2_read_boxhdr(opj_common_ptr cinfo, opj_cio_t *cio, opj_jp2_box_t *box);
50 /*static void jp2_write_url(opj_cio_t *cio, char *Idx_file);*/
52 Read the IHDR box - Image Header box
54 @param cio Input buffer stream
55 @return Returns true if successful, returns false otherwise
57 static opj_bool jp2_read_ihdr(opj_jp2_t *jp2, opj_cio_t *cio);
60 * Reads a IHDR box - Image Header box
62 * @param p_image_header_data pointer to actual data (already read from file)
63 * @param jp2 the jpeg2000 file codec.
64 * @param p_image_header_size the size of the image header
65 * @param p_manager the user event manager.
67 * @return true if the image header is valid, false else.
69 static opj_bool jp2_read_ihdr_v2( opj_jp2_v2_t *jp2,
70 OPJ_BYTE *p_image_header_data,
71 OPJ_INT32 p_image_header_size,
72 opj_event_mgr_t * p_manager );
74 static void jp2_write_ihdr(opj_jp2_t *jp2, opj_cio_t *cio);
77 * Writes the Image Header box - Image Header box.
79 * @param jp2 jpeg2000 file codec.
80 * @param p_nb_bytes_written pointer to store the nb of bytes written by the function.
82 * @return the data being copied.
84 static unsigned char * jp2_write_ihdr_v2( opj_jp2_v2_t *jp2,
85 unsigned int * p_nb_bytes_written );
87 static void jp2_write_bpcc(opj_jp2_t *jp2, opj_cio_t *cio);
90 * Writes the Bit per Component box.
92 * @param jp2 jpeg2000 file codec.
93 * @param p_nb_bytes_written pointer to store the nb of bytes written by the function.
95 * @return the data being copied.
97 static unsigned char * jp2_write_bpcc_v2( opj_jp2_v2_t *jp2,
98 unsigned int * p_nb_bytes_written );
100 static opj_bool jp2_read_bpcc(opj_jp2_t *jp2, opj_cio_t *cio);
103 * Reads a Bit per Component box.
105 * @param p_bpc_header_data pointer to actual data (already read from file)
106 * @param jp2 the jpeg2000 file codec.
107 * @param p_bpc_header_size the size of the bpc header
108 * @param p_manager the user event manager.
110 * @return true if the bpc header is valid, fale else.
112 static opj_bool jp2_read_bpcc_v2(
114 unsigned char * p_bpc_header_data,
115 unsigned int p_bpc_header_size,
116 struct opj_event_mgr * p_manager
119 static opj_bool jp2_read_cdef_v2( opj_jp2_v2_t * jp2,
120 unsigned char * p_cdef_header_data,
121 OPJ_UINT32 p_cdef_header_size,
122 opj_event_mgr_t * p_manager
125 static void jp2_write_colr(opj_jp2_t *jp2, opj_cio_t *cio);
127 Write the FTYP box - File type box
128 @param jp2 JP2 handle
129 @param cio Output buffer stream
133 * Writes the Colour Specification box.
135 * @param jp2 jpeg2000 file codec.
136 * @param p_nb_bytes_written pointer to store the nb of bytes written by the function.
138 * @return the data being copied.
140 static unsigned char *jp2_write_colr_v2(opj_jp2_v2_t *jp2,
141 unsigned int * p_nb_bytes_written );
143 static void jp2_write_ftyp(opj_jp2_t *jp2, opj_cio_t *cio);
145 Read the FTYP box - File type box
146 @param jp2 JP2 handle
147 @param cio Input buffer stream
148 @return Returns true if successful, returns false otherwise
150 static opj_bool jp2_read_ftyp(opj_jp2_t *jp2, opj_cio_t *cio);
153 * Writes a FTYP box - File type box
155 * @param cio the stream to write data to.
156 * @param jp2 the jpeg2000 file codec.
157 * @param p_manager the user event manager.
159 * @return true if writting was successful.
161 static opj_bool jp2_write_ftyp_v2( opj_jp2_v2_t *jp2,
162 struct opj_stream_private *cio,
163 struct opj_event_mgr * p_manager );
166 * Reads a a FTYP box - File type box
168 * @param p_header_data the data contained in the FTYP box.
169 * @param jp2 the jpeg2000 file codec.
170 * @param p_header_size the size of the data contained in the FTYP box.
171 * @param p_manager the user event manager.
173 * @return true if the FTYP box is valid.
175 static opj_bool jp2_read_ftyp_v2( opj_jp2_v2_t *jp2,
176 unsigned char * p_header_data,
177 OPJ_UINT32 p_header_size,
178 struct opj_event_mgr * p_manager );
181 * Skips the Jpeg2000 Codestream Header box - JP2C Header box.
183 * @param cio the stream to write data to.
184 * @param jp2 the jpeg2000 file codec.
185 * @param p_manager user event manager.
187 * @return true if writting was successful.
189 opj_bool jp2_skip_jp2c( opj_jp2_v2_t *jp2,
190 struct opj_stream_private *cio,
191 struct opj_event_mgr * p_manager );
194 * Reads the Jpeg2000 file Header box - JP2 Header box (warning, this is a super box).
196 * @param p_header_data the data contained in the file header box.
197 * @param jp2 the jpeg2000 file codec.
198 * @param p_header_size the size of the data contained in the file header box.
199 * @param p_manager the user event manager.
201 * @return true if the JP2 Header box was successfully reconized.
203 static opj_bool jp2_read_jp2h_v2(
205 unsigned char * p_header_data,
206 unsigned int p_header_size,
207 struct opj_event_mgr * p_manager
210 static int jp2_write_jp2c(opj_jp2_t *jp2, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info);
214 * Writes the Jpeg2000 codestream Header box - JP2C Header box. This function must be called AFTER the coding has been done.
216 * @param cio the stream to write data to.
217 * @param jp2 the jpeg2000 file codec.
218 * @param p_manager user event manager.
220 * @return true if writting was successful.
222 static opj_bool jp2_write_jp2c_v2( opj_jp2_v2_t *jp2,
223 struct opj_stream_private *cio,
224 struct opj_event_mgr * p_manager );
226 static opj_bool jp2_read_jp2c(opj_jp2_t *jp2, opj_cio_t *cio, unsigned int *j2k_codestream_length, unsigned int *j2k_codestream_offset);
227 static void jp2_write_jp(opj_cio_t *cio);
229 Read the JP box - JPEG 2000 signature
230 @param jp2 JP2 handle
231 @param cio Input buffer stream
232 @return Returns true if successful, returns false otherwise
234 static opj_bool jp2_read_jp(opj_jp2_t *jp2, opj_cio_t *cio);
237 * Reads a jpeg2000 file signature box.
239 * @param p_header_data the data contained in the signature box.
240 * @param jp2 the jpeg2000 file codec.
241 * @param p_header_size the size of the data contained in the signature box.
242 * @param p_manager the user event manager.
244 * @return true if the file signature box is valid.
246 static opj_bool jp2_read_jp_v2(
248 unsigned char * p_header_data,
249 unsigned int p_header_size,
250 struct opj_event_mgr * p_manager
254 Decode the structure of a JP2 file
255 @param jp2 JP2 handle
256 @param cio Input buffer stream
257 @param color Collector for profile, cdef and pclr data
258 @return Returns true if successful, returns false otherwise
260 static opj_bool jp2_read_struct(opj_jp2_t *jp2, opj_cio_t *cio,
261 opj_jp2_color_t *color);
263 Apply collected palette data
264 @param color Collector for profile, cdef and pclr data
267 static void jp2_apply_pclr(opj_image_t *image, opj_jp2_color_t *color);
270 @param jp2 JP2 handle
271 @param cio Input buffer stream
273 @param color Collector for profile, cdef and pclr data
274 @return Returns true if successful, returns false otherwise
276 static opj_bool jp2_read_pclr(opj_jp2_t *jp2, opj_cio_t *cio,
277 opj_jp2_box_t *box, opj_jp2_color_t *color);
279 static opj_bool jp2_read_pclr_v2( opj_jp2_v2_t *jp2,
280 unsigned char * p_pclr_header_data,
281 OPJ_UINT32 p_pclr_header_size,
282 opj_event_mgr_t * p_manager
286 Collect component mapping data
287 @param jp2 JP2 handle
288 @param cio Input buffer stream
290 @param color Collector for profile, cdef and pclr data
291 @return Returns true if successful, returns false otherwise
293 static opj_bool jp2_read_cmap(opj_jp2_t *jp2, opj_cio_t *cio,
294 opj_jp2_box_t *box, opj_jp2_color_t *color);
297 static opj_bool jp2_read_cmap_v2( opj_jp2_v2_t * jp2,
298 unsigned char * p_cmap_header_data,
299 OPJ_UINT32 p_cmap_header_size,
300 opj_event_mgr_t * p_manager
304 Collect colour specification data
305 @param jp2 JP2 handle
306 @param cio Input buffer stream
308 @param color Collector for profile, cdef and pclr data
309 @return Returns true if successful, returns false otherwise
311 static opj_bool jp2_read_colr(opj_jp2_t *jp2, opj_cio_t *cio,
312 opj_jp2_box_t *box, opj_jp2_color_t *color);
315 * Reads the Color Specification box.
317 * @param p_colr_header_data pointer to actual data (already read from file)
318 * @param jp2 the jpeg2000 file codec.
319 * @param p_colr_header_size the size of the color header
320 * @param p_manager the user event manager.
322 * @return true if the bpc header is valid, fale else.
324 static opj_bool jp2_read_colr_v2(
326 unsigned char * p_colr_header_data,
327 OPJ_UINT32 p_colr_header_size,
328 struct opj_event_mgr * p_manager
332 Write file Index (superbox)
333 @param[in] offset_jp2c offset of jp2c box
334 @param[in] length_jp2c length of jp2c box
335 @param[in] offset_idx offset of cidx box
336 @param[in] length_idx length of cidx box
337 @param[in] cio file output handle
338 @return length of fidx box
340 static int write_fidx( int offset_jp2c, int length_jp2c, int offset_idx, int length_idx, opj_cio_t *cio);
342 Write index Finder box
343 @param[in] offset offset of fidx box
344 @param[in] length length of fidx box
345 @param[in] cio file output handle
347 static void write_iptr( int offset, int length, opj_cio_t *cio);
351 @param[in] offset_jp2c offset of jp2c box
352 @param[in] length_jp2c length of jp2c box
353 @param[in] offset_idx offset of cidx box
354 @param[in] length_idx length of cidx box
355 @param[in] cio file output handle
357 static void write_prxy( int offset_jp2c, int length_jp2c, int offset_idx, int length_idx, opj_cio_t *cio);
363 * Sets up the procedures to do on writting header after the codestream.
364 * Developpers wanting to extend the library can add their own writting procedures.
366 static void jp2_setup_end_header_writting (opj_jp2_v2_t *jp2);
370 * Sets up the procedures to do on reading header after the codestream.
371 * Developpers wanting to extend the library can add their own writting procedures.
373 static void jp2_setup_end_header_reading (opj_jp2_v2_t *jp2);
376 * Reads a jpeg2000 file header structure.
378 * @param cio the stream to read data from.
379 * @param jp2 the jpeg2000 file header structure.
380 * @param p_manager the user event manager.
382 * @return true if the box is valid.
384 opj_bool jp2_read_header_procedure(
386 struct opj_stream_private *cio,
387 struct opj_event_mgr * p_manager
391 * Excutes the given procedures on the given codec.
393 * @param p_procedure_list the list of procedures to execute
394 * @param jp2 the jpeg2000 file codec to execute the procedures on.
395 * @param cio the stream to execute the procedures on.
396 * @param p_manager the user manager.
398 * @return true if all the procedures were successfully executed.
400 static opj_bool jp2_exec (
402 struct opj_procedure_list * p_procedure_list,
403 struct opj_stream_private *cio,
404 struct opj_event_mgr * p_manager
408 * Reads a box header. The box is the way data is packed inside a jpeg2000 file structure.
410 * @param cio the input stream to read data from.
411 * @param box the box structure to fill.
412 * @param p_number_bytes_read pointer to an int that will store the number of bytes read from the stream (shoul usually be 2).
413 * @param p_manager user event manager.
415 * @return true if the box is reconized, false otherwise
417 static opj_bool jp2_read_boxhdr_v2(
419 OPJ_UINT32 * p_number_bytes_read,
420 struct opj_stream_private *cio,
421 struct opj_event_mgr * p_manager
425 * Finds the execution function related to the given box id.
427 * @param p_id the id of the handler to fetch.
429 * @return the given handler or NULL if it could not be found.
431 static const opj_jp2_header_handler_t * jp2_find_handler (int p_id );
434 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
435 * are valid. Developpers wanting to extend the library can add their own validation procedures.
437 static void jp2_setup_encoding_validation (opj_jp2_v2_t *jp2);
441 * Sets up the procedures to do on writting header. Developpers wanting to extend the library can add their own writting procedures.
443 static void jp2_setup_header_writting (opj_jp2_v2_t *jp2);
446 * The default validation procedure without any extension.
448 * @param jp2 the jpeg2000 codec to validate.
449 * @param cio the input stream to validate.
450 * @param p_manager the user event manager.
452 * @return true if the parameters are correct.
454 opj_bool jp2_default_validation ( opj_jp2_v2_t * jp2,
455 struct opj_stream_private *cio,
456 struct opj_event_mgr * p_manager );
459 * Finds the image execution function related to the given box id.
461 * @param p_id the id of the handler to fetch.
463 * @return the given handler or NULL if it could not be found.
465 static const opj_jp2_header_handler_t * jp2_img_find_handler (int p_id);
467 const opj_jp2_header_handler_t jp2_header [] =
469 {JP2_JP,jp2_read_jp_v2},
470 {JP2_FTYP,jp2_read_ftyp_v2},
471 {JP2_JP2H,jp2_read_jp2h_v2}
474 const opj_jp2_header_handler_t jp2_img_header [] =
476 {JP2_IHDR,jp2_read_ihdr_v2},
477 {JP2_COLR,jp2_read_colr_v2},
478 {JP2_BPCC,jp2_read_bpcc_v2},
479 {JP2_PCLR,jp2_read_pclr_v2},
480 {JP2_CMAP,jp2_read_cmap_v2},
481 {JP2_CDEF,jp2_read_cdef_v2}
486 * Reads a box header. The box is the way data is packed inside a jpeg2000 file structure. Data is read from a character string
488 * @param p_data the character string to read data from.
489 * @param box the box structure to fill.
490 * @param p_number_bytes_read pointer to an int that will store the number of bytes read from the stream (shoul usually be 2).
491 * @param p_box_max_size the maximum number of bytes in the box.
493 * @return true if the box is reconized, false otherwise
495 static opj_bool jp2_read_boxhdr_char(
498 OPJ_UINT32 * p_number_bytes_read,
499 OPJ_UINT32 p_box_max_size,
500 struct opj_event_mgr * p_manager
504 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
505 * are valid. Developpers wanting to extend the library can add their own validation procedures.
507 static void jp2_setup_decoding_validation (opj_jp2_v2_t *jp2);
510 * Sets up the procedures to do on reading header.
511 * Developpers wanting to extend the library can add their own writting procedures.
513 static void jp2_setup_header_reading (opj_jp2_v2_t *jp2);
517 /* ----------------------------------------------------------------------- */
519 static opj_bool jp2_read_boxhdr(opj_common_ptr cinfo, opj_cio_t *cio, opj_jp2_box_t *box) {
520 box->init_pos = cio_tell(cio);
521 box->length = cio_read(cio, 4);
522 box->type = cio_read(cio, 4);
523 if (box->length == 1) {
524 if (cio_read(cio, 4) != 0) {
525 opj_event_msg(cinfo, EVT_ERROR, "Cannot handle box sizes higher than 2^32\n");
528 box->length = cio_read(cio, 4);
529 if (box->length == 0)
530 box->length = cio_numbytesleft(cio) + 12;
532 else if (box->length == 0) {
533 box->length = cio_numbytesleft(cio) + 8;
540 * Reads a box header. The box is the way data is packed inside a jpeg2000 file structure.
542 * @param cio the input stream to read data from.
543 * @param box the box structure to fill.
544 * @param p_number_bytes_read pointer to an int that will store the number of bytes read from the stream (should usually be 8).
545 * @param p_manager user event manager.
547 * @return true if the box is reconized, false otherwise
549 opj_bool jp2_read_boxhdr_v2(opj_jp2_box_t *box, OPJ_UINT32 * p_number_bytes_read, opj_stream_private_t *cio, opj_event_mgr_t * p_manager)
551 /* read header from file */
552 unsigned char l_data_header [8];
557 assert(p_number_bytes_read != 00);
558 assert(p_manager != 00);
560 *p_number_bytes_read = opj_stream_read_data(cio,l_data_header,8,p_manager);
561 if (*p_number_bytes_read != 8) {
565 /* process read data */
566 opj_read_bytes(l_data_header,&(box->length), 4);
567 opj_read_bytes(l_data_header+4,&(box->type), 4);
569 /* do we have a "special very large box ?" */
570 /* read then the XLBox */
571 if (box->length == 1) {
572 OPJ_UINT32 l_xl_part_size;
574 OPJ_UINT32 l_nb_bytes_read = opj_stream_read_data(cio,l_data_header,8,p_manager);
575 if (l_nb_bytes_read != 8) {
576 if (l_nb_bytes_read > 0) {
577 *p_number_bytes_read += l_nb_bytes_read;
583 opj_read_bytes(l_data_header,&l_xl_part_size, 4);
584 if (l_xl_part_size != 0) {
585 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot handle box sizes higher than 2^32\n");
588 opj_read_bytes(l_data_header,&(box->length), 4);
594 static void jp2_write_url(opj_cio_t *cio, char *Idx_file) {
598 box.init_pos = cio_tell(cio);
600 cio_write(cio, JP2_URL, 4); /* DBTL */
601 cio_write(cio, 0, 1); /* VERS */
602 cio_write(cio, 0, 3); /* FLAG */
605 for (i = 0; i < strlen(Idx_file); i++) {
606 cio_write(cio, Idx_file[i], 1);
610 box.length = cio_tell(cio) - box.init_pos;
611 cio_seek(cio, box.init_pos);
612 cio_write(cio, box.length, 4); /* L */
613 cio_seek(cio, box.init_pos + box.length);
617 static opj_bool jp2_read_ihdr(opj_jp2_t *jp2, opj_cio_t *cio) {
620 opj_common_ptr cinfo = jp2->cinfo;
622 jp2_read_boxhdr(cinfo, cio, &box);
623 if (JP2_IHDR != box.type) {
624 opj_event_msg(cinfo, EVT_ERROR, "Expected IHDR Marker\n");
628 jp2->h = cio_read(cio, 4); /* HEIGHT */
629 jp2->w = cio_read(cio, 4); /* WIDTH */
630 jp2->numcomps = cio_read(cio, 2); /* NC */
631 jp2->comps = (opj_jp2_comps_t*) opj_malloc(jp2->numcomps * sizeof(opj_jp2_comps_t));
633 jp2->bpc = cio_read(cio, 1); /* BPC */
635 jp2->C = cio_read(cio, 1); /* C */
636 jp2->UnkC = cio_read(cio, 1); /* UnkC */
637 jp2->IPR = cio_read(cio, 1); /* IPR */
639 if (cio_tell(cio) - box.init_pos != box.length) {
640 opj_event_msg(cinfo, EVT_ERROR, "Error with IHDR Box\n");
648 * Reads a IHDR box - Image Header box
650 * @param jp2 the jpeg2000 file codec.
651 * @param p_image_header_data pointer to actual data (already read from file)
652 * @param p_image_header_size the size of the image header
653 * @param p_manager the user event manager.
655 * @return true if the image header is valid, fale else.
657 opj_bool jp2_read_ihdr_v2( opj_jp2_v2_t *jp2,
658 OPJ_BYTE *p_image_header_data,
659 OPJ_INT32 p_image_header_size,
660 opj_event_mgr_t * p_manager
664 assert(p_image_header_data != 00);
666 assert(p_manager != 00);
668 if (p_image_header_size != 14) {
669 opj_event_msg_v2(p_manager, EVT_ERROR, "Bad image header box (bad size)\n");
673 opj_read_bytes(p_image_header_data,&(jp2->h),4); /* HEIGHT */
674 p_image_header_data += 4;
675 opj_read_bytes(p_image_header_data,&(jp2->w),4); /* WIDTH */
676 p_image_header_data += 4;
677 opj_read_bytes(p_image_header_data,&(jp2->numcomps),2); /* NC */
678 p_image_header_data += 2;
680 /* allocate memory for components */
681 jp2->comps = (opj_jp2_comps_t*) opj_malloc(jp2->numcomps * sizeof(opj_jp2_comps_t));
682 if (jp2->comps == 0) {
683 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to handle image header (ihdr)\n");
686 memset(jp2->comps,0,jp2->numcomps * sizeof(opj_jp2_comps_t));
688 opj_read_bytes(p_image_header_data,&(jp2->bpc),1); /* BPC */
689 ++ p_image_header_data;
691 /* if equal to 0 then need a BPC box (cf. chapter about image header box of the norm) */
692 /*if (jp2->bpc == 0){
693 indicate with a flag that we will wait a BPC box
696 opj_read_bytes(p_image_header_data,&(jp2->C),1); /* C */
697 ++ p_image_header_data;
699 /* Should be equal to 7 cf. chapter about image header box of the norm */
701 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);
704 opj_read_bytes(p_image_header_data,&(jp2->UnkC),1); /* UnkC */
705 ++ p_image_header_data;
706 opj_read_bytes(p_image_header_data,&(jp2->IPR),1); /* IPR */
707 ++ p_image_header_data;
712 static void jp2_write_ihdr(opj_jp2_t *jp2, opj_cio_t *cio) {
715 box.init_pos = cio_tell(cio);
717 cio_write(cio, JP2_IHDR, 4); /* IHDR */
719 cio_write(cio, jp2->h, 4); /* HEIGHT */
720 cio_write(cio, jp2->w, 4); /* WIDTH */
721 cio_write(cio, jp2->numcomps, 2); /* NC */
723 cio_write(cio, jp2->bpc, 1); /* BPC */
725 cio_write(cio, jp2->C, 1); /* C : Always 7 */
726 cio_write(cio, jp2->UnkC, 1); /* UnkC, colorspace unknown */
727 cio_write(cio, jp2->IPR, 1); /* IPR, no intellectual property */
729 box.length = cio_tell(cio) - box.init_pos;
730 cio_seek(cio, box.init_pos);
731 cio_write(cio, box.length, 4); /* L */
732 cio_seek(cio, box.init_pos + box.length);
736 * Writes the Image Header box - Image Header box.
738 * @param jp2 jpeg2000 file codec.
739 * @param p_nb_bytes_written pointer to store the nb of bytes written by the function.
741 * @return the data being copied.
743 static unsigned char * jp2_write_ihdr_v2( opj_jp2_v2_t *jp2,
744 unsigned int * p_nb_bytes_written )
746 unsigned char * l_ihdr_data,* l_current_ihdr_ptr;
750 assert(p_nb_bytes_written != 00);
752 /* default image header is 22 bytes wide */
753 l_ihdr_data = (unsigned char *) opj_malloc(22);
754 if (l_ihdr_data == 00) {
757 memset(l_ihdr_data,0,22);
759 l_current_ihdr_ptr = l_ihdr_data;
761 opj_write_bytes(l_current_ihdr_ptr,22,4); /* write box size */
762 l_current_ihdr_ptr+=4;
764 opj_write_bytes(l_current_ihdr_ptr,JP2_IHDR, 4); /* IHDR */
765 l_current_ihdr_ptr+=4;
767 opj_write_bytes(l_current_ihdr_ptr,jp2->h, 4); /* HEIGHT */
768 l_current_ihdr_ptr+=4;
770 opj_write_bytes(l_current_ihdr_ptr, jp2->w, 4); /* WIDTH */
771 l_current_ihdr_ptr+=4;
773 opj_write_bytes(l_current_ihdr_ptr, jp2->numcomps, 2); /* NC */
774 l_current_ihdr_ptr+=2;
776 opj_write_bytes(l_current_ihdr_ptr, jp2->bpc, 1); /* BPC */
777 ++l_current_ihdr_ptr;
779 opj_write_bytes(l_current_ihdr_ptr, jp2->C, 1); /* C : Always 7 */
780 ++l_current_ihdr_ptr;
782 opj_write_bytes(l_current_ihdr_ptr, jp2->UnkC, 1); /* UnkC, colorspace unknown */
783 ++l_current_ihdr_ptr;
785 opj_write_bytes(l_current_ihdr_ptr, jp2->IPR, 1); /* IPR, no intellectual property */
786 ++l_current_ihdr_ptr;
788 *p_nb_bytes_written = 22;
793 static void jp2_write_bpcc(opj_jp2_t *jp2, opj_cio_t *cio) {
797 box.init_pos = cio_tell(cio);
799 cio_write(cio, JP2_BPCC, 4); /* BPCC */
801 for (i = 0; i < jp2->numcomps; i++) {
802 cio_write(cio, jp2->comps[i].bpcc, 1);
805 box.length = cio_tell(cio) - box.init_pos;
806 cio_seek(cio, box.init_pos);
807 cio_write(cio, box.length, 4); /* L */
808 cio_seek(cio, box.init_pos + box.length);
813 * Writes the Bit per Component box.
815 * @param jp2 jpeg2000 file codec.
816 * @param p_nb_bytes_written pointer to store the nb of bytes written by the function.
818 * @return the data being copied.
820 unsigned char * jp2_write_bpcc_v2( opj_jp2_v2_t *jp2,
821 unsigned int * p_nb_bytes_written )
824 /* room for 8 bytes for box and 1 byte for each component */
825 int l_bpcc_size = 8 + jp2->numcomps;
826 unsigned char * l_bpcc_data,* l_current_bpcc_ptr;
830 assert(p_nb_bytes_written != 00);
832 l_bpcc_data = (unsigned char *) opj_malloc(l_bpcc_size);
833 if (l_bpcc_data == 00) {
836 memset(l_bpcc_data,0,l_bpcc_size);
838 l_current_bpcc_ptr = l_bpcc_data;
840 opj_write_bytes(l_current_bpcc_ptr,l_bpcc_size,4); /* write box size */
841 l_current_bpcc_ptr += 4;
843 opj_write_bytes(l_current_bpcc_ptr,JP2_BPCC,4); /* BPCC */
844 l_current_bpcc_ptr += 4;
846 for (i = 0; i < jp2->numcomps; ++i) {
847 opj_write_bytes(l_current_bpcc_ptr, jp2->comps[i].bpcc, 1); /* write each component information */
848 ++l_current_bpcc_ptr;
851 *p_nb_bytes_written = l_bpcc_size;
858 static opj_bool jp2_read_bpcc(opj_jp2_t *jp2, opj_cio_t *cio) {
862 opj_common_ptr cinfo = jp2->cinfo;
864 jp2_read_boxhdr(cinfo, cio, &box);
865 if (JP2_BPCC != box.type) {
866 opj_event_msg(cinfo, EVT_ERROR, "Expected BPCC Marker\n");
870 for (i = 0; i < jp2->numcomps; i++) {
871 jp2->comps[i].bpcc = cio_read(cio, 1);
874 if (cio_tell(cio) - box.init_pos != box.length) {
875 opj_event_msg(cinfo, EVT_ERROR, "Error with BPCC Box\n");
883 * Reads a Bit per Component box.
885 * @param jp2 the jpeg2000 file codec.
886 * @param p_bpc_header_data pointer to actual data (already read from file)
887 * @param p_bpc_header_size pointer that will hold the size of the bpc header
888 * @param p_manager the user event manager.
890 * @return true if the bpc header is valid, false otherwise.
892 opj_bool jp2_read_bpcc_v2( opj_jp2_v2_t *jp2,
893 unsigned char * p_bpc_header_data,
894 unsigned int p_bpc_header_size,
895 opj_event_mgr_t * p_manager
901 assert(p_bpc_header_data != 00);
903 assert(p_manager != 00);
906 /*if (jp2->bpc != 0 ){
907 opj_event_msg_v2(p_manager, EVT_WARNING, "A BPCC header box is available although BPC is different to zero (%d)\n",jp2->bpc);
910 /* and length is relevant */
911 if (p_bpc_header_size != jp2->numcomps) {
912 opj_event_msg_v2(p_manager, EVT_ERROR, "Bad BPCC header box (bad size)\n");
916 /* read info for each component */
917 for (i = 0; i < jp2->numcomps; ++i) {
918 opj_read_bytes(p_bpc_header_data,&jp2->comps[i].bpcc ,1); /* read each BPCC component */
925 static void jp2_write_colr(opj_jp2_t *jp2, opj_cio_t *cio) {
928 box.init_pos = cio_tell(cio);
930 cio_write(cio, JP2_COLR, 4); /* COLR */
932 cio_write(cio, jp2->meth, 1); /* METH */
933 cio_write(cio, jp2->precedence, 1); /* PRECEDENCE */
934 cio_write(cio, jp2->approx, 1); /* APPROX */
939 cio_write(cio, jp2->enumcs, 4); /* EnumCS */
941 box.length = cio_tell(cio) - box.init_pos;
942 cio_seek(cio, box.init_pos);
943 cio_write(cio, box.length, 4); /* L */
944 cio_seek(cio, box.init_pos + box.length);
948 * Writes the Colour Specification box.
950 * @param jp2 jpeg2000 file codec.
951 * @param p_nb_bytes_written pointer to store the nb of bytes written by the function.
953 * @return the data being copied.
955 unsigned char *jp2_write_colr_v2( opj_jp2_v2_t *jp2,
956 unsigned int * p_nb_bytes_written )
958 /* room for 8 bytes for box 3 for common data and variable upon profile*/
959 unsigned int l_colr_size = 11;
960 unsigned char * l_colr_data,* l_current_colr_ptr;
964 assert(p_nb_bytes_written != 00);
977 l_colr_data = (unsigned char *) opj_malloc(l_colr_size);
978 if (l_colr_data == 00) {
981 memset(l_colr_data,0,l_colr_size);
983 l_current_colr_ptr = l_colr_data;
985 opj_write_bytes(l_current_colr_ptr,l_colr_size,4); /* write box size */
986 l_current_colr_ptr += 4;
988 opj_write_bytes(l_current_colr_ptr,JP2_COLR,4); /* BPCC */
989 l_current_colr_ptr += 4;
991 opj_write_bytes(l_current_colr_ptr, jp2->meth,1); /* METH */
992 ++l_current_colr_ptr;
994 opj_write_bytes(l_current_colr_ptr, jp2->precedence,1); /* PRECEDENCE */
995 ++l_current_colr_ptr;
997 opj_write_bytes(l_current_colr_ptr, jp2->approx,1); /* APPROX */
998 ++l_current_colr_ptr;
1000 if (jp2->meth == 1) {
1001 opj_write_bytes(l_current_colr_ptr, jp2->enumcs,4); /* EnumCS */
1004 opj_write_bytes(l_current_colr_ptr, 0, 1); /* PROFILE (??) */
1007 *p_nb_bytes_written = l_colr_size;
1012 static void jp2_free_pclr(opj_jp2_color_t *color)
1014 opj_free(color->jp2_pclr->channel_sign);
1015 opj_free(color->jp2_pclr->channel_size);
1016 opj_free(color->jp2_pclr->entries);
1018 if(color->jp2_pclr->cmap) opj_free(color->jp2_pclr->cmap);
1020 opj_free(color->jp2_pclr); color->jp2_pclr = NULL;
1023 static void free_color_data(opj_jp2_color_t *color)
1027 jp2_free_pclr(color);
1031 if(color->jp2_cdef->info) opj_free(color->jp2_cdef->info);
1032 opj_free(color->jp2_cdef);
1034 if(color->icc_profile_buf) opj_free(color->icc_profile_buf);
1038 static void jp2_apply_pclr(opj_image_t *image, opj_jp2_color_t *color)
1040 opj_image_comp_t *old_comps, *new_comps;
1041 OPJ_BYTE *channel_size, *channel_sign;
1042 OPJ_UINT32 *entries;
1043 opj_jp2_cmap_comp_t *cmap;
1044 OPJ_INT32 *src, *dst;
1046 OPJ_UINT16 i, nr_channels, cmp, pcol;
1049 channel_size = color->jp2_pclr->channel_size;
1050 channel_sign = color->jp2_pclr->channel_sign;
1051 entries = color->jp2_pclr->entries;
1052 cmap = color->jp2_pclr->cmap;
1053 nr_channels = color->jp2_pclr->nr_channels;
1055 old_comps = image->comps;
1056 new_comps = (opj_image_comp_t*)
1057 opj_malloc(nr_channels * sizeof(opj_image_comp_t));
1059 for(i = 0; i < nr_channels; ++i) {
1060 pcol = cmap[i].pcol; cmp = cmap[i].cmp;
1062 new_comps[pcol] = old_comps[cmp];
1065 if(cmap[i].mtyp == 0){
1066 old_comps[cmp].data = NULL; continue;
1069 /* Palette mapping: */
1070 new_comps[pcol].data = (int*)
1071 opj_malloc(old_comps[cmp].w * old_comps[cmp].h * sizeof(int));
1072 new_comps[pcol].prec = channel_size[i];
1073 new_comps[pcol].sgnd = channel_sign[i];
1076 top_k = color->jp2_pclr->nr_entries - 1;
1078 for(i = 0; i < nr_channels; ++i) {
1080 if(cmap[i].mtyp == 0) continue;
1082 /* Palette mapping: */
1083 cmp = cmap[i].cmp; pcol = cmap[i].pcol;
1084 src = old_comps[cmp].data;
1085 dst = new_comps[pcol].data;
1086 max = new_comps[pcol].w * new_comps[pcol].h;
1088 for(j = 0; j < max; ++j)
1091 if((k = src[j]) < 0) k = 0; else if(k > top_k) k = top_k;
1094 dst[j] = entries[k * nr_channels + pcol];
1098 max = image->numcomps;
1099 for(i = 0; i < max; ++i) {
1100 if(old_comps[i].data) opj_free(old_comps[i].data);
1103 opj_free(old_comps);
1104 image->comps = new_comps;
1105 image->numcomps = nr_channels;
1107 jp2_free_pclr(color);
1112 static opj_bool jp2_read_pclr(opj_jp2_t *jp2, opj_cio_t *cio,
1113 opj_jp2_box_t *box, opj_jp2_color_t *color)
1115 opj_jp2_pclr_t *jp2_pclr;
1116 unsigned char *channel_size, *channel_sign;
1117 unsigned int *entries;
1118 unsigned short nr_entries, nr_channels;
1119 unsigned short i, j;
1122 OPJ_ARG_NOT_USED(box);
1123 OPJ_ARG_NOT_USED(jp2);
1125 /* Part 1, I.5.3.4: 'There shall be at most one Palette box inside
1126 * a JP2 Header box' :
1128 if(color->jp2_pclr) return OPJ_FALSE;
1130 nr_entries = (unsigned short)cio_read(cio, 2); /* NE */
1131 nr_channels = (unsigned short)cio_read(cio, 1);/* NPC */
1133 entries = (unsigned int*)
1134 opj_malloc(nr_channels * nr_entries * sizeof(unsigned int));
1135 channel_size = (unsigned char*)opj_malloc(nr_channels);
1136 channel_sign = (unsigned char*)opj_malloc(nr_channels);
1138 jp2_pclr = (opj_jp2_pclr_t*)opj_malloc(sizeof(opj_jp2_pclr_t));
1139 jp2_pclr->channel_sign = channel_sign;
1140 jp2_pclr->channel_size = channel_size;
1141 jp2_pclr->entries = entries;
1142 jp2_pclr->nr_entries = nr_entries;
1143 jp2_pclr->nr_channels = nr_channels;
1144 jp2_pclr->cmap = NULL;
1146 color->jp2_pclr = jp2_pclr;
1148 for(i = 0; i < nr_channels; ++i)
1150 uc = cio_read(cio, 1); /* Bi */
1151 channel_size[i] = (uc & 0x7f) + 1;
1152 channel_sign[i] = (uc & 0x80)?1:0;
1155 for(j = 0; j < nr_entries; ++j)
1157 for(i = 0; i < nr_channels; ++i)
1160 *entries++ = cio_read(cio, (channel_size[i]+7)>>3);
1165 }/* jp2_read_pclr() */
1168 * Reads a palette box.
1170 * @param jp2 the jpeg2000 file codec.
1171 * @param p_pclr_header_data pointer to actual data (already read from file)
1172 * @param p_pclr_header_size pointer that will hold the size of the PCLR header
1173 * @param p_manager the user event manager.
1175 * @return true if the bpc header is valid, fale else.
1177 opj_bool jp2_read_pclr_v2( opj_jp2_v2_t *jp2,
1178 unsigned char * p_pclr_header_data,
1179 OPJ_UINT32 p_pclr_header_size,
1180 opj_event_mgr_t * p_manager
1182 opj_jp2_pclr_t *jp2_pclr;
1183 OPJ_BYTE *channel_size, *channel_sign;
1184 OPJ_UINT32 *entries;
1185 OPJ_UINT16 nr_entries,nr_channels;
1190 assert(p_pclr_header_data != 00);
1192 assert(p_manager != 00);
1193 (void)p_pclr_header_size;
1195 if(jp2->color.jp2_pclr)
1198 opj_read_bytes(p_pclr_header_data, &l_value , 2); /* NE */
1199 p_pclr_header_data += 2;
1200 nr_entries = (OPJ_UINT16) l_value;
1202 opj_read_bytes(p_pclr_header_data, &l_value , 1); /* NPC */
1203 ++p_pclr_header_data;
1204 nr_channels = (OPJ_UINT16) l_value;
1206 entries = (OPJ_UINT32*) opj_malloc(nr_channels * nr_entries * sizeof(OPJ_UINT32));
1207 channel_size = (OPJ_BYTE*) opj_malloc(nr_channels);
1208 channel_sign = (OPJ_BYTE*) opj_malloc(nr_channels);
1210 jp2_pclr = (opj_jp2_pclr_t*)opj_malloc(sizeof(opj_jp2_pclr_t));
1211 jp2_pclr->channel_sign = channel_sign;
1212 jp2_pclr->channel_size = channel_size;
1213 jp2_pclr->entries = entries;
1214 jp2_pclr->nr_entries = nr_entries;
1215 jp2_pclr->nr_channels = nr_channels;
1216 jp2_pclr->cmap = NULL;
1218 jp2->color.jp2_pclr = jp2_pclr;
1220 for(i = 0; i < nr_channels; ++i) {
1221 opj_read_bytes(p_pclr_header_data, &l_value , 1); /* Bi */
1222 ++p_pclr_header_data;
1224 channel_size[i] = (l_value & 0x7f) + 1;
1225 channel_sign[i] = (l_value & 0x80)? 1 : 0;
1228 for(j = 0; j < nr_entries; ++j) {
1229 for(i = 0; i < nr_channels; ++i) {
1230 int bytes_to_read = (channel_size[i]+7)>>3;
1232 opj_read_bytes(p_pclr_header_data, &l_value , bytes_to_read); /* Cji */
1233 p_pclr_header_data += bytes_to_read;
1234 *entries = (OPJ_UINT32) l_value;
1243 static opj_bool jp2_read_cmap(opj_jp2_t *jp2, opj_cio_t *cio,
1244 opj_jp2_box_t *box, opj_jp2_color_t *color)
1246 opj_jp2_cmap_comp_t *cmap;
1247 unsigned short i, nr_channels;
1249 OPJ_ARG_NOT_USED(box);
1250 OPJ_ARG_NOT_USED(jp2);
1252 /* Need nr_channels: */
1253 if(color->jp2_pclr == NULL) return OPJ_FALSE;
1255 /* Part 1, I.5.3.5: 'There shall be at most one Component Mapping box
1256 * inside a JP2 Header box' :
1258 if(color->jp2_pclr->cmap) return OPJ_FALSE;
1260 nr_channels = color->jp2_pclr->nr_channels;
1261 cmap = (opj_jp2_cmap_comp_t*)
1262 opj_malloc(nr_channels * sizeof(opj_jp2_cmap_comp_t));
1264 for(i = 0; i < nr_channels; ++i)
1266 cmap[i].cmp = (unsigned short)cio_read(cio, 2);
1267 cmap[i].mtyp = cio_read(cio, 1);
1268 cmap[i].pcol = cio_read(cio, 1);
1271 color->jp2_pclr->cmap = cmap;
1275 }/* jp2_read_cmap() */
1278 * Reads the Component Mapping box.
1280 * @param p_cmap_header_data pointer to actual data (already read from file)
1281 * @param jp2 the jpeg2000 file codec.
1282 * @param p_cmap_header_size pointer that will hold the size of the color header
1283 * @param p_manager the user event manager.
1285 * @return true if the cdef header is valid, false else.
1287 static opj_bool jp2_read_cmap_v2( opj_jp2_v2_t * jp2,
1288 unsigned char * p_cmap_header_data,
1289 OPJ_UINT32 p_cmap_header_size,
1290 opj_event_mgr_t * p_manager
1293 opj_jp2_cmap_comp_t *cmap;
1294 OPJ_BYTE i, nr_channels;
1299 assert(p_cmap_header_data != 00);
1300 assert(p_manager != 00);
1301 (void)p_cmap_header_size;
1303 /* Need nr_channels: */
1304 if(jp2->color.jp2_pclr == NULL) {
1305 opj_event_msg_v2(p_manager, EVT_ERROR, "Need to read a PCLR box before the CMAP box.\n");
1309 /* Part 1, I.5.3.5: 'There shall be at most one Component Mapping box
1310 * inside a JP2 Header box' :
1312 if(jp2->color.jp2_pclr->cmap) {
1313 opj_event_msg_v2(p_manager, EVT_ERROR, "Only one CMAP box is allowed.\n");
1317 nr_channels = jp2->color.jp2_pclr->nr_channels;
1318 cmap = (opj_jp2_cmap_comp_t*) opj_malloc(nr_channels * sizeof(opj_jp2_cmap_comp_t));
1320 for(i = 0; i < nr_channels; ++i) {
1321 opj_read_bytes(p_cmap_header_data, &l_value, 2); /* CMP^i */
1322 p_cmap_header_data +=2;
1323 cmap[i].cmp = (OPJ_UINT16) l_value;
1325 opj_read_bytes(p_cmap_header_data, &l_value, 1); /* MTYP^i */
1326 ++p_cmap_header_data;
1327 cmap[i].mtyp = (OPJ_BYTE) l_value;
1329 opj_read_bytes(p_cmap_header_data, &l_value, 1); /* PCOL^i */
1330 ++p_cmap_header_data;
1331 cmap[i].pcol = (OPJ_BYTE) l_value;
1334 jp2->color.jp2_pclr->cmap = cmap;
1339 static void jp2_apply_cdef(opj_image_t *image, opj_jp2_color_t *color)
1341 opj_jp2_cdef_info_t *info;
1342 OPJ_INT32 color_space;
1343 OPJ_UINT16 i, n, cn, typ, asoc, acn;
1345 color_space = image->color_space;
1346 info = color->jp2_cdef->info;
1347 n = color->jp2_cdef->n;
1349 for(i = 0; i < n; ++i)
1351 /* WATCH: acn = asoc - 1 ! */
1352 if((asoc = info[i].asoc) == 0) continue;
1354 cn = info[i].cn; typ = info[i].typ; acn = asoc - 1;
1358 opj_image_comp_t saved;
1360 memcpy(&saved, &image->comps[cn], sizeof(opj_image_comp_t));
1361 memcpy(&image->comps[cn], &image->comps[acn], sizeof(opj_image_comp_t));
1362 memcpy(&image->comps[acn], &saved, sizeof(opj_image_comp_t));
1364 info[i].asoc = cn + 1;
1365 info[acn].asoc = info[acn].cn + 1;
1369 if(color->jp2_cdef->info) opj_free(color->jp2_cdef->info);
1371 opj_free(color->jp2_cdef); color->jp2_cdef = NULL;
1373 }/* jp2_apply_cdef() */
1375 static opj_bool jp2_read_cdef(opj_jp2_t *jp2, opj_cio_t *cio,
1376 opj_jp2_box_t *box, opj_jp2_color_t *color)
1378 opj_jp2_cdef_info_t *info;
1379 unsigned short i, n;
1381 OPJ_ARG_NOT_USED(box);
1382 OPJ_ARG_NOT_USED(jp2);
1384 /* Part 1, I.5.3.6: 'The shall be at most one Channel Definition box
1385 * inside a JP2 Header box.'
1387 if(color->jp2_cdef) return OPJ_FALSE;
1389 if((n = (unsigned short)cio_read(cio, 2)) == 0) return OPJ_FALSE; /* szukw000: FIXME */
1391 info = (opj_jp2_cdef_info_t*)
1392 opj_malloc(n * sizeof(opj_jp2_cdef_info_t));
1394 color->jp2_cdef = (opj_jp2_cdef_t*)opj_malloc(sizeof(opj_jp2_cdef_t));
1395 color->jp2_cdef->info = info;
1396 color->jp2_cdef->n = n;
1398 for(i = 0; i < n; ++i)
1400 info[i].cn = (unsigned short)cio_read(cio, 2);
1401 info[i].typ = (unsigned short)cio_read(cio, 2);
1402 info[i].asoc = (unsigned short)cio_read(cio, 2);
1406 }/* jp2_read_cdef() */
1409 * Reads the Component Definition box.
1411 * @param p_cdef_header_data pointer to actual data (already read from file)
1412 * @param jp2 the jpeg2000 file codec.
1413 * @param p_cdef_header_size pointer that will hold the size of the color header
1414 * @param p_manager the user event manager.
1416 * @return true if the cdef header is valid, false else.
1418 static opj_bool jp2_read_cdef_v2( opj_jp2_v2_t * jp2,
1419 unsigned char * p_cdef_header_data,
1420 OPJ_UINT32 p_cdef_header_size,
1421 opj_event_mgr_t * p_manager
1424 opj_jp2_cdef_info_t *cdef_info;
1430 assert(p_cdef_header_data != 00);
1431 assert(p_manager != 00);
1432 (void)p_cdef_header_size;
1434 /* Part 1, I.5.3.6: 'The shall be at most one Channel Definition box
1435 * inside a JP2 Header box.'*/
1436 if(jp2->color.jp2_cdef) return OPJ_FALSE;
1438 opj_read_bytes(p_cdef_header_data,&l_value ,2); /* N */
1439 p_cdef_header_data+= 2;
1441 if ( (OPJ_UINT16)l_value == 0){ /* szukw000: FIXME */
1442 opj_event_msg_v2(p_manager, EVT_ERROR, "Number of channel description is equal to zero in CDEF box.\n");
1446 cdef_info = (opj_jp2_cdef_info_t*) opj_malloc(l_value * sizeof(opj_jp2_cdef_info_t));
1448 jp2->color.jp2_cdef = (opj_jp2_cdef_t*)opj_malloc(sizeof(opj_jp2_cdef_t));
1449 jp2->color.jp2_cdef->info = cdef_info;
1450 jp2->color.jp2_cdef->n = (OPJ_UINT16) l_value;
1452 for(i = 0; i < jp2->color.jp2_cdef->n; ++i) {
1453 opj_read_bytes(p_cdef_header_data, &l_value, 2); /* Cn^i */
1454 p_cdef_header_data +=2;
1455 cdef_info[i].cn = (OPJ_UINT16) l_value;
1457 opj_read_bytes(p_cdef_header_data, &l_value, 2); /* Typ^i */
1458 p_cdef_header_data +=2;
1459 cdef_info[i].typ = (OPJ_UINT16) l_value;
1461 opj_read_bytes(p_cdef_header_data, &l_value, 2); /* Asoc^i */
1462 p_cdef_header_data +=2;
1463 cdef_info[i].asoc = (OPJ_UINT16) l_value;
1470 static opj_bool jp2_read_colr(opj_jp2_t *jp2, opj_cio_t *cio,
1471 opj_jp2_box_t *box, opj_jp2_color_t *color)
1474 opj_common_ptr cinfo;
1476 /* Part 1, I.5.3.3 : 'A conforming JP2 reader shall ignore all Colour
1477 * Specification boxes after the first.'
1479 if(color->jp2_has_colr) return OPJ_FALSE;
1483 jp2->meth = cio_read(cio, 1); /* METH */
1484 jp2->precedence = cio_read(cio, 1); /* PRECEDENCE */
1485 jp2->approx = cio_read(cio, 1); /* APPROX */
1489 jp2->enumcs = cio_read(cio, 4); /* EnumCS */
1495 skip_len = box->init_pos + box->length - cio_tell(cio);
1498 opj_event_msg(cinfo, EVT_ERROR, "Error with COLR box size\n");
1503 unsigned char *start;
1505 start = cio_getbp(cio);
1506 color->icc_profile_buf = (unsigned char*)opj_malloc(skip_len);
1507 color->icc_profile_len = skip_len;
1509 cio_skip(cio, box->init_pos + box->length - cio_tell(cio));
1511 memcpy(color->icc_profile_buf, start, skip_len);
1515 if (cio_tell(cio) - box->init_pos != box->length)
1517 opj_event_msg(cinfo, EVT_ERROR, "Error with COLR Box\n");
1520 color->jp2_has_colr = 1;
1523 }/* jp2_read_colr() */
1526 * Reads the Colour Specification box.
1528 * @param p_colr_header_data pointer to actual data (already read from file)
1529 * @param jp2 the jpeg2000 file codec.
1530 * @param p_colr_header_size pointer that will hold the size of the color header
1531 * @param p_colr_header_max_size maximum size of the header, any size bigger than this value should result the function to output false.
1532 * @param p_manager the user event manager.
1534 * @return true if the bpc header is valid, fale else.
1536 static opj_bool jp2_read_colr_v2( opj_jp2_v2_t * jp2,
1537 unsigned char * p_colr_header_data,
1538 OPJ_UINT32 p_colr_header_size,
1539 opj_event_mgr_t * p_manager
1546 assert(p_colr_header_data != 00);
1547 assert(p_manager != 00);
1549 if (p_colr_header_size < 3) {
1550 opj_event_msg_v2(p_manager, EVT_ERROR, "Bad COLR header box (bad size)\n");
1554 /* Part 1, I.5.3.3 : 'A conforming JP2 reader shall ignore all Colour
1555 * Specification boxes after the first.'
1557 if(jp2->color.jp2_has_colr) {
1558 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");
1559 p_colr_header_data += p_colr_header_size;
1563 opj_read_bytes(p_colr_header_data,&jp2->meth ,1); /* METH */
1564 ++p_colr_header_data;
1566 opj_read_bytes(p_colr_header_data,&jp2->precedence ,1); /* PRECEDENCE */
1567 ++p_colr_header_data;
1569 opj_read_bytes(p_colr_header_data,&jp2->approx ,1); /* APPROX */
1570 ++p_colr_header_data;
1572 if (jp2->meth == 1) {
1573 if (p_colr_header_size != 7) {
1574 opj_event_msg_v2(p_manager, EVT_ERROR, "Bad BPCC header box (bad size)\n");
1578 opj_read_bytes(p_colr_header_data,&jp2->enumcs ,4); /* EnumCS */
1580 else if (jp2->meth == 2) {
1582 int it_icc_value = 0;
1583 int icc_len = p_colr_header_size - 3;
1585 jp2->color.icc_profile_len = icc_len;
1586 jp2->color.icc_profile_buf = (unsigned char*) opj_malloc(icc_len);
1588 memset(jp2->color.icc_profile_buf, 0, icc_len * sizeof(unsigned char));
1590 for (it_icc_value = 0; it_icc_value < icc_len; ++it_icc_value)
1592 opj_read_bytes(p_colr_header_data,&l_value,1); /* icc values */
1593 ++p_colr_header_data;
1594 jp2->color.icc_profile_buf[it_icc_value] = (OPJ_BYTE) l_value;
1599 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);
1601 jp2->color.jp2_has_colr = 1;
1606 opj_bool jp2_read_jp2h(opj_jp2_t *jp2, opj_cio_t *cio, opj_jp2_color_t *color)
1611 opj_common_ptr cinfo = jp2->cinfo;
1613 jp2_read_boxhdr(cinfo, cio, &box);
1616 if (JP2_JP2H != box.type)
1618 if (box.type == JP2_JP2C)
1620 opj_event_msg(cinfo, EVT_ERROR, "Expected JP2H Marker\n");
1623 cio_skip(cio, box.length - 8);
1625 if(cio->bp >= cio->end) return OPJ_FALSE;
1627 jp2_read_boxhdr(cinfo, cio, &box);
1629 } while(JP2_JP2H != box.type);
1631 if (!jp2_read_ihdr(jp2, cio))
1633 jp2h_end = box.init_pos + box.length;
1635 if (jp2->bpc == 255)
1637 if (!jp2_read_bpcc(jp2, cio))
1640 jp2_read_boxhdr(cinfo, cio, &box);
1642 while(cio_tell(cio) < jp2h_end)
1644 if(box.type == JP2_COLR)
1646 if( !jp2_read_colr(jp2, cio, &box, color))
1648 cio_seek(cio, box.init_pos + 8);
1649 cio_skip(cio, box.length - 8);
1651 jp2_read_boxhdr(cinfo, cio, &box);
1654 if(box.type == JP2_CDEF && !jp2->ignore_pclr_cmap_cdef)
1656 if( !jp2_read_cdef(jp2, cio, &box, color))
1658 cio_seek(cio, box.init_pos + 8);
1659 cio_skip(cio, box.length - 8);
1661 jp2_read_boxhdr(cinfo, cio, &box);
1664 if(box.type == JP2_PCLR && !jp2->ignore_pclr_cmap_cdef)
1666 if( !jp2_read_pclr(jp2, cio, &box, color))
1668 cio_seek(cio, box.init_pos + 8);
1669 cio_skip(cio, box.length - 8);
1671 jp2_read_boxhdr(cinfo, cio, &box);
1674 if(box.type == JP2_CMAP && !jp2->ignore_pclr_cmap_cdef)
1676 if( !jp2_read_cmap(jp2, cio, &box, color))
1678 cio_seek(cio, box.init_pos + 8);
1679 cio_skip(cio, box.length - 8);
1681 jp2_read_boxhdr(cinfo, cio, &box);
1684 cio_seek(cio, box.init_pos + 8);
1685 cio_skip(cio, box.length - 8);
1686 jp2_read_boxhdr(cinfo, cio, &box);
1688 }/* while(cio_tell(cio) < box_end) */
1690 cio_seek(cio, jp2h_end);
1692 /* Part 1, I.5.3.3 : 'must contain at least one' */
1693 return (color->jp2_has_colr == 1);
1695 }/* jp2_read_jp2h() */
1699 opj_bool opj_jp2_decode(opj_jp2_v2_t *jp2,
1700 opj_stream_private_t *p_stream,
1701 opj_image_t* p_image,
1702 opj_event_mgr_t * p_manager)
1708 if( ! opj_j2k_decode(jp2->j2k, p_stream, p_image, p_manager) ) {
1709 opj_event_msg_v2(p_manager, EVT_ERROR, "Failed to decode the codestream in the JP2 file\n");
1713 if (!jp2->ignore_pclr_cmap_cdef){
1715 /* Set Image Color Space */
1716 if (jp2->enumcs == 16)
1717 p_image->color_space = CLRSPC_SRGB;
1718 else if (jp2->enumcs == 17)
1719 p_image->color_space = CLRSPC_GRAY;
1720 else if (jp2->enumcs == 18)
1721 p_image->color_space = CLRSPC_SYCC;
1723 p_image->color_space = CLRSPC_UNKNOWN;
1725 /* Apply the color space if needed */
1726 if(jp2->color.jp2_cdef) {
1727 jp2_apply_cdef(p_image, &(jp2->color));
1730 if(jp2->color.jp2_pclr) {
1731 /* Part 1, I.5.3.4: Either both or none : */
1732 if( !jp2->color.jp2_pclr->cmap)
1733 jp2_free_pclr(&(jp2->color));
1735 jp2_apply_pclr(p_image, &(jp2->color));
1738 if(jp2->color.icc_profile_buf) {
1739 p_image->icc_profile_buf = jp2->color.icc_profile_buf;
1740 p_image->icc_profile_len = jp2->color.icc_profile_len;
1741 jp2->color.icc_profile_buf = NULL;
1749 void jp2_write_jp2h(opj_jp2_t *jp2, opj_cio_t *cio) {
1752 box.init_pos = cio_tell(cio);
1754 cio_write(cio, JP2_JP2H, 4); /* JP2H */
1756 jp2_write_ihdr(jp2, cio);
1758 if (jp2->bpc == 255) {
1759 jp2_write_bpcc(jp2, cio);
1761 jp2_write_colr(jp2, cio);
1763 box.length = cio_tell(cio) - box.init_pos;
1764 cio_seek(cio, box.init_pos);
1765 cio_write(cio, box.length, 4); /* L */
1766 cio_seek(cio, box.init_pos + box.length);
1770 * Writes the Jpeg2000 file Header box - JP2 Header box (warning, this is a super box).
1772 * @param cio the stream to write data to.
1773 * @param jp2 the jpeg2000 file codec.
1774 * @param p_manager user event manager.
1776 * @return true if writting was successful.
1778 opj_bool jp2_write_jp2h_v2( opj_jp2_v2_t *jp2,
1779 opj_stream_private_t *cio,
1780 opj_event_mgr_t * p_manager )
1782 opj_jp2_img_header_writer_handler_t l_writers [3];
1783 opj_jp2_img_header_writer_handler_t * l_current_writer;
1786 /* size of data for super box*/
1787 int l_jp2h_size = 8;
1788 opj_bool l_result = OPJ_TRUE;
1790 /* to store the data of the super box */
1791 unsigned char l_jp2h_data [8];
1796 assert(p_manager != 00);
1798 memset(l_writers,0,sizeof(l_writers));
1800 if (jp2->bpc == 255) {
1802 l_writers[0].handler = jp2_write_ihdr_v2;
1803 l_writers[1].handler = jp2_write_bpcc_v2;
1804 l_writers[2].handler = jp2_write_colr_v2;
1808 l_writers[0].handler = jp2_write_ihdr_v2;
1809 l_writers[1].handler = jp2_write_colr_v2;
1812 /* write box header */
1813 /* write JP2H type */
1814 opj_write_bytes(l_jp2h_data+4,JP2_JP2H,4);
1816 l_current_writer = l_writers;
1817 for (i=0;i<l_nb_pass;++i) {
1818 l_current_writer->m_data = l_current_writer->handler(jp2,&(l_current_writer->m_size));
1819 if (l_current_writer->m_data == 00) {
1820 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to hold JP2 Header data\n");
1821 l_result = OPJ_FALSE;
1825 l_jp2h_size += l_current_writer->m_size;
1830 l_current_writer = l_writers;
1831 for (i=0;i<l_nb_pass;++i) {
1832 if (l_current_writer->m_data != 00) {
1833 opj_free(l_current_writer->m_data );
1841 /* write super box size */
1842 opj_write_bytes(l_jp2h_data,l_jp2h_size,4);
1844 /* write super box data on stream */
1845 if (opj_stream_write_data(cio,l_jp2h_data,8,p_manager) != 8) {
1846 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream error while writing JP2 Header box\n");
1847 l_result = OPJ_FALSE;
1851 l_current_writer = l_writers;
1852 for (i=0;i<l_nb_pass;++i) {
1853 if (opj_stream_write_data(cio,l_current_writer->m_data,l_current_writer->m_size,p_manager) != l_current_writer->m_size) {
1854 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream error while writting JP2 Header box\n");
1855 l_result = OPJ_FALSE;
1862 l_current_writer = l_writers;
1865 for (i=0;i<l_nb_pass;++i) {
1866 if (l_current_writer->m_data != 00) {
1867 opj_free(l_current_writer->m_data );
1875 static void jp2_write_ftyp(opj_jp2_t *jp2, opj_cio_t *cio) {
1879 box.init_pos = cio_tell(cio);
1881 cio_write(cio, JP2_FTYP, 4); /* FTYP */
1883 cio_write(cio, jp2->brand, 4); /* BR */
1884 cio_write(cio, jp2->minversion, 4); /* MinV */
1886 for (i = 0; i < jp2->numcl; i++) {
1887 cio_write(cio, jp2->cl[i], 4); /* CL */
1890 box.length = cio_tell(cio) - box.init_pos;
1891 cio_seek(cio, box.init_pos);
1892 cio_write(cio, box.length, 4); /* L */
1893 cio_seek(cio, box.init_pos + box.length);
1897 * Writes a FTYP box - File type box
1899 * @param cio the stream to write data to.
1900 * @param jp2 the jpeg2000 file codec.
1901 * @param p_manager the user event manager.
1903 * @return true if writting was successful.
1905 opj_bool jp2_write_ftyp_v2( opj_jp2_v2_t *jp2,
1906 opj_stream_private_t *cio,
1907 opj_event_mgr_t * p_manager )
1910 unsigned int l_ftyp_size = 16 + 4 * jp2->numcl;
1911 unsigned char * l_ftyp_data, * l_current_data_ptr;
1917 assert(p_manager != 00);
1919 l_ftyp_data = (unsigned char *) opj_malloc(l_ftyp_size);
1921 if (l_ftyp_data == 00) {
1922 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to handle ftyp data\n");
1926 memset(l_ftyp_data,0,l_ftyp_size);
1928 l_current_data_ptr = l_ftyp_data;
1930 opj_write_bytes(l_current_data_ptr, l_ftyp_size,4); /* box size */
1931 l_current_data_ptr += 4;
1933 opj_write_bytes(l_current_data_ptr, JP2_FTYP,4); /* FTYP */
1934 l_current_data_ptr += 4;
1936 opj_write_bytes(l_current_data_ptr, jp2->brand,4); /* BR */
1937 l_current_data_ptr += 4;
1939 opj_write_bytes(l_current_data_ptr, jp2->minversion,4); /* MinV */
1940 l_current_data_ptr += 4;
1942 for (i = 0; i < jp2->numcl; i++) {
1943 opj_write_bytes(l_current_data_ptr, jp2->cl[i],4); /* CL */
1946 l_result = (opj_stream_write_data(cio,l_ftyp_data,l_ftyp_size,p_manager) == l_ftyp_size);
1949 opj_event_msg_v2(p_manager, EVT_ERROR, "Error while writting ftyp data to stream\n");
1952 opj_free(l_ftyp_data);
1957 static opj_bool jp2_read_ftyp(opj_jp2_t *jp2, opj_cio_t *cio) {
1961 opj_common_ptr cinfo = jp2->cinfo;
1963 jp2_read_boxhdr(cinfo, cio, &box);
1965 if (JP2_FTYP != box.type) {
1966 opj_event_msg(cinfo, EVT_ERROR, "Expected FTYP Marker\n");
1970 jp2->brand = cio_read(cio, 4); /* BR */
1971 jp2->minversion = cio_read(cio, 4); /* MinV */
1972 jp2->numcl = (box.length - 16) / 4;
1973 jp2->cl = (unsigned int *) opj_malloc(jp2->numcl * sizeof(unsigned int));
1975 for (i = 0; i < (int)jp2->numcl; i++) {
1976 jp2->cl[i] = cio_read(cio, 4); /* CLi */
1979 if (cio_tell(cio) - box.init_pos != box.length) {
1980 opj_event_msg(cinfo, EVT_ERROR, "Error with FTYP Box\n");
1987 static int jp2_write_jp2c(opj_jp2_t *jp2, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info) {
1988 unsigned int j2k_codestream_offset, j2k_codestream_length;
1991 opj_j2k_t *j2k = jp2->j2k;
1993 box.init_pos = cio_tell(cio);
1995 cio_write(cio, JP2_JP2C, 4); /* JP2C */
1998 j2k_codestream_offset = cio_tell(cio);
1999 if(!j2k_encode(j2k, cio, image, cstr_info)) {
2000 opj_event_msg(j2k->cinfo, EVT_ERROR, "Failed to encode image\n");
2003 j2k_codestream_length = cio_tell(cio) - j2k_codestream_offset;
2005 jp2->j2k_codestream_offset = j2k_codestream_offset;
2006 jp2->j2k_codestream_length = j2k_codestream_length;
2008 box.length = 8 + jp2->j2k_codestream_length;
2009 cio_seek(cio, box.init_pos);
2010 cio_write(cio, box.length, 4); /* L */
2011 cio_seek(cio, box.init_pos + box.length);
2017 * Writes the Jpeg2000 codestream Header box - JP2C Header box.
2019 * @param cio the stream to write data to.
2020 * @param jp2 the jpeg2000 file codec.
2021 * @param p_manager user event manager.
2023 * @return true if writting was successful.
2025 opj_bool jp2_write_jp2c_v2( opj_jp2_v2_t *jp2,
2026 opj_stream_private_t *cio,
2027 opj_event_mgr_t * p_manager )
2029 unsigned int j2k_codestream_exit;
2030 unsigned char l_data_header [8];
2035 assert(p_manager != 00);
2036 assert(opj_stream_has_seek(cio));
2038 j2k_codestream_exit = opj_stream_tell(cio);
2039 opj_write_bytes(l_data_header,j2k_codestream_exit - jp2->j2k_codestream_offset,4); /* size of codestream */
2040 opj_write_bytes(l_data_header + 4,JP2_JP2C,4); /* JP2C */
2042 if (! opj_stream_seek(cio,jp2->j2k_codestream_offset,p_manager)) {
2043 opj_event_msg_v2(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
2047 if (opj_stream_write_data(cio,l_data_header,8,p_manager) != 8) {
2048 opj_event_msg_v2(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
2052 if (! opj_stream_seek(cio,j2k_codestream_exit,p_manager)) {
2053 opj_event_msg_v2(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
2060 static opj_bool jp2_read_jp2c(opj_jp2_t *jp2, opj_cio_t *cio, unsigned int *j2k_codestream_length, unsigned int *j2k_codestream_offset) {
2063 opj_common_ptr cinfo = jp2->cinfo;
2065 jp2_read_boxhdr(cinfo, cio, &box);
2067 if(JP2_JP2C != box.type) {
2068 cio_skip(cio, box.length - 8);
2069 jp2_read_boxhdr(cinfo, cio, &box);
2071 } while(JP2_JP2C != box.type);
2073 *j2k_codestream_offset = cio_tell(cio);
2074 *j2k_codestream_length = box.length - 8;
2079 static void jp2_write_jp(opj_cio_t *cio) {
2082 box.init_pos = cio_tell(cio);
2084 cio_write(cio, JP2_JP, 4); /* JP2 signature */
2085 cio_write(cio, 0x0d0a870a, 4);
2087 box.length = cio_tell(cio) - box.init_pos;
2088 cio_seek(cio, box.init_pos);
2089 cio_write(cio, box.length, 4); /* L */
2090 cio_seek(cio, box.init_pos + box.length);
2094 * Writes a jpeg2000 file signature box.
2096 * @param cio the stream to write data to.
2097 * @param jp2 the jpeg2000 file codec.
2098 * @param p_manager the user event manager.
2100 * @return true if writting was successful.
2102 opj_bool jp2_write_jp_v2( opj_jp2_v2_t *jp2,
2103 opj_stream_private_t *cio,
2104 opj_event_mgr_t * p_manager )
2106 /* 12 bytes will be read */
2107 unsigned char l_signature_data [12];
2112 assert(p_manager != 00);
2114 /* write box length */
2115 opj_write_bytes(l_signature_data,12,4);
2116 /* writes box type */
2117 opj_write_bytes(l_signature_data+4,JP2_JP,4);
2118 /* writes magic number*/
2119 opj_write_bytes(l_signature_data+8,0x0d0a870a,4);
2121 if (opj_stream_write_data(cio,l_signature_data,12,p_manager) != 12) {
2128 static opj_bool jp2_read_jp(opj_jp2_t *jp2, opj_cio_t *cio) {
2131 opj_common_ptr cinfo = jp2->cinfo;
2133 jp2_read_boxhdr(cinfo, cio, &box);
2134 if (JP2_JP != box.type) {
2135 opj_event_msg(cinfo, EVT_ERROR, "Expected JP Marker\n");
2138 if (0x0d0a870a != cio_read(cio, 4)) {
2139 opj_event_msg(cinfo, EVT_ERROR, "Error with JP Marker\n");
2142 if (cio_tell(cio) - box.init_pos != box.length) {
2143 opj_event_msg(cinfo, EVT_ERROR, "Error with JP Box size\n");
2151 static opj_bool jp2_read_struct(opj_jp2_t *jp2, opj_cio_t *cio,
2152 opj_jp2_color_t *color) {
2153 if (!jp2_read_jp(jp2, cio))
2155 if (!jp2_read_ftyp(jp2, cio))
2157 if (!jp2_read_jp2h(jp2, cio, color))
2159 if (!jp2_read_jp2c(jp2, cio, &jp2->j2k_codestream_length, &jp2->j2k_codestream_offset))
2166 static int write_fidx( int offset_jp2c, int length_jp2c, int offset_idx, int length_idx, opj_cio_t *cio)
2170 lenp = cio_tell( cio);
2171 cio_skip( cio, 4); /* L [at the end] */
2172 cio_write( cio, JPIP_FIDX, 4); /* IPTR */
2174 write_prxy( offset_jp2c, length_jp2c, offset_idx, length_idx, cio);
2176 len = cio_tell( cio)-lenp;
2177 cio_seek( cio, lenp);
2178 cio_write( cio, len, 4); /* L */
2179 cio_seek( cio, lenp+len);
2184 static void write_prxy( int offset_jp2c, int length_jp2c, int offset_idx, int length_idx, opj_cio_t *cio)
2188 lenp = cio_tell( cio);
2189 cio_skip( cio, 4); /* L [at the end] */
2190 cio_write( cio, JPIP_PRXY, 4); /* IPTR */
2192 cio_write( cio, offset_jp2c, 8); /* OOFF */
2193 cio_write( cio, length_jp2c, 4); /* OBH part 1 */
2194 cio_write( cio, JP2_JP2C, 4); /* OBH part 2 */
2196 cio_write( cio, 1,1); /* NI */
2198 cio_write( cio, offset_idx, 8); /* IOFF */
2199 cio_write( cio, length_idx, 4); /* IBH part 1 */
2200 cio_write( cio, JPIP_CIDX, 4); /* IBH part 2 */
2202 len = cio_tell( cio)-lenp;
2203 cio_seek( cio, lenp);
2204 cio_write( cio, len, 4); /* L */
2205 cio_seek( cio, lenp+len);
2208 static void write_iptr( int offset, int length, opj_cio_t *cio)
2212 lenp = cio_tell( cio);
2213 cio_skip( cio, 4); /* L [at the end] */
2214 cio_write( cio, JPIP_IPTR, 4); /* IPTR */
2216 cio_write( cio, offset, 8);
2217 cio_write( cio, length, 8);
2219 len = cio_tell( cio)-lenp;
2220 cio_seek( cio, lenp);
2221 cio_write( cio, len, 4); /* L */
2222 cio_seek( cio, lenp+len);
2226 /* ----------------------------------------------------------------------- */
2227 /* JP2 decoder interface */
2228 /* ----------------------------------------------------------------------- */
2231 void opj_jp2_setup_decoder(opj_jp2_v2_t *jp2, opj_dparameters_t *parameters)
2233 /* setup the J2K codec */
2234 opj_j2k_setup_decoder(jp2->j2k, parameters);
2236 /* further JP2 initializations go here */
2237 jp2->color.jp2_has_colr = 0;
2238 jp2->ignore_pclr_cmap_cdef = parameters->flags & OPJ_DPARAMETERS_IGNORE_PCLR_CMAP_CDEF_FLAG;
2242 /* ----------------------------------------------------------------------- */
2243 /* JP2 encoder interface */
2244 /* ----------------------------------------------------------------------- */
2246 opj_jp2_t* jp2_create_compress(opj_common_ptr cinfo) {
2247 opj_jp2_t *jp2 = (opj_jp2_t*)opj_malloc(sizeof(opj_jp2_t));
2250 /* create the J2K codec */
2251 jp2->j2k = j2k_create_compress(cinfo);
2252 if(jp2->j2k == NULL) {
2253 jp2_destroy_compress(jp2);
2260 void jp2_destroy_compress(opj_jp2_t *jp2) {
2262 /* destroy the J2K codec */
2263 j2k_destroy_compress(jp2->j2k);
2266 opj_free(jp2->comps);
2275 void jp2_setup_encoder( opj_jp2_v2_t *jp2,
2276 opj_cparameters_t *parameters,
2278 opj_event_mgr_t * p_manager) {
2282 if(!jp2 || !parameters || !image)
2285 /* setup the J2K codec */
2286 /* ------------------- */
2288 /* Check if number of components respects standard */
2289 if (image->numcomps < 1 || image->numcomps > 16384) {
2290 opj_event_msg_v2(p_manager, EVT_ERROR, "Invalid number of components specified while setting up JP2 encoder\n");
2294 j2k_setup_encoder_v2(jp2->j2k, parameters, image, p_manager );
2296 /* setup the JP2 codec */
2297 /* ------------------- */
2301 jp2->brand = JP2_JP2; /* BR */
2302 jp2->minversion = 0; /* MinV */
2304 jp2->cl = (unsigned int*) opj_malloc(jp2->numcl * sizeof(unsigned int));
2305 jp2->cl[0] = JP2_JP2; /* CL0 : JP2 */
2307 /* Image Header box */
2309 jp2->numcomps = image->numcomps; /* NC */
2310 jp2->comps = (opj_jp2_comps_t*) opj_malloc(jp2->numcomps * sizeof(opj_jp2_comps_t));
2311 jp2->h = image->y1 - image->y0; /* HEIGHT */
2312 jp2->w = image->x1 - image->x0; /* WIDTH */
2314 depth_0 = image->comps[0].prec - 1;
2315 sign = image->comps[0].sgnd;
2316 jp2->bpc = depth_0 + (sign << 7);
2317 for (i = 1; i < image->numcomps; i++) {
2318 int depth = image->comps[i].prec - 1;
2319 sign = image->comps[i].sgnd;
2320 if (depth_0 != depth)
2323 jp2->C = 7; /* C : Always 7 */
2324 jp2->UnkC = 0; /* UnkC, colorspace specified in colr box */
2325 jp2->IPR = 0; /* IPR, no intellectual property */
2327 /* BitsPerComponent box */
2328 for (i = 0; i < image->numcomps; i++) {
2329 jp2->comps[i].bpcc = image->comps[i].prec - 1 + (image->comps[i].sgnd << 7);
2332 /* Colour Specification box */
2333 if ((image->numcomps == 1 || image->numcomps == 3) && (jp2->bpc != 255)) {
2334 jp2->meth = 1; /* METH: Enumerated colourspace */
2336 jp2->meth = 2; /* METH: Restricted ICC profile */
2338 if (jp2->meth == 1) {
2339 if (image->color_space == 1)
2340 jp2->enumcs = 16; /* sRGB as defined by IEC 61966�2�1 */
2341 else if (image->color_space == 2)
2342 jp2->enumcs = 17; /* greyscale */
2343 else if (image->color_space == 3)
2344 jp2->enumcs = 18; /* YUV */
2346 jp2->enumcs = 0; /* PROFILE (??) */
2348 jp2->precedence = 0; /* PRECEDENCE */
2349 jp2->approx = 0; /* APPROX */
2351 // jp2->jpip_on = parameters->jpip_on;
2355 opj_bool opj_jp2_encode_v2( opj_jp2_v2_t *jp2,
2356 opj_stream_private_t *stream,
2357 opj_event_mgr_t * p_manager)
2359 return j2k_encode_v2(jp2->j2k, stream, p_manager);
2362 opj_bool opj_jp2_encode(opj_jp2_t *jp2, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info) {
2364 int pos_iptr, pos_cidx, pos_jp2c, len_jp2c, len_cidx, end_pos, pos_fidx, len_fidx;
2365 pos_jp2c = pos_iptr = -1; /* remove a warning */
2369 /* JPEG 2000 Signature box */
2372 jp2_write_ftyp(jp2, cio);
2373 /* JP2 Header box */
2374 jp2_write_jp2h(jp2, cio);
2377 pos_iptr = cio_tell( cio);
2378 cio_skip( cio, 24); /* IPTR further ! */
2380 pos_jp2c = cio_tell( cio);
2384 if(!(len_jp2c = jp2_write_jp2c( jp2, cio, image, cstr_info))){
2385 opj_event_msg(jp2->cinfo, EVT_ERROR, "Failed to encode image\n");
2390 pos_cidx = cio_tell( cio);
2392 len_cidx = write_cidx( pos_jp2c+8, cio, image, *cstr_info, len_jp2c-8);
2394 pos_fidx = cio_tell( cio);
2395 len_fidx = write_fidx( pos_jp2c, len_jp2c, pos_cidx, len_cidx, cio);
2397 end_pos = cio_tell( cio);
2399 cio_seek( cio, pos_iptr);
2400 write_iptr( pos_fidx, len_fidx, cio);
2401 cio_seek( cio, end_pos);
2408 * Ends the decompression procedures and possibiliy add data to be read after the
2411 opj_bool jp2_end_decompress(opj_jp2_v2_t *jp2, opj_stream_private_t *cio, opj_event_mgr_t * p_manager)
2416 assert(p_manager != 00);
2418 /* customization of the end encoding */
2419 jp2_setup_end_header_reading(jp2);
2422 if (! jp2_exec (jp2,jp2->m_procedure_list,cio,p_manager)) {
2426 return j2k_end_decompress(jp2->j2k, cio, p_manager);
2430 * Ends the compression procedures and possibility add data to be read after the
2433 opj_bool jp2_end_compress( opj_jp2_v2_t *jp2,
2434 opj_stream_private_t *cio,
2435 opj_event_mgr_t * p_manager)
2440 assert(p_manager != 00);
2442 /* customization of the end encoding */
2443 jp2_setup_end_header_writting(jp2);
2445 if (! j2k_end_compress(jp2->j2k,cio,p_manager)) {
2450 return jp2_exec(jp2,jp2->m_procedure_list,cio,p_manager);
2455 * Sets up the procedures to do on writing header after the codestream.
2456 * Developers wanting to extend the library can add their own writing procedures.
2458 void jp2_setup_end_header_writting (opj_jp2_v2_t *jp2)
2463 opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)jp2_write_jp2c_v2 );
2464 /* DEVELOPER CORNER, add your custom procedures */
2468 * Sets up the procedures to do on reading header after the codestream.
2469 * Developers wanting to extend the library can add their own writing procedures.
2471 void jp2_setup_end_header_reading (opj_jp2_v2_t *jp2)
2475 opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)jp2_read_header_procedure );
2476 /* DEVELOPER CORNER, add your custom procedures */
2480 * The default validation procedure without any extension.
2482 * @param jp2 the jpeg2000 codec to validate.
2483 * @param cio the input stream to validate.
2484 * @param p_manager the user event manager.
2486 * @return true if the parameters are correct.
2488 opj_bool jp2_default_validation ( opj_jp2_v2_t * jp2,
2489 opj_stream_private_t *cio,
2490 opj_event_mgr_t * p_manager )
2492 opj_bool l_is_valid = OPJ_TRUE;
2498 assert(p_manager != 00);
2500 /* JPEG2000 codec validation */
2503 /* STATE checking */
2504 /* make sure the state is at 0 */
2505 l_is_valid &= (jp2->jp2_state == JP2_STATE_NONE);
2507 /* make sure not reading a jp2h ???? WEIRD */
2508 l_is_valid &= (jp2->jp2_img_state == JP2_IMG_STATE_NONE);
2510 /* POINTER validation */
2511 /* make sure a j2k codec is present */
2512 l_is_valid &= (jp2->j2k != 00);
2514 /* make sure a procedure list is present */
2515 l_is_valid &= (jp2->m_procedure_list != 00);
2517 /* make sure a validation list is present */
2518 l_is_valid &= (jp2->m_validation_list != 00);
2520 /* PARAMETER VALIDATION */
2521 /* number of components */
2522 l_is_valid &= (jp2->numcl > 0);
2524 l_is_valid &= (jp2->h > 0);
2526 l_is_valid &= (jp2->w > 0);
2528 for (i = 0; i < jp2->numcomps; ++i) {
2529 l_is_valid &= (jp2->comps[i].bpcc > 0);
2533 l_is_valid &= ((jp2->meth > 0) && (jp2->meth < 3));
2535 /* stream validation */
2536 /* back and forth is needed */
2537 l_is_valid &= opj_stream_has_seek(cio);
2543 * Reads a jpeg2000 file header structure.
2545 * @param cio the stream to read data from.
2546 * @param jp2 the jpeg2000 file header structure.
2547 * @param p_manager the user event manager.
2549 * @return true if the box is valid.
2551 opj_bool jp2_read_header_procedure(
2553 opj_stream_private_t *cio,
2554 opj_event_mgr_t * p_manager)
2557 OPJ_UINT32 l_nb_bytes_read;
2558 const opj_jp2_header_handler_t * l_current_handler;
2559 OPJ_UINT32 l_last_data_size = BOX_SIZE;
2560 OPJ_UINT32 l_current_data_size;
2561 unsigned char * l_current_data = 00;
2566 assert(p_manager != 00);
2568 l_current_data = (unsigned char*)opj_malloc(l_last_data_size);
2570 if (l_current_data == 00) {
2571 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to handle jpeg2000 file header\n");
2574 memset(l_current_data, 0 , l_last_data_size);
2576 while (jp2_read_boxhdr_v2(&box,&l_nb_bytes_read,cio,p_manager)) {
2577 /* is it the codestream box ? */
2578 if (box.type == JP2_JP2C) {
2579 if (jp2->jp2_state & JP2_STATE_HEADER) {
2580 jp2->jp2_state |= JP2_STATE_CODESTREAM;
2581 opj_free(l_current_data);
2585 opj_event_msg_v2(p_manager, EVT_ERROR, "bad placed jpeg codestream\n");
2586 opj_free(l_current_data);
2590 else if (box.length == 0) {
2591 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot handle box of undefined sizes\n");
2592 opj_free(l_current_data);
2596 l_current_handler = jp2_find_handler(box.type);
2597 l_current_data_size = box.length - l_nb_bytes_read;
2599 if (l_current_handler != 00) {
2600 if (l_current_data_size > l_last_data_size) {
2601 l_current_data = (unsigned char*)opj_realloc(l_current_data,l_current_data_size);
2602 if (!l_current_data){
2603 opj_free(l_current_data);
2606 l_last_data_size = l_current_data_size;
2609 l_nb_bytes_read = opj_stream_read_data(cio,l_current_data,l_current_data_size,p_manager);
2610 if (l_nb_bytes_read != l_current_data_size) {
2611 opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with reading JPEG2000 box, stream error\n");
2615 if (! l_current_handler->handler(jp2,l_current_data,l_current_data_size,p_manager)) {
2616 opj_free(l_current_data);
2621 jp2->jp2_state |= JP2_STATE_UNKNOWN;
2622 if (opj_stream_skip(cio,l_current_data_size,p_manager) != l_current_data_size) {
2623 opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with skipping JPEG2000 box, stream error\n");
2624 opj_free(l_current_data);
2630 opj_free(l_current_data);
2636 * Excutes the given procedures on the given codec.
2638 * @param p_procedure_list the list of procedures to execute
2639 * @param jp2 the jpeg2000 file codec to execute the procedures on.
2640 * @param cio the stream to execute the procedures on.
2641 * @param p_manager the user manager.
2643 * @return true if all the procedures were successfully executed.
2647 opj_procedure_list_t * p_procedure_list,
2648 opj_stream_private_t *cio,
2649 opj_event_mgr_t * p_manager
2652 opj_bool (** l_procedure) (opj_jp2_v2_t * jp2, opj_stream_private_t *, opj_event_mgr_t *) = 00;
2653 opj_bool l_result = OPJ_TRUE;
2654 OPJ_UINT32 l_nb_proc, i;
2657 assert(p_procedure_list != 00);
2660 assert(p_manager != 00);
2662 l_nb_proc = opj_procedure_list_get_nb_procedures(p_procedure_list);
2663 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);
2665 for (i=0;i<l_nb_proc;++i) {
2666 l_result = l_result && (*l_procedure) (jp2,cio,p_manager);
2670 /* and clear the procedure list at the end. */
2671 opj_procedure_list_clear(p_procedure_list);
2676 * Starts a compression scheme, i.e. validates the codec parameters, writes the header.
2678 * @param jp2 the jpeg2000 file codec.
2679 * @param cio the stream object.
2681 * @return true if the codec is valid.
2683 opj_bool jp2_start_compress(opj_jp2_v2_t *jp2,
2684 struct opj_stream_private *cio,
2685 opj_image_t * p_image,
2686 struct opj_event_mgr * p_manager)
2691 assert(p_manager != 00);
2693 /* customization of the validation */
2694 jp2_setup_encoding_validation (jp2);
2696 /* validation of the parameters codec */
2697 if (! jp2_exec(jp2,jp2->m_validation_list,cio,p_manager)) {
2701 /* customization of the encoding */
2702 jp2_setup_header_writting(jp2);
2705 if (! jp2_exec (jp2,jp2->m_procedure_list,cio,p_manager)) {
2709 return j2k_start_compress(jp2->j2k,cio,p_image,p_manager);
2713 * Finds the execution function related to the given box id.
2715 * @param p_id the id of the handler to fetch.
2717 * @return the given handler or 00 if it could not be found.
2719 const opj_jp2_header_handler_t * jp2_find_handler (int p_id)
2721 OPJ_UINT32 i, l_handler_size = sizeof(jp2_header) / sizeof(opj_jp2_header_handler_t);
2723 for (i=0;i<l_handler_size;++i) {
2724 if (jp2_header[i].id == p_id) {
2725 return &jp2_header[i];
2732 * Finds the image execution function related to the given box id.
2734 * @param p_id the id of the handler to fetch.
2736 * @return the given handler or 00 if it could not be found.
2738 static const opj_jp2_header_handler_t * jp2_img_find_handler (
2742 OPJ_UINT32 i, l_handler_size = sizeof(jp2_img_header) / sizeof(opj_jp2_header_handler_t);
2743 for (i=0;i<l_handler_size;++i)
2745 if (jp2_img_header[i].id == p_id) {
2746 return &jp2_img_header[i];
2755 * Reads a jpeg2000 file signature box.
2757 * @param p_header_data the data contained in the signature box.
2758 * @param jp2 the jpeg2000 file codec.
2759 * @param p_header_size the size of the data contained in the signature box.
2760 * @param p_manager the user event manager.
2762 * @return true if the file signature box is valid.
2764 opj_bool jp2_read_jp_v2(
2766 unsigned char * p_header_data,
2767 unsigned int p_header_size,
2768 opj_event_mgr_t * p_manager
2771 unsigned int l_magic_number;
2774 assert(p_header_data != 00);
2776 assert(p_manager != 00);
2778 if (jp2->jp2_state != JP2_STATE_NONE) {
2779 opj_event_msg_v2(p_manager, EVT_ERROR, "The signature box must be the first box in the file.\n");
2783 /* assure length of data is correct (4 -> magic number) */
2784 if (p_header_size != 4) {
2785 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with JP signature Box size\n");
2789 /* rearrange data */
2790 opj_read_bytes(p_header_data,&l_magic_number,4);
2791 if (l_magic_number != 0x0d0a870a ) {
2792 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with JP Signature : bad magic number\n");
2796 jp2->jp2_state |= JP2_STATE_SIGNATURE;
2803 * Reads a a FTYP box - File type box
2805 * @param p_header_data the data contained in the FTYP box.
2806 * @param jp2 the jpeg2000 file codec.
2807 * @param p_header_size the size of the data contained in the FTYP box.
2808 * @param p_manager the user event manager.
2810 * @return true if the FTYP box is valid.
2812 opj_bool jp2_read_ftyp_v2(
2814 unsigned char * p_header_data,
2815 unsigned int p_header_size,
2816 opj_event_mgr_t * p_manager
2819 OPJ_UINT32 i, l_remaining_bytes;
2822 assert(p_header_data != 00);
2824 assert(p_manager != 00);
2826 if (jp2->jp2_state != JP2_STATE_SIGNATURE) {
2827 opj_event_msg_v2(p_manager, EVT_ERROR, "The ftyp box must be the second box in the file.\n");
2831 /* assure length of data is correct */
2832 if (p_header_size < 8) {
2833 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with FTYP signature Box size\n");
2837 opj_read_bytes(p_header_data,&jp2->brand,4); /* BR */
2840 opj_read_bytes(p_header_data,&jp2->minversion,4); /* MinV */
2843 l_remaining_bytes = p_header_size - 8;
2845 /* the number of remaining bytes should be a multiple of 4 */
2846 if ((l_remaining_bytes & 0x3) != 0) {
2847 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with FTYP signature Box size\n");
2852 jp2->numcl = l_remaining_bytes >> 2;
2854 jp2->cl = (unsigned int *) opj_malloc(jp2->numcl * sizeof(unsigned int));
2855 if (jp2->cl == 00) {
2856 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory with FTYP Box\n");
2859 memset(jp2->cl,0,jp2->numcl * sizeof(unsigned int));
2862 for (i = 0; i < jp2->numcl; ++i)
2864 opj_read_bytes(p_header_data,&jp2->cl[i],4); /* CLi */
2868 jp2->jp2_state |= JP2_STATE_FILE_TYPE;
2874 * Skips the Jpeg2000 Codestream Header box - JP2C Header box.
2876 * @param cio the stream to write data to.
2877 * @param jp2 the jpeg2000 file codec.
2878 * @param p_manager user event manager.
2880 * @return true if writting was successful.
2882 opj_bool jp2_skip_jp2c( opj_jp2_v2_t *jp2,
2883 struct opj_stream_private *stream,
2884 struct opj_event_mgr * p_manager )
2888 assert(stream != 00);
2889 assert(p_manager != 00);
2891 jp2->j2k_codestream_offset = opj_stream_tell(stream);
2893 if (opj_stream_skip(stream,8,p_manager) != 8) {
2901 * Reads the Jpeg2000 file Header box - JP2 Header box (warning, this is a super box).
2903 * @param p_header_data the data contained in the file header box.
2904 * @param jp2 the jpeg2000 file codec.
2905 * @param p_header_size the size of the data contained in the file header box.
2906 * @param p_manager the user event manager.
2908 * @return true if the JP2 Header box was successfully reconized.
2910 opj_bool jp2_read_jp2h_v2(
2912 unsigned char * p_header_data,
2913 unsigned int p_header_size,
2914 opj_event_mgr_t * p_manager
2917 OPJ_UINT32 l_box_size=0, l_current_data_size = 0;
2919 const opj_jp2_header_handler_t * l_current_handler;
2922 assert(p_header_data != 00);
2924 assert(p_manager != 00);
2926 /* make sure the box is well placed */
2927 if ((jp2->jp2_state & JP2_STATE_FILE_TYPE) != JP2_STATE_FILE_TYPE ) {
2928 opj_event_msg_v2(p_manager, EVT_ERROR, "The box must be the first box in the file.\n");
2932 jp2->jp2_img_state = JP2_IMG_STATE_NONE;
2934 /* iterate while remaining data */
2935 while (p_header_size > 0) {
2937 if (! jp2_read_boxhdr_char(&box,p_header_data,&l_box_size,p_header_size, p_manager)) {
2938 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream error while reading JP2 Header box\n");
2942 if (box.length > p_header_size) {
2943 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream error while reading JP2 Header box: box length is inconsistent.\n");
2947 l_current_handler = jp2_img_find_handler(box.type);
2948 l_current_data_size = box.length - l_box_size;
2949 p_header_data += l_box_size;
2951 if (l_current_handler != 00) {
2952 if (! l_current_handler->handler(jp2,p_header_data,l_current_data_size,p_manager)) {
2957 jp2->jp2_img_state |= JP2_IMG_STATE_UNKNOWN;
2960 p_header_data += l_current_data_size;
2961 p_header_size -= box.length;
2964 jp2->jp2_state |= JP2_STATE_HEADER;
2970 * Reads a box header. The box is the way data is packed inside a jpeg2000 file structure. Data is read from a character string
2972 * @param p_data the character string to read data from.
2973 * @param box the box structure to fill.
2974 * @param p_number_bytes_read pointer to an int that will store the number of bytes read from the stream (shoul usually be 2).
2975 * @param p_box_max_size the maximum number of bytes in the box.
2977 * @return true if the box is reconized, false otherwise
2979 static opj_bool jp2_read_boxhdr_char(
2982 OPJ_UINT32 * p_number_bytes_read,
2983 OPJ_UINT32 p_box_max_size,
2984 opj_event_mgr_t * p_manager
2990 assert(p_data != 00);
2992 assert(p_number_bytes_read != 00);
2993 assert(p_manager != 00);
2995 if (p_box_max_size < 8) {
2996 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot handle box of less than 8 bytes\n");
3000 /* process read data */
3001 opj_read_bytes(p_data, &l_value, 4);
3003 box->length = (OPJ_INT32)(l_value);
3005 opj_read_bytes(p_data, &l_value, 4);
3007 box->type = (OPJ_INT32)(l_value);
3009 *p_number_bytes_read = 8;
3011 /* do we have a "special very large box ?" */
3012 /* read then the XLBox */
3013 if (box->length == 1) {
3014 unsigned int l_xl_part_size;
3016 if (p_box_max_size < 16) {
3017 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot handle XL box of less than 16 bytes\n");
3021 opj_read_bytes(p_data,&l_xl_part_size, 4);
3023 *p_number_bytes_read += 4;
3025 if (l_xl_part_size != 0) {
3026 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot handle box sizes higher than 2^32\n");
3030 opj_read_bytes(p_data, &l_value, 4);
3031 *p_number_bytes_read += 4;
3032 box->length = (OPJ_INT32)(l_value);
3034 if (box->length == 0) {
3035 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot handle box of undefined sizes\n");
3039 else if (box->length == 0) {
3040 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot handle box of undefined sizes\n");
3049 * Reads a jpeg2000 file header structure.
3051 * @param cio the stream to read data from.
3052 * @param jp2 the jpeg2000 file header structure.
3053 * @param p_manager the user event manager.
3055 * @return true if the box is valid.
3057 opj_bool jp2_read_header( struct opj_stream_private *p_stream,
3059 opj_image_t** p_image,
3060 struct opj_event_mgr * p_manager
3065 assert(p_stream != 00);
3066 assert(p_manager != 00);
3068 /* customization of the validation */
3069 jp2_setup_decoding_validation (jp2);
3071 /* customization of the encoding */
3072 jp2_setup_header_reading(jp2);
3074 /* validation of the parameters codec */
3075 if (! jp2_exec(jp2,jp2->m_validation_list,p_stream,p_manager)) {
3080 if (! jp2_exec (jp2,jp2->m_procedure_list,p_stream,p_manager)) {
3084 return j2k_read_header( p_stream,
3091 * Sets up the validation ,i.e. adds the procedures to launch to make sure the codec parameters
3092 * are valid. Developers wanting to extend the library can add their own validation procedures.
3094 void jp2_setup_encoding_validation (opj_jp2_v2_t *jp2)
3099 opj_procedure_list_add_procedure(jp2->m_validation_list, (opj_procedure)jp2_default_validation);
3100 /* DEVELOPER CORNER, add your custom validation procedure */
3104 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
3105 * are valid. Developpers wanting to extend the library can add their own validation procedures.
3107 void jp2_setup_decoding_validation (opj_jp2_v2_t *jp2)
3111 /* DEVELOPER CORNER, add your custom validation procedure */
3115 * Sets up the procedures to do on writting header.
3116 * Developers wanting to extend the library can add their own writing procedures.
3118 void jp2_setup_header_writting (opj_jp2_v2_t *jp2)
3123 opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)jp2_write_jp_v2 );
3124 opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)jp2_write_ftyp_v2 );
3125 opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)jp2_write_jp2h_v2 );
3126 opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)jp2_skip_jp2c );
3128 /* DEVELOPER CORNER, insert your custom procedures */
3133 * Sets up the procedures to do on reading header.
3134 * Developpers wanting to extend the library can add their own writting procedures.
3136 void jp2_setup_header_reading (opj_jp2_v2_t *jp2)
3141 opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)jp2_read_header_procedure );
3142 /* DEVELOPER CORNER, add your custom procedures */
3147 * Reads a tile header.
3148 * @param p_j2k the jpeg2000 codec.
3149 * @param p_stream the stream to write data to.
3150 * @param p_manager the user event manager.
3152 opj_bool jp2_read_tile_header( opj_jp2_v2_t * p_jp2,
3153 OPJ_UINT32 * p_tile_index,
3154 OPJ_UINT32 * p_data_size,
3155 OPJ_INT32 * p_tile_x0, OPJ_INT32 * p_tile_y0,
3156 OPJ_INT32 * p_tile_x1, OPJ_INT32 * p_tile_y1,
3157 OPJ_UINT32 * p_nb_comps,
3159 opj_stream_private_t *p_stream,
3160 opj_event_mgr_t * p_manager )
3162 return j2k_read_tile_header(p_jp2->j2k,
3165 p_tile_x0, p_tile_y0,
3166 p_tile_x1, p_tile_y1,
3175 * @param p_j2k the jpeg2000 codec.
3176 * @param p_stream the stream to write data to.
3177 * @param p_manager the user event manager.
3179 opj_bool jp2_write_tile ( opj_jp2_v2_t *p_jp2,
3180 OPJ_UINT32 p_tile_index,
3182 OPJ_UINT32 p_data_size,
3183 struct opj_stream_private *p_stream,
3184 struct opj_event_mgr * p_manager )
3186 return j2k_write_tile (p_jp2->j2k,p_tile_index,p_data,p_data_size,p_stream,p_manager);
3191 * @param p_j2k the jpeg2000 codec.
3192 * @param p_stream the stream to write data to.
3193 * @param p_manager the user event manager.
3195 opj_bool jp2_decode_tile (
3196 opj_jp2_v2_t * p_jp2,
3197 OPJ_UINT32 p_tile_index,
3199 OPJ_UINT32 p_data_size,
3200 opj_stream_private_t *p_stream,
3201 opj_event_mgr_t * p_manager
3204 return j2k_decode_tile (p_jp2->j2k,p_tile_index,p_data,p_data_size,p_stream,p_manager);
3208 * Destroys a jpeg2000 file decompressor.
3210 * @param jp2 a jpeg2000 file decompressor.
3212 void jp2_destroy(opj_jp2_v2_t *jp2)
3215 /* destroy the J2K codec */
3216 j2k_destroy(jp2->j2k);
3220 opj_free(jp2->comps);
3229 if (jp2->color.icc_profile_buf) {
3230 opj_free(jp2->color.icc_profile_buf);
3231 jp2->color.icc_profile_buf = 00;
3234 if (jp2->color.jp2_cdef) {
3235 if (jp2->color.jp2_cdef->info) {
3236 opj_free(jp2->color.jp2_cdef->info);
3237 jp2->color.jp2_cdef->info = NULL;
3240 opj_free(jp2->color.jp2_cdef);
3241 jp2->color.jp2_cdef = 00;
3244 if (jp2->color.jp2_pclr) {
3245 if (jp2->color.jp2_pclr->cmap) {
3246 opj_free(jp2->color.jp2_pclr->cmap);
3247 jp2->color.jp2_pclr->cmap = NULL;
3249 if (jp2->color.jp2_pclr->channel_sign) {
3250 opj_free(jp2->color.jp2_pclr->channel_sign);
3251 jp2->color.jp2_pclr->channel_sign = NULL;
3253 if (jp2->color.jp2_pclr->channel_size) {
3254 opj_free(jp2->color.jp2_pclr->channel_size);
3255 jp2->color.jp2_pclr->channel_size = NULL;
3257 if (jp2->color.jp2_pclr->entries) {
3258 opj_free(jp2->color.jp2_pclr->entries);
3259 jp2->color.jp2_pclr->entries = NULL;
3262 opj_free(jp2->color.jp2_pclr);
3263 jp2->color.jp2_pclr = 00;
3266 if (jp2->m_validation_list) {
3267 opj_procedure_list_destroy(jp2->m_validation_list);
3268 jp2->m_validation_list = 00;
3271 if (jp2->m_procedure_list) {
3272 opj_procedure_list_destroy(jp2->m_procedure_list);
3273 jp2->m_procedure_list = 00;
3281 * Sets the given area to be decoded. This function should be called right after opj_read_header and before any tile header reading.
3283 * @param p_jp2 the jpeg2000 codec.
3284 * @param p_end_x the right position of the rectangle to decode (in image coordinates).
3285 * @param p_start_y the up position of the rectangle to decode (in image coordinates).
3286 * @param p_end_y the bottom position of the rectangle to decode (in image coordinates).
3287 * @param p_manager the user event manager
3289 * @return true if the area could be set.
3291 opj_bool jp2_set_decode_area( opj_jp2_v2_t *p_jp2,
3292 opj_image_t* p_image,
3293 OPJ_INT32 p_start_x, OPJ_INT32 p_start_y,
3294 OPJ_INT32 p_end_x, OPJ_INT32 p_end_y,
3295 struct opj_event_mgr * p_manager )
3297 return j2k_set_decode_area(p_jp2->j2k, p_image, p_start_x, p_start_y, p_end_x, p_end_y, p_manager);
3301 * Get the decoded tile.
3303 * @param jp2 the jpeg2000 codec.
3304 * @param p_stream input_stream
3305 * @param p_image output image. .
3306 * @param p_manager the user event manager
3307 * @param tile_index index of the tile we want decode
3309 * @return true if succeed.
3311 opj_bool jp2_get_tile( opj_jp2_v2_t *jp2,
3312 opj_stream_private_t *p_stream,
3313 opj_image_t* p_image,
3314 struct opj_event_mgr * p_manager,
3315 OPJ_UINT32 tile_index )
3320 opj_event_msg_v2(p_manager, EVT_WARNING, "JP2 box which are after the codestream will not be read by this function.\n");
3322 if (! j2k_get_tile(jp2->j2k, p_stream, p_image, p_manager, tile_index) ){
3323 opj_event_msg_v2(p_manager, EVT_ERROR, "Failed to decode the codestream in the JP2 file\n");
3327 /* Set Image Color Space */
3328 if (jp2->enumcs == 16)
3329 p_image->color_space = CLRSPC_SRGB;
3330 else if (jp2->enumcs == 17)
3331 p_image->color_space = CLRSPC_GRAY;
3332 else if (jp2->enumcs == 18)
3333 p_image->color_space = CLRSPC_SYCC;
3335 p_image->color_space = CLRSPC_UNKNOWN;
3337 /* Apply the color space if needed */
3338 if(jp2->color.jp2_cdef) {
3339 jp2_apply_cdef(p_image, &(jp2->color));
3342 if(jp2->color.jp2_pclr) {
3343 /* Part 1, I.5.3.4: Either both or none : */
3344 if( !jp2->color.jp2_pclr->cmap)
3345 jp2_free_pclr(&(jp2->color));
3347 jp2_apply_pclr(p_image, &(jp2->color));
3350 if(jp2->color.icc_profile_buf) {
3351 p_image->icc_profile_buf = jp2->color.icc_profile_buf;
3352 p_image->icc_profile_len = jp2->color.icc_profile_len;
3353 jp2->color.icc_profile_buf = NULL;
3361 /* ----------------------------------------------------------------------- */
3362 /* JP2 encoder interface */
3363 /* ----------------------------------------------------------------------- */
3365 opj_jp2_v2_t* jp2_create(opj_bool p_is_decoder)
3367 opj_jp2_v2_t *jp2 = (opj_jp2_v2_t*)opj_malloc(sizeof(opj_jp2_v2_t));
3369 memset(jp2,0,sizeof(opj_jp2_v2_t));
3371 /* create the J2K codec */
3372 if (! p_is_decoder) {
3373 jp2->j2k = j2k_create_compress_v2();
3376 jp2->j2k = opj_j2k_create_decompress();
3379 if (jp2->j2k == 00) {
3384 /* Color structure */
3385 jp2->color.icc_profile_buf = NULL;
3386 jp2->color.icc_profile_len = 0;
3387 jp2->color.jp2_cdef = NULL;
3388 jp2->color.jp2_pclr = NULL;
3389 jp2->color.jp2_has_colr = 0;
3391 /* validation list creation */
3392 jp2->m_validation_list = opj_procedure_list_create();
3393 if (! jp2->m_validation_list) {
3398 /* execution list creation */
3399 jp2->m_procedure_list = opj_procedure_list_create();
3400 if (! jp2->m_procedure_list) {
3409 void jp2_dump(opj_jp2_v2_t* p_jp2, OPJ_INT32 flag, FILE* out_stream)
3412 assert(p_jp2 != 00);
3414 j2k_dump(p_jp2->j2k,
3419 opj_codestream_index_t* jp2_get_cstr_index(opj_jp2_v2_t* p_jp2)
3421 return j2k_get_cstr_index(p_jp2->j2k);
3424 opj_codestream_info_v2_t* jp2_get_cstr_info(opj_jp2_v2_t* p_jp2)
3426 return j2k_get_cstr_info(p_jp2->j2k);
3429 opj_bool jp2_set_decoded_resolution_factor(opj_jp2_v2_t *p_jp2, OPJ_UINT32 res_factor, opj_event_mgr_t * p_manager)
3431 return j2k_set_decoded_resolution_factor(p_jp2->j2k, res_factor, p_manager);