| 1 | // Copyright (C) 2016 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | // Qt-Security score:critical reason:data-parser |
| 4 | |
| 5 | #ifndef QTGAFILE_H |
| 6 | #define QTGAFILE_H |
| 7 | |
| 8 | #include <QtGui/QColor> |
| 9 | #include <QtGui/QImage> |
| 10 | #include <QtCore/QCoreApplication> |
| 11 | |
| 12 | QT_BEGIN_NAMESPACE |
| 13 | |
| 14 | class QIODevice; |
| 15 | |
| 16 | class QTgaFile |
| 17 | { |
| 18 | Q_DECLARE_TR_FUNCTIONS(QTgaFile) |
| 19 | |
| 20 | public: |
| 21 | enum Compression { |
| 22 | NoCompression = 0, |
| 23 | RleCompression = 1 |
| 24 | }; |
| 25 | |
| 26 | enum { |
| 27 | IdLength = 0, /* 00h Size of Image ID field */ |
| 28 | ColorMapType = 1, /* 01h Color map type */ |
| 29 | ImageType = 2, /* 02h Image type code */ |
| 30 | CMapStart = 3, /* 03h Color map origin */ |
| 31 | CMapLength = 5, /* 05h Color map length */ |
| 32 | CMapDepth = 7, /* 07h Depth of color map entries */ |
| 33 | XOffset = 8, /* 08h X origin of image */ |
| 34 | YOffset = 10, /* 0Ah Y origin of image */ |
| 35 | Width = 12, /* 0Ch Width of image */ |
| 36 | Height = 14, /* 0Eh Height of image */ |
| 37 | PixelDepth = 16, /* 10h Image pixel size */ |
| 38 | ImageDescriptor = 17, /* 11h Image descriptor byte */ |
| 39 | = 18 |
| 40 | }; |
| 41 | |
| 42 | enum { |
| 43 | ExtensionOffset = 0, |
| 44 | DeveloperOffset = 4, |
| 45 | SignatureOffset = 8, |
| 46 | = 26 |
| 47 | }; |
| 48 | |
| 49 | QTgaFile(QIODevice *); |
| 50 | ~QTgaFile(); |
| 51 | |
| 52 | inline bool isValid() const; |
| 53 | inline QString errorMessage() const; |
| 54 | QImage readImage(); |
| 55 | inline int xOffset() const; |
| 56 | inline int yOffset() const; |
| 57 | inline int width() const; |
| 58 | inline int height() const; |
| 59 | inline QSize size() const; |
| 60 | inline Compression compression() const; |
| 61 | |
| 62 | private: |
| 63 | static inline quint16 littleEndianInt(const unsigned char *d); |
| 64 | |
| 65 | QString mErrorMessage; |
| 66 | unsigned char [HeaderSize]; |
| 67 | QIODevice *mDevice; |
| 68 | }; |
| 69 | |
| 70 | inline bool QTgaFile::isValid() const |
| 71 | { |
| 72 | return mErrorMessage.isEmpty(); |
| 73 | } |
| 74 | |
| 75 | inline QString QTgaFile::errorMessage() const |
| 76 | { |
| 77 | return mErrorMessage; |
| 78 | } |
| 79 | |
| 80 | /*! |
| 81 | \internal |
| 82 | Returns the integer encoded in the two little endian bytes at \a d. |
| 83 | */ |
| 84 | inline quint16 QTgaFile::littleEndianInt(const unsigned char *d) |
| 85 | { |
| 86 | return d[0] + d[1] * 256; |
| 87 | } |
| 88 | |
| 89 | inline int QTgaFile::xOffset() const |
| 90 | { |
| 91 | return littleEndianInt(d: &mHeader[XOffset]); |
| 92 | } |
| 93 | |
| 94 | inline int QTgaFile::yOffset() const |
| 95 | { |
| 96 | return littleEndianInt(d: &mHeader[YOffset]); |
| 97 | } |
| 98 | |
| 99 | inline int QTgaFile::width() const |
| 100 | { |
| 101 | return littleEndianInt(d: &mHeader[Width]); |
| 102 | } |
| 103 | |
| 104 | inline int QTgaFile::height() const |
| 105 | { |
| 106 | return littleEndianInt(d: &mHeader[Height]); |
| 107 | } |
| 108 | |
| 109 | inline QSize QTgaFile::size() const |
| 110 | { |
| 111 | return QSize(width(), height()); |
| 112 | } |
| 113 | |
| 114 | inline QTgaFile::Compression QTgaFile::compression() const |
| 115 | { |
| 116 | // TODO: for now, only handle type 2 files, with no color table |
| 117 | // and no compression |
| 118 | return NoCompression; |
| 119 | } |
| 120 | |
| 121 | QT_END_NAMESPACE |
| 122 | |
| 123 | #endif // QTGAFILE_H |
| 124 | |