-
-
Notifications
You must be signed in to change notification settings - Fork 181
/
tga.hexpat
82 lines (68 loc) · 1.6 KB
/
tga.hexpat
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
#pragma description Truevision TGA/TARGA image
#pragma MIME image/tga
#pragma endian little
import std.mem;
struct ColorMapSpec {
u16 entryIndex;
u16 entryLength;
u8 bpp;
} [[static]];
bitfield ImageDesc {
alphaDepth : 4;
orderRightToLeft : 1;
orderTopToBottom : 1;
padding : 2;
};
struct ImageSpec {
u16 xOrigin;
u16 yOrigin;
u16 width;
u16 height;
u8 depth;
ImageDesc imageDesc;
} [[static]];
enum ColorMapType : u8 {
None,
ColorPalette,
};
enum ImageType : u8 {
NoData,
UncompressedColorMapped,
UncompressedRGB,
UncompressedGreyscale,
RLEColorMapped = 9,
RLERGB,
RLEGreyscale,
// huffman + delta + rle
CompressedColorMapped = 32,
// huffman + delta + rle, 4-pass quadtree-type
CompressedColorMapped4Pass,
};
fn GetImageDataSize(ImageSpec imSpec) {
return imSpec.height * imSpec.width * (imSpec.depth / 8);
};
fn GetColorMapDataSize(ColorMapSpec cmSpec) {
return cmSpec.entryLength * (cmSpec.bpp / 8);
};
struct Header {
u8 idLength;
ColorMapType colorMapType;
ImageType imageType;
ColorMapSpec colorMapSpec;
ImageSpec imageSpec;
char imageId[idLength];
if (colorMapType == ColorMapType::ColorPalette) {
u8 colorMapData[GetColorMapDataSize(colorMapSpec)];
}
u8 imageData[GetImageDataSize(imageSpec)];
};
struct Footer {
u32 extensionOffset;
u32 developerDirectoryOffset;
char signature[0x10];
char dot;
char zero;
};
u8 visualizer[std::mem::size()] @ 0x00 [[sealed, hex::visualize("image", this)]];
Header header @ 0x0;
Footer footer @ std::mem::size() - 0x1A;