- /* 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;
-}
-
-/**
- * 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_image_t* p_image,
- 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);
- opj_image_t * l_image = p_j2k->m_private_image;
-
- OPJ_UINT32 it_comp;
- OPJ_INT32 l_comp_x1, l_comp_y1;
- opj_image_comp_t* l_img_comp = NULL;
-
- /* 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;
- }
-
- if ( !p_start_x && !p_start_y && !p_end_x && !p_end_y){
- opj_event_msg_v2(p_manager, EVT_INFO, "No decoded area parameters, set the decoded area to the whole image\n");
-
- p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
- p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
- p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
- p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
-
- return OPJ_TRUE;
- }
-
- /* ----- */
- /* Check if the positions provided by the user are correct */
-
- /* Left */
- assert(p_start_x >= 0 );
- assert(p_start_y >= 0 );
-
- if ((OPJ_UINT32)p_start_x > l_image->x1 ) {
- opj_event_msg_v2(p_manager, EVT_ERROR,
- "Left position of the decoded area (region_x0=%d) is outside the image area (Xsiz=%d).\n",
- p_start_x, l_image->x1);
- return OPJ_FALSE;
- }
- else if ((OPJ_UINT32)p_start_x < l_image->x0){
- opj_event_msg_v2(p_manager, EVT_WARNING,
- "Left position of the decoded area (region_x0=%d) is outside the image area (XOsiz=%d).\n",
- p_start_x, l_image->x0);
- p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
- p_image->x0 = l_image->x0;
- }
- else {
- p_j2k->m_specific_param.m_decoder.m_start_tile_x = (p_start_x - l_cp->tx0) / l_cp->tdx;
- p_image->x0 = p_start_x;
- }
-
- /* Up */
- if ((OPJ_UINT32)p_start_y > l_image->y1){
- opj_event_msg_v2(p_manager, EVT_ERROR,
- "Up position of the decoded area (region_y0=%d) is outside the image area (Ysiz=%d).\n",
- p_start_y, l_image->y1);
- return OPJ_FALSE;
- }
- else if ((OPJ_UINT32)p_start_y < l_image->y0){
- opj_event_msg_v2(p_manager, EVT_WARNING,
- "Up position of the decoded area (region_y0=%d) is outside the image area (YOsiz=%d).\n",
- p_start_y, l_image->y0);
- p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
- p_image->y0 = l_image->y0;
- }
- else {
- p_j2k->m_specific_param.m_decoder.m_start_tile_y = (p_start_y - l_cp->ty0) / l_cp->tdy;
- p_image->y0 = p_start_y;
- }
-
- /* Right */
- assert((OPJ_UINT32)p_end_x > 0);
- assert((OPJ_UINT32)p_end_y > 0);
- if ((OPJ_UINT32)p_end_x < l_image->x0) {
- opj_event_msg_v2(p_manager, EVT_ERROR,
- "Right position of the decoded area (region_x1=%d) is outside the image area (XOsiz=%d).\n",
- p_end_x, l_image->x0);
- return OPJ_FALSE;
- }
- else if ((OPJ_UINT32)p_end_x > l_image->x1) {
- opj_event_msg_v2(p_manager, EVT_WARNING,
- "Right position of the decoded area (region_x1=%d) is outside the image area (Xsiz=%d).\n",
- p_end_x, l_image->x1);
- p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
- p_image->x1 = l_image->x1;
- }
- else {
- p_j2k->m_specific_param.m_decoder.m_end_tile_x = int_ceildiv((p_end_x - l_cp->tx0), l_cp->tdx);
- p_image->x1 = p_end_x;
- }
-
- /* Bottom */
- if ((OPJ_UINT32)p_end_y < l_image->y0) {
- opj_event_msg_v2(p_manager, EVT_ERROR,
- "Bottom position of the decoded area (region_y1=%d) is outside the image area (YOsiz=%d).\n",
- p_end_y, l_image->y0);
- return OPJ_FALSE;
- }
- if ((OPJ_UINT32)p_end_y > l_image->y1){
- opj_event_msg_v2(p_manager, EVT_WARNING,
- "Bottom position of the decoded area (region_y1=%d) is outside the image area (Ysiz=%d).\n",
- p_end_y, l_image->y1);
- p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
- p_image->y1 = l_image->y1;
- }
- else{
- p_j2k->m_specific_param.m_decoder.m_end_tile_y = int_ceildiv((p_end_y - l_cp->ty0), l_cp->tdy);
- p_image->y1 = p_end_y;
- }
- /* ----- */
-
- p_j2k->m_specific_param.m_decoder.m_discard_tiles = 1;
-
- l_img_comp = p_image->comps;
- for (it_comp=0; it_comp < p_image->numcomps; ++it_comp)
- {
- OPJ_INT32 l_h,l_w;
-
- l_img_comp->x0 = int_ceildiv(p_image->x0, l_img_comp->dx);
- l_img_comp->y0 = int_ceildiv(p_image->y0, l_img_comp->dy);
- l_comp_x1 = int_ceildiv(p_image->x1, l_img_comp->dx);
- l_comp_y1 = int_ceildiv(p_image->y1, l_img_comp->dy);
-
- l_w = int_ceildivpow2(l_comp_x1, l_img_comp->factor)
- - int_ceildivpow2(l_img_comp->x0, l_img_comp->factor);
- if (l_w < 0){
- opj_event_msg_v2(p_manager, EVT_ERROR,
- "Size x of the decoded component image is incorrect (comp[%d].w=%d).\n",
- it_comp, l_w);
- return OPJ_FALSE;
- }
- l_img_comp->w = l_w;
-
- l_h = int_ceildivpow2(l_comp_y1, l_img_comp->factor)
- - int_ceildivpow2(l_img_comp->y0, l_img_comp->factor);
- if (l_h < 0){
- opj_event_msg_v2(p_manager, EVT_ERROR,
- "Size y of the decoded component image is incorrect (comp[%d].h=%d).\n",
- it_comp, l_h);
- return OPJ_FALSE;
- }
- l_img_comp->h = l_h;
-
- l_img_comp++;
- }
-
- opj_event_msg_v2( p_manager, EVT_INFO,"Setting decoding area to %d,%d,%d,%d\n",
- p_image->x0, p_image->y0, p_image->x1, p_image->y1);
-
-
- 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(void)
-{
- 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;
-
- l_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec = -1 ;
-
- l_j2k->m_specific_param.m_decoder.m_last_sot_read_pos = 0 ;
+ /* 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) {
+ OPJ_BYTE *new_header_data = (OPJ_BYTE *) opj_realloc(p_j2k->m_specific_param.m_decoder.m_header_data, l_marker_size);
+ if (! new_header_data) {
+ opj_free(p_j2k->m_specific_param.m_decoder.m_header_data);
+ p_j2k->m_specific_param.m_decoder.m_header_data = NULL;
+ p_j2k->m_specific_param.m_decoder.m_header_data_size = 0;
+ opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to read header\n");
+ return OPJ_FALSE;
+ }
+ p_j2k->m_specific_param.m_decoder.m_header_data = new_header_data;
+ 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;
+ }
+
+ if (!l_marker_handler->handler) {
+ /* See issue #175 */
+ opj_event_msg_v2(p_manager, EVT_ERROR, "Not sure how that happened.\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*/
+ if (OPJ_FALSE == opj_j2k_add_tlmarker(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 )) {
+ opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to add tl marker\n");
+ return OPJ_FALSE;
+ }
+
+ /* 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 (! opj_j2k_read_sod(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 (! opj_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 = opj_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 opj_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) {
+ opj_j2k_tcp_destroy(l_tcp);
+ return OPJ_FALSE;
+ }
+
+ if (! opj_tcd_decode_tile( p_j2k->m_tcd,
+ l_tcp->m_data,
+ l_tcp->m_data_size,
+ p_tile_index,
+ p_j2k->cstr_index) ) {
+ opj_j2k_tcp_destroy(l_tcp);
+ p_j2k->m_specific_param.m_decoder.m_state |= 0x8000;/*FIXME J2K_DEC_STATE_ERR;*/
+ return OPJ_FALSE;
+ }
+
+ if (! opj_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*/
+ /*opj_j2k_tcp_destroy(l_tcp);
+ p_j2k->m_tcd->tcp = 0;*/
+ opj_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 opj_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;
+}
+
+opj_bool opj_j2k_set_decode_area( opj_j2k_v2_t *p_j2k,
+ opj_image_t* p_image,
+ OPJ_INT32 p_start_x, OPJ_INT32 p_start_y,
+ OPJ_INT32 p_end_x, OPJ_INT32 p_end_y,
+ opj_event_mgr_t * p_manager )
+{
+ opj_cp_v2_t * l_cp = &(p_j2k->m_cp);
+ opj_image_t * l_image = p_j2k->m_private_image;
+
+ OPJ_UINT32 it_comp;
+ OPJ_INT32 l_comp_x1, l_comp_y1;
+ opj_image_comp_t* l_img_comp = NULL;
+
+ /* 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;
+ }
+
+ if ( !p_start_x && !p_start_y && !p_end_x && !p_end_y){
+ opj_event_msg_v2(p_manager, EVT_INFO, "No decoded area parameters, set the decoded area to the whole image\n");
+
+ p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
+ p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
+ p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
+ p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
+
+ return OPJ_TRUE;
+ }
+
+ /* ----- */
+ /* Check if the positions provided by the user are correct */
+
+ /* Left */
+ assert(p_start_x >= 0 );
+ assert(p_start_y >= 0 );
+
+ if ((OPJ_UINT32)p_start_x > l_image->x1 ) {
+ opj_event_msg_v2(p_manager, EVT_ERROR,
+ "Left position of the decoded area (region_x0=%d) is outside the image area (Xsiz=%d).\n",
+ p_start_x, l_image->x1);
+ return OPJ_FALSE;
+ }
+ else if ((OPJ_UINT32)p_start_x < l_image->x0){
+ opj_event_msg_v2(p_manager, EVT_WARNING,
+ "Left position of the decoded area (region_x0=%d) is outside the image area (XOsiz=%d).\n",
+ p_start_x, l_image->x0);
+ p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
+ p_image->x0 = l_image->x0;
+ }
+ else {
+ p_j2k->m_specific_param.m_decoder.m_start_tile_x = (p_start_x - l_cp->tx0) / l_cp->tdx;
+ p_image->x0 = p_start_x;
+ }
+
+ /* Up */
+ if ((OPJ_UINT32)p_start_y > l_image->y1){
+ opj_event_msg_v2(p_manager, EVT_ERROR,
+ "Up position of the decoded area (region_y0=%d) is outside the image area (Ysiz=%d).\n",
+ p_start_y, l_image->y1);
+ return OPJ_FALSE;
+ }
+ else if ((OPJ_UINT32)p_start_y < l_image->y0){
+ opj_event_msg_v2(p_manager, EVT_WARNING,
+ "Up position of the decoded area (region_y0=%d) is outside the image area (YOsiz=%d).\n",
+ p_start_y, l_image->y0);
+ p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
+ p_image->y0 = l_image->y0;
+ }
+ else {
+ p_j2k->m_specific_param.m_decoder.m_start_tile_y = (p_start_y - l_cp->ty0) / l_cp->tdy;
+ p_image->y0 = p_start_y;
+ }
+
+ /* Right */
+ assert((OPJ_UINT32)p_end_x > 0);
+ assert((OPJ_UINT32)p_end_y > 0);
+ if ((OPJ_UINT32)p_end_x < l_image->x0) {
+ opj_event_msg_v2(p_manager, EVT_ERROR,
+ "Right position of the decoded area (region_x1=%d) is outside the image area (XOsiz=%d).\n",
+ p_end_x, l_image->x0);
+ return OPJ_FALSE;
+ }
+ else if ((OPJ_UINT32)p_end_x > l_image->x1) {
+ opj_event_msg_v2(p_manager, EVT_WARNING,
+ "Right position of the decoded area (region_x1=%d) is outside the image area (Xsiz=%d).\n",
+ p_end_x, l_image->x1);
+ p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
+ p_image->x1 = l_image->x1;
+ }
+ else {
+ p_j2k->m_specific_param.m_decoder.m_end_tile_x = int_ceildiv((p_end_x - l_cp->tx0), l_cp->tdx);
+ p_image->x1 = p_end_x;
+ }
+
+ /* Bottom */
+ if ((OPJ_UINT32)p_end_y < l_image->y0) {
+ opj_event_msg_v2(p_manager, EVT_ERROR,
+ "Bottom position of the decoded area (region_y1=%d) is outside the image area (YOsiz=%d).\n",
+ p_end_y, l_image->y0);
+ return OPJ_FALSE;
+ }
+ if ((OPJ_UINT32)p_end_y > l_image->y1){
+ opj_event_msg_v2(p_manager, EVT_WARNING,
+ "Bottom position of the decoded area (region_y1=%d) is outside the image area (Ysiz=%d).\n",
+ p_end_y, l_image->y1);
+ p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
+ p_image->y1 = l_image->y1;
+ }
+ else{
+ p_j2k->m_specific_param.m_decoder.m_end_tile_y = int_ceildiv((p_end_y - l_cp->ty0), l_cp->tdy);
+ p_image->y1 = p_end_y;
+ }
+ /* ----- */
+
+ p_j2k->m_specific_param.m_decoder.m_discard_tiles = 1;
+
+ l_img_comp = p_image->comps;
+ for (it_comp=0; it_comp < p_image->numcomps; ++it_comp)
+ {
+ OPJ_INT32 l_h,l_w;
+
+ l_img_comp->x0 = int_ceildiv(p_image->x0, l_img_comp->dx);
+ l_img_comp->y0 = int_ceildiv(p_image->y0, l_img_comp->dy);
+ l_comp_x1 = int_ceildiv(p_image->x1, l_img_comp->dx);
+ l_comp_y1 = int_ceildiv(p_image->y1, l_img_comp->dy);
+
+ l_w = int_ceildivpow2(l_comp_x1, l_img_comp->factor)
+ - int_ceildivpow2(l_img_comp->x0, l_img_comp->factor);
+ if (l_w < 0){
+ opj_event_msg_v2(p_manager, EVT_ERROR,
+ "Size x of the decoded component image is incorrect (comp[%d].w=%d).\n",
+ it_comp, l_w);
+ return OPJ_FALSE;
+ }
+ l_img_comp->w = l_w;
+
+ l_h = int_ceildivpow2(l_comp_y1, l_img_comp->factor)
+ - int_ceildivpow2(l_img_comp->y0, l_img_comp->factor);
+ if (l_h < 0){
+ opj_event_msg_v2(p_manager, EVT_ERROR,
+ "Size y of the decoded component image is incorrect (comp[%d].h=%d).\n",
+ it_comp, l_h);
+ return OPJ_FALSE;
+ }
+ l_img_comp->h = l_h;
+
+ l_img_comp++;
+ }
+
+ opj_event_msg_v2( p_manager, EVT_INFO,"Setting decoding area to %d,%d,%d,%d\n",
+ p_image->x0, p_image->y0, p_image->x1, p_image->y1);
+
+ return OPJ_TRUE;
+}
+
+opj_j2k_v2_t* opj_j2k_create_decompress(void)
+{
+ 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) {
+ opj_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) {
+ opj_j2k_destroy(l_j2k);
+ return 00;
+ }
+
+ l_j2k->m_specific_param.m_decoder.m_header_data_size = J2K_DEFAULT_HEADER_SIZE;
+
+ l_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec = -1 ;
+
+ l_j2k->m_specific_param.m_decoder.m_last_sot_read_pos = 0 ;
+
+ /* codestream index creation */
+ l_j2k->cstr_index = opj_j2k_create_cstr_index();
+
+ /*(opj_codestream_index_t*) opj_malloc(sizeof(opj_codestream_index_t));
+ if (!l_j2k->cstr_index){
+ opj_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) {
+ opj_j2k_destroy(l_j2k);
+ return 00;
+ }
+
+ /* execution list creation */
+ l_j2k->m_procedure_list = opj_procedure_list_create();
+ if (! l_j2k->m_procedure_list) {
+ opj_j2k_destroy(l_j2k);
+ return 00;
+ }