-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtga.go
470 lines (399 loc) · 12.9 KB
/
tga.go
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
// Official specification: https://www.dca.fee.unicamp.br/~martino/disciplinas/ea978/tgaffs.pdf
package tga
import (
"bytes"
"encoding/binary"
"fmt"
"image"
"image/color"
"io"
)
type ImageType byte
const (
NoImage ImageType = iota // no image data is present
UncompressedColorMappedImage // 1 uncompressed color-mapped image
UncompressedRGBImage // 2 uncompressed true-color image
UncompressedGrayscaleImage // 3 uncompressed black-and-white (grayscale) image
RunLengthEncodedColorMappedImage = iota + 5 // 9 run-length encoded color-mapped image
RunLengthEncodedRGBImage // 10 run-length encoded true-color image
RunLengthEncodedGrayscaleImage // 11 run-length encoded black-and-white (grayscale) image
)
type TargaSize byte
const (
Targa16 TargaSize = 16
Targa24 TargaSize = 24
Targa32 TargaSize = 32
)
type File struct {
Header Header
Image Image
Footer Footer
}
func (f File) Pixels() [][]byte {
pixels := make([][]byte, 0)
for i := 0; i < f.Header.ImageBytes(); i += f.Header.BytesPerPixel() {
end := i + f.Header.BytesPerPixel()
if end > f.Header.ImageBytes() {
end = f.Header.ImageBytes()
}
pixels = append(pixels, f.Image.Data[i:end])
}
return pixels
}
func (f File) PixelAt(x, y int) []byte {
if x >= int(f.Header.Width) || y >= int(f.Header.Height) {
return nil
}
bytesPerPixel := f.Header.BytesPerPixel()
x = x * bytesPerPixel
y = y * bytesPerPixel
// row * width + column
begin := y*int(f.Header.Width) + x
return f.Image.Data[begin : begin+bytesPerPixel]
}
// RGBA only supports Targa 24 and Targa 32 for now
func (f File) RGBA() *image.RGBA {
img := image.NewRGBA(image.Rect(0, 0, int(f.Header.Width), int(f.Header.Height)))
for y := 0; y < img.Bounds().Max.Y; y++ {
for x := 0; x < img.Bounds().Max.X; x++ {
pixel := f.PixelAt(x, y)
img.Set(x, y, color.RGBA{
R: pixel[2],
G: pixel[1],
B: pixel[0],
A: 255,
})
}
}
return img
}
func (f File) Version() Version {
return f.Footer.version()
}
type ImageOrigin int
const (
BottomLeft ImageOrigin = iota
BottomRight
TopLeft
TopRight
)
func (o ImageOrigin) String() string {
return [...]string{"BottomLeft", "BottomRight", "TopLeft", "TopRight"}[o]
}
type ImageDescriptor byte
func (id ImageDescriptor) ImageOrigin() ImageOrigin {
switch {
case id&48 == 48:
return TopRight
case id&32 == 32:
return TopLeft
case id&16 == 16:
return BottomRight
default:
return BottomLeft
}
}
type Header struct {
IDLength byte // byte
ColorMapType byte // byte
ImageType ImageType // byte
ColorMapOrigin uint16 // 2 bytes, little-endian
ColorMapLength uint16 // 2 bytes, little-endian
ColorMapDepth TargaSize // byte
XOrigin uint16 // 2 bytes, little-endian
YOrigin uint16 // 2 bytes, little-endian
Width uint16 // 2 bytes, little-endian
Height uint16 // 2 bytes, little-endian
BitsPerPixel byte // byte
ImageDescriptor ImageDescriptor // byte
}
func (h Header) Rect() image.Rectangle {
return image.Rect(0, 0, int(h.Width), int(h.Height))
}
func (h Header) HasImageIDField() bool {
return h.IDLength > 0
}
func (h Header) HasColorMap() bool {
return h.ColorMapType == 1
}
func (h Header) BytesPerPixel() int {
return int(h.BitsPerPixel) / 8
}
func (h Header) ImageBytes() int {
return int(h.Width) * int(h.Height) * h.BytesPerPixel()
}
type Image struct {
ID []byte
ColorMap []byte
Data []byte
}
type Version int
const (
OriginalTGA Version = iota
NewTGA
)
func (v Version) String() string {
return [...]string{"OriginalTGA", "NewTGA"}[v]
}
type Footer struct {
ExtensionAreaOffset uint32 // Bytes 0-3: The Extension Area Offset
DeveloperDirectoryOffset uint32 // Bytes 4-7: The Developer Directory Offset
Signature [16]byte // Bytes 8-23: The Signature
Point byte // Byte 24: ASCII Character “.”
End byte // Byte 25: Binary zero string terminator (0x00)
}
// TODO: ensure its needed here, and not in File struct
func (f Footer) version() Version {
if string(f.Signature[:]) == "TRUEVISION-XFILE" {
return NewTGA
}
return OriginalTGA
}
type sectionConfig struct {
length int64
offset int64
whence int
}
func newSection(length int, offset int, whence int) sectionConfig {
return sectionConfig{
length: int64(length),
offset: int64(offset),
whence: whence,
}
}
var (
headerSection = newSection(18, 0, io.SeekStart)
footerSection = newSection(26, -26, io.SeekEnd)
)
func read(rs io.ReadSeeker, config sectionConfig, data any) error {
r := bytes.NewBuffer(nil)
_, err := rs.Seek(config.offset, int(config.whence))
if err != nil {
return fmt.Errorf("failed to seek file: %v", err)
}
// ensure reader is always in the beginning of the file
defer func() {
if config.offset != 0 && config.whence != io.SeekStart {
rs.Seek(0, io.SeekStart)
}
}()
_, err = io.CopyN(r, rs, config.length)
if err != nil {
return fmt.Errorf("failed to io.CopyN bytes: %v", err)
}
return binary.Read(r, binary.LittleEndian, data)
}
func Read(rs io.ReadSeeker) (File, error) {
var file File
err := read(rs, footerSection, &file.Footer)
if err != nil {
return file, fmt.Errorf("tga.Read: failed to read binary data into Footer: %v", err)
}
err = read(rs, headerSection, &file.Header)
if err != nil {
return file, fmt.Errorf("tga.Read: failed to read binary data into Header: %v", err)
}
file.Image = Image{
ID: make([]byte, file.Header.IDLength),
ColorMap: []byte{},
Data: make([]byte, file.Header.ImageBytes()),
}
// Read ImageID (CopyN of Header.IDLength)
err = read(rs,
newSection(len(file.Image.ID), int(headerSection.length), io.SeekStart),
file.Image.ID)
if err != nil {
return file, fmt.Errorf("tga.Read: failed to read binary data into ImageData: %v", err)
}
// Read ColorMapData (CopyN of ???)
// Read ImageData (CopyN of Header.Width * Header.Height)
err = read(rs,
newSection(len(file.Image.Data), int(headerSection.length)+len(file.Image.ID), io.SeekStart),
file.Image.Data)
if err != nil {
return file, fmt.Errorf("tga.Read: failed to read binary data info Image.Data: %v", err)
}
return file, err
}
// from: http://www.paulbourke.net/dataformats/tga/
// #include "stdio.h"
// #include "stdlib.h"
// #include "math.h"
// /*
// The following is rather crude demonstration code to read
// uncompressed and compressed TGA files of 16, 24, or 32 bit
// TGA.
// Hope it is helpful.
// */
// typedef struct {
// char idlength;
// char colourmaptype;
// char datatypecode;
// short int colourmaporigin;
// short int colourmaplength;
// char colourmapdepth;
// short int x_origin;
// short int y_origin;
// short width;
// short height;
// char bitsperpixel;
// char imagedescriptor;
// } HEADER;
// typedef struct {
// unsigned char r,g,b,a;
// } PIXEL;
// void MergeBytes(PIXEL *,unsigned char *,int);
// int main(int argc,char **argv)
// {
// int n=0,i,j;
// int bytes2read,skipover = 0;
// unsigned char p[5];
// FILE *fptr;
// HEADER header;
// PIXEL *pixels;
// if (argc < 2) {
// fprintf(stderr,"Usage: %s tgafile\n",argv[0]);
// exit(-1);
// }
// /* Open the file */
// if ((fptr = fopen(argv[1],"r")) == NULL) {
// fprintf(stderr,"File open failed\n");
// exit(-1);
// }
// /* Display the header fields */
// header.idlength = fgetc(fptr);
// fprintf(stderr,"ID length: %d\n",header.idlength);
// header.colourmaptype = fgetc(fptr);
// fprintf(stderr,"Colourmap type: %d\n",header.colourmaptype);
// header.datatypecode = fgetc(fptr);
// fprintf(stderr,"Image type: %d\n",header.datatypecode);
// fread(&header.colourmaporigin,2,1,fptr);
// fprintf(stderr,"Colour map offset: %d\n",header.colourmaporigin);
// fread(&header.colourmaplength,2,1,fptr);
// fprintf(stderr,"Colour map length: %d\n",header.colourmaplength);
// header.colourmapdepth = fgetc(fptr);
// fprintf(stderr,"Colour map depth: %d\n",header.colourmapdepth);
// fread(&header.x_origin,2,1,fptr);
// fprintf(stderr,"X origin: %d\n",header.x_origin);
// fread(&header.y_origin,2,1,fptr);
// fprintf(stderr,"Y origin: %d\n",header.y_origin);
// fread(&header.width,2,1,fptr);
// fprintf(stderr,"Width: %d\n",header.width);
// fread(&header.height,2,1,fptr);
// fprintf(stderr,"Height: %d\n",header.height);
// header.bitsperpixel = fgetc(fptr);
// fprintf(stderr,"Bits per pixel: %d\n",header.bitsperpixel);
// header.imagedescriptor = fgetc(fptr);
// fprintf(stderr,"Descriptor: %d\n",header.imagedescriptor);
// /* Allocate space for the image */
// if ((pixels = malloc(header.width*header.height*sizeof(PIXEL))) == NULL) {
// fprintf(stderr,"malloc of image failed\n");
// exit(-1);
// }
// for (i=0;i<header.width*header.height;i++) {
// pixels[i].r = 0;
// pixels[i].g = 0;
// pixels[i].b = 0;
// pixels[i].a = 0;
// }
// /* What can we handle */
// if (header.datatypecode != 2 && header.datatypecode != 10) {
// printf(stderr,"Can only handle image type 2 and 10\n");
// exit(-1);
// }
// if (header.bitsperpixel != 16 &&
// header.bitsperpixel != 24 && header.bitsperpixel != 32) {
// fprintf(stderr,"Can only handle pixel depths of 16, 24, and 32\n");
// exit(-1);
// }
// if (header.colourmaptype != 0 && header.colourmaptype != 1) {
// fprintf(stderr,"Can only handle colour map types of 0 and 1\n");
// exit(-1);
// }
// /* Skip over unnecessary stuff */
// skipover += header.idlength;
// skipover += header.colourmaptype * header.colourmaplength;
// fprintf(stderr,"Skip over %d bytes\n",skipover);
// fseek(fptr,skipover,SEEK_CUR);
// /* Read the image */
// bytes2read = header.bitsperpixel / 8;
// while (n < header.width * header.height) {
// if (header.datatypecode == 2) { /* Uncompressed */
// if (fread(p,1,bytes2read,fptr) != bytes2read) {
// fprintf(stderr,"Unexpected end of file at pixel %d\n",i);
// exit(-1);
// }
// MergeBytes(&(pixels[n]),p,bytes2read);
// n++;
// } else if (header.datatypecode == 10) { /* Compressed */
// if (fread(p,1,bytes2read+1,fptr) != bytes2read+1) {
// fprintf(stderr,"Unexpected end of file at pixel %d\n",i);
// exit(-1);
// }
// j = p[0] & 0x7f;
// MergeBytes(&(pixels[n]),&(p[1]),bytes2read);
// n++;
// if (p[0] & 0x80) { /* RLE chunk */
// for (i=0;i<j;i++) {
// MergeBytes(&(pixels[n]),&(p[1]),bytes2read);
// n++;
// }
// } else { /* Normal chunk */
// for (i=0;i<j;i++) {
// if (fread(p,1,bytes2read,fptr) != bytes2read) {
// fprintf(stderr,"Unexpected end of file at pixel %d\n",i);
// exit(-1);
// }
// MergeBytes(&(pixels[n]),p,bytes2read);
// n++;
// }
// }
// }
// }
// fclose(fptr);
// /* Write the result as a uncompressed TGA */
// if ((fptr = fopen("tgatest.tga","w")) == NULL) {
// fprintf(stderr,"Failed to open outputfile\n");
// exit(-1);
// }
// putc(0,fptr);
// putc(0,fptr);
// putc(2,fptr); /* uncompressed RGB */
// putc(0,fptr); putc(0,fptr);
// putc(0,fptr); putc(0,fptr);
// putc(0,fptr);
// putc(0,fptr); putc(0,fptr); /* X origin */
// putc(0,fptr); putc(0,fptr); /* y origin */
// putc((header.width & 0x00FF),fptr);
// putc((header.width & 0xFF00) / 256,fptr);
// putc((header.height & 0x00FF),fptr);
// putc((header.height & 0xFF00) / 256,fptr);
// putc(32,fptr); /* 24 bit bitmap */
// putc(0,fptr);
// for (i=0;i<header.height*header.width;i++) {
// putc(pixels[i].b,fptr);
// putc(pixels[i].g,fptr);
// putc(pixels[i].r,fptr);
// putc(pixels[i].a,fptr);
// }
// fclose(fptr);
// }
// void MergeBytes(PIXEL *pixel,unsigned char *p,int bytes)
// {
// if (bytes == 4) {
// pixel->r = p[2];
// pixel->g = p[1];
// pixel->b = p[0];
// pixel->a = p[3];
// } else if (bytes == 3) {
// pixel->r = p[2];
// pixel->g = p[1];
// pixel->b = p[0];
// pixel->a = 255;
// } else if (bytes == 2) {
// pixel->r = (p[1] & 0x7c) << 1;
// pixel->g = ((p[1] & 0x03) << 6) | ((p[0] & 0xe0) >> 2);
// pixel->b = (p[0] & 0x1f) << 3;
// pixel->a = (p[1] & 0x80);
// }
// }