Commit 9384338e authored by Debarshi Ray's avatar Debarshi Ray

shadows-highlights-correction: Simplify code

Since the code already scales the colour channels to [0.0, 1.0], it is
quite obvious what the maximum, half-maximum and double-maximum values
are. Moreover, variables like lmax aren't used consistently either.
eg., the code had both "1.0f - la" and "lmax - la". So, it's not clear
whether they were making it easy to understand overall algorithm, which
is otherwise undocumented.

Note that Darktable was using some of these variables to clamp the
output within [0.0, 1.0], which isn't relevant because the GEGL
operation always generates unbounded output.
parent ea4f1072
......@@ -8,10 +8,6 @@ float3 overlay(const float3 in_a,
/* a contains underlying image; b contains mask */
const float3 scale = (float3)(100.0f, 128.0f, 128.0f);
const float lmax = 1.0f;
const float halfmax = 0.5f;
const float doublemax = 2.0f;
float3 a = in_a / scale;
float3 b = in_b / scale;
......@@ -21,7 +17,7 @@ float3 overlay(const float3 in_a,
while(opacity2 > 0.0f)
{
float la = a.x;
float lb = (b.x - halfmax) * sign(opacity)*sign(lmax - la) + halfmax;
float lb = (b.x - 0.5f) * sign(opacity)*sign(1.0f - la) + 0.5f;
float lref = copysign(fabs(la) > low_approximation ? 1.0f/fabs(la) : 1.0f/low_approximation, la);
float href = copysign(fabs(1.0f - la) > low_approximation ? 1.0f/fabs(1.0f - la) : 1.0f/low_approximation, 1.0f - la);
......@@ -29,7 +25,7 @@ float3 overlay(const float3 in_a,
float optrans = chunk * transform;
opacity2 -= 1.0f;
a.x = la * (1.0f - optrans) + (la > halfmax ? lmax - (lmax - doublemax * (la - halfmax)) * (lmax-lb) : doublemax * la * lb) * optrans;
a.x = la * (1.0f - optrans) + (la > 0.5f ? 1.0f - (1.0f - 2.0f * (la - 0.5f)) * (1.0f-lb) : 2.0f * la * lb) * optrans;
a.y = a.y * (1.0f - optrans) + (a.y + b.y) * (a.x*lref * ccorrect + (1.0f - a.x)*href * (1.0f - ccorrect)) * optrans;
a.z = a.z * (1.0f - optrans) + (a.z + b.z) * (a.x*lref * ccorrect + (1.0f - a.x)*href * (1.0f - ccorrect)) * optrans;
}
......
......@@ -95,11 +95,6 @@ process (GeglOperation *operation,
gfloat highlights_ccorrect;
gfloat highlights_ccorrect_100 = (gfloat) o->highlights_ccorrect / 100.0f;
gfloat max[4] = { 1.0f, 1.0f, 1.0f, 1.0f };
gfloat min[4] = { 0.0f, -1.0f, -1.0f, 0.0f };
gfloat lmax = max[0] + fabsf(min[0]);
gfloat halfmax = lmax / 2.0f;
gfloat doublemax = lmax * 2.0f;
gfloat low_approximation = 0.01f;
compress = fminf ((gfloat) o->compress / 100.0f, 0.99f);
......@@ -154,7 +149,7 @@ process (GeglOperation *operation,
gfloat chunk, optrans;
gfloat la = ta[0];
gfloat lb = (tb[0] - halfmax) * SIGN(-highlights) * SIGN(lmax - la) + halfmax;
gfloat lb = (tb[0] - 0.5f) * SIGN(-highlights) * SIGN(1.0f - la) + 0.5f;
lref = copysignf(fabsf(la) > low_approximation ? 1.0f / fabsf(la) : 1.0f / low_approximation, la);
href = copysignf(
......@@ -165,8 +160,7 @@ process (GeglOperation *operation,
highlights2 -= 1.0f;
ta[0] = la * (1.0 - optrans)
+ (la > halfmax ? lmax - (lmax - doublemax * (la - halfmax)) * (lmax - lb) : doublemax * la
* lb) * optrans;
+ (la > 0.5f ? 1.0f - (1.0f - 2.0f * (la - 0.5f)) * (1.0f - lb) : 2.0f * la * lb) * optrans;
ta[1] = ta[1] * (1.0f - optrans)
+ (ta[1] + tb[1]) * (ta[0] * lref * (1.0f - highlights_ccorrect)
......@@ -186,7 +180,7 @@ process (GeglOperation *operation,
gfloat chunk, optrans;
gfloat la = ta[0];
gfloat lb = (tb[0] - halfmax) * SIGN(shadows) * SIGN(lmax - la) + halfmax;
gfloat lb = (tb[0] - 0.5f) * SIGN(shadows) * SIGN(1.0f - la) + 0.5f;
lref = copysignf(fabsf(la) > low_approximation ? 1.0f / fabsf(la) : 1.0f / low_approximation, la);
href = copysignf(
fabsf(1.0f - la) > low_approximation ? 1.0f / fabsf(1.0f - la) : 1.0f / low_approximation, 1.0f - la);
......@@ -196,8 +190,7 @@ process (GeglOperation *operation,
shadows2 -= 1.0f;
ta[0] = la * (1.0 - optrans)
+ (la > halfmax ? lmax - (lmax - doublemax * (la - halfmax)) * (lmax - lb) : doublemax * la
* lb) * optrans;
+ (la > 0.5f ? 1.0f - (1.0f - 2.0f * (la - 0.5f)) * (1.0f - lb) : 2.0f * la * lb) * optrans;
ta[1] = ta[1] * (1.0f - optrans)
+ (ta[1] + tb[1]) * (ta[0] * lref * shadows_ccorrect
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment