[Bf-blender-cvs] [c2f75c7008d] master: Cleanup: make image saving code reusable outside operator.

Brecht Van Lommel noreply at git.blender.org
Thu Apr 18 19:55:22 CEST 2019


Commit: c2f75c7008d63fef453042c6bcf73b888d6eca39
Author: Brecht Van Lommel
Date:   Sat Feb 20 22:39:20 2016 +0100
Branches: master
https://developer.blender.org/rBc2f75c7008d63fef453042c6bcf73b888d6eca39

Cleanup: make image saving code reusable outside operator.

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

M	source/blender/editors/space_image/image_ops.c

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

diff --git a/source/blender/editors/space_image/image_ops.c b/source/blender/editors/space_image/image_ops.c
index 086f3103523..11b34ff70b6 100644
--- a/source/blender/editors/space_image/image_ops.c
+++ b/source/blender/editors/space_image/image_ops.c
@@ -1679,16 +1679,32 @@ void IMAGE_OT_replace(wmOperatorType *ot)
 /******************** save image as operator ********************/
 
 typedef struct {
-  /* matching scene->r settings */
-  //short planes, imtype, subimtype, quality;
+  /* Context within which image is saved. */
+  Main *bmain;
+  Scene *scene;
+
+  /* Matching scene->r settings */
   ImageFormatData im_format;
   char filepath[FILE_MAX]; /* keep absolute */
+
+  bool relative;
+  bool save_copy;
+  bool save_as_render;
+  bool do_newpath;
 } SaveImageOptions;
 
-static void save_image_options_defaults(SaveImageOptions *simopts)
+static void save_image_options_defaults(SaveImageOptions *simopts, Main *bmain, Scene *scene)
 {
+  simopts->bmain = bmain;
+  simopts->scene = scene;
+
   BKE_imformat_defaults(&simopts->im_format);
   simopts->filepath[0] = '\0';
+
+  simopts->relative = false;
+  simopts->save_copy = false;
+  simopts->save_as_render = false;
+  simopts->do_newpath = false;
 }
 
 static char imtype_best_depth(ImBuf *ibuf, const char imtype)
