misc small changes.
[blender.git] / source / blender / editors / space_image / image_ops.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * Contributor(s): Blender Foundation, 2002-2009, Xavier Thomas
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 #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
892                 if(scene->r.scemode & R_EXTENSION)  {
893                         BKE_add_image_extension(path, sima->imtypenr);
894                 }
895                 
896                 if(sima->imtypenr==R_MULTILAYER) {
897                         RenderResult *rr= BKE_image_acquire_renderresult(scene, ima);
898                         if(rr) {
899                                 RE_WriteRenderResult(rr, path, scene->r.quality);
900                                 ok= TRUE;
901                         }
902                         else {
903                                 BKE_report(op->reports, RPT_ERROR, "Did not write, no Multilayer Image");
904                         }
905                         BKE_image_release_renderresult(scene, ima);
906                 }
907                 else if (BKE_write_ibuf(scene, ibuf, path, sima->imtypenr, scene->r.subimtype, scene->r.quality)) {
908                         ok= TRUE;
909                 }
910
911                 if(ok)  {
912                         if(relative)
913                                 BLI_path_rel(path, bmain->name); /* only after saving */
914
915                         if(ibuf->name[0]==0) {
916                                 BLI_strncpy(ibuf->name, path, sizeof(ibuf->name));
917                                 BLI_strncpy(ima->name, path, sizeof(ima->name));
918                         }
919
920                         if(!save_copy) {
921                                 if(do_newpath) {
922                                         BLI_strncpy(ima->name, path, sizeof(ima->name));
923                                         BLI_strncpy(ibuf->name, path, sizeof(ibuf->name));
924                                 }
925
926                                 ibuf->userflags &= ~IB_BITMAPDIRTY;
927
928                                 /* change type? */
929                                 if(ima->type==IMA_TYPE_R_RESULT) {
930                                         ima->type= IMA_TYPE_IMAGE;
931
932                                         /* workaround to ensure the render result buffer is no longer used
933                                          * by this image, otherwise can crash when a new render result is
934                                          * created. */
935                                         if(ibuf->rect && !(ibuf->mall & IB_rect))
936                                                 imb_freerectImBuf(ibuf);
937                                         if(ibuf->rect_float && !(ibuf->mall & IB_rectfloat))
938                                                 imb_freerectfloatImBuf(ibuf);
939                                         if(ibuf->zbuf && !(ibuf->mall & IB_zbuf))
940                                                 IMB_freezbufImBuf(ibuf);
941                                         if(ibuf->zbuf_float && !(ibuf->mall & IB_zbuffloat))
942                                                 IMB_freezbuffloatImBuf(ibuf);
943                                 }
944                                 if( ELEM(ima->source, IMA_SRC_GENERATED, IMA_SRC_VIEWER)) {
945                                         ima->source= IMA_SRC_FILE;
946                                         ima->type= IMA_TYPE_IMAGE;
947                                 }
948                         }
949                 }
950                 else {
951                         BKE_reportf(op->reports, RPT_ERROR, "Couldn't write image: %s", path);
952                 }
953
954
955                 WM_event_add_notifier(C, NC_IMAGE|NA_EDITED, sima->image);
956
957                 WM_cursor_wait(0);
958         }
959
960         ED_space_image_release_buffer(sima, lock);
961 }
962
963 static int save_as_exec(bContext *C, wmOperator *op)
964 {
965         SpaceImage *sima= CTX_wm_space_image(C);
966         Scene *scene= CTX_data_scene(C);
967         Image *ima = ED_space_image(sima);
968         char str[FILE_MAX];
969
970         if(!ima)
971                 return OPERATOR_CANCELLED;
972
973         sima->imtypenr= RNA_enum_get(op->ptr, "file_type");
974         RNA_string_get(op->ptr, "filepath", str);
975
976         save_image_doit(C, sima, scene, op, str, TRUE);
977
978         return OPERATOR_FINISHED;
979 }
980
981
982 static int save_as_check(bContext *UNUSED(C), wmOperator *op)
983 {
984         char filepath[FILE_MAX];
985         RNA_string_get(op->ptr, "filepath", filepath);
986         if(BKE_add_image_extension(filepath, RNA_enum_get(op->ptr, "file_type"))) {
987                 RNA_string_set(op->ptr, "filepath", filepath);
988                 return TRUE;
989         }
990         return FALSE;
991 }
992
993 static int save_as_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
994 {
995         SpaceImage *sima= CTX_wm_space_image(C);
996         Image *ima = ED_space_image(sima);
997         Scene *scene= CTX_data_scene(C);
998         ImBuf *ibuf;
999         char filename[FILE_MAX];
1000         
1001         void *lock;
1002
1003         if(!RNA_property_is_set(op->ptr, "relative_path"))
1004                 RNA_boolean_set(op->ptr, "relative_path", U.flag & USER_RELPATHS);
1005
1006         if(RNA_property_is_set(op->ptr, "filepath"))
1007                 return save_as_exec(C, op);
1008         
1009         if(!ima)
1010                 return OPERATOR_CANCELLED;
1011
1012         /* always opens fileselect */
1013         ibuf= ED_space_image_acquire_buffer(sima, &lock);
1014
1015         if(ibuf) {
1016                 /* cant save multilayer sequence, ima->rr isn't valid for a specific frame */
1017                 if(ima->rr && !(ima->source==IMA_SRC_SEQUENCE && ima->type==IMA_TYPE_MULTILAYER))
1018                         sima->imtypenr= R_MULTILAYER;
1019                 else if(ima->type==IMA_TYPE_R_RESULT)
1020                         sima->imtypenr= scene->r.imtype;
1021                 else if (ima->source == IMA_SRC_GENERATED)
1022                         sima->imtypenr= R_PNG;
1023                 else
1024                         sima->imtypenr= BKE_ftype_to_imtype(ibuf->ftype);
1025
1026                 RNA_enum_set(op->ptr, "file_type", sima->imtypenr);
1027                 
1028                 if(ibuf->name[0]==0)
1029                         if ( (G.ima[0] == '/') && (G.ima[1] == '/') && (G.ima[2] == '\0') ) {
1030                                 BLI_strncpy(filename, "//untitled", FILE_MAX);
1031                         } else {
1032                                 BLI_strncpy(filename, G.ima, FILE_MAX);
1033                         }
1034                 else
1035                         BLI_strncpy(filename, ibuf->name, FILE_MAX);
1036                 
1037                 /* enable save_copy by default for render results */
1038                 if(ELEM(ima->type, IMA_TYPE_R_RESULT, IMA_TYPE_COMPOSITE) && !RNA_property_is_set(op->ptr, "copy")) {
1039                         RNA_boolean_set(op->ptr, "copy", TRUE);
1040                 }
1041
1042                 // XXX note: we can give default menu enums to operator for this 
1043                 image_filesel(C, op, filename);
1044
1045                 ED_space_image_release_buffer(sima, lock);
1046                 
1047                 return OPERATOR_RUNNING_MODAL;
1048         }
1049
1050         ED_space_image_release_buffer(sima, lock);
1051
1052         return OPERATOR_CANCELLED;
1053 }
1054
1055 void IMAGE_OT_save_as(wmOperatorType *ot)
1056 {
1057         /* identifiers */
1058         ot->name= "Save As";
1059         ot->idname= "IMAGE_OT_save_as";
1060         
1061         /* api callbacks */
1062         ot->exec= save_as_exec;
1063         ot->check= save_as_check;
1064         ot->invoke= save_as_invoke;
1065         ot->poll= space_image_buffer_exists_poll;
1066
1067         /* flags */
1068         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1069
1070         /* properties */
1071         RNA_def_enum(ot->srna, "file_type", image_file_type_items, R_PNG, "File Type", "File type to save image as.");
1072         WM_operator_properties_filesel(ot, FOLDERFILE|IMAGEFILE|MOVIEFILE, FILE_SPECIAL, FILE_SAVE, WM_FILESEL_FILEPATH|WM_FILESEL_RELPATH);
1073
1074         RNA_def_boolean(ot->srna, "copy", 0, "Copy", "Create a new image file without modifying the current image in blender");
1075 }
1076
1077 /******************** save image operator ********************/
1078
1079 static int save_exec(bContext *C, wmOperator *op)
1080 {
1081         Main *bmain= CTX_data_main(C);
1082         SpaceImage *sima= CTX_wm_space_image(C);
1083         Image *ima = ED_space_image(sima);
1084         void *lock;
1085         ImBuf *ibuf= ED_space_image_acquire_buffer(sima, &lock);
1086         Scene *scene= CTX_data_scene(C);
1087         RenderResult *rr;
1088         char name[FILE_MAX];
1089
1090         if(!ima || !ibuf) {
1091                 ED_space_image_release_buffer(sima, lock);
1092                 return OPERATOR_CANCELLED;
1093         }
1094
1095         /* if exists, saves over without fileselect */
1096         
1097         BLI_strncpy(name, ima->name, FILE_MAX);
1098         if(name[0]==0)
1099                 BLI_strncpy(name, G.ima, FILE_MAX);
1100         else
1101                 BLI_path_abs(name, bmain->name);
1102         
1103         if(BLI_exists(name) && BLI_is_writable(name)) {
1104                 rr= BKE_image_acquire_renderresult(scene, ima);
1105
1106                 if(rr)
1107                         sima->imtypenr= R_MULTILAYER;
1108                 else 
1109                         sima->imtypenr= BKE_ftype_to_imtype(ibuf->ftype);
1110
1111                 BKE_image_release_renderresult(scene, ima);
1112                 ED_space_image_release_buffer(sima, lock);
1113                 
1114                 save_image_doit(C, sima, scene, op, name, FALSE);
1115         }
1116         else {
1117                 ED_space_image_release_buffer(sima, lock);
1118
1119                 BKE_report(op->reports, RPT_ERROR, "Can not save image.");
1120                 return OPERATOR_CANCELLED;
1121         }
1122
1123         return OPERATOR_FINISHED;
1124 }
1125
1126 void IMAGE_OT_save(wmOperatorType *ot)
1127 {
1128         /* identifiers */
1129         ot->name= "Save";
1130         ot->idname= "IMAGE_OT_save";
1131         
1132         /* api callbacks */
1133         ot->exec= save_exec;
1134         ot->poll= space_image_file_exists_poll;
1135
1136         /* flags */
1137         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1138 }
1139
1140 /******************* save sequence operator ********************/
1141
1142 static int save_sequence_exec(bContext *C, wmOperator *op)
1143 {
1144         Main *bmain= CTX_data_main(C);
1145         SpaceImage *sima= CTX_wm_space_image(C);
1146         ImBuf *ibuf;
1147         int tot= 0;
1148         char di[FILE_MAX], fi[FILE_MAX];
1149         
1150         if(sima->image==NULL)
1151                 return OPERATOR_CANCELLED;
1152
1153         if(sima->image->source!=IMA_SRC_SEQUENCE) {
1154                 BKE_report(op->reports, RPT_ERROR, "Can only save sequence on image sequences.");
1155                 return OPERATOR_CANCELLED;
1156         }
1157
1158         if(sima->image->type==IMA_TYPE_MULTILAYER) {
1159                 BKE_report(op->reports, RPT_ERROR, "Can't save multilayer sequences.");
1160                 return OPERATOR_CANCELLED;
1161         }
1162         
1163         /* get total */
1164         for(ibuf= sima->image->ibufs.first; ibuf; ibuf= ibuf->next) 
1165                 if(ibuf->userflags & IB_BITMAPDIRTY)
1166                         tot++;
1167         
1168         if(tot==0) {
1169                 BKE_report(op->reports, RPT_WARNING, "No images have been changed.");
1170                 return OPERATOR_CANCELLED;
1171         }
1172
1173         /* get a filename for menu */
1174         for(ibuf= sima->image->ibufs.first; ibuf; ibuf= ibuf->next) 
1175                 if(ibuf->userflags & IB_BITMAPDIRTY)
1176                         break;
1177         
1178         BLI_strncpy(di, ibuf->name, FILE_MAX);
1179         BLI_splitdirstring(di, fi);
1180         
1181         BKE_reportf(op->reports, RPT_INFO, "%d Image(s) will be saved in %s", tot, di);
1182
1183         for(ibuf= sima->image->ibufs.first; ibuf; ibuf= ibuf->next) {
1184                 if(ibuf->userflags & IB_BITMAPDIRTY) {
1185                         char name[FILE_MAX];
1186                         BLI_strncpy(name, ibuf->name, sizeof(name));
1187                         
1188                         BLI_path_abs(name, bmain->name);
1189
1190                         if(0 == IMB_saveiff(ibuf, name, IB_rect | IB_zbuf | IB_zbuffloat)) {
1191                                 BKE_reportf(op->reports, RPT_ERROR, "Could not write image %s.", name);
1192                                 break;
1193                         }
1194
1195                         BKE_reportf(op->reports, RPT_INFO, "Saved: %s\n", ibuf->name);
1196                         ibuf->userflags &= ~IB_BITMAPDIRTY;
1197                 }
1198         }
1199
1200         return OPERATOR_FINISHED;
1201 }
1202
1203 void IMAGE_OT_save_sequence(wmOperatorType *ot)
1204 {
1205         /* identifiers */
1206         ot->name= "Save Sequence";
1207         ot->idname= "IMAGE_OT_save_sequence";
1208         
1209         /* api callbacks */
1210         ot->exec= save_sequence_exec;
1211         ot->poll= space_image_buffer_exists_poll;
1212
1213         /* flags */
1214         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1215 }
1216
1217 /******************** reload image operator ********************/
1218
1219 static int reload_exec(bContext *C, wmOperator *UNUSED(op))
1220 {
1221         Image *ima= CTX_data_edit_image(C);
1222         SpaceImage *sima= CTX_wm_space_image(C);
1223
1224         if(!ima)
1225                 return OPERATOR_CANCELLED;
1226
1227         // XXX other users?
1228         BKE_image_signal(ima, (sima)? &sima->iuser: NULL, IMA_SIGNAL_RELOAD);
1229
1230         WM_event_add_notifier(C, NC_IMAGE|NA_EDITED, ima);
1231         
1232         return OPERATOR_FINISHED;
1233 }
1234
1235 void IMAGE_OT_reload(wmOperatorType *ot)
1236 {
1237         /* identifiers */
1238         ot->name= "Reload";
1239         ot->idname= "IMAGE_OT_reload";
1240         
1241         /* api callbacks */
1242         ot->exec= reload_exec;
1243
1244         /* flags */
1245         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1246 }
1247
1248 /********************** new image operator *********************/
1249
1250 static int image_new_exec(bContext *C, wmOperator *op)
1251 {
1252         SpaceImage *sima;
1253         Scene *scene;
1254         Object *obedit;
1255         Image *ima;
1256         PointerRNA ptr, idptr;
1257         PropertyRNA *prop;
1258         char name[22];
1259         float color[4];
1260         int width, height, floatbuf, uvtestgrid, alpha;
1261
1262         /* retrieve state */
1263         sima= CTX_wm_space_image(C);
1264         scene= (Scene*)CTX_data_scene(C);
1265         obedit= CTX_data_edit_object(C);
1266
1267         RNA_string_get(op->ptr, "name", name);
1268         width= RNA_int_get(op->ptr, "width");
1269         height= RNA_int_get(op->ptr, "height");
1270         floatbuf= RNA_boolean_get(op->ptr, "float");
1271         uvtestgrid= RNA_boolean_get(op->ptr, "uv_test_grid");
1272         RNA_float_get_array(op->ptr, "color", color);
1273         alpha= RNA_boolean_get(op->ptr, "alpha");
1274         
1275         if (!floatbuf && scene->r.color_mgt_flag & R_COLOR_MANAGEMENT)
1276                 linearrgb_to_srgb_v3_v3(color, color);
1277
1278         if(!alpha)
1279                 color[3]= 1.0f;
1280
1281         ima = BKE_add_image_size(width, height, name, alpha ? 32 : 24, floatbuf, uvtestgrid, color);
1282
1283         if(!ima)
1284                 return OPERATOR_CANCELLED;
1285
1286         /* hook into UI */
1287         uiIDContextProperty(C, &ptr, &prop);
1288
1289         if(prop) {
1290                 /* when creating new ID blocks, use is already 1, but RNA
1291                  * pointer se also increases user, so this compensates it */
1292                 ima->id.us--;
1293
1294                 RNA_id_pointer_create(&ima->id, &idptr);
1295                 RNA_property_pointer_set(&ptr, prop, idptr);
1296                 RNA_property_update(C, &ptr, prop);
1297         }
1298         else if(sima)
1299                 ED_space_image_set(C, sima, scene, obedit, ima);
1300
1301         // XXX other users?
1302         BKE_image_signal(ima, (sima)? &sima->iuser: NULL, IMA_SIGNAL_USER_NEW_IMAGE);
1303         
1304         return OPERATOR_FINISHED;
1305 }
1306
1307 /* XXX, Ton is not a fan of OK buttons but using this function to avoid undo/redo bug while in mesh-editmode, - campbell */
1308 static int image_new_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
1309 {
1310         return WM_operator_props_dialog_popup(C, op, 300, 100);
1311
1312 }
1313
1314 void IMAGE_OT_new(wmOperatorType *ot)
1315 {
1316         PropertyRNA *prop;
1317         static float default_color[4]= {0.0f, 0.0f, 0.0f, 1.0f};
1318         
1319         /* identifiers */
1320         ot->name= "New";
1321         ot->idname= "IMAGE_OT_new";
1322         
1323         /* api callbacks */
1324         ot->exec= image_new_exec;
1325         ot->invoke= image_new_invoke;
1326         
1327         /* flags */
1328         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1329
1330         /* properties */
1331         RNA_def_string(ot->srna, "name", "untitled", 21, "Name", "Image datablock name.");
1332         RNA_def_int(ot->srna, "width", 1024, 1, INT_MAX, "Width", "Image width.", 1, 16384);
1333         RNA_def_int(ot->srna, "height", 1024, 1, INT_MAX, "Height", "Image height.", 1, 16384);
1334         prop= RNA_def_float_color(ot->srna, "color", 4, NULL, 0.0f, FLT_MAX, "Color", "Default fill color.", 0.0f, 1.0f);
1335         RNA_def_property_float_array_default(prop, default_color);
1336         RNA_def_boolean(ot->srna, "alpha", 1, "Alpha", "Create an image with an alpha channel.");
1337         RNA_def_boolean(ot->srna, "uv_test_grid", 0, "UV Test Grid", "Fill the image with a grid for UV map testing.");
1338         RNA_def_boolean(ot->srna, "float", 0, "32 bit Float", "Create image with 32 bit floating point bit depth.");
1339 }
1340
1341 /********************* pack operator *********************/
1342
1343 static int pack_test(bContext *C, wmOperator *op)
1344 {
1345         Image *ima= CTX_data_edit_image(C);
1346         int as_png= RNA_boolean_get(op->ptr, "as_png");
1347
1348         if(!ima)
1349                 return 0;
1350         if(!as_png && ima->packedfile)
1351                 return 0;
1352
1353         if(ima->source==IMA_SRC_SEQUENCE || ima->source==IMA_SRC_MOVIE) {
1354                 BKE_report(op->reports, RPT_ERROR, "Packing movies or image sequences not supported.");
1355                 return 0;
1356         }
1357
1358         return 1;
1359 }
1360
1361 static int pack_exec(bContext *C, wmOperator *op)
1362 {
1363         Image *ima= CTX_data_edit_image(C);
1364         ImBuf *ibuf= BKE_image_get_ibuf(ima, NULL);
1365         int as_png= RNA_boolean_get(op->ptr, "as_png");
1366
1367         if(!pack_test(C, op))
1368                 return OPERATOR_CANCELLED;
1369         
1370         if(!as_png && (ibuf && (ibuf->userflags & IB_BITMAPDIRTY))) {
1371                 BKE_report(op->reports, RPT_ERROR, "Can't pack edited image from disk, only as internal PNG.");
1372                 return OPERATOR_CANCELLED;
1373         }
1374
1375         if(as_png)
1376                 BKE_image_memorypack(ima);
1377         else
1378                 ima->packedfile= newPackedFile(op->reports, ima->name);
1379
1380         WM_event_add_notifier(C, NC_IMAGE|NA_EDITED, ima);
1381         
1382         return OPERATOR_FINISHED;
1383 }
1384
1385 static int pack_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
1386 {
1387         Image *ima= CTX_data_edit_image(C);
1388         ImBuf *ibuf= BKE_image_get_ibuf(ima, NULL);
1389         uiPopupMenu *pup;
1390         uiLayout *layout;
1391         int as_png= RNA_boolean_get(op->ptr, "as_png");
1392
1393         if(!pack_test(C, op))
1394                 return OPERATOR_CANCELLED;
1395         
1396         if(!as_png && (ibuf && (ibuf->userflags & IB_BITMAPDIRTY))) {
1397                 pup= uiPupMenuBegin(C, "OK", ICON_QUESTION);
1398                 layout= uiPupMenuLayout(pup);
1399                 uiItemBooleanO(layout, "Can't pack edited image from disk. Pack as internal PNG?", 0, op->idname, "as_png", 1);
1400                 uiPupMenuEnd(C, pup);
1401
1402                 return OPERATOR_CANCELLED;
1403         }
1404
1405         return pack_exec(C, op);
1406 }
1407
1408 void IMAGE_OT_pack(wmOperatorType *ot)
1409 {
1410         /* identifiers */
1411         ot->name= "Pack";
1412         ot->description= "Pack an image as embedded data into the .blend file"; 
1413         ot->idname= "IMAGE_OT_pack";
1414         
1415         /* api callbacks */
1416         ot->exec= pack_exec;
1417         ot->invoke= pack_invoke;
1418
1419         /* flags */
1420         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1421
1422         /* properties */
1423         RNA_def_boolean(ot->srna, "as_png", 0, "Pack As PNG", "Pack image as lossless PNG.");
1424 }
1425
1426 /********************* unpack operator *********************/
1427
1428 void unpack_menu(bContext *C, char *opname, Image *ima, char *folder, PackedFile *pf)
1429 {
1430         PointerRNA props_ptr;
1431         uiPopupMenu *pup;
1432         uiLayout *layout;
1433         char line[FILE_MAXDIR + FILE_MAXFILE + 100];
1434         char local_name[FILE_MAXDIR + FILE_MAX], fi[FILE_MAX];
1435         char *abs_name = ima->name;
1436         
1437         strcpy(local_name, abs_name);
1438         BLI_splitdirstring(local_name, fi);
1439         sprintf(local_name, "//%s/%s", folder, fi);
1440
1441         pup= uiPupMenuBegin(C, "Unpack file", 0);
1442         layout= uiPupMenuLayout(pup);
1443
1444         uiItemEnumO(layout, opname, "Remove Pack", 0, "method", PF_REMOVE);
1445
1446         if(strcmp(abs_name, local_name)) {
1447                 switch(checkPackedFile(local_name, pf)) {
1448                         case PF_NOFILE:
1449                                 sprintf(line, "Create %s", local_name);
1450                                 props_ptr= uiItemFullO(layout, opname, line, 0, NULL, WM_OP_EXEC_DEFAULT, UI_ITEM_O_RETURN_PROPS);
1451                                 RNA_enum_set(&props_ptr, "method", PF_WRITE_LOCAL);
1452                                 RNA_string_set(&props_ptr, "image", ima->id.name+2);
1453         
1454                                 break;
1455                         case PF_EQUAL:
1456                                 sprintf(line, "Use %s (identical)", local_name);
1457                                 //uiItemEnumO(layout, opname, line, 0, "method", PF_USE_LOCAL);
1458                                 props_ptr= uiItemFullO(layout, opname, line, 0, NULL, WM_OP_EXEC_DEFAULT, UI_ITEM_O_RETURN_PROPS);
1459                                 RNA_enum_set(&props_ptr, "method", PF_USE_LOCAL);
1460                                 RNA_string_set(&props_ptr, "image", ima->id.name+2);
1461                                 
1462                                 break;
1463                         case PF_DIFFERS:
1464                                 sprintf(line, "Use %s (differs)", local_name);
1465                                 //uiItemEnumO(layout, opname, line, 0, "method", PF_USE_LOCAL);
1466                                 props_ptr= uiItemFullO(layout, opname, line, 0, NULL, WM_OP_EXEC_DEFAULT, UI_ITEM_O_RETURN_PROPS);
1467                                 RNA_enum_set(&props_ptr, "method", PF_USE_LOCAL);
1468                                 RNA_string_set(&props_ptr, "image", ima->id.name);
1469                                 
1470                                 sprintf(line, "Overwrite %s", local_name);
1471                                 //uiItemEnumO(layout, opname, line, 0, "method", PF_WRITE_LOCAL);
1472                                 props_ptr= uiItemFullO(layout, opname, line, 0, NULL, WM_OP_EXEC_DEFAULT, UI_ITEM_O_RETURN_PROPS);
1473                                 RNA_enum_set(&props_ptr, "method", PF_WRITE_LOCAL);
1474                                 RNA_string_set(&props_ptr, "image", ima->id.name+2);
1475                                 
1476                                 
1477                                 break;
1478                 }
1479         }
1480         
1481         switch(checkPackedFile(abs_name, pf)) {
1482                 case PF_NOFILE:
1483                         sprintf(line, "Create %s", abs_name);
1484                         //uiItemEnumO(layout, opname, line, 0, "method", PF_WRITE_ORIGINAL);
1485                         props_ptr= uiItemFullO(layout, opname, line, 0, NULL, WM_OP_EXEC_DEFAULT, UI_ITEM_O_RETURN_PROPS);
1486                         RNA_enum_set(&props_ptr, "method", PF_WRITE_ORIGINAL);
1487                         RNA_string_set(&props_ptr, "image", ima->id.name+2);
1488                         break;
1489                 case PF_EQUAL:
1490                         sprintf(line, "Use %s (identical)", abs_name);
1491                         //uiItemEnumO(layout, opname, line, 0, "method", PF_USE_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_USE_ORIGINAL);
1494                         RNA_string_set(&props_ptr, "image", ima->id.name+2);
1495                         break;
1496                 case PF_DIFFERS:
1497                         sprintf(line, "Use %s (differs)", local_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                         
1503                         sprintf(line, "Overwrite %s", local_name);
1504                         //uiItemEnumO(layout, opname, line, 0, "method", PF_WRITE_ORIGINAL);
1505                         props_ptr= uiItemFullO(layout, opname, line, 0, NULL, WM_OP_EXEC_DEFAULT, UI_ITEM_O_RETURN_PROPS);
1506                         RNA_enum_set(&props_ptr, "method", PF_WRITE_ORIGINAL);
1507                         RNA_string_set(&props_ptr, "image", ima->id.name+2);
1508                         break;
1509         }
1510
1511         uiPupMenuEnd(C, pup);
1512 }
1513
1514 static int unpack_exec(bContext *C, wmOperator *op)
1515 {
1516         Image *ima= CTX_data_edit_image(C);
1517         int method= RNA_enum_get(op->ptr, "method");
1518
1519         /* find the suppplied image by name */
1520         if (RNA_property_is_set(op->ptr, "image")) {
1521                 char imaname[22];
1522                 RNA_string_get(op->ptr, "image", imaname);
1523                 ima = BLI_findstring(&CTX_data_main(C)->image, imaname, offsetof(ID, name) + 2);
1524                 if (!ima) ima = CTX_data_edit_image(C);
1525         }
1526         
1527         if(!ima || !ima->packedfile)
1528                 return OPERATOR_CANCELLED;
1529
1530         if(ima->source==IMA_SRC_SEQUENCE || ima->source==IMA_SRC_MOVIE) {
1531                 BKE_report(op->reports, RPT_ERROR, "Unpacking movies or image sequences not supported.");
1532                 return OPERATOR_CANCELLED;
1533         }
1534
1535         if(G.fileflags & G_AUTOPACK)
1536                 BKE_report(op->reports, RPT_WARNING, "AutoPack is enabled, so image will be packed again on file save.");
1537                 
1538         unpackImage(op->reports, ima, method);
1539         
1540         WM_event_add_notifier(C, NC_IMAGE|NA_EDITED, ima);
1541
1542         return OPERATOR_FINISHED;
1543 }
1544
1545 static int unpack_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
1546 {
1547         Image *ima= CTX_data_edit_image(C);
1548
1549         if(RNA_property_is_set(op->ptr, "image"))
1550                 return unpack_exec(C, op);
1551                 
1552         if(!ima || !ima->packedfile)
1553                 return OPERATOR_CANCELLED;
1554
1555         if(ima->source==IMA_SRC_SEQUENCE || ima->source==IMA_SRC_MOVIE) {
1556                 BKE_report(op->reports, RPT_ERROR, "Unpacking movies or image sequences not supported.");
1557                 return OPERATOR_CANCELLED;
1558         }
1559
1560         if(G.fileflags & G_AUTOPACK)
1561                 BKE_report(op->reports, RPT_WARNING, "AutoPack is enabled, so image will be packed again on file save.");
1562         
1563         unpack_menu(C, "IMAGE_OT_unpack", ima, "textures", ima->packedfile);
1564
1565         return OPERATOR_FINISHED;
1566 }
1567
1568 void IMAGE_OT_unpack(wmOperatorType *ot)
1569 {
1570         /* identifiers */
1571         ot->name= "Unpack";
1572         ot->description= "Save an image packed in the .blend file to disk"; 
1573         ot->idname= "IMAGE_OT_unpack";
1574         
1575         /* api callbacks */
1576         ot->exec= unpack_exec;
1577         ot->invoke= unpack_invoke;
1578
1579         /* flags */
1580         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1581
1582         /* properties */
1583         RNA_def_enum(ot->srna, "method", unpack_method_items, PF_USE_LOCAL, "Method", "How to unpack.");
1584         RNA_def_string(ot->srna, "image", "", 21, "Image Name", "Image datablock name to unpack.");
1585 }
1586
1587 /******************** sample image operator ********************/
1588
1589 typedef struct ImageSampleInfo {
1590         ARegionType *art;
1591         void *draw_handle;
1592         int x, y;
1593         int channels;
1594
1595         char col[4];
1596         float colf[4];
1597         int z;
1598         float zf;
1599
1600         char *colp;
1601         float *colfp;
1602         int *zp;
1603         float *zfp;
1604
1605         int draw;
1606 } ImageSampleInfo;
1607
1608 static void sample_draw(const bContext *UNUSED(C), ARegion *ar, void *arg_info)
1609 {
1610         ImageSampleInfo *info= arg_info;
1611
1612         draw_image_info(ar, info->channels, info->x, info->y, info->colp,
1613                 info->colfp, info->zp, info->zfp);
1614 }
1615
1616 static void sample_apply(bContext *C, wmOperator *op, wmEvent *event)
1617 {
1618         SpaceImage *sima= CTX_wm_space_image(C);
1619         ARegion *ar= CTX_wm_region(C);
1620         void *lock;
1621         ImBuf *ibuf= ED_space_image_acquire_buffer(sima, &lock);
1622         ImageSampleInfo *info= op->customdata;
1623         float fx, fy;
1624         int x, y;
1625         
1626         if(ibuf == NULL) {
1627                 ED_space_image_release_buffer(sima, lock);
1628                 return;
1629         }
1630
1631         x= event->x - ar->winrct.xmin;
1632         y= event->y - ar->winrct.ymin;
1633         UI_view2d_region_to_view(&ar->v2d, x, y, &fx, &fy);
1634
1635         if(fx>=0.0 && fy>=0.0 && fx<1.0 && fy<1.0) {
1636                 float *fp;
1637                 char *cp;
1638                 int x= (int)(fx*ibuf->x), y= (int)(fy*ibuf->y);
1639
1640                 CLAMP(x, 0, ibuf->x-1);
1641                 CLAMP(y, 0, ibuf->y-1);
1642
1643                 info->x= x;
1644                 info->y= y;
1645                 info->draw= 1;
1646                 info->channels= ibuf->channels;
1647
1648                 info->colp= NULL;
1649                 info->colfp= NULL;
1650                 info->zp= NULL;
1651                 info->zfp= NULL;
1652                 
1653                 if(ibuf->rect) {
1654                         cp= (char *)(ibuf->rect + y*ibuf->x + x);
1655
1656                         info->col[0]= cp[0];
1657                         info->col[1]= cp[1];
1658                         info->col[2]= cp[2];
1659                         info->col[3]= cp[3];
1660                         info->colp= info->col;
1661
1662                         info->colf[0]= (float)cp[0]/255.0f;
1663                         info->colf[1]= (float)cp[1]/255.0f;
1664                         info->colf[2]= (float)cp[2]/255.0f;
1665                         info->colf[3]= (float)cp[3]/255.0f;
1666                         info->colfp= info->colf;
1667                 }
1668                 if(ibuf->rect_float) {
1669                         fp= (ibuf->rect_float + (ibuf->channels)*(y*ibuf->x + x));
1670
1671                         info->colf[0]= fp[0];
1672                         info->colf[1]= fp[1];
1673                         info->colf[2]= fp[2];
1674                         info->colf[3]= fp[3];
1675                         info->colfp= info->colf;
1676                 }
1677
1678                 if(ibuf->zbuf) {
1679                         info->z= ibuf->zbuf[y*ibuf->x + x];
1680                         info->zp= &info->z;
1681                 }
1682                 if(ibuf->zbuf_float) {
1683                         info->zf= ibuf->zbuf_float[y*ibuf->x + x];
1684                         info->zfp= &info->zf;
1685                 }
1686                 
1687                 if(sima->cumap && ibuf->channels==4) {
1688                         /* we reuse this callback for set curves point operators */
1689                         if(RNA_struct_find_property(op->ptr, "point")) {
1690                                 int point= RNA_enum_get(op->ptr, "point");
1691
1692                                 if(point == 1) {
1693                                         curvemapping_set_black_white(sima->cumap, NULL, info->colfp);
1694                                         if(ibuf->rect_float)
1695                                                 curvemapping_do_ibuf(sima->cumap, ibuf);
1696                                 }
1697                                 else if(point == 0) {
1698                                         curvemapping_set_black_white(sima->cumap, info->colfp, NULL);
1699                                         if(ibuf->rect_float)
1700                                                 curvemapping_do_ibuf(sima->cumap, ibuf);
1701                                 }
1702                         }
1703                 }
1704                                 
1705                 // XXX node curve integration ..
1706 #if 0
1707                 {
1708                         ScrArea *sa, *cur= curarea;
1709                         
1710                         node_curvemap_sample(fp);       /* sends global to node editor */
1711                         for(sa= G.curscreen->areabase.first; sa; sa= sa->next) {
1712                                 if(sa->spacetype==SPACE_NODE) {
1713                                         areawinset(sa->win);
1714                                         scrarea_do_windraw(sa);
1715                                 }
1716                         }
1717                         node_curvemap_sample(NULL);             /* clears global in node editor */
1718                         curarea= cur;
1719                 }
1720 #endif
1721         }
1722         else
1723                 info->draw= 0;
1724
1725         ED_space_image_release_buffer(sima, lock);
1726         ED_area_tag_redraw(CTX_wm_area(C));
1727 }
1728
1729 static void sample_exit(bContext *C, wmOperator *op)
1730 {
1731         ImageSampleInfo *info= op->customdata;
1732
1733         ED_region_draw_cb_exit(info->art, info->draw_handle);
1734         ED_area_tag_redraw(CTX_wm_area(C));
1735         MEM_freeN(info);
1736 }
1737
1738 static int sample_invoke(bContext *C, wmOperator *op, wmEvent *event)
1739 {
1740         SpaceImage *sima= CTX_wm_space_image(C);
1741         ARegion *ar= CTX_wm_region(C);
1742         ImageSampleInfo *info;
1743
1744         if(!ED_space_image_has_buffer(sima))
1745                 return OPERATOR_CANCELLED;
1746         
1747         info= MEM_callocN(sizeof(ImageSampleInfo), "ImageSampleInfo");
1748         info->art= ar->type;
1749         info->draw_handle = ED_region_draw_cb_activate(ar->type, sample_draw, info, REGION_DRAW_POST_PIXEL);
1750         op->customdata= info;
1751
1752         sample_apply(C, op, event);
1753
1754         WM_event_add_modal_handler(C, op);
1755
1756         return OPERATOR_RUNNING_MODAL;
1757 }
1758
1759 static int sample_modal(bContext *C, wmOperator *op, wmEvent *event)
1760 {
1761         switch(event->type) {
1762                 case LEFTMOUSE:
1763                 case RIGHTMOUSE: // XXX hardcoded
1764                         sample_exit(C, op);
1765                         return OPERATOR_CANCELLED;
1766                 case MOUSEMOVE:
1767                         sample_apply(C, op, event);
1768                         break;
1769         }
1770
1771         return OPERATOR_RUNNING_MODAL;
1772 }
1773
1774 static int sample_cancel(bContext *C, wmOperator *op)
1775 {
1776         sample_exit(C, op);
1777         return OPERATOR_CANCELLED;
1778 }
1779
1780 void IMAGE_OT_sample(wmOperatorType *ot)
1781 {
1782         /* identifiers */
1783         ot->name= "Sample";
1784         ot->idname= "IMAGE_OT_sample";
1785         
1786         /* api callbacks */
1787         ot->invoke= sample_invoke;
1788         ot->modal= sample_modal;
1789         ot->cancel= sample_cancel;
1790         ot->poll= space_image_main_area_poll;
1791
1792         /* flags */
1793         ot->flag= OPTYPE_BLOCKING;
1794 }
1795
1796 /******************** sample line operator ********************/
1797 static int sample_line_exec(bContext *C, wmOperator *op)
1798 {
1799         SpaceImage *sima= CTX_wm_space_image(C);
1800         ARegion *ar= CTX_wm_region(C);
1801         Scene *scene= CTX_data_scene(C);
1802         
1803         int x_start= RNA_int_get(op->ptr, "xstart");
1804         int y_start= RNA_int_get(op->ptr, "ystart");
1805         int x_end= RNA_int_get(op->ptr, "xend");
1806         int y_end= RNA_int_get(op->ptr, "yend");
1807         
1808         void *lock;
1809         ImBuf *ibuf= ED_space_image_acquire_buffer(sima, &lock);
1810         Histogram *hist= &sima->sample_line_hist;
1811         
1812         float x1f, y1f, x2f, y2f;
1813         int x1, y1, x2, y2;
1814         int i, x, y;
1815         float *fp;
1816         float rgb[3];
1817         unsigned char *cp;
1818         
1819         if (ibuf == NULL) {
1820                 ED_space_image_release_buffer(sima, lock);
1821                 return OPERATOR_CANCELLED;
1822         }
1823         /* hmmmm */
1824         if (ibuf->channels < 3) {
1825                 ED_space_image_release_buffer(sima, lock);
1826                 return OPERATOR_CANCELLED;
1827         }
1828         
1829         UI_view2d_region_to_view(&ar->v2d, x_start, y_start, &x1f, &y1f);
1830         UI_view2d_region_to_view(&ar->v2d, x_end, y_end, &x2f, &y2f);
1831         x1= 0.5f+ x1f*ibuf->x;
1832         x2= 0.5f+ x2f*ibuf->x;
1833         y1= 0.5f+ y1f*ibuf->y;
1834         y2= 0.5f+ y2f*ibuf->y;
1835         
1836         hist->channels = 3;
1837         hist->x_resolution = 256;
1838         hist->xmax = 1.0f;
1839         hist->ymax = 1.0f;
1840         
1841         for (i=0; i<256; i++) {
1842                 x= (int)(0.5f + x1 + (float)i*(x2-x1)/255.0f);
1843                 y= (int)(0.5f + y1 + (float)i*(y2-y1)/255.0f);
1844                 
1845                 if (x<0 || y<0 || x>=ibuf->x || y>=ibuf->y) {
1846                         hist->data_luma[i] = hist->data_r[i] = hist->data_g[i]= hist->data_b[i] = 0.0f;
1847                 } else {
1848                         if (ibuf->rect_float) {
1849                                 fp= (ibuf->rect_float + (ibuf->channels)*(y*ibuf->x + x));
1850
1851                                 if (scene->r.color_mgt_flag & R_COLOR_MANAGEMENT)
1852                                         linearrgb_to_srgb_v3_v3(rgb, fp);
1853                                 else
1854                                         copy_v3_v3(rgb, fp);
1855
1856                                 hist->data_r[i] = rgb[0];
1857                                 hist->data_g[i] = rgb[1];
1858                                 hist->data_b[i] = rgb[2];
1859                                 hist->data_luma[i] = (0.299f*rgb[0] + 0.587f*rgb[1] + 0.114f*rgb[2]);
1860                         }
1861                         else if (ibuf->rect) {
1862                                 cp= (unsigned char *)(ibuf->rect + y*ibuf->x + x);
1863                                 hist->data_r[i] = (float)cp[0]/255.0f;
1864                                 hist->data_g[i] = (float)cp[1]/255.0f;
1865                                 hist->data_b[i] = (float)cp[2]/255.0f;
1866                                 hist->data_luma[i] = (0.299f*cp[0] + 0.587f*cp[1] + 0.114f*cp[2])/255;
1867                         }
1868                 }
1869         }
1870         
1871         ED_space_image_release_buffer(sima, lock);
1872         
1873         ED_area_tag_redraw(CTX_wm_area(C));
1874         
1875         return OPERATOR_FINISHED;
1876 }
1877
1878 static int sample_line_invoke(bContext *C, wmOperator *op, wmEvent *event)
1879 {
1880         SpaceImage *sima= CTX_wm_space_image(C);
1881         
1882         if(!ED_space_image_has_buffer(sima))
1883                 return OPERATOR_CANCELLED;
1884         
1885         return WM_gesture_straightline_invoke(C, op, event);
1886 }
1887
1888 void IMAGE_OT_sample_line(wmOperatorType *ot)
1889 {
1890         /* identifiers */
1891         ot->name= "Sample Line";
1892         ot->idname= "IMAGE_OT_sample_line";
1893         
1894         /* api callbacks */
1895         ot->invoke= sample_line_invoke;
1896         ot->modal= WM_gesture_straightline_modal;
1897         ot->exec= sample_line_exec;
1898         ot->poll= space_image_main_area_poll;
1899         
1900         /* flags */
1901         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1902         
1903         WM_operator_properties_gesture_straightline(ot, CURSOR_EDIT);
1904 }
1905
1906 /******************** set curve point operator ********************/
1907
1908 void IMAGE_OT_curves_point_set(wmOperatorType *ot)
1909 {
1910         static EnumPropertyItem point_items[]= {
1911                 {0, "BLACK_POINT", 0, "Black Point", ""},
1912                 {1, "WHITE_POINT", 0, "White Point", ""},
1913                 {0, NULL, 0, NULL, NULL}};
1914
1915         /* identifiers */
1916         ot->name= "Set Curves Point";
1917         ot->idname= "IMAGE_OT_curves_point_set";
1918
1919         /* flags */
1920         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1921         
1922         /* api callbacks */
1923         ot->invoke= sample_invoke;
1924         ot->modal= sample_modal;
1925         ot->cancel= sample_cancel;
1926         ot->poll= space_image_main_area_poll;
1927
1928         /* properties */
1929         RNA_def_enum(ot->srna, "point", point_items, 0, "Point", "Set black point or white point for curves.");
1930 }
1931
1932 /******************** record composite operator *********************/
1933
1934 typedef struct RecordCompositeData {
1935         wmTimer *timer;
1936         int old_cfra;
1937         int sfra, efra;
1938 } RecordCompositeData;
1939
1940 int record_composite_apply(bContext *C, wmOperator *op)
1941 {
1942         SpaceImage *sima= CTX_wm_space_image(C);
1943         RecordCompositeData *rcd= op->customdata;
1944         Scene *scene= CTX_data_scene(C);
1945         ImBuf *ibuf;
1946         
1947         WM_timecursor(CTX_wm_window(C), scene->r.cfra);
1948
1949         // XXX scene->nodetree->test_break= blender_test_break;
1950         // XXX scene->nodetree->test_break= NULL;
1951         
1952         BKE_image_all_free_anim_ibufs(scene->r.cfra);
1953         ntreeCompositTagAnimated(scene->nodetree);
1954         ntreeCompositExecTree(scene->nodetree, &scene->r, scene->r.cfra != rcd->old_cfra);      /* 1 is no previews */
1955
1956         ED_area_tag_redraw(CTX_wm_area(C));
1957         
1958         ibuf= BKE_image_get_ibuf(sima->image, &sima->iuser);
1959         /* save memory in flipbooks */
1960         if(ibuf)
1961                 imb_freerectfloatImBuf(ibuf);
1962         
1963         scene->r.cfra++;
1964
1965         return (scene->r.cfra <= rcd->efra);
1966 }
1967
1968 static int record_composite_init(bContext *C, wmOperator *op)
1969 {
1970         SpaceImage *sima= CTX_wm_space_image(C);
1971         Scene *scene= CTX_data_scene(C);
1972         RecordCompositeData *rcd;
1973
1974         if(sima->iuser.frames < 2)
1975                 return 0;
1976         if(scene->nodetree == NULL)
1977                 return 0;
1978         
1979         op->customdata= rcd= MEM_callocN(sizeof(RecordCompositeData), "ImageRecordCompositeData");
1980
1981         rcd->old_cfra= scene->r.cfra;
1982         rcd->sfra= sima->iuser.sfra;
1983         rcd->efra= sima->iuser.sfra + sima->iuser.frames-1;
1984         scene->r.cfra= rcd->sfra;
1985
1986         return 1;
1987 }
1988
1989 static void record_composite_exit(bContext *C, wmOperator *op)
1990 {
1991         Scene *scene= CTX_data_scene(C);
1992         SpaceImage *sima= CTX_wm_space_image(C);
1993         RecordCompositeData *rcd= op->customdata;
1994
1995         scene->r.cfra= rcd->old_cfra;
1996
1997         WM_cursor_restore(CTX_wm_window(C));
1998
1999         if(rcd->timer)
2000                 WM_event_remove_timer(CTX_wm_manager(C), CTX_wm_window(C), rcd->timer);
2001
2002         WM_event_add_notifier(C, NC_IMAGE|NA_EDITED, sima->image);
2003
2004         // XXX play_anim(0);
2005         // XXX allqueue(REDRAWNODE, 1);
2006
2007         MEM_freeN(rcd);
2008 }
2009
2010 static int record_composite_exec(bContext *C, wmOperator *op)
2011 {
2012         if(!record_composite_init(C, op))
2013                 return OPERATOR_CANCELLED;
2014         
2015         while(record_composite_apply(C, op))
2016                 ;
2017         
2018         record_composite_exit(C, op);
2019         
2020         return OPERATOR_FINISHED;
2021 }
2022
2023 static int record_composite_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
2024 {
2025         RecordCompositeData *rcd= op->customdata;
2026         
2027         if(!record_composite_init(C, op))
2028                 return OPERATOR_CANCELLED;
2029
2030         rcd= op->customdata;
2031         rcd->timer= WM_event_add_timer(CTX_wm_manager(C), CTX_wm_window(C), TIMER, 0.0f);
2032         WM_event_add_modal_handler(C, op);
2033
2034         if(!record_composite_apply(C, op))
2035                 return OPERATOR_FINISHED;
2036
2037         return OPERATOR_RUNNING_MODAL;
2038 }
2039
2040 static int record_composite_modal(bContext *C, wmOperator *op, wmEvent *event)
2041 {
2042         RecordCompositeData *rcd= op->customdata;
2043
2044         switch(event->type) {
2045                 case TIMER:
2046                         if(rcd->timer == event->customdata) {
2047                                 if(!record_composite_apply(C, op)) {
2048                                         record_composite_exit(C, op);
2049                                         return OPERATOR_FINISHED;
2050                                 }
2051                         }
2052                         break;
2053                 case ESCKEY:
2054                         record_composite_exit(C, op);
2055                         return OPERATOR_FINISHED;
2056         }
2057
2058         return OPERATOR_RUNNING_MODAL;
2059 }
2060
2061 static int record_composite_cancel(bContext *C, wmOperator *op)
2062 {
2063         record_composite_exit(C, op);
2064         return OPERATOR_CANCELLED;
2065 }
2066
2067 void IMAGE_OT_record_composite(wmOperatorType *ot)
2068 {
2069         /* identifiers */
2070         ot->name= "Record Composite";
2071         ot->idname= "IMAGE_OT_record_composite";
2072         
2073         /* api callbacks */
2074         ot->exec= record_composite_exec;
2075         ot->invoke= record_composite_invoke;
2076         ot->modal= record_composite_modal;
2077         ot->cancel= record_composite_cancel;
2078         ot->poll= space_image_buffer_exists_poll;
2079 }
2080
2081 /********************* cycle render slot operator *********************/
2082
2083 static int cycle_render_slot_poll(bContext *C)
2084 {
2085         Image *ima= CTX_data_edit_image(C);
2086
2087         return (ima && ima->type == IMA_TYPE_R_RESULT);
2088 }
2089
2090 static int cycle_render_slot_exec(bContext *C, wmOperator *UNUSED(op))
2091 {
2092         Image *ima= CTX_data_edit_image(C);
2093         int a, slot, cur= ima->render_slot;
2094
2095         for(a=1; a<IMA_MAX_RENDER_SLOT; a++) {
2096                 slot= (cur+a)%IMA_MAX_RENDER_SLOT;
2097
2098                 if(ima->renders[slot] || slot == ima->last_render_slot) {
2099                         ima->render_slot= slot;
2100                         break;
2101                 }
2102                 else if((slot - 1) == ima->last_render_slot && slot < IMA_MAX_RENDER_SLOT) {
2103                         ima->render_slot= slot;
2104                         break;
2105                 }
2106         }
2107
2108         if(a == IMA_MAX_RENDER_SLOT)
2109                 ima->render_slot= ((cur == 1)? 0: 1);
2110         
2111         WM_event_add_notifier(C, NC_IMAGE|ND_DRAW, NULL);
2112
2113         return OPERATOR_FINISHED;
2114 }
2115
2116 void IMAGE_OT_cycle_render_slot(wmOperatorType *ot)
2117 {
2118         /* identifiers */
2119         ot->name= "Cycle Render Slot";
2120         ot->idname= "IMAGE_OT_cycle_render_slot";
2121         
2122         /* api callbacks */
2123         ot->exec= cycle_render_slot_exec;
2124         ot->poll= cycle_render_slot_poll;
2125
2126         /* flags */
2127         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2128 }
2129
2130 /******************** TODO ********************/
2131
2132 /* XXX notifier? */
2133
2134 /* goes over all ImageUsers, and sets frame numbers if auto-refresh is set */
2135
2136 void ED_image_update_frame(const Main *mainp, int cfra)
2137 {
2138         wmWindowManager *wm;
2139         wmWindow *win;
2140         Tex *tex;
2141         
2142         /* texture users */
2143         for(tex= mainp->tex.first; tex; tex= tex->id.next) {
2144                 if(tex->type==TEX_IMAGE && tex->ima) {
2145                         if(ELEM(tex->ima->source, IMA_SRC_MOVIE, IMA_SRC_SEQUENCE)) {
2146                                 if(tex->iuser.flag & IMA_ANIM_ALWAYS)
2147                                         BKE_image_user_calc_frame(&tex->iuser, cfra, 0);
2148                         }
2149                 }
2150         }
2151         
2152         /* image window, compo node users */
2153         for(wm=mainp->wm.first; wm; wm= wm->id.next) { /* only 1 wm */
2154                 for(win= wm->windows.first; win; win= win->next) {
2155                         ScrArea *sa;
2156                         for(sa= win->screen->areabase.first; sa; sa= sa->next) {
2157                                 if(sa->spacetype==SPACE_VIEW3D) {
2158                                         View3D *v3d= sa->spacedata.first;
2159                                         BGpic *bgpic;
2160                                         for(bgpic= v3d->bgpicbase.first; bgpic; bgpic= bgpic->next)
2161                                                 if(bgpic->iuser.flag & IMA_ANIM_ALWAYS)
2162                                                         BKE_image_user_calc_frame(&bgpic->iuser, cfra, 0);
2163                                 }
2164                                 else if(sa->spacetype==SPACE_IMAGE) {
2165                                         SpaceImage *sima= sa->spacedata.first;
2166                                         if(sima->iuser.flag & IMA_ANIM_ALWAYS)
2167                                                 BKE_image_user_calc_frame(&sima->iuser, cfra, 0);
2168                                 }
2169                                 else if(sa->spacetype==SPACE_NODE) {
2170                                         SpaceNode *snode= sa->spacedata.first;
2171                                         if((snode->treetype==NTREE_COMPOSIT) && (snode->nodetree)) {
2172                                                 bNode *node;
2173                                                 for(node= snode->nodetree->nodes.first; node; node= node->next) {
2174                                                         if(node->id && node->type==CMP_NODE_IMAGE) {
2175                                                                 Image *ima= (Image *)node->id;
2176                                                                 ImageUser *iuser= node->storage;
2177                                                                 if(ELEM(ima->source, IMA_SRC_MOVIE, IMA_SRC_SEQUENCE))
2178                                                                         if(iuser->flag & IMA_ANIM_ALWAYS)
2179                                                                                 BKE_image_user_calc_frame(iuser, cfra, 0);
2180                                                         }
2181                                                 }
2182                                         }
2183                                 }
2184                         }
2185                 }
2186         }
2187 }
2188