[Bf-blender-cvs] [29a3f43da5d] master: Python API: make Image.save and Image.save_render more consistent with operator

Brecht Van Lommel noreply at git.blender.org
Mon May 16 15:22:25 CEST 2022


Commit: 29a3f43da5d3423769f4871baad900a6e2f68942
Author: Brecht Van Lommel
Date:   Mon May 16 13:42:04 2022 +0200
Branches: master
https://developer.blender.org/rB29a3f43da5d3423769f4871baad900a6e2f68942

Python API: make Image.save and Image.save_render more consistent with operator

Previously these only supported a subset of what the save operator could do,
for example no multilayer or stereo saving, no proper color management. Now
share code with the image save operator so it's more consistent.

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

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
M	source/blender/makesrna/intern/rna_image_api.c

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

diff --git a/source/blender/blenkernel/BKE_image_save.h b/source/blender/blenkernel/BKE_image_save.h
index 673a7dffb82..b5db15bb174 100644
--- a/source/blender/blenkernel/BKE_image_save.h
+++ b/source/blender/blenkernel/BKE_image_save.h
@@ -46,8 +46,7 @@ bool BKE_image_save_options_init(ImageSaveOptions *opts,
                                  struct Scene *scene,
                                  struct Image *ima,
                                  struct ImageUser *iuser,
-                                 const bool guess_path,
-                                 const bool save_as_render);
+                                 const bool guess_path);
 void BKE_image_save_options_update(struct ImageSaveOptions *opts, struct Image *ima);
 void BKE_image_save_options_free(struct ImageSaveOptions *opts);
 
