Tomato Cycles: fix incorrect memory read when synchronizing mesh motion
[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 = ar->v2d.cur.xmax - ar->v2d.cur.xmin;
76         oldheight = ar->v2d.cur.ymax - ar->v2d.cur.ymin;
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 = cur_new.xmax - cur_new.xmin;
95                 height = cur_new.ymax - cur_new.ymin;
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                         width = cur_new.xmax - cur_new.xmin;
108                         height = cur_new.ymax - cur_new.ymin;
109
110                         if (width > height) {
111                                 float newheight;
112                                 newheight = oldheight * width / oldwidth;
113                                 cur_new.ymin = cur_new.ymin - newheight / 4;
114                                 cur_new.ymax = cur_new.ymax + newheight / 4;
115                         }
116                         else {
117                                 float newwidth;
118                                 newwidth = oldwidth * height / oldheight;
119                                 cur_new.xmin = cur_new.xmin - newwidth / 4;
120                                 cur_new.xmax = cur_new.xmax + newwidth / 4;
121                         }
122                 }
123
124                 UI_view2d_smooth_view(C, ar, &cur_new);
125         }
126
127         return (tot != 0);
128 }
129
130 static int node_view_all_exec(bContext *C, wmOperator *UNUSED(op))
131 {
132         ARegion *ar = CTX_wm_region(C);
133         SpaceNode *snode = CTX_wm_space_node(C);
134
135         /* is this really needed? */
136         snode->xof = 0;
137         snode->yof = 0;
138
139         if (space_node_view_flag(C, snode, ar, 0)) {
140                 return OPERATOR_FINISHED;
141         }
142         else {
143                 return OPERATOR_CANCELLED;
144         }
145 }
146
147 void NODE_OT_view_all(wmOperatorType *ot)
148 {
149         /* identifiers */
150         ot->name = "View All";
151         ot->idname = "NODE_OT_view_all";
152         ot->description = "Resize view so you can see all nodes";
153         
154         /* api callbacks */
155         ot->exec = node_view_all_exec;
156         ot->poll = ED_operator_node_active;
157         
158         /* flags */
159         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
160 }
161
162 static int node_view_selected_exec(bContext *C, wmOperator *UNUSED(op))
163 {
164         ARegion *ar = CTX_wm_region(C);
165         SpaceNode *snode = CTX_wm_space_node(C);
166
167         if (space_node_view_flag(C, snode, ar, NODE_SELECT)) {
168                 return OPERATOR_FINISHED;
169         }
170         else {
171                 return OPERATOR_CANCELLED;
172         }
173 }
174
175 void NODE_OT_view_selected(wmOperatorType *ot)
176 {
177         /* identifiers */
178         ot->name = "View Selected";
179         ot->idname = "NODE_OT_view_selected";
180         ot->description = "Resize view so you can see selected nodes";
181
182         /* api callbacks */
183         ot->exec = node_view_selected_exec;
184         ot->poll = ED_operator_node_active;
185
186         /* flags */
187         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
188 }
189
190 /* **************** Backround Image Operators ************** */
191
192 typedef struct NodeViewMove {
193         int mvalo[2];
194         int xmin, ymin, xmax, ymax;
195 } NodeViewMove;
196
197 static int snode_bg_viewmove_modal(bContext *C, wmOperator *op, wmEvent *event)
198 {
199         SpaceNode *snode = CTX_wm_space_node(C);
200         ARegion *ar = CTX_wm_region(C);
201         NodeViewMove *nvm = op->customdata;
202
203         switch (event->type) {
204                 case MOUSEMOVE:
205
206                         snode->xof -= (nvm->mvalo[0] - event->mval[0]);
207                         snode->yof -= (nvm->mvalo[1] - event->mval[1]);
208                         nvm->mvalo[0] = event->mval[0];
209                         nvm->mvalo[1] = event->mval[1];
210
211                         /* prevent dragging image outside of the window and losing it! */
212                         CLAMP(snode->xof, nvm->xmin, nvm->xmax);
213                         CLAMP(snode->yof, nvm->ymin, nvm->ymax);
214
215                         ED_region_tag_redraw(ar);
216
217                         break;
218
219                 case LEFTMOUSE:
220                 case MIDDLEMOUSE:
221                 case RIGHTMOUSE:
222
223                         MEM_freeN(nvm);
224                         op->customdata = NULL;
225
226                         return OPERATOR_FINISHED;
227         }
228
229         return OPERATOR_RUNNING_MODAL;
230 }
231
232 static int snode_bg_viewmove_invoke(bContext *C, wmOperator *op, wmEvent *event)
233 {
234         SpaceNode *snode = CTX_wm_space_node(C);
235         ARegion *ar = CTX_wm_region(C);
236         NodeViewMove *nvm;
237         Image *ima;
238         ImBuf *ibuf;
239         const float pad = 32.0f; /* better be bigger then scrollbars */
240
241         void *lock;
242
243         ima = BKE_image_verify_viewer(IMA_TYPE_COMPOSITE, "Viewer Node");
244         ibuf = BKE_image_acquire_ibuf(ima, NULL, &lock);
245
246         if (ibuf == NULL) {
247                 BKE_image_release_ibuf(ima, lock);
248                 return OPERATOR_CANCELLED;
249         }
250
251         nvm = MEM_callocN(sizeof(NodeViewMove), "NodeViewMove struct");
252         op->customdata = nvm;
253         nvm->mvalo[0] = event->mval[0];
254         nvm->mvalo[1] = event->mval[1];
255
256         nvm->xmin = -(ar->winx / 2) - (ibuf->x * (0.5f * snode->zoom)) + pad;
257         nvm->xmax =  (ar->winx / 2) + (ibuf->x * (0.5f * snode->zoom)) - pad;
258         nvm->ymin = -(ar->winy / 2) - (ibuf->y * (0.5f * snode->zoom)) + pad;
259         nvm->ymax =  (ar->winy / 2) + (ibuf->y * (0.5f * snode->zoom)) - pad;
260
261         BKE_image_release_ibuf(ima, lock);
262
263         /* add modal handler */
264         WM_event_add_modal_handler(C, op);
265
266         return OPERATOR_RUNNING_MODAL;
267 }
268
269 static int snode_bg_viewmove_cancel(bContext *UNUSED(C), wmOperator *op)
270 {
271         MEM_freeN(op->customdata);
272         op->customdata = NULL;
273
274         return OPERATOR_CANCELLED;
275 }
276
277 void NODE_OT_backimage_move(wmOperatorType *ot)
278 {
279         /* identifiers */
280         ot->name = "Background Image Move";
281         ot->description = "Move Node backdrop";
282         ot->idname = "NODE_OT_backimage_move";
283
284         /* api callbacks */
285         ot->invoke = snode_bg_viewmove_invoke;
286         ot->modal = snode_bg_viewmove_modal;
287         ot->poll = composite_node_active;
288         ot->cancel = snode_bg_viewmove_cancel;
289
290         /* flags */
291         ot->flag = OPTYPE_BLOCKING | OPTYPE_GRAB_POINTER;
292 }
293
294 static int backimage_zoom(bContext *C, wmOperator *op)
295 {
296         SpaceNode *snode = CTX_wm_space_node(C);
297         ARegion *ar = CTX_wm_region(C);
298         float fac = RNA_float_get(op->ptr, "factor");
299
300         snode->zoom *= fac;
301         ED_region_tag_redraw(ar);
302
303         return OPERATOR_FINISHED;
304 }
305
306
307 void NODE_OT_backimage_zoom(wmOperatorType *ot)
308 {
309
310         /* identifiers */
311         ot->name = "Background Image Zoom";
312         ot->idname = "NODE_OT_backimage_zoom";
313         ot->description = "Zoom in/out the background image";
314
315         /* api callbacks */
316         ot->exec = backimage_zoom;
317         ot->poll = composite_node_active;
318
319         /* flags */
320         ot->flag = OPTYPE_BLOCKING;
321
322         /* internal */
323         RNA_def_float(ot->srna, "factor", 1.2f, 0.0f, 10.0f, "Factor", "", 0.0f, 10.0f);
324 }
325
326 /******************** sample backdrop operator ********************/
327
328 typedef struct ImageSampleInfo {
329         ARegionType *art;
330         void *draw_handle;
331         int x, y;
332         int channels;
333         int color_manage;
334
335         unsigned char col[4];
336         float colf[4];
337
338         int draw;
339 } ImageSampleInfo;
340
341 static void sample_draw(const bContext *C, ARegion *ar, void *arg_info)
342 {
343         Scene *scene = CTX_data_scene(C);
344         ImageSampleInfo *info = arg_info;
345
346         if (info->draw) {
347                 ED_image_draw_info(ar, (scene->r.color_mgt_flag & R_COLOR_MANAGEMENT), info->channels,
348                                    info->x, info->y, info->col, info->colf,
349                                    NULL, NULL /* zbuf - unused for nodes */
350                                    );
351         }
352 }
353
354 /* returns color in SRGB */
355 /* matching ED_space_image_color_sample() */
356 int ED_space_node_color_sample(SpaceNode *snode, ARegion *ar, int mval[2], float r_col[3])
357 {
358         void *lock;
359         Image *ima;
360         ImBuf *ibuf;
361         float fx, fy, bufx, bufy;
362         int ret = FALSE;
363
364         ima = BKE_image_verify_viewer(IMA_TYPE_COMPOSITE, "Viewer Node");
365         ibuf = BKE_image_acquire_ibuf(ima, NULL, &lock);
366         if (!ibuf) {
367                 return FALSE;
368         }
369
370         /* map the mouse coords to the backdrop image space */
371         bufx = ibuf->x * snode->zoom;
372         bufy = ibuf->y * snode->zoom;
373         fx = (bufx > 0.0f ? ((float)mval[0] - 0.5f * ar->winx - snode->xof) / bufx + 0.5f : 0.0f);
374         fy = (bufy > 0.0f ? ((float)mval[1] - 0.5f * ar->winy - snode->yof) / bufy + 0.5f : 0.0f);
375
376         if (fx >= 0.0f && fy >= 0.0f && fx < 1.0f && fy < 1.0f) {
377                 float *fp;
378                 char *cp;
379                 int x = (int)(fx * ibuf->x), y = (int)(fy * ibuf->y);
380
381                 CLAMP(x, 0, ibuf->x - 1);
382                 CLAMP(y, 0, ibuf->y - 1);
383
384                 if (ibuf->rect_float) {
385                         fp = (ibuf->rect_float + (ibuf->channels) * (y * ibuf->x + x));
386
387                         if (ibuf->profile == IB_PROFILE_LINEAR_RGB) {
388                                 linearrgb_to_srgb_v3_v3(r_col, fp);
389                         }
390                         else {
391                                 copy_v3_v3(r_col, fp);
392                         }
393                         ret = TRUE;
394                 }
395                 else if (ibuf->rect) {
396                         cp = (char *)(ibuf->rect + y * ibuf->x + x);
397                         r_col[0] = cp[0] / 255.0f;
398                         r_col[1] = cp[1] / 255.0f;
399                         r_col[2] = cp[2] / 255.0f;
400                         ret = TRUE;
401                 }
402         }
403
404         BKE_image_release_ibuf(ima, lock);
405
406         return ret;
407 }
408
409 static void sample_apply(bContext *C, wmOperator *op, wmEvent *event)
410 {
411         SpaceNode *snode = CTX_wm_space_node(C);
412         ARegion *ar = CTX_wm_region(C);
413         ImageSampleInfo *info = op->customdata;
414         void *lock;
415         Image *ima;
416         ImBuf *ibuf;
417         float fx, fy, bufx, bufy;
418
419         ima = BKE_image_verify_viewer(IMA_TYPE_COMPOSITE, "Viewer Node");
420         ibuf = BKE_image_acquire_ibuf(ima, NULL, &lock);
421         if (!ibuf) {
422                 info->draw = 0;
423                 return;
424         }
425
426         if (!ibuf->rect) {
427                 if (info->color_manage)
428                         ibuf->profile = IB_PROFILE_LINEAR_RGB;
429                 else
430                         ibuf->profile = IB_PROFILE_NONE;
431                 IMB_rect_from_float(ibuf);
432         }
433
434         /* map the mouse coords to the backdrop image space */
435         bufx = ibuf->x * snode->zoom;
436         bufy = ibuf->y * snode->zoom;
437         fx = (bufx > 0.0f ? ((float)event->mval[0] - 0.5f * ar->winx - snode->xof) / bufx + 0.5f : 0.0f);
438         fy = (bufy > 0.0f ? ((float)event->mval[1] - 0.5f * ar->winy - snode->yof) / bufy + 0.5f : 0.0f);
439
440         if (fx >= 0.0f && fy >= 0.0f && fx < 1.0f && fy < 1.0f) {
441                 float *fp;
442                 char *cp;
443                 int x = (int)(fx * ibuf->x), y = (int)(fy * ibuf->y);
444
445                 CLAMP(x, 0, ibuf->x - 1);
446                 CLAMP(y, 0, ibuf->y - 1);
447
448                 info->x = x;
449                 info->y = y;
450                 info->draw = 1;
451                 info->channels = ibuf->channels;
452
453                 if (ibuf->rect) {
454                         cp = (char *)(ibuf->rect + y * ibuf->x + x);
455
456                         info->col[0] = cp[0];
457                         info->col[1] = cp[1];
458                         info->col[2] = cp[2];
459                         info->col[3] = cp[3];
460
461                         info->colf[0] = (float)cp[0] / 255.0f;
462                         info->colf[1] = (float)cp[1] / 255.0f;
463                         info->colf[2] = (float)cp[2] / 255.0f;
464                         info->colf[3] = (float)cp[3] / 255.0f;
465                 }
466                 if (ibuf->rect_float) {
467                         fp = (ibuf->rect_float + (ibuf->channels) * (y * ibuf->x + x));
468
469                         info->colf[0] = fp[0];
470                         info->colf[1] = fp[1];
471                         info->colf[2] = fp[2];
472                         info->colf[3] = fp[3];
473                 }
474
475                 ED_node_sample_set(info->colf);
476         }
477         else {
478                 info->draw = 0;
479                 ED_node_sample_set(NULL);
480         }
481
482         BKE_image_release_ibuf(ima, lock);
483
484         ED_area_tag_redraw(CTX_wm_area(C));
485 }
486
487 static void sample_exit(bContext *C, wmOperator *op)
488 {
489         ImageSampleInfo *info = op->customdata;
490
491         ED_node_sample_set(NULL);
492         ED_region_draw_cb_exit(info->art, info->draw_handle);
493         ED_area_tag_redraw(CTX_wm_area(C));
494         MEM_freeN(info);
495 }
496
497 static int sample_invoke(bContext *C, wmOperator *op, wmEvent *event)
498 {
499         SpaceNode *snode = CTX_wm_space_node(C);
500         ARegion *ar = CTX_wm_region(C);
501         ImageSampleInfo *info;
502
503         if (snode->treetype != NTREE_COMPOSIT || !(snode->flag & SNODE_BACKDRAW))
504                 return OPERATOR_CANCELLED;
505
506         info = MEM_callocN(sizeof(ImageSampleInfo), "ImageSampleInfo");
507         info->art = ar->type;
508         info->draw_handle = ED_region_draw_cb_activate(ar->type, sample_draw, info, REGION_DRAW_POST_PIXEL);
509         op->customdata = info;
510
511         sample_apply(C, op, event);
512
513         WM_event_add_modal_handler(C, op);
514
515         return OPERATOR_RUNNING_MODAL;
516 }
517
518 static int sample_modal(bContext *C, wmOperator *op, wmEvent *event)
519 {
520         switch (event->type) {
521                 case LEFTMOUSE:
522                 case RIGHTMOUSE: // XXX hardcoded
523                         sample_exit(C, op);
524                         return OPERATOR_CANCELLED;
525                 case MOUSEMOVE:
526                         sample_apply(C, op, event);
527                         break;
528         }
529
530         return OPERATOR_RUNNING_MODAL;
531 }
532
533 static int sample_cancel(bContext *C, wmOperator *op)
534 {
535         sample_exit(C, op);
536         return OPERATOR_CANCELLED;
537 }
538
539 void NODE_OT_backimage_sample(wmOperatorType *ot)
540 {
541         /* identifiers */
542         ot->name = "Backimage Sample";
543         ot->idname = "NODE_OT_backimage_sample";
544         ot->description = "Use mouse to sample background image";
545
546         /* api callbacks */
547         ot->invoke = sample_invoke;
548         ot->modal = sample_modal;
549         ot->cancel = sample_cancel;
550         ot->poll = ED_operator_node_active;
551
552         /* flags */
553         ot->flag = OPTYPE_BLOCKING;
554 }