Initial commit. Not in build system so shouldn't interfere with anything at this...
[blender-staging.git] / source / blender / nodes / intern / CMP_nodes / CMP_splitViewer.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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, 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 #include "../CMP_util.h"
31
32 /* **************** SPLIT VIEWER ******************** */
33 static bNodeSocketType cmp_node_splitviewer_in[]= {
34         {       SOCK_RGBA, 1, "Image",          0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f},
35         {       SOCK_RGBA, 1, "Image",          0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f},
36         {       -1, 0, ""       }
37 };
38
39 static void do_copy_split_rgba(bNode *node, float *out, float *in1, float *in2, float *fac)
40 {
41         if(*fac==0.0f) {
42                 QUATCOPY(out, in1);
43         }
44         else {
45                 QUATCOPY(out, in2);
46         }
47 }
48
49 static void node_composit_exec_splitviewer(void *data, bNode *node, bNodeStack **in, bNodeStack **out)
50 {
51         /* image assigned to output */
52         /* stack order input sockets: image image */
53         
54         if(in[0]->data==NULL || in[1]->data==NULL)
55                 return;
56         
57         if(node->id && (node->flag & NODE_DO_OUTPUT)) { /* only one works on out */
58                 Image *ima= (Image *)node->id;
59                 RenderData *rd= data;
60                 ImBuf *ibuf;
61                 CompBuf *cbuf, *buf1, *buf2, *mask;
62                 int x, y;
63                 float offset;
64                 
65                 buf1= typecheck_compbuf(in[0]->data, CB_RGBA);
66                 buf2= typecheck_compbuf(in[1]->data, CB_RGBA);
67                 
68                 BKE_image_user_calc_imanr(node->storage, rd->cfra, 0);
69                 
70                 /* always returns for viewer image, but we check nevertheless */
71                 ibuf= BKE_image_get_ibuf(ima, node->storage);
72                 if(ibuf==NULL) {
73                         printf("node_composit_exec_viewer error\n");
74                         return;
75                 }
76                 
77                 /* free all in ibuf */
78                 imb_freerectImBuf(ibuf);
79                 imb_freerectfloatImBuf(ibuf);
80                 IMB_freezbuffloatImBuf(ibuf);
81                 
82                 /* make ibuf, and connect to ima */
83                 ibuf->x= buf1->x;
84                 ibuf->y= buf1->y;
85                 imb_addrectfloatImBuf(ibuf);
86                 
87                 ima->ok= IMA_OK_LOADED;
88
89                 /* output buf */
90                 cbuf= alloc_compbuf(buf1->x, buf1->y, CB_RGBA, 0);      /* no alloc*/
91                 cbuf->rect= ibuf->rect_float;
92                 
93                 /* mask buf */
94                 mask= alloc_compbuf(buf1->x, buf1->y, CB_VAL, 1);
95                 
96                 
97                 /* Check which offset mode is selected and limit offset if needed */
98                 if(node->custom2 == 0) {
99                         offset = buf1->x / 100.0f * node->custom1;
100                         CLAMP(offset, 0, buf1->x);
101                 }
102                 else {
103                         offset = buf1->y / 100.0f * node->custom1;
104                         CLAMP(offset, 0, buf1->y);
105                 }
106                 
107                 if(node->custom2 == 0) {
108                         for(y=0; y<buf1->y; y++) {
109                                 float *fac= mask->rect + y*buf1->x;
110                                 for(x=offset; x>0; x--, fac++)
111                                         *fac= 1.0f;
112                         }
113                 }
114                 else {
115                         for(y=0; y<offset; y++) {
116                                 float *fac= mask->rect + y*buf1->x;
117                                 for(x=buf1->x; x>0; x--, fac++)
118                                         *fac= 1.0f;
119                         }
120                 }
121                 
122                 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);
123                 
124                 generate_preview(node, cbuf);
125                 free_compbuf(cbuf);
126                 free_compbuf(mask);
127                 
128                 if(in[0]->data != buf1) 
129                         free_compbuf(buf1);
130                 if(in[1]->data != buf2) 
131                         free_compbuf(buf2);
132         }
133 }
134
135 static int node_composit_buts_splitviewer(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
136 {
137    if(block) {  
138       uiBlockBeginAlign(block);
139
140       uiDefButS(block, ROW, B_NODE_EXEC+node->nr, "X",
141          butr->xmin, butr->ymin+19, (butr->xmax-butr->xmin)/2, 20, 
142          &node->custom2, 0.0, 0.0, 0, 0, "");
143       uiDefButS(block, ROW, B_NODE_EXEC+node->nr, "Y",
144          butr->xmin+(butr->xmax-butr->xmin)/2, butr->ymin+19, (butr->xmax-butr->xmin)/2, 20, 
145          &node->custom2, 0.0, 1.0, 0, 0, "");
146
147       uiDefButS(block, NUMSLI, B_NODE_EXEC+node->nr, "Split %: ",
148          butr->xmin, butr->ymin, butr->xmax-butr->xmin, 20, &node->custom1, 0, 100, 10, 0, "");
149    }
150    return 40;
151 }
152
153 static void node_composit_init_splitviewer(bNode* node)
154 {
155    ImageUser *iuser= MEM_callocN(sizeof(ImageUser), "node image user");
156    node->storage= iuser;
157    iuser->sfra= 1;
158    iuser->fie_ima= 2;
159    iuser->ok= 1;
160    node->custom1= 50;   /* default 50% split */
161 }
162
163 bNodeType cmp_node_splitviewer= {
164         /* type code   */       CMP_NODE_SPLITVIEWER,
165         /* name        */       "SplitViewer",
166         /* width+range */       140, 100, 320,
167         /* class+opts  */       NODE_CLASS_OUTPUT, NODE_PREVIEW|NODE_OPTIONS,
168         /* input sock  */       cmp_node_splitviewer_in,
169         /* output sock */       NULL,
170         /* storage     */       "ImageUser",
171         /* execfunc    */       node_composit_exec_splitviewer,
172    /* butfunc     */ node_composit_buts_splitviewer,
173                      node_composit_init_splitviewer
174 };
175
176
177
178