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 | /* ============================================================
*
* This file is a part of digiKam project
* https://www.digikam.org
*
* Date : 2019-09-26
* Description : item metadata interface - libheif helpers.
*
* SPDX-FileCopyrightText: 2020-2025 by Gilles Caulier <caulier dot gilles at gmail dot com>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*
* ============================================================ */
#include "dmetadata.h"
// Qt includes
#include <QFile>
#include <QFileInfo>
#include <QByteArray>
#include <qplatformdefs.h>
// Libheif includes
#include <libheif/heif.h>
// Local includes
#include "digikam_config.h"
#include "digikam_debug.h"
namespace Digikam
{
static int64_t heifQIODeviceMetaGetPosition(void* userdata) // krazy:exclude=typedefs
{
QFile* const file = static_cast<QFile*>(userdata);
return (int64_t)file->pos();
}
static int heifQIODeviceMetaRead(void* data, size_t size, void* userdata)
{
QFile* const file = static_cast<QFile*>(userdata);
if ((file->pos() + (qint64)size) > file->size())
{
return 0;
}
qint64 bytes = file->read(reinterpret_cast<char*>(data), size);
return (int)((file->error() != QFileDevice::NoError) || bytes != (qint64)size);
}
static int heifQIODeviceMetaSeek(int64_t position, void* userdata) // krazy:exclude=typedefs
{
QFile* const file = static_cast<QFile*>(userdata);
return (int)file->seek(position);
}
static heif_reader_grow_status heifQIODeviceMetaWait(int64_t target_size, void* userdata) // krazy:exclude=typedefs
{
QFile* const file = static_cast<QFile*>(userdata);<--- Variable 'file' can be declared as pointer to const
if ((qint64)target_size > file->size())
{
return heif_reader_grow_status_size_beyond_eof;
}
return heif_reader_grow_status_size_reached;
}
bool s_isHeifSuccess(const struct heif_error* const error)
{
if (error->code == 0)
{
return true;
}
qCWarning(DIGIKAM_METAENGINE_LOG) << "Error while processing HEIF image:" << error->message;
return false;
}
void s_readHEICMetadata(struct heif_context* const heif_context, heif_item_id image_id,
QByteArray& exif, QByteArray& iptc, QByteArray& xmp)
{
struct heif_image_handle* image_handle = nullptr;
struct heif_error error1 = heif_context_get_image_handle(heif_context,
image_id,
&image_handle);
if (!s_isHeifSuccess(&error1))
{
heif_image_handle_release(image_handle);
return;
}
heif_item_id dataIds[10];
int count = heif_image_handle_get_list_of_metadata_block_IDs(image_handle,
nullptr,
dataIds,
10);
qDebug(DIGIKAM_METAENGINE_LOG) << "Found" << count << "HEIF metadata chunk";
if (count > 0)
{
for (int i = 0 ; i < count ; ++i)
{
qDebug(DIGIKAM_METAENGINE_LOG) << "Parsing HEIF metadata chunk:" << heif_image_handle_get_metadata_type(image_handle, dataIds[i]);
if (QLatin1String(heif_image_handle_get_metadata_type(image_handle, dataIds[i])) == QLatin1String("Exif"))
{
// Read Exif chunk.
size_t length = heif_image_handle_get_metadata_size(image_handle, dataIds[i]);
QByteArray exifChunk;
exifChunk.resize((int)length);
struct heif_error error2 = heif_image_handle_get_metadata(image_handle,
dataIds[i],
exifChunk.data());
if ((error2.code == 0) && (length > 4))
{
// The first 4 bytes indicate the
// offset to the start of the TIFF header of the Exif data.
int skip = (
(exifChunk.constData()[0] << 24) |
(exifChunk.constData()[1] << 16) |
(exifChunk.constData()[2] << 8) |
exifChunk.constData()[3]
) + 4;
if (exifChunk.size() > skip)
{
// Copy the real exif data into the byte array
qDebug(DIGIKAM_METAENGINE_LOG) << "HEIF exif container found with size:" << length - skip;
exif.append(reinterpret_cast<char*>(exifChunk.data() + skip), exifChunk.size() - skip);
}
}
}
if (QLatin1String(heif_image_handle_get_metadata_type(image_handle, dataIds[i])) == QLatin1String("iptc"))
{
// Read Iptc chunk.
size_t length = heif_image_handle_get_metadata_size(image_handle, dataIds[i]);
iptc.resize((int)length);
struct heif_error error3 = heif_image_handle_get_metadata(image_handle,
dataIds[i],
iptc.data());
if (error3.code == 0)
{
qDebug(DIGIKAM_METAENGINE_LOG) << "HEIF iptc container found with size:" << length;
}
else
{
iptc = QByteArray();
}
}
if (
(QLatin1String(heif_image_handle_get_metadata_type(image_handle, dataIds[i])) == QLatin1String("mime")) &&
(QLatin1String(heif_image_handle_get_metadata_content_type(image_handle, dataIds[i])) == QLatin1String("application/rdf+xml"))
)
{
// Read Xmp chunk.
size_t length = heif_image_handle_get_metadata_size(image_handle, dataIds[i]);
xmp.resize((int)length);
struct heif_error error4 = heif_image_handle_get_metadata(image_handle,
dataIds[i],
xmp.data());
if (error4.code == 0)
{
qDebug(DIGIKAM_METAENGINE_LOG) << "HEIF xmp container found with size:" << length;
}
else
{
xmp = QByteArray();
}
}
}
}
heif_image_handle_release(image_handle);
}
bool DMetadata::loadUsingLibheif(const QString& filePath)
{
QFileInfo fileInfo(filePath);
QString ext = fileInfo.suffix().toUpper();
if (
!fileInfo.exists() || ext.isEmpty() ||
(ext != QLatin1String("HEIF")) ||
(ext != QLatin1String("HEIC")) ||
(ext != QLatin1String("HIF"))
)
{
return false;
}
QFile readFile(filePath);
if (!readFile.open(QIODevice::ReadOnly))
{
qCWarning(DIGIKAM_METAENGINE_LOG) << "Error: Could not open source file.";
return false;
}
const qint64 headerLen = 12;
QByteArray header(headerLen, '\0');
if (readFile.read(reinterpret_cast<char*>(header.data()), headerLen) != headerLen)
{
qCWarning(DIGIKAM_METAENGINE_LOG) << "Error: Could not parse magic identifier.";
return false;
}
if (
(memcmp(&header.data()[4], "ftyp", 4) != 0) &&
(memcmp(&header.data()[8], "heic", 4) != 0) &&
(memcmp(&header.data()[8], "heix", 4) != 0) &&
(memcmp(&header.data()[8], "mif1", 4) != 0)
)
{
qCWarning(DIGIKAM_METAENGINE_LOG) << "Error: source file is not HEIF image.";
return false;
}
readFile.reset();
// -------------------------------------------------------------------
// Initialize HEIF API.
heif_item_id primary_image_id;
struct heif_context* const heif_context = heif_context_alloc();
heif_reader reader;
reader.reader_api_version = 1;
reader.get_position = heifQIODeviceMetaGetPosition;
reader.read = heifQIODeviceMetaRead;
reader.seek = heifQIODeviceMetaSeek;
reader.wait_for_file_size = heifQIODeviceMetaWait;
struct heif_error error = heif_context_read_from_reader(heif_context,
&reader,
reinterpret_cast<void*>(&readFile),
nullptr);
if (!s_isHeifSuccess(&error))
{
qCWarning(DIGIKAM_METAENGINE_LOG) << "Error: Could not read source file.";
heif_context_free(heif_context);
return false;
}
error = heif_context_get_primary_image_ID(heif_context, &primary_image_id);
if (!s_isHeifSuccess(&error))
{
qCWarning(DIGIKAM_METAENGINE_LOG) << "Error: Could not load image data.";
heif_context_free(heif_context);
return false;
}
QByteArray exif;
QByteArray iptc;
QByteArray xmp;
bool ret = false;
s_readHEICMetadata(heif_context, primary_image_id, exif, iptc, xmp);
if (!exif.isEmpty() || !iptc.isEmpty() || !xmp.isEmpty())
{
if (!exif.isEmpty())
{
setExif(exif);
}
if (!iptc.isEmpty())
{
setIptc(iptc);
}
if (!xmp.isEmpty())
{
setXmp(xmp);
}
ret = true;
}
heif_context_free(heif_context);
return ret;
}
} // namespace Digikam
|