From 1d46bbef7e07d70cf44d304f261bc5a1e8056f24 Mon Sep 17 00:00:00 2001 From: Brecht Van Lommel Date: Thu, 19 Jan 2012 16:22:22 +0000 Subject: [PATCH] Fix issue in recent color commits, was still doing a multiplication by 255 too many, also don't check uchar range after casting to int, this can still cause overflow with large float values. --- source/blender/blenlib/BLI_math_color.h | 3 +++ source/blender/blenlib/intern/math_color.c | 22 ++-------------- .../blenlib/intern/math_color_inline.c | 26 +++++-------------- 3 files changed, 12 insertions(+), 39 deletions(-) diff --git a/source/blender/blenlib/BLI_math_color.h b/source/blender/blenlib/BLI_math_color.h index 453f1258272..f4d6882b5d8 100644 --- a/source/blender/blenlib/BLI_math_color.h +++ b/source/blender/blenlib/BLI_math_color.h @@ -91,6 +91,9 @@ MINLINE void linearrgb_to_srgb_v4(float srgb[4], const float linear[4]); MINLINE void srgb_to_linearrgb_predivide_v4(float linear[4], const float srgb[4]); MINLINE void linearrgb_to_srgb_predivide_v4(float srgb[4], const float linear[4]); +MINLINE void linearrgb_to_srgb_uchar3(unsigned char srgb[3], const float linear[3]); +MINLINE void linearrgb_to_srgb_uchar4(unsigned char srgb[4], const float linear[4]); + void BLI_init_srgb_conversion(void); /************************** Other *************************/ diff --git a/source/blender/blenlib/intern/math_color.c b/source/blender/blenlib/intern/math_color.c index 94dbdf9fe58..ca2aeca8f36 100644 --- a/source/blender/blenlib/intern/math_color.c +++ b/source/blender/blenlib/intern/math_color.c @@ -354,30 +354,12 @@ 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]) { - int r, g, b; - - r= (int)(col_f[0] * 255.0f); - g= (int)(col_f[1] * 255.0f); - b= (int)(col_f[2] * 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); + F3TOCHAR3(col_f, col_r); } 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); + F4TOCHAR4(col_f, col_r); } /* ********************************* color transforms ********************************* */ diff --git a/source/blender/blenlib/intern/math_color_inline.c b/source/blender/blenlib/intern/math_color_inline.c index 2c7b13b86a2..386452ed592 100644 --- a/source/blender/blenlib/intern/math_color_inline.c +++ b/source/blender/blenlib/intern/math_color_inline.c @@ -62,32 +62,20 @@ MINLINE void linearrgb_to_srgb_v4(float srgb[4], const float linear[4]) srgb[3] = linear[3]; } -MINLINE void linearrgb_to_srgb_uchar3(unsigned char srgb[4], const float linear[4]) +MINLINE void linearrgb_to_srgb_uchar3(unsigned char srgb[3], const float linear[3]) { - int r, g, b; + float srgb_f[3]; - r = 255 * linearrgb_to_srgb(linear[0]); - g = 255 * linearrgb_to_srgb(linear[1]); - b = 255 * linearrgb_to_srgb(linear[2]); - - srgb[0] = FTOCHAR(r); - srgb[1] = FTOCHAR(g); - srgb[2] = FTOCHAR(b); + linearrgb_to_srgb_v3_v3(srgb_f, linear); + F3TOCHAR3(srgb_f, srgb); } MINLINE void linearrgb_to_srgb_uchar4(unsigned char srgb[4], const float linear[4]) { - int r, g, b, a; + float srgb_f[4]; - r = 255 * linearrgb_to_srgb(linear[0]); - g = 255 * linearrgb_to_srgb(linear[1]); - b = 255 * linearrgb_to_srgb(linear[2]); - a = 255 * linear[3]; - - srgb[0] = FTOCHAR(r); - srgb[1] = FTOCHAR(g); - srgb[2] = FTOCHAR(b); - srgb[3] = FTOCHAR(a); + linearrgb_to_srgb_v4(srgb_f, linear); + F4TOCHAR4(srgb_f, srgb); } /* predivide versions to work on associated/premultipled alpha. if this should