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 *****
28 /** \file blender/editors/space_image/image_ops.c
38 #include "MEM_guardedalloc.h"
41 #include "BLI_blenlib.h"
42 #include "BLI_utildefines.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_imbuf.h"
61 #include "IMB_imbuf_types.h"
63 #include "RE_pipeline.h"
65 #include "RNA_access.h"
66 #include "RNA_define.h"
67 #include "RNA_enum_types.h"
70 #include "ED_render.h"
71 #include "ED_screen.h"
72 #include "ED_space_api.h"
73 #include "ED_uvedit.h"
76 #include "UI_interface.h"
77 #include "UI_resources.h"
78 #include "UI_view2d.h"
83 #include "image_intern.h"
85 /******************** view navigation utilities *********************/
87 static void sima_zoom_set(SpaceImage *sima, ARegion *ar, float zoom)
89 float oldzoom= sima->zoom;
94 if (sima->zoom > 0.1f && sima->zoom < 4.0f)
97 /* check zoom limits */
98 ED_space_image_size(sima, &width, &height);
101 height *= sima->zoom;
103 if((width < 4) && (height < 4))
105 else if((ar->winrct.xmax - ar->winrct.xmin) <= sima->zoom)
107 else if((ar->winrct.ymax - ar->winrct.ymin) <= sima->zoom)
111 static void sima_zoom_set_factor(SpaceImage *sima, ARegion *ar, float zoomfac)
113 sima_zoom_set(sima, ar, sima->zoom*zoomfac);
116 #if 0 // currently unused
117 static int image_poll(bContext *C)
119 return (CTX_data_edit_image(C) != NULL);
123 static int space_image_buffer_exists_poll(bContext *C)
125 SpaceImage *sima= CTX_wm_space_image(C);
126 if(sima && sima->spacetype==SPACE_IMAGE)
127 if(ED_space_image_has_buffer(sima))
132 static int space_image_file_exists_poll(bContext *C)
134 if(space_image_buffer_exists_poll(C)) {
135 Main *bmain= CTX_data_main(C);
136 SpaceImage *sima= CTX_wm_space_image(C);
142 ibuf= ED_space_image_acquire_buffer(sima, &lock);
144 BLI_strncpy(name, ibuf->name, FILE_MAX);
145 BLI_path_abs(name, bmain->name);
146 poll= (BLI_exists(name) && BLI_is_writable(name));
148 ED_space_image_release_buffer(sima, lock);
155 static int space_image_poll(bContext *C)
157 SpaceImage *sima= CTX_wm_space_image(C);
158 if(sima && sima->spacetype==SPACE_IMAGE && sima->image)
163 int space_image_main_area_poll(bContext *C)
165 SpaceImage *sima= CTX_wm_space_image(C);
166 // XXX ARegion *ar= CTX_wm_region(C);
169 return 1; // XXX (ar && ar->type->regionid == RGN_TYPE_WINDOW);
174 /********************** view pan operator *********************/
176 typedef struct ViewPanData {
182 static void view_pan_init(bContext *C, wmOperator *op, wmEvent *event)
184 SpaceImage *sima= CTX_wm_space_image(C);
187 op->customdata= vpd= MEM_callocN(sizeof(ViewPanData), "ImageViewPanData");
188 WM_cursor_modal(CTX_wm_window(C), BC_NSEW_SCROLLCURSOR);
194 vpd->event_type= event->type;
196 WM_event_add_modal_handler(C, op);
199 static void view_pan_exit(bContext *C, wmOperator *op, int cancel)
201 SpaceImage *sima= CTX_wm_space_image(C);
202 ViewPanData *vpd= op->customdata;
207 ED_region_tag_redraw(CTX_wm_region(C));
210 WM_cursor_restore(CTX_wm_window(C));
211 MEM_freeN(op->customdata);
214 static int view_pan_exec(bContext *C, wmOperator *op)
216 SpaceImage *sima= CTX_wm_space_image(C);
219 RNA_float_get_array(op->ptr, "offset", offset);
220 sima->xof += offset[0];
221 sima->yof += offset[1];
223 ED_region_tag_redraw(CTX_wm_region(C));
227 if(image_preview_active(curarea, NULL, NULL)) {
228 /* recalculates new preview rect */
229 scrarea_do_windraw(curarea);
230 image_preview_event(2);
234 return OPERATOR_FINISHED;
237 static int view_pan_invoke(bContext *C, wmOperator *op, wmEvent *event)
239 if (event->type == MOUSEPAN) {
240 SpaceImage *sima= CTX_wm_space_image(C);
243 offset[0]= (event->x - event->prevx)/sima->zoom;
244 offset[1]= (event->y - event->prevy)/sima->zoom;
245 RNA_float_set_array(op->ptr, "offset", offset);
247 view_pan_exec(C, op);
248 return OPERATOR_FINISHED;
251 view_pan_init(C, op, event);
252 return OPERATOR_RUNNING_MODAL;
256 static int view_pan_modal(bContext *C, wmOperator *op, wmEvent *event)
258 SpaceImage *sima= CTX_wm_space_image(C);
259 ViewPanData *vpd= op->customdata;
262 switch(event->type) {
266 offset[0]= (vpd->x - event->x)/sima->zoom;
267 offset[1]= (vpd->y - event->y)/sima->zoom;
268 RNA_float_set_array(op->ptr, "offset", offset);
269 view_pan_exec(C, op);
272 if(event->type==vpd->event_type && event->val==KM_RELEASE) {
273 view_pan_exit(C, op, 0);
274 return OPERATOR_FINISHED;
279 return OPERATOR_RUNNING_MODAL;
282 static int view_pan_cancel(bContext *C, wmOperator *op)
284 view_pan_exit(C, op, 1);
285 return OPERATOR_CANCELLED;
288 void IMAGE_OT_view_pan(wmOperatorType *ot)
291 ot->name= "View Pan";
292 ot->idname= "IMAGE_OT_view_pan";
295 ot->exec= view_pan_exec;
296 ot->invoke= view_pan_invoke;
297 ot->modal= view_pan_modal;
298 ot->cancel= view_pan_cancel;
299 ot->poll= space_image_main_area_poll;
302 ot->flag= OPTYPE_BLOCKING;
305 RNA_def_float_vector(ot->srna, "offset", 2, NULL, -FLT_MAX, FLT_MAX,
306 "Offset", "Offset in floating point units, 1.0 is the width and height of the image.", -FLT_MAX, FLT_MAX);
309 /********************** view zoom operator *********************/
311 typedef struct ViewZoomData {
317 static void view_zoom_init(bContext *C, wmOperator *op, wmEvent *event)
319 SpaceImage *sima= CTX_wm_space_image(C);
322 op->customdata= vpd= MEM_callocN(sizeof(ViewZoomData), "ImageViewZoomData");
323 WM_cursor_modal(CTX_wm_window(C), BC_NSEW_SCROLLCURSOR);
327 vpd->zoom= sima->zoom;
328 vpd->event_type= event->type;
330 WM_event_add_modal_handler(C, op);
333 static void view_zoom_exit(bContext *C, wmOperator *op, int cancel)
335 SpaceImage *sima= CTX_wm_space_image(C);
336 ViewZoomData *vpd= op->customdata;
339 sima->zoom= vpd->zoom;
340 ED_region_tag_redraw(CTX_wm_region(C));
343 WM_cursor_restore(CTX_wm_window(C));
344 MEM_freeN(op->customdata);
347 static int view_zoom_exec(bContext *C, wmOperator *op)
349 SpaceImage *sima= CTX_wm_space_image(C);
350 ARegion *ar= CTX_wm_region(C);
352 sima_zoom_set_factor(sima, ar, RNA_float_get(op->ptr, "factor"));
354 ED_region_tag_redraw(CTX_wm_region(C));
358 if(image_preview_active(curarea, NULL, NULL)) {
359 /* recalculates new preview rect */
360 scrarea_do_windraw(curarea);
361 image_preview_event(2);
365 return OPERATOR_FINISHED;
368 static int view_zoom_invoke(bContext *C, wmOperator *op, wmEvent *event)
370 if (event->type == MOUSEZOOM) {
371 SpaceImage *sima= CTX_wm_space_image(C);
372 ARegion *ar= CTX_wm_region(C);
375 factor= 1.0f + (event->x-event->prevx+event->y-event->prevy)/300.0f;
376 RNA_float_set(op->ptr, "factor", factor);
377 sima_zoom_set(sima, ar, sima->zoom*factor);
378 ED_region_tag_redraw(CTX_wm_region(C));
380 return OPERATOR_FINISHED;
383 view_zoom_init(C, op, event);
384 return OPERATOR_RUNNING_MODAL;
388 static int view_zoom_modal(bContext *C, wmOperator *op, wmEvent *event)
390 SpaceImage *sima= CTX_wm_space_image(C);
391 ARegion *ar= CTX_wm_region(C);
392 ViewZoomData *vpd= op->customdata;
395 switch(event->type) {
397 factor= 1.0f + (vpd->x-event->x+vpd->y-event->y)/300.0f;
398 RNA_float_set(op->ptr, "factor", factor);
399 sima_zoom_set(sima, ar, vpd->zoom*factor);
400 ED_region_tag_redraw(CTX_wm_region(C));
403 if(event->type==vpd->event_type && event->val==KM_RELEASE) {
404 view_zoom_exit(C, op, 0);
405 return OPERATOR_FINISHED;
410 return OPERATOR_RUNNING_MODAL;
413 static int view_zoom_cancel(bContext *C, wmOperator *op)
415 view_zoom_exit(C, op, 1);
416 return OPERATOR_CANCELLED;
419 void IMAGE_OT_view_zoom(wmOperatorType *ot)
422 ot->name= "View Zoom";
423 ot->idname= "IMAGE_OT_view_zoom";
426 ot->exec= view_zoom_exec;
427 ot->invoke= view_zoom_invoke;
428 ot->modal= view_zoom_modal;
429 ot->cancel= view_zoom_cancel;
430 ot->poll= space_image_main_area_poll;
433 ot->flag= OPTYPE_BLOCKING;
436 RNA_def_float(ot->srna, "factor", 0.0f, 0.0f, FLT_MAX,
437 "Factor", "Zoom factor, values higher than 1.0 zoom in, lower values zoom out.", -FLT_MAX, FLT_MAX);
440 /********************** view all operator *********************/
442 /* Updates the fields of the View2D member of the SpaceImage struct.
443 * Default behavior is to reset the position of the image and set the zoom to 1
444 * If the image will not fit within the window rectangle, the zoom is adjusted */
446 static int view_all_exec(bContext *C, wmOperator *UNUSED(op))
450 float aspx, aspy, zoomx, zoomy, w, h;
454 sima= CTX_wm_space_image(C);
455 ar= CTX_wm_region(C);
457 ED_space_image_size(sima, &width, &height);
458 ED_space_image_aspect(sima, &aspx, &aspy);
463 /* check if the image will fit in the image with zoom==1 */
464 width = ar->winrct.xmax - ar->winrct.xmin + 1;
465 height = ar->winrct.ymax - ar->winrct.ymin + 1;
467 if((w >= width || h >= height) && (width > 0 && height > 0)) {
468 /* find the zoom value that will fit the image in the image space */
471 sima_zoom_set(sima, ar, 1.0f/power_of_2(1/MIN2(zoomx, zoomy)));
474 sima_zoom_set(sima, ar, 1.0f);
476 sima->xof= sima->yof= 0.0f;
478 ED_region_tag_redraw(CTX_wm_region(C));
480 return OPERATOR_FINISHED;
483 void IMAGE_OT_view_all(wmOperatorType *ot)
486 ot->name= "View All";
487 ot->idname= "IMAGE_OT_view_all";
490 ot->exec= view_all_exec;
491 ot->poll= space_image_main_area_poll;
494 /********************** view selected operator *********************/
496 static int view_selected_exec(bContext *C, wmOperator *UNUSED(op))
503 float size, min[2], max[2], d[2];
507 sima= CTX_wm_space_image(C);
508 ar= CTX_wm_region(C);
509 scene= (Scene*)CTX_data_scene(C);
510 obedit= CTX_data_edit_object(C);
512 ima= ED_space_image(sima);
513 ED_space_image_size(sima, &width, &height);
516 if(!ED_uvedit_minmax(scene, ima, obedit, min, max))
517 return OPERATOR_CANCELLED;
519 /* adjust offset and zoom */
520 sima->xof= (int)(((min[0] + max[0])*0.5f - 0.5f)*width);
521 sima->yof= (int)(((min[1] + max[1])*0.5f - 0.5f)*height);
523 d[0]= max[0] - min[0];
524 d[1]= max[1] - min[1];
525 size= 0.5f*MAX2(d[0], d[1])*MAX2(width, height)/256.0f;
527 if(size<=0.01f) size= 0.01f;
528 sima_zoom_set(sima, ar, 0.7f/size);
530 ED_region_tag_redraw(CTX_wm_region(C));
532 return OPERATOR_FINISHED;
535 static int view_selected_poll(bContext *C)
537 return (space_image_main_area_poll(C) && ED_operator_uvedit(C));
540 void IMAGE_OT_view_selected(wmOperatorType *ot)
543 ot->name= "View Center";
544 ot->idname= "IMAGE_OT_view_selected";
547 ot->exec= view_selected_exec;
548 ot->poll= view_selected_poll;
551 /********************** view zoom in/out operator *********************/
553 static int view_zoom_in_exec(bContext *C, wmOperator *UNUSED(op))
555 SpaceImage *sima= CTX_wm_space_image(C);
556 ARegion *ar= CTX_wm_region(C);
558 sima_zoom_set_factor(sima, ar, 1.25f);
560 ED_region_tag_redraw(CTX_wm_region(C));
562 return OPERATOR_FINISHED;
565 void IMAGE_OT_view_zoom_in(wmOperatorType *ot)
568 ot->name= "View Zoom In";
569 ot->idname= "IMAGE_OT_view_zoom_in";
572 ot->exec= view_zoom_in_exec;
573 ot->poll= space_image_main_area_poll;
576 static int view_zoom_out_exec(bContext *C, wmOperator *UNUSED(op))
578 SpaceImage *sima= CTX_wm_space_image(C);
579 ARegion *ar= CTX_wm_region(C);
581 sima_zoom_set_factor(sima, ar, 0.8f);
583 ED_region_tag_redraw(CTX_wm_region(C));
585 return OPERATOR_FINISHED;
588 void IMAGE_OT_view_zoom_out(wmOperatorType *ot)
591 ot->name= "View Zoom Out";
592 ot->idname= "IMAGE_OT_view_zoom_out";
595 ot->exec= view_zoom_out_exec;
596 ot->poll= space_image_main_area_poll;
599 /********************** view zoom ratio operator *********************/
601 static int view_zoom_ratio_exec(bContext *C, wmOperator *op)
603 SpaceImage *sima= CTX_wm_space_image(C);
604 ARegion *ar= CTX_wm_region(C);
606 sima_zoom_set(sima, ar, RNA_float_get(op->ptr, "ratio"));
608 /* ensure pixel exact locations for draw */
609 sima->xof= (int)sima->xof;
610 sima->yof= (int)sima->yof;
614 if(image_preview_active(curarea, NULL, NULL)) {
615 /* recalculates new preview rect */
616 scrarea_do_windraw(curarea);
617 image_preview_event(2);
621 ED_region_tag_redraw(CTX_wm_region(C));
623 return OPERATOR_FINISHED;
626 void IMAGE_OT_view_zoom_ratio(wmOperatorType *ot)
629 ot->name= "View Zoom Ratio";
630 ot->idname= "IMAGE_OT_view_zoom_ratio";
633 ot->exec= view_zoom_ratio_exec;
634 ot->poll= space_image_main_area_poll;
637 RNA_def_float(ot->srna, "ratio", 0.0f, 0.0f, FLT_MAX,
638 "Ratio", "Zoom ratio, 1.0 is 1:1, higher is zoomed in, lower is zoomed out.", -FLT_MAX, FLT_MAX);
641 /**************** load/replace/save callbacks ******************/
643 /* XXX make dynamic */
644 static const EnumPropertyItem image_file_type_items[] = {
645 {R_TARGA, "TARGA", 0, "Targa", ""},
646 {R_RAWTGA, "TARGA RAW", 0, "Targa Raw", ""},
647 {R_PNG, "PNG", 0, "PNG", ""},
648 {R_BMP, "BMP", 0, "BMP", ""},
649 {R_JPEG90, "JPEG", 0, "Jpeg", ""},
651 {R_JP2, "JPEG_2000", 0, "Jpeg 2000", ""},
653 {R_IRIS, "IRIS", 0, "Iris", ""},
655 {R_TIFF, "TIFF", 0, "Tiff", ""},
658 {R_RADHDR, "RADIANCE_HDR", 0, "Radiance HDR", ""},
661 {R_CINEON, "CINEON", 0, "Cineon", ""},
662 {R_DPX, "DPX", 0, "DPX", ""},
665 {R_OPENEXR, "OPENEXR", 0, "OpenEXR", ""},
666 /* saving sequences of multilayer won't work, they copy buffers */
667 /*if(ima->source==IMA_SRC_SEQUENCE && ima->type==IMA_TYPE_MULTILAYER);
669 {R_MULTILAYER, "MULTILAYER", 0, "MultiLayer", ""},
671 {0, NULL, 0, NULL, NULL}};
673 static void image_filesel(bContext *C, wmOperator *op, const char *path)
675 RNA_string_set(op->ptr, "filepath", path);
676 WM_event_add_fileselect(C, op);
679 /******************** open image operator ********************/
681 static void open_init(bContext *C, wmOperator *op)
683 PropertyPointerRNA *pprop;
685 op->customdata= pprop= MEM_callocN(sizeof(PropertyPointerRNA), "OpenPropertyPointerRNA");
686 uiIDContextProperty(C, &pprop->ptr, &pprop->prop);
689 static int open_cancel(bContext *UNUSED(C), wmOperator *op)
691 MEM_freeN(op->customdata);
692 op->customdata= NULL;
693 return OPERATOR_CANCELLED;
696 static int open_exec(bContext *C, wmOperator *op)
698 SpaceImage *sima= CTX_wm_space_image(C); /* XXX other space types can call */
699 Scene *scene= CTX_data_scene(C);
700 Object *obedit= CTX_data_edit_object(C);
701 ImageUser *iuser= NULL;
702 PropertyPointerRNA *pprop;
707 RNA_string_get(op->ptr, "filepath", str);
708 /* default to frame 1 if there's no scene in context */
712 ima= BKE_add_image_file(str);
715 if(op->customdata) MEM_freeN(op->customdata);
716 BKE_reportf(op->reports, RPT_ERROR, "Can't read: \"%s\", %s.", str, errno ? strerror(errno) : "Unsupported image format");
717 return OPERATOR_CANCELLED;
724 pprop= op->customdata;
727 /* when creating new ID blocks, use is already 1, but RNA
728 * pointer se also increases user, so this compensates it */
731 RNA_id_pointer_create(&ima->id, &idptr);
732 RNA_property_pointer_set(&pprop->ptr, pprop->prop, idptr);
733 RNA_property_update(C, &pprop->ptr, pprop->prop);
736 ED_space_image_set(C, sima, scene, obedit, ima);
740 Tex *tex= CTX_data_pointer_get_type(C, "texture", &RNA_Texture).data;
741 if(tex && tex->type==TEX_IMAGE)
746 /* initialize because of new image */
753 /* XXX unpackImage frees image buffers */
754 ED_preview_kill_jobs(C);
756 BKE_image_signal(ima, iuser, IMA_SIGNAL_RELOAD);
757 WM_event_add_notifier(C, NC_IMAGE|NA_EDITED, ima);
759 MEM_freeN(op->customdata);
761 return OPERATOR_FINISHED;
764 static int open_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
766 SpaceImage *sima= CTX_wm_space_image(C); /* XXX other space types can call */
767 char *path=U.textudir;
775 Tex *tex= CTX_data_pointer_get_type(C, "texture", &RNA_Texture).data;
776 if(tex && tex->type==TEX_IMAGE)
784 if(!RNA_property_is_set(op->ptr, "relative_path"))
785 RNA_boolean_set(op->ptr, "relative_path", U.flag & USER_RELPATHS);
787 if(RNA_property_is_set(op->ptr, "filepath"))
788 return open_exec(C, op);
792 image_filesel(C, op, path);
794 return OPERATOR_RUNNING_MODAL;
797 /* called by other space types too */
798 void IMAGE_OT_open(wmOperatorType *ot)
801 ot->name= "Open Image";
802 ot->idname= "IMAGE_OT_open";
806 ot->invoke= open_invoke;
807 ot->cancel= open_cancel;
810 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
813 WM_operator_properties_filesel(ot, FOLDERFILE|IMAGEFILE|MOVIEFILE, FILE_SPECIAL, FILE_OPENFILE, WM_FILESEL_FILEPATH|WM_FILESEL_RELPATH);
816 /******************** replace image operator ********************/
818 static int replace_exec(bContext *C, wmOperator *op)
820 SpaceImage *sima= CTX_wm_space_image(C);
824 return OPERATOR_CANCELLED;
826 RNA_string_get(op->ptr, "filepath", str);
827 BLI_strncpy(sima->image->name, str, sizeof(sima->image->name)); /* we cant do much if the str is longer then 240 :/ */
829 /* XXX unpackImage frees image buffers */
830 ED_preview_kill_jobs(C);
832 BKE_image_signal(sima->image, &sima->iuser, IMA_SIGNAL_RELOAD);
833 WM_event_add_notifier(C, NC_IMAGE|NA_EDITED, sima->image);
835 return OPERATOR_FINISHED;
838 static int replace_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
840 SpaceImage *sima= CTX_wm_space_image(C);
843 return OPERATOR_CANCELLED;
845 if(RNA_property_is_set(op->ptr, "filepath"))
846 return replace_exec(C, op);
848 if(!RNA_property_is_set(op->ptr, "relative_path"))
849 RNA_boolean_set(op->ptr, "relative_path", (strncmp(sima->image->name, "//", 2))==0);
851 image_filesel(C, op, sima->image->name);
853 return OPERATOR_RUNNING_MODAL;
856 void IMAGE_OT_replace(wmOperatorType *ot)
859 ot->name= "Replace Image";
860 ot->idname= "IMAGE_OT_replace";
863 ot->exec= replace_exec;
864 ot->invoke= replace_invoke;
865 ot->poll= space_image_poll;
868 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
871 WM_operator_properties_filesel(ot, FOLDERFILE|IMAGEFILE|MOVIEFILE, FILE_SPECIAL, FILE_OPENFILE, WM_FILESEL_FILEPATH|WM_FILESEL_RELPATH);
874 /******************** save image as operator ********************/
876 /* assumes name is FILE_MAX */
877 /* ima->name and ibuf->name should end up the same */
878 static void save_image_doit(bContext *C, SpaceImage *sima, Scene *scene, wmOperator *op, char *path, int do_newpath)
880 Image *ima= ED_space_image(sima);
882 ImBuf *ibuf= ED_space_image_acquire_buffer(sima, &lock);
885 Main *bmain= CTX_data_main(C);
886 const short relative= (RNA_struct_find_property(op->ptr, "relative_path") && RNA_boolean_get(op->ptr, "relative_path"));
887 const short save_copy= (RNA_struct_find_property(op->ptr, "copy") && RNA_boolean_get(op->ptr, "copy"));
890 BLI_path_abs(path, bmain->name);
891 /* old global to ensure a 2nd save goes to same dir */
892 BLI_strncpy(G.ima, path, sizeof(G.ima));
896 if(ima->type == IMA_TYPE_R_RESULT) {
897 /* enforce user setting for RGB or RGBA, but skip BW */
898 if(scene->r.planes==32) {
901 else if(scene->r.planes==24) {
906 /* TODO, better solution, if a 24bit image is painted onto it may contain alpha */
907 if(ibuf->userflags & IB_BITMAPDIRTY) { /* it has been painted onto */
908 /* checks each pixel, not ideal */
909 ibuf->depth= BKE_alphatest_ibuf(ibuf) ? 32 : 24;
913 if(scene->r.scemode & R_EXTENSION) {
914 BKE_add_image_extension(path, sima->imtypenr);
917 if(sima->imtypenr==R_MULTILAYER) {
918 RenderResult *rr= BKE_image_acquire_renderresult(scene, ima);
920 RE_WriteRenderResult(rr, path, scene->r.quality);
924 BKE_report(op->reports, RPT_ERROR, "Did not write, no Multilayer Image");
926 BKE_image_release_renderresult(scene, ima);
928 else if (BKE_write_ibuf(scene, ibuf, path, sima->imtypenr, scene->r.subimtype, scene->r.quality)) {
934 BLI_path_rel(path, bmain->name); /* only after saving */
936 if(ibuf->name[0]==0) {
937 BLI_strncpy(ibuf->name, path, sizeof(ibuf->name));
938 BLI_strncpy(ima->name, path, sizeof(ima->name));
943 BLI_strncpy(ima->name, path, sizeof(ima->name));
944 BLI_strncpy(ibuf->name, path, sizeof(ibuf->name));
947 ibuf->userflags &= ~IB_BITMAPDIRTY;
950 if(ima->type==IMA_TYPE_R_RESULT) {
951 ima->type= IMA_TYPE_IMAGE;
953 /* workaround to ensure the render result buffer is no longer used
954 * by this image, otherwise can crash when a new render result is
956 if(ibuf->rect && !(ibuf->mall & IB_rect))
957 imb_freerectImBuf(ibuf);
958 if(ibuf->rect_float && !(ibuf->mall & IB_rectfloat))
959 imb_freerectfloatImBuf(ibuf);
960 if(ibuf->zbuf && !(ibuf->mall & IB_zbuf))
961 IMB_freezbufImBuf(ibuf);
962 if(ibuf->zbuf_float && !(ibuf->mall & IB_zbuffloat))
963 IMB_freezbuffloatImBuf(ibuf);
965 if( ELEM(ima->source, IMA_SRC_GENERATED, IMA_SRC_VIEWER)) {
966 ima->source= IMA_SRC_FILE;
967 ima->type= IMA_TYPE_IMAGE;
972 BKE_reportf(op->reports, RPT_ERROR, "Couldn't write image: %s", path);
976 WM_event_add_notifier(C, NC_IMAGE|NA_EDITED, sima->image);
981 ED_space_image_release_buffer(sima, lock);
984 static int save_as_exec(bContext *C, wmOperator *op)
986 SpaceImage *sima= CTX_wm_space_image(C);
987 Scene *scene= CTX_data_scene(C);
988 Image *ima = ED_space_image(sima);
992 return OPERATOR_CANCELLED;
994 sima->imtypenr= RNA_enum_get(op->ptr, "file_type");
995 RNA_string_get(op->ptr, "filepath", str);
997 save_image_doit(C, sima, scene, op, str, TRUE);
999 return OPERATOR_FINISHED;
1003 static int save_as_check(bContext *UNUSED(C), wmOperator *op)
1005 char filepath[FILE_MAX];
1006 RNA_string_get(op->ptr, "filepath", filepath);
1007 if(BKE_add_image_extension(filepath, RNA_enum_get(op->ptr, "file_type"))) {
1008 RNA_string_set(op->ptr, "filepath", filepath);
1014 static int save_as_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
1016 SpaceImage *sima= CTX_wm_space_image(C);
1017 Image *ima = ED_space_image(sima);
1018 Scene *scene= CTX_data_scene(C);
1020 char filename[FILE_MAX];
1024 if(!RNA_property_is_set(op->ptr, "relative_path"))
1025 RNA_boolean_set(op->ptr, "relative_path", U.flag & USER_RELPATHS);
1027 if(RNA_property_is_set(op->ptr, "filepath"))
1028 return save_as_exec(C, op);
1031 return OPERATOR_CANCELLED;
1033 /* always opens fileselect */
1034 ibuf= ED_space_image_acquire_buffer(sima, &lock);
1037 /* cant save multilayer sequence, ima->rr isn't valid for a specific frame */
1038 if(ima->rr && !(ima->source==IMA_SRC_SEQUENCE && ima->type==IMA_TYPE_MULTILAYER))
1039 sima->imtypenr= R_MULTILAYER;
1040 else if(ima->type==IMA_TYPE_R_RESULT)
1041 sima->imtypenr= scene->r.imtype;
1042 else if (ima->source == IMA_SRC_GENERATED)
1043 sima->imtypenr= R_PNG;
1045 sima->imtypenr= BKE_ftype_to_imtype(ibuf->ftype);
1047 RNA_enum_set(op->ptr, "file_type", sima->imtypenr);
1049 if(ibuf->name[0]==0)
1050 if ( (G.ima[0] == '/') && (G.ima[1] == '/') && (G.ima[2] == '\0') ) {
1051 BLI_strncpy(filename, "//untitled", FILE_MAX);
1053 BLI_strncpy(filename, G.ima, FILE_MAX);
1056 BLI_strncpy(filename, ibuf->name, FILE_MAX);
1058 /* enable save_copy by default for render results */
1059 if(ELEM(ima->type, IMA_TYPE_R_RESULT, IMA_TYPE_COMPOSITE) && !RNA_property_is_set(op->ptr, "copy")) {
1060 RNA_boolean_set(op->ptr, "copy", TRUE);
1063 // XXX note: we can give default menu enums to operator for this
1064 image_filesel(C, op, filename);
1066 ED_space_image_release_buffer(sima, lock);
1068 return OPERATOR_RUNNING_MODAL;
1071 ED_space_image_release_buffer(sima, lock);
1073 return OPERATOR_CANCELLED;
1076 void IMAGE_OT_save_as(wmOperatorType *ot)
1079 ot->name= "Save As Image";
1080 ot->idname= "IMAGE_OT_save_as";
1083 ot->exec= save_as_exec;
1084 ot->check= save_as_check;
1085 ot->invoke= save_as_invoke;
1086 ot->poll= space_image_buffer_exists_poll;
1089 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1092 RNA_def_enum(ot->srna, "file_type", image_file_type_items, R_PNG, "File Type", "File type to save image as.");
1093 WM_operator_properties_filesel(ot, FOLDERFILE|IMAGEFILE|MOVIEFILE, FILE_SPECIAL, FILE_SAVE, WM_FILESEL_FILEPATH|WM_FILESEL_RELPATH);
1095 RNA_def_boolean(ot->srna, "copy", 0, "Copy", "Create a new image file without modifying the current image in blender");
1098 /******************** save image operator ********************/
1100 static int save_exec(bContext *C, wmOperator *op)
1102 Main *bmain= CTX_data_main(C);
1103 SpaceImage *sima= CTX_wm_space_image(C);
1104 Image *ima = ED_space_image(sima);
1106 ImBuf *ibuf= ED_space_image_acquire_buffer(sima, &lock);
1107 Scene *scene= CTX_data_scene(C);
1109 char name[FILE_MAX];
1112 ED_space_image_release_buffer(sima, lock);
1113 return OPERATOR_CANCELLED;
1116 /* if exists, saves over without fileselect */
1118 BLI_strncpy(name, ima->name, FILE_MAX);
1120 BLI_strncpy(name, G.ima, FILE_MAX);
1122 BLI_path_abs(name, bmain->name);
1124 if(BLI_exists(name) && BLI_is_writable(name)) {
1125 rr= BKE_image_acquire_renderresult(scene, ima);
1128 sima->imtypenr= R_MULTILAYER;
1130 sima->imtypenr= BKE_ftype_to_imtype(ibuf->ftype);
1132 BKE_image_release_renderresult(scene, ima);
1133 ED_space_image_release_buffer(sima, lock);
1135 save_image_doit(C, sima, scene, op, name, FALSE);
1138 ED_space_image_release_buffer(sima, lock);
1140 BKE_report(op->reports, RPT_ERROR, "Can not save image.");
1141 return OPERATOR_CANCELLED;
1144 return OPERATOR_FINISHED;
1147 void IMAGE_OT_save(wmOperatorType *ot)
1150 ot->name= "Save Image";
1151 ot->idname= "IMAGE_OT_save";
1154 ot->exec= save_exec;
1155 ot->poll= space_image_file_exists_poll;
1158 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1161 /******************* save sequence operator ********************/
1163 static int save_sequence_exec(bContext *C, wmOperator *op)
1165 Main *bmain= CTX_data_main(C);
1166 SpaceImage *sima= CTX_wm_space_image(C);
1169 char di[FILE_MAX], fi[FILE_MAX];
1171 if(sima->image==NULL)
1172 return OPERATOR_CANCELLED;
1174 if(sima->image->source!=IMA_SRC_SEQUENCE) {
1175 BKE_report(op->reports, RPT_ERROR, "Can only save sequence on image sequences.");
1176 return OPERATOR_CANCELLED;
1179 if(sima->image->type==IMA_TYPE_MULTILAYER) {
1180 BKE_report(op->reports, RPT_ERROR, "Can't save multilayer sequences.");
1181 return OPERATOR_CANCELLED;
1185 for(ibuf= sima->image->ibufs.first; ibuf; ibuf= ibuf->next)
1186 if(ibuf->userflags & IB_BITMAPDIRTY)
1190 BKE_report(op->reports, RPT_WARNING, "No images have been changed.");
1191 return OPERATOR_CANCELLED;
1194 /* get a filename for menu */
1195 for(ibuf= sima->image->ibufs.first; ibuf; ibuf= ibuf->next)
1196 if(ibuf->userflags & IB_BITMAPDIRTY)
1199 BLI_strncpy(di, ibuf->name, FILE_MAX);
1200 BLI_splitdirstring(di, fi);
1202 BKE_reportf(op->reports, RPT_INFO, "%d Image(s) will be saved in %s", tot, di);
1204 for(ibuf= sima->image->ibufs.first; ibuf; ibuf= ibuf->next) {
1205 if(ibuf->userflags & IB_BITMAPDIRTY) {
1206 char name[FILE_MAX];
1207 BLI_strncpy(name, ibuf->name, sizeof(name));
1209 BLI_path_abs(name, bmain->name);
1211 if(0 == IMB_saveiff(ibuf, name, IB_rect | IB_zbuf | IB_zbuffloat)) {
1212 BKE_reportf(op->reports, RPT_ERROR, "Could not write image %s.", name);
1216 BKE_reportf(op->reports, RPT_INFO, "Saved: %s\n", ibuf->name);
1217 ibuf->userflags &= ~IB_BITMAPDIRTY;
1221 return OPERATOR_FINISHED;
1224 void IMAGE_OT_save_sequence(wmOperatorType *ot)
1227 ot->name= "Save Sequence";
1228 ot->idname= "IMAGE_OT_save_sequence";
1231 ot->exec= save_sequence_exec;
1232 ot->poll= space_image_buffer_exists_poll;
1235 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1238 /******************** reload image operator ********************/
1240 static int reload_exec(bContext *C, wmOperator *UNUSED(op))
1242 Image *ima= CTX_data_edit_image(C);
1243 SpaceImage *sima= CTX_wm_space_image(C);
1246 return OPERATOR_CANCELLED;
1248 /* XXX unpackImage frees image buffers */
1249 ED_preview_kill_jobs(C);
1252 BKE_image_signal(ima, (sima)? &sima->iuser: NULL, IMA_SIGNAL_RELOAD);
1254 WM_event_add_notifier(C, NC_IMAGE|NA_EDITED, ima);
1256 return OPERATOR_FINISHED;
1259 void IMAGE_OT_reload(wmOperatorType *ot)
1262 ot->name= "Reload Image";
1263 ot->idname= "IMAGE_OT_reload";
1266 ot->exec= reload_exec;
1269 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1272 /********************** new image operator *********************/
1274 static int image_new_exec(bContext *C, wmOperator *op)
1280 PointerRNA ptr, idptr;
1282 char name[MAX_ID_NAME-2];
1284 int width, height, floatbuf, uvtestgrid, alpha;
1286 /* retrieve state */
1287 sima= CTX_wm_space_image(C);
1288 scene= (Scene*)CTX_data_scene(C);
1289 obedit= CTX_data_edit_object(C);
1291 RNA_string_get(op->ptr, "name", name);
1292 width= RNA_int_get(op->ptr, "width");
1293 height= RNA_int_get(op->ptr, "height");
1294 floatbuf= RNA_boolean_get(op->ptr, "float");
1295 uvtestgrid= RNA_boolean_get(op->ptr, "uv_test_grid");
1296 RNA_float_get_array(op->ptr, "color", color);
1297 alpha= RNA_boolean_get(op->ptr, "alpha");
1299 if (!floatbuf && scene->r.color_mgt_flag & R_COLOR_MANAGEMENT)
1300 linearrgb_to_srgb_v3_v3(color, color);
1305 ima = BKE_add_image_size(width, height, name, alpha ? 32 : 24, floatbuf, uvtestgrid, color);
1308 return OPERATOR_CANCELLED;
1311 uiIDContextProperty(C, &ptr, &prop);
1314 /* when creating new ID blocks, use is already 1, but RNA
1315 * pointer se also increases user, so this compensates it */
1318 RNA_id_pointer_create(&ima->id, &idptr);
1319 RNA_property_pointer_set(&ptr, prop, idptr);
1320 RNA_property_update(C, &ptr, prop);
1323 ED_space_image_set(C, sima, scene, obedit, ima);
1326 BKE_image_signal(ima, (sima)? &sima->iuser: NULL, IMA_SIGNAL_USER_NEW_IMAGE);
1328 return OPERATOR_FINISHED;
1331 /* XXX, Ton is not a fan of OK buttons but using this function to avoid undo/redo bug while in mesh-editmode, - campbell */
1332 static int image_new_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
1334 return WM_operator_props_dialog_popup(C, op, 300, 100);
1338 void IMAGE_OT_new(wmOperatorType *ot)
1341 static float default_color[4]= {0.0f, 0.0f, 0.0f, 1.0f};
1344 ot->name= "New Image";
1345 ot->idname= "IMAGE_OT_new";
1348 ot->exec= image_new_exec;
1349 ot->invoke= image_new_invoke;
1352 ot->flag= OPTYPE_UNDO;
1355 RNA_def_string(ot->srna, "name", "untitled", MAX_ID_NAME-2, "Name", "Image datablock name.");
1356 RNA_def_int(ot->srna, "width", 1024, 1, INT_MAX, "Width", "Image width.", 1, 16384);
1357 RNA_def_int(ot->srna, "height", 1024, 1, INT_MAX, "Height", "Image height.", 1, 16384);
1358 prop= RNA_def_float_color(ot->srna, "color", 4, NULL, 0.0f, FLT_MAX, "Color", "Default fill color.", 0.0f, 1.0f);
1359 RNA_def_property_float_array_default(prop, default_color);
1360 RNA_def_boolean(ot->srna, "alpha", 1, "Alpha", "Create an image with an alpha channel.");
1361 RNA_def_boolean(ot->srna, "uv_test_grid", 0, "UV Test Grid", "Fill the image with a grid for UV map testing.");
1362 RNA_def_boolean(ot->srna, "float", 0, "32 bit Float", "Create image with 32 bit floating point bit depth.");
1365 /********************* invert operators *********************/
1367 static int image_invert_poll(bContext *C)
1369 Image *ima= CTX_data_edit_image(C);
1370 ImBuf *ibuf= BKE_image_get_ibuf(ima, NULL);
1377 static int image_invert_exec(bContext *C, wmOperator *op)
1379 Image *ima= CTX_data_edit_image(C);
1380 ImBuf *ibuf= BKE_image_get_ibuf(ima, NULL);
1382 // flags indicate if this channel should be inverted
1383 const short r= RNA_boolean_get(op->ptr, "invert_r");
1384 const short g= RNA_boolean_get(op->ptr, "invert_g");
1385 const short b= RNA_boolean_get(op->ptr, "invert_b");
1386 const short a= RNA_boolean_get(op->ptr, "invert_a");
1390 if( ibuf == NULL) // TODO: this should actually never happen, but does for render-results -> cleanup
1391 return OPERATOR_CANCELLED;
1393 /* TODO: make this into an IMB_invert_channels(ibuf,r,g,b,a) method!? */
1394 if (ibuf->rect_float) {
1396 float *fp = (float *) ibuf->rect_float;
1397 for( i = ibuf->x * ibuf->y; i > 0; i--, fp+=4 ) {
1398 if( r ) fp[0] = 1.0f - fp[0];
1399 if( g ) fp[1] = 1.0f - fp[1];
1400 if( b ) fp[2] = 1.0f - fp[2];
1401 if( a ) fp[3] = 1.0f - fp[3];
1405 IMB_rect_from_float(ibuf);
1408 else if(ibuf->rect) {
1410 char *cp = (char *) ibuf->rect;
1411 for( i = ibuf->x * ibuf->y; i > 0; i--, cp+=4 ) {
1412 if( r ) cp[0] = 255 - cp[0];
1413 if( g ) cp[1] = 255 - cp[1];
1414 if( b ) cp[2] = 255 - cp[2];
1415 if( a ) cp[3] = 255 - cp[3];
1419 return OPERATOR_CANCELLED;
1422 ibuf->userflags |= IB_BITMAPDIRTY;
1423 WM_event_add_notifier(C, NC_IMAGE|NA_EDITED, ima);
1424 return OPERATOR_FINISHED;
1427 void IMAGE_OT_invert(wmOperatorType *ot)
1430 ot->name= "Invert Channels";
1431 ot->idname= "IMAGE_OT_invert";
1434 ot->exec= image_invert_exec;
1435 ot->poll= image_invert_poll;
1438 RNA_def_boolean(ot->srna, "invert_r", 0, "Red", "Invert Red Channel");
1439 RNA_def_boolean(ot->srna, "invert_g", 0, "Green", "Invert Green Channel");
1440 RNA_def_boolean(ot->srna, "invert_b", 0, "Blue", "Invert Blue Channel");
1441 RNA_def_boolean(ot->srna, "invert_a", 0, "Alpha", "Invert Alpha Channel");
1444 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1447 /********************* pack operator *********************/
1449 static int pack_test(bContext *C, wmOperator *op)
1451 Image *ima= CTX_data_edit_image(C);
1452 int as_png= RNA_boolean_get(op->ptr, "as_png");
1456 if(!as_png && ima->packedfile)
1459 if(ima->source==IMA_SRC_SEQUENCE || ima->source==IMA_SRC_MOVIE) {
1460 BKE_report(op->reports, RPT_ERROR, "Packing movies or image sequences not supported.");
1467 static int pack_exec(bContext *C, wmOperator *op)
1469 Image *ima= CTX_data_edit_image(C);
1470 ImBuf *ibuf= BKE_image_get_ibuf(ima, NULL);
1471 int as_png= RNA_boolean_get(op->ptr, "as_png");
1473 if(!pack_test(C, op))
1474 return OPERATOR_CANCELLED;
1476 if(!as_png && (ibuf && (ibuf->userflags & IB_BITMAPDIRTY))) {
1477 BKE_report(op->reports, RPT_ERROR, "Can't pack edited image from disk, only as internal PNG.");
1478 return OPERATOR_CANCELLED;
1482 BKE_image_memorypack(ima);
1484 ima->packedfile= newPackedFile(op->reports, ima->name);
1486 WM_event_add_notifier(C, NC_IMAGE|NA_EDITED, ima);
1488 return OPERATOR_FINISHED;
1491 static int pack_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
1493 Image *ima= CTX_data_edit_image(C);
1494 ImBuf *ibuf= BKE_image_get_ibuf(ima, NULL);
1497 int as_png= RNA_boolean_get(op->ptr, "as_png");
1499 if(!pack_test(C, op))
1500 return OPERATOR_CANCELLED;
1502 if(!as_png && (ibuf && (ibuf->userflags & IB_BITMAPDIRTY))) {
1503 pup= uiPupMenuBegin(C, "OK", ICON_QUESTION);
1504 layout= uiPupMenuLayout(pup);
1505 uiItemBooleanO(layout, "Can't pack edited image from disk. Pack as internal PNG?", ICON_NONE, op->idname, "as_png", 1);
1506 uiPupMenuEnd(C, pup);
1508 return OPERATOR_CANCELLED;
1511 return pack_exec(C, op);
1514 void IMAGE_OT_pack(wmOperatorType *ot)
1517 ot->name= "Pack Image";
1518 ot->description= "Pack an image as embedded data into the .blend file";
1519 ot->idname= "IMAGE_OT_pack";
1522 ot->exec= pack_exec;
1523 ot->invoke= pack_invoke;
1526 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1529 RNA_def_boolean(ot->srna, "as_png", 0, "Pack As PNG", "Pack image as lossless PNG.");
1532 /********************* unpack operator *********************/
1534 static int image_unpack_exec(bContext *C, wmOperator *op)
1536 Image *ima= CTX_data_edit_image(C);
1537 int method= RNA_enum_get(op->ptr, "method");
1539 /* find the suppplied image by name */
1540 if (RNA_property_is_set(op->ptr, "id")) {
1541 char imaname[MAX_ID_NAME-2];
1542 RNA_string_get(op->ptr, "id", imaname);
1543 ima = BLI_findstring(&CTX_data_main(C)->image, imaname, offsetof(ID, name) + 2);
1544 if (!ima) ima = CTX_data_edit_image(C);
1547 if(!ima || !ima->packedfile)
1548 return OPERATOR_CANCELLED;
1550 if(ima->source==IMA_SRC_SEQUENCE || ima->source==IMA_SRC_MOVIE) {
1551 BKE_report(op->reports, RPT_ERROR, "Unpacking movies or image sequences not supported.");
1552 return OPERATOR_CANCELLED;
1555 if(G.fileflags & G_AUTOPACK)
1556 BKE_report(op->reports, RPT_WARNING, "AutoPack is enabled, so image will be packed again on file save.");
1558 /* XXX unpackImage frees image buffers */
1559 ED_preview_kill_jobs(C);
1561 unpackImage(op->reports, ima, method);
1563 WM_event_add_notifier(C, NC_IMAGE|NA_EDITED, ima);
1565 return OPERATOR_FINISHED;
1568 static int image_unpack_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
1570 Image *ima= CTX_data_edit_image(C);
1572 if(RNA_property_is_set(op->ptr, "id"))
1573 return image_unpack_exec(C, op);
1575 if(!ima || !ima->packedfile)
1576 return OPERATOR_CANCELLED;
1578 if(ima->source==IMA_SRC_SEQUENCE || ima->source==IMA_SRC_MOVIE) {
1579 BKE_report(op->reports, RPT_ERROR, "Unpacking movies or image sequences not supported.");
1580 return OPERATOR_CANCELLED;
1583 if(G.fileflags & G_AUTOPACK)
1584 BKE_report(op->reports, RPT_WARNING, "AutoPack is enabled, so image will be packed again on file save.");
1586 unpack_menu(C, "IMAGE_OT_unpack", ima->id.name+2, ima->name, "textures", ima->packedfile);
1588 return OPERATOR_FINISHED;
1591 void IMAGE_OT_unpack(wmOperatorType *ot)
1594 ot->name= "Unpack Image";
1595 ot->description= "Save an image packed in the .blend file to disk";
1596 ot->idname= "IMAGE_OT_unpack";
1599 ot->exec= image_unpack_exec;
1600 ot->invoke= image_unpack_invoke;
1603 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1606 RNA_def_enum(ot->srna, "method", unpack_method_items, PF_USE_LOCAL, "Method", "How to unpack.");
1607 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 */
1610 /******************** sample image operator ********************/
1612 typedef struct ImageSampleInfo {
1631 static void sample_draw(const bContext *UNUSED(C), ARegion *ar, void *arg_info)
1633 ImageSampleInfo *info= arg_info;
1635 /* no color management needed for images (color_manage=0) */
1636 draw_image_info(ar, 0, info->channels, info->x, info->y, info->colp, info->colfp, info->zp, info->zfp);
1640 static void sample_apply(bContext *C, wmOperator *op, wmEvent *event)
1642 SpaceImage *sima= CTX_wm_space_image(C);
1643 ARegion *ar= CTX_wm_region(C);
1645 ImBuf *ibuf= ED_space_image_acquire_buffer(sima, &lock);
1646 ImageSampleInfo *info= op->customdata;
1651 ED_space_image_release_buffer(sima, lock);
1655 mx= event->x - ar->winrct.xmin;
1656 my= event->y - ar->winrct.ymin;
1657 UI_view2d_region_to_view(&ar->v2d, mx, my, &fx, &fy);
1659 if(fx>=0.0f && fy>=0.0f && fx<1.0f && fy<1.0f) {
1662 int x= (int)(fx*ibuf->x), y= (int)(fy*ibuf->y);
1664 CLAMP(x, 0, ibuf->x-1);
1665 CLAMP(y, 0, ibuf->y-1);
1670 info->channels= ibuf->channels;
1678 cp= (char *)(ibuf->rect + y*ibuf->x + x);
1680 info->col[0]= cp[0];
1681 info->col[1]= cp[1];
1682 info->col[2]= cp[2];
1683 info->col[3]= cp[3];
1684 info->colp= info->col;
1686 info->colf[0]= (float)cp[0]/255.0f;
1687 info->colf[1]= (float)cp[1]/255.0f;
1688 info->colf[2]= (float)cp[2]/255.0f;
1689 info->colf[3]= (float)cp[3]/255.0f;
1690 info->colfp= info->colf;
1692 if(ibuf->rect_float) {
1693 fp= (ibuf->rect_float + (ibuf->channels)*(y*ibuf->x + x));
1695 info->colf[0]= fp[0];
1696 info->colf[1]= fp[1];
1697 info->colf[2]= fp[2];
1698 info->colf[3]= fp[3];
1699 info->colfp= info->colf;
1703 info->z= ibuf->zbuf[y*ibuf->x + x];
1706 if(ibuf->zbuf_float) {
1707 info->zf= ibuf->zbuf_float[y*ibuf->x + x];
1708 info->zfp= &info->zf;
1711 if(sima->cumap && ibuf->channels==4) {
1712 /* we reuse this callback for set curves point operators */
1713 if(RNA_struct_find_property(op->ptr, "point")) {
1714 int point= RNA_enum_get(op->ptr, "point");
1717 curvemapping_set_black_white(sima->cumap, NULL, info->colfp);
1718 if(ibuf->rect_float)
1719 curvemapping_do_ibuf(sima->cumap, ibuf);
1721 else if(point == 0) {
1722 curvemapping_set_black_white(sima->cumap, info->colfp, NULL);
1723 if(ibuf->rect_float)
1724 curvemapping_do_ibuf(sima->cumap, ibuf);
1729 // XXX node curve integration ..
1732 ScrArea *sa, *cur= curarea;
1734 node_curvemap_sample(fp); /* sends global to node editor */
1735 for(sa= G.curscreen->areabase.first; sa; sa= sa->next) {
1736 if(sa->spacetype==SPACE_NODE) {
1737 areawinset(sa->win);
1738 scrarea_do_windraw(sa);
1741 node_curvemap_sample(NULL); /* clears global in node editor */
1749 ED_space_image_release_buffer(sima, lock);
1750 ED_area_tag_redraw(CTX_wm_area(C));
1753 static void sample_exit(bContext *C, wmOperator *op)
1755 ImageSampleInfo *info= op->customdata;
1757 ED_region_draw_cb_exit(info->art, info->draw_handle);
1758 ED_area_tag_redraw(CTX_wm_area(C));
1762 static int sample_invoke(bContext *C, wmOperator *op, wmEvent *event)
1764 SpaceImage *sima= CTX_wm_space_image(C);
1765 ARegion *ar= CTX_wm_region(C);
1766 ImageSampleInfo *info;
1768 if(!ED_space_image_has_buffer(sima))
1769 return OPERATOR_CANCELLED;
1771 info= MEM_callocN(sizeof(ImageSampleInfo), "ImageSampleInfo");
1772 info->art= ar->type;
1773 info->draw_handle = ED_region_draw_cb_activate(ar->type, sample_draw, info, REGION_DRAW_POST_PIXEL);
1774 op->customdata= info;
1776 sample_apply(C, op, event);
1778 WM_event_add_modal_handler(C, op);
1780 return OPERATOR_RUNNING_MODAL;
1783 static int sample_modal(bContext *C, wmOperator *op, wmEvent *event)
1785 switch(event->type) {
1787 case RIGHTMOUSE: // XXX hardcoded
1789 return OPERATOR_CANCELLED;
1791 sample_apply(C, op, event);
1795 return OPERATOR_RUNNING_MODAL;
1798 static int sample_cancel(bContext *C, wmOperator *op)
1801 return OPERATOR_CANCELLED;
1804 void IMAGE_OT_sample(wmOperatorType *ot)
1807 ot->name= "Sample Color";
1808 ot->idname= "IMAGE_OT_sample";
1811 ot->invoke= sample_invoke;
1812 ot->modal= sample_modal;
1813 ot->cancel= sample_cancel;
1814 ot->poll= space_image_main_area_poll;
1817 ot->flag= OPTYPE_BLOCKING;
1820 /******************** sample line operator ********************/
1821 static int sample_line_exec(bContext *C, wmOperator *op)
1823 SpaceImage *sima= CTX_wm_space_image(C);
1824 ARegion *ar= CTX_wm_region(C);
1825 Scene *scene= CTX_data_scene(C);
1827 int x_start= RNA_int_get(op->ptr, "xstart");
1828 int y_start= RNA_int_get(op->ptr, "ystart");
1829 int x_end= RNA_int_get(op->ptr, "xend");
1830 int y_end= RNA_int_get(op->ptr, "yend");
1833 ImBuf *ibuf= ED_space_image_acquire_buffer(sima, &lock);
1834 Histogram *hist= &sima->sample_line_hist;
1836 float x1f, y1f, x2f, y2f;
1844 ED_space_image_release_buffer(sima, lock);
1845 return OPERATOR_CANCELLED;
1848 if (ibuf->channels < 3) {
1849 ED_space_image_release_buffer(sima, lock);
1850 return OPERATOR_CANCELLED;
1853 UI_view2d_region_to_view(&ar->v2d, x_start, y_start, &x1f, &y1f);
1854 UI_view2d_region_to_view(&ar->v2d, x_end, y_end, &x2f, &y2f);
1855 x1= 0.5f+ x1f*ibuf->x;
1856 x2= 0.5f+ x2f*ibuf->x;
1857 y1= 0.5f+ y1f*ibuf->y;
1858 y2= 0.5f+ y2f*ibuf->y;
1861 hist->x_resolution = 256;
1865 for (i=0; i<256; i++) {
1866 x= (int)(0.5f + x1 + (float)i*(x2-x1)/255.0f);
1867 y= (int)(0.5f + y1 + (float)i*(y2-y1)/255.0f);
1869 if (x<0 || y<0 || x>=ibuf->x || y>=ibuf->y) {
1870 hist->data_luma[i] = hist->data_r[i] = hist->data_g[i]= hist->data_b[i] = 0.0f;
1872 if (ibuf->rect_float) {
1873 fp= (ibuf->rect_float + (ibuf->channels)*(y*ibuf->x + x));
1875 if (scene->r.color_mgt_flag & R_COLOR_MANAGEMENT)
1876 linearrgb_to_srgb_v3_v3(rgb, fp);
1878 copy_v3_v3(rgb, fp);
1880 hist->data_r[i] = rgb[0];
1881 hist->data_g[i] = rgb[1];
1882 hist->data_b[i] = rgb[2];
1883 hist->data_luma[i] = (0.299f*rgb[0] + 0.587f*rgb[1] + 0.114f*rgb[2]);
1885 else if (ibuf->rect) {
1886 cp= (unsigned char *)(ibuf->rect + y*ibuf->x + x);
1887 hist->data_r[i] = (float)cp[0]/255.0f;
1888 hist->data_g[i] = (float)cp[1]/255.0f;
1889 hist->data_b[i] = (float)cp[2]/255.0f;
1890 hist->data_luma[i] = (0.299f*cp[0] + 0.587f*cp[1] + 0.114f*cp[2])/255;
1895 ED_space_image_release_buffer(sima, lock);
1897 ED_area_tag_redraw(CTX_wm_area(C));
1899 return OPERATOR_FINISHED;
1902 static int sample_line_invoke(bContext *C, wmOperator *op, wmEvent *event)
1904 SpaceImage *sima= CTX_wm_space_image(C);
1906 if(!ED_space_image_has_buffer(sima))
1907 return OPERATOR_CANCELLED;
1909 return WM_gesture_straightline_invoke(C, op, event);
1912 void IMAGE_OT_sample_line(wmOperatorType *ot)
1915 ot->name= "Sample Line";
1916 ot->idname= "IMAGE_OT_sample_line";
1919 ot->invoke= sample_line_invoke;
1920 ot->modal= WM_gesture_straightline_modal;
1921 ot->exec= sample_line_exec;
1922 ot->poll= space_image_main_area_poll;
1925 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1927 WM_operator_properties_gesture_straightline(ot, CURSOR_EDIT);
1930 /******************** set curve point operator ********************/
1932 void IMAGE_OT_curves_point_set(wmOperatorType *ot)
1934 static EnumPropertyItem point_items[]= {
1935 {0, "BLACK_POINT", 0, "Black Point", ""},
1936 {1, "WHITE_POINT", 0, "White Point", ""},
1937 {0, NULL, 0, NULL, NULL}};
1940 ot->name= "Set Curves Point";
1941 ot->idname= "IMAGE_OT_curves_point_set";
1944 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1947 ot->invoke= sample_invoke;
1948 ot->modal= sample_modal;
1949 ot->cancel= sample_cancel;
1950 ot->poll= space_image_main_area_poll;
1953 RNA_def_enum(ot->srna, "point", point_items, 0, "Point", "Set black point or white point for curves.");
1956 /******************** record composite operator *********************/
1958 typedef struct RecordCompositeData {
1962 } RecordCompositeData;
1964 static int record_composite_apply(bContext *C, wmOperator *op)
1966 SpaceImage *sima= CTX_wm_space_image(C);
1967 RecordCompositeData *rcd= op->customdata;
1968 Scene *scene= CTX_data_scene(C);
1971 WM_timecursor(CTX_wm_window(C), scene->r.cfra);
1973 // XXX scene->nodetree->test_break= blender_test_break;
1974 // XXX scene->nodetree->test_break= NULL;
1976 BKE_image_all_free_anim_ibufs(scene->r.cfra);
1977 ntreeCompositTagAnimated(scene->nodetree);
1978 ntreeCompositExecTree(scene->nodetree, &scene->r, scene->r.cfra != rcd->old_cfra); /* 1 is no previews */
1980 ED_area_tag_redraw(CTX_wm_area(C));
1982 ibuf= BKE_image_get_ibuf(sima->image, &sima->iuser);
1983 /* save memory in flipbooks */
1985 imb_freerectfloatImBuf(ibuf);
1989 return (scene->r.cfra <= rcd->efra);
1992 static int record_composite_init(bContext *C, wmOperator *op)
1994 SpaceImage *sima= CTX_wm_space_image(C);
1995 Scene *scene= CTX_data_scene(C);
1996 RecordCompositeData *rcd;
1998 if(sima->iuser.frames < 2)
2000 if(scene->nodetree == NULL)
2003 op->customdata= rcd= MEM_callocN(sizeof(RecordCompositeData), "ImageRecordCompositeData");
2005 rcd->old_cfra= scene->r.cfra;
2006 rcd->sfra= sima->iuser.sfra;
2007 rcd->efra= sima->iuser.sfra + sima->iuser.frames-1;
2008 scene->r.cfra= rcd->sfra;
2013 static void record_composite_exit(bContext *C, wmOperator *op)
2015 Scene *scene= CTX_data_scene(C);
2016 SpaceImage *sima= CTX_wm_space_image(C);
2017 RecordCompositeData *rcd= op->customdata;
2019 scene->r.cfra= rcd->old_cfra;
2021 WM_cursor_restore(CTX_wm_window(C));
2024 WM_event_remove_timer(CTX_wm_manager(C), CTX_wm_window(C), rcd->timer);
2026 WM_event_add_notifier(C, NC_IMAGE|NA_EDITED, sima->image);
2028 // XXX play_anim(0);
2029 // XXX allqueue(REDRAWNODE, 1);
2034 static int record_composite_exec(bContext *C, wmOperator *op)
2036 if(!record_composite_init(C, op))
2037 return OPERATOR_CANCELLED;
2039 while(record_composite_apply(C, op))
2042 record_composite_exit(C, op);
2044 return OPERATOR_FINISHED;
2047 static int record_composite_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
2049 RecordCompositeData *rcd;
2051 if(!record_composite_init(C, op))
2052 return OPERATOR_CANCELLED;
2054 rcd= op->customdata;
2055 rcd->timer= WM_event_add_timer(CTX_wm_manager(C), CTX_wm_window(C), TIMER, 0.0f);
2056 WM_event_add_modal_handler(C, op);
2058 if(!record_composite_apply(C, op))
2059 return OPERATOR_FINISHED;
2061 return OPERATOR_RUNNING_MODAL;
2064 static int record_composite_modal(bContext *C, wmOperator *op, wmEvent *event)
2066 RecordCompositeData *rcd= op->customdata;
2068 switch(event->type) {
2070 if(rcd->timer == event->customdata) {
2071 if(!record_composite_apply(C, op)) {
2072 record_composite_exit(C, op);
2073 return OPERATOR_FINISHED;
2078 record_composite_exit(C, op);
2079 return OPERATOR_FINISHED;
2082 return OPERATOR_RUNNING_MODAL;
2085 static int record_composite_cancel(bContext *C, wmOperator *op)
2087 record_composite_exit(C, op);
2088 return OPERATOR_CANCELLED;
2091 void IMAGE_OT_record_composite(wmOperatorType *ot)
2094 ot->name= "Record Composite";
2095 ot->idname= "IMAGE_OT_record_composite";
2098 ot->exec= record_composite_exec;
2099 ot->invoke= record_composite_invoke;
2100 ot->modal= record_composite_modal;
2101 ot->cancel= record_composite_cancel;
2102 ot->poll= space_image_buffer_exists_poll;
2105 /********************* cycle render slot operator *********************/
2107 static int cycle_render_slot_poll(bContext *C)
2109 Image *ima= CTX_data_edit_image(C);
2111 return (ima && ima->type == IMA_TYPE_R_RESULT);
2114 static int cycle_render_slot_exec(bContext *C, wmOperator *op)
2116 Image *ima= CTX_data_edit_image(C);
2117 int a, slot, cur= ima->render_slot;
2118 const short use_reverse= RNA_boolean_get(op->ptr, "reverse");
2120 for(a=1; a<IMA_MAX_RENDER_SLOT; a++) {
2121 slot= (cur + (use_reverse ? -a:a))%IMA_MAX_RENDER_SLOT;
2122 if(slot<0) slot+=IMA_MAX_RENDER_SLOT;
2124 if(ima->renders[slot] || slot == ima->last_render_slot) {
2125 ima->render_slot= slot;
2128 else if((slot - 1) == ima->last_render_slot && slot < IMA_MAX_RENDER_SLOT) {
2129 ima->render_slot= slot;
2134 if(a == IMA_MAX_RENDER_SLOT)
2135 ima->render_slot= ((cur == 1)? 0: 1);
2137 WM_event_add_notifier(C, NC_IMAGE|ND_DRAW, NULL);
2139 /* no undo push for browsing existing */
2140 if(ima->renders[ima->render_slot] || ima->render_slot==ima->last_render_slot)
2141 return OPERATOR_CANCELLED;
2143 return OPERATOR_FINISHED;
2146 void IMAGE_OT_cycle_render_slot(wmOperatorType *ot)
2149 ot->name= "Cycle Render Slot";
2150 ot->idname= "IMAGE_OT_cycle_render_slot";
2153 ot->exec= cycle_render_slot_exec;
2154 ot->poll= cycle_render_slot_poll;
2157 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2159 RNA_def_boolean(ot->srna, "reverse", 0, "Cycle in Reverse", "");
2162 /******************** TODO ********************/
2166 /* goes over all ImageUsers, and sets frame numbers if auto-refresh is set */
2168 void ED_image_update_frame(const Main *mainp, int cfra)
2170 wmWindowManager *wm;
2175 for(tex= mainp->tex.first; tex; tex= tex->id.next) {
2176 if(tex->type==TEX_IMAGE && tex->ima) {
2177 if(ELEM(tex->ima->source, IMA_SRC_MOVIE, IMA_SRC_SEQUENCE)) {
2178 if(tex->iuser.flag & IMA_ANIM_ALWAYS)
2179 BKE_image_user_calc_frame(&tex->iuser, cfra, 0);
2184 /* image window, compo node users */
2185 for(wm=mainp->wm.first; wm; wm= wm->id.next) { /* only 1 wm */
2186 for(win= wm->windows.first; win; win= win->next) {
2188 for(sa= win->screen->areabase.first; sa; sa= sa->next) {
2189 if(sa->spacetype==SPACE_VIEW3D) {
2190 View3D *v3d= sa->spacedata.first;
2192 for(bgpic= v3d->bgpicbase.first; bgpic; bgpic= bgpic->next)
2193 if(bgpic->iuser.flag & IMA_ANIM_ALWAYS)
2194 BKE_image_user_calc_frame(&bgpic->iuser, cfra, 0);
2196 else if(sa->spacetype==SPACE_IMAGE) {
2197 SpaceImage *sima= sa->spacedata.first;
2198 if(sima->iuser.flag & IMA_ANIM_ALWAYS)
2199 BKE_image_user_calc_frame(&sima->iuser, cfra, 0);
2201 else if(sa->spacetype==SPACE_NODE) {
2202 SpaceNode *snode= sa->spacedata.first;
2203 if((snode->treetype==NTREE_COMPOSIT) && (snode->nodetree)) {
2205 for(node= snode->nodetree->nodes.first; node; node= node->next) {
2206 if(node->id && node->type==CMP_NODE_IMAGE) {
2207 Image *ima= (Image *)node->id;
2208 ImageUser *iuser= node->storage;
2209 if(ELEM(ima->source, IMA_SRC_MOVIE, IMA_SRC_SEQUENCE))
2210 if(iuser->flag & IMA_ANIM_ALWAYS)
2211 BKE_image_user_calc_frame(iuser, cfra, 0);