Further work on the premul option for ton. This option
[blender-staging.git] / source / blender / nodes / intern / CMP_nodes / CMP_image.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
33 /* **************** IMAGE (and RenderResult, multilayer image) ******************** */
34
35 static bNodeSocketType cmp_node_rlayers_out[]= {
36         {       SOCK_RGBA, 0, "Image",          0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f},
37         {       SOCK_VALUE, 0, "Alpha",         1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
38         {       SOCK_VALUE, 0, "Z",                     1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
39         {       SOCK_VECTOR, 0, "Normal",       0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
40         {       SOCK_VECTOR, 0, "UV",           1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
41         {       SOCK_VECTOR, 0, "Speed",        1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
42         {       SOCK_RGBA, 0, "Color",          0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
43         {       SOCK_RGBA, 0, "Diffuse",        0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
44         {       SOCK_RGBA, 0, "Specular",       0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
45         {       SOCK_RGBA, 0, "Shadow",         0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
46         {       SOCK_RGBA, 0, "AO",                     0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
47         {       SOCK_RGBA, 0, "Reflect",        0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
48         {       SOCK_RGBA, 0, "Refract",        0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
49         {       SOCK_RGBA, 0, "Radio",          0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
50         {       SOCK_VALUE, 0, "IndexOB",       0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
51         {       SOCK_VALUE, 0, "Mist",          0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
52         {       -1, 0, ""       }
53 };
54
55
56 /* note: this function is used for multilayer too, to ensure uniform 
57    handling with BKE_image_get_ibuf() */
58 static CompBuf *node_composit_get_image(RenderData *rd, Image *ima, ImageUser *iuser)
59 {
60         ImBuf *ibuf;
61         CompBuf *stackbuf, *old;
62         int type;
63         
64         ibuf= BKE_image_get_ibuf(ima, iuser);
65         if(ibuf==NULL)
66                 return NULL;
67         
68         if(ibuf->rect_float==NULL)
69                 IMB_float_from_rect(ibuf);
70         
71         type= ibuf->channels;
72         
73         if(rd->scemode & R_COMP_CROP) {
74                 stackbuf= get_cropped_compbuf(&rd->disprect, ibuf->rect_float, ibuf->x, ibuf->y, type);
75         }
76         else {
77                 /* we put imbuf copy on stack, cbuf knows rect is from other ibuf when freed! */
78                 stackbuf= alloc_compbuf(ibuf->x, ibuf->y, type, 0);
79                 stackbuf->rect= ibuf->rect_float;
80         }
81         
82         old = stackbuf;
83         stackbuf = dupalloc_compbuf(stackbuf);
84         free_compbuf(old);      
85
86         if (type==CB_RGBA && iuser && (iuser->flag & IMA_DO_PREMUL)) {
87                 //premul the image
88                 int i;
89                 float *pixel = stackbuf->rect;
90                 
91                 for (i=0; i<stackbuf->x*stackbuf->y; i++, pixel += 4) {
92                         pixel[0] *= pixel[3];
93                         pixel[1] *= pixel[3];
94                         pixel[2] *= pixel[3];
95                 }
96         }
97         
98         return stackbuf;
99 };
100
101 static CompBuf *node_composit_get_zimage(bNode *node, RenderData *rd)
102 {
103         ImBuf *ibuf= BKE_image_get_ibuf((Image *)node->id, node->storage);
104         CompBuf *zbuf= NULL;
105         
106         if(ibuf && ibuf->zbuf_float) {
107                 if(rd->scemode & R_COMP_CROP) {
108                         zbuf= get_cropped_compbuf(&rd->disprect, ibuf->zbuf_float, ibuf->x, ibuf->y, CB_VAL);
109                 }
110                 else {
111                         zbuf= alloc_compbuf(ibuf->x, ibuf->y, CB_VAL, 0);
112                         zbuf->rect= ibuf->zbuf_float;
113                 }
114         }
115         return zbuf;
116 };
117
118 /* check if layer is available, returns pass buffer */
119 static CompBuf *compbuf_multilayer_get(RenderData *rd, RenderLayer *rl, Image *ima, ImageUser *iuser, int passtype)
120 {
121         RenderPass *rpass;
122         short index;
123         
124         for(index=0, rpass= rl->passes.first; rpass; rpass= rpass->next, index++)
125                 if(rpass->passtype==passtype)
126                         break;
127         
128         if(rpass) {
129                 CompBuf *cbuf;
130                 
131                 iuser->pass= index;
132                 BKE_image_multilayer_index(ima->rr, iuser);
133                 cbuf= node_composit_get_image(rd, ima, iuser);
134                 
135                 return cbuf;
136         }
137         return NULL;
138 };
139
140 void outputs_multilayer_get(RenderData *rd, RenderLayer *rl, bNodeStack **out, Image *ima, ImageUser *iuser)
141 {
142         if(out[RRES_OUT_Z]->hasoutput)
143                 out[RRES_OUT_Z]->data= compbuf_multilayer_get(rd, rl, ima, iuser, SCE_PASS_Z);
144         if(out[RRES_OUT_VEC]->hasoutput)
145                 out[RRES_OUT_VEC]->data= compbuf_multilayer_get(rd, rl, ima, iuser, SCE_PASS_VECTOR);
146         if(out[RRES_OUT_NORMAL]->hasoutput)
147                 out[RRES_OUT_NORMAL]->data= compbuf_multilayer_get(rd, rl, ima, iuser, SCE_PASS_NORMAL);
148         if(out[RRES_OUT_UV]->hasoutput)
149                 out[RRES_OUT_UV]->data= compbuf_multilayer_get(rd, rl, ima, iuser, SCE_PASS_UV);
150         
151         if(out[RRES_OUT_RGBA]->hasoutput)
152                 out[RRES_OUT_RGBA]->data= compbuf_multilayer_get(rd, rl, ima, iuser, SCE_PASS_RGBA);
153         if(out[RRES_OUT_DIFF]->hasoutput)
154                 out[RRES_OUT_DIFF]->data= compbuf_multilayer_get(rd, rl, ima, iuser, SCE_PASS_DIFFUSE);
155         if(out[RRES_OUT_SPEC]->hasoutput)
156                 out[RRES_OUT_SPEC]->data= compbuf_multilayer_get(rd, rl, ima, iuser, SCE_PASS_SPEC);
157         if(out[RRES_OUT_SHADOW]->hasoutput)
158                 out[RRES_OUT_SHADOW]->data= compbuf_multilayer_get(rd, rl, ima, iuser, SCE_PASS_SHADOW);
159         if(out[RRES_OUT_AO]->hasoutput)
160                 out[RRES_OUT_AO]->data= compbuf_multilayer_get(rd, rl, ima, iuser, SCE_PASS_AO);
161         if(out[RRES_OUT_REFLECT]->hasoutput)
162                 out[RRES_OUT_REFLECT]->data= compbuf_multilayer_get(rd, rl, ima, iuser, SCE_PASS_REFLECT);
163         if(out[RRES_OUT_REFRACT]->hasoutput)
164                 out[RRES_OUT_REFRACT]->data= compbuf_multilayer_get(rd, rl, ima, iuser, SCE_PASS_REFRACT);
165         if(out[RRES_OUT_RADIO]->hasoutput)
166                 out[RRES_OUT_RADIO]->data= compbuf_multilayer_get(rd, rl, ima, iuser, SCE_PASS_RADIO);
167         if(out[RRES_OUT_INDEXOB]->hasoutput)
168                 out[RRES_OUT_INDEXOB]->data= compbuf_multilayer_get(rd, rl, ima, iuser, SCE_PASS_INDEXOB);
169         if(out[RRES_OUT_MIST]->hasoutput)
170                 out[RRES_OUT_MIST]->data= compbuf_multilayer_get(rd, rl, ima, iuser, SCE_PASS_MIST);
171         
172 };
173
174
175 static void node_composit_exec_image(void *data, bNode *node, bNodeStack **in, bNodeStack **out)
176 {
177         
178         /* image assigned to output */
179         /* stack order input sockets: col, alpha */
180         if(node->id) {
181                 RenderData *rd= data;
182                 Image *ima= (Image *)node->id;
183                 ImageUser *iuser= (ImageUser *)node->storage;
184                 CompBuf *stackbuf= NULL;
185                 
186                 /* first set the right frame number in iuser */
187                 BKE_image_user_calc_imanr(iuser, rd->cfra, 0);
188                 
189                 /* force a load, we assume iuser index will be set OK anyway */
190                 if(ima->type==IMA_TYPE_MULTILAYER)
191                         BKE_image_get_ibuf(ima, iuser);
192                 
193                 if(ima->type==IMA_TYPE_MULTILAYER && ima->rr) {
194                         RenderLayer *rl= BLI_findlink(&ima->rr->layers, iuser->layer);
195                         
196                         if(rl) {
197                                 out[0]->data= stackbuf= compbuf_multilayer_get(rd, rl, ima, iuser, SCE_PASS_COMBINED);
198                                 
199                                 /* go over all layers */
200                                 outputs_multilayer_get(rd, rl, out, ima, iuser);
201                         }
202                 }
203                 else {
204                         stackbuf= node_composit_get_image(rd, ima, iuser);
205                         
206                         /* put image on stack */        
207                         out[0]->data= stackbuf;
208                         
209                         if(out[2]->hasoutput)
210                                 out[2]->data= node_composit_get_zimage(node, rd);
211                 }
212                 
213                 /* alpha and preview for both types */
214                 if(stackbuf) {
215                         if(out[1]->hasoutput)
216                                 out[1]->data= valbuf_from_rgbabuf(stackbuf, CHAN_A);
217
218                         generate_preview(node, stackbuf);
219                 }
220         }       
221 };
222
223 static void node_composit_init_image(bNode* node)
224 {
225    ImageUser *iuser= MEM_callocN(sizeof(ImageUser), "node image user");
226    node->storage= iuser;
227    iuser->sfra= 1;
228    iuser->fie_ima= 2;
229    iuser->ok= 1;
230 }
231
232 bNodeType cmp_node_image= {
233         /* *next,*prev */       NULL, NULL,
234         /* type code   */       CMP_NODE_IMAGE,
235         /* name        */       "Image",
236         /* width+range */       120, 80, 300,
237         /* class+opts  */       NODE_CLASS_INPUT, NODE_PREVIEW|NODE_OPTIONS,
238         /* input sock  */       NULL,
239         /* output sock */       cmp_node_rlayers_out,
240         /* storage     */       "ImageUser",
241         /* execfunc    */       node_composit_exec_image,
242         /* butfunc     */       NULL,
243         /* initfunc    */       node_composit_init_image,
244         /* freestoragefunc    */        node_free_standard_storage,
245         /* copystoragefunc    */        node_copy_standard_storage,
246         /* id          */       NULL
247 };
248
249 /* **************** RENDER RESULT ******************** */
250
251 static CompBuf *compbuf_from_pass(RenderData *rd, RenderLayer *rl, int rectx, int recty, int passcode)
252 {
253    float *fp= RE_RenderLayerGetPass(rl, passcode);
254    if(fp) {
255       CompBuf *buf;
256       int buftype= CB_VEC3;
257
258       if(ELEM3(passcode, SCE_PASS_Z, SCE_PASS_INDEXOB, SCE_PASS_MIST))
259          buftype= CB_VAL;
260       else if(passcode==SCE_PASS_VECTOR)
261          buftype= CB_VEC4;
262       else if(ELEM(passcode, SCE_PASS_COMBINED, SCE_PASS_RGBA))
263          buftype= CB_RGBA;
264
265       if(rd->scemode & R_COMP_CROP)
266          buf= get_cropped_compbuf(&rd->disprect, fp, rectx, recty, buftype);
267       else {
268          buf= alloc_compbuf(rectx, recty, buftype, 0);
269          buf->rect= fp;
270       }
271       return buf;
272    }
273    return NULL;
274 };
275
276 void node_composit_rlayers_out(RenderData *rd, RenderLayer *rl, bNodeStack **out, int rectx, int recty)
277 {
278    if(out[RRES_OUT_Z]->hasoutput)
279       out[RRES_OUT_Z]->data= compbuf_from_pass(rd, rl, rectx, recty, SCE_PASS_Z);
280    if(out[RRES_OUT_VEC]->hasoutput)
281       out[RRES_OUT_VEC]->data= compbuf_from_pass(rd, rl, rectx, recty, SCE_PASS_VECTOR);
282    if(out[RRES_OUT_NORMAL]->hasoutput)
283       out[RRES_OUT_NORMAL]->data= compbuf_from_pass(rd, rl, rectx, recty, SCE_PASS_NORMAL);
284    if(out[RRES_OUT_UV]->hasoutput)
285       out[RRES_OUT_UV]->data= compbuf_from_pass(rd, rl, rectx, recty, SCE_PASS_UV);
286
287    if(out[RRES_OUT_RGBA]->hasoutput)
288       out[RRES_OUT_RGBA]->data= compbuf_from_pass(rd, rl, rectx, recty, SCE_PASS_RGBA);
289    if(out[RRES_OUT_DIFF]->hasoutput)
290       out[RRES_OUT_DIFF]->data= compbuf_from_pass(rd, rl, rectx, recty, SCE_PASS_DIFFUSE);
291    if(out[RRES_OUT_SPEC]->hasoutput)
292       out[RRES_OUT_SPEC]->data= compbuf_from_pass(rd, rl, rectx, recty, SCE_PASS_SPEC);
293    if(out[RRES_OUT_SHADOW]->hasoutput)
294       out[RRES_OUT_SHADOW]->data= compbuf_from_pass(rd, rl, rectx, recty, SCE_PASS_SHADOW);
295    if(out[RRES_OUT_AO]->hasoutput)
296       out[RRES_OUT_AO]->data= compbuf_from_pass(rd, rl, rectx, recty, SCE_PASS_AO);
297    if(out[RRES_OUT_REFLECT]->hasoutput)
298       out[RRES_OUT_REFLECT]->data= compbuf_from_pass(rd, rl, rectx, recty, SCE_PASS_REFLECT);
299    if(out[RRES_OUT_REFRACT]->hasoutput)
300       out[RRES_OUT_REFRACT]->data= compbuf_from_pass(rd, rl, rectx, recty, SCE_PASS_REFRACT);
301    if(out[RRES_OUT_RADIO]->hasoutput)
302       out[RRES_OUT_RADIO]->data= compbuf_from_pass(rd, rl, rectx, recty, SCE_PASS_RADIO);
303    if(out[RRES_OUT_INDEXOB]->hasoutput)
304            out[RRES_OUT_INDEXOB]->data= compbuf_from_pass(rd, rl, rectx, recty, SCE_PASS_INDEXOB);
305    if(out[RRES_OUT_MIST]->hasoutput)
306            out[RRES_OUT_MIST]->data= compbuf_from_pass(rd, rl, rectx, recty, SCE_PASS_MIST);
307
308 };
309
310 static void node_composit_exec_rlayers(void *data, bNode *node, bNodeStack **in, bNodeStack **out)
311 {
312    Scene *sce= node->id?(Scene *)node->id:G.scene; /* G.scene is WEAK! */
313    RenderData *rd= data;
314    RenderResult *rr;
315
316    rr= RE_GetResult(RE_GetRender(sce->id.name));
317
318    if(rr) {
319       SceneRenderLayer *srl= BLI_findlink(&sce->r.layers, node->custom1);
320       if(srl) {
321          RenderLayer *rl= RE_GetRenderLayer(rr, srl->name);
322          if(rl && rl->rectf) {
323             CompBuf *stackbuf;
324
325             /* we put render rect on stack, cbuf knows rect is from other ibuf when freed! */
326             if(rd->scemode & R_COMP_CROP)
327                stackbuf= get_cropped_compbuf(&rd->disprect, rl->rectf, rr->rectx, rr->recty, CB_RGBA);
328             else {
329                stackbuf= alloc_compbuf(rr->rectx, rr->recty, CB_RGBA, 0);
330                stackbuf->rect= rl->rectf;
331             }
332             if(stackbuf==NULL) {
333                printf("Error; Preview Panel in UV Window returns zero sized image\n");
334             }
335             else {
336                stackbuf->xof= rr->xof;
337                stackbuf->yof= rr->yof;
338
339                /* put on stack */       
340                out[RRES_OUT_IMAGE]->data= stackbuf;
341
342                if(out[RRES_OUT_ALPHA]->hasoutput)
343                   out[RRES_OUT_ALPHA]->data= valbuf_from_rgbabuf(stackbuf, CHAN_A);
344
345                node_composit_rlayers_out(rd, rl, out, rr->rectx, rr->recty);
346
347                generate_preview(node, stackbuf);
348             }
349          }
350       }
351    }    
352 };
353
354
355 bNodeType cmp_node_rlayers= {
356         /* *next,*prev */       NULL, NULL,
357         /* type code   */       CMP_NODE_R_LAYERS,
358         /* name        */       "Render Layers",
359         /* width+range */       150, 100, 300,
360         /* class+opts  */       NODE_CLASS_INPUT, NODE_PREVIEW|NODE_OPTIONS,
361         /* input sock  */       NULL,
362         /* output sock */       cmp_node_rlayers_out,
363         /* storage     */       "",
364         /* execfunc    */       node_composit_exec_rlayers,
365         /* butfunc     */       NULL,
366         /* initfunc    */       NULL,
367         /* freestoragefunc    */        NULL,
368         /* copystoragefunc    */        NULL,
369         /* id          */       NULL
370
371 };
372
373