| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the TGA plugin in the Qt ImageFormats module. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** GNU Lesser General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
| 19 | ** General Public License version 3 as published by the Free Software |
| 20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
| 21 | ** packaging of this file. Please review the following information to |
| 22 | ** ensure the GNU Lesser General Public License version 3 requirements |
| 23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
| 24 | ** |
| 25 | ** GNU General Public License Usage |
| 26 | ** Alternatively, this file may be used under the terms of the GNU |
| 27 | ** General Public License version 2.0 or (at your option) the GNU General |
| 28 | ** Public license version 3 or any later version approved by the KDE Free |
| 29 | ** Qt Foundation. The licenses are as published by the Free Software |
| 30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
| 31 | ** included in the packaging of this file. Please review the following |
| 32 | ** information to ensure the GNU General Public License requirements will |
| 33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
| 34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
| 35 | ** |
| 36 | ** $QT_END_LICENSE$ |
| 37 | ** |
| 38 | ****************************************************************************/ |
| 39 | |
| 40 | #ifndef QTGAFILE_H |
| 41 | #define QTGAFILE_H |
| 42 | |
| 43 | #include <QtGui/QColor> |
| 44 | #include <QtGui/QImage> |
| 45 | #include <QtCore/QCoreApplication> |
| 46 | |
| 47 | QT_BEGIN_NAMESPACE |
| 48 | |
| 49 | class QIODevice; |
| 50 | |
| 51 | class QTgaFile |
| 52 | { |
| 53 | Q_DECLARE_TR_FUNCTIONS(QTgaFile) |
| 54 | |
| 55 | public: |
| 56 | enum Compression { |
| 57 | NoCompression = 0, |
| 58 | RleCompression = 1 |
| 59 | }; |
| 60 | |
| 61 | enum { |
| 62 | IdLength = 0, /* 00h Size of Image ID field */ |
| 63 | ColorMapType = 1, /* 01h Color map type */ |
| 64 | ImageType = 2, /* 02h Image type code */ |
| 65 | CMapStart = 3, /* 03h Color map origin */ |
| 66 | CMapLength = 5, /* 05h Color map length */ |
| 67 | CMapDepth = 7, /* 07h Depth of color map entries */ |
| 68 | XOffset = 8, /* 08h X origin of image */ |
| 69 | YOffset = 10, /* 0Ah Y origin of image */ |
| 70 | Width = 12, /* 0Ch Width of image */ |
| 71 | Height = 14, /* 0Eh Height of image */ |
| 72 | PixelDepth = 16, /* 10h Image pixel size */ |
| 73 | ImageDescriptor = 17, /* 11h Image descriptor byte */ |
| 74 | = 18 |
| 75 | }; |
| 76 | |
| 77 | enum { |
| 78 | ExtensionOffset = 0, |
| 79 | DeveloperOffset = 4, |
| 80 | SignatureOffset = 8, |
| 81 | = 26 |
| 82 | }; |
| 83 | |
| 84 | QTgaFile(QIODevice *); |
| 85 | ~QTgaFile(); |
| 86 | |
| 87 | inline bool isValid() const; |
| 88 | inline QString errorMessage() const; |
| 89 | QImage readImage(); |
| 90 | inline int xOffset() const; |
| 91 | inline int yOffset() const; |
| 92 | inline int width() const; |
| 93 | inline int height() const; |
| 94 | inline QSize size() const; |
| 95 | inline Compression compression() const; |
| 96 | |
| 97 | private: |
| 98 | static inline quint16 littleEndianInt(const unsigned char *d); |
| 99 | |
| 100 | QString mErrorMessage; |
| 101 | unsigned char [HeaderSize]; |
| 102 | QIODevice *mDevice; |
| 103 | }; |
| 104 | |
| 105 | inline bool QTgaFile::isValid() const |
| 106 | { |
| 107 | return mErrorMessage.isEmpty(); |
| 108 | } |
| 109 | |
| 110 | inline QString QTgaFile::errorMessage() const |
| 111 | { |
| 112 | return mErrorMessage; |
| 113 | } |
| 114 | |
| 115 | /*! |
| 116 | \internal |
| 117 | Returns the integer encoded in the two little endian bytes at \a d. |
| 118 | */ |
| 119 | inline quint16 QTgaFile::littleEndianInt(const unsigned char *d) |
| 120 | { |
| 121 | return d[0] + d[1] * 256; |
| 122 | } |
| 123 | |
| 124 | inline int QTgaFile::xOffset() const |
| 125 | { |
| 126 | return littleEndianInt(d: &mHeader[XOffset]); |
| 127 | } |
| 128 | |
| 129 | inline int QTgaFile::yOffset() const |
| 130 | { |
| 131 | return littleEndianInt(d: &mHeader[YOffset]); |
| 132 | } |
| 133 | |
| 134 | inline int QTgaFile::width() const |
| 135 | { |
| 136 | return littleEndianInt(d: &mHeader[Width]); |
| 137 | } |
| 138 | |
| 139 | inline int QTgaFile::height() const |
| 140 | { |
| 141 | return littleEndianInt(d: &mHeader[Height]); |
| 142 | } |
| 143 | |
| 144 | inline QSize QTgaFile::size() const |
| 145 | { |
| 146 | return QSize(width(), height()); |
| 147 | } |
| 148 | |
| 149 | inline QTgaFile::Compression QTgaFile::compression() const |
| 150 | { |
| 151 | // TODO: for now, only handle type 2 files, with no color table |
| 152 | // and no compression |
| 153 | return NoCompression; |
| 154 | } |
| 155 | |
| 156 | QT_END_NAMESPACE |
| 157 | |
| 158 | #endif // QTGAFILE_H |
| 159 | |