From 142d8e5125df70f30ba0e8ea939be4ee6644e3d7 Mon Sep 17 00:00:00 2001 From: Pekka Paalanen Date: Wed, 18 May 2022 14:54:56 +0300 Subject: [PATCH] tests/color_util: add RGB diff stat These helpers allow collecting color difference statistics easily. To be used in color-shaper-matrix-test. Signed-off-by: Pekka Paalanen --- tests/color_util.c | 60 ++++++++++++++++++++++++++++++++++++++++++++++ tests/color_util.h | 32 +++++++++++++++++++++++++ 2 files changed, 92 insertions(+) diff --git a/tests/color_util.c b/tests/color_util.c index 86d426f1..f51666dd 100644 --- a/tests/color_util.c +++ b/tests/color_util.c @@ -34,6 +34,7 @@ #include #include "color_util.h" +#include "weston-test-runner.h" #include "shared/helpers.h" static_assert(sizeof(struct color_float) == 4 * sizeof(float), @@ -330,3 +331,62 @@ lcmsMAT3_invert(struct lcmsMAT3 *result, const struct lcmsMAT3 *mat) assert(ret == 0); lcmsMAT3_from_weston_matrix(result, &inv); } + +void +scalar_stat_update(struct scalar_stat *stat, double val, struct color_float *pos) +{ + if (stat->count == 0 || stat->min > val) { + stat->min = val; + stat->min_pos = *pos; + } + + if (stat->count == 0 || stat->max < val) { + stat->max = val; + stat->max_pos = *pos; + } + + stat->sum += val; + stat->count++; +} + +float +scalar_stat_avg(const struct scalar_stat *stat) +{ + return stat->sum / stat->count; +} + +#define RGB888_FMT "(%3u, %3u, %3u)" +#define RGB888_VAL(cf) (unsigned)round((cf).r * 255.0), (unsigned)round((cf).g * 255.0), (unsigned)round((cf).b * 255.0) + +void +scalar_stat_print_rgb8bit(const struct scalar_stat *stat) +{ + testlog(" min %8.5f at " RGB888_FMT "\n", stat->min, RGB888_VAL(stat->min_pos)); + testlog(" max %8.5f at " RGB888_FMT "\n", stat->max, RGB888_VAL(stat->max_pos)); + testlog(" avg %8.5f\n", scalar_stat_avg(stat)); +} + +void +scalar_stat_print_float(const struct scalar_stat *stat) +{ + testlog(" min %11.5g at %.5f\n", stat->min, stat->min_pos.r); + testlog(" max %11.5g at %.5f\n", stat->max, stat->max_pos.r); + testlog(" avg %11.5g\n", scalar_stat_avg(stat)); +} + +void +rgb_diff_stat_update(struct rgb_diff_stat *stat, + struct color_float *ref, struct color_float *val) +{ + unsigned i; + double ssd = 0.0; + + for (i = 0; i < COLOR_CHAN_NUM; i++) { + double diff = val->rgb[i] - ref->rgb[i]; + + scalar_stat_update(&stat->rgb[i], diff, ref); + ssd += diff * diff; + } + + scalar_stat_update(&stat->two_norm, sqrt(ssd), ref); +} diff --git a/tests/color_util.h b/tests/color_util.h index ec442b80..6b6f96d7 100644 --- a/tests/color_util.h +++ b/tests/color_util.h @@ -112,3 +112,35 @@ transfer_fn_invert(enum transfer_fn fn); void lcmsMAT3_invert(struct lcmsMAT3 *result, const struct lcmsMAT3 *mat); + +struct scalar_stat { + double min; + struct color_float min_pos; + + double max; + struct color_float max_pos; + + double sum; + unsigned count; +}; + +struct rgb_diff_stat { + struct scalar_stat rgb[COLOR_CHAN_NUM]; + struct scalar_stat two_norm; +}; + +void +scalar_stat_update(struct scalar_stat *stat, double val, struct color_float *pos); + +float +scalar_stat_avg(const struct scalar_stat *stat); + +void +scalar_stat_print_rgb8bit(const struct scalar_stat *stat); + +void +scalar_stat_print_float(const struct scalar_stat *stat); + +void +rgb_diff_stat_update(struct rgb_diff_stat *stat, + struct color_float *ref, struct color_float *val);