File indexing completed on 2024-04-28 15:25:40

0001 /*
0002     High Efficiency Image File Format (HEIF) support for QImage.
0003 
0004     SPDX-FileCopyrightText: 2020 Sirius Bakke <sirius@bakke.co>
0005     SPDX-FileCopyrightText: 2021 Daniel Novomesky <dnovomesky@gmail.com>
0006 
0007     SPDX-License-Identifier: LGPL-2.0-or-later
0008 */
0009 
0010 #ifndef KIMG_HEIF_P_H
0011 #define KIMG_HEIF_P_H
0012 
0013 #include <QByteArray>
0014 #include <QImage>
0015 #include <QImageIOPlugin>
0016 #include <QMutex>
0017 
0018 class HEIFHandler : public QImageIOHandler
0019 {
0020 public:
0021     HEIFHandler();
0022 
0023     bool canRead() const override;
0024     bool read(QImage *image) override;
0025     bool write(const QImage &image) override;
0026 
0027     QVariant option(ImageOption option) const override;
0028     void setOption(ImageOption option, const QVariant &value) override;
0029     bool supportsOption(ImageOption option) const override;
0030 
0031     static bool isHeifDecoderAvailable();
0032     static bool isHeifEncoderAvailable();
0033     static bool isHej2DecoderAvailable();
0034 
0035     static bool isSupportedBMFFType(const QByteArray &header);
0036     static bool isSupportedHEJ2(const QByteArray &header);
0037 
0038 private:
0039     bool ensureParsed() const;
0040     bool ensureDecoder();
0041 
0042     enum ParseHeicState {
0043         ParseHeicError = -1,
0044         ParseHeicNotParsed = 0,
0045         ParseHeicSuccess = 1,
0046     };
0047 
0048     ParseHeicState m_parseState;
0049     int m_quality;
0050     QImage m_current_image;
0051 
0052     bool write_helper(const QImage &image);
0053 
0054     static void startHeifLib();
0055     static void finishHeifLib();
0056     static size_t m_initialized_count;
0057 
0058     static bool m_plugins_queried;
0059     static bool m_heif_decoder_available;
0060     static bool m_heif_encoder_available;
0061     static bool m_hej2_decoder_available;
0062 
0063     static QMutex &getHEIFHandlerMutex();
0064 };
0065 
0066 class HEIFPlugin : public QImageIOPlugin
0067 {
0068     Q_OBJECT
0069     Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QImageIOHandlerFactoryInterface" FILE "heif.json")
0070 
0071 public:
0072     Capabilities capabilities(QIODevice *device, const QByteArray &format) const override;
0073     QImageIOHandler *create(QIODevice *device, const QByteArray &format = QByteArray()) const override;
0074 };
0075 
0076 #endif // KIMG_HEIF_P_H