-
Notifications
You must be signed in to change notification settings - Fork 5
/
tga.h
345 lines (288 loc) · 7.87 KB
/
tga.h
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
#pragma once
#include <string.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
/*!
* @file tga.h
* @brief All C library functional.
*/
/**
* @brief Loads TGA from file.
*
* ## Example
*
* ```c
* int w, h, b;
* uint8_t* data = tga_load("image.tga", &w, &h, &b);
* ```
*
* @param Filename Image file to load.
* @param Width Pointer to int to store image width.
* @param Height Pointer to int to store image height.
* @param Bpp Pointer to int to store size of pixel in bytes.
*/
uint8_t* tga_load(const char* Filename, int* Width, int* Height, int* Bpp);
/**
* @brief Loads TGA from memory buffer.
* @param Data Buffer storing image file data.
* @param Size Size of buffer.
* @param Width Pointer to int to store image width.
* @param Height Pointer to int to store image height.
* @param Bpp Pointer to int to store sixe of pixel in bytes.
*/
uint8_t* tga_load_memory(uint8_t* Data, int Size, int* Width, int* Height, int* Bpp);
static void tga_load_compressed_paletted_8(uint8_t* InBuffer, uint8_t* ColorMap, uint8_t* OutBuffer, size_t Size, size_t PixelSize);
static void tga_load_compressed_paletted_16(uint16_t* InBuffer, uint8_t* ColorMap, uint8_t* OutBuffer, size_t Size, size_t PixelSize);
static void tga_load_compressed_true_color_24(uint8_t* InBuffer, uint8_t* OutBuffer, size_t Size);
static void tga_load_compressed_true_color_32(uint8_t* InBuffer, uint8_t* OutBuffer, size_t Size);
static void tga_load_compressed_monochrome(uint8_t* InBuffer, uint8_t* OutBuffer, size_t Size);
void tga_load_compressed_paletted_8(uint8_t* InBuffer, uint8_t* ColorMap, uint8_t* OutBuffer, size_t Size, size_t PixelSize)
{
uint8_t Index;
uint8_t Pixel[4];
uint8_t Tmp;
uint8_t* ColorMapPtr;
for (size_t i = 0; i < Size; i++)
{
Index = *InBuffer++;
ColorMapPtr = &ColorMap[Index * PixelSize];
memcpy(Pixel, ColorMapPtr, PixelSize);
Tmp = Pixel[0]; Pixel[0] = Pixel[2]; Pixel[2] = Tmp;
memcpy(OutBuffer, Pixel, PixelSize); OutBuffer += PixelSize;
}
}
void tga_load_compressed_paletted_16(uint16_t* InBuffer, uint8_t* ColorMap, uint8_t* OutBuffer, size_t Size, size_t PixelSize)
{
uint16_t Index;
uint8_t Pixel[4];
uint8_t Tmp;
uint8_t* ColorMapPtr;
for (size_t i = 0; i < Size; i++)
{
Index = *InBuffer++;
ColorMapPtr = &ColorMap[Index * PixelSize];
memcpy(Pixel, ColorMapPtr, PixelSize);
Tmp = Pixel[0]; Pixel[0] = Pixel[2]; Pixel[2] = Tmp;
memcpy(OutBuffer, Pixel, PixelSize); OutBuffer += PixelSize;
}
}
static void tga_load_compressed_true_color_24(uint8_t* InBuffer, uint8_t* OutBuffer, size_t Size)
{
uint8_t Header;
uint8_t Pixel[3];
size_t i, j, PixelCount;
for (i = 0; i < Size; )
{
Header = *InBuffer++;
PixelCount = (Header & 0x7F) + 1;
if (Header & 0x80)
{
Pixel[2] = *InBuffer++;
Pixel[1] = *InBuffer++;
Pixel[0] = *InBuffer++;
for (j = 0; j < PixelCount; j++)
{
memcpy(OutBuffer, Pixel, 3); OutBuffer += 3;
}
i += PixelCount;
}
else
{
for (j = 0; j < PixelCount; j++)
{
Pixel[2] = *InBuffer++;
Pixel[1] = *InBuffer++;
Pixel[0] = *InBuffer++;
memcpy(OutBuffer, Pixel, 3); OutBuffer += 3;
}
i += PixelCount;
}
}
}
static void tga_load_compressed_true_color_32(uint8_t* InBuffer, uint8_t* OutBuffer, size_t Size)
{
uint8_t Header;
uint8_t Pixel[4];
size_t i, j, PixelCount;
for (i = 0; i < Size; )
{
Header = *InBuffer++;
PixelCount = (Header & 0x7F) + 1;
if (Header & 0x80)
{
Pixel[2] = *InBuffer++;
Pixel[1] = *InBuffer++;
Pixel[0] = *InBuffer++;
Pixel[3] = *InBuffer++;
for (j = 0; j < PixelCount; j++)
{
memcpy(OutBuffer, Pixel, 4); OutBuffer += 4;
}
i += PixelCount;
}
else
{
for (j = 0; j < PixelCount; j++)
{
Pixel[2] = *InBuffer++;
Pixel[1] = *InBuffer++;
Pixel[0] = *InBuffer++;
Pixel[3] = *InBuffer++;
memcpy(OutBuffer, Pixel, 4); OutBuffer += 4;
}
i += PixelCount;
}
}
}
void tga_load_compressed_monochrome(uint8_t* InBuffer, uint8_t* OutBuffer, size_t Size)
{
uint8_t Header;
uint8_t Red;
size_t i, j, PixelCount;
for (i = 0; i < Size; )
{
Header = *InBuffer++;
PixelCount = (Header & 0x7F) + 1;
if (Header & 0x80)
{
Red = *InBuffer++;
for (j = 0; j < PixelCount; j++)
{
*OutBuffer++ = Red;
}
i += PixelCount;
}
else
{
for (j = 0; j < PixelCount; j++)
{
*OutBuffer++ = *InBuffer++;
}
i += PixelCount;
}
}
}
uint8_t* tga_load(const char* Filename, int* Width, int* Height, int* Bpp)
{
FILE* f = fopen(Filename, "rb");
if (f == NULL) return NULL;
size_t Size = 0;
fseek(f, 0, SEEK_END);
Size = ftell(f);
rewind(f);
uint8_t* Buffer = (uint8_t*)malloc(Size);
if (Buffer == NULL) return NULL;
if (fread(Buffer, 1, Size, f) != Size)
{
fclose(f);
return NULL;
}
fclose(f);
uint8_t* Result = tga_load_memory(Buffer, Size, Width, Height, Bpp);
free(Buffer);
return Result;
}
uint8_t* tga_load_memory(uint8_t* Data, int Size, int* Width, int* Height, int* Bpp)
{
struct
{
uint8_t IDLength;
uint8_t ColorMapType;
uint8_t ImageType;
uint16_t ColorMapOrigin;
uint16_t ColorMapLength;
uint8_t ColorMapEntrySize;
uint16_t XOrigin;
uint16_t YOrigin;
uint16_t Width;
uint16_t Height;
uint8_t Bits;
uint8_t ImageDescriptor;
} Header;
Header.IDLength = *Data++;
Header.ColorMapType = *Data++;
Header.ImageType = *Data++;
Header.ColorMapOrigin = *((uint16_t*)Data); Data += sizeof(uint16_t);
Header.ColorMapLength = *((uint16_t*)Data); Data += sizeof(uint16_t);
Header.ColorMapEntrySize = *Data++;
Header.XOrigin = *((uint16_t*)Data); Data += sizeof(uint16_t);
Header.YOrigin = *((uint16_t*)Data); Data += sizeof(uint16_t);
Header.Width = *((uint16_t*)Data); Data += sizeof(uint16_t);
Header.Height = *((uint16_t*)Data); Data += sizeof(uint16_t);
Header.Bits = *Data++;
Header.ImageDescriptor = *Data++;
Data += Header.ImageDescriptor;
size_t ColorMapElementSize = Header.ColorMapEntrySize / 8;
size_t ColorMapSize = Header.ColorMapLength * ColorMapElementSize;
uint8_t* ColorMap = (uint8_t*)malloc(ColorMapSize);
if (Header.ColorMapType == 1)
{
memcpy(ColorMap, Data, ColorMapSize);
Data += ColorMapSize;
}
size_t PixelSize = Header.ColorMapLength == 0 ? (Header.Bits / 8) : ColorMapElementSize;
size_t DataSize = Size - sizeof(Header) - (Header.ColorMapType == 1 ? ColorMapSize : 0);
size_t ImageSize = Header.Width * Header.Height * PixelSize;
uint8_t* Result = (uint8_t*)calloc(ImageSize, 1);
switch (Header.ImageType)
{
case 0: break; // No Image
case 1: // Uncompressed paletted
{
if (PixelSize == 3 || PixelSize == 4)
{
switch (Header.Bits)
{
case 8: tga_load_compressed_paletted_8 ((uint8_t*) Data, ColorMap, Result, Header.Width * Header.Height, PixelSize); break;
case 16: tga_load_compressed_paletted_16((uint16_t*)Data, ColorMap, Result, Header.Width * Header.Height, PixelSize); break;
}
}
break;
}
case 2: // Uncompressed TrueColor
{
if (Header.Bits = 24 || Header.Bits == 32)
{
memcpy(Result, Data, ImageSize);
uint8_t Tmp;
for (size_t i = 0; i < ImageSize; i += PixelSize)
{
Tmp = Result[i]; Result[i] = Result[i + 2]; Result[i + 2] = Tmp;
}
}
break;
}
case 3: // Uncompressed Monochrome
{
if (Header.Bits == 8)
{
memcpy(Result, Data, ImageSize);
}
break;
}
case 9: break; // Compressed paletted TODO
case 10: // Compressed TrueColor
{
switch (Header.Bits)
{
case 24: tga_load_compressed_true_color_24(Data, Result, Header.Width * Header.Height); break;
case 32: tga_load_compressed_true_color_32(Data, Result, Header.Width * Header.Height); break;
}
break;
}
case 11: // Compressed Monocrhome
{
if (Header.Bits == 8)
{
tga_load_compressed_monochrome(Data, Result, Header.Width * Header.Height);
}
break;
}
}
*Width = Header.Width;
*Height = Header.Height;
*Bpp = PixelSize;
free(ColorMap);
return Result;
}