Debian Codegraph @

src/plugins/imageformats/tga/qtgafile.cpp

raw symbols SCIP
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 #include "qtgafile.h"cxx . . $ `<file>/src/plugins/imageformats/tga/qtgafile.h`/
41
42 #include <QtCore/QIODevice>
43 #include <QtCore/QDebug>
44 #include <QtCore/QDateTime>
45
46 struct TgaReadercxx . . $ TgaReader#
47 {
48 Q_DISABLE_COPYcxx . . $ `<external>/15045578679651060896/qglobal.h:444:9`!(TgaReader)
49
50 TgaReadercxx . . $ TgaReader#TgaReader(ced63f7c635d850d).() = default;
51
52 virtual ~cxx . . $ TgaReader#`~TgaReader`(ced63f7c635d850d).TgaReadercxx . . $ TgaReader#() {}
53 virtual QRgbcxx . . $ QRgb# operatorcxx . . $ TgaReader#`operator()`(f4b916dc53426609).()(QIODevicecxx . . $ QIODevice# *s) const = 0;
54 };
55
56 struct Tga16Readercxx . . $ Tga16Reader# : public TgaReadercxx . . $ TgaReader#
57 {
58 ~cxx . . $ Tga16Reader#`~Tga16Reader`(ced63f7c635d850d).Tga16Readercxx . . $ Tga16Reader#() {}
59 QRgbcxx . . $ QRgb# operatorcxx . . $ Tga16Reader#`operator()`(f4b916dc53426609).()(QIODevicecxx . . $ QIODevice# *s) const override
60 {
61 char ch1, ch2;
62 if (s->getCharcxx . . $ QIODevice#getChar(c6b7e35ff6de5671).(&ch1) && s->getCharcxx . . $ QIODevice#getChar(c6b7e35ff6de5671).(&ch2)) {
63 quint16cxx . . $ quint16# d = (int(ch1) & 0xFF) | ((int(ch2) & 0xFF) << 8);
64 QRgbcxx . . $ QRgb# result = (d & 0x8000) ? 0xFF000000 : 0x00000000;
65 result |= ((d & 0x7C00) << 6) | ((d & 0x03E0) << 3) | (d & 0x001F);
66 return result;
67 } else {
68 return 0;
69 }
70 }
71 };
72
73 struct Tga24Readercxx . . $ Tga24Reader# : public TgaReadercxx . . $ TgaReader#
74 {
75 QRgbcxx . . $ QRgb# operatorcxx . . $ Tga24Reader#`operator()`(f4b916dc53426609).()(QIODevicecxx . . $ QIODevice# *s) const override
76 {
77 char r, g, b;
78 if (s->getCharcxx . . $ QIODevice#getChar(c6b7e35ff6de5671).(&b) && s->getCharcxx . . $ QIODevice#getChar(c6b7e35ff6de5671).(&g) && s->getCharcxx . . $ QIODevice#getChar(c6b7e35ff6de5671).(&r))
79 return qRgbcxx . . $ qRgb(530956271a9518a5).(ucharcxx . . $ uchar#(r), ucharcxx . . $ uchar#(g), ucharcxx . . $ uchar#(b));
80 else
81 return 0;
82 }
83 };
84
85 struct Tga32Readercxx . . $ Tga32Reader# : public TgaReadercxx . . $ TgaReader#
86 {
87 QRgbcxx . . $ QRgb# operatorcxx . . $ Tga32Reader#`operator()`(f4b916dc53426609).()(QIODevicecxx . . $ QIODevice# *s) const override
88 {
89 char r, g, b, a;
90 if (s->getCharcxx . . $ QIODevice#getChar(c6b7e35ff6de5671).(&b) && s->getCharcxx . . $ QIODevice#getChar(c6b7e35ff6de5671).(&g) && s->getCharcxx . . $ QIODevice#getChar(c6b7e35ff6de5671).(&r) && s->getCharcxx . . $ QIODevice#getChar(c6b7e35ff6de5671).(&a))
91 return qRgbacxx . . $ qRgba(7a6d2d8711beacc4).(ucharcxx . . $ uchar#(r), ucharcxx . . $ uchar#(g), ucharcxx . . $ uchar#(b), ucharcxx . . $ uchar#(a));
92 else
93 return 0;
94 }
95 };
96
97 /*!
98 \class QTgaFile
99 \since 4.8
100 \internal
101
102 File data container for a TrueVision Graphics format file.
103
104 Format is as described here:
105 http://local.wasp.uwa.edu.au/~pbourke/dataformats/tga/
106 http://netghost.narod.ru/gff2/graphics/summary/tga.htm
107
108 Usage is:
109 \code
110 QTgaFile tga(myFile);
111 QImage tgaImage;
112 if (tga.isValid())
113 tgaImage = tga.readImage();
114 \endcode
115
116 The class is designed to handle sequential and non-sequential
117 sources, so during construction the mHeader is read. Then during
118 the readImage() call the rest of the data is read.
119
120 After passing myFile to the constructor, if the QIODevice *myFile
121 is read, or has seek() called, the results are undefined - so don't
122 do that.
123 */
124
125 /*!
126 Construct a new QTgaFile object getting data from \a device.
127
128 The object does not take ownership of the \a device, but until the
129 object is destroyed do not do any non-const operations, eg seek or
130 read on the device.
131 */
132 QTgaFilecxx . . $ QTgaFile#::QTgaFilecxx . . $ QTgaFile#QTgaFile(bebe8f2dee7f9e86).(QIODevicecxx . . $ QIODevice# *device)
133 : mDevicecxx . . $ QTgaFile#mDevice.(device)
134 {
135 ::memset(mHeadercxx . . $ QTgaFile#mHeader., 0, HeaderSizecxx . . $ QTgaFile#HeaderSize.);
136 if (!mDevicecxx . . $ QTgaFile#mDevice.->isReadablecxx . . $ QIODevice#isReadable(50ce9a9e25b4a850).())
137 {
138 mErrorMessagecxx . . $ QTgaFile#mErrorMessage. =cxx . . $ QString#`operator=`(4f35bf974e7f8568). trcxx . . $ QTgaFile#tr(75d9f1c324b4579d).("Could not read image data");
139 return;
140 }
141 if (mDevicecxx . . $ QTgaFile#mDevice.->isSequentialcxx . . $ QIODevice#isSequential(50ce9a9e25b4a850).())
142 {
143 mErrorMessagecxx . . $ QTgaFile#mErrorMessage. =cxx . . $ QString#`operator=`(4f35bf974e7f8568). trcxx . . $ QTgaFile#tr(75d9f1c324b4579d).("Sequential device (eg socket) for image read not supported");
144 return;
145 }
146 if (!mDevicecxx . . $ QTgaFile#mDevice.->seekcxx . . $ QIODevice#seek(49597d8fcab147c1).(0))
147 {
148 mErrorMessagecxx . . $ QTgaFile#mErrorMessage. =cxx . . $ QString#`operator=`(4f35bf974e7f8568). trcxx . . $ QTgaFile#tr(75d9f1c324b4579d).("Seek file/device for image read failed");
149 return;
150 }
151 if (device->readcxx . . $ QIODevice#read(2e638bc8ed65be6c).(reinterpret_cast<char*>(mHeadercxx . . $ QTgaFile#mHeader.), HeaderSizecxx . . $ QTgaFile#HeaderSize.) != HeaderSizecxx . . $ QTgaFile#HeaderSize.) {
152 mErrorMessagecxx . . $ QTgaFile#mErrorMessage. =cxx . . $ QString#`operator=`(4f35bf974e7f8568). trcxx . . $ QTgaFile#tr(75d9f1c324b4579d).("Image header read failed");
153 return;
154 }
155 if (mHeadercxx . . $ QTgaFile#mHeader.[ImageTypecxx . . $ QTgaFile#ImageType.] != 2)
156 {
157 // TODO: should support other image types
158 mErrorMessagecxx . . $ QTgaFile#mErrorMessage. =cxx . . $ QString#`operator=`(4f35bf974e7f8568). trcxx . . $ QTgaFile#tr(75d9f1c324b4579d).("Image type not supported");
159 return;
160 }
161 int bitsPerPixel = mHeadercxx . . $ QTgaFile#mHeader.[PixelDepthcxx . . $ QTgaFile#PixelDepth.];
162 bool validDepth = (bitsPerPixel == 16 || bitsPerPixel == 24 || bitsPerPixel == 32);
163 if (!validDepth)
164 {
165 mErrorMessagecxx . . $ QTgaFile#mErrorMessage. =cxx . . $ QString#`operator=`(4f35bf974e7f8568). trcxx . . $ QTgaFile#tr(75d9f1c324b4579d).("Image depth not valid");
166 return;
167 }
168 if (quint64cxx . . $ quint64#(widthcxx . . $ QTgaFile#width(455f465bc33b4cdf).()) * quint64cxx . . $ quint64#(heightcxx . . $ QTgaFile#height(455f465bc33b4cdf).()) > (8192 * 8192))
169 {
170 mErrorMessagecxx . . $ QTgaFile#mErrorMessage. =cxx . . $ QString#`operator=`(4f35bf974e7f8568). trcxx . . $ QTgaFile#tr(75d9f1c324b4579d).("Image size exceeds limit");
171 return;
172 }
173 int curPos = mDevicecxx . . $ QTgaFile#mDevice.->poscxx . . $ QIODevice#pos(98833e9ab7de59a4).();
174 int fileBytes = mDevicecxx . . $ QTgaFile#mDevice.->sizecxx . . $ QIODevice#size(98833e9ab7de59a4).();
175 if (!mDevicecxx . . $ QTgaFile#mDevice.->seekcxx . . $ QIODevice#seek(49597d8fcab147c1).(fileBytes - FooterSizecxx . . $ QTgaFile#FooterSize.))
176 {
177 mErrorMessagecxx . . $ QTgaFile#mErrorMessage. =cxx . . $ QString#`operator=`(4f35bf974e7f8568). trcxx . . $ QTgaFile#tr(75d9f1c324b4579d).("Could not seek to image read footer");
178 return;
179 }
180 char footer[FooterSizecxx . . $ QTgaFile#FooterSize.];
181 if (mDevicecxx . . $ QTgaFile#mDevice.->readcxx . . $ QIODevice#read(2e638bc8ed65be6c).(reinterpret_cast<char*>(footer), FooterSizecxx . . $ QTgaFile#FooterSize.) != FooterSizecxx . . $ QTgaFile#FooterSize.) {
182 mErrorMessagecxx . . $ QTgaFile#mErrorMessage. =cxx . . $ QString#`operator=`(4f35bf974e7f8568). trcxx . . $ QTgaFile#tr(75d9f1c324b4579d).("Could not read footer");
183 }
184 if (qstrncmpcxx . . $ qstrncmp(54fb1e1f0004e0cf).(&footer[SignatureOffsetcxx . . $ QTgaFile#SignatureOffset.], "TRUEVISION-XFILE", 16) != 0)
185 {
186 mErrorMessagecxx . . $ QTgaFile#mErrorMessage. =cxx . . $ QString#`operator=`(4f35bf974e7f8568). trcxx . . $ QTgaFile#tr(75d9f1c324b4579d).("Image type (non-TrueVision 2.0) not supported");
187 }
188 if (!mDevicecxx . . $ QTgaFile#mDevice.->seekcxx . . $ QIODevice#seek(49597d8fcab147c1).(curPos))
189 {
190 mErrorMessagecxx . . $ QTgaFile#mErrorMessage. =cxx . . $ QString#`operator=`(4f35bf974e7f8568). trcxx . . $ QTgaFile#tr(75d9f1c324b4579d).("Could not reset to read data");
191 }
192 }
193
194 /*!
195 \internal
196 Destroy the device, recovering any resources.
197 */
198 QTgaFilecxx . . $ QTgaFile#::~cxx . . $ QTgaFile#`~QTgaFile`(ced63f7c635d850d).QTgaFilecxx . . $ QTgaFile#()
199 {
200 }
201
202 /*!
203 \internal
204 Reads an image file from the QTgaFile's device, and returns it.
205
206 This method seeks to the absolute position of the image data in the file,
207 so no assumptions are made about where the devices read pointer is when this
208 method is called. For this reason only random access devices are supported.
209
210 If the constructor completed successfully, such that isValid() returns true,
211 then this method is likely to succeed, unless the file is somehow corrupted.
212
213 In the case that the read fails, the QImage returned will be null, such that
214 QImage::isNull() will be true.
215 */
216 QImagecxx . . $ QImage# QTgaFilecxx . . $ QTgaFile#::readImagecxx . . $ QTgaFile#readImage(89a140e6ec82607c).()
217 {
218 if (!isValidcxx . . $ QTgaFile#isValid(50ce9a9e25b4a850).())
219 return QImagecxx . . $ QImage#QImage(ced63f7c635d850d).();
220
221 int offset = mHeadercxx . . $ QTgaFile#mHeader.[IdLengthcxx . . $ QTgaFile#IdLength.]; // Mostly always zero
222
223 // Even in TrueColor files a color palette may be present so we have to check it here
224 // even we only support image type 2 (= uncompressed true-color image)
225 if (mHeadercxx . . $ QTgaFile#mHeader.[ColorMapTypecxx . . $ QTgaFile#ColorMapType.] == 1) {
226 int cmapDepth = mHeadercxx . . $ QTgaFile#mHeader.[CMapDepthcxx . . $ QTgaFile#CMapDepth.];
227 if (cmapDepth == 15) // 15 bit is stored as 16 bit + ignoring the highest bit (no alpha)
228 cmapDepth = 16;
229 if (cmapDepth != 16 && cmapDepth != 24 && cmapDepth != 32) {
230 mErrorMessagecxx . . $ QTgaFile#mErrorMessage. =cxx . . $ QString#`operator=`(4f35bf974e7f8568). trcxx . . $ QTgaFile#tr(75d9f1c324b4579d).("Invalid color map depth (%1)").argcxx . . $ QString#arg(64ab2ed17cc11715).(cmapDepth);
231 return {cxx . . $ QImage#QImage(ced63f7c635d850d).};
232 }
233 offset += littleEndianIntcxx . . $ QTgaFile#littleEndianInt(469dcb4e54b221a8).(&mHeadercxx . . $ QTgaFile#mHeader.[CMapLengthcxx . . $ QTgaFile#CMapLength.]) * cmapDepth / 8;
234 }
235
236 mDevicecxx . . $ QTgaFile#mDevice.->seekcxx . . $ QIODevice#seek(49597d8fcab147c1).(HeaderSizecxx . . $ QTgaFile#HeaderSize. + offset);
237
238 char dummy;
239 for (int i = 0; i < offset; ++i)
240 mDevicecxx . . $ QTgaFile#mDevice.->getCharcxx . . $ QIODevice#getChar(c6b7e35ff6de5671).(&dummy);
241
242 int bitsPerPixel = mHeadercxx . . $ QTgaFile#mHeader.[PixelDepthcxx . . $ QTgaFile#PixelDepth.];
243 int imageWidth = widthcxx . . $ QTgaFile#width(455f465bc33b4cdf).();
244 int imageHeight = heightcxx . . $ QTgaFile#height(455f465bc33b4cdf).();
245
246 unsigned char desc = mHeadercxx . . $ QTgaFile#mHeader.[ImageDescriptorcxx . . $ QTgaFile#ImageDescriptor.];
247 //unsigned char xCorner = desc & 0x10; // 0 = left, 1 = right
248 unsigned char yCorner = desc & 0x20; // 0 = lower, 1 = upper
249
250 QImagecxx . . $ QImage# imcxx . . $ QImage#QImage(bf9e47a5364bf007).(imageWidth, imageHeight, QImagecxx . . $ QImage#::Format_ARGB32cxx . . $ QImage#Format_ARGB32.);
251 if (im.isNullcxx . . $ QImage#isNull(50ce9a9e25b4a850).())
252 return QImagecxx . . $ QImage#QImage(ced63f7c635d850d).();
253 TgaReadercxx . . $ TgaReader# *reader = 0;
254 if (bitsPerPixel == 16)
255 reader = new Tga16Readercxx . . $ Tga16Reader#();
256 else if (bitsPerPixel == 24)
257 reader = new Tga24Readercxx . . $ Tga24Reader#();
258 else if (bitsPerPixel == 32)
259 reader = new Tga32Readercxx . . $ Tga32Reader#();
260 TgaReadercxx . . $ TgaReader# &read = *reader;
261
262 // For now only deal with yCorner, since no one uses xCorner == 1
263 // Also this is upside down, since Qt has the origin flipped
264 if (yCorner)
265 {
266 for (int y = 0; y < imageHeight; ++y)
267 for (int x = 0; x < imageWidth; ++x)
268 im.setPixelcxx . . $ QImage#setPixel(f59d17f8b9f2d3e1).(x, y, read(cxx . . $ TgaReader#`operator()`(f4b916dc53426609).mDevicecxx . . $ QTgaFile#mDevice.));
269 }
270 else
271 {
272 for (int y = imageHeight - 1; y >= 0; --y)
273 for (int x = 0; x < imageWidth; ++x)
274 im.setPixelcxx . . $ QImage#setPixel(f59d17f8b9f2d3e1).(x, y, read(cxx . . $ TgaReader#`operator()`(f4b916dc53426609).mDevicecxx . . $ QTgaFile#mDevice.));
275 }
276
277 delete reader;
278
279 // TODO: add processing of TGA extension information - ie TGA 2.0 files
280 return imcxx . . $ QImage#QImage(bff971006d8420fb).;
281 }