render stamp drawing is now done everywhere - (not just when saving
[blender.git] / source / blender / imbuf / intern / rectop.c
index 591ab066c57f1297aec36e4770574dc57d112f44..fde0b2b3623be7709781f8c897423480336c8f05 100644 (file)
@@ -521,18 +521,19 @@ void IMB_rectfill(struct ImBuf *drect, float col[4])
 #define FTOCHAR(val) (val<=0.0f ? 0: (val>=1.0f ? 255: (char)(255.99f*val)))
 #define CLAMP(a, b, c)         if((a)<(b)) (a)=(b); else if((a)>(c)) (a)=(c)
 #define SWAP(type, a, b)        { type sw_ap; sw_ap=(a); (a)=(b); (b)=sw_ap; }
-void IMB_rectfill_area(struct ImBuf *ibuf, float *col, int x1, int y1, int x2, int y2)
+
+void buf_rectfill_area(unsigned char *rect, float *rectf, int width, int height, float *col, int x1, int y1, int x2, int y2)
 {
        int i, j;
        float a, ai;
-       if ((!ibuf) || (!col))
+       if ((!rect && !rectf) || (!col) || col[3]==0.0)
                return;
        
        /* sanity checks for coords */
-       CLAMP(x1, 0, ibuf->x);
-       CLAMP(x2, 0, ibuf->x);
-       CLAMP(y1, 0, ibuf->y);
-       CLAMP(y2, 0, ibuf->y);
+       CLAMP(x1, 0, width);
+       CLAMP(x2, 0, width);
+       CLAMP(y1, 0, height);
+       CLAMP(y2, 0, height);
 
        if (x1>x2) SWAP(int,x1,x2);
        if (y1>y2) SWAP(int,y1,y2);
@@ -541,38 +542,44 @@ void IMB_rectfill_area(struct ImBuf *ibuf, float *col, int x1, int y1, int x2, i
        a = col[3];
        ai = 1-a;
        
-       if (ibuf->rect) {
-               unsigned char *img, *pixel; 
+
+       if (rect) {
+               unsigned char *pixel; 
                unsigned char chr, chg, chb;
-       
-               chr = FTOCHAR(col[0]);
-               chg = FTOCHAR(col[1]);
-               chb = FTOCHAR(col[2]);
+               float fr, fg, fb;
                
-               img = (unsigned char *) ibuf->rect;
+               if (a == 1.0) {
+                       chr = FTOCHAR(col[0]);
+                       chg = FTOCHAR(col[1]);
+                       chb = FTOCHAR(col[2]);
+               } else {
+                       fr = col[0]*a;
+                       fg = col[1]*a;
+                       fb = col[2]*a;
+               }
                for (j = 0; j < y2-y1; j++) {
                        for (i = 0; i < x2-x1; i++) {
-                               pixel = img + 4 * (((y1 + j) * ibuf->x) + (x1 + i));
-                               if (a == 1.0) {
-                                       pixel[0] = chr;
-                                       pixel[1] = chg;
-                                       pixel[2] = chb;
-                               } else {
-                                       pixel[0] = (char)((chr*a) + (pixel[0]*ai));
-                                       pixel[1] = (char)((chg*a) + (pixel[1]*ai));
-                                       pixel[2] = (char)((chb*a) + (pixel[2]*ai));
+                               pixel = rect + 4 * (((y1 + j) * width) + (x1 + i));
+                               if (pixel >= rect && pixel < rect+ (4 * (width * height))) {
+                                       if (a == 1.0) {
+                                               pixel[0] = chr;
+                                               pixel[1] = chg;
+                                               pixel[2] = chb;
+                                       } else {
+                                               pixel[0] = (char)(fr + ((float)pixel[0]*ai));
+                                               pixel[1] = (char)(fg + ((float)pixel[1]*ai));
+                                               pixel[2] = (char)(fb + ((float)pixel[2]*ai));
+                                       }
                                }
-                               
                        }
                }
        }
        
-       if (ibuf->rect_float) {
-               float *img, *pixel;
-               img = ibuf->rect_float;
+       if (rectf) {
+               float *pixel;
                for (j = 0; j < y2-y1; j++) {
                        for (i = 0; i < x2-x1; i++) {
-                               pixel = img + 4 * (((y1 + j) * ibuf->x) + (x1 + i));
+                               pixel = rectf + 4 * (((y1 + j) * width) + (x1 + i));
                                if (a == 1.0) {
                                        pixel[0] = col[0];
                                        pixel[1] = col[1];
@@ -586,3 +593,9 @@ void IMB_rectfill_area(struct ImBuf *ibuf, float *col, int x1, int y1, int x2, i
                }
        }
 }
+
+void IMB_rectfill_area(struct ImBuf *ibuf, float *col, int x1, int y1, int x2, int y2)
+{
+       if (!ibuf) return;
+       buf_rectfill_area((unsigned char *) ibuf->rect, ibuf->rect_float, ibuf->x, ibuf->y, col, x1, y1, x2, y2);
+}