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