// (Triforce, 11-29) BC7 (BPTC unorm) decoding to RGBA8, for the Character page's textures (Link's face and adult hair are BC7). // Written from the format's specification (the D3D11 functional spec / KHR_texture_compression_bptc): 8 modes, 1-3 subsets, // partition tables, P-bits, index rotation and the separate alpha indexes of modes 4 and 5. namespace Triforce.Core.GameFiles; public static class Bc7 { struct Mode { public int Subsets, PartBits, RotBits, IdxSelBits, ColorBits, AlphaBits, EndPBits, SharedPBits, IdxBits, Idx2Bits; public Mode(int ns, int pb, int rb, int isb, int cb, int ab, int epb, int spb, int ib, int ib2) { Subsets = ns; PartBits = pb; RotBits = rb; IdxSelBits = isb; ColorBits = cb; AlphaBits = ab; EndPBits = epb; SharedPBits = spb; IdxBits = ib; Idx2Bits = ib2; } } static readonly Mode[] Modes = { new(3, 4, 0, 0, 4, 0, 1, 0, 3, 0), new(2, 6, 0, 0, 6, 0, 0, 1, 3, 0), new(3, 6, 0, 0, 5, 0, 0, 0, 2, 0), new(2, 6, 0, 0, 7, 0, 1, 0, 2, 0), new(1, 0, 2, 1, 5, 6, 0, 0, 2, 3), new(1, 0, 2, 0, 7, 8, 0, 0, 2, 2), new(1, 0, 0, 0, 7, 7, 1, 0, 4, 0), new(2, 6, 0, 0, 5, 5, 1, 0, 2, 0), }; static readonly byte[] W2 = { 0, 21, 43, 64 }; static readonly byte[] W3 = { 0, 9, 18, 27, 37, 46, 55, 64 }; static readonly byte[] W4 = { 0, 4, 9, 13, 17, 21, 26, 30, 34, 38, 43, 47, 51, 55, 60, 64 }; static readonly uint[] P2 = { 0xCCCC, 0x8888, 0xEEEE, 0xECC8, 0xC880, 0xFEEC, 0xFEC8, 0xEC80, 0xC800, 0xFFEC, 0xFE80, 0xE800, 0xFFE8, 0xFF00, 0xFFF0, 0xF000, 0xF710, 0x008E, 0x7100, 0x08CE, 0x008C, 0x7310, 0x3100, 0x8CCE, 0x088C, 0x3110, 0x6666, 0x366C, 0x17E8, 0x0FF0, 0x718E, 0x399C, 0xAAAA, 0xF0F0, 0x5A5A, 0x33CC, 0x3C3C, 0x55AA, 0x9696, 0xA55A, 0x73CE, 0x13C8, 0x324C, 0x3BDC, 0x6996, 0xC33C, 0x9966, 0x0660, 0x0272, 0x04E4, 0x4E40, 0x2720, 0xC936, 0x936C, 0x39C6, 0x639C, 0x9336, 0x9CC6, 0x817E, 0xE718, 0xCCF0, 0x0FCC, 0x7744, 0xEE22, }; static readonly string P3 = "0011001102212222" + "0001001122112221" + "0000200122112211" + "0222002200110111" + "0000000011221122" + "0011001100220022" + "0022002211111111" + "0011001122112211" + "0000000011112222" + "0000111111112222" + "0000111122222222" + "0012001200120012" + "0112011201120112" + "0122012201220122" + "0011011211221222" + "0011200122002220" + "0001001101121122" + "0111001120012200" + "0000112211221122" + "0022002200221111" + "0111011102220222" + "0001000122212221" + "0000001101220122" + "0000110022102210" + "0122012200110000" + "0012001211222222" + "0110122112210110" + "0000011012211221" + "0022110211020022" + "0110011020022222" + "0011012201220011" + "0000200022112221" + "0000000211221222" + "0222002200120011" + "0011001200220222" + "0120012001200120" + "0000111122220000" + "0120120120120120" + "0120201212010120" + "0011220011220011" + "0011112222000011" + "0101010122222222" + "0000000021212121" + "0022112200221122" + "0022001100220011" + "0220122102201221" + "0101222222220101" + "0000212121212121" + "0101010101012222" + "0222011102220111" + "0002111200021112" + "0000211221122112" + "0222011101110222" + "0002111211120002" + "0110011001102222" + "0000000021122112" + "0110011022222222" + "0022001100110022" + "0022112211220022" + "0000000000002112" + "0002000100020001" + "0222122202221222" + "0101222222222222" + "0111201122012220"; static readonly byte[] A2 = { 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 2, 8, 2, 2, 8, 8, 15, 2, 8, 2, 2, 8, 8, 2, 2, 15, 15, 6, 8, 2, 8, 15, 15, 2, 8, 2, 2, 2, 15, 15, 6, 6, 2, 6, 8, 15, 15, 2, 2, 15, 15, 15, 15, 15, 2, 2, 15, }; static readonly byte[] A3a = { 3, 3, 15, 15, 8, 3, 15, 15, 8, 8, 6, 6, 6, 5, 3, 3, 3, 3, 8, 15, 3, 3, 6, 10, 5, 8, 8, 6, 8, 5, 15, 15, 8, 15, 3, 5, 6, 10, 8, 15, 15, 3, 15, 5, 15, 15, 15, 15, 3, 15, 5, 5, 5, 8, 5, 10, 5, 10, 8, 13, 15, 12, 3, 3, }; static readonly byte[] A3b = { 15, 8, 8, 3, 15, 15, 3, 8, 15, 15, 15, 15, 15, 15, 15, 8, 15, 8, 15, 3, 15, 8, 15, 8, 3, 15, 6, 10, 15, 15, 10, 8, 15, 3, 15, 10, 10, 8, 9, 10, 6, 15, 8, 15, 3, 6, 6, 8, 15, 3, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 3, 15, 15, 8, }; ref struct Bits { readonly ReadOnlySpan b; int pos; public Bits(ReadOnlySpan block) { b = block; pos = 0; } public int Read(int n) { var v = 0; for (var i = 0; i < n; i++, pos++) v |= ((b[pos >> 3] >> (pos & 7)) & 1) << i; return v; } } /// BC7 blocks (16 bytes each, rows of blocks) decoded to RGBA8. public static byte[] Decode(ReadOnlySpan blocks, int width, int height) { ImageLimits.Check(width, height); int wb = (width + 3) / 4, hb = (height + 3) / 4; var output = new byte[width * height * 4]; Span texels = stackalloc byte[64]; for (var by = 0; by < hb; by++) for (var bx = 0; bx < wb; bx++) { DecodeBlock(blocks.Slice((by * wb + bx) * 16, 16), texels); for (var y = 0; y < 4 && by * 4 + y < height; y++) { var count = Math.Min(4, width - bx * 4); texels.Slice(y * 16, count * 4).CopyTo(output.AsSpan(((by * 4 + y) * width + bx * 4) * 4)); } } return output; } static int Subset(int subsets, int partition, int i) => subsets switch { 2 => (int)((P2[partition] >> i) & 1), 3 => P3[partition * 16 + i] - '0', _ => 0, }; static bool IsAnchor(int subsets, int partition, int i) => i == 0 || (subsets == 2 && i == A2[partition]) || (subsets == 3 && (i == A3a[partition] || i == A3b[partition])); static int Interp(int a, int b, int index, int bits) { var w = bits == 2 ? W2[index] : bits == 3 ? W3[index] : W4[index]; return ((64 - w) * a + w * b + 32) >> 6; } static void DecodeBlock(ReadOnlySpan block, Span texels) { var mode = 0; while (mode < 8 && ((block[0] >> mode) & 1) == 0) mode++; if (mode == 8) { texels.Clear(); return; } var m = Modes[mode]; var bits = new Bits(block); bits.Read(mode + 1); var partition = bits.Read(m.PartBits); var rotation = bits.Read(m.RotBits); var idxSel = bits.Read(m.IdxSelBits); // endpoints: [subset*2 + end][channel] Span ep = stackalloc int[6 * 4]; var ns = m.Subsets; for (var c = 0; c < 3; c++) for (var e = 0; e < ns * 2; e++) ep[e * 4 + c] = bits.Read(m.ColorBits); for (var e = 0; e < ns * 2; e++) ep[e * 4 + 3] = m.AlphaBits > 0 ? bits.Read(m.AlphaBits) : 255; var colorBits = m.ColorBits; var alphaBits = m.AlphaBits; if (m.EndPBits > 0) { for (var e = 0; e < ns * 2; e++) { var p = bits.Read(1); for (var c = 0; c < 3; c++) ep[e * 4 + c] = (ep[e * 4 + c] << 1) | p; if (alphaBits > 0) ep[e * 4 + 3] = (ep[e * 4 + 3] << 1) | p; } colorBits++; if (alphaBits > 0) alphaBits++; } else if (m.SharedPBits > 0) { for (var s = 0; s < ns; s++) { var p = bits.Read(1); for (var e = s * 2; e < s * 2 + 2; e++) for (var c = 0; c < 3; c++) ep[e * 4 + c] = (ep[e * 4 + c] << 1) | p; } colorBits++; } // expand to 8 bits for (var e = 0; e < ns * 2; e++) { for (var c = 0; c < 3; c++) ep[e * 4 + c] = Expand(ep[e * 4 + c], colorBits); if (m.AlphaBits > 0) ep[e * 4 + 3] = Expand(ep[e * 4 + 3], alphaBits); } Span idx1 = stackalloc int[16]; Span idx2 = stackalloc int[16]; for (var i = 0; i < 16; i++) { var anchor = IsAnchor(ns, partition, i) && (ns == 1 ? i == 0 : true) && IsAnchorFor(ns, partition, i); idx1[i] = bits.Read(anchor ? m.IdxBits - 1 : m.IdxBits); } if (m.Idx2Bits > 0) for (var i = 0; i < 16; i++) idx2[i] = bits.Read(i == 0 ? m.Idx2Bits - 1 : m.Idx2Bits); for (var i = 0; i < 16; i++) { var s = Subset(ns, partition, i); int e0 = s * 2 * 4, e1 = (s * 2 + 1) * 4; int r, g, b, a; if (m.Idx2Bits > 0) { int cIdx = idxSel == 0 ? idx1[i] : idx2[i], aIdx = idxSel == 0 ? idx2[i] : idx1[i]; int cBits = idxSel == 0 ? m.IdxBits : m.Idx2Bits, aBits = idxSel == 0 ? m.Idx2Bits : m.IdxBits; r = Interp(ep[e0], ep[e1], cIdx, cBits); g = Interp(ep[e0 + 1], ep[e1 + 1], cIdx, cBits); b = Interp(ep[e0 + 2], ep[e1 + 2], cIdx, cBits); a = Interp(ep[e0 + 3], ep[e1 + 3], aIdx, aBits); } else { r = Interp(ep[e0], ep[e1], idx1[i], m.IdxBits); g = Interp(ep[e0 + 1], ep[e1 + 1], idx1[i], m.IdxBits); b = Interp(ep[e0 + 2], ep[e1 + 2], idx1[i], m.IdxBits); a = m.AlphaBits > 0 ? Interp(ep[e0 + 3], ep[e1 + 3], idx1[i], m.IdxBits) : 255; } switch (rotation) { case 1: (a, r) = (r, a); break; case 2: (a, g) = (g, a); break; case 3: (a, b) = (b, a); break; } texels[i * 4] = (byte)r; texels[i * 4 + 1] = (byte)g; texels[i * 4 + 2] = (byte)b; texels[i * 4 + 3] = (byte)a; } } /// Whether texel i is the anchor of its own subset (its index has one bit less). static bool IsAnchorFor(int ns, int partition, int i) { if (i == 0) return true; if (ns == 2) return i == A2[partition]; if (ns == 3) return i == A3a[partition] || i == A3b[partition]; return false; } static int Expand(int v, int bits) => bits >= 8 ? v : (v << (8 - bits)) | (v >> (2 * bits - 8)); }