cppcheck fix for openjp2 (#740)
[openjpeg.git] / src / lib / openjp2 / jp2.c
index 96599a70e5a39108fb1bef56301c9a83f6bc7e42..a607c8a944b5b8a973c4d3e977ea90609d00140a 100644 (file)
@@ -94,7 +94,7 @@ static OPJ_BYTE * opj_jp2_write_bpcc( opj_jp2_t *jp2,
  * @param      p_bpc_header_size                       the size of the bpc header
  * @param      p_manager                                       the user event manager.
  *
- * @return     true if the bpc header is valid, fale else.
+ * @return     true if the bpc header is valid, false else.
  */
 static OPJ_BOOL opj_jp2_read_bpcc(  opj_jp2_t *jp2,
                                     OPJ_BYTE * p_bpc_header_data,
@@ -170,7 +170,7 @@ static OPJ_BOOL opj_jp2_skip_jp2c(  opj_jp2_t *jp2,
  * @param      p_header_size   the size of the data contained in the file header box.
  * @param      p_manager               the user event manager.
  *
- * @return true if the JP2 Header box was successfully reconized.
+ * @return true if the JP2 Header box was successfully recognized.
 */
 static OPJ_BOOL opj_jp2_read_jp2h(  opj_jp2_t *jp2,
                                     OPJ_BYTE *p_header_data,
@@ -311,7 +311,7 @@ static OPJ_BOOL opj_jp2_read_cmap(  opj_jp2_t * jp2,
  * @param      p_colr_header_size                      the size of the color header
  * @param      p_manager                                       the user event manager.
  *
- * @return     true if the bpc header is valid, fale else.
+ * @return     true if the bpc header is valid, false else.
 */
 static OPJ_BOOL opj_jp2_read_colr(  opj_jp2_t *jp2,
                                     OPJ_BYTE * p_colr_header_data,
@@ -370,7 +370,7 @@ static OPJ_BOOL opj_jp2_exec (  opj_jp2_t * jp2,
  * @param      p_number_bytes_read             pointer to an int that will store the number of bytes read from the stream (shoul usually be 2).
  * @param      p_manager                               user event manager.
  *
- * @return     true if the box is reconized, false otherwise
+ * @return     true if the box is recognized, false otherwise
 */
 static OPJ_BOOL opj_jp2_read_boxhdr(opj_jp2_box_t *box,
                                     OPJ_UINT32 * p_number_bytes_read,
@@ -437,7 +437,7 @@ static const opj_jp2_header_handler_t jp2_img_header [] =
  * @param      p_box_max_size                  the maximum number of bytes in the box.
  * @param      p_manager         FIXME DOC
  *
- * @return     true if the box is reconized, false otherwise
+ * @return     true if the box is recognized, false otherwise
 */
 static OPJ_BOOL opj_jp2_read_boxhdr_char(   opj_jp2_box_t *box,
                                             OPJ_BYTE * p_data,
@@ -482,12 +482,16 @@ static OPJ_BOOL opj_jp2_read_boxhdr(opj_jp2_box_t *box,
        opj_read_bytes(l_data_header+4,&(box->type), 4);
     
   if(box->length == 0)/* last box */
-    {
+  {
     const OPJ_OFF_T bleft = opj_stream_get_number_byte_left(cio);
-    box->length = (OPJ_UINT32)bleft;
-    assert( (OPJ_OFF_T)box->length == bleft );
-    return OPJ_TRUE;
+    if (bleft > (OPJ_OFF_T)(0xFFFFFFFFU - 8U)) {
+      opj_event_msg(p_manager, EVT_ERROR, "Cannot handle box sizes higher than 2^32\n");
+      return OPJ_FALSE;
     }
+    box->length = (OPJ_UINT32)bleft + 8U;
+    assert( (OPJ_OFF_T)box->length == bleft + 8 );
+    return OPJ_TRUE;
+  }
 
        /* do we have a "special very large box ?" */
        /* read then the XLBox */
@@ -642,12 +646,13 @@ static OPJ_BYTE * opj_jp2_write_bpcc(     opj_jp2_t *jp2,
 {
        OPJ_UINT32 i;
        /* room for 8 bytes for box and 1 byte for each component */
-       OPJ_UINT32 l_bpcc_size = 8 + jp2->numcomps;
+       OPJ_UINT32 l_bpcc_size;
        OPJ_BYTE * l_bpcc_data,* l_current_bpcc_ptr;
        
        /* preconditions */
        assert(jp2 != 00);
        assert(p_nb_bytes_written != 00);
+       l_bpcc_size = 8 + jp2->numcomps;
 
        l_bpcc_data = (OPJ_BYTE *) opj_calloc(1,l_bpcc_size);
        if (l_bpcc_data == 00) {
@@ -849,6 +854,8 @@ static OPJ_BOOL opj_jp2_check_color(opj_image_t *image, opj_jp2_color_t *color,
                                opj_event_msg(p_manager, EVT_ERROR, "Invalid component index %d (>= %d).\n", info[i].cn, nr_channels);
                                return OPJ_FALSE;
                        }
+                       if (info[i].asoc == 65535U) continue;
+
                        if (info[i].asoc > 0 && (OPJ_UINT32)(info[i].asoc - 1) >= nr_channels) {
                                opj_event_msg(p_manager, EVT_ERROR, "Invalid component index %d (>= %d).\n", info[i].asoc - 1, nr_channels);
                                return OPJ_FALSE;
@@ -887,7 +894,7 @@ static OPJ_BOOL opj_jp2_check_color(opj_image_t *image, opj_jp2_color_t *color,
                        }
                }
 
-               pcol_usage = opj_calloc(nr_channels, sizeof(OPJ_BOOL));
+               pcol_usage = (OPJ_BOOL *) opj_calloc(nr_channels, sizeof(OPJ_BOOL));
                if (!pcol_usage) {
                        opj_event_msg(p_manager, EVT_ERROR, "Unexpected OOM.\n");
                        return OPJ_FALSE;
@@ -1313,15 +1320,15 @@ static OPJ_BOOL opj_jp2_read_cdef(      opj_jp2_t * jp2,
        }
 
        cdef_info = (opj_jp2_cdef_info_t*) opj_malloc(l_value * sizeof(opj_jp2_cdef_info_t));
-    if (!cdef_info)
-        return OPJ_FALSE;
+       if (!cdef_info)
+               return OPJ_FALSE;
 
        jp2->color.jp2_cdef = (opj_jp2_cdef_t*)opj_malloc(sizeof(opj_jp2_cdef_t));
-    if(!jp2->color.jp2_cdef)
-    {
-        opj_free(cdef_info);
-        return OPJ_FALSE;
-    }
+       if(!jp2->color.jp2_cdef)
+       {
+               opj_free(cdef_info);
+               return OPJ_FALSE;
+       }
        jp2->color.jp2_cdef->info = cdef_info;
        jp2->color.jp2_cdef->n = (OPJ_UINT16) l_value;
 
@@ -1383,14 +1390,63 @@ static OPJ_BOOL opj_jp2_read_colr( opj_jp2_t *jp2,
                        opj_event_msg(p_manager, EVT_ERROR, "Bad COLR header box (bad size: %d)\n", p_colr_header_size);
                        return OPJ_FALSE;
                }
-               if (p_colr_header_size > 7) {
+               if ((p_colr_header_size > 7) && (jp2->enumcs != 14)) { /* handled below for CIELab) */
                        /* testcase Altona_Technical_v20_x4.pdf */
                        opj_event_msg(p_manager, EVT_WARNING, "Bad COLR header box (bad size: %d)\n", p_colr_header_size);
                }
 
                opj_read_bytes(p_colr_header_data,&jp2->enumcs ,4);                     /* EnumCS */
-        
-        jp2->color.jp2_has_colr = 1;
+
+               p_colr_header_data += 4;
+
+               if(jp2->enumcs == 14)/* CIELab */
+               {
+                       OPJ_UINT32 *cielab;
+                       OPJ_UINT32 rl, ol, ra, oa, rb, ob, il;
+
+                       cielab = (OPJ_UINT32*)opj_malloc(9 * sizeof(OPJ_UINT32));
+                       if(cielab == NULL){
+                               opj_event_msg(p_manager, EVT_ERROR, "Not enough memory for cielab\n");
+                               return OPJ_FALSE;
+                       }
+                       cielab[0] = 14; /* enumcs */
+                       
+                       /* default values */
+                       rl = ra = rb = ol = oa = ob = 0;
+                       il = 0x00443530; /* D50 */
+                       cielab[1] = 0x44454600;/* DEF */
+
+                       if(p_colr_header_size == 35)
+                       {
+                               opj_read_bytes(p_colr_header_data, &rl, 4);
+                               p_colr_header_data += 4;
+                               opj_read_bytes(p_colr_header_data, &ol, 4);
+                               p_colr_header_data += 4;
+                               opj_read_bytes(p_colr_header_data, &ra, 4);
+                               p_colr_header_data += 4;
+                               opj_read_bytes(p_colr_header_data, &oa, 4);
+                               p_colr_header_data += 4;
+                               opj_read_bytes(p_colr_header_data, &rb, 4);
+                               p_colr_header_data += 4;
+                               opj_read_bytes(p_colr_header_data, &ob, 4);
+                               p_colr_header_data += 4;
+                               opj_read_bytes(p_colr_header_data, &il, 4);
+                               p_colr_header_data += 4;
+                               
+                               cielab[1] = 0;
+                       }
+                       else if(p_colr_header_size != 7)
+                       {
+                               opj_event_msg(p_manager, EVT_WARNING, "Bad COLR header box (CIELab, bad size: %d)\n", p_colr_header_size);
+                       }
+                       cielab[2] = rl; cielab[4] = ra; cielab[6] = rb;
+                       cielab[3] = ol; cielab[5] = oa; cielab[7] = ob;
+                       cielab[8] = il;
+
+                       jp2->color.icc_profile_buf = (OPJ_BYTE*)cielab;
+                       jp2->color.icc_profile_len = 0;
+               }
+               jp2->color.jp2_has_colr = 1;
        }
        else if (jp2->meth == 2) {
                /* ICC profile */
@@ -1399,11 +1455,11 @@ static OPJ_BOOL opj_jp2_read_colr( opj_jp2_t *jp2,
 
                jp2->color.icc_profile_len = (OPJ_UINT32)icc_len;
                jp2->color.icc_profile_buf = (OPJ_BYTE*) opj_calloc(1,(size_t)icc_len);
-        if (!jp2->color.icc_profile_buf)
-        {
-            jp2->color.icc_profile_len = 0;
-            return OPJ_FALSE;
-        }
+               if (!jp2->color.icc_profile_buf)
+               {
+                       jp2->color.icc_profile_len = 0;
+                       return OPJ_FALSE;
+               }
 
                for (it_icc_value = 0; it_icc_value < icc_len; ++it_icc_value)
                {
@@ -1412,16 +1468,16 @@ static OPJ_BOOL opj_jp2_read_colr( opj_jp2_t *jp2,
                        jp2->color.icc_profile_buf[it_icc_value] = (OPJ_BYTE) l_value;
                }
            
-        jp2->color.jp2_has_colr = 1;
+               jp2->color.jp2_has_colr = 1;
        }
        else if (jp2->meth > 2)
-    {
-        /*     ISO/IEC 15444-1:2004 (E), Table I.9 Legal METH values:
-        conforming JP2 reader shall ignore the entire Colour Specification box.*/
-        opj_event_msg(p_manager, EVT_INFO, "COLR BOX meth value is not a regular value (%d), " 
-            "so we will ignore the entire Colour Specification box. \n", jp2->meth);
-    }
-    return OPJ_TRUE;
+       {
+               /*      ISO/IEC 15444-1:2004 (E), Table I.9 Legal METH values:
+               conforming JP2 reader shall ignore the entire Colour Specification box.*/
+               opj_event_msg(p_manager, EVT_INFO, "COLR BOX meth value is not a regular value (%d), "
+                       "so we will ignore the entire Colour Specification box. \n", jp2->meth);
+       }
+       return OPJ_TRUE;
 }
 
 OPJ_BOOL opj_jp2_decode(opj_jp2_t *jp2,
@@ -1438,42 +1494,44 @@ OPJ_BOOL opj_jp2_decode(opj_jp2_t *jp2,
                return OPJ_FALSE;
        }
 
-    if (!jp2->ignore_pclr_cmap_cdef){
-           if (!opj_jp2_check_color(p_image, &(jp2->color), p_manager)) {
-                   return OPJ_FALSE;
-           }
-
-           /* Set Image Color Space */
-           if (jp2->enumcs == 16)
-                   p_image->color_space = OPJ_CLRSPC_SRGB;
-           else if (jp2->enumcs == 17)
-                   p_image->color_space = OPJ_CLRSPC_GRAY;
-           else if (jp2->enumcs == 18)
-                   p_image->color_space = OPJ_CLRSPC_SYCC;
-            else if (jp2->enumcs == 24)
-                    p_image->color_space = OPJ_CLRSPC_EYCC;
-           else
-                   p_image->color_space = OPJ_CLRSPC_UNKNOWN;
-
-           if(jp2->color.jp2_pclr) {
-                   /* Part 1, I.5.3.4: Either both or none : */
-                   if( !jp2->color.jp2_pclr->cmap)
-                           opj_jp2_free_pclr(&(jp2->color));
-                   else
-                           opj_jp2_apply_pclr(p_image, &(jp2->color));
-           }
-
-           /* Apply the color space if needed */
-           if(jp2->color.jp2_cdef) {
-                   opj_jp2_apply_cdef(p_image, &(jp2->color), p_manager);
-           }
-
-           if(jp2->color.icc_profile_buf) {
-                   p_image->icc_profile_buf = jp2->color.icc_profile_buf;
-                   p_image->icc_profile_len = jp2->color.icc_profile_len;
-                   jp2->color.icc_profile_buf = NULL;
-           }
-    }
+       if (!jp2->ignore_pclr_cmap_cdef){
+               if (!opj_jp2_check_color(p_image, &(jp2->color), p_manager)) {
+                       return OPJ_FALSE;
+               }
+
+               /* Set Image Color Space */
+               if (jp2->enumcs == 16)
+                       p_image->color_space = OPJ_CLRSPC_SRGB;
+               else if (jp2->enumcs == 17)
+                       p_image->color_space = OPJ_CLRSPC_GRAY;
+               else if (jp2->enumcs == 18)
+                       p_image->color_space = OPJ_CLRSPC_SYCC;
+               else if (jp2->enumcs == 24)
+                       p_image->color_space = OPJ_CLRSPC_EYCC;
+               else if (jp2->enumcs == 12)
+                       p_image->color_space = OPJ_CLRSPC_CMYK;
+               else
+                       p_image->color_space = OPJ_CLRSPC_UNKNOWN;
+
+               if(jp2->color.jp2_pclr) {
+                       /* Part 1, I.5.3.4: Either both or none : */
+                       if( !jp2->color.jp2_pclr->cmap)
+                               opj_jp2_free_pclr(&(jp2->color));
+                       else
+                               opj_jp2_apply_pclr(p_image, &(jp2->color));
+               }
+
+               /* Apply the color space if needed */
+               if(jp2->color.jp2_cdef) {
+                       opj_jp2_apply_cdef(p_image, &(jp2->color), p_manager);
+               }
+
+               if(jp2->color.icc_profile_buf) {
+                       p_image->icc_profile_buf = jp2->color.icc_profile_buf;
+                       p_image->icc_profile_len = jp2->color.icc_profile_len;
+                       jp2->color.icc_profile_buf = NULL;
+               }
+       }
 
        return OPJ_TRUE;
 }
@@ -1586,7 +1644,7 @@ static OPJ_BOOL opj_jp2_write_ftyp(opj_jp2_t *jp2,
                                                        opj_event_mgr_t * p_manager )
 {
        OPJ_UINT32 i;
-       OPJ_UINT32 l_ftyp_size = 16 + 4 * jp2->numcl;
+       OPJ_UINT32 l_ftyp_size;
        OPJ_BYTE * l_ftyp_data, * l_current_data_ptr;
        OPJ_BOOL l_result;
 
@@ -1594,6 +1652,7 @@ static OPJ_BOOL opj_jp2_write_ftyp(opj_jp2_t *jp2,
        assert(cio != 00);
        assert(jp2 != 00);
        assert(p_manager != 00);
+       l_ftyp_size = 16 + 4 * jp2->numcl;
 
        l_ftyp_data = (OPJ_BYTE *) opj_calloc(1,l_ftyp_size);
        
@@ -2063,7 +2122,7 @@ static OPJ_BOOL opj_jp2_read_header_procedure(  opj_jp2_t *jp2,
                if (box.type == JP2_JP2C) {
                        if (jp2->jp2_state & JP2_STATE_HEADER) {
                                jp2->jp2_state |= JP2_STATE_CODESTREAM;
-                                opj_free(l_current_data);
+                               opj_free(l_current_data);
                                return OPJ_TRUE;
                        }
                        else {
@@ -2078,7 +2137,7 @@ static OPJ_BOOL opj_jp2_read_header_procedure(  opj_jp2_t *jp2,
                        return OPJ_FALSE;
                }
                /* testcase 1851.pdf.SIGSEGV.ce9.948 */
-        else if (box.length < l_nb_bytes_read) {
+               else if (box.length < l_nb_bytes_read) {
                        opj_event_msg(p_manager, EVT_ERROR, "invalid box size %d (%x)\n", box.length, box.type);
                        opj_free(l_current_data);
                        return OPJ_FALSE;
@@ -2135,16 +2194,16 @@ static OPJ_BOOL opj_jp2_read_header_procedure(  opj_jp2_t *jp2,
                        }
                }
                else {
-            if (!(jp2->jp2_state & JP2_STATE_SIGNATURE)) {
-                opj_event_msg(p_manager, EVT_ERROR, "Malformed JP2 file format: first box must be JPEG 2000 signature box\n");
-                opj_free(l_current_data);
-                return OPJ_FALSE;
-            }
-            if (!(jp2->jp2_state & JP2_STATE_FILE_TYPE)) {
-                opj_event_msg(p_manager, EVT_ERROR, "Malformed JP2 file format: second box must be file type box\n");
-                opj_free(l_current_data);
-                return OPJ_FALSE;
-            }
+                       if (!(jp2->jp2_state & JP2_STATE_SIGNATURE)) {
+                               opj_event_msg(p_manager, EVT_ERROR, "Malformed JP2 file format: first box must be JPEG 2000 signature box\n");
+                               opj_free(l_current_data);
+                               return OPJ_FALSE;
+                       }
+                       if (!(jp2->jp2_state & JP2_STATE_FILE_TYPE)) {
+                               opj_event_msg(p_manager, EVT_ERROR, "Malformed JP2 file format: second box must be file type box\n");
+                               opj_free(l_current_data);
+                               return OPJ_FALSE;
+                       }
                        jp2->jp2_state |= JP2_STATE_UNKNOWN;
                        if (opj_stream_skip(stream,l_current_data_size,p_manager) != l_current_data_size) {
                                opj_event_msg(p_manager, EVT_ERROR, "Problem with skipping JPEG2000 box, stream error\n");
@@ -2425,7 +2484,7 @@ static OPJ_BOOL opj_jpip_skip_iptr(       opj_jp2_t *jp2,
  * @param      p_header_size   the size of the data contained in the file header box.
  * @param      p_manager               the user event manager.
  *
- * @return true if the JP2 Header box was successfully reconized.
+ * @return true if the JP2 Header box was successfully recognized.
 */
 static OPJ_BOOL opj_jp2_read_jp2h(  opj_jp2_t *jp2,
                                     OPJ_BYTE *p_header_data,
@@ -2824,6 +2883,10 @@ OPJ_BOOL opj_jp2_get_tile(       opj_jp2_t *p_jp2,
                p_image->color_space = OPJ_CLRSPC_GRAY;
        else if (p_jp2->enumcs == 18)
                p_image->color_space = OPJ_CLRSPC_SYCC;
+       else if (p_jp2->enumcs == 24)
+               p_image->color_space = OPJ_CLRSPC_EYCC;
+       else if (p_jp2->enumcs == 12)
+               p_image->color_space = OPJ_CLRSPC_CMYK;
        else
                p_image->color_space = OPJ_CLRSPC_UNKNOWN;