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