- /* 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;
- }
-
- /* Add the marker to the codestream index*/
- j2k_add_tlmarker_v2(p_j2k->m_current_tile_number,
- p_j2k->cstr_index,
- l_marker_handler->id,
- (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4,
- l_marker_size + 4 );
-
- /* Keep the position of the last SOT marker read */
- if ( l_marker_handler->id == J2K_MS_SOT ) {
- OPJ_UINT32 sot_pos = (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4 ;
- if (sot_pos > p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos)
- {
- p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos = sot_pos;
- }
- }
-
-
- 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, (p_j2k->m_cp.th * p_j2k->m_cp.tw) - 1);
-
- *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(l_tcp);
- 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;
- }
-
- /* To avoid to destroy the tcp which can be useful when we try to decode a tile decoded before (cf j2k_random_tile_access)
- * we destroy just the data which will be re-read in read_tile_header*/
- /*j2k_tcp_destroy(l_tcp);
- p_j2k->m_tcd->tcp = 0;*/
- j2k_tcp_data_destroy(l_tcp);
-
- 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_image_t* p_output_image)
-{
- OPJ_UINT32 i,j,k = 0;
- OPJ_UINT32 l_width_src,l_height_src;
- OPJ_UINT32 l_width_dest,l_height_dest;
- OPJ_INT32 l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src;
- OPJ_INT32 l_start_offset_src, l_line_offset_src, l_end_offset_src ;
- OPJ_UINT32 l_start_x_dest , l_start_y_dest;
- OPJ_UINT32 l_x0_dest, l_y0_dest, l_x1_dest, l_y1_dest;
- OPJ_INT32 l_start_offset_dest, l_line_offset_dest;
-
- opj_image_comp_t * l_img_comp_src = 00;
- opj_image_comp_t * l_img_comp_dest = 00;
-
- opj_tcd_tilecomp_v2_t * l_tilec = 00;
- opj_image_t * l_image_src = 00;
- OPJ_UINT32 l_size_comp, l_remaining;
- OPJ_INT32 * l_dest_ptr;
- opj_tcd_resolution_v2_t* l_res= 00;
-
- l_tilec = p_tcd->tcd_image->tiles->comps;
- l_image_src = p_tcd->image;
- l_img_comp_src = l_image_src->comps;
-
- l_img_comp_dest = p_output_image->comps;
-
- for (i=0; i<l_image_src->numcomps; i++) {
-
- /* Allocate output component buffer if necessary */
- if (!l_img_comp_dest->data) {
-
- l_img_comp_dest->data = (OPJ_INT32*) opj_calloc(l_img_comp_dest->w * l_img_comp_dest->h, sizeof(OPJ_INT32));
- if (! l_img_comp_dest->data) {
- return OPJ_FALSE;
- }
- }
-
- /* Copy info from decoded comp image to output image */
- l_img_comp_dest->resno_decoded = l_img_comp_src->resno_decoded;
-
- /*-----*/
- /* Compute the precision of the output buffer */
- l_size_comp = l_img_comp_src->prec >> 3; /*(/ 8)*/
- l_remaining = l_img_comp_src->prec & 7; /* (%8) */
- l_res = l_tilec->resolutions + l_img_comp_src->resno_decoded;
-
- if (l_remaining) {
- ++l_size_comp;
- }
-
- if (l_size_comp == 3) {
- l_size_comp = 4;
- }
- /*-----*/
-
- /* Current tile component size*/
- /*if (i == 0) {
- fprintf(stdout, "SRC: l_res_x0=%d, l_res_x1=%d, l_res_y0=%d, l_res_y1=%d\n",
- l_res->x0, l_res->x1, l_res->y0, l_res->y1);
- }*/
-
- l_width_src = (l_res->x1 - l_res->x0);
- l_height_src = (l_res->y1 - l_res->y0);
-
- /* Border of the current output component*/
- l_x0_dest = int_ceildivpow2(l_img_comp_dest->x0, l_img_comp_dest->factor);
- l_y0_dest = int_ceildivpow2(l_img_comp_dest->y0, l_img_comp_dest->factor);
- l_x1_dest = l_x0_dest + l_img_comp_dest->w;
- l_y1_dest = l_y0_dest + l_img_comp_dest->h;
-
- /*if (i == 0) {
- fprintf(stdout, "DEST: l_x0_dest=%d, l_x1_dest=%d, l_y0_dest=%d, l_y1_dest=%d (%d)\n",
- l_x0_dest, l_x1_dest, l_y0_dest, l_y1_dest, l_img_comp_dest->factor );
- }*/
-
- /*-----*/
- /* Compute the area (l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src)
- * of the input buffer (decoded tile component) which will be move
- * in the output buffer. Compute the area of the output buffer (l_start_x_dest,
- * l_start_y_dest, l_width_dest, l_height_dest) which will be modified
- * by this input area.
- * */
- assert( l_res->x0 >= 0);
- assert( l_res->x1 >= 0);
- if ( l_x0_dest < (OPJ_UINT32)l_res->x0 ) {
- l_start_x_dest = l_res->x0 - l_x0_dest;
- l_offset_x0_src = 0;
-
- if ( l_x1_dest >= (OPJ_UINT32)l_res->x1 ) {
- l_width_dest = l_width_src;
- l_offset_x1_src = 0;
- }
- else {
- l_width_dest = l_x1_dest - l_res->x0 ;
- l_offset_x1_src = l_width_src - l_width_dest;
- }
- }
- else {
- l_start_x_dest = 0 ;
- l_offset_x0_src = l_x0_dest - l_res->x0;
-
- if ( l_x1_dest >= (OPJ_UINT32)l_res->x1 ) {
- l_width_dest = l_width_src - l_offset_x0_src;
- l_offset_x1_src = 0;
- }
- else {
- l_width_dest = l_img_comp_dest->w ;
- l_offset_x1_src = l_res->x1 - l_x1_dest;
- }
- }
-
- if ( l_y0_dest < (OPJ_UINT32)l_res->y0 ) {
- l_start_y_dest = l_res->y0 - l_y0_dest;
- l_offset_y0_src = 0;
-
- if ( l_y1_dest >= (OPJ_UINT32)l_res->y1 ) {
- l_height_dest = l_height_src;
- l_offset_y1_src = 0;
- }
- else {
- l_height_dest = l_y1_dest - l_res->y0 ;
- l_offset_y1_src = l_height_src - l_height_dest;
- }
- }
- else {
- l_start_y_dest = 0 ;
- l_offset_y0_src = l_y0_dest - l_res->y0;
-
- if ( l_y1_dest >= (OPJ_UINT32)l_res->y1 ) {
- l_height_dest = l_height_src - l_offset_y0_src;
- l_offset_y1_src = 0;
- }
- else {
- l_height_dest = l_img_comp_dest->h ;
- l_offset_y1_src = l_res->y1 - l_y1_dest;
- }
- }
-
- if( (l_offset_x0_src < 0 ) || (l_offset_y0_src < 0 ) || (l_offset_x1_src < 0 ) || (l_offset_y1_src < 0 ) ){
- return OPJ_FALSE;
- }
- /*-----*/
-
- /* Compute the input buffer offset */
- l_start_offset_src = l_offset_x0_src + l_offset_y0_src * l_width_src;
- l_line_offset_src = l_offset_x1_src + l_offset_x0_src;
- l_end_offset_src = l_offset_y1_src * l_width_src - l_offset_x0_src;
-
- /* Compute the output buffer offset */
- l_start_offset_dest = l_start_x_dest + l_start_y_dest * l_img_comp_dest->w;
- l_line_offset_dest = l_img_comp_dest->w - l_width_dest;
-
- /* Move the output buffer to the first place where we will write*/
- l_dest_ptr = l_img_comp_dest->data + l_start_offset_dest;
-
- /*if (i == 0) {
- fprintf(stdout, "COMPO[%d]:\n",i);
- fprintf(stdout, "SRC: l_start_x_src=%d, l_start_y_src=%d, l_width_src=%d, l_height_src=%d\n"
- "\t tile offset:%d, %d, %d, %d\n"
- "\t buffer offset: %d; %d, %d\n",
- l_res->x0, l_res->y0, l_width_src, l_height_src,
- l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src,
- l_start_offset_src, l_line_offset_src, l_end_offset_src);
-
- fprintf(stdout, "DEST: l_start_x_dest=%d, l_start_y_dest=%d, l_width_dest=%d, l_height_dest=%d\n"
- "\t start offset: %d, line offset= %d\n",
- l_start_x_dest, l_start_y_dest, l_width_dest, l_height_dest, l_start_offset_dest, l_line_offset_dest);
- }*/
-
-
- switch (l_size_comp) {
- case 1:
- {
- OPJ_CHAR * l_src_ptr = (OPJ_CHAR*) p_data;
- l_src_ptr += l_start_offset_src; /* Move to the first place where we will read*/
-
- if (l_img_comp_src->sgnd) {
- for (j = 0 ; j < l_height_dest ; ++j) {
- for ( k = 0 ; k < l_width_dest ; ++k) {
- *(l_dest_ptr++) = (OPJ_INT32) (*(l_src_ptr++)); /* Copy only the data needed for the output image */
- }
-
- l_dest_ptr+= l_line_offset_dest; /* Move to the next place where we will write */
- l_src_ptr += l_line_offset_src ; /* Move to the next place where we will read */
- }
- }
- else {
- for ( j = 0 ; j < l_height_dest ; ++j ) {
- for ( k = 0 ; k < l_width_dest ; ++k) {
- *(l_dest_ptr++) = (OPJ_INT32) ((*(l_src_ptr++))&0xff);
- }
-
- l_dest_ptr+= l_line_offset_dest;
- l_src_ptr += l_line_offset_src;
- }
- }
-
- l_src_ptr += l_end_offset_src; /* Move to the end of this component-part of the input buffer */
- p_data = (OPJ_BYTE*) l_src_ptr; /* Keep the current position for the next component-part */
- }
- break;
- case 2:
- {
- OPJ_INT16 * l_src_ptr = (OPJ_INT16 *) p_data;
- l_src_ptr += l_start_offset_src;
-
- if (l_img_comp_src->sgnd) {
- for (j=0;j<l_height_dest;++j) {
- for (k=0;k<l_width_dest;++k) {
- *(l_dest_ptr++) = *(l_src_ptr++);
- }
-
- l_dest_ptr+= l_line_offset_dest;
- l_src_ptr += l_line_offset_src ;
- }
- }
- else {
- for (j=0;j<l_height_dest;++j) {
- for (k=0;k<l_width_dest;++k) {
- *(l_dest_ptr++) = (*(l_src_ptr++))&0xffff;
- }
-
- l_dest_ptr+= l_line_offset_dest;
- l_src_ptr += l_line_offset_src ;
- }
- }
-
- l_src_ptr += l_end_offset_src;
- p_data = (OPJ_BYTE*) l_src_ptr;
- }
- break;
- case 4:
- {
- OPJ_INT32 * l_src_ptr = (OPJ_INT32 *) p_data;
- l_src_ptr += l_start_offset_src;
-
- for (j=0;j<l_height_dest;++j) {
- for (k=0;k<l_width_dest;++k) {
- *(l_dest_ptr++) = (*(l_src_ptr++));
- }
-
- l_dest_ptr+= l_line_offset_dest;
- l_src_ptr += l_line_offset_src ;
- }
-
- l_src_ptr += l_end_offset_src;
- p_data = (OPJ_BYTE*) l_src_ptr;
- }
- break;
- }
-
- ++l_img_comp_dest;
- ++l_img_comp_src;
- ++l_tilec;
- }
-
- return OPJ_TRUE;
-}