mirror of
https://gitlab.freedesktop.org/wayland/weston.git
synced 2025-12-27 10:30:17 +01:00
color: run vec3 through weston_color_curve_sample()
Future development will need to evaluate pipelines with curves and matrices. Such pipelines naturally operate on vec3, as matrices cannot be operated one channel at a time. Make weston_color_curve_sample() operate on arrays of vec3. Its currently only caller, weston_color_curve_to_3x1D_LUT(), is modified to employ a temporary array for the API impedance mismatch. This workaround will be removed later as weston_color_curve_to_3x1D_LUT() itself will be converted to operate on vec3 arrays. weston_v3f_array_to_planar() documentation was generated with AI. weston_color_curve_sample() is restructured a little bit, attempting to make it simpler to read. color-operations.h gets the #includes needed to make it self-standing. Assisted-by: Github Copilot (Claude Sonnet 3.5) Signed-off-by: Pekka Paalanen <pekka.paalanen@collabora.com>
This commit is contained in:
parent
e87eb19877
commit
120b88aa0a
4 changed files with 143 additions and 104 deletions
|
|
@ -30,6 +30,7 @@ extern "C" {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
#include <stddef.h>
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
|
|
||||||
#include <libweston/linalg-types.h>
|
#include <libweston/linalg-types.h>
|
||||||
|
|
@ -95,6 +96,37 @@ weston_v3f_dot_v3f(struct weston_vec3f a, struct weston_vec3f b)
|
||||||
return a.x * b.x + a.y * b.y + a.z * b.z;
|
return a.x * b.x + a.y * b.y + a.z * b.z;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert an array of vec3f into planar format
|
||||||
|
*
|
||||||
|
* Takes an array of RGB triplets and converts it into a planar format where all R
|
||||||
|
* values come first, then all G values, then all B values.
|
||||||
|
*
|
||||||
|
* \param planar The destination array for planar data, must have space for 3 * len floats
|
||||||
|
* \param arr Array of RGB triplets as vec3f structs
|
||||||
|
* \param len Number of RGB triplets in the array
|
||||||
|
*/
|
||||||
|
static inline void
|
||||||
|
weston_v3f_array_to_planar(float *planar, const struct weston_vec3f *arr, size_t len)
|
||||||
|
{
|
||||||
|
size_t i;
|
||||||
|
|
||||||
|
for (i = 0; i < len; i++) {
|
||||||
|
planar[i ] = arr[i].r;
|
||||||
|
planar[i + len] = arr[i].g;
|
||||||
|
planar[i + 2 * len] = arr[i].b;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Clamp each element to the range [a, b], replacing NaN with a. */
|
||||||
|
static inline struct weston_vec3f
|
||||||
|
weston_v3f_clamp(struct weston_vec3f v, float a, float b)
|
||||||
|
{
|
||||||
|
return WESTON_VEC3F(v.x >= a ? (v.x <= b ? v.x : b) : a,
|
||||||
|
v.y >= a ? (v.y <= b ? v.y : b) : a,
|
||||||
|
v.z >= a ? (v.z <= b ? v.z : b) : a);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Matrix infinity-norm
|
* Matrix infinity-norm
|
||||||
*
|
*
|
||||||
|
|
|
||||||
|
|
@ -32,21 +32,6 @@
|
||||||
#include "shared/helpers.h"
|
#include "shared/helpers.h"
|
||||||
#include "shared/weston-assert.h"
|
#include "shared/weston-assert.h"
|
||||||
|
|
||||||
/**
|
|
||||||
* Clamp value to [0.0, 1.0], except pass NaN through.
|
|
||||||
*
|
|
||||||
* This function is not intended for hiding NaN.
|
|
||||||
*/
|
|
||||||
static float
|
|
||||||
ensure_unorm(float v)
|
|
||||||
{
|
|
||||||
if (v <= 0.0f)
|
|
||||||
return 0.0f;
|
|
||||||
if (v > 1.0f)
|
|
||||||
return 1.0f;
|
|
||||||
return v;
|
|
||||||
}
|
|
||||||
|
|
||||||
static float
|
static float
|
||||||
linpow(float x, const union weston_color_curve_parametric_chan_data *p)
|
linpow(float x, const union weston_color_curve_parametric_chan_data *p)
|
||||||
{
|
{
|
||||||
|
|
@ -61,18 +46,21 @@ linpow(float x, const union weston_color_curve_parametric_chan_data *p)
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
sample_linpow(const union weston_color_curve_parametric_chan_data *p,
|
sample_linpow(const union weston_color_curve_parametric_data *p,
|
||||||
uint32_t len, bool clamp_input, float *in, float *out)
|
bool clamp_input, const struct weston_vec3f *in,
|
||||||
|
struct weston_vec3f *out, size_t len)
|
||||||
{
|
{
|
||||||
float x;
|
struct weston_vec3f tmp;
|
||||||
unsigned int i;
|
size_t i;
|
||||||
|
|
||||||
for (i = 0; i < len; i++) {
|
for (i = 0; i < len; i++) {
|
||||||
x = in[i];
|
tmp = in[i];
|
||||||
if (clamp_input)
|
if (clamp_input)
|
||||||
x = ensure_unorm(x);
|
tmp = weston_v3f_clamp(tmp, 0.0f, 1.0f);
|
||||||
|
|
||||||
out[i] = linpow(x, p);
|
out[i] = WESTON_VEC3F(linpow(tmp.x, &p->chan[0]),
|
||||||
|
linpow(tmp.y, &p->chan[1]),
|
||||||
|
linpow(tmp.z, &p->chan[2]));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -90,21 +78,43 @@ powlin(float x, const union weston_color_curve_parametric_chan_data *p)
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
sample_powlin(const union weston_color_curve_parametric_chan_data *p,
|
sample_powlin(const union weston_color_curve_parametric_data *p,
|
||||||
uint32_t len, bool clamp_input, float *in, float *out)
|
bool clamp_input, const struct weston_vec3f *in,
|
||||||
|
struct weston_vec3f *out, size_t len)
|
||||||
{
|
{
|
||||||
float x;
|
struct weston_vec3f tmp;
|
||||||
unsigned int i;
|
size_t i;
|
||||||
|
|
||||||
for (i = 0; i < len; i++) {
|
for (i = 0; i < len; i++) {
|
||||||
x = in[i];
|
tmp = in[i];
|
||||||
if (clamp_input)
|
if (clamp_input)
|
||||||
x = ensure_unorm(x);
|
tmp = weston_v3f_clamp(tmp, 0.0f, 1.0f);
|
||||||
|
|
||||||
out[i] = powlin(x, p);
|
out[i] = WESTON_VEC3F(powlin(tmp.x, &p->chan[0]),
|
||||||
|
powlin(tmp.y, &p->chan[1]),
|
||||||
|
powlin(tmp.z, &p->chan[2]));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
sample_parametric(struct weston_compositor *compositor,
|
||||||
|
const struct weston_color_curve_parametric *param,
|
||||||
|
const struct weston_vec3f *in,
|
||||||
|
struct weston_vec3f *out,
|
||||||
|
size_t len)
|
||||||
|
{
|
||||||
|
switch (param->type) {
|
||||||
|
case WESTON_COLOR_CURVE_PARAMETRIC_TYPE_LINPOW:
|
||||||
|
sample_linpow(¶m->params, param->clamped_input, in, out, len);
|
||||||
|
return;
|
||||||
|
case WESTON_COLOR_CURVE_PARAMETRIC_TYPE_POWLIN:
|
||||||
|
sample_powlin(¶m->params, param->clamped_input, in, out, len);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
weston_assert_not_reached(compositor, "unknown parametric color curve");
|
||||||
|
}
|
||||||
|
|
||||||
static float
|
static float
|
||||||
perceptual_quantizer(float x)
|
perceptual_quantizer(float x)
|
||||||
{
|
{
|
||||||
|
|
@ -142,44 +152,52 @@ perceptual_quantizer_inverse(float x)
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
sample_pq(enum weston_tf_direction tf_direction, uint32_t ch, uint32_t len,
|
sample_pq(enum weston_tf_direction tf_direction,
|
||||||
float *in, float *out)
|
const struct weston_vec3f *in,
|
||||||
|
struct weston_vec3f *out,
|
||||||
|
size_t len)
|
||||||
{
|
{
|
||||||
unsigned int i;
|
struct weston_vec3f tmp;
|
||||||
float x;
|
size_t i;
|
||||||
|
|
||||||
for (i = 0; i < len; i++) {
|
switch (tf_direction) {
|
||||||
/**
|
case WESTON_FORWARD_TF:
|
||||||
* PQ and inverse PQ are always clamped, undefined for values
|
for (i = 0; i < len; i++) {
|
||||||
* out of [0, 1] range.
|
tmp = weston_v3f_clamp(in[i], 0.0f, 1.0f);
|
||||||
*/
|
out[i] = WESTON_VEC3F(perceptual_quantizer(tmp.x),
|
||||||
x = ensure_unorm(in[i]);
|
perceptual_quantizer(tmp.y),
|
||||||
|
perceptual_quantizer(tmp.z));
|
||||||
if (tf_direction == WESTON_FORWARD_TF)
|
}
|
||||||
out[i] = perceptual_quantizer(x);
|
break;
|
||||||
else
|
case WESTON_INVERSE_TF:
|
||||||
out[i] = perceptual_quantizer_inverse(x);
|
for (i = 0; i < len; i++) {
|
||||||
|
tmp = weston_v3f_clamp(in[i], 0.0f, 1.0f);
|
||||||
|
out[i] = WESTON_VEC3F(perceptual_quantizer_inverse(tmp.x),
|
||||||
|
perceptual_quantizer_inverse(tmp.y),
|
||||||
|
perceptual_quantizer_inverse(tmp.z));
|
||||||
|
}
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Given a color curve and a channel, sample an input.
|
* Evaluate a color curve on an array
|
||||||
*
|
*
|
||||||
* This handles the parametric curves (LINPOW, POWLIN, etc) and enumerated color
|
* This handles the parametric curves (LINPOW, POWLIN, etc) and enumerated color
|
||||||
* curves. Others should result in failure.
|
* curves. Others result in failure.
|
||||||
*
|
*
|
||||||
* @param compositor The Weston compositor
|
* @param compositor The Weston compositor
|
||||||
* @param curve The color curve to be used to sample
|
* @param curve The color curve to evaluate
|
||||||
* @param ch The curve color channel to sample from
|
* @param in The input array of length @c len .
|
||||||
* @param len The in and out arrays length
|
* @param out The output array of length @c len .
|
||||||
* @param in The input array to sample
|
* @param len The in and out arrays' length.
|
||||||
* @param out The resulting array from sampling
|
*/
|
||||||
* @returns True on success, false otherwise
|
void
|
||||||
*/
|
|
||||||
bool
|
|
||||||
weston_color_curve_sample(struct weston_compositor *compositor,
|
weston_color_curve_sample(struct weston_compositor *compositor,
|
||||||
struct weston_color_curve *curve,
|
struct weston_color_curve *curve,
|
||||||
uint32_t ch, uint32_t len, float *in, float *out)
|
const struct weston_vec3f *in,
|
||||||
|
struct weston_vec3f *out,
|
||||||
|
size_t len)
|
||||||
{
|
{
|
||||||
struct weston_color_curve_parametric parametric;
|
struct weston_color_curve_parametric parametric;
|
||||||
bool ret;
|
bool ret;
|
||||||
|
|
@ -193,20 +211,19 @@ weston_color_curve_sample(struct weston_compositor *compositor,
|
||||||
*/
|
*/
|
||||||
switch (curve->u.enumerated.tf.info->tf) {
|
switch (curve->u.enumerated.tf.info->tf) {
|
||||||
case WESTON_TF_ST2084_PQ:
|
case WESTON_TF_ST2084_PQ:
|
||||||
sample_pq(curve->u.enumerated.tf_direction, ch, len, in, out);
|
sample_pq(curve->u.enumerated.tf_direction, in, out, len);
|
||||||
return true;
|
return;
|
||||||
default:
|
default:
|
||||||
ret = weston_color_curve_enum_get_parametric(compositor,
|
ret = weston_color_curve_enum_get_parametric(compositor,
|
||||||
&curve->u.enumerated,
|
&curve->u.enumerated,
|
||||||
¶metric);
|
¶metric);
|
||||||
if (!ret)
|
weston_assert_true(compositor, ret);
|
||||||
return false;
|
sample_parametric(compositor, ¶metric, in, out, len);
|
||||||
goto param;
|
return;
|
||||||
}
|
}
|
||||||
case WESTON_COLOR_CURVE_TYPE_PARAMETRIC:
|
case WESTON_COLOR_CURVE_TYPE_PARAMETRIC:
|
||||||
/* Parametric curve, let's copy it and we'll handle that below. */
|
sample_parametric(compositor, &curve->u.parametric, in, out, len);
|
||||||
parametric = curve->u.parametric;
|
return;
|
||||||
goto param;
|
|
||||||
case WESTON_COLOR_CURVE_TYPE_IDENTITY:
|
case WESTON_COLOR_CURVE_TYPE_IDENTITY:
|
||||||
weston_assert_not_reached(compositor,
|
weston_assert_not_reached(compositor,
|
||||||
"no need to sample identity");
|
"no need to sample identity");
|
||||||
|
|
@ -216,19 +233,4 @@ weston_color_curve_sample(struct weston_compositor *compositor,
|
||||||
}
|
}
|
||||||
|
|
||||||
weston_assert_not_reached(compositor, "unknown color curve");
|
weston_assert_not_reached(compositor, "unknown color curve");
|
||||||
|
|
||||||
param:
|
|
||||||
/* Sample from parametric curves. */
|
|
||||||
switch (parametric.type) {
|
|
||||||
case WESTON_COLOR_CURVE_PARAMETRIC_TYPE_LINPOW:
|
|
||||||
sample_linpow(¶metric.params.chan[ch],
|
|
||||||
len, parametric.clamped_input, in, out);
|
|
||||||
return true;
|
|
||||||
case WESTON_COLOR_CURVE_PARAMETRIC_TYPE_POWLIN:
|
|
||||||
sample_powlin(¶metric.params.chan[ch],
|
|
||||||
len, parametric.clamped_input, in, out);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
weston_assert_not_reached(compositor, "unknown parametric color curve");
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -27,10 +27,15 @@
|
||||||
#define WESTON_COLOR_OPERATIONS_H
|
#define WESTON_COLOR_OPERATIONS_H
|
||||||
|
|
||||||
#include <libweston/libweston.h>
|
#include <libweston/libweston.h>
|
||||||
|
#include <libweston/linalg-3.h>
|
||||||
|
|
||||||
bool
|
#include "color.h"
|
||||||
|
|
||||||
|
void
|
||||||
weston_color_curve_sample(struct weston_compositor *compositor,
|
weston_color_curve_sample(struct weston_compositor *compositor,
|
||||||
struct weston_color_curve *curve,
|
struct weston_color_curve *curve,
|
||||||
uint32_t ch, uint32_t len, float *in, float *out);
|
const struct weston_vec3f *in,
|
||||||
|
struct weston_vec3f *out,
|
||||||
|
size_t len);
|
||||||
|
|
||||||
#endif /* WESTON_COLOR_OPERATIONS_H */
|
#endif /* WESTON_COLOR_OPERATIONS_H */
|
||||||
|
|
|
||||||
|
|
@ -354,9 +354,9 @@ weston_color_curve_to_3x1D_LUT(struct weston_compositor *compositor,
|
||||||
struct weston_color_curve *curve;
|
struct weston_color_curve *curve;
|
||||||
float divider = lut_size - 1;
|
float divider = lut_size - 1;
|
||||||
const char *step_str;
|
const char *step_str;
|
||||||
float *in, *lut;
|
float *lut;
|
||||||
unsigned int i, ch;
|
struct weston_vec3f *tmp;
|
||||||
bool ret;
|
unsigned int i;
|
||||||
|
|
||||||
switch(step) {
|
switch(step) {
|
||||||
case WESTON_COLOR_CURVE_STEP_PRE:
|
case WESTON_COLOR_CURVE_STEP_PRE:
|
||||||
|
|
@ -389,30 +389,30 @@ weston_color_curve_to_3x1D_LUT(struct weston_compositor *compositor,
|
||||||
"result in bad precision\n", xform->id, step_str);
|
"result in bad precision\n", xform->id, step_str);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
lut = calloc(lut_size, 3 * sizeof *lut);
|
||||||
|
tmp = calloc(lut_size, sizeof *tmp);
|
||||||
|
if (!lut || !tmp) {
|
||||||
|
/* lut_size could be big. */
|
||||||
|
free(lut);
|
||||||
|
free(tmp);
|
||||||
|
str_printf(err_msg, "Out of memory");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
switch(curve->type) {
|
switch(curve->type) {
|
||||||
case WESTON_COLOR_CURVE_TYPE_LUT_3x1D:
|
case WESTON_COLOR_CURVE_TYPE_LUT_3x1D:
|
||||||
lut = xzalloc(3 * lut_size * sizeof(*lut));
|
|
||||||
curve->u.lut_3x1d.fill_in(xform, lut, lut_size);
|
curve->u.lut_3x1d.fill_in(xform, lut, lut_size);
|
||||||
|
free(tmp);
|
||||||
return lut;
|
return lut;
|
||||||
case WESTON_COLOR_CURVE_TYPE_ENUM:
|
case WESTON_COLOR_CURVE_TYPE_ENUM:
|
||||||
case WESTON_COLOR_CURVE_TYPE_PARAMETRIC:
|
case WESTON_COLOR_CURVE_TYPE_PARAMETRIC:
|
||||||
lut = xzalloc(3 * lut_size * sizeof(*lut));
|
for (i = 0; i < lut_size; i++) {
|
||||||
in = xzalloc(lut_size * sizeof(*lut));
|
float x = (float)i / divider;
|
||||||
for (i = 0; i < lut_size; i++)
|
tmp[i] = WESTON_VEC3F(x, x, x);
|
||||||
in[i] = (float)i / divider;
|
|
||||||
for (ch = 0; ch < 3; ch++) {
|
|
||||||
ret = weston_color_curve_sample(compositor, curve, ch, lut_size,
|
|
||||||
in, &lut[ch * lut_size]);
|
|
||||||
if (!ret) {
|
|
||||||
free(lut);
|
|
||||||
lut = NULL;
|
|
||||||
str_printf(err_msg, "can't create color LUT from xform (id %u) " \
|
|
||||||
"%s-curve, failed to sample color curve",
|
|
||||||
xform->id, step_str);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
free(in);
|
weston_color_curve_sample(compositor, curve, tmp, tmp, lut_size);
|
||||||
|
weston_v3f_array_to_planar(lut, tmp, lut_size);
|
||||||
|
free(tmp);
|
||||||
return lut;
|
return lut;
|
||||||
case WESTON_COLOR_CURVE_TYPE_IDENTITY:
|
case WESTON_COLOR_CURVE_TYPE_IDENTITY:
|
||||||
weston_assert_not_reached(compositor,
|
weston_assert_not_reached(compositor,
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue