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