Muting node patch: second part. Also fix [#27636] Muting shading nodes is ignored
[blender.git] / source / blender / nodes / composite / node_composite_util.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/node_composite_util.c
29  *  \ingroup nodes
30  */
31
32
33 #include "node_composite_util.h"
34
35 CompBuf *alloc_compbuf(int sizex, int sizey, int type, int alloc)
36 {
37         CompBuf *cbuf= MEM_callocN(sizeof(CompBuf), "compbuf");
38         
39         cbuf->x= sizex;
40         cbuf->y= sizey;
41         cbuf->xrad= sizex/2;
42         cbuf->yrad= sizey/2;
43         
44         cbuf->type= type;
45         if(alloc) {
46                 if(cbuf->type==CB_RGBA)
47                         cbuf->rect= MEM_mapallocN(4*sizeof(float)*sizex*sizey, "compbuf RGBA rect");
48                 else if(cbuf->type==CB_VEC3)
49                         cbuf->rect= MEM_mapallocN(3*sizeof(float)*sizex*sizey, "compbuf Vector3 rect");
50                 else if(cbuf->type==CB_VEC2)
51                         cbuf->rect= MEM_mapallocN(2*sizeof(float)*sizex*sizey, "compbuf Vector2 rect");
52                 else
53                         cbuf->rect= MEM_mapallocN(sizeof(float)*sizex*sizey, "compbuf Fac rect");
54                 cbuf->malloc= 1;
55         }
56         cbuf->disprect.xmin= 0;
57         cbuf->disprect.ymin= 0;
58         cbuf->disprect.xmax= sizex;
59         cbuf->disprect.ymax= sizey;
60         
61         return cbuf;
62 }
63
64 CompBuf *dupalloc_compbuf(CompBuf *cbuf)
65 {
66         CompBuf *dupbuf= alloc_compbuf(cbuf->x, cbuf->y, cbuf->type, 1);
67         if(dupbuf) {
68                 memcpy(dupbuf->rect, cbuf->rect, cbuf->type*sizeof(float)*cbuf->x*cbuf->y);
69         
70                 dupbuf->xof= cbuf->xof;
71                 dupbuf->yof= cbuf->yof;
72         }       
73         return dupbuf;
74 }
75
76 /* instead of reference counting, we create a list */
77 CompBuf *pass_on_compbuf(CompBuf *cbuf)
78 {
79         CompBuf *dupbuf= (cbuf)? alloc_compbuf(cbuf->x, cbuf->y, cbuf->type, 0): NULL;
80         CompBuf *lastbuf;
81         
82         if(dupbuf) {
83                 dupbuf->rect= cbuf->rect;
84                 dupbuf->xof= cbuf->xof;
85                 dupbuf->yof= cbuf->yof;
86                 dupbuf->malloc= 0;
87                 
88                 /* get last buffer in list, and append dupbuf */
89                 for(lastbuf= cbuf; lastbuf; lastbuf= lastbuf->next)
90                         if(lastbuf->next==NULL)
91                                 break;
92                 lastbuf->next= dupbuf;
93                 dupbuf->prev= lastbuf;
94         }       
95         return dupbuf;
96 }
97
98
99 void free_compbuf(CompBuf *cbuf)
100 {
101         /* check referencing, then remove from list and set malloc tag */
102         if(cbuf->prev || cbuf->next) {
103                 if(cbuf->prev)
104                         cbuf->prev->next= cbuf->next;
105                 if(cbuf->next)
106                         cbuf->next->prev= cbuf->prev;
107                 if(cbuf->malloc) {
108                         if(cbuf->prev)
109                                 cbuf->prev->malloc= 1;
110                         else
111                                 cbuf->next->malloc= 1;
112                         cbuf->malloc= 0;
113                 }
114         }
115         
116         if(cbuf->malloc && cbuf->rect)
117                 MEM_freeN(cbuf->rect);
118
119         MEM_freeN(cbuf);
120 }
121
122 void print_compbuf(char *str, CompBuf *cbuf)
123 {
124         printf("Compbuf %s %d %d %p\n", str, cbuf->x, cbuf->y, (void *)cbuf->rect);
125         
126 }
127
128 void compbuf_set_node(CompBuf *cbuf, bNode *node)
129 {
130         if (cbuf) cbuf->node = node;
131 }
132
133 /* used for disabling node  (similar code in node_draw.c for disable line and node_edit for untangling nodes) */
134 void node_compo_pass_on(void *UNUSED(data), int UNUSED(thread), struct bNode *node, void *UNUSED(nodedata),
135                         struct bNodeStack **in, struct bNodeStack **out)
136 {
137         ListBase links;
138         LinkInOutsMuteNode *lnk;
139         int i;
140
141         if(node->typeinfo->mutelinksfunc == NULL)
142                 return;
143
144         /* Get default muting links (as bNodeStack pointers). */
145         links = node->typeinfo->mutelinksfunc(NULL, node, in, out, NULL, NULL);
146
147         for(lnk = links.first; lnk; lnk = lnk->next) {
148                 for(i = 0; i < lnk->num_outs; i++) {
149                         if(((bNodeStack*)(lnk->in))->data)
150                                 (((bNodeStack*)(lnk->outs))+i)->data = pass_on_compbuf((CompBuf*)((bNodeStack*)(lnk->in))->data);
151                 }
152                 /* If num_outs > 1, lnk->outs was an allocated table of pointers... */
153                 if(i > 1)
154                         MEM_freeN(lnk->outs);
155         }
156         BLI_freelistN(&links);
157 }
158
159
160 CompBuf *get_cropped_compbuf(rcti *drect, float *rectf, int rectx, int recty, int type)
161 {
162         CompBuf *cbuf;
163         rcti disprect= *drect;
164         float *outfp;
165         int dx, y;
166         
167         if(disprect.xmax>rectx) disprect.xmax= rectx;
168         if(disprect.ymax>recty) disprect.ymax= recty;
169         if(disprect.xmin>= disprect.xmax) return NULL;
170         if(disprect.ymin>= disprect.ymax) return NULL;
171         
172         cbuf= alloc_compbuf(disprect.xmax-disprect.xmin, disprect.ymax-disprect.ymin, type, 1);
173         outfp= cbuf->rect;
174         rectf += type*(disprect.ymin*rectx + disprect.xmin);
175         dx= type*cbuf->x;
176         for(y=cbuf->y; y>0; y--, outfp+=dx, rectf+=type*rectx)
177                 memcpy(outfp, rectf, sizeof(float)*dx);
178         
179         return cbuf;
180 }
181
182 CompBuf *scalefast_compbuf(CompBuf *inbuf, int newx, int newy)
183 {
184         CompBuf *outbuf; 
185         float *rectf, *newrectf, *rf;
186         int x, y, c, pixsize= inbuf->type;
187         int ofsx, ofsy, stepx, stepy;
188         
189         if(inbuf->x==newx && inbuf->y==newy)
190                 return dupalloc_compbuf(inbuf);
191         
192         outbuf= alloc_compbuf(newx, newy, inbuf->type, 1);
193         newrectf= outbuf->rect;
194         
195         stepx = (65536.0 * (inbuf->x - 1.0) / (newx - 1.0)) + 0.5;
196         stepy = (65536.0 * (inbuf->y - 1.0) / (newy - 1.0)) + 0.5;
197         ofsy = 32768;
198         
199         for (y = newy; y > 0 ; y--){
200                 rectf = inbuf->rect;
201                 rectf += pixsize * (ofsy >> 16) * inbuf->x;
202
203                 ofsy += stepy;
204                 ofsx = 32768;
205                 
206                 for (x = newx ; x>0 ; x--) {
207                         
208                         rf= rectf + pixsize*(ofsx >> 16);
209                         for(c=0; c<pixsize; c++)
210                                 newrectf[c] = rf[c];
211                         
212                         newrectf+= pixsize;
213                         
214                         ofsx += stepx;
215                 }
216         }
217         
218         return outbuf;
219 }
220
221 void typecheck_compbuf_color(float *out, float *in, int outtype, int intype)
222 {
223         if(intype == outtype) {
224                 memcpy(out, in, sizeof(float)*outtype);
225         }
226         else if(outtype==CB_VAL) {
227                 if(intype==CB_VEC2) {
228                         *out= 0.5f*(in[0]+in[1]);
229                 }
230                 else if(intype==CB_VEC3) {
231                         *out= 0.333333f*(in[0]+in[1]+in[2]);
232                 }
233                 else if(intype==CB_RGBA) {
234                         *out= in[0]*0.35f + in[1]*0.45f + in[2]*0.2f;
235                 }
236         }
237         else if(outtype==CB_VEC2) {
238                 if(intype==CB_VAL) {
239                         out[0]= in[0];
240                         out[1]= in[0];
241                 }
242                 else if(intype==CB_VEC3) {
243                         out[0]= in[0];
244                         out[1]= in[1];
245                 }
246                 else if(intype==CB_RGBA) {
247                         out[0]= in[0];
248                         out[1]= in[1];
249                 }
250         }
251         else if(outtype==CB_VEC3) {
252                 if(intype==CB_VAL) {
253                         out[0]= in[0];
254                         out[1]= in[0];
255                         out[2]= in[0];
256                 }
257                 else if(intype==CB_VEC2) {
258                         out[0]= in[0];
259                         out[1]= in[1];
260                         out[2]= 0.0f;
261                 }
262                 else if(intype==CB_RGBA) {
263                         out[0]= in[0];
264                         out[1]= in[1];
265                         out[2]= in[2];
266                 }
267         }
268         else if(outtype==CB_RGBA) {
269                 if(intype==CB_VAL) {
270                         out[0]= in[0];
271                         out[1]= in[0];
272                         out[2]= in[0];
273                         out[3]= 1.0f;
274                 }
275                 else if(intype==CB_VEC2) {
276                         out[0]= in[0];
277                         out[1]= in[1];
278                         out[2]= 0.0f;
279                         out[3]= 1.0f;
280                 }
281                 else if(intype==CB_VEC3) {
282                         out[0]= in[0];
283                         out[1]= in[1];
284                         out[2]= in[2];
285                         out[3]= 1.0f;
286                 }
287         }
288 }
289
290 CompBuf *typecheck_compbuf(CompBuf *inbuf, int type)
291 {
292         if(inbuf && inbuf->type!=type) {
293                 CompBuf *outbuf;
294                 float *inrf, *outrf;
295                 int x;
296
297                 outbuf= alloc_compbuf(inbuf->x, inbuf->y, type, 1); 
298
299                 /* warning note: xof and yof are applied in pixelprocessor, but should be copied otherwise? */
300                 outbuf->xof= inbuf->xof;
301                 outbuf->yof= inbuf->yof;
302
303                 if(inbuf->rect_procedural) {
304                         outbuf->rect_procedural= inbuf->rect_procedural;
305                         copy_v3_v3(outbuf->procedural_size, inbuf->procedural_size);
306                         copy_v3_v3(outbuf->procedural_offset, inbuf->procedural_offset);
307                         outbuf->procedural_type= inbuf->procedural_type;
308                         outbuf->node= inbuf->node;
309                         return outbuf;
310                 }
311
312                 inrf= inbuf->rect;
313                 outrf= outbuf->rect;
314                 x= inbuf->x*inbuf->y;
315                 
316                 if(type==CB_VAL) {
317                         if(inbuf->type==CB_VEC2) {
318                                 for(; x>0; x--, outrf+= 1, inrf+= 2)
319                                         *outrf= 0.5f*(inrf[0]+inrf[1]);
320                         }
321                         else if(inbuf->type==CB_VEC3) {
322                                 for(; x>0; x--, outrf+= 1, inrf+= 3)
323                                         *outrf= 0.333333f*(inrf[0]+inrf[1]+inrf[2]);
324                         }
325                         else if(inbuf->type==CB_RGBA) {
326                                 for(; x>0; x--, outrf+= 1, inrf+= 4)
327                                         *outrf= inrf[0]*0.35f + inrf[1]*0.45f + inrf[2]*0.2f;
328                         }
329                 }
330                 else if(type==CB_VEC2) {
331                         if(inbuf->type==CB_VAL) {
332                                 for(; x>0; x--, outrf+= 2, inrf+= 1) {
333                                         outrf[0]= inrf[0];
334                                         outrf[1]= inrf[0];
335                                 }
336                         }
337                         else if(inbuf->type==CB_VEC3) {
338                                 for(; x>0; x--, outrf+= 2, inrf+= 3) {
339                                         outrf[0]= inrf[0];
340                                         outrf[1]= inrf[1];
341                                 }
342                         }
343                         else if(inbuf->type==CB_RGBA) {
344                                 for(; x>0; x--, outrf+= 2, inrf+= 4) {
345                                         outrf[0]= inrf[0];
346                                         outrf[1]= inrf[1];
347                                 }
348                         }
349                 }
350                 else if(type==CB_VEC3) {
351                         if(inbuf->type==CB_VAL) {
352                                 for(; x>0; x--, outrf+= 3, inrf+= 1) {
353                                         outrf[0]= inrf[0];
354                                         outrf[1]= inrf[0];
355                                         outrf[2]= inrf[0];
356                                 }
357                         }
358                         else if(inbuf->type==CB_VEC2) {
359                                 for(; x>0; x--, outrf+= 3, inrf+= 2) {
360                                         outrf[0]= inrf[0];
361                                         outrf[1]= inrf[1];
362                                         outrf[2]= 0.0f;
363                                 }
364                         }
365                         else if(inbuf->type==CB_RGBA) {
366                                 for(; x>0; x--, outrf+= 3, inrf+= 4) {
367                                         outrf[0]= inrf[0];
368                                         outrf[1]= inrf[1];
369                                         outrf[2]= inrf[2];
370                                 }
371                         }
372                 }
373                 else if(type==CB_RGBA) {
374                         if(inbuf->type==CB_VAL) {
375                                 for(; x>0; x--, outrf+= 4, inrf+= 1) {
376                                         outrf[0]= inrf[0];
377                                         outrf[1]= inrf[0];
378                                         outrf[2]= inrf[0];
379                                         outrf[3]= 1.0f;
380                                 }
381                         }
382                         else if(inbuf->type==CB_VEC2) {
383                                 for(; x>0; x--, outrf+= 4, inrf+= 2) {
384                                         outrf[0]= inrf[0];
385                                         outrf[1]= inrf[1];
386                                         outrf[2]= 0.0f;
387                                         outrf[3]= 1.0f;
388                                 }
389                         }
390                         else if(inbuf->type==CB_VEC3) {
391                                 for(; x>0; x--, outrf+= 4, inrf+= 3) {
392                                         outrf[0]= inrf[0];
393                                         outrf[1]= inrf[1];
394                                         outrf[2]= inrf[2];
395                                         outrf[3]= 1.0f;
396                                 }
397                         }
398                 }
399                 
400                 return outbuf;
401         }
402         return inbuf;
403 }
404
405 static float *compbuf_get_pixel(CompBuf *cbuf, float *defcol, float *use, int x, int y, int xrad, int yrad)
406 {
407         if(cbuf) {
408                 if(cbuf->rect_procedural) {
409                         cbuf->rect_procedural(cbuf, use, (float)x/(float)xrad, (float)y/(float)yrad);
410                         return use;
411                 }
412                 else {
413                         static float col[4]= {0.0f, 0.0f, 0.0f, 0.0f};
414                         
415                         /* map coords */
416                         x-= cbuf->xof;
417                         y-= cbuf->yof;
418                         
419                         if(y<-cbuf->yrad || y>= -cbuf->yrad+cbuf->y) return col;
420                         if(x<-cbuf->xrad || x>= -cbuf->xrad+cbuf->x) return col;
421                         
422                         return cbuf->rect + cbuf->type*( (cbuf->yrad+y)*cbuf->x + (cbuf->xrad+x) );
423                 }
424         }
425         else return defcol;
426 }
427
428 /* **************************************************** */
429
430 /* Pixel-to-Pixel operation, 1 Image in, 1 out */
431 void composit1_pixel_processor(bNode *node, CompBuf *out, CompBuf *src_buf, float *src_col,
432                                                                           void (*func)(bNode *, float *, float *), 
433                                                                           int src_type)
434 {
435         CompBuf *src_use;
436         float *outfp=out->rect, *srcfp;
437         float color[4]; /* local color if compbuf is procedural */
438         int xrad, yrad, x, y;
439         
440         src_use= typecheck_compbuf(src_buf, src_type);
441         
442         xrad= out->xrad;
443         yrad= out->yrad;
444         
445         for(y= -yrad; y<-yrad+out->y; y++) {
446                 for(x= -xrad; x<-xrad+out->x; x++, outfp+=out->type) {
447                         srcfp= compbuf_get_pixel(src_use, src_col, color, x, y, xrad, yrad);
448                         func(node, outfp, srcfp);
449                 }
450         }
451         
452         if(src_use!=src_buf)
453                 free_compbuf(src_use);
454 }
455
456 /* Pixel-to-Pixel operation, 2 Images in, 1 out */
457 void composit2_pixel_processor(bNode *node, CompBuf *out, CompBuf *src_buf, float *src_col,
458                                                                           CompBuf *fac_buf, float *fac, void (*func)(bNode *, float *, float *, float *), 
459                                                                           int src_type, int fac_type)
460 {
461         CompBuf *src_use, *fac_use;
462         float *outfp=out->rect, *srcfp, *facfp;
463         float color[4]; /* local color if compbuf is procedural */
464         int xrad, yrad, x, y;
465         
466         src_use= typecheck_compbuf(src_buf, src_type);
467         fac_use= typecheck_compbuf(fac_buf, fac_type);
468
469         xrad= out->xrad;
470         yrad= out->yrad;
471         
472         for(y= -yrad; y<-yrad+out->y; y++) {
473                 for(x= -xrad; x<-xrad+out->x; x++, outfp+=out->type) {
474                         srcfp= compbuf_get_pixel(src_use, src_col, color, x, y, xrad, yrad);
475                         facfp= compbuf_get_pixel(fac_use, fac, color, x, y, xrad, yrad);
476                         
477                         func(node, outfp, srcfp, facfp);
478                 }
479         }
480         if(src_use!=src_buf)
481                 free_compbuf(src_use);
482         if(fac_use!=fac_buf)
483                 free_compbuf(fac_use);
484 }
485
486 /* Pixel-to-Pixel operation, 3 Images in, 1 out */
487 void composit3_pixel_processor(bNode *node, CompBuf *out, CompBuf *src1_buf, float *src1_col, CompBuf *src2_buf, float *src2_col, 
488                                                                           CompBuf *fac_buf, float *fac, void (*func)(bNode *, float *, float *, float *, float *), 
489                                                                           int src1_type, int src2_type, int fac_type)
490 {
491         CompBuf *src1_use, *src2_use, *fac_use;
492         float *outfp=out->rect, *src1fp, *src2fp, *facfp;
493         float color[4]; /* local color if compbuf is procedural */
494         int xrad, yrad, x, y;
495         
496         src1_use= typecheck_compbuf(src1_buf, src1_type);
497         src2_use= typecheck_compbuf(src2_buf, src2_type);
498         fac_use= typecheck_compbuf(fac_buf, fac_type);
499         
500         xrad= out->xrad;
501         yrad= out->yrad;
502         
503         for(y= -yrad; y<-yrad+out->y; y++) {
504                 for(x= -xrad; x<-xrad+out->x; x++, outfp+=out->type) {
505                         src1fp= compbuf_get_pixel(src1_use, src1_col, color, x, y, xrad, yrad);
506                         src2fp= compbuf_get_pixel(src2_use, src2_col, color, x, y, xrad, yrad);
507                         facfp= compbuf_get_pixel(fac_use, fac, color, x, y, xrad, yrad);
508                         
509                         func(node, outfp, src1fp, src2fp, facfp);
510                 }
511         }
512         
513         if(src1_use!=src1_buf)
514                 free_compbuf(src1_use);
515         if(src2_use!=src2_buf)
516                 free_compbuf(src2_use);
517         if(fac_use!=fac_buf)
518                 free_compbuf(fac_use);
519 }
520
521 /* Pixel-to-Pixel operation, 4 Images in, 1 out */
522 void composit4_pixel_processor(bNode *node, CompBuf *out, CompBuf *src1_buf, float *src1_col, CompBuf *fac1_buf, float *fac1, 
523                                                                           CompBuf *src2_buf, float *src2_col, CompBuf *fac2_buf, float *fac2, 
524                                                                           void (*func)(bNode *, float *, float *, float *, float *, float *), 
525                                                                           int src1_type, int fac1_type, int src2_type, int fac2_type)
526 {
527         CompBuf *src1_use, *src2_use, *fac1_use, *fac2_use;
528         float *outfp=out->rect, *src1fp, *src2fp, *fac1fp, *fac2fp;
529         float color[4]; /* local color if compbuf is procedural */
530         int xrad, yrad, x, y;
531         
532         src1_use= typecheck_compbuf(src1_buf, src1_type);
533         src2_use= typecheck_compbuf(src2_buf, src2_type);
534         fac1_use= typecheck_compbuf(fac1_buf, fac1_type);
535         fac2_use= typecheck_compbuf(fac2_buf, fac2_type);
536         
537         xrad= out->xrad;
538         yrad= out->yrad;
539         
540         for(y= -yrad; y<-yrad+out->y; y++) {
541                 for(x= -xrad; x<-xrad+out->x; x++, outfp+=out->type) {
542                         src1fp= compbuf_get_pixel(src1_use, src1_col, color, x, y, xrad, yrad);
543                         src2fp= compbuf_get_pixel(src2_use, src2_col, color, x, y, xrad, yrad);
544                         fac1fp= compbuf_get_pixel(fac1_use, fac1, color, x, y, xrad, yrad);
545                         fac2fp= compbuf_get_pixel(fac2_use, fac2, color, x, y, xrad, yrad);
546                         
547                         func(node, outfp, src1fp, fac1fp, src2fp, fac2fp);
548                 }
549         }
550         
551         if(src1_use!=src1_buf)
552                 free_compbuf(src1_use);
553         if(src2_use!=src2_buf)
554                 free_compbuf(src2_use);
555         if(fac1_use!=fac1_buf)
556                 free_compbuf(fac1_use);
557         if(fac2_use!=fac2_buf)
558                 free_compbuf(fac2_use);
559 }
560
561
562 CompBuf *valbuf_from_rgbabuf(CompBuf *cbuf, int channel)
563 {
564         CompBuf *valbuf= alloc_compbuf(cbuf->x, cbuf->y, CB_VAL, 1);
565         float *valf, *rectf;
566         int tot;
567         
568         /* warning note: xof and yof are applied in pixelprocessor, but should be copied otherwise? */
569         valbuf->xof= cbuf->xof;
570         valbuf->yof= cbuf->yof;
571         
572         valf= valbuf->rect;
573
574         /* defaults to returning alpha channel */
575         if ((channel < CHAN_R) || (channel > CHAN_A)) channel = CHAN_A;
576
577         rectf= cbuf->rect + channel;
578         
579         for(tot= cbuf->x*cbuf->y; tot>0; tot--, valf++, rectf+=4)
580                 *valf= *rectf;
581         
582         return valbuf;
583 }
584
585 static CompBuf *generate_procedural_preview(CompBuf *cbuf, int newx, int newy)
586 {
587         CompBuf *outbuf;
588         float *outfp;
589         int xrad, yrad, x, y;
590         
591         outbuf= alloc_compbuf(newx, newy, CB_RGBA, 1);
592
593         outfp= outbuf->rect;
594         xrad= outbuf->xrad;
595         yrad= outbuf->yrad;
596         
597         for(y= -yrad; y<-yrad+outbuf->y; y++)
598                 for(x= -xrad; x<-xrad+outbuf->x; x++, outfp+=outbuf->type)
599                         cbuf->rect_procedural(cbuf, outfp, (float)x/(float)xrad, (float)y/(float)yrad);
600
601         return outbuf;
602 }
603
604 void generate_preview(void *data, bNode *node, CompBuf *stackbuf)
605 {
606         RenderData *rd= data;
607         bNodePreview *preview= node->preview;
608         int xsize, ysize;
609         int color_manage= rd->color_mgt_flag & R_COLOR_MANAGEMENT;
610         unsigned char *rect;
611         
612         if(preview && stackbuf) {
613                 CompBuf *cbuf, *stackbuf_use;
614                 
615                 if(stackbuf->rect==NULL && stackbuf->rect_procedural==NULL) return;
616                 
617                 stackbuf_use= typecheck_compbuf(stackbuf, CB_RGBA);
618
619                 if(stackbuf->x > stackbuf->y) {
620                         xsize= 140;
621                         ysize= (140*stackbuf->y)/stackbuf->x;
622                 }
623                 else {
624                         ysize= 140;
625                         xsize= (140*stackbuf->x)/stackbuf->y;
626                 }
627                 
628                 if(stackbuf_use->rect_procedural)
629                         cbuf= generate_procedural_preview(stackbuf_use, xsize, ysize);
630                 else
631                         cbuf= scalefast_compbuf(stackbuf_use, xsize, ysize);
632
633                 /* convert to byte for preview */
634                 rect= MEM_callocN(sizeof(unsigned char)*4*xsize*ysize, "bNodePreview.rect");
635
636                 if(color_manage)
637                         floatbuf_to_srgb_byte(cbuf->rect, rect, 0, xsize, 0, ysize, xsize);
638                 else
639                         floatbuf_to_byte(cbuf->rect, rect, 0, xsize, 0, ysize, xsize);
640                 
641                 free_compbuf(cbuf);
642                 if(stackbuf_use!=stackbuf)
643                         free_compbuf(stackbuf_use);
644
645                 BLI_lock_thread(LOCK_PREVIEW);
646
647                 if(preview->rect)
648                         MEM_freeN(preview->rect);
649                 preview->xsize= xsize;
650                 preview->ysize= ysize;
651                 preview->rect= rect;
652
653                 BLI_unlock_thread(LOCK_PREVIEW);
654         }
655 }
656
657 void do_rgba_to_yuva(bNode *UNUSED(node), float *out, float *in)
658 {
659         rgb_to_yuv(in[0],in[1],in[2], &out[0], &out[1], &out[2]);
660         out[3]=in[3];
661 }
662
663 void do_rgba_to_hsva(bNode *UNUSED(node), float *out, float *in)
664 {
665         rgb_to_hsv(in[0],in[1],in[2], &out[0], &out[1], &out[2]);
666         out[3]=in[3];
667 }
668
669 void do_rgba_to_ycca(bNode *UNUSED(node), float *out, float *in)
670 {
671         rgb_to_ycc(in[0],in[1],in[2], &out[0], &out[1], &out[2], BLI_YCC_ITU_BT601);
672         out[3]=in[3];
673 }
674
675 void do_yuva_to_rgba(bNode *UNUSED(node), float *out, float *in)
676 {
677         yuv_to_rgb(in[0],in[1],in[2], &out[0], &out[1], &out[2]);
678         out[3]=in[3];
679 }
680
681 void do_hsva_to_rgba(bNode *UNUSED(node), float *out, float *in)
682 {
683         hsv_to_rgb(in[0],in[1],in[2], &out[0], &out[1], &out[2]);
684         out[3]=in[3];
685 }
686
687 void do_ycca_to_rgba(bNode *UNUSED(node), float *out, float *in)
688 {
689         ycc_to_rgb(in[0],in[1],in[2], &out[0], &out[1], &out[2], BLI_YCC_ITU_BT601);
690         out[3]=in[3];
691 }
692
693 void do_copy_rgba(bNode *UNUSED(node), float *out, float *in)
694 {
695         copy_v4_v4(out, in);
696 }
697
698 void do_copy_rgb(bNode *UNUSED(node), float *out, float *in)
699 {
700         copy_v3_v3(out, in);
701         out[3]= 1.0f;
702 }
703
704 void do_copy_value(bNode *UNUSED(node), float *out, float *in)
705 {
706         out[0]= in[0];
707 }
708
709 void do_copy_a_rgba(bNode *UNUSED(node), float *out, float *in, float *fac)
710 {
711         copy_v3_v3(out, in);
712         out[3]= *fac;
713 }
714
715 /* only accepts RGBA buffers */
716 void gamma_correct_compbuf(CompBuf *img, int inversed)
717 {
718         float *drect;
719         int x;
720
721         if(img->type!=CB_RGBA) return;
722
723         drect= img->rect;
724         if(inversed) {
725                 for(x=img->x*img->y; x>0; x--, drect+=4) {
726                         if(drect[0]>0.0f) drect[0]= sqrt(drect[0]); else drect[0]= 0.0f;
727                         if(drect[1]>0.0f) drect[1]= sqrt(drect[1]); else drect[1]= 0.0f;
728                         if(drect[2]>0.0f) drect[2]= sqrt(drect[2]); else drect[2]= 0.0f;
729                 }
730         }
731         else {
732                 for(x=img->x*img->y; x>0; x--, drect+=4) {
733                         if(drect[0]>0.0f) drect[0]*= drect[0]; else drect[0]= 0.0f;
734                         if(drect[1]>0.0f) drect[1]*= drect[1]; else drect[1]= 0.0f;
735                         if(drect[2]>0.0f) drect[2]*= drect[2]; else drect[2]= 0.0f;
736                 }
737         }
738 }
739
740 void premul_compbuf(CompBuf *img, int inversed)
741 {
742         float *drect;
743         int x;
744
745         if(img->type!=CB_RGBA) return;
746
747         drect= img->rect;
748         if(inversed) {
749                 for(x=img->x*img->y; x>0; x--, drect+=4) {
750                         if(fabsf(drect[3]) < 1e-5f) {
751                                 drect[0]= 0.0f;
752                                 drect[1]= 0.0f;
753                                 drect[2]= 0.0f;
754                         }
755                         else {
756                                 drect[0] /= drect[3];
757                                 drect[1] /= drect[3];
758                                 drect[2] /= drect[3];
759                         }
760                 }
761         }
762         else {
763                 for(x=img->x*img->y; x>0; x--, drect+=4) {
764                         drect[0] *= drect[3];
765                         drect[1] *= drect[3];
766                         drect[2] *= drect[3];
767                 }
768         }
769 }
770
771
772
773 /*
774  *  2D Fast Hartley Transform, used for convolution
775  */
776
777 typedef float fREAL;
778
779 // returns next highest power of 2 of x, as well it's log2 in L2
780 static unsigned int nextPow2(unsigned int x, unsigned int* L2)
781 {
782         unsigned int pw, x_notpow2 = x & (x-1);
783         *L2 = 0;
784         while (x>>=1) ++(*L2);
785         pw = 1 << (*L2);
786         if (x_notpow2) { (*L2)++;  pw<<=1; }
787         return pw;
788 }
789
790 //------------------------------------------------------------------------------
791
792 // from FXT library by Joerg Arndt, faster in order bitreversal
793 // use: r = revbin_upd(r, h) where h = N>>1
794 static unsigned int revbin_upd(unsigned int r, unsigned int h)
795 {
796         while (!((r^=h)&h)) h >>= 1;
797         return r;
798 }
799 //------------------------------------------------------------------------------
800 static void FHT(fREAL* data, unsigned int M, unsigned int inverse)
801 {
802         double tt, fc, dc, fs, ds, a = M_PI;
803         fREAL t1, t2;
804         int n2, bd, bl, istep, k, len = 1 << M, n = 1;
805
806         int i, j = 0;
807         unsigned int Nh = len >> 1;
808         for (i=1;i<(len-1);++i) {
809                 j = revbin_upd(j, Nh);
810                 if (j>i) {
811                         t1 = data[i];
812                         data[i] = data[j];
813                         data[j] = t1;
814                 }
815         }
816
817         do {
818                 fREAL* data_n = &data[n];
819
820                 istep = n << 1;
821                 for (k=0; k<len; k+=istep) {
822                         t1 = data_n[k];
823                         data_n[k] = data[k] - t1;
824                         data[k] += t1;
825                 }
826
827                 n2 = n >> 1;
828                 if (n>2) {
829                         fc = dc = cos(a);
830                         fs = ds = sqrt(1.0 - fc*fc); //sin(a);
831                         bd = n-2;
832                         for (bl=1; bl<n2; bl++) {
833                                 fREAL* data_nbd = &data_n[bd];
834                                 fREAL* data_bd = &data[bd];
835                                 for (k=bl; k<len; k+=istep) {
836                                         t1 = fc*data_n[k] + fs*data_nbd[k];
837                                         t2 = fs*data_n[k] - fc*data_nbd[k];
838                                         data_n[k] = data[k] - t1;
839                                         data_nbd[k] = data_bd[k] - t2;
840                                         data[k] += t1;
841                                         data_bd[k] += t2;
842                                 }
843                                 tt = fc*dc - fs*ds;
844                                 fs = fs*dc + fc*ds;
845                                 fc = tt;
846                                 bd -= 2;
847                         }
848                 }
849
850                 if (n>1) {
851                         for (k=n2; k<len; k+=istep) {
852                                 t1 = data_n[k];
853                                 data_n[k] = data[k] - t1;
854                                 data[k] += t1;
855                         }
856                 }
857
858                 n = istep;
859                 a *= 0.5;
860         } while (n<len);
861
862         if (inverse) {
863                 fREAL sc = (fREAL)1 / (fREAL)len;
864                 for (k=0; k<len; ++k)
865                         data[k] *= sc;
866         }
867 }
868 //------------------------------------------------------------------------------
869 /* 2D Fast Hartley Transform, Mx/My -> log2 of width/height,
870         nzp -> the row where zero pad data starts,
871         inverse -> see above */
872 static void FHT2D(fREAL *data, unsigned int Mx, unsigned int My,
873                 unsigned int nzp, unsigned int inverse)
874 {
875         unsigned int i, j, Nx, Ny, maxy;
876         fREAL t;
877
878         Nx = 1 << Mx;
879         Ny = 1 << My;
880
881         // rows (forward transform skips 0 pad data)
882         maxy = inverse ? Ny : nzp;
883         for (j=0; j<maxy; ++j)
884                 FHT(&data[Nx*j], Mx, inverse);
885
886         // transpose data
887         if (Nx==Ny) {  // square
888                 for (j=0; j<Ny; ++j)
889                         for (i=j+1; i<Nx; ++i) {
890                                 unsigned int op = i + (j << Mx), np = j + (i << My);
891                                 t=data[op], data[op]=data[np], data[np]=t;
892                         }
893         }
894         else {  // rectangular
895                 unsigned int k, Nym = Ny-1, stm = 1 << (Mx + My);
896                 for (i=0; stm>0; i++) {
897                         #define pred(k) (((k & Nym) << Mx) + (k >> My))
898                         for (j=pred(i); j>i; j=pred(j));
899                         if (j < i) continue;
900                         for (k=i, j=pred(i); j!=i; k=j, j=pred(j), stm--)
901                                 { t=data[j], data[j]=data[k], data[k]=t; }
902                         #undef pred
903                         stm--;
904                 }
905         }
906         // swap Mx/My & Nx/Ny
907         i = Nx, Nx = Ny, Ny = i;
908         i = Mx, Mx = My, My = i;
909
910         // now columns == transposed rows
911         for (j=0; j<Ny; ++j)
912                 FHT(&data[Nx*j], Mx, inverse);
913
914         // finalize
915         for (j=0; j<=(Ny >> 1); j++) {
916                 unsigned int jm = (Ny - j) & (Ny-1);
917                 unsigned int ji = j << Mx;
918                 unsigned int jmi = jm << Mx;
919                 for (i=0; i<=(Nx >> 1); i++) {
920                         unsigned int im = (Nx - i) & (Nx-1);
921                         fREAL A = data[ji + i];
922                         fREAL B = data[jmi + i];
923                         fREAL C = data[ji + im];
924                         fREAL D = data[jmi + im];
925                         fREAL E = (fREAL)0.5*((A + D) - (B + C));
926                         data[ji + i] = A - E;
927                         data[jmi + i] = B + E;
928                         data[ji + im] = C + E;
929                         data[jmi + im] = D - E;
930                 }
931         }
932
933 }
934
935 //------------------------------------------------------------------------------
936
937 /* 2D convolution calc, d1 *= d2, M/N - > log2 of width/height */
938 static void fht_convolve(fREAL* d1, fREAL* d2, unsigned int M, unsigned int N)
939 {
940         fREAL a, b;
941         unsigned int i, j, k, L, mj, mL;
942         unsigned int m = 1 << M, n = 1 << N;
943         unsigned int m2 = 1 << (M-1), n2 = 1 << (N-1);
944         unsigned int mn2 = m << (N-1);
945
946         d1[0] *= d2[0];
947         d1[mn2] *= d2[mn2];
948         d1[m2] *= d2[m2];
949         d1[m2 + mn2] *= d2[m2 + mn2];
950         for (i=1; i<m2; i++) {
951                 k = m - i;
952                 a = d1[i]*d2[i] - d1[k]*d2[k];
953                 b = d1[k]*d2[i] + d1[i]*d2[k];
954                 d1[i] = (b + a)*(fREAL)0.5;
955                 d1[k] = (b - a)*(fREAL)0.5;
956                 a = d1[i + mn2]*d2[i + mn2] - d1[k + mn2]*d2[k + mn2];
957                 b = d1[k + mn2]*d2[i + mn2] + d1[i + mn2]*d2[k + mn2];
958                 d1[i + mn2] = (b + a)*(fREAL)0.5;
959                 d1[k + mn2] = (b - a)*(fREAL)0.5;
960         }
961         for (j=1; j<n2; j++) {
962                 L = n - j;
963                 mj = j << M;
964                 mL = L << M;
965                 a = d1[mj]*d2[mj] - d1[mL]*d2[mL];
966                 b = d1[mL]*d2[mj] + d1[mj]*d2[mL];
967                 d1[mj] = (b + a)*(fREAL)0.5;
968                 d1[mL] = (b - a)*(fREAL)0.5;
969                 a = d1[m2 + mj]*d2[m2 + mj] - d1[m2 + mL]*d2[m2 + mL];
970                 b = d1[m2 + mL]*d2[m2 + mj] + d1[m2 + mj]*d2[m2 + mL];
971                 d1[m2 + mj] = (b + a)*(fREAL)0.5;
972                 d1[m2 + mL] = (b - a)*(fREAL)0.5;
973         }
974         for (i=1; i<m2; i++) {
975                 k = m - i;
976                 for (j=1; j<n2; j++) {
977                         L = n - j;
978                         mj = j << M;
979                         mL = L << M;
980                         a = d1[i + mj]*d2[i + mj] - d1[k + mL]*d2[k + mL];
981                         b = d1[k + mL]*d2[i + mj] + d1[i + mj]*d2[k + mL];
982                         d1[i + mj] = (b + a)*(fREAL)0.5;
983                         d1[k + mL] = (b - a)*(fREAL)0.5;
984                         a = d1[i + mL]*d2[i + mL] - d1[k + mj]*d2[k + mj];
985                         b = d1[k + mj]*d2[i + mL] + d1[i + mL]*d2[k + mj];
986                         d1[i + mL] = (b + a)*(fREAL)0.5;
987                         d1[k + mj] = (b - a)*(fREAL)0.5;
988                 }
989         }
990 }
991
992 //------------------------------------------------------------------------------
993
994 void convolve(CompBuf* dst, CompBuf* in1, CompBuf* in2)
995 {
996         fREAL *data1, *data2, *fp;
997         unsigned int w2, h2, hw, hh, log2_w, log2_h;
998         fRGB wt, *colp;
999         int x, y, ch;
1000         int xbl, ybl, nxb, nyb, xbsz, ybsz;
1001         int in2done = 0;
1002
1003         CompBuf* rdst = alloc_compbuf(in1->x, in1->y, in1->type, 1);
1004
1005         // convolution result width & height
1006         w2 = 2*in2->x - 1;
1007         h2 = 2*in2->y - 1;
1008         // FFT pow2 required size & log2
1009         w2 = nextPow2(w2, &log2_w);
1010         h2 = nextPow2(h2, &log2_h);
1011
1012         // alloc space
1013         data1 = (fREAL*)MEM_callocN(3*w2*h2*sizeof(fREAL), "convolve_fast FHT data1");
1014         data2 = (fREAL*)MEM_callocN(w2*h2*sizeof(fREAL), "convolve_fast FHT data2");
1015
1016         // normalize convolutor
1017         wt[0] = wt[1] = wt[2] = 0.f;
1018         for (y=0; y<in2->y; y++) {
1019                 colp = (fRGB*)&in2->rect[y*in2->x*in2->type];
1020                 for (x=0; x<in2->x; x++)
1021                         fRGB_add(wt, colp[x]);
1022         }
1023         if (wt[0] != 0.f) wt[0] = 1.f/wt[0];
1024         if (wt[1] != 0.f) wt[1] = 1.f/wt[1];
1025         if (wt[2] != 0.f) wt[2] = 1.f/wt[2];
1026         for (y=0; y<in2->y; y++) {
1027                 colp = (fRGB*)&in2->rect[y*in2->x*in2->type];
1028                 for (x=0; x<in2->x; x++)
1029                         fRGB_colormult(colp[x], wt);
1030         }
1031
1032         // copy image data, unpacking interleaved RGBA into separate channels
1033         // only need to calc data1 once
1034
1035         // block add-overlap
1036         hw = in2->x >> 1;
1037         hh = in2->y >> 1;
1038         xbsz = (w2 + 1) - in2->x;
1039         ybsz = (h2 + 1) - in2->y;
1040         nxb = in1->x / xbsz;
1041         if (in1->x % xbsz) nxb++;
1042         nyb = in1->y / ybsz;
1043         if (in1->y % ybsz) nyb++;
1044         for (ybl=0; ybl<nyb; ybl++) {
1045                 for (xbl=0; xbl<nxb; xbl++) {
1046
1047                         // each channel one by one
1048                         for (ch=0; ch<3; ch++) {
1049                                 fREAL* data1ch = &data1[ch*w2*h2];
1050
1051                                 // only need to calc fht data from in2 once, can re-use for every block
1052                                 if (!in2done) {
1053                                         // in2, channel ch -> data1
1054                                         for (y=0; y<in2->y; y++) {
1055                                                 fp = &data1ch[y*w2];
1056                                                 colp = (fRGB*)&in2->rect[y*in2->x*in2->type];
1057                                                 for (x=0; x<in2->x; x++)
1058                                                         fp[x] = colp[x][ch];
1059                                         }
1060                                 }
1061
1062                                 // in1, channel ch -> data2
1063                                 memset(data2, 0, w2*h2*sizeof(fREAL));
1064                                 for (y=0; y<ybsz; y++) {
1065                                         int yy = ybl*ybsz + y;
1066                                         if (yy >= in1->y) continue;
1067                                         fp = &data2[y*w2];
1068                                         colp = (fRGB*)&in1->rect[yy*in1->x*in1->type];
1069                                         for (x=0; x<xbsz; x++) {
1070                                                 int xx = xbl*xbsz + x;
1071                                                 if (xx >= in1->x) continue;
1072                                                 fp[x] = colp[xx][ch];
1073                                         }
1074                                 }
1075
1076                                 // forward FHT
1077                                 // zero pad data start is different for each == height+1
1078                                 if (!in2done) FHT2D(data1ch, log2_w, log2_h, in2->y+1, 0);
1079                                 FHT2D(data2, log2_w, log2_h, in2->y+1, 0);
1080
1081                                 // FHT2D transposed data, row/col now swapped
1082                                 // convolve & inverse FHT
1083                                 fht_convolve(data2, data1ch, log2_h, log2_w);
1084                                 FHT2D(data2, log2_h, log2_w, 0, 1);
1085                                 // data again transposed, so in order again
1086
1087                                 // overlap-add result
1088                                 for (y=0; y<(int)h2; y++) {
1089                                         const int yy = ybl*ybsz + y - hh;
1090                                         if ((yy < 0) || (yy >= in1->y)) continue;
1091                                         fp = &data2[y*w2];
1092                                         colp = (fRGB*)&rdst->rect[yy*in1->x*in1->type];
1093                                         for (x=0; x<(int)w2; x++) {
1094                                                 const int xx = xbl*xbsz + x - hw;
1095                                                 if ((xx < 0) || (xx >= in1->x)) continue;
1096                                                 colp[xx][ch] += fp[x];
1097                                         }
1098                                 }
1099
1100                         }
1101                         in2done = 1;
1102                 }
1103         }
1104
1105         MEM_freeN(data2);
1106         MEM_freeN(data1);
1107         memcpy(dst->rect, rdst->rect, sizeof(float)*dst->x*dst->y*dst->type);
1108         free_compbuf(rdst);
1109 }
1110
1111
1112 /*
1113  *
1114  * Utility functions qd_* should probably be intergrated better with other functions here.
1115  *
1116  */
1117 // sets fcol to pixelcolor at (x, y)
1118 void qd_getPixel(CompBuf* src, int x, int y, float* col)
1119 {
1120         if(src->rect_procedural) {
1121                 float bc[4];
1122                 src->rect_procedural(src, bc, (float)x/(float)src->xrad, (float)y/(float)src->yrad);
1123
1124                 switch(src->type){
1125                         /* these fallthrough to get all the channels */
1126                         case CB_RGBA: col[3]=bc[3]; 
1127                         case CB_VEC3: col[2]=bc[2];
1128                         case CB_VEC2: col[1]=bc[1];
1129                         case CB_VAL: col[0]=bc[0];
1130                 }
1131         }
1132         else if ((x >= 0) && (x < src->x) && (y >= 0) && (y < src->y)) {
1133                 float* bc = &src->rect[(x + y*src->x)*src->type];
1134                 switch(src->type){
1135                         /* these fallthrough to get all the channels */
1136                         case CB_RGBA: col[3]=bc[3]; 
1137                         case CB_VEC3: col[2]=bc[2];
1138                         case CB_VEC2: col[1]=bc[1];
1139                         case CB_VAL: col[0]=bc[0];
1140                 }
1141         }
1142         else {
1143                 switch(src->type){
1144                         /* these fallthrough to get all the channels */
1145                         case CB_RGBA: col[3]=0.0; 
1146                         case CB_VEC3: col[2]=0.0; 
1147                         case CB_VEC2: col[1]=0.0; 
1148                         case CB_VAL: col[0]=0.0; 
1149                 }
1150         }
1151 }
1152
1153 // sets pixel (x, y) to color col
1154 void qd_setPixel(CompBuf* src, int x, int y, float* col)
1155 {
1156         if ((x >= 0) && (x < src->x) && (y >= 0) && (y < src->y)) {
1157                 float* bc = &src->rect[(x + y*src->x)*src->type];
1158                 switch(src->type){
1159                         /* these fallthrough to get all the channels */
1160                         case CB_RGBA: bc[3]=col[3]; 
1161                         case CB_VEC3: bc[2]=col[2];
1162                         case CB_VEC2: bc[1]=col[1];
1163                         case CB_VAL: bc[0]=col[0];
1164                 }
1165         }
1166 }
1167
1168 // adds fcol to pixelcolor (x, y)
1169 void qd_addPixel(CompBuf* src, int x, int y, float* col)
1170 {
1171         if ((x >= 0) && (x < src->x) && (y >= 0) && (y < src->y)) {
1172                 float* bc = &src->rect[(x + y*src->x)*src->type];
1173                 bc[0] += col[0], bc[1] += col[1], bc[2] += col[2];
1174         }
1175 }
1176
1177 // multiplies pixel by factor value f
1178 void qd_multPixel(CompBuf* src, int x, int y, float f)
1179 {
1180         if ((x >= 0) && (x < src->x) && (y >= 0) && (y < src->y)) {
1181                 float* bc = &src->rect[(x + y*src->x)*src->type];
1182                 bc[0] *= f, bc[1] *= f, bc[2] *= f;
1183         }
1184 }
1185
1186 // bilinear interpolation with wraparound
1187 void qd_getPixelLerpWrap(CompBuf* src, float u, float v, float* col)
1188 {
1189         const float ufl = floor(u), vfl = floor(v);
1190         const int nx = (int)ufl % src->x, ny = (int)vfl % src->y;
1191         const int x1 = (nx < 0) ? (nx + src->x) : nx;
1192         const int y1 = (ny < 0) ? (ny + src->y) : ny;
1193         const int x2 = (x1 + 1) % src->x, y2 = (y1 + 1) % src->y;
1194         const float* c00 = &src->rect[(x1 + y1*src->x)*src->type];
1195         const float* c10 = &src->rect[(x2 + y1*src->x)*src->type];
1196         const float* c01 = &src->rect[(x1 + y2*src->x)*src->type];
1197         const float* c11 = &src->rect[(x2 + y2*src->x)*src->type];
1198         const float uf = u - ufl, vf = v - vfl;
1199         const float w00=(1.f-uf)*(1.f-vf), w10=uf*(1.f-vf), w01=(1.f-uf)*vf, w11=uf*vf;
1200         col[0] = w00*c00[0] + w10*c10[0] + w01*c01[0] + w11*c11[0];
1201         if (src->type != CB_VAL) {
1202                 col[1] = w00*c00[1] + w10*c10[1] + w01*c01[1] + w11*c11[1];
1203                 col[2] = w00*c00[2] + w10*c10[2] + w01*c01[2] + w11*c11[2];
1204                 col[3] = w00*c00[3] + w10*c10[3] + w01*c01[3] + w11*c11[3];
1205         }
1206 }
1207
1208 // as above, without wrap around
1209 void qd_getPixelLerp(CompBuf* src, float u, float v, float* col)
1210 {
1211         const float ufl = floor(u), vfl = floor(v);
1212         const int x1 = (int)ufl, y1 = (int)vfl;
1213         const int x2 = (int)ceil(u), y2 = (int)ceil(v);
1214         if ((x2 >= 0) && (y2 >= 0) && (x1 < src->x) && (y1 < src->y)) {
1215                 const float B[4] = {0,0,0,0};
1216                 const int ox1 = (x1 < 0), oy1 = (y1 < 0), ox2 = (x2 >= src->x), oy2 = (y2 >= src->y);
1217                 const float* c00 = (ox1 || oy1) ? B : &src->rect[(x1 + y1*src->x)*src->type];
1218                 const float* c10 = (ox2 || oy1) ? B : &src->rect[(x2 + y1*src->x)*src->type];
1219                 const float* c01 = (ox1 || oy2) ? B : &src->rect[(x1 + y2*src->x)*src->type];
1220                 const float* c11 = (ox2 || oy2) ? B : &src->rect[(x2 + y2*src->x)*src->type];
1221                 const float uf = u - ufl, vf = v - vfl;
1222                 const float w00=(1.f-uf)*(1.f-vf), w10=uf*(1.f-vf), w01=(1.f-uf)*vf, w11=uf*vf;
1223                 col[0] = w00*c00[0] + w10*c10[0] + w01*c01[0] + w11*c11[0];
1224                 if (src->type != CB_VAL) {
1225                         col[1] = w00*c00[1] + w10*c10[1] + w01*c01[1] + w11*c11[1];
1226                         col[2] = w00*c00[2] + w10*c10[2] + w01*c01[2] + w11*c11[2];
1227                         col[3] = w00*c00[3] + w10*c10[3] + w01*c01[3] + w11*c11[3];
1228                 }
1229         }
1230         else col[0] = col[1] = col[2] = col[3] = 0.f;
1231 }
1232
1233 // as above, sampling only one channel
1234 void qd_getPixelLerpChan(CompBuf* src, float u, float v, int chan, float* out)
1235 {
1236         const float ufl = floor(u), vfl = floor(v);
1237         const int x1 = (int)ufl, y1 = (int)vfl;
1238         const int x2 = (int)ceil(u), y2 = (int)ceil(v);
1239         if (chan >= src->type) chan = 0;
1240         if ((x2 >= 0) && (y2 >= 0) && (x1 < src->x) && (y1 < src->y)) {
1241                 const float B[4] = {0,0,0,0};
1242                 const int ox1 = (x1 < 0), oy1 = (y1 < 0), ox2 = (x2 >= src->x), oy2 = (y2 >= src->y);
1243                 const float* c00 = (ox1 || oy1) ? B : &src->rect[(x1 + y1*src->x)*src->type + chan];
1244                 const float* c10 = (ox2 || oy1) ? B : &src->rect[(x2 + y1*src->x)*src->type + chan];
1245                 const float* c01 = (ox1 || oy2) ? B : &src->rect[(x1 + y2*src->x)*src->type + chan];
1246                 const float* c11 = (ox2 || oy2) ? B : &src->rect[(x2 + y2*src->x)*src->type + chan];
1247                 const float uf = u - ufl, vf = v - vfl;
1248                 const float w00=(1.f-uf)*(1.f-vf), w10=uf*(1.f-vf), w01=(1.f-uf)*vf, w11=uf*vf;
1249                 out[0] = w00*c00[0] + w10*c10[0] + w01*c01[0] + w11*c11[0];
1250         }
1251         else *out = 0.f;
1252 }
1253
1254
1255 CompBuf* qd_downScaledCopy(CompBuf* src, int scale)
1256 {
1257         CompBuf* fbuf;
1258         if (scale <= 1)
1259                 fbuf = dupalloc_compbuf(src);
1260         else {
1261                 int nw = src->x/scale, nh = src->y/scale;
1262                 if ((2*(src->x % scale)) > scale) nw++;
1263                 if ((2*(src->y % scale)) > scale) nh++;
1264                 fbuf = alloc_compbuf(nw, nh, src->type, 1);
1265                 {
1266                         int x, y, xx, yy, sx, sy, mx, my;
1267                         float colsum[4] = {0.0f, 0.0f, 0.0f, 0.0f};
1268                         float fscale = 1.f/(float)(scale*scale);
1269                         for (y=0; y<nh; y++) {
1270                                 fRGB* fcolp = (fRGB*)&fbuf->rect[y*fbuf->x*fbuf->type];
1271                                 yy = y*scale;
1272                                 my = yy + scale;
1273                                 if (my > src->y) my = src->y;
1274                                 for (x=0; x<nw; x++) {
1275                                         xx = x*scale;
1276                                         mx = xx + scale;
1277                                         if (mx > src->x) mx = src->x;
1278                                         colsum[0] = colsum[1] = colsum[2] = 0.f;
1279                                         for (sy=yy; sy<my; sy++) {
1280                                                 fRGB* scolp = (fRGB*)&src->rect[sy*src->x*src->type];
1281                                                 for (sx=xx; sx<mx; sx++)
1282                                                         fRGB_add(colsum, scolp[sx]);
1283                                         }
1284                                         fRGB_mult(colsum, fscale);
1285                                         fRGB_copy(fcolp[x], colsum);
1286                                 }
1287                         }
1288                 }
1289         }
1290         return fbuf;
1291 }
1292
1293 // fast g.blur, per channel
1294 // xy var. bits 1 & 2 ca be used to blur in x or y direction separately
1295 void IIR_gauss(CompBuf* src, float sigma, int chan, int xy)
1296 {
1297         double q, q2, sc, cf[4], tsM[9], tsu[3], tsv[3];
1298         double *X, *Y, *W;
1299         int i, x, y, sz;
1300
1301         // <0.5 not valid, though can have a possibly useful sort of sharpening effect
1302         if (sigma < 0.5f) return;
1303         
1304         if ((xy < 1) || (xy > 3)) xy = 3;
1305         
1306         // XXX The YVV macro defined below explicitly expects sources of at least 3x3 pixels,
1307         //     so just skiping blur along faulty direction if src's def is below that limit!
1308         if (src->x < 3) xy &= ~(int) 1;
1309         if (src->y < 3) xy &= ~(int) 2;
1310         if (xy < 1) return;
1311
1312         // see "Recursive Gabor Filtering" by Young/VanVliet
1313         // all factors here in double.prec. Required, because for single.prec it seems to blow up if sigma > ~200
1314         if (sigma >= 3.556f)
1315                 q = 0.9804f*(sigma - 3.556f) + 2.5091f;
1316         else // sigma >= 0.5
1317                 q = (0.0561f*sigma + 0.5784f)*sigma - 0.2568f;
1318         q2 = q*q;
1319         sc = (1.1668 + q)*(3.203729649  + (2.21566 + q)*q);
1320         // no gabor filtering here, so no complex multiplies, just the regular coefs.
1321         // all negated here, so as not to have to recalc Triggs/Sdika matrix
1322         cf[1] = q*(5.788961737 + (6.76492 + 3.0*q)*q)/ sc;
1323         cf[2] = -q2*(3.38246 + 3.0*q)/sc;
1324         // 0 & 3 unchanged
1325         cf[3] = q2*q/sc;
1326         cf[0] = 1.0 - cf[1] - cf[2] - cf[3];
1327
1328         // Triggs/Sdika border corrections,
1329         // it seems to work, not entirely sure if it is actually totally correct,
1330         // Besides J.M.Geusebroek's anigauss.c (see http://www.science.uva.nl/~mark),
1331         // found one other implementation by Cristoph Lampert,
1332         // but neither seem to be quite the same, result seems to be ok so far anyway.
1333         // Extra scale factor here to not have to do it in filter,
1334         // though maybe this had something to with the precision errors
1335         sc = cf[0]/((1.0 + cf[1] - cf[2] + cf[3])*(1.0 - cf[1] - cf[2] - cf[3])*(1.0 + cf[2] + (cf[1] - cf[3])*cf[3]));
1336         tsM[0] = sc*(-cf[3]*cf[1] + 1.0 - cf[3]*cf[3] - cf[2]);
1337         tsM[1] = sc*((cf[3] + cf[1])*(cf[2] + cf[3]*cf[1]));
1338         tsM[2] = sc*(cf[3]*(cf[1] + cf[3]*cf[2]));
1339         tsM[3] = sc*(cf[1] + cf[3]*cf[2]);
1340         tsM[4] = sc*(-(cf[2] - 1.0)*(cf[2] + cf[3]*cf[1]));
1341         tsM[5] = sc*(-(cf[3]*cf[1] + cf[3]*cf[3] + cf[2] - 1.0)*cf[3]);
1342         tsM[6] = sc*(cf[3]*cf[1] + cf[2] + cf[1]*cf[1] - cf[2]*cf[2]);
1343         tsM[7] = sc*(cf[1]*cf[2] + cf[3]*cf[2]*cf[2] - cf[1]*cf[3]*cf[3] - cf[3]*cf[3]*cf[3] - cf[3]*cf[2] + cf[3]);
1344         tsM[8] = sc*(cf[3]*(cf[1] + cf[3]*cf[2]));
1345
1346 #define YVV(L)                                                                \
1347 {                                                                             \
1348         W[0] = cf[0]*X[0] + cf[1]*X[0] + cf[2]*X[0] + cf[3]*X[0];                 \
1349         W[1] = cf[0]*X[1] + cf[1]*W[0] + cf[2]*X[0] + cf[3]*X[0];                 \
1350         W[2] = cf[0]*X[2] + cf[1]*W[1] + cf[2]*W[0] + cf[3]*X[0];                 \
1351         for (i=3; i<L; i++)                                                       \
1352                 W[i] = cf[0]*X[i] + cf[1]*W[i-1] + cf[2]*W[i-2] + cf[3]*W[i-3];       \
1353         tsu[0] = W[L-1] - X[L-1];                                                 \
1354         tsu[1] = W[L-2] - X[L-1];                                                 \
1355         tsu[2] = W[L-3] - X[L-1];                                                 \
1356         tsv[0] = tsM[0]*tsu[0] + tsM[1]*tsu[1] + tsM[2]*tsu[2] + X[L-1];          \
1357         tsv[1] = tsM[3]*tsu[0] + tsM[4]*tsu[1] + tsM[5]*tsu[2] + X[L-1];          \
1358         tsv[2] = tsM[6]*tsu[0] + tsM[7]*tsu[1] + tsM[8]*tsu[2] + X[L-1];          \
1359         Y[L-1] = cf[0]*W[L-1] + cf[1]*tsv[0] + cf[2]*tsv[1] + cf[3]*tsv[2];       \
1360         Y[L-2] = cf[0]*W[L-2] + cf[1]*Y[L-1] + cf[2]*tsv[0] + cf[3]*tsv[1];       \
1361         Y[L-3] = cf[0]*W[L-3] + cf[1]*Y[L-2] + cf[2]*Y[L-1] + cf[3]*tsv[0];       \
1362         for (i=L-4; i>=0; i--)                                                    \
1363                 Y[i] = cf[0]*W[i] + cf[1]*Y[i+1] + cf[2]*Y[i+2] + cf[3]*Y[i+3];       \
1364 }
1365
1366         // intermediate buffers
1367         sz = MAX2(src->x, src->y);
1368         X = MEM_callocN(sz*sizeof(double), "IIR_gauss X buf");
1369         Y = MEM_callocN(sz*sizeof(double), "IIR_gauss Y buf");
1370         W = MEM_callocN(sz*sizeof(double), "IIR_gauss W buf");
1371         if (xy & 1) {   // H
1372                 for (y=0; y<src->y; ++y) {
1373                         const int yx = y*src->x;
1374                         for (x=0; x<src->x; ++x)
1375                                 X[x] = src->rect[(x + yx)*src->type + chan];
1376                         YVV(src->x);
1377                         for (x=0; x<src->x; ++x)
1378                                 src->rect[(x + yx)*src->type + chan] = Y[x];
1379                 }
1380         }
1381         if (xy & 2) {   // V
1382                 for (x=0; x<src->x; ++x) {
1383                         for (y=0; y<src->y; ++y)
1384                                 X[y] = src->rect[(x + y*src->x)*src->type + chan];
1385                         YVV(src->y);
1386                         for (y=0; y<src->y; ++y)
1387                                 src->rect[(x + y*src->x)*src->type + chan] = Y[y];
1388                 }
1389         }
1390
1391         MEM_freeN(X);
1392         MEM_freeN(W);
1393         MEM_freeN(Y);
1394 #undef YVV
1395 }
1396