@@ -1732,16 +1748,16 @@ static char imtype_best_depth(ImBuf *ibuf, const char imtype)
 
 static int save_image_options_init(Main *bmain,
                                    SaveImageOptions *simopts,
-                                   SpaceImage *sima,
-                                   Scene *scene,
+                                   Image *ima,
+                                   ImageUser *iuser,
                                    const bool guess_path,
                                    const bool save_as_render)
 {
   void *lock;
-  ImBuf *ibuf = ED_space_image_acquire_buffer(sima, &lock);
+  ImBuf *ibuf = BKE_image_acquire_ibuf(ima, iuser, &lock);
 
   if (ibuf) {
-    Image *ima = sima->image;
+    Scene *scene = simopts->scene;
     bool is_depth_set = false;
 
     if (ELEM(ima->type, IMA_TYPE_R_RESULT, IMA_TYPE_COMPOSITE)) {
@@ -1817,7 +1833,7 @@ static int save_image_options_init(Main *bmain,
     BKE_color_managed_view_settings_copy(&simopts->im_format.view_settings, &scene->view_settings);
   }
 
-  ED_space_image_release_buffer(sima, ibuf, lock);
+  BKE_image_release_ibuf(ima, ibuf, lock);
 
   return (ibuf != NULL);
 }
@@ -1847,69 +1863,68 @@ static void save_image_options_to_op(SaveImageOptions *simopts, wmOperator *op)
   RNA_string_set(op->ptr, "filepath", simopts->filepath);
 }
 
-static void save_image_post(Main *bmain,
-                            wmOperator *op,
-                            ImBuf *ibuf,
+static void save_image_post(ReportList *reports,
+                            Main *bmain,
                             Image *ima,
+                            ImBuf *ibuf,
                             int ok,
+                            SaveImageOptions *simopts,
                             int save_copy,
-                            const char *relbase,
-                            int relative,
-                            int do_newpath,
                             const char *filepath)
 {
-  if (ok) {
-    if (!save_copy) {
-      ColorManagedColorspaceSettings old_colorspace_settings;
+  if (!ok) {
+    BKE_reportf(reports, RPT_ERROR, "Could not write image: %s", strerror(errno));
+    return;
+  }
 
-      if (do_newpath) {
-        BLI_strncpy(ibuf->name, filepath, sizeof(ibuf->name));
-        BLI_strncpy(ima->name, filepath, sizeof(ima->name));
-      }
+  if (save_copy) {
+    return;
+  }
 
-      ibuf->userflags &= ~IB_BITMAPDIRTY;
+  if (simopts->do_newpath) {
+    BLI_strncpy(ibuf->name, filepath, sizeof(ibuf->name));
+    BLI_strncpy(ima->name, filepath, sizeof(ima->name));
+  }
+
+  ibuf->userflags &= ~IB_BITMAPDIRTY;
 
-      /* change type? */
-      if (ima->type == IMA_TYPE_R_RESULT) {
-        ima->type = IMA_TYPE_IMAGE;
+  /* change type? */
+  if (ima->type == IMA_TYPE_R_RESULT) {
+    ima->type = IMA_TYPE_IMAGE;
 
-        /* workaround to ensure the render result buffer is no longer used
+    /* workaround to ensure the render result buffer is no longer used
          * by this image, otherwise can crash when a new render result is
          * created. */
-        if (ibuf->rect && !(ibuf->mall & IB_rect)) {
-          imb_freerectImBuf(ibuf);
-        }
-        if (ibuf->rect_float && !(ibuf->mall & IB_rectfloat)) {
-          imb_freerectfloatImBuf(ibuf);
-        }
-        if (ibuf->zbuf && !(ibuf->mall & IB_zbuf)) {
-          IMB_freezbufImBuf(ibuf);
-        }
-        if (ibuf->zbuf_float && !(ibuf->mall & IB_zbuffloat)) {
-          IMB_freezbuffloatImBuf(ibuf);
-        }
-      }
-      if (ELEM(ima->source, IMA_SRC_GENERATED, IMA_SRC_VIEWER)) {
-        ima->source = IMA_SRC_FILE;
-        ima->type = IMA_TYPE_IMAGE;
-      }
-
-      /* only image path, never ibuf */
-      if (relative) {
-        BLI_path_rel(ima->name, relbase); /* only after saving */
-      }
-
-      BKE_color_managed_colorspace_settings_copy(&old_colorspace_settings,
-                                                 &ima->colorspace_settings);
-      IMB_colormanagement_colorspace_from_ibuf_ftype(&ima->colorspace_settings, ibuf);
-      if (!BKE_color_managed_colorspace_settings_equals(&old_colorspace_settings,
-                                                        &ima->colorspace_settings)) {
-        BKE_image_signal(bmain, ima, NULL, IMA_SIGNAL_COLORMANAGE);
-      }
+    if (ibuf->rect && !(ibuf->mall & IB_rect)) {
+      imb_freerectImBuf(ibuf);
+    }
+    if (ibuf->rect_float && !(ibuf->mall & IB_rectfloat)) {
+      imb_freerectfloatImBuf(ibuf);
+    }
+    if (ibuf->zbuf && !(ibuf->mall & IB_zbuf)) {
+      IMB_freezbufImBuf(ibuf);
+    }
+    if (ibuf->zbuf_float && !(ibuf->mall & IB_zbuffloat)) {
+      IMB_freezbuffloatImBuf(ibuf);
     }
   }
-  else {
-    BKE_reportf(op->reports, RPT_ERROR, "Could not write image: %s", strerror(errno));
+  if (ELEM(ima->source, IMA_SRC_GENERATED, IMA_SRC_VIEWER)) {
+    ima->source = IMA_SRC_FILE;
+    ima->type = IMA_TYPE_IMAGE;
+  }
+
+  /* only image path, never ibuf */
+  if (simopts->relative) {
+    const char *relbase = ID_BLEND_PATH(simopts->bmain, &ima->id);
+    BLI_path_rel(ima->name, relbase); /* only after saving */
+  }
+
+  ColorManagedColorspaceSettings old_colorspace_settings;
+  BKE_color_managed_colorspace_settings_copy(&old_colorspace_settings, &ima->colorspace_settings);
+  IMB_colormanagement_colorspace_from_ibuf_ftype(&ima->colorspace_settings, ibuf);
+  if (!BKE_color_managed_colorspace_settings_equals(&old_colorspace_settings,
+                                                    &ima->colorspace_settings)) {
+    BKE_image_signal(bmain, ima, NULL, IMA_SIGNAL_COLORMANAGE);
   }
 }
 
@@ -1934,263 +1949,283 @@ static void save_imbuf_post(ImBuf *ibuf, ImBuf *colormanaged_ibuf)
  * \note for multiview the first ``ibuf`` is important to get the settings.
  */
 static bool save_image_doit(
-    bContext *C, SpaceImage *sima, wmOperator *op, SaveImageOptions *simopts, bool do_newpath)
+    ReportList *reports, Main *bmain, Image *ima, ImageUser *iuser, SaveImageOptions *simopts)
 {
-  Main *bmain = CTX_data_main(C);
-  Image *ima = ED_space_image(sima);
   void *lock;
-  ImBuf *ibuf = ED_space_image_acquire_buffer(sima, &lock);
-  Scene *scene;
+  ImBuf *ibuf = BKE_image_acquire_ibuf(ima, iuser, &lock);
   RenderResult *rr = NULL;
   bool ok = false;
 
-  WM_cursor_wait(1);
+  if (ibuf == NULL || (ibuf->rect == NULL && ibuf->rect_float == NULL)) {
+    BKE_image_release_ibuf(ima, ibuf, &lock);
+    goto cleanup;
+  }
 
-  if (ibuf) {
-    ImBuf *colormanaged_ibuf = NULL;
-    const char *relbase = ID_BLEND_PATH(CTX_data_main(C), &ima->id);
-    const bool relative = (RNA_struct_find_property(op->ptr, "relative_path") &&
-                           RNA_boolean_get(op->ptr, "relative_path"));
-    const bool save_copy = (RNA_struct_find_property(op->ptr, "copy") &&
-                            RNA_boolean_get(op->ptr, "copy"));
-    const bool save_as_render = (RNA_struct_find_property(op->ptr, "save_as_render") &&
-                                 RNA_boolean_get(op->ptr, "save_as_render"));
-    ImageFormatData *imf = &simopts->im_format;
-
-    /* old global to ensure a 2nd save goes to same dir */
-    BLI_strncpy(G.ima, simopts->filepath, sizeof(G.ima));
-
-    if (ima->type == IMA_TYPE_R_RESULT) {
-      /* enforce user setting for RGB or RGBA, but skip BW */
-      if (simopts->im_format.planes == R_IMF_PLANES_RGBA) {
-        ibuf->planes = R_IMF_PLANES_RGBA;
-      }
-      else if (simopts->im_format.planes == R_IMF_PLANES_RGB) {
-        ibuf->planes = R_IMF_PLANES_RGB;
-      }
+  ImBuf *colormanaged_ibuf = NULL;
+  const bool save_copy = simopts->save_copy;
+  const bool save_as_render = simopts->save_as_render;
+  ImageFormatData *imf = &simopts->im_format;
+
+  if (ima->type == IMA_TYPE_R_RESULT) {
+    /* enforce user setting for RGB or RGBA, but skip BW */
+    if (simopts->im_format.planes == R_IMF_PLANES_RGBA) {
+      ibuf->planes = R_IMF_PLANES_RGBA;
     }
-    else {
-      /* TODO, better solution, if a 24bit image is painted onto it may contain alpha */
-      if ((simopts->im_format.planes == R_IMF_PLANES_RGBA) &&
-          /* it has been painted onto */
-          (ibuf->userflags & IB_BITMAPDIRTY)) {
-        /* checks each pixel, not ideal */
-        ibuf->planes = BKE_imbuf_alpha_test(ibuf) ? R_IMF_PLANES_RGBA : R_IMF_PLANES_RGB;
-      }
+    else if (simopts->im_format.planes == R_IMF_PLANES_RGB) {
+      ibuf->planes = R_IMF_PLANES_RGB;
     }
+  }
+  else {
+    /* TODO, better solution, if a 24bit image is painted onto it may contain alpha */
+    if ((simopts->im_format.planes == R_IMF_PLANES_RGBA) &&
+        /* it has been painted onto */
+        (ibuf->userflags & IB_BITMAPDIRTY)) {
+      /* checks each pixel, not ideal */
+      ibuf->planes = BKE_imbuf_alpha_test(ibuf) ? R_IMF_PLANES_RGBA : R_IMF_PLANES_RGB;
+    }
+  }
 
-    /* we need renderresult for exr and rendered multiview */
-    scene = CTX_data_scene(C);
-    rr = 

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list