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 <pekka.paalanen@collabora.com>
dev
Pekka Paalanen 3 years ago committed by Pekka Paalanen
parent 2ac6b6b084
commit 4012062228
  1. 2
      tests/alpha-blending-test.c
  2. 4
      tests/color-shaper-matrix-test.c
  3. 24
      tests/color_util.c
  4. 15
      tests/color_util.h

@ -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 *bg_row = get_middle_row(bg);
uint32_t *fg_row = get_middle_row(fg); uint32_t *fg_row = get_middle_row(fg);
uint32_t *shot_row = get_middle_row(shot); 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; bool ret = true;
int x; int x;

@ -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); hue_index = MIN(hue_index, num_hues - 1);
for (x = 0; x < header->width; x++) { 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); 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 struct setup_args * arg)
{ {
const float max_pixel_value = 255.0; 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_allow_diff = arg->pipeline.tolerance / max_pixel_value;
float max_err = 0; float max_err = 0;
float f_max_err = 0; float f_max_err = 0;

@ -29,8 +29,18 @@
#include <assert.h> #include <assert.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <stddef.h>
#include "shared/helpers.h" #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 { struct color_tone_curve {
enum transfer_fn fn; enum transfer_fn fn;
enum transfer_fn inv_fn; enum transfer_fn inv_fn;
@ -154,9 +164,10 @@ Power2_4_EOTF_inv(float o)
void void
sRGB_linearize(struct color_float *cf) sRGB_linearize(struct color_float *cf)
{ {
cf->r = sRGB_EOTF(cf->r); int i;
cf->g = sRGB_EOTF(cf->g);
cf->b = sRGB_EOTF(cf->b); for (i = 0; i < COLOR_CHAN_NUM; i++)
cf->rgb[i] = sRGB_EOTF(cf->rgb[i]);
} }
static float static float
@ -191,9 +202,10 @@ apply_tone_curve(enum transfer_fn fn, float r)
void void
sRGB_delinearize(struct color_float *cf) sRGB_delinearize(struct color_float *cf)
{ {
cf->r = sRGB_EOTF_inv(cf->r); int i;
cf->g = sRGB_EOTF_inv(cf->g);
cf->b = sRGB_EOTF_inv(cf->b); for (i = 0; i < COLOR_CHAN_NUM; i++)
cf->rgb[i] = sRGB_EOTF_inv(cf->rgb[i]);
} }
struct color_float struct color_float

@ -27,8 +27,21 @@
#include <stdint.h> #include <stdint.h>
#include <stdbool.h> #include <stdbool.h>
enum color_chan_index {
COLOR_CHAN_R = 0,
COLOR_CHAN_G,
COLOR_CHAN_B,
COLOR_CHAN_NUM
};
struct color_float { struct color_float {
float r, g, b, a; union {
float rgb[COLOR_CHAN_NUM];
struct {
float r, g, b;
};
};
float a;
}; };
struct lcmsVEC3 { struct lcmsVEC3 {

Loading…
Cancel
Save