22f3dd3a7b00f5e97b5812a1fa8f4fa101bc4052
[blender.git] / source / blender / nodes / composite / nodes / node_composite_splitViewer.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_splitViewer.c
29  *  \ingroup cmpnodes
30  */
31
32
33 #include "node_composite_util.h"
34
35 /* **************** SPLIT VIEWER ******************** */
36 static bNodeSocketTemplate cmp_node_splitviewer_in[]= {
37         {       SOCK_RGBA, 1, "Image",          0.0f, 0.0f, 0.0f, 1.0f},
38         {       SOCK_RGBA, 1, "Image",          0.0f, 0.0f, 0.0f, 1.0f},
39         {       -1, 0, ""       }
40 };
41
42 static void do_copy_split_rgba(bNode *UNUSED(node), float *out, float *in1, float *in2, float *fac)
43 {
44         if(*fac==0.0f) {
45                 QUATCOPY(out, in1);
46         }
47         else {
48                 QUATCOPY(out, in2);
49         }
50 }
51
52 static void node_composit_exec_splitviewer(void *data, bNode *node, bNodeStack **in, bNodeStack **UNUSED(out))
53 {
54         /* image assigned to output */
55         /* stack order input sockets: image image */
56         
57         if(in[0]->data==NULL || in[1]->data==NULL)
58                 return;
59         
60         if(node->id && (node->flag & NODE_DO_OUTPUT)) { /* only one works on out */
61                 Image *ima= (Image *)node->id;
62                 RenderData *rd= data;
63                 ImBuf *ibuf;
64                 CompBuf *cbuf, *buf1, *buf2, *mask;
65                 int x, y;
66                 float offset;
67                 void *lock;
68                 
69                 buf1= typecheck_compbuf(in[0]->data, CB_RGBA);
70                 buf2= typecheck_compbuf(in[1]->data, CB_RGBA);
71                 
72                 BKE_image_user_calc_frame(node->storage, rd->cfra, 0);
73                 
74                 /* always returns for viewer image, but we check nevertheless */
75                 ibuf= BKE_image_acquire_ibuf(ima, node->storage, &lock);
76                 if(ibuf==NULL) {
77                         printf("node_composit_exec_viewer error\n");
78                         BKE_image_release_ibuf(ima, lock);
79                         return;
80                 }
81                 
82                 /* free all in ibuf */
83                 imb_freerectImBuf(ibuf);
84                 imb_freerectfloatImBuf(ibuf);
85                 IMB_freezbuffloatImBuf(ibuf);
86                 
87                 /* make ibuf, and connect to ima */
88                 ibuf->x= buf1->x;
89                 ibuf->y= buf1->y;
90                 imb_addrectfloatImBuf(ibuf);
91                 
92                 ima->ok= IMA_OK_LOADED;
93
94                 /* output buf */
95                 cbuf= alloc_compbuf(buf1->x, buf1->y, CB_RGBA, 0);      /* no alloc*/
96                 cbuf->rect= ibuf->rect_float;
97                 
98                 /* mask buf */
99                 mask= alloc_compbuf(buf1->x, buf1->y, CB_VAL, 1);
100                 
101                 
102                 /* Check which offset mode is selected and limit offset if needed */
103                 if(node->custom2 == 0) {
104                         offset = buf1->x / 100.0f * node->custom1;
105                         CLAMP(offset, 0, buf1->x);
106                 }
107                 else {
108                         offset = buf1->y / 100.0f * node->custom1;
109                         CLAMP(offset, 0, buf1->y);
110                 }
111                 
112                 if(node->custom2 == 0) {
113                         for(y=0; y<buf1->y; y++) {
114                                 float *fac= mask->rect + y*buf1->x;
115                                 for(x=offset; x>0; x--, fac++)
116                                         *fac= 1.0f;
117                         }
118                 }
119                 else {
120                         for(y=0; y<offset; y++) {
121                                 float *fac= mask->rect + y*buf1->x;
122                                 for(x=buf1->x; x>0; x--, fac++)
123                                         *fac= 1.0f;
124                         }
125                 }
126                 
127                 composit3_pixel_processor(node, cbuf, buf1, in[0]->vec, buf2, in[1]->vec, mask, NULL, do_copy_split_rgba, CB_RGBA, CB_RGBA, CB_VAL);
128                 
129                 BKE_image_release_ibuf(ima, lock);
130                 
131                 generate_preview(data, node, cbuf);
132                 free_compbuf(cbuf);
133                 free_compbuf(mask);
134                 
135                 if(in[0]->data != buf1) 
136                         free_compbuf(buf1);
137                 if(in[1]->data != buf2) 
138                         free_compbuf(buf2);
139         }
140 }
141
142 static void node_composit_init_splitviewer(bNodeTree *UNUSED(ntree), bNode* node, bNodeTemplate *UNUSED(ntemp))
143 {
144         ImageUser *iuser= MEM_callocN(sizeof(ImageUser), "node image user");
145         node->storage= iuser;
146         iuser->sfra= 1;
147         iuser->fie_ima= 2;
148         iuser->ok= 1;
149         node->custom1= 50;      /* default 50% split */
150 }
151
152 void register_node_type_cmp_splitviewer(ListBase *lb)
153 {
154         static bNodeType ntype;
155
156         node_type_base(&ntype, CMP_NODE_SPLITVIEWER, "SplitViewer", NODE_CLASS_OUTPUT, NODE_PREVIEW|NODE_OPTIONS);
157         node_type_socket_templates(&ntype, cmp_node_splitviewer_in, NULL);
158         node_type_size(&ntype, 140, 100, 320);
159         node_type_init(&ntype, node_composit_init_splitviewer);
160         node_type_storage(&ntype, "ImageUser", node_free_standard_storage, node_copy_standard_storage);
161         node_type_exec(&ntype, node_composit_exec_splitviewer);
162
163         nodeRegisterType(lb, &ntype);
164 }
165
166
167
168
169