[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [43531] trunk/blender/source/blender/ blenlib: Fix issue in recent color commits, was still doing a multiplication by 255 too

Brecht Van Lommel brechtvanlommel at pandora.be
Thu Jan 19 17:22:28 CET 2012


Revision: 43531
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=43531
Author:   blendix
Date:     2012-01-19 16:22:22 +0000 (Thu, 19 Jan 2012)
Log Message:
-----------
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.

Modified Paths:
--------------
    trunk/blender/source/blender/blenlib/BLI_math_color.h
    trunk/blender/source/blender/blenlib/intern/math_color.c
    trunk/blender/source/blender/blenlib/intern/math_color_inline.c

Modified: trunk/blender/source/blender/blenlib/BLI_math_color.h
===================================================================
--- trunk/blender/source/blender/blenlib/BLI_math_color.h	2012-01-19 16:12:40 UTC (rev 43530)
+++ trunk/blender/source/blender/blenlib/BLI_math_color.h	2012-01-19 16:22:22 UTC (rev 43531)
@@ -91,6 +91,9 @@
 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 *************************/

Modified: trunk/blender/source/blender/blenlib/intern/math_color.c
===================================================================
--- trunk/blender/source/blender/blenlib/intern/math_color.c	2012-01-19 16:12:40 UTC (rev 43530)
+++ trunk/blender/source/blender/blenlib/intern/math_color.c	2012-01-19 16:22:22 UTC (rev 43531)
@@ -354,30 +354,12 @@
 
 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 ********************************* */

Modified: trunk/blender/source/blender/blenlib/intern/math_color_inline.c
===================================================================
--- trunk/blender/source/blender/blenlib/intern/math_color_inline.c	2012-01-19 16:12:40 UTC (rev 43530)
+++ trunk/blender/source/blender/blenlib/intern/math_color_inline.c	2012-01-19 16:22:22 UTC (rev 43531)
@@ -62,32 +62,20 @@
 	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



More information about the Bf-blender-cvs mailing list