4 * ***** BEGIN GPL LICENSE BLOCK *****
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21 * All rights reserved.
23 * Contributor(s): Blender Foundation, 2002-2009, Xavier Thomas
25 * ***** END GPL LICENSE BLOCK *****
33 #include "MEM_guardedalloc.h"
36 #include "BLI_blenlib.h"
37 #include "BLI_utildefines.h"
39 #include "DNA_object_types.h"
40 #include "DNA_node_types.h"
41 #include "DNA_packedFile_types.h"
42 #include "DNA_scene_types.h"
44 #include "BKE_colortools.h"
45 #include "BKE_context.h"
46 #include "BKE_image.h"
47 #include "BKE_global.h"
48 #include "BKE_library.h"
51 #include "BKE_packedFile.h"
52 #include "BKE_report.h"
53 #include "BKE_screen.h"
55 #include "IMB_imbuf.h"
56 #include "IMB_imbuf_types.h"
58 #include "RE_pipeline.h"
60 #include "RNA_access.h"
61 #include "RNA_define.h"
62 #include "RNA_enum_types.h"
65 #include "ED_screen.h"
66 #include "ED_space_api.h"
67 #include "ED_uvedit.h"
70 #include "UI_interface.h"
71 #include "UI_resources.h"
72 #include "UI_view2d.h"
77 #include "image_intern.h"
79 /******************** view navigation utilities *********************/
81 static void sima_zoom_set(SpaceImage *sima, ARegion *ar, float zoom)
83 float oldzoom= sima->zoom;
88 if (sima->zoom > 0.1f && sima->zoom < 4.0f)
91 /* check zoom limits */
92 ED_space_image_size(sima, &width, &height);
97 if((width < 4) && (height < 4))
99 else if((ar->winrct.xmax - ar->winrct.xmin) <= sima->zoom)
101 else if((ar->winrct.ymax - ar->winrct.ymin) <= sima->zoom)
105 static void sima_zoom_set_factor(SpaceImage *sima, ARegion *ar, float zoomfac)
107 sima_zoom_set(sima, ar, sima->zoom*zoomfac);
110 #if 0 // currently unused
111 static int image_poll(bContext *C)
113 return (CTX_data_edit_image(C) != NULL);
117 static int space_image_buffer_exists_poll(bContext *C)
119 SpaceImage *sima= CTX_wm_space_image(C);
120 if(sima && sima->spacetype==SPACE_IMAGE)
121 if(ED_space_image_has_buffer(sima))
126 static int space_image_file_exists_poll(bContext *C)
128 if(space_image_buffer_exists_poll(C)) {
129 Main *bmain= CTX_data_main(C);
130 SpaceImage *sima= CTX_wm_space_image(C);
136 ibuf= ED_space_image_acquire_buffer(sima, &lock);
138 BLI_strncpy(name, ibuf->name, FILE_MAX);
139 BLI_path_abs(name, bmain->name);
140 poll= (BLI_exists(name) && BLI_is_writable(name));
142 ED_space_image_release_buffer(sima, lock);
149 static int space_image_poll(bContext *C)
151 SpaceImage *sima= CTX_wm_space_image(C);
152 if(sima && sima->spacetype==SPACE_IMAGE && sima->image)
157 int space_image_main_area_poll(bContext *C)
159 SpaceImage *sima= CTX_wm_space_image(C);
160 // XXX ARegion *ar= CTX_wm_region(C);
163 return 1; // XXX (ar && ar->type->regionid == RGN_TYPE_WINDOW);
168 /********************** view pan operator *********************/
170 typedef struct ViewPanData {
175 static void view_pan_init(bContext *C, wmOperator *op, wmEvent *event)
177 SpaceImage *sima= CTX_wm_space_image(C);
180 op->customdata= vpd= MEM_callocN(sizeof(ViewPanData), "ImageViewPanData");
181 WM_cursor_modal(CTX_wm_window(C), BC_NSEW_SCROLLCURSOR);
188 WM_event_add_modal_handler(C, op);
191 static void view_pan_exit(bContext *C, wmOperator *op, int cancel)
193 SpaceImage *sima= CTX_wm_space_image(C);
194 ViewPanData *vpd= op->customdata;
199 ED_region_tag_redraw(CTX_wm_region(C));
202 WM_cursor_restore(CTX_wm_window(C));
203 MEM_freeN(op->customdata);
206 static int view_pan_exec(bContext *C, wmOperator *op)
208 SpaceImage *sima= CTX_wm_space_image(C);
211 RNA_float_get_array(op->ptr, "offset", offset);
212 sima->xof += offset[0];
213 sima->yof += offset[1];
215 ED_region_tag_redraw(CTX_wm_region(C));
219 if(image_preview_active(curarea, NULL, NULL)) {
220 /* recalculates new preview rect */
221 scrarea_do_windraw(curarea);
222 image_preview_event(2);
226 return OPERATOR_FINISHED;
229 static int view_pan_invoke(bContext *C, wmOperator *op, wmEvent *event)
231 if (event->type == MOUSEPAN) {
232 SpaceImage *sima= CTX_wm_space_image(C);
235 offset[0]= (event->x - event->prevx)/sima->zoom;
236 offset[1]= (event->y - event->prevy)/sima->zoom;
237 RNA_float_set_array(op->ptr, "offset", offset);
239 view_pan_exec(C, op);
240 return OPERATOR_FINISHED;
243 view_pan_init(C, op, event);
244 return OPERATOR_RUNNING_MODAL;
248 static int view_pan_modal(bContext *C, wmOperator *op, wmEvent *event)
250 SpaceImage *sima= CTX_wm_space_image(C);
251 ViewPanData *vpd= op->customdata;
254 switch(event->type) {
258 offset[0]= (vpd->x - event->x)/sima->zoom;
259 offset[1]= (vpd->y - event->y)/sima->zoom;
260 RNA_float_set_array(op->ptr, "offset", offset);
261 view_pan_exec(C, op);
265 if(event->val==KM_RELEASE) {
266 view_pan_exit(C, op, 0);
267 return OPERATOR_FINISHED;
272 return OPERATOR_RUNNING_MODAL;
275 static int view_pan_cancel(bContext *C, wmOperator *op)
277 view_pan_exit(C, op, 1);
278 return OPERATOR_CANCELLED;
281 void IMAGE_OT_view_pan(wmOperatorType *ot)
284 ot->name= "View Pan";
285 ot->idname= "IMAGE_OT_view_pan";
288 ot->exec= view_pan_exec;
289 ot->invoke= view_pan_invoke;
290 ot->modal= view_pan_modal;
291 ot->cancel= view_pan_cancel;
292 ot->poll= space_image_main_area_poll;
295 ot->flag= OPTYPE_BLOCKING;
298 RNA_def_float_vector(ot->srna, "offset", 2, NULL, -FLT_MAX, FLT_MAX,
299 "Offset", "Offset in floating point units, 1.0 is the width and height of the image.", -FLT_MAX, FLT_MAX);
302 /********************** view zoom operator *********************/
304 typedef struct ViewZoomData {
309 static void view_zoom_init(bContext *C, wmOperator *op, wmEvent *event)
311 SpaceImage *sima= CTX_wm_space_image(C);
314 op->customdata= vpd= MEM_callocN(sizeof(ViewZoomData), "ImageViewZoomData");
315 WM_cursor_modal(CTX_wm_window(C), BC_NSEW_SCROLLCURSOR);
319 vpd->zoom= sima->zoom;
321 WM_event_add_modal_handler(C, op);
324 static void view_zoom_exit(bContext *C, wmOperator *op, int cancel)
326 SpaceImage *sima= CTX_wm_space_image(C);
327 ViewZoomData *vpd= op->customdata;
330 sima->zoom= vpd->zoom;
331 ED_region_tag_redraw(CTX_wm_region(C));
334 WM_cursor_restore(CTX_wm_window(C));
335 MEM_freeN(op->customdata);
338 static int view_zoom_exec(bContext *C, wmOperator *op)
340 SpaceImage *sima= CTX_wm_space_image(C);
341 ARegion *ar= CTX_wm_region(C);
343 sima_zoom_set_factor(sima, ar, RNA_float_get(op->ptr, "factor"));
345 ED_region_tag_redraw(CTX_wm_region(C));
349 if(image_preview_active(curarea, NULL, NULL)) {
350 /* recalculates new preview rect */
351 scrarea_do_windraw(curarea);
352 image_preview_event(2);
356 return OPERATOR_FINISHED;
359 static int view_zoom_invoke(bContext *C, wmOperator *op, wmEvent *event)
361 if (event->type == MOUSEZOOM) {
362 SpaceImage *sima= CTX_wm_space_image(C);
363 ARegion *ar= CTX_wm_region(C);
366 factor= 1.0 + (event->x-event->prevx+event->y-event->prevy)/300.0f;
367 RNA_float_set(op->ptr, "factor", factor);
368 sima_zoom_set(sima, ar, sima->zoom*factor);
369 ED_region_tag_redraw(CTX_wm_region(C));
371 return OPERATOR_FINISHED;
374 view_zoom_init(C, op, event);
375 return OPERATOR_RUNNING_MODAL;
379 static int view_zoom_modal(bContext *C, wmOperator *op, wmEvent *event)
381 SpaceImage *sima= CTX_wm_space_image(C);
382 ARegion *ar= CTX_wm_region(C);
383 ViewZoomData *vpd= op->customdata;
386 switch(event->type) {
388 factor= 1.0 + (vpd->x-event->x+vpd->y-event->y)/300.0f;
389 RNA_float_set(op->ptr, "factor", factor);
390 sima_zoom_set(sima, ar, vpd->zoom*factor);
391 ED_region_tag_redraw(CTX_wm_region(C));
395 if(event->val==KM_RELEASE) {
396 view_zoom_exit(C, op, 0);
397 return OPERATOR_FINISHED;
402 return OPERATOR_RUNNING_MODAL;
405 static int view_zoom_cancel(bContext *C, wmOperator *op)
407 view_zoom_exit(C, op, 1);
408 return OPERATOR_CANCELLED;
411 void IMAGE_OT_view_zoom(wmOperatorType *ot)
414 ot->name= "View Zoom";
415 ot->idname= "IMAGE_OT_view_zoom";
418 ot->exec= view_zoom_exec;
419 ot->invoke= view_zoom_invoke;
420 ot->modal= view_zoom_modal;
421 ot->cancel= view_zoom_cancel;
422 ot->poll= space_image_main_area_poll;
425 ot->flag= OPTYPE_BLOCKING;
428 RNA_def_float(ot->srna, "factor", 0.0f, 0.0f, FLT_MAX,
429 "Factor", "Zoom factor, values higher than 1.0 zoom in, lower values zoom out.", -FLT_MAX, FLT_MAX);
432 /********************** view all operator *********************/
434 /* Updates the fields of the View2D member of the SpaceImage struct.
435 * Default behavior is to reset the position of the image and set the zoom to 1
436 * If the image will not fit within the window rectangle, the zoom is adjusted */
438 static int view_all_exec(bContext *C, wmOperator *UNUSED(op))
442 float aspx, aspy, zoomx, zoomy, w, h;
446 sima= CTX_wm_space_image(C);
447 ar= CTX_wm_region(C);
449 ED_space_image_size(sima, &width, &height);
450 ED_space_image_aspect(sima, &aspx, &aspy);
455 /* check if the image will fit in the image with zoom==1 */
456 width = ar->winrct.xmax - ar->winrct.xmin + 1;
457 height = ar->winrct.ymax - ar->winrct.ymin + 1;
459 if((w >= width || h >= height) && (width > 0 && height > 0)) {
460 /* find the zoom value that will fit the image in the image space */
463 sima_zoom_set(sima, ar, 1.0f/power_of_2(1/MIN2(zoomx, zoomy)));
466 sima_zoom_set(sima, ar, 1.0f);
468 sima->xof= sima->yof= 0.0f;
470 ED_region_tag_redraw(CTX_wm_region(C));
472 return OPERATOR_FINISHED;
475 void IMAGE_OT_view_all(wmOperatorType *ot)
478 ot->name= "View All";
479 ot->idname= "IMAGE_OT_view_all";
482 ot->exec= view_all_exec;
483 ot->poll= space_image_main_area_poll;
486 /********************** view selected operator *********************/
488 static int view_selected_exec(bContext *C, wmOperator *UNUSED(op))
495 float size, min[2], max[2], d[2];
499 sima= CTX_wm_space_image(C);
500 ar= CTX_wm_region(C);
501 scene= (Scene*)CTX_data_scene(C);
502 obedit= CTX_data_edit_object(C);
504 ima= ED_space_image(sima);
505 ED_space_image_size(sima, &width, &height);
508 if(!ED_uvedit_minmax(scene, ima, obedit, min, max))
509 return OPERATOR_CANCELLED;
511 /* adjust offset and zoom */
512 sima->xof= (int)(((min[0] + max[0])*0.5f - 0.5f)*width);
513 sima->yof= (int)(((min[1] + max[1])*0.5f - 0.5f)*height);
515 d[0]= max[0] - min[0];
516 d[1]= max[1] - min[1];
517 size= 0.5*MAX2(d[0], d[1])*MAX2(width, height)/256.0f;
519 if(size<=0.01) size= 0.01;
520 sima_zoom_set(sima, ar, 0.7/size);
522 ED_region_tag_redraw(CTX_wm_region(C));
524 return OPERATOR_FINISHED;
527 static int view_selected_poll(bContext *C)
529 return (space_image_main_area_poll(C) && ED_operator_uvedit(C));
532 void IMAGE_OT_view_selected(wmOperatorType *ot)
535 ot->name= "View Center";
536 ot->idname= "IMAGE_OT_view_selected";
539 ot->exec= view_selected_exec;
540 ot->poll= view_selected_poll;
543 /********************** view zoom in/out operator *********************/
545 static int view_zoom_in_exec(bContext *C, wmOperator *UNUSED(op))
547 SpaceImage *sima= CTX_wm_space_image(C);
548 ARegion *ar= CTX_wm_region(C);
550 sima_zoom_set_factor(sima, ar, 1.25f);
552 ED_region_tag_redraw(CTX_wm_region(C));
554 return OPERATOR_FINISHED;
557 void IMAGE_OT_view_zoom_in(wmOperatorType *ot)
560 ot->name= "View Zoom In";
561 ot->idname= "IMAGE_OT_view_zoom_in";
564 ot->exec= view_zoom_in_exec;
565 ot->poll= space_image_main_area_poll;
568 static int view_zoom_out_exec(bContext *C, wmOperator *UNUSED(op))
570 SpaceImage *sima= CTX_wm_space_image(C);
571 ARegion *ar= CTX_wm_region(C);
573 sima_zoom_set_factor(sima, ar, 0.8f);
575 ED_region_tag_redraw(CTX_wm_region(C));
577 return OPERATOR_FINISHED;
580 void IMAGE_OT_view_zoom_out(wmOperatorType *ot)
583 ot->name= "View Zoom Out";
584 ot->idname= "IMAGE_OT_view_zoom_out";
587 ot->exec= view_zoom_out_exec;
588 ot->poll= space_image_main_area_poll;
591 /********************** view zoom ratio operator *********************/
593 static int view_zoom_ratio_exec(bContext *C, wmOperator *op)
595 SpaceImage *sima= CTX_wm_space_image(C);
596 ARegion *ar= CTX_wm_region(C);
598 sima_zoom_set(sima, ar, RNA_float_get(op->ptr, "ratio"));
600 /* ensure pixel exact locations for draw */
601 sima->xof= (int)sima->xof;
602 sima->yof= (int)sima->yof;
606 if(image_preview_active(curarea, NULL, NULL)) {
607 /* recalculates new preview rect */
608 scrarea_do_windraw(curarea);
609 image_preview_event(2);
613 ED_region_tag_redraw(CTX_wm_region(C));
615 return OPERATOR_FINISHED;
618 void IMAGE_OT_view_zoom_ratio(wmOperatorType *ot)
621 ot->name= "View Zoom Ratio";
622 ot->idname= "IMAGE_OT_view_zoom_ratio";
625 ot->exec= view_zoom_ratio_exec;
626 ot->poll= space_image_main_area_poll;
629 RNA_def_float(ot->srna, "ratio", 0.0f, 0.0f, FLT_MAX,
630 "Ratio", "Zoom ratio, 1.0 is 1:1, higher is zoomed in, lower is zoomed out.", -FLT_MAX, FLT_MAX);
633 /**************** load/replace/save callbacks ******************/
635 /* XXX make dynamic */
636 static const EnumPropertyItem image_file_type_items[] = {
637 {R_TARGA, "TARGA", 0, "Targa", ""},
638 {R_RAWTGA, "TARGA RAW", 0, "Targa Raw", ""},
639 {R_PNG, "PNG", 0, "PNG", ""},
640 {R_BMP, "BMP", 0, "BMP", ""},
641 {R_JPEG90, "JPEG", 0, "Jpeg", ""},
643 {R_JP2, "JPEG_2000", 0, "Jpeg 2000", ""},
645 {R_IRIS, "IRIS", 0, "Iris", ""},
647 {R_TIFF, "TIFF", 0, "Tiff", ""},
650 {R_RADHDR, "RADIANCE_HDR", 0, "Radiance HDR", ""},
653 {R_CINEON, "CINEON", 0, "Cineon", ""},
654 {R_DPX, "DPX", 0, "DPX", ""},
657 {R_OPENEXR, "OPENEXR", 0, "OpenEXR", ""},
658 /* saving sequences of multilayer won't work, they copy buffers */
659 /*if(ima->source==IMA_SRC_SEQUENCE && ima->type==IMA_TYPE_MULTILAYER);
661 {R_MULTILAYER, "MULTILAYER", 0, "MultiLayer", ""},
663 {0, NULL, 0, NULL, NULL}};
665 static void image_filesel(bContext *C, wmOperator *op, const char *path)
667 RNA_string_set(op->ptr, "filepath", path);
668 WM_event_add_fileselect(C, op);
671 /******************** open image operator ********************/
673 static void open_init(bContext *C, wmOperator *op)
675 PropertyPointerRNA *pprop;
677 op->customdata= pprop= MEM_callocN(sizeof(PropertyPointerRNA), "OpenPropertyPointerRNA");
678 uiIDContextProperty(C, &pprop->ptr, &pprop->prop);
681 static int open_cancel(bContext *UNUSED(C), wmOperator *op)
683 MEM_freeN(op->customdata);
684 op->customdata= NULL;
685 return OPERATOR_CANCELLED;
688 static int open_exec(bContext *C, wmOperator *op)
690 SpaceImage *sima= CTX_wm_space_image(C); /* XXX other space types can call */
691 Scene *scene= CTX_data_scene(C);
692 Object *obedit= CTX_data_edit_object(C);
693 ImageUser *iuser= NULL;
694 PropertyPointerRNA *pprop;
699 RNA_string_get(op->ptr, "filepath", str);
700 /* default to frame 1 if there's no scene in context */
704 ima= BKE_add_image_file(str);
707 if(op->customdata) MEM_freeN(op->customdata);
708 BKE_reportf(op->reports, RPT_ERROR, "Can't read: \"%s\", %s.", str, errno ? strerror(errno) : "Unsupported image format");
709 return OPERATOR_CANCELLED;
716 pprop= op->customdata;
719 /* when creating new ID blocks, use is already 1, but RNA
720 * pointer se also increases user, so this compensates it */
723 RNA_id_pointer_create(&ima->id, &idptr);
724 RNA_property_pointer_set(&pprop->ptr, pprop->prop, idptr);
725 RNA_property_update(C, &pprop->ptr, pprop->prop);
728 ED_space_image_set(C, sima, scene, obedit, ima);
732 Tex *tex= CTX_data_pointer_get_type(C, "texture", &RNA_Texture).data;
733 if(tex && tex->type==TEX_IMAGE)
738 /* initialize because of new image */
745 BKE_image_signal(ima, iuser, IMA_SIGNAL_RELOAD);
746 WM_event_add_notifier(C, NC_IMAGE|NA_EDITED, ima);
748 MEM_freeN(op->customdata);
750 return OPERATOR_FINISHED;
753 static int open_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
755 SpaceImage *sima= CTX_wm_space_image(C); /* XXX other space types can call */
756 char *path=U.textudir;
764 Tex *tex= CTX_data_pointer_get_type(C, "texture", &RNA_Texture).data;
765 if(tex && tex->type==TEX_IMAGE)
773 if(!RNA_property_is_set(op->ptr, "relative_path"))
774 RNA_boolean_set(op->ptr, "relative_path", U.flag & USER_RELPATHS);
776 if(RNA_property_is_set(op->ptr, "filepath"))
777 return open_exec(C, op);
781 image_filesel(C, op, path);
783 return OPERATOR_RUNNING_MODAL;
786 /* called by other space types too */
787 void IMAGE_OT_open(wmOperatorType *ot)
791 ot->idname= "IMAGE_OT_open";
795 ot->invoke= open_invoke;
796 ot->cancel= open_cancel;
799 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
802 WM_operator_properties_filesel(ot, FOLDERFILE|IMAGEFILE|MOVIEFILE, FILE_SPECIAL, FILE_OPENFILE, WM_FILESEL_FILEPATH|WM_FILESEL_RELPATH);
805 /******************** replace image operator ********************/
807 static int replace_exec(bContext *C, wmOperator *op)
809 SpaceImage *sima= CTX_wm_space_image(C);
813 return OPERATOR_CANCELLED;
815 RNA_string_get(op->ptr, "filepath", str);
816 BLI_strncpy(sima->image->name, str, sizeof(sima->image->name)); /* we cant do much if the str is longer then 240 :/ */
818 BKE_image_signal(sima->image, &sima->iuser, IMA_SIGNAL_RELOAD);
819 WM_event_add_notifier(C, NC_IMAGE|NA_EDITED, sima->image);
821 return OPERATOR_FINISHED;
824 static int replace_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
826 SpaceImage *sima= CTX_wm_space_image(C);
829 return OPERATOR_CANCELLED;
831 if(RNA_property_is_set(op->ptr, "filepath"))
832 return replace_exec(C, op);
834 if(!RNA_property_is_set(op->ptr, "relative_path"))
835 RNA_boolean_set(op->ptr, "relative_path", (strncmp(sima->image->name, "//", 2))==0);
837 image_filesel(C, op, sima->image->name);
839 return OPERATOR_RUNNING_MODAL;
842 void IMAGE_OT_replace(wmOperatorType *ot)
846 ot->idname= "IMAGE_OT_replace";
849 ot->exec= replace_exec;
850 ot->invoke= replace_invoke;
851 ot->poll= space_image_poll;
854 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
857 WM_operator_properties_filesel(ot, FOLDERFILE|IMAGEFILE|MOVIEFILE, FILE_SPECIAL, FILE_OPENFILE, WM_FILESEL_FILEPATH|WM_FILESEL_RELPATH);
860 /******************** save image as operator ********************/
862 /* assumes name is FILE_MAX */
863 /* ima->name and ibuf->name should end up the same */
864 static void save_image_doit(bContext *C, SpaceImage *sima, Scene *scene, wmOperator *op, char *path, int do_newpath)
866 Image *ima= ED_space_image(sima);
868 ImBuf *ibuf= ED_space_image_acquire_buffer(sima, &lock);
871 Main *bmain= CTX_data_main(C);
872 const short relative= (RNA_struct_find_property(op->ptr, "relative_path") && RNA_boolean_get(op->ptr, "relative_path"));
873 const short save_copy= (RNA_struct_find_property(op->ptr, "copy") && RNA_boolean_get(op->ptr, "copy"));
876 BLI_path_abs(path, bmain->name);
877 /* old global to ensure a 2nd save goes to same dir */
878 BLI_strncpy(G.ima, path, sizeof(G.ima));
882 if(ima->type == IMA_TYPE_R_RESULT) {
883 /* enforce user setting for RGB or RGBA, but skip BW */
884 if(scene->r.planes==32) {
887 else if(scene->r.planes==24) {
892 /* TODO, better solution, if a 24bit image is painted onto it may contain alpha */
893 if(ibuf->userflags & IB_BITMAPDIRTY) { /* it has been painted onto */
894 /* checks each pixel, not ideal */
895 ibuf->depth= BKE_alphatest_ibuf(ibuf) ? 32 : 24;
899 if(scene->r.scemode & R_EXTENSION) {
900 BKE_add_image_extension(path, sima->imtypenr);
903 if(sima->imtypenr==R_MULTILAYER) {
904 RenderResult *rr= BKE_image_acquire_renderresult(scene, ima);
906 RE_WriteRenderResult(rr, path, scene->r.quality);
910 BKE_report(op->reports, RPT_ERROR, "Did not write, no Multilayer Image");
912 BKE_image_release_renderresult(scene, ima);
914 else if (BKE_write_ibuf(scene, ibuf, path, sima->imtypenr, scene->r.subimtype, scene->r.quality)) {
920 BLI_path_rel(path, bmain->name); /* only after saving */
922 if(ibuf->name[0]==0) {
923 BLI_strncpy(ibuf->name, path, sizeof(ibuf->name));
924 BLI_strncpy(ima->name, path, sizeof(ima->name));
929 BLI_strncpy(ima->name, path, sizeof(ima->name));
930 BLI_strncpy(ibuf->name, path, sizeof(ibuf->name));
933 ibuf->userflags &= ~IB_BITMAPDIRTY;
936 if(ima->type==IMA_TYPE_R_RESULT) {
937 ima->type= IMA_TYPE_IMAGE;
939 /* workaround to ensure the render result buffer is no longer used
940 * by this image, otherwise can crash when a new render result is
942 if(ibuf->rect && !(ibuf->mall & IB_rect))
943 imb_freerectImBuf(ibuf);
944 if(ibuf->rect_float && !(ibuf->mall & IB_rectfloat))
945 imb_freerectfloatImBuf(ibuf);
946 if(ibuf->zbuf && !(ibuf->mall & IB_zbuf))
947 IMB_freezbufImBuf(ibuf);
948 if(ibuf->zbuf_float && !(ibuf->mall & IB_zbuffloat))
949 IMB_freezbuffloatImBuf(ibuf);
951 if( ELEM(ima->source, IMA_SRC_GENERATED, IMA_SRC_VIEWER)) {
952 ima->source= IMA_SRC_FILE;
953 ima->type= IMA_TYPE_IMAGE;
958 BKE_reportf(op->reports, RPT_ERROR, "Couldn't write image: %s", path);
962 WM_event_add_notifier(C, NC_IMAGE|NA_EDITED, sima->image);
967 ED_space_image_release_buffer(sima, lock);
970 static int save_as_exec(bContext *C, wmOperator *op)
972 SpaceImage *sima= CTX_wm_space_image(C);
973 Scene *scene= CTX_data_scene(C);
974 Image *ima = ED_space_image(sima);
978 return OPERATOR_CANCELLED;
980 sima->imtypenr= RNA_enum_get(op->ptr, "file_type");
981 RNA_string_get(op->ptr, "filepath", str);
983 save_image_doit(C, sima, scene, op, str, TRUE);
985 return OPERATOR_FINISHED;
989 static int save_as_check(bContext *UNUSED(C), wmOperator *op)
991 char filepath[FILE_MAX];
992 RNA_string_get(op->ptr, "filepath", filepath);
993 if(BKE_add_image_extension(filepath, RNA_enum_get(op->ptr, "file_type"))) {
994 RNA_string_set(op->ptr, "filepath", filepath);
1000 static int save_as_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
1002 SpaceImage *sima= CTX_wm_space_image(C);
1003 Image *ima = ED_space_image(sima);
1004 Scene *scene= CTX_data_scene(C);
1006 char filename[FILE_MAX];
1010 if(!RNA_property_is_set(op->ptr, "relative_path"))
1011 RNA_boolean_set(op->ptr, "relative_path", U.flag & USER_RELPATHS);
1013 if(RNA_property_is_set(op->ptr, "filepath"))
1014 return save_as_exec(C, op);
1017 return OPERATOR_CANCELLED;
1019 /* always opens fileselect */
1020 ibuf= ED_space_image_acquire_buffer(sima, &lock);
1023 /* cant save multilayer sequence, ima->rr isn't valid for a specific frame */
1024 if(ima->rr && !(ima->source==IMA_SRC_SEQUENCE && ima->type==IMA_TYPE_MULTILAYER))
1025 sima->imtypenr= R_MULTILAYER;
1026 else if(ima->type==IMA_TYPE_R_RESULT)
1027 sima->imtypenr= scene->r.imtype;
1028 else if (ima->source == IMA_SRC_GENERATED)
1029 sima->imtypenr= R_PNG;
1031 sima->imtypenr= BKE_ftype_to_imtype(ibuf->ftype);
1033 RNA_enum_set(op->ptr, "file_type", sima->imtypenr);
1035 if(ibuf->name[0]==0)
1036 if ( (G.ima[0] == '/') && (G.ima[1] == '/') && (G.ima[2] == '\0') ) {
1037 BLI_strncpy(filename, "//untitled", FILE_MAX);
1039 BLI_strncpy(filename, G.ima, FILE_MAX);
1042 BLI_strncpy(filename, ibuf->name, FILE_MAX);
1044 /* enable save_copy by default for render results */
1045 if(ELEM(ima->type, IMA_TYPE_R_RESULT, IMA_TYPE_COMPOSITE) && !RNA_property_is_set(op->ptr, "copy")) {
1046 RNA_boolean_set(op->ptr, "copy", TRUE);
1049 // XXX note: we can give default menu enums to operator for this
1050 image_filesel(C, op, filename);
1052 ED_space_image_release_buffer(sima, lock);
1054 return OPERATOR_RUNNING_MODAL;
1057 ED_space_image_release_buffer(sima, lock);
1059 return OPERATOR_CANCELLED;
1062 void IMAGE_OT_save_as(wmOperatorType *ot)
1065 ot->name= "Save As";
1066 ot->idname= "IMAGE_OT_save_as";
1069 ot->exec= save_as_exec;
1070 ot->check= save_as_check;
1071 ot->invoke= save_as_invoke;
1072 ot->poll= space_image_buffer_exists_poll;
1075 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1078 RNA_def_enum(ot->srna, "file_type", image_file_type_items, R_PNG, "File Type", "File type to save image as.");
1079 WM_operator_properties_filesel(ot, FOLDERFILE|IMAGEFILE|MOVIEFILE, FILE_SPECIAL, FILE_SAVE, WM_FILESEL_FILEPATH|WM_FILESEL_RELPATH);
1081 RNA_def_boolean(ot->srna, "copy", 0, "Copy", "Create a new image file without modifying the current image in blender");
1084 /******************** save image operator ********************/
1086 static int save_exec(bContext *C, wmOperator *op)
1088 Main *bmain= CTX_data_main(C);
1089 SpaceImage *sima= CTX_wm_space_image(C);
1090 Image *ima = ED_space_image(sima);
1092 ImBuf *ibuf= ED_space_image_acquire_buffer(sima, &lock);
1093 Scene *scene= CTX_data_scene(C);
1095 char name[FILE_MAX];
1098 ED_space_image_release_buffer(sima, lock);
1099 return OPERATOR_CANCELLED;
1102 /* if exists, saves over without fileselect */
1104 BLI_strncpy(name, ima->name, FILE_MAX);
1106 BLI_strncpy(name, G.ima, FILE_MAX);
1108 BLI_path_abs(name, bmain->name);
1110 if(BLI_exists(name) && BLI_is_writable(name)) {
1111 rr= BKE_image_acquire_renderresult(scene, ima);
1114 sima->imtypenr= R_MULTILAYER;
1116 sima->imtypenr= BKE_ftype_to_imtype(ibuf->ftype);
1118 BKE_image_release_renderresult(scene, ima);
1119 ED_space_image_release_buffer(sima, lock);
1121 save_image_doit(C, sima, scene, op, name, FALSE);
1124 ED_space_image_release_buffer(sima, lock);
1126 BKE_report(op->reports, RPT_ERROR, "Can not save image.");
1127 return OPERATOR_CANCELLED;
1130 return OPERATOR_FINISHED;
1133 void IMAGE_OT_save(wmOperatorType *ot)
1137 ot->idname= "IMAGE_OT_save";
1140 ot->exec= save_exec;
1141 ot->poll= space_image_file_exists_poll;
1144 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1147 /******************* save sequence operator ********************/
1149 static int save_sequence_exec(bContext *C, wmOperator *op)
1151 Main *bmain= CTX_data_main(C);
1152 SpaceImage *sima= CTX_wm_space_image(C);
1155 char di[FILE_MAX], fi[FILE_MAX];
1157 if(sima->image==NULL)
1158 return OPERATOR_CANCELLED;
1160 if(sima->image->source!=IMA_SRC_SEQUENCE) {
1161 BKE_report(op->reports, RPT_ERROR, "Can only save sequence on image sequences.");
1162 return OPERATOR_CANCELLED;
1165 if(sima->image->type==IMA_TYPE_MULTILAYER) {
1166 BKE_report(op->reports, RPT_ERROR, "Can't save multilayer sequences.");
1167 return OPERATOR_CANCELLED;
1171 for(ibuf= sima->image->ibufs.first; ibuf; ibuf= ibuf->next)
1172 if(ibuf->userflags & IB_BITMAPDIRTY)
1176 BKE_report(op->reports, RPT_WARNING, "No images have been changed.");
1177 return OPERATOR_CANCELLED;
1180 /* get a filename for menu */
1181 for(ibuf= sima->image->ibufs.first; ibuf; ibuf= ibuf->next)
1182 if(ibuf->userflags & IB_BITMAPDIRTY)
1185 BLI_strncpy(di, ibuf->name, FILE_MAX);
1186 BLI_splitdirstring(di, fi);
1188 BKE_reportf(op->reports, RPT_INFO, "%d Image(s) will be saved in %s", tot, di);
1190 for(ibuf= sima->image->ibufs.first; ibuf; ibuf= ibuf->next) {
1191 if(ibuf->userflags & IB_BITMAPDIRTY) {
1192 char name[FILE_MAX];
1193 BLI_strncpy(name, ibuf->name, sizeof(name));
1195 BLI_path_abs(name, bmain->name);
1197 if(0 == IMB_saveiff(ibuf, name, IB_rect | IB_zbuf | IB_zbuffloat)) {
1198 BKE_reportf(op->reports, RPT_ERROR, "Could not write image %s.", name);
1202 BKE_reportf(op->reports, RPT_INFO, "Saved: %s\n", ibuf->name);
1203 ibuf->userflags &= ~IB_BITMAPDIRTY;
1207 return OPERATOR_FINISHED;
1210 void IMAGE_OT_save_sequence(wmOperatorType *ot)
1213 ot->name= "Save Sequence";
1214 ot->idname= "IMAGE_OT_save_sequence";
1217 ot->exec= save_sequence_exec;
1218 ot->poll= space_image_buffer_exists_poll;
1221 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1224 /******************** reload image operator ********************/
1226 static int reload_exec(bContext *C, wmOperator *UNUSED(op))
1228 Image *ima= CTX_data_edit_image(C);
1229 SpaceImage *sima= CTX_wm_space_image(C);
1232 return OPERATOR_CANCELLED;
1235 BKE_image_signal(ima, (sima)? &sima->iuser: NULL, IMA_SIGNAL_RELOAD);
1237 WM_event_add_notifier(C, NC_IMAGE|NA_EDITED, ima);
1239 return OPERATOR_FINISHED;
1242 void IMAGE_OT_reload(wmOperatorType *ot)
1246 ot->idname= "IMAGE_OT_reload";
1249 ot->exec= reload_exec;
1252 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1255 /********************** new image operator *********************/
1257 static int image_new_exec(bContext *C, wmOperator *op)
1263 PointerRNA ptr, idptr;
1267 int width, height, floatbuf, uvtestgrid, alpha;
1269 /* retrieve state */
1270 sima= CTX_wm_space_image(C);
1271 scene= (Scene*)CTX_data_scene(C);
1272 obedit= CTX_data_edit_object(C);
1274 RNA_string_get(op->ptr, "name", name);
1275 width= RNA_int_get(op->ptr, "width");
1276 height= RNA_int_get(op->ptr, "height");
1277 floatbuf= RNA_boolean_get(op->ptr, "float");
1278 uvtestgrid= RNA_boolean_get(op->ptr, "uv_test_grid");
1279 RNA_float_get_array(op->ptr, "color", color);
1280 alpha= RNA_boolean_get(op->ptr, "alpha");
1282 if (!floatbuf && scene->r.color_mgt_flag & R_COLOR_MANAGEMENT)
1283 linearrgb_to_srgb_v3_v3(color, color);
1288 ima = BKE_add_image_size(width, height, name, alpha ? 32 : 24, floatbuf, uvtestgrid, color);
1291 return OPERATOR_CANCELLED;
1294 uiIDContextProperty(C, &ptr, &prop);
1297 /* when creating new ID blocks, use is already 1, but RNA
1298 * pointer se also increases user, so this compensates it */
1301 RNA_id_pointer_create(&ima->id, &idptr);
1302 RNA_property_pointer_set(&ptr, prop, idptr);
1303 RNA_property_update(C, &ptr, prop);
1306 ED_space_image_set(C, sima, scene, obedit, ima);
1309 BKE_image_signal(ima, (sima)? &sima->iuser: NULL, IMA_SIGNAL_USER_NEW_IMAGE);
1311 return OPERATOR_FINISHED;
1314 /* XXX, Ton is not a fan of OK buttons but using this function to avoid undo/redo bug while in mesh-editmode, - campbell */
1315 static int image_new_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
1317 return WM_operator_props_dialog_popup(C, op, 300, 100);
1321 void IMAGE_OT_new(wmOperatorType *ot)
1324 static float default_color[4]= {0.0f, 0.0f, 0.0f, 1.0f};
1328 ot->idname= "IMAGE_OT_new";
1331 ot->exec= image_new_exec;
1332 ot->invoke= image_new_invoke;
1335 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1338 RNA_def_string(ot->srna, "name", "untitled", 21, "Name", "Image datablock name.");
1339 RNA_def_int(ot->srna, "width", 1024, 1, INT_MAX, "Width", "Image width.", 1, 16384);
1340 RNA_def_int(ot->srna, "height", 1024, 1, INT_MAX, "Height", "Image height.", 1, 16384);
1341 prop= RNA_def_float_color(ot->srna, "color", 4, NULL, 0.0f, FLT_MAX, "Color", "Default fill color.", 0.0f, 1.0f);
1342 RNA_def_property_float_array_default(prop, default_color);
1343 RNA_def_boolean(ot->srna, "alpha", 1, "Alpha", "Create an image with an alpha channel.");
1344 RNA_def_boolean(ot->srna, "uv_test_grid", 0, "UV Test Grid", "Fill the image with a grid for UV map testing.");
1345 RNA_def_boolean(ot->srna, "float", 0, "32 bit Float", "Create image with 32 bit floating point bit depth.");
1348 /********************* pack operator *********************/
1350 static int pack_test(bContext *C, wmOperator *op)
1352 Image *ima= CTX_data_edit_image(C);
1353 int as_png= RNA_boolean_get(op->ptr, "as_png");
1357 if(!as_png && ima->packedfile)
1360 if(ima->source==IMA_SRC_SEQUENCE || ima->source==IMA_SRC_MOVIE) {
1361 BKE_report(op->reports, RPT_ERROR, "Packing movies or image sequences not supported.");
1368 static int pack_exec(bContext *C, wmOperator *op)
1370 Image *ima= CTX_data_edit_image(C);
1371 ImBuf *ibuf= BKE_image_get_ibuf(ima, NULL);
1372 int as_png= RNA_boolean_get(op->ptr, "as_png");
1374 if(!pack_test(C, op))
1375 return OPERATOR_CANCELLED;
1377 if(!as_png && (ibuf && (ibuf->userflags & IB_BITMAPDIRTY))) {
1378 BKE_report(op->reports, RPT_ERROR, "Can't pack edited image from disk, only as internal PNG.");
1379 return OPERATOR_CANCELLED;
1383 BKE_image_memorypack(ima);
1385 ima->packedfile= newPackedFile(op->reports, ima->name);
1387 WM_event_add_notifier(C, NC_IMAGE|NA_EDITED, ima);
1389 return OPERATOR_FINISHED;
1392 static int pack_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
1394 Image *ima= CTX_data_edit_image(C);
1395 ImBuf *ibuf= BKE_image_get_ibuf(ima, NULL);
1398 int as_png= RNA_boolean_get(op->ptr, "as_png");
1400 if(!pack_test(C, op))
1401 return OPERATOR_CANCELLED;
1403 if(!as_png && (ibuf && (ibuf->userflags & IB_BITMAPDIRTY))) {
1404 pup= uiPupMenuBegin(C, "OK", ICON_QUESTION);
1405 layout= uiPupMenuLayout(pup);
1406 uiItemBooleanO(layout, "Can't pack edited image from disk. Pack as internal PNG?", ICON_NULL, op->idname, "as_png", 1);
1407 uiPupMenuEnd(C, pup);
1409 return OPERATOR_CANCELLED;
1412 return pack_exec(C, op);
1415 void IMAGE_OT_pack(wmOperatorType *ot)
1419 ot->description= "Pack an image as embedded data into the .blend file";
1420 ot->idname= "IMAGE_OT_pack";
1423 ot->exec= pack_exec;
1424 ot->invoke= pack_invoke;
1427 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1430 RNA_def_boolean(ot->srna, "as_png", 0, "Pack As PNG", "Pack image as lossless PNG.");
1433 /********************* unpack operator *********************/
1435 static int image_unpack_exec(bContext *C, wmOperator *op)
1437 Image *ima= CTX_data_edit_image(C);
1438 int method= RNA_enum_get(op->ptr, "method");
1440 /* find the suppplied image by name */
1441 if (RNA_property_is_set(op->ptr, "id")) {
1443 RNA_string_get(op->ptr, "id", imaname);
1444 ima = BLI_findstring(&CTX_data_main(C)->image, imaname, offsetof(ID, name) + 2);
1445 if (!ima) ima = CTX_data_edit_image(C);
1448 if(!ima || !ima->packedfile)
1449 return OPERATOR_CANCELLED;
1451 if(ima->source==IMA_SRC_SEQUENCE || ima->source==IMA_SRC_MOVIE) {
1452 BKE_report(op->reports, RPT_ERROR, "Unpacking movies or image sequences not supported.");
1453 return OPERATOR_CANCELLED;
1456 if(G.fileflags & G_AUTOPACK)
1457 BKE_report(op->reports, RPT_WARNING, "AutoPack is enabled, so image will be packed again on file save.");
1459 unpackImage(op->reports, ima, method);
1461 WM_event_add_notifier(C, NC_IMAGE|NA_EDITED, ima);
1463 return OPERATOR_FINISHED;
1466 static int image_unpack_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
1468 Image *ima= CTX_data_edit_image(C);
1470 if(RNA_property_is_set(op->ptr, "id"))
1471 return image_unpack_exec(C, op);
1473 if(!ima || !ima->packedfile)
1474 return OPERATOR_CANCELLED;
1476 if(ima->source==IMA_SRC_SEQUENCE || ima->source==IMA_SRC_MOVIE) {
1477 BKE_report(op->reports, RPT_ERROR, "Unpacking movies or image sequences not supported.");
1478 return OPERATOR_CANCELLED;
1481 if(G.fileflags & G_AUTOPACK)
1482 BKE_report(op->reports, RPT_WARNING, "AutoPack is enabled, so image will be packed again on file save.");
1484 unpack_menu(C, "IMAGE_OT_unpack", ima->id.name+2, ima->name, "textures", ima->packedfile);
1486 return OPERATOR_FINISHED;
1489 void IMAGE_OT_unpack(wmOperatorType *ot)
1493 ot->description= "Save an image packed in the .blend file to disk";
1494 ot->idname= "IMAGE_OT_unpack";
1497 ot->exec= image_unpack_exec;
1498 ot->invoke= image_unpack_invoke;
1501 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1504 RNA_def_enum(ot->srna, "method", unpack_method_items, PF_USE_LOCAL, "Method", "How to unpack.");
1505 RNA_def_string(ot->srna, "id", "", 21, "Image Name", "Image datablock name to unpack."); /* XXX, weark!, will fail with library, name collisions */
1508 /******************** sample image operator ********************/
1510 typedef struct ImageSampleInfo {
1529 static void sample_draw(const bContext *UNUSED(C), ARegion *ar, void *arg_info)
1531 ImageSampleInfo *info= arg_info;
1533 draw_image_info(ar, info->channels, info->x, info->y, info->colp, info->colfp, info->zp, info->zfp);
1537 static void sample_apply(bContext *C, wmOperator *op, wmEvent *event)
1539 SpaceImage *sima= CTX_wm_space_image(C);
1540 ARegion *ar= CTX_wm_region(C);
1542 ImBuf *ibuf= ED_space_image_acquire_buffer(sima, &lock);
1543 ImageSampleInfo *info= op->customdata;
1548 ED_space_image_release_buffer(sima, lock);
1552 mx= event->x - ar->winrct.xmin;
1553 my= event->y - ar->winrct.ymin;
1554 UI_view2d_region_to_view(&ar->v2d, mx, my, &fx, &fy);
1556 if(fx>=0.0 && fy>=0.0 && fx<1.0 && fy<1.0) {
1559 int x= (int)(fx*ibuf->x), y= (int)(fy*ibuf->y);
1561 CLAMP(x, 0, ibuf->x-1);
1562 CLAMP(y, 0, ibuf->y-1);
1567 info->channels= ibuf->channels;
1575 cp= (char *)(ibuf->rect + y*ibuf->x + x);
1577 info->col[0]= cp[0];
1578 info->col[1]= cp[1];
1579 info->col[2]= cp[2];
1580 info->col[3]= cp[3];
1581 info->colp= info->col;
1583 info->colf[0]= (float)cp[0]/255.0f;
1584 info->colf[1]= (float)cp[1]/255.0f;
1585 info->colf[2]= (float)cp[2]/255.0f;
1586 info->colf[3]= (float)cp[3]/255.0f;
1587 info->colfp= info->colf;
1589 if(ibuf->rect_float) {
1590 fp= (ibuf->rect_float + (ibuf->channels)*(y*ibuf->x + x));
1592 info->colf[0]= fp[0];
1593 info->colf[1]= fp[1];
1594 info->colf[2]= fp[2];
1595 info->colf[3]= fp[3];
1596 info->colfp= info->colf;
1600 info->z= ibuf->zbuf[y*ibuf->x + x];
1603 if(ibuf->zbuf_float) {
1604 info->zf= ibuf->zbuf_float[y*ibuf->x + x];
1605 info->zfp= &info->zf;
1608 if(sima->cumap && ibuf->channels==4) {
1609 /* we reuse this callback for set curves point operators */
1610 if(RNA_struct_find_property(op->ptr, "point")) {
1611 int point= RNA_enum_get(op->ptr, "point");
1614 curvemapping_set_black_white(sima->cumap, NULL, info->colfp);
1615 if(ibuf->rect_float)
1616 curvemapping_do_ibuf(sima->cumap, ibuf);
1618 else if(point == 0) {
1619 curvemapping_set_black_white(sima->cumap, info->colfp, NULL);
1620 if(ibuf->rect_float)
1621 curvemapping_do_ibuf(sima->cumap, ibuf);
1626 // XXX node curve integration ..
1629 ScrArea *sa, *cur= curarea;
1631 node_curvemap_sample(fp); /* sends global to node editor */
1632 for(sa= G.curscreen->areabase.first; sa; sa= sa->next) {
1633 if(sa->spacetype==SPACE_NODE) {
1634 areawinset(sa->win);
1635 scrarea_do_windraw(sa);
1638 node_curvemap_sample(NULL); /* clears global in node editor */
1646 ED_space_image_release_buffer(sima, lock);
1647 ED_area_tag_redraw(CTX_wm_area(C));
1650 static void sample_exit(bContext *C, wmOperator *op)
1652 ImageSampleInfo *info= op->customdata;
1654 ED_region_draw_cb_exit(info->art, info->draw_handle);
1655 ED_area_tag_redraw(CTX_wm_area(C));
1659 static int sample_invoke(bContext *C, wmOperator *op, wmEvent *event)
1661 SpaceImage *sima= CTX_wm_space_image(C);
1662 ARegion *ar= CTX_wm_region(C);
1663 ImageSampleInfo *info;
1665 if(!ED_space_image_has_buffer(sima))
1666 return OPERATOR_CANCELLED;
1668 info= MEM_callocN(sizeof(ImageSampleInfo), "ImageSampleInfo");
1669 info->art= ar->type;
1670 info->draw_handle = ED_region_draw_cb_activate(ar->type, sample_draw, info, REGION_DRAW_POST_PIXEL);
1671 op->customdata= info;
1673 sample_apply(C, op, event);
1675 WM_event_add_modal_handler(C, op);
1677 return OPERATOR_RUNNING_MODAL;
1680 static int sample_modal(bContext *C, wmOperator *op, wmEvent *event)
1682 switch(event->type) {
1684 case RIGHTMOUSE: // XXX hardcoded
1686 return OPERATOR_CANCELLED;
1688 sample_apply(C, op, event);
1692 return OPERATOR_RUNNING_MODAL;
1695 static int sample_cancel(bContext *C, wmOperator *op)
1698 return OPERATOR_CANCELLED;
1701 void IMAGE_OT_sample(wmOperatorType *ot)
1705 ot->idname= "IMAGE_OT_sample";
1708 ot->invoke= sample_invoke;
1709 ot->modal= sample_modal;
1710 ot->cancel= sample_cancel;
1711 ot->poll= space_image_main_area_poll;
1714 ot->flag= OPTYPE_BLOCKING;
1717 /******************** sample line operator ********************/
1718 static int sample_line_exec(bContext *C, wmOperator *op)
1720 SpaceImage *sima= CTX_wm_space_image(C);
1721 ARegion *ar= CTX_wm_region(C);
1722 Scene *scene= CTX_data_scene(C);
1724 int x_start= RNA_int_get(op->ptr, "xstart");
1725 int y_start= RNA_int_get(op->ptr, "ystart");
1726 int x_end= RNA_int_get(op->ptr, "xend");
1727 int y_end= RNA_int_get(op->ptr, "yend");
1730 ImBuf *ibuf= ED_space_image_acquire_buffer(sima, &lock);
1731 Histogram *hist= &sima->sample_line_hist;
1733 float x1f, y1f, x2f, y2f;
1741 ED_space_image_release_buffer(sima, lock);
1742 return OPERATOR_CANCELLED;
1745 if (ibuf->channels < 3) {
1746 ED_space_image_release_buffer(sima, lock);
1747 return OPERATOR_CANCELLED;
1750 UI_view2d_region_to_view(&ar->v2d, x_start, y_start, &x1f, &y1f);
1751 UI_view2d_region_to_view(&ar->v2d, x_end, y_end, &x2f, &y2f);
1752 x1= 0.5f+ x1f*ibuf->x;
1753 x2= 0.5f+ x2f*ibuf->x;
1754 y1= 0.5f+ y1f*ibuf->y;
1755 y2= 0.5f+ y2f*ibuf->y;
1758 hist->x_resolution = 256;
1762 for (i=0; i<256; i++) {
1763 x= (int)(0.5f + x1 + (float)i*(x2-x1)/255.0f);
1764 y= (int)(0.5f + y1 + (float)i*(y2-y1)/255.0f);
1766 if (x<0 || y<0 || x>=ibuf->x || y>=ibuf->y) {
1767 hist->data_luma[i] = hist->data_r[i] = hist->data_g[i]= hist->data_b[i] = 0.0f;
1769 if (ibuf->rect_float) {
1770 fp= (ibuf->rect_float + (ibuf->channels)*(y*ibuf->x + x));
1772 if (scene->r.color_mgt_flag & R_COLOR_MANAGEMENT)
1773 linearrgb_to_srgb_v3_v3(rgb, fp);
1775 copy_v3_v3(rgb, fp);
1777 hist->data_r[i] = rgb[0];
1778 hist->data_g[i] = rgb[1];
1779 hist->data_b[i] = rgb[2];
1780 hist->data_luma[i] = (0.299f*rgb[0] + 0.587f*rgb[1] + 0.114f*rgb[2]);
1782 else if (ibuf->rect) {
1783 cp= (unsigned char *)(ibuf->rect + y*ibuf->x + x);
1784 hist->data_r[i] = (float)cp[0]/255.0f;
1785 hist->data_g[i] = (float)cp[1]/255.0f;
1786 hist->data_b[i] = (float)cp[2]/255.0f;
1787 hist->data_luma[i] = (0.299f*cp[0] + 0.587f*cp[1] + 0.114f*cp[2])/255;
1792 ED_space_image_release_buffer(sima, lock);
1794 ED_area_tag_redraw(CTX_wm_area(C));
1796 return OPERATOR_FINISHED;
1799 static int sample_line_invoke(bContext *C, wmOperator *op, wmEvent *event)
1801 SpaceImage *sima= CTX_wm_space_image(C);
1803 if(!ED_space_image_has_buffer(sima))
1804 return OPERATOR_CANCELLED;
1806 return WM_gesture_straightline_invoke(C, op, event);
1809 void IMAGE_OT_sample_line(wmOperatorType *ot)
1812 ot->name= "Sample Line";
1813 ot->idname= "IMAGE_OT_sample_line";
1816 ot->invoke= sample_line_invoke;
1817 ot->modal= WM_gesture_straightline_modal;
1818 ot->exec= sample_line_exec;
1819 ot->poll= space_image_main_area_poll;
1822 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1824 WM_operator_properties_gesture_straightline(ot, CURSOR_EDIT);
1827 /******************** set curve point operator ********************/
1829 void IMAGE_OT_curves_point_set(wmOperatorType *ot)
1831 static EnumPropertyItem point_items[]= {
1832 {0, "BLACK_POINT", 0, "Black Point", ""},
1833 {1, "WHITE_POINT", 0, "White Point", ""},
1834 {0, NULL, 0, NULL, NULL}};
1837 ot->name= "Set Curves Point";
1838 ot->idname= "IMAGE_OT_curves_point_set";
1841 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1844 ot->invoke= sample_invoke;
1845 ot->modal= sample_modal;
1846 ot->cancel= sample_cancel;
1847 ot->poll= space_image_main_area_poll;
1850 RNA_def_enum(ot->srna, "point", point_items, 0, "Point", "Set black point or white point for curves.");
1853 /******************** record composite operator *********************/
1855 typedef struct RecordCompositeData {
1859 } RecordCompositeData;
1861 static int record_composite_apply(bContext *C, wmOperator *op)
1863 SpaceImage *sima= CTX_wm_space_image(C);
1864 RecordCompositeData *rcd= op->customdata;
1865 Scene *scene= CTX_data_scene(C);
1868 WM_timecursor(CTX_wm_window(C), scene->r.cfra);
1870 // XXX scene->nodetree->test_break= blender_test_break;
1871 // XXX scene->nodetree->test_break= NULL;
1873 BKE_image_all_free_anim_ibufs(scene->r.cfra);
1874 ntreeCompositTagAnimated(scene->nodetree);
1875 ntreeCompositExecTree(scene->nodetree, &scene->r, scene->r.cfra != rcd->old_cfra); /* 1 is no previews */
1877 ED_area_tag_redraw(CTX_wm_area(C));
1879 ibuf= BKE_image_get_ibuf(sima->image, &sima->iuser);
1880 /* save memory in flipbooks */
1882 imb_freerectfloatImBuf(ibuf);
1886 return (scene->r.cfra <= rcd->efra);
1889 static int record_composite_init(bContext *C, wmOperator *op)
1891 SpaceImage *sima= CTX_wm_space_image(C);
1892 Scene *scene= CTX_data_scene(C);
1893 RecordCompositeData *rcd;
1895 if(sima->iuser.frames < 2)
1897 if(scene->nodetree == NULL)
1900 op->customdata= rcd= MEM_callocN(sizeof(RecordCompositeData), "ImageRecordCompositeData");
1902 rcd->old_cfra= scene->r.cfra;
1903 rcd->sfra= sima->iuser.sfra;
1904 rcd->efra= sima->iuser.sfra + sima->iuser.frames-1;
1905 scene->r.cfra= rcd->sfra;
1910 static void record_composite_exit(bContext *C, wmOperator *op)
1912 Scene *scene= CTX_data_scene(C);
1913 SpaceImage *sima= CTX_wm_space_image(C);
1914 RecordCompositeData *rcd= op->customdata;
1916 scene->r.cfra= rcd->old_cfra;
1918 WM_cursor_restore(CTX_wm_window(C));
1921 WM_event_remove_timer(CTX_wm_manager(C), CTX_wm_window(C), rcd->timer);
1923 WM_event_add_notifier(C, NC_IMAGE|NA_EDITED, sima->image);
1925 // XXX play_anim(0);
1926 // XXX allqueue(REDRAWNODE, 1);
1931 static int record_composite_exec(bContext *C, wmOperator *op)
1933 if(!record_composite_init(C, op))
1934 return OPERATOR_CANCELLED;
1936 while(record_composite_apply(C, op))
1939 record_composite_exit(C, op);
1941 return OPERATOR_FINISHED;
1944 static int record_composite_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
1946 RecordCompositeData *rcd;
1948 if(!record_composite_init(C, op))
1949 return OPERATOR_CANCELLED;
1951 rcd= op->customdata;
1952 rcd->timer= WM_event_add_timer(CTX_wm_manager(C), CTX_wm_window(C), TIMER, 0.0f);
1953 WM_event_add_modal_handler(C, op);
1955 if(!record_composite_apply(C, op))
1956 return OPERATOR_FINISHED;
1958 return OPERATOR_RUNNING_MODAL;
1961 static int record_composite_modal(bContext *C, wmOperator *op, wmEvent *event)
1963 RecordCompositeData *rcd= op->customdata;
1965 switch(event->type) {
1967 if(rcd->timer == event->customdata) {
1968 if(!record_composite_apply(C, op)) {
1969 record_composite_exit(C, op);
1970 return OPERATOR_FINISHED;
1975 record_composite_exit(C, op);
1976 return OPERATOR_FINISHED;
1979 return OPERATOR_RUNNING_MODAL;
1982 static int record_composite_cancel(bContext *C, wmOperator *op)
1984 record_composite_exit(C, op);
1985 return OPERATOR_CANCELLED;
1988 void IMAGE_OT_record_composite(wmOperatorType *ot)
1991 ot->name= "Record Composite";
1992 ot->idname= "IMAGE_OT_record_composite";
1995 ot->exec= record_composite_exec;
1996 ot->invoke= record_composite_invoke;
1997 ot->modal= record_composite_modal;
1998 ot->cancel= record_composite_cancel;
1999 ot->poll= space_image_buffer_exists_poll;
2002 /********************* cycle render slot operator *********************/
2004 static int cycle_render_slot_poll(bContext *C)
2006 Image *ima= CTX_data_edit_image(C);
2008 return (ima && ima->type == IMA_TYPE_R_RESULT);
2011 static int cycle_render_slot_exec(bContext *C, wmOperator *op)
2013 Image *ima= CTX_data_edit_image(C);
2014 int a, slot, cur= ima->render_slot;
2015 const short use_reverse= RNA_boolean_get(op->ptr, "reverse");
2017 for(a=1; a<IMA_MAX_RENDER_SLOT; a++) {
2018 slot= (cur + (use_reverse ? -a:a))%IMA_MAX_RENDER_SLOT;
2019 if(slot<0) slot+=IMA_MAX_RENDER_SLOT;
2021 if(ima->renders[slot] || slot == ima->last_render_slot) {
2022 ima->render_slot= slot;
2025 else if((slot - 1) == ima->last_render_slot && slot < IMA_MAX_RENDER_SLOT) {
2026 ima->render_slot= slot;
2031 if(a == IMA_MAX_RENDER_SLOT)
2032 ima->render_slot= ((cur == 1)? 0: 1);
2034 WM_event_add_notifier(C, NC_IMAGE|ND_DRAW, NULL);
2036 return OPERATOR_FINISHED;
2039 void IMAGE_OT_cycle_render_slot(wmOperatorType *ot)
2042 ot->name= "Cycle Render Slot";
2043 ot->idname= "IMAGE_OT_cycle_render_slot";
2046 ot->exec= cycle_render_slot_exec;
2047 ot->poll= cycle_render_slot_poll;
2050 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2052 RNA_def_boolean(ot->srna, "reverse", 0, "Cycle in Reverse", "");
2055 /******************** TODO ********************/
2059 /* goes over all ImageUsers, and sets frame numbers if auto-refresh is set */
2061 void ED_image_update_frame(const Main *mainp, int cfra)
2063 wmWindowManager *wm;
2068 for(tex= mainp->tex.first; tex; tex= tex->id.next) {
2069 if(tex->type==TEX_IMAGE && tex->ima) {
2070 if(ELEM(tex->ima->source, IMA_SRC_MOVIE, IMA_SRC_SEQUENCE)) {
2071 if(tex->iuser.flag & IMA_ANIM_ALWAYS)
2072 BKE_image_user_calc_frame(&tex->iuser, cfra, 0);
2077 /* image window, compo node users */
2078 for(wm=mainp->wm.first; wm; wm= wm->id.next) { /* only 1 wm */
2079 for(win= wm->windows.first; win; win= win->next) {
2081 for(sa= win->screen->areabase.first; sa; sa= sa->next) {
2082 if(sa->spacetype==SPACE_VIEW3D) {
2083 View3D *v3d= sa->spacedata.first;
2085 for(bgpic= v3d->bgpicbase.first; bgpic; bgpic= bgpic->next)
2086 if(bgpic->iuser.flag & IMA_ANIM_ALWAYS)
2087 BKE_image_user_calc_frame(&bgpic->iuser, cfra, 0);
2089 else if(sa->spacetype==SPACE_IMAGE) {
2090 SpaceImage *sima= sa->spacedata.first;
2091 if(sima->iuser.flag & IMA_ANIM_ALWAYS)
2092 BKE_image_user_calc_frame(&sima->iuser, cfra, 0);
2094 else if(sa->spacetype==SPACE_NODE) {
2095 SpaceNode *snode= sa->spacedata.first;
2096 if((snode->treetype==NTREE_COMPOSIT) && (snode->nodetree)) {
2098 for(node= snode->nodetree->nodes.first; node; node= node->next) {
2099 if(node->id && node->type==CMP_NODE_IMAGE) {
2100 Image *ima= (Image *)node->id;
2101 ImageUser *iuser= node->storage;
2102 if(ELEM(ima->source, IMA_SRC_MOVIE, IMA_SRC_SEQUENCE))
2103 if(iuser->flag & IMA_ANIM_ALWAYS)
2104 BKE_image_user_calc_frame(iuser, cfra, 0);