-
Notifications
You must be signed in to change notification settings - Fork 9
/
decoder.cpp
357 lines (317 loc) · 9.03 KB
/
decoder.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
// Aseprite TGA Library
// Copyright (C) 2020-2022 Igara Studio S.A.
//
// This file is released under the terms of the MIT license.
// Read LICENSE.txt for more information.
#include "tga.h"
#include <cassert>
namespace tga {
static inline uint8_t scale_5bits_to_8bits(uint8_t v) {
assert(v >= 0 && v < 32);
return (v << 3) | (v >> 2);
}
Decoder::Decoder(FileInterface* file)
: m_file(file)
{
}
bool Decoder::readHeader(Header& header)
{
header.idLength = read8();
header.colormapType = read8();
header.imageType = read8();
header.colormapOrigin = read16();
header.colormapLength = read16();
header.colormapDepth = read8();
header.xOrigin = read16();
header.yOrigin = read16();
header.width = read16();
header.height = read16();
header.bitsPerPixel = read8();
header.imageDescriptor = read8();
// Invalid image size
if (header.width == 0 ||
header.height == 0)
return false;
// Skip ID string (idLength bytes)
if (header.idLength > 0) {
uint8_t i = header.idLength;
while (i--) {
uint8_t chr = m_file->read8();
header.imageId.push_back(chr);
}
}
#if 0
// In the best case the "alphaBits" should be valid, but there are
// invalid TGA files out there which don't indicate the
// "alphaBits" correctly, so they could be 0 and use the alpha
// channel anyway on each pixel.
int alphaBits = (header.imageDescriptor & 15);
m_hasAlpha =
(header.bitsPerPixel == 32 && alphaBits == 8) ||
(header.bitsPerPixel == 16 && alphaBits == 1);
#else
// So to detect if a 32bpp or 16bpp TGA image has alpha, we'll use
// the "alpha histogram" in postProcessImage() to check if there are
// different alpha values. If there is only one alpha value (all 0
// or all 255), we create an opaque image anyway. The only exception
// to this rule is when all pixels are black and transparent
// (RGBA=0), that is the only case when an image is fully
// transparent.
//
// Note: This same heuristic is used in apps like macOS Preview:
// https://twitter.com/davidcapello/status/1242803110868893697
m_hasAlpha =
(header.bitsPerPixel == 32) ||
(header.bitsPerPixel == 16);
#endif
// Read colormap
if (header.colormapType == 1)
readColormap(header);
return (header.validColormapType() &&
header.valid());
}
void Decoder::readColormap(Header& header)
{
header.colormap = Colormap(header.colormapLength);
for (int i=0; i<header.colormapLength; ++i) {
switch (header.colormapDepth) {
case 15:
case 16: {
const uint16_t c = read16();
header.colormap[i] =
rgba(scale_5bits_to_8bits((c >> 10) & 0x1F),
scale_5bits_to_8bits((c >> 5) & 0x1F),
scale_5bits_to_8bits(c & 0x1F));
break;
}
case 24:
case 32: {
const uint8_t b = read8();
const uint8_t g = read8();
const uint8_t r = read8();
uint8_t a;
if (header.colormapDepth == 32)
a = read8();
else
a = 255;
header.colormap[i] = rgba(r, g, b, a);
break;
}
}
}
}
bool Decoder::readImage(const Header& header,
Image& image,
Delegate* delegate)
{
// Bit 4 means right-to-left, else left-to-right
// Bit 5 means top-to-bottom, else bottom-to-top
m_iterator = details::ImageIterator(header, image);
for (int y=0; y<header.height; ++y) {
switch (header.imageType) {
case UncompressedIndexed:
assert(header.bitsPerPixel == 8);
if (readUncompressedData<uint8_t>(header.width, &Decoder::read8Color))
return true;
break;
case UncompressedRgb:
switch (header.bitsPerPixel) {
case 15:
case 16:
if (readUncompressedData<uint32_t>(header.width, &Decoder::read16AsRgb))
return true;
break;
case 24:
if (readUncompressedData<uint32_t>(header.width, &Decoder::read24AsRgb))
return true;
break;
case 32:
if (readUncompressedData<uint32_t>(header.width, &Decoder::read32AsRgb))
return true;
break;
default:
assert(false);
break;
}
break;
case UncompressedGray:
assert(header.bitsPerPixel == 8);
if (readUncompressedData<uint8_t>(header.width, &Decoder::read8Color))
return true;
break;
case RleIndexed:
assert(header.bitsPerPixel == 8);
if (readRleData<uint8_t>(header.width, &Decoder::read8Color))
return true;
break;
case RleRgb:
switch (header.bitsPerPixel) {
case 15:
case 16:
if (readRleData<uint32_t>(header.width, &Decoder::read16AsRgb))
return true;
break;
case 24:
if (readRleData<uint32_t>(header.width, &Decoder::read24AsRgb))
return true;
break;
case 32:
if (readRleData<uint32_t>(header.width, &Decoder::read32AsRgb))
return true;
break;
default:
assert(false);
break;
}
break;
case RleGray:
assert(header.bitsPerPixel == 8);
if (readRleData<uint8_t>(header.width, &Decoder::read8Color))
return true;
break;
}
if (delegate &&
!delegate->notifyProgress(float(y) / float(header.height))) {
break;
}
}
return true;
}
void Decoder::postProcessImage(const Header& header,
Image& image)
{
// The post-processing is only for RGB images with possible invalid
// alpha information.
if (!header.isRgb() || !m_hasAlpha)
return;
bool transparentImage = true;
bool blackImage = true;
for (int y=0; y<header.height; ++y) {
auto p = (uint32_t*)(image.pixels + y*image.rowstride);
for (int x=0; x<header.width; ++x, ++p) {
color_t c = *p;
if (transparentImage &&
geta(c) != 0) {
transparentImage = false;
}
if (blackImage &&
(getr(c) != 0 ||
getg(c) != 0 ||
getb(c) != 0)) {
blackImage = false;
}
}
}
// If the image is fully transparent (all pixels with alpha=0) and
// there are pixels with RGB != 0 (!blackImage), we have to make the
// image completely opaque (alpha=255).
if (transparentImage && !blackImage) {
for (int y=0; y<header.height; ++y) {
auto p = (uint32_t*)(image.pixels + y*image.rowstride);
for (int x=0; x<header.width; ++x, ++p) {
color_t c = *p;
*p = rgba(getr(c),
getg(c),
getb(c), 255);
}
}
}
}
template<typename T>
bool Decoder::readUncompressedData(const int w, color_t (Decoder::*readPixel)())
{
for (int x=0; x<w && m_file->ok(); ++x) {
if (m_iterator.putPixel<T>(static_cast<T>((this->*readPixel)())))
return true;
}
return false;
}
// In the best case (TGA 2.0 spec) this should read just one
// scanline, but in old TGA versions (1.0) it was possible to save
// several scanlines with the same RLE data.
//
// Returns true when are are done.
template<typename T>
bool Decoder::readRleData(const int w, color_t (Decoder::*readPixel)())
{
for (int x=0; x<w && m_file->ok(); ) {
int c = read8();
if (c & 0x80) {
c = (c & 0x7f) + 1;
x += c;
const T pixel = static_cast<T>((this->*readPixel)());
while (c-- > 0)
if (m_iterator.putPixel<T>(pixel))
return true;
}
else {
++c;
x += c;
while (c-- > 0) {
if (m_iterator.putPixel<T>(static_cast<T>((this->*readPixel)())))
return true;
}
}
}
return false;
}
uint8_t Decoder::read8()
{
return m_file->read8();
}
// Reads a WORD (16 bits) using in little-endian byte ordering.
uint16_t Decoder::read16()
{
uint8_t b1 = m_file->read8();
uint8_t b2 = m_file->read8();
if (m_file->ok()) {
return ((b2 << 8) | b1); // Little endian
}
else
return 0;
}
// Reads a DWORD (32 bits) using in little-endian byte ordering.
uint32_t Decoder::read32()
{
const uint8_t b1 = m_file->read8();
const uint8_t b2 = m_file->read8();
const uint8_t b3 = m_file->read8();
const uint8_t b4 = m_file->read8();
if (m_file->ok()) {
// Little endian
return ((b4 << 24) | (b3 << 16) | (b2 << 8) | b1);
}
else
return 0;
}
color_t Decoder::read32AsRgb()
{
const uint8_t b = read8();
const uint8_t g = read8();
const uint8_t r = read8();
uint8_t a = read8();
if (!m_hasAlpha)
a = 255;
return rgba(r, g, b, a);
}
color_t Decoder::read24AsRgb()
{
const uint8_t b = read8();
const uint8_t g = read8();
const uint8_t r = read8();
return rgba(r, g, b, 255);
}
color_t Decoder::read16AsRgb()
{
const uint16_t v = read16();
uint8_t a = 255;
if (m_hasAlpha) {
if ((v & 0x8000) == 0) // Transparent bit
a = 0;
}
return rgba(scale_5bits_to_8bits((v >> 10) & 0x1F),
scale_5bits_to_8bits((v >> 5) & 0x1F),
scale_5bits_to_8bits(v & 0x1F),
a);
}
} // namespace tga