commit before doing some hefty shapekey change, will break compilation
[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;
62         int type;
63         
64         ibuf= BKE_image_get_ibuf(ima, iuser);
65         if(ibuf==NULL)
66                 return NULL;
67         
68         if (rd->color_mgt_flag & R_COLOR_MANAGEMENT) {
69                 if (ibuf->profile == IB_PROFILE_NONE) {
70                         /* if float buffer already exists = already linear */
71                         /* else ... */
72                         if (ibuf->rect_float == NULL) {
73                                 imb_freerectfloatImBuf(ibuf);
74                                 ibuf->profile = IB_PROFILE_SRGB;
75                                 IMB_float_from_rect(ibuf);
76                         } else {
77                                 ibuf->profile = IB_PROFILE_LINEAR_RGB;
78                         }
79                 }
80         } else {
81                 if (ibuf->profile == IB_PROFILE_SRGB) {
82                         if (ibuf->rect_float != NULL) {
83                                 imb_freerectfloatImBuf(ibuf);
84                         }
85                         ibuf->profile = IB_PROFILE_NONE;
86                         IMB_float_from_rect(ibuf);
87                 }
88         }
89         
90         if (ibuf->rect_float == NULL) {
91                 IMB_float_from_rect(ibuf);
92         }
93
94         type= ibuf->channels;
95         
96         if(rd->scemode & R_COMP_CROP) {
97                 stackbuf= get_cropped_compbuf(&rd->disprect, ibuf->rect_float, ibuf->x, ibuf->y, type);
98         }
99         else {
100                 /* we put imbuf copy on stack, cbuf knows rect is from other ibuf when freed! */
101                 stackbuf= alloc_compbuf(ibuf->x, ibuf->y, type, 0);
102                 stackbuf->rect= ibuf->rect_float;
103         }
104         
105         /*code to respect the premul flag of images; I'm
106           not sure if this is a good idea for multilayer images,
107           since it never worked before for them.
108         if (type==CB_RGBA && ima->flag & IMA_DO_PREMUL) {
109                 //premul the image
110                 int i;
111                 float *pixel = stackbuf->rect;
112                 
113                 for (i=0; i<stackbuf->x*stackbuf->y; i++, pixel += 4) {
114                         pixel[0] *= pixel[3];
115                         pixel[1] *= pixel[3];
116                         pixel[2] *= pixel[3];
117                 }
118         }
119         */
120         return stackbuf;
121 };
122
123 static CompBuf *node_composit_get_zimage(bNode *node, RenderData *rd)
124 {
125         ImBuf *ibuf= BKE_image_get_ibuf((Image *)node->id, node->storage);
126         CompBuf *zbuf= NULL;
127         
128         if(ibuf && ibuf->zbuf_float) {
129                 if(rd->scemode & R_COMP_CROP) {
130                         zbuf= get_cropped_compbuf(&rd->disprect, ibuf->zbuf_float, ibuf->x, ibuf->y, CB_VAL);
131                 }
132                 else {
133                         zbuf= alloc_compbuf(ibuf->x, ibuf->y, CB_VAL, 0);
134                         zbuf->rect= ibuf->zbuf_float;
135                 }
136         }
137         return zbuf;
138 };
139
140 /* check if layer is available, returns pass buffer */
141 static CompBuf *compbuf_multilayer_get(RenderData *rd, RenderLayer *rl, Image *ima, ImageUser *iuser, int passtype)
142 {
143         RenderPass *rpass;
144         short index;
145         
146         for(index=0, rpass= rl->passes.first; rpass; rpass= rpass->next, index++)
147                 if(rpass->passtype==passtype)
148                         break;
149         
150         if(rpass) {
151                 CompBuf *cbuf;
152                 
153                 iuser->pass= index;
154                 BKE_image_multilayer_index(ima->rr, iuser);
155                 cbuf= node_composit_get_image(rd, ima, iuser);
156                 
157                 return cbuf;
158         }
159         return NULL;
160 };
161
162 void outputs_multilayer_get(RenderData *rd, RenderLayer *rl, bNodeStack **out, Image *ima, ImageUser *iuser)
163 {
164         if(out[RRES_OUT_Z]->hasoutput)
165                 out[RRES_OUT_Z]->data= compbuf_multilayer_get(rd, rl, ima, iuser, SCE_PASS_Z);
166         if(out[RRES_OUT_VEC]->hasoutput)
167                 out[RRES_OUT_VEC]->data= compbuf_multilayer_get(rd, rl, ima, iuser, SCE_PASS_VECTOR);
168         if(out[RRES_OUT_NORMAL]->hasoutput)
169                 out[RRES_OUT_NORMAL]->data= compbuf_multilayer_get(rd, rl, ima, iuser, SCE_PASS_NORMAL);
170         if(out[RRES_OUT_UV]->hasoutput)
171                 out[RRES_OUT_UV]->data= compbuf_multilayer_get(rd, rl, ima, iuser, SCE_PASS_UV);
172         
173         if(out[RRES_OUT_RGBA]->hasoutput)
174                 out[RRES_OUT_RGBA]->data= compbuf_multilayer_get(rd, rl, ima, iuser, SCE_PASS_RGBA);
175         if(out[RRES_OUT_DIFF]->hasoutput)
176                 out[RRES_OUT_DIFF]->data= compbuf_multilayer_get(rd, rl, ima, iuser, SCE_PASS_DIFFUSE);
177         if(out[RRES_OUT_SPEC]->hasoutput)
178                 out[RRES_OUT_SPEC]->data= compbuf_multilayer_get(rd, rl, ima, iuser, SCE_PASS_SPEC);
179         if(out[RRES_OUT_SHADOW]->hasoutput)
180                 out[RRES_OUT_SHADOW]->data= compbuf_multilayer_get(rd, rl, ima, iuser, SCE_PASS_SHADOW);
181         if(out[RRES_OUT_AO]->hasoutput)
182                 out[RRES_OUT_AO]->data= compbuf_multilayer_get(rd, rl, ima, iuser, SCE_PASS_AO);
183         if(out[RRES_OUT_REFLECT]->hasoutput)
184                 out[RRES_OUT_REFLECT]->data= compbuf_multilayer_get(rd, rl, ima, iuser, SCE_PASS_REFLECT);
185         if(out[RRES_OUT_REFRACT]->hasoutput)
186                 out[RRES_OUT_REFRACT]->data= compbuf_multilayer_get(rd, rl, ima, iuser, SCE_PASS_REFRACT);
187         if(out[RRES_OUT_RADIO]->hasoutput)
188                 out[RRES_OUT_RADIO]->data= compbuf_multilayer_get(rd, rl, ima, iuser, SCE_PASS_RADIO);
189         if(out[RRES_OUT_INDEXOB]->hasoutput)
190                 out[RRES_OUT_INDEXOB]->data= compbuf_multilayer_get(rd, rl, ima, iuser, SCE_PASS_INDEXOB);
191         if(out[RRES_OUT_MIST]->hasoutput)
192                 out[RRES_OUT_MIST]->data= compbuf_multilayer_get(rd, rl, ima, iuser, SCE_PASS_MIST);
193         
194 };
195
196
197 static void node_composit_exec_image(void *data, bNode *node, bNodeStack **in, bNodeStack **out)
198 {
199         
200         /* image assigned to output */
201         /* stack order input sockets: col, alpha */
202         if(node->id) {
203                 RenderData *rd= data;
204                 Image *ima= (Image *)node->id;
205                 ImageUser *iuser= (ImageUser *)node->storage;
206                 CompBuf *stackbuf= NULL;
207                 
208                 /* first set the right frame number in iuser */
209                 BKE_image_user_calc_imanr(iuser, rd->cfra, 0);
210                 
211                 /* force a load, we assume iuser index will be set OK anyway */
212                 if(ima->type==IMA_TYPE_MULTILAYER)
213                         BKE_image_get_ibuf(ima, iuser);
214                 
215                 if(ima->type==IMA_TYPE_MULTILAYER && ima->rr) {
216                         RenderLayer *rl= BLI_findlink(&ima->rr->layers, iuser->layer);
217                         
218                         if(rl) {
219                                 out[0]->data= stackbuf= compbuf_multilayer_get(rd, rl, ima, iuser, SCE_PASS_COMBINED);
220                                 
221                                 /* go over all layers */
222                                 outputs_multilayer_get(rd, rl, out, ima, iuser);
223                         }
224                 }
225                 else {
226                         stackbuf= node_composit_get_image(rd, ima, iuser);
227
228                         if (stackbuf) {
229                                 /*respect image premul option*/
230                                 if (stackbuf->type==CB_RGBA && ima->flag & IMA_DO_PREMUL) {
231                                         int i;
232                                         float *pixel;
233                         
234                                         /*first duplicate stackbuf->rect, since it's just a pointer
235                                           to the source imbuf, and we don't want to change that.*/
236                                         stackbuf->rect = MEM_dupallocN(stackbuf->rect);
237                                 
238                                         /*premul the image*/
239                                 
240                                         pixel = stackbuf->rect;
241                                         for (i=0; i<stackbuf->x*stackbuf->y; i++, pixel += 4) {
242                                                 pixel[0] *= pixel[3];
243                                                 pixel[1] *= pixel[3];
244                                                 pixel[2] *= pixel[3];
245                                         }
246                                 }
247                         
248                                 /* put image on stack */        
249                                 out[0]->data= stackbuf;
250                         
251                                 if(out[2]->hasoutput)
252                                         out[2]->data= node_composit_get_zimage(node, rd);
253                         }
254                 }
255                 
256                 /* alpha and preview for both types */
257                 if(stackbuf) {
258                         if(out[1]->hasoutput)
259                                 out[1]->data= valbuf_from_rgbabuf(stackbuf, CHAN_A);
260
261                         generate_preview(data, node, stackbuf);
262                 }
263         }       
264 };
265
266 static void node_composit_init_image(bNode* node)
267 {
268    ImageUser *iuser= MEM_callocN(sizeof(ImageUser), "node image user");
269    node->storage= iuser;
270    iuser->frames= 1;
271    iuser->sfra= 1;
272    iuser->fie_ima= 2;
273    iuser->ok= 1;
274 }
275
276 bNodeType cmp_node_image= {
277         /* *next,*prev */       NULL, NULL,
278         /* type code   */       CMP_NODE_IMAGE,
279         /* name        */       "Image",
280         /* width+range */       120, 80, 300,
281         /* class+opts  */       NODE_CLASS_INPUT, NODE_PREVIEW|NODE_OPTIONS,
282         /* input sock  */       NULL,
283         /* output sock */       cmp_node_rlayers_out,
284         /* storage     */       "ImageUser",
285         /* execfunc    */       node_composit_exec_image,
286         /* butfunc     */       NULL,
287         /* initfunc    */       node_composit_init_image,
288         /* freestoragefunc    */        node_free_standard_storage,
289         /* copystoragefunc    */        node_copy_standard_storage,
290         /* id          */       NULL
291 };
292
293 /* **************** RENDER RESULT ******************** */
294
295 static CompBuf *compbuf_from_pass(RenderData *rd, RenderLayer *rl, int rectx, int recty, int passcode)
296 {
297    float *fp= RE_RenderLayerGetPass(rl, passcode);
298    if(fp) {
299       CompBuf *buf;
300       int buftype= CB_VEC3;
301
302       if(ELEM3(passcode, SCE_PASS_Z, SCE_PASS_INDEXOB, SCE_PASS_MIST))
303          buftype= CB_VAL;
304       else if(passcode==SCE_PASS_VECTOR)
305          buftype= CB_VEC4;
306       else if(ELEM(passcode, SCE_PASS_COMBINED, SCE_PASS_RGBA))
307          buftype= CB_RGBA;
308
309       if(rd->scemode & R_COMP_CROP)
310          buf= get_cropped_compbuf(&rd->disprect, fp, rectx, recty, buftype);
311       else {
312          buf= alloc_compbuf(rectx, recty, buftype, 0);
313          buf->rect= fp;
314       }
315       return buf;
316    }
317    return NULL;
318 };
319
320 void node_composit_rlayers_out(RenderData *rd, RenderLayer *rl, bNodeStack **out, int rectx, int recty)
321 {
322    if(out[RRES_OUT_Z]->hasoutput)
323       out[RRES_OUT_Z]->data= compbuf_from_pass(rd, rl, rectx, recty, SCE_PASS_Z);
324    if(out[RRES_OUT_VEC]->hasoutput)
325       out[RRES_OUT_VEC]->data= compbuf_from_pass(rd, rl, rectx, recty, SCE_PASS_VECTOR);
326    if(out[RRES_OUT_NORMAL]->hasoutput)
327       out[RRES_OUT_NORMAL]->data= compbuf_from_pass(rd, rl, rectx, recty, SCE_PASS_NORMAL);
328    if(out[RRES_OUT_UV]->hasoutput)
329       out[RRES_OUT_UV]->data= compbuf_from_pass(rd, rl, rectx, recty, SCE_PASS_UV);
330
331    if(out[RRES_OUT_RGBA]->hasoutput)
332       out[RRES_OUT_RGBA]->data= compbuf_from_pass(rd, rl, rectx, recty, SCE_PASS_RGBA);
333    if(out[RRES_OUT_DIFF]->hasoutput)
334       out[RRES_OUT_DIFF]->data= compbuf_from_pass(rd, rl, rectx, recty, SCE_PASS_DIFFUSE);
335    if(out[RRES_OUT_SPEC]->hasoutput)
336       out[RRES_OUT_SPEC]->data= compbuf_from_pass(rd, rl, rectx, recty, SCE_PASS_SPEC);
337    if(out[RRES_OUT_SHADOW]->hasoutput)
338       out[RRES_OUT_SHADOW]->data= compbuf_from_pass(rd, rl, rectx, recty, SCE_PASS_SHADOW);
339    if(out[RRES_OUT_AO]->hasoutput)
340       out[RRES_OUT_AO]->data= compbuf_from_pass(rd, rl, rectx, recty, SCE_PASS_AO);
341    if(out[RRES_OUT_REFLECT]->hasoutput)
342       out[RRES_OUT_REFLECT]->data= compbuf_from_pass(rd, rl, rectx, recty, SCE_PASS_REFLECT);
343    if(out[RRES_OUT_REFRACT]->hasoutput)
344       out[RRES_OUT_REFRACT]->data= compbuf_from_pass(rd, rl, rectx, recty, SCE_PASS_REFRACT);
345    if(out[RRES_OUT_RADIO]->hasoutput)
346       out[RRES_OUT_RADIO]->data= compbuf_from_pass(rd, rl, rectx, recty, SCE_PASS_RADIO);
347    if(out[RRES_OUT_INDEXOB]->hasoutput)
348            out[RRES_OUT_INDEXOB]->data= compbuf_from_pass(rd, rl, rectx, recty, SCE_PASS_INDEXOB);
349    if(out[RRES_OUT_MIST]->hasoutput)
350            out[RRES_OUT_MIST]->data= compbuf_from_pass(rd, rl, rectx, recty, SCE_PASS_MIST);
351
352 };
353
354 static void node_composit_exec_rlayers(void *data, bNode *node, bNodeStack **in, bNodeStack **out)
355 {
356    Scene *sce= (Scene *)node->id;
357    Render *re= (sce)? RE_GetRender(sce->id.name): NULL;
358    RenderData *rd= data;
359    RenderResult *rr= NULL;
360
361    if(re)
362            rr= RE_AcquireResultRead(re);
363
364    if(rr) {
365       SceneRenderLayer *srl= BLI_findlink(&sce->r.layers, node->custom1);
366       if(srl) {
367          RenderLayer *rl= RE_GetRenderLayer(rr, srl->name);
368          if(rl && rl->rectf) {
369             CompBuf *stackbuf;
370
371             /* we put render rect on stack, cbuf knows rect is from other ibuf when freed! */
372             if(rd->scemode & R_COMP_CROP)
373                stackbuf= get_cropped_compbuf(&rd->disprect, rl->rectf, rr->rectx, rr->recty, CB_RGBA);
374             else {
375                stackbuf= alloc_compbuf(rr->rectx, rr->recty, CB_RGBA, 0);
376                stackbuf->rect= rl->rectf;
377             }
378             if(stackbuf==NULL) {
379                printf("Error; Preview Panel in UV Window returns zero sized image\n");
380             }
381             else {
382                stackbuf->xof= rr->xof;
383                stackbuf->yof= rr->yof;
384
385                /* put on stack */       
386                out[RRES_OUT_IMAGE]->data= stackbuf;
387
388                if(out[RRES_OUT_ALPHA]->hasoutput)
389                   out[RRES_OUT_ALPHA]->data= valbuf_from_rgbabuf(stackbuf, CHAN_A);
390
391                node_composit_rlayers_out(rd, rl, out, rr->rectx, rr->recty);
392
393                generate_preview(data, node, stackbuf);
394             }
395          }
396       }
397    }
398
399    if(re)
400            RE_ReleaseResult(re);
401 };
402
403
404 bNodeType cmp_node_rlayers= {
405         /* *next,*prev */       NULL, NULL,
406         /* type code   */       CMP_NODE_R_LAYERS,
407         /* name        */       "Render Layers",
408         /* width+range */       150, 100, 300,
409         /* class+opts  */       NODE_CLASS_INPUT, NODE_PREVIEW|NODE_OPTIONS,
410         /* input sock  */       NULL,
411         /* output sock */       cmp_node_rlayers_out,
412         /* storage     */       "",
413         /* execfunc    */       node_composit_exec_rlayers,
414         /* butfunc     */       NULL,
415         /* initfunc    */       NULL,
416         /* freestoragefunc    */        NULL,
417         /* copystoragefunc    */        NULL,
418         /* id          */       NULL
419
420 };
421
422