2 * ***** BEGIN GPL LICENSE BLOCK *****
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19 * All rights reserved.
21 * Contributor(s): Blender Foundation, 2002-2009, Xavier Thomas
23 * ***** END GPL LICENSE BLOCK *****
26 /** \file blender/editors/space_image/image_ops.c
36 #include "MEM_guardedalloc.h"
39 #include "BLI_blenlib.h"
40 #include "BLI_utildefines.h"
42 #include "BLF_translation.h"
44 #include "DNA_object_types.h"
45 #include "DNA_node_types.h"
46 #include "DNA_packedFile_types.h"
47 #include "DNA_scene_types.h"
49 #include "BKE_colortools.h"
50 #include "BKE_context.h"
51 #include "BKE_image.h"
52 #include "BKE_global.h"
53 #include "BKE_library.h"
56 #include "BKE_packedFile.h"
57 #include "BKE_report.h"
58 #include "BKE_screen.h"
60 #include "IMB_colormanagement.h"
61 #include "IMB_imbuf.h"
62 #include "IMB_imbuf_types.h"
64 #include "RE_pipeline.h"
66 #include "RNA_access.h"
67 #include "RNA_define.h"
68 #include "RNA_enum_types.h"
71 #include "ED_render.h"
72 #include "ED_screen.h"
73 #include "ED_space_api.h"
74 #include "ED_uvedit.h"
77 #include "UI_interface.h"
78 #include "UI_resources.h"
79 #include "UI_view2d.h"
86 #include "image_intern.h"
88 /******************** view navigation utilities *********************/
90 static void sima_zoom_set(SpaceImage *sima, ARegion *ar, float zoom, float location[2])
92 float oldzoom = sima->zoom;
97 if (sima->zoom < 0.1f || sima->zoom > 4.0f) {
98 /* check zoom limits */
99 ED_space_image_get_size(sima, &width, &height);
102 height *= sima->zoom;
104 if ((width < 4) && (height < 4))
105 sima->zoom = oldzoom;
106 else if (BLI_rcti_size_x(&ar->winrct) <= sima->zoom)
107 sima->zoom = oldzoom;
108 else if (BLI_rcti_size_y(&ar->winrct) <= sima->zoom)
109 sima->zoom = oldzoom;
112 if ((U.uiflag & USER_ZOOM_TO_MOUSEPOS) && location) {
113 float aspx, aspy, w, h;
115 ED_space_image_get_size(sima, &width, &height);
116 ED_space_image_get_aspect(sima, &aspx, &aspy);
121 sima->xof += ((location[0] - 0.5f) * w - sima->xof) * (sima->zoom - oldzoom) / sima->zoom;
122 sima->yof += ((location[1] - 0.5f) * h - sima->yof) * (sima->zoom - oldzoom) / sima->zoom;
126 static void sima_zoom_set_factor(SpaceImage *sima, ARegion *ar, float zoomfac, float location[2])
128 sima_zoom_set(sima, ar, sima->zoom * zoomfac, location);
131 #if 0 // currently unused
132 static int image_poll(bContext *C)
134 return (CTX_data_edit_image(C) != NULL);
138 static int space_image_buffer_exists_poll(bContext *C)
140 SpaceImage *sima = CTX_wm_space_image(C);
141 if (sima && sima->spacetype == SPACE_IMAGE)
142 if (ED_space_image_has_buffer(sima))
147 static int space_image_file_exists_poll(bContext *C)
149 if (space_image_buffer_exists_poll(C)) {
150 Main *bmain = CTX_data_main(C);
151 SpaceImage *sima = CTX_wm_space_image(C);
157 ibuf = ED_space_image_acquire_buffer(sima, &lock);
159 BLI_strncpy(name, ibuf->name, FILE_MAX);
160 BLI_path_abs(name, bmain->name);
162 if (BLI_exists(name) == FALSE) {
163 CTX_wm_operator_poll_msg_set(C, "image file not found");
165 else if (BLI_file_is_writable(name) == FALSE) {
166 CTX_wm_operator_poll_msg_set(C, "image path can't be written to");
172 ED_space_image_release_buffer(sima, ibuf, lock);
179 static int space_image_poll(bContext *C)
181 SpaceImage *sima = CTX_wm_space_image(C);
182 if (sima && sima->spacetype == SPACE_IMAGE && sima->image)
187 int space_image_main_area_poll(bContext *C)
189 SpaceImage *sima = CTX_wm_space_image(C);
190 // XXX ARegion *ar = CTX_wm_region(C);
193 return 1; // XXX (ar && ar->type->regionid == RGN_TYPE_WINDOW);
198 /* For IMAGE_OT_curves_point_set to avoid sampling when in uv smooth mode or editmode */
199 static int space_image_main_area_not_uv_brush_poll(bContext *C)
201 SpaceImage *sima = CTX_wm_space_image(C);
202 Scene *scene = CTX_data_scene(C);
203 ToolSettings *toolsettings = scene->toolsettings;
205 if (sima && !toolsettings->uvsculpt && !scene->obedit)
211 static int image_sample_poll(bContext *C)
213 SpaceImage *sima = CTX_wm_space_image(C);
215 Scene *scene = CTX_data_scene(C);
216 Object *obedit = CTX_data_edit_object(C);
217 ToolSettings *toolsettings = scene->toolsettings;
220 if (ED_space_image_show_uvedit(sima, obedit) && (toolsettings->use_uv_sculpt))
223 else if (sima->mode != SI_MODE_VIEW) {
227 return space_image_main_area_poll(C);
233 /********************** view pan operator *********************/
235 typedef struct ViewPanData {
241 static void image_view_pan_init(bContext *C, wmOperator *op, wmEvent *event)
243 SpaceImage *sima = CTX_wm_space_image(C);
246 op->customdata = vpd = MEM_callocN(sizeof(ViewPanData), "ImageViewPanData");
247 WM_cursor_modal(CTX_wm_window(C), BC_NSEW_SCROLLCURSOR);
251 vpd->xof = sima->xof;
252 vpd->yof = sima->yof;
253 vpd->event_type = event->type;
255 WM_event_add_modal_handler(C, op);
258 static void image_view_pan_exit(bContext *C, wmOperator *op, int cancel)
260 SpaceImage *sima = CTX_wm_space_image(C);
261 ViewPanData *vpd = op->customdata;
264 sima->xof = vpd->xof;
265 sima->yof = vpd->yof;
266 ED_region_tag_redraw(CTX_wm_region(C));
269 WM_cursor_restore(CTX_wm_window(C));
270 MEM_freeN(op->customdata);
273 static int image_view_pan_exec(bContext *C, wmOperator *op)
275 SpaceImage *sima = CTX_wm_space_image(C);
278 RNA_float_get_array(op->ptr, "offset", offset);
279 sima->xof += offset[0];
280 sima->yof += offset[1];
282 ED_region_tag_redraw(CTX_wm_region(C));
286 if (image_preview_active(curarea, NULL, NULL)) {
287 /* recalculates new preview rect */
288 scrarea_do_windraw(curarea);
289 image_preview_event(2);
293 return OPERATOR_FINISHED;
296 static int image_view_pan_invoke(bContext *C, wmOperator *op, wmEvent *event)
298 if (event->type == MOUSEPAN) {
299 SpaceImage *sima = CTX_wm_space_image(C);
302 offset[0] = (event->prevx - event->x) / sima->zoom;
303 offset[1] = (event->prevy - event->y) / sima->zoom;
304 RNA_float_set_array(op->ptr, "offset", offset);
306 image_view_pan_exec(C, op);
307 return OPERATOR_FINISHED;
310 image_view_pan_init(C, op, event);
311 return OPERATOR_RUNNING_MODAL;
315 static int image_view_pan_modal(bContext *C, wmOperator *op, wmEvent *event)
317 SpaceImage *sima = CTX_wm_space_image(C);
318 ViewPanData *vpd = op->customdata;
321 switch (event->type) {
323 sima->xof = vpd->xof;
324 sima->yof = vpd->yof;
325 offset[0] = (vpd->x - event->x) / sima->zoom;
326 offset[1] = (vpd->y - event->y) / sima->zoom;
327 RNA_float_set_array(op->ptr, "offset", offset);
328 image_view_pan_exec(C, op);
331 if (event->type == vpd->event_type && event->val == KM_RELEASE) {
332 image_view_pan_exit(C, op, 0);
333 return OPERATOR_FINISHED;
338 return OPERATOR_RUNNING_MODAL;
341 static int image_view_pan_cancel(bContext *C, wmOperator *op)
343 image_view_pan_exit(C, op, 1);
344 return OPERATOR_CANCELLED;
347 void IMAGE_OT_view_pan(wmOperatorType *ot)
350 ot->name = "View Pan";
351 ot->idname = "IMAGE_OT_view_pan";
352 ot->description = "Pan the view";
355 ot->exec = image_view_pan_exec;
356 ot->invoke = image_view_pan_invoke;
357 ot->modal = image_view_pan_modal;
358 ot->cancel = image_view_pan_cancel;
359 ot->poll = space_image_main_area_poll;
362 ot->flag = OPTYPE_BLOCKING | OPTYPE_GRAB_POINTER;
365 RNA_def_float_vector(ot->srna, "offset", 2, NULL, -FLT_MAX, FLT_MAX,
366 "Offset", "Offset in floating point units, 1.0 is the width and height of the image", -FLT_MAX, FLT_MAX);
369 /********************** view zoom operator *********************/
371 typedef struct ViewZoomData {
377 /* needed for continuous zoom */
379 double timer_lastdraw;
386 static void image_view_zoom_init(bContext *C, wmOperator *op, wmEvent *event)
388 SpaceImage *sima = CTX_wm_space_image(C);
389 ARegion *ar = CTX_wm_region(C);
392 op->customdata = vpd = MEM_callocN(sizeof(ViewZoomData), "ImageViewZoomData");
393 WM_cursor_modal(CTX_wm_window(C), BC_NSEW_SCROLLCURSOR);
395 vpd->origx = event->x;
396 vpd->origy = event->y;
397 vpd->zoom = sima->zoom;
398 vpd->event_type = event->type;
400 UI_view2d_region_to_view(&ar->v2d, event->mval[0], event->mval[1], &vpd->location[0], &vpd->location[1]);
402 if (U.viewzoom == USER_ZOOM_CONT) {
403 /* needs a timer to continue redrawing */
404 vpd->timer = WM_event_add_timer(CTX_wm_manager(C), CTX_wm_window(C), TIMER, 0.01f);
405 vpd->timer_lastdraw = PIL_check_seconds_timer();
411 WM_event_add_modal_handler(C, op);
414 static void image_view_zoom_exit(bContext *C, wmOperator *op, int cancel)
416 SpaceImage *sima = CTX_wm_space_image(C);
417 ViewZoomData *vpd = op->customdata;
420 sima->zoom = vpd->zoom;
421 ED_region_tag_redraw(CTX_wm_region(C));
425 WM_event_remove_timer(CTX_wm_manager(C), vpd->timer->win, vpd->timer);
427 WM_cursor_restore(CTX_wm_window(C));
428 MEM_freeN(op->customdata);
431 static int image_view_zoom_exec(bContext *C, wmOperator *op)
433 SpaceImage *sima = CTX_wm_space_image(C);
434 ARegion *ar = CTX_wm_region(C);
436 sima_zoom_set_factor(sima, ar, RNA_float_get(op->ptr, "factor"), NULL);
438 ED_region_tag_redraw(CTX_wm_region(C));
442 if (image_preview_active(curarea, NULL, NULL)) {
443 /* recalculates new preview rect */
444 scrarea_do_windraw(curarea);
445 image_preview_event(2);
449 return OPERATOR_FINISHED;
458 static int image_view_zoom_invoke(bContext *C, wmOperator *op, wmEvent *event)
460 if (event->type == MOUSEZOOM || event->type == MOUSEPAN) {
461 SpaceImage *sima = CTX_wm_space_image(C);
462 ARegion *ar = CTX_wm_region(C);
463 float delta, factor, location[2];
465 UI_view2d_region_to_view(&ar->v2d, event->mval[0], event->mval[1], &location[0], &location[1]);
467 delta = event->prevx - event->x + event->prevy - event->y;
469 if (U.uiflag & USER_ZOOM_INVERT)
472 factor = 1.0f + delta / 300.0f;
473 RNA_float_set(op->ptr, "factor", factor);
474 sima_zoom_set(sima, ar, sima->zoom * factor, location);
475 ED_region_tag_redraw(CTX_wm_region(C));
477 return OPERATOR_FINISHED;
480 image_view_zoom_init(C, op, event);
481 return OPERATOR_RUNNING_MODAL;
485 static void image_zoom_apply(ViewZoomData *vpd, wmOperator *op, const int x, const int y, const short viewzoom, const short zoom_invert)
489 if (viewzoom == USER_ZOOM_CONT) {
490 double time = PIL_check_seconds_timer();
491 float time_step = (float)(time - vpd->timer_lastdraw);
495 if (U.uiflag & USER_ZOOM_HORIZ) {
496 fac = (float)(x - vpd->origx);
499 fac = (float)(y - vpd->origy);
507 zfac = 1.0f + ((fac / 20.0f) * time_step);
508 vpd->timer_lastdraw = time;
509 /* this is the final zoom, but instead make it into a factor */
510 //zoom = vpd->sima->zoom * zfac;
511 factor = (vpd->sima->zoom * zfac) / vpd->zoom;
514 /* for now do the same things for scale and dolly */
515 float delta = x - vpd->origx + y - vpd->origy;
520 factor = 1.0f + delta / 300.0f;
523 RNA_float_set(op->ptr, "factor", factor);
524 sima_zoom_set(vpd->sima, vpd->ar, vpd->zoom * factor, vpd->location);
525 ED_region_tag_redraw(vpd->ar);
528 static int image_view_zoom_modal(bContext *C, wmOperator *op, wmEvent *event)
530 ViewZoomData *vpd = op->customdata;
531 short event_code = VIEW_PASS;
533 /* execute the events */
534 if (event->type == TIMER && event->customdata == vpd->timer) {
535 /* continuous zoom */
536 event_code = VIEW_APPLY;
538 else if (event->type == MOUSEMOVE) {
539 event_code = VIEW_APPLY;
541 else if (event->type == vpd->event_type && event->val == KM_RELEASE) {
542 event_code = VIEW_CONFIRM;
545 if (event_code == VIEW_APPLY) {
546 image_zoom_apply(vpd, op, event->x, event->y, U.viewzoom, (U.uiflag & USER_ZOOM_INVERT) != 0);
548 else if (event_code == VIEW_CONFIRM) {
549 image_view_zoom_exit(C, op, 0);
550 return OPERATOR_FINISHED;
553 return OPERATOR_RUNNING_MODAL;
556 static int image_view_zoom_cancel(bContext *C, wmOperator *op)
558 image_view_zoom_exit(C, op, 1);
559 return OPERATOR_CANCELLED;
562 void IMAGE_OT_view_zoom(wmOperatorType *ot)
565 ot->name = "View Zoom";
566 ot->idname = "IMAGE_OT_view_zoom";
567 ot->description = "Zoom in/out the image";
570 ot->exec = image_view_zoom_exec;
571 ot->invoke = image_view_zoom_invoke;
572 ot->modal = image_view_zoom_modal;
573 ot->cancel = image_view_zoom_cancel;
574 ot->poll = space_image_main_area_poll;
577 ot->flag = OPTYPE_BLOCKING;
580 RNA_def_float(ot->srna, "factor", 0.0f, -FLT_MAX, FLT_MAX,
581 "Factor", "Zoom factor, values higher than 1.0 zoom in, lower values zoom out", -FLT_MAX, FLT_MAX);
584 /********************** NDOF operator *********************/
586 /* Combined pan/zoom from a 3D mouse device.
588 * "view" (not "paper") control -- user moves the viewpoint, not the image being viewed
589 * that explains the negative signs in the code below
592 static int image_view_ndof_invoke(bContext *C, wmOperator *UNUSED(op), wmEvent *event)
594 if (event->type != NDOF_MOTION)
595 return OPERATOR_CANCELLED;
597 SpaceImage *sima = CTX_wm_space_image(C);
598 ARegion *ar = CTX_wm_region(C);
600 wmNDOFMotionData *ndof = (wmNDOFMotionData *) event->customdata;
603 /* tune these until it feels right */
604 const float zoom_sensitivity = 0.5f; // 50% per second (I think)
605 const float pan_sensitivity = 300.f; // screen pixels per second
607 float pan_x = pan_sensitivity * dt * ndof->tvec[0] / sima->zoom;
608 float pan_y = pan_sensitivity * dt * ndof->tvec[1] / sima->zoom;
610 /* "mouse zoom" factor = 1 + (dx + dy) / 300
611 * what about "ndof zoom" factor? should behave like this:
612 * at rest -> factor = 1
613 * move forward -> factor > 1
614 * move backward -> factor < 1
616 float zoom_factor = 1.f + zoom_sensitivity * dt * -ndof->tvec[2];
618 if (U.ndof_flag & NDOF_ZOOM_INVERT)
619 zoom_factor = -zoom_factor;
621 sima_zoom_set_factor(sima, ar, zoom_factor, NULL);
625 ED_region_tag_redraw(ar);
627 return OPERATOR_FINISHED;
631 void IMAGE_OT_view_ndof(wmOperatorType *ot)
634 ot->name = "NDOF Pan/Zoom";
635 ot->idname = "IMAGE_OT_view_ndof";
636 ot->description = "Use a 3D mouse device to pan/zoom the view";
639 ot->invoke = image_view_ndof_invoke;
642 /********************** view all operator *********************/
644 /* Updates the fields of the View2D member of the SpaceImage struct.
645 * Default behavior is to reset the position of the image and set the zoom to 1
646 * If the image will not fit within the window rectangle, the zoom is adjusted */
648 static int image_view_all_exec(bContext *C, wmOperator *UNUSED(op))
652 float aspx, aspy, zoomx, zoomy, w, h;
656 sima = CTX_wm_space_image(C);
657 ar = CTX_wm_region(C);
659 ED_space_image_get_size(sima, &width, &height);
660 ED_space_image_get_aspect(sima, &aspx, &aspy);
665 /* check if the image will fit in the image with (zoom == 1) */
666 width = BLI_rcti_size_x(&ar->winrct) + 1;
667 height = BLI_rcti_size_y(&ar->winrct) + 1;
669 if ((w >= width || h >= height) && (width > 0 && height > 0)) {
670 /* find the zoom value that will fit the image in the image space */
673 sima_zoom_set(sima, ar, 1.0f / power_of_2(1.0f / min_ff(zoomx, zoomy)), NULL);
676 sima_zoom_set(sima, ar, 1.0f, NULL);
678 sima->xof = sima->yof = 0.0f;
680 ED_region_tag_redraw(CTX_wm_region(C));
682 return OPERATOR_FINISHED;
685 void IMAGE_OT_view_all(wmOperatorType *ot)
688 ot->name = "View All";
689 ot->idname = "IMAGE_OT_view_all";
690 ot->description = "View the entire image";
693 ot->exec = image_view_all_exec;
694 ot->poll = space_image_main_area_poll;
697 /********************** view selected operator *********************/
699 static int image_view_selected_exec(bContext *C, wmOperator *UNUSED(op))
706 float size, min[2], max[2], d[2], aspx, aspy;
710 sima = CTX_wm_space_image(C);
711 ar = CTX_wm_region(C);
712 scene = CTX_data_scene(C);
713 obedit = CTX_data_edit_object(C);
715 ima = ED_space_image(sima);
716 ED_space_image_get_size(sima, &width, &height);
717 ED_space_image_get_aspect(sima, &aspx, &aspy);
719 width = width * aspx;
720 height = height * aspy;
723 if (!ED_uvedit_minmax(scene, ima, obedit, min, max))
724 return OPERATOR_CANCELLED;
726 /* adjust offset and zoom */
727 sima->xof = (int)(((min[0] + max[0]) * 0.5f - 0.5f) * width);
728 sima->yof = (int)(((min[1] + max[1]) * 0.5f - 0.5f) * height);
730 d[0] = max[0] - min[0];
731 d[1] = max[1] - min[1];
732 size = 0.5f * MAX2(d[0], d[1]) * MAX2(width, height) / 256.0f;
734 if (size <= 0.01f) size = 0.01f;
735 sima_zoom_set(sima, ar, 0.7f / size, NULL);
737 ED_region_tag_redraw(CTX_wm_region(C));
739 return OPERATOR_FINISHED;
742 static int image_view_selected_poll(bContext *C)
744 return (space_image_main_area_poll(C) && ED_operator_uvedit(C));
747 void IMAGE_OT_view_selected(wmOperatorType *ot)
750 ot->name = "View Center";
751 ot->idname = "IMAGE_OT_view_selected";
752 ot->description = "View all selected UVs";
755 ot->exec = image_view_selected_exec;
756 ot->poll = image_view_selected_poll;
759 /********************** view zoom in/out operator *********************/
761 static int image_view_zoom_in_exec(bContext *C, wmOperator *op)
763 SpaceImage *sima = CTX_wm_space_image(C);
764 ARegion *ar = CTX_wm_region(C);
767 RNA_float_get_array(op->ptr, "location", location);
769 sima_zoom_set_factor(sima, ar, 1.25f, location);
771 ED_region_tag_redraw(CTX_wm_region(C));
773 return OPERATOR_FINISHED;
776 static int image_view_zoom_in_invoke(bContext *C, wmOperator *op, wmEvent *event)
778 ARegion *ar = CTX_wm_region(C);
781 UI_view2d_region_to_view(&ar->v2d, event->mval[0], event->mval[1], &location[0], &location[1]);
782 RNA_float_set_array(op->ptr, "location", location);
784 return image_view_zoom_in_exec(C, op);
787 void IMAGE_OT_view_zoom_in(wmOperatorType *ot)
790 ot->name = "View Zoom In";
791 ot->idname = "IMAGE_OT_view_zoom_in";
792 ot->description = "Zoom in the image (centered around 2D cursor)";
795 ot->invoke = image_view_zoom_in_invoke;
796 ot->exec = image_view_zoom_in_exec;
797 ot->poll = space_image_main_area_poll;
800 RNA_def_float_vector(ot->srna, "location", 2, NULL, -FLT_MAX, FLT_MAX, "Location", "Cursor location in screen coordinates", -10.0f, 10.0f);
803 static int image_view_zoom_out_exec(bContext *C, wmOperator *op)
805 SpaceImage *sima = CTX_wm_space_image(C);
806 ARegion *ar = CTX_wm_region(C);
809 RNA_float_get_array(op->ptr, "location", location);
811 sima_zoom_set_factor(sima, ar, 0.8f, location);
813 ED_region_tag_redraw(CTX_wm_region(C));
815 return OPERATOR_FINISHED;
818 static int image_view_zoom_out_invoke(bContext *C, wmOperator *op, wmEvent *event)
820 ARegion *ar = CTX_wm_region(C);
823 UI_view2d_region_to_view(&ar->v2d, event->mval[0], event->mval[1], &location[0], &location[1]);
824 RNA_float_set_array(op->ptr, "location", location);
826 return image_view_zoom_out_exec(C, op);
829 void IMAGE_OT_view_zoom_out(wmOperatorType *ot)
832 ot->name = "View Zoom Out";
833 ot->idname = "IMAGE_OT_view_zoom_out";
834 ot->description = "Zoom out the image (centered around 2D cursor)";
837 ot->invoke = image_view_zoom_out_invoke;
838 ot->exec = image_view_zoom_out_exec;
839 ot->poll = space_image_main_area_poll;
842 RNA_def_float_vector(ot->srna, "location", 2, NULL, -FLT_MAX, FLT_MAX, "Location", "Cursor location in screen coordinates", -10.0f, 10.0f);
845 /********************** view zoom ratio operator *********************/
847 static int image_view_zoom_ratio_exec(bContext *C, wmOperator *op)
849 SpaceImage *sima = CTX_wm_space_image(C);
850 ARegion *ar = CTX_wm_region(C);
852 sima_zoom_set(sima, ar, RNA_float_get(op->ptr, "ratio"), NULL);
854 /* ensure pixel exact locations for draw */
855 sima->xof = (int)sima->xof;
856 sima->yof = (int)sima->yof;
860 if (image_preview_active(curarea, NULL, NULL)) {
861 /* recalculates new preview rect */
862 scrarea_do_windraw(curarea);
863 image_preview_event(2);
867 ED_region_tag_redraw(CTX_wm_region(C));
869 return OPERATOR_FINISHED;
872 void IMAGE_OT_view_zoom_ratio(wmOperatorType *ot)
875 ot->name = "View Zoom Ratio";
876 ot->idname = "IMAGE_OT_view_zoom_ratio";
877 ot->description = "Set zoom ratio of the view";
880 ot->exec = image_view_zoom_ratio_exec;
881 ot->poll = space_image_main_area_poll;
884 RNA_def_float(ot->srna, "ratio", 0.0f, -FLT_MAX, FLT_MAX,
885 "Ratio", "Zoom ratio, 1.0 is 1:1, higher is zoomed in, lower is zoomed out", -FLT_MAX, FLT_MAX);
888 /**************** load/replace/save callbacks ******************/
889 static void image_filesel(bContext *C, wmOperator *op, const char *path)
891 RNA_string_set(op->ptr, "filepath", path);
892 WM_event_add_fileselect(C, op);
895 /******************** open image operator ********************/
897 static void image_open_init(bContext *C, wmOperator *op)
899 PropertyPointerRNA *pprop;
901 op->customdata = pprop = MEM_callocN(sizeof(PropertyPointerRNA), "OpenPropertyPointerRNA");
902 uiIDContextProperty(C, &pprop->ptr, &pprop->prop);
905 static int image_open_cancel(bContext *UNUSED(C), wmOperator *op)
907 MEM_freeN(op->customdata);
908 op->customdata = NULL;
909 return OPERATOR_CANCELLED;
912 static int image_open_exec(bContext *C, wmOperator *op)
914 SpaceImage *sima = CTX_wm_space_image(C); /* XXX other space types can call */
915 Scene *scene = CTX_data_scene(C);
916 Object *obedit = CTX_data_edit_object(C);
917 ImageUser *iuser = NULL;
918 PropertyPointerRNA *pprop;
923 RNA_string_get(op->ptr, "filepath", str);
924 /* default to frame 1 if there's no scene in context */
928 ima = BKE_image_load_exists(str);
931 if (op->customdata) MEM_freeN(op->customdata);
932 BKE_reportf(op->reports, RPT_ERROR, "Cannot read '%s': %s",
933 str, errno ? strerror(errno) : TIP_("unsupported image format"));
934 return OPERATOR_CANCELLED;
938 image_open_init(C, op);
941 pprop = op->customdata;
944 /* when creating new ID blocks, use is already 1, but RNA
945 * pointer se also increases user, so this compensates it */
948 RNA_id_pointer_create(&ima->id, &idptr);
949 RNA_property_pointer_set(&pprop->ptr, pprop->prop, idptr);
950 RNA_property_update(C, &pprop->ptr, pprop->prop);
953 ED_space_image_set(sima, scene, obedit, ima);
954 iuser = &sima->iuser;
957 Tex *tex = CTX_data_pointer_get_type(C, "texture", &RNA_Texture).data;
958 if (tex && tex->type == TEX_IMAGE)
963 /* initialize because of new image */
970 /* XXX unpackImage frees image buffers */
971 ED_preview_kill_jobs(C);
973 BKE_image_signal(ima, iuser, IMA_SIGNAL_RELOAD);
974 WM_event_add_notifier(C, NC_IMAGE | NA_EDITED, ima);
976 MEM_freeN(op->customdata);
978 return OPERATOR_FINISHED;
981 static int image_open_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
983 SpaceImage *sima = CTX_wm_space_image(C); /* XXX other space types can call */
984 char *path = U.textudir;
992 Tex *tex = CTX_data_pointer_get_type(C, "texture", &RNA_Texture).data;
993 if (tex && tex->type == TEX_IMAGE)
1002 uiIDContextProperty(C, &ptr, &prop);
1008 oldptr = RNA_property_pointer_get(&ptr, prop);
1009 oldima = (Image *)oldptr.id.data;
1010 /* unlikely to fail but better avoid strange crash */
1011 if (oldima && GS(oldima->id.name) == ID_IM) {
1020 if (RNA_struct_property_is_set(op->ptr, "filepath"))
1021 return image_open_exec(C, op);
1023 image_open_init(C, op);
1025 image_filesel(C, op, path);
1027 return OPERATOR_RUNNING_MODAL;
1030 /* called by other space types too */
1031 void IMAGE_OT_open(wmOperatorType *ot)
1034 ot->name = "Open Image";
1035 ot->description = "Open image";
1036 ot->idname = "IMAGE_OT_open";
1039 ot->exec = image_open_exec;
1040 ot->invoke = image_open_invoke;
1041 ot->cancel = image_open_cancel;
1044 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1047 WM_operator_properties_filesel(ot, FOLDERFILE | IMAGEFILE | MOVIEFILE, FILE_SPECIAL, FILE_OPENFILE,
1048 WM_FILESEL_FILEPATH | WM_FILESEL_RELPATH, FILE_DEFAULTDISPLAY);
1051 /******************** Match movie length operator ********************/
1052 static int image_match_len_exec(bContext *C, wmOperator *UNUSED(op))
1054 Scene *scene = CTX_data_scene(C);
1055 Image *ima = CTX_data_pointer_get_type(C, "edit_image", &RNA_Image).data;
1056 ImageUser *iuser = CTX_data_pointer_get_type(C, "edit_image_user", &RNA_ImageUser).data;
1058 if (!ima || !iuser) {
1059 /* Try to get a Texture, or a SpaceImage from context... */
1060 SpaceImage *sima = CTX_wm_space_image(C);
1061 Tex *tex = CTX_data_pointer_get_type(C, "texture", &RNA_Texture).data;
1062 if (tex && tex->type == TEX_IMAGE) {
1064 iuser = &tex->iuser;
1068 iuser = &sima->iuser;
1073 if (!ima || !iuser || !ima->anim)
1074 return OPERATOR_CANCELLED;
1076 iuser->frames = IMB_anim_get_duration(ima->anim, IMB_TC_RECORD_RUN);
1077 BKE_image_user_frame_calc(iuser, scene->r.cfra, 0);
1079 return OPERATOR_FINISHED;
1082 /* called by other space types too */
1083 void IMAGE_OT_match_movie_length(wmOperatorType *ot)
1086 ot->name = "Match Movie Length";
1087 ot->description = "Set image's users length to the one of this video";
1088 ot->idname = "IMAGE_OT_match_movie_length";
1091 ot->exec = image_match_len_exec;
1094 ot->flag = OPTYPE_REGISTER /* | OPTYPE_UNDO */; /* Don't think we need undo for that. */
1097 /******************** replace image operator ********************/
1099 static int image_replace_exec(bContext *C, wmOperator *op)
1101 SpaceImage *sima = CTX_wm_space_image(C);
1105 return OPERATOR_CANCELLED;
1107 RNA_string_get(op->ptr, "filepath", str);
1109 /* we cant do much if the str is longer then FILE_MAX :/ */
1110 BLI_strncpy(sima->image->name, str, sizeof(sima->image->name));
1112 if (sima->image->source == IMA_SRC_GENERATED) {
1113 sima->image->source = IMA_SRC_FILE;
1114 BKE_image_signal(sima->image, &sima->iuser, IMA_SIGNAL_SRC_CHANGE);
1117 if (BLI_testextensie_array(str, imb_ext_movie))
1118 sima->image->source = IMA_SRC_MOVIE;
1120 sima->image->source = IMA_SRC_FILE;
1122 /* XXX unpackImage frees image buffers */
1123 ED_preview_kill_jobs(C);
1125 BKE_image_signal(sima->image, &sima->iuser, IMA_SIGNAL_RELOAD);
1126 WM_event_add_notifier(C, NC_IMAGE | NA_EDITED, sima->image);
1128 return OPERATOR_FINISHED;
1131 static int image_replace_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
1133 SpaceImage *sima = CTX_wm_space_image(C);
1136 return OPERATOR_CANCELLED;
1138 if (RNA_struct_property_is_set(op->ptr, "filepath"))
1139 return image_replace_exec(C, op);
1141 if (!RNA_struct_property_is_set(op->ptr, "relative_path"))
1142 RNA_boolean_set(op->ptr, "relative_path", BLI_path_is_rel(sima->image->name));
1144 image_filesel(C, op, sima->image->name);
1146 return OPERATOR_RUNNING_MODAL;
1149 void IMAGE_OT_replace(wmOperatorType *ot)
1152 ot->name = "Replace Image";
1153 ot->idname = "IMAGE_OT_replace";
1154 ot->description = "Replace current image by another one from disk";
1157 ot->exec = image_replace_exec;
1158 ot->invoke = image_replace_invoke;
1159 ot->poll = space_image_poll;
1162 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1165 WM_operator_properties_filesel(ot, FOLDERFILE | IMAGEFILE | MOVIEFILE, FILE_SPECIAL, FILE_OPENFILE,
1166 WM_FILESEL_FILEPATH | WM_FILESEL_RELPATH, FILE_DEFAULTDISPLAY);
1169 /******************** save image as operator ********************/
1172 /* matching scene->r settings */
1173 //short planes, imtype, subimtype, quality;
1174 ImageFormatData im_format;
1175 char filepath[FILE_MAX]; /* keep absolute */
1178 static void save_image_options_defaults(SaveImageOptions *simopts)
1180 BKE_imformat_defaults(&simopts->im_format);
1181 simopts->filepath[0] = '\0';
1184 static char imtype_best_depth(ImBuf *ibuf, const char imtype)
1186 const char depth_ok = BKE_imtype_valid_depths(imtype);
1188 if (ibuf->rect_float) {
1189 if (depth_ok & R_IMF_CHAN_DEPTH_32) return R_IMF_CHAN_DEPTH_32;
1190 if (depth_ok & R_IMF_CHAN_DEPTH_24) return R_IMF_CHAN_DEPTH_24;
1191 if (depth_ok & R_IMF_CHAN_DEPTH_16) return R_IMF_CHAN_DEPTH_16;
1192 if (depth_ok & R_IMF_CHAN_DEPTH_12) return R_IMF_CHAN_DEPTH_12;
1193 return R_IMF_CHAN_DEPTH_8;
1196 if (depth_ok & R_IMF_CHAN_DEPTH_8) return R_IMF_CHAN_DEPTH_8;
1197 if (depth_ok & R_IMF_CHAN_DEPTH_12) return R_IMF_CHAN_DEPTH_12;
1198 if (depth_ok & R_IMF_CHAN_DEPTH_16) return R_IMF_CHAN_DEPTH_16;
1199 if (depth_ok & R_IMF_CHAN_DEPTH_24) return R_IMF_CHAN_DEPTH_24;
1200 if (depth_ok & R_IMF_CHAN_DEPTH_32) return R_IMF_CHAN_DEPTH_32;
1201 return R_IMF_CHAN_DEPTH_8; /* fallback, should not get here */
1205 static int save_image_options_init(SaveImageOptions *simopts, SpaceImage *sima, Scene *scene, const short guess_path)
1208 ImBuf *ibuf = ED_space_image_acquire_buffer(sima, &lock);
1211 Image *ima = sima->image;
1212 short is_depth_set = FALSE;
1214 simopts->im_format.planes = ibuf->planes;
1216 if (ELEM(ima->type, IMA_TYPE_R_RESULT, IMA_TYPE_COMPOSITE)) {
1218 simopts->im_format = scene->r.im_format;
1219 is_depth_set = TRUE;
1221 else if (ima->source == IMA_SRC_GENERATED) {
1222 simopts->im_format.imtype = R_IMF_IMTYPE_PNG;
1225 BKE_imbuf_to_image_format(&simopts->im_format, ibuf);
1227 //simopts->subimtype = scene->r.subimtype; /* XXX - this is lame, we need to make these available too! */
1228 simopts->im_format.quality = ibuf->ftype & 0xff;
1230 BLI_strncpy(simopts->filepath, ibuf->name, sizeof(simopts->filepath));
1232 /* sanitize all settings */
1234 /* unlikely but just in case */
1235 if (ELEM3(simopts->im_format.planes, R_IMF_PLANES_BW, R_IMF_PLANES_RGB, R_IMF_PLANES_RGBA) == 0) {
1236 simopts->im_format.planes = R_IMF_PLANES_RGBA;
1239 /* depth, account for float buffer and format support */
1240 if (is_depth_set == FALSE) {
1241 simopts->im_format.depth = imtype_best_depth(ibuf, simopts->im_format.imtype);
1244 /* some formats don't use quality so fallback to scenes quality */
1245 if (simopts->im_format.quality == 0) {
1246 simopts->im_format.quality = scene->r.im_format.quality;
1249 /* check for empty path */
1250 if (guess_path && simopts->filepath[0] == 0) {
1251 if ((G.ima[0] == '/') && (G.ima[1] == '/') && (G.ima[2] == '\0')) {
1252 BLI_strncpy(simopts->filepath, "//untitled", FILE_MAX);
1255 BLI_strncpy(simopts->filepath, G.ima, FILE_MAX);
1257 BLI_path_abs(simopts->filepath, G.main->name);
1260 /* color management */
1261 BKE_color_managed_display_settings_copy(&simopts->im_format.display_settings, &scene->display_settings);
1262 BKE_color_managed_view_settings_copy(&simopts->im_format.view_settings, &scene->view_settings);
1265 ED_space_image_release_buffer(sima, ibuf, lock);
1267 return (ibuf != NULL);
1270 static void save_image_options_from_op(SaveImageOptions *simopts, wmOperator *op)
1272 if (op->customdata) {
1273 BKE_color_managed_view_settings_free(&simopts->im_format.view_settings);
1275 simopts->im_format = *(ImageFormatData *)op->customdata;
1278 if (RNA_struct_property_is_set(op->ptr, "filepath")) {
1279 RNA_string_get(op->ptr, "filepath", simopts->filepath);
1280 BLI_path_abs(simopts->filepath, G.main->name);
1284 static void save_image_options_to_op(SaveImageOptions *simopts, wmOperator *op)
1286 if (op->customdata) {
1287 BKE_color_managed_view_settings_free(&((ImageFormatData *)op->customdata)->view_settings);
1289 *(ImageFormatData *)op->customdata = simopts->im_format;
1292 RNA_string_set(op->ptr, "filepath", simopts->filepath);
1295 /* assumes name is FILE_MAX */
1296 /* ima->name and ibuf->name should end up the same */
1297 static void save_image_doit(bContext *C, SpaceImage *sima, wmOperator *op, SaveImageOptions *simopts, int do_newpath)
1299 Image *ima = ED_space_image(sima);
1301 ImBuf *ibuf = ED_space_image_acquire_buffer(sima, &lock);
1304 ImBuf *colormanaged_ibuf;
1305 const char *relbase = ID_BLEND_PATH(CTX_data_main(C), &ima->id);
1306 const short relative = (RNA_struct_find_property(op->ptr, "relative_path") && RNA_boolean_get(op->ptr, "relative_path"));
1307 const short save_copy = (RNA_struct_find_property(op->ptr, "copy") && RNA_boolean_get(op->ptr, "copy"));
1308 const short save_as_render = (RNA_struct_find_property(op->ptr, "save_as_render") && RNA_boolean_get(op->ptr, "save_as_render"));
1309 ImageFormatData *imf = &simopts->im_format;
1312 /* old global to ensure a 2nd save goes to same dir */
1313 BLI_strncpy(G.ima, simopts->filepath, sizeof(G.ima));
1317 if (ima->type == IMA_TYPE_R_RESULT) {
1318 /* enforce user setting for RGB or RGBA, but skip BW */
1319 if (simopts->im_format.planes == R_IMF_PLANES_RGBA) {
1320 ibuf->planes = R_IMF_PLANES_RGBA;
1322 else if (simopts->im_format.planes == R_IMF_PLANES_RGB) {
1323 ibuf->planes = R_IMF_PLANES_RGB;
1327 /* TODO, better solution, if a 24bit image is painted onto it may contain alpha */
1328 if (ibuf->userflags & IB_BITMAPDIRTY) { /* it has been painted onto */
1329 /* checks each pixel, not ideal */
1330 ibuf->planes = BKE_imbuf_alpha_test(ibuf) ? 32 : 24;
1334 colormanaged_ibuf = IMB_colormanagement_imbuf_for_write(ibuf, save_as_render, TRUE, &imf->view_settings, &imf->display_settings, imf);
1336 if (simopts->im_format.imtype == R_IMF_IMTYPE_MULTILAYER) {
1337 Scene *scene = CTX_data_scene(C);
1338 RenderResult *rr = BKE_image_acquire_renderresult(scene, ima);
1340 RE_WriteRenderResult(op->reports, rr, simopts->filepath, simopts->im_format.quality);
1344 BKE_report(op->reports, RPT_ERROR, "Did not write, no Multilayer Image");
1346 BKE_image_release_renderresult(scene, ima);
1349 if (BKE_imbuf_write_as(colormanaged_ibuf, simopts->filepath, &simopts->im_format, save_copy)) {
1357 BLI_strncpy(ibuf->name, simopts->filepath, sizeof(ibuf->name));
1358 BLI_strncpy(ima->name, simopts->filepath, sizeof(ima->name));
1361 ibuf->userflags &= ~IB_BITMAPDIRTY;
1364 if (ima->type == IMA_TYPE_R_RESULT) {
1365 ima->type = IMA_TYPE_IMAGE;
1367 /* workaround to ensure the render result buffer is no longer used
1368 * by this image, otherwise can crash when a new render result is
1370 if (ibuf->rect && !(ibuf->mall & IB_rect))
1371 imb_freerectImBuf(ibuf);
1372 if (ibuf->rect_float && !(ibuf->mall & IB_rectfloat))
1373 imb_freerectfloatImBuf(ibuf);
1374 if (ibuf->zbuf && !(ibuf->mall & IB_zbuf))
1375 IMB_freezbufImBuf(ibuf);
1376 if (ibuf->zbuf_float && !(ibuf->mall & IB_zbuffloat))
1377 IMB_freezbuffloatImBuf(ibuf);
1379 if (ELEM(ima->source, IMA_SRC_GENERATED, IMA_SRC_VIEWER)) {
1380 ima->source = IMA_SRC_FILE;
1381 ima->type = IMA_TYPE_IMAGE;
1384 /* only image path, never ibuf */
1386 BLI_path_rel(ima->name, relbase); /* only after saving */
1389 IMB_colormanagment_colorspace_from_ibuf_ftype(&ima->colorspace_settings, ibuf);
1393 BKE_reportf(op->reports, RPT_ERROR, "Could not write image %s", simopts->filepath);
1397 WM_event_add_notifier(C, NC_IMAGE | NA_EDITED, sima->image);
1401 if (colormanaged_ibuf != ibuf)
1402 IMB_freeImBuf(colormanaged_ibuf);
1405 ED_space_image_release_buffer(sima, ibuf, lock);
1408 static void image_save_as_free(wmOperator *op)
1410 if (op->customdata) {
1411 ImageFormatData *im_format = (ImageFormatData *)op->customdata;
1412 BKE_color_managed_view_settings_free(&im_format->view_settings);
1414 MEM_freeN(op->customdata);
1415 op->customdata = NULL;
1419 static int image_save_as_exec(bContext *C, wmOperator *op)
1421 SpaceImage *sima = CTX_wm_space_image(C);
1422 SaveImageOptions simopts;
1424 save_image_options_defaults(&simopts);
1426 /* just in case to initialize values,
1427 * these should be set on invoke or by the caller. */
1428 save_image_options_init(&simopts, sima, CTX_data_scene(C), 0);
1430 save_image_options_from_op(&simopts, op);
1432 save_image_doit(C, sima, op, &simopts, TRUE);
1434 image_save_as_free(op);
1435 return OPERATOR_FINISHED;
1439 static int image_save_as_check(bContext *UNUSED(C), wmOperator *op)
1441 ImageFormatData *imf = op->customdata;
1442 return WM_operator_filesel_ensure_ext_imtype(op, imf);
1445 static int image_save_as_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
1447 SpaceImage *sima = CTX_wm_space_image(C);
1448 Image *ima = ED_space_image(sima);
1449 Scene *scene = CTX_data_scene(C);
1450 SaveImageOptions simopts;
1452 if (RNA_struct_property_is_set(op->ptr, "filepath"))
1453 return image_save_as_exec(C, op);
1455 save_image_options_defaults(&simopts);
1457 if (save_image_options_init(&simopts, sima, scene, TRUE) == 0)
1458 return OPERATOR_CANCELLED;
1459 save_image_options_to_op(&simopts, op);
1461 /* enable save_copy by default for render results */
1462 if (ELEM(ima->type, IMA_TYPE_R_RESULT, IMA_TYPE_COMPOSITE) && !RNA_struct_property_is_set(op->ptr, "copy")) {
1463 RNA_boolean_set(op->ptr, "copy", TRUE);
1466 if (ima->source == IMA_SRC_VIEWER || (ima->flag & IMA_VIEW_AS_RENDER))
1467 RNA_boolean_set(op->ptr, "save_as_render", TRUE);
1469 RNA_boolean_set(op->ptr, "save_as_render", FALSE);
1471 op->customdata = MEM_mallocN(sizeof(simopts.im_format), __func__);
1472 memcpy(op->customdata, &simopts.im_format, sizeof(simopts.im_format));
1474 image_filesel(C, op, simopts.filepath);
1476 return OPERATOR_RUNNING_MODAL;
1479 static int image_save_as_cancel(bContext *UNUSED(C), wmOperator *op)
1481 image_save_as_free(op);
1483 return OPERATOR_CANCELLED;
1486 static int image_save_as_draw_check_prop(PointerRNA *ptr, PropertyRNA *prop)
1488 const char *prop_id = RNA_property_identifier(prop);
1490 return !(strcmp(prop_id, "filepath") == 0 ||
1491 strcmp(prop_id, "directory") == 0 ||
1492 strcmp(prop_id, "filename") == 0 ||
1493 /* when saving a copy, relative path has no effect */
1494 ((strcmp(prop_id, "relative_path") == 0) && RNA_boolean_get(ptr, "copy"))
1498 static void image_save_as_draw(bContext *UNUSED(C), wmOperator *op)
1500 uiLayout *layout = op->layout;
1501 ImageFormatData *imf = op->customdata;
1504 /* image template */
1505 RNA_pointer_create(NULL, &RNA_ImageFormatSettings, imf, &ptr);
1506 uiTemplateImageSettings(layout, &ptr, FALSE);
1508 /* main draw call */
1509 RNA_pointer_create(NULL, op->type->srna, op->properties, &ptr);
1510 uiDefAutoButsRNA(layout, &ptr, image_save_as_draw_check_prop, '\0');
1513 static int image_save_as_poll(bContext *C)
1515 if (space_image_buffer_exists_poll(C)) {
1516 if (G.is_rendering) {
1517 /* no need to NULL check here */
1518 SpaceImage *sima = CTX_wm_space_image(C);
1519 Image *ima = ED_space_image(sima);
1521 if (ima->source == IMA_SRC_VIEWER) {
1522 CTX_wm_operator_poll_msg_set(C, "can't save image while rendering");
1531 void IMAGE_OT_save_as(wmOperatorType *ot)
1533 // PropertyRNA *prop;
1536 ot->name = "Save As Image";
1537 ot->idname = "IMAGE_OT_save_as";
1538 ot->description = "Save the image with another name and/or settings";
1541 ot->exec = image_save_as_exec;
1542 ot->check = image_save_as_check;
1543 ot->invoke = image_save_as_invoke;
1544 ot->cancel = image_save_as_cancel;
1545 ot->ui = image_save_as_draw;
1546 ot->poll = image_save_as_poll;
1549 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1552 RNA_def_boolean(ot->srna, "save_as_render", 0, "Save As Render", "Apply render part of display transform when saving byte image");
1553 RNA_def_boolean(ot->srna, "copy", 0, "Copy", "Create a new image file without modifying the current image in blender");
1555 WM_operator_properties_filesel(ot, FOLDERFILE | IMAGEFILE | MOVIEFILE, FILE_SPECIAL, FILE_SAVE,
1556 WM_FILESEL_FILEPATH | WM_FILESEL_RELPATH, FILE_DEFAULTDISPLAY);
1559 /******************** save image operator ********************/
1561 static int image_save_exec(bContext *C, wmOperator *op)
1563 SpaceImage *sima = CTX_wm_space_image(C);
1564 Scene *scene = CTX_data_scene(C);
1565 SaveImageOptions simopts;
1567 save_image_options_defaults(&simopts);
1568 if (save_image_options_init(&simopts, sima, scene, FALSE) == 0)
1569 return OPERATOR_CANCELLED;
1570 save_image_options_from_op(&simopts, op);
1572 if (BLI_exists(simopts.filepath) && BLI_file_is_writable(simopts.filepath)) {
1573 save_image_doit(C, sima, op, &simopts, FALSE);
1576 BKE_reportf(op->reports, RPT_ERROR, "Cannot save image, path '%s' is not writable", simopts.filepath);
1577 return OPERATOR_CANCELLED;
1580 return OPERATOR_FINISHED;
1583 void IMAGE_OT_save(wmOperatorType *ot)
1586 ot->name = "Save Image";
1587 ot->idname = "IMAGE_OT_save";
1588 ot->description = "Save the image with current name and settings";
1591 ot->exec = image_save_exec;
1592 ot->poll = space_image_file_exists_poll;
1595 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1598 /******************* save sequence operator ********************/
1600 static int image_save_sequence_exec(bContext *C, wmOperator *op)
1602 Main *bmain = CTX_data_main(C);
1603 SpaceImage *sima = CTX_wm_space_image(C);
1606 char di[FILE_MAX], fi[FILE_MAX];
1608 if (sima->image == NULL)
1609 return OPERATOR_CANCELLED;
1611 if (sima->image->source != IMA_SRC_SEQUENCE) {
1612 BKE_report(op->reports, RPT_ERROR, "Can only save sequence on image sequences");
1613 return OPERATOR_CANCELLED;
1616 if (sima->image->type == IMA_TYPE_MULTILAYER) {
1617 BKE_report(op->reports, RPT_ERROR, "Cannot save multilayer sequences");
1618 return OPERATOR_CANCELLED;
1622 for (ibuf = sima->image->ibufs.first; ibuf; ibuf = ibuf->next)
1623 if (ibuf->userflags & IB_BITMAPDIRTY)
1627 BKE_report(op->reports, RPT_WARNING, "No images have been changed");
1628 return OPERATOR_CANCELLED;
1631 /* get a filename for menu */
1632 for (ibuf = sima->image->ibufs.first; ibuf; ibuf = ibuf->next)
1633 if (ibuf->userflags & IB_BITMAPDIRTY)
1636 BLI_strncpy(di, ibuf->name, FILE_MAX);
1637 BLI_splitdirstring(di, fi);
1639 BKE_reportf(op->reports, RPT_INFO, "%d image(s) will be saved in %s", tot, di);
1641 for (ibuf = sima->image->ibufs.first; ibuf; ibuf = ibuf->next) {
1642 if (ibuf->userflags & IB_BITMAPDIRTY) {
1643 char name[FILE_MAX];
1644 BLI_strncpy(name, ibuf->name, sizeof(name));
1646 BLI_path_abs(name, bmain->name);
1648 if (0 == IMB_saveiff(ibuf, name, IB_rect | IB_zbuf | IB_zbuffloat)) {
1649 BKE_reportf(op->reports, RPT_ERROR, "Could not write image %s", name);
1653 BKE_reportf(op->reports, RPT_INFO, "Saved %s", ibuf->name);
1654 ibuf->userflags &= ~IB_BITMAPDIRTY;
1658 return OPERATOR_FINISHED;
1661 void IMAGE_OT_save_sequence(wmOperatorType *ot)
1664 ot->name = "Save Sequence";
1665 ot->idname = "IMAGE_OT_save_sequence";
1666 ot->description = "Save a sequence of images";
1669 ot->exec = image_save_sequence_exec;
1670 ot->poll = space_image_buffer_exists_poll;
1673 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1676 /******************** reload image operator ********************/
1678 static int image_reload_exec(bContext *C, wmOperator *UNUSED(op))
1680 Image *ima = CTX_data_edit_image(C);
1681 SpaceImage *sima = CTX_wm_space_image(C);
1684 return OPERATOR_CANCELLED;
1686 /* XXX unpackImage frees image buffers */
1687 ED_preview_kill_jobs(C);
1690 BKE_image_signal(ima, (sima) ? &sima->iuser : NULL, IMA_SIGNAL_RELOAD);
1692 WM_event_add_notifier(C, NC_IMAGE | NA_EDITED, ima);
1694 return OPERATOR_FINISHED;
1697 void IMAGE_OT_reload(wmOperatorType *ot)
1700 ot->name = "Reload Image";
1701 ot->idname = "IMAGE_OT_reload";
1702 ot->description = "Reload current image from disk";
1705 ot->exec = image_reload_exec;
1708 ot->flag = OPTYPE_REGISTER; /* no undo, image buffer is not handled by undo */
1711 /********************** new image operator *********************/
1713 static int image_new_exec(bContext *C, wmOperator *op)
1720 PointerRNA ptr, idptr;
1722 char name[MAX_ID_NAME - 2];
1724 int width, height, floatbuf, gen_type, alpha;
1726 /* retrieve state */
1727 sima = CTX_wm_space_image(C);
1728 scene = CTX_data_scene(C);
1729 obedit = CTX_data_edit_object(C);
1730 bmain = CTX_data_main(C);
1732 RNA_string_get(op->ptr, "name", name);
1733 width = RNA_int_get(op->ptr, "width");
1734 height = RNA_int_get(op->ptr, "height");
1735 floatbuf = RNA_boolean_get(op->ptr, "float");
1736 gen_type = RNA_enum_get(op->ptr, "generated_type");
1737 RNA_float_get_array(op->ptr, "color", color);
1738 alpha = RNA_boolean_get(op->ptr, "alpha");
1743 ima = BKE_image_add_generated(bmain, width, height, name, alpha ? 32 : 24, floatbuf, gen_type, color);
1746 return OPERATOR_CANCELLED;
1749 uiIDContextProperty(C, &ptr, &prop);
1752 /* when creating new ID blocks, use is already 1, but RNA
1753 * pointer se also increases user, so this compensates it */
1756 RNA_id_pointer_create(&ima->id, &idptr);
1757 RNA_property_pointer_set(&ptr, prop, idptr);
1758 RNA_property_update(C, &ptr, prop);
1761 ED_space_image_set(sima, scene, obedit, ima);
1764 Tex *tex = CTX_data_pointer_get_type(C, "texture", &RNA_Texture).data;
1765 if (tex && tex->type == TEX_IMAGE) {
1767 ED_area_tag_redraw(CTX_wm_area(C));
1771 BKE_image_signal(ima, (sima) ? &sima->iuser : NULL, IMA_SIGNAL_USER_NEW_IMAGE);
1773 return OPERATOR_FINISHED;
1776 /* XXX, Ton is not a fan of OK buttons but using this function to avoid undo/redo bug while in mesh-editmode, - campbell */
1777 /* XXX Note: the WM_operator_props_dialog_popup() doesn't work for uiIDContextProperty(), image is not being that way */
1778 static int image_new_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
1780 return WM_operator_props_dialog_popup(C, op, 300, 100);
1784 void IMAGE_OT_new(wmOperatorType *ot)
1787 static float default_color[4] = {0.0f, 0.0f, 0.0f, 1.0f};
1790 ot->name = "New Image";
1791 ot->description = "Create a new image";
1792 ot->idname = "IMAGE_OT_new";
1795 ot->exec = image_new_exec;
1796 ot->invoke = image_new_invoke;
1799 ot->flag = OPTYPE_UNDO;
1802 RNA_def_string(ot->srna, "name", "untitled", MAX_ID_NAME - 2, "Name", "Image datablock name");
1803 RNA_def_int(ot->srna, "width", 1024, 1, INT_MAX, "Width", "Image width", 1, 16384);
1804 RNA_def_int(ot->srna, "height", 1024, 1, INT_MAX, "Height", "Image height", 1, 16384);
1805 prop = RNA_def_float_color(ot->srna, "color", 4, NULL, 0.0f, FLT_MAX, "Color", "Default fill color", 0.0f, 1.0f);
1806 RNA_def_property_subtype(prop, PROP_COLOR_GAMMA);
1807 RNA_def_property_float_array_default(prop, default_color);
1808 RNA_def_boolean(ot->srna, "alpha", 1, "Alpha", "Create an image with an alpha channel");
1809 RNA_def_enum(ot->srna, "generated_type", image_generated_type_items, IMA_GENTYPE_BLANK,
1810 "Generated Type", "Fill the image with a grid for UV map testing");
1811 RNA_def_boolean(ot->srna, "float", 0, "32 bit Float", "Create image with 32 bit floating point bit depth");
1814 /********************* invert operators *********************/
1816 static int image_invert_poll(bContext *C)
1818 Image *ima = CTX_data_edit_image(C);
1820 return BKE_image_has_ibuf(ima, NULL);
1823 static int image_invert_exec(bContext *C, wmOperator *op)
1825 Image *ima = CTX_data_edit_image(C);
1826 ImBuf *ibuf = BKE_image_acquire_ibuf(ima, NULL, NULL);
1828 /* flags indicate if this channel should be inverted */
1829 const short r = RNA_boolean_get(op->ptr, "invert_r");
1830 const short g = RNA_boolean_get(op->ptr, "invert_g");
1831 const short b = RNA_boolean_get(op->ptr, "invert_b");
1832 const short a = RNA_boolean_get(op->ptr, "invert_a");
1836 if (ibuf == NULL) /* TODO: this should actually never happen, but does for render-results -> cleanup */
1837 return OPERATOR_CANCELLED;
1839 /* TODO: make this into an IMB_invert_channels(ibuf,r,g,b,a) method!? */
1840 if (ibuf->rect_float) {
1842 float *fp = (float *) ibuf->rect_float;
1843 for (i = ibuf->x * ibuf->y; i > 0; i--, fp += 4) {
1844 if (r) fp[0] = 1.0f - fp[0];
1845 if (g) fp[1] = 1.0f - fp[1];
1846 if (b) fp[2] = 1.0f - fp[2];
1847 if (a) fp[3] = 1.0f - fp[3];
1851 IMB_rect_from_float(ibuf);
1854 else if (ibuf->rect) {
1856 char *cp = (char *) ibuf->rect;
1857 for (i = ibuf->x * ibuf->y; i > 0; i--, cp += 4) {
1858 if (r) cp[0] = 255 - cp[0];
1859 if (g) cp[1] = 255 - cp[1];
1860 if (b) cp[2] = 255 - cp[2];
1861 if (a) cp[3] = 255 - cp[3];
1865 BKE_image_release_ibuf(ima, ibuf, NULL);
1866 return OPERATOR_CANCELLED;
1869 ibuf->userflags |= IB_BITMAPDIRTY;
1870 if (ibuf->mipmap[0])
1871 ibuf->userflags |= IB_MIPMAP_INVALID;
1873 WM_event_add_notifier(C, NC_IMAGE | NA_EDITED, ima);
1875 BKE_image_release_ibuf(ima, ibuf, NULL);
1877 return OPERATOR_FINISHED;
1880 void IMAGE_OT_invert(wmOperatorType *ot)
1883 ot->name = "Invert Channels";
1884 ot->idname = "IMAGE_OT_invert";
1885 ot->description = "Invert image's channels";
1888 ot->exec = image_invert_exec;
1889 ot->poll = image_invert_poll;
1892 RNA_def_boolean(ot->srna, "invert_r", 0, "Red", "Invert Red Channel");
1893 RNA_def_boolean(ot->srna, "invert_g", 0, "Green", "Invert Green Channel");
1894 RNA_def_boolean(ot->srna, "invert_b", 0, "Blue", "Invert Blue Channel");
1895 RNA_def_boolean(ot->srna, "invert_a", 0, "Alpha", "Invert Alpha Channel");
1898 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1901 /********************* pack operator *********************/
1903 static int image_pack_test(bContext *C, wmOperator *op)
1905 Image *ima = CTX_data_edit_image(C);
1906 int as_png = RNA_boolean_get(op->ptr, "as_png");
1910 if (!as_png && ima->packedfile)
1913 if (ima->source == IMA_SRC_SEQUENCE || ima->source == IMA_SRC_MOVIE) {
1914 BKE_report(op->reports, RPT_ERROR, "Packing movies or image sequences not supported");
1921 static int image_pack_exec(bContext *C, wmOperator *op)
1923 struct Main *bmain = CTX_data_main(C);
1924 Image *ima = CTX_data_edit_image(C);
1925 ImBuf *ibuf = BKE_image_acquire_ibuf(ima, NULL, NULL);
1926 int as_png = RNA_boolean_get(op->ptr, "as_png");
1928 if (!image_pack_test(C, op))
1929 return OPERATOR_CANCELLED;
1931 if (!as_png && (ibuf && (ibuf->userflags & IB_BITMAPDIRTY))) {
1932 BKE_report(op->reports, RPT_ERROR, "Cannot pack edited image from disk, only as internal PNG");
1933 return OPERATOR_CANCELLED;
1937 BKE_image_memorypack(ima);
1939 ima->packedfile = newPackedFile(op->reports, ima->name, ID_BLEND_PATH(bmain, &ima->id));
1941 WM_event_add_notifier(C, NC_IMAGE | NA_EDITED, ima);
1943 BKE_image_release_ibuf(ima, ibuf, NULL);
1945 return OPERATOR_FINISHED;
1948 static int image_pack_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
1950 Image *ima = CTX_data_edit_image(C);
1954 int as_png = RNA_boolean_get(op->ptr, "as_png");
1956 if (!image_pack_test(C, op))
1957 return OPERATOR_CANCELLED;
1959 ibuf = BKE_image_acquire_ibuf(ima, NULL, NULL);
1961 if (!as_png && (ibuf && (ibuf->userflags & IB_BITMAPDIRTY))) {
1962 pup = uiPupMenuBegin(C, "OK", ICON_QUESTION);
1963 layout = uiPupMenuLayout(pup);
1964 uiItemBooleanO(layout, "Can't pack edited image from disk. Pack as internal PNG?", ICON_NONE, op->idname, "as_png", 1);
1965 uiPupMenuEnd(C, pup);
1967 BKE_image_release_ibuf(ima, ibuf, NULL);
1969 return OPERATOR_CANCELLED;
1972 BKE_image_release_ibuf(ima, ibuf, NULL);
1974 return image_pack_exec(C, op);
1977 void IMAGE_OT_pack(wmOperatorType *ot)
1980 ot->name = "Pack Image";
1981 ot->description = "Pack an image as embedded data into the .blend file";
1982 ot->idname = "IMAGE_OT_pack";
1985 ot->exec = image_pack_exec;
1986 ot->invoke = image_pack_invoke;
1989 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1992 RNA_def_boolean(ot->srna, "as_png", 0, "Pack As PNG", "Pack image as lossless PNG");
1995 /********************* unpack operator *********************/
1997 static int image_unpack_exec(bContext *C, wmOperator *op)
1999 Image *ima = CTX_data_edit_image(C);
2000 int method = RNA_enum_get(op->ptr, "method");
2002 /* find the suppplied image by name */
2003 if (RNA_struct_property_is_set(op->ptr, "id")) {
2004 char imaname[MAX_ID_NAME - 2];
2005 RNA_string_get(op->ptr, "id", imaname);
2006 ima = BLI_findstring(&CTX_data_main(C)->image, imaname, offsetof(ID, name) + 2);
2007 if (!ima) ima = CTX_data_edit_image(C);
2010 if (!ima || !ima->packedfile)
2011 return OPERATOR_CANCELLED;
2013 if (ima->source == IMA_SRC_SEQUENCE || ima->source == IMA_SRC_MOVIE) {
2014 BKE_report(op->reports, RPT_ERROR, "Unpacking movies or image sequences not supported");
2015 return OPERATOR_CANCELLED;
2018 if (G.fileflags & G_AUTOPACK)
2019 BKE_report(op->reports, RPT_WARNING, "AutoPack is enabled, so image will be packed again on file save");
2021 /* XXX unpackImage frees image buffers */
2022 ED_preview_kill_jobs(C);
2024 unpackImage(op->reports, ima, method);
2026 WM_event_add_notifier(C, NC_IMAGE | NA_EDITED, ima);
2028 return OPERATOR_FINISHED;
2031 static int image_unpack_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
2033 Image *ima = CTX_data_edit_image(C);
2035 if (RNA_struct_property_is_set(op->ptr, "id"))
2036 return image_unpack_exec(C, op);
2038 if (!ima || !ima->packedfile)
2039 return OPERATOR_CANCELLED;
2041 if (ima->source == IMA_SRC_SEQUENCE || ima->source == IMA_SRC_MOVIE) {
2042 BKE_report(op->reports, RPT_ERROR, "Unpacking movies or image sequences not supported");
2043 return OPERATOR_CANCELLED;
2046 if (G.fileflags & G_AUTOPACK)
2047 BKE_report(op->reports, RPT_WARNING, "AutoPack is enabled, so image will be packed again on file save");
2049 unpack_menu(C, "IMAGE_OT_unpack", ima->id.name + 2, ima->name, "textures", ima->packedfile);
2051 return OPERATOR_FINISHED;
2054 void IMAGE_OT_unpack(wmOperatorType *ot)
2057 ot->name = "Unpack Image";
2058 ot->description = "Save an image packed in the .blend file to disk";
2059 ot->idname = "IMAGE_OT_unpack";
2062 ot->exec = image_unpack_exec;
2063 ot->invoke = image_unpack_invoke;
2066 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2069 RNA_def_enum(ot->srna, "method", unpack_method_items, PF_USE_LOCAL, "Method", "How to unpack");
2070 RNA_def_string(ot->srna, "id", "", MAX_ID_NAME - 2, "Image Name", "Image datablock name to unpack"); /* XXX, weark!, will fail with library, name collisions */
2073 /******************** sample image operator ********************/
2075 typedef struct ImageSampleInfo {
2081 unsigned char col[4];
2086 unsigned char *colp;
2093 int use_default_view;
2096 static void image_sample_draw(const bContext *C, ARegion *ar, void *arg_info)
2098 ImageSampleInfo *info = arg_info;
2100 Scene *scene = CTX_data_scene(C);
2102 ED_image_draw_info(scene, ar, info->color_manage, info->use_default_view, info->channels,
2103 info->x, info->y, info->colp, info->colfp, info->zp, info->zfp);
2107 /* returns color in SRGB */
2108 /* matching ED_space_node_color_sample() */
2109 int ED_space_image_color_sample(SpaceImage *sima, ARegion *ar, int mval[2], float r_col[3])
2112 ImBuf *ibuf = ED_space_image_acquire_buffer(sima, &lock);
2117 ED_space_image_release_buffer(sima, ibuf, lock);
2121 UI_view2d_region_to_view(&ar->v2d, mval[0], mval[1], &fx, &fy);
2123 if (fx >= 0.0f && fy >= 0.0f && fx < 1.0f && fy < 1.0f) {
2126 int x = (int)(fx * ibuf->x), y = (int)(fy * ibuf->y);
2128 CLAMP(x, 0, ibuf->x - 1);
2129 CLAMP(y, 0, ibuf->y - 1);
2131 if (ibuf->rect_float) {
2132 fp = (ibuf->rect_float + (ibuf->channels) * (y * ibuf->x + x));
2133 linearrgb_to_srgb_v3_v3(r_col, fp);
2136 else if (ibuf->rect) {
2137 cp = (unsigned char *)(ibuf->rect + y * ibuf->x + x);
2138 rgb_uchar_to_float(r_col, cp);
2143 ED_space_image_release_buffer(sima, ibuf, lock);
2147 static void image_sample_apply(bContext *C, wmOperator *op, wmEvent *event)
2149 SpaceImage *sima = CTX_wm_space_image(C);
2150 ARegion *ar = CTX_wm_region(C);
2152 ImBuf *ibuf = ED_space_image_acquire_buffer(sima, &lock);
2153 ImageSampleInfo *info = op->customdata;
2155 Scene *scene = CTX_data_scene(C);
2156 CurveMapping *curve_mapping = scene->view_settings.curve_mapping;
2159 ED_space_image_release_buffer(sima, ibuf, lock);
2164 UI_view2d_region_to_view(&ar->v2d, event->mval[0], event->mval[1], &fx, &fy);
2166 if (fx >= 0.0f && fy >= 0.0f && fx < 1.0f && fy < 1.0f) {
2169 int x = (int)(fx * ibuf->x), y = (int)(fy * ibuf->y);
2170 Image *image = ED_space_image(sima);
2172 CLAMP(x, 0, ibuf->x - 1);
2173 CLAMP(y, 0, ibuf->y - 1);
2178 info->channels = ibuf->channels;
2185 info->use_default_view = (image->flag & IMA_VIEW_AS_RENDER) ? FALSE : TRUE;
2188 cp = (unsigned char *)(ibuf->rect + y * ibuf->x + x);
2190 info->col[0] = cp[0];
2191 info->col[1] = cp[1];
2192 info->col[2] = cp[2];
2193 info->col[3] = cp[3];
2194 info->colp = info->col;
2196 info->colf[0] = (float)cp[0] / 255.0f;
2197 info->colf[1] = (float)cp[1] / 255.0f;
2198 info->colf[2] = (float)cp[2] / 255.0f;
2199 info->colf[3] = (float)cp[3] / 255.0f;
2200 info->colfp = info->colf;
2202 info->color_manage = FALSE;
2204 if (ibuf->rect_float) {
2205 fp = (ibuf->rect_float + (ibuf->channels) * (y * ibuf->x + x));
2207 info->colf[0] = fp[0];
2208 info->colf[1] = fp[1];
2209 info->colf[2] = fp[2];
2210 info->colf[3] = fp[3];
2211 info->colfp = info->colf;
2213 info->color_manage = TRUE;
2217 info->z = ibuf->zbuf[y * ibuf->x + x];
2218 info->zp = &info->z;
2220 if (ibuf->zbuf_float) {
2221 info->zf = ibuf->zbuf_float[y * ibuf->x + x];
2222 info->zfp = &info->zf;
2225 if (curve_mapping && ibuf->channels == 4) {
2226 /* we reuse this callback for set curves point operators */
2227 if (RNA_struct_find_property(op->ptr, "point")) {
2228 int point = RNA_enum_get(op->ptr, "point");
2231 curvemapping_set_black_white(curve_mapping, NULL, info->colfp);
2233 else if (point == 0) {
2234 curvemapping_set_black_white(curve_mapping, info->colfp, NULL);
2239 // XXX node curve integration ..
2242 ScrArea *sa, *cur = curarea;
2244 node_curvemap_sample(fp); /* sends global to node editor */
2245 for (sa = G.curscreen->areabase.first; sa; sa = sa->next) {
2246 if (sa->spacetype == SPACE_NODE) {
2247 areawinset(sa->win);
2248 scrarea_do_windraw(sa);
2251 node_curvemap_sample(NULL); /* clears global in node editor */
2260 ED_space_image_release_buffer(sima, ibuf, lock);
2261 ED_area_tag_redraw(CTX_wm_area(C));
2264 static void image_sample_exit(bContext *C, wmOperator *op)
2266 ImageSampleInfo *info = op->customdata;
2268 ED_region_draw_cb_exit(info->art, info->draw_handle);
2269 ED_area_tag_redraw(CTX_wm_area(C));
2273 static int image_sample_invoke(bContext *C, wmOperator *op, wmEvent *event)
2275 SpaceImage *sima = CTX_wm_space_image(C);
2276 ARegion *ar = CTX_wm_region(C);
2277 ImageSampleInfo *info;
2279 if (!ED_space_image_has_buffer(sima))
2280 return OPERATOR_CANCELLED;
2282 info = MEM_callocN(sizeof(ImageSampleInfo), "ImageSampleInfo");
2283 info->art = ar->type;
2284 info->draw_handle = ED_region_draw_cb_activate(ar->type, image_sample_draw, info, REGION_DRAW_POST_PIXEL);
2285 op->customdata = info;
2287 image_sample_apply(C, op, event);
2289 WM_event_add_modal_handler(C, op);
2291 return OPERATOR_RUNNING_MODAL;
2294 static int image_sample_modal(bContext *C, wmOperator *op, wmEvent *event)
2296 switch (event->type) {
2298 case RIGHTMOUSE: // XXX hardcoded
2299 image_sample_exit(C, op);
2300 return OPERATOR_CANCELLED;
2302 image_sample_apply(C, op, event);
2306 return OPERATOR_RUNNING_MODAL;
2309 static int image_sample_cancel(bContext *C, wmOperator *op)
2311 image_sample_exit(C, op);
2312 return OPERATOR_CANCELLED;
2315 void IMAGE_OT_sample(wmOperatorType *ot)
2318 ot->name = "Sample Color";
2319 ot->idname = "IMAGE_OT_sample";
2320 ot->description = "Use mouse to sample a color in current image";
2323 ot->invoke = image_sample_invoke;
2324 ot->modal = image_sample_modal;
2325 ot->cancel = image_sample_cancel;
2326 ot->poll = image_sample_poll;
2329 ot->flag = OPTYPE_BLOCKING;
2332 /******************** sample line operator ********************/
2333 static int image_sample_line_exec(bContext *C, wmOperator *op)
2335 SpaceImage *sima = CTX_wm_space_image(C);
2336 ARegion *ar = CTX_wm_region(C);
2337 Scene *scene = CTX_data_scene(C);
2339 int x_start = RNA_int_get(op->ptr, "xstart");
2340 int y_start = RNA_int_get(op->ptr, "ystart");
2341 int x_end = RNA_int_get(op->ptr, "xend");
2342 int y_end = RNA_int_get(op->ptr, "yend");
2345 ImBuf *ibuf = ED_space_image_acquire_buffer(sima, &lock);
2346 Histogram *hist = &sima->sample_line_hist;
2348 float x1f, y1f, x2f, y2f;
2351 ED_space_image_release_buffer(sima, ibuf, lock);
2352 return OPERATOR_CANCELLED;
2355 if (ibuf->channels < 3) {
2356 ED_space_image_release_buffer(sima, ibuf, lock);
2357 return OPERATOR_CANCELLED;
2360 UI_view2d_region_to_view(&ar->v2d, x_start, y_start, &x1f, &y1f);
2361 UI_view2d_region_to_view(&ar->v2d, x_end, y_end, &x2f, &y2f);
2363 hist->co[0][0] = x1f;
2364 hist->co[0][1] = y1f;
2365 hist->co[1][0] = x2f;
2366 hist->co[1][1] = y2f;
2368 BKE_histogram_update_sample_line(hist, ibuf, &scene->view_settings, &scene->display_settings);
2373 ED_space_image_release_buffer(sima, ibuf, lock);
2375 ED_area_tag_redraw(CTX_wm_area(C));
2377 return OPERATOR_FINISHED;
2380 static int image_sample_line_invoke(bContext *C, wmOperator *op, wmEvent *event)
2382 SpaceImage *sima = CTX_wm_space_image(C);
2384 Histogram *hist = &sima->sample_line_hist;
2385 hist->flag &= ~HISTO_FLAG_SAMPLELINE;
2387 if (!ED_space_image_has_buffer(sima))
2388 return OPERATOR_CANCELLED;
2390 return WM_gesture_straightline_invoke(C, op, event);
2393 void IMAGE_OT_sample_line(wmOperatorType *ot)
2396 ot->name = "Sample Line";
2397 ot->idname = "IMAGE_OT_sample_line";
2398 ot->description = "Sample a line and show it in Scope panels";
2401 ot->invoke = image_sample_line_invoke;
2402 ot->modal = WM_gesture_straightline_modal;
2403 ot->exec = image_sample_line_exec;
2404 ot->poll = space_image_main_area_poll;
2405 ot->cancel = WM_gesture_straightline_cancel;
2408 ot->flag = 0; /* no undo/register since this operates on the space */
2410 WM_operator_properties_gesture_straightline(ot, CURSOR_EDIT);
2413 /******************** set curve point operator ********************/
2415 void IMAGE_OT_curves_point_set(wmOperatorType *ot)
2417 static EnumPropertyItem point_items[] = {
2418 {0, "BLACK_POINT", 0, "Black Point", ""},
2419 {1, "WHITE_POINT", 0, "White Point", ""},
2420 {0, NULL, 0, NULL, NULL}
2424 ot->name = "Set Curves Point";
2425 ot->idname = "IMAGE_OT_curves_point_set";
2426 ot->description = "Set black point or white point for curves";
2429 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2432 ot->invoke = image_sample_invoke;
2433 ot->modal = image_sample_modal;
2434 ot->cancel = image_sample_cancel;
2435 ot->poll = space_image_main_area_not_uv_brush_poll;
2438 RNA_def_enum(ot->srna, "point", point_items, 0, "Point", "Set black point or white point for curves");
2441 /******************** record composite operator *********************/
2443 typedef struct RecordCompositeData {
2447 } RecordCompositeData;
2449 static int image_record_composite_apply(bContext *C, wmOperator *op)
2451 SpaceImage *sima = CTX_wm_space_image(C);
2452 RecordCompositeData *rcd = op->customdata;
2453 Scene *scene = CTX_data_scene(C);
2456 WM_cursor_time(CTX_wm_window(C), scene->r.cfra);
2458 // XXX scene->nodetree->test_break = blender_test_break;
2459 // XXX scene->nodetree->test_break = NULL;
2461 BKE_image_all_free_anim_ibufs(scene->r.cfra);
2462 ntreeCompositTagAnimated(scene->nodetree);
2463 ntreeCompositExecTree(scene->nodetree, &scene->r, 0, scene->r.cfra != rcd->old_cfra,
2464 &scene->view_settings, &scene->display_settings); /* 1 is no previews */
2466 ED_area_tag_redraw(CTX_wm_area(C));
2468 ibuf = BKE_image_acquire_ibuf(sima->image, &sima->iuser, NULL);
2469 /* save memory in flipbooks */
2471 imb_freerectfloatImBuf(ibuf);
2473 BKE_image_release_ibuf(sima->image, ibuf, NULL);
2477 return (scene->r.cfra <= rcd->efra);
2480 static int image_record_composite_init(bContext *C, wmOperator *op)
2482 SpaceImage *sima = CTX_wm_space_image(C);
2483 Scene *scene = CTX_data_scene(C);
2484 RecordCompositeData *rcd;
2486 if (sima->iuser.frames < 2)
2488 if (scene->nodetree == NULL)
2491 op->customdata = rcd = MEM_callocN(sizeof(RecordCompositeData), "ImageRecordCompositeData");
2493 rcd->old_cfra = scene->r.cfra;
2494 rcd->sfra = sima->iuser.sfra;
2495 rcd->efra = sima->iuser.sfra + sima->iuser.frames - 1;
2496 scene->r.cfra = rcd->sfra;
2501 static void image_record_composite_exit(bContext *C, wmOperator *op)
2503 Scene *scene = CTX_data_scene(C);
2504 SpaceImage *sima = CTX_wm_space_image(C);
2505 RecordCompositeData *rcd = op->customdata;
2507 scene->r.cfra = rcd->old_cfra;
2509 WM_cursor_restore(CTX_wm_window(C));
2512 WM_event_remove_timer(CTX_wm_manager(C), CTX_wm_window(C), rcd->timer);
2514 WM_event_add_notifier(C, NC_IMAGE | NA_EDITED, sima->image);
2516 // XXX play_anim(0);
2517 // XXX allqueue(REDRAWNODE, 1);
2522 static int image_record_composite_exec(bContext *C, wmOperator *op)
2524 if (!image_record_composite_init(C, op))
2525 return OPERATOR_CANCELLED;
2527 while (image_record_composite_apply(C, op)) {}
2529 image_record_composite_exit(C, op);
2531 return OPERATOR_FINISHED;
2534 static int image_record_composite_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
2536 RecordCompositeData *rcd;
2538 if (!image_record_composite_init(C, op))
2539 return OPERATOR_CANCELLED;
2541 rcd = op->customdata;
2542 rcd->timer = WM_event_add_timer(CTX_wm_manager(C), CTX_wm_window(C), TIMER, 0.0f);
2543 WM_event_add_modal_handler(C, op);
2545 if (!image_record_composite_apply(C, op))
2546 return OPERATOR_FINISHED;
2548 return OPERATOR_RUNNING_MODAL;
2551 static int image_record_composite_modal(bContext *C, wmOperator *op, wmEvent *event)
2553 RecordCompositeData *rcd = op->customdata;
2555 switch (event->type) {
2557 if (rcd->timer == event->customdata) {
2558 if (!image_record_composite_apply(C, op)) {
2559 image_record_composite_exit(C, op);
2560 return OPERATOR_FINISHED;
2565 image_record_composite_exit(C, op);
2566 return OPERATOR_FINISHED;
2569 return OPERATOR_RUNNING_MODAL;
2572 static int image_record_composite_cancel(bContext *C, wmOperator *op)
2574 image_record_composite_exit(C, op);
2575 return OPERATOR_CANCELLED;
2578 void IMAGE_OT_record_composite(wmOperatorType *ot)
2581 ot->name = "Record Composite";
2582 ot->idname = "IMAGE_OT_record_composite";
2585 ot->exec = image_record_composite_exec;
2586 ot->invoke = image_record_composite_invoke;
2587 ot->modal = image_record_composite_modal;
2588 ot->cancel = image_record_composite_cancel;
2589 ot->poll = space_image_buffer_exists_poll;
2592 /********************* cycle render slot operator *********************/
2594 static int image_cycle_render_slot_poll(bContext *C)
2596 Image *ima = CTX_data_edit_image(C);
2598 return (ima && ima->type == IMA_TYPE_R_RESULT);
2601 static int image_cycle_render_slot_exec(bContext *C, wmOperator *op)
2603 Image *ima = CTX_data_edit_image(C);
2604 int a, slot, cur = ima->render_slot;
2605 const short use_reverse = RNA_boolean_get(op->ptr, "reverse");
2607 for (a = 1; a < IMA_MAX_RENDER_SLOT; a++) {
2608 slot = (cur + (use_reverse ? -a : a)) % IMA_MAX_RENDER_SLOT;
2609 if (slot < 0) slot += IMA_MAX_RENDER_SLOT;
2611 if (ima->renders[slot] || slot == ima->last_render_slot) {
2612 ima->render_slot = slot;
2617 if (a == IMA_MAX_RENDER_SLOT)
2618 ima->render_slot = ((cur == 1) ? 0 : 1);
2620 WM_event_add_notifier(C, NC_IMAGE | ND_DRAW, NULL);
2622 /* no undo push for browsing existing */
2623 if (ima->renders[ima->render_slot] || ima->render_slot == ima->last_render_slot)
2624 return OPERATOR_CANCELLED;
2626 return OPERATOR_FINISHED;
2629 void IMAGE_OT_cycle_render_slot(wmOperatorType *ot)
2632 ot->name = "Cycle Render Slot";
2633 ot->idname = "IMAGE_OT_cycle_render_slot";
2634 ot->description = "Cycle through all non-void render slots";
2637 ot->exec = image_cycle_render_slot_exec;
2638 ot->poll = image_cycle_render_slot_poll;
2641 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2643 RNA_def_boolean(ot->srna, "reverse", 0, "Cycle in Reverse", "");