use color conversions functions in more places.
authorCampbell Barton <ideasman42@gmail.com>
Thu, 19 Jan 2012 10:04:51 +0000 (10:04 +0000)
committerCampbell Barton <ideasman42@gmail.com>
Thu, 19 Jan 2012 10:04:51 +0000 (10:04 +0000)
also add rgba_float_to_uchar, rgba_uchar_to_float

13 files changed:
source/blender/blenkernel/BKE_image.h
source/blender/blenkernel/intern/brush.c
source/blender/blenkernel/intern/image_gen.c
source/blender/blenkernel/intern/node.c
source/blender/blenlib/BLI_math_color.h
source/blender/blenlib/intern/math_color.c
source/blender/editors/interface/interface_intern.h
source/blender/editors/interface/interface_templates.c
source/blender/editors/interface/interface_widgets.c
source/blender/editors/object/object_bake.c
source/blender/editors/space_view3d/drawmesh.c
source/blender/imbuf/intern/divers.c
source/blender/render/intern/source/rendercore.c

index c829fc69b051b0b493234c18938991257acb7fd0..5cdc3537f5b23ee9033452aa2125fa55a988b9c1 100644 (file)
@@ -186,7 +186,7 @@ void BKE_image_merge(struct Image *dest, struct Image *source);
 int BKE_image_has_alpha(struct Image *image);
 
 /* image_gen.c */
-void BKE_image_buf_fill_color(unsigned char *rect, float *rect_float, int width, int height, float color[4]);
+void BKE_image_buf_fill_color(unsigned char *rect, float *rect_float, int width, int height, const float color[4]);
 void BKE_image_buf_fill_checker(unsigned char *rect, float *rect_float, int height, int width);
 void BKE_image_buf_fill_checker_color(unsigned char *rect, float *rect_float, int height, int width);
 
