Camera tracking integration
[blender.git] / source / blender / nodes / composite / nodes / node_composite_movieundistort.c
1 /*
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. 
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * The Original Code is Copyright (C) 2006 Blender Foundation.
21  * All rights reserved.
22  *
23  * The Original Code is: all of this file.
24  *
25  * Contributor(s): Blender Foundation,
26  *                 Sergey Sharybin
27  *
28  * ***** END GPL LICENSE BLOCK *****
29  */
30
31 /** \file blender/nodes/composite/nodes/node_composite_movieundistort.c
32  *  \ingroup cmpnodes
33  */
34
35
36 #include "node_composite_util.h"
37
38 /* **************** Translate  ******************** */
39
40 static bNodeSocketTemplate cmp_node_movieundistort_in[]= {
41         {       SOCK_RGBA, 1, "Image",                  0.8f, 0.8f, 0.8f, 1.0f, 0.0f, 1.0f},
42         {       -1, 0, ""       }
43 };
44
45 static bNodeSocketTemplate cmp_node_movieundistort_out[]= {
46         {       SOCK_RGBA, 0, "Image"},
47         {       -1, 0, ""       }
48 };
49
50 static void node_composit_exec_movieundistort(void *UNUSED(data), bNode *node, bNodeStack **in, bNodeStack **out)
51 {
52         if(in[0]->data && node->id) {
53                 MovieClip *clip= (MovieClip *)node->id;
54                 CompBuf *cbuf= typecheck_compbuf(in[0]->data, CB_RGBA);
55                 CompBuf *stackbuf= alloc_compbuf(cbuf->x, cbuf->y, CB_RGBA, 0);
56                 ImBuf *ibuf;
57
58                 ibuf= IMB_allocImBuf(cbuf->x, cbuf->y, 32, 0);
59
60                 if(ibuf) {
61                         ImBuf *obuf;
62                         MovieClipUser *user= (MovieClipUser *)node->storage;
63                         float aspy= 1.f/clip->tracking.camera.pixel_aspect;
64                         int scaled= 0, width, height;
65
66                         ibuf->rect_float= cbuf->rect;
67
68                         BKE_movieclip_acquire_size(clip, user, &width, &height);
69                         height*= aspy;
70
71                         if(ibuf->x!=width || ibuf->y!=height) {
72                                 /* TODO: not sure this is really needed, but distortion coefficients are
73                                          calculated using camera resolution, so if image with other resolution
74                                          is passed to undistortion node, it'll be undistorted correct (but quality can hurt)
75                                          This is also needed to deal when camera pixel aspect isn't 1. Problems in this case
76                                          are caused because of how aspect x/y are calculating. Currently. projeciton
77                                          matrices and reconstruction stuff are supposing that aspect x is always 1 and
78                                          aspect y is less than 1 (if x resolution is larger than y resolution) */
79
80                                 IMB_scaleImBuf(ibuf, width, height);
81                                 scaled= 1;
82                         }
83
84                         obuf= BKE_tracking_undistort(&clip->tracking, ibuf);
85
86                         if(scaled)
87                                 IMB_scaleImBuf(obuf, cbuf->x, cbuf->y);
88
89                         stackbuf->rect= obuf->rect_float;
90                         stackbuf->malloc= 1;
91
92                         obuf->mall&= ~IB_rectfloat;
93                         obuf->rect_float= NULL;
94
95                         IMB_freeImBuf(ibuf);
96                         IMB_freeImBuf(obuf);
97                 }
98
99                 /* pass on output and free */
100                 out[0]->data= stackbuf;
101
102                 if(cbuf!=in[0]->data)
103                         free_compbuf(cbuf);
104         }
105 }
106
107 void register_node_type_cmp_movieundistort(ListBase *lb)
108 {
109         static bNodeType ntype;
110
111         node_type_base(&ntype, CMP_NODE_MOVIEUNDISTORT, "Movie Undistort", NODE_CLASS_DISTORT, NODE_OPTIONS);
112         node_type_socket_templates(&ntype, cmp_node_movieundistort_in, cmp_node_movieundistort_out);
113         node_type_size(&ntype, 140, 100, 320);
114         node_type_exec(&ntype, node_composit_exec_movieundistort);
115
116         nodeRegisterType(lb, &ntype);
117 }