Image: add resize operator

Now possible with new image undo,
was added for testing but seems generally useful.
This commit is contained in:
Campbell Barton
2019-10-02 00:24:21 +10:00
parent 151cc02b6f
commit b155ece0c0
4 changed files with 93 additions and 8 deletions

View File

@@ -240,6 +240,7 @@ class IMAGE_MT_image(Menu):
layout.separator() layout.separator()
layout.menu("IMAGE_MT_image_invert") layout.menu("IMAGE_MT_image_invert")
layout.operator("image.resize", text="Resize")
if ima and not show_render: if ima and not show_render:
if ima.packed_file: if ima.packed_file:

View File

@@ -72,6 +72,7 @@ void IMAGE_OT_pack(struct wmOperatorType *ot);
void IMAGE_OT_unpack(struct wmOperatorType *ot); void IMAGE_OT_unpack(struct wmOperatorType *ot);
void IMAGE_OT_invert(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_cycle_render_slot(struct wmOperatorType *ot);
void IMAGE_OT_clear_render_slot(struct wmOperatorType *ot); void IMAGE_OT_clear_render_slot(struct wmOperatorType *ot);

View File

@@ -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) static bool image_buffer_exists_from_context(bContext *C)
{ {
Image *ima = image_from_context(C); Image *ima = image_from_context(C);
@@ -2753,13 +2760,6 @@ void IMAGE_OT_new(wmOperatorType *ot)
/** \name Invert Operators /** \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) static int image_invert_exec(bContext *C, wmOperator *op)
{ {
Image *ima = image_from_context(C); Image *ima = image_from_context(C);
@@ -2862,7 +2862,7 @@ void IMAGE_OT_invert(wmOperatorType *ot)
/* api callbacks */ /* api callbacks */
ot->exec = image_invert_exec; ot->exec = image_invert_exec;
ot->poll = image_invert_poll; ot->poll = image_buffer_exists_from_context_no_image_user;
/* properties */ /* properties */
prop = RNA_def_boolean(ot->srna, "invert_r", 0, "Red", "Invert Red Channel"); 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 /** \name Pack Operator
* \{ */ * \{ */

View File

@@ -230,6 +230,7 @@ static void image_operatortypes(void)
WM_operatortype_append(IMAGE_OT_unpack); WM_operatortype_append(IMAGE_OT_unpack);
WM_operatortype_append(IMAGE_OT_invert); 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_cycle_render_slot);
WM_operatortype_append(IMAGE_OT_clear_render_slot); WM_operatortype_append(IMAGE_OT_clear_render_slot);