Color management: display color managed RGB values in color sample line
[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         int color_manage;
331
332         unsigned char col[4];
333         float colf[4];
334
335         int draw;
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, (scene->r.color_mgt_flag & R_COLOR_MANAGEMENT), 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                         if (ELEM(ibuf->profile, IB_PROFILE_LINEAR_RGB, IB_PROFILE_NONE)) {
385                                 linearrgb_to_srgb_v3_v3(r_col, fp);
386                         }
387                         else {
388                                 copy_v3_v3(r_col, fp);
389                         }
390                         ret = TRUE;
391                 }
392                 else if (ibuf->rect) {
393                         cp = (unsigned char *)(ibuf->rect + y * ibuf->x + x);
394                         rgb_uchar_to_float(r_col, cp);
395                         ret = TRUE;
396                 }
397         }
398
399         BKE_image_release_ibuf(ima, lock);
400
401         return ret;
402 }
403
404 static void sample_apply(bContext *C, wmOperator *op, wmEvent *event)
405 {
406         SpaceNode *snode = CTX_wm_space_node(C);
407         ARegion *ar = CTX_wm_region(C);
408         ImageSampleInfo *info = op->customdata;
409         void *lock;
410         Image *ima;
411         ImBuf *ibuf;
412         float fx, fy, bufx, bufy;
413
414         ima = BKE_image_verify_viewer(IMA_TYPE_COMPOSITE, "Viewer Node");
415         ibuf = BKE_image_acquire_ibuf(ima, NULL, &lock);
416         if (!ibuf) {
417                 info->draw = 0;
418                 return;
419         }
420
421         if (!ibuf->rect) {
422                 if (info->color_manage)
423                         ibuf->profile = IB_PROFILE_LINEAR_RGB;
424                 else
425                         ibuf->profile = IB_PROFILE_NONE;
426                 IMB_rect_from_float(ibuf);
427         }
428
429         /* map the mouse coords to the backdrop image space */
430         bufx = ibuf->x * snode->zoom;
431         bufy = ibuf->y * snode->zoom;
432         fx = (bufx > 0.0f ? ((float)event->mval[0] - 0.5f * ar->winx - snode->xof) / bufx + 0.5f : 0.0f);
433         fy = (bufy > 0.0f ? ((float)event->mval[1] - 0.5f * ar->winy - snode->yof) / bufy + 0.5f : 0.0f);
434
435         if (fx >= 0.0f && fy >= 0.0f && fx < 1.0f && fy < 1.0f) {
436                 float *fp;
437                 unsigned char *cp;
438                 int x = (int)(fx * ibuf->x), y = (int)(fy * ibuf->y);
439
440                 CLAMP(x, 0, ibuf->x - 1);
441                 CLAMP(y, 0, ibuf->y - 1);
442
443                 info->x = x;
444                 info->y = y;
445                 info->draw = 1;
446                 info->channels = ibuf->channels;
447
448                 if (ibuf->rect) {
449                         cp = (unsigned char *)(ibuf->rect + y * ibuf->x + x);
450
451                         info->col[0] = cp[0];
452                         info->col[1] = cp[1];
453                         info->col[2] = cp[2];
454                         info->col[3] = cp[3];
455
456                         info->colf[0] = (float)cp[0] / 255.0f;
457                         info->colf[1] = (float)cp[1] / 255.0f;
458                         info->colf[2] = (float)cp[2] / 255.0f;
459                         info->colf[3] = (float)cp[3] / 255.0f;
460                 }
461                 if (ibuf->rect_float) {
462                         fp = (ibuf->rect_float + (ibuf->channels) * (y * ibuf->x + x));
463
464                         info->colf[0] = fp[0];
465                         info->colf[1] = fp[1];
466                         info->colf[2] = fp[2];
467                         info->colf[3] = fp[3];
468                 }
469
470                 ED_node_sample_set(info->colf);
471         }
472         else {
473                 info->draw = 0;
474                 ED_node_sample_set(NULL);
475         }
476
477         BKE_image_release_ibuf(ima, lock);
478
479         ED_area_tag_redraw(CTX_wm_area(C));
480 }
481
482 static void sample_exit(bContext *C, wmOperator *op)
483 {
484         ImageSampleInfo *info = op->customdata;
485
486         ED_node_sample_set(NULL);
487         ED_region_draw_cb_exit(info->art, info->draw_handle);
488         ED_area_tag_redraw(CTX_wm_area(C));
489         MEM_freeN(info);
490 }
491
492 static int sample_invoke(bContext *C, wmOperator *op, wmEvent *event)
493 {
494         SpaceNode *snode = CTX_wm_space_node(C);
495         ARegion *ar = CTX_wm_region(C);
496         ImageSampleInfo *info;
497
498         if (snode->treetype != NTREE_COMPOSIT || !(snode->flag & SNODE_BACKDRAW))
499                 return OPERATOR_CANCELLED;
500
501         info = MEM_callocN(sizeof(ImageSampleInfo), "ImageSampleInfo");
502         info->art = ar->type;
503         info->draw_handle = ED_region_draw_cb_activate(ar->type, sample_draw, info, REGION_DRAW_POST_PIXEL);
504         op->customdata = info;
505
506         sample_apply(C, op, event);
507
508         WM_event_add_modal_handler(C, op);
509
510         return OPERATOR_RUNNING_MODAL;
511 }
512
513 static int sample_modal(bContext *C, wmOperator *op, wmEvent *event)
514 {
515         switch (event->type) {
516                 case LEFTMOUSE:
517                 case RIGHTMOUSE: // XXX hardcoded
518                         sample_exit(C, op);
519                         return OPERATOR_CANCELLED;
520                 case MOUSEMOVE:
521                         sample_apply(C, op, event);
522                         break;
523         }
524
525         return OPERATOR_RUNNING_MODAL;
526 }
527
528 static int sample_cancel(bContext *C, wmOperator *op)
529 {
530         sample_exit(C, op);
531         return OPERATOR_CANCELLED;
532 }
533
534 void NODE_OT_backimage_sample(wmOperatorType *ot)
535 {
536         /* identifiers */
537         ot->name = "Backimage Sample";
538         ot->idname = "NODE_OT_backimage_sample";
539         ot->description = "Use mouse to sample background image";
540
541         /* api callbacks */
542         ot->invoke = sample_invoke;
543         ot->modal = sample_modal;
544         ot->cancel = sample_cancel;
545         ot->poll = ED_operator_node_active;
546
547         /* flags */
548         ot->flag = OPTYPE_BLOCKING;
549 }