[trunk] correct bug when we expand the list of marker in a tile
[openjpeg.git] / libopenjpeg / j2k.c
index 8795395f47d7ca258607b7566ce7e81d927deecb..9bfd4b359ef749793a23d2aec11871518a0f67f5 100644 (file)
@@ -3627,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) {
 
@@ -5757,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) {
@@ -5776,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_OFF_T 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) {
@@ -5795,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) {
@@ -5820,11 +5824,8 @@ static void j2k_add_tlmarker( int tileno, opj_codestream_info_t *cstr_info, unsi
 
 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) {
@@ -5847,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;
 
        }
-
 }
 
 
-
-
 /*
  * -----------------------------------------------------------------------
  * -----------------------------------------------------------------------
@@ -6124,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;
@@ -7141,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;
@@ -7214,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;
        }
@@ -7578,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;
                }
        }
@@ -7735,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 );
@@ -7762,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,
@@ -7772,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 );
@@ -7870,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 {
@@ -7906,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';
@@ -8113,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;
@@ -8151,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,
@@ -8175,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;
                        }
 
@@ -8185,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);
@@ -8237,8 +8239,10 @@ opj_bool j2k_decode_one_tile (   opj_j2k_v2_t *p_j2k,
        /*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))
+               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;
@@ -8286,6 +8290,7 @@ opj_bool j2k_decode_one_tile (    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;
                        }
 
@@ -8298,7 +8303,7 @@ opj_bool j2k_decode_one_tile (    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, (p_j2k->m_cp.th * p_j2k->m_cp.tw) - 1);
+               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);
@@ -8403,7 +8408,7 @@ opj_bool j2k_get_tile(    opj_j2k_v2_t *p_j2k,
        opj_image_comp_t* l_img_comp;
 
        if (!p_image) {
-               opj_event_msg_v2(p_manager, EVT_ERROR, "We need a image previously created.\n");
+               opj_event_msg_v2(p_manager, EVT_ERROR, "We need an image previously created.\n");
                return OPJ_FALSE;
        }
 
@@ -8499,7 +8504,7 @@ opj_bool j2k_set_decoded_resolution_factor(opj_j2k_v2_t *p_j2k, OPJ_UINT32 res_f
                                        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 superior to the maximum resolution in the component.\n");
+                                                       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;