Merge branch 'wip/otte/for-main' into 'main'

color: Handle negative values in all transfer functions

See merge request GNOME/gtk!7609
This commit is contained in:
Benjamin Otte 2024-08-13 23:52:32 +00:00
commit f3c8c5a9c0
3 changed files with 40 additions and 31 deletions

View File

@ -20,11 +20,17 @@
* and tests, and must not include other headers.
*/
static inline int
sign (float v)
{
return v < 0 ? -1 : 1;
}
static inline float
srgb_oetf (float v)
{
if (v > 0.0031308f)
return 1.055f * powf (v, 1.f / 2.4f) - 0.055f;
if (fabsf (v) > 0.0031308f)
return 1.055f * sign (v) * powf (fabsf (v), 1.f / 2.4f) - 0.055f;
else
return 12.92f * v;
}
@ -32,8 +38,8 @@ srgb_oetf (float v)
static inline float
srgb_eotf (float v)
{
if (v >= 0.04045f)
return powf (((v + 0.055f) / (1.f + 0.055f)), 2.4f);
if (fabsf (v) >= 0.04045f)
return sign (v) * powf (((fabsf (v) + 0.055f) / (1.f + 0.055f)), 2.4f);
else
return v / 12.92f;
}
@ -41,25 +47,25 @@ srgb_eotf (float v)
static inline float
gamma22_oetf (float v)
{
return powf (v, 1.f / 2.2f);
return sign (v) * powf (fabsf (v), 1.f / 2.2f);
}
static inline float
gamma22_eotf (float v)
{
return powf (v, 2.2f);
return sign (v) * powf (fabsf (v), 2.2f);
}
static inline float
gamma28_oetf (float v)
{
return powf (v, 1.f / 2.8f);
return sign (v) * powf (fabsf (v), 1.f / 2.8f);
}
static inline float
gamma28_eotf (float v)
{
return powf (v, 2.8f);
return sign (v) * powf (fabsf (v), 2.8f);
}
static inline float
@ -71,9 +77,10 @@ pq_eotf (float v)
float c2 = 2413.0 / (1 << 7);
float c3 = 2392.0 / (1 << 7);
float x = powf (MAX ((powf (v, minv) - c1), 0) / (c2 - (c3 * (powf (v, minv)))), ninv);
float x = powf (fabsf (v), minv);
x = powf (MAX ((x - c1), 0) / (c2 - (c3 * x)), ninv);
return x * 10000 / 203.0;
return sign (v) * x * 10000 / 203.0;
}
static inline float
@ -86,7 +93,8 @@ pq_oetf (float v)
float c2 = 2413.0 / (1 << 7);
float c3 = 2392.0 / (1 << 7);
return powf (((c1 + (c2 * powf (x, n))) / (1 + (c3 * powf (x, n)))), m);
x = powf (fabsf (x), n);
return sign (v) * powf (((c1 + (c2 * x)) / (1 + (c3 * x))), m);
}
static inline float
@ -95,10 +103,10 @@ bt709_eotf (float v)
const float a = 1.099;
const float d = 0.0812;
if (v < d)
if (fabsf (v) < d)
return v / 4.5f;
else
return powf ((v + (a - 1)) / a, 1 / 0.45f);
return sign (v) * powf ((fabsf (v) + (a - 1)) / a, 1 / 0.45f);
}
static inline float
@ -107,10 +115,10 @@ bt709_oetf (float v)
const float a = 1.099;
const float b = 0.018;
if (v < b)
if (fabsf (v) < b)
return v * 4.5f;
else
return a * powf (v, 0.45f) - (a - 1);
return a * sign (v) * powf (fabsf (v), 0.45f) - (a - 1);
}
static inline float

View File

@ -53,8 +53,8 @@ output_color_alpha (vec4 color,
float
srgb_eotf (float v)
{
if (v >= 0.04045)
return pow (((v + 0.055) / (1.0 + 0.055)), 2.4);
if (abs (v) >= 0.04045)
return sign (v) * pow (((abs (v) + 0.055) / (1.0 + 0.055)), 2.4);
else
return v / 12.92;
}
@ -62,8 +62,8 @@ srgb_eotf (float v)
float
srgb_oetf (float v)
{
if (v > 0.0031308)
return 1.055 * pow (v, 1.0 / 2.4) - 0.055;
if (abs (v) > 0.0031308)
return 1.055 * sign (v) * pow (abs (v), 1.0 / 2.4) - 0.055;
else
return 12.92 * v;
}
@ -77,9 +77,10 @@ pq_eotf (float v)
const float c2 = 2413.0 / 128.0;
const float c3 = 2392.0 / 128.0;
float x = pow (max ((pow (v, minv) - c1), 0.0) / (c2 - (c3 * (pow (v, minv)))), ninv);
float x = pow (abs (v), minv);
x = pow (max (x - c1, 0.0) / (c2 - (c3 * x)), ninv);
return x * 10000.0 / 203.0;
return sign (v) * x * 10000.0 / 203.0;
}
float
@ -91,9 +92,9 @@ pq_oetf (float v)
const float c2 = 2413.0 / 128.0;
const float c3 = 2392.0 / 128.0;
float x = v * 203.0 / 10000.0;
float x = pow (abs (v) * 203.0 / 10000.0, n);
return pow (((c1 + (c2 * pow (x, n))) / (1.0 + (c3 * pow (x, n)))), m);
return sign (v) * pow (((c1 + (c2 * x)) / (1.0 + (c3 * x))), m);
}
/* Note that these matrices are transposed from the C version */

View File

@ -161,43 +161,43 @@ run (out vec2 pos)
float
bt709_eotf (float v)
{
if (v < 0.081)
if (abs (v) < 0.081)
return v / 4.5;
else
return pow ((v + 0.099) / 1.099, 1.0/0.45);
return sign (v) * pow ((abs (v) + 0.099) / 1.099, 1.0/0.45);
}
float
bt709_oetf (float v)
{
if (v < 0.081)
if (abs (v) < 0.081)
return v * 4.5;
else
return 1.099 * pow (v, 0.45) - 0.099;
return 1.099 * sign (v) * pow (abs (v), 0.45) - 0.099;
}
float
gamma22_oetf (float v)
{
return pow (v, 1.0 / 2.2);
return sign (v) * pow (abs (v), 1.0 / 2.2);
}
float
gamma22_eotf (float v)
{
return pow (v, 2.2);
return sign (v) * pow (abs (v), 2.2);
}
float
gamma28_oetf (float v)
{
return pow (v, 1.0 / 2.8);
return sign (v) * pow (abs (v), 1.0 / 2.8);
}
float
gamma28_eotf (float v)
{
return pow (v, 2.8);
return sign (v) * pow (abs (v), 2.8);
}
float