[1.5] remove extra trailing semicolon
[openjpeg.git] / libopenjpeg / jp2.c
index 04381bd7f9025b7c5dfed1a8bf49ba11acc2203d..d43e70c560ece242ded91ea65177338516566e8b 100644 (file)
@@ -206,7 +206,10 @@ static opj_bool jp2_read_ihdr(opj_jp2_t *jp2, opj_cio_t *cio) {
 
        opj_common_ptr cinfo = jp2->cinfo;
 
-       jp2_read_boxhdr(cinfo, cio, &box);
+  if(jp2_read_boxhdr(cinfo, cio, &box) == OPJ_FALSE ) {
+    opj_event_msg(cinfo, EVT_ERROR, "Failed to read boxhdr\n");
+    return OPJ_FALSE;
+  }
        if (JP2_IHDR != box.type) {
                opj_event_msg(cinfo, EVT_ERROR, "Expected IHDR Marker\n");
                return OPJ_FALSE;
@@ -279,7 +282,10 @@ static opj_bool jp2_read_bpcc(opj_jp2_t *jp2, opj_cio_t *cio) {
 
        opj_common_ptr cinfo = jp2->cinfo;
 
-       jp2_read_boxhdr(cinfo, cio, &box);
+  if( jp2_read_boxhdr(cinfo, cio, &box) == OPJ_FALSE ) {
+    opj_event_msg(cinfo, EVT_ERROR, "Failed to read boxhdr\n");
+    return OPJ_FALSE;
+  }
        if (JP2_BPCC != box.type) {
                opj_event_msg(cinfo, EVT_ERROR, "Expected BPCC Marker\n");
                return OPJ_FALSE;
@@ -372,7 +378,10 @@ static void jp2_apply_pclr(opj_jp2_color_t *color, opj_image_t *image, opj_commo
   if( pcol < nr_channels )
     new_comps[pcol] = old_comps[cmp];
   else
-    opj_event_msg(cinfo, EVT_ERROR, "Error with pcol value. skipping\n");
+    {
+    opj_event_msg(cinfo, EVT_ERROR, "Error with pcol value %d (max: %d). skipping\n", pcol, nr_channels);
+    continue;
+    }
 
        if(cmap[i].mtyp == 0) /* Direct use */
   {
@@ -466,7 +475,7 @@ static opj_bool jp2_read_pclr(opj_jp2_t *jp2, opj_cio_t *cio,
        for(i = 0; i < nr_channels; ++i)
   {
 /* Cji */
-       *entries++ = cio_read(cio, channel_size[i]>>3);
+       *entries++ = cio_read(cio, (channel_size[i]+7)>>3);
   }
    }
 
@@ -636,87 +645,86 @@ opj_bool jp2_read_jp2h(opj_jp2_t *jp2, opj_cio_t *cio, opj_jp2_color_t *color)
 
        opj_common_ptr cinfo = jp2->cinfo;
 
-       jp2_read_boxhdr(cinfo, cio, &box);
-       do 
-   {
-       if (JP2_JP2H != box.type) 
-  {
-       if (box.type == JP2_JP2C) 
- {
-       opj_event_msg(cinfo, EVT_ERROR, "Expected JP2H Marker\n");
-       return OPJ_FALSE;
- }
-       cio_skip(cio, box.length - 8);
+  if( jp2_read_boxhdr(cinfo, cio, &box) == OPJ_FALSE ) return OPJ_FALSE;
+  do {
+    if (JP2_JP2H != box.type) 
+      {
+      if (box.type == JP2_JP2C) 
+        {
+        opj_event_msg(cinfo, EVT_ERROR, "Expected JP2H Marker\n");
+        return OPJ_FALSE;
+        }
+      cio_skip(cio, box.length - 8);
 
-       if(cio->bp >= cio->end) return OPJ_FALSE;
+      if(cio->bp >= cio->end) return OPJ_FALSE;
 
-       jp2_read_boxhdr(cinfo, cio, &box);
-  }
-   } while(JP2_JP2H != box.type);
+      if( jp2_read_boxhdr(cinfo, cio, &box) == OPJ_FALSE ) return OPJ_FALSE;
+      }
+  } while(JP2_JP2H != box.type);
 
        if (!jp2_read_ihdr(jp2, cio))
                return OPJ_FALSE;
        jp2h_end = box.init_pos + box.length;
 
-       if (jp2->bpc == 255) 
-   {
-       if (!jp2_read_bpcc(jp2, cio))
-               return OPJ_FALSE;
-   }
-       jp2_read_boxhdr(cinfo, cio, &box);
-
-       while(cio_tell(cio) < jp2h_end)
-   {
-       if(box.type == JP2_COLR)
-  {
-       if( !jp2_read_colr(jp2, cio, &box, color))
- {
-    cio_seek(cio, box.init_pos + 8);
-    cio_skip(cio, box.length - 8);
- }
-    jp2_read_boxhdr(cinfo, cio, &box);
-    continue;
-  }
-    if(box.type == JP2_CDEF)
-  {
-    if( !jp2_read_cdef(jp2, cio, &box, color))
- {
+  if (jp2->bpc == 255) 
+    {
+    if (!jp2_read_bpcc(jp2, cio))
+      return OPJ_FALSE;
+    }
+  if( jp2_read_boxhdr(cinfo, cio, &box) == OPJ_FALSE ) return OPJ_FALSE;
+
+  while(cio_tell(cio) < jp2h_end)
+    {
+    if(box.type == JP2_COLR)
+      {
+      if( !jp2_read_colr(jp2, cio, &box, color))
+        {
+        cio_seek(cio, box.init_pos + 8);
+        cio_skip(cio, box.length - 8);
+        }
+      if( jp2_read_boxhdr(cinfo, cio, &box) == OPJ_FALSE ) return OPJ_FALSE;
+      continue;
+      }
+    if(box.type == JP2_CDEF && !jp2->ignore_pclr_cmap_cdef)
+      {
+      if( !jp2_read_cdef(jp2, cio, &box, color))
+        {
+        cio_seek(cio, box.init_pos + 8);
+        cio_skip(cio, box.length - 8);
+        }
+      if( jp2_read_boxhdr(cinfo, cio, &box) == OPJ_FALSE ) return OPJ_FALSE;
+      continue;
+      }
+    if(box.type == JP2_PCLR && !jp2->ignore_pclr_cmap_cdef)
+      {
+      if( !jp2_read_pclr(jp2, cio, &box, color))
+        {
+        cio_seek(cio, box.init_pos + 8);
+        cio_skip(cio, box.length - 8);
+        }
+      if( jp2_read_boxhdr(cinfo, cio, &box) == OPJ_FALSE ) return OPJ_FALSE;
+      continue;
+      }
+    if(box.type == JP2_CMAP && !jp2->ignore_pclr_cmap_cdef)
+      {
+      if( !jp2_read_cmap(jp2, cio, &box, color))
+        {
+        cio_seek(cio, box.init_pos + 8);
+        cio_skip(cio, box.length - 8);
+        }
+      if( jp2_read_boxhdr(cinfo, cio, &box) == OPJ_FALSE ) return OPJ_FALSE;
+      continue;
+      }
     cio_seek(cio, box.init_pos + 8);
     cio_skip(cio, box.length - 8);
- }
-    jp2_read_boxhdr(cinfo, cio, &box);
-    continue;
-  }
-    if(box.type == JP2_PCLR)
-  {
-    if( !jp2_read_pclr(jp2, cio, &box, color))
- {
-    cio_seek(cio, box.init_pos + 8);
-    cio_skip(cio, box.length - 8);
- }
-    jp2_read_boxhdr(cinfo, cio, &box);
-    continue;
-  }
-    if(box.type == JP2_CMAP)
-  {
-    if( !jp2_read_cmap(jp2, cio, &box, color))
- {
-    cio_seek(cio, box.init_pos + 8);
-    cio_skip(cio, box.length - 8);
- }
-    jp2_read_boxhdr(cinfo, cio, &box);
-    continue;
-  }
-       cio_seek(cio, box.init_pos + 8);
-       cio_skip(cio, box.length - 8);
-       jp2_read_boxhdr(cinfo, cio, &box);
+    if( jp2_read_boxhdr(cinfo, cio, &box) == OPJ_FALSE ) return OPJ_FALSE;
 
-   }/* while(cio_tell(cio) < box_end) */
+    }/* while(cio_tell(cio) < box_end) */
 
-       cio_seek(cio, jp2h_end);
+  cio_seek(cio, jp2h_end);
 
-/* Part 1, I.5.3.3 : 'must contain at least one' */
-       return (color->jp2_has_colr == 1);
+  /* Part 1, I.5.3.3 : 'must contain at least one' */
+  return (color->jp2_has_colr == 1);
 
 }/* jp2_read_jp2h() */
 
@@ -751,8 +759,10 @@ opj_image_t* opj_jp2_decode(opj_jp2_t *jp2, opj_cio_t *cio,
        opj_event_msg(cinfo, EVT_ERROR, "Failed to decode J2K image\n");
        return NULL;
    }
+   
+    if (!jp2->ignore_pclr_cmap_cdef){
 
-/* Set Image Color Space */
+    /* Set Image Color Space */
        if (jp2->enumcs == 16)
                image->color_space = CLRSPC_SRGB;
        else if (jp2->enumcs == 17)
@@ -780,6 +790,8 @@ opj_image_t* opj_jp2_decode(opj_jp2_t *jp2, opj_cio_t *cio,
        color.icc_profile_buf = NULL;
        image->icc_profile_len = color.icc_profile_len;
    }
+   }
+   
        return image;
 
 }/* opj_jp2_decode() */
@@ -832,8 +844,10 @@ static opj_bool jp2_read_ftyp(opj_jp2_t *jp2, opj_cio_t *cio) {
 
        opj_common_ptr cinfo = jp2->cinfo;
 
-       jp2_read_boxhdr(cinfo, cio, &box);
-
+  if( jp2_read_boxhdr(cinfo, cio, &box) == OPJ_FALSE ) {
+    opj_event_msg(cinfo, EVT_ERROR, "Failed to read boxhdr\n");
+    return OPJ_FALSE;
+  }
        if (JP2_FTYP != box.type) {
                opj_event_msg(cinfo, EVT_ERROR, "Expected FTYP Marker\n");
                return OPJ_FALSE;
@@ -890,11 +904,14 @@ static opj_bool jp2_read_jp2c(opj_jp2_t *jp2, opj_cio_t *cio, unsigned int *j2k_
 
        opj_common_ptr cinfo = jp2->cinfo;
 
-       jp2_read_boxhdr(cinfo, cio, &box);
+  if( jp2_read_boxhdr(cinfo, cio, &box) == OPJ_FALSE ) {
+    opj_event_msg(cinfo, EVT_ERROR, "Failed to read boxhdr\n");
+    return OPJ_FALSE;
+  }
        do {
                if(JP2_JP2C != box.type) {
                        cio_skip(cio, box.length - 8);
-                       jp2_read_boxhdr(cinfo, cio, &box);
+                       if( jp2_read_boxhdr(cinfo, cio, &box) == OPJ_FALSE ) return OPJ_FALSE;
                }
        } while(JP2_JP2C != box.type);
 
@@ -923,7 +940,10 @@ static opj_bool jp2_read_jp(opj_jp2_t *jp2, opj_cio_t *cio) {
 
        opj_common_ptr cinfo = jp2->cinfo;
 
-       jp2_read_boxhdr(cinfo, cio, &box);
+  if( jp2_read_boxhdr(cinfo, cio, &box) == OPJ_FALSE ) {
+    opj_event_msg(cinfo, EVT_ERROR, "Failed to read boxhdr\n");
+    return OPJ_FALSE;
+  }
        if (JP2_JP != box.type) {
                opj_event_msg(cinfo, EVT_ERROR, "Expected JP Marker\n");
                return OPJ_FALSE;
@@ -1053,6 +1073,7 @@ void jp2_setup_decoder(opj_jp2_t *jp2, opj_dparameters_t *parameters) {
        /* setup the J2K codec */
        j2k_setup_decoder(jp2->j2k, parameters);
        /* further JP2 initializations go here */
+       jp2->ignore_pclr_cmap_cdef = parameters->flags & OPJ_DPARAMETERS_IGNORE_PCLR_CMAP_CDEF_FLAG;
 }
 
 /* ----------------------------------------------------------------------- */