[Bf-blender-cvs] [1159b63a07f] master: Cleanup: move image save options init to image_save.cc

Brecht Van Lommel noreply at git.blender.org
Thu May 12 23:20:13 CEST 2022


Commit: 1159b63a07fd2cbc7fc48e162d57721c9c85b3f6
Author: Brecht Van Lommel
Date:   Thu May 12 17:44:07 2022 +0200
Branches: master
https://developer.blender.org/rB1159b63a07fd2cbc7fc48e162d57721c9c85b3f6

Cleanup: move image save options init to image_save.cc

The logic here is tightly coupled to the other image saving code.

Ref D14899

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

M	source/blender/blenkernel/BKE_image_save.h
M	source/blender/blenkernel/intern/image_save.cc
M	source/blender/editors/space_image/image_ops.c

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

diff --git a/source/blender/blenkernel/BKE_image_save.h b/source/blender/blenkernel/BKE_image_save.h
index 052fc937af9..f5695eb11a4 100644
--- a/source/blender/blenkernel/BKE_image_save.h
+++ b/source/blender/blenkernel/BKE_image_save.h
@@ -13,10 +13,11 @@ extern "C" {
 #endif
 
 struct Image;
+struct ImageUser;
 struct Main;
+struct RenderResult;
 struct ReportList;
 struct Scene;
-struct RenderResult;
 
 /* Image datablock saving. */
 
@@ -36,9 +37,13 @@ typedef struct ImageSaveOptions {
   bool do_newpath;
 } ImageSaveOptions;
 
-void BKE_image_save_options_init(struct ImageSaveOptions *opts,
+bool BKE_image_save_options_init(ImageSaveOptions *opts,
                                  struct Main *bmain,
-                                 struct Scene *scene);
+                                 struct Scene *scene,
+                                 struct Image *ima,
+                                 struct ImageUser *iuser,
+                                 const bool guess_path,
+                                 const bool save_as_render);
 void BKE_image_save_options_free(struct ImageSaveOptions *opts);
 
 bool BKE_image_save(struct ReportList *reports,
diff --git a/source/blender/blenkernel/intern/image_save.cc b/source/blender/blenkernel/intern/image_save.cc
index 0d7d238f3b2..476c9cb3a12 100644
--- a/source/blender/blenkernel/intern/image_save.cc
+++ b/source/blender/blenkernel/intern/image_save.cc
@@ -23,6 +23,7 @@
 #include "IMB_openexr.h"
 
 #include "BKE_colortools.h"
+#include "BKE_global.h"
 #include "BKE_image.h"
 #include "BKE_image_format.h"
 #include "BKE_image_save.h"
@@ -34,7 +35,51 @@
 
 using blender::Vector;
 
-void BKE_image_save_options_init(ImageSaveOptions *opts, Main *bmain, Scene *scene)
+static char imtype_best_depth(ImBuf *ibuf, const char imtype)
+{
+  const char depth_ok = BKE_imtype_valid_depths(imtype);
+
+  if (ibuf->rect_float) {
+    if (depth_ok & R_IMF_CHAN_DEPTH_32) {
+      return R_IMF_CHAN_DEPTH_32;
+    }
+    if (depth_ok & R_IMF_CHAN_DEPTH_24) {
+      return R_IMF_CHAN_DEPTH_24;
+    }
+    if (depth_ok & R_IMF_CHAN_DEPTH_16) {
+      return R_IMF_CHAN_DEPTH_16;
+    }
+    if (depth_ok & R_IMF_CHAN_DEPTH_12) {
+      return R_IMF_CHAN_DEPTH_12;
+    }
+    return R_IMF_CHAN_DEPTH_8;
+  }
+
+  if (depth_ok & R_IMF_CHAN_DEPTH_8) {
+    return R_IMF_CHAN_DEPTH_8;
+  }
+  if (depth_ok & R_IMF_CHAN_DEPTH_12) {
+    return R_IMF_CHAN_DEPTH_12;
+  }
+  if (depth_ok & R_IMF_CHAN_DEPTH_16) {
+    return R_IMF_CHAN_DEPTH_16;
+  }
+  if (depth_ok & R_IMF_CHAN_DEPTH_24) {
+    return R_IMF_CHAN_DEPTH_24;
+  }
+  if (depth_ok & R_IMF_CHAN_DEPTH_32) {
+    return R_IMF_CHAN_DEPTH_32;
+  }
+  return R_IMF_CHAN_DEPTH_8; /* fallback, should not get here */
+}
+
+bool BKE_image_save_options_init(ImageSaveOptions *opts,
+                                 Main *bmain,
+                                 Scene *scene,
+                                 Image *ima,
+                                 ImageUser *iuser,
+                                 const bool guess_path,
+                                 const bool save_as_render)
 {
   memset(opts, 0, sizeof(*opts));
 
@@ -42,6 +87,98 @@ void BKE_image_save_options_init(ImageSaveOptions *opts, Main *bmain, Scene *sce
   opts->scene = scene;
 
   BKE_image_format_init(&opts->im_format, false);
+
+  void *lock;
+  ImBuf *ibuf = BKE_image_acquire_ibuf(ima, iuser, &lock);
+
+  if (ibuf) {
+    Scene *scene = opts->scene;
+    bool is_depth_set = false;
+
+    if (ELEM(ima->type, IMA_TYPE_R_RESULT, IMA_TYPE_COMPOSITE)) {
+      /* imtype */
+      BKE_image_format_init_for_write(&opts->im_format, scene, NULL);
+      is_depth_set = true;
+      if (!BKE_image_is_multiview(ima)) {
+        /* In case multiview is disabled,
+         * render settings would be invalid for render result in this area. */
+        opts->im_format.stereo3d_format = *ima->stereo3d_format;
+        opts->im_format.views_format = ima->views_format;
+      }
+    }
+    else {
+      if (ima->source == IMA_SRC_GENERATED) {
+        opts->im_format.imtype = R_IMF_IMTYPE_PNG;
+        opts->im_format.compress = ibuf->foptions.quality;
+        opts->im_format.planes = ibuf->planes;
+      }
+      else {
+        BKE_image_format_from_imbuf(&opts->im_format, ibuf);
+      }
+
+      /* use the multiview image settings as the default */
+      opts->im_format.stereo3d_format = *ima->stereo3d_format;
+      opts->im_format.views_format = ima->views_format;
+
+      BKE_image_format_color_management_copy_from_scene(&opts->im_format, scene);
+    }
+
+    opts->im_format.color_management = R_IMF_COLOR_MANAGEMENT_FOLLOW_SCENE;
+
+    if (ima->source == IMA_SRC_TILED) {
+      BLI_strncpy(opts->filepath, ima->filepath, sizeof(opts->filepath));
+      BLI_path_abs(opts->filepath, ID_BLEND_PATH_FROM_GLOBAL(&ima->id));
+    }
+    else {
+      BLI_strncpy(opts->filepath, ibuf->name, sizeof(opts->filepath));
+    }
+
+    /* sanitize all settings */
+
+    /* unlikely but just in case */
+    if (ELEM(opts->im_format.planes, R_IMF_PLANES_BW, R_IMF_PLANES_RGB, R_IMF_PLANES_RGBA) == 0) {
+      opts->im_format.planes = R_IMF_PLANES_RGBA;
+    }
+
+    /* depth, account for float buffer and format support */
+    if (is_depth_set == false) {
+      opts->im_format.depth = imtype_best_depth(ibuf, opts->im_format.imtype);
+    }
+
+    /* some formats don't use quality so fallback to scenes quality */
+    if (opts->im_format.quality == 0) {
+      opts->im_format.quality = scene->r.im_format.quality;
+    }
+
+    /* check for empty path */
+    if (guess_path && opts->filepath[0] == 0) {
+      const bool is_prev_save = !STREQ(G.ima, "//");
+      if (save_as_render) {
+        if (is_prev_save) {
+          BLI_strncpy(opts->filepath, G.ima, sizeof(opts->filepath));
+        }
+        else {
+          BLI_strncpy(opts->filepath, "//untitled", sizeof(opts->filepath));
+          BLI_path_abs(opts->filepath, BKE_main_blendfile_path(bmain));
+        }
+      }
+      else {
+        BLI_snprintf(opts->filepath, sizeof(opts->filepath), "//%s", ima->id.name + 2);
+        BLI_path_make_safe(opts->filepath);
+        BLI_path_abs(opts->filepath, is_prev_save ? G.ima : BKE_main_blendfile_path(bmain));
+      }
+
+      /* append UDIM marker if not present */
+      if (ima->source == IMA_SRC_TILED && strstr(opts->filepath, "<UDIM>") == NULL) {
+        int len = strlen(opts->filepath);
+        STR_CONCAT(opts->filepath, len, ".<UDIM>");
+      }
+    }
+  }
+
+  BKE_image_release_ibuf(ima, ibuf, lock);
+
+  return (ibuf != NULL);
 }
 
 void BKE_image_save_options_free(ImageSaveOptions *opts)
diff --git a/source/blender/editors/space_image/image_ops.c b/source/blender/editors/space_image/image_ops.c
index 0cfb924e618..940dca1a9ea 100644
--- a/source/blender/editors/space_image/image_ops.c
+++ b/source/blender/editors/space_image/image_ops.c
@@ -1683,144 +1683,6 @@ typedef struct ImageSaveData {
   ImageFormatData im_format;
 } ImageSaveData;
 
-static char imtype_best_depth(ImBuf *ibuf, const char imtype)
-{
-  const char depth_ok = BKE_imtype_valid_depths(imtype);
-
-  if (ibuf->rect_float) {
-    if (depth_ok & R_IMF_CHAN_DEPTH_32) {
-      return R_IMF_CHAN_DEPTH_32;
-    }
-    if (depth_ok & R_IMF_CHAN_DEPTH_24) {
-      return R_IMF_CHAN_DEPTH_24;
-    }
-    if (depth_ok & R_IMF_CHAN_DEPTH_16) {
-      return R_IMF_CHAN_DEPTH_16;
-    }
-    if (depth_ok & R_IMF_CHAN_DEPTH_12) {
-      return R_IMF_CHAN_DEPTH_12;
-    }
-    return R_IMF_CHAN_DEPTH_8;
-  }
-
-  if (depth_ok & R_IMF_CHAN_DEPTH_8) {
-    return R_IMF_CHAN_DEPTH_8;
-  }
-  if (depth_ok & R_IMF_CHAN_DEPTH_12) {
-    return R_IMF_CHAN_DEPTH_12;
-  }
-  if (depth_ok & R_IMF_CHAN_DEPTH_16) {
-    return R_IMF_CHAN_DEPTH_16;
-  }
-  if (depth_ok & R_IMF_CHAN_DEPTH_24) {
-    return R_IMF_CHAN_DEPTH_24;
-  }
-  if (depth_ok & R_IMF_CHAN_DEPTH_32) {
-    return R_IMF_CHAN_DEPTH_32;
-  }
-  return R_IMF_CHAN_DEPTH_8; /* fallback, should not get here */
-}
-
-static int image_save_options_init(Main *bmain,
-                                   ImageSaveOptions *opts,
-                                   Image *ima,
-                                   ImageUser *iuser,
-                                   const bool guess_path,
-                                   const bool save_as_render)
-{
-  void *lock;
-  ImBuf *ibuf = BKE_image_acquire_ibuf(ima, iuser, &lock);
-
-  if (ibuf) {
-    Scene *scene = opts->scene;
-    bool is_depth_set = false;
-
-    if (ELEM(ima->type, IMA_TYPE_R_RESULT, IMA_TYPE_COMPOSITE)) {
-      /* imtype */
-      BKE_image_format_init_for_write(&opts->im_format, scene, NULL);
-      is_depth_set = true;
-      if (!BKE_image_is_multiview(ima)) {
-        /* In case multiview is disabled,
-         * render settings would be invalid for render result in this area. */
-        opts->im_format.stereo3d_format = *ima->stereo3d_format;
-        opts->im_format.views_format = ima->views_format;
-      }
-    }
-    else {
-      if (ima->source == IMA_SRC_GENERATED) {
-        opts->im_format.imtype = R_IMF_IMTYPE_PNG;
-        opts->im_format.compress = ibuf->foptions.quality;
-        opts->im_format.planes = ibuf->planes;
-      }
-      else {
-        BKE_image_format_from_imbuf(&opts->im_format, ibuf);
-      }
-
-      /* use the multiview image settings as the default */
-      opts->im_format.stereo3d_format = *ima->stereo3d_format;
-      opts->im_format.views_format = ima->views_format;
-
-      BKE_image_format_color_management_copy_from_scene(&opts->im_format, scene);
-    }
-
-    opts->im_format.color_management = R_IMF_COLOR_MANAGEMENT_FOLLOW_SCENE;
-
-    if (ima->source == IMA_SRC_TILED) {
-      BLI_strncpy(opts->filepath, ima->filepath, sizeof(opts->filepath));
-      BLI_path_abs(opts->filepath, ID_BLEND_PATH_FROM_GLOBAL(&ima->id));
-    }
-    else {
-      BLI_strncpy(opts->filepath, ibuf->name, sizeof(opts->filepath));
-    }
-
-    /* sanitize all settings */
-
-    /* unlikely but just in case */
-    if (ELEM(opts->im_format.planes, R_IMF_PLANES_BW, R_IMF_PLANES_RGB, R_IMF_PLANES_RGBA) == 0) {
-      opts->im_format.planes = R_IMF_PLANES_RGBA;
-    }
-
-    /* depth, account for float buffer and format support */
- 

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list