[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [43378] trunk/blender/source/blender: add utility function to BLI_math_color - rgb_to_luma, rgb_to_luma_byte, also use rgb_to_grayscale in more places.

Campbell Barton ideasman42 at gmail.com
Sat Jan 14 18:14:33 CET 2012


Revision: 43378
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=43378
Author:   campbellbarton
Date:     2012-01-14 17:14:23 +0000 (Sat, 14 Jan 2012)
Log Message:
-----------
add utility function to BLI_math_color - rgb_to_luma, rgb_to_luma_byte, also use rgb_to_grayscale in more places.

Modified Paths:
--------------
    trunk/blender/source/blender/blenkernel/intern/colortools.c
    trunk/blender/source/blender/blenlib/BLI_math_color.h
    trunk/blender/source/blender/blenlib/intern/math_color.c
    trunk/blender/source/blender/editors/space_image/image_ops.c
    trunk/blender/source/blender/editors/space_sequencer/sequencer_scopes.c
    trunk/blender/source/blender/render/intern/source/shadeoutput.c

Modified: trunk/blender/source/blender/blenkernel/intern/colortools.c
===================================================================
--- trunk/blender/source/blender/blenkernel/intern/colortools.c	2012-01-14 16:26:08 UTC (rev 43377)
+++ trunk/blender/source/blender/blenkernel/intern/colortools.c	2012-01-14 17:14:23 UTC (rev 43378)
@@ -1002,7 +1002,7 @@
 			}
 
 			/* we still need luma for histogram */
-			luma = 0.299f * rgb[0] + 0.587f * rgb[1] + 0.114f * rgb[2];
+			luma = rgb_to_luma(rgb);
 
 			/* check for min max */
 			if(ycc_mode == -1 ) {

Modified: trunk/blender/source/blender/blenlib/BLI_math_color.h
===================================================================
--- trunk/blender/source/blender/blenlib/BLI_math_color.h	2012-01-14 16:26:08 UTC (rev 43377)
+++ trunk/blender/source/blender/blenlib/BLI_math_color.h	2012-01-14 17:14:23 UTC (rev 43378)
@@ -69,8 +69,10 @@
 unsigned int rgb_to_cpack(float r, float g, float b);
 unsigned int hsv_to_cpack(float h, float s, float v);
 
-float rgb_to_grayscale(float rgb[3]);
-unsigned char rgb_to_grayscale_byte(unsigned char rgb[3]);
+float rgb_to_grayscale(const float rgb[3]);
+unsigned char rgb_to_grayscale_byte(const unsigned char rgb[3]);
+float rgb_to_luma(const float rgb[3]);
+unsigned char rgb_to_luma_byte(const unsigned char rgb[3]);
 
 /**************** Profile Transformations *****************/
 

Modified: trunk/blender/source/blender/blenlib/intern/math_color.c
===================================================================
--- trunk/blender/source/blender/blenlib/intern/math_color.c	2012-01-14 16:26:08 UTC (rev 43377)
+++ trunk/blender/source/blender/blenlib/intern/math_color.c	2012-01-14 17:14:23 UTC (rev 43378)
@@ -434,16 +434,26 @@
 	return 0;                         /* Color within RGB gamut */
 }
 
-float rgb_to_grayscale(float rgb[3])
+float rgb_to_grayscale(const float rgb[3])
 {
 	return 0.3f*rgb[0] + 0.58f*rgb[1] + 0.12f*rgb[2];
 }
 
-unsigned char rgb_to_grayscale_byte(unsigned char rgb[3])
+unsigned char rgb_to_grayscale_byte(const unsigned char rgb[3])
 {
 	return (76*(unsigned short)rgb[0] + 148*(unsigned short)rgb[1] + 31*(unsigned short)rgb[2]) / 255;
 }
 
+float rgb_to_luma(const float rgb[3])
+{
+	return 0.299f*rgb[0] + 0.587f*rgb[1] + 0.114f*rgb[2];
+}
+
+unsigned char rgb_to_luma_byte(const unsigned char rgb[3])
+{
+	return (76*(unsigned short)rgb[0] + 150*(unsigned short)rgb[1] + 29*(unsigned short)rgb[2]) / 255;
+}
+
 /* ********************************* lift/gamma/gain / ASC-CDL conversion ********************************* */
 
 void lift_gamma_gain_to_asc_cdl(float *lift, float *gamma, float *gain, float *offset, float *slope, float *power)

Modified: trunk/blender/source/blender/editors/space_image/image_ops.c
===================================================================
--- trunk/blender/source/blender/editors/space_image/image_ops.c	2012-01-14 16:26:08 UTC (rev 43377)
+++ trunk/blender/source/blender/editors/space_image/image_ops.c	2012-01-14 17:14:23 UTC (rev 43378)
@@ -2016,14 +2016,14 @@
 				hist->data_r[i] = rgb[0];
 				hist->data_g[i] = rgb[1];
 				hist->data_b[i] = rgb[2];
-				hist->data_luma[i] = (0.299f*rgb[0] + 0.587f*rgb[1] + 0.114f*rgb[2]);
+				hist->data_luma[i] = rgb_to_luma(rgb);
 			}
 			else if (ibuf->rect) {
 				cp= (unsigned char *)(ibuf->rect + y*ibuf->x + x);
 				hist->data_r[i] = (float)cp[0]/255.0f;
 				hist->data_g[i] = (float)cp[1]/255.0f;
 				hist->data_b[i] = (float)cp[2]/255.0f;
-				hist->data_luma[i] = (0.299f*cp[0] + 0.587f*cp[1] + 0.114f*cp[2])/255;
+				hist->data_luma[i] = (float)rgb_to_luma_byte(cp)/255.0f;
 			}
 		}
 	}

