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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513 | /* ============================================================
*
* This file is a part of digiKam project
* https://www.digikam.org
*
* Date : 2006-09-15
* Description : Exiv2 library interface
* Exiv2: www.exiv2.org
* Exif : www.exif.org/Exif2-2.PDF
* Iptc : www.iptc.org/std/IIM/4.1/specification/IIMV4.1.pdf
* Xmp : www.adobe.com/devnet/xmp/pdfs/xmp_specification.pdf
* www.iptc.org/std/Iptc4xmpCore/1.0/specification/Iptc4xmpCore_1.0-spec-XMPSchema_8.pdf
* Paper: www.metadataworkinggroup.com/pdf/mwg_guidance.pdf
*
* SPDX-FileCopyrightText: 2006-2025 by Gilles Caulier <caulier dot gilles at gmail dot com>
* SPDX-FileCopyrightText: 2006-2013 by Marcel Wiesweg <marcel dot wiesweg at gmx dot de>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*
* ============================================================ */
#include "metaengine_p.h"
#include "metaengine_data_p.h"
// Local includes
#include "digikam_debug.h"
#include "digikam_version.h"
#if defined(Q_CC_CLANG)
# pragma clang diagnostic push
# pragma clang diagnostic ignored "-Wdeprecated-declarations"
#endif
namespace Digikam
{
MetaEngine::MetaEngine()
: d(new Private(this))
{
}
MetaEngine::MetaEngine(const MetaEngine& metadata)
: d(new Private(this))
{
d->copyPrivateData(metadata.d);
}
MetaEngine::MetaEngine(const MetaEngineData& data)
: d(new Private(this))
{
setData(data);
}
MetaEngine::MetaEngine(const QString& filePath)
: d(new Private(this))
{
// NOTE: use dynamic binding as this virtual method can be re-implemented in derived classes.
this->load(filePath);
}
MetaEngine::~MetaEngine()
{
delete d;
}
MetaEngine& MetaEngine::operator=(const MetaEngine& metadata)
{
d->copyPrivateData(metadata.d);
return *this;
}
//-- Statics methods ----------------------------------------------
bool MetaEngine::initializeExiv2()
{
#ifdef _XMP_SUPPORT_
if (!Exiv2::XmpParser::initialize())
{
return false;
}
registerXmpNameSpace(QLatin1String("http://ns.google.com/photos/1.0/container/"), // krazy:exclude=insecurenet
QLatin1String("Container"));
registerXmpNameSpace(QLatin1String("http://ns.google.com/photos/1.0/container_1_/"), // krazy:exclude=insecurenet
QLatin1String("Container_1_"));
registerXmpNameSpace(QLatin1String("http://ns.google.com/photos/1.0/gcontainer/"), // krazy:exclude=insecurenet
QLatin1String("GContainer"));
registerXmpNameSpace(QLatin1String("http://ns.google.com/photos/1.0/container/item/"), // krazy:exclude=insecurenet
QLatin1String("Item"));
registerXmpNameSpace(QLatin1String("http://ns.google.com/photos/1.0/container/item_1_/"), // krazy:exclude=insecurenet
QLatin1String("Item_1_"));
registerXmpNameSpace(QLatin1String("http://ns.google.com/photos/1.0/container/gitem/"), // krazy:exclude=insecurenet
QLatin1String("GItem"));
registerXmpNameSpace(QLatin1String("http://ns.google.com/photos/1.0/container/gcontaineritem/"), // krazy:exclude=insecurenet
QLatin1String("GContainerItem"));
registerXmpNameSpace(QLatin1String("http://ns.google.com/photos/1.0/camera/"), // krazy:exclude=insecurenet
QLatin1String("GCamera"));
registerXmpNameSpace(QLatin1String("http://ns.adobe.com/hdr-gain-map/1.0/"), // krazy:exclude=insecurenet
QLatin1String("hdrgm"));
registerXmpNameSpace(QLatin1String("http://ns.apple.com/faceinfo/1.0/"), // krazy:exclude=insecurenet
QLatin1String("apple-fi"));
registerXmpNameSpace(QLatin1String("http://ns.excire.com/foto/1.0/"), // krazy:exclude=insecurenet
QLatin1String("excire"));
registerXmpNameSpace(QLatin1String("http://ns.useplus.org/ldf/xmp/1.0/ImageCreator/plus_1__1_/"), // krazy:exclude=insecurenet
QLatin1String("plus_1__1_"));
registerXmpNameSpace(QLatin1String("http://ns.useplus.org/ldf/xmp/1.0/ImageCreator/plus_1_/"), // krazy:exclude=insecurenet
QLatin1String("plus_1_"));
/**
* It cleans up memory used by Adobe XMP SDK automatically at application exit.
* See Bug #166424 for details.
*/
::atexit(Exiv2::XmpParser::terminate);
#endif // _XMP_SUPPORT_
#if EXIV2_TEST_VERSION(0,28,3) && defined EXV_ENABLE_BMFF
s_metaEngineSupportBmff = true;
#elif EXIV2_TEST_VERSION(0,27,4) && defined EXV_ENABLE_BMFF
// For Base Media File Format (aka CR3, HEIF, HEIC, and AVIF)
s_metaEngineSupportBmff = Exiv2::enableBMFF(true);
#endif
return true;
}
bool MetaEngine::supportBmff()
{
return s_metaEngineSupportBmff;
}
bool MetaEngine::supportXmp()
{
#ifdef _XMP_SUPPORT_
return true;
#else
return false;
#endif // _XMP_SUPPORT_
}
bool MetaEngine::supportJpegXL()
{
#ifdef EXV_HAVE_BROTLI
return true;
#else
return false;
#endif // EXV_HAVE_BROTLI
}
bool MetaEngine::supportMetadataWriting(const QString& /*typeMime*/)
{
/* FIXME : use Exiv2 API to return right writings support
if (typeMime == QLatin1String("image/jpeg"))
{
return true;
}
else if (typeMime == QLatin1String("image/tiff"))
{
return true;
}
else if (typeMime == QLatin1String("image/png"))
{
return true;
}
else if (typeMime == QLatin1String("image/jp2"))
{
return true;
}
else if (typeMime == QLatin1String("image/x-raw"))
{
return true;
}
else if (typeMime == QLatin1String("image/pgf"))
{
return true;
}
*/
return true;
}
QString MetaEngine::Exiv2Version()
{
#if EXIV2_TEST_VERSION(0,27,0)
return QString::fromStdString(Exiv2::versionString());
#else
return QLatin1String(Exiv2::version());
#endif
}
//-- General methods ----------------------------------------------
MetaEngineData MetaEngine::data() const
{
MetaEngineData data;
data.d = d->data;
return data;
}
void MetaEngine::setData(const MetaEngineData& data)
{
if (data.d)
{
d->data = data.d;
}
else
{
// MetaEngineData can have a null pointer,
// but we never want a null pointer in Private.
d->data->clear();
}
}
bool MetaEngine::loadFromData(const QByteArray& imgData)
{
if (imgData.isEmpty())
{
return false;
}
QMutexLocker lock(&s_metaEngineMutex);
try
{
Exiv2::Image::AutoPtr image = Exiv2::ImageFactory::open((Exiv2::byte*)imgData.data(), imgData.size());<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected.
d->filePath.clear();
image->readMetadata();
// Size and mimetype ---------------------------------
d->pixelSize = QSize(image->pixelWidth(), image->pixelHeight());
d->mimeType = QString::fromStdString(image->mimeType());
// Image comments ---------------------------------
d->itemComments() = image->comment();
// Exif metadata ----------------------------------
d->exifMetadata() = image->exifData();
// Iptc metadata ----------------------------------
d->iptcMetadata() = image->iptcData();
#ifdef _XMP_SUPPORT_
// Xmp metadata -----------------------------------
d->xmpMetadata() = image->xmpData();
#endif // _XMP_SUPPORT_
// Exif byte order -------------------------------
d->exifByteOrder() = image->byteOrder();
// ICC Profile ------------------------------------
if (!getExifTagString("Exif.Image.Make").contains(QLatin1String("APPLE"), Qt::CaseInsensitive))
{
#if EXIV2_TEST_VERSION(0,27,99)
d->iccProfileBuf() = image->iccProfile();
#else
d->iccProfileBuf() = *(image->iccProfile());
#endif
}
return true;
}
catch (Exiv2::AnyError& e)
{
d->printExiv2ExceptionError(QLatin1String("Cannot load metadata with Exiv2:"), e);
}
catch (...)
{
qCCritical(DIGIKAM_METAENGINE_LOG) << "Default exception from Exiv2";
}
return false;
}
bool MetaEngine::loadFromDataAndMerge(const QByteArray& imgData, const QStringList& exclude)
{
if (imgData.isEmpty())
{
return false;
}
QMutexLocker lock(&s_metaEngineMutex);
try
{
Exiv2::Image::AutoPtr image = Exiv2::ImageFactory::open((Exiv2::byte*)imgData.data(), imgData.size());<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected.
d->filePath.clear();
image->readMetadata();
// Size and mimetype ---------------------------------
d->pixelSize = QSize(image->pixelWidth(), image->pixelHeight());
d->mimeType = QString::fromStdString(image->mimeType());
// Exif metadata ----------------------------------
for (const QString& exTag : EXIV2_AS_CONST(exclude))
{
Exiv2::ExifKey exifKey(exTag.toLatin1().constData());
Exiv2::ExifData::iterator it1 = image->exifData().findKey(exifKey);
Exiv2::ExifData::iterator it2 = d->exifMetadata().findKey(exifKey);
if ((it1 != image->exifData().end()) && (it2 != d->exifMetadata().end()))
{
image->exifData().erase(it1);
}
}
ExifMetaEngineMergeHelper exifHelper;
exifHelper.mergeAll(image->exifData(), d->exifMetadata());
// Iptc metadata ----------------------------------
IptcMetaEngineMergeHelper iptcHelper;
iptcHelper.mergeAll(image->iptcData(), d->iptcMetadata());
#ifdef _XMP_SUPPORT_
// Xmp metadata -----------------------------------
XmpMetaEngineMergeHelper xmpHelper;
xmpHelper.mergeAll(image->xmpData(), d->xmpMetadata());
#endif // _XMP_SUPPORT_
// Exif byte order -------------------------------
d->exifByteOrder() = image->byteOrder();
return true;
}
catch (Exiv2::AnyError& e)
{
d->printExiv2ExceptionError(QLatin1String("Cannot load and merge metadata with Exiv2:"), e);
}
catch (...)
{
qCCritical(DIGIKAM_METAENGINE_LOG) << "Default exception from Exiv2";
}
return false;
}
bool MetaEngine::isEmpty() const
{
if (!hasComments() && !hasExif() && !hasIptc() && !hasXmp())
{
return true;
}
return false;
}
QSize MetaEngine::getPixelSize() const
{
return d->pixelSize;
}
QString MetaEngine::getMimeType() const
{
return d->mimeType;
}
void MetaEngine::setReadWithExifTool(const bool on)
{
d->readWithExifTool = on;
}
bool MetaEngine::readWithExifTool() const
{
return d->readWithExifTool;
}
void MetaEngine::setWriteWithExifTool(const bool on)
{
d->writeWithExifTool = on;
}
bool MetaEngine::writeWithExifTool() const
{
return d->writeWithExifTool;
}
void MetaEngine::setWriteRawFiles(const bool on)
{
d->writeRawFiles = on;
}
bool MetaEngine::writeRawFiles() const
{
return d->writeRawFiles;
}
void MetaEngine::setWriteDngFiles(const bool on)
{
d->writeDngFiles = on;
}
bool MetaEngine::writeDngFiles() const
{
return d->writeDngFiles;
}
void MetaEngine::setUseXMPSidecar4Reading(const bool on)
{
d->useXMPSidecar4Reading = on;
}
bool MetaEngine::useXMPSidecar4Reading() const
{
return d->useXMPSidecar4Reading;
}
void MetaEngine::setUseCompatibleFileName(const bool on)
{
d->useCompatibleFileName = on;
}
bool MetaEngine::useCompatibleFileName() const
{
return d->useCompatibleFileName;
}
void MetaEngine::setMetadataWritingMode(const int mode)
{
d->metadataWritingMode = mode;
}
int MetaEngine::metadataWritingMode() const
{
return d->metadataWritingMode;
}
void MetaEngine::setUpdateFileTimeStamp(bool on)
{
d->updateFileTimeStamp = on;
}
bool MetaEngine::updateFileTimeStamp() const
{
return d->updateFileTimeStamp;
}
bool MetaEngine::setProgramId() const
{
QString version(digiKamVersion());
QLatin1String software("digiKam");
return setItemProgramId(software, version);
}
} // namespace Digikam
#if defined(Q_CC_CLANG)
# pragma clang diagnostic pop
#endif
|