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