fix for own error with image info display, also dont draw if the mouse is outside...
[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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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, Xavier Thomas
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 #include <stddef.h>
29 #include <string.h>
30 #include <stdlib.h>
31 #include <errno.h>
32
33 #include "MEM_guardedalloc.h"
34
35 #include "BLI_math.h"
36 #include "BLI_blenlib.h"
37 #include "BLI_utildefines.h"
38
39 #include "DNA_object_types.h"
40 #include "DNA_node_types.h"
41 #include "DNA_packedFile_types.h"
42 #include "DNA_scene_types.h"
43
44 #include "BKE_colortools.h"
45 #include "BKE_context.h"
46 #include "BKE_image.h"
47 #include "BKE_global.h"
48 #include "BKE_library.h"
49 #include "BKE_main.h"
50 #include "BKE_node.h"
51 #include "BKE_packedFile.h"
52 #include "BKE_report.h"
53 #include "BKE_screen.h"
54
55 #include "IMB_imbuf.h"
56 #include "IMB_imbuf_types.h"
57
58 #include "RE_pipeline.h"
59
60 #include "RNA_access.h"
61 #include "RNA_define.h"
62 #include "RNA_enum_types.h"
63
64 #include "ED_image.h"
65 #include "ED_screen.h"
66 #include "ED_space_api.h"
67 #include "ED_uvedit.h"
68 #include "ED_util.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 #if 0 // currently unused
111 static int image_poll(bContext *C)
112 {
113         return (CTX_data_edit_image(C) != NULL);
114 }
115 #endif
116
117 static int space_image_buffer_exists_poll(bContext *C)
118 {
119         SpaceImage *sima= CTX_wm_space_image(C);
120         if(sima && sima->spacetype==SPACE_IMAGE)
121                 if(ED_space_image_has_buffer(sima))
122                         return 1;
123         return 0;
124 }
125
126 static int space_image_file_exists_poll(bContext *C)
127 {
128         if(space_image_buffer_exists_poll(C)) {
129                 Main *bmain= CTX_data_main(C);
130                 SpaceImage *sima= CTX_wm_space_image(C);
131                 ImBuf *ibuf;
132                 void *lock;
133                 int poll= 0;
134                 char name[FILE_MAX];
135
136                 ibuf= ED_space_image_acquire_buffer(sima, &lock);
137                 if(ibuf) {
138                         BLI_strncpy(name, ibuf->name, FILE_MAX);
139                         BLI_path_abs(name, bmain->name);
140                         poll= (BLI_exists(name) && BLI_is_writable(name));
141                 }
142                 ED_space_image_release_buffer(sima, lock);
143
144                 return poll;
145         }
146         return 0;
147 }
148
149 static int space_image_poll(bContext *C)
150 {
151         SpaceImage *sima= CTX_wm_space_image(C);
152         if(sima && sima->spacetype==SPACE_IMAGE && sima->image)
153                 return 1;
154         return 0;
155 }
156
157 int space_image_main_area_poll(bContext *C)
158 {
159         SpaceImage *sima= CTX_wm_space_image(C);
160         // XXX ARegion *ar= CTX_wm_region(C);
161
162         if(sima)
163                 return 1; // XXX (ar && ar->type->regionid == RGN_TYPE_WINDOW);
164         
165         return 0;
166 }
167
168 /********************** view pan operator *********************/
169
170 typedef struct ViewPanData {
171         float x, y;
172         float xof, yof;
173 } ViewPanData;
174
175 static void view_pan_init(bContext *C, wmOperator *op, wmEvent *event)
176 {
177         SpaceImage *sima= CTX_wm_space_image(C);
178         ViewPanData *vpd;
179
180         op->customdata= vpd= MEM_callocN(sizeof(ViewPanData), "ImageViewPanData");
181         WM_cursor_modal(CTX_wm_window(C), BC_NSEW_SCROLLCURSOR);
182
183         vpd->x= event->x;
184         vpd->y= event->y;
185         vpd->xof= sima->xof;
186         vpd->yof= sima->yof;
187
188         WM_event_add_modal_handler(C, op);
189 }
190
191 static void view_pan_exit(bContext *C, wmOperator *op, int cancel)
192 {
193         SpaceImage *sima= CTX_wm_space_image(C);
194         ViewPanData *vpd= op->customdata;
195
196         if(cancel) {
197                 sima->xof= vpd->xof;
198                 sima->yof= vpd->yof;
199                 ED_region_tag_redraw(CTX_wm_region(C));
200         }
201
202         WM_cursor_restore(CTX_wm_window(C));
203         MEM_freeN(op->customdata);
204 }
205
206 static int view_pan_exec(bContext *C, wmOperator *op)
207 {
208         SpaceImage *sima= CTX_wm_space_image(C);
209         float offset[2];
210
211         RNA_float_get_array(op->ptr, "offset", offset);
212         sima->xof += offset[0];
213         sima->yof += offset[1];
214
215         ED_region_tag_redraw(CTX_wm_region(C));
216
217         /* XXX notifier? */
218 #if 0
219         if(image_preview_active(curarea, NULL, NULL)) {
220                 /* recalculates new preview rect */
221                 scrarea_do_windraw(curarea);
222                 image_preview_event(2);
223         }
224 #endif
225         
226         return OPERATOR_FINISHED;
227 }
228
229 static int view_pan_invoke(bContext *C, wmOperator *op, wmEvent *event)
230 {
231         if (event->type == MOUSEPAN) {
232                 SpaceImage *sima= CTX_wm_space_image(C);
233                 float offset[2];
234                 
235                 offset[0]= (event->x - event->prevx)/sima->zoom;
236                 offset[1]= (event->y - event->prevy)/sima->zoom;
237                 RNA_float_set_array(op->ptr, "offset", offset);
238
239                 view_pan_exec(C, op);
240                 return OPERATOR_FINISHED;
241         }
242         else {
243                 view_pan_init(C, op, event);
244                 return OPERATOR_RUNNING_MODAL;
245         }
246 }
247
248 static int view_pan_modal(bContext *C, wmOperator *op, wmEvent *event)
249 {
250         SpaceImage *sima= CTX_wm_space_image(C);
251         ViewPanData *vpd= op->customdata;
252         float offset[2];
253
254         switch(event->type) {
255                 case MOUSEMOVE:
256                         sima->xof= vpd->xof;
257                         sima->yof= vpd->yof;
258                         offset[0]= (vpd->x - event->x)/sima->zoom;
259                         offset[1]= (vpd->y - event->y)/sima->zoom;
260                         RNA_float_set_array(op->ptr, "offset", offset);
261                         view_pan_exec(C, op);
262                         break;
263                 case MIDDLEMOUSE:
264                 case LEFTMOUSE:
265                         if(event->val==KM_RELEASE) {
266                                 view_pan_exit(C, op, 0);
267                                 return OPERATOR_FINISHED;
268                         }
269                         break;
270         }
271
272         return OPERATOR_RUNNING_MODAL;
273 }
274
275 static int view_pan_cancel(bContext *C, wmOperator *op)
276 {
277         view_pan_exit(C, op, 1);
278         return OPERATOR_CANCELLED;
279 }
280
281 void IMAGE_OT_view_pan(wmOperatorType *ot)
282 {
283         /* identifiers */
284         ot->name= "View Pan";
285         ot->idname= "IMAGE_OT_view_pan";
286         
287         /* api callbacks */
288         ot->exec= view_pan_exec;
289         ot->invoke= view_pan_invoke;
290         ot->modal= view_pan_modal;
291         ot->cancel= view_pan_cancel;
292         ot->poll= space_image_main_area_poll;
293
294         /* flags */
295         ot->flag= OPTYPE_BLOCKING;
296         
297         /* properties */
298         RNA_def_float_vector(ot->srna, "offset", 2, NULL, -FLT_MAX, FLT_MAX,
299                 "Offset", "Offset in floating point units, 1.0 is the width and height of the image.", -FLT_MAX, FLT_MAX);
300 }
301
302 /********************** view zoom operator *********************/
303
304 typedef struct ViewZoomData {
305         float x, y;
306         float zoom;
307 } ViewZoomData;
308
309 static void view_zoom_init(bContext *C, wmOperator *op, wmEvent *event)
310 {
311         SpaceImage *sima= CTX_wm_space_image(C);
312         ViewZoomData *vpd;
313
314         op->customdata= vpd= MEM_callocN(sizeof(ViewZoomData), "ImageViewZoomData");
315         WM_cursor_modal(CTX_wm_window(C), BC_NSEW_SCROLLCURSOR);
316
317         vpd->x= event->x;
318         vpd->y= event->y;
319         vpd->zoom= sima->zoom;
320
321         WM_event_add_modal_handler(C, op);
322 }
323
324 static void view_zoom_exit(bContext *C, wmOperator *op, int cancel)
325 {
326         SpaceImage *sima= CTX_wm_space_image(C);
327         ViewZoomData *vpd= op->customdata;
328
329         if(cancel) {
330                 sima->zoom= vpd->zoom;
331                 ED_region_tag_redraw(CTX_wm_region(C));
332         }
333
334         WM_cursor_restore(CTX_wm_window(C));
335         MEM_freeN(op->customdata);
336 }
337
338 static int view_zoom_exec(bContext *C, wmOperator *op)
339 {
340         SpaceImage *sima= CTX_wm_space_image(C);
341         ARegion *ar= CTX_wm_region(C);
342
343         sima_zoom_set_factor(sima, ar, RNA_float_get(op->ptr, "factor"));
344
345         ED_region_tag_redraw(CTX_wm_region(C));
346
347         /* XXX notifier? */
348 #if 0
349         if(image_preview_active(curarea, NULL, NULL)) {
350                 /* recalculates new preview rect */
351                 scrarea_do_windraw(curarea);
352                 image_preview_event(2);
353         }
354 #endif
355         
356         return OPERATOR_FINISHED;
357 }
358
359 static int view_zoom_invoke(bContext *C, wmOperator *op, wmEvent *event)
360 {
361         if (event->type == MOUSEZOOM) {
362                 SpaceImage *sima= CTX_wm_space_image(C);
363                 ARegion *ar= CTX_wm_region(C);
364                 float factor;
365                 
366                 factor= 1.0 + (event->x-event->prevx+event->y-event->prevy)/300.0f;
367                 RNA_float_set(op->ptr, "factor", factor);
368                 sima_zoom_set(sima, ar, sima->zoom*factor);
369                 ED_region_tag_redraw(CTX_wm_region(C));
370                 
371                 return OPERATOR_FINISHED;
372         }
373         else {
374                 view_zoom_init(C, op, event);
375                 return OPERATOR_RUNNING_MODAL;
376         }
377 }
378
379 static int view_zoom_modal(bContext *C, wmOperator *op, wmEvent *event)
380 {
381         SpaceImage *sima= CTX_wm_space_image(C);
382         ARegion *ar= CTX_wm_region(C);
383         ViewZoomData *vpd= op->customdata;
384         float factor;
385
386         switch(event->type) {
387                 case MOUSEMOVE:
388                         factor= 1.0 + (vpd->x-event->x+vpd->y-event->y)/300.0f;
389                         RNA_float_set(op->ptr, "factor", factor);
390                         sima_zoom_set(sima, ar, vpd->zoom*factor);
391                         ED_region_tag_redraw(CTX_wm_region(C));
392                         break;
393                 case MIDDLEMOUSE:
394                 case LEFTMOUSE:
395                         if(event->val==KM_RELEASE) {
396                                 view_zoom_exit(C, op, 0);
397                                 return OPERATOR_FINISHED;
398                         }
399                         break;
400         }
401
402         return OPERATOR_RUNNING_MODAL;
403 }
404
405 static int view_zoom_cancel(bContext *C, wmOperator *op)
406 {
407         view_zoom_exit(C, op, 1);
408         return OPERATOR_CANCELLED;
409 }
410
411 void IMAGE_OT_view_zoom(wmOperatorType *ot)
412 {
413         /* identifiers */
414         ot->name= "View Zoom";
415         ot->idname= "IMAGE_OT_view_zoom";
416         
417         /* api callbacks */
418         ot->exec= view_zoom_exec;
419         ot->invoke= view_zoom_invoke;
420         ot->modal= view_zoom_modal;
421         ot->cancel= view_zoom_cancel;
422         ot->poll= space_image_main_area_poll;
423
424         /* flags */
425         ot->flag= OPTYPE_BLOCKING;
426         
427         /* properties */
428         RNA_def_float(ot->srna, "factor", 0.0f, 0.0f, FLT_MAX,
429                 "Factor", "Zoom factor, values higher than 1.0 zoom in, lower values zoom out.", -FLT_MAX, FLT_MAX);
430 }
431
432 /********************** view all operator *********************/
433
434 /* Updates the fields of the View2D member of the SpaceImage struct.
435  * Default behavior is to reset the position of the image and set the zoom to 1
436  * If the image will not fit within the window rectangle, the zoom is adjusted */
437
438 static int view_all_exec(bContext *C, wmOperator *UNUSED(op))
439 {
440         SpaceImage *sima;
441         ARegion *ar;
442         float aspx, aspy, zoomx, zoomy, w, h;
443         int width, height;
444
445         /* retrieve state */
446         sima= CTX_wm_space_image(C);
447         ar= CTX_wm_region(C);
448
449         ED_space_image_size(sima, &width, &height);
450         ED_space_image_aspect(sima, &aspx, &aspy);
451
452         w= width*aspx;
453         h= height*aspy;
454         
455         /* check if the image will fit in the image with zoom==1 */
456         width = ar->winrct.xmax - ar->winrct.xmin + 1;
457         height = ar->winrct.ymax - ar->winrct.ymin + 1;
458
459         if((w >= width || h >= height) && (width > 0 && height > 0)) {
460                 /* find the zoom value that will fit the image in the image space */
461                 zoomx= width/w;
462                 zoomy= height/h;
463                 sima_zoom_set(sima, ar, 1.0f/power_of_2(1/MIN2(zoomx, zoomy)));
464         }
465         else
466                 sima_zoom_set(sima, ar, 1.0f);
467
468         sima->xof= sima->yof= 0.0f;
469
470         ED_region_tag_redraw(CTX_wm_region(C));
471         
472         return OPERATOR_FINISHED;
473 }
474
475 void IMAGE_OT_view_all(wmOperatorType *ot)
476 {
477         /* identifiers */
478         ot->name= "View All";
479         ot->idname= "IMAGE_OT_view_all";
480         
481         /* api callbacks */
482         ot->exec= view_all_exec;
483         ot->poll= space_image_main_area_poll;
484 }
485
486 /********************** view selected operator *********************/
487
488 static int view_selected_exec(bContext *C, wmOperator *UNUSED(op))
489 {
490         SpaceImage *sima;
491         ARegion *ar;
492         Scene *scene;
493         Object *obedit;
494         Image *ima;
495         float size, min[2], max[2], d[2];
496         int width, height;
497
498         /* retrieve state */
499         sima= CTX_wm_space_image(C);
500         ar= CTX_wm_region(C);
501         scene= (Scene*)CTX_data_scene(C);
502         obedit= CTX_data_edit_object(C);
503
504         ima= ED_space_image(sima);
505         ED_space_image_size(sima, &width, &height);
506
507         /* get bounds */
508         if(!ED_uvedit_minmax(scene, ima, obedit, min, max))
509                 return OPERATOR_CANCELLED;
510
511         /* adjust offset and zoom */
512         sima->xof= (int)(((min[0] + max[0])*0.5f - 0.5f)*width);
513         sima->yof= (int)(((min[1] + max[1])*0.5f - 0.5f)*height);
514
515         d[0]= max[0] - min[0];
516         d[1]= max[1] - min[1];
517         size= 0.5*MAX2(d[0], d[1])*MAX2(width, height)/256.0f;
518         
519         if(size<=0.01) size= 0.01;
520         sima_zoom_set(sima, ar, 0.7/size);
521
522         ED_region_tag_redraw(CTX_wm_region(C));
523         
524         return OPERATOR_FINISHED;
525 }
526
527 static int view_selected_poll(bContext *C)
528 {
529         return (space_image_main_area_poll(C) && ED_operator_uvedit(C));
530 }
531
532 void IMAGE_OT_view_selected(wmOperatorType *ot)
533 {
534         /* identifiers */
535         ot->name= "View Center";
536         ot->idname= "IMAGE_OT_view_selected";
537         
538         /* api callbacks */
539         ot->exec= view_selected_exec;
540         ot->poll= view_selected_poll;
541 }
542
543 /********************** view zoom in/out operator *********************/
544
545 static int view_zoom_in_exec(bContext *C, wmOperator *UNUSED(op))
546 {
547         SpaceImage *sima= CTX_wm_space_image(C);
548         ARegion *ar= CTX_wm_region(C);
549
550         sima_zoom_set_factor(sima, ar, 1.25f);
551
552         ED_region_tag_redraw(CTX_wm_region(C));
553         
554         return OPERATOR_FINISHED;
555 }
556
557 void IMAGE_OT_view_zoom_in(wmOperatorType *ot)
558 {
559         /* identifiers */
560         ot->name= "View Zoom In";
561         ot->idname= "IMAGE_OT_view_zoom_in";
562         
563         /* api callbacks */
564         ot->exec= view_zoom_in_exec;
565         ot->poll= space_image_main_area_poll;
566 }
567
568 static int view_zoom_out_exec(bContext *C, wmOperator *UNUSED(op))
569 {
570         SpaceImage *sima= CTX_wm_space_image(C);
571         ARegion *ar= CTX_wm_region(C);
572
573         sima_zoom_set_factor(sima, ar, 0.8f);
574
575         ED_region_tag_redraw(CTX_wm_region(C));
576         
577         return OPERATOR_FINISHED;
578 }
579
580 void IMAGE_OT_view_zoom_out(wmOperatorType *ot)
581 {
582         /* identifiers */
583         ot->name= "View Zoom Out";
584         ot->idname= "IMAGE_OT_view_zoom_out";
585         
586         /* api callbacks */
587         ot->exec= view_zoom_out_exec;
588         ot->poll= space_image_main_area_poll;
589 }
590
591 /********************** view zoom ratio operator *********************/
592
593 static int view_zoom_ratio_exec(bContext *C, wmOperator *op)
594 {
595         SpaceImage *sima= CTX_wm_space_image(C);
596         ARegion *ar= CTX_wm_region(C);
597
598         sima_zoom_set(sima, ar, RNA_float_get(op->ptr, "ratio"));
599         
600         /* ensure pixel exact locations for draw */
601         sima->xof= (int)sima->xof;
602         sima->yof= (int)sima->yof;
603
604         /* XXX notifier? */
605 #if 0
606         if(image_preview_active(curarea, NULL, NULL)) {
607                 /* recalculates new preview rect */
608                 scrarea_do_windraw(curarea);
609                 image_preview_event(2);
610         }
611 #endif
612
613         ED_region_tag_redraw(CTX_wm_region(C));
614         
615         return OPERATOR_FINISHED;
616 }
617
618 void IMAGE_OT_view_zoom_ratio(wmOperatorType *ot)
619 {
620         /* identifiers */
621         ot->name= "View Zoom Ratio";
622         ot->idname= "IMAGE_OT_view_zoom_ratio";
623         
624         /* api callbacks */
625         ot->exec= view_zoom_ratio_exec;
626         ot->poll= space_image_main_area_poll;
627         
628         /* properties */
629         RNA_def_float(ot->srna, "ratio", 0.0f, 0.0f, FLT_MAX,
630                 "Ratio", "Zoom ratio, 1.0 is 1:1, higher is zoomed in, lower is zoomed out.", -FLT_MAX, FLT_MAX);
631 }
632
633 /**************** load/replace/save callbacks ******************/
634
635 /* XXX make dynamic */
636 static const EnumPropertyItem image_file_type_items[] = {
637                 {R_TARGA, "TARGA", 0, "Targa", ""},
638                 {R_RAWTGA, "TARGA RAW", 0, "Targa Raw", ""},
639                 {R_PNG, "PNG", 0, "PNG", ""},
640                 {R_BMP, "BMP", 0, "BMP", ""},
641                 {R_JPEG90, "JPEG", 0, "Jpeg", ""},
642 #ifdef WITH_OPENJPEG
643                 {R_JP2, "JPEG_2000", 0, "Jpeg 2000", ""},
644 #endif
645                 {R_IRIS, "IRIS", 0, "Iris", ""},
646 #ifdef WITH_TIFF
647                 {R_TIFF, "TIFF", 0, "Tiff", ""},
648 #endif
649 #ifdef WITH_DDS
650                 {R_RADHDR, "RADIANCE_HDR", 0, "Radiance HDR", ""},
651 #endif
652 #ifdef WITH_CINEON
653                 {R_CINEON, "CINEON", 0, "Cineon", ""},
654                 {R_DPX, "DPX", 0, "DPX", ""},
655 #endif
656 #ifdef WITH_OPENEXR
657                 {R_OPENEXR, "OPENEXR", 0, "OpenEXR", ""},
658         /* saving sequences of multilayer won't work, they copy buffers  */
659         /*if(ima->source==IMA_SRC_SEQUENCE && ima->type==IMA_TYPE_MULTILAYER);
660         else*/
661                 {R_MULTILAYER, "MULTILAYER", 0, "MultiLayer", ""},
662 #endif  
663                 {0, NULL, 0, NULL, NULL}};
664
665 static void image_filesel(bContext *C, wmOperator *op, const char *path)
666 {
667         RNA_string_set(op->ptr, "filepath", path);
668         WM_event_add_fileselect(C, op); 
669 }
670
671 /******************** open image operator ********************/
672
673 static void open_init(bContext *C, wmOperator *op)
674 {
675         PropertyPointerRNA *pprop;
676
677         op->customdata= pprop= MEM_callocN(sizeof(PropertyPointerRNA), "OpenPropertyPointerRNA");
678         uiIDContextProperty(C, &pprop->ptr, &pprop->prop);
679 }
680
681 static int open_cancel(bContext *UNUSED(C), wmOperator *op)
682 {
683         MEM_freeN(op->customdata);
684         op->customdata= NULL;
685         return OPERATOR_CANCELLED;
686 }
687
688 static int open_exec(bContext *C, wmOperator *op)
689 {
690         SpaceImage *sima= CTX_wm_space_image(C); /* XXX other space types can call */
691         Scene *scene= CTX_data_scene(C);
692         Object *obedit= CTX_data_edit_object(C);
693         ImageUser *iuser= NULL;
694         PropertyPointerRNA *pprop;
695         PointerRNA idptr;
696         Image *ima= NULL;
697         char str[FILE_MAX];
698
699         RNA_string_get(op->ptr, "filepath", str);
700         /* default to frame 1 if there's no scene in context */
701
702         errno= 0;
703
704         ima= BKE_add_image_file(str);
705
706         if(!ima) {
707                 if(op->customdata) MEM_freeN(op->customdata);
708                 BKE_reportf(op->reports, RPT_ERROR, "Can't read: \"%s\", %s.", str, errno ? strerror(errno) : "Unsupported image format");
709                 return OPERATOR_CANCELLED;
710         }
711         
712         if(!op->customdata)
713                 open_init(C, op);
714
715         /* hook into UI */
716         pprop= op->customdata;
717
718         if(pprop->prop) {
719                 /* when creating new ID blocks, use is already 1, but RNA
720                  * pointer se also increases user, so this compensates it */
721                 ima->id.us--;
722
723                 RNA_id_pointer_create(&ima->id, &idptr);
724                 RNA_property_pointer_set(&pprop->ptr, pprop->prop, idptr);
725                 RNA_property_update(C, &pprop->ptr, pprop->prop);
726         }
727         else if(sima) {
728                 ED_space_image_set(C, sima, scene, obedit, ima);
729                 iuser= &sima->iuser;
730         }
731         else {
732                 Tex *tex= CTX_data_pointer_get_type(C, "texture", &RNA_Texture).data;
733                 if(tex && tex->type==TEX_IMAGE)
734                         iuser= &tex->iuser;
735                 
736         }
737         
738         /* initialize because of new image */
739         if(iuser) {
740                 iuser->sfra= 1;
741                 iuser->offset= 0;
742                 iuser->fie_ima= 2;
743         }
744
745         BKE_image_signal(ima, iuser, IMA_SIGNAL_RELOAD);
746         WM_event_add_notifier(C, NC_IMAGE|NA_EDITED, ima);
747         
748         MEM_freeN(op->customdata);
749
750         return OPERATOR_FINISHED;
751 }
752
753 static int open_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
754 {
755         SpaceImage *sima= CTX_wm_space_image(C); /* XXX other space types can call */
756         char *path=U.textudir;
757         Image *ima= NULL;
758
759         if(sima) {
760                  ima= sima->image;
761         }
762
763         if (ima==NULL) {
764                  Tex *tex= CTX_data_pointer_get_type(C, "texture", &RNA_Texture).data;
765                  if(tex && tex->type==TEX_IMAGE)
766                          ima= tex->ima;
767         }
768
769         if(ima)
770                 path= ima->name;
771         
772
773         if(!RNA_property_is_set(op->ptr, "relative_path"))
774                 RNA_boolean_set(op->ptr, "relative_path", U.flag & USER_RELPATHS);
775
776         if(RNA_property_is_set(op->ptr, "filepath"))
777                 return open_exec(C, op);
778         
779         open_init(C, op);
780
781         image_filesel(C, op, path);
782
783         return OPERATOR_RUNNING_MODAL;
784 }
785
786 /* called by other space types too */
787 void IMAGE_OT_open(wmOperatorType *ot)
788 {
789         /* identifiers */
790         ot->name= "Open";
791         ot->idname= "IMAGE_OT_open";
792         
793         /* api callbacks */
794         ot->exec= open_exec;
795         ot->invoke= open_invoke;
796         ot->cancel= open_cancel;
797
798         /* flags */
799         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
800
801         /* properties */
802         WM_operator_properties_filesel(ot, FOLDERFILE|IMAGEFILE|MOVIEFILE, FILE_SPECIAL, FILE_OPENFILE, WM_FILESEL_FILEPATH|WM_FILESEL_RELPATH);
803 }
804
805 /******************** replace image operator ********************/
806
807 static int replace_exec(bContext *C, wmOperator *op)
808 {
809         SpaceImage *sima= CTX_wm_space_image(C);
810         char str[FILE_MAX];
811
812         if(!sima->image)
813                 return OPERATOR_CANCELLED;
814         
815         RNA_string_get(op->ptr, "filepath", str);
816         BLI_strncpy(sima->image->name, str, sizeof(sima->image->name)); /* we cant do much if the str is longer then 240 :/ */
817
818         BKE_image_signal(sima->image, &sima->iuser, IMA_SIGNAL_RELOAD);
819         WM_event_add_notifier(C, NC_IMAGE|NA_EDITED, sima->image);
820
821         return OPERATOR_FINISHED;
822 }
823
824 static int replace_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
825 {
826         SpaceImage *sima= CTX_wm_space_image(C);
827
828         if(!sima->image)
829                 return OPERATOR_CANCELLED;
830
831         if(RNA_property_is_set(op->ptr, "filepath"))
832                 return replace_exec(C, op);
833
834         if(!RNA_property_is_set(op->ptr, "relative_path"))
835                 RNA_boolean_set(op->ptr, "relative_path", (strncmp(sima->image->name, "//", 2))==0);
836
837         image_filesel(C, op, sima->image->name);
838
839         return OPERATOR_RUNNING_MODAL;
840 }
841
842 void IMAGE_OT_replace(wmOperatorType *ot)
843 {
844         /* identifiers */
845         ot->name= "Replace";
846         ot->idname= "IMAGE_OT_replace";
847         
848         /* api callbacks */
849         ot->exec= replace_exec;
850         ot->invoke= replace_invoke;
851         ot->poll= space_image_poll;
852
853         /* flags */
854         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
855
856         /* properties */
857         WM_operator_properties_filesel(ot, FOLDERFILE|IMAGEFILE|MOVIEFILE, FILE_SPECIAL, FILE_OPENFILE, WM_FILESEL_FILEPATH|WM_FILESEL_RELPATH);
858 }
859
860 /******************** save image as operator ********************/
861
862 /* assumes name is FILE_MAX */
863 /* ima->name and ibuf->name should end up the same */
864 static void save_image_doit(bContext *C, SpaceImage *sima, Scene *scene, wmOperator *op, char *path, int do_newpath)
865 {
866         Image *ima= ED_space_image(sima);
867         void *lock;
868         ImBuf *ibuf= ED_space_image_acquire_buffer(sima, &lock);
869
870         if (ibuf) {
871                 Main *bmain= CTX_data_main(C);
872                 const short relative= (RNA_struct_find_property(op->ptr, "relative_path") && RNA_boolean_get(op->ptr, "relative_path"));
873                 const short save_copy= (RNA_struct_find_property(op->ptr, "copy") && RNA_boolean_get(op->ptr, "copy"));
874                 short ok= FALSE;
875
876                 BLI_path_abs(path, bmain->name);
877                 /* old global to ensure a 2nd save goes to same dir */
878                 BLI_strncpy(G.ima, path, sizeof(G.ima));
879
880                 WM_cursor_wait(1);
881
882                 if(ima->type == IMA_TYPE_R_RESULT) {
883                         /* enforce user setting for RGB or RGBA, but skip BW */
884                         if(scene->r.planes==32) {
885                                 ibuf->depth= 32;
886                         }
887                         else if(scene->r.planes==24) {
888                                 ibuf->depth= 24;
889                         }
890                 }
891                 else {
892                         /* TODO, better solution, if a 24bit image is painted onto it may contain alpha */
893                         if(ibuf->userflags & IB_BITMAPDIRTY) { /* it has been painted onto */
894                                 /* checks each pixel, not ideal */
895                                 ibuf->depth= BKE_alphatest_ibuf(ibuf) ? 32 : 24;
896                         }
897                 }
898
899                 if(scene->r.scemode & R_EXTENSION)  {
900                         BKE_add_image_extension(path, sima->imtypenr);
901                 }
902                 
903                 if(sima->imtypenr==R_MULTILAYER) {
904                         RenderResult *rr= BKE_image_acquire_renderresult(scene, ima);
905                         if(rr) {
906                                 RE_WriteRenderResult(rr, path, scene->r.quality);
907                                 ok= TRUE;
908                         }
909                         else {
910                                 BKE_report(op->reports, RPT_ERROR, "Did not write, no Multilayer Image");
911                         }
912                         BKE_image_release_renderresult(scene, ima);
913                 }
914                 else if (BKE_write_ibuf(scene, ibuf, path, sima->imtypenr, scene->r.subimtype, scene->r.quality)) {
915                         ok= TRUE;
916                 }
917
918                 if(ok)  {
919                         if(relative)
920                                 BLI_path_rel(path, bmain->name); /* only after saving */
921
922                         if(ibuf->name[0]==0) {
923                                 BLI_strncpy(ibuf->name, path, sizeof(ibuf->name));
924                                 BLI_strncpy(ima->name, path, sizeof(ima->name));
925                         }
926
927                         if(!save_copy) {
928                                 if(do_newpath) {
929                                         BLI_strncpy(ima->name, path, sizeof(ima->name));
930                                         BLI_strncpy(ibuf->name, path, sizeof(ibuf->name));
931                                 }
932
933                                 ibuf->userflags &= ~IB_BITMAPDIRTY;
934
935                                 /* change type? */
936                                 if(ima->type==IMA_TYPE_R_RESULT) {
937                                         ima->type= IMA_TYPE_IMAGE;
938
939                                         /* workaround to ensure the render result buffer is no longer used
940                                          * by this image, otherwise can crash when a new render result is
941                                          * created. */
942                                         if(ibuf->rect && !(ibuf->mall & IB_rect))
943                                                 imb_freerectImBuf(ibuf);
944                                         if(ibuf->rect_float && !(ibuf->mall & IB_rectfloat))
945                                                 imb_freerectfloatImBuf(ibuf);
946                                         if(ibuf->zbuf && !(ibuf->mall & IB_zbuf))
947                                                 IMB_freezbufImBuf(ibuf);
948                                         if(ibuf->zbuf_float && !(ibuf->mall & IB_zbuffloat))
949                                                 IMB_freezbuffloatImBuf(ibuf);
950                                 }
951                                 if( ELEM(ima->source, IMA_SRC_GENERATED, IMA_SRC_VIEWER)) {
952                                         ima->source= IMA_SRC_FILE;
953                                         ima->type= IMA_TYPE_IMAGE;
954                                 }
955                         }
956                 }
957                 else {
958                         BKE_reportf(op->reports, RPT_ERROR, "Couldn't write image: %s", path);
959                 }
960
961
962                 WM_event_add_notifier(C, NC_IMAGE|NA_EDITED, sima->image);
963
964                 WM_cursor_wait(0);
965         }
966
967         ED_space_image_release_buffer(sima, lock);
968 }
969
970 static int save_as_exec(bContext *C, wmOperator *op)
971 {
972         SpaceImage *sima= CTX_wm_space_image(C);
973         Scene *scene= CTX_data_scene(C);
974         Image *ima = ED_space_image(sima);
975         char str[FILE_MAX];
976
977         if(!ima)
978                 return OPERATOR_CANCELLED;
979
980         sima->imtypenr= RNA_enum_get(op->ptr, "file_type");
981         RNA_string_get(op->ptr, "filepath", str);
982
983         save_image_doit(C, sima, scene, op, str, TRUE);
984
985         return OPERATOR_FINISHED;
986 }
987
988
989 static int save_as_check(bContext *UNUSED(C), wmOperator *op)
990 {
991         char filepath[FILE_MAX];
992         RNA_string_get(op->ptr, "filepath", filepath);
993         if(BKE_add_image_extension(filepath, RNA_enum_get(op->ptr, "file_type"))) {
994                 RNA_string_set(op->ptr, "filepath", filepath);
995                 return TRUE;
996         }
997         return FALSE;
998 }
999
1000 static int save_as_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
1001 {
1002         SpaceImage *sima= CTX_wm_space_image(C);
1003         Image *ima = ED_space_image(sima);
1004         Scene *scene= CTX_data_scene(C);
1005         ImBuf *ibuf;
1006         char filename[FILE_MAX];
1007         
1008         void *lock;
1009
1010         if(!RNA_property_is_set(op->ptr, "relative_path"))
1011                 RNA_boolean_set(op->ptr, "relative_path", U.flag & USER_RELPATHS);
1012
1013         if(RNA_property_is_set(op->ptr, "filepath"))
1014                 return save_as_exec(C, op);
1015         
1016         if(!ima)
1017                 return OPERATOR_CANCELLED;
1018
1019         /* always opens fileselect */
1020         ibuf= ED_space_image_acquire_buffer(sima, &lock);
1021
1022         if(ibuf) {
1023                 /* cant save multilayer sequence, ima->rr isn't valid for a specific frame */
1024                 if(ima->rr && !(ima->source==IMA_SRC_SEQUENCE && ima->type==IMA_TYPE_MULTILAYER))
1025                         sima->imtypenr= R_MULTILAYER;
1026                 else if(ima->type==IMA_TYPE_R_RESULT)
1027                         sima->imtypenr= scene->r.imtype;
1028                 else if (ima->source == IMA_SRC_GENERATED)
1029                         sima->imtypenr= R_PNG;
1030                 else
1031                         sima->imtypenr= BKE_ftype_to_imtype(ibuf->ftype);
1032
1033                 RNA_enum_set(op->ptr, "file_type", sima->imtypenr);
1034                 
1035                 if(ibuf->name[0]==0)
1036                         if ( (G.ima[0] == '/') && (G.ima[1] == '/') && (G.ima[2] == '\0') ) {
1037                                 BLI_strncpy(filename, "//untitled", FILE_MAX);
1038                         } else {
1039                                 BLI_strncpy(filename, G.ima, FILE_MAX);
1040                         }
1041                 else
1042                         BLI_strncpy(filename, ibuf->name, FILE_MAX);
1043                 
1044                 /* enable save_copy by default for render results */
1045                 if(ELEM(ima->type, IMA_TYPE_R_RESULT, IMA_TYPE_COMPOSITE) && !RNA_property_is_set(op->ptr, "copy")) {
1046                         RNA_boolean_set(op->ptr, "copy", TRUE);
1047                 }
1048
1049                 // XXX note: we can give default menu enums to operator for this 
1050                 image_filesel(C, op, filename);
1051
1052                 ED_space_image_release_buffer(sima, lock);
1053                 
1054                 return OPERATOR_RUNNING_MODAL;
1055         }
1056
1057         ED_space_image_release_buffer(sima, lock);
1058
1059         return OPERATOR_CANCELLED;
1060 }
1061
1062 void IMAGE_OT_save_as(wmOperatorType *ot)
1063 {
1064         /* identifiers */
1065         ot->name= "Save As";
1066         ot->idname= "IMAGE_OT_save_as";
1067         
1068         /* api callbacks */
1069         ot->exec= save_as_exec;
1070         ot->check= save_as_check;
1071         ot->invoke= save_as_invoke;
1072         ot->poll= space_image_buffer_exists_poll;
1073
1074         /* flags */
1075         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1076
1077         /* properties */
1078         RNA_def_enum(ot->srna, "file_type", image_file_type_items, R_PNG, "File Type", "File type to save image as.");
1079         WM_operator_properties_filesel(ot, FOLDERFILE|IMAGEFILE|MOVIEFILE, FILE_SPECIAL, FILE_SAVE, WM_FILESEL_FILEPATH|WM_FILESEL_RELPATH);
1080
1081         RNA_def_boolean(ot->srna, "copy", 0, "Copy", "Create a new image file without modifying the current image in blender");
1082 }
1083
1084 /******************** save image operator ********************/
1085
1086 static int save_exec(bContext *C, wmOperator *op)
1087 {
1088         Main *bmain= CTX_data_main(C);
1089         SpaceImage *sima= CTX_wm_space_image(C);
1090         Image *ima = ED_space_image(sima);
1091         void *lock;
1092         ImBuf *ibuf= ED_space_image_acquire_buffer(sima, &lock);
1093         Scene *scene= CTX_data_scene(C);
1094         RenderResult *rr;
1095         char name[FILE_MAX];
1096
1097         if(!ima || !ibuf) {
1098                 ED_space_image_release_buffer(sima, lock);
1099                 return OPERATOR_CANCELLED;
1100         }
1101
1102         /* if exists, saves over without fileselect */
1103         
1104         BLI_strncpy(name, ima->name, FILE_MAX);
1105         if(name[0]==0)
1106                 BLI_strncpy(name, G.ima, FILE_MAX);
1107         else
1108                 BLI_path_abs(name, bmain->name);
1109         
1110         if(BLI_exists(name) && BLI_is_writable(name)) {
1111                 rr= BKE_image_acquire_renderresult(scene, ima);
1112
1113                 if(rr)
1114                         sima->imtypenr= R_MULTILAYER;
1115                 else 
1116                         sima->imtypenr= BKE_ftype_to_imtype(ibuf->ftype);
1117
1118                 BKE_image_release_renderresult(scene, ima);
1119                 ED_space_image_release_buffer(sima, lock);
1120                 
1121                 save_image_doit(C, sima, scene, op, name, FALSE);
1122         }
1123         else {
1124                 ED_space_image_release_buffer(sima, lock);
1125
1126                 BKE_report(op->reports, RPT_ERROR, "Can not save image.");
1127                 return OPERATOR_CANCELLED;
1128         }
1129
1130         return OPERATOR_FINISHED;
1131 }
1132
1133 void IMAGE_OT_save(wmOperatorType *ot)
1134 {
1135         /* identifiers */
1136         ot->name= "Save";
1137         ot->idname= "IMAGE_OT_save";
1138         
1139         /* api callbacks */
1140         ot->exec= save_exec;
1141         ot->poll= space_image_file_exists_poll;
1142
1143         /* flags */
1144         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1145 }
1146
1147 /******************* save sequence operator ********************/
1148
1149 static int save_sequence_exec(bContext *C, wmOperator *op)
1150 {
1151         Main *bmain= CTX_data_main(C);
1152         SpaceImage *sima= CTX_wm_space_image(C);
1153         ImBuf *ibuf;
1154         int tot= 0;
1155         char di[FILE_MAX], fi[FILE_MAX];
1156         
1157         if(sima->image==NULL)
1158                 return OPERATOR_CANCELLED;
1159
1160         if(sima->image->source!=IMA_SRC_SEQUENCE) {
1161                 BKE_report(op->reports, RPT_ERROR, "Can only save sequence on image sequences.");
1162                 return OPERATOR_CANCELLED;
1163         }
1164
1165         if(sima->image->type==IMA_TYPE_MULTILAYER) {
1166                 BKE_report(op->reports, RPT_ERROR, "Can't save multilayer sequences.");
1167                 return OPERATOR_CANCELLED;
1168         }
1169         
1170         /* get total */
1171         for(ibuf= sima->image->ibufs.first; ibuf; ibuf= ibuf->next) 
1172                 if(ibuf->userflags & IB_BITMAPDIRTY)
1173                         tot++;
1174         
1175         if(tot==0) {
1176                 BKE_report(op->reports, RPT_WARNING, "No images have been changed.");
1177                 return OPERATOR_CANCELLED;
1178         }
1179
1180         /* get a filename for menu */
1181         for(ibuf= sima->image->ibufs.first; ibuf; ibuf= ibuf->next) 
1182                 if(ibuf->userflags & IB_BITMAPDIRTY)
1183                         break;
1184         
1185         BLI_strncpy(di, ibuf->name, FILE_MAX);
1186         BLI_splitdirstring(di, fi);
1187         
1188         BKE_reportf(op->reports, RPT_INFO, "%d Image(s) will be saved in %s", tot, di);
1189
1190         for(ibuf= sima->image->ibufs.first; ibuf; ibuf= ibuf->next) {
1191                 if(ibuf->userflags & IB_BITMAPDIRTY) {
1192                         char name[FILE_MAX];
1193                         BLI_strncpy(name, ibuf->name, sizeof(name));
1194                         
1195                         BLI_path_abs(name, bmain->name);
1196
1197                         if(0 == IMB_saveiff(ibuf, name, IB_rect | IB_zbuf | IB_zbuffloat)) {
1198                                 BKE_reportf(op->reports, RPT_ERROR, "Could not write image %s.", name);
1199                                 break;
1200                         }
1201
1202                         BKE_reportf(op->reports, RPT_INFO, "Saved: %s\n", ibuf->name);
1203                         ibuf->userflags &= ~IB_BITMAPDIRTY;
1204                 }
1205         }
1206
1207         return OPERATOR_FINISHED;
1208 }
1209
1210 void IMAGE_OT_save_sequence(wmOperatorType *ot)
1211 {
1212         /* identifiers */
1213         ot->name= "Save Sequence";
1214         ot->idname= "IMAGE_OT_save_sequence";
1215         
1216         /* api callbacks */
1217         ot->exec= save_sequence_exec;
1218         ot->poll= space_image_buffer_exists_poll;
1219
1220         /* flags */
1221         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1222 }
1223
1224 /******************** reload image operator ********************/
1225
1226 static int reload_exec(bContext *C, wmOperator *UNUSED(op))
1227 {
1228         Image *ima= CTX_data_edit_image(C);
1229         SpaceImage *sima= CTX_wm_space_image(C);
1230
1231         if(!ima)
1232                 return OPERATOR_CANCELLED;
1233
1234         // XXX other users?
1235         BKE_image_signal(ima, (sima)? &sima->iuser: NULL, IMA_SIGNAL_RELOAD);
1236
1237         WM_event_add_notifier(C, NC_IMAGE|NA_EDITED, ima);
1238         
1239         return OPERATOR_FINISHED;
1240 }
1241
1242 void IMAGE_OT_reload(wmOperatorType *ot)
1243 {
1244         /* identifiers */
1245         ot->name= "Reload";
1246         ot->idname= "IMAGE_OT_reload";
1247         
1248         /* api callbacks */
1249         ot->exec= reload_exec;
1250
1251         /* flags */
1252         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1253 }
1254
1255 /********************** new image operator *********************/
1256
1257 static int image_new_exec(bContext *C, wmOperator *op)
1258 {
1259         SpaceImage *sima;
1260         Scene *scene;
1261         Object *obedit;
1262         Image *ima;
1263         PointerRNA ptr, idptr;
1264         PropertyRNA *prop;
1265         char name[22];
1266         float color[4];
1267         int width, height, floatbuf, uvtestgrid, alpha;
1268
1269         /* retrieve state */
1270         sima= CTX_wm_space_image(C);
1271         scene= (Scene*)CTX_data_scene(C);
1272         obedit= CTX_data_edit_object(C);
1273
1274         RNA_string_get(op->ptr, "name", name);
1275         width= RNA_int_get(op->ptr, "width");
1276         height= RNA_int_get(op->ptr, "height");
1277         floatbuf= RNA_boolean_get(op->ptr, "float");
1278         uvtestgrid= RNA_boolean_get(op->ptr, "uv_test_grid");
1279         RNA_float_get_array(op->ptr, "color", color);
1280         alpha= RNA_boolean_get(op->ptr, "alpha");
1281         
1282         if (!floatbuf && scene->r.color_mgt_flag & R_COLOR_MANAGEMENT)
1283                 linearrgb_to_srgb_v3_v3(color, color);
1284
1285         if(!alpha)
1286                 color[3]= 1.0f;
1287
1288         ima = BKE_add_image_size(width, height, name, alpha ? 32 : 24, floatbuf, uvtestgrid, color);
1289
1290         if(!ima)
1291                 return OPERATOR_CANCELLED;
1292
1293         /* hook into UI */
1294         uiIDContextProperty(C, &ptr, &prop);
1295
1296         if(prop) {
1297                 /* when creating new ID blocks, use is already 1, but RNA
1298                  * pointer se also increases user, so this compensates it */
1299                 ima->id.us--;
1300
1301                 RNA_id_pointer_create(&ima->id, &idptr);
1302                 RNA_property_pointer_set(&ptr, prop, idptr);
1303                 RNA_property_update(C, &ptr, prop);
1304         }
1305         else if(sima)
1306                 ED_space_image_set(C, sima, scene, obedit, ima);
1307
1308         // XXX other users?
1309         BKE_image_signal(ima, (sima)? &sima->iuser: NULL, IMA_SIGNAL_USER_NEW_IMAGE);
1310         
1311         return OPERATOR_FINISHED;
1312 }
1313
1314 /* XXX, Ton is not a fan of OK buttons but using this function to avoid undo/redo bug while in mesh-editmode, - campbell */
1315 static int image_new_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
1316 {
1317         return WM_operator_props_dialog_popup(C, op, 300, 100);
1318
1319 }
1320
1321 void IMAGE_OT_new(wmOperatorType *ot)
1322 {
1323         PropertyRNA *prop;
1324         static float default_color[4]= {0.0f, 0.0f, 0.0f, 1.0f};
1325         
1326         /* identifiers */
1327         ot->name= "New";
1328         ot->idname= "IMAGE_OT_new";
1329         
1330         /* api callbacks */
1331         ot->exec= image_new_exec;
1332         ot->invoke= image_new_invoke;
1333         
1334         /* flags */
1335         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1336
1337         /* properties */
1338         RNA_def_string(ot->srna, "name", "untitled", 21, "Name", "Image datablock name.");
1339         RNA_def_int(ot->srna, "width", 1024, 1, INT_MAX, "Width", "Image width.", 1, 16384);
1340         RNA_def_int(ot->srna, "height", 1024, 1, INT_MAX, "Height", "Image height.", 1, 16384);
1341         prop= RNA_def_float_color(ot->srna, "color", 4, NULL, 0.0f, FLT_MAX, "Color", "Default fill color.", 0.0f, 1.0f);
1342         RNA_def_property_float_array_default(prop, default_color);
1343         RNA_def_boolean(ot->srna, "alpha", 1, "Alpha", "Create an image with an alpha channel.");
1344         RNA_def_boolean(ot->srna, "uv_test_grid", 0, "UV Test Grid", "Fill the image with a grid for UV map testing.");
1345         RNA_def_boolean(ot->srna, "float", 0, "32 bit Float", "Create image with 32 bit floating point bit depth.");
1346 }
1347
1348 /********************* pack operator *********************/
1349
1350 static int pack_test(bContext *C, wmOperator *op)
1351 {
1352         Image *ima= CTX_data_edit_image(C);
1353         int as_png= RNA_boolean_get(op->ptr, "as_png");
1354
1355         if(!ima)
1356                 return 0;
1357         if(!as_png && ima->packedfile)
1358                 return 0;
1359
1360         if(ima->source==IMA_SRC_SEQUENCE || ima->source==IMA_SRC_MOVIE) {
1361                 BKE_report(op->reports, RPT_ERROR, "Packing movies or image sequences not supported.");
1362                 return 0;
1363         }
1364
1365         return 1;
1366 }
1367
1368 static int pack_exec(bContext *C, wmOperator *op)
1369 {
1370         Image *ima= CTX_data_edit_image(C);
1371         ImBuf *ibuf= BKE_image_get_ibuf(ima, NULL);
1372         int as_png= RNA_boolean_get(op->ptr, "as_png");
1373
1374         if(!pack_test(C, op))
1375                 return OPERATOR_CANCELLED;
1376         
1377         if(!as_png && (ibuf && (ibuf->userflags & IB_BITMAPDIRTY))) {
1378                 BKE_report(op->reports, RPT_ERROR, "Can't pack edited image from disk, only as internal PNG.");
1379                 return OPERATOR_CANCELLED;
1380         }
1381
1382         if(as_png)
1383                 BKE_image_memorypack(ima);
1384         else
1385                 ima->packedfile= newPackedFile(op->reports, ima->name);
1386
1387         WM_event_add_notifier(C, NC_IMAGE|NA_EDITED, ima);
1388         
1389         return OPERATOR_FINISHED;
1390 }
1391
1392 static int pack_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
1393 {
1394         Image *ima= CTX_data_edit_image(C);
1395         ImBuf *ibuf= BKE_image_get_ibuf(ima, NULL);
1396         uiPopupMenu *pup;
1397         uiLayout *layout;
1398         int as_png= RNA_boolean_get(op->ptr, "as_png");
1399
1400         if(!pack_test(C, op))
1401                 return OPERATOR_CANCELLED;
1402         
1403         if(!as_png && (ibuf && (ibuf->userflags & IB_BITMAPDIRTY))) {
1404                 pup= uiPupMenuBegin(C, "OK", ICON_QUESTION);
1405                 layout= uiPupMenuLayout(pup);
1406                 uiItemBooleanO(layout, "Can't pack edited image from disk. Pack as internal PNG?", ICON_NULL, op->idname, "as_png", 1);
1407                 uiPupMenuEnd(C, pup);
1408
1409                 return OPERATOR_CANCELLED;
1410         }
1411
1412         return pack_exec(C, op);
1413 }
1414
1415 void IMAGE_OT_pack(wmOperatorType *ot)
1416 {
1417         /* identifiers */
1418         ot->name= "Pack";
1419         ot->description= "Pack an image as embedded data into the .blend file"; 
1420         ot->idname= "IMAGE_OT_pack";
1421         
1422         /* api callbacks */
1423         ot->exec= pack_exec;
1424         ot->invoke= pack_invoke;
1425
1426         /* flags */
1427         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1428
1429         /* properties */
1430         RNA_def_boolean(ot->srna, "as_png", 0, "Pack As PNG", "Pack image as lossless PNG.");
1431 }
1432
1433 /********************* unpack operator *********************/
1434
1435 static int image_unpack_exec(bContext *C, wmOperator *op)
1436 {
1437         Image *ima= CTX_data_edit_image(C);
1438         int method= RNA_enum_get(op->ptr, "method");
1439
1440         /* find the suppplied image by name */
1441         if (RNA_property_is_set(op->ptr, "id")) {
1442                 char imaname[22];
1443                 RNA_string_get(op->ptr, "id", imaname);
1444                 ima = BLI_findstring(&CTX_data_main(C)->image, imaname, offsetof(ID, name) + 2);
1445                 if (!ima) ima = CTX_data_edit_image(C);
1446         }
1447         
1448         if(!ima || !ima->packedfile)
1449                 return OPERATOR_CANCELLED;
1450
1451         if(ima->source==IMA_SRC_SEQUENCE || ima->source==IMA_SRC_MOVIE) {
1452                 BKE_report(op->reports, RPT_ERROR, "Unpacking movies or image sequences not supported.");
1453                 return OPERATOR_CANCELLED;
1454         }
1455
1456         if(G.fileflags & G_AUTOPACK)
1457                 BKE_report(op->reports, RPT_WARNING, "AutoPack is enabled, so image will be packed again on file save.");
1458                 
1459         unpackImage(op->reports, ima, method);
1460         
1461         WM_event_add_notifier(C, NC_IMAGE|NA_EDITED, ima);
1462
1463         return OPERATOR_FINISHED;
1464 }
1465
1466 static int image_unpack_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
1467 {
1468         Image *ima= CTX_data_edit_image(C);
1469
1470         if(RNA_property_is_set(op->ptr, "id"))
1471                 return image_unpack_exec(C, op);
1472                 
1473         if(!ima || !ima->packedfile)
1474                 return OPERATOR_CANCELLED;
1475
1476         if(ima->source==IMA_SRC_SEQUENCE || ima->source==IMA_SRC_MOVIE) {
1477                 BKE_report(op->reports, RPT_ERROR, "Unpacking movies or image sequences not supported.");
1478                 return OPERATOR_CANCELLED;
1479         }
1480
1481         if(G.fileflags & G_AUTOPACK)
1482                 BKE_report(op->reports, RPT_WARNING, "AutoPack is enabled, so image will be packed again on file save.");
1483
1484         unpack_menu(C, "IMAGE_OT_unpack", ima->id.name+2, ima->name, "textures", ima->packedfile);
1485
1486         return OPERATOR_FINISHED;
1487 }
1488
1489 void IMAGE_OT_unpack(wmOperatorType *ot)
1490 {
1491         /* identifiers */
1492         ot->name= "Unpack";
1493         ot->description= "Save an image packed in the .blend file to disk"; 
1494         ot->idname= "IMAGE_OT_unpack";
1495         
1496         /* api callbacks */
1497         ot->exec= image_unpack_exec;
1498         ot->invoke= image_unpack_invoke;
1499
1500         /* flags */
1501         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1502
1503         /* properties */
1504         RNA_def_enum(ot->srna, "method", unpack_method_items, PF_USE_LOCAL, "Method", "How to unpack.");
1505         RNA_def_string(ot->srna, "id", "", 21, "Image Name", "Image datablock name to unpack."); /* XXX, weark!, will fail with library, name collisions */
1506 }
1507
1508 /******************** sample image operator ********************/
1509
1510 typedef struct ImageSampleInfo {
1511         ARegionType *art;
1512         void *draw_handle;
1513         int x, y;
1514         int channels;
1515
1516         char col[4];
1517         float colf[4];
1518         int z;
1519         float zf;
1520
1521         char *colp;
1522         float *colfp;
1523         int *zp;
1524         float *zfp;
1525
1526         int draw;
1527 } ImageSampleInfo;
1528
1529 static void sample_draw(const bContext *UNUSED(C), ARegion *ar, void *arg_info)
1530 {
1531         ImageSampleInfo *info= arg_info;
1532         if(info->draw) {
1533                 draw_image_info(ar, info->channels, info->x, info->y, info->colp, info->colfp, info->zp, info->zfp);
1534         }
1535 }
1536
1537 static void sample_apply(bContext *C, wmOperator *op, wmEvent *event)
1538 {
1539         SpaceImage *sima= CTX_wm_space_image(C);
1540         ARegion *ar= CTX_wm_region(C);
1541         void *lock;
1542         ImBuf *ibuf= ED_space_image_acquire_buffer(sima, &lock);
1543         ImageSampleInfo *info= op->customdata;
1544         float fx, fy;
1545         int mx, my;
1546         
1547         if(ibuf == NULL) {
1548                 ED_space_image_release_buffer(sima, lock);
1549                 return;
1550         }
1551
1552         mx= event->x - ar->winrct.xmin;
1553         my= event->y - ar->winrct.ymin;
1554         UI_view2d_region_to_view(&ar->v2d, mx, my, &fx, &fy);
1555
1556         if(fx>=0.0 && fy>=0.0 && fx<1.0 && fy<1.0) {
1557                 float *fp;
1558                 char *cp;
1559                 int x= (int)(fx*ibuf->x), y= (int)(fy*ibuf->y);
1560
1561                 CLAMP(x, 0, ibuf->x-1);
1562                 CLAMP(y, 0, ibuf->y-1);
1563
1564                 info->x= x;
1565                 info->y= y;
1566                 info->draw= 1;
1567                 info->channels= ibuf->channels;
1568
1569                 info->colp= NULL;
1570                 info->colfp= NULL;
1571                 info->zp= NULL;
1572                 info->zfp= NULL;
1573                 
1574                 if(ibuf->rect) {
1575                         cp= (char *)(ibuf->rect + y*ibuf->x + x);
1576
1577                         info->col[0]= cp[0];
1578                         info->col[1]= cp[1];
1579                         info->col[2]= cp[2];
1580                         info->col[3]= cp[3];
1581                         info->colp= info->col;
1582
1583                         info->colf[0]= (float)cp[0]/255.0f;
1584                         info->colf[1]= (float)cp[1]/255.0f;
1585                         info->colf[2]= (float)cp[2]/255.0f;
1586                         info->colf[3]= (float)cp[3]/255.0f;
1587                         info->colfp= info->colf;
1588                 }
1589                 if(ibuf->rect_float) {
1590                         fp= (ibuf->rect_float + (ibuf->channels)*(y*ibuf->x + x));
1591
1592                         info->colf[0]= fp[0];
1593                         info->colf[1]= fp[1];
1594                         info->colf[2]= fp[2];
1595                         info->colf[3]= fp[3];
1596                         info->colfp= info->colf;
1597                 }
1598
1599                 if(ibuf->zbuf) {
1600                         info->z= ibuf->zbuf[y*ibuf->x + x];
1601                         info->zp= &info->z;
1602                 }
1603                 if(ibuf->zbuf_float) {
1604                         info->zf= ibuf->zbuf_float[y*ibuf->x + x];
1605                         info->zfp= &info->zf;
1606                 }
1607                 
1608                 if(sima->cumap && ibuf->channels==4) {
1609                         /* we reuse this callback for set curves point operators */
1610                         if(RNA_struct_find_property(op->ptr, "point")) {
1611                                 int point= RNA_enum_get(op->ptr, "point");
1612
1613                                 if(point == 1) {
1614                                         curvemapping_set_black_white(sima->cumap, NULL, info->colfp);
1615                                         if(ibuf->rect_float)
1616                                                 curvemapping_do_ibuf(sima->cumap, ibuf);
1617                                 }
1618                                 else if(point == 0) {
1619                                         curvemapping_set_black_white(sima->cumap, info->colfp, NULL);
1620                                         if(ibuf->rect_float)
1621                                                 curvemapping_do_ibuf(sima->cumap, ibuf);
1622                                 }
1623                         }
1624                 }
1625                                 
1626                 // XXX node curve integration ..
1627 #if 0
1628                 {
1629                         ScrArea *sa, *cur= curarea;
1630                         
1631                         node_curvemap_sample(fp);       /* sends global to node editor */
1632                         for(sa= G.curscreen->areabase.first; sa; sa= sa->next) {
1633                                 if(sa->spacetype==SPACE_NODE) {
1634                                         areawinset(sa->win);
1635                                         scrarea_do_windraw(sa);
1636                                 }
1637                         }
1638                         node_curvemap_sample(NULL);             /* clears global in node editor */
1639                         curarea= cur;
1640                 }
1641 #endif
1642         }
1643         else
1644                 info->draw= 0;
1645
1646         ED_space_image_release_buffer(sima, lock);
1647         ED_area_tag_redraw(CTX_wm_area(C));
1648 }
1649
1650 static void sample_exit(bContext *C, wmOperator *op)
1651 {
1652         ImageSampleInfo *info= op->customdata;
1653
1654         ED_region_draw_cb_exit(info->art, info->draw_handle);
1655         ED_area_tag_redraw(CTX_wm_area(C));
1656         MEM_freeN(info);
1657 }
1658
1659 static int sample_invoke(bContext *C, wmOperator *op, wmEvent *event)
1660 {
1661         SpaceImage *sima= CTX_wm_space_image(C);
1662         ARegion *ar= CTX_wm_region(C);
1663         ImageSampleInfo *info;
1664
1665         if(!ED_space_image_has_buffer(sima))
1666                 return OPERATOR_CANCELLED;
1667         
1668         info= MEM_callocN(sizeof(ImageSampleInfo), "ImageSampleInfo");
1669         info->art= ar->type;
1670         info->draw_handle = ED_region_draw_cb_activate(ar->type, sample_draw, info, REGION_DRAW_POST_PIXEL);
1671         op->customdata= info;
1672
1673         sample_apply(C, op, event);
1674
1675         WM_event_add_modal_handler(C, op);
1676
1677         return OPERATOR_RUNNING_MODAL;
1678 }
1679
1680 static int sample_modal(bContext *C, wmOperator *op, wmEvent *event)
1681 {
1682         switch(event->type) {
1683                 case LEFTMOUSE:
1684                 case RIGHTMOUSE: // XXX hardcoded
1685                         sample_exit(C, op);
1686                         return OPERATOR_CANCELLED;
1687                 case MOUSEMOVE:
1688                         sample_apply(C, op, event);
1689                         break;
1690         }
1691
1692         return OPERATOR_RUNNING_MODAL;
1693 }
1694
1695 static int sample_cancel(bContext *C, wmOperator *op)
1696 {
1697         sample_exit(C, op);
1698         return OPERATOR_CANCELLED;
1699 }
1700
1701 void IMAGE_OT_sample(wmOperatorType *ot)
1702 {
1703         /* identifiers */
1704         ot->name= "Sample";
1705         ot->idname= "IMAGE_OT_sample";
1706         
1707         /* api callbacks */
1708         ot->invoke= sample_invoke;
1709         ot->modal= sample_modal;
1710         ot->cancel= sample_cancel;
1711         ot->poll= space_image_main_area_poll;
1712
1713         /* flags */
1714         ot->flag= OPTYPE_BLOCKING;
1715 }
1716
1717 /******************** sample line operator ********************/
1718 static int sample_line_exec(bContext *C, wmOperator *op)
1719 {
1720         SpaceImage *sima= CTX_wm_space_image(C);
1721         ARegion *ar= CTX_wm_region(C);
1722         Scene *scene= CTX_data_scene(C);
1723         
1724         int x_start= RNA_int_get(op->ptr, "xstart");
1725         int y_start= RNA_int_get(op->ptr, "ystart");
1726         int x_end= RNA_int_get(op->ptr, "xend");
1727         int y_end= RNA_int_get(op->ptr, "yend");
1728         
1729         void *lock;
1730         ImBuf *ibuf= ED_space_image_acquire_buffer(sima, &lock);
1731         Histogram *hist= &sima->sample_line_hist;
1732         
1733         float x1f, y1f, x2f, y2f;
1734         int x1, y1, x2, y2;
1735         int i, x, y;
1736         float *fp;
1737         float rgb[3];
1738         unsigned char *cp;
1739         
1740         if (ibuf == NULL) {
1741                 ED_space_image_release_buffer(sima, lock);
1742                 return OPERATOR_CANCELLED;
1743         }
1744         /* hmmmm */
1745         if (ibuf->channels < 3) {
1746                 ED_space_image_release_buffer(sima, lock);
1747                 return OPERATOR_CANCELLED;
1748         }
1749         
1750         UI_view2d_region_to_view(&ar->v2d, x_start, y_start, &x1f, &y1f);
1751         UI_view2d_region_to_view(&ar->v2d, x_end, y_end, &x2f, &y2f);
1752         x1= 0.5f+ x1f*ibuf->x;
1753         x2= 0.5f+ x2f*ibuf->x;
1754         y1= 0.5f+ y1f*ibuf->y;
1755         y2= 0.5f+ y2f*ibuf->y;
1756         
1757         hist->channels = 3;
1758         hist->x_resolution = 256;
1759         hist->xmax = 1.0f;
1760         hist->ymax = 1.0f;
1761         
1762         for (i=0; i<256; i++) {
1763                 x= (int)(0.5f + x1 + (float)i*(x2-x1)/255.0f);
1764                 y= (int)(0.5f + y1 + (float)i*(y2-y1)/255.0f);
1765                 
1766                 if (x<0 || y<0 || x>=ibuf->x || y>=ibuf->y) {
1767                         hist->data_luma[i] = hist->data_r[i] = hist->data_g[i]= hist->data_b[i] = 0.0f;
1768                 } else {
1769                         if (ibuf->rect_float) {
1770                                 fp= (ibuf->rect_float + (ibuf->channels)*(y*ibuf->x + x));
1771
1772                                 if (scene->r.color_mgt_flag & R_COLOR_MANAGEMENT)
1773                                         linearrgb_to_srgb_v3_v3(rgb, fp);
1774                                 else
1775                                         copy_v3_v3(rgb, fp);
1776
1777                                 hist->data_r[i] = rgb[0];
1778                                 hist->data_g[i] = rgb[1];
1779                                 hist->data_b[i] = rgb[2];
1780                                 hist->data_luma[i] = (0.299f*rgb[0] + 0.587f*rgb[1] + 0.114f*rgb[2]);
1781                         }
1782                         else if (ibuf->rect) {
1783                                 cp= (unsigned char *)(ibuf->rect + y*ibuf->x + x);
1784                                 hist->data_r[i] = (float)cp[0]/255.0f;
1785                                 hist->data_g[i] = (float)cp[1]/255.0f;
1786                                 hist->data_b[i] = (float)cp[2]/255.0f;
1787                                 hist->data_luma[i] = (0.299f*cp[0] + 0.587f*cp[1] + 0.114f*cp[2])/255;
1788                         }
1789                 }
1790         }
1791         
1792         ED_space_image_release_buffer(sima, lock);
1793         
1794         ED_area_tag_redraw(CTX_wm_area(C));
1795         
1796         return OPERATOR_FINISHED;
1797 }
1798
1799 static int sample_line_invoke(bContext *C, wmOperator *op, wmEvent *event)
1800 {
1801         SpaceImage *sima= CTX_wm_space_image(C);
1802         
1803         if(!ED_space_image_has_buffer(sima))
1804                 return OPERATOR_CANCELLED;
1805         
1806         return WM_gesture_straightline_invoke(C, op, event);
1807 }
1808
1809 void IMAGE_OT_sample_line(wmOperatorType *ot)
1810 {
1811         /* identifiers */
1812         ot->name= "Sample Line";
1813         ot->idname= "IMAGE_OT_sample_line";
1814         
1815         /* api callbacks */
1816         ot->invoke= sample_line_invoke;
1817         ot->modal= WM_gesture_straightline_modal;
1818         ot->exec= sample_line_exec;
1819         ot->poll= space_image_main_area_poll;
1820         
1821         /* flags */
1822         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1823         
1824         WM_operator_properties_gesture_straightline(ot, CURSOR_EDIT);
1825 }
1826
1827 /******************** set curve point operator ********************/
1828
1829 void IMAGE_OT_curves_point_set(wmOperatorType *ot)
1830 {
1831         static EnumPropertyItem point_items[]= {
1832                 {0, "BLACK_POINT", 0, "Black Point", ""},
1833                 {1, "WHITE_POINT", 0, "White Point", ""},
1834                 {0, NULL, 0, NULL, NULL}};
1835
1836         /* identifiers */
1837         ot->name= "Set Curves Point";
1838         ot->idname= "IMAGE_OT_curves_point_set";
1839
1840         /* flags */
1841         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1842         
1843         /* api callbacks */
1844         ot->invoke= sample_invoke;
1845         ot->modal= sample_modal;
1846         ot->cancel= sample_cancel;
1847         ot->poll= space_image_main_area_poll;
1848
1849         /* properties */
1850         RNA_def_enum(ot->srna, "point", point_items, 0, "Point", "Set black point or white point for curves.");
1851 }
1852
1853 /******************** record composite operator *********************/
1854
1855 typedef struct RecordCompositeData {
1856         wmTimer *timer;
1857         int old_cfra;
1858         int sfra, efra;
1859 } RecordCompositeData;
1860
1861 static int record_composite_apply(bContext *C, wmOperator *op)
1862 {
1863         SpaceImage *sima= CTX_wm_space_image(C);
1864         RecordCompositeData *rcd= op->customdata;
1865         Scene *scene= CTX_data_scene(C);
1866         ImBuf *ibuf;
1867         
1868         WM_timecursor(CTX_wm_window(C), scene->r.cfra);
1869
1870         // XXX scene->nodetree->test_break= blender_test_break;
1871         // XXX scene->nodetree->test_break= NULL;
1872         
1873         BKE_image_all_free_anim_ibufs(scene->r.cfra);
1874         ntreeCompositTagAnimated(scene->nodetree);
1875         ntreeCompositExecTree(scene->nodetree, &scene->r, scene->r.cfra != rcd->old_cfra);      /* 1 is no previews */
1876
1877         ED_area_tag_redraw(CTX_wm_area(C));
1878         
1879         ibuf= BKE_image_get_ibuf(sima->image, &sima->iuser);
1880         /* save memory in flipbooks */
1881         if(ibuf)
1882                 imb_freerectfloatImBuf(ibuf);
1883         
1884         scene->r.cfra++;
1885
1886         return (scene->r.cfra <= rcd->efra);
1887 }
1888
1889 static int record_composite_init(bContext *C, wmOperator *op)
1890 {
1891         SpaceImage *sima= CTX_wm_space_image(C);
1892         Scene *scene= CTX_data_scene(C);
1893         RecordCompositeData *rcd;
1894
1895         if(sima->iuser.frames < 2)
1896                 return 0;
1897         if(scene->nodetree == NULL)
1898                 return 0;
1899         
1900         op->customdata= rcd= MEM_callocN(sizeof(RecordCompositeData), "ImageRecordCompositeData");
1901
1902         rcd->old_cfra= scene->r.cfra;
1903         rcd->sfra= sima->iuser.sfra;
1904         rcd->efra= sima->iuser.sfra + sima->iuser.frames-1;
1905         scene->r.cfra= rcd->sfra;
1906
1907         return 1;
1908 }
1909
1910 static void record_composite_exit(bContext *C, wmOperator *op)
1911 {
1912         Scene *scene= CTX_data_scene(C);
1913         SpaceImage *sima= CTX_wm_space_image(C);
1914         RecordCompositeData *rcd= op->customdata;
1915
1916         scene->r.cfra= rcd->old_cfra;
1917
1918         WM_cursor_restore(CTX_wm_window(C));
1919
1920         if(rcd->timer)
1921                 WM_event_remove_timer(CTX_wm_manager(C), CTX_wm_window(C), rcd->timer);
1922
1923         WM_event_add_notifier(C, NC_IMAGE|NA_EDITED, sima->image);
1924
1925         // XXX play_anim(0);
1926         // XXX allqueue(REDRAWNODE, 1);
1927
1928         MEM_freeN(rcd);
1929 }
1930
1931 static int record_composite_exec(bContext *C, wmOperator *op)
1932 {
1933         if(!record_composite_init(C, op))
1934                 return OPERATOR_CANCELLED;
1935         
1936         while(record_composite_apply(C, op))
1937                 ;
1938         
1939         record_composite_exit(C, op);
1940         
1941         return OPERATOR_FINISHED;
1942 }
1943
1944 static int record_composite_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
1945 {
1946         RecordCompositeData *rcd;
1947         
1948         if(!record_composite_init(C, op))
1949                 return OPERATOR_CANCELLED;
1950
1951         rcd= op->customdata;
1952         rcd->timer= WM_event_add_timer(CTX_wm_manager(C), CTX_wm_window(C), TIMER, 0.0f);
1953         WM_event_add_modal_handler(C, op);
1954
1955         if(!record_composite_apply(C, op))
1956                 return OPERATOR_FINISHED;
1957
1958         return OPERATOR_RUNNING_MODAL;
1959 }
1960
1961 static int record_composite_modal(bContext *C, wmOperator *op, wmEvent *event)
1962 {
1963         RecordCompositeData *rcd= op->customdata;
1964
1965         switch(event->type) {
1966                 case TIMER:
1967                         if(rcd->timer == event->customdata) {
1968                                 if(!record_composite_apply(C, op)) {
1969                                         record_composite_exit(C, op);
1970                                         return OPERATOR_FINISHED;
1971                                 }
1972                         }
1973                         break;
1974                 case ESCKEY:
1975                         record_composite_exit(C, op);
1976                         return OPERATOR_FINISHED;
1977         }
1978
1979         return OPERATOR_RUNNING_MODAL;
1980 }
1981
1982 static int record_composite_cancel(bContext *C, wmOperator *op)
1983 {
1984         record_composite_exit(C, op);
1985         return OPERATOR_CANCELLED;
1986 }
1987
1988 void IMAGE_OT_record_composite(wmOperatorType *ot)
1989 {
1990         /* identifiers */
1991         ot->name= "Record Composite";
1992         ot->idname= "IMAGE_OT_record_composite";
1993         
1994         /* api callbacks */
1995         ot->exec= record_composite_exec;
1996         ot->invoke= record_composite_invoke;
1997         ot->modal= record_composite_modal;
1998         ot->cancel= record_composite_cancel;
1999         ot->poll= space_image_buffer_exists_poll;
2000 }
2001
2002 /********************* cycle render slot operator *********************/
2003
2004 static int cycle_render_slot_poll(bContext *C)
2005 {
2006         Image *ima= CTX_data_edit_image(C);
2007
2008         return (ima && ima->type == IMA_TYPE_R_RESULT);
2009 }
2010
2011 static int cycle_render_slot_exec(bContext *C, wmOperator *op)
2012 {
2013         Image *ima= CTX_data_edit_image(C);
2014         int a, slot, cur= ima->render_slot;
2015         const short use_reverse= RNA_boolean_get(op->ptr, "reverse");
2016
2017         for(a=1; a<IMA_MAX_RENDER_SLOT; a++) {
2018                 slot= (cur + (use_reverse ? -a:a))%IMA_MAX_RENDER_SLOT;
2019                 if(slot<0) slot+=IMA_MAX_RENDER_SLOT;
2020
2021                 if(ima->renders[slot] || slot == ima->last_render_slot) {
2022                         ima->render_slot= slot;
2023                         break;
2024                 }
2025                 else if((slot - 1) == ima->last_render_slot && slot < IMA_MAX_RENDER_SLOT) {
2026                         ima->render_slot= slot;
2027                         break;
2028                 }
2029         }
2030
2031         if(a == IMA_MAX_RENDER_SLOT)
2032                 ima->render_slot= ((cur == 1)? 0: 1);
2033         
2034         WM_event_add_notifier(C, NC_IMAGE|ND_DRAW, NULL);
2035
2036         return OPERATOR_FINISHED;
2037 }
2038
2039 void IMAGE_OT_cycle_render_slot(wmOperatorType *ot)
2040 {
2041         /* identifiers */
2042         ot->name= "Cycle Render Slot";
2043         ot->idname= "IMAGE_OT_cycle_render_slot";
2044         
2045         /* api callbacks */
2046         ot->exec= cycle_render_slot_exec;
2047         ot->poll= cycle_render_slot_poll;
2048
2049         /* flags */
2050         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2051
2052         RNA_def_boolean(ot->srna, "reverse", 0, "Cycle in Reverse", "");
2053 }
2054
2055 /******************** TODO ********************/
2056
2057 /* XXX notifier? */
2058
2059 /* goes over all ImageUsers, and sets frame numbers if auto-refresh is set */
2060
2061 void ED_image_update_frame(const Main *mainp, int cfra)
2062 {
2063         wmWindowManager *wm;
2064         wmWindow *win;
2065         Tex *tex;
2066         
2067         /* texture users */
2068         for(tex= mainp->tex.first; tex; tex= tex->id.next) {
2069                 if(tex->type==TEX_IMAGE && tex->ima) {
2070                         if(ELEM(tex->ima->source, IMA_SRC_MOVIE, IMA_SRC_SEQUENCE)) {
2071                                 if(tex->iuser.flag & IMA_ANIM_ALWAYS)
2072                                         BKE_image_user_calc_frame(&tex->iuser, cfra, 0);
2073                         }
2074                 }
2075         }
2076         
2077         /* image window, compo node users */
2078         for(wm=mainp->wm.first; wm; wm= wm->id.next) { /* only 1 wm */
2079                 for(win= wm->windows.first; win; win= win->next) {
2080                         ScrArea *sa;
2081                         for(sa= win->screen->areabase.first; sa; sa= sa->next) {
2082                                 if(sa->spacetype==SPACE_VIEW3D) {
2083                                         View3D *v3d= sa->spacedata.first;
2084                                         BGpic *bgpic;
2085                                         for(bgpic= v3d->bgpicbase.first; bgpic; bgpic= bgpic->next)
2086                                                 if(bgpic->iuser.flag & IMA_ANIM_ALWAYS)
2087                                                         BKE_image_user_calc_frame(&bgpic->iuser, cfra, 0);
2088                                 }
2089                                 else if(sa->spacetype==SPACE_IMAGE) {
2090                                         SpaceImage *sima= sa->spacedata.first;
2091                                         if(sima->iuser.flag & IMA_ANIM_ALWAYS)
2092                                                 BKE_image_user_calc_frame(&sima->iuser, cfra, 0);
2093                                 }
2094                                 else if(sa->spacetype==SPACE_NODE) {
2095                                         SpaceNode *snode= sa->spacedata.first;
2096                                         if((snode->treetype==NTREE_COMPOSIT) && (snode->nodetree)) {
2097                                                 bNode *node;
2098                                                 for(node= snode->nodetree->nodes.first; node; node= node->next) {
2099                                                         if(node->id && node->type==CMP_NODE_IMAGE) {
2100                                                                 Image *ima= (Image *)node->id;
2101                                                                 ImageUser *iuser= node->storage;
2102                                                                 if(ELEM(ima->source, IMA_SRC_MOVIE, IMA_SRC_SEQUENCE))
2103                                                                         if(iuser->flag & IMA_ANIM_ALWAYS)
2104                                                                                 BKE_image_user_calc_frame(iuser, cfra, 0);
2105                                                         }
2106                                                 }
2107                                         }
2108                                 }
2109                         }
2110                 }
2111         }
2112 }
2113