2.5
[blender.git] / source / blender / editors / space_image / image_ops.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
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.
10  *
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.
15  *
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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * Contributor(s): Blender Foundation, 2002-2009
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 #include <string.h>
29 #include <stdlib.h>
30
31 #include "MEM_guardedalloc.h"
32
33 #include "DNA_image_types.h"
34 #include "DNA_node_types.h"
35 #include "DNA_object_types.h"
36 #include "DNA_packedFile_types.h"
37 #include "DNA_space_types.h"
38 #include "DNA_scene_types.h"
39 #include "DNA_screen_types.h"
40 #include "DNA_userdef_types.h"
41 #include "DNA_windowmanager_types.h"
42
43 #include "BKE_colortools.h"
44 #include "BKE_context.h"
45 #include "BKE_image.h"
46 #include "BKE_global.h"
47 #include "BKE_library.h"
48 #include "BKE_node.h"
49 #include "BKE_packedFile.h"
50 #include "BKE_report.h"
51 #include "BKE_screen.h"
52
53 #include "BLI_arithb.h"
54 #include "BLI_blenlib.h"
55
56 #include "IMB_imbuf.h"
57 #include "IMB_imbuf_types.h"
58
59 #include "RE_pipeline.h"
60
61 #include "RNA_access.h"
62 #include "RNA_define.h"
63 #include "RNA_types.h"
64
65 #include "ED_image.h"
66 #include "ED_screen.h"
67 #include "ED_space_api.h"
68 #include "ED_uvedit.h"
69
70 #include "UI_interface.h"
71 #include "UI_resources.h"
72 #include "UI_view2d.h"
73
74 #include "WM_api.h"
75 #include "WM_types.h"
76
77 #include "image_intern.h"
78
79 /******************** view navigation utilities *********************/
80
81 static void sima_zoom_set(SpaceImage *sima, ARegion *ar, float zoom)
82 {
83         float oldzoom= sima->zoom;
84         int width, height;
85
86         sima->zoom= zoom;
87
88         if (sima->zoom > 0.1f && sima->zoom < 4.0f)
89                 return;
90
91         /* check zoom limits */
92         ED_space_image_size(sima, &width, &height);
93
94         width *= sima->zoom;
95         height *= sima->zoom;
96
97         if((width < 4) && (height < 4))
98                 sima->zoom= oldzoom;
99         else if((ar->winrct.xmax - ar->winrct.xmin) <= sima->zoom)
100                 sima->zoom= oldzoom;
101         else if((ar->winrct.ymax - ar->winrct.ymin) <= sima->zoom)
102                 sima->zoom= oldzoom;
103 }
104
105 static void sima_zoom_set_factor(SpaceImage *sima, ARegion *ar, float zoomfac)
106 {
107         sima_zoom_set(sima, ar, sima->zoom*zoomfac);
108 }
109
110 static int space_image_poll(bContext *C)
111 {
112         SpaceImage *sima= (SpaceImage*)CTX_wm_space_data(C);
113         if(sima && sima->spacetype==SPACE_IMAGE)
114                 if(ED_space_image_buffer(sima))
115                         return 1;
116         return 0;
117 }
118
119 static int space_image_file_exists_poll(bContext *C)
120 {
121         if(space_image_poll(C)) {
122                 SpaceImage *sima= (SpaceImage*)CTX_wm_space_data(C);
123                 ImBuf *ibuf= ED_space_image_buffer(sima);
124                 
125                 if(ibuf && BLI_exists(ibuf->name) && BLI_is_writable(ibuf->name))
126                         return 1;
127         }
128         return 0;
129 }
130
131
132 int space_image_main_area_poll(bContext *C)
133 {
134         SpaceLink *slink= CTX_wm_space_data(C);
135         ARegion *ar= CTX_wm_region(C);
136
137         if(slink && (slink->spacetype == SPACE_IMAGE))
138                 return (ar && ar->type->regionid == RGN_TYPE_WINDOW);
139         
140         return 0;
141 }
142
143 /********************** view pan operator *********************/
144
145 typedef struct ViewPanData {
146         float x, y;
147         float xof, yof;
148 } ViewPanData;
149
150 static void view_pan_init(bContext *C, wmOperator *op, wmEvent *event)
151 {
152         SpaceImage *sima= (SpaceImage*)CTX_wm_space_data(C);
153         ViewPanData *vpd;
154
155         op->customdata= vpd= MEM_callocN(sizeof(ViewPanData), "ImageViewPanData");
156         WM_cursor_modal(CTX_wm_window(C), BC_NSEW_SCROLLCURSOR);
157
158         vpd->x= event->x;
159         vpd->y= event->y;
160         vpd->xof= sima->xof;
161         vpd->yof= sima->yof;
162
163         WM_event_add_modal_handler(C, &CTX_wm_window(C)->handlers, op);
164 }
165
166 static void view_pan_exit(bContext *C, wmOperator *op, int cancel)
167 {
168         SpaceImage *sima= (SpaceImage*)CTX_wm_space_data(C);
169         ViewPanData *vpd= op->customdata;
170
171         if(cancel) {
172                 sima->xof= vpd->xof;
173                 sima->yof= vpd->yof;
174                 ED_area_tag_redraw(CTX_wm_area(C));
175         }
176
177         WM_cursor_restore(CTX_wm_window(C));
178         MEM_freeN(op->customdata);
179 }
180
181 static int view_pan_exec(bContext *C, wmOperator *op)
182 {
183         SpaceImage *sima= (SpaceImage*)CTX_wm_space_data(C);
184         float offset[2];
185
186         RNA_float_get_array(op->ptr, "offset", offset);
187         sima->xof += offset[0];
188         sima->yof += offset[1];
189
190         ED_area_tag_redraw(CTX_wm_area(C));
191
192         /* XXX notifier? */
193 #if 0
194         if(image_preview_active(curarea, NULL, NULL)) {
195                 /* recalculates new preview rect */
196                 scrarea_do_windraw(curarea);
197                 image_preview_event(2);
198         }
199 #endif
200         
201         return OPERATOR_FINISHED;
202 }
203
204 static int view_pan_invoke(bContext *C, wmOperator *op, wmEvent *event)
205 {
206         view_pan_init(C, op, event);
207         return OPERATOR_RUNNING_MODAL;
208 }
209
210 static int view_pan_modal(bContext *C, wmOperator *op, wmEvent *event)
211 {
212         SpaceImage *sima= (SpaceImage*)CTX_wm_space_data(C);
213         ViewPanData *vpd= op->customdata;
214         float offset[2];
215
216         switch(event->type) {
217                 case MOUSEMOVE:
218                         sima->xof= vpd->xof;
219                         sima->yof= vpd->yof;
220                         offset[0]= (vpd->x - event->x)/sima->zoom;
221                         offset[1]= (vpd->y - event->y)/sima->zoom;
222                         RNA_float_set_array(op->ptr, "offset", offset);
223                         view_pan_exec(C, op);
224                         break;
225                 case MIDDLEMOUSE:
226                         if(event->val==0) {
227                                 view_pan_exit(C, op, 0);
228                                 return OPERATOR_FINISHED;
229                         }
230                         break;
231         }
232
233         return OPERATOR_RUNNING_MODAL;
234 }
235
236 static int view_pan_cancel(bContext *C, wmOperator *op)
237 {
238         view_pan_exit(C, op, 1);
239         return OPERATOR_CANCELLED;
240 }
241
242 void IMAGE_OT_view_pan(wmOperatorType *ot)
243 {
244         /* identifiers */
245         ot->name= "View Pan";
246         ot->idname= "IMAGE_OT_view_pan";
247         
248         /* api callbacks */
249         ot->exec= view_pan_exec;
250         ot->invoke= view_pan_invoke;
251         ot->modal= view_pan_modal;
252         ot->cancel= view_pan_cancel;
253         ot->poll= space_image_main_area_poll;
254         
255         /* properties */
256         RNA_def_float_vector(ot->srna, "offset", 2, NULL, -FLT_MAX, FLT_MAX,
257                 "Offset", "Offset in floating point units, 1.0 is the width and height of the image.", -FLT_MAX, FLT_MAX);
258 }
259
260 /********************** view zoom operator *********************/
261
262 typedef struct ViewZoomData {
263         float x, y;
264         float zoom;
265 } ViewZoomData;
266
267 static void view_zoom_init(bContext *C, wmOperator *op, wmEvent *event)
268 {
269         SpaceImage *sima= (SpaceImage*)CTX_wm_space_data(C);
270         ViewZoomData *vpd;
271
272         op->customdata= vpd= MEM_callocN(sizeof(ViewZoomData), "ImageViewZoomData");
273         WM_cursor_modal(CTX_wm_window(C), BC_NSEW_SCROLLCURSOR);
274
275         vpd->x= event->x;
276         vpd->y= event->y;
277         vpd->zoom= sima->zoom;
278
279         WM_event_add_modal_handler(C, &CTX_wm_window(C)->handlers, op);
280 }
281
282 static void view_zoom_exit(bContext *C, wmOperator *op, int cancel)
283 {
284         SpaceImage *sima= (SpaceImage*)CTX_wm_space_data(C);
285         ViewZoomData *vpd= op->customdata;
286
287         if(cancel) {
288                 sima->zoom= vpd->zoom;
289                 ED_area_tag_redraw(CTX_wm_area(C));
290         }
291
292         WM_cursor_restore(CTX_wm_window(C));
293         MEM_freeN(op->customdata);
294 }
295
296 static int view_zoom_exec(bContext *C, wmOperator *op)
297 {
298         SpaceImage *sima= (SpaceImage*)CTX_wm_space_data(C);
299         ARegion *ar= CTX_wm_region(C);
300
301         sima_zoom_set_factor(sima, ar, RNA_float_get(op->ptr, "factor"));
302
303         ED_area_tag_redraw(CTX_wm_area(C));
304
305         /* XXX notifier? */
306 #if 0
307         if(image_preview_active(curarea, NULL, NULL)) {
308                 /* recalculates new preview rect */
309                 scrarea_do_windraw(curarea);
310                 image_preview_event(2);
311         }
312 #endif
313         
314         return OPERATOR_FINISHED;
315 }
316
317 static int view_zoom_invoke(bContext *C, wmOperator *op, wmEvent *event)
318 {
319         view_zoom_init(C, op, event);
320         return OPERATOR_RUNNING_MODAL;
321 }
322
323 static int view_zoom_modal(bContext *C, wmOperator *op, wmEvent *event)
324 {
325         SpaceImage *sima= (SpaceImage*)CTX_wm_space_data(C);
326         ARegion *ar= CTX_wm_region(C);
327         ViewZoomData *vpd= op->customdata;
328         float factor;
329
330         switch(event->type) {
331                 case MOUSEMOVE:
332                         factor= 1.0 + (vpd->x-event->x+vpd->y-event->y)/300.0f;
333                         RNA_float_set(op->ptr, "factor", factor);
334                         sima_zoom_set(sima, ar, vpd->zoom*factor);
335                         ED_area_tag_redraw(CTX_wm_area(C));
336                         break;
337                 case MIDDLEMOUSE:
338                         if(event->val==0) {
339                                 view_zoom_exit(C, op, 0);
340                                 return OPERATOR_FINISHED;
341                         }
342                         break;
343         }
344
345         return OPERATOR_RUNNING_MODAL;
346 }
347
348 static int view_zoom_cancel(bContext *C, wmOperator *op)
349 {
350         view_zoom_exit(C, op, 1);
351         return OPERATOR_CANCELLED;
352 }
353
354 void IMAGE_OT_view_zoom(wmOperatorType *ot)
355 {
356         /* identifiers */
357         ot->name= "View Zoom";
358         ot->idname= "IMAGE_OT_view_zoom";
359         
360         /* api callbacks */
361         ot->exec= view_zoom_exec;
362         ot->invoke= view_zoom_invoke;
363         ot->modal= view_zoom_modal;
364         ot->cancel= view_zoom_cancel;
365         ot->poll= space_image_main_area_poll;
366         
367         /* properties */
368         RNA_def_float(ot->srna, "factor", 0.0f, 0.0f, FLT_MAX,
369                 "Factor", "Zoom factor, values higher than 1.0 zoom in, lower values zoom out.", -FLT_MAX, FLT_MAX);
370 }
371
372 /********************** view all operator *********************/
373
374 /* Updates the fields of the View2D member of the SpaceImage struct.
375  * Default behavior is to reset the position of the image and set the zoom to 1
376  * If the image will not fit within the window rectangle, the zoom is adjusted */
377
378 static int view_all_exec(bContext *C, wmOperator *op)
379 {
380         SpaceImage *sima;
381         ARegion *ar;
382         Scene *scene;
383         Object *obedit;
384         Image *ima;
385         ImBuf *ibuf;
386         float aspx, aspy, zoomx, zoomy, w, h;
387         int width, height;
388
389         /* retrieve state */
390         sima= (SpaceImage*)CTX_wm_space_data(C);
391         ar= CTX_wm_region(C);
392         scene= (Scene*)CTX_data_scene(C);
393         obedit= CTX_data_edit_object(C);
394
395         ima= ED_space_image(sima);
396         ibuf= ED_space_image_buffer(sima);
397         ED_space_image_size(sima, &width, &height);
398         ED_space_image_aspect(sima, &aspx, &aspy);
399
400         w= width*aspx;
401         h= height*aspy;
402         
403         /* check if the image will fit in the image with zoom==1 */
404         width = ar->winrct.xmax - ar->winrct.xmin + 1;
405         height = ar->winrct.ymax - ar->winrct.ymin + 1;
406
407         if((w >= width || h >= height) && (width > 0 && height > 0)) {
408                 /* find the zoom value that will fit the image in the image space */
409                 zoomx= width/w;
410                 zoomy= height/h;
411                 sima_zoom_set(sima, ar, 1.0f/power_of_2(1/MIN2(zoomx, zoomy)));
412         }
413         else
414                 sima_zoom_set(sima, ar, 1.0f);
415
416         sima->xof= sima->yof= 0.0f;
417
418         ED_area_tag_redraw(CTX_wm_area(C));
419         
420         return OPERATOR_FINISHED;
421 }
422
423 void IMAGE_OT_view_all(wmOperatorType *ot)
424 {
425         /* identifiers */
426         ot->name= "View All";
427         ot->idname= "IMAGE_OT_view_all";
428         
429         /* api callbacks */
430         ot->exec= view_all_exec;
431         ot->poll= space_image_main_area_poll;
432 }
433
434 /********************** view selected operator *********************/
435
436 static int view_selected_exec(bContext *C, wmOperator *op)
437 {
438         SpaceImage *sima;
439         ARegion *ar;
440         Scene *scene;
441         Object *obedit;
442         Image *ima;
443         ImBuf *ibuf;
444         float size, min[2], max[2], d[2];
445         int width, height;
446
447         /* retrieve state */
448         sima= (SpaceImage*)CTX_wm_space_data(C);
449         ar= CTX_wm_region(C);
450         scene= (Scene*)CTX_data_scene(C);
451         obedit= CTX_data_edit_object(C);
452
453         ima= ED_space_image(sima);
454         ibuf= ED_space_image_buffer(sima);
455         ED_space_image_size(sima, &width, &height);
456
457         /* get bounds */
458         if(!ED_uvedit_minmax(scene, ima, obedit, min, max))
459                 return OPERATOR_CANCELLED;
460
461         /* adjust offset and zoom */
462         sima->xof= (int)(((min[0] + max[0])*0.5f - 0.5f)*width);
463         sima->yof= (int)(((min[1] + max[1])*0.5f - 0.5f)*height);
464
465         d[0]= max[0] - min[0];
466         d[1]= max[1] - min[1];
467         size= 0.5*MAX2(d[0], d[1])*MAX2(width, height)/256.0f;
468         
469         if(size<=0.01) size= 0.01;
470         sima_zoom_set(sima, ar, 0.7/size);
471
472         ED_area_tag_redraw(CTX_wm_area(C));
473         
474         return OPERATOR_FINISHED;
475 }
476
477 void IMAGE_OT_view_selected(wmOperatorType *ot)
478 {
479         /* identifiers */
480         ot->name= "View Center";
481         ot->idname= "IMAGE_OT_view_selected";
482         
483         /* api callbacks */
484         ot->exec= view_selected_exec;
485         ot->poll= ED_operator_uvedit;
486 }
487
488 /********************** view zoom in/out operator *********************/
489
490 static int view_zoom_in_exec(bContext *C, wmOperator *op)
491 {
492         SpaceImage *sima= (SpaceImage*)CTX_wm_space_data(C);
493         ARegion *ar= CTX_wm_region(C);
494
495         sima_zoom_set_factor(sima, ar, 1.25f);
496
497         ED_area_tag_redraw(CTX_wm_area(C));
498         
499         return OPERATOR_FINISHED;
500 }
501
502 void IMAGE_OT_view_zoom_in(wmOperatorType *ot)
503 {
504         /* identifiers */
505         ot->name= "View Zoom In";
506         ot->idname= "IMAGE_OT_view_zoom_in";
507         
508         /* api callbacks */
509         ot->exec= view_zoom_in_exec;
510         ot->poll= space_image_main_area_poll;
511 }
512
513 static int view_zoom_out_exec(bContext *C, wmOperator *op)
514 {
515         SpaceImage *sima= (SpaceImage*)CTX_wm_space_data(C);
516         ARegion *ar= CTX_wm_region(C);
517
518         sima_zoom_set_factor(sima, ar, 0.8f);
519
520         ED_area_tag_redraw(CTX_wm_area(C));
521         
522         return OPERATOR_FINISHED;
523 }
524
525 void IMAGE_OT_view_zoom_out(wmOperatorType *ot)
526 {
527         /* identifiers */
528         ot->name= "View Zoom Out";
529         ot->idname= "IMAGE_OT_view_zoom_out";
530         
531         /* api callbacks */
532         ot->exec= view_zoom_out_exec;
533         ot->poll= space_image_main_area_poll;
534 }
535
536 /********************** view zoom ratio operator *********************/
537
538 static int view_zoom_ratio_exec(bContext *C, wmOperator *op)
539 {
540         SpaceImage *sima= (SpaceImage*)CTX_wm_space_data(C);
541         ARegion *ar= CTX_wm_region(C);
542
543         sima_zoom_set(sima, ar, RNA_float_get(op->ptr, "ratio"));
544         
545         /* ensure pixel exact locations for draw */
546         sima->xof= (int)sima->xof;
547         sima->yof= (int)sima->yof;
548
549         /* XXX notifier? */
550 #if 0
551         if(image_preview_active(curarea, NULL, NULL)) {
552                 /* recalculates new preview rect */
553                 scrarea_do_windraw(curarea);
554                 image_preview_event(2);
555         }
556 #endif
557
558         ED_area_tag_redraw(CTX_wm_area(C));
559         
560         return OPERATOR_FINISHED;
561 }
562
563 void IMAGE_OT_view_zoom_ratio(wmOperatorType *ot)
564 {
565         /* identifiers */
566         ot->name= "View Zoom Ratio";
567         ot->idname= "IMAGE_OT_view_zoom_ratio";
568         
569         /* api callbacks */
570         ot->exec= view_zoom_ratio_exec;
571         ot->poll= space_image_main_area_poll;
572         
573         /* properties */
574         RNA_def_float(ot->srna, "ratio", 0.0f, 0.0f, FLT_MAX,
575                 "Ratio", "Zoom ratio, 1.0 is 1:1, higher is zoomed in, lower is zoomed out.", -FLT_MAX, FLT_MAX);
576 }
577
578 /**************** load/replace/save callbacks ******************/
579
580 static char *filesel_imagetype_string(Image *ima)
581 {
582         char *strp, *str= MEM_callocN(14*32, "menu for filesel");
583         
584         strp= str;
585         str += sprintf(str, "Save Image as: %%t|");
586         str += sprintf(str, "Targa %%x%d|", R_TARGA);
587         str += sprintf(str, "Targa Raw %%x%d|", R_RAWTGA);
588         str += sprintf(str, "PNG %%x%d|", R_PNG);
589         str += sprintf(str, "BMP %%x%d|", R_BMP);
590         str += sprintf(str, "Jpeg %%x%d|", R_JPEG90);
591         str += sprintf(str, "Iris %%x%d|", R_IRIS);
592         if(G.have_libtiff)
593                 str += sprintf(str, "Tiff %%x%d|", R_TIFF);
594         str += sprintf(str, "Radiance HDR %%x%d|", R_RADHDR);
595         str += sprintf(str, "Cineon %%x%d|", R_CINEON);
596         str += sprintf(str, "DPX %%x%d|", R_DPX);
597 #ifdef WITH_OPENEXR
598         str += sprintf(str, "OpenEXR %%x%d|", R_OPENEXR);
599         /* saving sequences of multilayer won't work, they copy buffers  */
600         if(ima->source==IMA_SRC_SEQUENCE && ima->type==IMA_TYPE_MULTILAYER);
601         else str += sprintf(str, "MultiLayer %%x%d|", R_MULTILAYER);
602 #endif  
603         return strp;
604 }
605
606 static void image_filesel(bContext *C, wmOperator *op, const char *path)
607 {
608         RNA_string_set(op->ptr, "filename", path);
609         WM_event_add_fileselect(C, op); 
610 }
611
612 /******************** open image operator ********************/
613
614 static int open_exec(bContext *C, wmOperator *op)
615 {
616         SpaceImage *sima= (SpaceImage*)CTX_wm_space_data(C);
617         Scene *scene= CTX_data_scene(C);
618         Object *obedit= CTX_data_edit_object(C);
619         Image *ima= NULL;
620         char str[FILE_MAX];
621
622         RNA_string_get(op->ptr, "filename", str);
623         ima= BKE_add_image_file(str, scene->r.cfra);
624
625         if(!ima)
626                 return OPERATOR_CANCELLED;
627
628         BKE_image_signal(ima, &sima->iuser, IMA_SIGNAL_RELOAD);
629         ED_space_image_set(C, sima, scene, obedit, ima);
630
631         return OPERATOR_FINISHED;
632 }
633
634 static int open_invoke(bContext *C, wmOperator *op, wmEvent *event)
635 {
636         SpaceImage *sima= (SpaceImage*)CTX_wm_space_data(C);
637         char *path= (sima->image)? sima->image->name: U.textudir;
638
639         if(RNA_property_is_set(op->ptr, "filename"))
640                 return open_exec(C, op);
641         
642         image_filesel(C, op, path);
643
644         return OPERATOR_RUNNING_MODAL;
645 }
646
647 void IMAGE_OT_open(wmOperatorType *ot)
648 {
649         /* identifiers */
650         ot->name= "Open";
651         ot->idname= "IMAGE_OT_open";
652         
653         /* api callbacks */
654         ot->exec= open_exec;
655         ot->invoke= open_invoke;
656         ot->poll= ED_operator_image_active;
657
658         /* flags */
659         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
660
661         /* properties */
662         RNA_def_string_file_path(ot->srna, "filename", "", FILE_MAX, "Filename", "File path of image to open.");
663 }
664
665 /******************** replace image operator ********************/
666
667 static int replace_exec(bContext *C, wmOperator *op)
668 {
669         SpaceImage *sima= (SpaceImage*)CTX_wm_space_data(C);
670         char str[FILE_MAX];
671
672         if(!sima->image)
673                 return OPERATOR_CANCELLED;
674         
675         RNA_string_get(op->ptr, "filename", str);
676         BLI_strncpy(sima->image->name, str, sizeof(sima->image->name)-1); /* we cant do much if the str is longer then 240 :/ */
677
678         BKE_image_signal(sima->image, &sima->iuser, IMA_SIGNAL_RELOAD);
679         WM_event_add_notifier(C, NC_IMAGE|NA_EDITED, sima->image);
680
681         return OPERATOR_FINISHED;
682 }
683
684 static int replace_invoke(bContext *C, wmOperator *op, wmEvent *event)
685 {
686         SpaceImage *sima= (SpaceImage*)CTX_wm_space_data(C);
687         char *path= (sima->image)? sima->image->name: U.textudir;
688
689         if(!sima->image)
690                 return OPERATOR_CANCELLED;
691
692         if(RNA_property_is_set(op->ptr, "filename"))
693                 return replace_exec(C, op);
694
695         image_filesel(C, op, path);
696
697         return OPERATOR_RUNNING_MODAL;
698 }
699
700 void IMAGE_OT_replace(wmOperatorType *ot)
701 {
702         /* identifiers */
703         ot->name= "Replace";
704         ot->idname= "IMAGE_OT_replace";
705         
706         /* api callbacks */
707         ot->exec= replace_exec;
708         ot->invoke= replace_invoke;
709         ot->poll= space_image_poll;
710
711         /* flags */
712         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
713
714         /* properties */
715         RNA_def_string_file_path(ot->srna, "filename", "", FILE_MAX, "Filename", "File path of image to replace current image with.");
716 }
717
718 /******************** save image as operator ********************/
719
720 /* assumes name is FILE_MAX */
721 /* ima->name and ibuf->name should end up the same */
722 static void save_image_doit(bContext *C, SpaceImage *sima, Scene *scene, wmOperator *op, char *name)
723 {
724         Image *ima= ED_space_image(sima);
725         ImBuf *ibuf= ED_space_image_buffer(sima);
726         int len;
727
728         if (ibuf) {     
729                 BLI_convertstringcode(name, G.sce);
730                 BLI_convertstringframe(name, scene->r.cfra);
731                 
732                 if(scene->r.scemode & R_EXTENSION)  {
733                         BKE_add_image_extension(scene, name, sima->imtypenr);
734                         BKE_add_image_extension(scene, name, sima->imtypenr);
735                 }
736                 
737                 /* enforce user setting for RGB or RGBA, but skip BW */
738                 if(scene->r.planes==32)
739                         ibuf->depth= 32;
740                 else if(scene->r.planes==24)
741                         ibuf->depth= 24;
742                 
743                 WM_cursor_wait(1);
744
745                 if(sima->imtypenr==R_MULTILAYER) {
746                         RenderResult *rr= BKE_image_get_renderresult(scene, ima);
747                         if(rr) {
748                                 RE_WriteRenderResult(rr, name, scene->r.quality);
749                                 
750                                 BLI_strncpy(ima->name, name, sizeof(ima->name));
751                                 BLI_strncpy(ibuf->name, name, sizeof(ibuf->name));
752                                 
753                                 /* should be function? nevertheless, saving only happens here */
754                                 for(ibuf= ima->ibufs.first; ibuf; ibuf= ibuf->next)
755                                         ibuf->userflags &= ~IB_BITMAPDIRTY;
756                                 
757                         }
758                         else
759                                 BKE_report(op->reports, RPT_ERROR, "Did not write, no Multilayer Image");
760                 }
761                 else if (BKE_write_ibuf(scene, ibuf, name, sima->imtypenr, scene->r.subimtype, scene->r.quality)) {
762                         BLI_strncpy(ima->name, name, sizeof(ima->name));
763                         BLI_strncpy(ibuf->name, name, sizeof(ibuf->name));
764                         
765                         ibuf->userflags &= ~IB_BITMAPDIRTY;
766                         
767                         /* change type? */
768                         if( ELEM(ima->source, IMA_SRC_GENERATED, IMA_SRC_VIEWER)) {
769                                 ima->source= IMA_SRC_FILE;
770                                 ima->type= IMA_TYPE_IMAGE;
771                         }
772                         if(ima->type==IMA_TYPE_R_RESULT)
773                                 ima->type= IMA_TYPE_IMAGE;
774                         
775                         /* name image as how we saved it */
776                         len= strlen(name);
777                         while (len > 0 && name[len - 1] != '/' && name[len - 1] != '\\') len--;
778                         rename_id(&ima->id, name+len);
779                 } 
780                 else
781                         BKE_reportf(op->reports, RPT_ERROR, "Couldn't write image: %s", name);
782
783                 WM_event_add_notifier(C, NC_IMAGE|NA_EDITED, sima->image);
784
785                 WM_cursor_wait(0);
786         }
787 }
788
789 static int save_as_exec(bContext *C, wmOperator *op)
790 {
791         SpaceImage *sima= (SpaceImage*)CTX_wm_space_data(C);
792         Scene *scene= CTX_data_scene(C);
793         Image *ima = ED_space_image(sima);
794         char str[FILE_MAX];
795
796         if(!ima)
797                 return OPERATOR_CANCELLED;
798
799         RNA_string_get(op->ptr, "filename", str);
800         save_image_doit(C, sima, scene, op, str);
801
802         return OPERATOR_FINISHED;
803 }
804
805 static int save_as_invoke(bContext *C, wmOperator *op, wmEvent *event)
806 {
807         SpaceImage *sima= (SpaceImage*)CTX_wm_space_data(C);
808         Image *ima = ED_space_image(sima);
809         ImBuf *ibuf= ED_space_image_buffer(sima);
810         Scene *scene= CTX_data_scene(C);
811
812         if(RNA_property_is_set(op->ptr, "filename"))
813                 return save_as_exec(C, op);
814         
815         if(!ima)
816                 return OPERATOR_CANCELLED;
817
818         /* always opens fileselect */
819         if(ibuf) {
820                 char *strp;
821                 
822                 strp= filesel_imagetype_string(ima); // XXX unused still
823                 
824                 /* cant save multilayer sequence, ima->rr isn't valid for a specific frame */
825                 if(ima->rr && !(ima->source==IMA_SRC_SEQUENCE && ima->type==IMA_TYPE_MULTILAYER))
826                         sima->imtypenr= R_MULTILAYER;
827                 else if(ima->type==IMA_TYPE_R_RESULT)
828                         sima->imtypenr= scene->r.imtype;
829                 else
830                         sima->imtypenr= BKE_ftype_to_imtype(ibuf->ftype);
831                 
832                 if(ibuf->name[0]==0)
833                         BLI_strncpy(ibuf->name, G.ima, FILE_MAX);
834                 
835                 // XXX note: we can give default menu enums to operator for this 
836                 image_filesel(C, op, ibuf->name);
837
838                 MEM_freeN(strp);
839                 
840                 return OPERATOR_RUNNING_MODAL;
841         }
842
843         return OPERATOR_CANCELLED;
844 }
845
846 void IMAGE_OT_save_as(wmOperatorType *ot)
847 {
848         /* identifiers */
849         ot->name= "Save As";
850         ot->idname= "IMAGE_OT_save_as";
851         
852         /* api callbacks */
853         ot->exec= save_as_exec;
854         ot->invoke= save_as_invoke;
855         ot->poll= space_image_poll;
856
857         /* flags */
858         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
859
860         /* properties */
861         RNA_def_string_file_path(ot->srna, "filename", "", FILE_MAX, "Filename", "File path to save image to.");
862 }
863
864 /******************** save image operator ********************/
865
866 static int save_exec(bContext *C, wmOperator *op)
867 {
868         SpaceImage *sima= (SpaceImage*)CTX_wm_space_data(C);
869         Image *ima = ED_space_image(sima);
870         ImBuf *ibuf= ED_space_image_buffer(sima);
871         Scene *scene= CTX_data_scene(C);
872         char name[FILE_MAX];
873
874         if(!ima || !ibuf)
875                 return OPERATOR_CANCELLED;
876
877         /* if exists, saves over without fileselect */
878         
879         BLI_strncpy(name, ibuf->name, FILE_MAX);
880         if(name[0]==0)
881                 BLI_strncpy(name, G.ima, FILE_MAX);
882         
883         if(BLI_exists(name) && BLI_is_writable(name)) {
884                 if(BKE_image_get_renderresult(scene, ima)) 
885                         sima->imtypenr= R_MULTILAYER;
886                 else 
887                         sima->imtypenr= BKE_ftype_to_imtype(ibuf->ftype);
888                 
889                 save_image_doit(C, sima, scene, op, name);
890         }
891         else {
892                 BKE_report(op->reports, RPT_ERROR, "Can not save image.");
893                 return OPERATOR_CANCELLED;
894         }
895
896         return OPERATOR_FINISHED;
897 }
898
899 void IMAGE_OT_save(wmOperatorType *ot)
900 {
901         /* identifiers */
902         ot->name= "Save";
903         ot->idname= "IMAGE_OT_save";
904         
905         /* api callbacks */
906         ot->exec= save_exec;
907         ot->poll= space_image_file_exists_poll;
908
909         /* flags */
910         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
911 }
912
913 /******************* save sequence operator ********************/
914
915 static int save_sequence_exec(bContext *C, wmOperator *op)
916 {
917         SpaceImage *sima= (SpaceImage*)CTX_wm_space_data(C);
918         ImBuf *ibuf;
919         int tot= 0;
920         char di[FILE_MAX], fi[FILE_MAX];
921         
922         if(sima->image==NULL)
923                 return OPERATOR_CANCELLED;
924
925         if(sima->image->source!=IMA_SRC_SEQUENCE) {
926                 BKE_report(op->reports, RPT_ERROR, "Can only save sequence on image sequences.");
927                 return OPERATOR_CANCELLED;
928         }
929
930         if(sima->image->type==IMA_TYPE_MULTILAYER) {
931                 BKE_report(op->reports, RPT_ERROR, "Can't save multilayer sequences.");
932                 return OPERATOR_CANCELLED;
933         }
934         
935         /* get total */
936         for(ibuf= sima->image->ibufs.first; ibuf; ibuf= ibuf->next) 
937                 if(ibuf->userflags & IB_BITMAPDIRTY)
938                         tot++;
939         
940         if(tot==0) {
941                 BKE_report(op->reports, RPT_WARNING, "No images have been changed.");
942                 return OPERATOR_CANCELLED;
943         }
944
945         /* get a filename for menu */
946         for(ibuf= sima->image->ibufs.first; ibuf; ibuf= ibuf->next) 
947                 if(ibuf->userflags & IB_BITMAPDIRTY)
948                         break;
949         
950         BLI_strncpy(di, ibuf->name, FILE_MAX);
951         BLI_splitdirstring(di, fi);
952         
953         BKE_reportf(op->reports, RPT_INFO, "%d Image(s) will be saved in %s", tot, di);
954
955         for(ibuf= sima->image->ibufs.first; ibuf; ibuf= ibuf->next) {
956                 if(ibuf->userflags & IB_BITMAPDIRTY) {
957                         char name[FILE_MAX];
958                         BLI_strncpy(name, ibuf->name, sizeof(name));
959                         
960                         BLI_convertstringcode(name, G.sce);
961
962                         if(0 == IMB_saveiff(ibuf, name, IB_rect | IB_zbuf | IB_zbuffloat)) {
963                                 BKE_reportf(op->reports, RPT_ERROR, "Could not write image %s.", name);
964                                 break;
965                         }
966
967                         printf("Saved: %s\n", ibuf->name);
968                         ibuf->userflags &= ~IB_BITMAPDIRTY;
969                 }
970         }
971
972         return OPERATOR_FINISHED;
973 }
974
975 void IMAGE_OT_save_sequence(wmOperatorType *ot)
976 {
977         /* identifiers */
978         ot->name= "Save Sequence";
979         ot->idname= "IMAGE_OT_save_sequence";
980         
981         /* api callbacks */
982         ot->exec= save_sequence_exec;
983         ot->poll= space_image_poll;
984
985         /* flags */
986         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
987 }
988
989 /******************** reload image operator ********************/
990
991 static int reload_exec(bContext *C, wmOperator *op)
992 {
993         SpaceImage *sima;
994
995         /* retrieve state */
996         sima= (SpaceImage*)CTX_wm_space_data(C);
997
998         if(!sima->image)
999                 return OPERATOR_CANCELLED;
1000
1001         BKE_image_signal(sima->image, &sima->iuser, IMA_SIGNAL_RELOAD);
1002         /* ED_space_image_set(C, sima, scene, obedit, NULL); - do we really need this? */
1003
1004         // XXX BIF_preview_changed(ID_TE);
1005         WM_event_add_notifier(C, NC_IMAGE|NA_EDITED, sima->image);
1006         ED_area_tag_redraw(CTX_wm_area(C));
1007         
1008         return OPERATOR_FINISHED;
1009 }
1010
1011 void IMAGE_OT_reload(wmOperatorType *ot)
1012 {
1013         /* identifiers */
1014         ot->name= "Reload";
1015         ot->idname= "IMAGE_OT_reload";
1016         
1017         /* api callbacks */
1018         ot->exec= reload_exec;
1019         ot->poll= space_image_poll;
1020
1021         /* flags */
1022         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1023 }
1024
1025 /********************** new image operator *********************/
1026
1027 static int new_exec(bContext *C, wmOperator *op)
1028 {
1029         SpaceImage *sima;
1030         Scene *scene;
1031         Object *obedit;
1032         Image *ima;
1033         char name[22];
1034         float color[4];
1035         int width, height, floatbuf, uvtestgrid;
1036
1037         /* retrieve state */
1038         sima= (SpaceImage*)CTX_wm_space_data(C);
1039         scene= (Scene*)CTX_data_scene(C);
1040         obedit= CTX_data_edit_object(C);
1041
1042         RNA_string_get(op->ptr, "name", name);
1043         width= RNA_int_get(op->ptr, "width");
1044         height= RNA_int_get(op->ptr, "height");
1045         floatbuf= RNA_boolean_get(op->ptr, "float");
1046         uvtestgrid= RNA_boolean_get(op->ptr, "uv_test_grid");
1047         RNA_float_get_array(op->ptr, "color", color);
1048         color[3]= RNA_float_get(op->ptr, "alpha");
1049
1050         ima = BKE_add_image_size(width, height, name, floatbuf, uvtestgrid, color);
1051         BKE_image_signal(sima->image, &sima->iuser, IMA_SIGNAL_USER_NEW_IMAGE);
1052         ED_space_image_set(C, sima, scene, obedit, ima);
1053         
1054         return OPERATOR_FINISHED;
1055 }
1056
1057 void IMAGE_OT_new(wmOperatorType *ot)
1058 {
1059         /* identifiers */
1060         ot->name= "New";
1061         ot->idname= "IMAGE_OT_new";
1062         
1063         /* api callbacks */
1064         ot->exec= new_exec;
1065         ot->poll= ED_operator_image_active;
1066
1067         /* flags */
1068         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1069
1070         /* properties */
1071         RNA_def_string(ot->srna, "name", "Untitled", 21, "Name", "Image datablock name.");
1072         RNA_def_int(ot->srna, "width", 1024, 1, INT_MAX, "Width", "Image width.", 1, 16384);
1073         RNA_def_int(ot->srna, "height", 1024, 1, INT_MAX, "Height", "Image height.", 1, 16384);
1074         RNA_def_float_color(ot->srna, "color", 3, NULL, 0.0f, FLT_MAX, "Color", "Default fill color.", 0.0f, 1.0f);
1075         RNA_def_float(ot->srna, "alpha", 1.0f, 0.0f, 1.0f, "Alpha", "Default fill alpha.", 0.0f, 1.0f);
1076         RNA_def_boolean(ot->srna, "uv_test_grid", 0, "UV Test Grid", "Fill the image with a grid for UV map testing.");
1077         RNA_def_boolean(ot->srna, "float", 0, "32 bit Float", "Create image with 32 bit floating point bit depth.");
1078 }
1079
1080 /********************* pack operator *********************/
1081
1082 static int pack_test(bContext *C, wmOperator *op)
1083 {
1084         Image *ima= CTX_data_edit_image(C);
1085         int as_png= RNA_boolean_get(op->ptr, "as_png");
1086
1087         if(!ima)
1088                 return 0;
1089         if(!as_png && ima->packedfile)
1090                 return 0;
1091
1092         if(ima->source==IMA_SRC_SEQUENCE || ima->source==IMA_SRC_MOVIE) {
1093                 BKE_report(op->reports, RPT_ERROR, "Can't pack movie or image sequence.");
1094                 return 0;
1095         }
1096
1097         return 1;
1098 }
1099
1100 static int pack_exec(bContext *C, wmOperator *op)
1101 {
1102         SpaceImage *sima= (SpaceImage*)CTX_wm_space_data(C);
1103         Image *ima= ED_space_image(sima);
1104         ImBuf *ibuf= ED_space_image_buffer(sima);
1105         int as_png= RNA_boolean_get(op->ptr, "as_png");
1106
1107         if(!pack_test(C, op))
1108                 return OPERATOR_CANCELLED;
1109         
1110         if(!as_png && (ibuf && (ibuf->userflags & IB_BITMAPDIRTY))) {
1111                 BKE_report(op->reports, RPT_ERROR, "Can't pack edited image from disk, only as internal PNG.");
1112                 return OPERATOR_CANCELLED;
1113         }
1114
1115         if(as_png)
1116                 BKE_image_memorypack(ima);
1117         else
1118                 ima->packedfile= newPackedFile(ima->name);
1119
1120         return OPERATOR_FINISHED;
1121 }
1122
1123 static int pack_invoke(bContext *C, wmOperator *op, wmEvent *event)
1124 {
1125         SpaceImage *sima= (SpaceImage*)CTX_wm_space_data(C);
1126         ImBuf *ibuf= ED_space_image_buffer(sima);
1127         uiMenuItem *head;
1128         int as_png= RNA_boolean_get(op->ptr, "as_png");
1129
1130         if(!pack_test(C, op))
1131                 return OPERATOR_CANCELLED;
1132         
1133         if(!as_png && (ibuf && (ibuf->userflags & IB_BITMAPDIRTY))) {
1134                 head= uiPupMenuBegin("OK", ICON_HELP);
1135                 uiMenuItemBooleanO(head, "Can't pack edited image from disk. Pack as internal PNG?", 0, op->idname, "as_png", 1);
1136                 uiPupMenuEnd(C, head);
1137
1138                 return OPERATOR_CANCELLED;
1139         }
1140
1141         return pack_exec(C, op);
1142 }
1143
1144 void IMAGE_OT_pack(wmOperatorType *ot)
1145 {
1146         /* identifiers */
1147         ot->name= "Pack";
1148         ot->idname= "IMAGE_OT_pack";
1149         
1150         /* api callbacks */
1151         ot->exec= pack_exec;
1152         ot->invoke= pack_invoke;
1153         ot->poll= space_image_poll;
1154
1155         /* flags */
1156         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1157
1158         /* properties */
1159         RNA_def_boolean(ot->srna, "as_png", 0, "Pack As PNG", "Pack image as lossless PNG.");
1160 }
1161
1162 /********************* unpack operator *********************/
1163
1164 static int unpack_exec(bContext *C, wmOperator *op)
1165 {
1166         Image *ima= CTX_data_edit_image(C);
1167
1168         if(!ima)
1169                 return OPERATOR_CANCELLED;
1170         if(!ima->packedfile)
1171                 return OPERATOR_CANCELLED;
1172
1173         if(ima->source==IMA_SRC_SEQUENCE || ima->source==IMA_SRC_MOVIE) {
1174                 BKE_report(op->reports, RPT_ERROR, "Can't unpack movie or image sequence.");
1175                 return OPERATOR_CANCELLED;
1176         }
1177
1178         if(G.fileflags & G_AUTOPACK)
1179                 BKE_report(op->reports, RPT_WARNING, "AutoPack is enabled, so image will be packed again on file save.");
1180         
1181         unpackImage(ima, PF_ASK);
1182
1183         return OPERATOR_FINISHED;
1184 }
1185
1186 void IMAGE_OT_unpack(wmOperatorType *ot)
1187 {
1188         /* identifiers */
1189         ot->name= "Unpack";
1190         ot->idname= "IMAGE_OT_unpack";
1191         
1192         /* api callbacks */
1193         ot->exec= unpack_exec;
1194         ot->poll= space_image_poll;
1195
1196         /* flags */
1197         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1198 }
1199
1200 /******************** sample image operator ********************/
1201
1202 typedef struct ImageSampleInfo {
1203         ARegionType *art;
1204         void *draw_handle;
1205         int x, y;
1206
1207         char col[4];
1208         float colf[4];
1209         int z;
1210         float zf;
1211
1212         char *colp;
1213         float *colfp;
1214         int *zp;
1215         float *zfp;
1216
1217         int draw;
1218 } ImageSampleInfo;
1219
1220 static void sample_draw(const bContext *C, ARegion *ar, void *arg_info)
1221 {
1222         SpaceImage *sima= (SpaceImage*)CTX_wm_space_data(C);
1223         ImBuf *ibuf= ED_space_image_buffer(sima);
1224         ImageSampleInfo *info= arg_info;
1225
1226         if(ibuf == NULL)
1227                 return;
1228         
1229         draw_image_info(ar, ibuf->channels, info->x, info->y, info->colp,
1230                 info->colfp, info->zp, info->zfp);
1231 }
1232
1233 static void sample_apply(bContext *C, wmOperator *op, wmEvent *event)
1234 {
1235         SpaceImage *sima= (SpaceImage*)CTX_wm_space_data(C);
1236         ARegion *ar= CTX_wm_region(C);
1237         ImBuf *ibuf= ED_space_image_buffer(sima);
1238         ImageSampleInfo *info= op->customdata;
1239         float fx, fy;
1240         int x, y;
1241         
1242         if(ibuf == NULL)
1243                 return;
1244
1245         x= event->x - ar->winrct.xmin;
1246         y= event->y - ar->winrct.ymin;
1247         UI_view2d_region_to_view(&ar->v2d, x, y, &fx, &fy);
1248
1249         if(fx>=0.0 && fy>=0.0 && fx<1.0 && fy<1.0) {
1250                 float *fp;
1251                 char *cp;
1252                 int x= (int)(fx*ibuf->x), y= (int)(fy*ibuf->y);
1253
1254                 CLAMP(x, 0, ibuf->x-1);
1255                 CLAMP(y, 0, ibuf->y-1);
1256
1257                 info->x= x;
1258                 info->y= y;
1259                 info->draw= 1;
1260
1261                 info->colp= NULL;
1262                 info->colfp= NULL;
1263                 info->zp= NULL;
1264                 info->zfp= NULL;
1265                 
1266                 if(ibuf->rect) {
1267                         cp= (char *)(ibuf->rect + y*ibuf->x + x);
1268
1269                         info->col[0]= cp[0];
1270                         info->col[1]= cp[1];
1271                         info->col[2]= cp[2];
1272                         info->col[3]= cp[3];
1273                         info->colp= info->col;
1274
1275                         info->colf[0]= (float)cp[0]/255.0f;
1276                         info->colf[1]= (float)cp[1]/255.0f;
1277                         info->colf[2]= (float)cp[2]/255.0f;
1278                         info->colf[3]= (float)cp[3]/255.0f;
1279                         info->colfp= info->colf;
1280                 }
1281                 if(ibuf->rect_float) {
1282                         fp= (ibuf->rect_float + (ibuf->channels)*(y*ibuf->x + x));
1283
1284                         info->colf[0]= fp[0];
1285                         info->colf[1]= fp[1];
1286                         info->colf[2]= fp[2];
1287                         info->colf[3]= fp[4];
1288                         info->colfp= info->colf;
1289                 }
1290
1291                 if(ibuf->zbuf) {
1292                         info->z= ibuf->zbuf[y*ibuf->x + x];
1293                         info->zp= &info->z;
1294                 }
1295                 if(ibuf->zbuf_float) {
1296                         info->zf= ibuf->zbuf_float[y*ibuf->x + x];
1297                         info->zfp= &info->zf;
1298                 }
1299                 
1300                 if(sima->cumap && ibuf->channels==4) {
1301                         /* we reuse this callback for set curves point operators */
1302                         if(RNA_struct_find_property(op->ptr, "point")) {
1303                                 int point= RNA_enum_get(op->ptr, "point");
1304
1305                                 if(point == 1) {
1306                                         curvemapping_set_black_white(sima->cumap, NULL, info->colfp);
1307                                         curvemapping_do_ibuf(sima->cumap, ibuf);
1308                                 }
1309                                 else if(point == 0) {
1310                                         curvemapping_set_black_white(sima->cumap, info->colfp, NULL);
1311                                         curvemapping_do_ibuf(sima->cumap, ibuf);
1312                                 }
1313                         }
1314                 }
1315                                 
1316                 // XXX node curve integration ..
1317 #if 0
1318                 {
1319                         ScrArea *sa, *cur= curarea;
1320                         
1321                         node_curvemap_sample(fp);       /* sends global to node editor */
1322                         for(sa= G.curscreen->areabase.first; sa; sa= sa->next) {
1323                                 if(sa->spacetype==SPACE_NODE) {
1324                                         areawinset(sa->win);
1325                                         scrarea_do_windraw(sa);
1326                                 }
1327                         }
1328                         node_curvemap_sample(NULL);             /* clears global in node editor */
1329                         curarea= cur;
1330                 }
1331 #endif
1332         }
1333         else
1334                 info->draw= 0;
1335
1336         ED_area_tag_redraw(CTX_wm_area(C));
1337 }
1338
1339 static void sample_exit(bContext *C, wmOperator *op)
1340 {
1341         ImageSampleInfo *info= op->customdata;
1342
1343         ED_region_draw_cb_exit(info->art, info->draw_handle);
1344         ED_area_tag_redraw(CTX_wm_area(C));
1345         MEM_freeN(info);
1346 }
1347
1348 static int sample_invoke(bContext *C, wmOperator *op, wmEvent *event)
1349 {
1350         SpaceImage *sima= (SpaceImage*)CTX_wm_space_data(C);
1351         ARegion *ar= CTX_wm_region(C);
1352         ImBuf *ibuf= ED_space_image_buffer(sima);
1353         ImageSampleInfo *info;
1354
1355         if(ibuf == NULL)
1356                 return OPERATOR_CANCELLED;
1357         
1358         info= MEM_callocN(sizeof(ImageSampleInfo), "ImageSampleInfo");
1359         info->art= ar->type;
1360         info->draw_handle = ED_region_draw_cb_activate(ar->type, sample_draw, info, REGION_DRAW_POST);
1361         op->customdata= info;
1362
1363         sample_apply(C, op, event);
1364
1365         WM_event_add_modal_handler(C, &CTX_wm_window(C)->handlers, op);
1366
1367         return OPERATOR_RUNNING_MODAL;
1368 }
1369
1370 static int sample_modal(bContext *C, wmOperator *op, wmEvent *event)
1371 {
1372         switch(event->type) {
1373                 case LEFTMOUSE:
1374                 case RIGHTMOUSE: // XXX hardcoded
1375                         sample_exit(C, op);
1376                         return OPERATOR_CANCELLED;
1377                 case MOUSEMOVE:
1378                         sample_apply(C, op, event);
1379                         break;
1380         }
1381
1382         return OPERATOR_RUNNING_MODAL;
1383 }
1384
1385 static int sample_cancel(bContext *C, wmOperator *op)
1386 {
1387         sample_exit(C, op);
1388         return OPERATOR_CANCELLED;
1389 }
1390
1391 void IMAGE_OT_sample(wmOperatorType *ot)
1392 {
1393         /* identifiers */
1394         ot->name= "Sample";
1395         ot->idname= "IMAGE_OT_sample";
1396         
1397         /* api callbacks */
1398         ot->invoke= sample_invoke;
1399         ot->modal= sample_modal;
1400         ot->cancel= sample_cancel;
1401         ot->poll= space_image_main_area_poll;
1402 }
1403
1404 /******************** set curve point operator ********************/
1405
1406 void IMAGE_OT_curves_point_set(wmOperatorType *ot)
1407 {
1408         static EnumPropertyItem point_items[]= {
1409                 {0, "BLACK_POINT", "Black Point", ""},
1410                 {1, "WHITE_POINT", "White Point", ""},
1411                 {0, NULL, NULL, NULL}};
1412
1413         /* identifiers */
1414         ot->name= "Set Curves Point";
1415         ot->idname= "IMAGE_OT_curves_point_set";
1416
1417         /* flags */
1418         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1419         
1420         /* api callbacks */
1421         ot->invoke= sample_invoke;
1422         ot->modal= sample_modal;
1423         ot->cancel= sample_cancel;
1424         ot->poll= space_image_main_area_poll;
1425
1426         /* properties */
1427         RNA_def_enum(ot->srna, "point", point_items, 0, "Point", "Set black point or white point for curves.");
1428 }
1429
1430 /******************** record composite operator *********************/
1431
1432 typedef struct RecordCompositeData {
1433         wmTimer *timer;
1434         int old_cfra;
1435         int sfra, efra;
1436 } RecordCompositeData;
1437
1438 int record_composite_apply(bContext *C, wmOperator *op)
1439 {
1440         SpaceImage *sima= (SpaceImage*)CTX_wm_space_data(C);
1441         RecordCompositeData *rcd= op->customdata;
1442         Scene *scene= CTX_data_scene(C);
1443         ImBuf *ibuf;
1444         
1445         WM_timecursor(CTX_wm_window(C), scene->r.cfra);
1446
1447         // XXX scene->nodetree->test_break= blender_test_break;
1448         // XXX scene->nodetree->test_break= NULL;
1449         
1450         BKE_image_all_free_anim_ibufs(scene->r.cfra);
1451         ntreeCompositTagAnimated(scene->nodetree);
1452         ntreeCompositExecTree(scene->nodetree, &scene->r, scene->r.cfra != rcd->old_cfra);      /* 1 is no previews */
1453
1454         ED_area_tag_redraw(CTX_wm_area(C));
1455         
1456         ibuf= BKE_image_get_ibuf(sima->image, &sima->iuser);
1457         /* save memory in flipbooks */
1458         if(ibuf)
1459                 imb_freerectfloatImBuf(ibuf);
1460         
1461         scene->r.cfra++;
1462
1463         return (scene->r.cfra <= rcd->efra);
1464 }
1465
1466 static int record_composite_init(bContext *C, wmOperator *op)
1467 {
1468         SpaceImage *sima= (SpaceImage*)CTX_wm_space_data(C);
1469         Scene *scene= CTX_data_scene(C);
1470         RecordCompositeData *rcd;
1471
1472         if(sima->iuser.frames < 2)
1473                 return 0;
1474         if(scene->nodetree == NULL)
1475                 return 0;
1476         
1477         op->customdata= rcd= MEM_callocN(sizeof(RecordCompositeData), "ImageRecordCompositeData");
1478
1479         rcd->old_cfra= scene->r.cfra;
1480         rcd->sfra= sima->iuser.sfra;
1481         rcd->efra= sima->iuser.sfra + sima->iuser.frames-1;
1482         scene->r.cfra= rcd->sfra;
1483
1484         return 1;
1485 }
1486
1487 static void record_composite_exit(bContext *C, wmOperator *op)
1488 {
1489         Scene *scene= CTX_data_scene(C);
1490         SpaceImage *sima= (SpaceImage*)CTX_wm_space_data(C);
1491         RecordCompositeData *rcd= op->customdata;
1492
1493         scene->r.cfra= rcd->old_cfra;
1494
1495         WM_cursor_restore(CTX_wm_window(C));
1496
1497         if(rcd->timer)
1498                 WM_event_remove_window_timer(CTX_wm_window(C), rcd->timer);
1499
1500         WM_event_add_notifier(C, NC_IMAGE|NA_EDITED, sima->image);
1501
1502         // XXX play_anim(0);
1503         // XXX allqueue(REDRAWNODE, 1);
1504
1505         MEM_freeN(rcd);
1506 }
1507
1508 static int record_composite_exec(bContext *C, wmOperator *op)
1509 {
1510         if(!record_composite_init(C, op))
1511                 return OPERATOR_CANCELLED;
1512         
1513         while(record_composite_apply(C, op))
1514                 ;
1515         
1516         record_composite_exit(C, op);
1517         
1518         return OPERATOR_FINISHED;
1519 }
1520
1521 static int record_composite_invoke(bContext *C, wmOperator *op, wmEvent *event)
1522 {
1523         RecordCompositeData *rcd= op->customdata;
1524         
1525         if(!record_composite_init(C, op))
1526                 return OPERATOR_CANCELLED;
1527
1528         rcd= op->customdata;
1529         rcd->timer= WM_event_add_window_timer(CTX_wm_window(C), TIMER, 0.0f);
1530         WM_event_add_modal_handler(C, &CTX_wm_window(C)->handlers, op);
1531
1532         if(!record_composite_apply(C, op))
1533                 return OPERATOR_FINISHED;
1534
1535         return OPERATOR_RUNNING_MODAL;
1536 }
1537
1538 static int record_composite_modal(bContext *C, wmOperator *op, wmEvent *event)
1539 {
1540         RecordCompositeData *rcd= op->customdata;
1541
1542         switch(event->type) {
1543                 case TIMER:
1544                         if(rcd->timer == event->customdata) {
1545                                 if(!record_composite_apply(C, op)) {
1546                                         record_composite_exit(C, op);
1547                                         return OPERATOR_FINISHED;
1548                                 }
1549                         }
1550                         break;
1551                 case ESCKEY:
1552                         record_composite_exit(C, op);
1553                         return OPERATOR_FINISHED;
1554         }
1555
1556         return OPERATOR_RUNNING_MODAL;
1557 }
1558
1559 static int record_composite_cancel(bContext *C, wmOperator *op)
1560 {
1561         record_composite_exit(C, op);
1562         return OPERATOR_CANCELLED;
1563 }
1564
1565 void IMAGE_OT_record_composite(wmOperatorType *ot)
1566 {
1567         /* identifiers */
1568         ot->name= "Record Composite";
1569         ot->idname= "IMAGE_OT_record_composite";
1570         
1571         /* api callbacks */
1572         ot->exec= record_composite_exec;
1573         ot->invoke= record_composite_invoke;
1574         ot->modal= record_composite_modal;
1575         ot->cancel= record_composite_cancel;
1576         ot->poll= space_image_poll;
1577 }
1578
1579 /******************** TODO ********************/
1580
1581 /* XXX notifier? */
1582 #if 0
1583 /* goes over all ImageUsers, and sets frame numbers if auto-refresh is set */
1584 void BIF_image_update_frame(void)
1585 {
1586         Tex *tex;
1587         
1588         /* texture users */
1589         for(tex= G.main->tex.first; tex; tex= tex->id.next) {
1590                 if(tex->type==TEX_IMAGE && tex->ima)
1591                         if(ELEM(tex->ima->source, IMA_SRC_MOVIE, IMA_SRC_SEQUENCE))
1592                                 if(tex->iuser.flag & IMA_ANIM_ALWAYS)
1593                                         BKE_image_user_calc_imanr(&tex->iuser, scene->r.cfra, 0);
1594                 
1595         }
1596         /* image window, compo node users */
1597         if(G.curscreen) {
1598                 ScrArea *sa;
1599                 for(sa= G.curscreen->areabase.first; sa; sa= sa->next) {
1600                         if(sa->spacetype==SPACE_VIEW3D) {
1601                                 View3D *v3d= sa->spacedata.first;
1602                                 if(v3d->bgpic)
1603                                         if(v3d->bgpic->iuser.flag & IMA_ANIM_ALWAYS)
1604                                                 BKE_image_user_calc_imanr(&v3d->bgpic->iuser, scene->r.cfra, 0);
1605                         }
1606                         else if(sa->spacetype==SPACE_IMAGE) {
1607                                 SpaceImage *sima= sa->spacedata.first;
1608                                 if(sima->iuser.flag & IMA_ANIM_ALWAYS)
1609                                         BKE_image_user_calc_imanr(&sima->iuser, scene->r.cfra, 0);
1610                         }
1611                         else if(sa->spacetype==SPACE_NODE) {
1612                                 SpaceNode *snode= sa->spacedata.first;
1613                                 if((snode->treetype==NTREE_COMPOSIT) && (snode->nodetree)) {
1614                                         bNode *node;
1615                                         for(node= snode->nodetree->nodes.first; node; node= node->next) {
1616                                                 if(node->id && node->type==CMP_NODE_IMAGE) {
1617                                                         Image *ima= (Image *)node->id;
1618                                                         ImageUser *iuser= node->storage;
1619                                                         if(ELEM(ima->source, IMA_SRC_MOVIE, IMA_SRC_SEQUENCE))
1620                                                                 if(iuser->flag & IMA_ANIM_ALWAYS)
1621                                                                         BKE_image_user_calc_imanr(iuser, scene->r.cfra, 0);
1622                                                 }
1623                                         }
1624                                 }
1625                         }
1626                 }
1627         }
1628 }
1629 #endif
1630