| 1 | /* |
| 2 | This file is part of the KDE project |
| 3 | SPDX-FileCopyrightText: 2015 The Qt Company Ltd |
| 4 | SPDX-FileCopyrightText: 2013 Ivan Komissarov |
| 5 | SPDX-FileCopyrightText: 2024 Mirco Miranda |
| 6 | |
| 7 | SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only |
| 8 | */ |
| 9 | |
| 10 | #ifndef QDDSHANDLER_H |
| 11 | #define QDDSHANDLER_H |
| 12 | |
| 13 | #include <QImageIOPlugin> |
| 14 | |
| 15 | struct DDSPixelFormat |
| 16 | { |
| 17 | enum DDSPixelFormatFlags { |
| 18 | FlagAlphaPixels = 0x00000001, |
| 19 | FlagAlpha = 0x00000002, |
| 20 | FlagFourCC = 0x00000004, |
| 21 | FlagPaletteIndexed4 = 0x00000008, |
| 22 | FlagPaletteIndexed8 = 0x00000020, |
| 23 | FlagRGB = 0x00000040, |
| 24 | FlagYUV = 0x00000200, |
| 25 | FlagLuminance = 0x00020000, |
| 26 | FlagNormal = 0x00080000, |
| 27 | FlagRGBA = FlagAlphaPixels | FlagRGB, |
| 28 | FlagLA = FlagAlphaPixels | FlagLuminance |
| 29 | }; |
| 30 | |
| 31 | quint32 size; |
| 32 | quint32 flags; |
| 33 | quint32 fourCC; |
| 34 | quint32 rgbBitCount; |
| 35 | quint32 rBitMask; |
| 36 | quint32 gBitMask; |
| 37 | quint32 bBitMask; |
| 38 | quint32 aBitMask; |
| 39 | }; |
| 40 | |
| 41 | struct |
| 42 | { |
| 43 | quint32 = 0; |
| 44 | quint32 = 0; |
| 45 | quint32 = 0; |
| 46 | quint32 = 0; |
| 47 | quint32 = 0; |
| 48 | }; |
| 49 | |
| 50 | struct |
| 51 | { |
| 52 | enum { |
| 53 | = 0x000001, |
| 54 | = 0x000002, |
| 55 | = 0x000004, |
| 56 | = 0x000008, |
| 57 | = 0x001000, |
| 58 | = 0x020000, |
| 59 | = 0x080000, |
| 60 | = 0x800000 |
| 61 | }; |
| 62 | |
| 63 | enum { |
| 64 | = 0x000008, |
| 65 | = 0x001000, |
| 66 | = 0x400000 |
| 67 | }; |
| 68 | |
| 69 | enum { |
| 70 | = 0x0200, |
| 71 | = 0x0400, |
| 72 | = 0x0800, |
| 73 | = 0x1000, |
| 74 | = 0x2000, |
| 75 | = 0x4000, |
| 76 | = 0x8000, |
| 77 | = 0x200000 |
| 78 | }; |
| 79 | |
| 80 | enum { = 11 }; |
| 81 | |
| 82 | quint32 ; |
| 83 | quint32 ; |
| 84 | quint32 ; |
| 85 | quint32 ; |
| 86 | quint32 ; |
| 87 | quint32 ; |
| 88 | quint32 ; |
| 89 | quint32 ; |
| 90 | quint32 [ReservedCount]; |
| 91 | DDSPixelFormat ; |
| 92 | quint32 ; |
| 93 | quint32 ; |
| 94 | quint32 ; |
| 95 | quint32 ; |
| 96 | quint32 ; |
| 97 | DDSHeaderDX10 ; |
| 98 | }; |
| 99 | |
| 100 | class QDDSHandler : public QImageIOHandler |
| 101 | { |
| 102 | public: |
| 103 | QDDSHandler(); |
| 104 | |
| 105 | bool canRead() const override; |
| 106 | bool read(QImage *image) override; |
| 107 | bool write(const QImage &image) override; |
| 108 | |
| 109 | QVariant option(QImageIOHandler::ImageOption option) const override; |
| 110 | void setOption(ImageOption option, const QVariant &value) override; |
| 111 | bool supportsOption(QImageIOHandler::ImageOption option) const override; |
| 112 | |
| 113 | int imageCount() const override; |
| 114 | bool jumpToImage(int imageNumber) override; |
| 115 | |
| 116 | static bool canRead(QIODevice *device); |
| 117 | |
| 118 | private: |
| 119 | bool ensureScanned() const; |
| 120 | bool verifyHeader(const DDSHeader &dds) const; |
| 121 | |
| 122 | private: |
| 123 | enum ScanState { |
| 124 | ScanError = -1, |
| 125 | ScanNotScanned = 0, |
| 126 | ScanSuccess = 1, |
| 127 | }; |
| 128 | |
| 129 | DDSHeader m_header; |
| 130 | int m_format; |
| 131 | int m_currentImage; |
| 132 | mutable ScanState m_scanState; |
| 133 | }; |
| 134 | |
| 135 | class QDDSPlugin : public QImageIOPlugin |
| 136 | { |
| 137 | Q_OBJECT |
| 138 | Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QImageIOHandlerFactoryInterface" FILE "dds.json" ) |
| 139 | public: |
| 140 | Capabilities capabilities(QIODevice *device, const QByteArray &format) const override; |
| 141 | QImageIOHandler *create(QIODevice *device, const QByteArray &format = QByteArray()) const override; |
| 142 | }; |
| 143 | |
| 144 | #endif // QDDSHANDLER_H |
| 145 | |