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