-
Notifications
You must be signed in to change notification settings - Fork 1
/
ClmbsImg_TGA.c
272 lines (219 loc) · 6 KB
/
ClmbsImg_TGA.c
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
#include "ColumbusImage.h"
#include "ClmbsImg_Util.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#ifdef __cplusplus
extern "C"
{
#endif
#define READPIXEL24(a) \
blue = *a++; \
green = *a++; \
red = *a++;
#define READPIXEL32(a) \
READPIXEL24(a) \
alpha = *a++;
#define WRITEPIXEL24(a) \
*a++ = red; \
*a++ = green; \
*a++ = blue;
#define WRITEPIXEL32(a) \
WRITEPIXEL24(a) \
*a++ = alpha;
typedef struct
{
uint8_t idlen; //Image ID Lenght
uint8_t color_map_type; //Color map type
uint8_t image_type; //Image Type
/*
0 - No image data included.
1 - Uncompressed, color-mapped images.
2 - Uncompressed, RGB images.
3 - Uncompressed, black and white images.
9 - Runlength encoded color-mapped images.
10 - Runlength encoded RGB images.
11 - Compressed, black and white images.
32 - Compressed color-mapped data, using Huffman, Delta, and runlength encoding.
33 - Compressed color-mapped data, using Huffman, Delta, and runlength encoding. 4-pass quadtree-type process.
*/
uint16_t color_map_origin; //Color
uint16_t color_map_length; //map
uint8_t color_map_entry_size; //specification
uint16_t x_origin; //
uint16_t y_origin; //Image
uint16_t width; //specification
uint16_t height; //
uint8_t bits; // Bit depth. 8, 15, 16, 24 or 32
uint8_t image_descriptor;
} TGA_HEADER;
static bool ReadHeader(TGA_HEADER* header, FILE* fp)
{
if (header == NULL || fp == NULL) return false;
if (!ReadUint8(&header->idlen, fp)) return false;
if (!ReadUint8(&header->color_map_type, fp)) return false;
if (!ReadUint8(&header->image_type, fp)) return false;
if (!ReadUint16(&header->color_map_origin, fp)) return false;
if (!ReadUint16(&header->color_map_length, fp)) return false;
if (!ReadUint8(&header->color_map_entry_size, fp)) return false;
if (!ReadUint16(&header->x_origin, fp)) return false;
if (!ReadUint16(&header->y_origin, fp)) return false;
if (!ReadUint16(&header->width, fp)) return false;
if (!ReadUint16(&header->height, fp)) return false;
if (!ReadUint8(&header->bits, fp)) return false;
if (!ReadUint8(&header->image_descriptor, fp)) return false;
return true;
}
static bool WriteHeader(TGA_HEADER header, FILE* fp)
{
if (fp == NULL) return false;
if (!WriteUint8(&header.idlen, fp)) return false;
if (!WriteUint8(&header.color_map_type, fp)) return false;
if (!WriteUint8(&header.image_type, fp)) return false;
if (!WriteUint16(&header.color_map_origin, fp)) return false;
if (!WriteUint16(&header.color_map_length, fp)) return false;
if (!WriteUint8(&header.color_map_entry_size, fp)) return false;
if (!WriteUint16(&header.x_origin, fp)) return false;
if (!WriteUint16(&header.y_origin, fp)) return false;
if (!WriteUint16(&header.width, fp)) return false;
if (!WriteUint16(&header.height, fp)) return false;
if (!WriteUint8(&header.bits, fp)) return false;
if (!WriteUint8(&header.image_descriptor, fp)) return false;
return true;
}
bool ClmbsImg_IsTGA(const char* file)
{
return true;
}
static void RGBCompressedTGA(uint8_t* aIn, uint8_t* aOut, size_t aSize)
{
int header;
int blue, green, red;
uint8_t* pixel = (uint8_t*)malloc(3);
size_t i, j, pixelcount;
for (i = 0; i < aSize; )
{
header = *aIn++;
pixelcount = (header & 0x7f) + 1;
if (header & 0x80)
{
READPIXEL24(aIn)
for (j = 0; j < pixelcount; j++)
{
WRITEPIXEL24(aOut)
}
i += pixelcount;
} else
{
for (j = 0; j < pixelcount; j++)
{
READPIXEL24(aIn)
WRITEPIXEL24(aOut)
}
i += pixelcount;
}
}
}
static void RGBACompressedTGA(uint8_t* aIn, uint8_t* aOut, size_t aSize)
{
int header;
int blue, green, red, alpha;
int pix;
size_t i, j, pixelcount;
for (i = 0; i < aSize; )
{
header = *aIn++;
pixelcount = (header & 0x7f) + 1;
if (header & 0x80)
{
READPIXEL32(aIn);
pix = red | (green << 8) | (blue << 16) | (alpha << 24);
for (j = 0; j < pixelcount; j++)
{
memcpy(aOut, &pix, 4);
aOut += 4;
}
i += pixelcount;
}
else
{
for (j = 0; j < pixelcount; j++)
{
READPIXEL32(aIn)
WRITEPIXEL32(aOut)
}
i += pixelcount;
}
}
}
ClmbsImg_Data ClmbsImg_LoadTGA(const char* file)
{
ClmbsImg_Data ret;
FILE* fp = fopen(file, "rb");
if (fp == NULL) return ret;
TGA_HEADER tga;
if (!ReadHeader(&tga, fp)) return ret;
size_t offset = ftell(fp);
fseek(fp, 0, SEEK_END);
size_t s = ftell(fp);
fseek(fp, offset, SEEK_SET);
size_t dSize = s - sizeof(TGA_HEADER);
size_t size = tga.width * tga.height * tga.bits / 8;
uint8_t* buffer = (uint8_t*)malloc(dSize);
fread(buffer, dSize, 1, fp);
uint8_t* data = NULL;
switch (tga.image_type)
{
case 2:
//Uncompressed RGB
data = buffer;
if (tga.bits == 24)
bgr2rgb(buffer, size);
else
bgra2rgba(buffer, size);
break;
case 10:
//Compressed RGB
data = (uint8_t*)malloc(size);
if (tga.bits == 24)
RGBCompressedTGA(buffer, data, tga.width * tga.height);
else
RGBACompressedTGA(buffer, data, tga.width * tga.height);
break;
}
if (tga.x_origin != 0)
flipX(buffer, tga.width, tga.height, tga.bits / 8);
if (tga.y_origin != 0)
flipY(data, tga.width, tga.height, tga.bits / 8);
fclose(fp);
ret.w = tga.width;
ret.h = tga.height;
ret.bpp = tga.bits / 8;
ret.data = data;
return ret;
}
bool ClmbsImg_SaveTGA(const char* file, ClmbsImg_Data data)
{
FILE* fp = fopen(file, "wb");
if (fp == NULL) return false;
TGA_HEADER tga = {0, 0, 2, 0, 0, 0, 0, 0, data.w, data.h, data.bpp * 8, 8};
size_t size = data.w * data.h * data.bpp;
uint8_t* buffer = (uint8_t*)malloc(size);
memcpy(buffer, data.data, size);
if (tga.bits == 24)
rgb2bgr(buffer, size);
if (tga.bits == 32)
rgba2bgra(buffer, size);
WriteHeader(tga, fp);
fwrite(buffer, data.w * data.h * data.bpp, 1, fp);
fclose(fp);
free(buffer);
return true;
}
#undef READPIXEL24
#undef WRITEPIXEL24
#undef WRITEPIXEL32
#ifdef __cplusplus
}
#endif