1 /* ============================================================
2  *
3  * This file is a part of digiKam project
4  * https://www.digikam.org
5  *
6  * Date        : 2019-09-26
7  * Description : A HEIF IO file for DImg framework
8  *
9  * Copyright (C) 2019-2021 by Gilles Caulier <caulier dot gilles at gmail dot com>
10  *
11  * Other HEIF loader implementions:
12  *     https://github.com/KDE/krita/tree/master/plugins/impex/heif
13  *     https://github.com/jakar/qt-heif-image-plugin
14  *     https://github.com/ImageMagick/ImageMagick/blob/master/coders/heic.c
15  *     https://github.com/GNOME/gimp/blob/master/plug-ins/common/file-heif.c
16  *
17  * Specification: https://nokiatech.github.io/heif/technical.html
18  *
19  * This program is free software; you can redistribute it
20  * and/or modify it under the terms of the GNU General
21  * Public License as published by the Free Software Foundation;
22  * either version 2, or (at your option)
23  * any later version.
24  *
25  * This program is distributed in the hope that it will be useful,
26  * but WITHOUT ANY WARRANTY; without even the implied warranty of
27  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28  * GNU General Public License for more details.
29  *
30  * ============================================================ */
31 
32 #ifndef DIGIKAM_DIMG_HEIF_LOADER_H
33 #define DIGIKAM_DIMG_HEIF_LOADER_H
34 
35 // Local includes
36 
37 #include "dimg.h"
38 #include "dimgloader.h"
39 #include "heif.h"
40 
41 using namespace Digikam;
42 
43 namespace Digikam
44 {
45 
46 class DImgHEIFLoader : public DImgLoader
47 {
48 
49 public:
50 
51     explicit DImgHEIFLoader(DImg* const image);
52     ~DImgHEIFLoader()                                                      override;
53 
54     bool load(const QString& filePath, DImgLoaderObserver* const observer) override;
55     bool save(const QString& filePath, DImgLoaderObserver* const observer) override;
56 
57     bool hasAlpha()                                                  const override;
58     bool sixteenBit()                                                const override;
59     bool isReadOnly()                                                const override;
60 
61     /**
62      * Determine libx265 encoder bits depth capability: 8=standard, 10, 12, or more.
63      * Return -1 if encoder instance is not found.
64      */
65     static int x265MaxBitsDepth();
66 
67 private:
68 
69     bool isHeifSuccess(struct heif_error* const error);
70 
71     /// Read operations
72 
73     bool readHEICColorProfile(struct heif_image_handle* const image_handle);
74     bool readHEICImageByID(struct heif_context* const heif_context,
75                            heif_item_id image_id);
76 
77     bool readHEICImageByHandle(struct heif_image_handle* image_handle,
78                                struct heif_image* heif_image, bool loadImageData);
79 
80     /// Save operations
81 
82     bool saveHEICColorProfile(struct heif_image* const image);
83 
84     bool saveHEICMetadata(struct heif_context* const heif_context,
85                           struct heif_image_handle* const image_handle);
86 
87 private:
88 
89     bool                m_sixteenBit;
90     bool                m_hasAlpha;
91 
92     DImgLoaderObserver* m_observer;
93 };
94 
95 } // namespace Digikam
96 
97 #endif // DIGIKAM_DIMG_HEIF_LOADER_H
98