+ if(!(len_jp2c = jp2_write_jp2c( jp2, cio, image, cstr_info))){
+ opj_event_msg(jp2->cinfo, EVT_ERROR, "Failed to encode image\n");
+ return OPJ_FALSE;
+ }
+
+ if( jp2->jpip_on){
+ pos_cidx = cio_tell( cio);
+
+ write_cidx( pos_jp2c+8, cio, image, *cstr_info, len_jp2c-8);
+
+ pos_fidx = cio_tell( cio);
+ len_fidx = write_fidx( pos_jp2c, len_jp2c, pos_cidx, cio_tell(cio), cio);
+
+ end_pos = cio_tell( cio);
+
+ cio_seek( cio, pos_iptr);
+ write_iptr( pos_fidx, len_fidx, cio);
+ cio_seek( cio, end_pos);
+ }
+
+ return OPJ_TRUE;
+}
+
+/**
+ * Ends the decompression procedures and possibiliy add data to be read after the
+ * codestream.
+ */
+opj_bool jp2_end_decompress(opj_jp2_v2_t *jp2, opj_stream_private_t *cio, opj_event_mgr_t * p_manager)
+{
+ // preconditions
+ assert(jp2 != 00);
+ assert(cio != 00);
+ assert(p_manager != 00);
+
+ /* customization of the end encoding */
+ jp2_setup_end_header_reading(jp2);
+
+ /* write header */
+ if (! jp2_exec (jp2,jp2->m_procedure_list,cio,p_manager)) {
+ return OPJ_FALSE;
+ }
+
+ return j2k_end_decompress(jp2->j2k, cio, p_manager);
+}
+
+/**
+ * Sets up the procedures to do on reading header after the codestream.
+ * Developpers wanting to extend the library can add their own writting procedures.
+ */
+void jp2_setup_end_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 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_procedure(
+ opj_jp2_v2_t *jp2,
+ opj_stream_private_t *cio,
+ opj_event_mgr_t * p_manager)
+{
+ opj_jp2_box_t box;
+ OPJ_UINT32 l_nb_bytes_read;
+ const opj_jp2_header_handler_t * l_current_handler;
+ OPJ_UINT32 l_last_data_size = BOX_SIZE;
+ OPJ_UINT32 l_current_data_size;
+ unsigned char * l_current_data = 00;
+
+ // preconditions
+ assert(cio != 00);
+ assert(jp2 != 00);
+ assert(p_manager != 00);
+
+ l_current_data = (unsigned char*)opj_malloc(l_last_data_size);
+
+ if (l_current_data == 00) {
+ opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to handle jpeg2000 file header\n");
+ return OPJ_FALSE;
+ }
+ memset(l_current_data, 0 , l_last_data_size);
+
+ while (jp2_read_boxhdr_v2(&box,&l_nb_bytes_read,cio,p_manager)) {
+ // is it the codestream box ?
+ if (box.type == JP2_JP2C) {
+ if (jp2->jp2_state & JP2_STATE_HEADER) {
+ jp2->jp2_state |= JP2_STATE_CODESTREAM;
+ opj_free(l_current_data);
+ return OPJ_TRUE;
+ }
+ else {
+ opj_event_msg_v2(p_manager, EVT_ERROR, "bad placed jpeg codestream\n");
+ opj_free(l_current_data);
+ return OPJ_FALSE;
+ }
+ }
+ else if (box.length == 0) {
+ opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot handle box of undefined sizes\n");
+ opj_free(l_current_data);
+ return OPJ_FALSE;
+ }
+
+ l_current_handler = jp2_find_handler(box.type);
+ l_current_data_size = box.length - l_nb_bytes_read;
+
+ if (l_current_handler != 00) {
+ if (l_current_data_size > l_last_data_size) {
+ l_current_data = (unsigned char*)opj_realloc(l_current_data,l_current_data_size);
+ l_last_data_size = l_current_data_size;
+ }
+
+ l_nb_bytes_read = opj_stream_read_data(cio,l_current_data,l_current_data_size,p_manager);
+ if (l_nb_bytes_read != l_current_data_size) {
+ opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with reading JPEG2000 box, stream error\n");
+ return OPJ_FALSE;
+ }
+
+ if (! l_current_handler->handler(jp2,l_current_data,l_current_data_size,p_manager)) {
+ opj_free(l_current_data);
+ return OPJ_FALSE;
+ }
+ }
+ else {
+ jp2->jp2_state |= JP2_STATE_UNKNOWN;
+ if (opj_stream_skip(cio,l_current_data_size,p_manager) != l_current_data_size) {
+ opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with skipping JPEG2000 box, stream error\n");
+ opj_free(l_current_data);
+ return OPJ_FALSE;
+ }
+ }
+ }
+
+ opj_free(l_current_data);
+
+ return OPJ_TRUE;
+}
+
+/**
+ * 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;