Camera tracking integration
[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 *UNUSED(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                                 ImBuf *obuf;
61                                 MovieTracking *tracking= &clip->tracking;
62                                 int width, height;
63                                 float overscan= 0.0f;
64
65                                 ibuf->rect_float= cbuf->rect;
66
67                                 BKE_movieclip_get_size(clip, NULL, &width, &height);
68
69                                 if(!node->storage)
70                                         node->storage= BKE_tracking_distortion_create();
71
72                                 if(node->custom1==0)
73                                         obuf= BKE_tracking_distortion_exec(node->storage, tracking, ibuf, width, height, overscan, 1);
74                                 else
75                                         obuf= BKE_tracking_distortion_exec(node->storage, tracking, ibuf, width, height, overscan, 0);
76
77                                 stackbuf->rect= obuf->rect_float;
78                                 stackbuf->malloc= 1;
79
80                                 obuf->mall&= ~IB_rectfloat;
81                                 obuf->rect_float= NULL;
82
83                                 IMB_freeImBuf(ibuf);
84                                 IMB_freeImBuf(obuf);
85                         }
86
87                         /* pass on output and free */
88                         out[0]->data= stackbuf;
89
90                         if(cbuf!=in[0]->data)
91                                 free_compbuf(cbuf);
92                 } else {
93                         CompBuf *cbuf= in[0]->data;
94                         CompBuf *stackbuf= pass_on_compbuf(cbuf);
95
96                         out[0]->data= stackbuf;
97                 }
98         }
99 }
100
101 static const char *label(bNode *node)
102 {
103         if(node->custom1==0)
104                 return "Undistortion";
105         else
106                 return "Distortion";
107 }
108
109 static void storage_free(bNode *node)
110 {
111         if(node->storage)
112                 BKE_tracking_distortion_destroy(node->storage);
113
114         node->storage= NULL;
115 }
116
117 void storage_copy(bNode *orig_node, bNode *new_node)
118 {
119         if(orig_node->storage)
120                 new_node->storage= BKE_tracking_distortion_copy(orig_node->storage);
121 }
122
123 void register_node_type_cmp_moviedistortion(ListBase *lb)
124 {
125         static bNodeType ntype;
126
127         node_type_base(&ntype, CMP_NODE_MOVIEDISTORTION, "Movie Distortion", NODE_CLASS_DISTORT, NODE_OPTIONS);
128         node_type_socket_templates(&ntype, cmp_node_moviedistortion_in, cmp_node_moviedistortion_out);
129         node_type_size(&ntype, 140, 100, 320);
130         node_type_label(&ntype, label);
131         node_type_exec(&ntype, exec);
132         node_type_storage(&ntype, NULL, storage_free, storage_copy);
133
134         nodeRegisterType(lb, &ntype);
135 }