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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463 | /* ============================================================
*
* This file is a part of digiKam project
* https://www.digikam.org
*
* Date : 2009-02-01
* Description : Content aware resizer class.
*
* SPDX-FileCopyrightText: 2009 by Julien Pontabry <julien dot pontabry at ulp dot u-strasbg dot fr>
* SPDX-FileCopyrightText: 2009-2025 by Gilles Caulier <caulier dot gilles at gmail dot com>
* SPDX-FileCopyrightText: 2010 by Martin Klapetek <martin dot klapetek at gmail dot com>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*
* ============================================================ */
#include "contentawarefilter.h"
// Liquid rescale library include
// Pragma directives to reduce warnings from Liblqr header files.
#if defined(Q_CC_GNU)
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wzero-as-null-pointer-constant"
#endif
#include <lqr.h>
// Restore warnings
#if defined(Q_CC_GNU)
# pragma GCC diagnostic pop
#endif
// Qt includes
#include <QColor>
// Local includes
#include "digikam_debug.h"
#include "digikam_globals_p.h" // For KF6::Ki18n deprecated
namespace Digikam
{
// Static methods.
LqrRetVal s_carverProgressInit(const gchar* init_message);
LqrRetVal s_carverProgressUpdate(gdouble percentage);
LqrRetVal s_carverProgressEnd(const gchar* end_message);
// Static members.
/**
* Resizement is decomposed in 2 stages: horizontal and vertical.
*/
bool s_stage = false;
bool s_wResize = false;
bool s_hResize = false;
ContentAwareFilter* s_resiser = nullptr;
static LqrEnergyFuncBuiltinType toLqrEnergy(ContentAwareContainer::EnergyFunction func)
{
switch (func)
{
case ContentAwareContainer::GradientNorm:
default:
{
return LQR_EF_GRAD_NORM;
}
case ContentAwareContainer::SumOfAbsoluteValues:
{
return LQR_EF_GRAD_SUMABS;
}
case ContentAwareContainer::XAbsoluteValue:
{
return LQR_EF_GRAD_XABS;
}
case ContentAwareContainer::LumaGradientNorm:
{
return LQR_EF_LUMA_GRAD_NORM;
}
case ContentAwareContainer::LumaSumOfAbsoluteValues:
{
return LQR_EF_LUMA_GRAD_SUMABS;
}
case ContentAwareContainer::LumaXAbsoluteValue:
{
return LQR_EF_LUMA_GRAD_XABS;
}
}
}
static LqrResizeOrder toLqrOrder(Qt::Orientation direction)
{
switch (direction)
{
case Qt::Horizontal:
default:
{
return LQR_RES_ORDER_HOR;
}
case Qt::Vertical:
{
return LQR_RES_ORDER_VERT;
}
}
}
// --------------------------------------------------------------------------------------
class Q_DECL_HIDDEN ContentAwareFilter::Private
{
public:
Private() = default;
public:
ContentAwareContainer settings;
LqrCarver* carver = nullptr;
LqrProgress* progress = nullptr;
};
ContentAwareFilter::ContentAwareFilter(QObject* const parent)
: DImgThreadedFilter(parent),
d (new Private)
{
initFilter();
}
ContentAwareFilter::ContentAwareFilter(DImg* const orgImage, QObject* const parent, const ContentAwareContainer& settings)
: DImgThreadedFilter(orgImage, parent, QLatin1String("ContentAwareFilter")),
d (new Private)
{
initFilter();
s_stage = false;
s_resiser = this;
d->settings = settings;
d->carver = lqr_carver_new_ext(m_orgImage.bits(), m_orgImage.width(), m_orgImage.height(), 4,
m_orgImage.sixteenBit() ? LQR_COLDEPTH_16I : LQR_COLDEPTH_8I);
if (d->carver)
{
// Non null carver object operations
// Ask Lqr library to preserve our picture
lqr_carver_set_preserve_input_image(d->carver);
// Initialize the carver object
lqr_carver_init(d->carver, d->settings.step, d->settings.rigidity);
// Create a progress object
d->progress = lqr_progress_new();
lqr_progress_set_init(d->progress, s_carverProgressInit);
lqr_progress_set_update(d->progress, s_carverProgressUpdate);
lqr_progress_set_end(d->progress, s_carverProgressEnd);
lqr_carver_set_progress(d->carver, d->progress);
lqr_carver_set_side_switch_frequency(d->carver, d->settings.side_switch_freq);
// Set enlargement steps as suggested by Carlo Baldassi
lqr_carver_set_enl_step(d->carver, 1.5);
// Choose a gradient function
lqr_carver_set_energy_function_builtin(d->carver, toLqrEnergy(d->settings.func));
// Choose the resize order
lqr_carver_set_resize_order(d->carver, toLqrOrder(d->settings.resize_order));
// Set a bias if any mask
if (!d->settings.mask.isNull())
{
buildBias(d->settings.mask);
}
// Set skin tone mask if option is activated
if (d->settings.preserve_skin_tones)
{
buildSkinToneBias();
}
}
}
ContentAwareFilter::~ContentAwareFilter()
{
this->cancelFilter();
if (d->carver)
{
lqr_carver_destroy(d->carver);
}
delete d;
}
QString ContentAwareFilter::DisplayableName()
{
return QString::fromUtf8(I18N_NOOP("Content-Aware Filter"));
}
void ContentAwareFilter::getEnergyImage()
{
if (!d->carver)
{
return;
}
int w = lqr_carver_get_width(d->carver);
int h = lqr_carver_get_height(d->carver);
guchar* buff = reinterpret_cast<guchar*>(malloc(w * h * 3 * sizeof(guchar)));
lqr_carver_get_energy_image(d->carver, buff, 1, LQR_COLDEPTH_8I, LQR_RGBA_IMAGE);
}
void ContentAwareFilter::filterImage()
{
if (!d->carver)
{
return;
}
uint x = 0;
uint y = 0;
uint w = 0;
uint h = 0;
s_wResize = (m_orgImage.width() == d->settings.width) ? false : true;
s_hResize = (m_orgImage.height() == d->settings.height) ? false : true;
// Liquid rescale
lqr_carver_resize(d->carver, d->settings.width, d->settings.height);
if (!runningFlag())
{
return;
}
// Create a new image
w = lqr_carver_get_width(d->carver);
h = lqr_carver_get_height(d->carver);
m_destImage = DImg(w, h, m_orgImage.sixteenBit());
// Write pixels in the DImg structure image
lqr_carver_scan_reset(d->carver);
void* rgb = nullptr;
uchar* rgbOut8 = nullptr;<--- Variable 'rgbOut8' can be declared as pointer to const
unsigned short* rgbOut16 = nullptr;<--- Variable 'rgbOut16' can be declared as pointer to const
if (m_orgImage.sixteenBit())
{
while (runningFlag() && lqr_carver_scan_ext(d->carver, reinterpret_cast<gint*>(&x), reinterpret_cast<gint*>(&y), &rgb))
{
rgbOut16 = reinterpret_cast<unsigned short*>(rgb);
m_destImage.setPixelColor(x, y, DColor(rgbOut16[2], rgbOut16[1], rgbOut16[0], 65535, true));
}
}
else
{
while (runningFlag() && lqr_carver_scan_ext(d->carver, reinterpret_cast<gint*>(&x), reinterpret_cast<gint*>(&y), &rgb))
{
rgbOut8 = reinterpret_cast<uchar*>(rgb);
m_destImage.setPixelColor(x, y, DColor(rgbOut8[2], rgbOut8[1], rgbOut8[0], 255, false));
}
}
}
void ContentAwareFilter::progressCallback(int progress)
{
if ((progress % 5) == 0)
{
postProgress(progress);
}
//qCDebug(DIGIKAM_DIMG_LOG) << "Content Aware Resizing: " << progress << " %";
}
void ContentAwareFilter::cancelFilter()
{
// Handle cancel operations with lqr library.
qCDebug(DIGIKAM_DIMG_LOG) << "Stop LibLqr computation...";
lqr_carver_cancel(d->carver);
DImgThreadedFilter::cancelFilter();
}
bool ContentAwareFilter::isSkinTone(const DColor& color)
{
// NOTE: color is previously converted to eight bits.
double R = color.red() / 255.0;
double G = color.green() / 255.0;
double B = color.blue() / 255.0;
double S = R + G + B;
return (
((B / G) < 1.249) &&
((S / 3.0 * R) > 0.696) &&
((1.0 / 3.0 - B / S) > 0.014) &&
((G / (3.0 * S)) < 0.108)
);
}
void ContentAwareFilter::buildSkinToneBias()
{
DColor c;
for (uint x = 0 ; x < m_orgImage.width() ; ++x)
{
for (uint y = 0 ; y < m_orgImage.height() ; ++y)
{
c = m_orgImage.getPixelColor(x, y);
c.convertToEightBit();
gdouble bias = 10000 * isSkinTone(c);
lqr_carver_bias_add_xy(d->carver, bias, x, y);
}
}
}
void ContentAwareFilter::buildBias(const QImage& mask)
{
QColor pixColor;
int r, g, b, a;
for (int x = 0 ; x < mask.width() ; ++x)
{
for (int y = 0 ; y < mask.height() ; ++y)
{
pixColor = QColor::fromRgba(mask.pixel(x, y));
pixColor.getRgb(&r, &g, &b, &a);
gdouble bias = 0.0;
if (g == 255)
{
bias = 1000000.0;
}
if (r == 255)
{
bias = -1000000.0;
}
lqr_carver_bias_add_xy(d->carver, bias, x, y);
}
}
}
FilterAction ContentAwareFilter::filterAction()
{
bool isReproducible = d->settings.mask.isNull();
DefaultFilterAction<ContentAwareFilter> action(isReproducible);
action.addParameter(QLatin1String("height"), d->settings.height);
action.addParameter(QLatin1String("preserve_skin_tones"), d->settings.preserve_skin_tones);
action.addParameter(QLatin1String("rigidity"), d->settings.rigidity);
action.addParameter(QLatin1String("side_switch_freq"), d->settings.side_switch_freq);
action.addParameter(QLatin1String("step"), d->settings.step);
action.addParameter(QLatin1String("width"), d->settings.width);
action.addParameter(QLatin1String("func"), d->settings.func);
action.addParameter(QLatin1String("resize_order"), d->settings.resize_order);
return action;
}
void ContentAwareFilter::readParameters(const FilterAction& action)
{
d->settings.height = action.parameter(QLatin1String("height")).toUInt();
d->settings.preserve_skin_tones = action.parameter(QLatin1String("preserve_skin_tones")).toBool();
d->settings.rigidity = action.parameter(QLatin1String("rigidity")).toDouble();
d->settings.side_switch_freq = action.parameter(QLatin1String("side_switch_freq")).toInt();
d->settings.step = action.parameter(QLatin1String("step")).toInt();
d->settings.width = action.parameter(QLatin1String("width")).toUInt();
d->settings.func = (ContentAwareContainer::EnergyFunction)action.parameter(QLatin1String("func")).toInt();
d->settings.resize_order = (Qt::Orientation)action.parameter(QLatin1String("resize_order")).toInt();
}
// ------------------------------------------------------------------------------------
// Static methods.
LqrRetVal s_carverProgressInit(const gchar* /*init_message*/)
{
if (!s_stage)
{
s_resiser->progressCallback(0);
}
else
{
s_resiser->progressCallback(50);
}
return LQR_OK;
}
LqrRetVal s_carverProgressUpdate(gdouble percentage)
{
int progress;
if (!s_stage)
{
if (!s_wResize || !s_hResize)
{
progress = (int)(percentage * 100.0);
}
else
{
progress = (int)(percentage * 50.0);
}
}
else
{
progress = (int)(50.0 + percentage * 50.0);
}
s_resiser->progressCallback(progress);
return LQR_OK;
}
LqrRetVal s_carverProgressEnd(const gchar* /*end_message*/)
{
if (!s_stage)
{
if (!s_wResize || !s_hResize)
{
s_resiser->progressCallback(100);
}
else
{
s_resiser->progressCallback(50);
}
s_stage = true;
}
else
{
s_resiser->progressCallback(100);
}
return LQR_OK;
}
} // namespace Digikam
#include "moc_contentawarefilter.cpp"
|