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

0001 /*
0002     AV1 Image File Format (AVIF) support for QImage.
0003 
0004     SPDX-FileCopyrightText: 2020 Daniel Novomesky <dnovomesky@gmail.com>
0005 
0006     SPDX-License-Identifier: BSD-2-Clause
0007 */
0008 
0009 #ifndef KIMG_AVIF_P_H
0010 #define KIMG_AVIF_P_H
0011 
0012 #include <QByteArray>
0013 #include <QImage>
0014 #include <QImageIOPlugin>
0015 #include <QPointF>
0016 #include <QSize>
0017 #include <QVariant>
0018 #include <avif/avif.h>
0019 #include <qimageiohandler.h>
0020 
0021 class QAVIFHandler : public QImageIOHandler
0022 {
0023 public:
0024     QAVIFHandler();
0025     ~QAVIFHandler();
0026 
0027     bool canRead() const override;
0028     bool read(QImage *image) override;
0029     bool write(const QImage &image) override;
0030 
0031     static bool canRead(QIODevice *device);
0032 
0033     QVariant option(ImageOption option) const override;
0034     void setOption(ImageOption option, const QVariant &value) override;
0035     bool supportsOption(ImageOption option) const override;
0036 
0037     int imageCount() const override;
0038     int currentImageNumber() const override;
0039     bool jumpToNextImage() override;
0040     bool jumpToImage(int imageNumber) override;
0041 
0042     int nextImageDelay() const override;
0043 
0044     int loopCount() const override;
0045 
0046 private:
0047     static QPointF CompatibleChromacity(qreal chrX, qreal chrY);
0048     bool ensureParsed() const;
0049     bool ensureOpened() const;
0050     bool ensureDecoder();
0051     bool decode_one_frame();
0052 
0053     enum ParseAvifState {
0054         ParseAvifError = -1,
0055         ParseAvifNotParsed = 0,
0056         ParseAvifSuccess = 1,
0057         ParseAvifMetadata = 2,
0058         ParseAvifFinished = 3,
0059     };
0060 
0061     ParseAvifState m_parseState;
0062     int m_quality;
0063 
0064     uint32_t m_container_width;
0065     uint32_t m_container_height;
0066     QSize m_estimated_dimensions;
0067 
0068     QByteArray m_rawData;
0069     avifROData m_rawAvifData;
0070 
0071     avifDecoder *m_decoder;
0072     QImage m_current_image;
0073 
0074     bool m_must_jump_to_next_image;
0075 };
0076 
0077 class QAVIFPlugin : public QImageIOPlugin
0078 {
0079     Q_OBJECT
0080     Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QImageIOHandlerFactoryInterface" FILE "avif.json")
0081 
0082 public:
0083     Capabilities capabilities(QIODevice *device, const QByteArray &format) const override;
0084     QImageIOHandler *create(QIODevice *device, const QByteArray &format = QByteArray()) const override;
0085 };
0086 
0087 #endif // KIMG_AVIF_P_H