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
500
501
502
503
504
505
506
507
508
|
/*
* TGA is a fairly dead standard, however in the video industry
* it is still used a little for test patterns and the like.
*
* Thus we ignore any alpha channels.
*/
#include <u.h>
#include <libc.h>
#include <bio.h>
#include <draw.h>
#include <ctype.h>
#include "imagefile.h"
enum {
HdrLen = 18,
};
typedef struct {
int idlen; /* length of string after header */
int cmaptype; /* 1 => datatype = 1 => colourmapped */
int datatype; /* see below */
int cmaporigin; /* index of first entry in colour map */
int cmaplen; /* length of colour map */
int cmapbpp; /* bits per pixel of colour map: 16, 24, or 32 */
int xorigin; /* source image origin */
int yorigin;
int width;
int height;
int bpp; /* bits per pixel of image: 16, 24, or 32 */
int descriptor;
uchar *cmap; /* colour map (optional) */
} Tga;
/*
* descriptor:
* d0-3 = number of attribute bits per pixel
* d4 = reserved, always zero
* d6-7 = origin: 0=lower left, 1=upper left, 2=lower right, 3=upper right
* d8-9 = interleave: 0=progressive, 1=2 way, 3=4 way, 4=reserved.
*/
char *datatype[] = {
[0] "No image data",
[1] "Color-mapped",
[2] "RGB",
[3] "B&W",
[9] "RLE color-mapped",
[10] "RLE RGB",
[11] "RLE B&W",
[32] "Compressed color",
[33] "Quadtree compressed color",
};
static int
Bgeti(Biobuf *bp)
{
int x, y;
if((x = Bgetc(bp)) < 0)
return -1;
if((y = Bgetc(bp)) < 0)
return -1;
return (y<<8)|x;
}
static int
fixcmap(uchar *cmap, int *cmapbpp, int cmaplen)
{
int i;
ushort x;
uchar tmp;
switch(*cmapbpp){
case 32:
/* swap B with R */
for(i = 0; i < cmaplen; i++){
tmp = cmap[4*i+0];
cmap[4*i+0] = cmap[4*i+2];
cmap[4*i+2] = tmp;
}
break;
case 24:
/* swap B with R */
for(i = 0; i < cmaplen; i++){
tmp = cmap[3*i+0];
cmap[3*i+0] = cmap[3*i+2];
cmap[3*i+2] = tmp;
}
break;
case 16:
/* convert to 24-bit colormap */
if((cmap = realloc(cmap, 3*cmaplen)) == nil)
return -1;
for(i = cmaplen-1; i >= 0; i--){
x = (cmap[2*i+1]<<8) | cmap[2*i+0];
tmp = (x>>0)&0x1f;
cmap[3*i+2] = (tmp<<3) | (tmp>>2);
tmp = (x>>5)&0x1f;
cmap[3*i+1] = (tmp<<3) | (tmp>>2);
tmp = (x>>10)&0x1f;
cmap[3*i+0] = (tmp<<3) | (tmp>>2);
}
*cmapbpp = 24;
break;
default:
break;
}
return 0;
}
static Tga *
rdhdr(Biobuf *bp)
{
int n;
Tga *h;
if((h = malloc(sizeof(Tga))) == nil)
return nil;
if((h->idlen = Bgetc(bp)) == -1)
return nil;
if((h->cmaptype = Bgetc(bp)) == -1)
return nil;
if((h->datatype = Bgetc(bp)) == -1)
return nil;
if((h->cmaporigin = Bgeti(bp)) == -1)
return nil;
if((h->cmaplen = Bgeti(bp)) == -1)
return nil;
if((h->cmapbpp = Bgetc(bp)) == -1)
return nil;
if((h->xorigin = Bgeti(bp)) == -1)
return nil;
if((h->yorigin = Bgeti(bp)) == -1)
return nil;
if((h->width = Bgeti(bp)) == -1)
return nil;
if((h->height = Bgeti(bp)) == -1)
return nil;
if((h->bpp = Bgetc(bp)) == -1)
return nil;
if((h->descriptor = Bgetc(bp)) == -1)
return nil;
/* skip over ID, usually empty anyway */
if(Bseek(bp, h->idlen, 1) < 0){
free(h);
return nil;
}
if(h->cmaptype == 0){
h->cmap = 0;
return h;
}
/* skip over unused color map data */
n = (h->cmapbpp/8)*h->cmaporigin;
if(Bseek(bp, n, 1) < 0){
free(h);
return nil;
}
h->cmaplen -= h->cmaporigin;
n = (h->cmapbpp/8)*h->cmaplen;
if((h->cmap = malloc(n)) == nil){
free(h);
return nil;
}
if(Bread(bp, h->cmap, n) != n){
free(h);
free(h->cmap);
return nil;
}
if(fixcmap(h->cmap, &h->cmapbpp, h->cmaplen) != 0){
free(h);
free(h->cmap);
return nil;
}
return h;
}
static int
cmap(Biobuf *bp, uchar *l, int num)
{
return Bread(bp, l, num);
}
static int
luma(Biobuf *bp, int bpp, uchar *l, int num)
{
char tmp[2];
int got;
if(bpp == 8){
got = Bread(bp, l, num);
}
else{
for(got = 0; got < num; got++){
if(Bread(bp, tmp, 2) != 2)
break;
*l++ = tmp[0];
}
}
return got;
}
static int
luma_rle(Biobuf *bp, int bpp, uchar *l, int num)
{
uchar len, p;
int got;
for(got = 0; got < num;){
if(Bread(bp, &len, 1) != 1)
break;
if(len & 0x80){
len &= 0x7f;
if(luma(bp, bpp, &p, 1) != 1)
break;
for(len++; len > 0 && got < num; len--, got++)
*l++ = p;
}
else{
for(len++; len > 0 && got < num; len--, got++)
if(luma(bp, bpp, l++, 1) != 1)
return got;
}
}
return got;
}
static int
cmap_rle(Biobuf *bp, uchar *l, int num)
{
return luma_rle(bp, 8, l, num);
}
static int
rgba(Biobuf *bp, int bpp, uchar *r, uchar *g, uchar *b, int num)
{
int i;
uchar buf[4], tmp;
ushort x;
switch(bpp){
case 16:
for(i = 0; i < num; i++){
if(Bread(bp, buf, 2) != 2)
break;
x = (buf[1]<<8) | buf[0];
tmp = (x>>0)&0x1f;
*b++ = (tmp<<3) | (tmp>>2);
tmp = (x>>5)&0x1f;
*g++ = (tmp<<3) | (tmp>>2);
tmp = (x>>10)&0x1f;
*r++ = (tmp<<3) | (tmp>>2);
}
break;
case 24:
for(i = 0; i < num; i++){
if(Bread(bp, buf, 3) != 3)
break;
*b++ = buf[0];
*g++ = buf[1];
*r++ = buf[2];
}
break;
case 32:
for(i = 0; i < num; i++){
if(Bread(bp, buf, 4) != 4)
break;
*b++ = buf[0];
*g++ = buf[1];
*r++ = buf[2];
}
break;
default:
i = 0;
break;
}
return i;
}
static int
rgba_rle(Biobuf *bp, int bpp, uchar *r, uchar *g, uchar *b, int num)
{
uchar len;
int i, got;
for(got = 0; got < num; got += len){
if(Bread(bp, &len, 1) != 1)
break;
if(len & 0x80){
len &= 0x7f;
len += 1; /* run of zero is meaningless */
if(rgba(bp, bpp, r, g, b, 1) != 1)
break;
for(i = 1; i < len && got+i < num; i++){
r[i] = *r;
g[i] = *g;
b[i] = *b;
}
len = i;
}
else{
len += 1; /* raw block of zero is meaningless */
if(rgba(bp, bpp, r, g, b, len) != len)
break;
}
r += len;
g += len;
b += len;
}
return got;
}
int
flip(Rawimage *ar)
{
int w, h, c, l;
uchar *t, *s, *d;
w = Dx(ar->r);
h = Dy(ar->r);
if((t = malloc(w)) == nil){
werrstr("ReadTGA: no memory - %r\n");
return -1;
}
for(c = 0; c < ar->nchans; c++){
s = ar->chans[c];
d = ar->chans[c] + ar->chanlen - w;
for(l = 0; l < (h/2); l++){
memcpy(t, s, w);
memcpy(s, d, w);
memcpy(d, t, w);
s += w;
d -= w;
}
}
free(t);
return 0;
}
int
reflect(Rawimage *ar)
{
int w, h, c, l, p;
uchar t, *sol, *eol, *s, *d;
w = Dx(ar->r);
h = Dy(ar->r);
for(c = 0; c < ar->nchans; c++){
sol = ar->chans[c];
eol = ar->chans[c] +w -1;
for(l = 0; l < h; l++){
s = sol;
d = eol;
for(p = 0; p < w/2; p++){
t = *s;
*s = *d;
*d = t;
s++;
d--;
}
sol += w;
eol += w;
}
}
return 0;
}
Rawimage**
Breadtga(Biobuf *bp)
{
Tga *h;
int n, c, num;
uchar *r, *g, *b;
Rawimage *ar, **array;
if((h = rdhdr(bp)) == nil){
werrstr("ReadTGA: bad header %r");
return nil;
}
if(0){
fprint(2, "idlen=%d\n", h->idlen);
fprint(2, "cmaptype=%d\n", h->cmaptype);
fprint(2, "datatype=%s\n", datatype[h->datatype]);
fprint(2, "cmaporigin=%d\n", h->cmaporigin);
fprint(2, "cmaplen=%d\n", h->cmaplen);
fprint(2, "cmapbpp=%d\n", h->cmapbpp);
fprint(2, "xorigin=%d\n", h->xorigin);
fprint(2, "yorigin=%d\n", h->yorigin);
fprint(2, "width=%d\n", h->width);
fprint(2, "height=%d\n", h->height);
fprint(2, "bpp=%d\n", h->bpp);
fprint(2, "descriptor=%d\n", h->descriptor);
}
array = nil;
if((ar = calloc(sizeof(Rawimage), 1)) == nil){
werrstr("ReadTGA: no memory - %r\n");
goto Error;
}
if((array = calloc(sizeof(Rawimage *), 2)) == nil){
werrstr("ReadTGA: no memory - %r\n");
goto Error;
}
array[0] = ar;
array[1] = nil;
if(h->datatype == 3 || h->datatype == 11){
ar->nchans = 1;
ar->chandesc = CY;
}
else if(h->datatype == 1){
ar->nchans = 1;
ar->chandesc = CRGB1;
}
else if(h->datatype == 9){
ar->nchans = 1;
ar->chandesc = (h->cmapbpp == 32) ? CRGBV : CRGB1;
}
else{
ar->nchans = 3;
ar->chandesc = CRGB;
}
ar->cmap = h->cmap;
ar->cmaplen = (h->cmapbpp/8)*h->cmaplen;
ar->chanlen = h->width*h->height;
ar->r = Rect(0, 0, h->width, h->height);
for(c = 0; c < ar->nchans; c++)
if((ar->chans[c] = malloc(h->width*h->height)) == nil){
werrstr("ReadTGA: no memory - %r\n");
goto Error;
}
r = ar->chans[0];
g = ar->chans[1];
b = ar->chans[2];
num = h->width*h->height;
switch(h->datatype){
case 1:
n = cmap(bp, r, num);
break;
case 2:
n = rgba(bp, h->bpp, r, g, b, num);
break;
case 3:
n = luma(bp, h->bpp, r, num);
break;
case 9:
n = cmap_rle(bp, r, num);
break;
case 10:
n = rgba_rle(bp, h->bpp, r, g, b, num);
break;
case 11:
n = luma_rle(bp, h->bpp, r, num);
break;
default:
werrstr("ReadTGA: type=%d (%s) unsupported\n", h->datatype, datatype[h->datatype]);
goto Error;
}
if(n != num){
werrstr("ReadTGA: decode fail (%d!=%d) - %r\n", n, num);
goto Error;
}
if((h->descriptor&(1<<4)) != 0)
reflect(ar);
if((h->descriptor&(1<<5)) == 0)
flip(ar);
free(h);
return array;
Error:
if(ar)
for (c = 0; c < ar->nchans; c++)
free(ar->chans[c]);
free(ar);
free(array);
free(h->cmap);
free(h);
return nil;
}
Rawimage**
readtga(int fd)
{
Rawimage * *a;
Biobuf b;
if(Binit(&b, fd, OREAD) < 0)
return nil;
a = Breadtga(&b);
Bterm(&b);
return a;
}
|