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