+/**
+ * Ends the compression procedures and possibility add data to be read after the
+ * codestream.
+ */
+opj_bool jp2_end_compress( 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_writting(jp2);
+
+ if (! j2k_end_compress(jp2->j2k,cio,p_manager)) {
+ return OPJ_FALSE;
+ }
+
+ /* write header */
+ return jp2_exec(jp2,jp2->m_procedure_list,cio,p_manager);
+}
+
+
+/**
+ * Sets up the procedures to do on writing header after the codestream.
+ * Developers wanting to extend the library can add their own writing procedures.
+ */
+void jp2_setup_end_header_writting (opj_jp2_v2_t *jp2)
+{
+ /* preconditions */
+ assert(jp2 != 00);
+
+ opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)jp2_write_jp2c_v2 );
+ /* DEVELOPER CORNER, add your custom procedures */
+}
+
+/**
+ * Sets up the procedures to do on reading header after the codestream.
+ * Developers wanting to extend the library can add their own writing 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,(opj_procedure)jp2_read_header_procedure );
+ /* DEVELOPER CORNER, add your custom procedures */
+}
+
+/**
+ * The default validation procedure without any extension.
+ *
+ * @param jp2 the jpeg2000 codec to validate.
+ * @param cio the input stream to validate.
+ * @param p_manager the user event manager.
+ *
+ * @return true if the parameters are correct.
+ */
+opj_bool jp2_default_validation ( opj_jp2_v2_t * jp2,
+ opj_stream_private_t *cio,
+ opj_event_mgr_t * p_manager )
+{
+ opj_bool l_is_valid = OPJ_TRUE;
+ unsigned int i;
+
+ /* preconditions */
+ assert(jp2 != 00);
+ assert(cio != 00);
+ assert(p_manager != 00);
+
+ /* JPEG2000 codec validation */
+ /*TODO*/
+
+ /* STATE checking */
+ /* make sure the state is at 0 */
+ l_is_valid &= (jp2->jp2_state == JP2_STATE_NONE);
+
+ /* make sure not reading a jp2h ???? WEIRD */
+ l_is_valid &= (jp2->jp2_img_state == JP2_IMG_STATE_NONE);
+
+ /* POINTER validation */
+ /* make sure a j2k codec is present */
+ l_is_valid &= (jp2->j2k != 00);
+
+ /* make sure a procedure list is present */
+ l_is_valid &= (jp2->m_procedure_list != 00);
+
+ /* make sure a validation list is present */
+ l_is_valid &= (jp2->m_validation_list != 00);
+
+ /* PARAMETER VALIDATION */
+ /* number of components */
+ l_is_valid &= (jp2->numcl > 0);
+ /* width */
+ l_is_valid &= (jp2->h > 0);
+ /* height */
+ l_is_valid &= (jp2->w > 0);
+ /* precision */
+ for (i = 0; i < jp2->numcomps; ++i) {
+ l_is_valid &= (jp2->comps[i].bpcc > 0);
+ }
+
+ /* METH */
+ l_is_valid &= ((jp2->meth > 0) && (jp2->meth < 3));
+
+ /* stream validation */
+ /* back and forth is needed */
+ l_is_valid &= opj_stream_has_seek(cio);
+
+ return l_is_valid;
+}
+
+/**
+ * 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);
+ if (!l_current_data){
+ opj_free(l_current_data);
+ return OPJ_FALSE;
+ }
+ 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;
+}
+
+/**
+ * Starts a compression scheme, i.e. validates the codec parameters, writes the header.
+ *
+ * @param jp2 the jpeg2000 file codec.
+ * @param cio the stream object.
+ *
+ * @return true if the codec is valid.
+ */
+opj_bool jp2_start_compress(opj_jp2_v2_t *jp2,
+ struct opj_stream_private *cio,
+ opj_image_t * p_image,
+ struct opj_event_mgr * p_manager)
+{
+ /* preconditions */
+ assert(jp2 != 00);
+ assert(cio != 00);
+ assert(p_manager != 00);
+
+ /* customization of the validation */
+ jp2_setup_encoding_validation (jp2);
+
+ /* validation of the parameters codec */
+ if (! jp2_exec(jp2,jp2->m_validation_list,cio,p_manager)) {
+ return OPJ_FALSE;
+ }
+
+ /* customization of the encoding */
+ jp2_setup_header_writting(jp2);
+
+ /* write header */
+ if (! jp2_exec (jp2,jp2->m_procedure_list,cio,p_manager)) {
+ return OPJ_FALSE;
+ }
+
+ return j2k_start_compress(jp2->j2k,cio,p_image,p_manager);
+}
+
+/**
+ * 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;
+}
+
+/**
+ * Skips the Jpeg2000 Codestream Header box - JP2C Header box.
+ *
+ * @param cio the stream to write data to.
+ * @param jp2 the jpeg2000 file codec.
+ * @param p_manager user event manager.
+ *
+ * @return true if writting was successful.