diff options
author | Campbell Barton <ideasman42@gmail.com> | 2012-01-14 21:14:23 +0400 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2012-01-14 21:14:23 +0400 |
commit | 0d5595befb71303926d03e40dc5791e55f7f7ad9 (patch) | |
tree | d4c3f976df9ea92c546d35ed99215e4710a3db51 | |
parent | 6e72a7c1138169eb23ff3628e5e0a27349e7015a (diff) |
add utility function to BLI_math_color - rgb_to_luma, rgb_to_luma_byte, also use rgb_to_grayscale in more places.
-rw-r--r-- | source/blender/blenkernel/intern/colortools.c | 2 | ||||
-rw-r--r-- | source/blender/blenlib/BLI_math_color.h | 6 | ||||
-rw-r--r-- | source/blender/blenlib/intern/math_color.c | 14 | ||||
-rw-r--r-- | source/blender/editors/space_image/image_ops.c | 4 | ||||
-rw-r--r-- | source/blender/editors/space_sequencer/sequencer_scopes.c | 23 | ||||
-rw-r--r-- | source/blender/render/intern/source/shadeoutput.c | 5 |
6 files changed, 31 insertions, 23 deletions
diff --git a/source/blender/blenkernel/intern/colortools.c b/source/blender/blenkernel/intern/colortools.c index 67bd6a22348..108b4e48a95 100644 --- a/source/blender/blenkernel/intern/colortools.c +++ b/source/blender/blenkernel/intern/colortools.c @@ -1002,7 +1002,7 @@ void scopes_update(Scopes *scopes, ImBuf *ibuf, int use_color_management) } /* 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 ) { diff --git a/source/blender/blenlib/BLI_math_color.h b/source/blender/blenlib/BLI_math_color.h index 2d8b0188165..6d37aabd6ab 100644 --- a/source/blender/blenlib/BLI_math_color.h +++ b/source/blender/blenlib/BLI_math_color.h @@ -69,8 +69,10 @@ void rgb_to_hsv_compat(float r, float g, float b, float *lh, float *ls, float *l 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 *****************/ diff --git a/source/blender/blenlib/intern/math_color.c b/source/blender/blenlib/intern/math_color.c index e9e700d355a..20df893015d 100644 --- a/source/blender/blenlib/intern/math_color.c +++ b/source/blender/blenlib/intern/math_color.c @@ -434,16 +434,26 @@ int constrain_rgb(float *r, float *g, float *b) 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) diff --git a/source/blender/editors/space_image/image_ops.c b/source/blender/editors/space_image/image_ops.c index 999a25f57b5..06674513868 100644 --- a/source/blender/editors/space_image/image_ops.c +++ b/source/blender/editors/space_image/image_ops.c @@ -2016,14 +2016,14 @@ static int image_sample_line_exec(bContext *C, wmOperator *op) 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; } } } diff --git a/source/blender/editors/space_sequencer/sequencer_scopes.c b/source/blender/editors/space_sequencer/sequencer_scopes.c index 8c189d96062..2bb40ce51f4 100644 --- a/source/blender/editors/space_sequencer/sequencer_scopes.c +++ b/source/blender/editors/space_sequencer/sequencer_scopes.c @@ -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 @@ static struct ImBuf *make_waveform_view_from_ibuf_byte(struct ImBuf * ibuf) 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 @@ static struct ImBuf *make_waveform_view_from_ibuf_float(struct ImBuf * ibuf) 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 @@ static void vectorscope_put_cross(unsigned char r, unsigned char g, 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 @@ static struct ImBuf *make_vectorscope_view_from_ibuf_byte(struct ImBuf * ibuf) 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 @@ static struct ImBuf *make_vectorscope_view_from_ibuf_float(struct ImBuf * ibuf) 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))); diff --git a/source/blender/render/intern/source/shadeoutput.c b/source/blender/render/intern/source/shadeoutput.c index 9cb30c2ba1a..e2cfd25c40b 100644 --- a/source/blender/render/intern/source/shadeoutput.c +++ b/source/blender/render/intern/source/shadeoutput.c @@ -900,7 +900,7 @@ static void ramp_diffuse_result(float *diff, ShadeInput *shi) 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 @@ static void add_to_diffuse(float *diff, ShadeInput *shi, float is, float r, floa /* 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 @@ static void ramp_spec_result(float spec_col[3], ShadeInput *shi) 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); |