Temp fix for stupid -Wformat-security issue.
[blender.git] / source / blender / editors / space_image / image_buttons.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * Contributor(s): Blender Foundation, 2002-2009
22  *
23  * ***** END GPL LICENSE BLOCK *****
24  */
25
26 /** \file blender/editors/space_image/image_buttons.c
27  *  \ingroup spimage
28  */
29
30 #include <string.h>
31 #include <stdio.h>
32
33 #include "DNA_meshdata_types.h"
34 #include "DNA_object_types.h"
35 #include "DNA_node_types.h"
36 #include "DNA_scene_types.h"
37
38 #include "MEM_guardedalloc.h"
39
40 #include "BLI_blenlib.h"
41 #include "BLI_math.h"
42 #include "BLI_rand.h"
43 #include "BLI_utildefines.h"
44
45 #include "BLF_translation.h"
46
47 #include "BKE_colortools.h"
48 #include "BKE_context.h"
49 #include "BKE_customdata.h"
50 #include "BKE_image.h"
51 #include "BKE_mesh.h"
52 #include "BKE_node.h"
53 #include "BKE_screen.h"
54
55 #include "RE_pipeline.h"
56
57 #include "IMB_imbuf.h"
58 #include "IMB_imbuf_types.h"
59
60 #include "ED_gpencil.h"
61 #include "ED_image.h"
62 #include "ED_screen.h"
63
64 #include "RNA_access.h"
65
66 #include "WM_api.h"
67 #include "WM_types.h"
68
69 #include "UI_interface.h"
70 #include "UI_resources.h"
71
72 #include "image_intern.h"
73
74 /* proto */
75
76 static void image_info(Scene *scene, ImageUser *iuser, Image *ima, ImBuf *ibuf, char *str, size_t len)
77 {
78         size_t ofs = 0;
79
80         str[0] = 0;
81         if (ima == NULL)
82                 return;
83
84         if (ibuf == NULL) {
85                 ofs += BLI_snprintf(str + ofs, len - ofs, "%s", IFACE_("Can't Load Image"));
86         }
87         else {
88                 if (ima->source == IMA_SRC_MOVIE) {
89                         ofs += BLI_snprintf(str + ofs, len - ofs, "%s", IFACE_("Movie"));
90                         if (ima->anim)
91                                 ofs += BLI_snprintf(str + ofs, len - ofs, IFACE_(" %d frs"),
92                                                     IMB_anim_get_duration(ima->anim, IMB_TC_RECORD_RUN));
93                 }
94                 else
95                         ofs += BLI_snprintf(str, len - ofs, "%s", IFACE_("Image"));
96
97                 ofs += BLI_snprintf(str + ofs, len - ofs, IFACE_(": size %d x %d,"), ibuf->x, ibuf->y);
98
99                 if (ibuf->rect_float) {
100                         if (ibuf->channels != 4) {
101                                 ofs += BLI_snprintf(str + ofs, len - ofs, IFACE_("%d float channel(s)"), ibuf->channels);
102                         }
103                         else if (ibuf->planes == R_IMF_PLANES_RGBA)
104                                 ofs += BLI_snprintf(str + ofs, len - ofs, "%s", IFACE_(" RGBA float"));
105                         else
106                                 ofs += BLI_snprintf(str + ofs, len - ofs, "%s", IFACE_(" RGB float"));
107                 }
108                 else {
109                         if (ibuf->planes == R_IMF_PLANES_RGBA)
110                                 ofs += BLI_snprintf(str + ofs, len - ofs, "%s", IFACE_(" RGBA byte"));
111                         else
112                                 ofs += BLI_snprintf(str + ofs, len - ofs, "%s", IFACE_(" RGB byte"));
113                 }
114                 if (ibuf->zbuf || ibuf->zbuf_float)
115                         ofs += BLI_snprintf(str + ofs, len - ofs, "%s", IFACE_(" + Z"));
116
117                 if (ima->source == IMA_SRC_SEQUENCE) {
118                         const char *file = BLI_last_slash(ibuf->name);
119                         if (file == NULL)
120                                 file = ibuf->name;
121                         else
122                                 file++;
123                         ofs += BLI_snprintf(str + ofs, len - ofs, ", %s", file);
124                 }
125         }
126
127         /* the frame number, even if we cant */
128         if (ima->source == IMA_SRC_SEQUENCE) {
129                 /* don't use iuser->framenr directly because it may not be updated if auto-refresh is off */
130                 const int framenr = BKE_image_user_frame_get(iuser, CFRA, 0, NULL);
131                 ofs += BLI_snprintf(str + ofs, len - ofs, IFACE_(", Frame: %d"), framenr);
132         }
133 }
134
135 /* gets active viewer user */
136 struct ImageUser *ntree_get_active_iuser(bNodeTree *ntree)
137 {
138         bNode *node;
139         
140         if (ntree)
141                 for (node = ntree->nodes.first; node; node = node->next)
142                         if (ELEM(node->type, CMP_NODE_VIEWER, CMP_NODE_SPLITVIEWER))
143                                 if (node->flag & NODE_DO_OUTPUT)
144                                         return node->storage;
145         return NULL;
146 }
147
148
149 /* ************ panel stuff ************* */
150
151 #if 0
152 /* 0: disable preview 
153  * otherwise refresh preview
154  *
155  * XXX if you put this back, also check XXX in image_main_area_draw() */
156  * /
157 void image_preview_event(int event)
158 {
159         int exec = 0;
160         
161         if (event == 0) {
162                 G.scene->r.scemode &= ~R_COMP_CROP;
163                 exec = 1;
164         }
165         else {
166                 if (image_preview_active(curarea, NULL, NULL)) {
167                         G.scene->r.scemode |= R_COMP_CROP;
168                         exec = 1;
169                 }
170                 else
171                         G.scene->r.scemode &= ~R_COMP_CROP;
172         }
173         
174         if (exec && G.scene->nodetree) {
175                 Scene *scene = G.scene;
176                 /* should work when no node editor in screen..., so we execute right away */
177                 
178                 ntreeCompositTagGenerators(G.scene->nodetree);
179
180                 G.is_break = FALSE;
181                 G.scene->nodetree->timecursor = set_timecursor;
182                 G.scene->nodetree->test_break = blender_test_break;
183                 
184                 BIF_store_spare();
185                 
186                 ntreeCompositExecTree(scene->nodetree, &scene->r, 1, &scene->view_settings, &scene->display_settings);   /* 1 is do_previews */
187                 
188                 G.scene->nodetree->timecursor = NULL;
189                 G.scene->nodetree->test_break = NULL;
190                 
191                 scrarea_do_windraw(curarea);
192                 waitcursor(0);
193                 
194                 WM_event_add_notifier(C, NC_IMAGE, ima_v);
195         }
196 }
197
198
199 /* nothing drawn here, we use it to store values */
200 static void preview_cb(ScrArea *sa, struct uiBlock *block)
201 {
202         SpaceImage *sima = sa->spacedata.first;
203         rctf dispf;
204         rcti *disprect = &G.scene->r.disprect;
205         int winx = (G.scene->r.size * G.scene->r.xsch) / 100;
206         int winy = (G.scene->r.size * G.scene->r.ysch) / 100;
207         int mval[2];
208         
209         if (G.scene->r.mode & R_BORDER) {
210                 winx *= BLI_rcti_size_x(&G.scene->r.border);
211                 winy *= BLI_rctf_size_y(&G.scene->r.border);
212         }
213         
214         /* while dragging we need to update the rects, otherwise it doesn't end with correct one */
215
216         BLI_rctf_init(&dispf, 15.0f, BLI_rcti_size_x(&block->rect) - 15.0f, 15.0f, (BLI_rctf_size_y(&block->rect)) - 15.0f);
217         ui_graphics_to_window_rct(sa->win, &dispf, disprect);
218         
219         /* correction for gla draw */
220         BLI_rcti_translate(disprect, -curarea->winrct.xmin, -curarea->winrct.ymin);
221         
222         calc_image_view(sima, 'p');
223 //      printf("winrct %d %d %d %d\n", disprect->xmin, disprect->ymin, disprect->xmax, disprect->ymax);
224         /* map to image space coordinates */
225         mval[0] = disprect->xmin; mval[1] = disprect->ymin;
226         areamouseco_to_ipoco(v2d, mval, &dispf.xmin, &dispf.ymin);
227         mval[0] = disprect->xmax; mval[1] = disprect->ymax;
228         areamouseco_to_ipoco(v2d, mval, &dispf.xmax, &dispf.ymax);
229         
230         /* map to render coordinates */
231         disprect->xmin = dispf.xmin;
232         disprect->xmax = dispf.xmax;
233         disprect->ymin = dispf.ymin;
234         disprect->ymax = dispf.ymax;
235         
236         CLAMP(disprect->xmin, 0, winx);
237         CLAMP(disprect->xmax, 0, winx);
238         CLAMP(disprect->ymin, 0, winy);
239         CLAMP(disprect->ymax, 0, winy);
240 //      printf("drawrct %d %d %d %d\n", disprect->xmin, disprect->ymin, disprect->xmax, disprect->ymax);
241
242 }
243
244 static int is_preview_allowed(ScrArea *cur)
245 {
246         SpaceImage *sima = cur->spacedata.first;
247         ScrArea *sa;
248
249         /* check if another areawindow has preview set */
250         for (sa = G.curscreen->areabase.first; sa; sa = sa->next) {
251                 if (sa != cur && sa->spacetype == SPACE_IMAGE) {
252                         if (image_preview_active(sa, NULL, NULL))
253                                 return 0;
254                 }
255         }
256         /* check image type */
257         if (sima->image == NULL || sima->image->type != IMA_TYPE_COMPOSITE)
258                 return 0;
259         
260         return 1;
261 }
262
263
264 static void image_panel_preview(ScrArea *sa, short cntrl)   // IMAGE_HANDLER_PREVIEW
265 {
266         uiBlock *block;
267         SpaceImage *sima = sa->spacedata.first;
268         int ofsx, ofsy;
269         
270         if (is_preview_allowed(sa) == 0) {
271                 rem_blockhandler(sa, IMAGE_HANDLER_PREVIEW);
272                 G.scene->r.scemode &= ~R_COMP_CROP; /* quite weak */
273                 return;
274         }
275         
276         block = uiBeginBlock(C, ar, __func__, UI_EMBOSS);
277         uiPanelControl(UI_PNL_SOLID | UI_PNL_CLOSE | UI_PNL_SCALE | cntrl);
278         uiSetPanelHandler(IMAGE_HANDLER_PREVIEW);  // for close and esc
279         
280         ofsx = -150 + (sa->winx / 2) / sima->blockscale;
281         ofsy = -100 + (sa->winy / 2) / sima->blockscale;
282         if (uiNewPanel(C, ar, block, "Preview", "Image", ofsx, ofsy, 300, 200) == 0) return;
283         
284         uiBlockSetDrawExtraFunc(block, preview_cb);
285         
286 }
287 #endif
288
289
290 /* ********************* callbacks for standard image buttons *************** */
291
292 static char *slot_menu(void)
293 {
294         char *str;
295         int a, slot;
296         
297         str = MEM_callocN(IMA_MAX_RENDER_SLOT * 32, "menu slots");
298         
299         strcpy(str, IFACE_("Slot %t"));
300         a = strlen(str);
301
302         for (slot = 0; slot < IMA_MAX_RENDER_SLOT; slot++)
303                 a += sprintf(str + a, IFACE_("|Slot %d %%x%d"), slot + 1, slot);
304         
305         return str;
306 }
307
308 /* TODO, curlay should be removed? */
309 static char *layer_menu(RenderResult *rr, short *UNUSED(curlay))
310 {
311         RenderLayer *rl;
312         int len = 64 + RE_MAXNAME * BLI_countlist(&rr->layers);
313         short a, nr = 0;
314         char *str = MEM_callocN(len, "menu layers");
315         
316         strcpy(str, IFACE_("Layer %t"));
317         a = strlen(str);
318         
319         /* compo result */
320         if (rr->rectf) {
321                 a += sprintf(str + a, "|%s %%x0", IFACE_("Composite"));
322                 nr = 1;
323         }
324         else if (rr->rect32) {
325                 a += sprintf(str + a, "|%s %%x0", IFACE_("Sequence"));
326                 nr = 1;
327         }
328         for (rl = rr->layers.first; rl; rl = rl->next, nr++) {
329                 a += sprintf(str + a, "|%s %%x%d", rl->name, nr);
330         }
331         
332         /* no curlay clip here, on render (redraws) the amount of layers can be 1 fir single-layer render */
333         
334         return str;
335 }
336
337 /* rl==NULL means composite result */
338 static char *pass_menu(RenderLayer *rl, short *curpass)
339 {
340         RenderPass *rpass;
341         int len = 64 + 32 * (rl ? BLI_countlist(&rl->passes) : 1);
342         short a, nr = 0;
343         char *str = MEM_callocN(len, "menu layers");
344         
345         strcpy(str, IFACE_("Pass %t"));
346         a = strlen(str);
347         
348         /* rendered results don't have a Combined pass */
349         if (rl == NULL || rl->rectf) {
350                 a += sprintf(str + a, "|%s %%x0", IFACE_("Combined"));
351                 nr = 1;
352         }
353         
354         if (rl)
355                 for (rpass = rl->passes.first; rpass; rpass = rpass->next, nr++)
356                         a += sprintf(str + a, "|%s %%x%d", IFACE_(rpass->name), nr);
357         
358         if (*curpass >= nr)
359                 *curpass = 0;
360         
361         return str;
362 }
363
364 /* 5 layer button callbacks... */
365 static void image_multi_cb(bContext *C, void *rr_v, void *iuser_v) 
366 {
367         ImageUser *iuser = iuser_v;
368
369         BKE_image_multilayer_index(rr_v, iuser); 
370         WM_event_add_notifier(C, NC_IMAGE | ND_DRAW, NULL);
371 }
372 static void image_multi_inclay_cb(bContext *C, void *rr_v, void *iuser_v) 
373 {
374         RenderResult *rr = rr_v;
375         ImageUser *iuser = iuser_v;
376         int tot = BLI_countlist(&rr->layers);
377
378         if (rr->rectf || rr->rect32)
379                 tot++;  /* fake compo/sequencer layer */
380
381         if (iuser->layer < tot - 1) {
382                 iuser->layer++;
383                 BKE_image_multilayer_index(rr, iuser); 
384                 WM_event_add_notifier(C, NC_IMAGE | ND_DRAW, NULL);
385         }
386 }
387 static void image_multi_declay_cb(bContext *C, void *rr_v, void *iuser_v) 
388 {
389         ImageUser *iuser = iuser_v;
390
391         if (iuser->layer > 0) {
392                 iuser->layer--;
393                 BKE_image_multilayer_index(rr_v, iuser); 
394                 WM_event_add_notifier(C, NC_IMAGE | ND_DRAW, NULL);
395         }
396 }
397 static void image_multi_incpass_cb(bContext *C, void *rr_v, void *iuser_v) 
398 {
399         RenderResult *rr = rr_v;
400         ImageUser *iuser = iuser_v;
401         RenderLayer *rl = BLI_findlink(&rr->layers, iuser->layer);
402
403         if (rl) {
404                 int tot = BLI_countlist(&rl->passes);
405
406                 if (rr->rectf || rr->rect32)
407                         tot++;  /* fake compo/sequencer layer */
408
409                 if (iuser->pass < tot - 1) {
410                         iuser->pass++;
411                         BKE_image_multilayer_index(rr, iuser); 
412                         WM_event_add_notifier(C, NC_IMAGE | ND_DRAW, NULL);
413                 }
414         }
415 }
416 static void image_multi_decpass_cb(bContext *C, void *rr_v, void *iuser_v) 
417 {
418         ImageUser *iuser = iuser_v;
419
420         if (iuser->pass > 0) {
421                 iuser->pass--;
422                 BKE_image_multilayer_index(rr_v, iuser); 
423                 WM_event_add_notifier(C, NC_IMAGE | ND_DRAW, NULL);
424         }
425 }
426
427 #if 0
428 static void image_freecache_cb(bContext *C, void *ima_v, void *unused) 
429 {
430         Scene *scene = CTX_data_scene(C);
431         BKE_image_free_anim_ibufs(ima_v, scene->r.cfra);
432         WM_event_add_notifier(C, NC_IMAGE, ima_v);
433 }
434 #endif
435
436 #if 0
437 static void image_user_change(bContext *C, void *iuser_v, void *unused)
438 {
439         Scene *scene = CTX_data_scene(C);
440         BKE_image_user_calc_imanr(iuser_v, scene->r.cfra, 0);
441 }
442 #endif
443
444 static void uiblock_layer_pass_buttons(uiLayout *layout, RenderResult *rr, ImageUser *iuser, int w, short *render_slot)
445 {
446         uiBlock *block = uiLayoutGetBlock(layout);
447         uiBut *but;
448         RenderLayer *rl = NULL;
449         int wmenu1, wmenu2, wmenu3, layer;
450         char *strp;
451
452         uiLayoutRow(layout, TRUE);
453
454         /* layer menu is 1/3 larger than pass */
455         wmenu1 = (2 * w) / 5;
456         wmenu2 = (3 * w) / 5;
457         wmenu3 = (3 * w) / 6;
458         
459         /* menu buts */
460         if (render_slot) {
461                 strp = slot_menu();
462                 but = uiDefButS(block, MENU, 0, strp, 0, 0, wmenu1, UI_UNIT_Y, render_slot, 0, 0, 0, 0, TIP_("Select Slot"));
463                 uiButSetFunc(but, image_multi_cb, rr, iuser);
464                 MEM_freeN(strp);
465         }
466
467         if (rr) {
468                 strp = layer_menu(rr, &iuser->layer);
469                 but = uiDefButS(block, MENU, 0, strp, 0, 0, wmenu2, UI_UNIT_Y, &iuser->layer, 0, 0, 0, 0, TIP_("Select Layer"));
470                 uiButSetFunc(but, image_multi_cb, rr, iuser);
471                 MEM_freeN(strp);
472
473                 layer = iuser->layer;
474                 if (rr->rectf || rr->rect32)
475                         layer--;  /* fake compo/sequencer layer */
476                 
477                 rl = BLI_findlink(&rr->layers, layer); /* return NULL is meant to be */
478                 strp = pass_menu(rl, &iuser->pass);
479                 but = uiDefButS(block, MENU, 0, strp, 0, 0, wmenu3, UI_UNIT_Y, &iuser->pass, 0, 0, 0, 0, TIP_("Select Pass"));
480                 uiButSetFunc(but, image_multi_cb, rr, iuser);
481                 MEM_freeN(strp);
482         }
483 }
484
485 static void uiblock_layer_pass_arrow_buttons(uiLayout *layout, RenderResult *rr, ImageUser *iuser, short *render_slot)
486 {
487         uiBlock *block = uiLayoutGetBlock(layout);
488         uiLayout *row;
489         uiBut *but;
490         const float dpi_fac = UI_DPI_FAC;
491         
492         row = uiLayoutRow(layout, TRUE);
493
494         if (rr == NULL || iuser == NULL)
495                 return;
496         if (rr->layers.first == NULL) {
497                 uiItemL(row, IFACE_("No Layers in Render Result"), ICON_NONE);
498                 return;
499         }
500
501         /* decrease, increase arrows */
502         but = uiDefIconBut(block, BUT, 0, ICON_TRIA_LEFT,   0, 0, 0.85f * UI_UNIT_X, UI_UNIT_Y, NULL, 0, 0, 0, 0, TIP_("Previous Layer"));
503         uiButSetFunc(but, image_multi_declay_cb, rr, iuser);
504         but = uiDefIconBut(block, BUT, 0, ICON_TRIA_RIGHT,  0, 0, 0.90f * UI_UNIT_X, UI_UNIT_Y, NULL, 0, 0, 0, 0, TIP_("Next Layer"));
505         uiButSetFunc(but, image_multi_inclay_cb, rr, iuser);
506
507         uiblock_layer_pass_buttons(row, rr, iuser, 230 * dpi_fac, render_slot);
508
509         /* decrease, increase arrows */
510         but = uiDefIconBut(block, BUT, 0, ICON_TRIA_LEFT,   0, 0, 0.85f * UI_UNIT_X, UI_UNIT_Y, NULL, 0, 0, 0, 0, TIP_("Previous Pass"));
511         uiButSetFunc(but, image_multi_decpass_cb, rr, iuser);
512         but = uiDefIconBut(block, BUT, 0, ICON_TRIA_RIGHT,  0, 0, 0.90f * UI_UNIT_X, UI_UNIT_Y, NULL, 0, 0, 0, 0, TIP_("Next Pass"));
513         uiButSetFunc(but, image_multi_incpass_cb, rr, iuser);
514
515         uiBlockEndAlign(block);
516 }
517
518 // XXX HACK!
519 // static int packdummy=0;
520
521 typedef struct RNAUpdateCb {
522         PointerRNA ptr;
523         PropertyRNA *prop;
524         ImageUser *iuser;
525 } RNAUpdateCb;
526
527 static void rna_update_cb(bContext *C, void *arg_cb, void *UNUSED(arg))
528 {
529         RNAUpdateCb *cb = (RNAUpdateCb *)arg_cb;
530
531         /* ideally this would be done by RNA itself, but there we have
532          * no image user available, so we just update this flag here */
533         cb->iuser->ok = 1;
534
535         /* we call update here on the pointer property, this way the
536          * owner of the image pointer can still define it's own update
537          * and notifier */
538         RNA_property_update(C, &cb->ptr, cb->prop);
539 }
540
541 void uiTemplateImage(uiLayout *layout, bContext *C, PointerRNA *ptr, const char *propname, PointerRNA *userptr, int compact)
542 {
543 #define MAX_INFO_LEN  128
544
545         PropertyRNA *prop;
546         PointerRNA imaptr;
547         RNAUpdateCb *cb;
548         Image *ima;
549         ImageUser *iuser;
550         ImBuf *ibuf;
551         Scene *scene = CTX_data_scene(C);
552         uiLayout *row, *split, *col;
553         uiBlock *block;
554         char str[MAX_INFO_LEN];
555
556         void *lock;
557
558         if (!ptr->data)
559                 return;
560
561         prop = RNA_struct_find_property(ptr, propname);
562         if (!prop) {
563                 printf("%s: property not found: %s.%s\n",
564                        __func__, RNA_struct_identifier(ptr->type), propname);
565                 return;
566         }
567
568         if (RNA_property_type(prop) != PROP_POINTER) {
569                 printf("%s: expected pointer property for %s.%s\n",
570                        __func__, RNA_struct_identifier(ptr->type), propname);
571                 return;
572         }
573
574         block = uiLayoutGetBlock(layout);
575
576         imaptr = RNA_property_pointer_get(ptr, prop);
577         ima = imaptr.data;
578         iuser = userptr->data;
579
580         BKE_image_user_check_frame_calc(iuser, (int)scene->r.cfra, 0);
581
582         cb = MEM_callocN(sizeof(RNAUpdateCb), "RNAUpdateCb");
583         cb->ptr = *ptr;
584         cb->prop = prop;
585         cb->iuser = iuser;
586
587         uiLayoutSetContextPointer(layout, "edit_image", &imaptr);
588         uiLayoutSetContextPointer(layout, "edit_image_user", userptr);
589
590         if (!compact)
591                 uiTemplateID(layout, C, ptr, propname, "IMAGE_OT_new", "IMAGE_OT_open", NULL);
592
593         if (ima) {
594                 uiBlockSetNFunc(block, rna_update_cb, MEM_dupallocN(cb), NULL);
595
596                 if (ima->source == IMA_SRC_VIEWER) {
597                         ibuf = BKE_image_acquire_ibuf(ima, iuser, &lock);
598                         image_info(scene, iuser, ima, ibuf, str, MAX_INFO_LEN);
599                         BKE_image_release_ibuf(ima, ibuf, lock);
600
601                         uiItemL(layout, ima->id.name + 2, ICON_NONE);
602                         uiItemL(layout, str, ICON_NONE);
603
604                         if (ima->type == IMA_TYPE_COMPOSITE) {
605                                 // XXX not working yet
606 #if 0
607                                 iuser = ntree_get_active_iuser(scene->nodetree);
608                                 if (iuser) {
609                                         uiBlockBeginAlign(block);
610                                         uiDefIconTextBut(block, BUT, B_SIMA_RECORD, ICON_REC, "Record", 10, 120, 100, 20, 0, 0, 0, 0, 0, "");
611                                         uiDefIconTextBut(block, BUT, B_SIMA_PLAY, ICON_PLAY, "Play",    110, 120, 100, 20, 0, 0, 0, 0, 0, "");
612                                         but = uiDefBut(block, BUT, B_NOP, "Free Cache", 210, 120, 100, 20, 0, 0, 0, 0, 0, "");
613                                         uiButSetFunc(but, image_freecache_cb, ima, NULL);
614                                         
615                                         if (iuser->frames)
616                                                 BLI_snprintf(str, sizeof(str), "(%d) Frames:", iuser->framenr);
617                                         else strcpy(str, "Frames:");
618                                         uiBlockBeginAlign(block);
619                                         uiDefButI(block, NUM, imagechanged, str,        10, 90, 150, 20, &iuser->frames, 0.0, MAXFRAMEF, 0, 0, "Number of images of a movie to use");
620                                         uiDefButI(block, NUM, imagechanged, "StartFr:", 160, 90, 150, 20, &iuser->sfra, 1.0, MAXFRAMEF, 0, 0, "Global starting frame of the movie");
621                                 }
622 #endif
623                         }
624                         else if (ima->type == IMA_TYPE_R_RESULT) {
625                                 /* browse layer/passes */
626                                 RenderResult *rr;
627
628                                 /* use BKE_image_acquire_renderresult  so we get the correct slot in the menu */
629                                 rr = BKE_image_acquire_renderresult(scene, ima);
630                                 uiblock_layer_pass_arrow_buttons(layout, rr, iuser, &ima->render_slot);
631                                 BKE_image_release_renderresult(scene, ima);
632                         }
633                 }
634                 else {
635                         uiItemR(layout, &imaptr, "source", 0, NULL, ICON_NONE);
636
637                         if (ima->source != IMA_SRC_GENERATED) {
638                                 row = uiLayoutRow(layout, TRUE);
639                                 if (ima->packedfile)
640                                         uiItemO(row, "", ICON_PACKAGE, "image.unpack");
641                                 else
642                                         uiItemO(row, "", ICON_UGLYPACKAGE, "image.pack");
643                                 
644                                 row = uiLayoutRow(row, FALSE);
645                                 uiLayoutSetEnabled(row, ima->packedfile == NULL);
646                                 uiItemR(row, &imaptr, "filepath", 0, "", ICON_NONE);
647                                 uiItemO(row, "", ICON_FILE_REFRESH, "image.reload");
648                         }
649
650                         // XXX what was this for?
651 #if 0
652                         /* check for re-render, only buttons */
653                         if (imagechanged == B_IMAGECHANGED) {
654                                 if (iuser->flag & IMA_ANIM_REFRESHED) {
655                                         iuser->flag &= ~IMA_ANIM_REFRESHED;
656                                         WM_event_add_notifier(C, NC_IMAGE, ima);
657                                 }
658                         }
659 #endif
660
661                         /* multilayer? */
662                         if (ima->type == IMA_TYPE_MULTILAYER && ima->rr) {
663                                 uiblock_layer_pass_arrow_buttons(layout, ima->rr, iuser, NULL);
664                         }
665                         else if (ima->source != IMA_SRC_GENERATED) {
666                                 if (compact == 0) {
667                                         ibuf = BKE_image_acquire_ibuf(ima, iuser, &lock);
668                                         image_info(scene, iuser, ima, ibuf, str, MAX_INFO_LEN);
669                                         BKE_image_release_ibuf(ima, ibuf, lock);
670                                         uiItemL(layout, str, ICON_NONE);
671                                 }
672                         }
673
674                         col = uiLayoutColumn(layout, FALSE);
675                         uiTemplateColorspaceSettings(col, &imaptr, "colorspace_settings");
676                         uiItemR(col, &imaptr, "view_as_render", 0, NULL, ICON_NONE);
677
678                         if (ima->source != IMA_SRC_GENERATED) {
679                                 if (compact == 0) { /* background image view doesnt need these */
680                                         ImBuf *ibuf = BKE_image_acquire_ibuf(ima, iuser, NULL);
681                                         int has_alpha = TRUE;
682
683                                         if (ibuf) {
684                                                 int imtype = BKE_ftype_to_imtype(ibuf->ftype);
685                                                 char valid_channels = BKE_imtype_valid_channels(imtype);
686
687                                                 has_alpha = valid_channels & IMA_CHAN_FLAG_ALPHA;
688
689                                                 BKE_image_release_ibuf(ima, ibuf, NULL);
690                                         }
691
692                                         if (has_alpha) {
693                                                 col = uiLayoutColumn(layout, FALSE);
694                                                 uiItemR(col, &imaptr, "use_alpha", 0, NULL, ICON_NONE);
695                                                 uiItemR(col, &imaptr, "alpha_mode", 0, "Alpha", ICON_NONE);
696                                         }
697
698                                         uiItemS(layout);
699
700                                         split = uiLayoutSplit(layout, 0.0f, FALSE);
701
702                                         col = uiLayoutColumn(split, FALSE);
703                                         /* XXX Why only display fields_per_frame only for video image types?
704                                          *     And why allow fields for non-video image types at all??? */
705                                         if (ELEM(ima->source, IMA_SRC_MOVIE, IMA_SRC_SEQUENCE)) {
706                                                 uiLayout *subsplit = uiLayoutSplit(col, 0.0f, FALSE);
707                                                 uiLayout *subcol = uiLayoutColumn(subsplit, FALSE);
708                                                 uiItemR(subcol, &imaptr, "use_fields", 0, NULL, ICON_NONE);
709                                                 subcol = uiLayoutColumn(subsplit, FALSE);
710                                                 uiLayoutSetActive(subcol, RNA_boolean_get(&imaptr, "use_fields"));
711                                                 uiItemR(subcol, userptr, "fields_per_frame", 0, IFACE_("Fields"), ICON_NONE);
712                                         }
713                                         else
714                                                 uiItemR(col, &imaptr, "use_fields", 0, NULL, ICON_NONE);
715                                         row = uiLayoutRow(col, FALSE);
716                                         uiLayoutSetActive(row, RNA_boolean_get(&imaptr, "use_fields"));
717                                         uiItemR(row, &imaptr, "field_order", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
718                                 }
719                         }
720
721                         if (ELEM(ima->source, IMA_SRC_MOVIE, IMA_SRC_SEQUENCE)) {
722                                 uiItemS(layout);
723
724                                 split = uiLayoutSplit(layout, 0.0f, FALSE);
725
726                                 col = uiLayoutColumn(split, FALSE);
727
728                                 BLI_snprintf(str, sizeof(str), IFACE_("(%d) Frames"), iuser->framenr);
729                                 uiItemR(col, userptr, "frame_duration", 0, str, ICON_NONE);
730                                 uiItemR(col, userptr, "frame_start", 0, IFACE_("Start"), ICON_NONE);
731                                 uiItemR(col, userptr, "frame_offset", 0, NULL, ICON_NONE);
732
733                                 col = uiLayoutColumn(split, FALSE);
734                                 uiItemO(col, NULL, ICON_NONE, "IMAGE_OT_match_movie_length");
735                                 uiItemR(col, userptr, "use_auto_refresh", 0, NULL, ICON_NONE);
736                                 uiItemR(col, userptr, "use_cyclic", 0, NULL, ICON_NONE);
737                         }
738                         else if (ima->source == IMA_SRC_GENERATED) {
739                                 split = uiLayoutSplit(layout, 0.0f, FALSE);
740
741                                 col = uiLayoutColumn(split, TRUE);
742                                 uiItemR(col, &imaptr, "generated_width", 0, "X", ICON_NONE);
743                                 uiItemR(col, &imaptr, "generated_height", 0, "Y", ICON_NONE);
744                                 
745                                 uiItemR(col, &imaptr, "use_generated_float", 0, NULL, ICON_NONE);
746
747                                 uiItemR(split, &imaptr, "generated_type", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
748                         }
749
750                 }
751
752                 uiBlockSetNFunc(block, NULL, NULL, NULL);
753         }
754
755         MEM_freeN(cb);
756
757 #undef MAX_INFO_LEN
758 }
759
760 void uiTemplateImageSettings(uiLayout *layout, PointerRNA *imfptr, int color_management)
761 {
762         ImageFormatData *imf = imfptr->data;
763         ID *id = imfptr->id.data;
764         PointerRNA display_settings_ptr;
765         PropertyRNA *prop;
766         const int depth_ok = BKE_imtype_valid_depths(imf->imtype);
767         /* some settings depend on this being a scene thats rendered */
768         const short is_render_out = (id && GS(id->name) == ID_SCE);
769
770         uiLayout *col, *row, *split, *sub;
771         int show_preview = FALSE;
772
773         col = uiLayoutColumn(layout, FALSE);
774
775         split = uiLayoutSplit(col, 0.5f, FALSE);
776         
777         uiItemR(split, imfptr, "file_format", 0, "", ICON_NONE);
778         sub = uiLayoutRow(split, FALSE);
779         uiItemR(sub, imfptr, "color_mode", UI_ITEM_R_EXPAND, IFACE_("Color"), ICON_NONE);
780
781         /* only display depth setting if multiple depths can be used */
782         if ((ELEM7(depth_ok,
783                    R_IMF_CHAN_DEPTH_1,
784                    R_IMF_CHAN_DEPTH_8,
785                    R_IMF_CHAN_DEPTH_10,
786                    R_IMF_CHAN_DEPTH_12,
787                    R_IMF_CHAN_DEPTH_16,
788                    R_IMF_CHAN_DEPTH_24,
789                    R_IMF_CHAN_DEPTH_32)) == 0)
790         {
791                 row = uiLayoutRow(col, FALSE);
792
793                 uiItemL(row, IFACE_("Color Depth:"), ICON_NONE);
794                 uiItemR(row, imfptr, "color_depth", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
795         }
796
797         if (BKE_imtype_supports_quality(imf->imtype)) {
798                 uiItemR(col, imfptr, "quality", 0, NULL, ICON_NONE);
799         }
800
801         if (BKE_imtype_supports_compress(imf->imtype)) {
802                 uiItemR(col, imfptr, "compression", 0, NULL, ICON_NONE);
803         }
804
805         if (ELEM(imf->imtype, R_IMF_IMTYPE_OPENEXR, R_IMF_IMTYPE_MULTILAYER)) {
806                 uiItemR(col, imfptr, "exr_codec", 0, NULL, ICON_NONE);
807         }
808         
809         row = uiLayoutRow(col, FALSE);
810         if (BKE_imtype_supports_zbuf(imf->imtype)) {
811                 uiItemR(row, imfptr, "use_zbuffer", 0, NULL, ICON_NONE);
812         }
813
814         if (is_render_out && (imf->imtype == R_IMF_IMTYPE_OPENEXR)) {
815                 show_preview = TRUE;
816                 uiItemR(row, imfptr, "use_preview", 0, NULL, ICON_NONE);
817         }
818
819         if (imf->imtype == R_IMF_IMTYPE_JP2) {
820                 uiItemR(col, imfptr, "jpeg2k_codec", 0, NULL, ICON_NONE);
821
822                 row = uiLayoutRow(col, FALSE);
823                 uiItemR(row, imfptr, "use_jpeg2k_cinema_preset", 0, NULL, ICON_NONE);
824                 uiItemR(row, imfptr, "use_jpeg2k_cinema_48", 0, NULL, ICON_NONE);
825                 
826                 uiItemR(col, imfptr, "use_jpeg2k_ycc", 0, NULL, ICON_NONE);
827         }
828
829         if (imf->imtype == R_IMF_IMTYPE_DPX) {
830                 uiItemR(col, imfptr, "use_cineon_log", 0, NULL, ICON_NONE);
831         }
832
833         if (imf->imtype == R_IMF_IMTYPE_CINEON) {
834 #if 1
835                 uiItemL(col, IFACE_("Hard coded Non-Linear, Gamma:1.7"), ICON_NONE);
836 #else
837                 uiItemR(col, imfptr, "use_cineon_log", 0, NULL, ICON_NONE);
838                 uiItemR(col, imfptr, "cineon_black", 0, NULL, ICON_NONE);
839                 uiItemR(col, imfptr, "cineon_white", 0, NULL, ICON_NONE);
840                 uiItemR(col, imfptr, "cineon_gamma", 0, NULL, ICON_NONE);
841 #endif
842         }
843
844         /* color management */
845         if (color_management &&
846             (!BKE_imtype_requires_linear_float(imf->imtype) ||
847              (show_preview && imf->flag & R_IMF_FLAG_PREVIEW_JPG)))
848         {
849                 prop = RNA_struct_find_property(imfptr, "display_settings");
850                 display_settings_ptr = RNA_property_pointer_get(imfptr, prop);
851
852                 col = uiLayoutColumn(layout, FALSE);
853                 uiItemL(col, IFACE_("Color Management"), ICON_NONE);
854
855                 uiItemR(col, &display_settings_ptr, "display_device", 0, NULL, ICON_NONE);
856
857                 uiTemplateColormanagedViewSettings(col, NULL, imfptr, "view_settings");
858         }
859 }
860
861 void uiTemplateImageLayers(uiLayout *layout, bContext *C, Image *ima, ImageUser *iuser)
862 {
863         Scene *scene = CTX_data_scene(C);
864
865         /* render layers and passes */
866         if (ima && iuser) {
867                 const float dpi_fac = UI_DPI_FAC;
868                 RenderResult *rr;
869
870                 /* use BKE_image_acquire_renderresult  so we get the correct slot in the menu */
871                 rr = BKE_image_acquire_renderresult(scene, ima);
872                 uiblock_layer_pass_buttons(layout, rr, iuser, 160 * dpi_fac, (ima->type == IMA_TYPE_R_RESULT) ? &ima->render_slot : NULL);
873                 BKE_image_release_renderresult(scene, ima);
874         }
875 }
876
877 void image_buttons_register(ARegionType *art)
878 {
879         PanelType *pt;
880
881         pt = MEM_callocN(sizeof(PanelType), "spacetype image panel gpencil");
882         strcpy(pt->idname, "IMAGE_PT_gpencil");
883         strcpy(pt->label, "Grease Pencil");
884         pt->draw_header = gpencil_panel_standard_header;
885         pt->draw = gpencil_panel_standard;
886         BLI_addtail(&art->paneltypes, pt);
887 }
888
889 static int image_properties(bContext *C, wmOperator *UNUSED(op))
890 {
891         ScrArea *sa = CTX_wm_area(C);
892         ARegion *ar = image_has_buttons_region(sa);
893         
894         if (ar)
895                 ED_region_toggle_hidden(C, ar);
896
897         return OPERATOR_FINISHED;
898 }
899
900 void IMAGE_OT_properties(wmOperatorType *ot)
901 {
902         ot->name = "Properties";
903         ot->idname = "IMAGE_OT_properties";
904         ot->description = "Toggle display properties panel";
905         
906         ot->exec = image_properties;
907         ot->poll = ED_operator_image_active;
908         
909         /* flags */
910         ot->flag = 0;
911 }
912
913 static int image_scopes(bContext *C, wmOperator *UNUSED(op))
914 {
915         ScrArea *sa = CTX_wm_area(C);
916         ARegion *ar = image_has_scope_region(sa);
917         
918         if (ar)
919                 ED_region_toggle_hidden(C, ar);
920         
921         return OPERATOR_FINISHED;
922 }
923
924 void IMAGE_OT_scopes(wmOperatorType *ot)
925 {
926         ot->name = "Scopes";
927         ot->idname = "IMAGE_OT_scopes";
928         ot->description = "Toggle display scopes panel";
929         
930         ot->exec = image_scopes;
931         ot->poll = ED_operator_image_active;
932         
933         /* flags */
934         ot->flag = 0;
935 }
936