svn merge ^/trunk/blender -r42920:42927
[blender.git] / source / blender / nodes / composite / nodes / node_composite_image.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_image.c
29  *  \ingroup cmpnodes
30  */
31
32
33 #include "node_composite_util.h"
34
35
36 /* **************** IMAGE (and RenderResult, multilayer image) ******************** */
37
38 static bNodeSocketTemplate cmp_node_rlayers_out[]= {
39         {       SOCK_RGBA, 0, "Image",          0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
40         {       SOCK_FLOAT, 0, "Alpha",         1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
41         {       SOCK_FLOAT, 0, "Z",                     1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
42         {       SOCK_VECTOR, 0, "Normal",       0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
43         {       SOCK_VECTOR, 0, "UV",           1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
44         {       SOCK_VECTOR, 0, "Speed",        1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
45         {       SOCK_RGBA, 0, "Color",          0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
46         {       SOCK_RGBA, 0, "Diffuse",        0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
47         {       SOCK_RGBA, 0, "Specular",       0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
48         {       SOCK_RGBA, 0, "Shadow",         0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
49         {       SOCK_RGBA, 0, "AO",                     0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
50         {       SOCK_RGBA, 0, "Reflect",        0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
51         {       SOCK_RGBA, 0, "Refract",        0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
52         {       SOCK_RGBA, 0, "Indirect",       0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
53         {       SOCK_FLOAT, 0, "IndexOB",       0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
54         {       SOCK_FLOAT, 0, "IndexMA",       0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
55         {       SOCK_FLOAT, 0, "Mist",          0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
56         {       SOCK_RGBA, 0, "Emit",           0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
57         {       SOCK_RGBA, 0, "Environment",0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
58         {       -1, 0, ""       }
59 };
60
61 /* float buffer from the image with matching color management */
62 float *node_composit_get_float_buffer(RenderData *rd, ImBuf *ibuf, int *alloc)
63 {
64         float *rect;
65
66         *alloc= FALSE;
67
68         if(rd->color_mgt_flag & R_COLOR_MANAGEMENT) {
69                 if(ibuf->profile != IB_PROFILE_NONE) {
70                         rect= ibuf->rect_float;
71                 }
72                 else {
73                         rect= MEM_mapallocN(sizeof(float) * 4 * ibuf->x * ibuf->y, "node_composit_get_image");
74                         srgb_to_linearrgb_rgba_rgba_buf(rect, ibuf->rect_float, ibuf->x * ibuf->y);
75                         *alloc= TRUE;
76                 }
77         }
78         else {
79                 if(ibuf->profile == IB_PROFILE_NONE) {
80                         rect= ibuf->rect_float;
81                 }
82                 else {
83                         rect= MEM_mapallocN(sizeof(float) * 4 * ibuf->x * ibuf->y, "node_composit_get_image");
84                         linearrgb_to_srgb_rgba_rgba_buf(rect, ibuf->rect_float, ibuf->x * ibuf->y);
85                         *alloc= TRUE;
86                 }
87         }
88
89         return rect;
90 }
91
92 /* note: this function is used for multilayer too, to ensure uniform 
93    handling with BKE_image_get_ibuf() */
94 static CompBuf *node_composit_get_image(RenderData *rd, Image *ima, ImageUser *iuser)
95 {
96         ImBuf *ibuf;
97         CompBuf *stackbuf;
98         int type;
99
100         float *rect;
101         int alloc= FALSE;
102
103         ibuf= BKE_image_get_ibuf(ima, iuser);
104         if(ibuf==NULL || (ibuf->rect==NULL && ibuf->rect_float==NULL)) {
105                 return NULL;
106         }
107
108         if (ibuf->rect_float == NULL) {
109                 IMB_float_from_rect(ibuf);
110         }
111
112         /* now we need a float buffer from the image with matching color management */
113         /* XXX weak code, multilayer is excluded from this */
114         if(ibuf->channels == 4 && ima->rr==NULL) {
115                 rect= node_composit_get_float_buffer(rd, ibuf, &alloc);
116         }
117         else {
118                 /* non-rgba passes can't use color profiles */
119                 rect= ibuf->rect_float;
120         }
121         /* done coercing into the correct color management */
122
123
124         type= ibuf->channels;
125         
126         if(rd->scemode & R_COMP_CROP) {
127                 stackbuf= get_cropped_compbuf(&rd->disprect, rect, ibuf->x, ibuf->y, type);
128                 if(alloc)
129                         MEM_freeN(rect);
130         }
131         else {
132                 /* we put imbuf copy on stack, cbuf knows rect is from other ibuf when freed! */
133                 stackbuf= alloc_compbuf(ibuf->x, ibuf->y, type, FALSE);
134                 stackbuf->rect= rect;
135                 stackbuf->malloc= alloc;
136         }
137         
138         /*code to respect the premul flag of images; I'm
139           not sure if this is a good idea for multilayer images,
140           since it never worked before for them.
141         if (type==CB_RGBA && ima->flag & IMA_DO_PREMUL) {
142                 //premul the image
143                 int i;
144                 float *pixel = stackbuf->rect;
145                 
146                 for (i=0; i<stackbuf->x*stackbuf->y; i++, pixel += 4) {
147                         pixel[0] *= pixel[3];
148                         pixel[1] *= pixel[3];
149                         pixel[2] *= pixel[3];
150                 }
151         }
152         */
153         return stackbuf;
154 }
155
156 static CompBuf *node_composit_get_zimage(bNode *node, RenderData *rd)
157 {
158         ImBuf *ibuf= BKE_image_get_ibuf((Image *)node->id, node->storage);
159         CompBuf *zbuf= NULL;
160         
161         if(ibuf && ibuf->zbuf_float) {
162                 if(rd->scemode & R_COMP_CROP) {
163                         zbuf= get_cropped_compbuf(&rd->disprect, ibuf->zbuf_float, ibuf->x, ibuf->y, CB_VAL);
164                 }
165                 else {
166                         zbuf= alloc_compbuf(ibuf->x, ibuf->y, CB_VAL, 0);
167                         zbuf->rect= ibuf->zbuf_float;
168                 }
169         }
170         return zbuf;
171 }
172
173 /* check if layer is available, returns pass buffer */
174 static CompBuf *compbuf_multilayer_get(RenderData *rd, RenderLayer *rl, Image *ima, ImageUser *iuser, int passtype)
175 {
176         RenderPass *rpass;
177         short index;
178         
179         for(index=0, rpass= rl->passes.first; rpass; rpass= rpass->next, index++)
180                 if(rpass->passtype==passtype)
181                         break;
182         
183         if(rpass) {
184                 CompBuf *cbuf;
185                 
186                 iuser->pass= index;
187                 BKE_image_multilayer_index(ima->rr, iuser);
188                 cbuf= node_composit_get_image(rd, ima, iuser);
189                 
190                 return cbuf;
191         }
192         return NULL;
193 }
194
195 static void outputs_multilayer_get(RenderData *rd, RenderLayer *rl, bNodeStack **out, Image *ima, ImageUser *iuser)
196 {
197         if(out[RRES_OUT_Z]->hasoutput)
198                 out[RRES_OUT_Z]->data= compbuf_multilayer_get(rd, rl, ima, iuser, SCE_PASS_Z);
199         if(out[RRES_OUT_VEC]->hasoutput)
200                 out[RRES_OUT_VEC]->data= compbuf_multilayer_get(rd, rl, ima, iuser, SCE_PASS_VECTOR);
201         if(out[RRES_OUT_NORMAL]->hasoutput)
202                 out[RRES_OUT_NORMAL]->data= compbuf_multilayer_get(rd, rl, ima, iuser, SCE_PASS_NORMAL);
203         if(out[RRES_OUT_UV]->hasoutput)
204                 out[RRES_OUT_UV]->data= compbuf_multilayer_get(rd, rl, ima, iuser, SCE_PASS_UV);
205         
206         if(out[RRES_OUT_RGBA]->hasoutput)
207                 out[RRES_OUT_RGBA]->data= compbuf_multilayer_get(rd, rl, ima, iuser, SCE_PASS_RGBA);
208         if(out[RRES_OUT_DIFF]->hasoutput)
209                 out[RRES_OUT_DIFF]->data= compbuf_multilayer_get(rd, rl, ima, iuser, SCE_PASS_DIFFUSE);
210         if(out[RRES_OUT_SPEC]->hasoutput)
211                 out[RRES_OUT_SPEC]->data= compbuf_multilayer_get(rd, rl, ima, iuser, SCE_PASS_SPEC);
212         if(out[RRES_OUT_SHADOW]->hasoutput)
213                 out[RRES_OUT_SHADOW]->data= compbuf_multilayer_get(rd, rl, ima, iuser, SCE_PASS_SHADOW);
214         if(out[RRES_OUT_AO]->hasoutput)
215                 out[RRES_OUT_AO]->data= compbuf_multilayer_get(rd, rl, ima, iuser, SCE_PASS_AO);
216         if(out[RRES_OUT_REFLECT]->hasoutput)
217                 out[RRES_OUT_REFLECT]->data= compbuf_multilayer_get(rd, rl, ima, iuser, SCE_PASS_REFLECT);
218         if(out[RRES_OUT_REFRACT]->hasoutput)
219                 out[RRES_OUT_REFRACT]->data= compbuf_multilayer_get(rd, rl, ima, iuser, SCE_PASS_REFRACT);
220         if(out[RRES_OUT_INDIRECT]->hasoutput)
221                 out[RRES_OUT_INDIRECT]->data= compbuf_multilayer_get(rd, rl, ima, iuser, SCE_PASS_INDIRECT);
222         if(out[RRES_OUT_INDEXOB]->hasoutput)
223                 out[RRES_OUT_INDEXOB]->data= compbuf_multilayer_get(rd, rl, ima, iuser, SCE_PASS_INDEXOB);
224         if(out[RRES_OUT_INDEXMA]->hasoutput)
225                 out[RRES_OUT_INDEXMA]->data= compbuf_multilayer_get(rd, rl, ima, iuser, SCE_PASS_INDEXMA);
226         if(out[RRES_OUT_MIST]->hasoutput)
227                 out[RRES_OUT_MIST]->data= compbuf_multilayer_get(rd, rl, ima, iuser, SCE_PASS_MIST);
228         if(out[RRES_OUT_EMIT]->hasoutput)
229                 out[RRES_OUT_EMIT]->data= compbuf_multilayer_get(rd, rl, ima, iuser, SCE_PASS_EMIT);
230         if(out[RRES_OUT_ENV]->hasoutput)
231                 out[RRES_OUT_ENV]->data= compbuf_multilayer_get(rd, rl, ima, iuser, SCE_PASS_ENVIRONMENT);
232 }
233
234
235 static void node_composit_exec_image(void *data, bNode *node, bNodeStack **UNUSED(in), bNodeStack **out)
236 {
237         
238         /* image assigned to output */
239         /* stack order input sockets: col, alpha */
240         if(node->id) {
241                 RenderData *rd= data;
242                 Image *ima= (Image *)node->id;
243                 ImageUser *iuser= (ImageUser *)node->storage;
244                 CompBuf *stackbuf= NULL;
245                 
246                 /* first set the right frame number in iuser */
247                 BKE_image_user_calc_frame(iuser, rd->cfra, 0);
248                 
249                 /* force a load, we assume iuser index will be set OK anyway */
250                 if(ima->type==IMA_TYPE_MULTILAYER)
251                         BKE_image_get_ibuf(ima, iuser);
252                 
253                 if(ima->type==IMA_TYPE_MULTILAYER && ima->rr) {
254                         RenderLayer *rl= BLI_findlink(&ima->rr->layers, iuser->layer);
255                         
256                         if(rl) {
257                                 out[0]->data= stackbuf= compbuf_multilayer_get(rd, rl, ima, iuser, SCE_PASS_COMBINED);
258                                 
259                                 /* go over all layers */
260                                 outputs_multilayer_get(rd, rl, out, ima, iuser);
261                         }
262                 }
263                 else {
264                         stackbuf= node_composit_get_image(rd, ima, iuser);
265
266                         if (stackbuf) {
267                                 /*respect image premul option*/
268                                 if (stackbuf->type==CB_RGBA && ima->flag & IMA_DO_PREMUL) {
269                                         int i;
270                                         float *pixel;
271                         
272                                         /*first duplicate stackbuf->rect, since it's just a pointer
273                                           to the source imbuf, and we don't want to change that.*/
274                                         stackbuf->rect = MEM_dupallocN(stackbuf->rect);
275                                         
276                                         /* since stackbuf now has allocated memory, rather than just a pointer,
277                                          * mark it as allocated so it can be freed properly */
278                                         stackbuf->malloc=1;
279                                         
280                                         /*premul the image*/
281                                         pixel = stackbuf->rect;
282                                         for (i=0; i<stackbuf->x*stackbuf->y; i++, pixel += 4) {
283                                                 pixel[0] *= pixel[3];
284                                                 pixel[1] *= pixel[3];
285                                                 pixel[2] *= pixel[3];
286                                         }
287                                 }
288                         
289                                 /* put image on stack */        
290                                 out[0]->data= stackbuf;
291                         
292                                 if(out[2]->hasoutput)
293                                         out[2]->data= node_composit_get_zimage(node, rd);
294                         }
295                 }
296                 
297                 /* alpha and preview for both types */
298                 if(stackbuf) {
299                         if(out[1]->hasoutput)
300                                 out[1]->data= valbuf_from_rgbabuf(stackbuf, CHAN_A);
301
302                         generate_preview(data, node, stackbuf);
303                 }
304         }       
305 }
306
307 static void node_composit_init_image(bNodeTree *UNUSED(ntree), bNode* node, bNodeTemplate *UNUSED(ntemp))
308 {
309         ImageUser *iuser= MEM_callocN(sizeof(ImageUser), "node image user");
310         node->storage= iuser;
311         iuser->frames= 1;
312         iuser->sfra= 1;
313         iuser->fie_ima= 2;
314         iuser->ok= 1;
315 }
316
317 void register_node_type_cmp_image(bNodeTreeType *ttype)
318 {
319         static bNodeType ntype;
320
321         node_type_base(ttype, &ntype, CMP_NODE_IMAGE, "Image", NODE_CLASS_INPUT, NODE_PREVIEW|NODE_OPTIONS);
322         node_type_socket_templates(&ntype, NULL, cmp_node_rlayers_out);
323         node_type_size(&ntype, 120, 80, 300);
324         node_type_init(&ntype, node_composit_init_image);
325         node_type_storage(&ntype, "ImageUser", node_free_standard_storage, node_copy_standard_storage);
326         node_type_exec(&ntype, node_composit_exec_image);
327
328         nodeRegisterType(ttype, &ntype);
329 }
330
331
332 /* **************** RENDER RESULT ******************** */
333
334 static CompBuf *compbuf_from_pass(RenderData *rd, RenderLayer *rl, int rectx, int recty, int passcode)
335 {
336         float *fp= RE_RenderLayerGetPass(rl, passcode);
337         if(fp) {
338                 CompBuf *buf;
339                 int buftype= CB_VEC3;
340
341                 if(ELEM4(passcode, SCE_PASS_Z, SCE_PASS_INDEXOB, SCE_PASS_MIST, SCE_PASS_INDEXMA))
342                         buftype= CB_VAL;
343                 else if(passcode==SCE_PASS_VECTOR)
344                         buftype= CB_VEC4;
345                 else if(ELEM(passcode, SCE_PASS_COMBINED, SCE_PASS_RGBA))
346                         buftype= CB_RGBA;
347
348                 if(rd->scemode & R_COMP_CROP)
349                         buf= get_cropped_compbuf(&rd->disprect, fp, rectx, recty, buftype);
350                 else {
351                         buf= alloc_compbuf(rectx, recty, buftype, 0);
352                         buf->rect= fp;
353                 }
354                 return buf;
355         }
356         return NULL;
357 }
358
359 static void node_composit_rlayers_out(RenderData *rd, RenderLayer *rl, bNodeStack **out, int rectx, int recty)
360 {
361         if(out[RRES_OUT_Z]->hasoutput)
362                 out[RRES_OUT_Z]->data= compbuf_from_pass(rd, rl, rectx, recty, SCE_PASS_Z);
363         if(out[RRES_OUT_VEC]->hasoutput)
364                 out[RRES_OUT_VEC]->data= compbuf_from_pass(rd, rl, rectx, recty, SCE_PASS_VECTOR);
365         if(out[RRES_OUT_NORMAL]->hasoutput)
366                 out[RRES_OUT_NORMAL]->data= compbuf_from_pass(rd, rl, rectx, recty, SCE_PASS_NORMAL);
367         if(out[RRES_OUT_UV]->hasoutput)
368                 out[RRES_OUT_UV]->data= compbuf_from_pass(rd, rl, rectx, recty, SCE_PASS_UV);
369
370         if(out[RRES_OUT_RGBA]->hasoutput)
371                 out[RRES_OUT_RGBA]->data= compbuf_from_pass(rd, rl, rectx, recty, SCE_PASS_RGBA);
372         if(out[RRES_OUT_DIFF]->hasoutput)
373                 out[RRES_OUT_DIFF]->data= compbuf_from_pass(rd, rl, rectx, recty, SCE_PASS_DIFFUSE);
374         if(out[RRES_OUT_SPEC]->hasoutput)
375                 out[RRES_OUT_SPEC]->data= compbuf_from_pass(rd, rl, rectx, recty, SCE_PASS_SPEC);
376         if(out[RRES_OUT_SHADOW]->hasoutput)
377                 out[RRES_OUT_SHADOW]->data= compbuf_from_pass(rd, rl, rectx, recty, SCE_PASS_SHADOW);
378         if(out[RRES_OUT_AO]->hasoutput)
379                 out[RRES_OUT_AO]->data= compbuf_from_pass(rd, rl, rectx, recty, SCE_PASS_AO);
380         if(out[RRES_OUT_REFLECT]->hasoutput)
381                 out[RRES_OUT_REFLECT]->data= compbuf_from_pass(rd, rl, rectx, recty, SCE_PASS_REFLECT);
382         if(out[RRES_OUT_REFRACT]->hasoutput)
383                 out[RRES_OUT_REFRACT]->data= compbuf_from_pass(rd, rl, rectx, recty, SCE_PASS_REFRACT);
384         if(out[RRES_OUT_INDIRECT]->hasoutput)
385                 out[RRES_OUT_INDIRECT]->data= compbuf_from_pass(rd, rl, rectx, recty, SCE_PASS_INDIRECT);
386         if(out[RRES_OUT_INDEXOB]->hasoutput)
387                 out[RRES_OUT_INDEXOB]->data= compbuf_from_pass(rd, rl, rectx, recty, SCE_PASS_INDEXOB);
388         if(out[RRES_OUT_INDEXMA]->hasoutput)
389                 out[RRES_OUT_INDEXMA]->data= compbuf_from_pass(rd, rl, rectx, recty, SCE_PASS_INDEXMA);
390         if(out[RRES_OUT_MIST]->hasoutput)
391                 out[RRES_OUT_MIST]->data= compbuf_from_pass(rd, rl, rectx, recty, SCE_PASS_MIST);
392         if(out[RRES_OUT_EMIT]->hasoutput)
393                 out[RRES_OUT_EMIT]->data= compbuf_from_pass(rd, rl, rectx, recty, SCE_PASS_EMIT);
394         if(out[RRES_OUT_ENV]->hasoutput)
395                 out[RRES_OUT_ENV]->data= compbuf_from_pass(rd, rl, rectx, recty, SCE_PASS_ENVIRONMENT);
396 }
397
398 static void node_composit_exec_rlayers(void *data, bNode *node, bNodeStack **UNUSED(in), bNodeStack **out)
399 {
400         Scene *sce= (Scene *)node->id;
401         Render *re= (sce)? RE_GetRender(sce->id.name): NULL;
402         RenderData *rd= data;
403         RenderResult *rr= NULL;
404
405         if(re)
406                 rr= RE_AcquireResultRead(re);
407
408         if(rr) {
409                 SceneRenderLayer *srl= BLI_findlink(&sce->r.layers, node->custom1);
410                 if(srl) {
411                         RenderLayer *rl= RE_GetRenderLayer(rr, srl->name);
412                         if(rl && rl->rectf) {
413                                 CompBuf *stackbuf;
414
415                                 /* we put render rect on stack, cbuf knows rect is from other ibuf when freed! */
416                                 if(rd->scemode & R_COMP_CROP)
417                                         stackbuf= get_cropped_compbuf(&rd->disprect, rl->rectf, rr->rectx, rr->recty, CB_RGBA);
418                                 else {
419                                         stackbuf= alloc_compbuf(rr->rectx, rr->recty, CB_RGBA, 0);
420                                         stackbuf->rect= rl->rectf;
421                                 }
422                                 if(stackbuf==NULL) {
423                                         printf("Error; Preview Panel in UV Window returns zero sized image\n");
424                                 }
425                                 else {
426                                         stackbuf->xof= rr->xof;
427                                         stackbuf->yof= rr->yof;
428
429                                         /* put on stack */
430                                         out[RRES_OUT_IMAGE]->data= stackbuf;
431
432                                         if(out[RRES_OUT_ALPHA]->hasoutput)
433                                                 out[RRES_OUT_ALPHA]->data= valbuf_from_rgbabuf(stackbuf, CHAN_A);
434
435                                         node_composit_rlayers_out(rd, rl, out, rr->rectx, rr->recty);
436
437                                         generate_preview(data, node, stackbuf);
438                                 }
439                         }
440                 }
441         }
442
443         if(re)
444                 RE_ReleaseResult(re);
445 }
446
447
448 void register_node_type_cmp_rlayers(bNodeTreeType *ttype)
449 {
450         static bNodeType ntype;
451
452         node_type_base(ttype, &ntype, CMP_NODE_R_LAYERS, "Render Layers", NODE_CLASS_INPUT, NODE_PREVIEW|NODE_OPTIONS);
453         node_type_socket_templates(&ntype, NULL, cmp_node_rlayers_out);
454         node_type_size(&ntype, 150, 100, 300);
455         node_type_exec(&ntype, node_composit_exec_rlayers);
456
457         nodeRegisterType(ttype, &ntype);
458 }