style cleanup: follow style guide for formatting of if/for/while loops, and else...
[blender.git] / source / blender / nodes / composite / node_composite_util.c
index b8282f2c452c29737b2bf22059e701c6da4fd17f..8e98c05515773c6d5d31c95910df0670e3684bc9 100644 (file)
@@ -42,21 +42,21 @@ CompBuf *alloc_compbuf(int sizex, int sizey, int type, int alloc)
        cbuf->yrad= sizey/2;
        
        cbuf->type= type;
-       if(alloc) {
-               if(cbuf->type==CB_RGBA)
+       if (alloc) {
+               if (cbuf->type==CB_RGBA)
                        cbuf->rect= MEM_mapallocN(4*sizeof(float)*sizex*sizey, "compbuf RGBA rect");
-               else if(cbuf->type==CB_VEC3)
+               else if (cbuf->type==CB_VEC3)
                        cbuf->rect= MEM_mapallocN(3*sizeof(float)*sizex*sizey, "compbuf Vector3 rect");
-               else if(cbuf->type==CB_VEC2)
+               else if (cbuf->type==CB_VEC2)
                        cbuf->rect= MEM_mapallocN(2*sizeof(float)*sizex*sizey, "compbuf Vector2 rect");
                else
                        cbuf->rect= MEM_mapallocN(sizeof(float)*sizex*sizey, "compbuf Fac rect");
                cbuf->malloc= 1;
        }
-       cbuf->disprect.xmin= 0;
-       cbuf->disprect.ymin= 0;
-       cbuf->disprect.xmax= sizex;
-       cbuf->disprect.ymax= sizey;
+       cbuf->disprect.xmin = 0;
+       cbuf->disprect.ymin = 0;
+       cbuf->disprect.xmax = sizex;
+       cbuf->disprect.ymax = sizey;
        
        return cbuf;
 }
