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) 2008, Jerome Fimes, Communications & Systemes <jerome.fimes@c-s.fr>
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.
34 #include "opj_malloc.h"
37 #include "function_list.h"
40 /** @defgroup JP2 JP2 - JPEG-2000 file format reader/writer */
47 /** @name Local static functions */
53 * Writes the Jpeg2000 file Header box - JP2 Header box (warning, this is a super box).
55 * @param cio the stream to write data to.
56 * @param jp2 the jpeg2000 file codec.
57 * @param p_manager user event manager.
59 * @return true if writting was successful.
63 struct opj_stream_private *cio,
64 struct opj_event_mgr * p_manager
68 * Skips the Jpeg2000 Codestream Header box - JP2C Header box.
70 * @param cio the stream to write data to.
71 * @param jp2 the jpeg2000 file codec.
72 * @param p_manager user event manager.
74 * @return true if writting was successful.
78 struct opj_stream_private *cio,
79 struct opj_event_mgr * p_manager
83 * Reads the Jpeg2000 file Header box - JP2 Header box (warning, this is a super box).
85 * @param p_header_data the data contained in the file header box.
86 * @param jp2 the jpeg2000 file codec.
87 * @param p_header_size the size of the data contained in the file header box.
88 * @param p_manager the user event manager.
90 * @return true if the JP2 Header box was successfully reconized.
94 unsigned char * p_header_data,
95 unsigned int p_header_size,
96 struct opj_event_mgr * p_manager
100 * Writes the Jpeg2000 codestream Header box - JP2C Header box. This function must be called AFTER the coding has been done.
102 * @param cio the stream to write data to.
103 * @param jp2 the jpeg2000 file codec.
104 * @param p_manager user event manager.
106 * @return true if writting was successful.
108 static bool jp2_write_jp2c(
110 struct opj_stream_private *cio,
111 struct opj_event_mgr * p_manager
115 * Reads a box header. The box is the way data is packed inside a jpeg2000 file structure.
117 * @param cio the input stream to read data from.
118 * @param box the box structure to fill.
119 * @param p_number_bytes_read pointer to an int that will store the number of bytes read from the stream (shoul usually be 2).
120 * @param p_manager user event manager.
122 * @return true if the box is reconized, false otherwise
124 static bool jp2_read_boxhdr(
126 OPJ_UINT32 * p_number_bytes_read,
127 struct opj_stream_private *cio,
128 struct opj_event_mgr * p_manager
132 * Reads a box header. The box is the way data is packed inside a jpeg2000 file structure. Data is read from a character string
134 * @param p_data the character string to read data from.
135 * @param box the box structure to fill.
136 * @param p_number_bytes_read pointer to an int that will store the number of bytes read from the stream (shoul usually be 2).
137 * @param p_box_max_size the maximum number of bytes in the box.
139 * @return true if the box is reconized, false otherwise
141 static bool jp2_read_boxhdr_char(
144 OPJ_UINT32 * p_number_bytes_read,
145 OPJ_UINT32 p_box_max_size,
146 struct opj_event_mgr * p_manager
150 * Reads a jpeg2000 file signature box.
152 * @param p_header_data the data contained in the signature box.
153 * @param jp2 the jpeg2000 file codec.
154 * @param p_header_size the size of the data contained in the signature box.
155 * @param p_manager the user event manager.
157 * @return true if the file signature box is valid.
159 static bool jp2_read_jp(
161 unsigned char * p_header_data,
162 unsigned int p_header_size,
163 struct opj_event_mgr * p_manager
167 * Writes a jpeg2000 file signature box.
169 * @param cio the stream to write data to.
170 * @param jp2 the jpeg2000 file codec.
171 * @param p_manager the user event manager.
173 * @return true if writting was successful.
175 static bool jp2_write_jp(
177 struct opj_stream_private *cio,
178 struct opj_event_mgr * p_manager
182 * Writes a FTYP box - File type box
184 * @param cio the stream to write data to.
185 * @param jp2 the jpeg2000 file codec.
186 * @param p_manager the user event manager.
188 * @return true if writting was successful.
190 static bool jp2_write_ftyp(
192 struct opj_stream_private *cio,
193 struct opj_event_mgr * p_manager
197 * Reads a a FTYP box - File type box
199 * @param p_header_data the data contained in the FTYP box.
200 * @param jp2 the jpeg2000 file codec.
201 * @param p_header_size the size of the data contained in the FTYP box.
202 * @param p_manager the user event manager.
204 * @return true if the FTYP box is valid.
206 static bool jp2_read_ftyp(
208 unsigned char * p_header_data,
209 unsigned int p_header_size,
210 struct opj_event_mgr * p_manager
214 * Reads a IHDR box - Image Header box
216 * @param p_image_header_data pointer to actual data (already read from file)
217 * @param jp2 the jpeg2000 file codec.
218 * @param p_image_header_size the size of the image header
219 * @param p_manager the user event manager.
221 * @return true if the image header is valid, fale else.
223 static bool jp2_read_ihdr(
225 unsigned char * p_image_header_data,
226 unsigned int p_image_header_size,
227 struct opj_event_mgr * p_manager
231 * Writes the Image Header box - Image Header box.
233 * @param jp2 jpeg2000 file codec.
234 * @param p_nb_bytes_written pointer to store the nb of bytes written by the function.
236 * @return the data being copied.
238 static unsigned char * jp2_write_ihdr(
240 unsigned int * p_nb_bytes_written
244 * Reads a Bit per Component box.
246 * @param p_bpc_header_data pointer to actual data (already read from file)
247 * @param jp2 the jpeg2000 file codec.
248 * @param p_bpc_header_size the size of the bpc header
249 * @param p_manager the user event manager.
251 * @return true if the bpc header is valid, fale else.
253 static bool jp2_read_bpcc(
255 unsigned char * p_bpc_header_data,
256 unsigned int p_bpc_header_size,
257 struct opj_event_mgr * p_manager
262 * Writes the Bit per Component box.
264 * @param jp2 jpeg2000 file codec.
265 * @param p_nb_bytes_written pointer to store the nb of bytes written by the function.
267 * @return the data being copied.
269 static unsigned char * jp2_write_bpcc(
271 unsigned int * p_nb_bytes_written
275 * Reads the Colour Specification box.
277 * @param p_colr_header_data pointer to actual data (already read from file)
278 * @param jp2 the jpeg2000 file codec.
279 * @param p_colr_header_size the size of the color header
280 * @param p_manager the user event manager.
282 * @return true if the bpc header is valid, fale else.
284 static bool jp2_read_colr(
286 unsigned char * p_colr_header_data,
287 unsigned int p_colr_header_size,
288 struct opj_event_mgr * p_manager
292 * Writes the Colour Specification box.
294 * @param jp2 jpeg2000 file codec.
295 * @param p_nb_bytes_written pointer to store the nb of bytes written by the function.
297 * @return the data being copied.
299 static unsigned char *jp2_write_colr(
301 unsigned int * p_nb_bytes_written
306 * Reads a jpeg2000 file header structure.
308 * @param cio the stream to read data from.
309 * @param jp2 the jpeg2000 file header structure.
310 * @param p_manager the user event manager.
312 * @return true if the box is valid.
314 bool jp2_read_header_procedure(
316 struct opj_stream_private *cio,
317 struct opj_event_mgr * p_manager
320 * Excutes the given procedures on the given codec.
322 * @param p_procedure_list the list of procedures to execute
323 * @param jp2 the jpeg2000 file codec to execute the procedures on.
324 * @param cio the stream to execute the procedures on.
325 * @param p_manager the user manager.
327 * @return true if all the procedures were successfully executed.
329 static bool jp2_exec (
331 struct opj_procedure_list * p_procedure_list,
332 struct opj_stream_private *cio,
333 struct opj_event_mgr * p_manager
336 * Finds the execution function related to the given box id.
338 * @param p_id the id of the handler to fetch.
340 * @return the given handler or NULL if it could not be found.
342 static const opj_jp2_header_handler_t * jp2_find_handler (int p_id);
345 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
346 * are valid. Developpers wanting to extend the library can add their own validation procedures.
348 static void jp2_setup_encoding_validation (opj_jp2_t *jp2);
351 * Sets up the procedures to do on writting header. Developpers wanting to extend the library can add their own writting procedures.
353 static void jp2_setup_header_writting (opj_jp2_t *jp2);
356 * The default validation procedure without any extension.
358 * @param jp2 the jpeg2000 codec to validate.
359 * @param cio the input stream to validate.
360 * @param p_manager the user event manager.
362 * @return true if the parameters are correct.
364 bool jp2_default_validation (
366 struct opj_stream_private *cio,
367 struct opj_event_mgr * p_manager
371 * Finds the execution function related to the given box id.
373 * @param p_id the id of the handler to fetch.
375 * @return the given handler or NULL if it could not be found.
377 static const opj_jp2_header_handler_t * jp2_find_handler (
382 * Finds the image execution function related to the given box id.
384 * @param p_id the id of the handler to fetch.
386 * @return the given handler or NULL if it could not be found.
388 static const opj_jp2_header_handler_t * jp2_img_find_handler (
393 * Sets up the procedures to do on writting header after the codestream.
394 * Developpers wanting to extend the library can add their own writting procedures.
396 static void jp2_setup_end_header_writting (opj_jp2_t *jp2);
399 * Sets up the procedures to do on reading header after the codestream.
400 * Developpers wanting to extend the library can add their own writting procedures.
402 static void jp2_setup_end_header_reading (opj_jp2_t *jp2);
405 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
406 * are valid. Developpers wanting to extend the library can add their own validation procedures.
408 static void jp2_setup_decoding_validation (opj_jp2_t *jp2);
411 * Sets up the procedures to do on reading header.
412 * Developpers wanting to extend the library can add their own writting procedures.
414 static void jp2_setup_header_reading (opj_jp2_t *jp2);
419 /* ----------------------------------------------------------------------- */
420 const opj_jp2_header_handler_t jp2_header [] =
422 {JP2_JP,jp2_read_jp},
423 {JP2_FTYP,jp2_read_ftyp},
424 {JP2_JP2H,jp2_read_jp2h}
427 const opj_jp2_header_handler_t jp2_img_header [] =
429 {JP2_IHDR,jp2_read_ihdr},
430 {JP2_COLR,jp2_read_colr},
431 {JP2_BPCC,jp2_read_bpcc}
434 * Finds the execution function related to the given box id.
436 * @param p_id the id of the handler to fetch.
438 * @return the given handler or 00 if it could not be found.
440 const opj_jp2_header_handler_t * jp2_find_handler (
444 unsigned int i, l_handler_size = sizeof(jp2_header) / sizeof(opj_jp2_header_handler_t);
446 (i=0;i<l_handler_size;++i)
449 (jp2_header[i].id == p_id)
451 return &jp2_header[i];
458 * Finds the image execution function related to the given box id.
460 * @param p_id the id of the handler to fetch.
462 * @return the given handler or 00 if it could not be found.
464 static const opj_jp2_header_handler_t * jp2_img_find_handler (
468 unsigned int i, l_handler_size = sizeof(jp2_img_header) / sizeof(opj_jp2_header_handler_t);
470 (i=0;i<l_handler_size;++i)
473 (jp2_img_header[i].id == p_id)
475 return &jp2_img_header[i];
483 * Reads a jpeg2000 file header structure.
485 * @param cio the stream to read data from.
486 * @param jp2 the jpeg2000 file header structure.
487 * @param p_manager the user event manager.
489 * @return true if the box is valid.
491 bool jp2_read_header_procedure(
493 opj_stream_private_t *cio,
494 opj_event_mgr_t * p_manager)
497 unsigned int l_nb_bytes_read;
498 const opj_jp2_header_handler_t * l_current_handler;
499 unsigned int l_last_data_size = BOX_SIZE;
500 unsigned int l_current_data_size;
501 unsigned char * l_current_data = 00;
506 assert(p_manager != 00);
508 l_current_data = opj_malloc(l_last_data_size);
511 (l_current_data == 00)
513 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to handle jpeg2000 file header\n");
516 memset(l_current_data, 0 , l_last_data_size);
518 (jp2_read_boxhdr(&box,&l_nb_bytes_read,cio,p_manager))
520 // is it the codestream box ?
522 (box.type == JP2_JP2C)
525 (jp2->jp2_state & JP2_STATE_HEADER)
527 jp2->jp2_state |= JP2_STATE_CODESTREAM;
532 opj_event_msg(p_manager, EVT_ERROR, "bad placed jpeg codestream\n");
539 opj_event_msg(p_manager, EVT_ERROR, "Cannot handle box of undefined sizes\n");
543 l_current_handler = jp2_find_handler(box.type);
544 l_current_data_size = box.length - l_nb_bytes_read;
547 (l_current_handler != 00)
550 (l_current_data_size > l_last_data_size)
552 l_current_data = opj_realloc(l_current_data,l_current_data_size);
553 l_last_data_size = l_current_data_size;
555 l_nb_bytes_read = opj_stream_read_data(cio,l_current_data,l_current_data_size,p_manager);
557 (l_nb_bytes_read != l_current_data_size)
559 opj_event_msg(p_manager, EVT_ERROR, "Problem with reading JPEG2000 box, stream error\n");
563 (! l_current_handler->handler(jp2,l_current_data,l_current_data_size,p_manager))
570 jp2->jp2_state |= JP2_STATE_UNKNOWN;
572 (opj_stream_skip(cio,l_current_data_size,p_manager) != l_current_data_size)
574 opj_event_msg(p_manager, EVT_ERROR, "Problem with skipping JPEG2000 box, stream error\n");
583 * Reads a box header. The box is the way data is packed inside a jpeg2000 file structure.
585 * @param cio the input stream to read data from.
586 * @param box the box structure to fill.
587 * @param p_number_bytes_read pointer to an int that will store the number of bytes read from the stream (should usually be 8).
588 * @param p_manager user event manager.
590 * @return true if the box is reconized, false otherwise
592 bool jp2_read_boxhdr(opj_jp2_box_t *box, OPJ_UINT32 * p_number_bytes_read,opj_stream_private_t *cio, opj_event_mgr_t * p_manager)
594 /* read header from file */
595 unsigned char l_data_header [8];
600 assert(p_number_bytes_read != 00);
601 assert(p_manager != 00);
603 *p_number_bytes_read = opj_stream_read_data(cio,l_data_header,8,p_manager);
605 (*p_number_bytes_read != 8)
609 /* process read data */
610 opj_read_bytes(l_data_header,&(box->length), 4);
611 opj_read_bytes(l_data_header+4,&(box->type), 4);
613 // do we have a "special very large box ?"
614 // read then the XLBox
618 OPJ_UINT32 l_xl_part_size;
619 OPJ_UINT32 l_nb_bytes_read = opj_stream_read_data(cio,l_data_header,8,p_manager);
621 (l_nb_bytes_read != 8)
624 (l_nb_bytes_read > 0)
626 *p_number_bytes_read += l_nb_bytes_read;
630 opj_read_bytes(l_data_header,&l_xl_part_size, 4);
632 (l_xl_part_size != 0)
634 opj_event_msg(p_manager, EVT_ERROR, "Cannot handle box sizes higher than 2^32\n");
637 opj_read_bytes(l_data_header,&(box->length), 4);
643 * Reads a box header. The box is the way data is packed inside a jpeg2000 file structure. Data is read from a character string
645 * @param p_data the character string to read data from.
646 * @param box the box structure to fill.
647 * @param p_number_bytes_read pointer to an int that will store the number of bytes read from the stream (shoul usually be 2).
648 * @param p_box_max_size the maximum number of bytes in the box.
650 * @return true if the box is reconized, false otherwise
652 static bool jp2_read_boxhdr_char(
655 OPJ_UINT32 * p_number_bytes_read,
656 OPJ_UINT32 p_box_max_size,
657 opj_event_mgr_t * p_manager
661 assert(p_data != 00);
663 assert(p_number_bytes_read != 00);
664 assert(p_manager != 00);
669 opj_event_msg(p_manager, EVT_ERROR, "Cannot handle box of less than 8 bytes\n");
672 /* process read data */
673 opj_read_bytes(p_data,&(box->length), 4);
675 opj_read_bytes(p_data,&(box->type), 4);
677 *p_number_bytes_read = 8;
679 // do we have a "special very large box ?"
680 // read then the XLBox
684 unsigned int l_xl_part_size;
686 (p_box_max_size < 16)
688 opj_event_msg(p_manager, EVT_ERROR, "Cannot handle XL box of less than 16 bytes\n");
692 opj_read_bytes(p_data,&l_xl_part_size, 4);
694 *p_number_bytes_read += 4;
696 (l_xl_part_size != 0)
698 opj_event_msg(p_manager, EVT_ERROR, "Cannot handle box sizes higher than 2^32\n");
701 opj_read_bytes(p_data,&(box->length), 4);
702 *p_number_bytes_read += 4;
706 opj_event_msg(p_manager, EVT_ERROR, "Cannot handle box of undefined sizes\n");
714 opj_event_msg(p_manager, EVT_ERROR, "Cannot handle box of undefined sizes\n");
722 * Reads a jpeg2000 file signature box.
724 * @param p_header_data the data contained in the signature box.
725 * @param jp2 the jpeg2000 file codec.
726 * @param p_header_size the size of the data contained in the signature box.
727 * @param p_manager the user event manager.
729 * @return true if the file signature box is valid.
733 unsigned char * p_header_data,
734 unsigned int p_header_size,
735 opj_event_mgr_t * p_manager
738 unsigned int l_magic_number;
741 assert(p_header_data != 00);
743 assert(p_manager != 00);
746 (jp2->jp2_state != JP2_STATE_NONE)
748 opj_event_msg(p_manager, EVT_ERROR, "The signature box must be the first box in the file.\n");
753 /* assure length of data is correct (4 -> magic number) */
757 opj_event_msg(p_manager, EVT_ERROR, "Error with JP signature Box size\n");
762 opj_read_bytes(p_header_data,&l_magic_number,4);
764 (l_magic_number != 0x0d0a870a )
766 opj_event_msg(p_manager, EVT_ERROR, "Error with JP Signature : bad magic number\n");
769 jp2->jp2_state |= JP2_STATE_SIGNATURE;
774 * Reads a a FTYP box - File type box
776 * @param p_header_data the data contained in the FTYP box.
777 * @param jp2 the jpeg2000 file codec.
778 * @param p_header_size the size of the data contained in the FTYP box.
779 * @param p_manager the user event manager.
781 * @return true if the FTYP box is valid.
785 unsigned char * p_header_data,
786 unsigned int p_header_size,
787 opj_event_mgr_t * p_manager
791 unsigned int l_remaining_bytes;
794 assert(p_header_data != 00);
796 assert(p_manager != 00);
799 (jp2->jp2_state != JP2_STATE_SIGNATURE)
801 opj_event_msg(p_manager, EVT_ERROR, "The ftyp box must be the second box in the file.\n");
805 /* assure length of data is correct */
809 opj_event_msg(p_manager, EVT_ERROR, "Error with FTYP signature Box size\n");
813 opj_read_bytes(p_header_data,&jp2->brand,4); /* BR */
816 opj_read_bytes(p_header_data,&jp2->minversion,4); /* MinV */
819 l_remaining_bytes = p_header_size - 8;
821 /* the number of remaining bytes should be a multiple of 4 */
823 ((l_remaining_bytes & 0x3) != 0)
825 opj_event_msg(p_manager, EVT_ERROR, "Error with FTYP signature Box size\n");
829 jp2->numcl = l_remaining_bytes >> 2;
833 jp2->cl = (unsigned int *) opj_malloc(jp2->numcl * sizeof(unsigned int));
837 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory with FTYP Box\n");
840 memset(jp2->cl,0,jp2->numcl * sizeof(unsigned int));
845 (i = 0; i < jp2->numcl; ++i)
847 opj_read_bytes(p_header_data,&jp2->cl[i],4); /* CLi */
851 jp2->jp2_state |= JP2_STATE_FILE_TYPE;
856 * Writes a jpeg2000 file signature box.
858 * @param cio the stream to write data to.
859 * @param jp2 the jpeg2000 file codec.
860 * @param p_manager the user event manager.
862 * @return true if writting was successful.
866 opj_stream_private_t *cio,
867 opj_event_mgr_t * p_manager
870 /* 12 bytes will be read */
871 unsigned char l_signature_data [12];
876 assert(p_manager != 00);
879 /* write box length */
880 opj_write_bytes(l_signature_data,12,4);
881 /* writes box type */
882 opj_write_bytes(l_signature_data+4,JP2_JP,4);
883 /* writes magic number*/
884 opj_write_bytes(l_signature_data+8,0x0d0a870a,4);
886 (opj_stream_write_data(cio,l_signature_data,12,p_manager) != 12)
895 * Writes a FTYP box - File type box
897 * @param cio the stream to write data to.
898 * @param jp2 the jpeg2000 file codec.
899 * @param p_manager the user event manager.
901 * @return true if writting was successful.
905 opj_stream_private_t *cio,
906 opj_event_mgr_t * p_manager
910 unsigned int l_ftyp_size = 16 + 4 * jp2->numcl;
911 unsigned char * l_ftyp_data, * l_current_data_ptr;
917 assert(p_manager != 00);
919 l_ftyp_data = (unsigned char *) opj_malloc(l_ftyp_size);
924 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to handle ftyp data\n");
927 memset(l_ftyp_data,0,l_ftyp_size);
929 l_current_data_ptr = l_ftyp_data;
931 opj_write_bytes(l_current_data_ptr, l_ftyp_size,4); /* box size */
932 l_current_data_ptr += 4;
934 opj_write_bytes(l_current_data_ptr, JP2_FTYP,4); /* FTYP */
935 l_current_data_ptr += 4;
937 opj_write_bytes(l_current_data_ptr, jp2->brand,4); /* BR */
938 l_current_data_ptr += 4;
940 opj_write_bytes(l_current_data_ptr, jp2->minversion,4); /* MinV */
941 l_current_data_ptr += 4;
944 (i = 0; i < jp2->numcl; i++)
946 opj_write_bytes(l_current_data_ptr, jp2->cl[i],4); /* CL */
949 l_result = (opj_stream_write_data(cio,l_ftyp_data,l_ftyp_size,p_manager) == l_ftyp_size);
953 opj_event_msg(p_manager, EVT_ERROR, "Error while writting ftyp data to stream\n");
955 opj_free(l_ftyp_data);
960 * Writes the Jpeg2000 file Header box - JP2 Header box (warning, this is a super box).
962 * @param cio the stream to write data to.
963 * @param jp2 the jpeg2000 file codec.
964 * @param p_manager user event manager.
966 * @return true if writting was successful.
970 opj_stream_private_t *cio,
971 opj_event_mgr_t * p_manager
974 opj_jp2_img_header_writer_handler_t l_writers [3];
975 opj_jp2_img_header_writer_handler_t * l_current_writer;
978 /* size of data for super box*/
980 bool l_result = true;
982 /* to store the data of the super box */
983 unsigned char l_jp2h_data [8];
988 assert(p_manager != 00);
990 memset(l_writers,0,sizeof(l_writers));
996 l_writers[0].handler = jp2_write_ihdr;
997 l_writers[1].handler = jp2_write_bpcc;
998 l_writers[2].handler = jp2_write_colr;
1003 l_writers[0].handler = jp2_write_ihdr;
1004 l_writers[1].handler = jp2_write_colr;
1007 /* write box header */
1008 /* write JP2H type */
1009 opj_write_bytes(l_jp2h_data+4,JP2_JP2H,4);
1011 l_current_writer = l_writers;
1013 (i=0;i<l_nb_pass;++i)
1015 l_current_writer->m_data = l_current_writer->handler(jp2,&(l_current_writer->m_size));
1017 (l_current_writer->m_data == 00)
1019 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to hold JP2 Header data\n");
1023 l_jp2h_size += l_current_writer->m_size;
1030 l_current_writer = l_writers;
1032 (i=0;i<l_nb_pass;++i)
1035 (l_current_writer->m_data != 00)
1037 opj_free(l_current_writer->m_data );
1044 /* write super box size */
1045 opj_write_bytes(l_jp2h_data,l_jp2h_size,4);
1047 /* write super box data on stream */
1049 (opj_stream_write_data(cio,l_jp2h_data,8,p_manager) != 8)
1051 opj_event_msg(p_manager, EVT_ERROR, "Stream error while writting JP2 Header box\n");
1058 l_current_writer = l_writers;
1060 (i=0;i<l_nb_pass;++i)
1063 (opj_stream_write_data(cio,l_current_writer->m_data,l_current_writer->m_size,p_manager) != l_current_writer->m_size)
1065 opj_event_msg(p_manager, EVT_ERROR, "Stream error while writting JP2 Header box\n");
1072 l_current_writer = l_writers;
1075 (i=0;i<l_nb_pass;++i)
1078 (l_current_writer->m_data != 00)
1080 opj_free(l_current_writer->m_data );
1088 * Reads the Jpeg2000 file Header box - JP2 Header box (warning, this is a super box).
1090 * @param p_header_data the data contained in the file header box.
1091 * @param jp2 the jpeg2000 file codec.
1092 * @param p_header_size the size of the data contained in the file header box.
1093 * @param p_manager the user event manager.
1095 * @return true if the JP2 Header box was successfully reconized.
1099 unsigned char * p_header_data,
1100 unsigned int p_header_size,
1101 opj_event_mgr_t * p_manager
1104 unsigned int l_box_size=0, l_current_data_size = 0;
1106 const opj_jp2_header_handler_t * l_current_handler;
1109 assert(p_header_data != 00);
1111 assert(p_manager != 00);
1113 /* make sure the box is well placed */
1115 ((jp2->jp2_state & JP2_STATE_FILE_TYPE) != JP2_STATE_FILE_TYPE )
1117 opj_event_msg(p_manager, EVT_ERROR, "The box must be the first box in the file.\n");
1120 jp2->jp2_img_state = JP2_IMG_STATE_NONE;
1122 /* iterate while remaining data */
1127 (! jp2_read_boxhdr_char(&box,p_header_data,&l_box_size,p_header_size, p_manager))
1129 opj_event_msg(p_manager, EVT_ERROR, "Stream error while reading JP2 Header box\n");
1133 (box.length > p_header_size)
1135 opj_event_msg(p_manager, EVT_ERROR, "Stream error while reading JP2 Header box\n");
1138 l_current_handler = jp2_img_find_handler(box.type);
1140 l_current_data_size = box.length - l_box_size;
1141 p_header_data += l_box_size;
1144 (l_current_handler != 00)
1147 (! l_current_handler->handler(jp2,p_header_data,l_current_data_size,p_manager))
1154 jp2->jp2_img_state |= JP2_IMG_STATE_UNKNOWN;
1156 p_header_data += l_current_data_size;
1157 p_header_size -= box.length;
1159 jp2->jp2_state |= JP2_STATE_HEADER;
1164 * Reads a IHDR box - Image Header box
1166 * @param p_image_header_data pointer to actual data (already read from file)
1167 * @param jp2 the jpeg2000 file codec.
1168 * @param p_image_header_size the size of the image header
1169 * @param p_image_header_max_size maximum size of the header, any size bigger than this value should result the function to output false.
1170 * @param p_manager the user event manager.
1172 * @return true if the image header is valid, fale else.
1176 unsigned char * p_image_header_data,
1177 unsigned int p_image_header_size,
1178 opj_event_mgr_t * p_manager
1182 assert(p_image_header_data != 00);
1184 assert(p_manager != 00);
1187 (p_image_header_size != 14)
1189 opj_event_msg(p_manager, EVT_ERROR, "Bad image header box (bad size)\n");
1192 opj_read_bytes(p_image_header_data,&(jp2->h),4); /* HEIGHT */
1193 p_image_header_data += 4;
1194 opj_read_bytes(p_image_header_data,&(jp2->w),4); /* WIDTH */
1195 p_image_header_data += 4;
1196 opj_read_bytes(p_image_header_data,&(jp2->numcomps),2); /* NC */
1197 p_image_header_data += 2;
1199 /* allocate memory for components */
1200 jp2->comps = (opj_jp2_comps_t*) opj_malloc(jp2->numcomps * sizeof(opj_jp2_comps_t));
1204 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to handle image header (ihdr)\n");
1207 memset(jp2->comps,0,jp2->numcomps * sizeof(opj_jp2_comps_t));
1209 opj_read_bytes(p_image_header_data,&(jp2->bpc),1); /* BPC */
1210 ++ p_image_header_data;
1211 opj_read_bytes(p_image_header_data,&(jp2->C),1); /* C */
1212 ++ p_image_header_data;
1213 opj_read_bytes(p_image_header_data,&(jp2->UnkC),1); /* UnkC */
1214 ++ p_image_header_data;
1215 opj_read_bytes(p_image_header_data,&(jp2->IPR),1); /* IPR */
1216 ++ p_image_header_data;
1221 * Writes the Image Header box - Image Header box.
1223 * @param jp2 jpeg2000 file codec.
1224 * @param p_nb_bytes_written pointer to store the nb of bytes written by the function.
1226 * @return the data being copied.
1228 static unsigned char * jp2_write_ihdr(
1230 unsigned int * p_nb_bytes_written
1233 unsigned char * l_ihdr_data,* l_current_ihdr_ptr;
1237 assert(p_nb_bytes_written != 00);
1239 /* default image header is 22 bytes wide */
1240 l_ihdr_data = (unsigned char *) opj_malloc(22);
1246 memset(l_ihdr_data,0,22);
1248 l_current_ihdr_ptr = l_ihdr_data;
1250 opj_write_bytes(l_current_ihdr_ptr,22,4); /* write box size */
1251 l_current_ihdr_ptr+=4;
1252 opj_write_bytes(l_current_ihdr_ptr,JP2_IHDR, 4); /* IHDR */
1253 l_current_ihdr_ptr+=4;
1254 opj_write_bytes(l_current_ihdr_ptr,jp2->h, 4); /* HEIGHT */
1255 l_current_ihdr_ptr+=4;
1256 opj_write_bytes(l_current_ihdr_ptr, jp2->w, 4); /* WIDTH */
1257 l_current_ihdr_ptr+=4;
1258 opj_write_bytes(l_current_ihdr_ptr, jp2->numcomps, 2); /* NC */
1259 l_current_ihdr_ptr+=2;
1260 opj_write_bytes(l_current_ihdr_ptr, jp2->bpc, 1); /* BPC */
1261 ++l_current_ihdr_ptr;
1262 opj_write_bytes(l_current_ihdr_ptr, jp2->C, 1); /* C : Always 7 */
1263 ++l_current_ihdr_ptr;
1264 opj_write_bytes(l_current_ihdr_ptr, jp2->UnkC, 1); /* UnkC, colorspace unknown */
1265 ++l_current_ihdr_ptr;
1266 opj_write_bytes(l_current_ihdr_ptr, jp2->IPR, 1); /* IPR, no intellectual property */
1267 ++l_current_ihdr_ptr;
1268 *p_nb_bytes_written = 22;
1273 * Writes the Bit per Component box.
1275 * @param jp2 jpeg2000 file codec.
1276 * @param p_nb_bytes_written pointer to store the nb of bytes written by the function.
1278 * @return the data being copied.
1280 unsigned char * jp2_write_bpcc(
1282 unsigned int * p_nb_bytes_written
1286 /* room for 8 bytes for box and 1 byte for each component */
1287 int l_bpcc_size = 8 + jp2->numcomps;
1288 unsigned char * l_bpcc_data,* l_current_bpcc_ptr;
1292 assert(p_nb_bytes_written != 00);
1294 l_bpcc_data = (unsigned char *) opj_malloc(l_bpcc_size);
1300 memset(l_bpcc_data,0,l_bpcc_size);
1302 l_current_bpcc_ptr = l_bpcc_data;
1304 opj_write_bytes(l_current_bpcc_ptr,l_bpcc_size,4); /* write box size */
1305 l_current_bpcc_ptr += 4;
1306 opj_write_bytes(l_current_bpcc_ptr,JP2_BPCC,4); /* BPCC */
1307 l_current_bpcc_ptr += 4;
1310 (i = 0; i < jp2->numcomps; ++i)
1312 opj_write_bytes(l_current_bpcc_ptr, jp2->comps[i].bpcc, 1); /* write each component information */
1313 ++l_current_bpcc_ptr;
1315 *p_nb_bytes_written = l_bpcc_size;
1320 * Reads a Bit per Component box.
1322 * @param p_bpc_header_data pointer to actual data (already read from file)
1323 * @param jp2 the jpeg2000 file codec.
1324 * @param p_bpc_header_size pointer that will hold the size of the bpc header
1325 * @param p_bpc_header_max_size maximum size of the header, any size bigger than this value should result the function to output false.
1326 * @param p_manager the user event manager.
1328 * @return true if the bpc header is valid, fale else.
1332 unsigned char * p_bpc_header_data,
1333 unsigned int p_bpc_header_size,
1334 opj_event_mgr_t * p_manager
1340 assert(p_bpc_header_data != 00);
1342 assert(p_manager != 00);
1344 // and length is relevant
1346 (p_bpc_header_size != jp2->numcomps)
1348 opj_event_msg(p_manager, EVT_ERROR, "Bad BPCC header box (bad size)\n");
1352 // read info for each component
1354 (i = 0; i < jp2->numcomps; ++i)
1356 opj_read_bytes(p_bpc_header_data,&jp2->comps[i].bpcc ,1); /* read each BPCC component */
1357 ++p_bpc_header_data;
1363 * Writes the Colour Specification box.
1365 * @param jp2 jpeg2000 file codec.
1366 * @param p_nb_bytes_written pointer to store the nb of bytes written by the function.
1368 * @return the data being copied.
1370 unsigned char *jp2_write_colr(
1372 unsigned int * p_nb_bytes_written
1375 /* room for 8 bytes for box 3 for common data and variable upon profile*/
1376 unsigned int l_colr_size = 11;
1377 unsigned char * l_colr_data,* l_current_colr_ptr;
1381 assert(p_nb_bytes_written != 00);
1396 l_colr_data = (unsigned char *) opj_malloc(l_colr_size);
1402 memset(l_colr_data,0,l_colr_size);
1403 l_current_colr_ptr = l_colr_data;
1405 opj_write_bytes(l_current_colr_ptr,l_colr_size,4); /* write box size */
1406 l_current_colr_ptr += 4;
1407 opj_write_bytes(l_current_colr_ptr,JP2_COLR,4); /* BPCC */
1408 l_current_colr_ptr += 4;
1410 opj_write_bytes(l_current_colr_ptr, jp2->meth,1); /* METH */
1411 ++l_current_colr_ptr;
1412 opj_write_bytes(l_current_colr_ptr, jp2->precedence,1); /* PRECEDENCE */
1413 ++l_current_colr_ptr;
1414 opj_write_bytes(l_current_colr_ptr, jp2->approx,1); /* APPROX */
1415 ++l_current_colr_ptr;
1420 opj_write_bytes(l_current_colr_ptr, jp2->enumcs,4); /* EnumCS */
1424 opj_write_bytes(l_current_colr_ptr, 0, 1); /* PROFILE (??) */
1426 *p_nb_bytes_written = l_colr_size;
1431 * Reads the Colour Specification box.
1433 * @param p_colr_header_data pointer to actual data (already read from file)
1434 * @param jp2 the jpeg2000 file codec.
1435 * @param p_colr_header_size pointer that will hold the size of the color header
1436 * @param p_colr_header_max_size maximum size of the header, any size bigger than this value should result the function to output false.
1437 * @param p_manager the user event manager.
1439 * @return true if the bpc header is valid, fale else.
1443 unsigned char * p_colr_header_data,
1444 unsigned int p_colr_header_size,
1445 opj_event_mgr_t * p_manager
1450 assert(p_colr_header_data != 00);
1451 assert(p_manager != 00);
1454 (p_colr_header_size < 3)
1456 opj_event_msg(p_manager, EVT_ERROR, "Bad BPCC header box (bad size)\n");
1460 opj_read_bytes(p_colr_header_data,&jp2->meth ,1); /* METH */
1461 ++p_colr_header_data;
1463 opj_read_bytes(p_colr_header_data,&jp2->precedence ,1); /* PRECEDENCE */
1464 ++p_colr_header_data;
1466 opj_read_bytes(p_colr_header_data,&jp2->approx ,1); /* APPROX */
1467 ++p_colr_header_data;
1474 (p_colr_header_size != 7)
1476 opj_event_msg(p_manager, EVT_ERROR, "Bad BPCC header box (bad size)\n");
1479 opj_read_bytes(p_colr_header_data,&jp2->enumcs ,4); /* EnumCS */
1483 // do not care with profiles.
1489 * Writes the Jpeg2000 codestream Header box - JP2C Header box.
1491 * @param cio the stream to write data to.
1492 * @param jp2 the jpeg2000 file codec.
1493 * @param p_manager user event manager.
1495 * @return true if writting was successful.
1497 bool jp2_write_jp2c(
1499 opj_stream_private_t *cio,
1500 opj_event_mgr_t * p_manager
1503 unsigned int j2k_codestream_exit;
1504 unsigned char l_data_header [8];
1509 assert(p_manager != 00);
1510 assert(opj_stream_has_seek(cio));
1512 j2k_codestream_exit = opj_stream_tell(cio);
1513 opj_write_bytes(l_data_header,j2k_codestream_exit - jp2->j2k_codestream_offset,4); /* size of codestream */
1514 opj_write_bytes(l_data_header + 4,JP2_JP2C,4); /* JP2C */
1517 (! opj_stream_seek(cio,jp2->j2k_codestream_offset,p_manager))
1519 opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
1524 (opj_stream_write_data(cio,l_data_header,8,p_manager) != 8)
1526 opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
1531 (! opj_stream_seek(cio,j2k_codestream_exit,p_manager))
1533 opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
1540 * Destroys a jpeg2000 file decompressor.
1542 * @param jp2 a jpeg2000 file decompressor.
1544 void jp2_destroy(opj_jp2_t *jp2)
1549 /* destroy the J2K codec */
1550 j2k_destroy(jp2->j2k);
1555 opj_free(jp2->comps);
1565 (jp2->m_validation_list)
1567 opj_procedure_list_destroy(jp2->m_validation_list);
1568 jp2->m_validation_list = 00;
1571 (jp2->m_procedure_list)
1573 opj_procedure_list_destroy(jp2->m_procedure_list);
1574 jp2->m_procedure_list = 00;
1584 /* ----------------------------------------------------------------------- */
1585 /* JP2 encoder interface */
1586 /* ----------------------------------------------------------------------- */
1588 opj_jp2_t* jp2_create(bool p_is_decoder)
1590 opj_jp2_t *jp2 = (opj_jp2_t*)opj_malloc(sizeof(opj_jp2_t));
1594 memset(jp2,0,sizeof(opj_jp2_t));
1595 /* create the J2K codec */
1599 jp2->j2k = j2k_create_compress();
1603 jp2->j2k = j2k_create_decompress();
1611 // validation list creation
1612 jp2->m_validation_list = opj_procedure_list_create();
1614 (! jp2->m_validation_list)
1620 // execution list creation
1621 jp2->m_procedure_list = opj_procedure_list_create();
1623 (! jp2->m_procedure_list)
1633 * Excutes the given procedures on the given codec.
1635 * @param p_procedure_list the list of procedures to execute
1636 * @param jp2 the jpeg2000 file codec to execute the procedures on.
1637 * @param cio the stream to execute the procedures on.
1638 * @param p_manager the user manager.
1640 * @return true if all the procedures were successfully executed.
1644 opj_procedure_list_t * p_procedure_list,
1645 opj_stream_private_t *cio,
1646 opj_event_mgr_t * p_manager
1649 bool (** l_procedure) (opj_jp2_t * jp2,opj_stream_private_t *,opj_event_mgr_t *) = 00;
1650 bool l_result = true;
1651 unsigned int l_nb_proc, i;
1654 assert(p_procedure_list != 00);
1657 assert(p_manager != 00);
1659 l_nb_proc = opj_procedure_list_get_nb_procedures(p_procedure_list);
1660 l_procedure = (bool (**) (opj_jp2_t * jp2,opj_stream_private_t *,opj_event_mgr_t *)) opj_procedure_list_get_first_procedure(p_procedure_list);
1662 (i=0;i<l_nb_proc;++i)
1664 l_result = l_result && (*l_procedure) (jp2,cio,p_manager);
1667 // and clear the procedure list at the end.
1668 opj_procedure_list_clear(p_procedure_list);
1674 * Starts a compression scheme, i.e. validates the codec parameters, writes the header.
1676 * @param jp2 the jpeg2000 file codec.
1677 * @param cio the stream object.
1679 * @return true if the codec is valid.
1681 bool jp2_start_compress(opj_jp2_t *jp2, struct opj_stream_private *cio,opj_image_t * p_image, struct opj_event_mgr * p_manager)
1686 assert(p_manager != 00);
1688 /* customization of the validation */
1689 jp2_setup_encoding_validation (jp2);
1691 /* validation of the parameters codec */
1693 (! jp2_exec(jp2,jp2->m_validation_list,cio,p_manager))
1698 /* customization of the encoding */
1699 jp2_setup_header_writting(jp2);
1703 (! jp2_exec (jp2,jp2->m_procedure_list,cio,p_manager))
1707 return j2k_start_compress(jp2->j2k,cio,p_image,p_manager);
1711 * Reads a jpeg2000 file header structure.
1713 * @param cio the stream to read data from.
1714 * @param jp2 the jpeg2000 file header structure.
1715 * @param p_manager the user event manager.
1717 * @return true if the box is valid.
1719 bool jp2_read_header(
1721 opj_image_t ** p_image,
1722 OPJ_INT32 * p_tile_x0,
1723 OPJ_INT32 * p_tile_y0,
1724 OPJ_UINT32 * p_tile_width,
1725 OPJ_UINT32 * p_tile_height,
1726 OPJ_UINT32 * p_nb_tiles_x,
1727 OPJ_UINT32 * p_nb_tiles_y,
1728 struct opj_stream_private *cio,
1729 struct opj_event_mgr * p_manager
1735 assert(p_manager != 00);
1737 /* customization of the validation */
1738 jp2_setup_decoding_validation (jp2);
1740 /* customization of the encoding */
1741 jp2_setup_header_reading(jp2);
1743 /* validation of the parameters codec */
1745 (! jp2_exec(jp2,jp2->m_validation_list,cio,p_manager))
1752 (! jp2_exec (jp2,jp2->m_procedure_list,cio,p_manager))
1756 return j2k_read_header(
1770 * Ends the decompression procedures and possibiliy add data to be read after the
1773 bool jp2_end_decompress(opj_jp2_t *jp2, opj_stream_private_t *cio, opj_event_mgr_t * p_manager)
1778 assert(p_manager != 00);
1780 /* customization of the end encoding */
1781 jp2_setup_end_header_reading(jp2);
1785 (! jp2_exec (jp2,jp2->m_procedure_list,cio,p_manager))
1789 return j2k_end_decompress(jp2->j2k, cio, p_manager);
1794 * Ends the compression procedures and possibiliy add data to be read after the
1797 bool jp2_end_compress(opj_jp2_t *jp2, opj_stream_private_t *cio, opj_event_mgr_t * p_manager)
1802 assert(p_manager != 00);
1804 /* customization of the end encoding */
1805 jp2_setup_end_header_writting(jp2);
1808 (! j2k_end_compress(jp2->j2k,cio,p_manager))
1813 return jp2_exec (jp2,jp2->m_procedure_list,cio,p_manager);
1817 Encode an image into a JPEG-2000 file stream
1818 @param jp2 JP2 compressor handle
1819 @param cio Output buffer stream
1820 @param image Image to encode
1821 @param cstr_info Codestream information structure if required, NULL otherwise
1822 @return Returns true if successful, returns false otherwise
1824 bool jp2_encode(opj_jp2_t *jp2, struct opj_stream_private *cio, struct opj_event_mgr * p_manager)
1826 return j2k_encode(jp2->j2k,cio,p_manager);
1830 * @param p_j2k the jpeg2000 codec.
1831 * @param p_stream the stream to write data to.
1832 * @param p_manager the user event manager.
1834 bool jp2_write_tile (
1836 OPJ_UINT32 p_tile_index,
1838 OPJ_UINT32 p_data_size,
1839 struct opj_stream_private *p_stream,
1840 struct opj_event_mgr * p_manager
1843 return j2k_write_tile (p_jp2->j2k,p_tile_index,p_data,p_data_size,p_stream,p_manager);
1848 * @param p_j2k the jpeg2000 codec.
1849 * @param p_stream the stream to write data to.
1850 * @param p_manager the user event manager.
1852 bool jp2_decode_tile (
1854 OPJ_UINT32 p_tile_index,
1856 OPJ_UINT32 p_data_size,
1857 opj_stream_private_t *p_stream,
1858 opj_event_mgr_t * p_manager
1861 return j2k_decode_tile (p_jp2->j2k,p_tile_index,p_data,p_data_size,p_stream,p_manager);
1864 * Reads a tile header.
1865 * @param p_j2k the jpeg2000 codec.
1866 * @param p_stream the stream to write data to.
1867 * @param p_manager the user event manager.
1869 bool jp2_read_tile_header (
1871 OPJ_UINT32 * p_tile_index,
1872 OPJ_UINT32 * p_data_size,
1873 OPJ_INT32 * p_tile_x0,
1874 OPJ_INT32 * p_tile_y0,
1875 OPJ_INT32 * p_tile_x1,
1876 OPJ_INT32 * p_tile_y1,
1877 OPJ_UINT32 * p_nb_comps,
1879 opj_stream_private_t *p_stream,
1880 opj_event_mgr_t * p_manager
1883 return j2k_read_tile_header (p_jp2->j2k,
1897 * Sets up the procedures to do on writting header after the codestream.
1898 * Developpers wanting to extend the library can add their own writting procedures.
1900 void jp2_setup_end_header_writting (opj_jp2_t *jp2)
1905 opj_procedure_list_add_procedure(jp2->m_procedure_list,jp2_write_jp2c );
1906 /* DEVELOPER CORNER, add your custom procedures */
1910 * Sets up the procedures to do on reading header.
1911 * Developpers wanting to extend the library can add their own writting procedures.
1913 void jp2_setup_header_reading (opj_jp2_t *jp2)
1918 opj_procedure_list_add_procedure(jp2->m_procedure_list,jp2_read_header_procedure );
1919 /* DEVELOPER CORNER, add your custom procedures */
1923 * Sets up the procedures to do on reading header after the codestream.
1924 * Developpers wanting to extend the library can add their own writting procedures.
1926 void jp2_setup_end_header_reading (opj_jp2_t *jp2)
1930 opj_procedure_list_add_procedure(jp2->m_procedure_list,jp2_read_header_procedure );
1931 /* DEVELOPER CORNER, add your custom procedures */
1936 * The default validation procedure without any extension.
1938 * @param jp2 the jpeg2000 codec to validate.
1939 * @param cio the input stream to validate.
1940 * @param p_manager the user event manager.
1942 * @return true if the parameters are correct.
1944 bool jp2_default_validation (
1946 opj_stream_private_t *cio,
1947 opj_event_mgr_t * p_manager
1950 bool l_is_valid = true;
1956 assert(p_manager != 00);
1957 /* JPEG2000 codec validation */
1960 /* STATE checking */
1961 /* make sure the state is at 0 */
1962 l_is_valid &= (jp2->jp2_state == JP2_STATE_NONE);
1963 /* make sure not reading a jp2h ???? WEIRD */
1964 l_is_valid &= (jp2->jp2_img_state == JP2_IMG_STATE_NONE);
1966 /* POINTER validation */
1967 /* make sure a j2k codec is present */
1968 l_is_valid &= (jp2->j2k != 00);
1969 /* make sure a procedure list is present */
1970 l_is_valid &= (jp2->m_procedure_list != 00);
1971 /* make sure a validation list is present */
1972 l_is_valid &= (jp2->m_validation_list != 00);
1974 /* PARAMETER VALIDATION */
1975 /* number of components */
1976 l_is_valid &= (jp2->numcl > 0);
1978 l_is_valid &= (jp2->h > 0);
1980 l_is_valid &= (jp2->w > 0);
1983 (i = 0; i < jp2->numcomps; ++i)
1985 l_is_valid &= (jp2->comps[i].bpcc > 0);
1988 l_is_valid &= ((jp2->meth > 0) && (jp2->meth < 3));
1992 /* stream validation */
1993 /* back and forth is needed */
1994 l_is_valid &= opj_stream_has_seek(cio);
2001 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
2002 * are valid. Developpers wanting to extend the library can add their own validation procedures.
2004 void jp2_setup_encoding_validation (opj_jp2_t *jp2)
2008 opj_procedure_list_add_procedure(jp2->m_validation_list, jp2_default_validation);
2009 /* DEVELOPER CORNER, add your custom validation procedure */
2013 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
2014 * are valid. Developpers wanting to extend the library can add their own validation procedures.
2016 void jp2_setup_decoding_validation (opj_jp2_t *jp2)
2020 /* DEVELOPER CORNER, add your custom validation procedure */
2024 * Sets up the procedures to do on writting header. Developpers wanting to extend the library can add their own writting procedures.
2026 void jp2_setup_header_writting (opj_jp2_t *jp2)
2030 opj_procedure_list_add_procedure(jp2->m_procedure_list,jp2_write_jp );
2031 opj_procedure_list_add_procedure(jp2->m_procedure_list,jp2_write_ftyp );
2032 opj_procedure_list_add_procedure(jp2->m_procedure_list,jp2_write_jp2h );
2033 opj_procedure_list_add_procedure(jp2->m_procedure_list,jp2_skip_jp2c );
2035 /* DEVELOPER CORNER, insert your custom procedures */
2041 * Skips the Jpeg2000 Codestream Header box - JP2C Header box.
2043 * @param cio the stream to write data to.
2044 * @param jp2 the jpeg2000 file codec.
2045 * @param p_manager user event manager.
2047 * @return true if writting was successful.
2051 struct opj_stream_private *cio,
2052 struct opj_event_mgr * p_manager
2058 assert(p_manager != 00);
2060 jp2->j2k_codestream_offset = opj_stream_tell(cio);
2062 (opj_stream_skip(cio,8,p_manager) != 8)
2069 struct opj_image * jp2_decode(opj_jp2_t *jp2, struct opj_stream_private *cio, struct opj_event_mgr * p_manager)
2072 struct opj_image * image = j2k_decode(jp2->j2k, cio, p_manager);
2076 opj_event_msg(p_manager, EVT_ERROR, "Failed to decode J2K image\n");
2080 /* Set Image Color Space */
2081 if (jp2->enumcs == 16)
2082 image->color_space = CLRSPC_SRGB;
2083 else if (jp2->enumcs == 17)
2084 image->color_space = CLRSPC_GRAY;
2085 else if (jp2->enumcs == 18)
2086 image->color_space = CLRSPC_SYCC;
2088 image->color_space = CLRSPC_UNKNOWN;
2092 void jp2_setup_encoder(opj_jp2_t *jp2, opj_cparameters_t *parameters, opj_image_t *image,opj_event_mgr_t * p_manager)
2097 if(!jp2 || !parameters || !image)
2100 /* setup the J2K codec */
2101 /* ------------------- */
2103 /* Check if number of components respects standard */
2104 if (image->numcomps < 1 || image->numcomps > 16384) {
2105 opj_event_msg(p_manager, EVT_ERROR, "Invalid number of components specified while setting up JP2 encoder\n");
2109 j2k_setup_encoder(jp2->j2k, parameters, image,p_manager);
2111 /* setup the JP2 codec */
2112 /* ------------------- */
2116 jp2->brand = JP2_JP2; /* BR */
2117 jp2->minversion = 0; /* MinV */
2119 jp2->cl = (unsigned int*) opj_malloc(jp2->numcl * sizeof(unsigned int));
2120 jp2->cl[0] = JP2_JP2; /* CL0 : JP2 */
2122 /* Image Header box */
2124 jp2->numcomps = image->numcomps; /* NC */
2125 jp2->comps = (opj_jp2_comps_t*) opj_malloc(jp2->numcomps * sizeof(opj_jp2_comps_t));
2126 jp2->h = image->y1 - image->y0; /* HEIGHT */
2127 jp2->w = image->x1 - image->x0; /* WIDTH */
2129 depth_0 = image->comps[0].prec - 1;
2130 sign = image->comps[0].sgnd;
2131 jp2->bpc = depth_0 + (sign << 7);
2132 for (i = 1; i < image->numcomps; i++) {
2133 int depth = image->comps[i].prec - 1;
2134 sign = image->comps[i].sgnd;
2135 if (depth_0 != depth)
2138 jp2->C = 7; /* C : Always 7 */
2139 jp2->UnkC = 0; /* UnkC, colorspace specified in colr box */
2140 jp2->IPR = 0; /* IPR, no intellectual property */
2142 /* BitsPerComponent box */
2144 for (i = 0; i < image->numcomps; i++) {
2145 jp2->comps[i].bpcc = image->comps[i].prec - 1 + (image->comps[i].sgnd << 7);
2148 /* Colour Specification box */
2150 if ((image->numcomps == 1 || image->numcomps == 3) && (jp2->bpc != 255)) {
2151 jp2->meth = 1; /* METH: Enumerated colourspace */
2153 jp2->meth = 2; /* METH: Restricted ICC profile */
2155 if (jp2->meth == 1) {
2156 if (image->color_space == 1)
2157 jp2->enumcs = 16; /* sRGB as defined by IEC 61966�2�1 */
2158 else if (image->color_space == 2)
2159 jp2->enumcs = 17; /* greyscale */
2160 else if (image->color_space == 3)
2161 jp2->enumcs = 18; /* YUV */
2163 jp2->enumcs = 0; /* PROFILE (??) */
2165 jp2->precedence = 0; /* PRECEDENCE */
2166 jp2->approx = 0; /* APPROX */
2170 void jp2_setup_decoder(opj_jp2_t *jp2, opj_dparameters_t *parameters)
2172 if(!jp2 || !parameters)
2175 /* setup the J2K codec */
2176 /* ------------------- */
2177 j2k_setup_decoder(jp2->j2k, parameters);
2180 * Sets the given area to be decoded. This function should be called right after opj_read_header and before any tile header reading.
2182 * @param p_jp2 the jpeg2000 codec.
2183 * @param p_end_x the right position of the rectangle to decode (in image coordinates).
2184 * @param p_start_y the up position of the rectangle to decode (in image coordinates).
2185 * @param p_end_y the bottom position of the rectangle to decode (in image coordinates).
2186 * @param p_manager the user event manager
2188 * @return true if the area could be set.
2190 bool jp2_set_decode_area(
2192 OPJ_INT32 p_start_x,
2193 OPJ_INT32 p_start_y,
2196 struct opj_event_mgr * p_manager
2199 return j2k_set_decode_area(p_jp2->j2k,p_start_x,p_start_y,p_end_x,p_end_y,p_manager);
2208 static void jp2_write_url(opj_cio_t *cio, char *Idx_file) {
2212 box.init_pos = cio_tell(cio);
2214 cio_write(cio, JP2_URL, 4); /* DBTL */
2215 cio_write(cio, 0, 1); /* VERS */
2216 cio_write(cio, 0, 3); /* FLAG */
2219 for (i = 0; i < strlen(Idx_file); i++) {
2220 cio_write(cio, Idx_file[i], 1);
2224 box.length = cio_tell(cio) - box.init_pos;
2225 cio_seek(cio, box.init_pos);
2226 cio_write(cio, box.length, 4); /* L */
2227 cio_seek(cio, box.init_pos + box.length);