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