From 4012062228489ad96c5db685e8abe938e2c06809 Mon Sep 17 00:00:00 2001 From: Pekka Paalanen Date: Tue, 15 Feb 2022 13:37:32 +0200 Subject: [PATCH] tests: add rgb[] alias in color_float Individual struct fields are inconvenient to index into, yet most operations on a color just repeat the same for each of RGB channel. Being able to index into RGB avoids repeating the same code for each channel. Alpha channel is left as separate, since it is almost never handled the same as RGB. The union keeps the old .r, .g and .b addressing working. The static asserts ensure the aliasing is correct. For demonstration, two simple functions in color_util.c are converted. Unfortunately initializers need to be corrected everywhere. Field .a is not explicitly initialized because it is unused in these cases. This change should make code easier to read. This change requires gnu99 or c11 standard. gnu99 is already the default in top-level meson.build. Signed-off-by: Pekka Paalanen --- tests/alpha-blending-test.c | 2 +- tests/color-shaper-matrix-test.c | 4 ++-- tests/color_util.c | 24 ++++++++++++++++++------ tests/color_util.h | 15 ++++++++++++++- 4 files changed, 35 insertions(+), 10 deletions(-) diff --git a/tests/alpha-blending-test.c b/tests/alpha-blending-test.c index e2916be9..b3f8bdee 100644 --- a/tests/alpha-blending-test.c +++ b/tests/alpha-blending-test.c @@ -299,7 +299,7 @@ check_blend_pattern(struct buffer *bg, struct buffer *fg, struct buffer *shot, uint32_t *bg_row = get_middle_row(bg); uint32_t *fg_row = get_middle_row(fg); uint32_t *shot_row = get_middle_row(shot); - struct color_float max_diff = { 0.0f, 0.0f, 0.0f, 0.0f }; + struct color_float max_diff = { .rgb = { 0.0f, 0.0f, 0.0f } }; bool ret = true; int x; diff --git a/tests/color-shaper-matrix-test.c b/tests/color-shaper-matrix-test.c index 0c04e070..331336a3 100644 --- a/tests/color-shaper-matrix-test.c +++ b/tests/color-shaper-matrix-test.c @@ -193,7 +193,7 @@ gen_ramp_rgb(const struct image_header *header, int bitwidth, int width_bar) hue_index = MIN(hue_index, num_hues - 1); for (x = 0; x < header->width; x++) { - struct color_float rgb = { 0, 0, 0 }; + struct color_float rgb = { .rgb = { 0, 0, 0 } }; value = (float)x / (float)(header->width - 1); @@ -360,7 +360,7 @@ process_pipeline_comparison(const struct image_header *src, const struct setup_args * arg) { const float max_pixel_value = 255.0; - struct color_float max_diff_pipeline = { 0.0f, 0.0f, 0.0f, 0.0f }; + struct color_float max_diff_pipeline = { .rgb = { 0.0f, 0.0f, 0.0f } }; float max_allow_diff = arg->pipeline.tolerance / max_pixel_value; float max_err = 0; float f_max_err = 0; diff --git a/tests/color_util.c b/tests/color_util.c index 511b8d9e..bc5ca801 100644 --- a/tests/color_util.c +++ b/tests/color_util.c @@ -29,8 +29,18 @@ #include #include #include +#include #include "shared/helpers.h" +static_assert(sizeof(struct color_float) == 4 * sizeof(float), + "unexpected padding in struct color_float"); +static_assert(offsetof(struct color_float, r) == offsetof(struct color_float, rgb[COLOR_CHAN_R]), + "unexpected offset for struct color_float::r"); +static_assert(offsetof(struct color_float, g) == offsetof(struct color_float, rgb[COLOR_CHAN_G]), + "unexpected offset for struct color_float::g"); +static_assert(offsetof(struct color_float, b) == offsetof(struct color_float, rgb[COLOR_CHAN_B]), + "unexpected offset for struct color_float::b"); + struct color_tone_curve { enum transfer_fn fn; enum transfer_fn inv_fn; @@ -154,9 +164,10 @@ Power2_4_EOTF_inv(float o) 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); + int i; + + for (i = 0; i < COLOR_CHAN_NUM; i++) + cf->rgb[i] = sRGB_EOTF(cf->rgb[i]); } static float @@ -191,9 +202,10 @@ apply_tone_curve(enum transfer_fn fn, float r) 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); + int i; + + for (i = 0; i < COLOR_CHAN_NUM; i++) + cf->rgb[i] = sRGB_EOTF_inv(cf->rgb[i]); } struct color_float diff --git a/tests/color_util.h b/tests/color_util.h index 96f50049..f822d00a 100644 --- a/tests/color_util.h +++ b/tests/color_util.h @@ -27,8 +27,21 @@ #include #include +enum color_chan_index { + COLOR_CHAN_R = 0, + COLOR_CHAN_G, + COLOR_CHAN_B, + COLOR_CHAN_NUM +}; + struct color_float { - float r, g, b, a; + union { + float rgb[COLOR_CHAN_NUM]; + struct { + float r, g, b; + }; + }; + float a; }; struct lcmsVEC3 {