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

0001 /*
0002     JPEG XL (JXL) support for QImage.
0003 
0004     SPDX-FileCopyrightText: 2021 Daniel Novomesky <dnovomesky@gmail.com>
0005 
0006     SPDX-License-Identifier: BSD-2-Clause
0007 */
0008 
0009 #ifndef KIMG_JXL_P_H
0010 #define KIMG_JXL_P_H
0011 
0012 #include <QByteArray>
0013 #include <QColorSpace>
0014 #include <QImage>
0015 #include <QImageIOHandler>
0016 #include <QImageIOPlugin>
0017 #include <QVariant>
0018 #include <QVector>
0019 
0020 #include <jxl/decode.h>
0021 
0022 class QJpegXLHandler : public QImageIOHandler
0023 {
0024 public:
0025     QJpegXLHandler();
0026     ~QJpegXLHandler();
0027 
0028     bool canRead() const override;
0029     bool read(QImage *image) override;
0030     bool write(const QImage &image) override;
0031 
0032     static bool canRead(QIODevice *device);
0033 
0034     QVariant option(ImageOption option) const override;
0035     void setOption(ImageOption option, const QVariant &value) override;
0036     bool supportsOption(ImageOption option) const override;
0037 
0038     int imageCount() const override;
0039     int currentImageNumber() const override;
0040     bool jumpToNextImage() override;
0041     bool jumpToImage(int imageNumber) override;
0042 
0043     int nextImageDelay() const override;
0044 
0045     int loopCount() const override;
0046 
0047 private:
0048     bool ensureParsed() const;
0049     bool ensureALLCounted() const;
0050     bool ensureDecoder();
0051     bool countALLFrames();
0052     bool decode_one_frame();
0053     bool rewind();
0054 
0055     enum ParseJpegXLState {
0056         ParseJpegXLError = -1,
0057         ParseJpegXLNotParsed = 0,
0058         ParseJpegXLSuccess = 1,
0059         ParseJpegXLBasicInfoParsed = 2,
0060         ParseJpegXLFinished = 3,
0061     };
0062 
0063     ParseJpegXLState m_parseState;
0064     int m_quality;
0065     int m_currentimage_index;
0066     int m_previousimage_index;
0067 
0068     QByteArray m_rawData;
0069 
0070     JxlDecoder *m_decoder;
0071     void *m_runner;
0072     JxlBasicInfo m_basicinfo;
0073 
0074     QVector<int> m_framedelays;
0075     int m_next_image_delay;
0076 
0077     QImage m_current_image;
0078     QColorSpace m_colorspace;
0079 
0080     QImage::Format m_input_image_format;
0081     QImage::Format m_target_image_format;
0082 
0083     JxlPixelFormat m_input_pixel_format;
0084     size_t m_buffer_size;
0085 };
0086 
0087 class QJpegXLPlugin : public QImageIOPlugin
0088 {
0089     Q_OBJECT
0090     Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QImageIOHandlerFactoryInterface" FILE "jxl.json")
0091 
0092 public:
0093     Capabilities capabilities(QIODevice *device, const QByteArray &format) const override;
0094     QImageIOHandler *create(QIODevice *device, const QByteArray &format = QByteArray()) const override;
0095 };
0096 
0097 #endif // KIMG_JXL_P_H