[Bf-blender-cvs] [c922b9e2c14] master: Fix image-from-imbuf resulting in invalid image configuration

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


Commit: c922b9e2c145b0c945fdb9d99f1209df3bf8bad0
Author: Sergey Sharybin
Date:   Thu Jun 30 15:17:30 2022 +0200
Branches: master
https://developer.blender.org/rBc922b9e2c145b0c945fdb9d99f1209df3bf8bad0

Fix image-from-imbuf resulting in invalid image configuration

The image which source is set to file is not expected to have empty
file path. If it happens it becomes very tricky to save the image on
exit using the standard quit dialog.

This change makes it so if the image buffer does not have file path
then the new image is set to the "generated" source and it behaves
as if the image was created like so and was fully painted on.

Additionally, mark image as dirty, so that quitting Blender after
such image was added will warn about possible data loss.

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

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

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

diff --git a/source/blender/blenkernel/intern/image.cc b/source/blender/blenkernel/intern/image.cc
index 0c4dc3c87f3..ed8652ca470 100644
--- a/source/blender/blenkernel/intern/image.cc
+++ b/source/blender/blenkernel/intern/image.cc
@@ -1174,18 +1174,35 @@ Image *BKE_image_add_generated(Main *bmain,
 
 Image *BKE_image_add_from_imbuf(Main *bmain, ImBuf *ibuf, const char *name)
 {
-  Image *ima;
-
   if (name == nullptr) {
     name = BLI_path_basename(ibuf->name);
   }
 
-  ima = image_alloc(bmain, name, IMA_SRC_FILE, IMA_TYPE_IMAGE);
+  /* When the image buffer has valid path create a new image with "file" source and copy the path
+   * from the image buffer.
+   * Otherwise create "generated" image, avoiding invalid configuration with an empty file path. */
+  const eImageSource source = ibuf->name[0] != '\0' ? IMA_SRC_FILE : IMA_SRC_GENERATED;
 
-  if (ima) {
+  Image *ima = image_alloc(bmain, name, source, IMA_TYPE_IMAGE);
+
+  if (!ima) {
+    return nullptr;
+  }
+
+  if (source == IMA_SRC_FILE) {
     STRNCPY(ima->filepath, ibuf->name);
-    image_assign_ibuf(ima, ibuf, IMA_NO_INDEX, 0);
   }
+  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;
+  }
+
+  image_assign_ibuf(ima, ibuf, IMA_NO_INDEX, 0);
+
+  /* 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;
 }



More information about the Bf-blender-cvs mailing list