From 0d385ffacbf51ceedea96b0d313263f45a2520ed Mon Sep 17 00:00:00 2001 From: Pekka Paalanen Date: Mon, 13 Jun 2022 14:51:44 +0300 Subject: [PATCH] tests/alpha-blending: move unpremult to color_util More tests are going to need this. The API is changed to work by copy in and copy out to match the other color_util API. Hopefully this makes the caller code easier to read. Signed-off-by: Pekka Paalanen --- tests/alpha-blending-test.c | 20 +++----------------- tests/color_util.c | 18 ++++++++++++++++++ tests/color_util.h | 3 +++ 3 files changed, 24 insertions(+), 17 deletions(-) diff --git a/tests/alpha-blending-test.c b/tests/alpha-blending-test.c index 3f7317f3..2ec2b255 100644 --- a/tests/alpha-blending-test.c +++ b/tests/alpha-blending-test.c @@ -94,20 +94,6 @@ premult_color(uint32_t a, uint32_t r, uint32_t g, uint32_t b) return c; } -static void -unpremult_float(struct color_float *cf) -{ - int i; - - if (cf->a == 0.0f) { - for (i = 0; i < COLOR_CHAN_NUM; i++) - cf->rgb[i] = 0.0f; - } else { - for (i = 0; i < COLOR_CHAN_NUM; i++) - cf->rgb[i] /= cf->a; - } -} - static void fill_alpha_pattern(struct buffer *buf) { @@ -205,9 +191,9 @@ verify_sRGB_blend_a8r8g8b8(uint32_t bg32, uint32_t fg32, uint32_t dst32, bool ok = true; int i; - unpremult_float(&bg); - unpremult_float(&fg); - unpremult_float(&dst); + bg = color_float_unpremult(bg); + fg = color_float_unpremult(fg); + dst = color_float_unpremult(dst); if (space == BLEND_LINEAR) { sRGB_linearize(&bg); diff --git a/tests/color_util.c b/tests/color_util.c index 4bbd4071..c1bf622d 100644 --- a/tests/color_util.c +++ b/tests/color_util.c @@ -280,6 +280,24 @@ sRGB_delinearize(struct color_float *cf) *cf = color_float_apply_curve(TRANSFER_FN_SRGB_EOTF_INVERSE, *cf); } +struct color_float +color_float_unpremult(struct color_float in) +{ + static const struct color_float transparent = { + .r = 0.0f, .g = 0.0f, .b = 0.0f, .a = 0.0f, + }; + struct color_float out; + int i; + + if (in.a == 0.0f) + return transparent; + + for (i = 0; i < COLOR_CHAN_NUM; i++) + out.rgb[i] = in.rgb[i] / in.a; + out.a = in.a; + return out; +} + /* * Returns the result of the matrix-vector multiplication mat * c. */ diff --git a/tests/color_util.h b/tests/color_util.h index e9d931cd..ffcbd7a8 100644 --- a/tests/color_util.h +++ b/tests/color_util.h @@ -107,6 +107,9 @@ process_pixel_using_pipeline(enum transfer_fn pre_curve, const struct color_float *in, struct color_float *out); +struct color_float +color_float_unpremult(struct color_float in); + struct color_float color_float_apply_matrix(const struct lcmsMAT3 *mat, struct color_float c);