[Bf-blender-cvs] [eea7521] master: leanup: style, use 'const' where possible, and simplified blend funcs.

Bastien Montagne noreply at git.blender.org
Wed Jul 30 12:30:30 CEST 2014


Commit: eea7521e21f9aed1c996dde5b248c5c8d5926440
Author: Bastien Montagne
Date:   Wed Jul 30 10:42:25 2014 +0200
Branches: master
https://developer.blender.org/rBeea7521e21f9aed1c996dde5b248c5c8d5926440

leanup: style, use 'const' where possible, and simplified blend funcs.

Much better to use small loops when doing complex operations over color elements
(any serious compiler will flatten them anyway), avoids (some!) stupid mistakes when
editing their code.

Also, use min/max funcs instead of lengthier 'if (foo < 0) foo = 0'.

===================================================================

M	source/blender/blenlib/intern/math_color_blend_inline.c

===================================================================

diff --git a/source/blender/blenlib/intern/math_color_blend_inline.c b/source/blender/blenlib/intern/math_color_blend_inline.c
index 1985e06..2522fe5 100644
--- a/source/blender/blenlib/intern/math_color_blend_inline.c
+++ b/source/blender/blenlib/intern/math_color_blend_inline.c
@@ -225,40 +225,22 @@ MINLINE void blend_color_add_alpha_byte(unsigned char dst[4], const unsigned cha
 
 MINLINE void blend_color_overlay_byte(unsigned char dst[4], unsigned const char src1[4], unsigned const char src2[4])
 {
-	const unsigned char fac = src2[3];
+	const int fac = (int)src2[3];
 	if (fac != 0) {
-		int temp;
-		int mfac = 255 - fac;
-		if (src1[0] > 127)
-			temp = 255 - ((255 - 2 * (src1[0] - 127)) * (255 - src2[0]) / 255);
-		else
-			temp = (2 * src1[0] * src2[0]) >> 8;
-		temp = (temp * fac + src1[0] * mfac) / 255;
-		if (temp < 255)
-			dst[0] = temp;
-		else
-			dst[0] = 255;
-		if (src1[1] > 127)
-			temp = 255 - ((255 - 2 * (src1[1] - 127)) * (255 - src2[1]) / 255);
-		else
-			temp = (2 * src1[1] * src2[1]) / 255;
-
-		temp = (temp * fac + src1[1] * mfac) / 255;
-		if (temp < 255)
-			dst[1] = temp;
-		else
-			dst[1] = 255;
-
-		if (src1[2] > 127)
-			temp = 255 - ((255 - 2 * (src1[2] - 127)) * (255 - src2[2]) / 255);
-		else
-			temp = (2 * src1[2] * src2[2]) / 255;
-
-		temp = (temp * fac + src1[2] * mfac) / 255;
-		if (temp < 255)
-			dst[2] = temp;
-		else
-			dst[2] = 255;
+		const int mfac = 255 - fac;
+		int i = 3;
+
+		while (i--) {
+			int temp;
+
+			if (src1[i] > 127) {
+				temp = 255 - ((255 - 2 * (src1[i] - 127)) * (255 - src2[i]) / 255);
+			}
+			else {
+				temp = (2 * src1[i] * src2[i]) >> 8;
+			}
+			dst[i] = (unsigned char)min_ii((temp * fac + src1[i] * mfac) / 255, 255);
+		}
 	}
 	else {
 		/* no op */
@@ -269,33 +251,22 @@ MINLINE void blend_color_overlay_byte(unsigned char dst[4], unsigned const char
 
 MINLINE void blend_color_hardlight_byte(unsigned char dst[4], unsigned const char src1[4], unsigned const char src2[4])
 {
-	const unsigned char fac = src2[3];
+	const int fac = (int)src2[3];
 	if (fac != 0) {
-		int temp;
-		int mfac = 255 - fac;
-		if (src2[0] > 127)
-			temp = 255 - ((255 - 2 * (src2[0] - 127)) * (255 - src1[0]) / 255);
-		else
-			temp = (2 * src2[0] * src1[0]) >> 8;
-		temp = (temp * fac + src1[0] * mfac) / 255;
-		if (temp < 255) dst[0] = temp; else dst[0] = 255;
-
-
-		if (src2[1] > 127)
-			temp = 255 - ((255 - 2 * (src2[1] - 127)) * (255 - src1[1]) / 255);
-		else
-			temp = (2 * src2[1] * src1[1]) / 255;
-		temp = (temp * fac + src1[1] * mfac) / 255;
-		if (temp < 255) dst[1] = temp; else dst[1] = 255;
-
-
-		if (src2[2] > 127)
-			temp = 255 - ((255 - 2 * (src2[2] - 127)) * (255 - src1[2]) / 255);
-		else
-			temp = (2 * src2[2] * src1[2]) / 255;
-
-		temp = (temp * fac + src1[2] * mfac) / 255;
-		if (temp < 255) dst[2] = temp; else dst[2] = 255;
+		const int mfac = 255 - fac;
+		int i = 3;
+
+		while (i--) {
+			int temp;
+
+			if (src2[i] > 127) {
+				temp = 255 - ((255 - 2 * (src2[i] - 127)) * (255 - src1[i]) / 255);
+			}
+			else {
+				temp = (2 * src2[i] * src1[i]) >> 8;
+			}
+			dst[i] = (unsigned char)min_ii((temp * fac + src1[i] * mfac) / 255, 255);
+		}
 	}
 	else {
 		/* no op */
@@ -306,38 +277,15 @@ MINLINE void blend_color_hardlight_byte(unsigned char dst[4], unsigned const cha
 
 MINLINE void blend_color_burn_byte(unsigned char dst[4], unsigned const char src1[4], unsigned const char src2[4])
 {
-	const unsigned char fac = src2[3];
+	const int fac = src2[3];
 	if (fac != 0) {
-		int temp;
-		int mfac = 255 - fac;
-
-
-		if (src2[0] == 0)
-			temp = 0;
-		else
-			temp = 255 - ((255 - src1[0]) * 255) / src2[0];
-		if (temp < 0)
-			temp = 0;
-		dst[0] = (temp * fac + src1[0] * mfac) / 255;
-
-
-		if (src2[1] == 0)
-			temp = 0;
-		else
-			temp = 255 - ((255 - src1[1]) * 255) / src2[1];
-		if (temp < 0)
-			temp = 0;
-		dst[1] = (temp * fac + src1[1] * mfac) / 255;
-
-
-		if (src2[2] == 0)
-			temp = 0;
-		else
-			temp = 255 - ((255 - src1[2]) * 255) / src2[2];
-		if (temp < 0)
-			temp = 0;
-		dst[2] = (temp * fac + src1[2] * mfac) / 255;
+		const int mfac = 255 - fac;
+		int i = 3;
 
+		while (i--) {
+			const int temp = (src2[i] == 0) ? 0 : max_ii(255 - ((255 - src1[i]) * 255) / src2[i], 0);
+			dst[i] = (unsigned char)((temp * fac + src1[i] * mfac) / 255);
+		}
 	}
 	else {
 		/* no op */
@@ -348,22 +296,15 @@ MINLINE void blend_color_burn_byte(unsigned char dst[4], unsigned const char src
 
 MINLINE void blend_color_linearburn_byte(unsigned char dst[4], unsigned const char src1[4], unsigned const char src2[4])
 {
-	const unsigned char fac = src2[3];
+	const int fac = src2[3];
 	if (fac != 0) {
-		int temp;
-		int mfac = 255 - fac;
-
-		temp = src1[0] + src2[0] - 255;
-		if (temp < 0) temp = 0;
-		dst[0] = (temp * fac + src1[0] * mfac) / 255;
+		const int mfac = 255 - fac;
+		int i = 3;
 
-		temp = src1[1] + src2[1] - 255;
-		if (temp < 0) temp = 0;
-		dst[1] = (temp * fac + src1[1] * mfac) / 255;
-
-		temp = src1[2] + src2[2] - 255;
-		if (temp < 0) temp = 0;
-		dst[2] = (temp * fac + src1[2] * mfac) / 255;
+		while (i--) {
+			const int temp = max_ii(src1[i] + src2[i] - 255, 0);
+			dst[i] = (unsigned char)((temp * fac + src1[i] * mfac) / 255);
+		}
 	}
 	else {
 		/* no op */
@@ -374,25 +315,15 @@ MINLINE void blend_color_linearburn_byte(unsigned char dst[4], unsigned const ch
 
 MINLINE void blend_color_dodge_byte(unsigned char dst[4], unsigned const char src1[4], unsigned const char src2[4])
 {
-	const unsigned char fac = src2[3];
+	const int fac = src2[3];
 	if (fac != 0) {
-		int temp;
-		int mfac = 255 - fac;
+		const int mfac = 255 - fac;
+		int i = 3;
 
-		if (src2[0] == 255) temp = 255;
-		else temp = (src1[0] * 255) / (255 - src2[0]);
-		if (temp > 255) temp = 255;
-		dst[0] = (temp * fac + src1[0] * mfac) / 255;
-
-		if (src2[1] == 255) temp = 255;
-		else temp = (src1[1] * 255) / (255 - src2[1]);
-		if (temp > 255) temp = 255;
-		dst[1] = (temp * fac + src1[1] * mfac) / 255;
-
-		if (src2[2] == 255) temp = 255;
-		else temp = (src1[2] * 255) / (255 - src2[2]);
-		if (temp > 255) temp = 255;
-		dst[2] = (temp * fac + src1[2] * mfac) / 255;
+		while (i--) {
+			const int temp = (src2[i] == 255) ? 255 : min_ii((src1[i] * 255) / (255 - src2[i]), 255);
+			dst[i] = (unsigned char)((temp * fac + src1[i] * mfac) / 255);
+		}
 	}
 	else {
 		/* no op */
@@ -402,22 +333,15 @@ MINLINE void blend_color_dodge_byte(unsigned char dst[4], unsigned const char sr
 
 MINLINE void blend_color_screen_byte(unsigned char dst[4], unsigned const char src1[4], unsigned const char src2[4])
 {
-	const unsigned char fac = src2[3];
+	const int fac = src2[3];
 	if (fac != 0) {
-		int temp;
-		int mfac = 255 - fac;
-
-		temp = 255 - (((255 - src1[0]) * (255 - src2[0])) / 255);
-		if (temp < 0) temp = 0;
-		dst[0] = (temp * fac + src1[0] * mfac) / 255;
+		const int mfac = 255 - fac;
+		int i = 3;
 
-		temp = 255 - (((255 - src1[1]) * (255 - src2[1])) / 255);
-		if (temp < 0) temp = 0;
-		dst[1] = (temp * fac + src1[1] * mfac) / 255;
-
-		temp = 255 - (((255 - src1[2]) * (255 - src2[2])) / 255);
-		if (temp < 0) temp = 0;
-		dst[2] = (temp * fac + src1[2] * mfac) / 255;
+		while (i--) {
+			const int temp = max_ii(255 - (((255 - src1[i]) * (255 - src2[i])) / 255), 0);
+			dst[i] = (unsigned char)((temp * fac + src1[i] * mfac) / 255);
+		}
 	}
 	else {
 		/* no op */
@@ -428,19 +352,22 @@ MINLINE void blend_color_screen_byte(unsigned char dst[4], unsigned const char s
 
 MINLINE void blend_color_softlight_byte(unsigned char dst[4], unsigned const char src1[4], unsigned const char src2[4])
 {
-	const unsigned char fac = src2[3];
+	const int fac = src2[3];
 	if (fac != 0) {
-		int temp;
-		int mfac = 255 - fac;
-
-		temp = ((unsigned char)((src1[0] < 127) ? ((2 * ((src2[0] / 2) + 64)) * (src1[0])) / 255 : (255 - (2 * (255 - ((src2[0] / 2) + 64)) * (255 - src1[0]) / 255))));
-		dst[0] = (temp * fac + src1[0] * mfac) / 255;
-
-		temp = ((unsigned char)((src1[1] < 127) ? ((2 * ((src2[1] / 2) + 64)) * (src1[1])) / 255 : (255 - (2 * (255 - ((src2[1] / 2) + 64)) * (255 - src1[1]) / 255))));
-		dst[1] = (temp * fac + src1[1] * mfac) / 255;
-
-		temp = ((unsigned char)((src1[2] < 127) ? ((2 * ((src2[2] / 2) + 64)) * (src1[2])) / 255 : (255 - (2 * (255 - ((src2[2] / 2) + 64)) * (255 - src1[2]) / 255))));
-		dst[2] = (temp * fac + src1[2] * mfac) / 255;
+		const int mfac = 255 - fac;
+		int i = 3;
+
+		while (i--) {
+			int temp;
+
+			if (src1[i] < 127) {
+				temp = ((2 * ((src2[i] / 2) + 64)) * src1[i]) / 255;
+			}
+			else {
+				temp = 255 - (2 * (255 - ((src2[i] / 2) + 64)) * (255 - src1[i]) / 255);
+			}
+			dst[i] = (unsigned char)((temp * fac + src1[i] * mfac) / 255);
+		}
 	}
 	else {
 		/* no op */
@@ -451,46 +378,22 @@ MINLINE void blend_color_softlight_byte(unsigned char dst[4], unsigned const cha
 
 MINLINE void blend_color_pinlight_byte(unsigned char dst[4], unsigned const char src1[4], unsigned const char src2[4])
 {
-	const unsigned char fac = src2[3];
+	const int fac = src2[3];
 	if (fac != 0) {
-		int temp;
-		int mfac = 255 - fac;
-
-		if (src2[0] > 127) {
-			temp = 2 * (src2[0] - 127);
-			if (src1[0] > temp)
-				temp = src1[0];
-		}
-		else {
-			temp = 2 * src2[0];
-			if (src1[0] < temp)
-				temp = src1[0];
-		}
-
-		dst[0] = (temp * fac + src1[0] * mfac) / 255;
-
-
-		if (src2[1] > 127) {
-			temp = 2 * (src2[1] - 127);
-			if (src1[1] > temp) temp = src1[1];
+		const int mfac = 255 - fac;
+		int i = 3;
+
+		while (i--) {
+			int temp;
+
+			if (src2[i] > 127) {
+				temp = max_ii(2 * (src2[i] - 127), src1[i]);
+			}
+			else {
+				temp = min_ii(2 * src2[i], src1[i]);
+			}
+			dst[i] = (unsigned char)((temp * fac + src1[i] * mfac) / 255);
 		}
-		else {
-			temp = 2 * src2[1];
-			if (src1[1] < temp) temp = src1[1];
-		}
-
-		dst[1] = (temp * fac + src1[1] * mfac) / 255;
-
-
-		if (src2[2] > 127) {
-			temp = 2 * (src2[2] - 127);
-			if (src1[2] > temp) temp = src1[2];
-		}
-		else {
-			temp = 2 * src2[2];
-			if (src1[2] < temp) temp = src1[2];
-		}
-		dst[2] = (temp * fac + src1[2] * mfac) / 255;
 	}
 	else {
 		/* no op */
@@ -501,43 +404,22 @@ MINLINE void blend_color_pinlight_byte(unsigned char dst[4], unsigned const char
 
 MINLINE void blend_color_linearlight_byt

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list