style cleanup: follow style guide for formatting of if/for/while loops, and else...
[blender.git] / source / blender / render / intern / source / imagetexture.c
index d1f7059416d33e1c070f7bdea403ffffc01d380f..6d8ae20985c0c7d750df6f6cbcde87198b7b22ab 100644 (file)
@@ -85,12 +85,12 @@ static void ibuf_get_color(float *col, struct ImBuf *ibuf, int x, int y)
 {
        int ofs = y * ibuf->x + x;
        
-       if(ibuf->rect_float) {
-               if(ibuf->channels==4) {
+       if (ibuf->rect_float) {
+               if (ibuf->channels==4) {
                        float *fp= ibuf->rect_float + 4*ofs;
                        copy_v4_v4(col, fp);
                }
-               else if(ibuf->channels==3) {
+               else if (ibuf->channels==3) {
                        float *fp= ibuf->rect_float + 3*ofs;
                        copy_v3_v3(col, fp);
                        col[3]= 1.0f;
@@ -122,21 +122,23 @@ int imagewrap(Tex *tex, Image *ima, ImBuf *ibuf, const float texvec[3], TexResul
        retval= texres->nor?3:1;
        
        /* quick tests */
-       if(ibuf==NULL && ima==NULL)
+       if (ibuf==NULL && ima==NULL)
                return retval;
-       if(ima) {
+       if (ima) {
                
                /* hack for icon render */
-               if(ima->ibufs.first==NULL && (R.r.scemode & R_NO_IMAGE_LOAD))
+               if (ima->ibufs.first==NULL && (R.r.scemode & R_NO_IMAGE_LOAD))
                        return retval;
                
                ibuf= BKE_image_get_ibuf(ima, &tex->iuser);
+
+               ima->flag|= IMA_USED_FOR_RENDER;
        }
-       if(ibuf==NULL || (ibuf->rect==NULL && ibuf->rect_float==NULL))
+       if (ibuf==NULL || (ibuf->rect==NULL && ibuf->rect_float==NULL))
                return retval;
        
        /* setup mapping */
-       if(tex->imaflag & TEX_IMAROT) {
+       if (tex->imaflag & TEX_IMAROT) {
                fy= texvec[0];
                fx= texvec[1];
        }
@@ -145,7 +147,7 @@ int imagewrap(Tex *tex, Image *ima, ImBuf *ibuf, const float texvec[3], TexResul
                fy= texvec[1];
        }
        
-       if(tex->extend == TEX_CHECKER) {
+       if (tex->extend == TEX_CHECKER) {
                int xs, ys;
                
                xs= (int)floor(fx);
@@ -153,14 +155,14 @@ int imagewrap(Tex *tex, Image *ima, ImBuf *ibuf, const float texvec[3], TexResul
                fx-= xs;
                fy-= ys;
 
-               if( (tex->flag & TEX_CHECKER_ODD)==0) {
-                       if((xs+ys) & 1);else return retval;
+               if ( (tex->flag & TEX_CHECKER_ODD)==0) {
+                       if ((xs+ys) & 1);else return retval;
                }
-               if( (tex->flag & TEX_CHECKER_EVEN)==0) {
-                       if((xs+ys) & 1) return retval; 
+               if ( (tex->flag & TEX_CHECKER_EVEN)==0) {
+                       if ((xs+ys) & 1) return retval;
                }
                /* scale around center, (0.5, 0.5) */
-               if(tex->checkerdist<1.0f) {
+               if (tex->checkerdist<1.0f) {
                        fx= (fx-0.5f)/(1.0f-tex->checkerdist) +0.5f;
                        fy= (fy-0.5f)/(1.0f-tex->checkerdist) +0.5f;
                }
@@ -169,40 +171,47 @@ int imagewrap(Tex *tex, Image *ima, ImBuf *ibuf, const float texvec[3], TexResul
        x= xi= (int)floorf(fx*ibuf->x);
        y= yi= (int)floorf(fy*ibuf->y);
 
-       if(tex->extend == TEX_CLIPCUBE) {
-               if(x<0 || y<0 || x>=ibuf->x || y>=ibuf->y || texvec[2]<-1.0f || texvec[2]>1.0f) {
+       if (tex->extend == TEX_CLIPCUBE) {
+               if (x<0 || y<0 || x>=ibuf->x || y>=ibuf->y || texvec[2]<-1.0f || texvec[2]>1.0f) {
                        return retval;
                }
        }
-       else if( tex->extend==TEX_CLIP || tex->extend==TEX_CHECKER) {
-               if(x<0 || y<0 || x>=ibuf->x || y>=ibuf->y) {
+       else if ( tex->extend==TEX_CLIP || tex->extend==TEX_CHECKER) {
+               if (x<0 || y<0 || x>=ibuf->x || y>=ibuf->y) {
                        return retval;
                }
        }
        else {
-               if(tex->extend==TEX_EXTEND) {
-                       if(x>=ibuf->x) x = ibuf->x-1;
-                       else if(x<0) x= 0;
+               if (tex->extend==TEX_EXTEND) {
+                       if (x>=ibuf->x) x = ibuf->x-1;
+                       else if (x<0) x= 0;
                }
                else {
                        x= x % ibuf->x;
-                       if(x<0) x+= ibuf->x;
+                       if (x<0) x+= ibuf->x;
                }
-               if(tex->extend==TEX_EXTEND) {
-                       if(y>=ibuf->y) y = ibuf->y-1;
-                       else if(y<0) y= 0;
+               if (tex->extend==TEX_EXTEND) {
+                       if (y>=ibuf->y) y = ibuf->y-1;
+                       else if (y<0) y= 0;
                }
                else {
                        y= y % ibuf->y;
-                       if(y<0) y+= ibuf->y;
+                       if (y<0) y+= ibuf->y;
                }
        }
        
        /* warning, no return before setting back! */
-       if( (R.flag & R_SEC_FIELD) && (ibuf->flags & IB_fields) ) {
+       if ( (R.flag & R_SEC_FIELD) && (ibuf->flags & IB_fields) ) {
                ibuf->rect+= (ibuf->x*ibuf->y);
        }
 
+       /* keep this before interpolation [#29761] */
+       if (tex->imaflag & TEX_USEALPHA) {
+               if ((tex->imaflag & TEX_CALCALPHA) == 0) {
+                       texres->talpha = TRUE;
+               } 
+       }
+
        /* interpolate */
        if (tex->imaflag & TEX_INTERPOL) {
                float filterx, filtery;
@@ -221,17 +230,12 @@ int imagewrap(Tex *tex, Image *ima, ImBuf *ibuf, const float texvec[3], TexResul
                ibuf_get_color(&texres->tr, ibuf, x, y);
        }
        
-       if( (R.flag & R_SEC_FIELD) && (ibuf->flags & IB_fields) ) {
+       if ( (R.flag & R_SEC_FIELD) && (ibuf->flags & IB_fields) ) {
                ibuf->rect-= (ibuf->x*ibuf->y);
        }
 
-       if(tex->imaflag & TEX_USEALPHA) {
-               if(tex->imaflag & TEX_CALCALPHA);
-               else texres->talpha= 1;
-       }
-       
-       if(texres->nor) {
-               if(tex->imaflag & TEX_NORMALMAP) {
+       if (texres->nor) {
+               if (tex->imaflag & TEX_NORMALMAP) {
                        // qdn: normal from color
                        // The invert of the red channel is to make
                        // the normal map compliant with the outside world.
@@ -247,14 +251,14 @@ int imagewrap(Tex *tex, Image *ima, ImBuf *ibuf, const float texvec[3], TexResul
                        /* bump: take three samples */
                        val1= texres->tr+texres->tg+texres->tb;
 
-                       if(x<ibuf->x-1) {
+                       if (x<ibuf->x-1) {
                                float col[4];
                                ibuf_get_color(col, ibuf, x+1, y);
                                val2= (col[0]+col[1]+col[2]);
                        }
                        else val2= val1;
 
-                       if(y<ibuf->y-1) {
+                       if (y<ibuf->y-1) {
                                float col[4];
                                ibuf_get_color(col, ibuf, x, y+1);
                                val3= (col[0]+col[1]+col[2]);
@@ -267,16 +271,16 @@ int imagewrap(Tex *tex, Image *ima, ImBuf *ibuf, const float texvec[3], TexResul
                }
        }
 
-       if(texres->talpha) texres->tin= texres->ta;
-       else if(tex->imaflag & TEX_CALCALPHA) {
+       if (texres->talpha) texres->tin= texres->ta;
+       else if (tex->imaflag & TEX_CALCALPHA) {
                texres->ta= texres->tin= MAX3(texres->tr, texres->tg, texres->tb);
        }
        else texres->ta= texres->tin= 1.0;
        
-       if(tex->flag & TEX_NEGALPHA) texres->ta= 1.0f-texres->ta;
+       if (tex->flag & TEX_NEGALPHA) texres->ta= 1.0f-texres->ta;
 
        /* de-premul, this is being premulled in shade_input_do_shade() */
-       if(texres->ta!=1.0f && texres->ta>1e-4f) {
+       if (texres->ta!=1.0f && texres->ta>1e-4f) {
                fx= 1.0f/texres->ta;
                texres->tr*= fx;
                texres->tg*= fx;
@@ -295,45 +299,45 @@ static void clipx_rctf_swap(rctf *stack, short *count, float x1, float x2)
 
        a= *count;
        rf= stack;
-       for(;a>0;a--) {
-               if(rf->xmin<x1) {
-                       if(rf->xmax<x1) {
+       for (;a>0;a--) {
+               if (rf->xmin<x1) {
+                       if (rf->xmax<x1) {
                                rf->xmin+= (x2-x1);
                                rf->xmax+= (x2-x1);
                        }
                        else {
-                               if(rf->xmax>x2) rf->xmax= x2;
+                               if (rf->xmax>x2) rf->xmax = x2;
                                newrct= stack+ *count;
                                (*count)++;
 
-                               newrct->xmax= x2;
-                               newrct->xmin= rf->xmin+(x2-x1);
-                               newrct->ymin= rf->ymin;
-                               newrct->ymax= rf->ymax;
+                               newrct->xmax = x2;
+                               newrct->xmin = rf->xmin+(x2-x1);
+                               newrct->ymin = rf->ymin;
+                               newrct->ymax = rf->ymax;
                                
-                               if(newrct->xmin==newrct->xmax) (*count)--;
+                               if (newrct->xmin ==newrct->xmax) (*count)--;
                                
-                               rf->xmin= x1;
+                               rf->xmin = x1;
                        }
                }
-               else if(rf->xmax>x2) {
-                       if(rf->xmin>x2) {
+               else if (rf->xmax>x2) {
+                       if (rf->xmin>x2) {
                                rf->xmin-= (x2-x1);
                                rf->xmax-= (x2-x1);
                        }
                        else {
-                               if(rf->xmin<x1) rf->xmin= x1;
+                               if (rf->xmin<x1) rf->xmin = x1;
                                newrct= stack+ *count;
                                (*count)++;
 
-                               newrct->xmin= x1;
-                               newrct->xmax= rf->xmax-(x2-x1);
-                               newrct->ymin= rf->ymin;
-                               newrct->ymax= rf->ymax;
+                               newrct->xmin = x1;
+                               newrct->xmax = rf->xmax-(x2-x1);
+                               newrct->ymin = rf->ymin;
+                               newrct->ymax = rf->ymax;
 
-                               if(newrct->xmin==newrct->xmax) (*count)--;
+                               if (newrct->xmin ==newrct->xmax) (*count)--;
 
-                               rf->xmax= x2;
+                               rf->xmax = x2;
                        }
                }
                rf++;
@@ -348,45 +352,45 @@ static void clipy_rctf_swap(rctf *stack, short *count, float y1, float y2)
 
        a= *count;
        rf= stack;
-       for(;a>0;a--) {
-               if(rf->ymin<y1) {
-                       if(rf->ymax<y1) {
+       for (;a>0;a--) {
+               if (rf->ymin<y1) {
+                       if (rf->ymax<y1) {
                                rf->ymin+= (y2-y1);
                                rf->ymax+= (y2-y1);
                        }
                        else {
-                               if(rf->ymax>y2) rf->ymax= y2;
+                               if (rf->ymax>y2) rf->ymax = y2;
                                newrct= stack+ *count;
                                (*count)++;
 
-                               newrct->ymax= y2;
-                               newrct->ymin= rf->ymin+(y2-y1);
-                               newrct->xmin= rf->xmin;
-                               newrct->xmax= rf->xmax;
+                               newrct->ymax = y2;
+                               newrct->ymin = rf->ymin+(y2-y1);
+                               newrct->xmin = rf->xmin;
+                               newrct->xmax = rf->xmax;
 
-                               if(newrct->ymin==newrct->ymax) (*count)--;
+                               if (newrct->ymin==newrct->ymax) (*count)--;
 
-                               rf->ymin= y1;
+                               rf->ymin = y1;
                        }
                }
-               else if(rf->ymax>y2) {
-                       if(rf->ymin>y2) {
+               else if (rf->ymax>y2) {
+                       if (rf->ymin>y2) {
                                rf->ymin-= (y2-y1);
                                rf->ymax-= (y2-y1);
                        }
                        else {
-                               if(rf->ymin<y1) rf->ymin= y1;
+                               if (rf->ymin<y1) rf->ymin = y1;
                                newrct= stack+ *count;
                                (*count)++;
 
-                               newrct->ymin= y1;
-                               newrct->ymax= rf->ymax-(y2-y1);
-                               newrct->xmin= rf->xmin;
-                               newrct->xmax= rf->xmax;
+                               newrct->ymin = y1;
+                               newrct->ymax = rf->ymax-(y2-y1);
+                               newrct->xmin = rf->xmin;
+                               newrct->xmax = rf->xmax;
 
-                               if(newrct->ymin==newrct->ymax) (*count)--;
+                               if (newrct->ymin==newrct->ymax) (*count)--;
 
-                               rf->ymax= y2;
+                               rf->ymax = y2;
                        }
                }
                rf++;
@@ -408,17 +412,17 @@ static float clipx_rctf(rctf *rf, float x1, float x2)
 
        size= rf->xmax - rf->xmin;
 
-       if(rf->xmin<x1) {
-               rf->xmin= x1;
+       if (rf->xmin<x1) {
+               rf->xmin = x1;
        }
-       if(rf->xmax>x2) {
-               rf->xmax= x2;
+       if (rf->xmax>x2) {
+               rf->xmax = x2;
        }
-       if(rf->xmin > rf->xmax) {
+       if (rf->xmin > rf->xmax) {
                rf->xmin = rf->xmax;
                return 0.0;
        }
-       else if(size!=0.0f) {
+       else if (size!=0.0f) {
                return (rf->xmax - rf->xmin)/size;
        }
        return 1.0;
@@ -430,18 +434,18 @@ static float clipy_rctf(rctf *rf, float y1, float y2)
 
        size= rf->ymax - rf->ymin;
 
-       if(rf->ymin<y1) {
-               rf->ymin= y1;
+       if (rf->ymin<y1) {
+               rf->ymin = y1;
        }
-       if(rf->ymax>y2) {
-               rf->ymax= y2;
+       if (rf->ymax>y2) {
+               rf->ymax = y2;
        }
 
-       if(rf->ymin > rf->ymax) {
+       if (rf->ymin > rf->ymax) {
                rf->ymin = rf->ymax;
                return 0.0;
        }
-       else if(size!=0.0f) {
+       else if (size!=0.0f) {
                return (rf->ymax - rf->ymin)/size;
        }
        return 1.0;
@@ -451,7 +455,7 @@ static float clipy_rctf(rctf *rf, float y1, float y2)
 static void boxsampleclip(struct ImBuf *ibuf, rctf *rf, TexResult *texres)
 {
        /* sample box, is clipped already, and minx etc. have been set at ibuf size.
-          Enlarge with antialiased edges of the pixels */
+        * Enlarge with antialiased edges of the pixels */
 
        float muly, mulx, div, col[4];
        int x, y, startx, endx, starty, endy;
@@ -461,27 +465,27 @@ static void boxsampleclip(struct ImBuf *ibuf, rctf *rf, TexResult *texres)
        starty= (int)floor(rf->ymin);
        endy= (int)floor(rf->ymax);
 
-       if(startx < 0) startx= 0;
-       if(starty < 0) starty= 0;
-       if(endx>=ibuf->x) endx= ibuf->x-1;
-       if(endy>=ibuf->y) endy= ibuf->y-1;
+       if (startx < 0) startx= 0;
+       if (starty < 0) starty= 0;
+       if (endx>=ibuf->x) endx= ibuf->x-1;
+       if (endy>=ibuf->y) endy= ibuf->y-1;
 
-       if(starty==endy && startx==endx) {
+       if (starty==endy && startx==endx) {
                ibuf_get_color(&texres->tr, ibuf, startx, starty);
        }
        else {
                div= texres->tr= texres->tg= texres->tb= texres->ta= 0.0;
-               for(y=starty; y<=endy; y++) {
+               for (y=starty; y<=endy; y++) {
                        
                        muly= 1.0;
 
-                       if(starty==endy);
+                       if (starty==endy);
                        else {
-                               if(y==starty) muly= 1.0f-(rf->ymin - y);
-                               if(y==endy) muly= (rf->ymax - y);
+                               if (y==starty) muly= 1.0f-(rf->ymin - y);
+                               if (y==endy) muly= (rf->ymax - y);
                        }
                        
-                       if(startx==endx) {
+                       if (startx==endx) {
                                mulx= muly;
                                
                                ibuf_get_color(col, ibuf, startx, y);
@@ -493,14 +497,14 @@ static void boxsampleclip(struct ImBuf *ibuf, rctf *rf, TexResult *texres)
                                div+= mulx;
                        }
                        else {
-                               for(x=startx; x<=endx; x++) {
+                               for (x=startx; x<=endx; x++) {
                                        mulx= muly;
-                                       if(x==startx) mulx*= 1.0f-(rf->xmin - x);
-                                       if(x==endx) mulx*= (rf->xmax - x);
+                                       if (x==startx) mulx*= 1.0f-(rf->xmin - x);
+                                       if (x==endx) mulx*= (rf->xmax - x);
 
                                        ibuf_get_color(col, ibuf, x, y);
                                        
-                                       if(mulx==1.0f) {
+                                       if (mulx==1.0f) {
                                                texres->ta+= col[3];
                                                texres->tr+= col[0];
                                                texres->tg+= col[1];
@@ -518,7 +522,7 @@ static void boxsampleclip(struct ImBuf *ibuf, rctf *rf, TexResult *texres)
                        }
                }
 
-               if(div!=0.0f) {
+               if (div!=0.0f) {
                        div= 1.0f/div;
                        texres->tb*= div;
                        texres->tg*= div;
@@ -538,7 +542,7 @@ static void boxsample(ImBuf *ibuf, float minx, float miny, float maxx, float max
         * If variable 'imaprepeat' has been set, the
         * clipped-away parts are sampled as well.
         */
-       /* note: actually minx etc isnt in the proper range... this due to filter size and offset vectors for bump */
+       /* note: actually minx etc isn't in the proper range... this due to filter size and offset vectors for bump */
        /* note: talpha must be initialized */
        /* note: even when 'imaprepeat' is set, this can only repeate once in any direction.
         * the point which min/max is derived from is assumed to be wrapped */
@@ -548,44 +552,44 @@ static void boxsample(ImBuf *ibuf, float minx, float miny, float maxx, float max
        short count=1;
 
        rf= stack;
-       rf->xmin= minx*(ibuf->x);
-       rf->xmax= maxx*(ibuf->x);
-       rf->ymin= miny*(ibuf->y);
-       rf->ymax= maxy*(ibuf->y);
+       rf->xmin = minx*(ibuf->x);
+       rf->xmax = maxx*(ibuf->x);
+       rf->ymin = miny*(ibuf->y);
+       rf->ymax = maxy*(ibuf->y);
 
        texr.talpha= texres->talpha;    /* is read by boxsample_clip */
        
-       if(imapextend) {
+       if (imapextend) {
                CLAMP(rf->xmin, 0.0f, ibuf->x-1);
                CLAMP(rf->xmax, 0.0f, ibuf->x-1);
        }
-       else if(imaprepeat) 
+       else if (imaprepeat)
                clipx_rctf_swap(stack, &count, 0.0, (float)(ibuf->x));
        else {
                alphaclip= clipx_rctf(rf, 0.0, (float)(ibuf->x));
 
-               if(alphaclip<=0.0f) {
+               if (alphaclip<=0.0f) {
                        texres->tr= texres->tb= texres->tg= texres->ta= 0.0;
                        return;
                }
        }
 
-       if(imapextend) {
+       if (imapextend) {
                CLAMP(rf->ymin, 0.0f, ibuf->y-1);
                CLAMP(rf->ymax, 0.0f, ibuf->y-1);
        }
-       else if(imaprepeat) 
+       else if (imaprepeat)
                clipy_rctf_swap(stack, &count, 0.0, (float)(ibuf->y));
        else {
                alphaclip*= clipy_rctf(rf, 0.0, (float)(ibuf->y));
 
-               if(alphaclip<=0.0f) {
+               if (alphaclip<=0.0f) {
                        texres->tr= texres->tb= texres->tg= texres->ta= 0.0;
                        return;
                }
        }
 
-       if(count>1) {
+       if (count>1) {
                tot= texres->tr= texres->tb= texres->tg= texres->ta= 0.0;
                while(count--) {
                        boxsampleclip(ibuf, rf, &texr);
@@ -596,22 +600,22 @@ static void boxsample(ImBuf *ibuf, float minx, float miny, float maxx, float max
                        texres->tr+= opp*texr.tr;
                        texres->tg+= opp*texr.tg;
                        texres->tb+= opp*texr.tb;
-                       if(texres->talpha) texres->ta+= opp*texr.ta;
+                       if (texres->talpha) texres->ta+= opp*texr.ta;
                        rf++;
                }
-               if(tot!= 0.0f) {
+               if (tot!= 0.0f) {
                        texres->tr/= tot;
                        texres->tg/= tot;
                        texres->tb/= tot;
-                       if(texres->talpha) texres->ta/= tot;
+                       if (texres->talpha) texres->ta/= tot;
                }
        }
        else
                boxsampleclip(ibuf, rf, texres);
 
-       if(texres->talpha==0) texres->ta= 1.0;
+       if (texres->talpha==0) texres->ta= 1.0;
        
-       if(alphaclip!=1.0f) {
+       if (alphaclip!=1.0f) {
                /* premul it all */
                texres->tr*= alphaclip;
                texres->tg*= alphaclip;
@@ -974,17 +978,17 @@ static void alpha_clip_aniso(ImBuf *ibuf, float minx, float miny, float maxx, fl
        // TXF apha: we're doing the same alphaclip here as boxsample, but i'm doubting
        // if this is actually correct for the all the filtering algorithms ..
 
-       if(!(extflag == TXC_REPT || extflag == TXC_EXTD)) {
-               rf.xmin= minx*(ibuf->x);
-               rf.xmax= maxx*(ibuf->x);
-               rf.ymin= miny*(ibuf->y);
-               rf.ymax= maxy*(ibuf->y);
+       if (!(extflag == TXC_REPT || extflag == TXC_EXTD)) {
+               rf.xmin = minx*(ibuf->x);
+               rf.xmax = maxx*(ibuf->x);
+               rf.ymin = miny*(ibuf->y);
+               rf.ymax = maxy*(ibuf->y);
 
                alphaclip = clipx_rctf(&rf, 0.0, (float)(ibuf->x));
                alphaclip*= clipy_rctf(&rf, 0.0, (float)(ibuf->y));
                alphaclip= MAX2(alphaclip, 0.0f);
 
-               if(alphaclip!=1.0f) {
+               if (alphaclip!=1.0f) {
                        /* premul it all */
                        texres->tr*= alphaclip;
                        texres->tg*= alphaclip;
@@ -1092,7 +1096,7 @@ static int imagewraposa_aniso(Tex *tex, Image *ima, ImBuf *ibuf, const float tex
 
        if (tex->imaflag & TEX_FILTER_MIN) {
                // make sure the filtersize is minimal in pixels (normal, ref map can have miniature pixel dx/dy)
-                const float addval = (0.5f * tex->filtersize) / (float)MIN2(ibuf->x, ibuf->y);
+               const float addval = (0.5f * tex->filtersize) / (float)MIN2(ibuf->x, ibuf->y);
                if (addval > minx) minx = addval;
                if (addval > miny) miny = addval;
        }
@@ -1209,9 +1213,9 @@ static int imagewraposa_aniso(Tex *tex, Image *ima, ImBuf *ibuf, const float tex
        // brecht: added stupid clamping here, large dx/dy can give very large
        // filter sizes which take ages to render, it may be better to do this
        // more intelligently later in the code .. probably it's not noticeable
-       if(AFD.dxt[0]*AFD.dxt[0] + AFD.dxt[1]*AFD.dxt[1] > 2.0f*2.0f)
+       if (AFD.dxt[0]*AFD.dxt[0] + AFD.dxt[1]*AFD.dxt[1] > 2.0f*2.0f)
                mul_v2_fl(AFD.dxt, 2.0f/len_v2(AFD.dxt));
-       if(AFD.dyt[0]*AFD.dyt[0] + AFD.dyt[1]*AFD.dyt[1] > 2.0f*2.0f)
+       if (AFD.dyt[0]*AFD.dyt[0] + AFD.dyt[1]*AFD.dyt[1] > 2.0f*2.0f)
                mul_v2_fl(AFD.dyt, 2.0f/len_v2(AFD.dyt));
 
        // choice:
@@ -1430,30 +1434,32 @@ int imagewraposa(Tex *tex, Image *ima, ImBuf *ibuf, const float texvec[3], const
        retval= texres->nor?3:1;
        
        /* quick tests */
-       if(ibuf==NULL && ima==NULL)
+       if (ibuf==NULL && ima==NULL)
                return retval;
-       if(ima) {
+       if (ima) {
 
                /* hack for icon render */
-               if(ima->ibufs.first==NULL && (R.r.scemode & R_NO_IMAGE_LOAD))
+               if (ima->ibufs.first==NULL && (R.r.scemode & R_NO_IMAGE_LOAD))
                        return retval;
                
                ibuf= BKE_image_get_ibuf(ima, &tex->iuser); 
+
+               ima->flag|= IMA_USED_FOR_RENDER;
        }
-       if(ibuf==NULL || (ibuf->rect==NULL && ibuf->rect_float==NULL))
+       if (ibuf==NULL || (ibuf->rect==NULL && ibuf->rect_float==NULL))
                return retval;
        
        /* mipmap test */
        image_mipmap_test(tex, ibuf);
 
-       if(tex->imaflag & TEX_USEALPHA) {
-               if(tex->imaflag & TEX_CALCALPHA);
+       if (tex->imaflag & TEX_USEALPHA) {
+               if (tex->imaflag & TEX_CALCALPHA);
                else texres->talpha= 1;
        }
        
        texr.talpha= texres->talpha;
        
-       if(tex->imaflag & TEX_IMAROT) {
+       if (tex->imaflag & TEX_IMAROT) {
                fy= texvec[0];
                fx= texvec[1];
        }
@@ -1462,9 +1468,9 @@ int imagewraposa(Tex *tex, Image *ima, ImBuf *ibuf, const float texvec[3], const
                fy= texvec[1];
        }
        
-       if(ibuf->flags & IB_fields) {
-               if(R.r.mode & R_FIELDS) {                       /* field render */
-                       if(R.flag & R_SEC_FIELD) {              /* correction for 2nd field */
+       if (ibuf->flags & IB_fields) {
+               if (R.r.mode & R_FIELDS) {                      /* field render */
+                       if (R.flag & R_SEC_FIELD) {             /* correction for 2nd field */
                                /* fac1= 0.5/( (float)ibuf->y ); */
                                /* fy-= fac1; */
                        }
@@ -1485,16 +1491,16 @@ int imagewraposa(Tex *tex, Image *ima, ImBuf *ibuf, const float texvec[3], const
        minx= (maxx-minx)/2.0f;
        miny= (maxy-miny)/2.0f;
        
-       if(tex->imaflag & TEX_FILTER_MIN) {
+       if (tex->imaflag & TEX_FILTER_MIN) {
                /* make sure the filtersize is minimal in pixels (normal, ref map can have miniature pixel dx/dy) */
                float addval= (0.5f * tex->filtersize) / (float) MIN2(ibuf->x, ibuf->y);
 
-               if(addval > minx)
+               if (addval > minx)
                        minx= addval;
-               if(addval > miny)
+               if (addval > miny)
                        miny= addval;
        }
-       else if(tex->filtersize!=1.0f) {
+       else if (tex->filtersize!=1.0f) {
                minx*= tex->filtersize;
                miny*= tex->filtersize;
                
@@ -1504,33 +1510,33 @@ int imagewraposa(Tex *tex, Image *ima, ImBuf *ibuf, const float texvec[3], const
                dyt[1]*= tex->filtersize;
        }
 
-       if(tex->imaflag & TEX_IMAROT) SWAP(float, minx, miny);
+       if (tex->imaflag & TEX_IMAROT) SWAP(float, minx, miny);
        
-       if(minx>0.25f) minx= 0.25f;
-       else if(minx<0.00001f) minx= 0.00001f;  /* side faces of unit-cube */
-       if(miny>0.25f) miny= 0.25f;
-       else if(miny<0.00001f) miny= 0.00001f;
+       if (minx>0.25f) minx= 0.25f;
+       else if (minx<0.00001f) minx= 0.00001f; /* side faces of unit-cube */
+       if (miny>0.25f) miny= 0.25f;
+       else if (miny<0.00001f) miny= 0.00001f;
 
        
        /* repeat and clip */
        imaprepeat= (tex->extend==TEX_REPEAT);
        imapextend= (tex->extend==TEX_EXTEND);
 
-       if(tex->extend == TEX_REPEAT) {
-               if(tex->flag & (TEX_REPEAT_XMIR|TEX_REPEAT_YMIR)) {
+       if (tex->extend == TEX_REPEAT) {
+               if (tex->flag & (TEX_REPEAT_XMIR|TEX_REPEAT_YMIR)) {
                        imaprepeat= 0;
                        imapextend= 1;
                }
        }
 
-       if(tex->extend == TEX_CHECKER) {
+       if (tex->extend == TEX_CHECKER) {
                int xs, ys, xs1, ys1, xs2, ys2, boundary;
                
                xs= (int)floor(fx);
                ys= (int)floor(fy);
                
                // both checkers available, no boundary exceptions, checkerdist will eat aliasing
-               if( (tex->flag & TEX_CHECKER_ODD) && (tex->flag & TEX_CHECKER_EVEN) ) {
+               if ( (tex->flag & TEX_CHECKER_ODD) && (tex->flag & TEX_CHECKER_EVEN) ) {
                        fx-= xs;
                        fy-= ys;
                }
@@ -1542,37 +1548,37 @@ int imagewraposa(Tex *tex, Image *ima, ImBuf *ibuf, const float texvec[3], const
                        ys2= (int)floor(fy+miny);
                        boundary= (xs1!=xs2) || (ys1!=ys2);
 
-                       if(boundary==0) {
-                               if( (tex->flag & TEX_CHECKER_ODD)==0) {
-                                       if((xs+ys) & 1); 
+                       if (boundary==0) {
+                               if ( (tex->flag & TEX_CHECKER_ODD)==0) {
+                                       if ((xs+ys) & 1);
                                        else return retval;
                                }
-                               if( (tex->flag & TEX_CHECKER_EVEN)==0) {
-                                       if((xs+ys) & 1) return retval;
+                               if ( (tex->flag & TEX_CHECKER_EVEN)==0) {
+                                       if ((xs+ys) & 1) return retval;
                                }
                                fx-= xs;
                                fy-= ys;
                        }
                        else {
-                               if(tex->flag & TEX_CHECKER_ODD) {
-                                       if((xs1+ys) & 1) fx-= xs2;
+                               if (tex->flag & TEX_CHECKER_ODD) {
+                                       if ((xs1+ys) & 1) fx-= xs2;
                                        else fx-= xs1;
                                        
-                                       if((ys1+xs) & 1) fy-= ys2;
+                                       if ((ys1+xs) & 1) fy-= ys2;
                                        else fy-= ys1;
                                }
-                               if(tex->flag & TEX_CHECKER_EVEN) {
-                                       if((xs1+ys) & 1) fx-= xs1;
+                               if (tex->flag & TEX_CHECKER_EVEN) {
+                                       if ((xs1+ys) & 1) fx-= xs1;
                                        else fx-= xs2;
                                        
-                                       if((ys1+xs) & 1) fy-= ys1;
+                                       if ((ys1+xs) & 1) fy-= ys1;
                                        else fy-= ys2;
                                }
                        }
                }
 
                /* scale around center, (0.5, 0.5) */
-               if(tex->checkerdist<1.0f) {
+               if (tex->checkerdist<1.0f) {
                        fx= (fx-0.5f)/(1.0f-tex->checkerdist) +0.5f;
                        fy= (fy-0.5f)/(1.0f-tex->checkerdist) +0.5f;
                        minx/= (1.0f-tex->checkerdist);
@@ -1580,74 +1586,74 @@ int imagewraposa(Tex *tex, Image *ima, ImBuf *ibuf, const float texvec[3], const
                }
        }
 
-       if(tex->extend == TEX_CLIPCUBE) {
-               if(fx+minx<0.0f || fy+miny<0.0f || fx-minx>1.0f || fy-miny>1.0f || texvec[2]<-1.0f || texvec[2]>1.0f) {
+       if (tex->extend == TEX_CLIPCUBE) {
+               if (fx+minx<0.0f || fy+miny<0.0f || fx-minx>1.0f || fy-miny>1.0f || texvec[2]<-1.0f || texvec[2]>1.0f) {
                        return retval;
                }
        }
-       else if(tex->extend==TEX_CLIP || tex->extend==TEX_CHECKER) {
-               if(fx+minx<0.0f || fy+miny<0.0f || fx-minx>1.0f || fy-miny>1.0f) {
+       else if (tex->extend==TEX_CLIP || tex->extend==TEX_CHECKER) {
+               if (fx+minx<0.0f || fy+miny<0.0f || fx-minx>1.0f || fy-miny>1.0f) {
                        return retval;
                }
        }
        else {
-               if(imapextend) {
-                       if(fx>1.0f) fx = 1.0f;
-                       else if(fx<0.0f) fx= 0.0f;
+               if (imapextend) {
+                       if (fx>1.0f) fx = 1.0f;
+                       else if (fx<0.0f) fx= 0.0f;
                }
                else {
-                       if(fx>1.0f) fx -= (int)(fx);
-                       else if(fx<0.0f) fx+= 1-(int)(fx);
+                       if (fx>1.0f) fx -= (int)(fx);
+                       else if (fx<0.0f) fx+= 1-(int)(fx);
                }
                
-               if(imapextend) {
-                       if(fy>1.0f) fy = 1.0f;
-                       else if(fy<0.0f) fy= 0.0f;
+               if (imapextend) {
+                       if (fy>1.0f) fy = 1.0f;
+                       else if (fy<0.0f) fy= 0.0f;
                }
                else {
-                       if(fy>1.0f) fy -= (int)(fy);
-                       else if(fy<0.0f) fy+= 1-(int)(fy);
+                       if (fy>1.0f) fy -= (int)(fy);
+                       else if (fy<0.0f) fy+= 1-(int)(fy);
                }
        }
 
        /* warning no return! */
-       if( (R.flag & R_SEC_FIELD) && (ibuf->flags & IB_fields) ) {
+       if ( (R.flag & R_SEC_FIELD) && (ibuf->flags & IB_fields) ) {
                ibuf->rect+= (ibuf->x*ibuf->y);
        }
 
        /* choice:  */
-       if(tex->imaflag & TEX_MIPMAP) {
+       if (tex->imaflag & TEX_MIPMAP) {
                ImBuf *previbuf, *curibuf;
                float bumpscale;
                
                dx= minx;
                dy= miny;
                maxd= MAX2(dx, dy);
-               if(maxd>0.5f) maxd= 0.5f;
+               if (maxd>0.5f) maxd= 0.5f;
 
                pixsize = 1.0f/ (float) MIN2(ibuf->x, ibuf->y);
                
                bumpscale= pixsize/maxd;
-               if(bumpscale>1.0f) bumpscale= 1.0f;
+               if (bumpscale>1.0f) bumpscale= 1.0f;
                else bumpscale*=bumpscale;
                
                curmap= 0;
                previbuf= curibuf= ibuf;
                while(curmap<IB_MIPMAP_LEVELS && ibuf->mipmap[curmap]) {
-                       if(maxd < pixsize) break;
+                       if (maxd < pixsize) break;
                        previbuf= curibuf;
                        curibuf= ibuf->mipmap[curmap];
                        pixsize= 1.0f / (float)MIN2(curibuf->x, curibuf->y);
                        curmap++;
                }
 
-               if(previbuf!=curibuf || (tex->imaflag & TEX_INTERPOL)) {
+               if (previbuf!=curibuf || (tex->imaflag & TEX_INTERPOL)) {
                        /* sample at least 1 pixel */
                        if (minx < 0.5f / ibuf->x) minx = 0.5f / ibuf->x;
                        if (miny < 0.5f / ibuf->y) miny = 0.5f / ibuf->y;
                }
                
-               if(texres->nor && (tex->imaflag & TEX_NORMALMAP)==0) {
+               if (texres->nor && (tex->imaflag & TEX_NORMALMAP)==0) {
                        /* a bit extra filter */
                        //minx*= 1.35f;
                        //miny*= 1.35f;
@@ -1663,13 +1669,13 @@ int imagewraposa(Tex *tex, Image *ima, ImBuf *ibuf, const float texvec[3], const
                        texres->nor[0]= (val1-val2);
                        texres->nor[1]= (val1-val3);
                        
-                       if(previbuf!=curibuf) {  /* interpolate */
+                       if (previbuf!=curibuf) {  /* interpolate */
                                
                                boxsample(previbuf, fx-minx, fy-miny, fx+minx, fy+miny, &texr, imaprepeat, imapextend);
                                
                                /* calc rgb */
                                dx= 2.0f*(pixsize-maxd)/pixsize;
-                               if(dx>=1.0f) {
+                               if (dx>=1.0f) {
                                        texres->ta= texr.ta; texres->tb= texr.tb;
                                        texres->tg= texr.tg; texres->tr= texr.tr;
                                }
@@ -1690,7 +1696,7 @@ int imagewraposa(Tex *tex, Image *ima, ImBuf *ibuf, const float texvec[3], const
                                texres->nor[0]= (val1-val2);    /* vals have been interpolated above! */
                                texres->nor[1]= (val1-val3);
                                
-                               if(dx<1.0f) {
+                               if (dx<1.0f) {
                                        dy= 1.0f-dx;
                                        texres->tb= dy*texres->tb+ dx*texr.tb;
                                        texres->tg= dy*texres->tg+ dx*texr.tg;
@@ -1709,15 +1715,16 @@ int imagewraposa(Tex *tex, Image *ima, ImBuf *ibuf, const float texvec[3], const
 
                        boxsample(curibuf, minx, miny, maxx, maxy, texres, imaprepeat, imapextend);
 
-                       if(previbuf!=curibuf) {  /* interpolate */
+                       if (previbuf!=curibuf) {  /* interpolate */
                                boxsample(previbuf, minx, miny, maxx, maxy, &texr, imaprepeat, imapextend);
                                
                                fx= 2.0f*(pixsize-maxd)/pixsize;
                                
-                               if(fx>=1.0f) {
+                               if (fx>=1.0f) {
                                        texres->ta= texr.ta; texres->tb= texr.tb;
                                        texres->tg= texr.tg; texres->tr= texr.tr;
-                               } else {
+                               }
+                               else {
                                        fy= 1.0f-fx;
                                        texres->tb= fy*texres->tb+ fx*texr.tb;
                                        texres->tg= fy*texres->tg+ fx*texr.tg;
@@ -1735,7 +1742,7 @@ int imagewraposa(Tex *tex, Image *ima, ImBuf *ibuf, const float texvec[3], const
                        if (miny < 0.5f / ibuf->y) miny = 0.5f / ibuf->y;
                }
 
-               if(texres->nor && (tex->imaflag & TEX_NORMALMAP)==0) {
+               if (texres->nor && (tex->imaflag & TEX_NORMALMAP)==0) {
                        boxsample(ibuf, fx-minx, fy-miny, fx+minx, fy+miny, texres, imaprepeat, imapextend);
                        val1= texres->tr+texres->tg+texres->tb;
                        boxsample(ibuf, fx-minx+dxt[0], fy-miny+dxt[1], fx+minx+dxt[0], fy+miny+dxt[1], &texr, imaprepeat, imapextend);
@@ -1751,18 +1758,18 @@ int imagewraposa(Tex *tex, Image *ima, ImBuf *ibuf, const float texvec[3], const
                        boxsample(ibuf, fx-minx, fy-miny, fx+minx, fy+miny, texres, imaprepeat, imapextend);
        }
        
-       if(tex->imaflag & TEX_CALCALPHA) {
+       if (tex->imaflag & TEX_CALCALPHA) {
                texres->ta= texres->tin= texres->ta*MAX3(texres->tr, texres->tg, texres->tb);
        }
        else texres->tin= texres->ta;
 
-       if(tex->flag & TEX_NEGALPHA) texres->ta= 1.0f-texres->ta;
+       if (tex->flag & TEX_NEGALPHA) texres->ta= 1.0f-texres->ta;
        
-       if( (R.flag & R_SEC_FIELD) && (ibuf->flags & IB_fields) ) {
+       if ( (R.flag & R_SEC_FIELD) && (ibuf->flags & IB_fields) ) {
                ibuf->rect-= (ibuf->x*ibuf->y);
        }
 
-       if(texres->nor && (tex->imaflag & TEX_NORMALMAP)) {
+       if (texres->nor && (tex->imaflag & TEX_NORMALMAP)) {
                // qdn: normal from color
                // The invert of the red channel is to make
                // the normal map compliant with the outside world.
@@ -1776,7 +1783,7 @@ int imagewraposa(Tex *tex, Image *ima, ImBuf *ibuf, const float texvec[3], const
        }
        
        /* de-premul, this is being premulled in shade_input_do_shade() */
-       if(texres->ta!=1.0f && texres->ta>1e-4f) {
+       if (texres->ta!=1.0f && texres->ta>1e-4f) {
                fx= 1.0f/texres->ta;
                texres->tr*= fx;
                texres->tg*= fx;
@@ -1793,12 +1800,12 @@ void image_sample(Image *ima, float fx, float fy, float dx, float dy, float *res
        TexResult texres;
        ImBuf *ibuf= BKE_image_get_ibuf(ima, NULL);
        
-       if(ibuf==NULL) {
+       if (ibuf==NULL) {
                result[0]= result[1]= result[2]= result[3]= 0.0f;
                return;
        }
        
-       if( (R.flag & R_SEC_FIELD) && (ibuf->flags & IB_fields) )
+       if ( (R.flag & R_SEC_FIELD) && (ibuf->flags & IB_fields) )
                ibuf->rect+= (ibuf->x*ibuf->y);
 
        texres.talpha= 1; /* boxsample expects to be initialized */
@@ -1808,8 +1815,10 @@ void image_sample(Image *ima, float fx, float fy, float dx, float dy, float *res
        result[2]= texres.tb;
        result[3]= texres.ta;
        
-       if( (R.flag & R_SEC_FIELD) && (ibuf->flags & IB_fields) )
+       if ( (R.flag & R_SEC_FIELD) && (ibuf->flags & IB_fields) )
                ibuf->rect-= (ibuf->x*ibuf->y);
+
+       ima->flag|= IMA_USED_FOR_RENDER;
 }
 
 void ibuf_sample(ImBuf *ibuf, float fx, float fy, float dx, float dy, float *result)
@@ -1817,7 +1826,7 @@ void ibuf_sample(ImBuf *ibuf, float fx, float fy, float dx, float dy, float *res
        TexResult texres;
        afdata_t AFD;
        
-       if(ibuf==NULL) {
+       if (ibuf==NULL) {
                return;
        }