From f74d85ffc8232a859b1419f5dc25b244ae04375f Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Mon, 7 May 2018 17:31:28 +0200 Subject: Cleanup: rename char/float conversion functions - FTOCHAR -> unit_float_to_uchar_clamp - F3TOCHAR3 -> unit_float_to_uchar_clamp_v3 (swap args) - F4TOCHAR4 -> unit_float_to_uchar_clamp_v4 (swap args) - FTOUSHORT -> unit_float_to_ushort_clamp - USHORTTOUCHAR -> unit_ushort_to_uchar --- source/blender/blenlib/intern/math_base_inline.c | 31 +++++++++++------------ source/blender/blenlib/intern/math_color.c | 4 +-- source/blender/blenlib/intern/math_color_inline.c | 28 ++++++++++---------- 3 files changed, 31 insertions(+), 32 deletions(-) (limited to 'source/blender/blenlib') diff --git a/source/blender/blenlib/intern/math_base_inline.c b/source/blender/blenlib/intern/math_base_inline.c index 82ed6cd1cd1..268279f98b1 100644 --- a/source/blender/blenlib/intern/math_base_inline.c +++ b/source/blender/blenlib/intern/math_base_inline.c @@ -514,35 +514,34 @@ MALWAYS_INLINE __m128 _bli_math_blend_sse(const __m128 mask, } /* Low level conversion functions */ -/* TODO: name sensibly. */ -MINLINE unsigned char FTOCHAR(float val) +MINLINE unsigned char unit_float_to_uchar_clamp(float val) { return (unsigned char)(((val <= 0.0f) ? 0 : ((val > (1.0f - 0.5f / 255.0f)) ? 255 : ((255.0f * val) + 0.5f)))); } -#define FTOCHAR(val) ((CHECK_TYPE_INLINE(val, float)), FTOCHAR(val)) +#define unit_float_to_uchar_clamp(val) ((CHECK_TYPE_INLINE(val, float)), unit_float_to_uchar_clamp(val)) -MINLINE unsigned short FTOUSHORT(float val) +MINLINE unsigned short unit_float_to_ushort_clamp(float val) { return (unsigned short)((val >= 1.0f - 0.5f / 65535) ? 65535 : (val <= 0.0f) ? 0 : (val * 65535.0f + 0.5f)); } -#define FTOUSHORT(val) ((CHECK_TYPE_INLINE(val, float)), FTOUSHORT(val)) +#define unit_float_to_ushort_clamp(val) ((CHECK_TYPE_INLINE(val, float)), unit_float_to_ushort_clamp(val)) -MINLINE unsigned char USHORTTOUCHAR(unsigned short val) +MINLINE unsigned char unit_ushort_to_uchar(unsigned short val) { return (unsigned char)(((val) >= 65535 - 128) ? 255 : ((val) + 128) >> 8); } -#define USHORTTOUCHAR(val) ((CHECK_TYPE_INLINE(val, unsigned short)), USHORTTOUCHAR(val)) +#define unit_ushort_to_uchar(val) ((CHECK_TYPE_INLINE(val, unsigned short)), unit_ushort_to_uchar(val)) -#define F3TOCHAR3(v2, v1) { \ - (v1)[0] = FTOCHAR((v2[0])); \ - (v1)[1] = FTOCHAR((v2[1])); \ - (v1)[2] = FTOCHAR((v2[2])); \ +#define unit_float_to_uchar_clamp_v3(v1, v2) { \ + (v1)[0] = unit_float_to_uchar_clamp((v2[0])); \ + (v1)[1] = unit_float_to_uchar_clamp((v2[1])); \ + (v1)[2] = unit_float_to_uchar_clamp((v2[2])); \ } ((void)0) -#define F4TOCHAR4(v2, v1) { \ - (v1)[0] = FTOCHAR((v2[0])); \ - (v1)[1] = FTOCHAR((v2[1])); \ - (v1)[2] = FTOCHAR((v2[2])); \ - (v1)[3] = FTOCHAR((v2[3])); \ +#define unit_float_to_uchar_clamp_v4(v1, v2) { \ + (v1)[0] = unit_float_to_uchar_clamp((v2[0])); \ + (v1)[1] = unit_float_to_uchar_clamp((v2[1])); \ + (v1)[2] = unit_float_to_uchar_clamp((v2[2])); \ + (v1)[3] = unit_float_to_uchar_clamp((v2[3])); \ } ((void)0) #endif /* __SSE2__ */ diff --git a/source/blender/blenlib/intern/math_color.c b/source/blender/blenlib/intern/math_color.c index a091595bc6c..ef9e46fc62d 100644 --- a/source/blender/blenlib/intern/math_color.c +++ b/source/blender/blenlib/intern/math_color.c @@ -439,12 +439,12 @@ void rgba_uchar_to_float(float r_col[4], const unsigned char col_ub[4]) void rgb_float_to_uchar(unsigned char r_col[3], const float col_f[3]) { - F3TOCHAR3(col_f, r_col); + unit_float_to_uchar_clamp_v3(r_col, col_f); } void rgba_float_to_uchar(unsigned char r_col[4], const float col_f[4]) { - F4TOCHAR4(col_f, r_col); + unit_float_to_uchar_clamp_v4(r_col, col_f); } /* ********************************* color transforms ********************************* */ diff --git a/source/blender/blenlib/intern/math_color_inline.c b/source/blender/blenlib/intern/math_color_inline.c index bc3a1ee3e90..d8e5d530b25 100644 --- a/source/blender/blenlib/intern/math_color_inline.c +++ b/source/blender/blenlib/intern/math_color_inline.c @@ -117,7 +117,7 @@ MINLINE void linearrgb_to_srgb_uchar3(unsigned char srgb[3], const float linear[ float srgb_f[3]; linearrgb_to_srgb_v3_v3(srgb_f, linear); - F3TOCHAR3(srgb_f, srgb); + unit_float_to_uchar_clamp_v3(srgb, srgb_f); } MINLINE void linearrgb_to_srgb_uchar4(unsigned char srgb[4], const float linear[4]) @@ -125,7 +125,7 @@ MINLINE void linearrgb_to_srgb_uchar4(unsigned char srgb[4], const float linear[ float srgb_f[4]; linearrgb_to_srgb_v4(srgb_f, linear); - F4TOCHAR4(srgb_f, srgb); + unit_float_to_uchar_clamp_v4(srgb, srgb_f); } /* predivide versions to work on associated/pre-multiplied alpha. if this should @@ -204,7 +204,7 @@ MINLINE void linearrgb_to_srgb_ushort4(unsigned short srgb[4], const float linea srgb[0] = to_srgb_table_lookup(linear[0]); srgb[1] = to_srgb_table_lookup(linear[1]); srgb[2] = to_srgb_table_lookup(linear[2]); - srgb[3] = FTOUSHORT(linear[3]); + srgb[3] = unit_float_to_ushort_clamp(linear[3]); } MINLINE void srgb_to_linearrgb_uchar4(float linear[4], const unsigned char srgb[4]) @@ -334,9 +334,9 @@ MINLINE void float_to_byte_dither_v3(unsigned char b[3], const float f[3], float { float dither_value = dither_random_value(s, t) * 0.005f * dither; - b[0] = FTOCHAR(dither_value + f[0]); - b[1] = FTOCHAR(dither_value + f[1]); - b[2] = FTOCHAR(dither_value + f[2]); + b[0] = unit_float_to_uchar_clamp(dither_value + f[0]); + b[1] = unit_float_to_uchar_clamp(dither_value + f[1]); + b[2] = unit_float_to_uchar_clamp(dither_value + f[2]); } /**************** Alpha Transformations *****************/ @@ -391,19 +391,19 @@ MINLINE void straight_uchar_to_premul_float(float result[4], const unsigned char MINLINE void premul_float_to_straight_uchar(unsigned char *result, const float color[4]) { if (color[3] == 0.0f || color[3] == 1.0f) { - result[0] = FTOCHAR(color[0]); - result[1] = FTOCHAR(color[1]); - result[2] = FTOCHAR(color[2]); - result[3] = FTOCHAR(color[3]); + result[0] = unit_float_to_uchar_clamp(color[0]); + result[1] = unit_float_to_uchar_clamp(color[1]); + result[2] = unit_float_to_uchar_clamp(color[2]); + result[3] = unit_float_to_uchar_clamp(color[3]); } else { const float alpha_inv = 1.0f / color[3]; /* hopefully this would be optimized */ - result[0] = FTOCHAR(color[0] * alpha_inv); - result[1] = FTOCHAR(color[1] * alpha_inv); - result[2] = FTOCHAR(color[2] * alpha_inv); - result[3] = FTOCHAR(color[3]); + result[0] = unit_float_to_uchar_clamp(color[0] * alpha_inv); + result[1] = unit_float_to_uchar_clamp(color[1] * alpha_inv); + result[2] = unit_float_to_uchar_clamp(color[2] * alpha_inv); + result[3] = unit_float_to_uchar_clamp(color[3]); } } -- cgit v1.2.3