@@ -64,7 +64,7 @@ CompBuf *alloc_compbuf(int sizex, int sizey, int type, int alloc)
 CompBuf *dupalloc_compbuf(CompBuf *cbuf)
 {
        CompBuf *dupbuf= alloc_compbuf(cbuf->x, cbuf->y, cbuf->type, 1);
-       if(dupbuf) {
+       if (dupbuf) {
                memcpy(dupbuf->rect, cbuf->rect, cbuf->type*sizeof(float)*cbuf->x*cbuf->y);
        
                dupbuf->xof= cbuf->xof;
@@ -79,15 +79,15 @@ CompBuf *pass_on_compbuf(CompBuf *cbuf)
        CompBuf *dupbuf= (cbuf)? alloc_compbuf(cbuf->x, cbuf->y, cbuf->type, 0): NULL;
        CompBuf *lastbuf;
        
-       if(dupbuf) {
+       if (dupbuf) {
                dupbuf->rect= cbuf->rect;
                dupbuf->xof= cbuf->xof;
                dupbuf->yof= cbuf->yof;
                dupbuf->malloc= 0;
                
                /* get last buffer in list, and append dupbuf */
-               for(lastbuf= cbuf; lastbuf; lastbuf= lastbuf->next)
-                       if(lastbuf->next==NULL)
+               for (lastbuf= cbuf; lastbuf; lastbuf= lastbuf->next)
+                       if (lastbuf->next==NULL)
                                break;
                lastbuf->next= dupbuf;
                dupbuf->prev= lastbuf;
@@ -99,13 +99,13 @@ CompBuf *pass_on_compbuf(CompBuf *cbuf)
 void free_compbuf(CompBuf *cbuf)
 {
        /* check referencing, then remove from list and set malloc tag */
-       if(cbuf->prev || cbuf->next) {
-               if(cbuf->prev)
+       if (cbuf->prev || cbuf->next) {
+               if (cbuf->prev)
                        cbuf->prev->next= cbuf->next;
-               if(cbuf->next)
+               if (cbuf->next)
                        cbuf->next->prev= cbuf->prev;
-               if(cbuf->malloc) {
-                       if(cbuf->prev)
+               if (cbuf->malloc) {
+                       if (cbuf->prev)
                                cbuf->prev->malloc= 1;
                        else
                                cbuf->next->malloc= 1;
@@ -113,7 +113,7 @@ void free_compbuf(CompBuf *cbuf)
                }
        }
        
-       if(cbuf->malloc && cbuf->rect)
+       if (cbuf->malloc && cbuf->rect)
                MEM_freeN(cbuf->rect);
 
        MEM_freeN(cbuf);
@@ -130,47 +130,6 @@ void compbuf_set_node(CompBuf *cbuf, bNode *node)
        if (cbuf) cbuf->node = node;
 }
 
-/* used for disabling node  (similar code in node_draw.c for disable line and node_edit for untangling nodes) */
-void node_compo_pass_on(bNode *node, bNodeStack **nsin, bNodeStack **nsout)
-{
-       CompBuf *valbuf= NULL, *colbuf= NULL, *vecbuf= NULL;
-       bNodeSocket *sock;
-       int a;
-       
-       /* connect the first value buffer in with first value out */
-       /* connect the first RGBA buffer in with first RGBA out */
-       
-       /* test the inputs */
-       for(a=0, sock= node->inputs.first; sock; sock= sock->next, a++) {
-               if(nsin[a]->data) {
-                       CompBuf *cbuf= nsin[a]->data;
-                       if(cbuf->type==1 && valbuf==NULL) valbuf= cbuf;
-                       if(cbuf->type==3 && vecbuf==NULL) vecbuf= cbuf;
-                       if(cbuf->type==4 && colbuf==NULL) colbuf= cbuf;
-               }
-       }
-       
-       /* outputs */
-       if(valbuf || colbuf || vecbuf) {
-               for(a=0, sock= node->outputs.first; sock; sock= sock->next, a++) {
-                       if(nsout[a]->hasoutput) {
-                               if(sock->type==SOCK_FLOAT && valbuf) {
-                                       nsout[a]->data= pass_on_compbuf(valbuf);
-                                       valbuf= NULL;
-                               }
-                               if(sock->type==SOCK_VECTOR && vecbuf) {
-                                       nsout[a]->data= pass_on_compbuf(vecbuf);
-                                       vecbuf= NULL;
-                               }
-                               if(sock->type==SOCK_RGBA && colbuf) {
-                                       nsout[a]->data= pass_on_compbuf(colbuf);
-                                       colbuf= NULL;
-                               }
-                       }
-               }
-       }
-}
-
 
 CompBuf *get_cropped_compbuf(rcti *drect, float *rectf, int rectx, int recty, int type)
 {
@@ -179,16 +138,16 @@ CompBuf *get_cropped_compbuf(rcti *drect, float *rectf, int rectx, int recty, in
        float *outfp;
        int dx, y;
        
-       if(disprect.xmax>rectx) disprect.xmax= rectx;
-       if(disprect.ymax>recty) disprect.ymax= recty;
-       if(disprect.xmin>= disprect.xmax) return NULL;
-       if(disprect.ymin>= disprect.ymax) return NULL;
+       if (disprect.xmax>rectx) disprect.xmax = rectx;
+       if (disprect.ymax>recty) disprect.ymax = recty;
+       if (disprect.xmin>= disprect.xmax) return NULL;
+       if (disprect.ymin>= disprect.ymax) return NULL;
        
        cbuf= alloc_compbuf(disprect.xmax-disprect.xmin, disprect.ymax-disprect.ymin, type, 1);
        outfp= cbuf->rect;
        rectf += type*(disprect.ymin*rectx + disprect.xmin);
        dx= type*cbuf->x;
-       for(y=cbuf->y; y>0; y--, outfp+=dx, rectf+=type*rectx)
+       for (y=cbuf->y; y>0; y--, outfp+=dx, rectf+=type*rectx)
                memcpy(outfp, rectf, sizeof(float)*dx);
        
        return cbuf;
@@ -201,7 +160,7 @@ CompBuf *scalefast_compbuf(CompBuf *inbuf, int newx, int newy)
        int x, y, c, pixsize= inbuf->type;
        int ofsx, ofsy, stepx, stepy;
        
-       if(inbuf->x==newx && inbuf->y==newy)
+       if (inbuf->x==newx && inbuf->y==newy)
                return dupalloc_compbuf(inbuf);
        
        outbuf= alloc_compbuf(newx, newy, inbuf->type, 1);
@@ -211,7 +170,7 @@ CompBuf *scalefast_compbuf(CompBuf *inbuf, int newx, int newy)
        stepy = (65536.0 * (inbuf->y - 1.0) / (newy - 1.0)) + 0.5;
        ofsy = 32768;
        
-       for (y = newy; y > 0 ; y--){
+       for (y = newy; y > 0 ; y--) {
                rectf = inbuf->rect;
                rectf += pixsize * (ofsy >> 16) * inbuf->x;
 
@@ -221,7 +180,7 @@ CompBuf *scalefast_compbuf(CompBuf *inbuf, int newx, int newy)
                for (x = newx ; x>0 ; x--) {
                        
                        rf= rectf + pixsize*(ofsx >> 16);
-                       for(c=0; c<pixsize; c++)
+                       for (c=0; c<pixsize; c++)
                                newrectf[c] = rf[c];
                        
                        newrectf+= pixsize;
@@ -235,65 +194,65 @@ CompBuf *scalefast_compbuf(CompBuf *inbuf, int newx, int newy)
 
 void typecheck_compbuf_color(float *out, float *in, int outtype, int intype)
 {
-       if(intype == outtype) {
+       if (intype == outtype) {
                memcpy(out, in, sizeof(float)*outtype);
        }
-       else if(outtype==CB_VAL) {
-               if(intype==CB_VEC2) {
+       else if (outtype==CB_VAL) {
+               if (intype==CB_VEC2) {
                        *out= 0.5f*(in[0]+in[1]);
                }
-               else if(intype==CB_VEC3) {
+               else if (intype==CB_VEC3) {
                        *out= 0.333333f*(in[0]+in[1]+in[2]);
                }
-               else if(intype==CB_RGBA) {
+               else if (intype==CB_RGBA) {
                        *out= in[0]*0.35f + in[1]*0.45f + in[2]*0.2f;
                }
        }
-       else if(outtype==CB_VEC2) {
-               if(intype==CB_VAL) {
+       else if (outtype==CB_VEC2) {
+               if (intype==CB_VAL) {
                        out[0]= in[0];
                        out[1]= in[0];
                }
-               else if(intype==CB_VEC3) {
+               else if (intype==CB_VEC3) {
                        out[0]= in[0];
                        out[1]= in[1];
                }
-               else if(intype==CB_RGBA) {
+               else if (intype==CB_RGBA) {
                        out[0]= in[0];
                        out[1]= in[1];
                }
        }
-       else if(outtype==CB_VEC3) {
-               if(intype==CB_VAL) {
+       else if (outtype==CB_VEC3) {
+               if (intype==CB_VAL) {
                        out[0]= in[0];
                        out[1]= in[0];
                        out[2]= in[0];
                }
-               else if(intype==CB_VEC2) {
+               else if (intype==CB_VEC2) {
                        out[0]= in[0];
                        out[1]= in[1];
                        out[2]= 0.0f;
                }
-               else if(intype==CB_RGBA) {
+               else if (intype==CB_RGBA) {
                        out[0]= in[0];
                        out[1]= in[1];
                        out[2]= in[2];
                }
        }
-       else if(outtype==CB_RGBA) {
-               if(intype==CB_VAL) {
+       else if (outtype==CB_RGBA) {
+               if (intype==CB_VAL) {
                        out[0]= in[0];
                        out[1]= in[0];
                        out[2]= in[0];
                        out[3]= 1.0f;
                }
-               else if(intype==CB_VEC2) {
+               else if (intype==CB_VEC2) {
                        out[0]= in[0];
                        out[1]= in[1];
                        out[2]= 0.0f;
                        out[3]= 1.0f;
                }
-               else if(intype==CB_VEC3) {
+               else if (intype==CB_VEC3) {
                        out[0]= in[0];
                        out[1]= in[1];
                        out[2]= in[2];
@@ -304,7 +263,7 @@ void typecheck_compbuf_color(float *out, float *in, int outtype, int intype)
 
 CompBuf *typecheck_compbuf(CompBuf *inbuf, int type)
 {
-       if(inbuf && inbuf->type!=type) {
+       if (inbuf && inbuf->type!=type) {
                CompBuf *outbuf;
                float *inrf, *outrf;
                int x;
@@ -315,10 +274,10 @@ CompBuf *typecheck_compbuf(CompBuf *inbuf, int type)
                outbuf->xof= inbuf->xof;
                outbuf->yof= inbuf->yof;
 
-               if(inbuf->rect_procedural) {
+               if (inbuf->rect_procedural) {
                        outbuf->rect_procedural= inbuf->rect_procedural;
-                       VECCOPY(outbuf->procedural_size, inbuf->procedural_size);
-                       VECCOPY(outbuf->procedural_offset, inbuf->procedural_offset);
+                       copy_v3_v3(outbuf->procedural_size, inbuf->procedural_size);
+                       copy_v3_v3(outbuf->procedural_offset, inbuf->procedural_offset);
                        outbuf->procedural_type= inbuf->procedural_type;
                        outbuf->node= inbuf->node;
                        return outbuf;
@@ -328,82 +287,82 @@ CompBuf *typecheck_compbuf(CompBuf *inbuf, int type)
                outrf= outbuf->rect;
                x= inbuf->x*inbuf->y;
                
-               if(type==CB_VAL) {
-                       if(inbuf->type==CB_VEC2) {
-                               for(; x>0; x--, outrf+= 1, inrf+= 2)
+               if (type==CB_VAL) {
+                       if (inbuf->type==CB_VEC2) {
+                               for (; x>0; x--, outrf+= 1, inrf+= 2)
                                        *outrf= 0.5f*(inrf[0]+inrf[1]);
                        }
-                       else if(inbuf->type==CB_VEC3) {
-                               for(; x>0; x--, outrf+= 1, inrf+= 3)
+                       else if (inbuf->type==CB_VEC3) {
+                               for (; x>0; x--, outrf+= 1, inrf+= 3)
                                        *outrf= 0.333333f*(inrf[0]+inrf[1]+inrf[2]);
                        }
-                       else if(inbuf->type==CB_RGBA) {
-                               for(; x>0; x--, outrf+= 1, inrf+= 4)
+                       else if (inbuf->type==CB_RGBA) {
+                               for (; x>0; x--, outrf+= 1, inrf+= 4)
                                        *outrf= inrf[0]*0.35f + inrf[1]*0.45f + inrf[2]*0.2f;
                        }
                }
-               else if(type==CB_VEC2) {
-                       if(inbuf->type==CB_VAL) {
-                               for(; x>0; x--, outrf+= 2, inrf+= 1) {
+               else if (type==CB_VEC2) {
+                       if (inbuf->type==CB_VAL) {
+                               for (; x>0; x--, outrf+= 2, inrf+= 1) {
                                        outrf[0]= inrf[0];
                                        outrf[1]= inrf[0];
                                }
                        }
-                       else if(inbuf->type==CB_VEC3) {
-                               for(; x>0; x--, outrf+= 2, inrf+= 3) {
+                       else if (inbuf->type==CB_VEC3) {
+                               for (; x>0; x--, outrf+= 2, inrf+= 3) {
                                        outrf[0]= inrf[0];
                                        outrf[1]= inrf[1];
                                }
                        }
-                       else if(inbuf->type==CB_RGBA) {
-                               for(; x>0; x--, outrf+= 2, inrf+= 4) {
+                       else if (inbuf->type==CB_RGBA) {
+                               for (; x>0; x--, outrf+= 2, inrf+= 4) {
                                        outrf[0]= inrf[0];
                                        outrf[1]= inrf[1];
                                }
                        }
                }
-               else if(type==CB_VEC3) {
-                       if(inbuf->type==CB_VAL) {
-                               for(; x>0; x--, outrf+= 3, inrf+= 1) {
+               else if (type==CB_VEC3) {
+                       if (inbuf->type==CB_VAL) {
+                               for (; x>0; x--, outrf+= 3, inrf+= 1) {
                                        outrf[0]= inrf[0];
                                        outrf[1]= inrf[0];
                                        outrf[2]= inrf[0];
                                }
                        }
-                       else if(inbuf->type==CB_VEC2) {
-                               for(; x>0; x--, outrf+= 3, inrf+= 2) {
+                       else if (inbuf->type==CB_VEC2) {
+                               for (; x>0; x--, outrf+= 3, inrf+= 2) {
                                        outrf[0]= inrf[0];
                                        outrf[1]= inrf[1];
                                        outrf[2]= 0.0f;
                                }
                        }
-                       else if(inbuf->type==CB_RGBA) {
-                               for(; x>0; x--, outrf+= 3, inrf+= 4) {
+                       else if (inbuf->type==CB_RGBA) {
+                               for (; x>0; x--, outrf+= 3, inrf+= 4) {
                                        outrf[0]= inrf[0];
                                        outrf[1]= inrf[1];
                                        outrf[2]= inrf[2];
                                }
                        }
                }
-               else if(type==CB_RGBA) {
-                       if(inbuf->type==CB_VAL) {
-                               for(; x>0; x--, outrf+= 4, inrf+= 1) {
+               else if (type==CB_RGBA) {
+                       if (inbuf->type==CB_VAL) {
+                               for (; x>0; x--, outrf+= 4, inrf+= 1) {
                                        outrf[0]= inrf[0];
                                        outrf[1]= inrf[0];
                                        outrf[2]= inrf[0];
                                        outrf[3]= 1.0f;
                                }
                        }
-                       else if(inbuf->type==CB_VEC2) {
-                               for(; x>0; x--, outrf+= 4, inrf+= 2) {
+                       else if (inbuf->type==CB_VEC2) {
+                               for (; x>0; x--, outrf+= 4, inrf+= 2) {
                                        outrf[0]= inrf[0];
                                        outrf[1]= inrf[1];
                                        outrf[2]= 0.0f;
                                        outrf[3]= 1.0f;
                                }
                        }
-                       else if(inbuf->type==CB_VEC3) {
-                               for(; x>0; x--, outrf+= 4, inrf+= 3) {
+                       else if (inbuf->type==CB_VEC3) {
+                               for (; x>0; x--, outrf+= 4, inrf+= 3) {
                                        outrf[0]= inrf[0];
                                        outrf[1]= inrf[1];
                                        outrf[2]= inrf[2];
@@ -417,10 +376,10 @@ CompBuf *typecheck_compbuf(CompBuf *inbuf, int type)
        return inbuf;
 }
 
-static float *compbuf_get_pixel(CompBuf *cbuf, float *defcol, float *use, int x, int y, int xrad, int yrad)
+float *compbuf_get_pixel(CompBuf *cbuf, float *defcol, float *use, int x, int y, int xrad, int yrad)
 {
-       if(cbuf) {
-               if(cbuf->rect_procedural) {
+       if (cbuf) {
+               if (cbuf->rect_procedural) {
                        cbuf->rect_procedural(cbuf, use, (float)x/(float)xrad, (float)y/(float)yrad);
                        return use;
                }
@@ -431,8 +390,8 @@ static float *compbuf_get_pixel(CompBuf *cbuf, float *defcol, float *use, int x,
                        x-= cbuf->xof;
                        y-= cbuf->yof;
                        
-                       if(y<-cbuf->yrad || y>= -cbuf->yrad+cbuf->y) return col;
-                       if(x<-cbuf->xrad || x>= -cbuf->xrad+cbuf->x) return col;
+                       if (y<-cbuf->yrad || y>= -cbuf->yrad+cbuf->y) return col;
+                       if (x<-cbuf->xrad || x>= -cbuf->xrad+cbuf->x) return col;
                        
                        return cbuf->rect + cbuf->type*( (cbuf->yrad+y)*cbuf->x + (cbuf->xrad+x) );
                }
@@ -442,6 +401,18 @@ static float *compbuf_get_pixel(CompBuf *cbuf, float *defcol, float *use, int x,
 
 /* **************************************************** */
 
+static CompBuf *composit_check_compbuf(CompBuf *cbuf, int type, CompBuf *outbuf)
+{
+       /* check type */
+       CompBuf *dbuf= typecheck_compbuf(cbuf, type);
+
+       /* if same as output and translated, duplicate so pixels don't interfere */
+       if (dbuf == outbuf && !dbuf->rect_procedural && (dbuf->xof || dbuf->yof))
+               dbuf= dupalloc_compbuf(dbuf);
+       
+       return dbuf;
+}
+
 /* Pixel-to-Pixel operation, 1 Image in, 1 out */
 void composit1_pixel_processor(bNode *node, CompBuf *out, CompBuf *src_buf, float *src_col,
                                                                          void (*func)(bNode *, float *, float *), 
@@ -452,19 +423,19 @@ void composit1_pixel_processor(bNode *node, CompBuf *out, CompBuf *src_buf, floa
        float color[4]; /* local color if compbuf is procedural */
        int xrad, yrad, x, y;
        
-       src_use= typecheck_compbuf(src_buf, src_type);
+       src_use= composit_check_compbuf(src_buf, src_type, out);
        
        xrad= out->xrad;
        yrad= out->yrad;
        
-       for(y= -yrad; y<-yrad+out->y; y++) {
-               for(x= -xrad; x<-xrad+out->x; x++, outfp+=out->type) {
+       for (y= -yrad; y<-yrad+out->y; y++) {
+               for (x= -xrad; x<-xrad+out->x; x++, outfp+=out->type) {
                        srcfp= compbuf_get_pixel(src_use, src_col, color, x, y, xrad, yrad);
                        func(node, outfp, srcfp);
                }
        }
        
-       if(src_use!=src_buf)
+       if (src_use!=src_buf)
                free_compbuf(src_use);
 }
 
@@ -478,23 +449,23 @@ void composit2_pixel_processor(bNode *node, CompBuf *out, CompBuf *src_buf, floa
        float color[4]; /* local color if compbuf is procedural */
        int xrad, yrad, x, y;
        
-       src_use= typecheck_compbuf(src_buf, src_type);
-       fac_use= typecheck_compbuf(fac_buf, fac_type);
+       src_use= composit_check_compbuf(src_buf, src_type, out);
+       fac_use= composit_check_compbuf(fac_buf, fac_type, out);
 
        xrad= out->xrad;
        yrad= out->yrad;
        
-       for(y= -yrad; y<-yrad+out->y; y++) {
-               for(x= -xrad; x<-xrad+out->x; x++, outfp+=out->type) {
+       for (y= -yrad; y<-yrad+out->y; y++) {
+               for (x= -xrad; x<-xrad+out->x; x++, outfp+=out->type) {
                        srcfp= compbuf_get_pixel(src_use, src_col, color, x, y, xrad, yrad);
                        facfp= compbuf_get_pixel(fac_use, fac, color, x, y, xrad, yrad);
                        
                        func(node, outfp, srcfp, facfp);
                }
        }
-       if(src_use!=src_buf)
+       if (src_use!=src_buf)
                free_compbuf(src_use);
-       if(fac_use!=fac_buf)
+       if (fac_use!=fac_buf)
                free_compbuf(fac_use);
 }
 
@@ -508,15 +479,15 @@ void composit3_pixel_processor(bNode *node, CompBuf *out, CompBuf *src1_buf, flo
        float color[4]; /* local color if compbuf is procedural */
        int xrad, yrad, x, y;
        
-       src1_use= typecheck_compbuf(src1_buf, src1_type);
-       src2_use= typecheck_compbuf(src2_buf, src2_type);
-       fac_use= typecheck_compbuf(fac_buf, fac_type);
+       src1_use= composit_check_compbuf(src1_buf, src1_type, out);
+       src2_use= composit_check_compbuf(src2_buf, src2_type, out);
+       fac_use= composit_check_compbuf(fac_buf, fac_type, out);
        
        xrad= out->xrad;
        yrad= out->yrad;
        
-       for(y= -yrad; y<-yrad+out->y; y++) {
-               for(x= -xrad; x<-xrad+out->x; x++, outfp+=out->type) {
+       for (y= -yrad; y<-yrad+out->y; y++) {
+               for (x= -xrad; x<-xrad+out->x; x++, outfp+=out->type) {
                        src1fp= compbuf_get_pixel(src1_use, src1_col, color, x, y, xrad, yrad);
                        src2fp= compbuf_get_pixel(src2_use, src2_col, color, x, y, xrad, yrad);
                        facfp= compbuf_get_pixel(fac_use, fac, color, x, y, xrad, yrad);
@@ -525,11 +496,11 @@ void composit3_pixel_processor(bNode *node, CompBuf *out, CompBuf *src1_buf, flo
                }
        }
        
-       if(src1_use!=src1_buf)
+       if (src1_use!=src1_buf)
                free_compbuf(src1_use);
-       if(src2_use!=src2_buf)
+       if (src2_use!=src2_buf)
                free_compbuf(src2_use);
-       if(fac_use!=fac_buf)
+       if (fac_use!=fac_buf)
                free_compbuf(fac_use);
 }
 
@@ -544,16 +515,16 @@ void composit4_pixel_processor(bNode *node, CompBuf *out, CompBuf *src1_buf, flo
        float color[4]; /* local color if compbuf is procedural */
        int xrad, yrad, x, y;
        
-       src1_use= typecheck_compbuf(src1_buf, src1_type);
-       src2_use= typecheck_compbuf(src2_buf, src2_type);
-       fac1_use= typecheck_compbuf(fac1_buf, fac1_type);
-       fac2_use= typecheck_compbuf(fac2_buf, fac2_type);
+       src1_use= composit_check_compbuf(src1_buf, src1_type, out);
+       src2_use= composit_check_compbuf(src2_buf, src2_type, out);
+       fac1_use= composit_check_compbuf(fac1_buf, fac1_type, out);
+       fac2_use= composit_check_compbuf(fac2_buf, fac2_type, out);
        
        xrad= out->xrad;
        yrad= out->yrad;
        
-       for(y= -yrad; y<-yrad+out->y; y++) {
-               for(x= -xrad; x<-xrad+out->x; x++, outfp+=out->type) {
+       for (y= -yrad; y<-yrad+out->y; y++) {
+               for (x= -xrad; x<-xrad+out->x; x++, outfp+=out->type) {
                        src1fp= compbuf_get_pixel(src1_use, src1_col, color, x, y, xrad, yrad);
                        src2fp= compbuf_get_pixel(src2_use, src2_col, color, x, y, xrad, yrad);
                        fac1fp= compbuf_get_pixel(fac1_use, fac1, color, x, y, xrad, yrad);
@@ -563,13 +534,13 @@ void composit4_pixel_processor(bNode *node, CompBuf *out, CompBuf *src1_buf, flo
                }
        }
        
-       if(src1_use!=src1_buf)
+       if (src1_use!=src1_buf)
                free_compbuf(src1_use);
-       if(src2_use!=src2_buf)
+       if (src2_use!=src2_buf)
                free_compbuf(src2_use);
-       if(fac1_use!=fac1_buf)
+       if (fac1_use!=fac1_buf)
                free_compbuf(fac1_use);
-       if(fac2_use!=fac2_buf)
+       if (fac2_use!=fac2_buf)
                free_compbuf(fac2_use);
 }
 
@@ -591,7 +562,7 @@ CompBuf *valbuf_from_rgbabuf(CompBuf *cbuf, int channel)
 
        rectf= cbuf->rect + channel;
        
-       for(tot= cbuf->x*cbuf->y; tot>0; tot--, valf++, rectf+=4)
+       for (tot= cbuf->x*cbuf->y; tot>0; tot--, valf++, rectf+=4)
                *valf= *rectf;
        
        return valbuf;
@@ -609,8 +580,8 @@ static CompBuf *generate_procedural_preview(CompBuf *cbuf, int newx, int newy)
        xrad= outbuf->xrad;
        yrad= outbuf->yrad;
        
-       for(y= -yrad; y<-yrad+outbuf->y; y++)
-               for(x= -xrad; x<-xrad+outbuf->x; x++, outfp+=outbuf->type)
+       for (y= -yrad; y<-yrad+outbuf->y; y++)
+               for (x= -xrad; x<-xrad+outbuf->x; x++, outfp+=outbuf->type)
                        cbuf->rect_procedural(cbuf, outfp, (float)x/(float)xrad, (float)y/(float)yrad);
 
        return outbuf;
@@ -621,17 +592,19 @@ void generate_preview(void *data, bNode *node, CompBuf *stackbuf)
        RenderData *rd= data;
        bNodePreview *preview= node->preview;
        int xsize, ysize;
-       int color_manage= rd->color_mgt_flag & R_COLOR_MANAGEMENT;
+       int profile_from= (rd->color_mgt_flag & R_COLOR_MANAGEMENT)? IB_PROFILE_LINEAR_RGB: IB_PROFILE_SRGB;
+       int predivide= (rd->color_mgt_flag & R_COLOR_MANAGEMENT_PREDIVIDE);
+       int dither= 0;
        unsigned char *rect;
        
-       if(preview && stackbuf) {
+       if (preview && stackbuf) {
                CompBuf *cbuf, *stackbuf_use;
                
-               if(stackbuf->rect==NULL && stackbuf->rect_procedural==NULL) return;
+               if (stackbuf->rect==NULL && stackbuf->rect_procedural==NULL) return;
                
                stackbuf_use= typecheck_compbuf(stackbuf, CB_RGBA);
 
-               if(stackbuf->x > stackbuf->y) {
+               if (stackbuf->x > stackbuf->y) {
                        xsize= 140;
                        ysize= (140*stackbuf->y)/stackbuf->x;
                }
@@ -640,7 +613,7 @@ void generate_preview(void *data, bNode *node, CompBuf *stackbuf)
                        xsize= (140*stackbuf->x)/stackbuf->y;
                }
                
-               if(stackbuf_use->rect_procedural)
+               if (stackbuf_use->rect_procedural)
                        cbuf= generate_procedural_preview(stackbuf_use, xsize, ysize);
                else
                        cbuf= scalefast_compbuf(stackbuf_use, xsize, ysize);
@@ -648,18 +621,17 @@ void generate_preview(void *data, bNode *node, CompBuf *stackbuf)
                /* convert to byte for preview */
                rect= MEM_callocN(sizeof(unsigned char)*4*xsize*ysize, "bNodePreview.rect");
 
-               if(color_manage)
-                       floatbuf_to_srgb_byte(cbuf->rect, rect, 0, xsize, 0, ysize, xsize);
-               else
-                       floatbuf_to_byte(cbuf->rect, rect, 0, xsize, 0, ysize, xsize);
+               IMB_buffer_byte_from_float(rect, cbuf->rect,
+                       4, dither, IB_PROFILE_SRGB, profile_from, predivide, 
+                       xsize, ysize, xsize, xsize);
                
                free_compbuf(cbuf);
-               if(stackbuf_use!=stackbuf)
+               if (stackbuf_use!=stackbuf)
                        free_compbuf(stackbuf_use);
 
                BLI_lock_thread(LOCK_PREVIEW);
 
-               if(preview->rect)
+               if (preview->rect)
                        MEM_freeN(preview->rect);
                preview->xsize= xsize;
                preview->ysize= ysize;
@@ -707,12 +679,12 @@ void do_ycca_to_rgba(bNode *UNUSED(node), float *out, float *in)
 
 void do_copy_rgba(bNode *UNUSED(node), float *out, float *in)
 {
-       QUATCOPY(out, in);
+       copy_v4_v4(out, in);
 }
 
 void do_copy_rgb(bNode *UNUSED(node), float *out, float *in)
 {
-       VECCOPY(out, in);
+       copy_v3_v3(out, in);
        out[3]= 1.0f;
 }
 
@@ -723,7 +695,7 @@ void do_copy_value(bNode *UNUSED(node), float *out, float *in)
 
 void do_copy_a_rgba(bNode *UNUSED(node), float *out, float *in, float *fac)
 {
-       VECCOPY(out, in);
+       copy_v3_v3(out, in);
        out[3]= *fac;
 }
 
@@ -733,21 +705,21 @@ void gamma_correct_compbuf(CompBuf *img, int inversed)
        float *drect;
        int x;
 
-       if(img->type!=CB_RGBA) return;
+       if (img->type!=CB_RGBA) return;
 
        drect= img->rect;
-       if(inversed) {
-               for(x=img->x*img->y; x>0; x--, drect+=4) {
-                       if(drect[0]>0.0f) drect[0]= sqrt(drect[0]); else drect[0]= 0.0f;
-                       if(drect[1]>0.0f) drect[1]= sqrt(drect[1]); else drect[1]= 0.0f;
-                       if(drect[2]>0.0f) drect[2]= sqrt(drect[2]); else drect[2]= 0.0f;
+       if (inversed) {
+               for (x=img->x*img->y; x>0; x--, drect+=4) {
+                       if (drect[0]>0.0f) drect[0]= sqrt(drect[0]); else drect[0]= 0.0f;
+                       if (drect[1]>0.0f) drect[1]= sqrt(drect[1]); else drect[1]= 0.0f;
+                       if (drect[2]>0.0f) drect[2]= sqrt(drect[2]); else drect[2]= 0.0f;
                }
        }
        else {
-               for(x=img->x*img->y; x>0; x--, drect+=4) {
-                       if(drect[0]>0.0f) drect[0]*= drect[0]; else drect[0]= 0.0f;
-                       if(drect[1]>0.0f) drect[1]*= drect[1]; else drect[1]= 0.0f;
-                       if(drect[2]>0.0f) drect[2]*= drect[2]; else drect[2]= 0.0f;
+               for (x=img->x*img->y; x>0; x--, drect+=4) {
+                       if (drect[0]>0.0f) drect[0]*= drect[0]; else drect[0]= 0.0f;
+                       if (drect[1]>0.0f) drect[1]*= drect[1]; else drect[1]= 0.0f;
+                       if (drect[2]>0.0f) drect[2]*= drect[2]; else drect[2]= 0.0f;
                }
        }
 }
@@ -757,12 +729,12 @@ void premul_compbuf(CompBuf *img, int inversed)
        float *drect;
        int x;
 
-       if(img->type!=CB_RGBA) return;
+       if (img->type!=CB_RGBA) return;
 
        drect= img->rect;
-       if(inversed) {
-               for(x=img->x*img->y; x>0; x--, drect+=4) {
-                       if(fabs(drect[3]) < 1e-5f) {
+       if (inversed) {
+               for (x=img->x*img->y; x>0; x--, drect+=4) {
+                       if (fabsf(drect[3]) < 1e-5f) {
                                drect[0]= 0.0f;
                                drect[1]= 0.0f;
                                drect[2]= 0.0f;
@@ -775,7 +747,7 @@ void premul_compbuf(CompBuf *img, int inversed)
                }
        }
        else {
-               for(x=img->x*img->y; x>0; x--, drect+=4) {
+               for (x=img->x*img->y; x>0; x--, drect+=4) {
                        drect[0] *= drect[3];
                        drect[1] *= drect[3];
                        drect[2] *= drect[3];
@@ -1132,11 +1104,11 @@ void convolve(CompBuf* dst, CompBuf* in1, CompBuf* in2)
 // sets fcol to pixelcolor at (x, y)
 void qd_getPixel(CompBuf* src, int x, int y, float* col)
 {
-       if(src->rect_procedural) {
+       if (src->rect_procedural) {
                float bc[4];
                src->rect_procedural(src, bc, (float)x/(float)src->xrad, (float)y/(float)src->yrad);
 
-               switch(src->type){
+               switch(src->type) {
                        /* these fallthrough to get all the channels */
                        case CB_RGBA: col[3]=bc[3]; 
                        case CB_VEC3: col[2]=bc[2];
@@ -1146,7 +1118,7 @@ void qd_getPixel(CompBuf* src, int x, int y, float* col)
        }
        else if ((x >= 0) && (x < src->x) && (y >= 0) && (y < src->y)) {
                float* bc = &src->rect[(x + y*src->x)*src->type];
-               switch(src->type){
+               switch(src->type) {
                        /* these fallthrough to get all the channels */
                        case CB_RGBA: col[3]=bc[3]; 
                        case CB_VEC3: col[2]=bc[2];
@@ -1155,7 +1127,7 @@ void qd_getPixel(CompBuf* src, int x, int y, float* col)
                }
        }
        else {
-               switch(src->type){
+               switch(src->type) {
                        /* these fallthrough to get all the channels */
                        case CB_RGBA: col[3]=0.0; 
                        case CB_VEC3: col[2]=0.0; 
@@ -1170,7 +1142,7 @@ void qd_setPixel(CompBuf* src, int x, int y, float* col)
 {
        if ((x >= 0) && (x < src->x) && (y >= 0) && (y < src->y)) {
                float* bc = &src->rect[(x + y*src->x)*src->type];
-               switch(src->type){
+               switch(src->type) {
                        /* these fallthrough to get all the channels */
                        case CB_RGBA: bc[3]=col[3]; 
                        case CB_VEC3: bc[2]=col[2];
@@ -1314,7 +1286,7 @@ void IIR_gauss(CompBuf* src, float sigma, int chan, int xy)
        int i, x, y, sz;
 
        // <0.5 not valid, though can have a possibly useful sort of sharpening effect
-       if (sigma < 0.5) return;
+       if (sigma < 0.5f) return;
        
        if ((xy < 1) || (xy > 3)) xy = 3;
        
@@ -1326,10 +1298,10 @@ void IIR_gauss(CompBuf* src, float sigma, int chan, int xy)
 
        // see "Recursive Gabor Filtering" by Young/VanVliet
        // all factors here in double.prec. Required, because for single.prec it seems to blow up if sigma > ~200
-       if (sigma >= 3.556)
-               q = 0.9804*(sigma - 3.556) + 2.5091;
+       if (sigma >= 3.556f)
+               q = 0.9804f*(sigma - 3.556f) + 2.5091f;
        else // sigma >= 0.5
-               q = (0.0561*sigma + 0.5784)*sigma - 0.2568;
+               q = (0.0561f*sigma + 0.5784f)*sigma - 0.2568f;
        q2 = q*q;
        sc = (1.1668 + q)*(3.203729649  + (2.21566 + q)*q);
        // no gabor filtering here, so no complex multiplies, just the regular coefs.
@@ -1358,24 +1330,24 @@ void IIR_gauss(CompBuf* src, float sigma, int chan, int xy)
        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]);
        tsM[8] = sc*(cf[3]*(cf[1] + cf[3]*cf[2]));
 
-#define YVV(L)\
-{\
-       W[0] = cf[0]*X[0] + cf[1]*X[0] + cf[2]*X[0] + cf[3]*X[0];\
-       W[1] = cf[0]*X[1] + cf[1]*W[0] + cf[2]*X[0] + cf[3]*X[0];\
-       W[2] = cf[0]*X[2] + cf[1]*W[1] + cf[2]*W[0] + cf[3]*X[0];\
-       for (i=3; i<L; i++)\
-               W[i] = cf[0]*X[i] + cf[1]*W[i-1] + cf[2]*W[i-2] + cf[3]*W[i-3];\
-       tsu[0] = W[L-1] - X[L-1];\
-       tsu[1] = W[L-2] - X[L-1];\
-       tsu[2] = W[L-3] - X[L-1];\
-       tsv[0] = tsM[0]*tsu[0] + tsM[1]*tsu[1] + tsM[2]*tsu[2] + X[L-1];\
-       tsv[1] = tsM[3]*tsu[0] + tsM[4]*tsu[1] + tsM[5]*tsu[2] + X[L-1];\
-       tsv[2] = tsM[6]*tsu[0] + tsM[7]*tsu[1] + tsM[8]*tsu[2] + X[L-1];\
-       Y[L-1] = cf[0]*W[L-1] + cf[1]*tsv[0] + cf[2]*tsv[1] + cf[3]*tsv[2];\
-       Y[L-2] = cf[0]*W[L-2] + cf[1]*Y[L-1] + cf[2]*tsv[0] + cf[3]*tsv[1];\
-       Y[L-3] = cf[0]*W[L-3] + cf[1]*Y[L-2] + cf[2]*Y[L-1] + cf[3]*tsv[0];\
-       for (i=L-4; i>=0; i--)\
-               Y[i] = cf[0]*W[i] + cf[1]*Y[i+1] + cf[2]*Y[i+2] + cf[3]*Y[i+3];\
+#define YVV(L)                                                                \
+{                                                                             \
+       W[0] = cf[0]*X[0] + cf[1]*X[0] + cf[2]*X[0] + cf[3]*X[0];                 \
+       W[1] = cf[0]*X[1] + cf[1]*W[0] + cf[2]*X[0] + cf[3]*X[0];                 \
+       W[2] = cf[0]*X[2] + cf[1]*W[1] + cf[2]*W[0] + cf[3]*X[0];                 \
+       for (i=3; i<L; i++)                                                       \
+               W[i] = cf[0]*X[i] + cf[1]*W[i-1] + cf[2]*W[i-2] + cf[3]*W[i-3];       \
+       tsu[0] = W[L-1] - X[L-1];                                                 \
+       tsu[1] = W[L-2] - X[L-1];                                                 \
+       tsu[2] = W[L-3] - X[L-1];                                                 \
+       tsv[0] = tsM[0]*tsu[0] + tsM[1]*tsu[1] + tsM[2]*tsu[2] + X[L-1];          \
+       tsv[1] = tsM[3]*tsu[0] + tsM[4]*tsu[1] + tsM[5]*tsu[2] + X[L-1];          \
+       tsv[2] = tsM[6]*tsu[0] + tsM[7]*tsu[1] + tsM[8]*tsu[2] + X[L-1];          \
+       Y[L-1] = cf[0]*W[L-1] + cf[1]*tsv[0] + cf[2]*tsv[1] + cf[3]*tsv[2];       \
+       Y[L-2] = cf[0]*W[L-2] + cf[1]*Y[L-1] + cf[2]*tsv[0] + cf[3]*tsv[1];       \
+       Y[L-3] = cf[0]*W[L-3] + cf[1]*Y[L-2] + cf[2]*Y[L-1] + cf[3]*tsv[0];       \
+       for (i=L-4; i>=0; i--)                                                    \
+               Y[i] = cf[0]*W[i] + cf[1]*Y[i+1] + cf[2]*Y[i+2] + cf[3]*Y[i+3];       \
 }
 
        // intermediate buffers