1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/* ============================================================
 *
 * This file is a part of digiKam project
 * https://www.digikam.org
 *
 * Date        : 2005-03-06
 * Description : Hue/Saturation/Lightness image filter.
 *
 * SPDX-FileCopyrightText: 2005-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
 * SPDX-FileCopyrightText: 2010      by Julien Narboux <julien at narboux dot fr>
 * SPDX-FileCopyrightText: 2010      by Martin Klapetek <martin dot klapetek at gmail dot com>
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 *
 * ============================================================ */

#include "hslfilter.h"

// C++ includes

#include <cstdio>
#include <cmath>

// Local includes

#include "dimg.h"
#include "digikam_globals_p.h"      // For KF6::Ki18n deprecated

namespace Digikam
{

class Q_DECL_HIDDEN HSLFilter::Private
{
public:

    Private() = default;

    int          htransfer[256]     = { 0 };
    int          ltransfer[256]     = { 0 };
    int          stransfer[256]     = { 0 };

    int          htransfer16[65536] = { 0 };
    int          ltransfer16[65536] = { 0 };
    int          stransfer16[65536] = { 0 };

    HSLContainer settings;
};

HSLFilter::HSLFilter(QObject* const parent)
    : DImgThreadedFilter(parent),
      d                 (new Private)
{
    reset();
    initFilter();
}

HSLFilter::HSLFilter(DImg* const orgImage, QObject* const parent, const HSLContainer& settings)
    : DImgThreadedFilter(orgImage, parent, QLatin1String("HSLFilter")),
      d                 (new Private)
{
    d->settings = settings;
    reset();
    initFilter();
}

HSLFilter::~HSLFilter()
{
    cancelFilter();
    delete d;
}

QString HSLFilter::DisplayableName()
{
    return QString::fromUtf8(I18N_NOOP("Hue / Saturation / Lightness Filter"));
}

void HSLFilter::filterImage()
{
    setHue(d->settings.hue);
    setSaturation(d->settings.saturation);
    setLightness(d->settings.lightness);
    applyHSL(m_orgImage);
    m_destImage = m_orgImage;
}

void HSLFilter::reset()
{
    // initialize to linear mapping

    for (int i = 0 ; i < 65536 ; ++i)
    {
        d->htransfer16[i] = i;
        d->ltransfer16[i] = i;
        d->stransfer16[i] = i;
    }

    for (int i = 0 ; i < 256 ; ++i)
    {
        d->htransfer[i] = i;
        d->ltransfer[i] = i;
        d->stransfer[i] = i;
    }
}

void HSLFilter::setHue(double val)
{
    int value;

    for (int i = 0 ; i < 65536 ; ++i)
    {
        value = lround(val * 65535.0 / 360.0);

        if      ((i + value) < 0)
        {
            d->htransfer16[i] = 65535 + (i + value);
        }
        else if ((i + value) > 65535)
        {
            d->htransfer16[i] = i + value - 65535;
        }
        else
        {
            d->htransfer16[i] = i + value;
        }
    }

    for (int i = 0 ; i < 256 ; ++i)
    {
        value = lround(val * 255.0 / 360.0);

        if      ((i + value) < 0)
        {
            d->htransfer[i] = 255 + (i + value);
        }
        else if ((i + value) > 255)
        {
            d->htransfer[i] = i + value - 255;
        }
        else
        {
            d->htransfer[i] = i + value;
        }
    }
}

void HSLFilter::setSaturation(double val)
{
    val = CLAMP(val, -100.0, 100.0);
    int value;

    for (int i = 0 ; i < 65536 ; ++i)
    {
        value             = lround((i * (100.0 + val)) / 100.0);
        d->stransfer16[i] = CLAMP065535(value);
    }

    for (int i = 0 ; i < 256 ; ++i)
    {
        value            = lround((i * (100.0 + val)) / 100.0);
        d->stransfer[i]  = CLAMP0255(value);
    }
}

void HSLFilter::setLightness(double val)
{
    // val needs to be in that range so that the result is in the range 0..65535
    val = CLAMP(val, -100.0, 100.0);

    if (val < 0)
    {
        for (int i = 0 ; i < 65536 ; ++i)
        {
            d->ltransfer16[i] = lround((i * (val + 100.0)) / 100.0);
        }

        for (int i = 0 ; i < 256 ; ++i)
        {
            d->ltransfer[i] = lround((i * (val + 100.0)) / 100.0);
        }
    }
    else
    {
        for (int i = 0 ; i < 65536 ; ++i)
        {
            d->ltransfer16[i] = lround(i * (1.0 - val / 100.0)  + 65535.0 / 100.0 * val);
        }

        for (int i = 0 ; i < 256 ; ++i)
        {
            d->ltransfer[i] = lround(i * (1.0 - val / 100.0)  + 255.0 / 100.0 * val);
        }
    }
}

int HSLFilter::vibranceBias(double sat, double hue, double vib, bool sixteenbit)
{
    double ratio;
    int    localsat;
    double normalized_hue = hue / (sixteenbit ? 65535.0 : 255.0);

    if ((normalized_hue > 0.85) || (normalized_hue < 0.2))
    {
        ratio = 0.3;
    }
    else
    {
        ratio = 1.0;
    }

    localsat = lround((sat * (100.0 + vib * ratio)) / 100.0);

    if (sixteenbit)
    {
        return (CLAMP065535(localsat));
    }
    else
    {
        return (CLAMP0255(localsat));
    }
}

void HSLFilter::applyHSL(DImg& image)<--- Parameter 'image' can be declared as reference to const
{
    if (image.isNull())
    {
        return;
    }

    bool   sixteenBit     = image.sixteenBit();
    uint   numberOfPixels = image.numPixels();
    int    progress;
    int    hue, sat, lig;
    double vib = d->settings.vibrance;
    DColor color;

    if (sixteenBit)                   // 16 bits image.
    {
        unsigned short* data = reinterpret_cast<unsigned short*>(image.bits());

        for (uint i = 0 ; runningFlag() && (i < numberOfPixels) ; ++i)
        {
            color = DColor(data[2], data[1], data[0], 0, sixteenBit);

            // convert RGB to HSL

            color.getHSL(&hue, &sat, &lig);

            // convert HSL to RGB

            color.setHSL(d->htransfer16[hue], vibranceBias(d->stransfer16[sat], hue, vib, sixteenBit), d->ltransfer16[lig], sixteenBit);

            data[2] = color.red();
            data[1] = color.green();
            data[0] = color.blue();

            data   += 4;

            progress = (int)(((double)i * 100.0) / numberOfPixels);

            if ((progress % 5) == 0)
            {
                postProgress(progress);
            }
        }
    }
    else                                      // 8 bits image.
    {
        uchar* data = image.bits();

        for (uint i = 0 ; runningFlag() && (i < numberOfPixels) ; ++i)
        {
            color = DColor(data[2], data[1], data[0], 0, sixteenBit);

            // convert RGB to HSL

            color.getHSL(&hue, &sat, &lig);

            // convert HSL to RGB

            color.setHSL(d->htransfer[hue], vibranceBias(d->stransfer[sat], hue, vib, sixteenBit), d->ltransfer[lig], sixteenBit);

            data[2] = color.red();
            data[1] = color.green();
            data[0] = color.blue();

            data   += 4;

            progress = (int)(((double)i * 100.0) / numberOfPixels);

            if ((progress % 5) == 0)
            {
                postProgress(progress);
            }
        }
    }
}

FilterAction HSLFilter::filterAction()
{
    FilterAction action(FilterIdentifier(), CurrentVersion());
    action.setDisplayableName(DisplayableName());

    action.addParameter(QLatin1String("hue"),        d->settings.hue);
    action.addParameter(QLatin1String("lightness"),  d->settings.lightness);
    action.addParameter(QLatin1String("saturation"), d->settings.saturation);
    action.addParameter(QLatin1String("vibrance"),   d->settings.vibrance);

    return action;
}

void HSLFilter::readParameters(const Digikam::FilterAction& action)
{
    d->settings.hue        = action.parameter(QLatin1String("hue")).toDouble();
    d->settings.lightness  = action.parameter(QLatin1String("lightness")).toDouble();
    d->settings.saturation = action.parameter(QLatin1String("saturation")).toDouble();
    d->settings.vibrance   = action.parameter(QLatin1String("vibrance")).toDouble();
}

} // namespace Digikam

#include "moc_hslfilter.cpp"