svn merge -r41638:41648 ^/trunk/blender
[blender.git] / source / blender / nodes / composite / nodes / node_composite_moviedistortion.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) 2011 Blender Foundation.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): Blender Foundation,
24  *                 Sergey Sharybin
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 /** \file blender/nodes/composite/nodes/node_composite_moviedistortion.c
30  *  \ingroup cmpnodes
31  */
32
33
34 #include "node_composite_util.h"
35
36 /* **************** Translate  ******************** */
37
38 static bNodeSocketTemplate cmp_node_moviedistortion_in[]= {
39         {       SOCK_RGBA, 1, "Image",                  0.8f, 0.8f, 0.8f, 1.0f, 0.0f, 1.0f},
40         {       -1, 0, ""       }
41 };
42
43 static bNodeSocketTemplate cmp_node_moviedistortion_out[]= {
44         {       SOCK_RGBA, 0, "Image"},
45         {       -1, 0, ""       }
46 };
47
48 static void exec(void *data, bNode *node, bNodeStack **in, bNodeStack **out)
49 {
50         if(in[0]->data) {
51                 if(node->id) {
52                         MovieClip *clip= (MovieClip *)node->id;
53                         CompBuf *cbuf= typecheck_compbuf(in[0]->data, CB_RGBA);
54                         CompBuf *stackbuf= alloc_compbuf(cbuf->x, cbuf->y, CB_RGBA, 0);
55                         ImBuf *ibuf;
56
57                         ibuf= IMB_allocImBuf(cbuf->x, cbuf->y, 32, 0);
58
59                         if(ibuf) {
60                                 RenderData *rd= data;
61                                 ImBuf *obuf;
62                                 MovieTracking *tracking= &clip->tracking;
63                                 int width, height;
64                                 float overscan= 0.0f;
65                                 MovieClipUser user= {0};
66
67                                 BKE_movieclip_user_set_frame(&user, rd->cfra);
68
69                                 ibuf->rect_float= cbuf->rect;
70
71                                 BKE_movieclip_get_size(clip, &user, &width, &height);
72
73                                 if(!node->storage)
74                                         node->storage= BKE_tracking_distortion_create();
75
76                                 if(node->custom1==0)
77                                         obuf= BKE_tracking_distortion_exec(node->storage, tracking, ibuf, width, height, overscan, 1);
78                                 else
79                                         obuf= BKE_tracking_distortion_exec(node->storage, tracking, ibuf, width, height, overscan, 0);
80
81                                 stackbuf->rect= obuf->rect_float;
82                                 stackbuf->malloc= 1;
83
84                                 obuf->mall&= ~IB_rectfloat;
85                                 obuf->rect_float= NULL;
86
87                                 IMB_freeImBuf(ibuf);
88                                 IMB_freeImBuf(obuf);
89                         }
90
91                         /* pass on output and free */
92                         out[0]->data= stackbuf;
93
94                         if(cbuf!=in[0]->data)
95                                 free_compbuf(cbuf);
96                 } else {
97                         CompBuf *cbuf= in[0]->data;
98                         CompBuf *stackbuf= pass_on_compbuf(cbuf);
99
100                         out[0]->data= stackbuf;
101                 }
102         }
103 }
104
105 static const char *label(bNode *node)
106 {
107         if(node->custom1==0)
108                 return "Undistortion";
109         else
110                 return "Distortion";
111 }
112
113 static void storage_free(bNode *node)
114 {
115         if(node->storage)
116                 BKE_tracking_distortion_destroy(node->storage);
117
118         node->storage= NULL;
119 }
120
121 void storage_copy(bNode *orig_node, bNode *new_node)
122 {
123         if(orig_node->storage)
124                 new_node->storage= BKE_tracking_distortion_copy(orig_node->storage);
125 }
126
127 void register_node_type_cmp_moviedistortion(ListBase *lb)
128 {
129         static bNodeType ntype;
130
131         node_type_base(&ntype, CMP_NODE_MOVIEDISTORTION, "Movie Distortion", NODE_CLASS_DISTORT, NODE_OPTIONS);
132         node_type_socket_templates(&ntype, cmp_node_moviedistortion_in, cmp_node_moviedistortion_out);
133         node_type_size(&ntype, 140, 100, 320);
134         node_type_label(&ntype, label);
135         node_type_exec(&ntype, exec);
136         node_type_storage(&ntype, NULL, storage_free, storage_copy);
137
138         nodeRegisterType(lb, &ntype);
139 }