svn merge https://svn.blender.org/svnroot/bf-blender/trunk/blender -r22704:22717
[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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, 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
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 #include <string.h>
29 #include <stdlib.h>
30
31 #include "MEM_guardedalloc.h"
32
33 #include "DNA_image_types.h"
34 #include "DNA_node_types.h"
35 #include "DNA_object_types.h"
36 #include "DNA_packedFile_types.h"
37 #include "DNA_space_types.h"
38 #include "DNA_scene_types.h"
39 #include "DNA_screen_types.h"
40 #include "DNA_userdef_types.h"
41 #include "DNA_windowmanager_types.h"
42
43 #include "BKE_colortools.h"
44 #include "BKE_context.h"
45 #include "BKE_image.h"
46 #include "BKE_global.h"
47 #include "BKE_library.h"
48 #include "BKE_node.h"
49 #include "BKE_packedFile.h"
50 #include "BKE_report.h"
51 #include "BKE_screen.h"
52
53 #include "BLI_arithb.h"
54 #include "BLI_blenlib.h"
55
56 #include "IMB_imbuf.h"
57 #include "IMB_imbuf_types.h"
58
59 #include "RE_pipeline.h"
60
61 #include "RNA_access.h"
62 #include "RNA_define.h"
63 #include "RNA_types.h"
64
65 #include "ED_image.h"
66 #include "ED_screen.h"
67 #include "ED_space_api.h"
68 #include "ED_uvedit.h"
69
70 #include "UI_interface.h"
71 #include "UI_resources.h"
72 #include "UI_view2d.h"
73
74 #include "WM_api.h"
75 #include "WM_types.h"
76
77 #include "image_intern.h"
78
79 /******************** view navigation utilities *********************/
80
81 static void sima_zoom_set(SpaceImage *sima, ARegion *ar, float zoom)
82 {
83         float oldzoom= sima->zoom;
84         int width, height;
85
86         sima->zoom= zoom;
87
88         if (sima->zoom > 0.1f && sima->zoom < 4.0f)
89                 return;
90
91         /* check zoom limits */
92         ED_space_image_size(sima, &width, &height);
93
94         width *= sima->zoom;
95         height *= sima->zoom;
96
97         if((width < 4) && (height < 4))
98                 sima->zoom= oldzoom;
99         else if((ar->winrct.xmax - ar->winrct.xmin) <= sima->zoom)
100                 sima->zoom= oldzoom;
101         else if((ar->winrct.ymax - ar->winrct.ymin) <= sima->zoom)
102                 sima->zoom= oldzoom;
103 }
104
105 static void sima_zoom_set_factor(SpaceImage *sima, ARegion *ar, float zoomfac)
106 {
107         sima_zoom_set(sima, ar, sima->zoom*zoomfac);
108 }
109
110 static int space_image_poll(bContext *C)
111 {
112         SpaceImage *sima= CTX_wm_space_image(C);
113         if(sima && sima->spacetype==SPACE_IMAGE)
114                 if(ED_space_image_buffer(sima))
115                         return 1;
116         return 0;
117 }
118
119 static int space_image_file_exists_poll(bContext *C)
120 {
121         if(space_image_poll(C)) {
122                 SpaceImage *sima= CTX_wm_space_image(C);
123                 ImBuf *ibuf= ED_space_image_buffer(sima);
124                 
125                 if(ibuf && BLI_exists(ibuf->name) && BLI_is_writable(ibuf->name))
126                         return 1;
127         }
128         return 0;
129 }
130
131
132 int space_image_main_area_poll(bContext *C)
133 {
134         SpaceImage *sima= CTX_wm_space_image(C);
135         // XXX ARegion *ar= CTX_wm_region(C);
136
137         if(sima)
138                 return 1; // XXX (ar && ar->type->regionid == RGN_TYPE_WINDOW);
139         
140         return 0;
141 }
142
143 /********************** view pan operator *********************/
144
145 typedef struct ViewPanData {
146         float x, y;
147         float xof, yof;
148 } ViewPanData;
149
150 static void view_pan_init(bContext *C, wmOperator *op, wmEvent *event)
151 {
152         SpaceImage *sima= CTX_wm_space_image(C);
153         ViewPanData *vpd;
154
155         op->customdata= vpd= MEM_callocN(sizeof(ViewPanData), "ImageViewPanData");
156         WM_cursor_modal(CTX_wm_window(C), BC_NSEW_SCROLLCURSOR);
157
158         vpd->x= event->x;
159         vpd->y= event->y;
160         vpd->xof= sima->xof;
161         vpd->yof= sima->yof;
162
163         WM_event_add_modal_handler(C, &CTX_wm_window(C)->handlers, op);
164 }
165
166 static void view_pan_exit(bContext *C, wmOperator *op, int cancel)
167 {
168         SpaceImage *sima= CTX_wm_space_image(C);
169         ViewPanData *vpd= op->customdata;
170
171         if(cancel) {
172                 sima->xof= vpd->xof;
173                 sima->yof= vpd->yof;
174                 ED_area_tag_redraw(CTX_wm_area(C));
175         }
176
177         WM_cursor_restore(CTX_wm_window(C));
178         MEM_freeN(op->customdata);
179 }
180
181 static int view_pan_exec(bContext *C, wmOperator *op)
182 {
183         SpaceImage *sima= CTX_wm_space_image(C);
184         float offset[2];
185
186         RNA_float_get_array(op->ptr, "offset", offset);
187         sima->xof += offset[0];
188         sima->yof += offset[1];
189
190         ED_area_tag_redraw(CTX_wm_area(C));
191
192         /* XXX notifier? */
193 #if 0
194         if(image_preview_active(curarea, NULL, NULL)) {
195                 /* recalculates new preview rect */
196                 scrarea_do_windraw(curarea);
197                 image_preview_event(2);
198         }
199 #endif
200         
201         return OPERATOR_FINISHED;
202 }
203
204 static int view_pan_invoke(bContext *C, wmOperator *op, wmEvent *event)
205 {
206         view_pan_init(C, op, event);
207         return OPERATOR_RUNNING_MODAL;
208 }
209
210 static int view_pan_modal(bContext *C, wmOperator *op, wmEvent *event)
211 {
212         SpaceImage *sima= CTX_wm_space_image(C);
213         ViewPanData *vpd= op->customdata;
214         float offset[2];
215
216         switch(event->type) {
217                 case MOUSEMOVE:
218                         sima->xof= vpd->xof;
219                         sima->yof= vpd->yof;
220                         offset[0]= (vpd->x - event->x)/sima->zoom;
221                         offset[1]= (vpd->y - event->y)/sima->zoom;
222                         RNA_float_set_array(op->ptr, "offset", offset);
223                         view_pan_exec(C, op);
224                         break;
225                 case MIDDLEMOUSE:
226                         if(event->val==0) {
227                                 view_pan_exit(C, op, 0);
228                                 return OPERATOR_FINISHED;
229                         }
230                         break;
231         }
232
233         return OPERATOR_RUNNING_MODAL;
234 }
235
236 static int view_pan_cancel(bContext *C, wmOperator *op)
237 {
238         view_pan_exit(C, op, 1);
239         return OPERATOR_CANCELLED;
240 }
241
242 void IMAGE_OT_view_pan(wmOperatorType *ot)
243 {
244         /* identifiers */
245         ot->name= "View Pan";
246         ot->idname= "IMAGE_OT_view_pan";
247         
248         /* api callbacks */
249         ot->exec= view_pan_exec;
250         ot->invoke= view_pan_invoke;
251         ot->modal= view_pan_modal;
252         ot->cancel= view_pan_cancel;
253         ot->poll= space_image_main_area_poll;
254
255         /* flags */
256         ot->flag= OPTYPE_BLOCKING;
257         
258         /* properties */
259         RNA_def_float_vector(ot->srna, "offset", 2, NULL, -FLT_MAX, FLT_MAX,
260                 "Offset", "Offset in floating point units, 1.0 is the width and height of the image.", -FLT_MAX, FLT_MAX);
261 }
262
263 /********************** view zoom operator *********************/
264
265 typedef struct ViewZoomData {
266         float x, y;
267         float zoom;
268 } ViewZoomData;
269
270 static void view_zoom_init(bContext *C, wmOperator *op, wmEvent *event)
271 {
272         SpaceImage *sima= CTX_wm_space_image(C);
273         ViewZoomData *vpd;
274
275         op->customdata= vpd= MEM_callocN(sizeof(ViewZoomData), "ImageViewZoomData");
276         WM_cursor_modal(CTX_wm_window(C), BC_NSEW_SCROLLCURSOR);
277
278         vpd->x= event->x;
279         vpd->y= event->y;
280         vpd->zoom= sima->zoom;
281
282         WM_event_add_modal_handler(C, &CTX_wm_window(C)->handlers, op);
283 }
284
285 static void view_zoom_exit(bContext *C, wmOperator *op, int cancel)
286 {
287         SpaceImage *sima= CTX_wm_space_image(C);
288         ViewZoomData *vpd= op->customdata;
289
290         if(cancel) {
291                 sima->zoom= vpd->zoom;
292                 ED_area_tag_redraw(CTX_wm_area(C));
293         }
294
295         WM_cursor_restore(CTX_wm_window(C));
296         MEM_freeN(op->customdata);
297 }
298
299 static int view_zoom_exec(bContext *C, wmOperator *op)
300 {
301         SpaceImage *sima= CTX_wm_space_image(C);
302         ARegion *ar= CTX_wm_region(C);
303
304         sima_zoom_set_factor(sima, ar, RNA_float_get(op->ptr, "factor"));
305
306         ED_area_tag_redraw(CTX_wm_area(C));
307
308         /* XXX notifier? */
309 #if 0
310         if(image_preview_active(curarea, NULL, NULL)) {
311                 /* recalculates new preview rect */
312                 scrarea_do_windraw(curarea);
313                 image_preview_event(2);
314         }
315 #endif
316         
317         return OPERATOR_FINISHED;
318 }
319
320 static int view_zoom_invoke(bContext *C, wmOperator *op, wmEvent *event)
321 {
322         view_zoom_init(C, op, event);
323         return OPERATOR_RUNNING_MODAL;
324 }
325
326 static int view_zoom_modal(bContext *C, wmOperator *op, wmEvent *event)
327 {
328         SpaceImage *sima= CTX_wm_space_image(C);
329         ARegion *ar= CTX_wm_region(C);
330         ViewZoomData *vpd= op->customdata;
331         float factor;
332
333         switch(event->type) {
334                 case MOUSEMOVE:
335                         factor= 1.0 + (vpd->x-event->x+vpd->y-event->y)/300.0f;
336                         RNA_float_set(op->ptr, "factor", factor);
337                         sima_zoom_set(sima, ar, vpd->zoom*factor);
338                         ED_area_tag_redraw(CTX_wm_area(C));
339                         break;
340                 case MIDDLEMOUSE:
341                         if(event->val==0) {
342                                 view_zoom_exit(C, op, 0);
343                                 return OPERATOR_FINISHED;
344                         }
345                         break;
346         }
347
348         return OPERATOR_RUNNING_MODAL;
349 }
350
351 static int view_zoom_cancel(bContext *C, wmOperator *op)
352 {
353         view_zoom_exit(C, op, 1);
354         return OPERATOR_CANCELLED;
355 }
356
357 void IMAGE_OT_view_zoom(wmOperatorType *ot)
358 {
359         /* identifiers */
360         ot->name= "View Zoom";
361         ot->idname= "IMAGE_OT_view_zoom";
362         
363         /* api callbacks */
364         ot->exec= view_zoom_exec;
365         ot->invoke= view_zoom_invoke;
366         ot->modal= view_zoom_modal;
367         ot->cancel= view_zoom_cancel;
368         ot->poll= space_image_main_area_poll;
369
370         /* flags */
371         ot->flag= OPTYPE_BLOCKING;
372         
373         /* properties */
374         RNA_def_float(ot->srna, "factor", 0.0f, 0.0f, FLT_MAX,
375                 "Factor", "Zoom factor, values higher than 1.0 zoom in, lower values zoom out.", -FLT_MAX, FLT_MAX);
376 }
377
378 /********************** view all operator *********************/
379
380 /* Updates the fields of the View2D member of the SpaceImage struct.
381  * Default behavior is to reset the position of the image and set the zoom to 1
382  * If the image will not fit within the window rectangle, the zoom is adjusted */
383
384 static int view_all_exec(bContext *C, wmOperator *op)
385 {
386         SpaceImage *sima;
387         ARegion *ar;
388         Scene *scene;
389         Object *obedit;
390         ImBuf *ibuf;
391         float aspx, aspy, zoomx, zoomy, w, h;
392         int width, height;
393
394         /* retrieve state */
395         sima= CTX_wm_space_image(C);
396         ar= CTX_wm_region(C);
397         scene= (Scene*)CTX_data_scene(C);
398         obedit= CTX_data_edit_object(C);
399
400         ibuf= ED_space_image_buffer(sima);
401         ED_space_image_size(sima, &width, &height);
402         ED_space_image_aspect(sima, &aspx, &aspy);
403
404         w= width*aspx;
405         h= height*aspy;
406         
407         /* check if the image will fit in the image with zoom==1 */
408         width = ar->winrct.xmax - ar->winrct.xmin + 1;
409         height = ar->winrct.ymax - ar->winrct.ymin + 1;
410
411         if((w >= width || h >= height) && (width > 0 && height > 0)) {
412                 /* find the zoom value that will fit the image in the image space */
413                 zoomx= width/w;
414                 zoomy= height/h;
415                 sima_zoom_set(sima, ar, 1.0f/power_of_2(1/MIN2(zoomx, zoomy)));
416         }
417         else
418                 sima_zoom_set(sima, ar, 1.0f);
419
420         sima->xof= sima->yof= 0.0f;
421
422         ED_area_tag_redraw(CTX_wm_area(C));
423         
424         return OPERATOR_FINISHED;
425 }
426
427 void IMAGE_OT_view_all(wmOperatorType *ot)
428 {
429         /* identifiers */
430         ot->name= "View All";
431         ot->idname= "IMAGE_OT_view_all";
432         
433         /* api callbacks */
434         ot->exec= view_all_exec;
435         ot->poll= space_image_main_area_poll;
436 }
437
438 /********************** view selected operator *********************/
439
440 static int view_selected_exec(bContext *C, wmOperator *op)
441 {
442         SpaceImage *sima;
443         ARegion *ar;
444         Scene *scene;
445         Object *obedit;
446         Image *ima;
447         ImBuf *ibuf;
448         float size, min[2], max[2], d[2];
449         int width, height;
450
451         /* retrieve state */
452         sima= CTX_wm_space_image(C);
453         ar= CTX_wm_region(C);
454         scene= (Scene*)CTX_data_scene(C);
455         obedit= CTX_data_edit_object(C);
456
457         ima= ED_space_image(sima);
458         ibuf= ED_space_image_buffer(sima);
459         ED_space_image_size(sima, &width, &height);
460
461         /* get bounds */
462         if(!ED_uvedit_minmax(scene, ima, obedit, min, max))
463                 return OPERATOR_CANCELLED;
464
465         /* adjust offset and zoom */
466         sima->xof= (int)(((min[0] + max[0])*0.5f - 0.5f)*width);
467         sima->yof= (int)(((min[1] + max[1])*0.5f - 0.5f)*height);
468
469         d[0]= max[0] - min[0];
470         d[1]= max[1] - min[1];
471         size= 0.5*MAX2(d[0], d[1])*MAX2(width, height)/256.0f;
472         
473         if(size<=0.01) size= 0.01;
474         sima_zoom_set(sima, ar, 0.7/size);
475
476         ED_area_tag_redraw(CTX_wm_area(C));
477         
478         return OPERATOR_FINISHED;
479 }
480
481 void IMAGE_OT_view_selected(wmOperatorType *ot)
482 {
483         /* identifiers */
484         ot->name= "View Center";
485         ot->idname= "IMAGE_OT_view_selected";
486         
487         /* api callbacks */
488         ot->exec= view_selected_exec;
489         ot->poll= ED_operator_uvedit;
490 }
491
492 /********************** view zoom in/out operator *********************/
493
494 static int view_zoom_in_exec(bContext *C, wmOperator *op)
495 {
496         SpaceImage *sima= CTX_wm_space_image(C);
497         ARegion *ar= CTX_wm_region(C);
498
499         sima_zoom_set_factor(sima, ar, 1.25f);
500
501         ED_area_tag_redraw(CTX_wm_area(C));
502         
503         return OPERATOR_FINISHED;
504 }
505
506 void IMAGE_OT_view_zoom_in(wmOperatorType *ot)
507 {
508         /* identifiers */
509         ot->name= "View Zoom In";
510         ot->idname= "IMAGE_OT_view_zoom_in";
511         
512         /* api callbacks */
513         ot->exec= view_zoom_in_exec;
514         ot->poll= space_image_main_area_poll;
515 }
516
517 static int view_zoom_out_exec(bContext *C, wmOperator *op)
518 {
519         SpaceImage *sima= CTX_wm_space_image(C);
520         ARegion *ar= CTX_wm_region(C);
521
522         sima_zoom_set_factor(sima, ar, 0.8f);
523
524         ED_area_tag_redraw(CTX_wm_area(C));
525         
526         return OPERATOR_FINISHED;
527 }
528
529 void IMAGE_OT_view_zoom_out(wmOperatorType *ot)
530 {
531         /* identifiers */
532         ot->name= "View Zoom Out";
533         ot->idname= "IMAGE_OT_view_zoom_out";
534         
535         /* api callbacks */
536         ot->exec= view_zoom_out_exec;
537         ot->poll= space_image_main_area_poll;
538 }
539
540 /********************** view zoom ratio operator *********************/
541
542 static int view_zoom_ratio_exec(bContext *C, wmOperator *op)
543 {
544         SpaceImage *sima= CTX_wm_space_image(C);
545         ARegion *ar= CTX_wm_region(C);
546
547         sima_zoom_set(sima, ar, RNA_float_get(op->ptr, "ratio"));
548         
549         /* ensure pixel exact locations for draw */
550         sima->xof= (int)sima->xof;
551         sima->yof= (int)sima->yof;
552
553         /* XXX notifier? */
554 #if 0
555         if(image_preview_active(curarea, NULL, NULL)) {
556                 /* recalculates new preview rect */
557                 scrarea_do_windraw(curarea);
558                 image_preview_event(2);
559         }
560 #endif
561
562         ED_area_tag_redraw(CTX_wm_area(C));
563         
564         return OPERATOR_FINISHED;
565 }
566
567 void IMAGE_OT_view_zoom_ratio(wmOperatorType *ot)
568 {
569         /* identifiers */
570         ot->name= "View Zoom Ratio";
571         ot->idname= "IMAGE_OT_view_zoom_ratio";
572         
573         /* api callbacks */
574         ot->exec= view_zoom_ratio_exec;
575         ot->poll= space_image_main_area_poll;
576         
577         /* properties */
578         RNA_def_float(ot->srna, "ratio", 0.0f, 0.0f, FLT_MAX,
579                 "Ratio", "Zoom ratio, 1.0 is 1:1, higher is zoomed in, lower is zoomed out.", -FLT_MAX, FLT_MAX);
580 }
581
582 /**************** load/replace/save callbacks ******************/
583
584 /* XXX make dynamic */
585 static const EnumPropertyItem image_file_type_items[] = {
586                 {R_TARGA, "TARGA", 0, "Targa", ""},
587                 {R_RAWTGA, "TARGA RAW", 0, "Targa Raw", ""},
588                 {R_PNG, "PNG", 0, "PNG", ""},
589                 {R_BMP, "BMP", 0, "BMP", ""},
590                 {R_JPEG90, "JPEG", 0, "Jpeg", ""},
591 #ifdef WITH_OPENJPEG
592                 {R_JP2, "JPEG_2000", 0, "Jpeg 2000", ""},
593 #endif
594                 {R_IRIS, "IRIS", 0, "Iris", ""},
595         //if(G.have_libtiff)
596                 {R_TIFF, "TIFF", 0, "Tiff", ""},
597                 {R_RADHDR, "RADIANCE_HDR", 0, "Radiance HDR", ""},
598                 {R_CINEON, "CINEON", 0, "Cineon", ""},
599                 {R_DPX, "DPX", 0, "DPX", ""},
600 #ifdef WITH_OPENEXR
601                 {R_OPENEXR, "OPENEXR", 0, "OpenEXR", ""},
602         /* saving sequences of multilayer won't work, they copy buffers  */
603         /*if(ima->source==IMA_SRC_SEQUENCE && ima->type==IMA_TYPE_MULTILAYER);
604         else*/
605                 {R_MULTILAYER, "MULTILAYER", 0, "MultiLayer", ""},
606 #endif  
607                 {0, NULL, 0, NULL, NULL}};
608
609 static void image_filesel(bContext *C, wmOperator *op, const char *path)
610 {
611         RNA_string_set(op->ptr, "filename", path);
612         WM_event_add_fileselect(C, op); 
613 }
614
615 /******************** open image operator ********************/
616
617 static int open_exec(bContext *C, wmOperator *op)
618 {
619         SpaceImage *sima= CTX_wm_space_image(C);
620         Scene *scene= CTX_data_scene(C);
621         Object *obedit= CTX_data_edit_object(C);
622         Image *ima= NULL;
623         char str[FILE_MAX];
624
625         RNA_string_get(op->ptr, "filename", str);
626         ima= BKE_add_image_file(str, scene->r.cfra);
627
628         if(!ima)
629                 return OPERATOR_CANCELLED;
630
631         BKE_image_signal(ima, &sima->iuser, IMA_SIGNAL_RELOAD);
632         ED_space_image_set(C, sima, scene, obedit, ima);
633
634         return OPERATOR_FINISHED;
635 }
636
637 static int open_invoke(bContext *C, wmOperator *op, wmEvent *event)
638 {
639         SpaceImage *sima= CTX_wm_space_image(C);
640         char *path= (sima->image)? sima->image->name: U.textudir;
641
642         if(RNA_property_is_set(op->ptr, "filename"))
643                 return open_exec(C, op);
644         
645         image_filesel(C, op, path);
646
647         return OPERATOR_RUNNING_MODAL;
648 }
649
650 void IMAGE_OT_open(wmOperatorType *ot)
651 {
652         /* identifiers */
653         ot->name= "Open";
654         ot->idname= "IMAGE_OT_open";
655         
656         /* api callbacks */
657         ot->exec= open_exec;
658         ot->invoke= open_invoke;
659         ot->poll= ED_operator_image_active;
660
661         /* flags */
662         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
663
664         /* properties */
665         WM_operator_properties_filesel(ot, FOLDERFILE|IMAGEFILE|MOVIEFILE);
666 }
667
668 /******************** replace image operator ********************/
669
670 static int replace_exec(bContext *C, wmOperator *op)
671 {
672         SpaceImage *sima= CTX_wm_space_image(C);
673         char str[FILE_MAX];
674
675         if(!sima->image)
676                 return OPERATOR_CANCELLED;
677         
678         RNA_string_get(op->ptr, "filename", str);
679         BLI_strncpy(sima->image->name, str, sizeof(sima->image->name)-1); /* we cant do much if the str is longer then 240 :/ */
680
681         BKE_image_signal(sima->image, &sima->iuser, IMA_SIGNAL_RELOAD);
682         WM_event_add_notifier(C, NC_IMAGE|NA_EDITED, sima->image);
683
684         return OPERATOR_FINISHED;
685 }
686
687 static int replace_invoke(bContext *C, wmOperator *op, wmEvent *event)
688 {
689         SpaceImage *sima= CTX_wm_space_image(C);
690         char *path= (sima->image)? sima->image->name: U.textudir;
691
692         if(!sima->image)
693                 return OPERATOR_CANCELLED;
694
695         if(RNA_property_is_set(op->ptr, "filename"))
696                 return replace_exec(C, op);
697
698         image_filesel(C, op, path);
699
700         return OPERATOR_RUNNING_MODAL;
701 }
702
703 void IMAGE_OT_replace(wmOperatorType *ot)
704 {
705         /* identifiers */
706         ot->name= "Replace";
707         ot->idname= "IMAGE_OT_replace";
708         
709         /* api callbacks */
710         ot->exec= replace_exec;
711         ot->invoke= replace_invoke;
712         ot->poll= space_image_poll;
713
714         /* flags */
715         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
716
717         /* properties */
718         WM_operator_properties_filesel(ot, FOLDERFILE|IMAGEFILE|MOVIEFILE);
719 }
720
721 /******************** save image as operator ********************/
722
723 /* assumes name is FILE_MAX */
724 /* ima->name and ibuf->name should end up the same */
725 static void save_image_doit(bContext *C, SpaceImage *sima, Scene *scene, wmOperator *op, char *name)
726 {
727         Image *ima= ED_space_image(sima);
728         ImBuf *ibuf= ED_space_image_buffer(sima);
729         int len;
730
731         if (ibuf) {     
732                 BLI_convertstringcode(name, G.sce);
733                 BLI_convertstringframe(name, scene->r.cfra);
734                 
735                 if(scene->r.scemode & R_EXTENSION)  {
736                         BKE_add_image_extension(scene, name, sima->imtypenr);
737                         BKE_add_image_extension(scene, name, sima->imtypenr);
738                 }
739                 
740                 /* enforce user setting for RGB or RGBA, but skip BW */
741                 if(scene->r.planes==32)
742                         ibuf->depth= 32;
743                 else if(scene->r.planes==24)
744                         ibuf->depth= 24;
745                 
746                 WM_cursor_wait(1);
747
748                 if(sima->imtypenr==R_MULTILAYER) {
749                         RenderResult *rr= BKE_image_get_renderresult(scene, ima);
750                         if(rr) {
751                                 RE_WriteRenderResult(rr, name, scene->r.quality);
752                                 
753                                 BLI_strncpy(ima->name, name, sizeof(ima->name));
754                                 BLI_strncpy(ibuf->name, name, sizeof(ibuf->name));
755                                 
756                                 /* should be function? nevertheless, saving only happens here */
757                                 for(ibuf= ima->ibufs.first; ibuf; ibuf= ibuf->next)
758                                         ibuf->userflags &= ~IB_BITMAPDIRTY;
759                                 
760                         }
761                         else
762                                 BKE_report(op->reports, RPT_ERROR, "Did not write, no Multilayer Image");
763                 }
764                 else if (BKE_write_ibuf(scene, ibuf, name, sima->imtypenr, scene->r.subimtype, scene->r.quality)) {
765                         BLI_strncpy(ima->name, name, sizeof(ima->name));
766                         BLI_strncpy(ibuf->name, name, sizeof(ibuf->name));
767                         
768                         ibuf->userflags &= ~IB_BITMAPDIRTY;
769                         
770                         /* change type? */
771                         if( ELEM(ima->source, IMA_SRC_GENERATED, IMA_SRC_VIEWER)) {
772                                 ima->source= IMA_SRC_FILE;
773                                 ima->type= IMA_TYPE_IMAGE;
774                         }
775                         if(ima->type==IMA_TYPE_R_RESULT)
776                                 ima->type= IMA_TYPE_IMAGE;
777                         
778                         /* name image as how we saved it */
779                         len= strlen(name);
780                         while (len > 0 && name[len - 1] != '/' && name[len - 1] != '\\') len--;
781                         rename_id(&ima->id, name+len);
782                 } 
783                 else
784                         BKE_reportf(op->reports, RPT_ERROR, "Couldn't write image: %s", name);
785
786                 WM_event_add_notifier(C, NC_IMAGE|NA_EDITED, sima->image);
787
788                 WM_cursor_wait(0);
789         }
790 }
791
792 static int save_as_exec(bContext *C, wmOperator *op)
793 {
794         SpaceImage *sima= CTX_wm_space_image(C);
795         Scene *scene= CTX_data_scene(C);
796         Image *ima = ED_space_image(sima);
797         char str[FILE_MAX];
798
799         if(!ima)
800                 return OPERATOR_CANCELLED;
801
802         sima->imtypenr= RNA_enum_get(op->ptr, "file_type");
803         RNA_string_get(op->ptr, "filename", str);
804
805         save_image_doit(C, sima, scene, op, str);
806
807         return OPERATOR_FINISHED;
808 }
809
810 static int save_as_invoke(bContext *C, wmOperator *op, wmEvent *event)
811 {
812         SpaceImage *sima= CTX_wm_space_image(C);
813         Image *ima = ED_space_image(sima);
814         ImBuf *ibuf= ED_space_image_buffer(sima);
815         Scene *scene= CTX_data_scene(C);
816
817         if(RNA_property_is_set(op->ptr, "filename"))
818                 return save_as_exec(C, op);
819         
820         if(!ima)
821                 return OPERATOR_CANCELLED;
822
823         /* always opens fileselect */
824         if(ibuf) {
825                 /* cant save multilayer sequence, ima->rr isn't valid for a specific frame */
826                 if(ima->rr && !(ima->source==IMA_SRC_SEQUENCE && ima->type==IMA_TYPE_MULTILAYER))
827                         sima->imtypenr= R_MULTILAYER;
828                 else if(ima->type==IMA_TYPE_R_RESULT)
829                         sima->imtypenr= scene->r.imtype;
830                 else
831                         sima->imtypenr= BKE_ftype_to_imtype(ibuf->ftype);
832
833                 RNA_enum_set(op->ptr, "file_type", sima->imtypenr);
834                 
835                 if(ibuf->name[0]==0)
836                         BLI_strncpy(ibuf->name, G.ima, FILE_MAX);
837                 
838                 // XXX note: we can give default menu enums to operator for this 
839                 image_filesel(C, op, ibuf->name);
840                 
841                 return OPERATOR_RUNNING_MODAL;
842         }
843
844         return OPERATOR_CANCELLED;
845 }
846
847 void IMAGE_OT_save_as(wmOperatorType *ot)
848 {
849         /* identifiers */
850         ot->name= "Save As";
851         ot->idname= "IMAGE_OT_save_as";
852         
853         /* api callbacks */
854         ot->exec= save_as_exec;
855         ot->invoke= save_as_invoke;
856         ot->poll= space_image_poll;
857
858         /* flags */
859         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
860
861         /* properties */
862         RNA_def_enum(ot->srna, "file_type", image_file_type_items, R_PNG, "File Type", "File type to save image as.");
863         WM_operator_properties_filesel(ot, FOLDERFILE|IMAGEFILE|MOVIEFILE);
864 }
865
866 /******************** save image operator ********************/
867
868 static int save_exec(bContext *C, wmOperator *op)
869 {
870         SpaceImage *sima= CTX_wm_space_image(C);
871         Image *ima = ED_space_image(sima);
872         ImBuf *ibuf= ED_space_image_buffer(sima);
873         Scene *scene= CTX_data_scene(C);
874         char name[FILE_MAX];
875
876         if(!ima || !ibuf)
877                 return OPERATOR_CANCELLED;
878
879         /* if exists, saves over without fileselect */
880         
881         BLI_strncpy(name, ibuf->name, FILE_MAX);
882         if(name[0]==0)
883                 BLI_strncpy(name, G.ima, FILE_MAX);
884         
885         if(BLI_exists(name) && BLI_is_writable(name)) {
886                 if(BKE_image_get_renderresult(scene, ima)) 
887                         sima->imtypenr= R_MULTILAYER;
888                 else 
889                         sima->imtypenr= BKE_ftype_to_imtype(ibuf->ftype);
890                 
891                 save_image_doit(C, sima, scene, op, name);
892         }
893         else {
894                 BKE_report(op->reports, RPT_ERROR, "Can not save image.");
895                 return OPERATOR_CANCELLED;
896         }
897
898         return OPERATOR_FINISHED;
899 }
900
901 void IMAGE_OT_save(wmOperatorType *ot)
902 {
903         /* identifiers */
904         ot->name= "Save";
905         ot->idname= "IMAGE_OT_save";
906         
907         /* api callbacks */
908         ot->exec= save_exec;
909         ot->poll= space_image_file_exists_poll;
910
911         /* flags */
912         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
913 }
914
915 /******************* save sequence operator ********************/
916
917 static int save_sequence_exec(bContext *C, wmOperator *op)
918 {
919         SpaceImage *sima= CTX_wm_space_image(C);
920         ImBuf *ibuf;
921         int tot= 0;
922         char di[FILE_MAX], fi[FILE_MAX];
923         
924         if(sima->image==NULL)
925                 return OPERATOR_CANCELLED;
926
927         if(sima->image->source!=IMA_SRC_SEQUENCE) {
928                 BKE_report(op->reports, RPT_ERROR, "Can only save sequence on image sequences.");
929                 return OPERATOR_CANCELLED;
930         }
931
932         if(sima->image->type==IMA_TYPE_MULTILAYER) {
933                 BKE_report(op->reports, RPT_ERROR, "Can't save multilayer sequences.");
934                 return OPERATOR_CANCELLED;
935         }
936         
937         /* get total */
938         for(ibuf= sima->image->ibufs.first; ibuf; ibuf= ibuf->next) 
939                 if(ibuf->userflags & IB_BITMAPDIRTY)
940                         tot++;
941         
942         if(tot==0) {
943                 BKE_report(op->reports, RPT_WARNING, "No images have been changed.");
944                 return OPERATOR_CANCELLED;
945         }
946
947         /* get a filename for menu */
948         for(ibuf= sima->image->ibufs.first; ibuf; ibuf= ibuf->next) 
949                 if(ibuf->userflags & IB_BITMAPDIRTY)
950                         break;
951         
952         BLI_strncpy(di, ibuf->name, FILE_MAX);
953         BLI_splitdirstring(di, fi);
954         
955         BKE_reportf(op->reports, RPT_INFO, "%d Image(s) will be saved in %s", tot, di);
956
957         for(ibuf= sima->image->ibufs.first; ibuf; ibuf= ibuf->next) {
958                 if(ibuf->userflags & IB_BITMAPDIRTY) {
959                         char name[FILE_MAX];
960                         BLI_strncpy(name, ibuf->name, sizeof(name));
961                         
962                         BLI_convertstringcode(name, G.sce);
963
964                         if(0 == IMB_saveiff(ibuf, name, IB_rect | IB_zbuf | IB_zbuffloat)) {
965                                 BKE_reportf(op->reports, RPT_ERROR, "Could not write image %s.", name);
966                                 break;
967                         }
968
969                         printf("Saved: %s\n", ibuf->name);
970                         ibuf->userflags &= ~IB_BITMAPDIRTY;
971                 }
972         }
973
974         return OPERATOR_FINISHED;
975 }
976
977 void IMAGE_OT_save_sequence(wmOperatorType *ot)
978 {
979         /* identifiers */
980         ot->name= "Save Sequence";
981         ot->idname= "IMAGE_OT_save_sequence";
982         
983         /* api callbacks */
984         ot->exec= save_sequence_exec;
985         ot->poll= space_image_poll;
986
987         /* flags */
988         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
989 }
990
991 /******************** reload image operator ********************/
992
993 static int reload_exec(bContext *C, wmOperator *op)
994 {
995         SpaceImage *sima;
996
997         /* retrieve state */
998         sima= CTX_wm_space_image(C);
999
1000         if(!sima->image)
1001                 return OPERATOR_CANCELLED;
1002
1003         BKE_image_signal(sima->image, &sima->iuser, IMA_SIGNAL_RELOAD);
1004         /* ED_space_image_set(C, sima, scene, obedit, NULL); - do we really need this? */
1005
1006         // XXX BIF_preview_changed(ID_TE);
1007         WM_event_add_notifier(C, NC_IMAGE|NA_EDITED, sima->image);
1008         ED_area_tag_redraw(CTX_wm_area(C));
1009         
1010         return OPERATOR_FINISHED;
1011 }
1012
1013 void IMAGE_OT_reload(wmOperatorType *ot)
1014 {
1015         /* identifiers */
1016         ot->name= "Reload";
1017         ot->idname= "IMAGE_OT_reload";
1018         
1019         /* api callbacks */
1020         ot->exec= reload_exec;
1021         ot->poll= space_image_poll;
1022
1023         /* flags */
1024         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1025 }
1026
1027 /********************** new image operator *********************/
1028
1029 static int new_exec(bContext *C, wmOperator *op)
1030 {
1031         SpaceImage *sima;
1032         Scene *scene;
1033         Object *obedit;
1034         Image *ima;
1035         char name[22];
1036         float color[4];
1037         int width, height, floatbuf, uvtestgrid;
1038
1039         /* retrieve state */
1040         sima= CTX_wm_space_image(C);
1041         scene= (Scene*)CTX_data_scene(C);
1042         obedit= CTX_data_edit_object(C);
1043
1044         RNA_string_get(op->ptr, "name", name);
1045         width= RNA_int_get(op->ptr, "width");
1046         height= RNA_int_get(op->ptr, "height");
1047         floatbuf= RNA_boolean_get(op->ptr, "float");
1048         uvtestgrid= RNA_boolean_get(op->ptr, "uv_test_grid");
1049         RNA_float_get_array(op->ptr, "color", color);
1050         color[3]= RNA_float_get(op->ptr, "alpha");
1051
1052         ima = BKE_add_image_size(width, height, name, floatbuf, uvtestgrid, color);
1053         BKE_image_signal(sima->image, &sima->iuser, IMA_SIGNAL_USER_NEW_IMAGE);
1054         ED_space_image_set(C, sima, scene, obedit, ima);
1055         
1056         return OPERATOR_FINISHED;
1057 }
1058
1059 void IMAGE_OT_new(wmOperatorType *ot)
1060 {
1061         /* identifiers */
1062         ot->name= "New";
1063         ot->idname= "IMAGE_OT_new";
1064         
1065         /* api callbacks */
1066         ot->exec= new_exec;
1067         ot->invoke= WM_operator_props_popup;
1068         ot->poll= ED_operator_image_active;
1069
1070         /* flags */
1071         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1072
1073         /* properties */
1074         RNA_def_string(ot->srna, "name", "Untitled", 21, "Name", "Image datablock name.");
1075         RNA_def_int(ot->srna, "width", 1024, 1, INT_MAX, "Width", "Image width.", 1, 16384);
1076         RNA_def_int(ot->srna, "height", 1024, 1, INT_MAX, "Height", "Image height.", 1, 16384);
1077         RNA_def_float_color(ot->srna, "color", 3, NULL, 0.0f, FLT_MAX, "Color", "Default fill color.", 0.0f, 1.0f);
1078         RNA_def_float(ot->srna, "alpha", 1.0f, 0.0f, 1.0f, "Alpha", "Default fill alpha.", 0.0f, 1.0f);
1079         RNA_def_boolean(ot->srna, "uv_test_grid", 0, "UV Test Grid", "Fill the image with a grid for UV map testing.");
1080         RNA_def_boolean(ot->srna, "float", 0, "32 bit Float", "Create image with 32 bit floating point bit depth.");
1081 }
1082
1083 /********************* pack operator *********************/
1084
1085 static int pack_test(bContext *C, wmOperator *op)
1086 {
1087         Image *ima= CTX_data_edit_image(C);
1088         int as_png= RNA_boolean_get(op->ptr, "as_png");
1089
1090         if(!ima)
1091                 return 0;
1092         if(!as_png && ima->packedfile)
1093                 return 0;
1094
1095         if(ima->source==IMA_SRC_SEQUENCE || ima->source==IMA_SRC_MOVIE) {
1096                 BKE_report(op->reports, RPT_ERROR, "Can't pack movie or image sequence.");
1097                 return 0;
1098         }
1099
1100         return 1;
1101 }
1102
1103 static int pack_exec(bContext *C, wmOperator *op)
1104 {
1105         SpaceImage *sima= CTX_wm_space_image(C);
1106         Image *ima= ED_space_image(sima);
1107         ImBuf *ibuf= ED_space_image_buffer(sima);
1108         int as_png= RNA_boolean_get(op->ptr, "as_png");
1109
1110         if(!pack_test(C, op))
1111                 return OPERATOR_CANCELLED;
1112         
1113         if(!as_png && (ibuf && (ibuf->userflags & IB_BITMAPDIRTY))) {
1114                 BKE_report(op->reports, RPT_ERROR, "Can't pack edited image from disk, only as internal PNG.");
1115                 return OPERATOR_CANCELLED;
1116         }
1117
1118         if(as_png)
1119                 BKE_image_memorypack(ima);
1120         else
1121                 ima->packedfile= newPackedFile(op->reports, ima->name);
1122
1123         return OPERATOR_FINISHED;
1124 }
1125
1126 static int pack_invoke(bContext *C, wmOperator *op, wmEvent *event)
1127 {
1128         SpaceImage *sima= CTX_wm_space_image(C);
1129         ImBuf *ibuf= ED_space_image_buffer(sima);
1130         uiPopupMenu *pup;
1131         uiLayout *layout;
1132         int as_png= RNA_boolean_get(op->ptr, "as_png");
1133
1134         if(!pack_test(C, op))
1135                 return OPERATOR_CANCELLED;
1136         
1137         if(!as_png && (ibuf && (ibuf->userflags & IB_BITMAPDIRTY))) {
1138                 pup= uiPupMenuBegin(C, "OK", ICON_QUESTION);
1139                 layout= uiPupMenuLayout(pup);
1140                 uiItemBooleanO(layout, "Can't pack edited image from disk. Pack as internal PNG?", 0, op->idname, "as_png", 1);
1141                 uiPupMenuEnd(C, pup);
1142
1143                 return OPERATOR_CANCELLED;
1144         }
1145
1146         return pack_exec(C, op);
1147 }
1148
1149 void IMAGE_OT_pack(wmOperatorType *ot)
1150 {
1151         /* identifiers */
1152         ot->name= "Pack";
1153         ot->idname= "IMAGE_OT_pack";
1154         
1155         /* api callbacks */
1156         ot->exec= pack_exec;
1157         ot->invoke= pack_invoke;
1158         ot->poll= space_image_poll;
1159
1160         /* flags */
1161         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1162
1163         /* properties */
1164         RNA_def_boolean(ot->srna, "as_png", 0, "Pack As PNG", "Pack image as lossless PNG.");
1165 }
1166
1167 /********************* unpack operator *********************/
1168
1169 /* XXX move this to some place where it can be reused */
1170
1171 const EnumPropertyItem unpack_method_items[] = {
1172         {PF_USE_LOCAL, "USE_LOCAL", 0, "Use Local File", ""},
1173         {PF_WRITE_LOCAL, "WRITE_LOCAL", 0, "Write Local File (overwrite existing)", ""},
1174         {PF_USE_ORIGINAL, "USE_ORIGINAL", 0, "Use Original File", ""},
1175         {PF_WRITE_ORIGINAL, "WRITE_ORIGINAL", 0, "Write Original File (overwrite existing)", ""},
1176         {0, NULL, 0, NULL, NULL}};
1177
1178 void unpack_menu(bContext *C, char *opname, char *abs_name, char *folder, PackedFile *pf)
1179 {
1180         uiPopupMenu *pup;
1181         uiLayout *layout;
1182         char line[FILE_MAXDIR + FILE_MAXFILE + 100];
1183         char local_name[FILE_MAXDIR + FILE_MAX], fi[FILE_MAX];
1184         
1185         strcpy(local_name, abs_name);
1186         BLI_splitdirstring(local_name, fi);
1187         sprintf(local_name, "//%s/%s", folder, fi);
1188
1189         pup= uiPupMenuBegin(C, "Unpack file", 0);
1190         layout= uiPupMenuLayout(pup);
1191
1192         uiItemEnumO(layout, "Remove Pack", 0, opname, "method", PF_REMOVE);
1193
1194         if(strcmp(abs_name, local_name)) {
1195                 switch(checkPackedFile(local_name, pf)) {
1196                         case PF_NOFILE:
1197                                 sprintf(line, "Create %s", local_name);
1198                                 uiItemEnumO(layout, line, 0, opname, "method", PF_WRITE_LOCAL);
1199                                 break;
1200                         case PF_EQUAL:
1201                                 sprintf(line, "Use %s (identical)", local_name);
1202                                 uiItemEnumO(layout, line, 0, opname, "method", PF_USE_LOCAL);
1203                                 break;
1204                         case PF_DIFFERS:
1205                                 sprintf(line, "Use %s (differs)", local_name);
1206                                 uiItemEnumO(layout, line, 0, opname, "method", PF_USE_LOCAL);
1207                                 sprintf(line, "Overwrite %s", local_name);
1208                                 uiItemEnumO(layout, line, 0, opname, "method", PF_WRITE_LOCAL);
1209                                 break;
1210                 }
1211         }
1212         
1213         switch(checkPackedFile(abs_name, pf)) {
1214                 case PF_NOFILE:
1215                         sprintf(line, "Create %s", abs_name);
1216                         uiItemEnumO(layout, line, 0, opname, "method", PF_WRITE_ORIGINAL);
1217                         break;
1218                 case PF_EQUAL:
1219                         sprintf(line, "Use %s (identical)", abs_name);
1220                         uiItemEnumO(layout, line, 0, opname, "method", PF_USE_ORIGINAL);
1221                         break;
1222                 case PF_DIFFERS:
1223                         sprintf(line, "Use %s (differs)", local_name);
1224                         uiItemEnumO(layout, line, 0, opname, "method", PF_USE_ORIGINAL);
1225                         sprintf(line, "Overwrite %s", local_name);
1226                         uiItemEnumO(layout, line, 0, opname, "method", PF_WRITE_ORIGINAL);
1227                         break;
1228         }
1229
1230         uiPupMenuEnd(C, pup);
1231 }
1232
1233 static int unpack_exec(bContext *C, wmOperator *op)
1234 {
1235         Image *ima= CTX_data_edit_image(C);
1236         int method= RNA_enum_get(op->ptr, "method");
1237
1238         if(!ima || !ima->packedfile)
1239                 return OPERATOR_CANCELLED;
1240
1241         if(ima->source==IMA_SRC_SEQUENCE || ima->source==IMA_SRC_MOVIE) {
1242                 BKE_report(op->reports, RPT_ERROR, "Can't unpack movie or image sequence.");
1243                 return OPERATOR_CANCELLED;
1244         }
1245
1246         if(G.fileflags & G_AUTOPACK)
1247                 BKE_report(op->reports, RPT_WARNING, "AutoPack is enabled, so image will be packed again on file save.");
1248         
1249         unpackImage(op->reports, ima, method);
1250
1251         return OPERATOR_FINISHED;
1252 }
1253
1254 static int unpack_invoke(bContext *C, wmOperator *op, wmEvent *event)
1255 {
1256         Image *ima= CTX_data_edit_image(C);
1257
1258         if(!ima || !ima->packedfile)
1259                 return OPERATOR_CANCELLED;
1260
1261         if(ima->source==IMA_SRC_SEQUENCE || ima->source==IMA_SRC_MOVIE) {
1262                 BKE_report(op->reports, RPT_ERROR, "Can't unpack movie or image sequence.");
1263                 return OPERATOR_CANCELLED;
1264         }
1265
1266         if(G.fileflags & G_AUTOPACK)
1267                 BKE_report(op->reports, RPT_WARNING, "AutoPack is enabled, so image will be packed again on file save.");
1268         
1269         unpack_menu(C, "IMAGE_OT_unpack", ima->name, "textures", ima->packedfile);
1270
1271         return OPERATOR_FINISHED;
1272 }
1273
1274 void IMAGE_OT_unpack(wmOperatorType *ot)
1275 {
1276         /* identifiers */
1277         ot->name= "Unpack";
1278         ot->idname= "IMAGE_OT_unpack";
1279         
1280         /* api callbacks */
1281         ot->exec= unpack_exec;
1282         ot->invoke= unpack_invoke;
1283         ot->poll= space_image_poll;
1284
1285         /* flags */
1286         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1287
1288         /* properties */
1289         RNA_def_enum(ot->srna, "method", unpack_method_items, PF_USE_LOCAL, "Method", "How to unpack.");
1290 }
1291
1292 /******************** sample image operator ********************/
1293
1294 typedef struct ImageSampleInfo {
1295         ARegionType *art;
1296         void *draw_handle;
1297         int x, y;
1298
1299         char col[4];
1300         float colf[4];
1301         int z;
1302         float zf;
1303
1304         char *colp;
1305         float *colfp;
1306         int *zp;
1307         float *zfp;
1308
1309         int draw;
1310 } ImageSampleInfo;
1311
1312 static void sample_draw(const bContext *C, ARegion *ar, void *arg_info)
1313 {
1314         SpaceImage *sima= CTX_wm_space_image(C);
1315         ImBuf *ibuf= ED_space_image_buffer(sima);
1316         ImageSampleInfo *info= arg_info;
1317
1318         if(ibuf == NULL)
1319                 return;
1320         
1321         draw_image_info(ar, ibuf->channels, info->x, info->y, info->colp,
1322                 info->colfp, info->zp, info->zfp);
1323 }
1324
1325 static void sample_apply(bContext *C, wmOperator *op, wmEvent *event)
1326 {
1327         SpaceImage *sima= CTX_wm_space_image(C);
1328         ARegion *ar= CTX_wm_region(C);
1329         ImBuf *ibuf= ED_space_image_buffer(sima);
1330         ImageSampleInfo *info= op->customdata;
1331         float fx, fy;
1332         int x, y;
1333         
1334         if(ibuf == NULL)
1335                 return;
1336
1337         x= event->x - ar->winrct.xmin;
1338         y= event->y - ar->winrct.ymin;
1339         UI_view2d_region_to_view(&ar->v2d, x, y, &fx, &fy);
1340
1341         if(fx>=0.0 && fy>=0.0 && fx<1.0 && fy<1.0) {
1342                 float *fp;
1343                 char *cp;
1344                 int x= (int)(fx*ibuf->x), y= (int)(fy*ibuf->y);
1345
1346                 CLAMP(x, 0, ibuf->x-1);
1347                 CLAMP(y, 0, ibuf->y-1);
1348
1349                 info->x= x;
1350                 info->y= y;
1351                 info->draw= 1;
1352
1353                 info->colp= NULL;
1354                 info->colfp= NULL;
1355                 info->zp= NULL;
1356                 info->zfp= NULL;
1357                 
1358                 if(ibuf->rect) {
1359                         cp= (char *)(ibuf->rect + y*ibuf->x + x);
1360
1361                         info->col[0]= cp[0];
1362                         info->col[1]= cp[1];
1363                         info->col[2]= cp[2];
1364                         info->col[3]= cp[3];
1365                         info->colp= info->col;
1366
1367                         info->colf[0]= (float)cp[0]/255.0f;
1368                         info->colf[1]= (float)cp[1]/255.0f;
1369                         info->colf[2]= (float)cp[2]/255.0f;
1370                         info->colf[3]= (float)cp[3]/255.0f;
1371                         info->colfp= info->colf;
1372                 }
1373                 if(ibuf->rect_float) {
1374                         fp= (ibuf->rect_float + (ibuf->channels)*(y*ibuf->x + x));
1375
1376                         info->colf[0]= fp[0];
1377                         info->colf[1]= fp[1];
1378                         info->colf[2]= fp[2];
1379                         info->colf[3]= fp[4];
1380                         info->colfp= info->colf;
1381                 }
1382
1383                 if(ibuf->zbuf) {
1384                         info->z= ibuf->zbuf[y*ibuf->x + x];
1385                         info->zp= &info->z;
1386                 }
1387                 if(ibuf->zbuf_float) {
1388                         info->zf= ibuf->zbuf_float[y*ibuf->x + x];
1389                         info->zfp= &info->zf;
1390                 }
1391                 
1392                 if(sima->cumap && ibuf->channels==4) {
1393                         /* we reuse this callback for set curves point operators */
1394                         if(RNA_struct_find_property(op->ptr, "point")) {
1395                                 int point= RNA_enum_get(op->ptr, "point");
1396
1397                                 if(point == 1) {
1398                                         curvemapping_set_black_white(sima->cumap, NULL, info->colfp);
1399                                         curvemapping_do_ibuf(sima->cumap, ibuf);
1400                                 }
1401                                 else if(point == 0) {
1402                                         curvemapping_set_black_white(sima->cumap, info->colfp, NULL);
1403                                         curvemapping_do_ibuf(sima->cumap, ibuf);
1404                                 }
1405                         }
1406                 }
1407                                 
1408                 // XXX node curve integration ..
1409 #if 0
1410                 {
1411                         ScrArea *sa, *cur= curarea;
1412                         
1413                         node_curvemap_sample(fp);       /* sends global to node editor */
1414                         for(sa= G.curscreen->areabase.first; sa; sa= sa->next) {
1415                                 if(sa->spacetype==SPACE_NODE) {
1416                                         areawinset(sa->win);
1417                                         scrarea_do_windraw(sa);
1418                                 }
1419                         }
1420                         node_curvemap_sample(NULL);             /* clears global in node editor */
1421                         curarea= cur;
1422                 }
1423 #endif
1424         }
1425         else
1426                 info->draw= 0;
1427
1428         ED_area_tag_redraw(CTX_wm_area(C));
1429 }
1430
1431 static void sample_exit(bContext *C, wmOperator *op)
1432 {
1433         ImageSampleInfo *info= op->customdata;
1434
1435         ED_region_draw_cb_exit(info->art, info->draw_handle);
1436         ED_area_tag_redraw(CTX_wm_area(C));
1437         MEM_freeN(info);
1438 }
1439
1440 static int sample_invoke(bContext *C, wmOperator *op, wmEvent *event)
1441 {
1442         SpaceImage *sima= CTX_wm_space_image(C);
1443         ARegion *ar= CTX_wm_region(C);
1444         ImBuf *ibuf= ED_space_image_buffer(sima);
1445         ImageSampleInfo *info;
1446
1447         if(ibuf == NULL)
1448                 return OPERATOR_CANCELLED;
1449         
1450         info= MEM_callocN(sizeof(ImageSampleInfo), "ImageSampleInfo");
1451         info->art= ar->type;
1452         info->draw_handle = ED_region_draw_cb_activate(ar->type, sample_draw, info, REGION_DRAW_POST);
1453         op->customdata= info;
1454
1455         sample_apply(C, op, event);
1456
1457         WM_event_add_modal_handler(C, &CTX_wm_window(C)->handlers, op);
1458
1459         return OPERATOR_RUNNING_MODAL;
1460 }
1461
1462 static int sample_modal(bContext *C, wmOperator *op, wmEvent *event)
1463 {
1464         switch(event->type) {
1465                 case LEFTMOUSE:
1466                 case RIGHTMOUSE: // XXX hardcoded
1467                         sample_exit(C, op);
1468                         return OPERATOR_CANCELLED;
1469                 case MOUSEMOVE:
1470                         sample_apply(C, op, event);
1471                         break;
1472         }
1473
1474         return OPERATOR_RUNNING_MODAL;
1475 }
1476
1477 static int sample_cancel(bContext *C, wmOperator *op)
1478 {
1479         sample_exit(C, op);
1480         return OPERATOR_CANCELLED;
1481 }
1482
1483 void IMAGE_OT_sample(wmOperatorType *ot)
1484 {
1485         /* identifiers */
1486         ot->name= "Sample";
1487         ot->idname= "IMAGE_OT_sample";
1488         
1489         /* api callbacks */
1490         ot->invoke= sample_invoke;
1491         ot->modal= sample_modal;
1492         ot->cancel= sample_cancel;
1493         ot->poll= space_image_main_area_poll;
1494
1495         /* flags */
1496         ot->flag= OPTYPE_BLOCKING;
1497 }
1498
1499 /******************** set curve point operator ********************/
1500
1501 void IMAGE_OT_curves_point_set(wmOperatorType *ot)
1502 {
1503         static EnumPropertyItem point_items[]= {
1504                 {0, "BLACK_POINT", 0, "Black Point", ""},
1505                 {1, "WHITE_POINT", 0, "White Point", ""},
1506                 {0, NULL, 0, NULL, NULL}};
1507
1508         /* identifiers */
1509         ot->name= "Set Curves Point";
1510         ot->idname= "IMAGE_OT_curves_point_set";
1511
1512         /* flags */
1513         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1514         
1515         /* api callbacks */
1516         ot->invoke= sample_invoke;
1517         ot->modal= sample_modal;
1518         ot->cancel= sample_cancel;
1519         ot->poll= space_image_main_area_poll;
1520
1521         /* properties */
1522         RNA_def_enum(ot->srna, "point", point_items, 0, "Point", "Set black point or white point for curves.");
1523 }
1524
1525 /******************** record composite operator *********************/
1526
1527 typedef struct RecordCompositeData {
1528         wmTimer *timer;
1529         int old_cfra;
1530         int sfra, efra;
1531 } RecordCompositeData;
1532
1533 int record_composite_apply(bContext *C, wmOperator *op)
1534 {
1535         SpaceImage *sima= CTX_wm_space_image(C);
1536         RecordCompositeData *rcd= op->customdata;
1537         Scene *scene= CTX_data_scene(C);
1538         ImBuf *ibuf;
1539         
1540         WM_timecursor(CTX_wm_window(C), scene->r.cfra);
1541
1542         // XXX scene->nodetree->test_break= blender_test_break;
1543         // XXX scene->nodetree->test_break= NULL;
1544         
1545         BKE_image_all_free_anim_ibufs(scene->r.cfra);
1546         ntreeCompositTagAnimated(scene->nodetree);
1547         ntreeCompositExecTree(scene->nodetree, &scene->r, scene->r.cfra != rcd->old_cfra);      /* 1 is no previews */
1548
1549         ED_area_tag_redraw(CTX_wm_area(C));
1550         
1551         ibuf= BKE_image_get_ibuf(sima->image, &sima->iuser);
1552         /* save memory in flipbooks */
1553         if(ibuf)
1554                 imb_freerectfloatImBuf(ibuf);
1555         
1556         scene->r.cfra++;
1557
1558         return (scene->r.cfra <= rcd->efra);
1559 }
1560
1561 static int record_composite_init(bContext *C, wmOperator *op)
1562 {
1563         SpaceImage *sima= CTX_wm_space_image(C);
1564         Scene *scene= CTX_data_scene(C);
1565         RecordCompositeData *rcd;
1566
1567         if(sima->iuser.frames < 2)
1568                 return 0;
1569         if(scene->nodetree == NULL)
1570                 return 0;
1571         
1572         op->customdata= rcd= MEM_callocN(sizeof(RecordCompositeData), "ImageRecordCompositeData");
1573
1574         rcd->old_cfra= scene->r.cfra;
1575         rcd->sfra= sima->iuser.sfra;
1576         rcd->efra= sima->iuser.sfra + sima->iuser.frames-1;
1577         scene->r.cfra= rcd->sfra;
1578
1579         return 1;
1580 }
1581
1582 static void record_composite_exit(bContext *C, wmOperator *op)
1583 {
1584         Scene *scene= CTX_data_scene(C);
1585         SpaceImage *sima= CTX_wm_space_image(C);
1586         RecordCompositeData *rcd= op->customdata;
1587
1588         scene->r.cfra= rcd->old_cfra;
1589
1590         WM_cursor_restore(CTX_wm_window(C));
1591
1592         if(rcd->timer)
1593                 WM_event_remove_window_timer(CTX_wm_window(C), rcd->timer);
1594
1595         WM_event_add_notifier(C, NC_IMAGE|NA_EDITED, sima->image);
1596
1597         // XXX play_anim(0);
1598         // XXX allqueue(REDRAWNODE, 1);
1599
1600         MEM_freeN(rcd);
1601 }
1602
1603 static int record_composite_exec(bContext *C, wmOperator *op)
1604 {
1605         if(!record_composite_init(C, op))
1606                 return OPERATOR_CANCELLED;
1607         
1608         while(record_composite_apply(C, op))
1609                 ;
1610         
1611         record_composite_exit(C, op);
1612         
1613         return OPERATOR_FINISHED;
1614 }
1615
1616 static int record_composite_invoke(bContext *C, wmOperator *op, wmEvent *event)
1617 {
1618         RecordCompositeData *rcd= op->customdata;
1619         
1620         if(!record_composite_init(C, op))
1621                 return OPERATOR_CANCELLED;
1622
1623         rcd= op->customdata;
1624         rcd->timer= WM_event_add_window_timer(CTX_wm_window(C), TIMER, 0.0f);
1625         WM_event_add_modal_handler(C, &CTX_wm_window(C)->handlers, op);
1626
1627         if(!record_composite_apply(C, op))
1628                 return OPERATOR_FINISHED;
1629
1630         return OPERATOR_RUNNING_MODAL;
1631 }
1632
1633 static int record_composite_modal(bContext *C, wmOperator *op, wmEvent *event)
1634 {
1635         RecordCompositeData *rcd= op->customdata;
1636
1637         switch(event->type) {
1638                 case TIMER:
1639                         if(rcd->timer == event->customdata) {
1640                                 if(!record_composite_apply(C, op)) {
1641                                         record_composite_exit(C, op);
1642                                         return OPERATOR_FINISHED;
1643                                 }
1644                         }
1645                         break;
1646                 case ESCKEY:
1647                         record_composite_exit(C, op);
1648                         return OPERATOR_FINISHED;
1649         }
1650
1651         return OPERATOR_RUNNING_MODAL;
1652 }
1653
1654 static int record_composite_cancel(bContext *C, wmOperator *op)
1655 {
1656         record_composite_exit(C, op);
1657         return OPERATOR_CANCELLED;
1658 }
1659
1660 void IMAGE_OT_record_composite(wmOperatorType *ot)
1661 {
1662         /* identifiers */
1663         ot->name= "Record Composite";
1664         ot->idname= "IMAGE_OT_record_composite";
1665         
1666         /* api callbacks */
1667         ot->exec= record_composite_exec;
1668         ot->invoke= record_composite_invoke;
1669         ot->modal= record_composite_modal;
1670         ot->cancel= record_composite_cancel;
1671         ot->poll= space_image_poll;
1672 }
1673
1674 /******************** TODO ********************/
1675
1676 /* XXX notifier? */
1677 #if 0
1678 /* goes over all ImageUsers, and sets frame numbers if auto-refresh is set */
1679 void BIF_image_update_frame(void)
1680 {
1681         Tex *tex;
1682         
1683         /* texture users */
1684         for(tex= G.main->tex.first; tex; tex= tex->id.next) {
1685                 if(tex->type==TEX_IMAGE && tex->ima)
1686                         if(ELEM(tex->ima->source, IMA_SRC_MOVIE, IMA_SRC_SEQUENCE))
1687                                 if(tex->iuser.flag & IMA_ANIM_ALWAYS)
1688                                         BKE_image_user_calc_imanr(&tex->iuser, scene->r.cfra, 0);
1689                 
1690         }
1691         /* image window, compo node users */
1692         if(G.curscreen) {
1693                 ScrArea *sa;
1694                 for(sa= G.curscreen->areabase.first; sa; sa= sa->next) {
1695                         if(sa->spacetype==SPACE_VIEW3D) {
1696                                 View3D *v3d= sa->spacedata.first;
1697                                 if(v3d->bgpic)
1698                                         if(v3d->bgpic->iuser.flag & IMA_ANIM_ALWAYS)
1699                                                 BKE_image_user_calc_imanr(&v3d->bgpic->iuser, scene->r.cfra, 0);
1700                         }
1701                         else if(sa->spacetype==SPACE_IMAGE) {
1702                                 SpaceImage *sima= sa->spacedata.first;
1703                                 if(sima->iuser.flag & IMA_ANIM_ALWAYS)
1704                                         BKE_image_user_calc_imanr(&sima->iuser, scene->r.cfra, 0);
1705                         }
1706                         else if(sa->spacetype==SPACE_NODE) {
1707                                 SpaceNode *snode= sa->spacedata.first;
1708                                 if((snode->treetype==NTREE_COMPOSIT) && (snode->nodetree)) {
1709                                         bNode *node;
1710                                         for(node= snode->nodetree->nodes.first; node; node= node->next) {
1711                                                 if(node->id && node->type==CMP_NODE_IMAGE) {
1712                                                         Image *ima= (Image *)node->id;
1713                                                         ImageUser *iuser= node->storage;
1714                                                         if(ELEM(ima->source, IMA_SRC_MOVIE, IMA_SRC_SEQUENCE))
1715                                                                 if(iuser->flag & IMA_ANIM_ALWAYS)
1716                                                                         BKE_image_user_calc_imanr(iuser, scene->r.cfra, 0);
1717                                                 }
1718                                         }
1719                                 }
1720                         }
1721                 }
1722         }
1723 }
1724 #endif
1725