-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtga.c
339 lines (305 loc) · 8.51 KB
/
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
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
/*
Copyright (C) 1995-2011 Jan Eric Kyprianidis <www.kyprianidis.com>
All rights reserved.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include "tga.h"
/*--Descriptor flags---------------*/
#define ds_BottomLeft 0x00
#define ds_BottomRight 0x01
#define ds_TopLeft 0x02
#define ds_TopRight 0x03
/*--Color map types----------------*/
#define cm_NoColorMap 0
#define cm_ColorMap 1
/*--Image data types---------------*/
#define im_NoImageData 0
#define im_ColMapImage 1
#define im_RgbImage 2
#define im_MonoImage 3
#define im_ColMapImageRLE 9
#define im_RgbImageRLE 10
#define im_MonoImageRLE 11
static unsigned short fgetword(FILE *f) {
unsigned char b[2];
fread(b, 2, 1, f);
return ((unsigned short)b[1] << 8) | (unsigned short)b[0];
}
static void fputword(unsigned short s, FILE *f) {
unsigned char b[2];
b[0] = (unsigned char)(s & 0xff);
b[1] = (unsigned char)((s >> 8) & 0xff);
fwrite(b, 2, 1, f);
}
int tga_load(const char *name, unsigned char **buffer, int *width, int *height) {
FILE *f;
int idLen;
int colorMapType;
int imageType;
int firstColor;
int colorMapLen;
int colorMapBits;
int xOrgin;
int yOrgin;
int iw;
int ih;
int bitsPerPix;
int descriptor;
int i, j, k;
unsigned char *p;
unsigned char *q;
unsigned char *pp;
int bb;
int cp[4];
int rle;
int n;
int c;
int ix, iy;
unsigned char colorMap[256][3];
unsigned char *data;
data = NULL;
if ((f = fopen(name, "rb")) == NULL)
return 0;
idLen = fgetc(f);
colorMapType = fgetc(f);
imageType = fgetc(f);
firstColor = fgetword(f);
colorMapLen = fgetword(f);
colorMapBits = fgetc(f);
xOrgin = fgetword(f);
yOrgin = fgetword(f);
iw = fgetword(f);
ih = fgetword(f);
bitsPerPix = fgetc(f);
descriptor = fgetc(f);
switch(imageType) {
case im_MonoImage:
case im_ColMapImage:
case im_RgbImage:
case im_MonoImageRLE:
case im_ColMapImageRLE:
case im_RgbImageRLE:
break;
default:
goto error;
}
switch (bitsPerPix) {
case 8:
case 24:
case 32:
bb = bitsPerPix / 8;
break;
default:
goto error;
}
if (idLen)
fseek(f, idLen, SEEK_CUR);
*width = iw;
*height = ih;
if (colorMapType == cm_ColorMap) {
memset(colorMap, 0, sizeof(colorMap));
if ((colorMapBits != 24) || (colorMapLen + firstColor > 256)) {
goto error;
}
for (i = 0, pp = colorMap[firstColor]; i < colorMapLen; i++) {
*pp++ = fgetc(f);
*pp++ = fgetc(f);
*pp++ = fgetc(f);
}
} else {
for (i = 0; i < 256; ++i)
colorMap[i][0] = colorMap[i][1] = colorMap[i][2] = i;
}
if ((data = malloc(iw * bb * ih)) == NULL) {
goto error;
}
if ((imageType == im_MonoImage) || (imageType == im_ColMapImage) || (imageType == im_RgbImage)) {
fread(data, ih, iw*bb, f);
} else {
p = data;
n = 0;
for (j = 0; j < ih; ++j) {
for (i = 0; i < iw; ++i, p += bb) {
if (n == 0) {
c = fgetc(f);
n = (c & 0x7F) + 1;
if (c&0x80) {
for (k = 0; k < bb; k++) cp[k] = fgetc(f);
rle = 1;
} else {
rle = 0;
}
}
if (rle)
for (k = 0; k < bb; k++) p[k] = cp[k];
else
for (k = 0; k < bb; k++) p[k] = fgetc(f);
n--;
}
}
}
if (ferror(f)) {
goto error;
}
fclose(f);
*buffer = (unsigned char*)malloc(iw * ih * 4);
if (!*buffer)
goto error;
p = *buffer;
switch ((descriptor >> 4) & 0x3) {
case ds_BottomLeft:
ix = bb;
iy = -2 * iw * bb;
q = data + iw * bb * (ih - 1);
break;
case ds_BottomRight:
ix = -bb;
iy = 0;
q = data + (iw - 1) * bb * (ih);
break;
case ds_TopLeft:
ix = bb;
iy = 0;
q = data;
break;
case ds_TopRight:
ix = -bb;
iy = 2 * iw * bb;
q = data + (iw - 1) * bb;
break;
}
switch (bitsPerPix) {
case 8: {
for (j = 0; j < ih; ++j) {
for (i = 0; i < iw; ++i) {
for (k = 0; k < 3; ++k) {
p[k] = colorMap[*q][k];
}
p[3] = 0xff;
p += 4;
q += ix;
}
q += iy;
}
break;
}
case 24: {
for (j = 0; j < ih; ++j) {
for (i = 0; i < iw; ++i) {
p[0] = q[0];
p[1] = q[1];
p[2] = q[2];
p[3] = 0xff;
p += 4;
q += ix;
}
q += iy;
}
break;
}
case 32: {
for (j = 0; j < ih; ++j) {
for (i = 0; i < iw; ++i) {
p[0] = q[0];
p[1] = q[1];
p[2] = q[2];
p[3] = q[3];
p += 4;
q += ix;
}
q += iy;
}
break;
}
}
free(data);
return 1;
error:
if (data) free(data);
if (f) fclose(f);
*buffer = NULL;
*width = 0;
*height = 0;
return 0;
}
int tga_save(const char *name, unsigned char *buffer, unsigned char *colorMap, int width, int height, int bits) {
FILE *f;
int colorMapType = 0;
int imageType;
int colorMapLen = 0;
int colorMapBits = 0;
int bitsPerPix;
int i;
int bb;
unsigned char *pp;
if ((f = fopen(name, "wb+")) == NULL)
return 0;
switch (bits) {
case 8:
imageType = im_ColMapImage;
if (colorMap != NULL) {
colorMapType = cm_ColorMap;
colorMapLen = 256;
colorMapBits = 24;
}
bitsPerPix = 8;
bb = 1;
break;
case 16:
imageType = im_RgbImage;
bitsPerPix = 16;
bb = 2;
break;
case 24:
imageType = im_RgbImage;
bitsPerPix = 24;
bb = 3;
break;
case 32:
imageType = im_RgbImage;
bitsPerPix = 32;
bb = 4;
break;
default:
fclose(f);
return 0;
}
fputc(0, f);
fputc(colorMapType, f);
fputc(imageType, f);
fputword(0, f);
fputword(colorMapLen, f);
fputc(colorMapBits, f);
fputword(0, f);
fputword(0, f);
fputword(width, f);
fputword(height, f);
fputc(bitsPerPix, f);
fputc(ds_TopLeft << 4, f);
if (colorMapType == cm_ColorMap) {
for (i = 0, pp = colorMap; i < 256; i++, pp += 3) {
fputc(pp[0], f);
fputc(pp[1], f);
fputc(pp[2], f);
}
}
fwrite(buffer, width*bb, height, f);
if (ferror(f)) {
fclose(f);
return 0;
}
fclose(f);
return 1;
}