From 15d7546b2d478aba25077881d5e84b52fc7ca132 Mon Sep 17 00:00:00 2001 From: Vitaly Prosyak Date: Tue, 9 Nov 2021 14:02:01 -0500 Subject: [PATCH] tests: refactoring alpha-blending No functional change. Moved color processing functions into shared files which can be used between different tests. Signed-off-by: Vitaly Prosyak --- tests/alpha-blending-test.c | 86 +++++++------------------------------ tests/color_util.c | 84 ++++++++++++++++++++++++++++++++++++ tests/color_util.h | 42 ++++++++++++++++++ tests/meson.build | 2 + 4 files changed, 144 insertions(+), 70 deletions(-) create mode 100644 tests/color_util.c create mode 100644 tests/color_util.h diff --git a/tests/alpha-blending-test.c b/tests/alpha-blending-test.c index 8aa2fcdd..e2916be9 100644 --- a/tests/alpha-blending-test.c +++ b/tests/alpha-blending-test.c @@ -1,5 +1,6 @@ /* * Copyright 2020 Collabora, Ltd. + * Copyright 2021 Advanced Micro Devices, Inc. * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the @@ -29,6 +30,7 @@ #include "weston-test-client-helper.h" #include "weston-test-fixture-compositor.h" +#include "color_util.h" struct setup_args { struct fixture_metadata meta; @@ -104,6 +106,20 @@ premult_color(uint32_t a, uint32_t r, uint32_t g, uint32_t b) return c; } +static void +unpremult_float(struct color_float *cf) +{ + if (cf->a == 0.0f) { + cf->r = 0.0f; + cf->g = 0.0f; + cf->b = 0.0f; + } else { + cf->r /= cf->a; + cf->g /= cf->a; + cf->b /= cf->a; + } +} + static void fill_alpha_pattern(struct buffer *buf) { @@ -137,76 +153,6 @@ fill_alpha_pattern(struct buffer *buf) } } -struct color_float { - float r, g, b, a; -}; - -static struct color_float -a8r8g8b8_to_float(uint32_t v) -{ - struct color_float cf; - - cf.a = ((v >> 24) & 0xff) / 255.f; - cf.r = ((v >> 16) & 0xff) / 255.f; - cf.g = ((v >> 8) & 0xff) / 255.f; - cf.b = ((v >> 0) & 0xff) / 255.f; - - return cf; -} - -static void -unpremult_float(struct color_float *cf) -{ - if (cf->a == 0.0f) { - cf->r = 0.0f; - cf->g = 0.0f; - cf->b = 0.0f; - } else { - cf->r /= cf->a; - cf->g /= cf->a; - cf->b /= cf->a; - } -} - -static float -sRGB_EOTF(float e) -{ - assert(e >= 0.0f); - assert(e <= 1.0f); - - if (e <= 0.04045) - return e / 12.92; - else - return pow((e + 0.055) / 1.055, 2.4); -} - -static void -sRGB_linearize(struct color_float *cf) -{ - cf->r = sRGB_EOTF(cf->r); - cf->g = sRGB_EOTF(cf->g); - cf->b = sRGB_EOTF(cf->b); -} - -static float -sRGB_EOTF_inv(float o) -{ - assert(o >= 0.0f); - assert(o <= 1.0f); - - if (o <= 0.04045 / 12.92) - return o * 12.92; - else - return pow(o, 1.0 / 2.4) * 1.055 - 0.055; -} - -static void -sRGB_delinearize(struct color_float *cf) -{ - cf->r = sRGB_EOTF_inv(cf->r); - cf->g = sRGB_EOTF_inv(cf->g); - cf->b = sRGB_EOTF_inv(cf->b); -} static bool compare_float(float ref, float dst, int x, const char *chan, float *max_diff) diff --git a/tests/color_util.c b/tests/color_util.c new file mode 100644 index 00000000..52250781 --- /dev/null +++ b/tests/color_util.c @@ -0,0 +1,84 @@ +/* + * Copyright 2020 Collabora, Ltd. + * Copyright 2021 Advanced Micro Devices, Inc. + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice (including the + * next paragraph) shall be included in all copies or substantial + * portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS + * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "config.h" +#include +#include "color_util.h" +#include + +static float +sRGB_EOTF(float e) +{ + assert(e >= 0.0f); + assert(e <= 1.0f); + + if (e <= 0.04045) + return e / 12.92; + else + return pow((e + 0.055) / 1.055, 2.4); +} + +static float +sRGB_EOTF_inv(float o) +{ + assert(o >= 0.0f); + assert(o <= 1.0f); + + if (o <= 0.04045 / 12.92) + return o * 12.92; + else + return pow(o, 1.0 / 2.4) * 1.055 - 0.055; +} + + +void +sRGB_linearize(struct color_float *cf) +{ + cf->r = sRGB_EOTF(cf->r); + cf->g = sRGB_EOTF(cf->g); + cf->b = sRGB_EOTF(cf->b); +} + +void +sRGB_delinearize(struct color_float *cf) +{ + cf->r = sRGB_EOTF_inv(cf->r); + cf->g = sRGB_EOTF_inv(cf->g); + cf->b = sRGB_EOTF_inv(cf->b); +} + +struct color_float +a8r8g8b8_to_float(uint32_t v) +{ + struct color_float cf; + + cf.a = ((v >> 24) & 0xff) / 255.f; + cf.r = ((v >> 16) & 0xff) / 255.f; + cf.g = ((v >> 8) & 0xff) / 255.f; + cf.b = ((v >> 0) & 0xff) / 255.f; + + return cf; +} diff --git a/tests/color_util.h b/tests/color_util.h new file mode 100644 index 00000000..a9cfd02d --- /dev/null +++ b/tests/color_util.h @@ -0,0 +1,42 @@ +/* + * Copyright 2020 Collabora, Ltd. + * Copyright 2021 Advanced Micro Devices, Inc. + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice (including the + * next paragraph) shall be included in all copies or substantial + * portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS + * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include + + +struct color_float { + float r, g, b, a; +}; + +void +sRGB_linearize(struct color_float *cf); + +void +sRGB_delinearize(struct color_float *cf); + + +struct color_float +a8r8g8b8_to_float(uint32_t v); diff --git a/tests/meson.build b/tests/meson.build index e3ee1d67..0f70420f 100644 --- a/tests/meson.build +++ b/tests/meson.build @@ -33,6 +33,8 @@ lib_test_client = static_library( weston_test_protocol_c, viewporter_client_protocol_h, viewporter_protocol_c, + 'color_util.h', + 'color_util.c', ], include_directories: common_inc, dependencies: [