[Bf-blender-cvs] [b547ac32d94] master: Cleanup: use early return for imbuf image loader functions

Campbell Barton noreply at git.blender.org
Wed Mar 31 08:11:38 CEST 2021


Commit: b547ac32d94e988a5514dc1e79f2d7181a5385d5
Author: Campbell Barton
Date:   Wed Mar 31 17:05:57 2021 +1100
Branches: master
https://developer.blender.org/rBb547ac32d94e988a5514dc1e79f2d7181a5385d5

Cleanup: use early return for imbuf image loader functions

Most imbuf loaders already did this, use early exit for the remaining
loaders that didn't.

===================================================================

M	source/blender/imbuf/intern/cineon/cineon_dpx.c
M	source/blender/imbuf/intern/iris.c
M	source/blender/imbuf/intern/radiance_hdr.c
M	source/blender/imbuf/intern/tiff.c

===================================================================

diff --git a/source/blender/imbuf/intern/cineon/cineon_dpx.c b/source/blender/imbuf/intern/cineon/cineon_dpx.c
index de54e6dab9d..91d7b9a8b9e 100644
--- a/source/blender/imbuf/intern/cineon/cineon_dpx.c
+++ b/source/blender/imbuf/intern/cineon/cineon_dpx.c
@@ -198,10 +198,10 @@ ImBuf *imb_load_cineon(const unsigned char *mem,
                        int flags,
                        char colorspace[IM_MAX_SPACE])
 {
-  if (imb_is_a_cineon(mem, size)) {
-    return imb_load_dpx_cineon(mem, size, 1, flags, colorspace);
+  if (!imb_is_a_cineon(mem, size)) {
+    return NULL;
   }
-  return NULL;
+  return imb_load_dpx_cineon(mem, size, 1, flags, colorspace);
 }
 
 bool imb_save_dpx(struct ImBuf *buf, const char *filepath, int flags)
@@ -219,8 +219,8 @@ ImBuf *imb_load_dpx(const unsigned char *mem,
                     int flags,
                     char colorspace[IM_MAX_SPACE])
 {
-  if (imb_is_a_dpx(mem, size)) {
-    return imb_load_dpx_cineon(mem, size, 0, flags, colorspace);
+  if (!imb_is_a_dpx(mem, size)) {
+    return NULL;
   }
-  return NULL;
+  return imb_load_dpx_cineon(mem, size, 0, flags, colorspace);
 }
diff --git a/source/blender/imbuf/intern/iris.c b/source/blender/imbuf/intern/iris.c
index 112b95bf1a1..547af472d73 100644
--- a/source/blender/imbuf/intern/iris.c
+++ b/source/blender/imbuf/intern/iris.c
@@ -270,11 +270,13 @@ struct ImBuf *imb_loadiris(const uchar *mem, size_t size, int flags, char colors
   ImBuf *ibuf = NULL;
   uchar dirty_flag = 0;
 
-  if (size < HEADER_SIZE) {
+  if (!imb_is_a_iris(mem, size)) {
     return NULL;
   }
 
-  if (!imb_is_a_iris(mem, size)) {
+  /* Could pe part of the magic check above,
+   * by convention this check only requests the size needed to read it's magic though. */
+  if (size < HEADER_SIZE) {
     return NULL;
   }
 
diff --git a/source/blender/imbuf/intern/radiance_hdr.c b/source/blender/imbuf/intern/radiance_hdr.c
index 285b18595f7..94b2a62aa26 100644
--- a/source/blender/imbuf/intern/radiance_hdr.c
+++ b/source/blender/imbuf/intern/radiance_hdr.c
@@ -229,87 +229,89 @@ struct ImBuf *imb_loadhdr(const unsigned char *mem,
   const unsigned char *ptr, *mem_eof = mem + size;
   char oriY[80], oriX[80];
 
-  if (imb_is_a_hdr(mem, size)) {
-    colorspace_set_default_role(colorspace, IM_MAX_SPACE, COLOR_ROLE_DEFAULT_FLOAT);
-
-    /* find empty line, next line is resolution info */
-    size_t x;
-    for (x = 1; x < size; x++) {
-      if ((mem[x - 1] == '\n') && (mem[x] == '\n')) {
-        found = 1;
-        break;
-      }
+  if (!imb_is_a_hdr(mem, size)) {
+    return NULL;
+  }
+
+  colorspace_set_default_role(colorspace, IM_MAX_SPACE, COLOR_ROLE_DEFAULT_FLOAT);
+
+  /* find empty line, next line is resolution info */
+  size_t x;
+  for (x = 1; x < size; x++) {
+    if ((mem[x - 1] == '\n') && (mem[x] == '\n')) {
+      found = 1;
+      break;
     }
-    if (found && (x < (size + 2))) {
-      if (sscanf((char *)&mem[x + 1],
-                 "%79s %d %79s %d",
-                 (char *)&oriY,
-                 &height,
-                 (char *)&oriX,
-                 &width) != 4) {
-        return NULL;
-      }
+  }
 
-      /* find end of this line, data right behind it */
-      ptr = (unsigned char *)strchr((char *)&mem[x + 1], '\n');
-      ptr++;
+  if ((found && (x < (size + 2))) == 0) {
+    /* Data not found! */
+    return NULL;
+  }
 
-      if (flags & IB_test) {
-        ibuf = IMB_allocImBuf(width, height, 32, 0);
-      }
-      else {
-        ibuf = IMB_allocImBuf(width, height, 32, (flags & IB_rect) | IB_rectfloat);
-      }
+  if (sscanf((const char *)&mem[x + 1],
+             "%79s %d %79s %d",
+             (char *)&oriY,
+             &height,
+             (char *)&oriX,
+             &width) != 4) {
+    return NULL;
+  }
 
-      if (UNLIKELY(ibuf == NULL)) {
-        return NULL;
-      }
-      ibuf->ftype = IMB_FTYPE_RADHDR;
+  /* find end of this line, data right behind it */
+  ptr = (const unsigned char *)strchr((const char *)&mem[x + 1], '\n');
+  ptr++;
 
-      if (flags & IB_alphamode_detect) {
-        ibuf->flags |= IB_alphamode_premul;
-      }
+  if (flags & IB_test) {
+    ibuf = IMB_allocImBuf(width, height, 32, 0);
+  }
+  else {
+    ibuf = IMB_allocImBuf(width, height, 32, (flags & IB_rect) | IB_rectfloat);
+  }
 
-      if (flags & IB_test) {
-        return ibuf;
-      }
+  if (UNLIKELY(ibuf == NULL)) {
+    return NULL;
+  }
 
-      /* read in and decode the actual data */
-      sline = (RGBE *)MEM_mallocN(sizeof(*sline) * width, __func__);
-      rect_float = ibuf->rect_float;
+  ibuf->ftype = IMB_FTYPE_RADHDR;
 
-      for (size_t y = 0; y < height; y++) {
-        ptr = freadcolrs(sline, ptr, width, mem_eof);
-        if (ptr == NULL) {
-          printf(
-              "WARNING! HDR decode error, image may be just truncated, or completely wrong...\n");
-          break;
-        }
-        for (x = 0; x < width; x++) {
-          /* convert to ldr */
-          RGBE2FLOAT(sline[x], fcol);
-          *rect_float++ = fcol[RED];
-          *rect_float++ = fcol[GRN];
-          *rect_float++ = fcol[BLU];
-          *rect_float++ = 1.0f;
-        }
-      }
-      MEM_freeN(sline);
-      if (oriY[0] == '-') {
-        IMB_flipy(ibuf);
-      }
+  if (flags & IB_alphamode_detect) {
+    ibuf->flags |= IB_alphamode_premul;
+  }
 
-      if (flags & IB_rect) {
-        IMB_rect_from_float(ibuf);
-      }
+  if (flags & IB_test) {
+    return ibuf;
+  }
 
-      return ibuf;
+  /* read in and decode the actual data */
+  sline = (RGBE *)MEM_mallocN(sizeof(*sline) * width, __func__);
+  rect_float = ibuf->rect_float;
+
+  for (size_t y = 0; y < height; y++) {
+    ptr = freadcolrs(sline, ptr, width, mem_eof);
+    if (ptr == NULL) {
+      printf("WARNING! HDR decode error, image may be just truncated, or completely wrong...\n");
+      break;
     }
-    // else printf("Data not found!\n");
+    for (x = 0; x < width; x++) {
+      /* convert to ldr */
+      RGBE2FLOAT(sline[x], fcol);
+      *rect_float++ = fcol[RED];
+      *rect_float++ = fcol[GRN];
+      *rect_float++ = fcol[BLU];
+      *rect_float++ = 1.0f;
+    }
+  }
+  MEM_freeN(sline);
+  if (oriY[0] == '-') {
+    IMB_flipy(ibuf);
+  }
+
+  if (flags & IB_rect) {
+    IMB_rect_from_float(ibuf);
   }
-  // else printf("Not a valid radiance HDR file!\n");
 
-  return NULL;
+  return ibuf;
 }
 
 /* ImBuf write */
diff --git a/source/blender/imbuf/intern/tiff.c b/source/blender/imbuf/intern/tiff.c
index 2fb14e40d9d..001cd4e1575 100644
--- a/source/blender/imbuf/intern/tiff.c
+++ b/source/blender/imbuf/intern/tiff.c
@@ -576,11 +576,7 @@ ImBuf *imb_loadtiff(const unsigned char *mem,
   int ib_depth;
   int found;
 
-  /* check whether or not we have a TIFF file */
-  if (size < IMB_TIFF_NCB) {
-    fprintf(stderr, "imb_loadtiff: size < IMB_TIFF_NCB\n");
-    return NULL;
-  }
+  /* Check whether or not we have a TIFF file. */
   if (imb_is_a_tiff(mem, size) == 0) {
     return NULL;
   }



More information about the Bf-blender-cvs mailing list