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