Color Management, Stage 2: Switch color pipeline to use OpenColorIO
[blender.git] / source / blender / editors / space_node / node_view.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) 2008 Blender Foundation.
19  * All rights reserved.
20  *
21  * 
22  * Contributor(s): Blender Foundation, Nathan Letwory
23  *
24  * ***** END GPL LICENSE BLOCK *****
25  */
26
27 /** \file blender/editors/space_node/node_view.c
28  *  \ingroup spnode
29  */
30
31 #include "DNA_node_types.h"
32
33 #include "BLI_rect.h"
34 #include "BLI_utildefines.h"
35 #include "BLI_math.h"
36
37 #include "BKE_context.h"
38 #include "BKE_image.h"
39 #include "BKE_screen.h"
40 #include "BKE_node.h"
41
42 #include "ED_node.h"  /* own include */
43 #include "ED_screen.h"
44 #include "ED_space_api.h"
45 #include "ED_image.h"
46
47 #include "UI_view2d.h"
48
49 #include "RNA_access.h"
50 #include "RNA_define.h"
51
52 #include "WM_api.h"
53 #include "WM_types.h"
54
55 #include "UI_view2d.h"
56
57 #include "MEM_guardedalloc.h"
58
59 #include "IMB_imbuf.h"
60 #include "IMB_imbuf_types.h"
61
62 #include "node_intern.h"  /* own include */
63
64
65 /* **************** View All Operator ************** */
66
67 static int space_node_view_flag(bContext *C, SpaceNode *snode, ARegion *ar, const int node_flag)
68 {
69         bNode *node;
70         rctf cur_new;
71         float oldwidth, oldheight, width, height;
72         int tot = 0;
73         int has_frame = FALSE;
74         
75         oldwidth  = BLI_RCT_SIZE_X(&ar->v2d.cur);
76         oldheight = BLI_RCT_SIZE_Y(&ar->v2d.cur);
77
78         BLI_rctf_init_minmax(&cur_new);
79
80         if (snode->edittree) {
81                 for (node = snode->edittree->nodes.first; node; node = node->next) {
82                         if ((node->flag & node_flag) == node_flag) {
83                                 BLI_rctf_union(&cur_new, &node->totr);
84                                 tot++;
85
86                                 if (node->type == NODE_FRAME) {
87                                         has_frame = TRUE;
88                                 }
89                         }
90                 }
91         }
92
93         if (tot) {
94                 width  = BLI_RCT_SIZE_X(&cur_new);
95                 height = BLI_RCT_SIZE_Y(&cur_new);
96
97                 /* for single non-frame nodes, don't zoom in, just pan view,
98                  * but do allow zooming out, this allows for big nodes to be zoomed out */
99                 if ((tot == 1) &&
100                     (has_frame == FALSE) &&
101                     ((oldwidth * oldheight) > (width * height)))
102                 {
103                         /* center, don't zoom */
104                         BLI_rctf_resize(&cur_new, oldwidth, oldheight);
105                 }
106                 else {
107                         if (width > height) {
108                                 float newheight;
109                                 newheight = oldheight * width / oldwidth;
110                                 cur_new.ymin = cur_new.ymin - newheight / 4;
111                                 cur_new.ymax = cur_new.ymax + newheight / 4;
112                         }
113                         else {
114                                 float newwidth;
115                                 newwidth = oldwidth * height / oldheight;
116                                 cur_new.xmin = cur_new.xmin - newwidth / 4;
117                                 cur_new.xmax = cur_new.xmax + newwidth / 4;
118                         }
119                 }
120
121                 UI_view2d_smooth_view(C, ar, &cur_new);
122         }
123
124         return (tot != 0);
125 }
126
127 static int node_view_all_exec(bContext *C, wmOperator *UNUSED(op))
128 {
129         ARegion *ar = CTX_wm_region(C);
130         SpaceNode *snode = CTX_wm_space_node(C);
131
132         /* is this really needed? */
133         snode->xof = 0;
134         snode->yof = 0;
135
136         if (space_node_view_flag(C, snode, ar, 0)) {
137                 return OPERATOR_FINISHED;
138         }
139         else {
140                 return OPERATOR_CANCELLED;
141         }
142 }
143
144 void NODE_OT_view_all(wmOperatorType *ot)
145 {
146         /* identifiers */
147         ot->name = "View All";
148         ot->idname = "NODE_OT_view_all";
149         ot->description = "Resize view so you can see all nodes";
150         
151         /* api callbacks */
152         ot->exec = node_view_all_exec;
153         ot->poll = ED_operator_node_active;
154         
155         /* flags */
156         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
157 }
158
159 static int node_view_selected_exec(bContext *C, wmOperator *UNUSED(op))
160 {
161         ARegion *ar = CTX_wm_region(C);
162         SpaceNode *snode = CTX_wm_space_node(C);
163
164         if (space_node_view_flag(C, snode, ar, NODE_SELECT)) {
165                 return OPERATOR_FINISHED;
166         }
167         else {
168                 return OPERATOR_CANCELLED;
169         }
170 }
171
172 void NODE_OT_view_selected(wmOperatorType *ot)
173 {
174         /* identifiers */
175         ot->name = "View Selected";
176         ot->idname = "NODE_OT_view_selected";
177         ot->description = "Resize view so you can see selected nodes";
178
179         /* api callbacks */
180         ot->exec = node_view_selected_exec;
181         ot->poll = ED_operator_node_active;
182
183         /* flags */
184         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
185 }
186
187 /* **************** Backround Image Operators ************** */
188
189 typedef struct NodeViewMove {
190         int mvalo[2];
191         int xmin, ymin, xmax, ymax;
192 } NodeViewMove;
193
194 static int snode_bg_viewmove_modal(bContext *C, wmOperator *op, wmEvent *event)
195 {
196         SpaceNode *snode = CTX_wm_space_node(C);
197         ARegion *ar = CTX_wm_region(C);
198         NodeViewMove *nvm = op->customdata;
199
200         switch (event->type) {
201                 case MOUSEMOVE:
202
203                         snode->xof -= (nvm->mvalo[0] - event->mval[0]);
204                         snode->yof -= (nvm->mvalo[1] - event->mval[1]);
205                         nvm->mvalo[0] = event->mval[0];
206                         nvm->mvalo[1] = event->mval[1];
207
208                         /* prevent dragging image outside of the window and losing it! */
209                         CLAMP(snode->xof, nvm->xmin, nvm->xmax);
210                         CLAMP(snode->yof, nvm->ymin, nvm->ymax);
211
212                         ED_region_tag_redraw(ar);
213
214                         break;
215
216                 case LEFTMOUSE:
217                 case MIDDLEMOUSE:
218                 case RIGHTMOUSE:
219
220                         MEM_freeN(nvm);
221                         op->customdata = NULL;
222
223                         return OPERATOR_FINISHED;
224         }
225
226         return OPERATOR_RUNNING_MODAL;
227 }
228
229 static int snode_bg_viewmove_invoke(bContext *C, wmOperator *op, wmEvent *event)
230 {
231         SpaceNode *snode = CTX_wm_space_node(C);
232         ARegion *ar = CTX_wm_region(C);
233         NodeViewMove *nvm;
234         Image *ima;
235         ImBuf *ibuf;
236         const float pad = 32.0f; /* better be bigger then scrollbars */
237
238         void *lock;
239
240         ima = BKE_image_verify_viewer(IMA_TYPE_COMPOSITE, "Viewer Node");
241         ibuf = BKE_image_acquire_ibuf(ima, NULL, &lock);
242
243         if (ibuf == NULL) {
244                 BKE_image_release_ibuf(ima, lock);
245                 return OPERATOR_CANCELLED;
246         }
247
248         nvm = MEM_callocN(sizeof(NodeViewMove), "NodeViewMove struct");
249         op->customdata = nvm;
250         nvm->mvalo[0] = event->mval[0];
251         nvm->mvalo[1] = event->mval[1];
252
253         nvm->xmin = -(ar->winx / 2) - (ibuf->x * (0.5f * snode->zoom)) + pad;
254         nvm->xmax =  (ar->winx / 2) + (ibuf->x * (0.5f * snode->zoom)) - pad;
255         nvm->ymin = -(ar->winy / 2) - (ibuf->y * (0.5f * snode->zoom)) + pad;
256         nvm->ymax =  (ar->winy / 2) + (ibuf->y * (0.5f * snode->zoom)) - pad;
257
258         BKE_image_release_ibuf(ima, lock);
259
260         /* add modal handler */
261         WM_event_add_modal_handler(C, op);
262
263         return OPERATOR_RUNNING_MODAL;
264 }
265
266 static int snode_bg_viewmove_cancel(bContext *UNUSED(C), wmOperator *op)
267 {
268         MEM_freeN(op->customdata);
269         op->customdata = NULL;
270
271         return OPERATOR_CANCELLED;
272 }
273
274 void NODE_OT_backimage_move(wmOperatorType *ot)
275 {
276         /* identifiers */
277         ot->name = "Background Image Move";
278         ot->description = "Move Node backdrop";
279         ot->idname = "NODE_OT_backimage_move";
280
281         /* api callbacks */
282         ot->invoke = snode_bg_viewmove_invoke;
283         ot->modal = snode_bg_viewmove_modal;
284         ot->poll = composite_node_active;
285         ot->cancel = snode_bg_viewmove_cancel;
286
287         /* flags */
288         ot->flag = OPTYPE_BLOCKING | OPTYPE_GRAB_POINTER;
289 }
290
291 static int backimage_zoom(bContext *C, wmOperator *op)
292 {
293         SpaceNode *snode = CTX_wm_space_node(C);
294         ARegion *ar = CTX_wm_region(C);
295         float fac = RNA_float_get(op->ptr, "factor");
296
297         snode->zoom *= fac;
298         ED_region_tag_redraw(ar);
299
300         return OPERATOR_FINISHED;
301 }
302
303
304 void NODE_OT_backimage_zoom(wmOperatorType *ot)
305 {
306
307         /* identifiers */
308         ot->name = "Background Image Zoom";
309         ot->idname = "NODE_OT_backimage_zoom";
310         ot->description = "Zoom in/out the background image";
311
312         /* api callbacks */
313         ot->exec = backimage_zoom;
314         ot->poll = composite_node_active;
315
316         /* flags */
317         ot->flag = OPTYPE_BLOCKING;
318
319         /* internal */
320         RNA_def_float(ot->srna, "factor", 1.2f, 0.0f, 10.0f, "Factor", "", 0.0f, 10.0f);
321 }
322
323 /******************** sample backdrop operator ********************/
324
325 typedef struct ImageSampleInfo {
326         ARegionType *art;
327         void *draw_handle;
328         int x, y;
329         int channels;
330
331         unsigned char col[4];
332         float colf[4];
333
334         int draw;
335         int color_manage;
336 } ImageSampleInfo;
337
338 static void sample_draw(const bContext *C, ARegion *ar, void *arg_info)
339 {
340         Scene *scene = CTX_data_scene(C);
341         ImageSampleInfo *info = arg_info;
342
343         if (info->draw) {
344                 ED_image_draw_info(scene, ar, info->color_manage, FALSE, info->channels,
345                                    info->x, info->y, info->col, info->colf,
346                                    NULL, NULL /* zbuf - unused for nodes */
347                                    );
348         }
349 }
350
351 /* returns color in SRGB */
352 /* matching ED_space_image_color_sample() */
353 int ED_space_node_color_sample(SpaceNode *snode, ARegion *ar, int mval[2], float r_col[3])
354 {
355         void *lock;
356         Image *ima;
357         ImBuf *ibuf;
358         float fx, fy, bufx, bufy;
359         int ret = FALSE;
360
361         ima = BKE_image_verify_viewer(IMA_TYPE_COMPOSITE, "Viewer Node");
362         ibuf = BKE_image_acquire_ibuf(ima, NULL, &lock);
363         if (!ibuf) {
364                 return FALSE;
365         }
366
367         /* map the mouse coords to the backdrop image space */
368         bufx = ibuf->x * snode->zoom;
369         bufy = ibuf->y * snode->zoom;
370         fx = (bufx > 0.0f ? ((float)mval[0] - 0.5f * ar->winx - snode->xof) / bufx + 0.5f : 0.0f);
371         fy = (bufy > 0.0f ? ((float)mval[1] - 0.5f * ar->winy - snode->yof) / bufy + 0.5f : 0.0f);
372
373         if (fx >= 0.0f && fy >= 0.0f && fx < 1.0f && fy < 1.0f) {
374                 float *fp;
375                 unsigned char *cp;
376                 int x = (int)(fx * ibuf->x), y = (int)(fy * ibuf->y);
377
378                 CLAMP(x, 0, ibuf->x - 1);
379                 CLAMP(y, 0, ibuf->y - 1);
380
381                 if (ibuf->rect_float) {
382                         fp = (ibuf->rect_float + (ibuf->channels) * (y * ibuf->x + x));
383                         /* IB_PROFILE_NONE is default but infact its linear */
384                         linearrgb_to_srgb_v3_v3(r_col, fp);
385                         ret = TRUE;
386                 }
387                 else if (ibuf->rect) {
388                         cp = (unsigned char *)(ibuf->rect + y * ibuf->x + x);
389                         rgb_uchar_to_float(r_col, cp);
390                         ret = TRUE;
391                 }
392         }
393
394         BKE_image_release_ibuf(ima, lock);
395
396         return ret;
397 }
398
399 static void sample_apply(bContext *C, wmOperator *op, wmEvent *event)
400 {
401         SpaceNode *snode = CTX_wm_space_node(C);
402         ARegion *ar = CTX_wm_region(C);
403         ImageSampleInfo *info = op->customdata;
404         void *lock;
405         Image *ima;
406         ImBuf *ibuf;
407         float fx, fy, bufx, bufy;
408
409         ima = BKE_image_verify_viewer(IMA_TYPE_COMPOSITE, "Viewer Node");
410         ibuf = BKE_image_acquire_ibuf(ima, NULL, &lock);
411         if (!ibuf) {
412                 info->draw = 0;
413                 return;
414         }
415
416         if (!ibuf->rect) {
417                 IMB_rect_from_float(ibuf);
418         }
419
420         /* map the mouse coords to the backdrop image space */
421         bufx = ibuf->x * snode->zoom;
422         bufy = ibuf->y * snode->zoom;
423         fx = (bufx > 0.0f ? ((float)event->mval[0] - 0.5f * ar->winx - snode->xof) / bufx + 0.5f : 0.0f);
424         fy = (bufy > 0.0f ? ((float)event->mval[1] - 0.5f * ar->winy - snode->yof) / bufy + 0.5f : 0.0f);
425
426         if (fx >= 0.0f && fy >= 0.0f && fx < 1.0f && fy < 1.0f) {
427                 float *fp;
428                 unsigned char *cp;
429                 int x = (int)(fx * ibuf->x), y = (int)(fy * ibuf->y);
430
431                 CLAMP(x, 0, ibuf->x - 1);
432                 CLAMP(y, 0, ibuf->y - 1);
433
434                 info->x = x;
435                 info->y = y;
436                 info->draw = 1;
437                 info->channels = ibuf->channels;
438
439                 if (ibuf->rect) {
440                         cp = (unsigned char *)(ibuf->rect + y * ibuf->x + x);
441
442                         info->col[0] = cp[0];
443                         info->col[1] = cp[1];
444                         info->col[2] = cp[2];
445                         info->col[3] = cp[3];
446
447                         info->colf[0] = (float)cp[0] / 255.0f;
448                         info->colf[1] = (float)cp[1] / 255.0f;
449                         info->colf[2] = (float)cp[2] / 255.0f;
450                         info->colf[3] = (float)cp[3] / 255.0f;
451
452                         info->color_manage = FALSE;
453                 }
454                 if (ibuf->rect_float) {
455                         fp = (ibuf->rect_float + (ibuf->channels) * (y * ibuf->x + x));
456
457                         info->colf[0] = fp[0];
458                         info->colf[1] = fp[1];
459                         info->colf[2] = fp[2];
460                         info->colf[3] = fp[3];
461
462                         info->color_manage = TRUE;
463                 }
464
465                 ED_node_sample_set(info->colf);
466         }
467         else {
468                 info->draw = 0;
469                 ED_node_sample_set(NULL);
470         }
471
472         BKE_image_release_ibuf(ima, lock);
473
474         ED_area_tag_redraw(CTX_wm_area(C));
475 }
476
477 static void sample_exit(bContext *C, wmOperator *op)
478 {
479         ImageSampleInfo *info = op->customdata;
480
481         ED_node_sample_set(NULL);
482         ED_region_draw_cb_exit(info->art, info->draw_handle);
483         ED_area_tag_redraw(CTX_wm_area(C));
484         MEM_freeN(info);
485 }
486
487 static int sample_invoke(bContext *C, wmOperator *op, wmEvent *event)
488 {
489         SpaceNode *snode = CTX_wm_space_node(C);
490         ARegion *ar = CTX_wm_region(C);
491         ImageSampleInfo *info;
492
493         if (snode->treetype != NTREE_COMPOSIT || !(snode->flag & SNODE_BACKDRAW))
494                 return OPERATOR_CANCELLED;
495
496         info = MEM_callocN(sizeof(ImageSampleInfo), "ImageSampleInfo");
497         info->art = ar->type;
498         info->draw_handle = ED_region_draw_cb_activate(ar->type, sample_draw, info, REGION_DRAW_POST_PIXEL);
499         op->customdata = info;
500
501         sample_apply(C, op, event);
502
503         WM_event_add_modal_handler(C, op);
504
505         return OPERATOR_RUNNING_MODAL;
506 }
507
508 static int sample_modal(bContext *C, wmOperator *op, wmEvent *event)
509 {
510         switch (event->type) {
511                 case LEFTMOUSE:
512                 case RIGHTMOUSE: // XXX hardcoded
513                         sample_exit(C, op);
514                         return OPERATOR_CANCELLED;
515                 case MOUSEMOVE:
516                         sample_apply(C, op, event);
517                         break;
518         }
519
520         return OPERATOR_RUNNING_MODAL;
521 }
522
523 static int sample_cancel(bContext *C, wmOperator *op)
524 {
525         sample_exit(C, op);
526         return OPERATOR_CANCELLED;
527 }
528
529 void NODE_OT_backimage_sample(wmOperatorType *ot)
530 {
531         /* identifiers */
532         ot->name = "Backimage Sample";
533         ot->idname = "NODE_OT_backimage_sample";
534         ot->description = "Use mouse to sample background image";
535
536         /* api callbacks */
537         ot->invoke = sample_invoke;
538         ot->modal = sample_modal;
539         ot->cancel = sample_cancel;
540         ot->poll = ED_operator_node_active;
541
542         /* flags */
543         ot->flag = OPTYPE_BLOCKING;
544 }