+ l_dest_mct_rec->m_data = (OPJ_BYTE*) opj_malloc(l_src_mct_rec->m_data_size);
+ if(! l_dest_mct_rec->m_data) {
+ return OPJ_FALSE;
+ }
+ memcpy(l_dest_mct_rec->m_data,l_src_mct_rec->m_data,l_src_mct_rec->m_data_size);
+ }
+
+ ++l_src_mct_rec;
+ ++l_dest_mct_rec;
+ }
+
+ /* Get the mcc_record of the dflt_tile_cp and copy them into the current tile cp*/
+ l_mcc_records_size = l_default_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t);
+ l_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*) opj_malloc(l_mcc_records_size);
+ if (! l_tcp->m_mcc_records) {
+ return OPJ_FALSE;
+ }
+ memcpy(l_tcp->m_mcc_records,l_default_tcp->m_mcc_records,l_mcc_records_size);
+
+ /* Copy the mcc record data from dflt_tile_cp to the current tile*/
+ l_src_mcc_rec = l_default_tcp->m_mcc_records;
+ l_dest_mcc_rec = l_tcp->m_mcc_records;
+
+ for (j=0;j<l_default_tcp->m_nb_max_mcc_records;++j) {
+
+ if (l_src_mcc_rec->m_decorrelation_array) {
+ l_offset = l_src_mcc_rec->m_decorrelation_array - l_default_tcp->m_mct_records;
+ l_dest_mcc_rec->m_decorrelation_array = l_tcp->m_mct_records + l_offset;
+ }
+
+ if (l_src_mcc_rec->m_offset_array) {
+ l_offset = l_src_mcc_rec->m_offset_array - l_default_tcp->m_mct_records;
+ l_dest_mcc_rec->m_offset_array = l_tcp->m_mct_records + l_offset;
+ }
+
+ ++l_src_mcc_rec;
+ ++l_dest_mcc_rec;
+ }
+
+ /* Copy all the dflt_tile_compo_cp to the current tile cp */
+ memcpy(l_current_tccp,l_default_tcp->tccps,l_tccp_size);
+
+ /* Move to next tile cp*/
+ ++l_tcp;
+ }
+
+ /* Create the current tile decoder*/
+ p_j2k->m_tcd = (opj_tcd_v2_t*)tcd_create_v2(OPJ_TRUE); // FIXME why a cast ?
+ if (! p_j2k->m_tcd ) {
+ return OPJ_FALSE;
+ }
+
+ if ( !tcd_init_v2(p_j2k->m_tcd, l_image, &(p_j2k->m_cp)) ) {
+ tcd_destroy_v2(p_j2k->m_tcd);
+ p_j2k->m_tcd = 00;
+ opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
+ return OPJ_FALSE;
+ }
+
+ return OPJ_TRUE;
+}
+
+/**
+ * Reads the lookup table containing all the marker, status and action, and returns the handler associated
+ * with the marker value.
+ * @param p_id Marker value to look up
+ *
+ * @return the handler associated with the id.
+*/
+const opj_dec_memory_marker_handler_t * j2k_get_marker_handler (const OPJ_UINT32 p_id)
+{
+ const opj_dec_memory_marker_handler_t *e;
+ for (e = j2k_memory_marker_handler_tab; e->id != 0; ++e) {
+ if (e->id == p_id) {
+ break; // we find a handler corresponding to the marker ID
+ }
+ }
+ return e;
+}
+
+
+/**
+ * Destroys a jpeg2000 codec.
+ *
+ * @param p_j2k the jpeg20000 structure to destroy.
+ */
+void j2k_destroy (opj_j2k_v2_t *p_j2k)
+{
+ if (p_j2k == 00) {
+ return;
+ }
+
+ if (p_j2k->m_is_decoder) {
+
+ if (p_j2k->m_specific_param.m_decoder.m_default_tcp != 00) {
+ j2k_tcp_destroy(p_j2k->m_specific_param.m_decoder.m_default_tcp);
+ opj_free(p_j2k->m_specific_param.m_decoder.m_default_tcp);
+ p_j2k->m_specific_param.m_decoder.m_default_tcp = 00;
+ }
+
+ if (p_j2k->m_specific_param.m_decoder.m_header_data != 00) {
+ opj_free(p_j2k->m_specific_param.m_decoder.m_header_data);
+ p_j2k->m_specific_param.m_decoder.m_header_data = 00;
+ p_j2k->m_specific_param.m_decoder.m_header_data_size = 0;
+ }
+ }
+ else {
+
+ if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data) {
+ opj_free(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
+ p_j2k->m_specific_param.m_encoder.m_encoded_tile_data = 00;
+ }
+
+ if (p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
+ opj_free(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer);
+ p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer = 00;
+ p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current = 00;
+ }
+
+ if (p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
+ opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
+ p_j2k->m_specific_param.m_encoder.m_header_tile_data = 00;
+ p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
+ }
+ }
+
+ tcd_destroy_v2(p_j2k->m_tcd);
+
+ j2k_cp_destroy(&(p_j2k->m_cp));
+ memset(&(p_j2k->m_cp),0,sizeof(opj_cp_t));
+
+ opj_procedure_list_destroy(p_j2k->m_procedure_list);
+ p_j2k->m_procedure_list = 00;
+
+ opj_procedure_list_destroy(p_j2k->m_validation_list);
+ p_j2k->m_procedure_list = 00;
+
+ j2k_destroy_cstr_index(p_j2k->cstr_index);
+ p_j2k->cstr_index = NULL;
+
+ opj_free(p_j2k);
+}
+
+void j2k_destroy_cstr_index (opj_codestream_index_t *p_cstr_ind)
+{
+ if (!p_cstr_ind) {
+ return;
+ }
+
+ if (p_cstr_ind->marker) {
+ opj_free(p_cstr_ind->marker);
+ p_cstr_ind->marker = NULL;
+ }
+
+ if (p_cstr_ind->tile_index) {
+ // FIXME
+ }
+}
+
+
+
+/**
+ * Destroys a tile coding parameter structure.
+ *
+ * @param p_tcp the tile coding parameter to destroy.
+ */
+void j2k_tcp_destroy (opj_tcp_v2_t *p_tcp)
+{
+ if (p_tcp == 00) {
+ return;
+ }
+
+ if (p_tcp->ppt_buffer != 00) {
+ opj_free(p_tcp->ppt_buffer);
+ p_tcp->ppt_buffer = 00;
+ }
+
+ if (p_tcp->tccps != 00) {
+ opj_free(p_tcp->tccps);
+ p_tcp->tccps = 00;
+ }
+
+ if (p_tcp->m_mct_coding_matrix != 00) {
+ opj_free(p_tcp->m_mct_coding_matrix);
+ p_tcp->m_mct_coding_matrix = 00;
+ }
+
+ if (p_tcp->m_mct_decoding_matrix != 00) {
+ opj_free(p_tcp->m_mct_decoding_matrix);
+ p_tcp->m_mct_decoding_matrix = 00;
+ }
+
+ if (p_tcp->m_mcc_records) {
+ opj_free(p_tcp->m_mcc_records);
+ p_tcp->m_mcc_records = 00;
+ p_tcp->m_nb_max_mcc_records = 0;
+ p_tcp->m_nb_mcc_records = 0;
+ }
+
+ if (p_tcp->m_mct_records) {
+ opj_mct_data_t * l_mct_data = p_tcp->m_mct_records;
+ OPJ_UINT32 i;
+
+ for (i=0;i<p_tcp->m_nb_mct_records;++i) {
+ if (l_mct_data->m_data) {
+ opj_free(l_mct_data->m_data);
+ l_mct_data->m_data = 00;
+ }
+
+ ++l_mct_data;
+ }
+
+ opj_free(p_tcp->m_mct_records);
+ p_tcp->m_mct_records = 00;
+ }
+
+ if (p_tcp->mct_norms != 00) {
+ opj_free(p_tcp->mct_norms);
+ p_tcp->mct_norms = 00;
+ }
+
+ if (p_tcp->m_data) {
+ opj_free(p_tcp->m_data);
+ p_tcp->m_data = 00;
+ }
+}
+
+
+/**
+ * Destroys a coding parameter structure.
+ *
+ * @param p_cp the coding parameter to destroy.
+ */
+void j2k_cp_destroy (opj_cp_v2_t *p_cp)
+{
+ OPJ_UINT32 l_nb_tiles;
+ opj_tcp_v2_t * l_current_tile = 00;
+ OPJ_UINT32 i;
+
+ if
+ (p_cp == 00)
+ {
+ return;
+ }
+ if
+ (p_cp->tcps != 00)
+ {
+ l_current_tile = p_cp->tcps;
+ l_nb_tiles = p_cp->th * p_cp->tw;
+
+ for
+ (i = 0; i < l_nb_tiles; ++i)
+ {
+ j2k_tcp_destroy(l_current_tile);
+ ++l_current_tile;
+ }
+ opj_free(p_cp->tcps);
+ p_cp->tcps = 00;
+ }
+ if
+ (p_cp->ppm_buffer != 00)
+ {
+ opj_free(p_cp->ppm_buffer);
+ p_cp->ppm_buffer = 00;
+ }
+ if
+ (p_cp->comment != 00)
+ {
+ opj_free(p_cp->comment);
+ p_cp->comment = 00;
+ }
+ if
+ (! p_cp->m_is_decoder)
+ {
+ if
+ (p_cp->m_specific_param.m_enc.m_matrice)
+ {
+ opj_free(p_cp->m_specific_param.m_enc.m_matrice);
+ p_cp->m_specific_param.m_enc.m_matrice = 00;
+ }
+ }
+}
+
+
+
+/**
+ * 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 j2k_read_tile_header( opj_j2k_v2_t * p_j2k,
+ 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 )
+{
+ OPJ_UINT32 l_current_marker = J2K_MS_SOT;
+ OPJ_UINT32 l_marker_size;
+ const opj_dec_memory_marker_handler_t * l_marker_handler = 00;
+ opj_tcp_v2_t * l_tcp = NULL;
+ OPJ_UINT32 l_nb_tiles;
+
+ /* preconditions */
+ assert(p_stream != 00);
+ assert(p_j2k != 00);
+ assert(p_manager != 00);
+
+ /* Reach the End Of Codestream ?*/
+ if (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_EOC){
+ l_current_marker = J2K_MS_EOC;
+ }
+ /* We need to encounter a SOT marker (a new tile-part header) */
+ else if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_TPHSOT){
+ return OPJ_FALSE;
+ }
+
+ /* Read into the codestream until reach the EOC or ! can_decode ??? FIXME */
+ while ( (!p_j2k->m_specific_param.m_decoder.m_can_decode) && (l_current_marker != J2K_MS_EOC) ) {
+
+ /* Try to read until the Start Of Data is detected */
+ while (l_current_marker != J2K_MS_SOD) {
+
+ /* Try to read 2 bytes (the marker size) from stream and copy them into the buffer */
+ if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
+ opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
+ return OPJ_FALSE;
+ }
+
+ /* Read 2 bytes from the buffer as the marker size */
+ opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_marker_size,2);
+
+ /* Why this condition? FIXME */
+ if (p_j2k->m_specific_param.m_decoder.m_state & J2K_STATE_TPH){
+ p_j2k->m_specific_param.m_decoder.m_sot_length -= (l_marker_size + 2);
+ }
+ l_marker_size -= 2; /* Subtract the size of the marker ID already read */
+
+ /* Get the marker handler from the marker ID */
+ l_marker_handler = j2k_get_marker_handler(l_current_marker);
+
+ /* Check if the marker is known and if it is the right place to find it */
+ if (! (p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states) ) {
+ opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
+ return OPJ_FALSE;
+ }
+/* FIXME manage case of unknown marker as in the main header ? */
+
+ /* Check if the marker size is compatible with the header data size */
+ if (l_marker_size > p_j2k->m_specific_param.m_decoder.m_header_data_size) {
+ p_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE*)
+ opj_realloc(p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size);
+ if (p_j2k->m_specific_param.m_decoder.m_header_data == 00) {
+ return OPJ_FALSE;
+ }
+ p_j2k->m_specific_param.m_decoder.m_header_data_size = l_marker_size;
+ }
+
+ /* Try to read the rest of the marker segment from stream and copy them into the buffer */
+ if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager) != l_marker_size) {
+ opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
+ return OPJ_FALSE;
+ }
+
+ /* Read the marker segment with the correct marker handler */
+ if (! (*(l_marker_handler->handler))(p_j2k,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager)) {
+ opj_event_msg_v2(p_manager, EVT_ERROR, "Fail to read the current marker segment (%#x)\n", l_current_marker);
+ return OPJ_FALSE;
+ }
+
+ if (p_j2k->m_specific_param.m_decoder.m_skip_data) {
+ /* Skip the rest of the tile part header*/
+ if (opj_stream_skip(p_stream,p_j2k->m_specific_param.m_decoder.m_sot_length,p_manager) != p_j2k->m_specific_param.m_decoder.m_sot_length) {
+ opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
+ return OPJ_FALSE;
+ }
+ l_current_marker = J2K_MS_SOD; /* Normally we reached a SOD */
+ }
+ else {
+ /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer*/
+ if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
+ opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
+ return OPJ_FALSE;
+ }
+ /* Read 2 bytes from the buffer as the new marker ID */
+ opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
+ }
+ }
+
+ /* If we didn't skip data before, we need to read the SOD marker*/
+ if (! p_j2k->m_specific_param.m_decoder.m_skip_data) {
+ /* Try to read the SOD marker and skip data ? FIXME */
+ if (! j2k_read_sod_v2(p_j2k, p_stream, p_manager)) {
+ return OPJ_FALSE;
+ }
+
+ if (! p_j2k->m_specific_param.m_decoder.m_can_decode){
+ /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
+ if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
+ opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
+ return OPJ_FALSE;
+ }
+
+ /* Read 2 bytes from buffer as the new marker ID */
+ opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
+ }
+ }
+ else {
+ /* Indicate we will try to read a new tile-part header*/
+ p_j2k->m_specific_param.m_decoder.m_skip_data = 0;
+ p_j2k->m_specific_param.m_decoder.m_can_decode = 0;
+ p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
+
+ /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
+ if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
+ opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
+ return OPJ_FALSE;
+ }
+
+ /* Read 2 bytes from buffer as the new marker ID */
+ opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
+ }
+ }
+
+ /* Current marker is the EOC marker ?*/
+ if (l_current_marker == J2K_MS_EOC) {
+ if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_EOC ){
+ p_j2k->m_current_tile_number = 0;
+ p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_EOC;
+ }
+ }
+
+ /* FIXME DOC ???*/
+ if ( ! p_j2k->m_specific_param.m_decoder.m_can_decode) {
+ l_tcp = p_j2k->m_cp.tcps + p_j2k->m_current_tile_number;
+ l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
+
+ while( (p_j2k->m_current_tile_number < l_nb_tiles) && (l_tcp->m_data == 00) ) {
+ ++p_j2k->m_current_tile_number;
+ ++l_tcp;
+ }
+
+ if (p_j2k->m_current_tile_number == l_nb_tiles) {
+ *p_go_on = OPJ_FALSE;
+ return OPJ_TRUE;
+ }
+ }
+
+ /*FIXME ???*/
+ if (! tcd_init_decode_tile(p_j2k->m_tcd, p_j2k->m_current_tile_number)) {
+ opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
+ return OPJ_FALSE;
+ }
+
+ opj_event_msg_v2(p_manager, EVT_INFO, "Header of tile %d / %d has been read.\n",
+ p_j2k->m_current_tile_number +1, p_j2k->m_cp.th * p_j2k->m_cp.tw);
+
+ *p_tile_index = p_j2k->m_current_tile_number;
+ *p_go_on = OPJ_TRUE;
+ *p_data_size = tcd_get_decoded_tile_size(p_j2k->m_tcd);
+ *p_tile_x0 = p_j2k->m_tcd->tcd_image->tiles->x0;
+ *p_tile_y0 = p_j2k->m_tcd->tcd_image->tiles->y0;
+ *p_tile_x1 = p_j2k->m_tcd->tcd_image->tiles->x1;
+ *p_tile_y1 = p_j2k->m_tcd->tcd_image->tiles->y1;
+ *p_nb_comps = p_j2k->m_tcd->tcd_image->tiles->numcomps;
+
+ p_j2k->m_specific_param.m_decoder.m_state |= 0x0080;// FIXME J2K_DEC_STATE_DATA;
+
+ return OPJ_TRUE;
+}
+
+
+opj_bool j2k_decode_tile ( opj_j2k_v2_t * p_j2k,
+ 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 )
+{
+ OPJ_UINT32 l_current_marker;
+ OPJ_BYTE l_data [2];
+ opj_tcp_v2_t * l_tcp;
+
+ /* preconditions */
+ assert(p_stream != 00);
+ assert(p_j2k != 00);
+ assert(p_manager != 00);
+
+ if ( !(p_j2k->m_specific_param.m_decoder.m_state & 0x0080/*FIXME J2K_DEC_STATE_DATA*/)
+ || (p_tile_index != p_j2k->m_current_tile_number) ) {
+ return OPJ_FALSE;
+ }
+
+ l_tcp = &(p_j2k->m_cp.tcps[p_tile_index]);
+ if (! l_tcp->m_data) {
+ j2k_tcp_destroy(&(p_j2k->m_cp.tcps[p_tile_index]));
+ return OPJ_FALSE;
+ }
+
+ if (! tcd_decode_tile_v2( p_j2k->m_tcd,
+ l_tcp->m_data,
+ l_tcp->m_data_size,
+ p_tile_index,
+ p_j2k->cstr_index) ) {
+ j2k_tcp_destroy(l_tcp);
+ p_j2k->m_specific_param.m_decoder.m_state |= 0x8000;//FIXME J2K_DEC_STATE_ERR;
+ return OPJ_FALSE;
+ }
+
+ if (! tcd_update_tile_data(p_j2k->m_tcd,p_data,p_data_size)) {
+ return OPJ_FALSE;
+ }
+
+ j2k_tcp_destroy(l_tcp);
+ p_j2k->m_tcd->tcp = 0;
+
+ p_j2k->m_specific_param.m_decoder.m_can_decode = 0;
+ p_j2k->m_specific_param.m_decoder.m_state &= (~ (0x0080));// FIXME J2K_DEC_STATE_DATA);
+
+ if (p_j2k->m_specific_param.m_decoder.m_state != 0x0100){ //FIXME J2K_DEC_STATE_EOC)
+ if (opj_stream_read_data(p_stream,l_data,2,p_manager) != 2) {
+ opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
+ return OPJ_FALSE;
+ }
+
+ opj_read_bytes(l_data,&l_current_marker,2);
+
+ if (l_current_marker == J2K_MS_EOC) {
+ p_j2k->m_current_tile_number = 0;
+ p_j2k->m_specific_param.m_decoder.m_state = 0x0100;//FIXME J2K_DEC_STATE_EOC;
+ }
+ else if (l_current_marker != J2K_MS_SOT)
+ {
+ opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short, expected SOT\n");
+ return OPJ_FALSE;
+ }
+ }
+
+ return OPJ_TRUE;
+}
+
+
+opj_bool j2k_update_image_data (opj_tcd_v2_t * p_tcd, OPJ_BYTE * p_data)
+{
+ OPJ_UINT32 i,j,k = 0;
+ OPJ_UINT32 l_width,l_height,l_offset_x,l_offset_y;
+ opj_image_comp_t * l_img_comp = 00;
+ opj_tcd_tilecomp_v2_t * l_tilec = 00;
+ opj_image_t * l_image = 00;
+ OPJ_UINT32 l_size_comp, l_remaining;
+ OPJ_UINT32 l_dest_stride;
+ OPJ_INT32 * l_dest_ptr;
+ opj_tcd_resolution_v2_t* l_res= 00;
+
+
+ l_tilec = p_tcd->tcd_image->tiles->comps;
+ l_image = p_tcd->image;
+ l_img_comp = l_image->comps;
+
+ for (i=0;i<p_tcd->image->numcomps;++i) {
+
+ if (!l_img_comp->data) {
+
+ l_img_comp->data = (OPJ_INT32*) opj_malloc(l_img_comp->w * l_img_comp->h * sizeof(OPJ_INT32));
+ if (! l_img_comp->data) {
+ return OPJ_FALSE;
+ }
+ memset(l_img_comp->data,0,l_img_comp->w * l_img_comp->h * sizeof(OPJ_INT32));
+ }
+
+ l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
+ l_remaining = l_img_comp->prec & 7; /* (%8) */
+ l_res = l_tilec->resolutions + l_img_comp->resno_decoded;
+
+ if (l_remaining) {
+ ++l_size_comp;
+ }
+
+ if (l_size_comp == 3) {
+ l_size_comp = 4;
+ }
+
+ l_width = (l_res->x1 - l_res->x0);
+ l_height = (l_res->y1 - l_res->y0);
+
+ l_dest_stride = (l_img_comp->w) - l_width;
+
+ l_offset_x = int_ceildivpow2(l_img_comp->x0, l_img_comp->factor);
+ l_offset_y = int_ceildivpow2(l_img_comp->y0, l_img_comp->factor);
+ l_dest_ptr = l_img_comp->data + (l_res->x0 - l_offset_x) + (l_res->y0 - l_offset_y) * l_img_comp->w;
+
+ switch (l_size_comp) {
+ case 1:
+ {
+ OPJ_CHAR * l_src_ptr = (OPJ_CHAR*) p_data;
+ if (l_img_comp->sgnd) {
+ for (j=0;j<l_height;++j) {
+ for (k=0;k<l_width;++k) {
+ *(l_dest_ptr++) = (OPJ_INT32) (*(l_src_ptr++));
+ }
+
+ l_dest_ptr += l_dest_stride;
+ }
+ }
+ else {
+ for (j=0;j<l_height;++j) {
+ for (k=0;k<l_width;++k) {
+ *(l_dest_ptr++) = (OPJ_INT32) ((*(l_src_ptr++))&0xff);
+ }
+
+ l_dest_ptr += l_dest_stride;
+ }
+ }
+
+ p_data = (OPJ_BYTE*) l_src_ptr;
+ }
+ break;
+ case 2:
+ {
+ OPJ_INT16 * l_src_ptr = (OPJ_INT16 *) p_data;
+
+ if (l_img_comp->sgnd) {
+ for (j=0;j<l_height;++j) {
+ for (k=0;k<l_width;++k) {
+ *(l_dest_ptr++) = *(l_src_ptr++);
+ }
+
+ l_dest_ptr += l_dest_stride;
+ }
+ }
+ else {
+ for (j=0;j<l_height;++j) {
+ for (k=0;k<l_width;++k) {
+ *(l_dest_ptr++) = (*(l_src_ptr++))&0xffff;
+ }
+
+ l_dest_ptr += l_dest_stride;
+ }
+ }
+
+ p_data = (OPJ_BYTE*) l_src_ptr;
+ }
+ break;
+ case 4:
+ {
+ OPJ_INT32 * l_src_ptr = (OPJ_INT32 *) p_data;
+ for (j=0;j<l_height;++j) {
+ for (k=0;k<l_width;++k) {
+ *(l_dest_ptr++) = (*(l_src_ptr++));
+ }
+
+ l_dest_ptr += l_dest_stride;
+ }
+
+ p_data = (OPJ_BYTE*) l_src_ptr;
+ }
+ break;
+ }
+
+ ++l_img_comp;
+ ++l_tilec;
+ }
+ return OPJ_TRUE;
+}
+
+/**
+ * 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_j2k the jpeg2000 codec.
+ * @param p_start_x the left position of the rectangle to decode (in image coordinates).
+ * @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 j2k_set_decode_area( opj_j2k_v2_t *p_j2k,
+ 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 )
+{
+ opj_cp_v2_t * l_cp = &(p_j2k->m_cp);
+
+ /* Check if we are read the main header */
+ if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_TPHSOT) { // FIXME J2K_DEC_STATE_TPHSOT)
+ opj_event_msg_v2(p_manager, EVT_ERROR, "Need to decode the main header before begin to decode the remaining codestream");
+ return OPJ_FALSE;
+ }
+
+ /* ----- */
+ /* Check if the positions provided by the user are correct */
+
+ /* Left */
+ if (p_start_x > l_cp->tx0 + l_cp->tw * l_cp->tdx) {
+ opj_event_msg_v2(p_manager, EVT_ERROR,
+ "Left position of the decoded area (ROI_x0=%d) is outside the tile area (XTOsiz + nb_tw x XTsiz=%d).\n",
+ p_start_x, l_cp->tx0 + l_cp->tw * l_cp->tdx);
+ return OPJ_FALSE;
+ }
+ else if (p_start_x < l_cp->tx0){
+ opj_event_msg_v2(p_manager, EVT_WARNING,
+ "Left position of the decoded area (ROI_x0=%d) is outside the tile area (XTOsiz=%d).\n",
+ p_start_x, l_cp->tx0);
+ p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
+ }
+ else
+ p_j2k->m_specific_param.m_decoder.m_start_tile_x = (p_start_x - l_cp->tx0) / l_cp->tdx;
+
+ /* Up */
+ if (p_start_y > l_cp->ty0 + l_cp->th * l_cp->tdy){
+ opj_event_msg_v2(p_manager, EVT_ERROR,
+ "Up position of the decoded area (ROI_y0=%d) is outside the tile area (YTOsiz + nb_th x YTsiz=%d).\n",
+ p_start_y, l_cp->ty0 + l_cp->th * l_cp->tdy);
+ return OPJ_FALSE;
+ }
+ else if (p_start_y < l_cp->ty0){
+ opj_event_msg_v2(p_manager, EVT_WARNING,
+ "Up position of the decoded area (ROI_y0=%d) is outside the tile area (YTOsiz=%d).\n",
+ p_start_y, l_cp->ty0);
+ p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
+ }
+ else
+ p_j2k->m_specific_param.m_decoder.m_start_tile_y = (p_start_y - l_cp->ty0) / l_cp->tdy;
+
+ /* Right */
+ if (p_end_x < l_cp->tx0) {
+ opj_event_msg_v2(p_manager, EVT_ERROR,
+ "Right position of the decoded area (ROI_x1=%d) is outside the tile area (XTOsiz=%d).\n",
+ p_end_x, l_cp->tx0);
+ return OPJ_FALSE;
+ }
+ else if (p_end_x > l_cp->tx0 + l_cp->tw * l_cp->tdx) {
+ opj_event_msg_v2(p_manager, EVT_WARNING,
+ "Right position of the decoded area (ROI_x1=%d) is outside the tile area (XTOsiz + nb_tw x XTsiz=%d).\n",
+ p_end_x, l_cp->tx0 + l_cp->tw * l_cp->tdx);
+ p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw; // FIXME (-1) ???
+ }
+ else
+ p_j2k->m_specific_param.m_decoder.m_end_tile_x = int_ceildiv((p_end_x - l_cp->tx0), l_cp->tdx);
+
+ /* Bottom */
+ if (p_end_y < l_cp->ty0) {
+ opj_event_msg_v2(p_manager, EVT_ERROR,
+ "Right position of the decoded area (ROI_y1=%d) is outside the tile area (YTOsiz=%d).\n",
+ p_end_y, l_cp->ty0);
+ return OPJ_FALSE;
+ }
+ if (p_end_y > l_cp->ty0 + l_cp->th * l_cp->tdy){
+ opj_event_msg_v2(p_manager, EVT_WARNING,
+ "Bottom position of the decoded area (ROI_y1=%d) is outside the tile area (YTOsiz + nb_th x YTsiz=%d).\n",
+ p_end_y, l_cp->ty0 + l_cp->th * l_cp->tdy);
+ p_j2k->m_specific_param.m_decoder.m_start_tile_y = l_cp->th; // FIXME (-1) ???
+ }
+ else
+ p_j2k->m_specific_param.m_decoder.m_end_tile_y = int_ceildiv((p_end_y - l_cp->ty0), l_cp->tdy);
+ /* ----- */
+
+ p_j2k->m_specific_param.m_decoder.m_discard_tiles = 1;
+
+ return OPJ_TRUE;
+}
+
+
+/* ----------------------------------------------------------------------- */
+/* J2K / JPT decoder interface */
+/* ----------------------------------------------------------------------- */
+/**
+ * Creates a J2K decompression structure.
+ *
+ * @return a handle to a J2K decompressor if successful, NULL otherwise.
+*/
+opj_j2k_v2_t* j2k_create_decompress_v2()
+{
+ opj_j2k_v2_t *l_j2k = (opj_j2k_v2_t*) opj_malloc(sizeof(opj_j2k_v2_t));
+ if (!l_j2k) {
+ return 00;
+ }
+ memset(l_j2k,0,sizeof(opj_j2k_v2_t));
+
+ l_j2k->m_is_decoder = 1;
+ l_j2k->m_cp.m_is_decoder = 1;
+
+ l_j2k->m_specific_param.m_decoder.m_default_tcp = (opj_tcp_v2_t*) opj_malloc(sizeof(opj_tcp_v2_t));
+ if (!l_j2k->m_specific_param.m_decoder.m_default_tcp) {
+ j2k_destroy(l_j2k);
+ return 00;
+ }
+ memset(l_j2k->m_specific_param.m_decoder.m_default_tcp,0,sizeof(opj_tcp_v2_t));
+
+ l_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE *) opj_malloc(J2K_DEFAULT_HEADER_SIZE);
+ if (! l_j2k->m_specific_param.m_decoder.m_header_data) {
+ j2k_destroy(l_j2k);
+ return 00;
+ }
+
+ l_j2k->m_specific_param.m_decoder.m_header_data_size = J2K_DEFAULT_HEADER_SIZE;
+
+ /* codestream index creation */
+ l_j2k->cstr_index = j2k_create_cstr_index();
+
+ /*(opj_codestream_index_t*) opj_malloc(sizeof(opj_codestream_index_t));
+ if (!l_j2k->cstr_index){
+ j2k_destroy(l_j2k);
+ return NULL;
+ }
+
+ l_j2k->cstr_index->marker = (opj_marker_info_t*) opj_malloc(100 * sizeof(opj_marker_info_t));
+*/
+
+ /* validation list creation */
+ l_j2k->m_validation_list = opj_procedure_list_create();
+ if (! l_j2k->m_validation_list) {
+ j2k_destroy(l_j2k);
+ return 00;
+ }
+
+ /* execution list creation */
+ l_j2k->m_procedure_list = opj_procedure_list_create();
+ if (! l_j2k->m_procedure_list) {
+ j2k_destroy(l_j2k);
+ return 00;
+ }
+
+ return l_j2k;
+}
+
+
+opj_codestream_index_t* j2k_create_cstr_index(void)
+{
+ opj_codestream_index_t* cstr_index = (opj_codestream_index_t*)
+ opj_calloc(1,sizeof(opj_codestream_index_t));
+ if (!cstr_index)
+ return NULL;
+
+ cstr_index->maxmarknum = 100;
+ cstr_index->marknum = 0;
+ cstr_index->marker = (opj_marker_info_t*)
+ opj_calloc(cstr_index->maxmarknum, sizeof(opj_marker_info_t));
+ if (!cstr_index-> marker)
+ return NULL;
+
+ cstr_index->tile_index = NULL;
+
+ return cstr_index;
+}
+
+/**
+ * Reads a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.
+ * @param p_header_data the data contained in the COM box.
+ * @param p_j2k the jpeg2000 codec.
+ * @param p_header_size the size of the data contained in the COM marker.
+ * @param p_manager the user event manager.
+*/
+opj_bool j2k_read_SPCod_SPCoc(
+ opj_j2k_v2_t *p_j2k,
+ OPJ_UINT32 compno,
+ OPJ_BYTE * p_header_data,
+ OPJ_UINT32 * p_header_size,
+ struct opj_event_mgr * p_manager
+ )
+{
+ OPJ_UINT32 i, l_tmp;
+ opj_cp_v2_t *l_cp = NULL;
+ opj_tcp_v2_t *l_tcp = NULL;
+ opj_tccp_t *l_tccp = NULL;
+ OPJ_BYTE * l_current_ptr = NULL;
+
+ /* preconditions */
+ assert(p_j2k != 00);
+ assert(p_manager != 00);
+ assert(p_header_data != 00);
+
+ l_cp = &(p_j2k->m_cp);
+ l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
+ &l_cp->tcps[p_j2k->m_current_tile_number] :
+ p_j2k->m_specific_param.m_decoder.m_default_tcp;
+
+ /* precondition again */
+ assert(compno < p_j2k->m_image->numcomps);
+
+ l_tccp = &l_tcp->tccps[compno];
+ l_current_ptr = p_header_data;
+
+ /* make sure room is sufficient */
+ if (*p_header_size < 5) {
+ opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element\n");
+ return OPJ_FALSE;
+ }
+
+ opj_read_bytes(l_current_ptr, &l_tccp->numresolutions ,1); /* SPcox (D) */
+ ++l_tccp->numresolutions; /* tccp->numresolutions = read() + 1 */
+ ++l_current_ptr;
+
+ /* If user wants to remove more resolutions than the codestream contains, return error */
+ if (l_cp->m_specific_param.m_dec.m_reduce >= l_tccp->numresolutions) {
+ opj_event_msg_v2(p_manager, EVT_ERROR, "Error decoding component %d.\nThe number of resolutions to remove is higher than the number "
+ "of resolutions of this component\nModify the cp_reduce parameter.\n\n", compno);
+ p_j2k->m_specific_param.m_decoder.m_state |= 0x8000;// FIXME J2K_DEC_STATE_ERR;
+ return OPJ_FALSE;
+ }
+
+ opj_read_bytes(l_current_ptr,&l_tccp->cblkw ,1); /* SPcoc (E) */
+ ++l_current_ptr;
+ l_tccp->cblkw += 2;
+
+ opj_read_bytes(l_current_ptr,&l_tccp->cblkh ,1); /* SPcoc (F) */
+ ++l_current_ptr;
+ l_tccp->cblkh += 2;
+
+ opj_read_bytes(l_current_ptr,&l_tccp->cblksty ,1); /* SPcoc (G) */
+ ++l_current_ptr;
+
+ opj_read_bytes(l_current_ptr,&l_tccp->qmfbid ,1); /* SPcoc (H) */
+ ++l_current_ptr;
+
+ *p_header_size = *p_header_size - 5;
+
+ /* use custom precinct size ? */
+ if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
+ if (*p_header_size < l_tccp->numresolutions) {
+ opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element\n");
+ return OPJ_FALSE;
+ }
+
+ for (i = 0; i < l_tccp->numresolutions; ++i) {
+ opj_read_bytes(l_current_ptr,&l_tmp ,1); /* SPcoc (I_i) */
+ ++l_current_ptr;
+ l_tccp->prcw[i] = l_tmp & 0xf;
+ l_tccp->prch[i] = l_tmp >> 4;
+ }
+
+ *p_header_size = *p_header_size - l_tccp->numresolutions;
+ }
+ else {
+ /* set default size for the precinct width and height */
+ for (i = 0; i < l_tccp->numresolutions; ++i) {
+ l_tccp->prcw[i] = 15;
+ l_tccp->prch[i] = 15;
+ }
+ }
+
+#ifdef WIP_REMOVE_MSD
+ /* INDEX >> */
+ if (p_j2k->cstr_info && compno == 0) {
+ OPJ_UINT32 l_data_size = l_tccp->numresolutions * sizeof(OPJ_UINT32);
+
+ p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblkh = l_tccp->cblkh;
+ p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblkw = l_tccp->cblkw;
+ p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].numresolutions = l_tccp->numresolutions;
+ p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblksty = l_tccp->cblksty;
+ p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].qmfbid = l_tccp->qmfbid;
+
+
+ memcpy(p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].pdx,l_tccp->prcw, l_data_size);
+ memcpy(p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].pdy,l_tccp->prch, l_data_size);
+ }
+ /* << INDEX */
+#endif
+
+ return OPJ_TRUE;
+}
+
+/**
+ * Copies the tile component parameters of all the component from the first tile component.
+ *
+ * @param p_j2k the J2k codec.
+ */
+void j2k_copy_tile_component_parameters( opj_j2k_v2_t *p_j2k )
+{
+ // loop
+ OPJ_UINT32 i;
+ opj_cp_v2_t *l_cp = NULL;
+ opj_tcp_v2_t *l_tcp = NULL;
+ opj_tccp_t *l_ref_tccp = NULL, *l_copied_tccp = NULL;
+ OPJ_UINT32 l_prc_size;
+
+ /* preconditions */
+ assert(p_j2k != 00);
+
+ l_cp = &(p_j2k->m_cp);
+ l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? /* FIXME J2K_DEC_STATE_TPH*/
+ &l_cp->tcps[p_j2k->m_current_tile_number] :
+ p_j2k->m_specific_param.m_decoder.m_default_tcp;
+
+ l_ref_tccp = &l_tcp->tccps[0];
+ l_copied_tccp = l_ref_tccp + 1;
+ l_prc_size = l_ref_tccp->numresolutions * sizeof(OPJ_UINT32);
+
+ for (i=1; i<p_j2k->m_image->numcomps; ++i) {
+ l_copied_tccp->numresolutions = l_ref_tccp->numresolutions;
+ l_copied_tccp->cblkw = l_ref_tccp->cblkw;
+ l_copied_tccp->cblkh = l_ref_tccp->cblkh;
+ l_copied_tccp->cblksty = l_ref_tccp->cblksty;
+ l_copied_tccp->qmfbid = l_ref_tccp->qmfbid;
+ memcpy(l_copied_tccp->prcw,l_ref_tccp->prcw,l_prc_size);
+ memcpy(l_copied_tccp->prch,l_ref_tccp->prch,l_prc_size);
+ ++l_copied_tccp;
+ }
+}
+
+/**
+ * Reads a SQcd or SQcc element, i.e. the quantization values of a band.
+ *
+ * @param p_comp_no the component being targeted.
+ * @param p_header_data the data contained in the COM box.
+ * @param p_j2k the jpeg2000 codec.
+ * @param p_header_size the size of the data contained in the COM marker.
+ * @param p_manager the user event manager.
+*/
+opj_bool j2k_read_SQcd_SQcc(
+ opj_j2k_v2_t *p_j2k,
+ OPJ_UINT32 p_comp_no,
+ OPJ_BYTE* p_header_data,
+ OPJ_UINT32 * p_header_size,
+ struct opj_event_mgr * p_manager
+ )
+{
+ // loop
+ OPJ_UINT32 l_band_no;
+ opj_cp_v2_t *l_cp = 00;
+ opj_tcp_v2_t *l_tcp = 00;
+ opj_tccp_t *l_tccp = 00;
+ OPJ_BYTE * l_current_ptr = 00;
+ OPJ_UINT32 l_tmp, l_num_band;
+
+ // preconditions
+ assert(p_j2k != 00);
+ assert(p_manager != 00);
+ assert(p_header_data != 00);
+
+ l_cp = &(p_j2k->m_cp);
+ // come from tile part header or main header ?
+ l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? /*FIXME J2K_DEC_STATE_TPH*/
+ &l_cp->tcps[p_j2k->m_current_tile_number] :
+ p_j2k->m_specific_param.m_decoder.m_default_tcp;
+
+ // precondition again
+ assert(p_comp_no < p_j2k->m_image->numcomps);
+
+ l_tccp = &l_tcp->tccps[p_comp_no];
+ l_current_ptr = p_header_data;
+
+ if (*p_header_size < 1) {
+ opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SQcd or SQcc element\n");
+ return OPJ_FALSE;
+ }
+ *p_header_size -= 1;
+
+ opj_read_bytes(l_current_ptr, &l_tmp ,1); /* Sqcx */
+ ++l_current_ptr;
+
+ l_tccp->qntsty = l_tmp & 0x1f;
+ l_tccp->numgbits = l_tmp >> 5;
+ if (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
+ l_num_band = 1;
+ }
+ else {
+ l_num_band = (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) ?
+ (*p_header_size) :
+ (*p_header_size) / 2;
+
+ if( l_num_band > J2K_MAXBANDS ) {
+ opj_event_msg_v2(p_manager, EVT_WARNING, "While reading CCP_QNTSTY element inside QCD or QCC marker segment, "
+ "number of subbands (%d) is greater to J2K_MAXBANDS (%d). So we limiting the number of elements stored to "
+ "J2K_MAXBANDS (%d) and skip the other. \n", l_num_band, J2K_MAXBANDS, J2K_MAXBANDS);
+ //return OPJ_FALSE;
+ }
+ }
+
+#ifdef USE_JPWL
+ if (l_cp->correct) {
+
+ /* if JPWL is on, we check whether there are too many subbands */
+ if ((l_num_band < 0) || (l_num_band >= J2K_MAXBANDS)) {
+ opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
+ "JPWL: bad number of subbands in Sqcx (%d)\n",
+ l_num_band);
+ if (!JPWL_ASSUME) {
+ opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
+ return OPJ_FALSE;
+ }
+ /* we try to correct */
+ l_num_band = 1;
+ opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n"
+ "- setting number of bands to %d => HYPOTHESIS!!!\n",
+ l_num_band);
+ };
+
+ };
+#endif /* USE_JPWL */
+
+ if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
+ for (l_band_no = 0; l_band_no < l_num_band; l_band_no++) {
+ opj_read_bytes(l_current_ptr, &l_tmp ,1); /* SPqcx_i */
+ ++l_current_ptr;
+ if (l_band_no < J2K_MAXBANDS){
+ l_tccp->stepsizes[l_band_no].expn = l_tmp>>3;
+ l_tccp->stepsizes[l_band_no].mant = 0;
+ }
+ }
+ *p_header_size = *p_header_size - l_num_band;
+ }
+ else {
+ for (l_band_no = 0; l_band_no < l_num_band; l_band_no++) {
+ opj_read_bytes(l_current_ptr, &l_tmp ,2); /* SPqcx_i */
+ l_current_ptr+=2;
+ if (l_band_no < J2K_MAXBANDS){
+ l_tccp->stepsizes[l_band_no].expn = l_tmp >> 11;
+ l_tccp->stepsizes[l_band_no].mant = l_tmp & 0x7ff;
+ }
+ }
+ *p_header_size = *p_header_size - 2*l_num_band;
+ }
+
+ /* Add Antonin : if scalar_derived -> compute other stepsizes */
+ if (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
+ for (l_band_no = 1; l_band_no < J2K_MAXBANDS; l_band_no++) {
+ l_tccp->stepsizes[l_band_no].expn =
+ ((l_tccp->stepsizes[0].expn) - ((l_band_no - 1) / 3) > 0) ?
+ (l_tccp->stepsizes[0].expn) - ((l_band_no - 1) / 3) : 0;
+ l_tccp->stepsizes[l_band_no].mant = l_tccp->stepsizes[0].mant;
+ }
+ }
+
+ return OPJ_TRUE;
+}
+
+/**
+ * Copies the tile component parameters of all the component from the first tile component.
+ *
+ * @param p_j2k the J2k codec.
+ */
+void j2k_copy_tile_quantization_parameters( opj_j2k_v2_t *p_j2k )
+{
+ OPJ_UINT32 i;
+ opj_cp_v2_t *l_cp = NULL;
+ opj_tcp_v2_t *l_tcp = NULL;
+ opj_tccp_t *l_ref_tccp = NULL;
+ opj_tccp_t *l_copied_tccp = NULL;
+ OPJ_UINT32 l_size;
+
+ /* preconditions */
+ assert(p_j2k != 00);
+
+ l_cp = &(p_j2k->m_cp);
+ l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
+ &l_cp->tcps[p_j2k->m_current_tile_number] :
+ p_j2k->m_specific_param.m_decoder.m_default_tcp;
+
+ l_ref_tccp = &l_tcp->tccps[0];
+ l_copied_tccp = l_ref_tccp + 1;
+ l_size = J2K_MAXBANDS * sizeof(opj_stepsize_t);
+
+ for (i=1;i<p_j2k->m_image->numcomps;++i) {
+ l_copied_tccp->qntsty = l_ref_tccp->qntsty;
+ l_copied_tccp->numgbits = l_ref_tccp->numgbits;
+ memcpy(l_copied_tccp->stepsizes,l_ref_tccp->stepsizes,l_size);
+ ++l_copied_tccp;
+ }
+}
+
+/**
+ * Dump some elements from the J2K decompression structure .
+ *
+ *@param p_j2k the jpeg2000 codec.
+ *@param flag flag to describe what elments are dump.
+ *@param out_stream output stream where dump the elements.
+ *
+*/
+void j2k_dump (opj_j2k_v2_t* p_j2k, OPJ_INT32 flag, FILE* out_stream)
+{
+ /* Check if the flag is compatible with j2k file*/
+ if ( (flag & OPJ_JP2_INFO) || (flag & OPJ_JP2_IND)){
+ fprintf(out_stream, "Wrong flag\n");
+ return;
+ }
+
+ /* Dump the image_header */
+ if (flag & OPJ_IMG_INFO){
+ if (p_j2k->m_image)
+ j2k_dump_image_header(p_j2k->m_image, 0, out_stream);
+ }
+
+ /* Dump the codestream info from main header */
+ if (flag & OPJ_J2K_MH_INFO){
+ j2k_dump_MH_info(p_j2k, out_stream);
+ }
+
+
+ /* Dump the codestream info of the current tile */
+ if (flag & OPJ_J2K_TH_INFO){
+
+ }
+
+ /* Dump the codestream index from main header */
+ if (flag & OPJ_J2K_MH_IND){
+ j2k_dump_MH_index(p_j2k, out_stream);
+ }
+
+ /* Dump the codestream index of the current tile */
+ if (flag & OPJ_J2K_TH_IND){
+
+ }
+
+}
+
+/**
+ * Dump index elements of the codestream extract from the main header.
+ *
+ *@param p_j2k the jpeg2000 codec.
+ *@param out_stream output stream where dump the elements.
+ *
+*/
+void j2k_dump_MH_index(opj_j2k_v2_t* p_j2k, FILE* out_stream)
+{
+ opj_codestream_index_t* cstr_index = p_j2k->cstr_index;
+ OPJ_UINT32 it_marker;
+
+ fprintf(out_stream, "Codestream index from main header: {\n");
+
+ fprintf(out_stream, "\t Main header start position=%d\n\t Main header end position=%d\n",
+ cstr_index->main_head_start, cstr_index->main_head_end);
+
+ fprintf(out_stream, "\t Marker list: {\n");
+
+ for (it_marker=0; it_marker < cstr_index->marknum ; it_marker++){
+ fprintf(out_stream, "\t\t type=%#x, pos=%d, len=%d\n",
+ cstr_index->marker[it_marker].type,
+ cstr_index->marker[it_marker].pos,
+ cstr_index->marker[it_marker].len );
+ }
+
+ fprintf(out_stream, "\t }\n}\n");
+
+}
+
+/**
+ * Dump info elements of the codestream extract from the main header.
+ *
+ *@param p_j2k the jpeg2000 codec.
+ *@param out_stream output stream where dump the elements.
+ *
+*/
+void j2k_dump_MH_info(opj_j2k_v2_t* p_j2k, FILE* out_stream)
+{
+ opj_tcp_v2_t * l_default_tile=NULL;
+
+ fprintf(out_stream, "Codestream info from main header: {\n");
+
+ fprintf(out_stream, "\t tx0=%d, ty0=%d\n", p_j2k->m_cp.tx0, p_j2k->m_cp.ty0);
+ fprintf(out_stream, "\t tdx=%d, tdy=%d\n", p_j2k->m_cp.tdx, p_j2k->m_cp.tdy);
+ fprintf(out_stream, "\t tw=%d, th=%d\n", p_j2k->m_cp.tw, p_j2k->m_cp.th);
+
+ l_default_tile = p_j2k->m_specific_param.m_decoder.m_default_tcp;
+ if (l_default_tile)
+ {
+ OPJ_INT32 compno;
+ OPJ_INT32 numcomps = p_j2k->m_image->numcomps;
+
+ fprintf(out_stream, "\t default tile {\n");
+ fprintf(out_stream, "\t\t csty=%#x\n", l_default_tile->csty);
+ fprintf(out_stream, "\t\t prg=%#x\n", l_default_tile->prg);
+ fprintf(out_stream, "\t\t numlayers=%d\n", l_default_tile->numlayers);
+ fprintf(out_stream, "\t\t mct=%x\n", l_default_tile->mct);
+
+ for (compno = 0; compno < numcomps; compno++) {
+ opj_tccp_t *l_tccp = &(l_default_tile->tccps[compno]);
+ OPJ_INT32 resno, bandno, numbands;
+
+ /* coding style*/
+ fprintf(out_stream, "\t\t comp %d {\n", compno);
+ fprintf(out_stream, "\t\t\t csty=%#x\n", l_tccp->csty);
+ fprintf(out_stream, "\t\t\t numresolutions=%d\n", l_tccp->numresolutions);
+ fprintf(out_stream, "\t\t\t cblkw=2^%d\n", l_tccp->cblkw);
+ fprintf(out_stream, "\t\t\t cblkh=2^%d\n", l_tccp->cblkh);
+ fprintf(out_stream, "\t\t\t cblksty=%#x\n", l_tccp->cblksty);
+ fprintf(out_stream, "\t\t\t qmfbid=%d\n", l_tccp->qmfbid);
+
+ fprintf(out_stream, "\t\t\t preccintsize (w,h)=");
+ for (resno = 0; resno < l_tccp->numresolutions; resno++) {
+ fprintf(out_stream, "(%d,%d) ", l_tccp->prcw[resno], l_tccp->prch[resno]);
+ }
+ fprintf(out_stream, "\n");
+
+ /* quantization style*/
+ fprintf(out_stream, "\t\t\t qntsty=%d\n", l_tccp->qntsty);
+ fprintf(out_stream, "\t\t\t numgbits=%d\n", l_tccp->numgbits);
+ fprintf(out_stream, "\t\t\t stepsizes (m,e)=");
+ numbands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : l_tccp->numresolutions * 3 - 2;
+ for (bandno = 0; bandno < numbands; bandno++) {
+ fprintf(out_stream, "(%d,%d) ", l_tccp->stepsizes[bandno].mant,
+ l_tccp->stepsizes[bandno].expn);
+ }
+ fprintf(out_stream, "\n");
+
+ /* RGN value*/
+ fprintf(out_stream, "\t\t\t roishift=%d\n", l_tccp->roishift);
+
+ fprintf(out_stream, "\t\t }\n");
+ } /*end of component of default tile*/
+ fprintf(out_stream, "\t }\n"); /*end of default tile*/
+
+ }
+
+ fprintf(out_stream, "}\n");
+
+}
+
+/**
+ * Dump an image header structure.
+ *
+ *@param img_header the image header to dump.
+ *@param dev_dump_flag flag to describe if we are in the case of this function is use outside j2k_dump function
+ *@param out_stream output stream where dump the elements.
+ */
+void j2k_dump_image_header(opj_image_t* img_header, opj_bool dev_dump_flag, FILE* out_stream)
+{
+ char tab[2];
+
+ if (dev_dump_flag){
+ fprintf(stdout, "[DEV] Dump a image_header struct {\n");
+ tab[0] = '\0';
+ }
+ else {
+ fprintf(out_stream, "Image info {\n");
+ tab[0] = '\t';tab[1] = '\0';
+ }
+
+ fprintf(out_stream, "%s x0=%d, y0=%d\n", tab, img_header->x0, img_header->y0);
+ fprintf(out_stream, "%s x1=%d, y1=%d\n", tab, img_header->x1, img_header->y1);
+ fprintf(out_stream, "%s numcomps=%d\n", tab, img_header->numcomps);
+
+ if (img_header->comps){
+ int compno;
+ for (compno = 0; compno < img_header->numcomps; compno++) {
+ fprintf(out_stream, "%s\t component %d {\n", tab, compno);
+ j2k_dump_image_comp_header(&(img_header->comps[compno]), dev_dump_flag, out_stream);
+ fprintf(out_stream,"%s}\n",tab);
+ }
+ }
+
+ fprintf(out_stream, "}\n");
+}
+
+/**
+ * Dump a component image header structure.
+ *
+ *@param comp_header the component image header to dump.
+ *@param dev_dump_flag flag to describe if we are in the case of this function is use outside j2k_dump function
+ *@param out_stream output stream where dump the elements.
+ */
+void j2k_dump_image_comp_header(opj_image_comp_t* comp_header, opj_bool dev_dump_flag, FILE* out_stream)
+{
+ char tab[3];
+
+ if (dev_dump_flag){
+ fprintf(stdout, "[DEV] Dump a image_comp_header struct {\n");
+ tab[0] = '\0';
+ } else {
+ tab[0] = '\t';tab[1] = '\t';tab[2] = '\0';
+ }
+
+ fprintf(out_stream, "%s dx=%d, dy=%d\n", tab, comp_header->dx, comp_header->dy);
+ fprintf(out_stream, "%s prec=%d\n", tab, comp_header->prec);
+ fprintf(out_stream, "%s sgnd=%d\n", tab, comp_header->sgnd);
+
+ if (dev_dump_flag)
+ fprintf(out_stream, "}\n");
+}
+
+
+/**
+ * Get the codestream info from a JPEG2000 codec.
+ *
+ *@param p_j2k the component image header to dump.
+ *
+ *@return the codestream information extract from the jpg2000 codec
+ */
+opj_codestream_info_v2_t* j2k_get_cstr_info(opj_j2k_v2_t* p_j2k)
+{
+ OPJ_UINT16 compno;
+ OPJ_UINT16 numcomps = p_j2k->m_image->numcomps;
+ opj_tcp_v2_t *l_default_tile;
+ opj_codestream_info_v2_t* cstr_info = (opj_codestream_info_v2_t*) opj_calloc(1,sizeof(opj_codestream_info_v2_t));
+
+ cstr_info->nbcomps = p_j2k->m_image->numcomps;
+
+ cstr_info->tx0 = p_j2k->m_cp.tx0;
+ cstr_info->ty0 = p_j2k->m_cp.ty0;
+ cstr_info->tdx = p_j2k->m_cp.tdx;
+ cstr_info->tdy = p_j2k->m_cp.tdy;
+ cstr_info->tw = p_j2k->m_cp.tw;
+ cstr_info->th = p_j2k->m_cp.th;
+
+ l_default_tile = p_j2k->m_specific_param.m_decoder.m_default_tcp;
+
+ cstr_info->m_default_tile_info.csty = l_default_tile->csty;
+ cstr_info->m_default_tile_info.prg = l_default_tile->prg;
+ cstr_info->m_default_tile_info.numlayers = l_default_tile->numlayers;
+ cstr_info->m_default_tile_info.mct = l_default_tile->mct;
+
+ cstr_info->m_default_tile_info.tccp_info = (opj_tccp_info_t*) opj_calloc(1,sizeof(opj_tccp_info_t));
+
+ for (compno = 0; compno < numcomps; compno++) {
+ opj_tccp_t *l_tccp = &(l_default_tile->tccps[compno]);
+ opj_tccp_info_t *l_tccp_info = &(cstr_info->m_default_tile_info.tccp_info[compno]);
+ OPJ_INT32 bandno, numbands;
+
+ /* coding style*/
+ l_tccp_info->csty = l_tccp->csty;
+ l_tccp_info->numresolutions = l_tccp->numresolutions;
+ l_tccp_info->cblkw = l_tccp->cblkw;
+ l_tccp_info->cblkh = l_tccp->cblkh;
+ l_tccp_info->cblksty = l_tccp->cblksty;
+ l_tccp_info->qmfbid = l_tccp->qmfbid;
+ if (l_tccp->numresolutions < J2K_MAXRLVLS)
+ {
+ memcpy(l_tccp_info->prch, l_tccp->prch, l_tccp->numresolutions);
+ memcpy(l_tccp_info->prcw, l_tccp->prcw, l_tccp->numresolutions);
+ }
+
+ /* quantization style*/
+ l_tccp_info->qntsty = l_tccp->qntsty;
+ l_tccp_info->numgbits = l_tccp->numgbits;
+
+ numbands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : l_tccp->numresolutions * 3 - 2;
+ if (numbands < J2K_MAXBANDS) {
+ for (bandno = 0; bandno < numbands; bandno++) {
+ l_tccp_info->stepsizes_mant[bandno] = l_tccp->stepsizes[bandno].mant;
+ l_tccp_info->stepsizes_expn[bandno] = l_tccp->stepsizes[bandno].expn;
+ }
+ }
+
+ /* RGN value*/
+ l_tccp_info->roishift = l_tccp->roishift;
+ }
+
+
+ return cstr_info;
+}
+
+/**
+ * Get the codestream index from a JPEG2000 codec.
+ *
+ *@param p_j2k the component image header to dump.
+ *
+ *@return the codestream index extract from the jpg2000 codec
+ */
+opj_codestream_index_t* j2k_get_cstr_index(opj_j2k_v2_t* p_j2k)
+{
+ opj_codestream_index_t* l_cstr_index = (opj_codestream_index_t*)
+ opj_calloc(1,sizeof(opj_codestream_index_t));
+ if (!l_cstr_index)
+ return NULL;
+
+ l_cstr_index->main_head_start = p_j2k->cstr_index->main_head_start;
+ l_cstr_index->main_head_end = p_j2k->cstr_index->main_head_end;
+ l_cstr_index->codestream_size = p_j2k->cstr_index->codestream_size;
+
+ l_cstr_index->maxmarknum = p_j2k->cstr_index->maxmarknum;
+ l_cstr_index->marknum = p_j2k->cstr_index->marknum;
+ l_cstr_index->marker = (opj_marker_info_t*)opj_malloc(l_cstr_index->marknum*sizeof(opj_marker_info_t));
+ if (!l_cstr_index->marker)
+ return NULL;
+
+ memcpy(l_cstr_index->marker, p_j2k->cstr_index->marker, l_cstr_index->marknum * sizeof(opj_marker_info_t) );
+
+
+ return l_cstr_index;
+}
+
+
+
+/**
+ * Reads the tiles.
+ */
+opj_bool j2k_decode_tiles ( opj_j2k_v2_t *p_j2k,
+ opj_stream_private_t *p_stream,
+ opj_event_mgr_t * p_manager)
+{
+ opj_bool l_go_on = OPJ_TRUE;
+ OPJ_UINT32 l_current_tile_no;
+ OPJ_UINT32 l_data_size,l_max_data_size;
+ OPJ_INT32 l_tile_x0,l_tile_y0,l_tile_x1,l_tile_y1;
+ OPJ_UINT32 l_nb_comps;
+ OPJ_BYTE * l_current_data;
+
+ l_current_data = (OPJ_BYTE*)opj_malloc(1000);
+ if (! l_current_data) {
+ return OPJ_FALSE;
+ }
+ l_max_data_size = 1000;
+
+ while (OPJ_TRUE) {
+ if (! j2k_read_tile_header( p_j2k,
+ &l_current_tile_no,
+ &l_data_size,
+ &l_tile_x0, &l_tile_y0,
+ &l_tile_x1, &l_tile_y1,
+ &l_nb_comps,
+ &l_go_on,
+ p_stream,
+ p_manager)) {
+ return OPJ_FALSE;
+ }
+
+ if (! l_go_on) {
+ break;
+ }
+
+ if (l_data_size > l_max_data_size) {
+ l_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,l_data_size);
+ if (! l_current_data) {
+ return OPJ_FALSE;
+ }
+
+ l_max_data_size = l_data_size;
+ }
+
+ if (! j2k_decode_tile(p_j2k,l_current_tile_no,l_current_data,l_data_size,p_stream,p_manager)) {
+ opj_free(l_current_data);
+ return OPJ_FALSE;
+ }
+ opj_event_msg_v2(p_manager, EVT_INFO, "Tile %d/%d has been decode.\n", l_current_tile_no +1, p_j2k->m_cp.th * p_j2k->m_cp.tw);
+
+ if (! j2k_update_image_data(p_j2k->m_tcd,l_current_data)) {
+ opj_free(l_current_data);
+ return OPJ_FALSE;
+ }
+ opj_event_msg_v2(p_manager, EVT_INFO, "Image data has been updated with tile %d.\n\n", l_current_tile_no + 1);
+
+ }
+
+ opj_free(l_current_data);
+ return OPJ_TRUE;
+}
+
+
+/**
+ * Sets up the procedures to do on decoding data. Developpers wanting to extend the library can add their own reading procedures.
+ */
+void j2k_setup_decoding (opj_j2k_v2_t *p_j2k)
+{
+ // preconditions
+ assert(p_j2k != 00);
+
+ opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(void*)j2k_decode_tiles);
+ /* DEVELOPER CORNER, add your custom procedures */
+
+}
+
+
+/**
+ * Decodes the tiles of the stream.
+ */
+opj_bool j2k_decode_v2( opj_j2k_v2_t * p_j2k,
+ opj_stream_private_t * p_stream,
+ opj_image_t * p_image,
+ opj_event_mgr_t * p_manager)
+{
+ OPJ_UINT32 compno;
+
+ /* customization of the decoding */
+ j2k_setup_decoding(p_j2k);
+
+ /* write header */
+ if (! j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
+ opj_image_destroy(p_j2k->m_image);
+ p_j2k->m_image = NULL;
+ return OPJ_FALSE;
+ }
+
+ for (compno = 0; compno < p_image->numcomps; compno++) {
+ p_image->comps[compno].data = p_j2k->m_image->comps[compno].data;
+ p_j2k->m_image->comps[compno].data = NULL;
+ }
+
+ return OPJ_TRUE /*p_j2k->m_image*/;
+}