|
| 1 | +#pragma author MrClock |
| 2 | +#pragma description Arma 3 PAA image format |
| 3 | + |
| 4 | +#pragma endian little |
| 5 | + |
| 6 | +#pragma MIME image/x.a3-paa |
| 7 | + |
| 8 | +import type.color; |
| 9 | +import std.mem; |
| 10 | +import std.sys; |
| 11 | + |
| 12 | +struct Color<auto alpha> { |
| 13 | + u8 b; |
| 14 | + u8 g; |
| 15 | + u8 r; |
| 16 | + if (alpha) u8 a; |
| 17 | +} [[sealed, format("type::impl::format_color"), color(std::format("{0:02X}{1:02X}{2:02X}", r, g, b))]]; |
| 18 | + |
| 19 | +using BGR8 = Color<false>; |
| 20 | +using BGRA8 = Color<true>; |
| 21 | + |
| 22 | +enum PixelFormat: u16 { |
| 23 | + DXT1 = 0xFF01, |
| 24 | + DXT2 = 0xFF02, |
| 25 | + DXT3 = 0xFF03, |
| 26 | + DXT4 = 0xFF04, |
| 27 | + DXT5 = 0xFF05, |
| 28 | + RGBA4 = 0x4444, |
| 29 | + RGBA5 = 0x1555, |
| 30 | + RGBA8 = 0x8888, |
| 31 | + GRAY = 0x8080 |
| 32 | +}; |
| 33 | + |
| 34 | +enum AlphaMode: u32 { |
| 35 | + NONE = 0, |
| 36 | + INTERPOLATED = 1, |
| 37 | + BINARY = 2 |
| 38 | +}; |
| 39 | + |
| 40 | +enum Swizzle: u8 { |
| 41 | + ALPHA = 0, |
| 42 | + RED = 1, |
| 43 | + GREEN = 2, |
| 44 | + BLUE = 3, |
| 45 | + INVERTED_ALPHA = 4, |
| 46 | + INVERTED_RED = 5, |
| 47 | + INVERTED_GREEN = 6, |
| 48 | + INVERTED_BLUE = 7, |
| 49 | + BLANK_WHITE = 8, |
| 50 | + BLANK_BLACK = 9 |
| 51 | +}; |
| 52 | + |
| 53 | +struct Tagg { |
| 54 | + char signature[8]; |
| 55 | + u32 length; |
| 56 | + |
| 57 | + match (signature) { |
| 58 | + ("GGATCGVA"): BGRA8 color; |
| 59 | + ("GGATCXAM"): BGRA8 color; |
| 60 | + ("GGATGALF"): AlphaMode alpha; |
| 61 | + ("GGATSFFO"): u32 offsets[16]; |
| 62 | + ("GGATZIWS"): Swizzle copies[4]; |
| 63 | + (_): u8 data[length]; |
| 64 | + } |
| 65 | +} [[format("format_tagg_name")]]; |
| 66 | + |
| 67 | +struct Palette { |
| 68 | + u16 length; |
| 69 | + BGR8 colors[length]; |
| 70 | +}; |
| 71 | + |
| 72 | +struct Mipmap { |
| 73 | + u16 width_and_lzo [[format("format_width_lzo")]]; |
| 74 | + u16 height; |
| 75 | + |
| 76 | + u16 width = width_and_lzo & 0x7FFF; |
| 77 | + |
| 78 | + if (width == 0 && height == 0) { |
| 79 | + break; |
| 80 | + } |
| 81 | + |
| 82 | + u24 size; |
| 83 | + u8 data[size]; |
| 84 | +} [[format("format_resolution")]]; |
| 85 | + |
| 86 | +struct PAA { |
| 87 | + PixelFormat format; |
| 88 | + Tagg taggs[while(std::mem::read_string($, 4) == "GGAT")]; |
| 89 | + Palette palette; |
| 90 | + Mipmap mipmaps[while(true)]; |
| 91 | + u16 EOF; |
| 92 | + |
| 93 | + std::assert_warn(EOF == 0, "Invalid EOF sentinel"); |
| 94 | +}; |
| 95 | + |
| 96 | +fn format_resolution(ref auto mip) { |
| 97 | + return std::format("{0:d} x {1:d}", mip.width, mip.height); |
| 98 | +}; |
| 99 | + |
| 100 | +fn format_width_lzo(u16 value) { |
| 101 | + u16 width = value & 0x7FFF; |
| 102 | + if (value & 0x8000) { |
| 103 | + return std::format("{0:d} (+LZO)", width); |
| 104 | + } else { |
| 105 | + return std::format("{0:d}", width); |
| 106 | + } |
| 107 | +}; |
| 108 | + |
| 109 | +fn format_tagg_name(Tagg data) { |
| 110 | + match (data.signature) { |
| 111 | + ("GGATCGVA"): return "Average color"; |
| 112 | + ("GGATCXAM"): return "Max color"; |
| 113 | + ("GGATGALF"): return "Alpha flag"; |
| 114 | + ("GGATZIWS"): return "Swizzle"; |
| 115 | + ("GGATSFFO"): return "Mipmap offsets"; |
| 116 | + (_): return "Unknown"; |
| 117 | + } |
| 118 | +}; |
| 119 | + |
| 120 | +PAA file @ 0x0000; |
0 commit comments