Modified: trunk/blender/source/blender/editors/space_sequencer/sequencer_scopes.c
===================================================================
--- trunk/blender/source/blender/editors/space_sequencer/sequencer_scopes.c	2012-01-14 16:26:08 UTC (rev 43377)
+++ trunk/blender/source/blender/editors/space_sequencer/sequencer_scopes.c	2012-01-14 17:14:23 UTC (rev 43378)
@@ -29,16 +29,17 @@
 #include <math.h>
 #include <string.h>
 
+#include "BLI_math_color.h"
 #include "BLI_utildefines.h"
 
-
-
 #include "IMB_imbuf_types.h"
 #include "IMB_imbuf.h"
 
 #include "sequencer_intern.h"
 
-static void rgb_to_yuv(float rgb[3], float yuv[3]) 
+/* XXX, why is this function better then BLI_math version?
+ * only difference is it does some normalize after, need to double check on this - campbell */
+static void rgb_to_yuv_normalized(const float rgb[3], float yuv[3])
 {
 		yuv[0]= 0.299f*rgb[0] + 0.587f*rgb[1] + 0.114f*rgb[2];
 		yuv[1]= 0.492f*(rgb[2] - yuv[0]);
@@ -169,10 +170,7 @@
 
 		for (x = 0; x < ibuf->x; x++) {
 			unsigned char * rgb = src + 4 * (ibuf->x * y + x);
-			float v = 1.0 * 
-				(  0.299*rgb[0] 
-				 + 0.587*rgb[1] 
-				 + 0.114*rgb[2]) / 255.0;
+			float v = (float)rgb_to_luma_byte(rgb) / 255.0;
 			unsigned char * p = tgt;
 			p += 4 * (w * ((int) (v * (h - 3)) + 1) + x + 1);
 
@@ -215,10 +213,7 @@
 
 		for (x = 0; x < ibuf->x; x++) {
 			float * rgb = src + 4 * (ibuf->x * y + x);
-			float v = 1.0f *
-				(  0.299f*rgb[0]
-				 + 0.587f*rgb[1]
-				 + 0.114f*rgb[2]);
+			float v = rgb_to_luma(rgb);
 			unsigned char * p = tgt;
 
 			CLAMP(v, 0.0f, 1.0f);
@@ -583,7 +578,7 @@
 	rgb[0]= (float)r/255.0f;
 	rgb[1]= (float)g/255.0f;
 	rgb[2]= (float)b/255.0f;
-	rgb_to_yuv(rgb, yuv);
+	rgb_to_yuv_normalized(rgb, yuv);
 			
 	p = tgt + 4 * (w * (int) ((yuv[2] * (h - 3) + 1)) 
 			   + (int) ((yuv[1] * (w - 3) + 1)));
@@ -634,7 +629,7 @@
 			rgb[0]= (float)src1[0]/255.0f;
 			rgb[1]= (float)src1[1]/255.0f;
 			rgb[2]= (float)src1[2]/255.0f;
-			rgb_to_yuv(rgb, yuv);
+			rgb_to_yuv_normalized(rgb, yuv);
 			
 			p = tgt + 4 * (w * (int) ((yuv[2] * (h - 3) + 1)) 
 					   + (int) ((yuv[1] * (w - 3) + 1)));
@@ -684,7 +679,7 @@
 			CLAMP(rgb[1], 0.0f, 1.0f);
 			CLAMP(rgb[2], 0.0f, 1.0f);
 
-			rgb_to_yuv(rgb, yuv);
+			rgb_to_yuv_normalized(rgb, yuv);
 			
 			p = tgt + 4 * (w * (int) ((yuv[2] * (h - 3) + 1)) 
 					   + (int) ((yuv[1] * (w - 3) + 1)));

Modified: trunk/blender/source/blender/render/intern/source/shadeoutput.c
===================================================================
--- trunk/blender/source/blender/render/intern/source/shadeoutput.c	2012-01-14 16:26:08 UTC (rev 43377)
+++ trunk/blender/source/blender/render/intern/source/shadeoutput.c	2012-01-14 17:14:23 UTC (rev 43378)
@@ -900,7 +900,7 @@
 
 	if(ma->ramp_col) {
 		if(ma->rampin_col==MA_RAMP_IN_RESULT) {
-			float fac= 0.3f*diff[0] + 0.58f*diff[1] + 0.12f*diff[2];
+			float fac = rgb_to_grayscale(diff);
 			do_colorband(ma->ramp_col, fac, col);
 			
 			/* blending method */
@@ -932,6 +932,7 @@
 			/* input */
 			switch(ma->rampin_col) {
 			case MA_RAMP_IN_ENERGY:
+				/* should use 'rgb_to_grayscale' but we only have a vector version */
 				fac= 0.3f*r + 0.58f*g + 0.12f*b;
 				break;
 			case MA_RAMP_IN_SHADER:
@@ -974,7 +975,7 @@
 
 	if(ma->ramp_spec && (ma->rampin_spec==MA_RAMP_IN_RESULT)) {
 		float col[4];
-		float fac= 0.3f*spec_col[0] + 0.58f*spec_col[1] + 0.12f*spec_col[2];
+		float fac = rgb_to_grayscale(spec_col);
 
 		do_colorband(ma->ramp_spec, fac, col);
 		




More information about the Bf-blender-cvs mailing list