converted more mixed tab/space indentations to tabs. only whitespace changes.
[blender.git] / source / blender / nodes / intern / CMP_nodes / CMP_viewer.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): none yet.
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  */
29
30 /** \file blender/nodes/intern/CMP_nodes/CMP_viewer.c
31  *  \ingroup cmpnodes
32  */
33
34
35 #include "../CMP_util.h"
36
37
38 /* **************** VIEWER ******************** */
39 static bNodeSocketType cmp_node_viewer_in[]= {
40         {       SOCK_RGBA, 1, "Image",          0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f},
41         {       SOCK_VALUE, 1, "Alpha",         1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
42         {       SOCK_VALUE, 1, "Z",                     1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
43         {       -1, 0, ""       }
44 };
45
46
47 static void node_composit_exec_viewer(void *data, bNode *node, bNodeStack **in, bNodeStack **UNUSED(out))
48 {
49         /* image assigned to output */
50         /* stack order input sockets: col, alpha, z */
51         
52         if(node->id && (node->flag & NODE_DO_OUTPUT)) { /* only one works on out */
53                 RenderData *rd= data;
54                 Image *ima= (Image *)node->id;
55                 ImBuf *ibuf;
56                 CompBuf *cbuf, *tbuf;
57                 int rectx, recty;
58                 void *lock;
59                 
60                 BKE_image_user_calc_frame(node->storage, rd->cfra, 0);
61
62                 /* always returns for viewer image, but we check nevertheless */
63                 ibuf= BKE_image_acquire_ibuf(ima, node->storage, &lock);
64                 if(ibuf==NULL) {
65                         printf("node_composit_exec_viewer error\n");
66                         BKE_image_release_ibuf(ima, lock);
67                         return;
68                 }
69                 
70                 /* free all in ibuf */
71                 imb_freerectImBuf(ibuf);
72                 imb_freerectfloatImBuf(ibuf);
73                 IMB_freezbuffloatImBuf(ibuf);
74                 
75                 /* get size */
76                 tbuf= in[0]->data?in[0]->data:(in[1]->data?in[1]->data:in[2]->data);
77                 if(tbuf==NULL) {
78                         rectx= 320; recty= 256;
79                 }
80                 else {
81                         rectx= tbuf->x;
82                         recty= tbuf->y;
83                 }
84                 
85                 /* make ibuf, and connect to ima */
86                 ibuf->x= rectx;
87                 ibuf->y= recty;
88                 imb_addrectfloatImBuf(ibuf);
89                 
90                 ima->ok= IMA_OK_LOADED;
91
92                 /* now we combine the input with ibuf */
93                 cbuf= alloc_compbuf(rectx, recty, CB_RGBA, 0);  /* no alloc*/
94                 cbuf->rect= ibuf->rect_float;
95                 
96                 /* when no alpha, we can simply copy */
97                 if(in[1]->data==NULL) {
98                         composit1_pixel_processor(node, cbuf, in[0]->data, in[0]->vec, do_copy_rgba, CB_RGBA);
99                 }
100                 else
101                         composit2_pixel_processor(node, cbuf, in[0]->data, in[0]->vec, in[1]->data, in[1]->vec, do_copy_a_rgba, CB_RGBA, CB_VAL);
102                 
103                 /* zbuf option */
104                 if(in[2]->data) {
105                         CompBuf *zbuf= alloc_compbuf(rectx, recty, CB_VAL, 1);
106                         ibuf->zbuf_float= zbuf->rect;
107                         ibuf->mall |= IB_zbuffloat;
108                         
109                         composit1_pixel_processor(node, zbuf, in[2]->data, in[2]->vec, do_copy_value, CB_VAL);
110                         
111                         /* free compbuf, but not the rect */
112                         zbuf->malloc= 0;
113                         free_compbuf(zbuf);
114                 }
115
116                 BKE_image_release_ibuf(ima, lock);
117
118                 generate_preview(data, node, cbuf);
119                 free_compbuf(cbuf);
120
121         }
122         else if(in[0]->data) {
123                 generate_preview(data, node, in[0]->data);
124         }
125 }
126
127 static void node_composit_init_viewer(bNode* node)
128 {
129         ImageUser *iuser= MEM_callocN(sizeof(ImageUser), "node image user");
130         node->storage= iuser;
131         iuser->sfra= 1;
132         iuser->fie_ima= 2;
133         iuser->ok= 1;
134 }
135
136 void register_node_type_cmp_viewer(ListBase *lb)
137 {
138         static bNodeType ntype;
139
140         node_type_base(&ntype, CMP_NODE_VIEWER, "Viewer", NODE_CLASS_OUTPUT, NODE_PREVIEW,
141                 cmp_node_viewer_in, NULL);
142         node_type_size(&ntype, 80, 60, 200);
143         node_type_init(&ntype, node_composit_init_viewer);
144         node_type_storage(&ntype, "ImageUser", node_free_standard_storage, node_copy_standard_storage);
145         node_type_exec(&ntype, node_composit_exec_viewer);
146
147         nodeRegisterType(lb, &ntype);
148 }
149
150