svn merge ^/trunk/blender -r42927:42931
[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 profile_from= (rd->color_mgt_flag & R_COLOR_MANAGEMENT)? IB_PROFILE_LINEAR_RGB: IB_PROFILE_SRGB;
610         int predivide= 0;
611         int dither= 0;
612         unsigned char *rect;
613         
614         if(preview && stackbuf) {
615                 CompBuf *cbuf, *stackbuf_use;
616                 
617                 if(stackbuf->rect==NULL && stackbuf->rect_procedural==NULL) return;
618                 
619                 stackbuf_use= typecheck_compbuf(stackbuf, CB_RGBA);
620
621                 if(stackbuf->x > stackbuf->y) {
622                         xsize= 140;
623                         ysize= (140*stackbuf->y)/stackbuf->x;
624                 }
625                 else {
626                         ysize= 140;
627                         xsize= (140*stackbuf->x)/stackbuf->y;
628                 }
629                 
630                 if(stackbuf_use->rect_procedural)
631                         cbuf= generate_procedural_preview(stackbuf_use, xsize, ysize);
632                 else
633                         cbuf= scalefast_compbuf(stackbuf_use, xsize, ysize);
634
635                 /* convert to byte for preview */
636                 rect= MEM_callocN(sizeof(unsigned char)*4*xsize*ysize, "bNodePreview.rect");
637
638                 IMB_buffer_byte_from_float(rect, cbuf->rect,
639                         4, dither, IB_PROFILE_SRGB, profile_from, predivide, 
640                         xsize, ysize, xsize, xsize);
641                 
642                 free_compbuf(cbuf);
643                 if(stackbuf_use!=stackbuf)
644                         free_compbuf(stackbuf_use);
645
646                 BLI_lock_thread(LOCK_PREVIEW);
647
648                 if(preview->rect)
649                         MEM_freeN(preview->rect);
650                 preview->xsize= xsize;
651                 preview->ysize= ysize;
652                 preview->rect= rect;
653
654                 BLI_unlock_thread(LOCK_PREVIEW);
655         }
656 }
657
658 void do_rgba_to_yuva(bNode *UNUSED(node), float *out, float *in)
659 {
660         rgb_to_yuv(in[0],in[1],in[2], &out[0], &out[1], &out[2]);
661         out[3]=in[3];
662 }
663
664 void do_rgba_to_hsva(bNode *UNUSED(node), float *out, float *in)
665 {
666         rgb_to_hsv(in[0],in[1],in[2], &out[0], &out[1], &out[2]);
667         out[3]=in[3];
668 }
669
670 void do_rgba_to_ycca(bNode *UNUSED(node), float *out, float *in)
671 {
672         rgb_to_ycc(in[0],in[1],in[2], &out[0], &out[1], &out[2], BLI_YCC_ITU_BT601);
673         out[3]=in[3];
674 }
675
676 void do_yuva_to_rgba(bNode *UNUSED(node), float *out, float *in)
677 {
678         yuv_to_rgb(in[0],in[1],in[2], &out[0], &out[1], &out[2]);
679         out[3]=in[3];
680 }
681
682 void do_hsva_to_rgba(bNode *UNUSED(node), float *out, float *in)
683 {
684         hsv_to_rgb(in[0],in[1],in[2], &out[0], &out[1], &out[2]);
685         out[3]=in[3];
686 }
687
688 void do_ycca_to_rgba(bNode *UNUSED(node), float *out, float *in)
689 {
690         ycc_to_rgb(in[0],in[1],in[2], &out[0], &out[1], &out[2], BLI_YCC_ITU_BT601);
691         out[3]=in[3];
692 }
693
694 void do_copy_rgba(bNode *UNUSED(node), float *out, float *in)
695 {
696         copy_v4_v4(out, in);
697 }
698
699 void do_copy_rgb(bNode *UNUSED(node), float *out, float *in)
700 {
701         copy_v3_v3(out, in);
702         out[3]= 1.0f;
703 }
704
705 void do_copy_value(bNode *UNUSED(node), float *out, float *in)
706 {
707         out[0]= in[0];
708 }
709
710 void do_copy_a_rgba(bNode *UNUSED(node), float *out, float *in, float *fac)
711 {
712         copy_v3_v3(out, in);
713         out[3]= *fac;
714 }
715
716 /* only accepts RGBA buffers */
717 void gamma_correct_compbuf(CompBuf *img, int inversed)
718 {
719         float *drect;
720         int x;
721
722         if(img->type!=CB_RGBA) return;
723
724         drect= img->rect;
725         if(inversed) {
726                 for(x=img->x*img->y; x>0; x--, drect+=4) {
727                         if(drect[0]>0.0f) drect[0]= sqrt(drect[0]); else drect[0]= 0.0f;
728                         if(drect[1]>0.0f) drect[1]= sqrt(drect[1]); else drect[1]= 0.0f;
729                         if(drect[2]>0.0f) drect[2]= sqrt(drect[2]); else drect[2]= 0.0f;
730                 }
731         }
732         else {
733                 for(x=img->x*img->y; x>0; x--, drect+=4) {
734                         if(drect[0]>0.0f) drect[0]*= drect[0]; else drect[0]= 0.0f;
735                         if(drect[1]>0.0f) drect[1]*= drect[1]; else drect[1]= 0.0f;
736                         if(drect[2]>0.0f) drect[2]*= drect[2]; else drect[2]= 0.0f;
737                 }
738         }
739 }
740
741 void premul_compbuf(CompBuf *img, int inversed)
742 {
743         float *drect;
744         int x;
745
746         if(img->type!=CB_RGBA) return;
747
748         drect= img->rect;
749         if(inversed) {
750                 for(x=img->x*img->y; x>0; x--, drect+=4) {
751                         if(fabsf(drect[3]) < 1e-5f) {
752                                 drect[0]= 0.0f;
753                                 drect[1]= 0.0f;
754                                 drect[2]= 0.0f;
755                         }
756                         else {
757                                 drect[0] /= drect[3];
758                                 drect[1] /= drect[3];
759                                 drect[2] /= drect[3];
760                         }
761                 }
762         }
763         else {
764                 for(x=img->x*img->y; x>0; x--, drect+=4) {
765                         drect[0] *= drect[3];
766                         drect[1] *= drect[3];
767                         drect[2] *= drect[3];
768                 }
769         }
770 }
771
772
773
774 /*
775  *  2D Fast Hartley Transform, used for convolution
776  */
777
778 typedef float fREAL;
779
780 // returns next highest power of 2 of x, as well it's log2 in L2
781 static unsigned int nextPow2(unsigned int x, unsigned int* L2)
782 {
783         unsigned int pw, x_notpow2 = x & (x-1);
784         *L2 = 0;
785         while (x>>=1) ++(*L2);
786         pw = 1 << (*L2);
787         if (x_notpow2) { (*L2)++;  pw<<=1; }
788         return pw;
789 }
790
791 //------------------------------------------------------------------------------
792
793 // from FXT library by Joerg Arndt, faster in order bitreversal
794 // use: r = revbin_upd(r, h) where h = N>>1
795 static unsigned int revbin_upd(unsigned int r, unsigned int h)
796 {
797         while (!((r^=h)&h)) h >>= 1;
798         return r;
799 }
800 //------------------------------------------------------------------------------
801 static void FHT(fREAL* data, unsigned int M, unsigned int inverse)
802 {
803         double tt, fc, dc, fs, ds, a = M_PI;
804         fREAL t1, t2;
805         int n2, bd, bl, istep, k, len = 1 << M, n = 1;
806
807         int i, j = 0;
808         unsigned int Nh = len >> 1;
809         for (i=1;i<(len-1);++i) {
810                 j = revbin_upd(j, Nh);
811                 if (j>i) {
812                         t1 = data[i];
813                         data[i] = data[j];
814                         data[j] = t1;
815                 }
816         }
817
818         do {
819                 fREAL* data_n = &data[n];
820
821                 istep = n << 1;
822                 for (k=0; k<len; k+=istep) {
823                         t1 = data_n[k];
824                         data_n[k] = data[k] - t1;
825                         data[k] += t1;
826                 }
827
828                 n2 = n >> 1;
829                 if (n>2) {
830                         fc = dc = cos(a);
831                         fs = ds = sqrt(1.0 - fc*fc); //sin(a);
832                         bd = n-2;
833                         for (bl=1; bl<n2; bl++) {
834                                 fREAL* data_nbd = &data_n[bd];
835                                 fREAL* data_bd = &data[bd];
836                                 for (k=bl; k<len; k+=istep) {
837                                         t1 = fc*data_n[k] + fs*data_nbd[k];
838                                         t2 = fs*data_n[k] - fc*data_nbd[k];
839                                         data_n[k] = data[k] - t1;
840                                         data_nbd[k] = data_bd[k] - t2;
841                                         data[k] += t1;
842                                         data_bd[k] += t2;
843                                 }
844                                 tt = fc*dc - fs*ds;
845                                 fs = fs*dc + fc*ds;
846                                 fc = tt;
847                                 bd -= 2;
848                         }
849                 }
850
851                 if (n>1) {
852                         for (k=n2; k<len; k+=istep) {
853                                 t1 = data_n[k];
854                                 data_n[k] = data[k] - t1;
855                                 data[k] += t1;
856                         }
857                 }
858
859                 n = istep;
860                 a *= 0.5;
861         } while (n<len);
862
863         if (inverse) {
864                 fREAL sc = (fREAL)1 / (fREAL)len;
865                 for (k=0; k<len; ++k)
866                         data[k] *= sc;
867         }
868 }
869 //------------------------------------------------------------------------------
870 /* 2D Fast Hartley Transform, Mx/My -> log2 of width/height,
871         nzp -> the row where zero pad data starts,
872         inverse -> see above */
873 static void FHT2D(fREAL *data, unsigned int Mx, unsigned int My,
874                 unsigned int nzp, unsigned int inverse)
875 {
876         unsigned int i, j, Nx, Ny, maxy;
877         fREAL t;
878
879         Nx = 1 << Mx;
880         Ny = 1 << My;
881
882         // rows (forward transform skips 0 pad data)
883         maxy = inverse ? Ny : nzp;
884         for (j=0; j<maxy; ++j)
885                 FHT(&data[Nx*j], Mx, inverse);
886
887         // transpose data
888         if (Nx==Ny) {  // square
889                 for (j=0; j<Ny; ++j)
890                         for (i=j+1; i<Nx; ++i) {
891                                 unsigned int op = i + (j << Mx), np = j + (i << My);
892                                 t=data[op], data[op]=data[np], data[np]=t;
893                         }
894         }
895         else {  // rectangular
896                 unsigned int k, Nym = Ny-1, stm = 1 << (Mx + My);
897                 for (i=0; stm>0; i++) {
898                         #define pred(k) (((k & Nym) << Mx) + (k >> My))
899                         for (j=pred(i); j>i; j=pred(j));
900                         if (j < i) continue;
901                         for (k=i, j=pred(i); j!=i; k=j, j=pred(j), stm--)
902                                 { t=data[j], data[j]=data[k], data[k]=t; }
903                         #undef pred
904                         stm--;
905                 }
906         }
907         // swap Mx/My & Nx/Ny
908         i = Nx, Nx = Ny, Ny = i;
909         i = Mx, Mx = My, My = i;
910
911         // now columns == transposed rows
912         for (j=0; j<Ny; ++j)
913                 FHT(&data[Nx*j], Mx, inverse);
914
915         // finalize
916         for (j=0; j<=(Ny >> 1); j++) {
917                 unsigned int jm = (Ny - j) & (Ny-1);
918                 unsigned int ji = j << Mx;
919                 unsigned int jmi = jm << Mx;
920                 for (i=0; i<=(Nx >> 1); i++) {
921                         unsigned int im = (Nx - i) & (Nx-1);
922                         fREAL A = data[ji + i];
923                         fREAL B = data[jmi + i];
924                         fREAL C = data[ji + im];
925                         fREAL D = data[jmi + im];
926                         fREAL E = (fREAL)0.5*((A + D) - (B + C));
927                         data[ji + i] = A - E;
928                         data[jmi + i] = B + E;
929                         data[ji + im] = C + E;
930                         data[jmi + im] = D - E;
931                 }
932         }
933
934 }
935
936 //------------------------------------------------------------------------------
937
938 /* 2D convolution calc, d1 *= d2, M/N - > log2 of width/height */
939 static void fht_convolve(fREAL* d1, fREAL* d2, unsigned int M, unsigned int N)
940 {
941         fREAL a, b;
942         unsigned int i, j, k, L, mj, mL;
943         unsigned int m = 1 << M, n = 1 << N;
944         unsigned int m2 = 1 << (M-1), n2 = 1 << (N-1);
945         unsigned int mn2 = m << (N-1);
946
947         d1[0] *= d2[0];
948         d1[mn2] *= d2[mn2];
949         d1[m2] *= d2[m2];
950         d1[m2 + mn2] *= d2[m2 + mn2];
951         for (i=1; i<m2; i++) {
952                 k = m - i;
953                 a = d1[i]*d2[i] - d1[k]*d2[k];
954                 b = d1[k]*d2[i] + d1[i]*d2[k];
955                 d1[i] = (b + a)*(fREAL)0.5;
956                 d1[k] = (b - a)*(fREAL)0.5;
957                 a = d1[i + mn2]*d2[i + mn2] - d1[k + mn2]*d2[k + mn2];
958                 b = d1[k + mn2]*d2[i + mn2] + d1[i + mn2]*d2[k + mn2];
959                 d1[i + mn2] = (b + a)*(fREAL)0.5;
960                 d1[k + mn2] = (b - a)*(fREAL)0.5;
961         }
962         for (j=1; j<n2; j++) {
963                 L = n - j;
964                 mj = j << M;
965                 mL = L << M;
966                 a = d1[mj]*d2[mj] - d1[mL]*d2[mL];
967                 b = d1[mL]*d2[mj] + d1[mj]*d2[mL];
968                 d1[mj] = (b + a)*(fREAL)0.5;
969                 d1[mL] = (b - a)*(fREAL)0.5;
970                 a = d1[m2 + mj]*d2[m2 + mj] - d1[m2 + mL]*d2[m2 + mL];
971                 b = d1[m2 + mL]*d2[m2 + mj] + d1[m2 + mj]*d2[m2 + mL];
972                 d1[m2 + mj] = (b + a)*(fREAL)0.5;
973                 d1[m2 + mL] = (b - a)*(fREAL)0.5;
974         }
975         for (i=1; i<m2; i++) {
976                 k = m - i;
977                 for (j=1; j<n2; j++) {
978                         L = n - j;
979                         mj = j << M;
980                         mL = L << M;
981                         a = d1[i + mj]*d2[i + mj] - d1[k + mL]*d2[k + mL];
982                         b = d1[k + mL]*d2[i + mj] + d1[i + mj]*d2[k + mL];
983                         d1[i + mj] = (b + a)*(fREAL)0.5;
984                         d1[k + mL] = (b - a)*(fREAL)0.5;
985                         a = d1[i + mL]*d2[i + mL] - d1[k + mj]*d2[k + mj];
986                         b = d1[k + mj]*d2[i + mL] + d1[i + mL]*d2[k + mj];
987                         d1[i + mL] = (b + a)*(fREAL)0.5;
988                         d1[k + mj] = (b - a)*(fREAL)0.5;
989                 }
990         }
991 }
992
993 //------------------------------------------------------------------------------
994
995 void convolve(CompBuf* dst, CompBuf* in1, CompBuf* in2)
996 {
997         fREAL *data1, *data2, *fp;
998         unsigned int w2, h2, hw, hh, log2_w, log2_h;
999         fRGB wt, *colp;
1000         int x, y, ch;
1001         int xbl, ybl, nxb, nyb, xbsz, ybsz;
1002         int in2done = 0;
1003
1004         CompBuf* rdst = alloc_compbuf(in1->x, in1->y, in1->type, 1);
1005
1006         // convolution result width & height
1007         w2 = 2*in2->x - 1;
1008         h2 = 2*in2->y - 1;
1009         // FFT pow2 required size & log2
1010         w2 = nextPow2(w2, &log2_w);
1011         h2 = nextPow2(h2, &log2_h);
1012
1013         // alloc space
1014         data1 = (fREAL*)MEM_callocN(3*w2*h2*sizeof(fREAL), "convolve_fast FHT data1");
1015         data2 = (fREAL*)MEM_callocN(w2*h2*sizeof(fREAL), "convolve_fast FHT data2");
1016
1017         // normalize convolutor
1018         wt[0] = wt[1] = wt[2] = 0.f;
1019         for (y=0; y<in2->y; y++) {
1020                 colp = (fRGB*)&in2->rect[y*in2->x*in2->type];
1021                 for (x=0; x<in2->x; x++)
1022                         fRGB_add(wt, colp[x]);
1023         }
1024         if (wt[0] != 0.f) wt[0] = 1.f/wt[0];
1025         if (wt[1] != 0.f) wt[1] = 1.f/wt[1];
1026         if (wt[2] != 0.f) wt[2] = 1.f/wt[2];
1027         for (y=0; y<in2->y; y++) {
1028                 colp = (fRGB*)&in2->rect[y*in2->x*in2->type];
1029                 for (x=0; x<in2->x; x++)
1030                         fRGB_colormult(colp[x], wt);
1031         }
1032
1033         // copy image data, unpacking interleaved RGBA into separate channels
1034         // only need to calc data1 once
1035
1036         // block add-overlap
1037         hw = in2->x >> 1;
1038         hh = in2->y >> 1;
1039         xbsz = (w2 + 1) - in2->x;
1040         ybsz = (h2 + 1) - in2->y;
1041         nxb = in1->x / xbsz;
1042         if (in1->x % xbsz) nxb++;
1043         nyb = in1->y / ybsz;
1044         if (in1->y % ybsz) nyb++;
1045         for (ybl=0; ybl<nyb; ybl++) {
1046                 for (xbl=0; xbl<nxb; xbl++) {
1047
1048                         // each channel one by one
1049                         for (ch=0; ch<3; ch++) {
1050                                 fREAL* data1ch = &data1[ch*w2*h2];
1051
1052                                 // only need to calc fht data from in2 once, can re-use for every block
1053                                 if (!in2done) {
1054                                         // in2, channel ch -> data1
1055                                         for (y=0; y<in2->y; y++) {
1056                                                 fp = &data1ch[y*w2];
1057                                                 colp = (fRGB*)&in2->rect[y*in2->x*in2->type];
1058                                                 for (x=0; x<in2->x; x++)
1059                                                         fp[x] = colp[x][ch];
1060                                         }
1061                                 }
1062
1063                                 // in1, channel ch -> data2
1064                                 memset(data2, 0, w2*h2*sizeof(fREAL));
1065                                 for (y=0; y<ybsz; y++) {
1066                                         int yy = ybl*ybsz + y;
1067                                         if (yy >= in1->y) continue;
1068                                         fp = &data2[y*w2];
1069                                         colp = (fRGB*)&in1->rect[yy*in1->x*in1->type];
1070                                         for (x=0; x<xbsz; x++) {
1071                                                 int xx = xbl*xbsz + x;
1072                                                 if (xx >= in1->x) continue;
1073                                                 fp[x] = colp[xx][ch];
1074                                         }
1075                                 }
1076
1077                                 // forward FHT
1078                                 // zero pad data start is different for each == height+1
1079                                 if (!in2done) FHT2D(data1ch, log2_w, log2_h, in2->y+1, 0);
1080                                 FHT2D(data2, log2_w, log2_h, in2->y+1, 0);
1081
1082                                 // FHT2D transposed data, row/col now swapped
1083                                 // convolve & inverse FHT
1084                                 fht_convolve(data2, data1ch, log2_h, log2_w);
1085                                 FHT2D(data2, log2_h, log2_w, 0, 1);
1086                                 // data again transposed, so in order again
1087
1088                                 // overlap-add result
1089                                 for (y=0; y<(int)h2; y++) {
1090                                         const int yy = ybl*ybsz + y - hh;
1091                                         if ((yy < 0) || (yy >= in1->y)) continue;
1092                                         fp = &data2[y*w2];
1093                                         colp = (fRGB*)&rdst->rect[yy*in1->x*in1->type];
1094                                         for (x=0; x<(int)w2; x++) {
1095                                                 const int xx = xbl*xbsz + x - hw;
1096                                                 if ((xx < 0) || (xx >= in1->x)) continue;
1097                                                 colp[xx][ch] += fp[x];
1098                                         }
1099                                 }
1100
1101                         }
1102                         in2done = 1;
1103                 }
1104         }
1105
1106         MEM_freeN(data2);
1107         MEM_freeN(data1);
1108         memcpy(dst->rect, rdst->rect, sizeof(float)*dst->x*dst->y*dst->type);
1109         free_compbuf(rdst);
1110 }
1111
1112
1113 /*
1114  *
1115  * Utility functions qd_* should probably be intergrated better with other functions here.
1116  *
1117  */
1118 // sets fcol to pixelcolor at (x, y)
1119 void qd_getPixel(CompBuf* src, int x, int y, float* col)
1120 {
1121         if(src->rect_procedural) {
1122                 float bc[4];
1123                 src->rect_procedural(src, bc, (float)x/(float)src->xrad, (float)y/(float)src->yrad);
1124
1125                 switch(src->type){
1126                         /* these fallthrough to get all the channels */
1127                         case CB_RGBA: col[3]=bc[3]; 
1128                         case CB_VEC3: col[2]=bc[2];
1129                         case CB_VEC2: col[1]=bc[1];
1130                         case CB_VAL: col[0]=bc[0];
1131                 }
1132         }
1133         else if ((x >= 0) && (x < src->x) && (y >= 0) && (y < src->y)) {
1134                 float* bc = &src->rect[(x + y*src->x)*src->type];
1135                 switch(src->type){
1136                         /* these fallthrough to get all the channels */
1137                         case CB_RGBA: col[3]=bc[3]; 
1138                         case CB_VEC3: col[2]=bc[2];
1139                         case CB_VEC2: col[1]=bc[1];
1140                         case CB_VAL: col[0]=bc[0];
1141                 }
1142         }
1143         else {
1144                 switch(src->type){
1145                         /* these fallthrough to get all the channels */
1146                         case CB_RGBA: col[3]=0.0; 
1147                         case CB_VEC3: col[2]=0.0; 
1148                         case CB_VEC2: col[1]=0.0; 
1149                         case CB_VAL: col[0]=0.0; 
1150                 }
1151         }
1152 }
1153
1154 // sets pixel (x, y) to color col
1155 void qd_setPixel(CompBuf* src, int x, int y, float* col)
1156 {
1157         if ((x >= 0) && (x < src->x) && (y >= 0) && (y < src->y)) {
1158                 float* bc = &src->rect[(x + y*src->x)*src->type];
1159                 switch(src->type){
1160                         /* these fallthrough to get all the channels */
1161                         case CB_RGBA: bc[3]=col[3]; 
1162                         case CB_VEC3: bc[2]=col[2];
1163                         case CB_VEC2: bc[1]=col[1];
1164                         case CB_VAL: bc[0]=col[0];
1165                 }
1166         }
1167 }
1168
1169 // adds fcol to pixelcolor (x, y)
1170 void qd_addPixel(CompBuf* src, int x, int y, float* col)
1171 {
1172         if ((x >= 0) && (x < src->x) && (y >= 0) && (y < src->y)) {
1173                 float* bc = &src->rect[(x + y*src->x)*src->type];
1174                 bc[0] += col[0], bc[1] += col[1], bc[2] += col[2];
1175         }
1176 }
1177
1178 // multiplies pixel by factor value f
1179 void qd_multPixel(CompBuf* src, int x, int y, float f)
1180 {
1181         if ((x >= 0) && (x < src->x) && (y >= 0) && (y < src->y)) {
1182                 float* bc = &src->rect[(x + y*src->x)*src->type];
1183                 bc[0] *= f, bc[1] *= f, bc[2] *= f;
1184         }
1185 }
1186
1187 // bilinear interpolation with wraparound
1188 void qd_getPixelLerpWrap(CompBuf* src, float u, float v, float* col)
1189 {
1190         const float ufl = floor(u), vfl = floor(v);
1191         const int nx = (int)ufl % src->x, ny = (int)vfl % src->y;
1192         const int x1 = (nx < 0) ? (nx + src->x) : nx;
1193         const int y1 = (ny < 0) ? (ny + src->y) : ny;
1194         const int x2 = (x1 + 1) % src->x, y2 = (y1 + 1) % src->y;
1195         const float* c00 = &src->rect[(x1 + y1*src->x)*src->type];
1196         const float* c10 = &src->rect[(x2 + y1*src->x)*src->type];
1197         const float* c01 = &src->rect[(x1 + y2*src->x)*src->type];
1198         const float* c11 = &src->rect[(x2 + y2*src->x)*src->type];
1199         const float uf = u - ufl, vf = v - vfl;
1200         const float w00=(1.f-uf)*(1.f-vf), w10=uf*(1.f-vf), w01=(1.f-uf)*vf, w11=uf*vf;
1201         col[0] = w00*c00[0] + w10*c10[0] + w01*c01[0] + w11*c11[0];
1202         if (src->type != CB_VAL) {
1203                 col[1] = w00*c00[1] + w10*c10[1] + w01*c01[1] + w11*c11[1];
1204                 col[2] = w00*c00[2] + w10*c10[2] + w01*c01[2] + w11*c11[2];
1205                 col[3] = w00*c00[3] + w10*c10[3] + w01*c01[3] + w11*c11[3];
1206         }
1207 }
1208
1209 // as above, without wrap around
1210 void qd_getPixelLerp(CompBuf* src, float u, float v, float* col)
1211 {
1212         const float ufl = floor(u), vfl = floor(v);
1213         const int x1 = (int)ufl, y1 = (int)vfl;
1214         const int x2 = (int)ceil(u), y2 = (int)ceil(v);
1215         if ((x2 >= 0) && (y2 >= 0) && (x1 < src->x) && (y1 < src->y)) {
1216                 const float B[4] = {0,0,0,0};
1217                 const int ox1 = (x1 < 0), oy1 = (y1 < 0), ox2 = (x2 >= src->x), oy2 = (y2 >= src->y);
1218                 const float* c00 = (ox1 || oy1) ? B : &src->rect[(x1 + y1*src->x)*src->type];
1219                 const float* c10 = (ox2 || oy1) ? B : &src->rect[(x2 + y1*src->x)*src->type];
1220                 const float* c01 = (ox1 || oy2) ? B : &src->rect[(x1 + y2*src->x)*src->type];
1221                 const float* c11 = (ox2 || oy2) ? B : &src->rect[(x2 + y2*src->x)*src->type];
1222                 const float uf = u - ufl, vf = v - vfl;
1223                 const float w00=(1.f-uf)*(1.f-vf), w10=uf*(1.f-vf), w01=(1.f-uf)*vf, w11=uf*vf;
1224                 col[0] = w00*c00[0] + w10*c10[0] + w01*c01[0] + w11*c11[0];
1225                 if (src->type != CB_VAL) {
1226                         col[1] = w00*c00[1] + w10*c10[1] + w01*c01[1] + w11*c11[1];
1227                         col[2] = w00*c00[2] + w10*c10[2] + w01*c01[2] + w11*c11[2];
1228                         col[3] = w00*c00[3] + w10*c10[3] + w01*c01[3] + w11*c11[3];
1229                 }
1230         }
1231         else col[0] = col[1] = col[2] = col[3] = 0.f;
1232 }
1233
1234 // as above, sampling only one channel
1235 void qd_getPixelLerpChan(CompBuf* src, float u, float v, int chan, float* out)
1236 {
1237         const float ufl = floor(u), vfl = floor(v);
1238         const int x1 = (int)ufl, y1 = (int)vfl;
1239         const int x2 = (int)ceil(u), y2 = (int)ceil(v);
1240         if (chan >= src->type) chan = 0;
1241         if ((x2 >= 0) && (y2 >= 0) && (x1 < src->x) && (y1 < src->y)) {
1242                 const float B[4] = {0,0,0,0};
1243                 const int ox1 = (x1 < 0), oy1 = (y1 < 0), ox2 = (x2 >= src->x), oy2 = (y2 >= src->y);
1244                 const float* c00 = (ox1 || oy1) ? B : &src->rect[(x1 + y1*src->x)*src->type + chan];
1245                 const float* c10 = (ox2 || oy1) ? B : &src->rect[(x2 + y1*src->x)*src->type + chan];
1246                 const float* c01 = (ox1 || oy2) ? B : &src->rect[(x1 + y2*src->x)*src->type + chan];
1247                 const float* c11 = (ox2 || oy2) ? B : &src->rect[(x2 + y2*src->x)*src->type + chan];
1248                 const float uf = u - ufl, vf = v - vfl;
1249                 const float w00=(1.f-uf)*(1.f-vf), w10=uf*(1.f-vf), w01=(1.f-uf)*vf, w11=uf*vf;
1250                 out[0] = w00*c00[0] + w10*c10[0] + w01*c01[0] + w11*c11[0];
1251         }
1252         else *out = 0.f;
1253 }
1254
1255
1256 CompBuf* qd_downScaledCopy(CompBuf* src, int scale)
1257 {
1258         CompBuf* fbuf;
1259         if (scale <= 1)
1260                 fbuf = dupalloc_compbuf(src);
1261         else {
1262                 int nw = src->x/scale, nh = src->y/scale;
1263                 if ((2*(src->x % scale)) > scale) nw++;
1264                 if ((2*(src->y % scale)) > scale) nh++;
1265                 fbuf = alloc_compbuf(nw, nh, src->type, 1);
1266                 {
1267                         int x, y, xx, yy, sx, sy, mx, my;
1268                         float colsum[4] = {0.0f, 0.0f, 0.0f, 0.0f};
1269                         float fscale = 1.f/(float)(scale*scale);
1270                         for (y=0; y<nh; y++) {
1271                                 fRGB* fcolp = (fRGB*)&fbuf->rect[y*fbuf->x*fbuf->type];
1272                                 yy = y*scale;
1273                                 my = yy + scale;
1274                                 if (my > src->y) my = src->y;
1275                                 for (x=0; x<nw; x++) {
1276                                         xx = x*scale;
1277                                         mx = xx + scale;
1278                                         if (mx > src->x) mx = src->x;
1279                                         colsum[0] = colsum[1] = colsum[2] = 0.f;
1280                                         for (sy=yy; sy<my; sy++) {
1281                                                 fRGB* scolp = (fRGB*)&src->rect[sy*src->x*src->type];
1282                                                 for (sx=xx; sx<mx; sx++)
1283                                                         fRGB_add(colsum, scolp[sx]);
1284                                         }
1285                                         fRGB_mult(colsum, fscale);
1286                                         fRGB_copy(fcolp[x], colsum);
1287                                 }
1288                         }
1289                 }
1290         }
1291         return fbuf;
1292 }
1293
1294 // fast g.blur, per channel
1295 // xy var. bits 1 & 2 ca be used to blur in x or y direction separately
1296 void IIR_gauss(CompBuf* src, float sigma, int chan, int xy)
1297 {
1298         double q, q2, sc, cf[4], tsM[9], tsu[3], tsv[3];
1299         double *X, *Y, *W;
1300         int i, x, y, sz;
1301
1302         // <0.5 not valid, though can have a possibly useful sort of sharpening effect
1303         if (sigma < 0.5f) return;
1304         
1305         if ((xy < 1) || (xy > 3)) xy = 3;
1306         
1307         // XXX The YVV macro defined below explicitly expects sources of at least 3x3 pixels,
1308         //     so just skiping blur along faulty direction if src's def is below that limit!
1309         if (src->x < 3) xy &= ~(int) 1;
1310         if (src->y < 3) xy &= ~(int) 2;
1311         if (xy < 1) return;
1312
1313         // see "Recursive Gabor Filtering" by Young/VanVliet
1314         // all factors here in double.prec. Required, because for single.prec it seems to blow up if sigma > ~200
1315         if (sigma >= 3.556f)
1316                 q = 0.9804f*(sigma - 3.556f) + 2.5091f;
1317         else // sigma >= 0.5
1318                 q = (0.0561f*sigma + 0.5784f)*sigma - 0.2568f;
1319         q2 = q*q;
1320         sc = (1.1668 + q)*(3.203729649  + (2.21566 + q)*q);
1321         // no gabor filtering here, so no complex multiplies, just the regular coefs.
1322         // all negated here, so as not to have to recalc Triggs/Sdika matrix
1323         cf[1] = q*(5.788961737 + (6.76492 + 3.0*q)*q)/ sc;
1324         cf[2] = -q2*(3.38246 + 3.0*q)/sc;
1325         // 0 & 3 unchanged
1326         cf[3] = q2*q/sc;
1327         cf[0] = 1.0 - cf[1] - cf[2] - cf[3];
1328
1329         // Triggs/Sdika border corrections,
1330         // it seems to work, not entirely sure if it is actually totally correct,
1331         // Besides J.M.Geusebroek's anigauss.c (see http://www.science.uva.nl/~mark),
1332         // found one other implementation by Cristoph Lampert,
1333         // but neither seem to be quite the same, result seems to be ok so far anyway.
1334         // Extra scale factor here to not have to do it in filter,
1335         // though maybe this had something to with the precision errors
1336         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]));
1337         tsM[0] = sc*(-cf[3]*cf[1] + 1.0 - cf[3]*cf[3] - cf[2]);
1338         tsM[1] = sc*((cf[3] + cf[1])*(cf[2] + cf[3]*cf[1]));
1339         tsM[2] = sc*(cf[3]*(cf[1] + cf[3]*cf[2]));
1340         tsM[3] = sc*(cf[1] + cf[3]*cf[2]);
1341         tsM[4] = sc*(-(cf[2] - 1.0)*(cf[2] + cf[3]*cf[1]));
1342         tsM[5] = sc*(-(cf[3]*cf[1] + cf[3]*cf[3] + cf[2] - 1.0)*cf[3]);
1343         tsM[6] = sc*(cf[3]*cf[1] + cf[2] + cf[1]*cf[1] - cf[2]*cf[2]);
1344         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]);
1345         tsM[8] = sc*(cf[3]*(cf[1] + cf[3]*cf[2]));
1346
1347 #define YVV(L)                                                                \
1348 {                                                                             \
1349         W[0] = cf[0]*X[0] + cf[1]*X[0] + cf[2]*X[0] + cf[3]*X[0];                 \
1350         W[1] = cf[0]*X[1] + cf[1]*W[0] + cf[2]*X[0] + cf[3]*X[0];                 \
1351         W[2] = cf[0]*X[2] + cf[1]*W[1] + cf[2]*W[0] + cf[3]*X[0];                 \
1352         for (i=3; i<L; i++)                                                       \
1353                 W[i] = cf[0]*X[i] + cf[1]*W[i-1] + cf[2]*W[i-2] + cf[3]*W[i-3];       \
1354         tsu[0] = W[L-1] - X[L-1];                                                 \
1355         tsu[1] = W[L-2] - X[L-1];                                                 \
1356         tsu[2] = W[L-3] - X[L-1];                                                 \
1357         tsv[0] = tsM[0]*tsu[0] + tsM[1]*tsu[1] + tsM[2]*tsu[2] + X[L-1];          \
1358         tsv[1] = tsM[3]*tsu[0] + tsM[4]*tsu[1] + tsM[5]*tsu[2] + X[L-1];          \
1359         tsv[2] = tsM[6]*tsu[0] + tsM[7]*tsu[1] + tsM[8]*tsu[2] + X[L-1];          \
1360         Y[L-1] = cf[0]*W[L-1] + cf[1]*tsv[0] + cf[2]*tsv[1] + cf[3]*tsv[2];       \
1361         Y[L-2] = cf[0]*W[L-2] + cf[1]*Y[L-1] + cf[2]*tsv[0] + cf[3]*tsv[1];       \
1362         Y[L-3] = cf[0]*W[L-3] + cf[1]*Y[L-2] + cf[2]*Y[L-1] + cf[3]*tsv[0];       \
1363         for (i=L-4; i>=0; i--)                                                    \
1364                 Y[i] = cf[0]*W[i] + cf[1]*Y[i+1] + cf[2]*Y[i+2] + cf[3]*Y[i+3];       \
1365 }
1366
1367         // intermediate buffers
1368         sz = MAX2(src->x, src->y);
1369         X = MEM_callocN(sz*sizeof(double), "IIR_gauss X buf");
1370         Y = MEM_callocN(sz*sizeof(double), "IIR_gauss Y buf");
1371         W = MEM_callocN(sz*sizeof(double), "IIR_gauss W buf");
1372         if (xy & 1) {   // H
1373                 for (y=0; y<src->y; ++y) {
1374                         const int yx = y*src->x;
1375                         for (x=0; x<src->x; ++x)
1376                                 X[x] = src->rect[(x + yx)*src->type + chan];
1377                         YVV(src->x);
1378                         for (x=0; x<src->x; ++x)
1379                                 src->rect[(x + yx)*src->type + chan] = Y[x];
1380                 }
1381         }
1382         if (xy & 2) {   // V
1383                 for (x=0; x<src->x; ++x) {
1384                         for (y=0; y<src->y; ++y)
1385                                 X[y] = src->rect[(x + y*src->x)*src->type + chan];
1386                         YVV(src->y);
1387                         for (y=0; y<src->y; ++y)
1388                                 src->rect[(x + y*src->x)*src->type + chan] = Y[y];
1389                 }
1390         }
1391
1392         MEM_freeN(X);
1393         MEM_freeN(W);
1394         MEM_freeN(Y);
1395 #undef YVV
1396 }
1397