diff --git a/source/blender/blenkernel/intern/image_save.cc b/source/blender/blenkernel/intern/image_save.cc
index a4c48c90d17..46407f622ba 100644
--- a/source/blender/blenkernel/intern/image_save.cc
+++ b/source/blender/blenkernel/intern/image_save.cc
@@ -78,14 +78,21 @@ bool BKE_image_save_options_init(ImageSaveOptions *opts,
                                  Scene *scene,
                                  Image *ima,
                                  ImageUser *iuser,
-                                 const bool guess_path,
-                                 const bool save_as_render)
+                                 const bool guess_path)
 {
+  /* For saving a tiled image we need an iuser, so use a local one if there isn't already one. */
+  ImageUser save_iuser;
+  if (iuser == nullptr) {
+    BKE_imageuser_default(&save_iuser);
+    iuser = &save_iuser;
+    iuser->scene = scene;
+  }
+
   memset(opts, 0, sizeof(*opts));
 
   opts->bmain = bmain;
   opts->scene = scene;
-  opts->save_as_render = save_as_render;
+  opts->save_as_render = ima->source == IMA_SRC_VIEWER;
 
   BKE_image_format_init(&opts->im_format, false);
 
@@ -126,7 +133,7 @@ bool BKE_image_save_options_init(ImageSaveOptions *opts,
     }
 
     /* Default to saving in the same colorspace as the image setting. */
-    if (!save_as_render) {
+    if (!opts->save_as_render) {
       BKE_color_managed_colorspace_settings_copy(&opts->im_format.linear_colorspace_settings,
                                                  &ima->colorspace_settings);
     }
@@ -161,7 +168,7 @@ bool BKE_image_save_options_init(ImageSaveOptions *opts,
     /* check for empty path */
     if (guess_path && opts->filepath[0] == 0) {
       const bool is_prev_save = !STREQ(G.ima, "//");
-      if (save_as_render) {
+      if (opts->save_as_render) {
         if (is_prev_save) {
           BLI_strncpy(opts->filepath, G.ima, sizeof(opts->filepath));
         }
@@ -596,8 +603,13 @@ static bool image_save_single(ReportList *reports,
 bool BKE_image_save(
     ReportList *reports, Main *bmain, Image *ima, ImageUser *iuser, ImageSaveOptions *opts)
 {
+  /* For saving a tiled image we need an iuser, so use a local one if there isn't already one. */
   ImageUser save_iuser;
-  BKE_imageuser_default(&save_iuser);
+  if (iuser == nullptr) {
+    BKE_imageuser_default(&save_iuser);
+    iuser = &save_iuser;
+    iuser->scene = opts->scene;
+  }
 
   bool colorspace_changed = false;
 
@@ -614,12 +626,6 @@ bool BKE_image_save(
                   opts->filepath);
       return false;
     }
-
-    /* For saving a tiled image we need an iuser, so use a local one if there isn't already one.
-     */
-    if (iuser == nullptr) {
-      iuser = &save_iuser;
-    }
   }
 
   /* Save images */
diff --git a/source/blender/editors/space_image/image_ops.c b/source/blender/editors/space_image/image_ops.c
index 01be2ef22d5..336331e44e7 100644
--- a/source/blender/editors/space_image/image_ops.c
+++ b/source/blender/editors/space_image/image_ops.c
@@ -1722,13 +1722,12 @@ static ImageSaveData *image_save_as_init(bContext *C, wmOperator *op)
   Image *image = image_from_context(C);
   ImageUser *iuser = image_user_from_context(C);
   Scene *scene = CTX_data_scene(C);
-  const bool save_as_render = (image->source == IMA_SRC_VIEWER);
 
   ImageSaveData *isd = MEM_callocN(sizeof(*isd), __func__);
   isd->image = image;
   isd->iuser = iuser;
 
-  if (!BKE_image_save_options_init(&isd->opts, bmain, scene, image, iuser, true, save_as_render)) {
+  if (!BKE_image_save_options_init(&isd->opts, bmain, scene, image, iuser, true)) {
     BKE_image_save_options_free(&isd->opts);
     MEM_freeN(isd);
     return NULL;
@@ -1995,7 +1994,7 @@ static int image_save_exec(bContext *C, wmOperator *op)
     return OPERATOR_FINISHED;
   }
 
-  if (!BKE_image_save_options_init(&opts, bmain, scene, image, iuser, false, false)) {
+  if (!BKE_image_save_options_init(&opts, bmain, scene, image, iuser, false)) {
     BKE_image_save_options_free(&opts);
     return OPERATOR_CANCELLED;
   }
@@ -2267,7 +2266,7 @@ bool ED_image_save_all_modified(const bContext *C, ReportList *reports)
         if (image_has_valid_path(ima)) {
           ImageSaveOptions opts;
           Scene *scene = CTX_data_scene(C);
-          if (!BKE_image_save_options_init(&opts, bmain, scene, ima, NULL, false, false)) {
+          if (!BKE_image_save_options_init(&opts, bmain, scene, ima, NULL, false)) {
             bool saved_successfully = BKE_image_save(reports, bmain, ima, NULL, &opts);
             ok = ok && saved_successfully;
           }
diff --git a/source/blender/makesrna/intern/rna_image_api.c b/source/blender/makesrna/intern/rna_image_api.c
index 7bd2040dab0..b385f576585 100644
--- a/source/blender/makesrna/intern/rna_image_api.c
+++ b/source/blender/makesrna/intern/rna_image_api.c
@@ -26,6 +26,7 @@
 
 #  include "BKE_image.h"
 #  include "BKE_image_format.h"
+#  include "BKE_image_save.h"
 #  include "BKE_main.h"
 #  include "BKE_scene.h"
 #  include <errno.h>
@@ -50,77 +51,40 @@ static void rna_ImagePackedFile_save(ImagePackedFile *imapf, Main *bmain, Report
 static void rna_Image_save_render(
     Image *image, bContext *C, ReportList *reports, const char *path, Scene *scene)
 {
-  ImBuf *ibuf;
+  Main *bmain = CTX_data_main(C);
 
   if (scene == NULL) {
     scene = CTX_data_scene(C);
   }
 
-  if (scene) {
-    ImageUser iuser = {NULL};
-    void *lock;
+  ImageSaveOptions opts;
 
-    iuser.scene = scene;
+  if (BKE_image_save_options_init(&opts, bmain, scene, image, NULL, false)) {
+    opts.save_copy = true;
+    opts.save_as_render = true;
+    STRNCPY(opts.filepath, path);
 
-    ibuf = BKE_image_acquire_ibuf(image, &iuser, &lock);
-
-    if (ibuf == NULL) {
-      BKE_report(reports, RPT_ERROR, "Could not acquire buffer from image");
-    }
-    else {
-      ImBuf *write_ibuf;
-
-      ImageFormatData image_format;
-      BKE_image_format_init_for_write(&image_format, scene, NULL);
-
-      write_ibuf = IMB_colormanagement_imbuf_for_write(ibuf, true, true, &image_format);
-
-      write_ibuf->planes = image_format.planes;
-      write_ibuf->dither = scene->r.dither_intensity;
-
-      if (!BKE_imbuf_write(write_ibuf, path, &image_format)) {
-        BKE_reportf(reports, RPT_ERROR, "Could not write image: %s, '%s'", strerror(errno), path);
-      }
-
-      if (write_ibuf != ibuf) {
-        IMB_freeImBuf(write_ibuf);
-      }
-
-      BKE_image_format_free(&image_format);
+    if (!BKE_image_save(reports, bmain, image, NULL, &opts)) {
+      BKE_reportf(
+          reports, RPT_ERROR, "Image '%s' could not be saved to '%s'", image->id.name + 2, path);
     }
-
-    BKE_image_release_ibuf(image, ibuf, lock);
   }
   else {
-    BKE_report(reports, RPT_ERROR, "Scene not in context, could not get save parameters");
+    BKE_reportf(reports, RPT_ERROR, "Image '%s' does not have any image data", image->id.name + 2);
   }
+
+  BKE_image_save_options_free(&opts);
+
+  WM_event_add_notifier(C, NC_IMAGE | NA_EDITED, image);
 }
 
 static void rna_Image_save(Image *image, Main *bmain, bContext *C, ReportList *reports)
 {
-  void *lock;
-
-  ImBuf *ibuf = BKE_image_acquire_ibuf(image, NULL, &lock);
-  if (ibuf) {
-    char filepath[FILE_MAX];
-    BLI_strncpy(filepath, image->filepath, sizeof(filepath));
-    BLI_path_abs(filepath, ID_BLEND_PATH(bmain, &image->id));
-
-    /* NOTE: we purposefully ignore packed files here,
-     * developers need to explicitly write them via 'packed_files' */
+  Scene *scene = CTX_data_scene(C);
+  ImageSaveOptions opts;
 
-    if (IMB_saveiff(ibuf, filepath, ibuf->flags)) {
-      image->type = IMA_TYPE_IMAGE;
-
-      if (image->source == IMA_SRC_GENERATED) {
-        image->source = IMA_SRC_FILE;
-      }
-
-      IMB_colormanagement_colorspace_from_ibuf_ftype(&image->colorspace_settings, ibuf);
-
-      ibuf->userflags &= ~IB_BITMAPDIRTY;
-    }
-    else {
+  if (BKE_image_save_options_init(&opts, bmain, scene, image, NULL, false)) {
+    if (!BKE_image_save(reports, bmain, image, NULL, &opts)) {
       BKE_reportf(reports,
                   RPT_ERROR,
                   "Image '%s' could not be saved to '%s'",
@@ -132,7 +96,8 @@ static void rna_Image_save(Image *image, Main *bmain, bContext *C, ReportList *r
     BKE_reportf(reports, RPT_ERROR, "Image '%s' does not have any image data", image->id.name + 2);
   }
 
-  BKE_image_release_ibuf(image, ibuf, lock);
+  BKE_image_save_options_free(&opts);
+
   WM_event_add_notifier(C, NC_IMAGE | NA_EDITED, image);
 }



More information about the Bf-blender-cvs mailing list