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