[Bf-blender-cvs] [b155ece0c0e] master: Image: add resize operator

Campbell Barton noreply at git.blender.org
Tue Oct 1 16:49:51 CEST 2019


Commit: b155ece0c0e5feb5f51690989687d4b825cd848c
Author: Campbell Barton
Date:   Wed Oct 2 00:24:21 2019 +1000
Branches: master
https://developer.blender.org/rBb155ece0c0e5feb5f51690989687d4b825cd848c

Image: add resize operator

Now possible with new image undo,
was added for testing but seems generally useful.

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

M	release/scripts/startup/bl_ui/space_image.py
M	source/blender/editors/space_image/image_intern.h
M	source/blender/editors/space_image/image_ops.c
M	source/blender/editors/space_image/space_image.c

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

diff --git a/release/scripts/startup/bl_ui/space_image.py b/release/scripts/startup/bl_ui/space_image.py
index 0044475d67d..aaa73b2c864 100644
--- a/release/scripts/startup/bl_ui/space_image.py
+++ b/release/scripts/startup/bl_ui/space_image.py
@@ -240,6 +240,7 @@ class IMAGE_MT_image(Menu):
             layout.separator()
 
             layout.menu("IMAGE_MT_image_invert")
+            layout.operator("image.resize", text="Resize")
 
         if ima and not show_render:
             if ima.packed_file:
diff --git a/source/blender/editors/space_image/image_intern.h b/source/blender/editors/space_image/image_intern.h
index 6f1ce01ceac..f8ce065d46c 100644
--- a/source/blender/editors/space_image/image_intern.h
+++ b/source/blender/editors/space_image/image_intern.h
@@ -72,6 +72,7 @@ void IMAGE_OT_pack(struct wmOperatorType *ot);
 void IMAGE_OT_unpack(struct wmOperatorType *ot);
 
 void IMAGE_OT_invert(struct wmOperatorType *ot);
+void IMAGE_OT_resize(struct wmOperatorType *ot);
 
 void IMAGE_OT_cycle_render_slot(struct wmOperatorType *ot);
 void IMAGE_OT_clear_render_slot(struct wmOperatorType *ot);
diff --git a/source/blender/editors/space_image/image_ops.c b/source/blender/editors/space_image/image_ops.c
index 5fa51170f0e..b9d5c927092 100644
--- a/source/blender/editors/space_image/image_ops.c
+++ b/source/blender/editors/space_image/image_ops.c
@@ -213,6 +213,13 @@ static ImageUser *image_user_from_context(const bContext *C)
   }
 }
 
