[Bf-blender-cvs] [72b9e07cf26] master: Add helper function to replace buffer of a single-frame image

Sergey Sharybin noreply at git.blender.org
Fri Jul 1 09:51:44 CEST 2022


Commit: 72b9e07cf26ddeb26ea8773004b951a7f1bff7c5
Author: Sergey Sharybin
Date:   Thu Jun 30 15:37:32 2022 +0200
Branches: master
https://developer.blender.org/rB72b9e07cf26ddeb26ea8773004b951a7f1bff7c5

Add helper function to replace buffer of a single-frame image

Very similar to BKE_image_add_from_imbuf with the exception that no
new image data-block is created, but instead the given one is modified.

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

M	source/blender/blenkernel/BKE_image.h
M	source/blender/blenkernel/intern/image.cc

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

diff --git a/source/blender/blenkernel/BKE_image.h b/source/blender/blenkernel/BKE_image.h
index 1f131568900..e54932fbc4e 100644
--- a/source/blender/blenkernel/BKE_image.h
+++ b/source/blender/blenkernel/BKE_image.h
@@ -194,6 +194,14 @@ struct Image *BKE_image_add_generated(struct Main *bmain,
  */
 struct Image *BKE_image_add_from_imbuf(struct Main *bmain, struct ImBuf *ibuf, const char *name);
 
+/**
+ * For a non-viewer single-buffer image (single frame file, or generated image) replace its image
+ * buffer with the given one.
+ * If an unsupported image type (multi-layer, image sequence, ...) the function will assert in the
+ * debug mode and will have an underfined behavior in the release mode.
+ */
+void BKE_image_replace_imbuf(struct Image *image, struct ImBuf *ibuf);
+
 /**
  * For reload, refresh, pack.
  */
diff --git a/source/blender/blenkernel/intern/image.cc b/source/blender/blenkernel/intern/image.cc
index b6294eb058c..92e98935e83 100644
--- a/source/blender/blenkernel/intern/image.cc
+++ b/source/blender/blenkernel/intern/image.cc
@@ -1216,23 +1216,37 @@ Image *BKE_image_add_from_imbuf(Main *bmain, ImBuf *ibuf, const char *name)
     return nullptr;
   }
 
-  if (source == IMA_SRC_FILE) {
-    STRNCPY(ima->filepath, ibuf->name);
-  }
-  else if (ibuf->rect_float) {
-    /* For the consistency with manual image creation: when the image buffer is float reflect it in
-     * the generated flags. */
-    ima->gen_flag |= IMA_GEN_FLOAT;
-  }
+  BKE_image_replace_imbuf(ima, ibuf);
+
+  return ima;
+}
+
+void BKE_image_replace_imbuf(Image *image, ImBuf *ibuf)
+{
+  BLI_assert(image->type == IMA_TYPE_IMAGE &&
+             ELEM(image->source, IMA_SRC_FILE, IMA_SRC_GENERATED));
+
+  BKE_image_free_buffers(image);
 
-  image_assign_ibuf(ima, ibuf, IMA_NO_INDEX, 0);
-  image_colorspace_from_imbuf(ima, ibuf);
+  image_assign_ibuf(image, ibuf, IMA_NO_INDEX, 0);
+  image_colorspace_from_imbuf(image, ibuf);
+
+  /* Keep generated image type flags consistent with the image buffer. */
+  if (image->source == IMA_SRC_GENERATED) {
+    if (ibuf->rect_float) {
+      image->gen_flag |= IMA_GEN_FLOAT;
+    }
+    else {
+      image->gen_flag &= ~IMA_GEN_FLOAT;
+    }
+
+    image->gen_x = ibuf->x;
+    image->gen_y = ibuf->y;
+  }
 
   /* Consider image dirty since its content can not be re-created unless the image is explicitly
    * saved. */
-  BKE_image_mark_dirty(ima, ibuf);
-
-  return ima;
+  BKE_image_mark_dirty(image, ibuf);
 }
 
 /** Pack image buffer to memory as PNG or EXR. */



More information about the Bf-blender-cvs mailing list