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