/
TGALoader.cpp
436 lines (374 loc) · 11.3 KB
/
TGALoader.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
/*
Copyright (C) 2001-2006, William Joseph.
All Rights Reserved.
This file is part of GtkRadiant.
GtkRadiant is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
GtkRadiant is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GtkRadiant; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "TGALoader.h"
#include "ifilesystem.h"
#include "iarchive.h"
#include "idatastream.h"
#include "itextstream.h"
typedef unsigned char byte;
#include <stdlib.h>
#include "stream/ScopedArchiveBuffer.h"
#include "stream/PointerInputStream.h"
#include "RGBAImage.h"
#include "stream/utils.h"
namespace image
{
// represents x,y origin of tga image being decoded
class Flip00 {}; // no flip
class Flip01 {}; // vertical flip only
class Flip10 {}; // horizontal flip only
class Flip11 {}; // both
template<typename PixelDecoder>
void image_decode(stream::PointerInputStream& istream, PixelDecoder& decode, RGBAImage& image, const Flip00&)
{
RGBAPixel* end = image.pixels + (image.getHeight() * image.getWidth());
for(RGBAPixel* row = end; row != image.pixels; row -= image.getWidth())
{
for(RGBAPixel* pixel = row - image.getWidth(); pixel != row; ++pixel)
{
decode(istream, *pixel);
}
}
}
template<typename PixelDecoder>
void image_decode(stream::PointerInputStream& istream, PixelDecoder& decode, RGBAImage& image, const Flip01&)
{
RGBAPixel* end = image.pixels + (image.getHeight() * image.getWidth());
for(RGBAPixel* row = image.pixels; row != end; row += image.getWidth())
{
for(RGBAPixel* pixel = row; pixel != row + image.getWidth(); ++pixel)
{
decode(istream, *pixel);
}
}
}
template<typename PixelDecoder>
void image_decode(stream::PointerInputStream& istream, PixelDecoder& decode, RGBAImage& image, const Flip10&)
{
RGBAPixel* end = image.pixels + (image.getHeight() * image.getWidth());
for(RGBAPixel* row = end; row != image.pixels; row -= image.getWidth())
{
for(RGBAPixel* pixel = row; pixel != row - image.getWidth();)
{
decode(istream, *--pixel);
}
}
}
template<typename PixelDecoder>
void image_decode(stream::PointerInputStream& istream, PixelDecoder& decode, RGBAImage& image, const Flip11&)
{
RGBAPixel* end = image.pixels + (image.getHeight() * image.getWidth());
for(RGBAPixel* row = image.pixels; row != end; row += image.getWidth())
{
for(RGBAPixel* pixel = row + image.getWidth(); pixel != row;)
{
decode(istream, *--pixel);
}
}
}
inline void istream_read_gray(stream::PointerInputStream& istream, RGBAPixel& pixel)
{
istream.read(&pixel.blue, 1);
pixel.red = pixel.green = pixel.blue;
pixel.alpha = 0xff;
}
inline void istream_read_rgb(stream::PointerInputStream& istream, RGBAPixel& pixel)
{
istream.read(&pixel.blue, 1);
istream.read(&pixel.green, 1);
istream.read(&pixel.red, 1);
pixel.alpha = 0xff;
}
inline void istream_read_rgba(stream::PointerInputStream& istream, RGBAPixel& pixel)
{
istream.read(&pixel.blue, 1);
istream.read(&pixel.green, 1);
istream.read(&pixel.red, 1);
istream.read(&pixel.alpha, 1);
}
class TargaDecodeGrayPixel
{
public:
void operator()(stream::PointerInputStream& istream, RGBAPixel& pixel)
{
istream_read_gray(istream, pixel);
}
};
template<typename Flip>
void targa_decode_grayscale(stream::PointerInputStream& istream, RGBAImage& image, const Flip& flip)
{
TargaDecodeGrayPixel decode;
image_decode(istream, decode, image, flip);
}
class TargaDecodeRGBPixel
{
public:
void operator()(stream::PointerInputStream& istream, RGBAPixel& pixel)
{
istream_read_rgb(istream, pixel);
}
};
template<typename Flip>
void targa_decode_rgb(stream::PointerInputStream& istream, RGBAImage& image, const Flip& flip)
{
TargaDecodeRGBPixel decode;
image_decode(istream, decode, image, flip);
}
class TargaDecodeRGBAPixel
{
public:
void operator()(stream::PointerInputStream& istream, RGBAPixel& pixel)
{
istream_read_rgba(istream, pixel);
}
};
template<typename Flip>
void targa_decode_rgba(stream::PointerInputStream& istream, RGBAImage& image, const Flip& flip)
{
TargaDecodeRGBAPixel decode;
image_decode(istream, decode, image, flip);
}
typedef byte TargaPacket;
typedef byte TargaPacketSize;
inline void targa_packet_read_istream(TargaPacket& packet, stream::PointerInputStream& istream)
{
istream.read(&packet, 1);
}
inline bool targa_packet_is_rle(const TargaPacket& packet)
{
return (packet & 0x80) != 0;
}
inline TargaPacketSize targa_packet_size(const TargaPacket& packet)
{
return 1 + (packet & 0x7f);
}
class TargaDecodeRGBPixelRLE
{
TargaPacketSize m_packetSize;
RGBAPixel m_pixel;
TargaPacket m_packet;
public:
TargaDecodeRGBPixelRLE() : m_packetSize(0)
{
}
void operator()(stream::PointerInputStream& istream, RGBAPixel& pixel)
{
if(m_packetSize == 0)
{
targa_packet_read_istream(m_packet, istream);
m_packetSize = targa_packet_size(m_packet);
if(targa_packet_is_rle(m_packet))
{
istream_read_rgb(istream, m_pixel);
}
}
if(targa_packet_is_rle(m_packet))
{
pixel = m_pixel;
}
else
{
istream_read_rgb(istream, pixel);
}
--m_packetSize;
}
};
template<typename Flip>
void targa_decode_rle_rgb(stream::PointerInputStream& istream, RGBAImage& image, const Flip& flip)
{
TargaDecodeRGBPixelRLE decode;
image_decode(istream, decode, image, flip);
}
class TargaDecodeRGBAPixelRLE
{
TargaPacketSize m_packetSize;
RGBAPixel m_pixel;
TargaPacket m_packet;
public:
TargaDecodeRGBAPixelRLE() : m_packetSize(0)
{
}
void operator()(stream::PointerInputStream& istream, RGBAPixel& pixel)
{
if(m_packetSize == 0)
{
targa_packet_read_istream(m_packet, istream);
m_packetSize = targa_packet_size(m_packet);
if(targa_packet_is_rle(m_packet))
{
istream_read_rgba(istream, m_pixel);
}
}
if(targa_packet_is_rle(m_packet))
{
pixel = m_pixel;
}
else
{
istream_read_rgba(istream, pixel);
}
--m_packetSize;
}
};
template<typename Flip>
void targa_decode_rle_rgba(stream::PointerInputStream& istream, RGBAImage& image, const Flip& flip)
{
TargaDecodeRGBAPixelRLE decode;
image_decode(istream, decode, image, flip);
}
struct TargaHeader
{
unsigned char id_length, colormap_type, image_type;
unsigned short colormap_index, colormap_length;
unsigned char colormap_size;
unsigned short x_origin, y_origin, width, height;
unsigned char pixel_size, attributes;
};
inline void targa_header_read_istream(TargaHeader& targa_header, stream::PointerInputStream& istream)
{
static_assert(sizeof(unsigned short) == sizeof(uint16_t), "TGA Loader Code relies on unsigned short being 16 bits wide!");
targa_header.id_length = stream::readByte(istream);
targa_header.colormap_type = stream::readByte(istream);
targa_header.image_type = stream::readByte(istream);
targa_header.colormap_index = stream::readLittleEndian<uint16_t>(istream);
targa_header.colormap_length = stream::readLittleEndian<uint16_t>(istream);
targa_header.colormap_size = stream::readByte(istream);
targa_header.x_origin = stream::readLittleEndian<uint16_t>(istream);
targa_header.y_origin = stream::readLittleEndian<uint16_t>(istream);
targa_header.width = stream::readLittleEndian<uint16_t>(istream);
targa_header.height = stream::readLittleEndian<uint16_t>(istream);
targa_header.pixel_size = stream::readByte(istream);
targa_header.attributes = stream::readByte(istream);
if (targa_header.id_length != 0)
istream.seek(targa_header.id_length); // skip TARGA image comment
}
template<typename Type>
class ScopeDelete
{
Type* m_value;
ScopeDelete(const ScopeDelete&);
ScopeDelete& operator=(const ScopeDelete&);
public:
ScopeDelete(Type* value) : m_value(value)
{
}
~ScopeDelete()
{
delete m_value;
}
Type* get_pointer() const
{
return m_value;
}
};
template<typename Flip>
RGBAImagePtr Targa_decodeImageData(const TargaHeader& targa_header, stream::PointerInputStream& istream, const Flip& flip)
{
auto image = std::make_shared<RGBAImage>(targa_header.width, targa_header.height);
if (targa_header.image_type == 2 || targa_header.image_type == 3)
{
switch (targa_header.pixel_size)
{
case 8:
targa_decode_grayscale(istream, *image, flip);
break;
case 24:
targa_decode_rgb(istream, *image, flip);
break;
case 32:
targa_decode_rgba(istream, *image, flip);
break;
default:
rError() << "LoadTGA: illegal pixel_size '" << targa_header.pixel_size << "'\n";
return RGBAImagePtr();
}
}
else if (targa_header.image_type == 10)
{
switch (targa_header.pixel_size)
{
case 24:
targa_decode_rle_rgb(istream, *image, flip);
break;
case 32:
targa_decode_rle_rgba(istream, *image, flip);
break;
default:
rError() << "LoadTGA: illegal pixel_size '" << targa_header.pixel_size << "'\n";
return RGBAImagePtr();
}
}
return image;
}
const unsigned int TGA_FLIP_HORIZONTAL = 0x10;
const unsigned int TGA_FLIP_VERTICAL = 0x20;
RGBAImagePtr LoadTGABuff(const byte* buffer)
{
stream::PointerInputStream istream(buffer);
TargaHeader targa_header;
targa_header_read_istream(targa_header, istream);
if (targa_header.image_type != 2 && targa_header.image_type != 10 && targa_header.image_type != 3)
{
rError() << "LoadTGA: TGA type " << targa_header.image_type << " not supported\n";
rError() << "LoadTGA: Only type 2 (RGB), 3 (gray), and 10 (RGB) TGA images supported\n";
return RGBAImagePtr();
}
if (targa_header.colormap_type != 0)
{
rError() << "LoadTGA: colormaps not supported\n";
return RGBAImagePtr();
}
if ((targa_header.pixel_size != 32 && targa_header.pixel_size != 24)
&& targa_header.image_type != 3)
{
rError() << "LoadTGA: Only 32 or 24 bit images supported\n";
return RGBAImagePtr();
}
if ((targa_header.attributes & (TGA_FLIP_HORIZONTAL|TGA_FLIP_VERTICAL)) == 0)
{
return Targa_decodeImageData(targa_header, istream, Flip00());
}
if((targa_header.attributes & TGA_FLIP_HORIZONTAL) == 0 &&
(targa_header.attributes & TGA_FLIP_VERTICAL) != 0)
{
return Targa_decodeImageData(targa_header, istream, Flip01());
}
if ((targa_header.attributes & TGA_FLIP_HORIZONTAL) != 0 &&
(targa_header.attributes & TGA_FLIP_VERTICAL) == 0)
{
return Targa_decodeImageData(targa_header, istream, Flip10());
}
if ((targa_header.attributes & TGA_FLIP_HORIZONTAL) != 0 &&
(targa_header.attributes & TGA_FLIP_VERTICAL) != 0)
{
return Targa_decodeImageData(targa_header, istream, Flip11());
}
// unreachable
return RGBAImagePtr();
}
ImagePtr TGALoader::load(ArchiveFile& file) const
{
archive::ScopedArchiveBuffer buffer(file);
return LoadTGABuff(buffer.buffer);
}
ImageTypeLoader::Extensions TGALoader::getExtensions() const
{
Extensions extensions;
extensions.push_back("tga");
return extensions;
}
}