2.5: Image Editor
[blender-staging.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         // XXX ARegion *ar= CTX_wm_region(C);
136
137         if(slink && (slink->spacetype == SPACE_IMAGE))
138                 return 1; // XXX (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(15*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 #ifdef WITH_OPENJPEG
592         str += sprintf(str, "Jpeg 2000 %%x%d|", R_JP2);
593 #endif
594         str += sprintf(str, "Iris %%x%d|", R_IRIS);
595         if(G.have_libtiff)
596                 str += sprintf(str, "Tiff %%x%d|", R_TIFF);
597         str += sprintf(str, "Radiance HDR %%x%d|", R_RADHDR);
598         str += sprintf(str, "Cineon %%x%d|", R_CINEON);
599         str += sprintf(str, "DPX %%x%d|", R_DPX);
600 #ifdef WITH_OPENEXR
601         str += sprintf(str, "OpenEXR %%x%d|", R_OPENEXR);
602         /* saving sequences of multilayer won't work, they copy buffers  */
603         if(ima->source==IMA_SRC_SEQUENCE && ima->type==IMA_TYPE_MULTILAYER);
604         else str += sprintf(str, "MultiLayer %%x%d|", R_MULTILAYER);
605 #endif  
606         return strp;
607 }
608
609 static void image_filesel(bContext *C, wmOperator *op, const char *path)
610 {
611         RNA_string_set(op->ptr, "filename", path);
612         WM_event_add_fileselect(C, op); 
613 }
614
615 /******************** open image operator ********************/
616
617 static int open_exec(bContext *C, wmOperator *op)
618 {
619         SpaceImage *sima= (SpaceImage*)CTX_wm_space_data(C);
620         Scene *scene= CTX_data_scene(C);
621         Object *obedit= CTX_data_edit_object(C);
622         Image *ima= NULL;
623         char str[FILE_MAX];
624
625         RNA_string_get(op->ptr, "filename", str);
626         ima= BKE_add_image_file(str, scene->r.cfra);
627
628         if(!ima)
629                 return OPERATOR_CANCELLED;
630
631         BKE_image_signal(ima, &sima->iuser, IMA_SIGNAL_RELOAD);
632         ED_space_image_set(C, sima, scene, obedit, ima);
633
634         return OPERATOR_FINISHED;
635 }
636
637 static int open_invoke(bContext *C, wmOperator *op, wmEvent *event)
638 {
639         SpaceImage *sima= (SpaceImage*)CTX_wm_space_data(C);
640         char *path= (sima->image)? sima->image->name: U.textudir;
641
642         if(RNA_property_is_set(op->ptr, "filename"))
643                 return open_exec(C, op);
644         
645         image_filesel(C, op, path);
646
647         return OPERATOR_RUNNING_MODAL;
648 }
649
650 void IMAGE_OT_open(wmOperatorType *ot)
651 {
652         /* identifiers */
653         ot->name= "Open";
654         ot->idname= "IMAGE_OT_open";
655         
656         /* api callbacks */
657         ot->exec= open_exec;
658         ot->invoke= open_invoke;
659         ot->poll= ED_operator_image_active;
660
661         /* flags */
662         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
663
664         /* properties */
665         RNA_def_string_file_path(ot->srna, "filename", "", FILE_MAX, "Filename", "File path of image to open.");
666 }
667
668 /******************** replace image operator ********************/
669
670 static int replace_exec(bContext *C, wmOperator *op)
671 {
672         SpaceImage *sima= (SpaceImage*)CTX_wm_space_data(C);
673         char str[FILE_MAX];
674
675         if(!sima->image)
676                 return OPERATOR_CANCELLED;
677         
678         RNA_string_get(op->ptr, "filename", str);
679         BLI_strncpy(sima->image->name, str, sizeof(sima->image->name)-1); /* we cant do much if the str is longer then 240 :/ */
680
681         BKE_image_signal(sima->image, &sima->iuser, IMA_SIGNAL_RELOAD);
682         WM_event_add_notifier(C, NC_IMAGE|NA_EDITED, sima->image);
683
684         return OPERATOR_FINISHED;
685 }
686
687 static int replace_invoke(bContext *C, wmOperator *op, wmEvent *event)
688 {
689         SpaceImage *sima= (SpaceImage*)CTX_wm_space_data(C);
690         char *path= (sima->image)? sima->image->name: U.textudir;
691
692         if(!sima->image)
693                 return OPERATOR_CANCELLED;
694
695         if(RNA_property_is_set(op->ptr, "filename"))
696                 return replace_exec(C, op);
697
698         image_filesel(C, op, path);
699
700         return OPERATOR_RUNNING_MODAL;
701 }
702
703 void IMAGE_OT_replace(wmOperatorType *ot)
704 {
705         /* identifiers */
706         ot->name= "Replace";
707         ot->idname= "IMAGE_OT_replace";
708         
709         /* api callbacks */
710         ot->exec= replace_exec;
711         ot->invoke= replace_invoke;
712         ot->poll= space_image_poll;
713
714         /* flags */
715         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
716
717         /* properties */
718         RNA_def_string_file_path(ot->srna, "filename", "", FILE_MAX, "Filename", "File path of image to replace current image with.");
719 }
720
721 /******************** save image as operator ********************/
722
723 /* assumes name is FILE_MAX */
724 /* ima->name and ibuf->name should end up the same */
725 static void save_image_doit(bContext *C, SpaceImage *sima, Scene *scene, wmOperator *op, char *name)
726 {
727         Image *ima= ED_space_image(sima);
728         ImBuf *ibuf= ED_space_image_buffer(sima);
729         int len;
730
731         if (ibuf) {     
732                 BLI_convertstringcode(name, G.sce);
733                 BLI_convertstringframe(name, scene->r.cfra);
734                 
735                 if(scene->r.scemode & R_EXTENSION)  {
736                         BKE_add_image_extension(scene, name, sima->imtypenr);
737                         BKE_add_image_extension(scene, name, sima->imtypenr);
738                 }
739                 
740                 /* enforce user setting for RGB or RGBA, but skip BW */
741                 if(scene->r.planes==32)
742                         ibuf->depth= 32;
743                 else if(scene->r.planes==24)
744                         ibuf->depth= 24;
745                 
746                 WM_cursor_wait(1);
747
748                 if(sima->imtypenr==R_MULTILAYER) {
749                         RenderResult *rr= BKE_image_get_renderresult(scene, ima);
750                         if(rr) {
751                                 RE_WriteRenderResult(rr, name, scene->r.quality);
752                                 
753                                 BLI_strncpy(ima->name, name, sizeof(ima->name));
754                                 BLI_strncpy(ibuf->name, name, sizeof(ibuf->name));
755                                 
756                                 /* should be function? nevertheless, saving only happens here */
757                                 for(ibuf= ima->ibufs.first; ibuf; ibuf= ibuf->next)
758                                         ibuf->userflags &= ~IB_BITMAPDIRTY;
759                                 
760                         }
761                         else
762                                 BKE_report(op->reports, RPT_ERROR, "Did not write, no Multilayer Image");
763                 }
764                 else if (BKE_write_ibuf(scene, ibuf, name, sima->imtypenr, scene->r.subimtype, scene->r.quality)) {
765                         BLI_strncpy(ima->name, name, sizeof(ima->name));
766                         BLI_strncpy(ibuf->name, name, sizeof(ibuf->name));
767                         
768                         ibuf->userflags &= ~IB_BITMAPDIRTY;
769                         
770                         /* change type? */
771                         if( ELEM(ima->source, IMA_SRC_GENERATED, IMA_SRC_VIEWER)) {
772                                 ima->source= IMA_SRC_FILE;
773                                 ima->type= IMA_TYPE_IMAGE;
774                         }
775                         if(ima->type==IMA_TYPE_R_RESULT)
776                                 ima->type= IMA_TYPE_IMAGE;
777                         
778                         /* name image as how we saved it */
779                         len= strlen(name);
780                         while (len > 0 && name[len - 1] != '/' && name[len - 1] != '\\') len--;
781                         rename_id(&ima->id, name+len);
782                 } 
783                 else
784                         BKE_reportf(op->reports, RPT_ERROR, "Couldn't write image: %s", name);
785
786                 WM_event_add_notifier(C, NC_IMAGE|NA_EDITED, sima->image);
787
788                 WM_cursor_wait(0);
789         }
790 }
791
792 static int save_as_exec(bContext *C, wmOperator *op)
793 {
794         SpaceImage *sima= (SpaceImage*)CTX_wm_space_data(C);
795         Scene *scene= CTX_data_scene(C);
796         Image *ima = ED_space_image(sima);
797         char str[FILE_MAX];
798
799         if(!ima)
800                 return OPERATOR_CANCELLED;
801
802         RNA_string_get(op->ptr, "filename", str);
803         save_image_doit(C, sima, scene, op, str);
804
805         return OPERATOR_FINISHED;
806 }
807
808 static int save_as_invoke(bContext *C, wmOperator *op, wmEvent *event)
809 {
810         SpaceImage *sima= (SpaceImage*)CTX_wm_space_data(C);
811         Image *ima = ED_space_image(sima);
812         ImBuf *ibuf= ED_space_image_buffer(sima);
813         Scene *scene= CTX_data_scene(C);
814
815         if(RNA_property_is_set(op->ptr, "filename"))
816                 return save_as_exec(C, op);
817         
818         if(!ima)
819                 return OPERATOR_CANCELLED;
820
821         /* always opens fileselect */
822         if(ibuf) {
823                 char *strp;
824                 
825                 strp= filesel_imagetype_string(ima); // XXX unused still
826                 
827                 /* cant save multilayer sequence, ima->rr isn't valid for a specific frame */
828                 if(ima->rr && !(ima->source==IMA_SRC_SEQUENCE && ima->type==IMA_TYPE_MULTILAYER))
829                         sima->imtypenr= R_MULTILAYER;
830                 else if(ima->type==IMA_TYPE_R_RESULT)
831                         sima->imtypenr= scene->r.imtype;
832                 else
833                         sima->imtypenr= BKE_ftype_to_imtype(ibuf->ftype);
834                 
835                 if(ibuf->name[0]==0)
836                         BLI_strncpy(ibuf->name, G.ima, FILE_MAX);
837                 
838                 // XXX note: we can give default menu enums to operator for this 
839                 image_filesel(C, op, ibuf->name);
840
841                 MEM_freeN(strp);
842                 
843                 return OPERATOR_RUNNING_MODAL;
844         }
845
846         return OPERATOR_CANCELLED;
847 }
848
849 void IMAGE_OT_save_as(wmOperatorType *ot)
850 {
851         /* identifiers */
852         ot->name= "Save As";
853         ot->idname= "IMAGE_OT_save_as";
854         
855         /* api callbacks */
856         ot->exec= save_as_exec;
857         ot->invoke= save_as_invoke;
858         ot->poll= space_image_poll;
859
860         /* flags */
861         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
862
863         /* properties */
864         RNA_def_string_file_path(ot->srna, "filename", "", FILE_MAX, "Filename", "File path to save image to.");
865 }
866
867 /******************** save image operator ********************/
868
869 static int save_exec(bContext *C, wmOperator *op)
870 {
871         SpaceImage *sima= (SpaceImage*)CTX_wm_space_data(C);
872         Image *ima = ED_space_image(sima);
873         ImBuf *ibuf= ED_space_image_buffer(sima);
874         Scene *scene= CTX_data_scene(C);
875         char name[FILE_MAX];
876
877         if(!ima || !ibuf)
878                 return OPERATOR_CANCELLED;
879
880         /* if exists, saves over without fileselect */
881         
882         BLI_strncpy(name, ibuf->name, FILE_MAX);
883         if(name[0]==0)
884                 BLI_strncpy(name, G.ima, FILE_MAX);
885         
886         if(BLI_exists(name) && BLI_is_writable(name)) {
887                 if(BKE_image_get_renderresult(scene, ima)) 
888                         sima->imtypenr= R_MULTILAYER;
889                 else 
890                         sima->imtypenr= BKE_ftype_to_imtype(ibuf->ftype);
891                 
892                 save_image_doit(C, sima, scene, op, name);
893         }
894         else {
895                 BKE_report(op->reports, RPT_ERROR, "Can not save image.");
896                 return OPERATOR_CANCELLED;
897         }
898
899         return OPERATOR_FINISHED;
900 }
901
902 void IMAGE_OT_save(wmOperatorType *ot)
903 {
904         /* identifiers */
905         ot->name= "Save";
906         ot->idname= "IMAGE_OT_save";
907         
908         /* api callbacks */
909         ot->exec= save_exec;
910         ot->poll= space_image_file_exists_poll;
911
912         /* flags */
913         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
914 }
915
916 /******************* save sequence operator ********************/
917
918 static int save_sequence_exec(bContext *C, wmOperator *op)
919 {
920         SpaceImage *sima= (SpaceImage*)CTX_wm_space_data(C);
921         ImBuf *ibuf;
922         int tot= 0;
923         char di[FILE_MAX], fi[FILE_MAX];
924         
925         if(sima->image==NULL)
926                 return OPERATOR_CANCELLED;
927
928         if(sima->image->source!=IMA_SRC_SEQUENCE) {
929                 BKE_report(op->reports, RPT_ERROR, "Can only save sequence on image sequences.");
930                 return OPERATOR_CANCELLED;
931         }
932
933         if(sima->image->type==IMA_TYPE_MULTILAYER) {
934                 BKE_report(op->reports, RPT_ERROR, "Can't save multilayer sequences.");
935                 return OPERATOR_CANCELLED;
936         }
937         
938         /* get total */
939         for(ibuf= sima->image->ibufs.first; ibuf; ibuf= ibuf->next) 
940                 if(ibuf->userflags & IB_BITMAPDIRTY)
941                         tot++;
942         
943         if(tot==0) {
944                 BKE_report(op->reports, RPT_WARNING, "No images have been changed.");
945                 return OPERATOR_CANCELLED;
946         }
947
948         /* get a filename for menu */
949         for(ibuf= sima->image->ibufs.first; ibuf; ibuf= ibuf->next) 
950                 if(ibuf->userflags & IB_BITMAPDIRTY)
951                         break;
952         
953         BLI_strncpy(di, ibuf->name, FILE_MAX);
954         BLI_splitdirstring(di, fi);
955         
956         BKE_reportf(op->reports, RPT_INFO, "%d Image(s) will be saved in %s", tot, di);
957
958         for(ibuf= sima->image->ibufs.first; ibuf; ibuf= ibuf->next) {
959                 if(ibuf->userflags & IB_BITMAPDIRTY) {
960                         char name[FILE_MAX];
961                         BLI_strncpy(name, ibuf->name, sizeof(name));
962                         
963                         BLI_convertstringcode(name, G.sce);
964
965                         if(0 == IMB_saveiff(ibuf, name, IB_rect | IB_zbuf | IB_zbuffloat)) {
966                                 BKE_reportf(op->reports, RPT_ERROR, "Could not write image %s.", name);
967                                 break;
968                         }
969
970                         printf("Saved: %s\n", ibuf->name);
971                         ibuf->userflags &= ~IB_BITMAPDIRTY;
972                 }
973         }
974
975         return OPERATOR_FINISHED;
976 }
977
978 void IMAGE_OT_save_sequence(wmOperatorType *ot)
979 {
980         /* identifiers */
981         ot->name= "Save Sequence";
982         ot->idname= "IMAGE_OT_save_sequence";
983         
984         /* api callbacks */
985         ot->exec= save_sequence_exec;
986         ot->poll= space_image_poll;
987
988         /* flags */
989         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
990 }
991
992 /******************** reload image operator ********************/
993
994 static int reload_exec(bContext *C, wmOperator *op)
995 {
996         SpaceImage *sima;
997
998         /* retrieve state */
999         sima= (SpaceImage*)CTX_wm_space_data(C);
1000
1001         if(!sima->image)
1002                 return OPERATOR_CANCELLED;
1003
1004         BKE_image_signal(sima->image, &sima->iuser, IMA_SIGNAL_RELOAD);
1005         /* ED_space_image_set(C, sima, scene, obedit, NULL); - do we really need this? */
1006
1007         // XXX BIF_preview_changed(ID_TE);
1008         WM_event_add_notifier(C, NC_IMAGE|NA_EDITED, sima->image);
1009         ED_area_tag_redraw(CTX_wm_area(C));
1010         
1011         return OPERATOR_FINISHED;
1012 }
1013
1014 void IMAGE_OT_reload(wmOperatorType *ot)
1015 {
1016         /* identifiers */
1017         ot->name= "Reload";
1018         ot->idname= "IMAGE_OT_reload";
1019         
1020         /* api callbacks */
1021         ot->exec= reload_exec;
1022         ot->poll= space_image_poll;
1023
1024         /* flags */
1025         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1026 }
1027
1028 /********************** new image operator *********************/
1029
1030 static int new_exec(bContext *C, wmOperator *op)
1031 {
1032         SpaceImage *sima;
1033         Scene *scene;
1034         Object *obedit;
1035         Image *ima;
1036         char name[22];
1037         float color[4];
1038         int width, height, floatbuf, uvtestgrid;
1039
1040         /* retrieve state */
1041         sima= (SpaceImage*)CTX_wm_space_data(C);
1042         scene= (Scene*)CTX_data_scene(C);
1043         obedit= CTX_data_edit_object(C);
1044
1045         RNA_string_get(op->ptr, "name", name);
1046         width= RNA_int_get(op->ptr, "width");
1047         height= RNA_int_get(op->ptr, "height");
1048         floatbuf= RNA_boolean_get(op->ptr, "float");
1049         uvtestgrid= RNA_boolean_get(op->ptr, "uv_test_grid");
1050         RNA_float_get_array(op->ptr, "color", color);
1051         color[3]= RNA_float_get(op->ptr, "alpha");
1052
1053         ima = BKE_add_image_size(width, height, name, floatbuf, uvtestgrid, color);
1054         BKE_image_signal(sima->image, &sima->iuser, IMA_SIGNAL_USER_NEW_IMAGE);
1055         ED_space_image_set(C, sima, scene, obedit, ima);
1056         
1057         return OPERATOR_FINISHED;
1058 }
1059
1060 void IMAGE_OT_new(wmOperatorType *ot)
1061 {
1062         /* identifiers */
1063         ot->name= "New";
1064         ot->idname= "IMAGE_OT_new";
1065         
1066         /* api callbacks */
1067         ot->exec= new_exec;
1068         ot->invoke= WM_operator_redo;
1069         ot->poll= ED_operator_image_active;
1070
1071         /* flags */
1072         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1073
1074         /* properties */
1075         RNA_def_string(ot->srna, "name", "Untitled", 21, "Name", "Image datablock name.");
1076         RNA_def_int(ot->srna, "width", 1024, 1, INT_MAX, "Width", "Image width.", 1, 16384);
1077         RNA_def_int(ot->srna, "height", 1024, 1, INT_MAX, "Height", "Image height.", 1, 16384);
1078         RNA_def_float_color(ot->srna, "color", 3, NULL, 0.0f, FLT_MAX, "Color", "Default fill color.", 0.0f, 1.0f);
1079         RNA_def_float(ot->srna, "alpha", 1.0f, 0.0f, 1.0f, "Alpha", "Default fill alpha.", 0.0f, 1.0f);
1080         RNA_def_boolean(ot->srna, "uv_test_grid", 0, "UV Test Grid", "Fill the image with a grid for UV map testing.");
1081         RNA_def_boolean(ot->srna, "float", 0, "32 bit Float", "Create image with 32 bit floating point bit depth.");
1082 }
1083
1084 /********************* pack operator *********************/
1085
1086 static int pack_test(bContext *C, wmOperator *op)
1087 {
1088         Image *ima= CTX_data_edit_image(C);
1089         int as_png= RNA_boolean_get(op->ptr, "as_png");
1090
1091         if(!ima)
1092                 return 0;
1093         if(!as_png && ima->packedfile)
1094                 return 0;
1095
1096         if(ima->source==IMA_SRC_SEQUENCE || ima->source==IMA_SRC_MOVIE) {
1097                 BKE_report(op->reports, RPT_ERROR, "Can't pack movie or image sequence.");
1098                 return 0;
1099         }
1100
1101         return 1;
1102 }
1103
1104 static int pack_exec(bContext *C, wmOperator *op)
1105 {
1106         SpaceImage *sima= (SpaceImage*)CTX_wm_space_data(C);
1107         Image *ima= ED_space_image(sima);
1108         ImBuf *ibuf= ED_space_image_buffer(sima);
1109         int as_png= RNA_boolean_get(op->ptr, "as_png");
1110
1111         if(!pack_test(C, op))
1112                 return OPERATOR_CANCELLED;
1113         
1114         if(!as_png && (ibuf && (ibuf->userflags & IB_BITMAPDIRTY))) {
1115                 BKE_report(op->reports, RPT_ERROR, "Can't pack edited image from disk, only as internal PNG.");
1116                 return OPERATOR_CANCELLED;
1117         }
1118
1119         if(as_png)
1120                 BKE_image_memorypack(ima);
1121         else
1122                 ima->packedfile= newPackedFile(ima->name);
1123
1124         return OPERATOR_FINISHED;
1125 }
1126
1127 static int pack_invoke(bContext *C, wmOperator *op, wmEvent *event)
1128 {
1129         SpaceImage *sima= (SpaceImage*)CTX_wm_space_data(C);
1130         ImBuf *ibuf= ED_space_image_buffer(sima);
1131         uiPopupMenu *pup;
1132         uiLayout *layout;
1133         int as_png= RNA_boolean_get(op->ptr, "as_png");
1134
1135         if(!pack_test(C, op))
1136                 return OPERATOR_CANCELLED;
1137         
1138         if(!as_png && (ibuf && (ibuf->userflags & IB_BITMAPDIRTY))) {
1139                 pup= uiPupMenuBegin(C, "OK", ICON_QUESTION);
1140                 layout= uiPupMenuLayout(pup);
1141                 uiItemBooleanO(layout, "Can't pack edited image from disk. Pack as internal PNG?", 0, op->idname, "as_png", 1);
1142                 uiPupMenuEnd(C, pup);
1143
1144                 return OPERATOR_CANCELLED;
1145         }
1146
1147         return pack_exec(C, op);
1148 }
1149
1150 void IMAGE_OT_pack(wmOperatorType *ot)
1151 {
1152         /* identifiers */
1153         ot->name= "Pack";
1154         ot->idname= "IMAGE_OT_pack";
1155         
1156         /* api callbacks */
1157         ot->exec= pack_exec;
1158         ot->invoke= pack_invoke;
1159         ot->poll= space_image_poll;
1160
1161         /* flags */
1162         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1163
1164         /* properties */
1165         RNA_def_boolean(ot->srna, "as_png", 0, "Pack As PNG", "Pack image as lossless PNG.");
1166 }
1167
1168 /********************* unpack operator *********************/
1169
1170 static int unpack_exec(bContext *C, wmOperator *op)
1171 {
1172         Image *ima= CTX_data_edit_image(C);
1173
1174         if(!ima)
1175                 return OPERATOR_CANCELLED;
1176         if(!ima->packedfile)
1177                 return OPERATOR_CANCELLED;
1178
1179         if(ima->source==IMA_SRC_SEQUENCE || ima->source==IMA_SRC_MOVIE) {
1180                 BKE_report(op->reports, RPT_ERROR, "Can't unpack movie or image sequence.");
1181                 return OPERATOR_CANCELLED;
1182         }
1183
1184         if(G.fileflags & G_AUTOPACK)
1185                 BKE_report(op->reports, RPT_WARNING, "AutoPack is enabled, so image will be packed again on file save.");
1186         
1187         unpackImage(ima, PF_ASK);
1188
1189         return OPERATOR_FINISHED;
1190 }
1191
1192 void IMAGE_OT_unpack(wmOperatorType *ot)
1193 {
1194         /* identifiers */
1195         ot->name= "Unpack";
1196         ot->idname= "IMAGE_OT_unpack";
1197         
1198         /* api callbacks */
1199         ot->exec= unpack_exec;
1200         ot->poll= space_image_poll;
1201
1202         /* flags */
1203         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1204 }
1205
1206 /******************** sample image operator ********************/
1207
1208 typedef struct ImageSampleInfo {
1209         ARegionType *art;
1210         void *draw_handle;
1211         int x, y;
1212
1213         char col[4];
1214         float colf[4];
1215         int z;
1216         float zf;
1217
1218         char *colp;
1219         float *colfp;
1220         int *zp;
1221         float *zfp;
1222
1223         int draw;
1224 } ImageSampleInfo;
1225
1226 static void sample_draw(const bContext *C, ARegion *ar, void *arg_info)
1227 {
1228         SpaceImage *sima= (SpaceImage*)CTX_wm_space_data(C);
1229         ImBuf *ibuf= ED_space_image_buffer(sima);
1230         ImageSampleInfo *info= arg_info;
1231
1232         if(ibuf == NULL)
1233                 return;
1234         
1235         draw_image_info(ar, ibuf->channels, info->x, info->y, info->colp,
1236                 info->colfp, info->zp, info->zfp);
1237 }
1238
1239 static void sample_apply(bContext *C, wmOperator *op, wmEvent *event)
1240 {
1241         SpaceImage *sima= (SpaceImage*)CTX_wm_space_data(C);
1242         ARegion *ar= CTX_wm_region(C);
1243         ImBuf *ibuf= ED_space_image_buffer(sima);
1244         ImageSampleInfo *info= op->customdata;
1245         float fx, fy;
1246         int x, y;
1247         
1248         if(ibuf == NULL)
1249                 return;
1250
1251         x= event->x - ar->winrct.xmin;
1252         y= event->y - ar->winrct.ymin;
1253         UI_view2d_region_to_view(&ar->v2d, x, y, &fx, &fy);
1254
1255         if(fx>=0.0 && fy>=0.0 && fx<1.0 && fy<1.0) {
1256                 float *fp;
1257                 char *cp;
1258                 int x= (int)(fx*ibuf->x), y= (int)(fy*ibuf->y);
1259
1260                 CLAMP(x, 0, ibuf->x-1);
1261                 CLAMP(y, 0, ibuf->y-1);
1262
1263                 info->x= x;
1264                 info->y= y;
1265                 info->draw= 1;
1266
1267                 info->colp= NULL;
1268                 info->colfp= NULL;
1269                 info->zp= NULL;
1270                 info->zfp= NULL;
1271                 
1272                 if(ibuf->rect) {
1273                         cp= (char *)(ibuf->rect + y*ibuf->x + x);
1274
1275                         info->col[0]= cp[0];
1276                         info->col[1]= cp[1];
1277                         info->col[2]= cp[2];
1278                         info->col[3]= cp[3];
1279                         info->colp= info->col;
1280
1281                         info->colf[0]= (float)cp[0]/255.0f;
1282                         info->colf[1]= (float)cp[1]/255.0f;
1283                         info->colf[2]= (float)cp[2]/255.0f;
1284                         info->colf[3]= (float)cp[3]/255.0f;
1285                         info->colfp= info->colf;
1286                 }
1287                 if(ibuf->rect_float) {
1288                         fp= (ibuf->rect_float + (ibuf->channels)*(y*ibuf->x + x));
1289
1290                         info->colf[0]= fp[0];
1291                         info->colf[1]= fp[1];
1292                         info->colf[2]= fp[2];
1293                         info->colf[3]= fp[4];
1294                         info->colfp= info->colf;
1295                 }
1296
1297                 if(ibuf->zbuf) {
1298                         info->z= ibuf->zbuf[y*ibuf->x + x];
1299                         info->zp= &info->z;
1300                 }
1301                 if(ibuf->zbuf_float) {
1302                         info->zf= ibuf->zbuf_float[y*ibuf->x + x];
1303                         info->zfp= &info->zf;
1304                 }
1305                 
1306                 if(sima->cumap && ibuf->channels==4) {
1307                         /* we reuse this callback for set curves point operators */
1308                         if(RNA_struct_find_property(op->ptr, "point")) {
1309                                 int point= RNA_enum_get(op->ptr, "point");
1310
1311                                 if(point == 1) {
1312                                         curvemapping_set_black_white(sima->cumap, NULL, info->colfp);
1313                                         curvemapping_do_ibuf(sima->cumap, ibuf);
1314                                 }
1315                                 else if(point == 0) {
1316                                         curvemapping_set_black_white(sima->cumap, info->colfp, NULL);
1317                                         curvemapping_do_ibuf(sima->cumap, ibuf);
1318                                 }
1319                         }
1320                 }
1321                                 
1322                 // XXX node curve integration ..
1323 #if 0
1324                 {
1325                         ScrArea *sa, *cur= curarea;
1326                         
1327                         node_curvemap_sample(fp);       /* sends global to node editor */
1328                         for(sa= G.curscreen->areabase.first; sa; sa= sa->next) {
1329                                 if(sa->spacetype==SPACE_NODE) {
1330                                         areawinset(sa->win);
1331                                         scrarea_do_windraw(sa);
1332                                 }
1333                         }
1334                         node_curvemap_sample(NULL);             /* clears global in node editor */
1335                         curarea= cur;
1336                 }
1337 #endif
1338         }
1339         else
1340                 info->draw= 0;
1341
1342         ED_area_tag_redraw(CTX_wm_area(C));
1343 }
1344
1345 static void sample_exit(bContext *C, wmOperator *op)
1346 {
1347         ImageSampleInfo *info= op->customdata;
1348
1349         ED_region_draw_cb_exit(info->art, info->draw_handle);
1350         ED_area_tag_redraw(CTX_wm_area(C));
1351         MEM_freeN(info);
1352 }
1353
1354 static int sample_invoke(bContext *C, wmOperator *op, wmEvent *event)
1355 {
1356         SpaceImage *sima= (SpaceImage*)CTX_wm_space_data(C);
1357         ARegion *ar= CTX_wm_region(C);
1358         ImBuf *ibuf= ED_space_image_buffer(sima);
1359         ImageSampleInfo *info;
1360
1361         if(ibuf == NULL)
1362                 return OPERATOR_CANCELLED;
1363         
1364         info= MEM_callocN(sizeof(ImageSampleInfo), "ImageSampleInfo");
1365         info->art= ar->type;
1366         info->draw_handle = ED_region_draw_cb_activate(ar->type, sample_draw, info, REGION_DRAW_POST);
1367         op->customdata= info;
1368
1369         sample_apply(C, op, event);
1370
1371         WM_event_add_modal_handler(C, &CTX_wm_window(C)->handlers, op);
1372
1373         return OPERATOR_RUNNING_MODAL;
1374 }
1375
1376 static int sample_modal(bContext *C, wmOperator *op, wmEvent *event)
1377 {
1378         switch(event->type) {
1379                 case LEFTMOUSE:
1380                 case RIGHTMOUSE: // XXX hardcoded
1381                         sample_exit(C, op);
1382                         return OPERATOR_CANCELLED;
1383                 case MOUSEMOVE:
1384                         sample_apply(C, op, event);
1385                         break;
1386         }
1387
1388         return OPERATOR_RUNNING_MODAL;
1389 }
1390
1391 static int sample_cancel(bContext *C, wmOperator *op)
1392 {
1393         sample_exit(C, op);
1394         return OPERATOR_CANCELLED;
1395 }
1396
1397 void IMAGE_OT_sample(wmOperatorType *ot)
1398 {
1399         /* identifiers */
1400         ot->name= "Sample";
1401         ot->idname= "IMAGE_OT_sample";
1402         
1403         /* api callbacks */
1404         ot->invoke= sample_invoke;
1405         ot->modal= sample_modal;
1406         ot->cancel= sample_cancel;
1407         ot->poll= space_image_main_area_poll;
1408 }
1409
1410 /******************** set curve point operator ********************/
1411
1412 void IMAGE_OT_curves_point_set(wmOperatorType *ot)
1413 {
1414         static EnumPropertyItem point_items[]= {
1415                 {0, "BLACK_POINT", 0, "Black Point", ""},
1416                 {1, "WHITE_POINT", 0, "White Point", ""},
1417                 {0, NULL, 0, NULL, NULL}};
1418
1419         /* identifiers */
1420         ot->name= "Set Curves Point";
1421         ot->idname= "IMAGE_OT_curves_point_set";
1422
1423         /* flags */
1424         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1425         
1426         /* api callbacks */
1427         ot->invoke= sample_invoke;
1428         ot->modal= sample_modal;
1429         ot->cancel= sample_cancel;
1430         ot->poll= space_image_main_area_poll;
1431
1432         /* properties */
1433         RNA_def_enum(ot->srna, "point", point_items, 0, "Point", "Set black point or white point for curves.");
1434 }
1435
1436 /******************** record composite operator *********************/
1437
1438 typedef struct RecordCompositeData {
1439         wmTimer *timer;
1440         int old_cfra;
1441         int sfra, efra;
1442 } RecordCompositeData;
1443
1444 int record_composite_apply(bContext *C, wmOperator *op)
1445 {
1446         SpaceImage *sima= (SpaceImage*)CTX_wm_space_data(C);
1447         RecordCompositeData *rcd= op->customdata;
1448         Scene *scene= CTX_data_scene(C);
1449         ImBuf *ibuf;
1450         
1451         WM_timecursor(CTX_wm_window(C), scene->r.cfra);
1452
1453         // XXX scene->nodetree->test_break= blender_test_break;
1454         // XXX scene->nodetree->test_break= NULL;
1455         
1456         BKE_image_all_free_anim_ibufs(scene->r.cfra);
1457         ntreeCompositTagAnimated(scene->nodetree);
1458         ntreeCompositExecTree(scene->nodetree, &scene->r, scene->r.cfra != rcd->old_cfra);      /* 1 is no previews */
1459
1460         ED_area_tag_redraw(CTX_wm_area(C));
1461         
1462         ibuf= BKE_image_get_ibuf(sima->image, &sima->iuser);
1463         /* save memory in flipbooks */
1464         if(ibuf)
1465                 imb_freerectfloatImBuf(ibuf);
1466         
1467         scene->r.cfra++;
1468
1469         return (scene->r.cfra <= rcd->efra);
1470 }
1471
1472 static int record_composite_init(bContext *C, wmOperator *op)
1473 {
1474         SpaceImage *sima= (SpaceImage*)CTX_wm_space_data(C);
1475         Scene *scene= CTX_data_scene(C);
1476         RecordCompositeData *rcd;
1477
1478         if(sima->iuser.frames < 2)
1479                 return 0;
1480         if(scene->nodetree == NULL)
1481                 return 0;
1482         
1483         op->customdata= rcd= MEM_callocN(sizeof(RecordCompositeData), "ImageRecordCompositeData");
1484
1485         rcd->old_cfra= scene->r.cfra;
1486         rcd->sfra= sima->iuser.sfra;
1487         rcd->efra= sima->iuser.sfra + sima->iuser.frames-1;
1488         scene->r.cfra= rcd->sfra;
1489
1490         return 1;
1491 }
1492
1493 static void record_composite_exit(bContext *C, wmOperator *op)
1494 {
1495         Scene *scene= CTX_data_scene(C);
1496         SpaceImage *sima= (SpaceImage*)CTX_wm_space_data(C);
1497         RecordCompositeData *rcd= op->customdata;
1498
1499         scene->r.cfra= rcd->old_cfra;
1500
1501         WM_cursor_restore(CTX_wm_window(C));
1502
1503         if(rcd->timer)
1504                 WM_event_remove_window_timer(CTX_wm_window(C), rcd->timer);
1505
1506         WM_event_add_notifier(C, NC_IMAGE|NA_EDITED, sima->image);
1507
1508         // XXX play_anim(0);
1509         // XXX allqueue(REDRAWNODE, 1);
1510
1511         MEM_freeN(rcd);
1512 }
1513
1514 static int record_composite_exec(bContext *C, wmOperator *op)
1515 {
1516         if(!record_composite_init(C, op))
1517                 return OPERATOR_CANCELLED;
1518         
1519         while(record_composite_apply(C, op))
1520                 ;
1521         
1522         record_composite_exit(C, op);
1523         
1524         return OPERATOR_FINISHED;
1525 }
1526
1527 static int record_composite_invoke(bContext *C, wmOperator *op, wmEvent *event)
1528 {
1529         RecordCompositeData *rcd= op->customdata;
1530         
1531         if(!record_composite_init(C, op))
1532                 return OPERATOR_CANCELLED;
1533
1534         rcd= op->customdata;
1535         rcd->timer= WM_event_add_window_timer(CTX_wm_window(C), TIMER, 0.0f);
1536         WM_event_add_modal_handler(C, &CTX_wm_window(C)->handlers, op);
1537
1538         if(!record_composite_apply(C, op))
1539                 return OPERATOR_FINISHED;
1540
1541         return OPERATOR_RUNNING_MODAL;
1542 }
1543
1544 static int record_composite_modal(bContext *C, wmOperator *op, wmEvent *event)
1545 {
1546         RecordCompositeData *rcd= op->customdata;
1547
1548         switch(event->type) {
1549                 case TIMER:
1550                         if(rcd->timer == event->customdata) {
1551                                 if(!record_composite_apply(C, op)) {
1552                                         record_composite_exit(C, op);
1553                                         return OPERATOR_FINISHED;
1554                                 }
1555                         }
1556                         break;
1557                 case ESCKEY:
1558                         record_composite_exit(C, op);
1559                         return OPERATOR_FINISHED;
1560         }
1561
1562         return OPERATOR_RUNNING_MODAL;
1563 }
1564
1565 static int record_composite_cancel(bContext *C, wmOperator *op)
1566 {
1567         record_composite_exit(C, op);
1568         return OPERATOR_CANCELLED;
1569 }
1570
1571 void IMAGE_OT_record_composite(wmOperatorType *ot)
1572 {
1573         /* identifiers */
1574         ot->name= "Record Composite";
1575         ot->idname= "IMAGE_OT_record_composite";
1576         
1577         /* api callbacks */
1578         ot->exec= record_composite_exec;
1579         ot->invoke= record_composite_invoke;
1580         ot->modal= record_composite_modal;
1581         ot->cancel= record_composite_cancel;
1582         ot->poll= space_image_poll;
1583 }
1584
1585 /******************** TODO ********************/
1586
1587 /* XXX notifier? */
1588 #if 0
1589 /* goes over all ImageUsers, and sets frame numbers if auto-refresh is set */
1590 void BIF_image_update_frame(void)
1591 {
1592         Tex *tex;
1593         
1594         /* texture users */
1595         for(tex= G.main->tex.first; tex; tex= tex->id.next) {
1596                 if(tex->type==TEX_IMAGE && tex->ima)
1597                         if(ELEM(tex->ima->source, IMA_SRC_MOVIE, IMA_SRC_SEQUENCE))
1598                                 if(tex->iuser.flag & IMA_ANIM_ALWAYS)
1599                                         BKE_image_user_calc_imanr(&tex->iuser, scene->r.cfra, 0);
1600                 
1601         }
1602         /* image window, compo node users */
1603         if(G.curscreen) {
1604                 ScrArea *sa;
1605                 for(sa= G.curscreen->areabase.first; sa; sa= sa->next) {
1606                         if(sa->spacetype==SPACE_VIEW3D) {
1607                                 View3D *v3d= sa->spacedata.first;
1608                                 if(v3d->bgpic)
1609                                         if(v3d->bgpic->iuser.flag & IMA_ANIM_ALWAYS)
1610                                                 BKE_image_user_calc_imanr(&v3d->bgpic->iuser, scene->r.cfra, 0);
1611                         }
1612                         else if(sa->spacetype==SPACE_IMAGE) {
1613                                 SpaceImage *sima= sa->spacedata.first;
1614                                 if(sima->iuser.flag & IMA_ANIM_ALWAYS)
1615                                         BKE_image_user_calc_imanr(&sima->iuser, scene->r.cfra, 0);
1616                         }
1617                         else if(sa->spacetype==SPACE_NODE) {
1618                                 SpaceNode *snode= sa->spacedata.first;
1619                                 if((snode->treetype==NTREE_COMPOSIT) && (snode->nodetree)) {
1620                                         bNode *node;
1621                                         for(node= snode->nodetree->nodes.first; node; node= node->next) {
1622                                                 if(node->id && node->type==CMP_NODE_IMAGE) {
1623                                                         Image *ima= (Image *)node->id;
1624                                                         ImageUser *iuser= node->storage;
1625                                                         if(ELEM(ima->source, IMA_SRC_MOVIE, IMA_SRC_SEQUENCE))
1626                                                                 if(iuser->flag & IMA_ANIM_ALWAYS)
1627                                                                         BKE_image_user_calc_imanr(iuser, scene->r.cfra, 0);
1628                                                 }
1629                                         }
1630                                 }
1631                         }
1632                 }
1633         }
1634 }
1635 #endif
1636