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
|
/**
@file GImage_tga.cpp
@author Morgan McGuire, http://graphics.cs.williams.edu
@created 2002-05-27
@edited 2009-05-10
*/
#include "G3D/platform.h"
#include "G3D/GImage.h"
#include "G3D/BinaryInput.h"
#include "G3D/BinaryOutput.h"
#include "G3D/Log.h"
namespace G3D {
void GImage::encodeTGA(
BinaryOutput& out) const {
out.setEndian(G3D_LITTLE_ENDIAN);
// ID length
out.writeUInt8(0);
// Color map Type
out.writeUInt8(0);
// Type
out.writeUInt8(2);
// Color map
out.skip(5);
// x, y offsets
out.writeUInt16(0);
out.writeUInt16(0);
// Width & height
out.writeUInt16(m_width);
out.writeUInt16(m_height);
// Color depth
if (m_channels == 1) {
// Force RGB mode
out.writeUInt8(8 * 3);
} else {
out.writeUInt8(8 * m_channels);
}
// Image descriptor
if (m_channels < 4) {
// 0 alpha bits
out.writeUInt8(0);
} else {
// 8 alpha bits
out.writeUInt8(8);
}
// Image ID (zero length)
if (m_channels == 1) {
// Pixels are upside down in BGR format.
for (int y = m_height - 1; y >= 0; --y) {
for (int x = 0; x < m_width; ++x) {
uint8 p = (m_byte[(y * m_width + x)]);
out.writeUInt8(p);
out.writeUInt8(p);
out.writeUInt8(p);
}
}
} else if (m_channels == 3) {
// Pixels are upside down in BGR format.
for (int y = m_height - 1; y >= 0; --y) {
for (int x = 0; x < m_width; ++x) {
uint8* p = &(m_byte[3 * (y * m_width + x)]);
out.writeUInt8(p[2]);
out.writeUInt8(p[1]);
out.writeUInt8(p[0]);
}
}
} else {
// Pixels are upside down in BGRA format.
for (int y = m_height - 1; y >= 0; --y) {
for (int x = 0; x < m_width; ++x) {
uint8* p = &(m_byte[4 * (y * m_width + x)]);
out.writeUInt8(p[2]);
out.writeUInt8(p[1]);
out.writeUInt8(p[0]);
out.writeUInt8(p[3]);
}
}
}
// Write "TRUEVISION-XFILE " 18 bytes from the end
// (with null termination)
out.writeString("TRUEVISION-XFILE ");
}
inline static void readBGR(uint8* byte, BinaryInput& bi) {
int b = bi.readUInt8();
int g = bi.readUInt8();
int r = bi.readUInt8();
byte[0] = r;
byte[1] = g;
byte[2] = b;
}
inline static void readBGRA(uint8* byte, BinaryInput& bi) {
readBGR(byte, bi);
byte[3] = bi.readUInt8();
}
void GImage::decodeTGA(
BinaryInput& input) {
// This is a simple TGA loader that can handle uncompressed
// truecolor TGA files (TGA type 2).
// Verify this is a TGA file by looking for the TRUEVISION tag.
int pos = input.getPosition();
input.setPosition(input.size() - 18);
std::string tag = input.readString(16);
if (tag != "TRUEVISION-XFILE") {
throw Error("Not a TGA file", input.getFilename());
}
input.setPosition(pos);
int IDLength = input.readUInt8();
int colorMapType = input.readUInt8();
int imageType = input.readUInt8();
(void)colorMapType;
// 2 is the type supported by this routine.
if (imageType != 2 && imageType != 10) {
throw Error("TGA images must be type 2 (Uncompressed truecolor) or 10 (Run-length truecolor)", input.getFilename());
}
// Color map specification
input.skip(5);
// Image specification
// Skip x and y offsets
input.skip(4);
m_width = input.readInt16();
m_height = input.readInt16();
int colorDepth = input.readUInt8();
if ((colorDepth != 24) && (colorDepth != 32)) {
throw Error("TGA files must be 24 or 32 bit.", input.getFilename());
}
if (colorDepth == 32) {
m_channels = 4;
} else {
m_channels = 3;
}
// Image descriptor contains overlay data as well
// as data indicating where the origin is
int imageDescriptor = input.readUInt8();
(void)imageDescriptor;
// Image ID
input.skip(IDLength);
m_byte = (uint8*)m_memMan->alloc(m_width * m_height * m_channels);
debugAssert(m_byte);
// Pixel data
int x;
int y;
if (imageType == 2) {
// Uncompressed
if (m_channels == 3) {
for (y = m_height - 1; y >= 0; --y) {
for (x = 0; x < m_width; ++x) {
int i = (x + y * m_width) * 3;
readBGR(m_byte + i, input);
}
}
} else {
for (y = m_height - 1; y >= 0; --y) {
for (x = 0; x < m_width; ++x) {
int i = (x + y * m_width) * 4;
readBGRA(m_byte + i, input);
}
}
}
} else if (imageType == 10) {
// Run-length encoded
for (y = m_height - 1; y >= 0; --y) {
for (int x = 0; x < m_width; /* intentionally no x increment */) {
// The specification guarantees that no packet will wrap past the end of a row
const uint8 repetitionCount = input.readUInt8();
const uint8 numValues = (repetitionCount & (~128)) + 1;
int byteOffset = (x + y * m_width) * 3;
if (repetitionCount & 128) {
// When the high bit is 1, this is a run-length packet
if (m_channels == 3) {
Color3uint8 value;
readBGR((uint8*)(&value), input);
for (int i = 0; i < numValues; ++i, ++x) {
for (int b = 0; b < 3; ++b, ++byteOffset) {
m_byte[byteOffset] = value[b];
}
}
} else {
Color4uint8 value;
readBGRA((uint8*)(&value), input);
for (int i = 0; i < numValues; ++i, ++x) {
for (int b = 0; b < 3; ++b, ++byteOffset) {
m_byte[byteOffset] = value[b];
}
}
}
} else {
// When the high bit is 0, this is a raw packet
for (int i = 0; i < numValues; ++i, ++x, byteOffset += m_channels) {
readBGR(m_byte + byteOffset, input);
}
}
}
}
} else {
alwaysAssertM(false, "Unsupported type");
}
}
}
|