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
464
465
466
467
468
469
470 | /* ============================================================
*
* This file is a part of digiKam project
* https://www.digikam.org
*
* Date : 2004-02-14
* Description : image data interface for image tools
*
* SPDX-FileCopyrightText: 2004-2005 by Renchi Raju <renchi dot raju at gmail dot com>
* SPDX-FileCopyrightText: 2004-2025 by Gilles Caulier <caulier dot gilles at gmail dot com>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*
* ============================================================ */
#include "imageiface.h"
// Qt includes
#include <QWidget>
#include <QSize>
#include <QBitmap>
#include <QPainter>
// Local includes
#include "digikam_debug.h"
#include "exposurecontainer.h"
#include "iccmanager.h"
#include "iccsettingscontainer.h"
#include "icctransform.h"
#include "editorcore.h"
#include "dmetadata.h"
#include "digikam_globals.h"
namespace Digikam
{
class Q_DECL_HIDDEN ImageIface::Private
{
public:
Private() = default;
QPixmap checkPixmap();
uchar* previewImageData();
public:
ImageIface::PreviewType previewType = FullImage;
int originalWidth = 0;
int originalHeight = 0;
int originalBytesDepth = 0;
int constrainWidth = 0;
int constrainHeight = 0;
int previewWidth = 0;
int previewHeight = 0;
QPixmap qcheck;
DImg previewImage;
DImg targetPreviewImage;
EditorCore* const core = EditorCore::defaultInstance();
};
QPixmap ImageIface::Private::checkPixmap()
{
if (qcheck.isNull())
{
qcheck = QPixmap(8, 8);
QPainter p;
p.begin(&qcheck);
p.fillRect(0, 0, 4, 4, QColor(144, 144, 144));
p.fillRect(4, 4, 4, 4, QColor(144, 144, 144));
p.fillRect(0, 4, 4, 4, QColor(100, 100, 100));
p.fillRect(4, 0, 4, 4, QColor(100, 100, 100));
p.end();
}
return qcheck;
}
uchar* ImageIface::Private::previewImageData()
{
if (previewImage.isNull())
{
DImg* im = nullptr;
if (previewType == FullImage)
{
im = core->getImg();
if (!im || im->isNull())
{
return nullptr;
}
}
else // ImageSelection
{
im = new DImg(core->getImgSelection());
if (!im)
{
return nullptr;
}
if (im->isNull())
{
delete im;
return nullptr;
}
im->setIccProfile(core->getEmbeddedICC());
}
QSize sz(im->width(), im->height());
sz.scale(constrainWidth, constrainHeight, Qt::KeepAspectRatio);
previewImage = im->smoothScale(sz.width(), sz.height());
previewWidth = previewImage.width();
previewHeight = previewImage.height();
// only create another copy if needed, in setPreviewImage
targetPreviewImage = previewImage;
if (previewType == ImageSelection)
{
delete im;
}
}
DImg previewData = previewImage.copyImageData();
return previewData.stripImageData();
}
// ------------------------------------------------------------------------------------------------------
ImageIface::ImageIface(const QSize& size)
: d(new Private)
{
d->constrainWidth = size.width();
d->constrainHeight = size.height();
d->originalWidth = d->core->origWidth();
d->originalHeight = d->core->origHeight();
d->originalBytesDepth = d->core->bytesDepth();
}
ImageIface::~ImageIface()
{
delete d;
}
void ImageIface::setPreviewType(PreviewType type)
{
d->previewType = type;
}
ImageIface::PreviewType ImageIface::previewType() const
{
return d->previewType;
}
DImg *ImageIface::previewReference()
{
return &(d->previewImage);
}
DColor ImageIface::colorInfoFromOriginal(const QPoint& point) const
{
if (!original() || original()->isNull())
{
qCWarning(DIGIKAM_GENERAL_LOG) << "No image data available!";
return DColor();
}
if ((point.x() > originalSize().width()) || (point.y() > originalSize().height()))
{
qCWarning(DIGIKAM_GENERAL_LOG) << "Coordinate out of range!";
return DColor();
}
return original()->getPixelColor(point.x(), point.y());
}
DColor ImageIface::colorInfoFromPreview(const QPoint& point) const
{
if (d->previewImage.isNull() || (point.x() > d->previewWidth) || (point.y() > d->previewHeight))
{
qCWarning(DIGIKAM_GENERAL_LOG) << "Coordinate out of range or no image data available!";
return DColor();
}
return d->previewImage.getPixelColor(point.x(), point.y());
}
DColor ImageIface::colorInfoFromTargetPreview(const QPoint& point) const
{
if (d->targetPreviewImage.isNull() || (point.x() > d->previewWidth) || (point.y() > d->previewHeight))
{
qCWarning(DIGIKAM_GENERAL_LOG) << "Coordinate out of range or no image data available!";
return DColor();
}
return d->targetPreviewImage.getPixelColor(point.x(), point.y());
}
DImg ImageIface::setPreviewSize(const QSize& size) const
{
d->previewImage.reset();
d->targetPreviewImage.reset();
d->constrainWidth = size.width();
d->constrainHeight = size.height();
return preview();
}
DImg ImageIface::preview() const
{
// NOTE: corrects the values for width and height of the preview image from the image data interface
// See Bug #320382 for details.
uchar* const data = d->previewImageData();
return DImg(d->previewWidth, d->previewHeight, previewSixteenBit(), previewHasAlpha(), data, false);
}
DImg* ImageIface::original() const
{
return d->core->getImg();
}
DImg ImageIface::selection() const
{
return d->core->getImgSelection();
}
void ImageIface::setPreviewIccProfile(const IccProfile& profile)
{
d->targetPreviewImage.detach();
d->targetPreviewImage.setIccProfile(profile);
}
void ImageIface::crop(const QRect& region)
{
d->core->crop(region);
}
void ImageIface::setOriginalIccProfile(const IccProfile& profile)
{
d->core->putIccProfile(profile);
}
QSize ImageIface::previewSize() const
{
return QSize(d->previewWidth, d->previewHeight);
}
bool ImageIface::previewSixteenBit() const
{
return originalSixteenBit();
}
bool ImageIface::previewHasAlpha() const
{
return originalHasAlpha();
}
QSize ImageIface::originalSize() const
{
return QSize(d->core->origWidth(), d->core->origHeight());
}
bool ImageIface::originalSixteenBit() const
{
return d->core->sixteenBit();
}
bool ImageIface::originalHasAlpha() const
{
return d->core->hasAlpha();
}
QRect ImageIface::selectionRect() const
{
return (d->core->getSelectedArea());
}
void ImageIface::convertOriginalColorDepth(int depth)
{
d->core->convertDepth(depth);
}
QPixmap ImageIface::convertToPixmap(const DImg& img) const
{
return d->core->convertToPixmap(img);
}
IccProfile ImageIface::originalIccProfile() const
{
return d->core->getEmbeddedICC();
}
MetaEngineData ImageIface::originalMetadata() const
{
DImg* const img = original();<--- Variable 'img' can be declared as pointer to const
if (img)
{
return (img->getMetadata());
}
return MetaEngineData();
}
void ImageIface::setOriginalMetadata(const MetaEngineData& meta)
{
DImg* const img = original();
if (img)
{
img->setMetadata(meta);
}
}
PhotoInfoContainer ImageIface::originalPhotoInfo() const
{
return (DMetadata(originalMetadata()).getPhotographInformation());
}
void ImageIface::paint(QPaintDevice* const device, const QRect& rect, QPainter* const painter)
{
int x = rect.x();
int y = rect.y();
int w = rect.width();
int h = rect.height();
QPainter* p = nullptr;
QPainter localPainter;
if (painter)
{
p = painter;
}
else
{
p = &localPainter;
p->begin(device);
}
int width = (w > 0) ? qMin(d->previewWidth, w) : d->previewWidth;
int height = (h > 0) ? qMin(d->previewHeight, h) : d->previewHeight;
if (!d->targetPreviewImage.isNull())
{
if (d->targetPreviewImage.hasAlpha())
{
p->drawTiledPixmap(x, y, width, height, d->checkPixmap());
}
QPixmap pixImage;
bool doSoftProofing = d->core->softProofingEnabled();
ICCSettingsContainer iccSettings = d->core->getICCSettings();
if (iccSettings.enableCM && (iccSettings.useManagedView || doSoftProofing))
{
IccManager manager(d->targetPreviewImage);
IccTransform monitorICCtrans;
if (doSoftProofing)
{
monitorICCtrans = manager.displaySoftProofingTransform(IccProfile(iccSettings.defaultProofProfile));
}
else
{
monitorICCtrans = manager.displayTransform();
}
pixImage = d->targetPreviewImage.convertToPixmap(monitorICCtrans);
}
else
{
pixImage = d->targetPreviewImage.convertToPixmap();
}
p->drawPixmap(x, y, pixImage, 0, 0, width, height);
// Show the Over/Under exposure pixels indicators
ExposureSettingsContainer* const expoSettings = d->core->getExposureSettings();
if (expoSettings && (expoSettings->underExposureIndicator || expoSettings->overExposureIndicator))
{
QImage pureColorMask = d->targetPreviewImage.pureColorMask(expoSettings);
QPixmap pixMask = QPixmap::fromImage(pureColorMask);
p->drawPixmap(x, y, pixMask, 0, 0, width, height);
}
}
if (!painter)
{
p->end();
}
}
void ImageIface::setSelection(const QString& caller, const FilterAction& action, const DImg& img)
{
if (
(img.hasAlpha() != originalHasAlpha()) ||
(img.sixteenBit() != originalSixteenBit()) ||
(img.size() != selectionRect().size())
)
{
qCDebug(DIGIKAM_GENERAL_LOG) << "Properties of image to overwrite selection differs than original image";
return;
}
if (img.isNull())
{
qCDebug(DIGIKAM_GENERAL_LOG) << "No image data to handle";
return;
}
d->core->putImgSelection(caller, action, img);
}
void ImageIface::setPreview(const DImg& img)
{
if (
(img.hasAlpha() != previewHasAlpha()) ||
(img.sixteenBit() != previewSixteenBit())
)
{
qCDebug(DIGIKAM_GENERAL_LOG) << "Properties of image differs than preview";
return;
}
uchar* const data = img.bits();
if (!data)
{
qCDebug(DIGIKAM_GENERAL_LOG) << "No preview image data to handle";
return;
}
d->targetPreviewImage.detach();
d->targetPreviewImage.putImageData(data);
}
void ImageIface::setOriginal(const QString& caller, const FilterAction& action, const DImg& img)
{
if (img.isNull())
{
qCDebug(DIGIKAM_GENERAL_LOG) << "No image data to handle";
return;
}
d->core->putImg(caller, action, img);
}
} // namespace Digikam
|