[trunk] merge r1322 from branch 1.5 over to trunk
[openjpeg.git] / libopenjpeg / j2k.c
index 111e8c71d0204595932188f156e0a0c3faeea75e..410fe24092cd996084604e92bbccb47ef961c6b6 100644 (file)
@@ -125,6 +125,12 @@ static const struct opj_dec_memory_marker_handler * j2k_get_marker_handler (OPJ_
  */
 static void j2k_tcp_destroy (opj_tcp_v2_t *p_tcp);
 
+/**
+ * Destroys the data inside a tile coding parameter structure.
+ *
+ * @param      p_tcp           the tile coding parameter which contain data to destroy.
+ */
+static void j2k_tcp_data_destroy (opj_tcp_v2_t *p_tcp);
 
 /**
  * Destroys a coding parameter structure.
@@ -669,7 +675,7 @@ Add main header marker information
  */
 static void j2k_add_mhmarker(opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len);
 
-static void j2k_add_mhmarker_v2(opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_UINT32 pos, OPJ_UINT32 len) ;
+static void j2k_add_mhmarker_v2(opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len) ;
 /**
 Add tile header marker information
 @param tileno tile index number
@@ -680,7 +686,7 @@ Add tile header marker information
  */
 static void j2k_add_tlmarker( int tileno, opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len);
 
-static void j2k_add_tlmarker_v2(OPJ_UINT32 tileno, opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_UINT32 pos, OPJ_UINT32 len);
+static void j2k_add_tlmarker_v2(OPJ_UINT32 tileno, opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len);
 
 /**
  * Reads an unknown marker
@@ -1239,7 +1245,7 @@ static opj_bool j2k_read_soc_v2(  opj_j2k_v2_t *p_j2k,
        p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MHSIZ;
 
        /* FIXME move it in a index structure included in p_j2k*/
-       p_j2k->cstr_index->main_head_start = (OPJ_UINT32) opj_stream_tell(p_stream) - 2;
+       p_j2k->cstr_index->main_head_start = opj_stream_tell(p_stream) - 2;
 
        opj_event_msg_v2(p_manager, EVT_INFO, "Start to read j2k main header (%d).\n", p_j2k->cstr_index->main_head_start);
 
@@ -1899,7 +1905,7 @@ static void j2k_read_cox(opj_j2k_t *j2k, int compno) {
 
        tccp->numresolutions = cio_read(cio, 1) + 1;    /* SPcox (D) */
 
-       // If user wants to remove more resolutions than the codestream contains, return error
+       /* If user wants to remove more resolutions than the codestream contains, return error*/
        if (cp->reduce >= tccp->numresolutions) {
                opj_event_msg(j2k->cinfo, 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);
@@ -3524,7 +3530,7 @@ static void j2k_read_sot(opj_j2k_t *j2k) {
     if (numparts)
       j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_realloc(j2k->cstr_info->tile[tileno].tp, numparts * sizeof(opj_tp_info_t));
     else
-      j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_realloc(j2k->cstr_info->tile[tileno].tp, 10 * sizeof(opj_tp_info_t)); // Fixme (10)
+      j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_realloc(j2k->cstr_info->tile[tileno].tp, 10 * sizeof(opj_tp_info_t)); /* Fixme (10)*/
                j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos = cio_tell(cio) - 12;
                j2k->cstr_info->tile[tileno].tp[partno].tp_end_pos = 
                        j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos + totlen - 1;
@@ -3621,6 +3627,14 @@ opj_bool j2k_read_sot_v2 (
        opj_read_bytes(p_header_data,&l_tot_len,4);             /* Psot */
        p_header_data+=4;
 
+       /* PSot should be equal to zero or >=14 or <= 2^32-1 */
+       if ((l_tot_len !=0 ) && (l_tot_len < 14) )
+       {
+               opj_event_msg_v2(p_manager, EVT_ERROR, "Psot value (%d) is not correct regards to the JPEG2000 norm!\n", l_tot_len);
+               return OPJ_FALSE;
+       }
+
+
 #ifdef USE_JPWL
        if (l_cp->correct) {
 
@@ -3687,12 +3701,17 @@ opj_bool j2k_read_sot_v2 (
 
        p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPH;
 
-       /* Check if the current tile is outside the area we want decode (in tile index)*/
-       p_j2k->m_specific_param.m_decoder.m_skip_data =
-                       (l_tile_x < p_j2k->m_specific_param.m_decoder.m_start_tile_x)
-               ||      (l_tile_x >= p_j2k->m_specific_param.m_decoder.m_end_tile_x)
-               ||  (l_tile_y < p_j2k->m_specific_param.m_decoder.m_start_tile_y)
-               ||      (l_tile_y >= p_j2k->m_specific_param.m_decoder.m_end_tile_y);
+       /* Check if the current tile is outside the area we want decode or not corresponding to the tile index*/
+       if (p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec == -1) {
+               p_j2k->m_specific_param.m_decoder.m_skip_data =
+                               (l_tile_x < p_j2k->m_specific_param.m_decoder.m_start_tile_x)
+                       ||      (l_tile_x >= p_j2k->m_specific_param.m_decoder.m_end_tile_x)
+                       ||  (l_tile_y < p_j2k->m_specific_param.m_decoder.m_start_tile_y)
+                       ||      (l_tile_y >= p_j2k->m_specific_param.m_decoder.m_end_tile_y);
+       }
+       else
+               p_j2k->m_specific_param.m_decoder.m_skip_data =
+                       (p_j2k->m_current_tile_number != p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec);
 
        /* Index */
        if (p_j2k->cstr_index)
@@ -3909,8 +3928,13 @@ opj_bool j2k_read_sod_v2 (
 
        l_tcp = &(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
 
-       if (p_j2k->m_specific_param.m_decoder.m_last_tile_part)
-               p_j2k->m_specific_param.m_decoder.m_sot_length = opj_stream_get_number_byte_left(p_stream) - 2;
+       if (p_j2k->m_specific_param.m_decoder.m_last_tile_part) {
+               // opj_stream_get_number_byte_left returns OPJ_OFF_T
+               // but we are in the last tile part,
+               // so its result will fit on OPJ_UINT32 unless we find
+               // a file with a single tile part of more than 4 GB...
+               p_j2k->m_specific_param.m_decoder.m_sot_length = (OPJ_UINT32)(opj_stream_get_number_byte_left(p_stream) - 2);
+       }
        else
                p_j2k->m_specific_param.m_decoder.m_sot_length -= 2;
 
@@ -3918,10 +3942,10 @@ opj_bool j2k_read_sod_v2 (
        l_tile_len = &l_tcp->m_data_size;
 
        if (! *l_current_data) {
-               *l_current_data = (OPJ_BYTE*) opj_malloc/*FIXME V2 -> my_opj_malloc*/(p_j2k->m_specific_param.m_decoder.m_sot_length);
+               *l_current_data = (OPJ_BYTE*) opj_malloc(p_j2k->m_specific_param.m_decoder.m_sot_length);
        }
        else {
-               *l_current_data = (OPJ_BYTE*) opj_realloc/*FIXME V2 -> my_opj_realloc*/(*l_current_data, *l_tile_len + p_j2k->m_specific_param.m_decoder.m_sot_length);
+               *l_current_data = (OPJ_BYTE*) opj_realloc(*l_current_data, *l_tile_len + p_j2k->m_specific_param.m_decoder.m_sot_length);
        }
 
        if (*l_current_data == 00) {
@@ -3933,8 +3957,9 @@ opj_bool j2k_read_sod_v2 (
        /* Index */
        l_cstr_index = p_j2k->cstr_index;
        if (l_cstr_index) {
-               OPJ_SIZE_T l_current_pos = opj_stream_tell(p_stream) - 2;
-               OPJ_UINT32 l_current_tile_part =l_cstr_index->tile_index[p_j2k->m_current_tile_number].current_tpsno;
+               OPJ_OFF_T l_current_pos = opj_stream_tell(p_stream) - 2;
+
+               OPJ_UINT32 l_current_tile_part = l_cstr_index->tile_index[p_j2k->m_current_tile_number].current_tpsno;
                l_cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index[l_current_tile_part].end_header =
                                l_current_pos;
                l_cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index[l_current_tile_part].end_pos =
@@ -3949,12 +3974,6 @@ opj_bool j2k_read_sod_v2 (
                /*l_cstr_index->packno = 0;*/
        }
 
-
-
-
-
-
-
        l_current_read_size = opj_stream_read_data(     p_stream,
                                                                                                *l_current_data + *l_tile_len,
                                                                                                p_j2k->m_specific_param.m_decoder.m_sot_length,
@@ -5078,13 +5097,13 @@ opj_image_t* j2k_decode(opj_j2k_t *j2k, opj_cio_t *cio, opj_codestream_info_t *c
                        return 0;
                }
                e = j2k_dec_mstab_lookup(id);
-               // Check if the marker is known
+               /* Check if the marker is known*/
                if (!(j2k->state & e->states)) {
                        opj_image_destroy(image);
                        opj_event_msg(cinfo, EVT_ERROR, "%.8x: unexpected marker %x\n", cio_tell(cio) - 2, id);
                        return 0;
                }
-               // Check if the decoding is limited to the main header
+               /* Check if the decoding is limited to the main header*/
                if (e->id == J2K_MS_SOT && j2k->cp->limit_decoding == LIMIT_TO_MAIN_HEADER) {
                        opj_event_msg(cinfo, EVT_INFO, "Main Header decoded.\n");
                        return image;
@@ -5468,10 +5487,10 @@ void j2k_setup_encoder(opj_j2k_t *j2k, opj_cparameters_t *parameters, opj_image_
 
                        if(parameters->cp_cinema)
                        {
-                               //Precinct size for lowest frequency subband=128
+                               /*Precinct size for lowest frequency subband=128*/
                                tccp->prcw[0] = 7;
                                tccp->prch[0] = 7;
-                               //Precinct size at all other resolutions = 256
+                               /*Precinct size at all other resolutions = 256*/
                                for (j = 1; j < tccp->numresolutions; j++) {
                                        tccp->prcw[j] = 8;
                                        tccp->prch[j] = 8;
@@ -5513,7 +5532,7 @@ void j2k_setup_encoder(opj_j2k_t *j2k, opj_cparameters_t *parameters, opj_image_
                                                }
                                                p++;
                                                /*printf("\nsize precinct for level %d : %d,%d\n", j,tccp->prcw[j], tccp->prch[j]); */
-                                       }       //end for
+                                       }       /*end for*/
                                } else {
                                        for (j = 0; j < tccp->numresolutions; j++) {
                                                tccp->prcw[j] = 15;
@@ -5746,10 +5765,9 @@ opj_bool j2k_encode(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image, opj_code
        return OPJ_TRUE;
 }
 
-static void j2k_add_mhmarker(opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len) {
-
-       if (!cstr_info)
-               return;
+static void j2k_add_mhmarker(opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len)
+{
+       assert(cstr_info != 00);
 
        /* expand the list? */
        if ((cstr_info->marknum + 1) > cstr_info->maxmarknum) {
@@ -5765,10 +5783,9 @@ static void j2k_add_mhmarker(opj_codestream_info_t *cstr_info, unsigned short in
 
 }
 
-static void j2k_add_mhmarker_v2(opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_UINT32 pos, OPJ_UINT32 len) {
-
-       if (!cstr_index)
-               return;
+static void j2k_add_mhmarker_v2(opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len)
+{
+       assert(cstr_index != 00);
 
        /* expand the list? */
        if ((cstr_index->marknum + 1) > cstr_index->maxmarknum) {
@@ -5784,13 +5801,11 @@ static void j2k_add_mhmarker_v2(opj_codestream_index_t *cstr_index, OPJ_UINT32 t
 
 }
 
-static void j2k_add_tlmarker( int tileno, opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len) {
-
-
-  opj_marker_info_t *marker;
+static void j2k_add_tlmarker( int tileno, opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len)
+{
+       opj_marker_info_t *marker;
 
-       if (!cstr_info)
-               return;
+       assert(cstr_info != 00);
 
        /* expand the list? */
        if ((cstr_info->tile[tileno].marknum + 1) > cstr_info->tile[tileno].maxmarknum) {
@@ -5807,13 +5822,10 @@ static void j2k_add_tlmarker( int tileno, opj_codestream_info_t *cstr_info, unsi
        cstr_info->tile[tileno].marknum++;
 }
 
-static void j2k_add_tlmarker_v2(OPJ_UINT32 tileno, opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_UINT32 pos, OPJ_UINT32 len)
+static void j2k_add_tlmarker_v2(OPJ_UINT32 tileno, opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len)
 {
-
-       if (!cstr_index)
-               return;
-
-       if (!cstr_index->tile_index)
+       assert(cstr_index != 00);
+       assert(cstr_index->tile_index != 00);
 
        /* expand the list? */
        if ((cstr_index->tile_index[tileno].marknum + 1) > cstr_index->tile_index[tileno].maxmarknum) {
@@ -5836,12 +5848,9 @@ static void j2k_add_tlmarker_v2(OPJ_UINT32 tileno, opj_codestream_index_t *cstr_
                        cstr_index->tile_index[tileno].tp_index[l_current_tile_part].start_pos = pos;
 
        }
-
 }
 
 
-
-
 /*
  * -----------------------------------------------------------------------
  * -----------------------------------------------------------------------
@@ -6113,7 +6122,7 @@ opj_bool j2k_read_header_procedure(       opj_j2k_v2_t *p_j2k,
                opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
        }
 
-       opj_event_msg_v2(p_manager, EVT_INFO, "Main header has been correctly decode.\n");
+       opj_event_msg_v2(p_manager, EVT_INFO, "Main header has been correctly decoded.\n");
 
        /* Position of the last element if the main header */
        p_j2k->cstr_index->main_head_end = (OPJ_UINT32) opj_stream_tell(p_stream) - 2;
@@ -6302,7 +6311,7 @@ opj_bool j2k_copy_default_tcp_and_create_tcd
  *
  * @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 * j2k_get_marker_handler (OPJ_UINT32 p_id)
 {
        const opj_dec_memory_marker_handler_t *e;
        for (e = j2k_memory_marker_handler_tab; e->id != 0; ++e) {
@@ -6483,13 +6492,24 @@ void j2k_tcp_destroy (opj_tcp_v2_t *p_tcp)
                p_tcp->mct_norms = 00;
        }
 
+       j2k_tcp_data_destroy(p_tcp);
+
+}
+
+/**
+ * Destroys the data inside a tile coding parameter structure.
+ *
+ * @param      p_tcp           the tile coding parameter which contain data to destroy.
+ */
+void j2k_tcp_data_destroy (opj_tcp_v2_t *p_tcp)
+{
        if (p_tcp->m_data) {
                opj_free(p_tcp->m_data);
-               p_tcp->m_data = 00;
+               p_tcp->m_data = NULL;
+               p_tcp->m_data_size = 0;
        }
 }
 
-
 /**
  * Destroys a coding parameter structure.
  *
@@ -6636,9 +6656,6 @@ opj_bool j2k_read_tile_header(    opj_j2k_v2_t * p_j2k,
                                return OPJ_FALSE;
                        }
 
-/*                     if (l_current_marker == J2K_MS_SOT)
-                               j2k_add_tlmarker();*/
-
                        /* Add the marker to the codestream index*/
                        j2k_add_tlmarker_v2(p_j2k->m_current_tile_number,
                                                                p_j2k->cstr_index,
@@ -6646,6 +6663,16 @@ opj_bool j2k_read_tile_header(   opj_j2k_v2_t * p_j2k,
                                                                (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) {
@@ -6733,7 +6760,7 @@ opj_bool j2k_read_tile_header(    opj_j2k_v2_t * p_j2k,
        }
 
        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_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;
@@ -6773,7 +6800,7 @@ opj_bool j2k_decode_tile (        opj_j2k_v2_t * p_j2k,
 
        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]));
+               j2k_tcp_destroy(l_tcp);
                return OPJ_FALSE;
        }
 
@@ -6791,8 +6818,11 @@ opj_bool j2k_decode_tile (       opj_j2k_v2_t * p_j2k,
                return OPJ_FALSE;
        }
 
-       j2k_tcp_destroy(l_tcp);
-       p_j2k->m_tcd->tcp = 0;
+       /* 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);
@@ -6876,6 +6906,11 @@ opj_bool j2k_update_image_data (opj_tcd_v2_t * p_tcd, OPJ_BYTE * p_data, opj_ima
                /*-----*/
 
                /* 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);
 
@@ -6885,6 +6920,11 @@ opj_bool j2k_update_image_data (opj_tcd_v2_t * p_tcd, OPJ_BYTE * p_data, opj_ima
                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
@@ -6963,7 +7003,7 @@ opj_bool j2k_update_image_data (opj_tcd_v2_t * p_tcd, OPJ_BYTE * p_data, opj_ima
                /* 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) {
+               /*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"
@@ -6975,7 +7015,7 @@ opj_bool j2k_update_image_data (opj_tcd_v2_t * p_tcd, OPJ_BYTE * p_data, opj_ima
                        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) {
@@ -7099,7 +7139,7 @@ opj_bool j2k_set_decode_area(     opj_j2k_v2_t *p_j2k,
        }
 
        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 all image\n");
+               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;
@@ -7172,7 +7212,7 @@ opj_bool j2k_set_decode_area(     opj_j2k_v2_t *p_j2k,
        /* Bottom */
        if (p_end_y < l_image->y0) {
                opj_event_msg_v2(p_manager, EVT_ERROR,
-                       "Right position of the decoded area (region_y1=%d) is outside the image area (YOsiz=%d).\n",
+                       "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;
        }
@@ -7194,26 +7234,32 @@ opj_bool j2k_set_decode_area(   opj_j2k_v2_t *p_j2k,
        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_img_comp->w = int_ceildivpow2(l_comp_x1 - l_img_comp->x0, l_img_comp->factor);
-               if (l_img_comp->w <= 0){
+               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_img_comp->w);
+                               it_comp, l_w);
                        return OPJ_FALSE;
                }
+               l_img_comp->w = l_w;
 
-               l_img_comp->h = int_ceildivpow2(l_comp_y1 - l_img_comp->y0, l_img_comp->factor);
-               if (l_img_comp->h <= 0){
+               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_img_comp->h);
+                               it_comp, l_h);
                        return OPJ_FALSE;
                }
+               l_img_comp->h = l_h;
 
                l_img_comp++;
        }
@@ -7260,6 +7306,10 @@ opj_j2k_v2_t* j2k_create_decompress_v2()
 
        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 = j2k_create_cstr_index();
 
@@ -7526,8 +7576,8 @@ opj_bool j2k_read_SQcd_SQcc(
 
                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);
+                               "number of subbands (%d) is greater to J2K_MAXBANDS (%d). So we limit the number of elements stored to "
+                               "J2K_MAXBANDS (%d) and skip the rest. \n", l_num_band, J2K_MAXBANDS, J2K_MAXBANDS);
                        //return OPJ_FALSE;
                }
        }
@@ -7683,14 +7733,15 @@ void j2k_dump_MH_index(opj_j2k_v2_t* p_j2k, FILE* out_stream)
 
        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",
+       fprintf(out_stream, "\t Main header start position=%" OPJ_OFF_F "d\n"
+                                   "\t Main header end position=%" OPJ_OFF_F "d\n",
                        cstr_index->main_head_start, cstr_index->main_head_end);
 
        fprintf(out_stream, "\t Marker list: {\n");
 
        if (cstr_index->marker){
                for (it_marker=0; it_marker < cstr_index->marknum ; it_marker++){
-                       fprintf(out_stream, "\t\t type=%#x, pos=%d, len=%d\n",
+                       fprintf(out_stream, "\t\t type=%#x, pos=%" OPJ_OFF_F "d, len=%d\n",
                                        cstr_index->marker[it_marker].type,
                                        cstr_index->marker[it_marker].pos,
                                        cstr_index->marker[it_marker].len );
@@ -7710,7 +7761,7 @@ void j2k_dump_MH_index(opj_j2k_v2_t* p_j2k, FILE* out_stream)
 
                        if (cstr_index->tile_index[it_tile].tp_index){
                                for (it_tile_part =0; it_tile_part < nb_of_tile_part; it_tile_part++){
-                                       fprintf(out_stream, "\t\t\t tile-part[%d]: star_pos=%d, end_header=%d, end_pos=%d.\n",
+                                       fprintf(out_stream, "\t\t\t tile-part[%d]: star_pos=%" OPJ_OFF_F "d, end_header=%" OPJ_OFF_F "d, end_pos=%" OPJ_OFF_F "d.\n",
                                                        it_tile_part,
                                                        cstr_index->tile_index[it_tile].tp_index[it_tile_part].start_pos,
                                                        cstr_index->tile_index[it_tile].tp_index[it_tile_part].end_header,
@@ -7720,7 +7771,7 @@ void j2k_dump_MH_index(opj_j2k_v2_t* p_j2k, FILE* out_stream)
 
                        if (cstr_index->tile_index[it_tile].marker){
                                for (it_marker=0; it_marker < cstr_index->tile_index[it_tile].marknum ; it_marker++){
-                                       fprintf(out_stream, "\t\t type=%#x, pos=%d, len=%d\n",
+                                       fprintf(out_stream, "\t\t type=%#x, pos=%" OPJ_OFF_F "d, len=%d\n",
                                                        cstr_index->tile_index[it_tile].marker[it_marker].type,
                                                        cstr_index->tile_index[it_tile].marker[it_marker].pos,
                                                        cstr_index->tile_index[it_tile].marker[it_marker].len );
@@ -7818,7 +7869,7 @@ void j2k_dump_image_header(opj_image_t* img_header, opj_bool dev_dump_flag, FILE
        char tab[2];
 
        if (dev_dump_flag){
-               fprintf(stdout, "[DEV] Dump a image_header struct {\n");
+               fprintf(stdout, "[DEV] Dump an image_header struct {\n");
                tab[0] = '\0';
        }
        else {
@@ -7854,7 +7905,7 @@ void j2k_dump_image_comp_header(opj_image_comp_t* comp_header, opj_bool dev_dump
        char tab[3];
 
        if (dev_dump_flag){
-               fprintf(stdout, "[DEV] Dump a image_comp_header struct {\n");
+               fprintf(stdout, "[DEV] Dump an image_comp_header struct {\n");
                tab[0] = '\0';
        }       else {
                tab[0] = '\t';tab[1] = '\t';tab[2] = '\0';
@@ -8061,7 +8112,7 @@ opj_bool j2k_allocate_tile_element_cstr_index(opj_j2k_v2_t *p_j2k)
 {
        OPJ_UINT32 it_tile=0;
 
-       p_j2k->cstr_index->nb_of_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.tw;
+       p_j2k->cstr_index->nb_of_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th;
        p_j2k->cstr_index->tile_index = (opj_tile_index_t*)opj_calloc(p_j2k->cstr_index->nb_of_tiles, sizeof(opj_tile_index_t));
        if (!p_j2k->cstr_index->tile_index)
                return OPJ_FALSE;
@@ -8099,8 +8150,10 @@ opj_bool j2k_decode_tiles (      opj_j2k_v2_t *p_j2k,
        l_max_data_size = 1000;
 
        /*Allocate and initialize some elements of codestrem index*/
-       if (!j2k_allocate_tile_element_cstr_index(p_j2k))
+       if (!j2k_allocate_tile_element_cstr_index(p_j2k)){
+               opj_free(l_current_data);
                return OPJ_FALSE;
+       }
 
        while (OPJ_TRUE) {
                if (! j2k_read_tile_header(     p_j2k,
@@ -8123,6 +8176,7 @@ opj_bool j2k_decode_tiles (       opj_j2k_v2_t *p_j2k,
                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) {
+                               opj_free(l_current_data);
                                return OPJ_FALSE;
                        }
 
@@ -8133,7 +8187,7 @@ opj_bool j2k_decode_tiles (       opj_j2k_v2_t *p_j2k,
                        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);
+               opj_event_msg_v2(p_manager, EVT_INFO, "Tile %d/%d has been decoded.\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, p_j2k->m_output_image)) {
                        opj_free(l_current_data);
@@ -8161,6 +8215,136 @@ void j2k_setup_decoding (opj_j2k_v2_t *p_j2k)
 
 }
 
+/*
+ * Read and decode one tile.
+ */
+opj_bool j2k_decode_one_tile ( 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_tile_no_to_dec;
+       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;
+
+       /*Allocate and initialize some elements of codestrem index if not already done*/
+       if( !p_j2k->cstr_index->tile_index)
+       {
+               if (!j2k_allocate_tile_element_cstr_index(p_j2k)){
+                       opj_free(l_current_data);
+                       return OPJ_FALSE;
+               }
+       }
+       /* Move into the codestream to the first SOT used to decode the desired tile */
+       l_tile_no_to_dec = p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec;
+       if (p_j2k->cstr_index->tile_index)
+               if(p_j2k->cstr_index->tile_index->tp_index)
+               {
+                       if ( ! p_j2k->cstr_index->tile_index[l_tile_no_to_dec].nb_tps) {
+                               /* the index for this tile has not been built,
+                                *  so move to the last SOT read */
+                               if ( opj_stream_read_seek(p_stream, p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos+2, p_manager) ){
+                                       opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with seek function\n");
+                                       return OPJ_FALSE;
+                               }
+                       }
+                       else{
+                               if (opj_stream_read_seek(p_stream, p_j2k->cstr_index->tile_index[l_tile_no_to_dec].tp_index[0].start_pos+2, p_manager)) {
+                                       opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with seek function\n");
+                                       return OPJ_FALSE;
+                               }
+                       }
+                       /* Special case if we have previously read the EOC marker (if the previous tile getted is the last ) */
+                       if(p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_EOC)
+                               p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
+               }
+
+       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)) {
+                       opj_free(l_current_data);
+                       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) {
+                               opj_free(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 decoded.\n", l_current_tile_no, (p_j2k->m_cp.th * p_j2k->m_cp.tw) - 1);
+
+               if (! j2k_update_image_data(p_j2k->m_tcd,l_current_data, p_j2k->m_output_image)) {
+                       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);
+
+               if(l_current_tile_no == l_tile_no_to_dec)
+               {
+                       /* move into the codestream to the the first SOT (FIXME or not move?)*/
+                       if (opj_stream_read_seek(p_stream, p_j2k->cstr_index->main_head_end + 2, p_manager) ) {
+                               opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with seek function\n");
+                               return OPJ_FALSE;
+                       }
+                       break;
+               }
+               else {
+                       opj_event_msg_v2(p_manager, EVT_WARNING, "Tile read, decode and updated is not the desired (%d vs %d).\n", l_current_tile_no, l_tile_no_to_dec);
+               }
+
+       }
+
+       opj_free(l_current_data);
+
+       return OPJ_TRUE;
+}
+
+
+/**
+ * Sets up the procedures to do on decoding one tile. Developpers wanting to extend the library can add their own reading procedures.
+ */
+void j2k_setup_decoding_tile (opj_j2k_v2_t *p_j2k)
+{
+       // preconditions
+       assert(p_j2k != 00);
+
+       opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(void*)j2k_decode_one_tile);
+       /* DEVELOPER CORNER, add your custom procedures */
+
+}
+
 
 /**
  * Decodes the tiles of the stream.
@@ -8200,3 +8384,136 @@ opj_bool j2k_decode_v2( opj_j2k_v2_t * p_j2k,
 
        return OPJ_TRUE;
 }
+
+
+/**
+ * Get the decoded tile.
+ *
+ * @param      p_j2k                   the jpeg2000 codestream codec.
+ * @param      p_stream                input_stream
+ * @param      p_image                 output image.   .
+ * @param      p_manager               the user event manager
+ * @param      tile_index              index of the tile we want decode
+ *
+ * @return     true                    if succeed.
+ */
+opj_bool j2k_get_tile( opj_j2k_v2_t *p_j2k,
+                                               opj_stream_private_t *p_stream,
+                                               opj_image_t* p_image,
+                                               struct opj_event_mgr * p_manager,
+                                               OPJ_UINT32 tile_index )
+{
+       OPJ_UINT32 compno;
+       OPJ_UINT32 l_tile_x, l_tile_y;
+       opj_image_comp_t* l_img_comp;
+
+       if (!p_image) {
+               opj_event_msg_v2(p_manager, EVT_ERROR, "We need an image previously created.\n");
+               return OPJ_FALSE;
+       }
+
+       if ( (tile_index < 0) && (tile_index >= p_j2k->m_cp.tw * p_j2k->m_cp.th) ){
+               opj_event_msg_v2(p_manager, EVT_ERROR, "Tile index provided by the user is incorrect %d (max = %d) \n", tile_index, (p_j2k->m_cp.tw * p_j2k->m_cp.th) - 1);
+               return OPJ_FALSE;
+       }
+
+       /* Compute the dimension of the desired tile*/
+       l_tile_x = tile_index % p_j2k->m_cp.tw;
+       l_tile_y = tile_index / p_j2k->m_cp.tw;
+
+       p_image->x0 = l_tile_x * p_j2k->m_cp.tdx + p_j2k->m_cp.tx0;
+       if (p_image->x0 < p_j2k->m_private_image->x0)
+               p_image->x0 = p_j2k->m_private_image->x0;
+       p_image->x1 = (l_tile_x + 1) * p_j2k->m_cp.tdx + p_j2k->m_cp.tx0;
+       if (p_image->x1 > p_j2k->m_private_image->x1)
+               p_image->x1 = p_j2k->m_private_image->x1;
+
+       p_image->y0 = l_tile_y * p_j2k->m_cp.tdy + p_j2k->m_cp.ty0;
+       if (p_image->y0 < p_j2k->m_private_image->y0)
+               p_image->y0 = p_j2k->m_private_image->y0;
+       p_image->y1 = (l_tile_y + 1) * p_j2k->m_cp.tdy + p_j2k->m_cp.ty0;
+       if (p_image->y1 > p_j2k->m_private_image->y1)
+               p_image->y1 = p_j2k->m_private_image->y1;
+
+       l_img_comp = p_image->comps;
+       for (compno=0; compno < p_image->numcomps; ++compno)
+       {
+               OPJ_INT32 l_comp_x1, l_comp_y1;
+
+               l_img_comp->factor = p_j2k->m_private_image->comps[compno].factor;
+
+               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_img_comp->w = int_ceildivpow2(l_comp_x1, l_img_comp->factor) - int_ceildivpow2(l_img_comp->x0, l_img_comp->factor);
+               l_img_comp->h = int_ceildivpow2(l_comp_y1, l_img_comp->factor) - int_ceildivpow2(l_img_comp->y0, l_img_comp->factor);
+
+               l_img_comp++;
+       }
+
+       /* Destroy the previous output image*/
+       if (p_j2k->m_output_image)
+               opj_image_destroy(p_j2k->m_output_image);
+
+       /* Create the ouput image from the information previously computed*/
+       p_j2k->m_output_image = opj_image_create0();
+       if (! (p_j2k->m_output_image)) {
+               return OPJ_FALSE;
+       }
+       opj_copy_image_header(p_image, p_j2k->m_output_image);
+
+       p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec = tile_index;
+
+       /* customization of the decoding */
+       j2k_setup_decoding_tile(p_j2k);
+
+       /* Decode the codestream */
+       if (! j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
+               opj_image_destroy(p_j2k->m_private_image);
+               p_j2k->m_private_image = NULL;
+               return OPJ_FALSE;
+       }
+
+       /* Move data and copy one information from codec to output image*/
+       for (compno = 0; compno < p_image->numcomps; compno++) {
+               p_image->comps[compno].resno_decoded = p_j2k->m_output_image->comps[compno].resno_decoded;
+
+               if (p_image->comps[compno].data)
+                       opj_free(p_image->comps[compno].data);
+
+               p_image->comps[compno].data = p_j2k->m_output_image->comps[compno].data;
+
+               p_j2k->m_output_image->comps[compno].data = NULL;
+       }
+
+       return OPJ_TRUE;
+}
+
+opj_bool j2k_set_decoded_resolution_factor(opj_j2k_v2_t *p_j2k, OPJ_UINT32 res_factor, opj_event_mgr_t * p_manager)
+{
+       OPJ_UINT32 it_comp;
+
+       p_j2k->m_cp.m_specific_param.m_dec.m_reduce = res_factor;
+
+       if (p_j2k->m_private_image) {
+               if (p_j2k->m_private_image->comps) {
+                       if (p_j2k->m_specific_param.m_decoder.m_default_tcp) {
+                               if (p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps) {
+                                       for (it_comp = 0 ; it_comp < p_j2k->m_private_image->numcomps; it_comp++) {
+                                               OPJ_UINT32 max_res = p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps[it_comp].numresolutions;
+                                               if ( res_factor >= max_res){
+                                                       opj_event_msg_v2(p_manager, EVT_ERROR, "Resolution factor is greater than the maximum resolution in the component.\n");
+                                                       return OPJ_FALSE;
+                                               }
+                                               p_j2k->m_private_image->comps[it_comp].factor = res_factor;
+                                       }
+                                       return OPJ_TRUE;
+                               }
+                       }
+               }
+       }
+
+       return OPJ_FALSE;
+}