+
+/**
+ * Excutes the given procedures on the given codec.
+ *
+ * @param p_procedure_list the list of procedures to execute
+ * @param jp2 the jpeg2000 file codec to execute the procedures on.
+ * @param cio the stream to execute the procedures on.
+ * @param p_manager the user manager.
+ *
+ * @return true if all the procedures were successfully executed.
+ */
+opj_bool jp2_exec (
+ opj_jp2_v2_t * jp2,
+ opj_procedure_list_t * p_procedure_list,
+ opj_stream_private_t *cio,
+ opj_event_mgr_t * p_manager
+ )
+{
+ opj_bool (** l_procedure) (opj_jp2_v2_t * jp2, opj_stream_private_t *, opj_event_mgr_t *) = 00;
+ opj_bool l_result = OPJ_TRUE;
+ OPJ_UINT32 l_nb_proc, i;
+
+ // preconditions
+ assert(p_procedure_list != 00);
+ assert(jp2 != 00);
+ assert(cio != 00);
+ assert(p_manager != 00);
+
+ l_nb_proc = opj_procedure_list_get_nb_procedures(p_procedure_list);
+ l_procedure = (opj_bool (**) (opj_jp2_v2_t * jp2, opj_stream_private_t *, opj_event_mgr_t *)) opj_procedure_list_get_first_procedure(p_procedure_list);
+
+ for (i=0;i<l_nb_proc;++i) {
+ l_result = l_result && (*l_procedure) (jp2,cio,p_manager);
+ ++l_procedure;
+ }
+
+ // and clear the procedure list at the end.
+ opj_procedure_list_clear(p_procedure_list);
+ return l_result;
+}
+
+
+/**
+ * Finds the execution function related to the given box id.
+ *
+ * @param p_id the id of the handler to fetch.
+ *
+ * @return the given handler or 00 if it could not be found.
+ */
+const opj_jp2_header_handler_t * jp2_find_handler (int p_id)
+{
+ OPJ_UINT32 i, l_handler_size = sizeof(jp2_header) / sizeof(opj_jp2_header_handler_t);
+
+ for (i=0;i<l_handler_size;++i) {
+ if (jp2_header[i].id == p_id) {
+ return &jp2_header[i];
+ }
+ }
+ return NULL;
+}
+
+/**
+ * Finds the image execution function related to the given box id.
+ *
+ * @param p_id the id of the handler to fetch.
+ *
+ * @return the given handler or 00 if it could not be found.
+ */
+static const opj_jp2_header_handler_t * jp2_img_find_handler (
+ int p_id
+ )
+{
+ OPJ_UINT32 i, l_handler_size = sizeof(jp2_img_header) / sizeof(opj_jp2_header_handler_t);
+ for (i=0;i<l_handler_size;++i)
+ {
+ if (jp2_img_header[i].id == p_id) {
+ return &jp2_img_header[i];
+ }
+ }
+
+ return NULL;
+}
+
+
+/**
+ * Reads a jpeg2000 file signature box.
+ *
+ * @param p_header_data the data contained in the signature box.
+ * @param jp2 the jpeg2000 file codec.
+ * @param p_header_size the size of the data contained in the signature box.
+ * @param p_manager the user event manager.
+ *
+ * @return true if the file signature box is valid.
+ */
+opj_bool jp2_read_jp_v2(
+ opj_jp2_v2_t *jp2,
+ unsigned char * p_header_data,
+ unsigned int p_header_size,
+ opj_event_mgr_t * p_manager
+ )
+{
+ unsigned int l_magic_number;
+
+ // preconditions
+ assert(p_header_data != 00);
+ assert(jp2 != 00);
+ assert(p_manager != 00);
+
+ if (jp2->jp2_state != JP2_STATE_NONE) {
+ opj_event_msg_v2(p_manager, EVT_ERROR, "The signature box must be the first box in the file.\n");
+ return OPJ_FALSE;
+ }
+
+ /* assure length of data is correct (4 -> magic number) */
+ if (p_header_size != 4) {
+ opj_event_msg_v2(p_manager, EVT_ERROR, "Error with JP signature Box size\n");
+ return OPJ_FALSE;
+ }
+
+ // rearrange data
+ opj_read_bytes(p_header_data,&l_magic_number,4);
+ if (l_magic_number != 0x0d0a870a ) {
+ opj_event_msg_v2(p_manager, EVT_ERROR, "Error with JP Signature : bad magic number\n");
+ return OPJ_FALSE;
+ }
+
+ jp2->jp2_state |= JP2_STATE_SIGNATURE;
+
+ return OPJ_TRUE;
+}
+
+
+/**
+ * Reads a a FTYP box - File type box
+ *
+ * @param p_header_data the data contained in the FTYP box.
+ * @param jp2 the jpeg2000 file codec.
+ * @param p_header_size the size of the data contained in the FTYP box.
+ * @param p_manager the user event manager.
+ *
+ * @return true if the FTYP box is valid.
+ */
+opj_bool jp2_read_ftyp_v2(
+ opj_jp2_v2_t *jp2,
+ unsigned char * p_header_data,
+ unsigned int p_header_size,
+ opj_event_mgr_t * p_manager
+ )
+{
+ OPJ_UINT32 i, l_remaining_bytes;
+
+ // preconditions
+ assert(p_header_data != 00);
+ assert(jp2 != 00);
+ assert(p_manager != 00);
+
+ if (jp2->jp2_state != JP2_STATE_SIGNATURE) {
+ opj_event_msg_v2(p_manager, EVT_ERROR, "The ftyp box must be the second box in the file.\n");
+ return OPJ_FALSE;
+ }
+
+ /* assure length of data is correct */
+ if (p_header_size < 8) {
+ opj_event_msg_v2(p_manager, EVT_ERROR, "Error with FTYP signature Box size\n");
+ return OPJ_FALSE;
+ }
+
+ opj_read_bytes(p_header_data,&jp2->brand,4); /* BR */
+ p_header_data += 4;
+
+ opj_read_bytes(p_header_data,&jp2->minversion,4); /* MinV */
+ p_header_data += 4;
+
+ l_remaining_bytes = p_header_size - 8;
+
+ /* the number of remaining bytes should be a multiple of 4 */
+ if ((l_remaining_bytes & 0x3) != 0) {
+ opj_event_msg_v2(p_manager, EVT_ERROR, "Error with FTYP signature Box size\n");
+ return OPJ_FALSE;
+ }
+
+ /* div by 4 */
+ jp2->numcl = l_remaining_bytes >> 2;
+ if (jp2->numcl) {
+ jp2->cl = (unsigned int *) opj_malloc(jp2->numcl * sizeof(unsigned int));
+ if (jp2->cl == 00) {
+ opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory with FTYP Box\n");
+ return OPJ_FALSE;
+ }
+ memset(jp2->cl,0,jp2->numcl * sizeof(unsigned int));
+ }
+
+ for (i = 0; i < jp2->numcl; ++i)
+ {
+ opj_read_bytes(p_header_data,&jp2->cl[i],4); /* CLi */
+ p_header_data += 4;
+ }
+
+ jp2->jp2_state |= JP2_STATE_FILE_TYPE;
+
+ return OPJ_TRUE;
+}
+
+
+/**
+ * Reads the Jpeg2000 file Header box - JP2 Header box (warning, this is a super box).
+ *
+ * @param p_header_data the data contained in the file header box.
+ * @param jp2 the jpeg2000 file codec.
+ * @param p_header_size the size of the data contained in the file header box.
+ * @param p_manager the user event manager.
+ *
+ * @return true if the JP2 Header box was successfully reconized.
+*/
+opj_bool jp2_read_jp2h_v2(
+ opj_jp2_v2_t *jp2,
+ unsigned char * p_header_data,
+ unsigned int p_header_size,
+ opj_event_mgr_t * p_manager
+ )
+{
+ OPJ_UINT32 l_box_size=0, l_current_data_size = 0;
+ opj_jp2_box_t box;
+ const opj_jp2_header_handler_t * l_current_handler;
+
+ // preconditions
+ assert(p_header_data != 00);
+ assert(jp2 != 00);
+ assert(p_manager != 00);
+
+ /* make sure the box is well placed */
+ if ((jp2->jp2_state & JP2_STATE_FILE_TYPE) != JP2_STATE_FILE_TYPE ) {
+ opj_event_msg_v2(p_manager, EVT_ERROR, "The box must be the first box in the file.\n");
+ return OPJ_FALSE;
+ }
+
+ jp2->jp2_img_state = JP2_IMG_STATE_NONE;
+
+ /* iterate while remaining data */
+ while (p_header_size > 0) {
+
+ if (! jp2_read_boxhdr_char(&box,p_header_data,&l_box_size,p_header_size, p_manager)) {
+ opj_event_msg_v2(p_manager, EVT_ERROR, "Stream error while reading JP2 Header box\n");
+ return OPJ_FALSE;
+ }
+
+ if (box.length > p_header_size) {
+ opj_event_msg_v2(p_manager, EVT_ERROR, "Stream error while reading JP2 Header box: box length is inconsistent.\n");
+ return OPJ_FALSE;
+ }
+
+ l_current_handler = jp2_img_find_handler(box.type);
+ l_current_data_size = box.length - l_box_size;
+ p_header_data += l_box_size;
+
+ if (l_current_handler != 00) {
+ if (! l_current_handler->handler(jp2,p_header_data,l_current_data_size,p_manager)) {
+ return OPJ_FALSE;
+ }
+ }
+ else {
+ jp2->jp2_img_state |= JP2_IMG_STATE_UNKNOWN;
+ }
+
+ p_header_data += l_current_data_size;
+ p_header_size -= box.length;
+ }
+
+ jp2->jp2_state |= JP2_STATE_HEADER;
+
+ return OPJ_TRUE;
+}
+
+/**
+ * Reads a box header. The box is the way data is packed inside a jpeg2000 file structure. Data is read from a character string
+ *
+ * @param p_data the character string to read data from.
+ * @param box the box structure to fill.
+ * @param p_number_bytes_read pointer to an int that will store the number of bytes read from the stream (shoul usually be 2).
+ * @param p_box_max_size the maximum number of bytes in the box.
+ *
+ * @return true if the box is reconized, false otherwise
+*/
+static opj_bool jp2_read_boxhdr_char(
+ opj_jp2_box_t *box,
+ OPJ_BYTE * p_data,
+ OPJ_UINT32 * p_number_bytes_read,
+ OPJ_UINT32 p_box_max_size,
+ opj_event_mgr_t * p_manager
+ )
+{
+ OPJ_UINT32 l_value;
+
+ // preconditions
+ assert(p_data != 00);
+ assert(box != 00);
+ assert(p_number_bytes_read != 00);
+ assert(p_manager != 00);
+
+ if (p_box_max_size < 8) {
+ opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot handle box of less than 8 bytes\n");
+ return OPJ_FALSE;
+ }
+
+ /* process read data */
+ opj_read_bytes(p_data, &l_value, 4);
+ p_data += 4;
+ box->length = (OPJ_INT32)(l_value);
+
+ opj_read_bytes(p_data, &l_value, 4);
+ p_data += 4;
+ box->type = (OPJ_INT32)(l_value);
+
+ *p_number_bytes_read = 8;
+
+ // do we have a "special very large box ?"
+ // read then the XLBox
+ if (box->length == 1) {
+ unsigned int l_xl_part_size;
+
+ if (p_box_max_size < 16) {
+ opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot handle XL box of less than 16 bytes\n");
+ return OPJ_FALSE;
+ }
+
+ opj_read_bytes(p_data,&l_xl_part_size, 4);
+ p_data += 4;
+ *p_number_bytes_read += 4;
+
+ if (l_xl_part_size != 0) {
+ opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot handle box sizes higher than 2^32\n");
+ return OPJ_FALSE;
+ }
+
+ opj_read_bytes(p_data, &l_value, 4);
+ *p_number_bytes_read += 4;
+ box->length = (OPJ_INT32)(l_value);
+
+ if (box->length == 0) {
+ opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot handle box of undefined sizes\n");
+ return OPJ_FALSE;
+ }
+ }
+ else if (box->length == 0) {
+ opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot handle box of undefined sizes\n");
+ return OPJ_FALSE;
+ }
+
+ return OPJ_TRUE;
+}
+
+
+/**
+ * Reads a jpeg2000 file header structure.
+ *
+ * @param cio the stream to read data from.
+ * @param jp2 the jpeg2000 file header structure.
+ * @param p_manager the user event manager.
+ *
+ * @return true if the box is valid.
+ */
+opj_bool jp2_read_header( struct opj_stream_private *p_stream,
+ opj_jp2_v2_t *jp2,
+ opj_file_info_t * p_file_info,
+ struct opj_event_mgr * p_manager
+ )
+{
+ // preconditions
+ assert(jp2 != 00);
+ assert(p_stream != 00);
+ assert(p_manager != 00);
+
+ /* customization of the validation */
+ jp2_setup_decoding_validation (jp2);
+
+ /* customization of the encoding */
+ jp2_setup_header_reading(jp2);
+
+ /* validation of the parameters codec */
+ if (! jp2_exec(jp2,jp2->m_validation_list,p_stream,p_manager)) {
+ return OPJ_FALSE;
+ }
+
+ /* read header */
+ if (! jp2_exec (jp2,jp2->m_procedure_list,p_stream,p_manager)) {
+ return OPJ_FALSE;
+ }
+
+ return j2k_read_header( p_stream,
+ jp2->j2k,
+ p_file_info,
+ p_manager);
+}
+
+/**
+ * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
+ * are valid. Developpers wanting to extend the library can add their own validation procedures.
+ */
+void jp2_setup_decoding_validation (opj_jp2_v2_t *jp2)
+{
+ // preconditions
+ assert(jp2 != 00);
+ /* DEVELOPER CORNER, add your custom validation procedure */
+}
+
+/**
+ * Sets up the procedures to do on reading header.
+ * Developpers wanting to extend the library can add their own writting procedures.
+ */
+void jp2_setup_header_reading (opj_jp2_v2_t *jp2)
+{
+ // preconditions
+ assert(jp2 != 00);
+
+ opj_procedure_list_add_procedure(jp2->m_procedure_list,(void*)jp2_read_header_procedure );
+ /* DEVELOPER CORNER, add your custom procedures */
+}
+
+
+/**
+ * Reads a tile header.
+ * @param p_j2k the jpeg2000 codec.
+ * @param p_stream the stream to write data to.
+ * @param p_manager the user event manager.
+ */
+opj_bool jp2_read_tile_header (
+ opj_jp2_v2_t * p_jp2,
+ OPJ_UINT32 * p_tile_index,
+ OPJ_UINT32 * p_data_size,
+ OPJ_INT32 * p_tile_x0,
+ OPJ_INT32 * p_tile_y0,
+ OPJ_INT32 * p_tile_x1,
+ OPJ_INT32 * p_tile_y1,
+ OPJ_UINT32 * p_nb_comps,
+ opj_bool * p_go_on,
+ opj_stream_private_t *p_stream,
+ opj_event_mgr_t * p_manager
+ )
+{
+ return j2k_read_tile_header (p_jp2->j2k,
+ p_tile_index,
+ p_data_size,
+ p_tile_x0,
+ p_tile_y0,
+ p_tile_x1,
+ p_tile_y1,
+ p_nb_comps,
+ p_go_on,
+ p_stream,
+ p_manager);
+}
+
+/**
+ * Decode tile data.
+ * @param p_j2k the jpeg2000 codec.
+ * @param p_stream the stream to write data to.
+ * @param p_manager the user event manager.
+ */
+opj_bool opj_jp2_decode_tile (
+ opj_jp2_v2_t * p_jp2,
+ OPJ_UINT32 p_tile_index,
+ OPJ_BYTE * p_data,
+ OPJ_UINT32 p_data_size,
+ opj_stream_private_t *p_stream,
+ opj_event_mgr_t * p_manager
+ )
+{
+ return j2k_decode_tile (p_jp2->j2k,p_tile_index,p_data,p_data_size,p_stream,p_manager);
+}
+
+/**
+ * Destroys a jpeg2000 file decompressor.
+ *
+ * @param jp2 a jpeg2000 file decompressor.
+ */
+void jp2_destroy(opj_jp2_v2_t *jp2)
+{
+ if (jp2) {
+ /* destroy the J2K codec */
+ j2k_destroy(jp2->j2k);
+ jp2->j2k = 00;
+
+ if (jp2->comps) {
+ opj_free(jp2->comps);
+ jp2->comps = 00;
+ }
+
+ if (jp2->cl) {
+ opj_free(jp2->cl);
+ jp2->cl = 00;
+ }
+
+ if (jp2->color.icc_profile_buf) {
+ opj_free(jp2->color.icc_profile_buf);
+ jp2->color.icc_profile_buf = 00;
+ }
+
+ if (jp2->color.jp2_cdef) {
+ opj_free(jp2->color.jp2_cdef);
+ jp2->color.jp2_cdef = 00;
+ }
+
+ if (jp2->color.jp2_pclr) {
+ opj_free(jp2->color.jp2_pclr);
+ jp2->color.jp2_pclr = 00;
+ }
+
+ if (jp2->m_validation_list) {
+ opj_procedure_list_destroy(jp2->m_validation_list);
+ jp2->m_validation_list = 00;
+ }
+
+ if (jp2->m_procedure_list) {
+ opj_procedure_list_destroy(jp2->m_procedure_list);
+ jp2->m_procedure_list = 00;
+ }
+
+ opj_free(jp2);
+ }
+}
+
+/**
+ * Sets the given area to be decoded. This function should be called right after opj_read_header and before any tile header reading.
+ *
+ * @param p_jp2 the jpeg2000 codec.
+ * @param p_end_x the right position of the rectangle to decode (in image coordinates).
+ * @param p_start_y the up position of the rectangle to decode (in image coordinates).
+ * @param p_end_y the bottom position of the rectangle to decode (in image coordinates).
+ * @param p_manager the user event manager
+ *
+ * @return true if the area could be set.
+ */
+opj_bool jp2_set_decode_area(
+ opj_jp2_v2_t *p_jp2,
+ OPJ_INT32 p_start_x,
+ OPJ_INT32 p_start_y,
+ OPJ_INT32 p_end_x,
+ OPJ_INT32 p_end_y,
+ struct opj_event_mgr * p_manager
+ )
+{
+ return j2k_set_decode_area(p_jp2->j2k,p_start_x,p_start_y,p_end_x,p_end_y,p_manager);
+}
+
+/* ----------------------------------------------------------------------- */
+/* JP2 encoder interface */
+/* ----------------------------------------------------------------------- */
+
+opj_jp2_v2_t* jp2_create(opj_bool p_is_decoder)
+{
+ opj_jp2_v2_t *jp2 = (opj_jp2_v2_t*)opj_malloc(sizeof(opj_jp2_v2_t));
+ if (jp2) {
+ memset(jp2,0,sizeof(opj_jp2_t));
+
+ /* create the J2K codec */
+ if (! p_is_decoder) {
+ jp2->j2k = j2k_create_compress_v2();
+ }
+ else {
+ jp2->j2k = j2k_create_decompress_v2();
+ }
+
+ if (jp2->j2k == 00) {
+ jp2_destroy(jp2);
+ return 00;
+ }
+
+ // validation list creation
+ jp2->m_validation_list = opj_procedure_list_create();
+ if (! jp2->m_validation_list) {
+ jp2_destroy(jp2);
+ return 00;
+ }
+
+ // execution list creation
+ jp2->m_procedure_list = opj_procedure_list_create();
+ if (! jp2->m_procedure_list) {
+ jp2_destroy(jp2);
+ return 00;
+ }
+ }
+
+ return jp2;
+}