+static bool image_buffer_exists_from_context_no_image_user(bContext *C)
+{
+  Image *ima = image_from_context(C);
+
+  return BKE_image_has_ibuf(ima, NULL);
+}
+
 static bool image_buffer_exists_from_context(bContext *C)
 {
   Image *ima = image_from_context(C);
@@ -2753,13 +2760,6 @@ void IMAGE_OT_new(wmOperatorType *ot)
 /** \name Invert Operators
  * \{ */
 
-static bool image_invert_poll(bContext *C)
-{
-  Image *ima = image_from_context(C);
-
-  return BKE_image_has_ibuf(ima, NULL);
-}
-
 static int image_invert_exec(bContext *C, wmOperator *op)
 {
   Image *ima = image_from_context(C);
@@ -2862,7 +2862,7 @@ void IMAGE_OT_invert(wmOperatorType *ot)
 
   /* api callbacks */
   ot->exec = image_invert_exec;
-  ot->poll = image_invert_poll;
+  ot->poll = image_buffer_exists_from_context_no_image_user;
 
   /* properties */
   prop = RNA_def_boolean(ot->srna, "invert_r", 0, "Red", "Invert Red Channel");
@@ -2880,6 +2880,88 @@ void IMAGE_OT_invert(wmOperatorType *ot)
 
 /** \} */
 
+/* -------------------------------------------------------------------- */
+/** \name Scale Operator
+ * \{ */
+
+static int image_scale_invoke(bContext *C, wmOperator *op, const wmEvent *UNUSED(event))
+{
+  Image *ima = image_from_context(C);
+  PropertyRNA *prop = RNA_struct_find_property(op->ptr, "size");
+  if (!RNA_property_is_set(op->ptr, prop)) {
+    ImBuf *ibuf = BKE_image_acquire_ibuf(ima, NULL, NULL);
+    const int size[2] = {ibuf->x, ibuf->y};
+    RNA_property_int_set_array(op->ptr, prop, size);
+    BKE_image_release_ibuf(ima, ibuf, NULL);
+  }
+  return WM_operator_props_dialog_popup(C, op, 200, 200);
+}
+
+static int image_scale_exec(bContext *C, wmOperator *op)
+{
+  Image *ima = image_from_context(C);
+  ImBuf *ibuf = BKE_image_acquire_ibuf(ima, NULL, NULL);
+  SpaceImage *sima = CTX_wm_space_image(C);
+  const bool is_paint = ((sima != NULL) && (sima->mode == SI_MODE_PAINT));
+
+  if (ibuf == NULL) {
+    /* TODO: this should actually never happen, but does for render-results -> cleanup */
+    return OPERATOR_CANCELLED;
+  }
+
+  if (is_paint) {
+    ED_imapaint_clear_partial_redraw();
+  }
+
+  PropertyRNA *prop = RNA_struct_find_property(op->ptr, "size");
+  int size[2];
+  if (RNA_property_is_set(op->ptr, prop)) {
+    RNA_property_int_get_array(op->ptr, prop, size);
+  }
+  else {
+    size[0] = ibuf->x;
+    size[1] = ibuf->y;
+    RNA_property_int_set_array(op->ptr, prop, size);
+  }
+
+  ED_image_undo_push_begin_with_image(op->type->name, ima, ibuf);
+
+  ibuf->userflags |= IB_DISPLAY_BUFFER_INVALID;
+  IMB_scaleImBuf(ibuf, size[0], size[1]);
+  BKE_image_release_ibuf(ima, ibuf, NULL);
+
+  ED_image_undo_push_end();
+
+  /* force GPU reupload, all image is invalid */
+  GPU_free_image(ima);
+
+  DEG_id_tag_update(&ima->id, 0);
+  WM_event_add_notifier(C, NC_IMAGE | NA_EDITED, ima);
+
+  return OPERATOR_FINISHED;
+}
+
+void IMAGE_OT_resize(wmOperatorType *ot)
+{
+  /* identifiers */
+  ot->name = "Resize Image";
+  ot->idname = "IMAGE_OT_resize";
+  ot->description = "Resize the image";
+
+  /* api callbacks */
+  ot->invoke = image_scale_invoke;
+  ot->exec = image_scale_exec;
+  ot->poll = image_buffer_exists_from_context_no_image_user;
+
+  /* properties */
+  RNA_def_int_vector(ot->srna, "size", 2, NULL, 1, INT_MAX, "Size", "", 1, SHRT_MAX);
+
+  /* flags */
+  ot->flag = OPTYPE_REGISTER;
+}
+
+/** \} */
+
 /* -------------------------------------------------------------------- */
 /** \name Pack Operator
  * \{ */
diff --git a/source/blender/editors/space_image/space_image.c b/source/blender/editors/space_image/space_image.c
index 749266149c1..a88ecc91868 100644
--- a/source/blender/editors/space_image/space_image.c
+++ b/source/blender/editors/space_image/space_image.c
@@ -230,6 +230,7 @@ static void image_operatortypes(void)
   WM_operatortype_append(IMAGE_OT_unpack);
 
   WM_operatortype_append(IMAGE_OT_invert);
+  WM_operatortype_append(IMAGE_OT_resize);
 
   WM_operatortype_append(IMAGE_OT_cycle_render_slot);
   WM_operatortype_append(IMAGE_OT_clear_render_slot);



More information about the Bf-blender-cvs mailing list