index 362fa6e5e9ac7cd8d27e803f7f19c5c1e597d761..f2514c1030ba08acbaa02ceae041368b20e2ee94 100644 (file)
@@ -527,7 +527,7 @@ void brush_imbuf_new(const Scene *scene, Brush *brush, short flt, short texfall,
        float xy[2], rgba[4], *dstf;
        int x, y, rowbytes, xoff, yoff, imbflag;
        const int radius= brush_size(scene, brush);
-       char *dst, crgb[3];
+       unsigned char *dst, crgb[3];
        const float alpha= brush_alpha(scene, brush);
        float brush_rgb[3];
     
@@ -571,10 +571,10 @@ void brush_imbuf_new(const Scene *scene, Brush *brush, short flt, short texfall,
        }
        else {
                float alpha_f; /* final float alpha to convert to char */
-               F3TOCHAR3(brush->rgb, crgb);
+               rgb_float_to_uchar(crgb, brush->rgb);
 
                for (y=0; y < ibuf->y; y++) {
-                       dst = (char*)ibuf->rect + y*rowbytes;
+                       dst = (unsigned char *)ibuf->rect + y*rowbytes;
 
                        for (x=0; x < ibuf->x; x++, dst+=4) {
                                xy[0] = x + xoff;
@@ -590,19 +590,15 @@ void brush_imbuf_new(const Scene *scene, Brush *brush, short flt, short texfall,
                                }
                                else if (texfall == 1) {
                                        brush_sample_tex(scene, brush, xy, rgba, 0);
-                                       dst[0] = FTOCHAR(rgba[0]);
-                                       dst[1] = FTOCHAR(rgba[1]);
-                                       dst[2] = FTOCHAR(rgba[2]);
-                                       dst[3] = FTOCHAR(rgba[3]);
+                                       rgba_float_to_uchar(dst, rgba);
                                }
                                else if (texfall == 2) {
                                        brush_sample_tex(scene, brush, xy, rgba, 0);
                                        mul_v3_v3(rgba, brush->rgb);
                                        alpha_f = rgba[3] * alpha * brush_curve_strength_clamp(brush, len_v2(xy), radius);
 
-                                       dst[0] = FTOCHAR(rgba[0]);
-                                       dst[1] = FTOCHAR(rgba[1]);
-                                       dst[2] = FTOCHAR(rgba[2]);
+                                       rgb_float_to_uchar(dst, rgba);
+
                                        dst[3] = FTOCHAR(alpha_f);
                                }
                                else {
@@ -843,7 +839,7 @@ static void brush_painter_do_partial(BrushPainter *painter, ImBuf *oldtexibuf, i
        Brush *brush= painter->brush;
        ImBuf *ibuf, *maskibuf, *texibuf;
        float *bf, *mf, *tf, *otf=NULL, xoff, yoff, xy[2], rgba[4];
-       char *b, *m, *t, *ot= NULL;
+       unsigned char *b, *m, *t, *ot= NULL;
        int dotexold, origx= x, origy= y;
        const int radius= brush_size(painter->scene, brush);
 
@@ -895,12 +891,12 @@ static void brush_painter_do_partial(BrushPainter *painter, ImBuf *oldtexibuf, i
        }
        else {
                for (; y < h; y++) {
-                       b = (char*)ibuf->rect + (y*ibuf->x + origx)*4;
-                       t = (char*)texibuf->rect + (y*texibuf->x + origx)*4;
-                       m = (char*)maskibuf->rect + (y*maskibuf->x + origx)*4;
+                       b = (unsigned char *)ibuf->rect + (y*ibuf->x + origx)*4;
+                       t = (unsigned char *)texibuf->rect + (y*texibuf->x + origx)*4;
+                       m = (unsigned char *)maskibuf->rect + (y*maskibuf->x + origx)*4;
 
                        if (dotexold)
-                               ot = (char*)oldtexibuf->rect + ((y - origy + yt)*oldtexibuf->x + xt)*4;
+                               ot = (unsigned char *)oldtexibuf->rect + ((y - origy + yt)*oldtexibuf->x + xt)*4;
 
                        for (x=origx; x < w; x++, b+=4, m+=4, t+=4) {
                                if (dotexold) {
@@ -915,10 +911,7 @@ static void brush_painter_do_partial(BrushPainter *painter, ImBuf *oldtexibuf, i
                                        xy[1] = y + yoff;
 
                                        brush_sample_tex(scene, brush, xy, rgba, 0);
-                                       t[0]= FTOCHAR(rgba[0]);
-                                       t[1]= FTOCHAR(rgba[1]);
-                                       t[2]= FTOCHAR(rgba[2]);
-                                       t[3]= FTOCHAR(rgba[3]);
+                                       rgba_float_to_uchar(t, rgba);
                                }
 
                                b[0] = t[0]*m[0]/255;
index a93d0221cf09de3e745e86d4a8a3627ea28e0cdd..c6cb8c9582abe6b9a503eecd40d0cd632f322d4b 100644 (file)
@@ -33,7 +33,7 @@
 #include "BLI_math_base.h"
 #include "BLF_api.h"
 
-void BKE_image_buf_fill_color(unsigned char *rect, float *rect_float, int width, int height, float color[4])
+void BKE_image_buf_fill_color(unsigned char *rect, float *rect_float, int width, int height, const float color[4])
 {
        int x, y;
 
@@ -41,22 +41,17 @@ void BKE_image_buf_fill_color(unsigned char *rect, float *rect_float, int width,
        if(rect_float) {
                for(y= 0; y<height; y++) {
                        for(x= 0; x<width; x++) {
-                               rect_float[0]= color[0];
-                               rect_float[1]= color[1];
-                               rect_float[2]= color[2];
-                               rect_float[3]= color[3];
+                               copy_v4_v4(rect_float, color);
                                rect_float+= 4;
                        }
                }
        }
        
        if(rect) {
-               char ccol[4];
+               unsigned char ccol[4];
+
+               rgba_float_to_uchar(ccol, color);
 
-               ccol[0]= (char)(color[0]*255.0f);
-               ccol[1]= (char)(color[1]*255.0f);
-               ccol[2]= (char)(color[2]*255.0f);
-               ccol[3]= (char)(color[3]*255.0f);
                for(y= 0; y<height; y++) {
                        for(x= 0; x<width; x++) {
                                
index 61111e82cc7d3a68d07900eb6c9b112659b339af..a2d7d9b502cdaaaf6c03e7355d7597852b0625d4 100644 (file)
@@ -793,10 +793,7 @@ void nodeAddToPreview(bNode *node, float *col, int x, int y, int do_manage)
                                        linearrgb_to_srgb_uchar4(tar, col);
                                }
                                else {
-                                       tar[0]= FTOCHAR(col[0]);
-                                       tar[1]= FTOCHAR(col[1]);
-                                       tar[2]= FTOCHAR(col[2]);
-                                       tar[3]= FTOCHAR(col[3]);
+                                       rgba_float_to_uchar(tar, col);
                                }
                        }
                        //else printf("prv out bound x y %d %d\n", x, y);
index 1b8cc08abf7ea0ba4dc3e552b6a32180f9e4a870..453f12582725d527df54680be7456ad8768ec4f2 100644 (file)
@@ -102,7 +102,9 @@ void rgb_float_set_hue_float_offset(float * rgb, float hue_offset);
 void rgb_byte_set_hue_float_offset(unsigned char * rgb, float hue_offset);
 
 void rgb_uchar_to_float(float col_r[3], const unsigned char col_ub[3]);
+void rgba_uchar_to_float(float col_r[4], const unsigned char col_ub[4]);
 void rgb_float_to_uchar(unsigned char col_r[3], const float col_f[3]);
+void rgba_float_to_uchar(unsigned char col_r[4], const float col_f[4]);
 
 /***************** lift/gamma/gain / ASC-CDL conversion *****************/
 
index 31d69ba68b43294f4598189182de20e12744672d..94dbdf9fe58dee7183aee0cdc868e95de38a7240 100644 (file)
@@ -344,6 +344,14 @@ void rgb_uchar_to_float(float col_r[3], const unsigned char col_ub[3])
        col_r[2]= ((float)col_ub[2]) / 255.0f;
 }
 
+void rgba_uchar_to_float(float col_r[4], const unsigned char col_ub[4])
+{
+       col_r[0]= ((float)col_ub[0]) / 255.0f;
+       col_r[1]= ((float)col_ub[1]) / 255.0f;
+       col_r[2]= ((float)col_ub[2]) / 255.0f;
+       col_r[3]= ((float)col_ub[3]) / 255.0f;
+}
+
 void rgb_float_to_uchar(unsigned char col_r[3], const float col_f[3])
 {
        int r, g, b;
@@ -357,6 +365,21 @@ void rgb_float_to_uchar(unsigned char col_r[3], const float col_f[3])
        col_r[2]= (char)((b <= 0)? 0 : (b >= 255)? 255 : b);
 }
 
+void rgba_float_to_uchar(unsigned char col_r[4], const float col_f[4])
+{
+       int r, g, b, a;
+
+       r= (int)(col_f[0] * 255.0f);
+       g= (int)(col_f[1] * 255.0f);
+       b= (int)(col_f[2] * 255.0f);
+       a= (int)(col_f[3] * 255.0f);
+
+       col_r[0]= (char)((r <= 0)? 0 : (r >= 255)? 255 : r);
+       col_r[1]= (char)((g <= 0)? 0 : (g >= 255)? 255 : g);
+       col_r[2]= (char)((b <= 0)? 0 : (b >= 255)? 255 : b);
+       col_r[3]= (char)((a <= 0)? 0 : (a >= 255)? 255 : a);
+}
+
 /* ********************************* color transforms ********************************* */
 
 
index e31d7e39d896ccc7ee6db86622644935348efe6e..7d53e7acbc2a24362baa7006108a7c1092e44635 100644 (file)
@@ -163,7 +163,7 @@ struct uiBut {
        float hardmin, hardmax, softmin, softmax;
        float a1, a2;
        float aspect;
-       char col[4];
+       unsigned char col[4];
 
        uiButHandleFunc func;
        void *func_arg1;
index 0862af2756aa0ff9fd0ffdd37aa299c823161ff2..a1eb20c32e24336907d90ecd09cdf8d3fa4db312 100644 (file)
@@ -2626,9 +2626,7 @@ void uiTemplateReportsBanner(uiLayout *layout, bContext *C)
        uiBlockBeginAlign(block);
        but= uiDefBut(block, ROUNDBOX, 0, "", 0, 0, UI_UNIT_X+10, UI_UNIT_Y, NULL, 0.0f, 0.0f, 0, 0, "");
        /* set the report's bg color in but->col - ROUNDBOX feature */
-       but->col[0]= FTOCHAR(rti->col[0]);
-       but->col[1]= FTOCHAR(rti->col[1]);
-       but->col[2]= FTOCHAR(rti->col[2]);
+       rgb_float_to_uchar(but->col, rti->col);
        but->col[3]= 255; 
 
        but= uiDefBut(block, ROUNDBOX, 0, "", UI_UNIT_X+10, 0, UI_UNIT_X+width, UI_UNIT_Y, NULL, 0.0f, 0.0f, 0, 0, "");
index f5c62f65512da47846fa1189255560156c287349..19acfaccaa354e645671e71bbab7fc9cc92cf3a0 100644 (file)
@@ -2501,7 +2501,7 @@ static void widget_swatch(uiBut *but, uiWidgetColors *wcol, rcti *rect, int stat
        if (color_profile)
                linearrgb_to_srgb_v3_v3(col, col);
        
-       F4TOCHAR4(col, wcol->inner);
+       rgba_float_to_uchar((unsigned char *)wcol->inner, col);
 
        wcol->shaded = 0;
        wcol->alpha_check = (wcol->inner[3] < 255);
index a02b90a1d1675e4a58a7c4df3dfaa1ea8020aa13..6ec778d7ee923b9c133d7ada95a191e2a839dc35 100644 (file)
@@ -807,10 +807,8 @@ static void apply_tangmat_callback(DerivedMesh *lores_dm, DerivedMesh *hires_dm,
 
                ibuf->userflags= IB_RECT_INVALID;
        } else {
-               char *rrgb= (char*)ibuf->rect + pixel*4;
-               rrgb[0]= FTOCHAR(vec[0]);
-               rrgb[1]= FTOCHAR(vec[1]);
-               rrgb[2]= FTOCHAR(vec[2]);
+               unsigned char *rrgb= (unsigned char *)ibuf->rect + pixel*4;
+               rgb_float_to_uchar(rrgb, vec);
                rrgb[3]= 255;
        }
 }
index b1842578c205caccb2ba0bbe53f575777ed2b889..5f521cc3dd1e5786e4cd2bbddf3352a952e4577f 100644 (file)
@@ -349,11 +349,8 @@ static void draw_textured_begin(Scene *scene, View3D *v3d, RegionView3D *rv3d, O
                Gtexdraw.islit= GPU_scene_object_lights(scene, ob, v3d->lay, rv3d->viewmat, !rv3d->is_persp);
        }
        
-       obcol[0]= CLAMPIS(ob->col[0]*255, 0, 255);
-       obcol[1]= CLAMPIS(ob->col[1]*255, 0, 255);
-       obcol[2]= CLAMPIS(ob->col[2]*255, 0, 255);
-       obcol[3]= CLAMPIS(ob->col[3]*255, 0, 255);
-       
+       rgba_float_to_uchar(obcol, ob->col);
+
        glCullFace(GL_BACK); glEnable(GL_CULL_FACE);
        if(solidtex || v3d->drawtype==OB_TEXTURE) istex= 1;
        else istex= 0;
index 442309ccf7ac0612e3142ba1345960bc235a830f..7e086716d3e064bf18001baba1d42e9534d1e1de 100644 (file)
@@ -164,19 +164,6 @@ MINLINE unsigned char dither_value(unsigned short v_in, DitherContext *di, int i
 
 /************************* Generic Buffer Conversion *************************/
 
-MINLINE void byte_to_float_v4(float f[4], const uchar b[4])
-{
-       f[0]= b[0] * (1.0f/255.0f);
-       f[1]= b[1] * (1.0f/255.0f);
-       f[2]= b[2] * (1.0f/255.0f);
-       f[3]= b[3] * (1.0f/255.0f);
-}
-
-MINLINE void float_to_byte_v4(uchar b[4], const float f[4])
-{
-       F4TOCHAR4(f, b);
-}
-
 MINLINE void ushort_to_byte_v4(uchar b[4], const unsigned short us[4])
 {
        b[0]= USHORTTOUCHAR(us[0]);
@@ -233,7 +220,7 @@ void IMB_buffer_byte_from_float(uchar *rect_to, const float *rect_from,
                        if(profile_to == profile_from) {
                                /* no color space conversion */
                                for(x = 0; x < width; x++, from+=3, to+=4) {
-                                       F3TOCHAR3(from, to);
+                                       rgb_float_to_uchar(to, from);
                                        to[3] = 255;
                                }
                        }
@@ -241,7 +228,7 @@ void IMB_buffer_byte_from_float(uchar *rect_to, const float *rect_from,
                                /* convert from linear to sRGB */
                                for(x = 0; x < width; x++, from+=3, to+=4) {
                                        linearrgb_to_srgb_v3_v3(tmp, from);
-                                       F3TOCHAR3(tmp, to);
+                                       rgb_float_to_uchar(to, tmp);
                                        to[3] = 255;
                                }
                        }
@@ -249,7 +236,7 @@ void IMB_buffer_byte_from_float(uchar *rect_to, const float *rect_from,
                                /* convert from sRGB to linear */
                                for(x = 0; x < width; x++, from+=3, to+=4) {
                                        srgb_to_linearrgb_v3_v3(tmp, from);
-                                       F3TOCHAR3(tmp, to);
+                                       rgb_float_to_uchar(to, tmp);
                                        to[3] = 255;
                                }
                        }
@@ -267,7 +254,7 @@ void IMB_buffer_byte_from_float(uchar *rect_to, const float *rect_from,
                                }
                                else {
                                        for(x = 0; x < width; x++, from+=4, to+=4)
-                                               float_to_byte_v4(to, from);
+                                               rgba_float_to_uchar(to, from);
                                }
                        }
                        else if(profile_to == IB_PROFILE_SRGB) {
@@ -316,13 +303,13 @@ void IMB_buffer_byte_from_float(uchar *rect_to, const float *rect_from,
                                else if(predivide) {
                                        for(x = 0; x < width; x++, from+=4, to+=4) {
                                                srgb_to_linearrgb_predivide_v4(tmp, from);
-                                               float_to_byte_v4(to, tmp);
+                                               rgba_float_to_uchar(to, tmp);
                                        }
                                }
                                else {
                                        for(x = 0; x < width; x++, from+=4, to+=4) {
                                                srgb_to_linearrgb_v4(tmp, from);
-                                               float_to_byte_v4(to, tmp);
+                                               rgba_float_to_uchar(to, tmp);
                                        }
                                }
                        }
@@ -358,7 +345,7 @@ void IMB_buffer_float_from_byte(float *rect_to, const uchar *rect_from,
                if(profile_to == profile_from) {
                        /* no color space conversion */
                        for(x = 0; x < width; x++, from+=4, to+=4)
-                               byte_to_float_v4(to, from);
+                               rgba_uchar_to_float(to, from);
                }
                else if(profile_to == IB_PROFILE_LINEAR_RGB) {
                        /* convert sRGB to linear */
@@ -377,13 +364,13 @@ void IMB_buffer_float_from_byte(float *rect_to, const uchar *rect_from,
                        /* convert linear to sRGB */
                        if(predivide) {
                                for(x = 0; x < width; x++, from+=4, to+=4) {
-                                       byte_to_float_v4(tmp, from);
+                                       rgba_uchar_to_float(tmp, from);
                                        linearrgb_to_srgb_predivide_v4(to, tmp);
                                }
                        }
                        else {
                                for(x = 0; x < width; x++, from+=4, to+=4) {
-                                       byte_to_float_v4(tmp, from);
+                                       rgba_uchar_to_float(tmp, from);
                                        linearrgb_to_srgb_v4(to, tmp);
                                }
                        }
@@ -502,16 +489,16 @@ void IMB_buffer_byte_from_byte(uchar *rect_to, const uchar *rect_from,
                        /* convert to sRGB to linear */
                        if(predivide) {
                                for(x = 0; x < width; x++, from+=4, to+=4) {
-                                       byte_to_float_v4(tmp, from);
+                                       rgba_uchar_to_float(tmp, from);
                                        srgb_to_linearrgb_predivide_v4(tmp, tmp);
-                                       float_to_byte_v4(to, tmp);
+                                       rgba_float_to_uchar(to, tmp);
                                }
                        }
                        else {
                                for(x = 0; x < width; x++, from+=4, to+=4) {
-                                       byte_to_float_v4(tmp, from);
+                                       rgba_uchar_to_float(tmp, from);
                                        srgb_to_linearrgb_v4(tmp, tmp);
-                                       float_to_byte_v4(to, tmp);
+                                       rgba_float_to_uchar(to, tmp);
                                }
                        }
                }
@@ -519,16 +506,16 @@ void IMB_buffer_byte_from_byte(uchar *rect_to, const uchar *rect_from,
                        /* convert from linear to sRGB */
                        if(predivide) {
                                for(x = 0; x < width; x++, from+=4, to+=4) {
-                                       byte_to_float_v4(tmp, from);
+                                       rgba_uchar_to_float(tmp, from);
                                        linearrgb_to_srgb_predivide_v4(tmp, tmp);
-                                       float_to_byte_v4(to, tmp);
+                                       rgba_float_to_uchar(to, tmp);
                                }
                        }
                        else {
                                for(x = 0; x < width; x++, from+=4, to+=4) {
-                                       byte_to_float_v4(tmp, from);
+                                       rgba_uchar_to_float(tmp, from);
                                        linearrgb_to_srgb_v4(tmp, tmp);
-                                       float_to_byte_v4(to, tmp);
+                                       rgba_float_to_uchar(to, tmp);
                                }
                        }
                }
index c5d580b2e9f5fbbd982fc9030d17f9a2d9cc5a34..773af96671a8b17f61e342aef68715ed0c3b35a2 100644 (file)
@@ -2170,10 +2170,9 @@ static void bake_shade(void *handle, Object *ob, ShadeInput *shi, int UNUSED(qua
 
                if (ELEM(bs->type, RE_BAKE_ALL, RE_BAKE_TEXTURE) &&     (R.r.color_mgt_flag & R_COLOR_MANAGEMENT)) {
                        linearrgb_to_srgb_uchar3(col, shr.combined);
-               } else {
-                       col[0]= FTOCHAR(shr.combined[0]);
-                       col[1]= FTOCHAR(shr.combined[1]);
-                       col[2]= FTOCHAR(shr.combined[2]);
+               }
+               else {
+                       rgb_float_to_uchar(col, shr.combined);
                }
                
                if (ELEM(bs->type, RE_BAKE_ALL, RE_BAKE_TEXTURE)) {
@@ -2205,9 +2204,7 @@ static void bake_displacement(void *handle, ShadeInput *UNUSED(shi), float dist,
                col[3]= 1.0f;
        } else {        
                char *col= (char *)(bs->rect + bs->rectx*y + x);
-               col[0]= FTOCHAR(disp);
-               col[1]= FTOCHAR(disp);
-               col[2]= FTOCHAR(disp);
+               col[0] = col[1] = col[2] = FTOCHAR(disp);
                col[3]= 255;
        }
        if (bs->rect_mask) {