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
11QT_BEGIN_NAMESPACE
12
13class QIODevice;
14
15class QTgaFile
16{
17 Q_DECLARE_TR_FUNCTIONS(QTgaFile)
18
19public:
20 enum Compression {
21 NoCompression = 0,
22 RleCompression = 1
23 };
24
25 enum HeaderOffset {
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 HeaderSize = 18
39 };
40
41 enum FooterOffset {
42 ExtensionOffset = 0,
43 DeveloperOffset = 4,
44 SignatureOffset = 8,
45 FooterSize = 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
61private:
62 static inline quint16 littleEndianInt(const unsigned char *d);
63
64 QString mErrorMessage;
65 unsigned char mHeader[HeaderSize];
66 QIODevice *mDevice;
67};
68
69inline bool QTgaFile::isValid() const
70{
71 return mErrorMessage.isEmpty();
72}
73
74inline 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*/
83inline quint16 QTgaFile::littleEndianInt(const unsigned char *d)
84{
85 return d[0] + d[1] * 256;
86}
87
88inline int QTgaFile::xOffset() const
89{
90 return littleEndianInt(d: &mHeader[XOffset]);
91}
92
93inline int QTgaFile::yOffset() const
94{
95 return littleEndianInt(d: &mHeader[YOffset]);
96}
97
98inline int QTgaFile::width() const
99{
100 return littleEndianInt(d: &mHeader[Width]);
101}
102
103inline int QTgaFile::height() const
104{
105 return littleEndianInt(d: &mHeader[Height]);
106}
107
108inline QSize QTgaFile::size() const
109{
110 return QSize(width(), height());
111}
112
113inline 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
120QT_END_NAMESPACE
121
122#endif // QTGAFILE_H
123

source code of qtimageformats/src/plugins/imageformats/tga/qtgafile.h