Enhanced version of the "L*a*b* regions" color toning, adding mask blur and ASC-CDL controls

This commit is contained in:
Alberto Griggio
2018-11-22 09:45:59 +01:00
parent 655bdfd7e7
commit 73898e1a3f
6 changed files with 306 additions and 40 deletions

View File

@@ -143,8 +143,12 @@ BENCHFUN
}
for (int i = begin_idx; i < end_idx; ++i) {
rtengine::guidedFilter(guide, abmask[i], abmask[i], max(int(4 / scale + 0.5), 1), 0.001, multiThread);
rtengine::guidedFilter(guide, Lmask[i], Lmask[i], max(int(25 / scale + 0.5), 1), 0.0001, multiThread);
float blur = params->colorToning.labregions[i].maskBlur;
blur = blur < 0.f ? -1.f/blur : 1.f + blur;
int r1 = max(int(4 / scale * blur + 0.5), 1);
int r2 = max(int(25 / scale * blur + 0.5), 1);
rtengine::guidedFilter(guide, abmask[i], abmask[i], r1, 0.001, multiThread);
rtengine::guidedFilter(guide, Lmask[i], Lmask[i], r2, 0.0001, multiThread);
}
if (show_mask_idx >= 0) {
@@ -166,21 +170,115 @@ BENCHFUN
const auto abcoord =
[](float x) -> float
{
return 12000.f * SGN(x) * xlog2lin(std::abs(x), 4.f);
return /*12000.f **/ SGN(x) * xlog2lin(std::abs(x), 4.f);
};
float abca[n];
float abcb[n];
float rs[n];
float rl[n];
float slope[n];
float offset[n];
float power[n];
int channel[n];
for (int i = 0; i < n; ++i) {
auto &r = params->colorToning.labregions[i];
abca[i] = abcoord(r.a);
abcb[i] = abcoord(r.b);
rs[i] = 1.f + r.saturation / 100.f;
rl[i] = 1.f + r.lightness / 500.f;
rs[i] = 1.f + r.saturation / (SGN(r.saturation) > 0 ? 50.f : 100.f);
slope[i] = r.slope;
offset[i] = r.offset;
power[i] = r.power;
channel[i] = r.channel;
}
TMatrix ws = ICCStore::getInstance()->workingSpaceMatrix(params->icm.workingProfile);
TMatrix iws = ICCStore::getInstance()->workingSpaceInverseMatrix(params->icm.workingProfile);
const auto CDL =
[=](float &l, float &a, float &b, float slope, float offset, float power, float saturation) -> void
{
if (slope != 1.f || offset != 0.f || power != 1.f || saturation != 1.f) {
float rgb[3];
float x, y, z;
Color::Lab2XYZ(l, a, b, x, y, z);
Color::xyz2rgb(x, y, z, rgb[0], rgb[1], rgb[2], iws);
for (int i = 0; i < 3; ++i) {
rgb[i] = (pow_F(max((rgb[i] / 65535.f) * slope + offset, 0.f), power)) * 65535.f;
}
if (saturation != 1.f) {
float Y = Color::rgbLuminance(rgb[0], rgb[1], rgb[2], ws);
for (int i = 0; i < 3; ++i) {
rgb[i] = max(Y + saturation * (rgb[i] - Y), 0.f);
}
}
Color::rgbxyz(rgb[0], rgb[1], rgb[2], x, y, z, ws);
Color::XYZ2Lab(x, y, z, l, a, b);
}
};
const auto chan =
[=](float prev_l, float prev_a, float prev_b, float &l, float &a, float &b, int channel) -> void
{
if (channel >= 0) {
float prev_rgb[3];
float rgb[3];
float x, y, z;
Color::Lab2XYZ(l, a, b, x, y, z);
Color::xyz2rgb(x, y, z, rgb[0], rgb[1], rgb[2], iws);
Color::Lab2XYZ(prev_l, prev_a, prev_b, x, y, z);
Color::xyz2rgb(x, y, z, prev_rgb[0], prev_rgb[1], prev_rgb[2], iws);
prev_rgb[channel] = rgb[channel];
Color::rgbxyz(prev_rgb[0], prev_rgb[1], prev_rgb[2], x, y, z, ws);
Color::XYZ2Lab(x, y, z, l, a, b);
}
};
#ifdef __SSE2__
const auto CDL_v =
[=](vfloat &l, vfloat &a, vfloat &b, float slope, float offset, float power, float saturation) -> void
{
if (slope != 1.f || offset != 0.f || power != 1.f || saturation != 1.f) {
float ll[4];
float aa[4];
float bb[4];
STVFU(ll[0], l);
STVFU(aa[0], a);
STVFU(bb[0], b);
for (int i = 0; i < 4; ++i) {
CDL(ll[i], aa[i], bb[i], slope, offset, power, saturation);
}
l = LVFU(ll[0]);
a = LVFU(aa[0]);
b = LVFU(bb[0]);
}
};
const auto chan_v =
[=](vfloat prev_l, vfloat prev_a, vfloat prev_b, vfloat &l, vfloat &a, vfloat &b, int channel) -> void
{
if (channel >= 0) {
float ll[4];
float aa[4];
float bb[4];
STVFU(ll[0], l);
STVFU(aa[0], a);
STVFU(bb[0], b);
float prev_ll[4];
float prev_aa[4];
float prev_bb[4];
STVFU(prev_ll[0], prev_l);
STVFU(prev_aa[0], prev_a);
STVFU(prev_bb[0], prev_b);
for (int i = 0; i < 4; ++i) {
chan(prev_ll[i], prev_aa[i], prev_bb[i], ll[i], aa[i], bb[i], channel);
}
l = LVFU(ll[0]);
a = LVFU(aa[0]);
b = LVFU(bb[0]);
}
};
#endif
#ifdef _OPENMP
#pragma omp parallel if (multiThread)
#endif
@@ -188,7 +286,6 @@ BENCHFUN
#ifdef __SSE2__
vfloat c42000v = F2V(42000.f);
vfloat cm42000v = F2V(-42000.f);
vfloat c32768v = F2V(32768.f);
#endif
#ifdef _OPENMP
#pragma omp for
@@ -203,10 +300,12 @@ BENCHFUN
for (int i = 0; i < n; ++i) {
vfloat blendv = LVFU(abmask[i][y][x]);
vfloat sv = F2V(rs[i]);
vfloat a_newv = vclampf(sv * (av + F2V(abca[i])), cm42000v, c42000v);
vfloat b_newv = vclampf(sv * (bv + F2V(abcb[i])), cm42000v, c42000v);
vfloat l_newv = vclampf(lv * F2V(rl[i]), ZEROV, c32768v);
vfloat l_newv = lv;
vfloat a_newv = vclampf(av + lv * F2V(abca[i]), cm42000v, c42000v);
vfloat b_newv = vclampf(bv + lv * F2V(abcb[i]), cm42000v, c42000v);
CDL_v(l_newv, a_newv, b_newv, slope[i], offset[i], power[i], rs[i]);
l_newv = vmaxf(l_newv, ZEROV);
chan_v(lv, av, bv, l_newv, a_newv, b_newv, channel[i]);
lv = vintpf(LVFU(Lmask[i][y][x]), l_newv, lv);
av = vintpf(blendv, a_newv, av);
bv = vintpf(blendv, b_newv, bv);
@@ -223,10 +322,12 @@ BENCHFUN
for (int i = 0; i < n; ++i) {
float blend = abmask[i][y][x];
float s = rs[i];
float a_new = LIM(s * (a + abca[i]), -42000.f, 42000.f);
float b_new = LIM(s * (b + abcb[i]), -42000.f, 42000.f);
float l_new = LIM(l * rl[i], 0.f, 32768.f);
float l_new = l;
float a_new = LIM(a + l * abca[i], -42000.f, 42000.f);
float b_new = LIM(b + l * abcb[i], -42000.f, 42000.f);
CDL(l_new, a_new, b_new, slope[i], offset[i], power[i], rs[i]);
l_new = max(l_new, 0.f);
chan(l, a, b, l_new, a_new, b_new, channel[i]);
l = intp(Lmask[i][y][x], l_new, l);
a = intp(blend, a_new, a);
b = intp(blend, b_new, b);

View File

@@ -611,7 +611,9 @@ ColorToningParams::LabCorrectionRegion::LabCorrectionRegion():
a(0),
b(0),
saturation(0),
lightness(0),
slope(1),
offset(0),
power(1),
hueMask{
FCT_MinMaxCPoints,
0.166666667,
@@ -644,7 +646,9 @@ ColorToningParams::LabCorrectionRegion::LabCorrectionRegion():
1.,
0.35,
0.35
}
},
maskBlur(0),
channel(ColorToningParams::LabCorrectionRegion::CHAN_ALL)
{
}
@@ -654,10 +658,14 @@ bool ColorToningParams::LabCorrectionRegion::operator==(const LabCorrectionRegio
return a == other.a
&& b == other.b
&& saturation == other.saturation
&& lightness == other.lightness
&& slope == other.slope
&& offset == other.offset
&& power == other.power
&& hueMask == other.hueMask
&& chromaticityMask == other.chromaticityMask
&& lightnessMask == other.lightnessMask;
&& lightnessMask == other.lightnessMask
&& maskBlur == other.maskBlur
&& channel == other.channel;
}
@@ -3479,10 +3487,14 @@ int ProcParams::save(const Glib::ustring& fname, const Glib::ustring& fname2, bo
putToKeyfile("ColorToning", Glib::ustring("LabRegionA_") + n, l.a, keyFile);
putToKeyfile("ColorToning", Glib::ustring("LabRegionB_") + n, l.b, keyFile);
putToKeyfile("ColorToning", Glib::ustring("LabRegionSaturation_") + n, l.saturation, keyFile);
putToKeyfile("ColorToning", Glib::ustring("LabRegionLightness_") + n, l.lightness, keyFile);
putToKeyfile("ColorToning", Glib::ustring("LabRegionSlope_") + n, l.slope, keyFile);
putToKeyfile("ColorToning", Glib::ustring("LabRegionOffset_") + n, l.offset, keyFile);
putToKeyfile("ColorToning", Glib::ustring("LabRegionPower_") + n, l.power, keyFile);
putToKeyfile("ColorToning", Glib::ustring("LabRegionHueMask_") + n, l.hueMask, keyFile);
putToKeyfile("ColorToning", Glib::ustring("LabRegionChromaticityMask_") + n, l.chromaticityMask, keyFile);
putToKeyfile("ColorToning", Glib::ustring("LabRegionLightnessMask_") + n, l.lightnessMask, keyFile);
putToKeyfile("ColorToning", Glib::ustring("LabRegionMaskBlur_") + n, l.maskBlur, keyFile);
putToKeyfile("ColorToning", Glib::ustring("LabRegionChannel_") + n, l.channel, keyFile);
}
}
saveToKeyfile(!pedited || pedited->colorToning.labregionsShowMask, "ColorToning", "LabRegionsShowMask", colorToning.labregionsShowMask, keyFile);
@@ -4869,7 +4881,15 @@ int ProcParams::load(const Glib::ustring& fname, ParamsEdited* pedited)
found = true;
done = false;
}
if (assignFromKeyfile(keyFile, "ColorToning", Glib::ustring("LabRegionLightness_") + n, pedited, cur.lightness, pedited->colorToning.labregions)) {
if (assignFromKeyfile(keyFile, "ColorToning", Glib::ustring("LabRegionSlope_") + n, pedited, cur.slope, pedited->colorToning.labregions)) {
found = true;
done = false;
}
if (assignFromKeyfile(keyFile, "ColorToning", Glib::ustring("LabRegionOffset_") + n, pedited, cur.offset, pedited->colorToning.labregions)) {
found = true;
done = false;
}
if (assignFromKeyfile(keyFile, "ColorToning", Glib::ustring("LabRegionPower_") + n, pedited, cur.power, pedited->colorToning.labregions)) {
found = true;
done = false;
}
@@ -4885,6 +4905,14 @@ int ProcParams::load(const Glib::ustring& fname, ParamsEdited* pedited)
found = true;
done = false;
}
if (assignFromKeyfile(keyFile, "ColorToning", Glib::ustring("LabRegionMaskBlur_") + n, pedited, cur.maskBlur, pedited->colorToning.labregions)) {
found = true;
done = false;
}
if (assignFromKeyfile(keyFile, "ColorToning", Glib::ustring("LabRegionChannel_") + n, pedited, cur.channel, pedited->colorToning.labregions)) {
found = true;
done = false;
}
if (!done) {
lg.emplace_back(cur);
}

View File

@@ -456,13 +456,18 @@ struct ColorToningParams {
static const double LABGRID_CORR_SCALE;
struct LabCorrectionRegion {
enum { CHAN_ALL = -1, CHAN_R, CHAN_G, CHAN_B };
double a;
double b;
double saturation;
double lightness;
double slope;
double offset;
double power;
std::vector<double> hueMask;
std::vector<double> chromaticityMask;
std::vector<double> lightnessMask;
double maskBlur;
int channel;
LabCorrectionRegion();
bool operator==(const LabCorrectionRegion &other) const;