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 = (unsigned char*)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;
528 opj_free(l_current_data);
533 opj_event_msg(p_manager, EVT_ERROR, "bad placed jpeg codestream\n");
534 opj_free(l_current_data);
541 opj_event_msg(p_manager, EVT_ERROR, "Cannot handle box of undefined sizes\n");
542 opj_free(l_current_data);
546 l_current_handler = jp2_find_handler(box.type);
547 l_current_data_size = box.length - l_nb_bytes_read;
550 (l_current_handler != 00)
553 (l_current_data_size > l_last_data_size)
555 l_current_data = (unsigned char*)opj_realloc(l_current_data,l_current_data_size);
556 l_last_data_size = l_current_data_size;
558 l_nb_bytes_read = opj_stream_read_data(cio,l_current_data,l_current_data_size,p_manager);
560 (l_nb_bytes_read != l_current_data_size)
562 opj_event_msg(p_manager, EVT_ERROR, "Problem with reading JPEG2000 box, stream error\n");
566 (! l_current_handler->handler(jp2,l_current_data,l_current_data_size,p_manager))
568 opj_free(l_current_data);
574 jp2->jp2_state |= JP2_STATE_UNKNOWN;
576 (opj_stream_skip(cio,l_current_data_size,p_manager) != l_current_data_size)
578 opj_event_msg(p_manager, EVT_ERROR, "Problem with skipping JPEG2000 box, stream error\n");
579 opj_free(l_current_data);
585 opj_free(l_current_data);
591 * Reads a box header. The box is the way data is packed inside a jpeg2000 file structure.
593 * @param cio the input stream to read data from.
594 * @param box the box structure to fill.
595 * @param p_number_bytes_read pointer to an int that will store the number of bytes read from the stream (should usually be 8).
596 * @param p_manager user event manager.
598 * @return true if the box is reconized, false otherwise
600 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)
602 /* read header from file */
603 unsigned char l_data_header [8];
608 assert(p_number_bytes_read != 00);
609 assert(p_manager != 00);
611 *p_number_bytes_read = opj_stream_read_data(cio,l_data_header,8,p_manager);
613 (*p_number_bytes_read != 8)
617 /* process read data */
618 opj_read_bytes(l_data_header,&(box->length), 4);
619 opj_read_bytes(l_data_header+4,&(box->type), 4);
621 // do we have a "special very large box ?"
622 // read then the XLBox
626 OPJ_UINT32 l_xl_part_size;
627 OPJ_UINT32 l_nb_bytes_read = opj_stream_read_data(cio,l_data_header,8,p_manager);
629 (l_nb_bytes_read != 8)
632 (l_nb_bytes_read > 0)
634 *p_number_bytes_read += l_nb_bytes_read;
638 opj_read_bytes(l_data_header,&l_xl_part_size, 4);
640 (l_xl_part_size != 0)
642 opj_event_msg(p_manager, EVT_ERROR, "Cannot handle box sizes higher than 2^32\n");
645 opj_read_bytes(l_data_header,&(box->length), 4);
651 * Reads a box header. The box is the way data is packed inside a jpeg2000 file structure. Data is read from a character string
653 * @param p_data the character string to read data from.
654 * @param box the box structure to fill.
655 * @param p_number_bytes_read pointer to an int that will store the number of bytes read from the stream (shoul usually be 2).
656 * @param p_box_max_size the maximum number of bytes in the box.
658 * @return true if the box is reconized, false otherwise
660 static bool jp2_read_boxhdr_char(
663 OPJ_UINT32 * p_number_bytes_read,
664 OPJ_UINT32 p_box_max_size,
665 opj_event_mgr_t * p_manager
669 assert(p_data != 00);
671 assert(p_number_bytes_read != 00);
672 assert(p_manager != 00);
677 opj_event_msg(p_manager, EVT_ERROR, "Cannot handle box of less than 8 bytes\n");
680 /* process read data */
681 opj_read_bytes(p_data,&(box->length), 4);
683 opj_read_bytes(p_data,&(box->type), 4);
685 *p_number_bytes_read = 8;
687 // do we have a "special very large box ?"
688 // read then the XLBox
692 unsigned int l_xl_part_size;
694 (p_box_max_size < 16)
696 opj_event_msg(p_manager, EVT_ERROR, "Cannot handle XL box of less than 16 bytes\n");
700 opj_read_bytes(p_data,&l_xl_part_size, 4);
702 *p_number_bytes_read += 4;
704 (l_xl_part_size != 0)
706 opj_event_msg(p_manager, EVT_ERROR, "Cannot handle box sizes higher than 2^32\n");
709 opj_read_bytes(p_data,&(box->length), 4);
710 *p_number_bytes_read += 4;
714 opj_event_msg(p_manager, EVT_ERROR, "Cannot handle box of undefined sizes\n");
722 opj_event_msg(p_manager, EVT_ERROR, "Cannot handle box of undefined sizes\n");
730 * Reads a jpeg2000 file signature box.
732 * @param p_header_data the data contained in the signature box.
733 * @param jp2 the jpeg2000 file codec.
734 * @param p_header_size the size of the data contained in the signature box.
735 * @param p_manager the user event manager.
737 * @return true if the file signature box is valid.
741 unsigned char * p_header_data,
742 unsigned int p_header_size,
743 opj_event_mgr_t * p_manager
746 unsigned int l_magic_number;
749 assert(p_header_data != 00);
751 assert(p_manager != 00);
754 (jp2->jp2_state != JP2_STATE_NONE)
756 opj_event_msg(p_manager, EVT_ERROR, "The signature box must be the first box in the file.\n");
761 /* assure length of data is correct (4 -> magic number) */
765 opj_event_msg(p_manager, EVT_ERROR, "Error with JP signature Box size\n");
770 opj_read_bytes(p_header_data,&l_magic_number,4);
772 (l_magic_number != 0x0d0a870a )
774 opj_event_msg(p_manager, EVT_ERROR, "Error with JP Signature : bad magic number\n");
777 jp2->jp2_state |= JP2_STATE_SIGNATURE;
782 * Reads a a FTYP box - File type box
784 * @param p_header_data the data contained in the FTYP box.
785 * @param jp2 the jpeg2000 file codec.
786 * @param p_header_size the size of the data contained in the FTYP box.
787 * @param p_manager the user event manager.
789 * @return true if the FTYP box is valid.
793 unsigned char * p_header_data,
794 unsigned int p_header_size,
795 opj_event_mgr_t * p_manager
799 unsigned int l_remaining_bytes;
802 assert(p_header_data != 00);
804 assert(p_manager != 00);
807 (jp2->jp2_state != JP2_STATE_SIGNATURE)
809 opj_event_msg(p_manager, EVT_ERROR, "The ftyp box must be the second box in the file.\n");
813 /* assure length of data is correct */
817 opj_event_msg(p_manager, EVT_ERROR, "Error with FTYP signature Box size\n");
821 opj_read_bytes(p_header_data,&jp2->brand,4); /* BR */
824 opj_read_bytes(p_header_data,&jp2->minversion,4); /* MinV */
827 l_remaining_bytes = p_header_size - 8;
829 /* the number of remaining bytes should be a multiple of 4 */
831 ((l_remaining_bytes & 0x3) != 0)
833 opj_event_msg(p_manager, EVT_ERROR, "Error with FTYP signature Box size\n");
837 jp2->numcl = l_remaining_bytes >> 2;
841 jp2->cl = (unsigned int *) opj_malloc(jp2->numcl * sizeof(unsigned int));
845 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory with FTYP Box\n");
848 memset(jp2->cl,0,jp2->numcl * sizeof(unsigned int));
853 (i = 0; i < jp2->numcl; ++i)
855 opj_read_bytes(p_header_data,&jp2->cl[i],4); /* CLi */
859 jp2->jp2_state |= JP2_STATE_FILE_TYPE;
864 * Writes a jpeg2000 file signature box.
866 * @param cio the stream to write data to.
867 * @param jp2 the jpeg2000 file codec.
868 * @param p_manager the user event manager.
870 * @return true if writting was successful.
874 opj_stream_private_t *cio,
875 opj_event_mgr_t * p_manager
878 /* 12 bytes will be read */
879 unsigned char l_signature_data [12];
884 assert(p_manager != 00);
887 /* write box length */
888 opj_write_bytes(l_signature_data,12,4);
889 /* writes box type */
890 opj_write_bytes(l_signature_data+4,JP2_JP,4);
891 /* writes magic number*/
892 opj_write_bytes(l_signature_data+8,0x0d0a870a,4);
894 (opj_stream_write_data(cio,l_signature_data,12,p_manager) != 12)
903 * Writes a FTYP box - File type box
905 * @param cio the stream to write data to.
906 * @param jp2 the jpeg2000 file codec.
907 * @param p_manager the user event manager.
909 * @return true if writting was successful.
913 opj_stream_private_t *cio,
914 opj_event_mgr_t * p_manager
918 unsigned int l_ftyp_size = 16 + 4 * jp2->numcl;
919 unsigned char * l_ftyp_data, * l_current_data_ptr;
925 assert(p_manager != 00);
927 l_ftyp_data = (unsigned char *) opj_malloc(l_ftyp_size);
932 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to handle ftyp data\n");
935 memset(l_ftyp_data,0,l_ftyp_size);
937 l_current_data_ptr = l_ftyp_data;
939 opj_write_bytes(l_current_data_ptr, l_ftyp_size,4); /* box size */
940 l_current_data_ptr += 4;
942 opj_write_bytes(l_current_data_ptr, JP2_FTYP,4); /* FTYP */
943 l_current_data_ptr += 4;
945 opj_write_bytes(l_current_data_ptr, jp2->brand,4); /* BR */
946 l_current_data_ptr += 4;
948 opj_write_bytes(l_current_data_ptr, jp2->minversion,4); /* MinV */
949 l_current_data_ptr += 4;
952 (i = 0; i < jp2->numcl; i++)
954 opj_write_bytes(l_current_data_ptr, jp2->cl[i],4); /* CL */
957 l_result = (opj_stream_write_data(cio,l_ftyp_data,l_ftyp_size,p_manager) == l_ftyp_size);
961 opj_event_msg(p_manager, EVT_ERROR, "Error while writting ftyp data to stream\n");
963 opj_free(l_ftyp_data);
968 * Writes the Jpeg2000 file Header box - JP2 Header box (warning, this is a super box).
970 * @param cio the stream to write data to.
971 * @param jp2 the jpeg2000 file codec.
972 * @param p_manager user event manager.
974 * @return true if writting was successful.
978 opj_stream_private_t *cio,
979 opj_event_mgr_t * p_manager
982 opj_jp2_img_header_writer_handler_t l_writers [3];
983 opj_jp2_img_header_writer_handler_t * l_current_writer;
986 /* size of data for super box*/
988 bool l_result = true;
990 /* to store the data of the super box */
991 unsigned char l_jp2h_data [8];
996 assert(p_manager != 00);
998 memset(l_writers,0,sizeof(l_writers));
1004 l_writers[0].handler = jp2_write_ihdr;
1005 l_writers[1].handler = jp2_write_bpcc;
1006 l_writers[2].handler = jp2_write_colr;
1011 l_writers[0].handler = jp2_write_ihdr;
1012 l_writers[1].handler = jp2_write_colr;
1015 /* write box header */
1016 /* write JP2H type */
1017 opj_write_bytes(l_jp2h_data+4,JP2_JP2H,4);
1019 l_current_writer = l_writers;
1021 (i=0;i<l_nb_pass;++i)
1023 l_current_writer->m_data = l_current_writer->handler(jp2,&(l_current_writer->m_size));
1025 (l_current_writer->m_data == 00)
1027 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to hold JP2 Header data\n");
1031 l_jp2h_size += l_current_writer->m_size;
1038 l_current_writer = l_writers;
1040 (i=0;i<l_nb_pass;++i)
1043 (l_current_writer->m_data != 00)
1045 opj_free(l_current_writer->m_data );
1052 /* write super box size */
1053 opj_write_bytes(l_jp2h_data,l_jp2h_size,4);
1055 /* write super box data on stream */
1057 (opj_stream_write_data(cio,l_jp2h_data,8,p_manager) != 8)
1059 opj_event_msg(p_manager, EVT_ERROR, "Stream error while writting JP2 Header box\n");
1066 l_current_writer = l_writers;
1068 (i=0;i<l_nb_pass;++i)
1071 (opj_stream_write_data(cio,l_current_writer->m_data,l_current_writer->m_size,p_manager) != l_current_writer->m_size)
1073 opj_event_msg(p_manager, EVT_ERROR, "Stream error while writting JP2 Header box\n");
1080 l_current_writer = l_writers;
1083 (i=0;i<l_nb_pass;++i)
1086 (l_current_writer->m_data != 00)
1088 opj_free(l_current_writer->m_data );
1096 * Reads the Jpeg2000 file Header box - JP2 Header box (warning, this is a super box).
1098 * @param p_header_data the data contained in the file header box.
1099 * @param jp2 the jpeg2000 file codec.
1100 * @param p_header_size the size of the data contained in the file header box.
1101 * @param p_manager the user event manager.
1103 * @return true if the JP2 Header box was successfully reconized.
1107 unsigned char * p_header_data,
1108 unsigned int p_header_size,
1109 opj_event_mgr_t * p_manager
1112 unsigned int l_box_size=0, l_current_data_size = 0;
1114 const opj_jp2_header_handler_t * l_current_handler;
1117 assert(p_header_data != 00);
1119 assert(p_manager != 00);
1121 /* make sure the box is well placed */
1123 ((jp2->jp2_state & JP2_STATE_FILE_TYPE) != JP2_STATE_FILE_TYPE )
1125 opj_event_msg(p_manager, EVT_ERROR, "The box must be the first box in the file.\n");
1128 jp2->jp2_img_state = JP2_IMG_STATE_NONE;
1130 /* iterate while remaining data */
1135 (! jp2_read_boxhdr_char(&box,p_header_data,&l_box_size,p_header_size, p_manager))
1137 opj_event_msg(p_manager, EVT_ERROR, "Stream error while reading JP2 Header box\n");
1141 (box.length > p_header_size)
1143 opj_event_msg(p_manager, EVT_ERROR, "Stream error while reading JP2 Header box\n");
1146 l_current_handler = jp2_img_find_handler(box.type);
1148 l_current_data_size = box.length - l_box_size;
1149 p_header_data += l_box_size;
1152 (l_current_handler != 00)
1155 (! l_current_handler->handler(jp2,p_header_data,l_current_data_size,p_manager))
1162 jp2->jp2_img_state |= JP2_IMG_STATE_UNKNOWN;
1164 p_header_data += l_current_data_size;
1165 p_header_size -= box.length;
1167 jp2->jp2_state |= JP2_STATE_HEADER;
1172 * Reads a IHDR box - Image Header box
1174 * @param p_image_header_data pointer to actual data (already read from file)
1175 * @param jp2 the jpeg2000 file codec.
1176 * @param p_image_header_size the size of the image header
1177 * @param p_image_header_max_size maximum size of the header, any size bigger than this value should result the function to output false.
1178 * @param p_manager the user event manager.
1180 * @return true if the image header is valid, fale else.
1184 unsigned char * p_image_header_data,
1185 unsigned int p_image_header_size,
1186 opj_event_mgr_t * p_manager
1190 assert(p_image_header_data != 00);
1192 assert(p_manager != 00);
1195 (p_image_header_size != 14)
1197 opj_event_msg(p_manager, EVT_ERROR, "Bad image header box (bad size)\n");
1200 opj_read_bytes(p_image_header_data,&(jp2->h),4); /* HEIGHT */
1201 p_image_header_data += 4;
1202 opj_read_bytes(p_image_header_data,&(jp2->w),4); /* WIDTH */
1203 p_image_header_data += 4;
1204 opj_read_bytes(p_image_header_data,&(jp2->numcomps),2); /* NC */
1205 p_image_header_data += 2;
1207 /* allocate memory for components */
1208 jp2->comps = (opj_jp2_comps_t*) opj_malloc(jp2->numcomps * sizeof(opj_jp2_comps_t));
1212 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to handle image header (ihdr)\n");
1215 memset(jp2->comps,0,jp2->numcomps * sizeof(opj_jp2_comps_t));
1217 opj_read_bytes(p_image_header_data,&(jp2->bpc),1); /* BPC */
1218 ++ p_image_header_data;
1219 opj_read_bytes(p_image_header_data,&(jp2->C),1); /* C */
1220 ++ p_image_header_data;
1221 opj_read_bytes(p_image_header_data,&(jp2->UnkC),1); /* UnkC */
1222 ++ p_image_header_data;
1223 opj_read_bytes(p_image_header_data,&(jp2->IPR),1); /* IPR */
1224 ++ p_image_header_data;
1229 * Writes the Image Header box - Image Header box.
1231 * @param jp2 jpeg2000 file codec.
1232 * @param p_nb_bytes_written pointer to store the nb of bytes written by the function.
1234 * @return the data being copied.
1236 static unsigned char * jp2_write_ihdr(
1238 unsigned int * p_nb_bytes_written
1241 unsigned char * l_ihdr_data,* l_current_ihdr_ptr;
1245 assert(p_nb_bytes_written != 00);
1247 /* default image header is 22 bytes wide */
1248 l_ihdr_data = (unsigned char *) opj_malloc(22);
1254 memset(l_ihdr_data,0,22);
1256 l_current_ihdr_ptr = l_ihdr_data;
1258 opj_write_bytes(l_current_ihdr_ptr,22,4); /* write box size */
1259 l_current_ihdr_ptr+=4;
1260 opj_write_bytes(l_current_ihdr_ptr,JP2_IHDR, 4); /* IHDR */
1261 l_current_ihdr_ptr+=4;
1262 opj_write_bytes(l_current_ihdr_ptr,jp2->h, 4); /* HEIGHT */
1263 l_current_ihdr_ptr+=4;
1264 opj_write_bytes(l_current_ihdr_ptr, jp2->w, 4); /* WIDTH */
1265 l_current_ihdr_ptr+=4;
1266 opj_write_bytes(l_current_ihdr_ptr, jp2->numcomps, 2); /* NC */
1267 l_current_ihdr_ptr+=2;
1268 opj_write_bytes(l_current_ihdr_ptr, jp2->bpc, 1); /* BPC */
1269 ++l_current_ihdr_ptr;
1270 opj_write_bytes(l_current_ihdr_ptr, jp2->C, 1); /* C : Always 7 */
1271 ++l_current_ihdr_ptr;
1272 opj_write_bytes(l_current_ihdr_ptr, jp2->UnkC, 1); /* UnkC, colorspace unknown */
1273 ++l_current_ihdr_ptr;
1274 opj_write_bytes(l_current_ihdr_ptr, jp2->IPR, 1); /* IPR, no intellectual property */
1275 ++l_current_ihdr_ptr;
1276 *p_nb_bytes_written = 22;
1281 * Writes the Bit per Component box.
1283 * @param jp2 jpeg2000 file codec.
1284 * @param p_nb_bytes_written pointer to store the nb of bytes written by the function.
1286 * @return the data being copied.
1288 unsigned char * jp2_write_bpcc(
1290 unsigned int * p_nb_bytes_written
1294 /* room for 8 bytes for box and 1 byte for each component */
1295 int l_bpcc_size = 8 + jp2->numcomps;
1296 unsigned char * l_bpcc_data,* l_current_bpcc_ptr;
1300 assert(p_nb_bytes_written != 00);
1302 l_bpcc_data = (unsigned char *) opj_malloc(l_bpcc_size);
1308 memset(l_bpcc_data,0,l_bpcc_size);
1310 l_current_bpcc_ptr = l_bpcc_data;
1312 opj_write_bytes(l_current_bpcc_ptr,l_bpcc_size,4); /* write box size */
1313 l_current_bpcc_ptr += 4;
1314 opj_write_bytes(l_current_bpcc_ptr,JP2_BPCC,4); /* BPCC */
1315 l_current_bpcc_ptr += 4;
1318 (i = 0; i < jp2->numcomps; ++i)
1320 opj_write_bytes(l_current_bpcc_ptr, jp2->comps[i].bpcc, 1); /* write each component information */
1321 ++l_current_bpcc_ptr;
1323 *p_nb_bytes_written = l_bpcc_size;
1328 * Reads a Bit per Component box.
1330 * @param p_bpc_header_data pointer to actual data (already read from file)
1331 * @param jp2 the jpeg2000 file codec.
1332 * @param p_bpc_header_size pointer that will hold the size of the bpc header
1333 * @param p_bpc_header_max_size maximum size of the header, any size bigger than this value should result the function to output false.
1334 * @param p_manager the user event manager.
1336 * @return true if the bpc header is valid, fale else.
1340 unsigned char * p_bpc_header_data,
1341 unsigned int p_bpc_header_size,
1342 opj_event_mgr_t * p_manager
1348 assert(p_bpc_header_data != 00);
1350 assert(p_manager != 00);
1352 // and length is relevant
1354 (p_bpc_header_size != jp2->numcomps)
1356 opj_event_msg(p_manager, EVT_ERROR, "Bad BPCC header box (bad size)\n");
1360 // read info for each component
1362 (i = 0; i < jp2->numcomps; ++i)
1364 opj_read_bytes(p_bpc_header_data,&jp2->comps[i].bpcc ,1); /* read each BPCC component */
1365 ++p_bpc_header_data;
1371 * Writes the Colour Specification box.
1373 * @param jp2 jpeg2000 file codec.
1374 * @param p_nb_bytes_written pointer to store the nb of bytes written by the function.
1376 * @return the data being copied.
1378 unsigned char *jp2_write_colr(
1380 unsigned int * p_nb_bytes_written
1383 /* room for 8 bytes for box 3 for common data and variable upon profile*/
1384 unsigned int l_colr_size = 11;
1385 unsigned char * l_colr_data,* l_current_colr_ptr;
1389 assert(p_nb_bytes_written != 00);
1404 l_colr_data = (unsigned char *) opj_malloc(l_colr_size);
1410 memset(l_colr_data,0,l_colr_size);
1411 l_current_colr_ptr = l_colr_data;
1413 opj_write_bytes(l_current_colr_ptr,l_colr_size,4); /* write box size */
1414 l_current_colr_ptr += 4;
1415 opj_write_bytes(l_current_colr_ptr,JP2_COLR,4); /* BPCC */
1416 l_current_colr_ptr += 4;
1418 opj_write_bytes(l_current_colr_ptr, jp2->meth,1); /* METH */
1419 ++l_current_colr_ptr;
1420 opj_write_bytes(l_current_colr_ptr, jp2->precedence,1); /* PRECEDENCE */
1421 ++l_current_colr_ptr;
1422 opj_write_bytes(l_current_colr_ptr, jp2->approx,1); /* APPROX */
1423 ++l_current_colr_ptr;
1428 opj_write_bytes(l_current_colr_ptr, jp2->enumcs,4); /* EnumCS */
1432 opj_write_bytes(l_current_colr_ptr, 0, 1); /* PROFILE (??) */
1434 *p_nb_bytes_written = l_colr_size;
1439 * Reads the Colour Specification box.
1441 * @param p_colr_header_data pointer to actual data (already read from file)
1442 * @param jp2 the jpeg2000 file codec.
1443 * @param p_colr_header_size pointer that will hold the size of the color header
1444 * @param p_colr_header_max_size maximum size of the header, any size bigger than this value should result the function to output false.
1445 * @param p_manager the user event manager.
1447 * @return true if the bpc header is valid, fale else.
1451 unsigned char * p_colr_header_data,
1452 unsigned int p_colr_header_size,
1453 opj_event_mgr_t * p_manager
1458 assert(p_colr_header_data != 00);
1459 assert(p_manager != 00);
1462 (p_colr_header_size < 3)
1464 opj_event_msg(p_manager, EVT_ERROR, "Bad BPCC header box (bad size)\n");
1468 opj_read_bytes(p_colr_header_data,&jp2->meth ,1); /* METH */
1469 ++p_colr_header_data;
1471 opj_read_bytes(p_colr_header_data,&jp2->precedence ,1); /* PRECEDENCE */
1472 ++p_colr_header_data;
1474 opj_read_bytes(p_colr_header_data,&jp2->approx ,1); /* APPROX */
1475 ++p_colr_header_data;
1482 (p_colr_header_size != 7)
1484 opj_event_msg(p_manager, EVT_ERROR, "Bad BPCC header box (bad size)\n");
1487 opj_read_bytes(p_colr_header_data,&jp2->enumcs ,4); /* EnumCS */
1491 // do not care with profiles.
1497 * Writes the Jpeg2000 codestream Header box - JP2C Header box.
1499 * @param cio the stream to write data to.
1500 * @param jp2 the jpeg2000 file codec.
1501 * @param p_manager user event manager.
1503 * @return true if writting was successful.
1505 bool jp2_write_jp2c(
1507 opj_stream_private_t *cio,
1508 opj_event_mgr_t * p_manager
1511 unsigned int j2k_codestream_exit;
1512 unsigned char l_data_header [8];
1517 assert(p_manager != 00);
1518 assert(opj_stream_has_seek(cio));
1520 j2k_codestream_exit = opj_stream_tell(cio);
1521 opj_write_bytes(l_data_header,j2k_codestream_exit - jp2->j2k_codestream_offset,4); /* size of codestream */
1522 opj_write_bytes(l_data_header + 4,JP2_JP2C,4); /* JP2C */
1525 (! opj_stream_seek(cio,jp2->j2k_codestream_offset,p_manager))
1527 opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
1532 (opj_stream_write_data(cio,l_data_header,8,p_manager) != 8)
1534 opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
1539 (! opj_stream_seek(cio,j2k_codestream_exit,p_manager))
1541 opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
1548 * Destroys a jpeg2000 file decompressor.
1550 * @param jp2 a jpeg2000 file decompressor.
1552 void jp2_destroy(opj_jp2_t *jp2)
1557 /* destroy the J2K codec */
1558 j2k_destroy(jp2->j2k);
1563 opj_free(jp2->comps);
1573 (jp2->m_validation_list)
1575 opj_procedure_list_destroy(jp2->m_validation_list);
1576 jp2->m_validation_list = 00;
1579 (jp2->m_procedure_list)
1581 opj_procedure_list_destroy(jp2->m_procedure_list);
1582 jp2->m_procedure_list = 00;
1592 /* ----------------------------------------------------------------------- */
1593 /* JP2 encoder interface */
1594 /* ----------------------------------------------------------------------- */
1596 opj_jp2_t* jp2_create(bool p_is_decoder)
1598 opj_jp2_t *jp2 = (opj_jp2_t*)opj_malloc(sizeof(opj_jp2_t));
1602 memset(jp2,0,sizeof(opj_jp2_t));
1603 /* create the J2K codec */
1607 jp2->j2k = j2k_create_compress();
1611 jp2->j2k = j2k_create_decompress();
1619 // validation list creation
1620 jp2->m_validation_list = opj_procedure_list_create();
1622 (! jp2->m_validation_list)
1628 // execution list creation
1629 jp2->m_procedure_list = opj_procedure_list_create();
1631 (! jp2->m_procedure_list)
1641 * Excutes the given procedures on the given codec.
1643 * @param p_procedure_list the list of procedures to execute
1644 * @param jp2 the jpeg2000 file codec to execute the procedures on.
1645 * @param cio the stream to execute the procedures on.
1646 * @param p_manager the user manager.
1648 * @return true if all the procedures were successfully executed.
1652 opj_procedure_list_t * p_procedure_list,
1653 opj_stream_private_t *cio,
1654 opj_event_mgr_t * p_manager
1657 bool (** l_procedure) (opj_jp2_t * jp2,opj_stream_private_t *,opj_event_mgr_t *) = 00;
1658 bool l_result = true;
1659 unsigned int l_nb_proc, i;
1662 assert(p_procedure_list != 00);
1665 assert(p_manager != 00);
1667 l_nb_proc = opj_procedure_list_get_nb_procedures(p_procedure_list);
1668 l_procedure = (bool (**) (opj_jp2_t * jp2,opj_stream_private_t *,opj_event_mgr_t *)) opj_procedure_list_get_first_procedure(p_procedure_list);
1670 (i=0;i<l_nb_proc;++i)
1672 l_result = l_result && (*l_procedure) (jp2,cio,p_manager);
1675 // and clear the procedure list at the end.
1676 opj_procedure_list_clear(p_procedure_list);
1682 * Starts a compression scheme, i.e. validates the codec parameters, writes the header.
1684 * @param jp2 the jpeg2000 file codec.
1685 * @param cio the stream object.
1687 * @return true if the codec is valid.
1689 bool jp2_start_compress(opj_jp2_t *jp2, struct opj_stream_private *cio,opj_image_t * p_image, struct opj_event_mgr * p_manager)
1694 assert(p_manager != 00);
1696 /* customization of the validation */
1697 jp2_setup_encoding_validation (jp2);
1699 /* validation of the parameters codec */
1701 (! jp2_exec(jp2,jp2->m_validation_list,cio,p_manager))
1706 /* customization of the encoding */
1707 jp2_setup_header_writting(jp2);
1711 (! jp2_exec (jp2,jp2->m_procedure_list,cio,p_manager))
1715 return j2k_start_compress(jp2->j2k,cio,p_image,p_manager);
1719 * Reads a jpeg2000 file header structure.
1721 * @param cio the stream to read data from.
1722 * @param jp2 the jpeg2000 file header structure.
1723 * @param p_manager the user event manager.
1725 * @return true if the box is valid.
1727 bool jp2_read_header(
1729 opj_image_t ** p_image,
1730 OPJ_INT32 * p_tile_x0,
1731 OPJ_INT32 * p_tile_y0,
1732 OPJ_UINT32 * p_tile_width,
1733 OPJ_UINT32 * p_tile_height,
1734 OPJ_UINT32 * p_nb_tiles_x,
1735 OPJ_UINT32 * p_nb_tiles_y,
1736 struct opj_stream_private *cio,
1737 struct opj_event_mgr * p_manager
1743 assert(p_manager != 00);
1745 /* customization of the validation */
1746 jp2_setup_decoding_validation (jp2);
1748 /* customization of the encoding */
1749 jp2_setup_header_reading(jp2);
1751 /* validation of the parameters codec */
1753 (! jp2_exec(jp2,jp2->m_validation_list,cio,p_manager))
1760 (! jp2_exec (jp2,jp2->m_procedure_list,cio,p_manager))
1764 return j2k_read_header(
1778 * Ends the decompression procedures and possibiliy add data to be read after the
1781 bool jp2_end_decompress(opj_jp2_t *jp2, opj_stream_private_t *cio, opj_event_mgr_t * p_manager)
1786 assert(p_manager != 00);
1788 /* customization of the end encoding */
1789 jp2_setup_end_header_reading(jp2);
1793 (! jp2_exec (jp2,jp2->m_procedure_list,cio,p_manager))
1797 return j2k_end_decompress(jp2->j2k, cio, p_manager);
1802 * Ends the compression procedures and possibiliy add data to be read after the
1805 bool jp2_end_compress(opj_jp2_t *jp2, opj_stream_private_t *cio, opj_event_mgr_t * p_manager)
1810 assert(p_manager != 00);
1812 /* customization of the end encoding */
1813 jp2_setup_end_header_writting(jp2);
1816 (! j2k_end_compress(jp2->j2k,cio,p_manager))
1821 return jp2_exec (jp2,jp2->m_procedure_list,cio,p_manager);
1825 Encode an image into a JPEG-2000 file stream
1826 @param jp2 JP2 compressor handle
1827 @param cio Output buffer stream
1828 @param image Image to encode
1829 @param cstr_info Codestream information structure if required, NULL otherwise
1830 @return Returns true if successful, returns false otherwise
1832 bool jp2_encode(opj_jp2_t *jp2, struct opj_stream_private *cio, struct opj_event_mgr * p_manager)
1834 return j2k_encode(jp2->j2k,cio,p_manager);
1838 * @param p_j2k the jpeg2000 codec.
1839 * @param p_stream the stream to write data to.
1840 * @param p_manager the user event manager.
1842 bool jp2_write_tile (
1844 OPJ_UINT32 p_tile_index,
1846 OPJ_UINT32 p_data_size,
1847 struct opj_stream_private *p_stream,
1848 struct opj_event_mgr * p_manager
1851 return j2k_write_tile (p_jp2->j2k,p_tile_index,p_data,p_data_size,p_stream,p_manager);
1856 * @param p_j2k the jpeg2000 codec.
1857 * @param p_stream the stream to write data to.
1858 * @param p_manager the user event manager.
1860 bool jp2_decode_tile (
1862 OPJ_UINT32 p_tile_index,
1864 OPJ_UINT32 p_data_size,
1865 opj_stream_private_t *p_stream,
1866 opj_event_mgr_t * p_manager
1869 return j2k_decode_tile (p_jp2->j2k,p_tile_index,p_data,p_data_size,p_stream,p_manager);
1872 * Reads a tile header.
1873 * @param p_j2k the jpeg2000 codec.
1874 * @param p_stream the stream to write data to.
1875 * @param p_manager the user event manager.
1877 bool jp2_read_tile_header (
1879 OPJ_UINT32 * p_tile_index,
1880 OPJ_UINT32 * p_data_size,
1881 OPJ_INT32 * p_tile_x0,
1882 OPJ_INT32 * p_tile_y0,
1883 OPJ_INT32 * p_tile_x1,
1884 OPJ_INT32 * p_tile_y1,
1885 OPJ_UINT32 * p_nb_comps,
1887 opj_stream_private_t *p_stream,
1888 opj_event_mgr_t * p_manager
1891 return j2k_read_tile_header (p_jp2->j2k,
1905 * Sets up the procedures to do on writting header after the codestream.
1906 * Developpers wanting to extend the library can add their own writting procedures.
1908 void jp2_setup_end_header_writting (opj_jp2_t *jp2)
1913 opj_procedure_list_add_procedure(jp2->m_procedure_list,(void*)jp2_write_jp2c );
1914 /* DEVELOPER CORNER, add your custom procedures */
1918 * Sets up the procedures to do on reading header.
1919 * Developpers wanting to extend the library can add their own writting procedures.
1921 void jp2_setup_header_reading (opj_jp2_t *jp2)
1926 opj_procedure_list_add_procedure(jp2->m_procedure_list,(void*)jp2_read_header_procedure );
1927 /* DEVELOPER CORNER, add your custom procedures */
1931 * Sets up the procedures to do on reading header after the codestream.
1932 * Developpers wanting to extend the library can add their own writting procedures.
1934 void jp2_setup_end_header_reading (opj_jp2_t *jp2)
1938 opj_procedure_list_add_procedure(jp2->m_procedure_list,(void*)jp2_read_header_procedure );
1939 /* DEVELOPER CORNER, add your custom procedures */
1944 * The default validation procedure without any extension.
1946 * @param jp2 the jpeg2000 codec to validate.
1947 * @param cio the input stream to validate.
1948 * @param p_manager the user event manager.
1950 * @return true if the parameters are correct.
1952 bool jp2_default_validation (
1954 opj_stream_private_t *cio,
1955 opj_event_mgr_t * p_manager
1958 bool l_is_valid = true;
1964 assert(p_manager != 00);
1965 /* JPEG2000 codec validation */
1968 /* STATE checking */
1969 /* make sure the state is at 0 */
1970 l_is_valid &= (jp2->jp2_state == JP2_STATE_NONE);
1971 /* make sure not reading a jp2h ???? WEIRD */
1972 l_is_valid &= (jp2->jp2_img_state == JP2_IMG_STATE_NONE);
1974 /* POINTER validation */
1975 /* make sure a j2k codec is present */
1976 l_is_valid &= (jp2->j2k != 00);
1977 /* make sure a procedure list is present */
1978 l_is_valid &= (jp2->m_procedure_list != 00);
1979 /* make sure a validation list is present */
1980 l_is_valid &= (jp2->m_validation_list != 00);
1982 /* PARAMETER VALIDATION */
1983 /* number of components */
1984 l_is_valid &= (jp2->numcl > 0);
1986 l_is_valid &= (jp2->h > 0);
1988 l_is_valid &= (jp2->w > 0);
1991 (i = 0; i < jp2->numcomps; ++i)
1993 l_is_valid &= (jp2->comps[i].bpcc > 0);
1996 l_is_valid &= ((jp2->meth > 0) && (jp2->meth < 3));
2000 /* stream validation */
2001 /* back and forth is needed */
2002 l_is_valid &= opj_stream_has_seek(cio);
2009 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
2010 * are valid. Developpers wanting to extend the library can add their own validation procedures.
2012 void jp2_setup_encoding_validation (opj_jp2_t *jp2)
2016 opj_procedure_list_add_procedure(jp2->m_validation_list, (void*)jp2_default_validation);
2017 /* DEVELOPER CORNER, add your custom validation procedure */
2021 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
2022 * are valid. Developpers wanting to extend the library can add their own validation procedures.
2024 void jp2_setup_decoding_validation (opj_jp2_t *jp2)
2028 /* DEVELOPER CORNER, add your custom validation procedure */
2032 * Sets up the procedures to do on writting header. Developpers wanting to extend the library can add their own writting procedures.
2034 void jp2_setup_header_writting (opj_jp2_t *jp2)
2038 opj_procedure_list_add_procedure(jp2->m_procedure_list,(void*)jp2_write_jp );
2039 opj_procedure_list_add_procedure(jp2->m_procedure_list,(void*)jp2_write_ftyp );
2040 opj_procedure_list_add_procedure(jp2->m_procedure_list,(void*)jp2_write_jp2h );
2041 opj_procedure_list_add_procedure(jp2->m_procedure_list,(void*)jp2_skip_jp2c );
2043 /* DEVELOPER CORNER, insert your custom procedures */
2049 * Skips the Jpeg2000 Codestream Header box - JP2C Header box.
2051 * @param cio the stream to write data to.
2052 * @param jp2 the jpeg2000 file codec.
2053 * @param p_manager user event manager.
2055 * @return true if writting was successful.
2059 struct opj_stream_private *cio,
2060 struct opj_event_mgr * p_manager
2066 assert(p_manager != 00);
2068 jp2->j2k_codestream_offset = opj_stream_tell(cio);
2070 (opj_stream_skip(cio,8,p_manager) != 8)
2077 struct opj_image * jp2_decode(opj_jp2_t *jp2, struct opj_stream_private *cio, struct opj_event_mgr * p_manager)
2080 struct opj_image * image = j2k_decode(jp2->j2k, cio, p_manager);
2084 opj_event_msg(p_manager, EVT_ERROR, "Failed to decode J2K image\n");
2088 /* Set Image Color Space */
2089 if (jp2->enumcs == 16)
2090 image->color_space = CLRSPC_SRGB;
2091 else if (jp2->enumcs == 17)
2092 image->color_space = CLRSPC_GRAY;
2093 else if (jp2->enumcs == 18)
2094 image->color_space = CLRSPC_SYCC;
2096 image->color_space = CLRSPC_UNKNOWN;
2100 void jp2_setup_encoder(opj_jp2_t *jp2, opj_cparameters_t *parameters, opj_image_t *image,opj_event_mgr_t * p_manager)
2105 if(!jp2 || !parameters || !image)
2108 /* setup the J2K codec */
2109 /* ------------------- */
2111 /* Check if number of components respects standard */
2112 if (image->numcomps < 1 || image->numcomps > 16384) {
2113 opj_event_msg(p_manager, EVT_ERROR, "Invalid number of components specified while setting up JP2 encoder\n");
2117 j2k_setup_encoder(jp2->j2k, parameters, image,p_manager);
2119 /* setup the JP2 codec */
2120 /* ------------------- */
2124 jp2->brand = JP2_JP2; /* BR */
2125 jp2->minversion = 0; /* MinV */
2127 jp2->cl = (unsigned int*) opj_malloc(jp2->numcl * sizeof(unsigned int));
2128 jp2->cl[0] = JP2_JP2; /* CL0 : JP2 */
2130 /* Image Header box */
2132 jp2->numcomps = image->numcomps; /* NC */
2133 jp2->comps = (opj_jp2_comps_t*) opj_malloc(jp2->numcomps * sizeof(opj_jp2_comps_t));
2134 jp2->h = image->y1 - image->y0; /* HEIGHT */
2135 jp2->w = image->x1 - image->x0; /* WIDTH */
2137 depth_0 = image->comps[0].prec - 1;
2138 sign = image->comps[0].sgnd;
2139 jp2->bpc = depth_0 + (sign << 7);
2140 for (i = 1; i < image->numcomps; i++) {
2141 int depth = image->comps[i].prec - 1;
2142 sign = image->comps[i].sgnd;
2143 if (depth_0 != depth)
2146 jp2->C = 7; /* C : Always 7 */
2147 jp2->UnkC = 0; /* UnkC, colorspace specified in colr box */
2148 jp2->IPR = 0; /* IPR, no intellectual property */
2150 /* BitsPerComponent box */
2152 for (i = 0; i < image->numcomps; i++) {
2153 jp2->comps[i].bpcc = image->comps[i].prec - 1 + (image->comps[i].sgnd << 7);
2156 /* Colour Specification box */
2158 if ((image->numcomps == 1 || image->numcomps == 3) && (jp2->bpc != 255)) {
2159 jp2->meth = 1; /* METH: Enumerated colourspace */
2161 jp2->meth = 2; /* METH: Restricted ICC profile */
2163 if (jp2->meth == 1) {
2164 if (image->color_space == 1)
2165 jp2->enumcs = 16; /* sRGB as defined by IEC 61966�2�1 */
2166 else if (image->color_space == 2)
2167 jp2->enumcs = 17; /* greyscale */
2168 else if (image->color_space == 3)
2169 jp2->enumcs = 18; /* YUV */
2171 jp2->enumcs = 0; /* PROFILE (??) */
2173 jp2->precedence = 0; /* PRECEDENCE */
2174 jp2->approx = 0; /* APPROX */
2178 void jp2_setup_decoder(opj_jp2_t *jp2, opj_dparameters_t *parameters)
2180 if(!jp2 || !parameters)
2183 /* setup the J2K codec */
2184 /* ------------------- */
2185 j2k_setup_decoder(jp2->j2k, parameters);
2188 * Sets the given area to be decoded. This function should be called right after opj_read_header and before any tile header reading.
2190 * @param p_jp2 the jpeg2000 codec.
2191 * @param p_end_x the right position of the rectangle to decode (in image coordinates).
2192 * @param p_start_y the up position of the rectangle to decode (in image coordinates).
2193 * @param p_end_y the bottom position of the rectangle to decode (in image coordinates).
2194 * @param p_manager the user event manager
2196 * @return true if the area could be set.
2198 bool jp2_set_decode_area(
2200 OPJ_INT32 p_start_x,
2201 OPJ_INT32 p_start_y,
2204 struct opj_event_mgr * p_manager
2207 return j2k_set_decode_area(p_jp2->j2k,p_start_x,p_start_y,p_end_x,p_end_y,p_manager);
2216 static void jp2_write_url(opj_cio_t *cio, char *Idx_file) {
2220 box.init_pos = cio_tell(cio);
2222 cio_write(cio, JP2_URL, 4); /* DBTL */
2223 cio_write(cio, 0, 1); /* VERS */
2224 cio_write(cio, 0, 3); /* FLAG */
2227 for (i = 0; i < strlen(Idx_file); i++) {
2228 cio_write(cio, Idx_file[i], 1);
2232 box.length = cio_tell(cio) - box.init_pos;
2233 cio_seek(cio, box.init_pos);
2234 cio_write(cio, box.length, 4); /* L */
2235 cio_seek(cio, box.init_pos + box.length);