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
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640 | /* ============================================================
*
* This file is a part of digiKam project
* https://www.digikam.org
*
* Date : 2019-09-26
* Description : A HEIF IO file for DImg framework - save operations
*
* SPDX-FileCopyrightText: 2019-2025 by Gilles Caulier <caulier dot gilles at gmail dot com>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*
* ============================================================ */
#include "dimgheifloader.h"
// Qt includes
#include <QFile>
#include <QVariant>
#include <QByteArray>
#include <QTextStream>
#include <QElapsedTimer>
#include <qplatformdefs.h>
#include <QScopedPointer>
// Local includes
#include "digikam_config.h"
#include "digikam_debug.h"
#include "dimg.h"
#include "dimgloaderobserver.h"
#include "metaengine.h"
// libx265 includes
#if defined(__clang__)
# pragma clang diagnostic push
# pragma clang diagnostic ignored "-Wundef"
# pragma clang diagnostic ignored "-Wgnu-anonymous-struct"
# pragma clang diagnostic ignored "-Wnested-anon-types"
#endif
#ifdef HAVE_X265
# include <x265.h>
#endif
#if defined(__clang__)
# pragma clang diagnostic pop
#endif
namespace Digikam
{
static struct heif_error heifQIODeviceWriter(struct heif_context* /* ctx */,
const void* data, size_t size, void* userdata) // cppcheck-suppress constParameterCallback
{
QFile saveFile(QString::fromUtf8(static_cast<const char*>(userdata)));
heif_error error;
if (!saveFile.open(QIODevice::WriteOnly))
{
qCWarning(DIGIKAM_DIMG_LOG_HEIF) << "Cannot open target image file:"
<< saveFile.fileName();
error.code = heif_error_Encoding_error;
error.subcode = heif_suberror_Cannot_write_output_data;
error.message = QByteArray("File open error").constData();
return error;
}
error.code = heif_error_Ok;
error.subcode = heif_suberror_Unspecified;
error.message = QByteArray("Success").constData();
qint64 bytesWritten = saveFile.write((const char*)data, 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.
if (bytesWritten < (qint64)size)
{
error.code = heif_error_Encoding_error;
error.subcode = heif_suberror_Cannot_write_output_data;
error.message = QByteArray("File write error").constData();
}
saveFile.close();
return error;
}
int DImgHEIFLoader::x265MaxBitsDepth()
{
int maxOutputBitsDepth = -1;
#ifdef HAVE_X265
qCDebug(DIGIKAM_DIMG_LOG_HEIF) << "HEVC encoder max bit depth:" << x265_max_bit_depth;
const x265_api* api = x265_api_get(x265_max_bit_depth);
if (api)
{
maxOutputBitsDepth = x265_max_bit_depth;
qCDebug(DIGIKAM_DIMG_LOG_HEIF) << "HEVC encoder max bits depth:" << maxOutputBitsDepth;
}
else
{
api = x265_api_get(8); // Try to failback to default 8 bits
if (api)
{
maxOutputBitsDepth = 8;
qCDebug(DIGIKAM_DIMG_LOG_HEIF) << "HEVC encoder max bits depth: 8 (default failback value)";
}
}
#endif
// cppcheck-suppress knownConditionTrueFalse
if (maxOutputBitsDepth == -1)
{
qCWarning(DIGIKAM_DIMG_LOG_HEIF) << "Cannot get max supported HEVC encoder bits depth!";
}
return maxOutputBitsDepth;
}
bool DImgHEIFLoader::save(const QString& filePath, DImgLoaderObserver* const observer)
{
m_observer = observer;
QVariant qualityAttr = imageGetAttribute(QLatin1String("quality"));
int quality = qualityAttr.isValid() ? qualityAttr.toInt() : 75;
bool lossless = (quality == 0);
// --- Determine libx265 encoder bits depth capability: 8=standard, 10, 12, or later 16.
int maxOutputBitsDepth = x265MaxBitsDepth();
if (maxOutputBitsDepth == -1)
{
return false;
}
heif_chroma chroma;
if (maxOutputBitsDepth > 8) // 16 bits image.
{
chroma = imageHasAlpha() ? heif_chroma_interleaved_RRGGBBAA_BE
: heif_chroma_interleaved_RRGGBB_BE;
}
else
{
chroma = imageHasAlpha() ? heif_chroma_interleaved_RGBA
: heif_chroma_interleaved_RGB;
}
// --- use standard HEVC encoder
QElapsedTimer timer;
timer.start();
qCDebug(DIGIKAM_DIMG_LOG_HEIF) << "HEVC encoder setup...";
#if LIBHEIF_NUMERIC_VERSION >= 0x010d0000
heif_init(nullptr);
#endif
struct heif_context* const ctx = heif_context_alloc();
if (!ctx)
{
qCWarning(DIGIKAM_DIMG_LOG_HEIF) << "Cannot create HEIF context!";
return false;
}
struct heif_encoder* encoder = nullptr;
struct heif_error error = heif_context_get_encoder_for_format(ctx,
heif_compression_HEVC,
&encoder);
if (!isHeifSuccess(&error))
{
heif_context_free(ctx);
#if LIBHEIF_NUMERIC_VERSION >= 0x010d0000
heif_deinit();
#endif
return false;
}
heif_encoder_set_lossy_quality(encoder, quality);
heif_encoder_set_lossless(encoder, lossless);
struct heif_image* image = nullptr;
error = heif_image_create(imageWidth(),
imageHeight(),
heif_colorspace_RGB,
chroma,
&image);
if (!isHeifSuccess(&error))
{
heif_encoder_release(encoder);
heif_context_free(ctx);
#if LIBHEIF_NUMERIC_VERSION >= 0x010d0000
heif_deinit();
#endif
return false;
}
// --- Save color profile before to create image data, as converting to color space can be processed at this stage.
qCDebug(DIGIKAM_DIMG_LOG_HEIF) << "HEIF set color profile...";
saveHEICColorProfile(image);
// --- Add image data
qCDebug(DIGIKAM_DIMG_LOG_HEIF) << "HEIF setup data plane...";
error = heif_image_add_plane(image,
heif_channel_interleaved,
imageWidth(),
imageHeight(),
maxOutputBitsDepth);
if (!isHeifSuccess(&error))
{
heif_encoder_release(encoder);
heif_context_free(ctx);
#if LIBHEIF_NUMERIC_VERSION >= 0x010d0000
heif_deinit();
#endif
return false;
}
int stride = 0;
uint8_t* const data = heif_image_get_plane(image,
heif_channel_interleaved,
&stride);
if (!data || (stride <= 0))
{
qCWarning(DIGIKAM_DIMG_LOG_HEIF) << "HEIF data pixels information not valid!";
heif_encoder_release(encoder);
heif_context_free(ctx);
#if LIBHEIF_NUMERIC_VERSION >= 0x010d0000
heif_deinit();
#endif
return false;
}
qCDebug(DIGIKAM_DIMG_LOG_HEIF) << "HEIF data container:" << data;
qCDebug(DIGIKAM_DIMG_LOG_HEIF) << "HEIF bytes per line:" << stride;
uint checkpoint = 0;
unsigned char r = 0;
unsigned char g = 0;
unsigned char b = 0;
unsigned char a = 0;
unsigned char* src = nullptr;
unsigned char* dst = nullptr;
unsigned short r16 = 0;
unsigned short g16 = 0;
unsigned short b16 = 0;
unsigned short a16 = 0;
unsigned short* src16 = nullptr;
unsigned short* dst16 = nullptr;
int div16 = 16 - maxOutputBitsDepth;
int mul8 = maxOutputBitsDepth - 8;
int nbOutputBytesPerColor = (maxOutputBitsDepth > 8) ? (imageHasAlpha() ? 4 * 2 : 3 * 2) // output data stored on 16 bits
: (imageHasAlpha() ? 4 : 3 ); // output data stored on 8 bits
qCDebug(DIGIKAM_DIMG_LOG_HEIF) << "HEIF output bytes per color:" << nbOutputBytesPerColor;
qCDebug(DIGIKAM_DIMG_LOG_HEIF) << "HEIF 16 to 8 bits coeff. :" << div16;
qCDebug(DIGIKAM_DIMG_LOG_HEIF) << "HEIF 8 to 16 bits coeff. :" << mul8;
for (unsigned int y = 0 ; y < imageHeight() ; ++y)
{
src = &imageData()[(y * imageWidth()) * imageBytesDepth()];
src16 = reinterpret_cast<unsigned short*>(src);
dst = reinterpret_cast<unsigned char*>(data + (y * stride));
dst16 = reinterpret_cast<unsigned short*>(dst);
for (unsigned int x = 0 ; x < imageWidth() ; ++x)
{
if (imageSixteenBit()) // 16 bits source image.
{
b16 = src16[0];
g16 = src16[1];
r16 = src16[2];
if (imageHasAlpha())
{
a16 = src16[3];
}
if (maxOutputBitsDepth > 8) // From 16 bits to 10 bits or more.
{
dst16[0] = (unsigned short)(r16 >> div16);
dst16[1] = (unsigned short)(g16 >> div16);
dst16[2] = (unsigned short)(b16 >> div16);
if (imageHasAlpha())
{
dst16[3] = (unsigned short)(a16 >> div16);
dst16 += 4;
}
else
{
dst16 += 3;
}
}
else // From 16 bits to 8 bits.
{
dst[0] = (unsigned char)(r16 >> div16);
dst[1] = (unsigned char)(g16 >> div16);
dst[2] = (unsigned char)(b16 >> div16);
if (imageHasAlpha())
{
dst[3] = (unsigned char)(a16 >> div16);
dst += 4;
}
else
{
dst += 3;
}
}
src16 += 4;
}
else // 8 bits source image.
{
b = src[0];
g = src[1];
r = src[2];
if (imageHasAlpha())
{
a = src[3];
}
if (maxOutputBitsDepth > 8) // From 8 bits to 10 bits or more.
{
dst16[0] = (unsigned short)(r << mul8);
dst16[1] = (unsigned short)(g << mul8);
dst16[2] = (unsigned short)(b << mul8);
if (imageHasAlpha())
{
dst16[3] = (unsigned short)(a << mul8);
dst16 += 4;
}
else
{
dst16 += 3;
}
}
else // From 8 bits to 8 bits.
{
dst[0] = r;
dst[1] = g;
dst[2] = b;
if (imageHasAlpha())
{
dst[3] = a;
dst += 4;
}
else
{
dst += 3;
}
}
src += 4;
}
}
if (m_observer && (y == (unsigned int)checkpoint))
{
checkpoint += granularity(m_observer, imageHeight(), 0.8F);
if (!m_observer->continueQuery())
{
heif_encoder_release(encoder);
heif_context_free(ctx);
#if LIBHEIF_NUMERIC_VERSION >= 0x010d0000
heif_deinit();
#endif
return false;
}
m_observer->progressInfo(0.1F + (0.8F * (((float)y) / ((float)imageHeight()))));
}
}
qCDebug(DIGIKAM_DIMG_LOG_HEIF) << "HEIF master image encoding...";
// --- encode and write master image
struct heif_encoding_options* const options = heif_encoding_options_alloc();
options->save_alpha_channel = imageHasAlpha() ? 1 : 0;
struct heif_image_handle* image_handle = nullptr;
error = heif_context_encode_image(ctx,
image,
encoder,
options,
&image_handle);
if (!isHeifSuccess(&error))
{
heif_encoding_options_free(options);
heif_image_handle_release(image_handle);
heif_encoder_release(encoder);
heif_context_free(ctx);
#if LIBHEIF_NUMERIC_VERSION >= 0x010d0000
heif_deinit();
#endif
return false;
}
// --- encode thumbnail
// Note: Only encode preview for large image.
// We will use the same preview size than DImg::prepareMetadataToSave()
const int previewSize = 1280;
if (qMin(imageWidth(), imageHeight()) > previewSize)
{
qCDebug(DIGIKAM_DIMG_LOG_HEIF) << "HEIF preview storage in thumbnail chunk...";
struct heif_image_handle* thumbnail_handle = nullptr;
error = heif_context_encode_thumbnail(ctx,
image,
image_handle,
encoder,
options,
previewSize,
&thumbnail_handle);
if (!isHeifSuccess(&error))
{
heif_encoding_options_free(options);
heif_image_handle_release(image_handle);
heif_encoder_release(encoder);
heif_context_free(ctx);
#if LIBHEIF_NUMERIC_VERSION >= 0x010d0000
heif_deinit();
#endif
return false;
}
heif_image_handle_release(thumbnail_handle);
}
heif_encoding_options_free(options);
heif_encoder_release(encoder);
// --- Add Exif and XMP metadata
qCDebug(DIGIKAM_DIMG_LOG_HEIF) << "HEIF metadata storage...";
saveHEICMetadata(ctx, image_handle);
heif_image_handle_release(image_handle);
// --- write HEIF file
qCDebug(DIGIKAM_DIMG_LOG_HEIF) << "HEIF flush to file...";
qCDebug(DIGIKAM_DIMG_LOG_HEIF) << "HEIF encoding took:" << timer.elapsed() << "ms";
heif_writer writer;
writer.writer_api_version = 1;
writer.write = heifQIODeviceWriter;
error = heif_context_write(ctx, &writer, (void*)filePath.toUtf8().constData());<--- 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.
if (!isHeifSuccess(&error))
{
heif_context_free(ctx);
#if LIBHEIF_NUMERIC_VERSION >= 0x010d0000
heif_deinit();
#endif
return false;
}
heif_context_free(ctx);
imageSetAttribute(QLatin1String("savedFormat"), QLatin1String("HEIF"));
saveMetadata(filePath);
#if LIBHEIF_NUMERIC_VERSION >= 0x010d0000
heif_deinit();
#endif
return true;
}
bool DImgHEIFLoader::saveHEICColorProfile(struct heif_image* const image)
{
#if LIBHEIF_NUMERIC_VERSION >= 0x01040000
QByteArray profile = m_image->getIccProfile().data();
if (!profile.isEmpty())
{
// Save color profile.
struct heif_error error = heif_image_set_raw_color_profile(image,
"prof", // FIXME: detect string in profile data
profile.data(),
profile.size());
if (error.code != 0)
{
qCWarning(DIGIKAM_DIMG_LOG_HEIF) << "Cannot set HEIF color profile!";
return false;
}
qCDebug(DIGIKAM_DIMG_LOG_HEIF) << "Stored HEIF color profile size:" << profile.size();
}
#else
Q_UNUSED(image_handle);
#endif
return true;
}
bool DImgHEIFLoader::saveHEICMetadata(struct heif_context* const heif_context,
struct heif_image_handle* const image_handle)
{
QScopedPointer<MetaEngine> meta(new MetaEngine(m_image->getMetadata()));
if (!meta->hasExif() && !meta->hasIptc() && !meta->hasXmp())
{
return false;
}
QByteArray exif = meta->getExifEncoded();
QByteArray iptc = meta->getIptc();
QByteArray xmp = meta->getXmp();
struct heif_error error;
if (!exif.isEmpty())
{
error = heif_context_add_exif_metadata(heif_context,
image_handle,
exif.data(),
exif.size());
if (error.code != 0)
{
qCWarning(DIGIKAM_DIMG_LOG_HEIF) << "Cannot store HEIF Exif metadata!";
return false;
}
qCDebug(DIGIKAM_DIMG_LOG_HEIF) << "Stored HEIF Exif data size:" << exif.size();
}
if (!iptc.isEmpty())
{
error = heif_context_add_generic_metadata(heif_context,
image_handle,
iptc.data(),
iptc.size(),
"iptc",
nullptr);
if (error.code != 0)
{
qCWarning(DIGIKAM_DIMG_LOG_HEIF) << "Cannot store HEIF Iptc metadata!";
return false;
}
qCDebug(DIGIKAM_DIMG_LOG_HEIF) << "Stored HEIF Iptc data size:" << iptc.size();
}
if (!xmp.isEmpty())
{
error = heif_context_add_XMP_metadata(heif_context,
image_handle,
xmp.data(),
xmp.size());
if (error.code != 0)
{
qCWarning(DIGIKAM_DIMG_LOG_HEIF) << "Cannot store HEIF Xmp metadata!";
return false;
}
qCDebug(DIGIKAM_DIMG_LOG_HEIF) << "Stored HEIF Xmp data size:" << xmp.size();
}
return true;
}
} // namespace Digikam
|