-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathTarga.h
499 lines (461 loc) · 12.8 KB
/
Targa.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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
#ifndef TARGA_H
#define TARGA_H
/* define generic and easily accessible image type, used
for targa conversion */
typedef struct ImageData {
short width;
short height;
short bytesperpixel;
unsigned data_size;
unsigned char* data;
} ImageData;
#ifndef TARGA_IMPL
#define TARGA_EXPORT extern
TARGA_EXPORT int
Targa_writefile(const char *name, ImageData* d, unsigned char *palette);
TARGA_EXPORT int
Targa_readfile(const char *name, ImageData *idata, int skip_palette);
#else
struct TargaHeader {
char idlength;
char colourmaptype;
char datatypecode;
short colourmaporigin;
short colourmaplength;
char colourmapdepth;
short x_origin;
short y_origin;
short width;
short height;
char bitsperpixel;
char imagedescriptor;
};
#define THO(f) THO_ ## f
enum TargaHeader_offsets {
THO(idlength) = 0,
THO(colourmaptype) = 1,
THO(datatypecode) = 2,
THO(colourmaporigin) = 3,
THO(colourmaplength) = 5,
THO(colourmapdepth) = 7,
THO(x_origin) = 8,
THO(y_origin) = 10,
THO(width) = 12,
THO(height) = 14,
THO(bitsperpixel) = 16,
THO(imagedescriptor) = 17,
};
static inline uint8_t read_header_field1(unsigned char *hdr, unsigned offset) {
return hdr[offset];
}
static inline uint16_t read_header_field2(unsigned char *hdr, unsigned offset) {
uint16_t tmp;
memcpy(&tmp, hdr+offset, 2);
return end_le16toh(tmp);
}
static void TargaHeader_from_buf(struct TargaHeader *hdr, unsigned char* hdr_buf) {
hdr->idlength = read_header_field1(hdr_buf, THO(idlength));
hdr->colourmaptype = read_header_field1(hdr_buf, THO(colourmaptype));
hdr->datatypecode = read_header_field1(hdr_buf, THO(datatypecode));
hdr->colourmaporigin = read_header_field2(hdr_buf, THO(colourmaporigin));
hdr->colourmaplength = read_header_field2(hdr_buf, THO(colourmaplength));
hdr->colourmapdepth = read_header_field1(hdr_buf, THO(colourmapdepth));
hdr->x_origin = read_header_field2(hdr_buf, THO(x_origin));
hdr->y_origin = read_header_field2(hdr_buf, THO(y_origin));
hdr->width = read_header_field2(hdr_buf, THO(width));
hdr->height = read_header_field2(hdr_buf, THO(height));
hdr->bitsperpixel = read_header_field1(hdr_buf, THO(bitsperpixel));
hdr->imagedescriptor = read_header_field1(hdr_buf, THO(imagedescriptor));
}
static inline void write_header_field1(unsigned char* buf, unsigned off, uint8_t v) {
buf[off] = v;
}
static inline void write_header_field2(unsigned char* buf, unsigned off, uint16_t v) {
uint16_t tmp = end_htole16(v);
memcpy(buf+off, &tmp, 2);
}
static void TargaHeader_to_buf(struct TargaHeader *hdr, unsigned char* hdr_buf) {
write_header_field1(hdr_buf, THO(idlength), hdr->idlength);
write_header_field1(hdr_buf, THO(colourmaptype), hdr->colourmaptype);
write_header_field1(hdr_buf, THO(datatypecode), hdr->datatypecode);
write_header_field2(hdr_buf, THO(colourmaporigin), hdr->colourmaporigin);
write_header_field2(hdr_buf, THO(colourmaplength), hdr->colourmaplength);
write_header_field1(hdr_buf, THO(colourmapdepth), hdr->colourmapdepth);
write_header_field2(hdr_buf, THO(x_origin), hdr->x_origin);
write_header_field2(hdr_buf, THO(y_origin), hdr->y_origin);
write_header_field2(hdr_buf, THO(width), hdr->width);
write_header_field2(hdr_buf, THO(height), hdr->height);
write_header_field1(hdr_buf, THO(bitsperpixel), hdr->bitsperpixel);
write_header_field1(hdr_buf, THO(imagedescriptor), hdr->imagedescriptor);
}
enum TargaImageType {
TIT_COLOR_MAPPED = 1,
TIT_TRUE_COLOR = 2,
TIT_BLACK_WHITE = 3,
TIT_RLE_COLOR_MAPPED = 9,
TIT_RLE_TRUE_COLOR = 10,
TIT_RLE_BLACK_WHITE = 11,
};
struct TargaFooter {
unsigned extensionareaoffset;
unsigned developerdirectoryoffset;
char signature[16];
char dot;
char null;
};
#define STATIC_ASSERT(COND) static char static_assert_ ## __LINE__ [COND ? 1 : -1]
//STATIC_ASSERT(sizeof(struct TargaHeader) == 18);
#ifndef TARGA_EXPORT
#define TARGA_EXPORT static
#endif
#define TARGA_FOOTER_SIGNATURE "TRUEVISION-XFILE"
/* helper funcs */
#include <stdlib.h>
#include <stdio.h>
#include "endianness.h"
#define rle_read_col(OUT, IDX) \
for(OUT=0, i=0; i<bpp;++i) {OUT |= (q[bpp*(IDX)+i] << (i*8));}
static unsigned rle_encode(
unsigned char *data, unsigned data_size,
unsigned bpp, unsigned char** result)
{
/* worst case length: entire file consisting of sequences of 2
identical, and one different pixel, resulting in
1 byte flag + 1 pixel + 1 byte flag + 1 pixel. in the case of
8 bit, that's 1 byte overhead every 3 pixels. */
unsigned char *out = malloc(data_size + 1 + (data_size/bpp/3));
if(!out) return 0;
unsigned char *p = out, *q = data;
unsigned i, count = 0, togo = data_size/bpp, repcol = 0;
unsigned mode = 0; /* 0: stateless, 1: series: 2: repetition */
unsigned jump_flag;
while(1) {
jump_flag = 0;
unsigned col[2] = {0};
if(togo) {
rle_read_col(col[0], count);
if(togo>1 && mode < 2) rle_read_col(col[1], count+1);
} else {
if(count) goto write_series;
else break;
}
switch(mode) {
case 0:
if(togo>1) {
if(col[0] == col[1]) {
start_rep:
mode = 2;
repcol = col[0];
} else {
start_series:
mode = 1;
}
count = 1;
} else {
goto start_series;
}
break;
case 1:
if(togo>1) {
if(col[0] == col[1]) {
jump_flag = 1;
goto write_series;
} else {
advance:
if(++count == 128) {
write_series:
*(p++) = ((mode - 1) << 7) | (count - 1);
if(mode == 1) for(i=0;i<count*bpp;++i)
*(p++) = *(q++);
else {
for(i=0;i<bpp*8;i+=8)
*(p++) = (repcol & (0xff << i)) >> i;
q += count * bpp;
}
if(!togo) goto done;
if(jump_flag == 1) goto start_rep;
if(jump_flag == 2) goto start_series;
mode = 0;
count = 0;
}
}
} else goto advance;
break;
case 2:
if(col[0] == repcol) goto advance;
else {
jump_flag = 2;
goto write_series;
}
}
togo--;
}
done:
*result = out;
return p-out;
}
#undef rle_read_col
/* caller needs to provide result buffer of w*h*bpp size. */
static void rle_decode(
unsigned char *data, unsigned data_size,
unsigned bpp, unsigned char* result, unsigned result_size) {
unsigned char
*p = result, *p_e = p + result_size,
*q = data, *q_e = q + data_size;
while(q+1+bpp <= q_e) {
unsigned count = (*q & 127) + 1;
unsigned rep = *q & 128;
unsigned color = 0, i, j;
++q;
if(rep) {
for(i = 0; i < bpp; ++i)
color = (color << 8) | *(q++);
for(i = 0; i < count && p+bpp <= p_e; ++i)
for(j=0; j<bpp; j++)
*(p++) = (color >> ((bpp-j-1)*8)) & 0xff;
} else {
for(i = 0; i < count && p+bpp <= p_e && q+bpp <= q_e; ++i)
for(j=0; j<bpp; j++) *(p++) = *(q++);
}
}
}
static void rgb565_to_888(unsigned lo, unsigned hi, unsigned *r, unsigned *g, unsigned *b)
{
*r = (hi & ~7) | (hi >> 5);
*g = ((hi & 7) << 5) | ((lo & 224) >> 3) | ((hi & 7) >> 1);
*b = ((lo & 31) << 3) | ((lo & 28) >> 2);
}
static int ImageData_convert_16_to_24(ImageData *d) {
size_t outsz = d->width*d->height*3UL;
unsigned char *out = malloc(outsz),
*p = out, *pe = out + outsz,
*q = d->data;
if(!out) return 0;
while(p < pe) {
unsigned r,g,b,lo,hi;
lo = *(q++);
hi = *(q++);
rgb565_to_888(lo, hi, &r, &g, &b);
*(p++) = b;
*(p++) = g;
*(p++) = r;
}
free(d->data);
d->data = out;
d->data_size = outsz;
d->bytesperpixel = 3;
return 1;
}
static int lookup_palette(unsigned color, unsigned *palette, int ncols)
{
int i;
for(i=0; i<ncols; ++i)
if(palette[i] == color) return i;
return -1;
}
static int ImageData_create_palette_pic(const ImageData* d, unsigned *palette, unsigned char **data)
{
int ret = 0;
unsigned char *p = d->data, *q = p + d->data_size;
*data = malloc(d->width * d->height);
unsigned char *o = *data, *e = o + (d->width * d->height);
unsigned a = 0xff;
while(p < q && o < e) {
unsigned col, r, g, b;
switch(d->bytesperpixel) {
case 2: {
unsigned lo = *(p++);
unsigned hi = *(p++);
rgb565_to_888(lo, hi, &r, &g, &b);
break; }
case 3:
b = *(p++);
g = *(p++);
r = *(p++);
break;
case 4:
b = *(p++);
g = *(p++);
r = *(p++);
a = *(p++);
break;
default: b=g=r=0; break;
}
if(a != 0xff) goto nope;
col = a << 24 | r << 16 | g << 8 | b;
int n = lookup_palette(col, palette, ret);
if(n < 0) {
if(ret == 256) {
nope:
free(*data);
*data = 0;
return -1;
}
n = ret;
palette[ret++] = col;
}
*(o++) = n;
}
return ret;
}
static void convert_bottom_left_tga(ImageData *d) {
size_t y, w = d->width*d->bytesperpixel;
unsigned char *swp = malloc(w);
if(!swp) return;
for(y = 0; y < d->height/2; ++y) {
size_t to = w*y, bo = (d->height-1-y)*w;
memcpy(swp, d->data + to, w);
memcpy(d->data + to, d->data + bo, w);
memcpy(d->data + bo, swp, w);
}
free(swp);
}
/* exports */
TARGA_EXPORT int
Targa_readfile(const char *name, ImageData *idata, int skip_palette) {
struct TargaHeader hdr; unsigned char hdr_buf[18];
unsigned char ftr_buf[18+8];
FILE *f = fopen(name, "rb");
if(!f) return 0;
fread(&hdr_buf, 1, sizeof(hdr_buf), f);
fseek(f, 0, SEEK_END);
off_t fs = ftello(f);
if(fs > sizeof ftr_buf) {
fseek(f, 0-sizeof ftr_buf, SEEK_END);
fread(ftr_buf, 1, sizeof ftr_buf, f);
if(!memcmp(ftr_buf+8, TARGA_FOOTER_SIGNATURE, 16))
fs -= sizeof ftr_buf;
}
TargaHeader_from_buf(&hdr, hdr_buf);
fseek(f, sizeof(hdr_buf) + hdr.idlength, SEEK_SET);
fs -= ftello(f);
unsigned char *data = malloc(fs), *palette = 0;
unsigned palsz = 0;
fread(data, 1, fs, f);
if(hdr.colourmaptype) {
palette = data;
palsz = hdr.colourmaplength * hdr.colourmapdepth/8;
if(fs <= palsz) return 0;
data += palsz;
fs -= palsz;
}
unsigned char *workdata = 0;
unsigned tmp;
switch(hdr.datatypecode) {
case TIT_RLE_COLOR_MAPPED:
case TIT_RLE_TRUE_COLOR:
tmp = hdr.width*hdr.height*hdr.bitsperpixel/8;
workdata = malloc(tmp);
rle_decode(data, fs, hdr.bitsperpixel/8, workdata, tmp);
break;
case TIT_COLOR_MAPPED:
case TIT_TRUE_COLOR:
workdata = data;
break;
default:
return 0;
}
idata->width = hdr.width;
idata->height = hdr.height;
if(skip_palette)
idata->bytesperpixel = hdr.bitsperpixel/8;
else
idata->bytesperpixel = hdr.colourmapdepth? hdr.colourmapdepth/8 : hdr.bitsperpixel/8;
tmp = idata->width*idata->height*idata->bytesperpixel;
idata->data_size = tmp;
idata->data = malloc(tmp);
if(palette && !skip_palette) {
unsigned i, j, bpp = hdr.colourmapdepth/8;
unsigned char *p = idata->data, *q = workdata;
for(i=0; i < idata->width*idata->height; ++i) {
unsigned idx = *(q++);
if(idx >= hdr.colourmaplength) return 0;
for(j=0; j < bpp; ++j)
*(p++) = palette[idx*bpp+j];
}
} else {
memcpy(idata->data, workdata, tmp);
}
if(workdata != data) free(workdata);
if(palette) free(palette);
else free(data);
if(hdr.y_origin == 0) convert_bottom_left_tga(idata);
fclose(f);
return 1;
}
/* palette needs to be either 24bit rgb data of 256*3 bytes, or NULL.
it's only used if imagedata.bytesperpixel is 1. in this case, if
NULL, a random palette is used. */
TARGA_EXPORT int
Targa_writefile(const char *name, ImageData* d, unsigned char *palette)
{
unsigned pal[256];
unsigned char *paldata = 0, *data = d->data;
unsigned bpp = d->bytesperpixel;
unsigned data_size = d->data_size;
int palcount = 256;
int i;
FILE *f = fopen(name, "wb");
if(!f) {
fprintf(stderr, "error opening %s\n", name);
return 0;
}
if(bpp == 1) {
if(!palette) for(i=0; i<256; ++i) pal[i] = rand();
else for(i=0; i<256; ++i) {
unsigned r = *(palette++);
unsigned g = *(palette++);
unsigned b = *(palette++);
pal[i] = r << 16 | g << 8 | b ;
}
} else if( /* bpp != 2 && */
(palcount = ImageData_create_palette_pic(d, pal, &paldata)) > 0) {
/* can be saved as 8 bit palettized image */
bpp = 1;
data = paldata;
data_size = d->width*d->height;
} else if(bpp == 2 && ImageData_convert_16_to_24(d)) {
bpp = 3;
data = d->data;
data_size = d->data_size;
}
unsigned char *rle_data = 0;
unsigned rle_data_size = rle_encode(data, data_size, bpp, &rle_data);
int use_rle = 0;
if(rle_data && rle_data_size < data_size) {
data_size = rle_data_size;
data = rle_data;
use_rle = 1;
}
struct TargaHeader hdr = {
.idlength = 0,
.colourmaptype = bpp == 1 ? 1 : 0,
.datatypecode = bpp == 1 ?
(use_rle ? TIT_RLE_COLOR_MAPPED : TIT_COLOR_MAPPED) :
(use_rle ? TIT_RLE_TRUE_COLOR : TIT_TRUE_COLOR),
.colourmaporigin = 0,
.colourmaplength = bpp == 1 ? palcount : 0,
.colourmapdepth = bpp == 1 ? 24 : 0,
.x_origin = 0,
.y_origin = d->height, /* image starts at the top */
.width = d->width,
.height = d->height,
.bitsperpixel = bpp*8,
.imagedescriptor = 0x20, /* image starts at the top */
};
unsigned char hdr_buf[18];
TargaHeader_to_buf(&hdr, hdr_buf);
fwrite(&hdr_buf, 1, sizeof hdr_buf, f);
unsigned tmp;
if(bpp == 1) for(i=0; i<palcount; ++i) {
tmp = end_htole32(pal[i]);
fwrite(&tmp, 1, 3, f);
}
fwrite(data, 1, data_size, f);
fclose(f);
free(paldata);
free(rle_data);
return 1;
}
#endif /* TARGA_IMPL */
#endif