--!strict --!native --!optimize 2 -- codecs/image/Bcn/Decode: the BC1..BC7 block decoders (S3TC / DXT1-5, RGTC, BPTC). Ported from Pillow's -- src/libImaging/BcnDecode.c (CC0, licenses/pillow-bcn-CC0.txt) and changed to follow the D3D11 functional spec where -- Pillow does not: BC6H interpolation rounds ((a * (64 - w) + b * w + 32) >> 6), BC6H keeps its float values (no 8-bit -- clamp), BC7's reserved mode 8 and BC6H's reserved modes decode to transparent / zero, BC4 / BC5 SNORM interpolate -- the signed values (-128 reads as -127). Each decoder writes one 4x4 block into a 64-byte (rgba8) or 256-byte -- (rgbaf32) scratch block; the caller clips it into the image. Never throws: the caller checks the source size. local Half = require("@formats/codecs/image/Half") local Decode = {} local readu8, readu16, readu32, writeu8, writeu32, writef32 = buffer.readu8, buffer.readu16, buffer.readu32, buffer.writeu8, buffer.writeu32, buffer.writef32 local band, bor, rshift, lshift = bit32.band, bit32.bor, bit32.rshift, bit32.lshift local HALF = Half.TABLE -- ------------------------------------------------------------------------------------------------ BC1 / BC2 / BC3 local pal = buffer.create(16) -- 4 rgba8 colours local function c565(c: number): (number, number, number) local r = rshift(band(c, 0xF800), 8) local g = rshift(band(c, 0x7E0), 3) local b = lshift(band(c, 0x1F), 3) return bor(r, rshift(r, 5)), bor(g, rshift(g, 6)), bor(b, rshift(b, 5)) end -- the BC1 colour block at p into blk (64 bytes); fourColour = BC2 / BC3 (always the 4-colour palette) local function colour(src: buffer, p: number, blk: buffer, fourColour: boolean) local c0, c1 = readu16(src, p), readu16(src, p + 2) local r0, g0, b0 = c565(c0) local r1, g1, b1 = c565(c1) writeu32(pal, 0, r0 + g0 * 256 + b0 * 65536 + 0xFF000000) writeu32(pal, 4, r1 + g1 * 256 + b1 * 65536 + 0xFF000000) if c0 > c1 or fourColour then writeu32(pal, 8, (2 * r0 + r1) // 3 + ((2 * g0 + g1) // 3) * 256 + ((2 * b0 + b1) // 3) * 65536 + 0xFF000000) writeu32(pal, 12, (r0 + 2 * r1) // 3 + ((g0 + 2 * g1) // 3) * 256 + ((b0 + 2 * b1) // 3) * 65536 + 0xFF000000) else writeu32(pal, 8, (r0 + r1) // 2 + ((g0 + g1) // 2) * 256 + ((b0 + b1) // 2) * 65536 + 0xFF000000) writeu32(pal, 12, 0) end local idx = readu32(src, p + 4) for i = 0, 15 do writeu32(blk, i * 4, readu32(pal, band(rshift(idx, i * 2), 3) * 4)) end end -- the 8 values of a BC3 alpha / BC4 block (unsigned) into vals local vals = table.create(8, 0) local function ramp8(a0: number, a1: number) vals[1], vals[2] = a0, a1 if a0 > a1 then for i = 1, 6 do vals[i + 2] = ((7 - i) * a0 + i * a1) // 7 end else for i = 1, 4 do vals[i + 2] = ((5 - i) * a0 + i * a1) // 5 end vals[7], vals[8] = 0, 255 end end -- signed (SNORM) ramp, values -127..127 rounded to the nearest integer, biased by 128 into 1..255 local function ramp8s(u0: number, u1: number) local s0: number = math.max(if u0 >= 128 then u0 - 256 else u0, -127) local s1: number = math.max(if u1 >= 128 then u1 - 256 else u1, -127) vals[1], vals[2] = s0 + 128, s1 + 128 if s0 > s1 then for i = 1, 6 do vals[i + 2] = math.floor(((7 - i) * s0 + i * s1) / 7 + 0.5) + 128 end else for i = 1, 4 do vals[i + 2] = math.floor(((5 - i) * s0 + i * s1) / 5 + 0.5) + 128 end vals[7], vals[8] = 1, 255 end end -- one 8-byte BC3 alpha / BC4 block at p into byte `channel` of every blk pixel (and the next ones when spread) local function single(src: buffer, p: number, blk: buffer, channel: number, signed: boolean, spread: boolean) if signed then ramp8s(readu8(src, p), readu8(src, p + 1)) else ramp8(readu8(src, p), readu8(src, p + 1)) end local lo = readu8(src, p + 2) + readu8(src, p + 3) * 256 + readu8(src, p + 4) * 65536 local hi = readu8(src, p + 5) + readu8(src, p + 6) * 256 + readu8(src, p + 7) * 65536 for i = 0, 15 do local sel = if i < 8 then band(rshift(lo, i * 3), 7) else band(rshift(hi, (i - 8) * 3), 7) local v = vals[sel + 1] local o = i * 4 if spread then writeu32(blk, o, v + v * 256 + v * 65536 + 0xFF000000) else writeu8(blk, o + channel, v) end end end function Decode.bc1(src: buffer, p: number, blk: buffer) colour(src, p, blk, false) end function Decode.bc2(src: buffer, p: number, blk: buffer) colour(src, p + 8, blk, true) for i = 0, 15 do local a = band(rshift(readu8(src, p + rshift(i, 1)), band(i, 1) * 4), 15) writeu8(blk, i * 4 + 3, a * 17) end end function Decode.bc3(src: buffer, p: number, blk: buffer) colour(src, p + 8, blk, true) single(src, p, blk, 3, false, false) end function Decode.bc4(src: buffer, p: number, blk: buffer) single(src, p, blk, 0, false, true) end function Decode.bc4s(src: buffer, p: number, blk: buffer) single(src, p, blk, 0, true, true) end local function bc5(src: buffer, p: number, blk: buffer, signed: boolean) for i = 0, 15 do writeu32(blk, i * 4, 0xFF000000) end single(src, p, blk, 0, signed, false) single(src, p + 8, blk, 1, signed, false) end function Decode.bc5(src: buffer, p: number, blk: buffer) bc5(src, p, blk, false) end function Decode.bc5s(src: buffer, p: number, blk: buffer) bc5(src, p, blk, true) end -- ------------------------------------------------------------------------------------------------ BPTC tables -- partition tables (BC7 Table.P2 1 bit per pixel, Table.P3 2 bits per pixel) and the anchor indices local SI2 = { 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, } local SI3 = { 0xaa685050, 0x6a5a5040, 0x5a5a4200, 0x5450a0a8, 0xa5a50000, 0xa0a05050, 0x5555a0a0, 0x5a5a5050, 0xaa550000, 0xaa555500, 0xaaaa5500, 0x90909090, 0x94949494, 0xa4a4a4a4, 0xa9a59450, 0x2a0a4250, 0xa5945040, 0x0a425054, 0xa5a5a500, 0x55a0a0a0, 0xa8a85454, 0x6a6a4040, 0xa4a45000, 0x1a1a0500, 0x0050a4a4, 0xaaa59090, 0x14696914, 0x69691400, 0xa08585a0, 0xaa821414, 0x50a4a450, 0x6a5a0200, 0xa9a58000, 0x5090a0a8, 0xa8a09050, 0x24242424, 0x00aa5500, 0x24924924, 0x24499224, 0x50a50a50, 0x500aa550, 0xaaaa4444, 0x66660000, 0xa5a0a5a0, 0x50a050a0, 0x69286928, 0x44aaaa44, 0x66666600, 0xaa444444, 0x54a854a8, 0x95809580, 0x96969600, 0xa85454a8, 0x80959580, 0xaa141414, 0x96960000, 0xaaaa1414, 0xa05050a0, 0xa0a5a5a0, 0x96000000, 0x40804080, 0xa9a8a9a8, 0xaaaaaa44, 0x2a4a5254, } local AI0 = { 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, } local AI1 = { 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, } local AI2 = { 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, } local W2 = { [0] = 0, 21, 43, 64 } local W3 = { [0] = 0, 9, 18, 27, 37, 46, 55, 64 } local W4 = { [0] = 0, 4, 9, 13, 17, 21, 26, 30, 34, 38, 43, 47, 51, 55, 60, 64 } local WEIGHTS = { [2] = W2, [3] = W3, [4] = W4 } Decode.SI2, Decode.SI3, Decode.AI0, Decode.AI1, Decode.AI2, Decode.WEIGHTS = SI2, SI3, AI0, AI1, AI2, WEIGHTS local function subset(ns: number, part: number, i: number): number if ns == 2 then return band(rshift(SI2[part + 1], i), 1) elseif ns == 3 then return band(rshift(SI3[part + 1], 2 * i), 3) end return 0 end -- the 128-bit block as four u32 words: bits(at, n) for n <= 16 local w0, w1, w2, w3 = 0, 0, 0, 0 local function load(src: buffer, p: number) w0, w1, w2, w3 = readu32(src, p), readu32(src, p + 4), readu32(src, p + 8), readu32(src, p + 12) end local function word(i: number): number return if i == 0 then w0 elseif i == 1 then w1 elseif i == 2 then w2 else w3 end local function bits(at: number, n: number): number if n == 0 then return 0 end local wi, sh = rshift(at, 5), band(at, 31) local v = rshift(word(wi), sh) if sh + n > 32 and wi < 3 then v = bor(v, lshift(word(wi + 1), 32 - sh)) end return band(v, lshift(1, n) - 1) end -- ------------------------------------------------------------------------------------------------ BC7 -- ns, pb, rb, isb, cb, ab, epb, spb, ib, ib2 local MODES7 = { { 3, 4, 0, 0, 4, 0, 1, 0, 3, 0 }, { 2, 6, 0, 0, 6, 0, 0, 1, 3, 0 }, { 3, 6, 0, 0, 5, 0, 0, 0, 2, 0 }, { 2, 6, 0, 0, 7, 0, 1, 0, 2, 0 }, { 1, 0, 2, 1, 5, 6, 0, 0, 2, 3 }, { 1, 0, 2, 0, 7, 8, 0, 0, 2, 2 }, { 1, 0, 0, 0, 7, 7, 1, 0, 4, 0 }, { 2, 6, 0, 0, 5, 5, 1, 0, 2, 0 }, } local er, eg, eb, ea = table.create(6, 0), table.create(6, 0), table.create(6, 0), table.create(6, 0) local function expand(v: number, n: number): number v = band(lshift(v, 8 - n), 255) return bor(v, rshift(v, n)) end function Decode.bc7(src: buffer, p: number, blk: buffer) local m0 = readu8(src, p) if m0 == 0 then buffer.fill(blk, 0, 0, 64) return end load(src, p) local mode = 0 while band(m0, lshift(1, mode)) == 0 do mode += 1 end local info = MODES7[mode + 1] local ns, pb, rb, isb, cb, ab, epb, spb, ib, ib2 = info[1], info[2], info[3], info[4], info[5], info[6], info[7], info[8], info[9], info[10] local bit = mode + 1 local part = bits(bit, pb) bit += pb local rot = bits(bit, rb) bit += rb local sel = bits(bit, isb) bit += isb local nep = ns * 2 for i = 1, nep do er[i] = bits(bit, cb) bit += cb end for i = 1, nep do eg[i] = bits(bit, cb) bit += cb end for i = 1, nep do eb[i] = bits(bit, cb) bit += cb end for i = 1, nep do if ab > 0 then ea[i] = bits(bit, ab) bit += ab else ea[i] = 255 end end if epb == 1 then cb += 1 if ab > 0 then ab += 1 end for i = 1, nep do local v = bits(bit, 1) bit += 1 er[i], eg[i], eb[i] = er[i] * 2 + v, eg[i] * 2 + v, eb[i] * 2 + v if ab > 0 then ea[i] = ea[i] * 2 + v end end end if spb == 1 then cb += 1 if ab > 0 then ab += 1 end for i = 1, nep, 2 do local v = bits(bit, 1) bit += 1 for j = i, i + 1 do er[j], eg[j], eb[j] = er[j] * 2 + v, eg[j] * 2 + v, eb[j] * 2 + v if ab > 0 then ea[j] = ea[j] * 2 + v end end end end for i = 1, nep do er[i], eg[i], eb[i] = expand(er[i], cb), expand(eg[i], cb), expand(eb[i], cb) if ab > 0 then ea[i] = expand(ea[i], ab) end end local cw = WEIGHTS[ib] local aw = if ab > 0 and ib2 > 0 then WEIGHTS[ib2] else cw local cbit = bit local abit = cbit + 16 * ib - ns local a1 = if ns == 2 then AI0[part + 1] elseif ns == 3 then AI1[part + 1] else -1 local a2 = if ns == 3 then AI2[part + 1] else -1 for i = 0, 15 do local s = subset(ns, part, i) * 2 + 1 local n = ib if i == 0 or i == a1 or i == a2 then n -= 1 end local i0 = bits(cbit, n) cbit += n local wc, wa if ab > 0 and ib2 > 0 then local n2 = if i == 0 then ib2 - 1 else ib2 local i1 = bits(abit, n2) abit += n2 if sel == 1 then wc, wa = aw[i1], cw[i0] else wc, wa = cw[i0], aw[i1] end else wc, wa = cw[i0], cw[i0] end local tc, ta = 64 - wc, 64 - wa local r = rshift(tc * er[s] + wc * er[s + 1] + 32, 6) local g = rshift(tc * eg[s] + wc * eg[s + 1] + 32, 6) local b = rshift(tc * eb[s] + wc * eb[s + 1] + 32, 6) local a = rshift(ta * ea[s] + wa * ea[s + 1] + 32, 6) if rot == 1 then r, a = a, r elseif rot == 2 then g, a = a, g elseif rot == 3 then b, a = a, b end writeu32(blk, i * 4, r + g * 256 + b * 65536 + a * 16777216) end end -- ------------------------------------------------------------------------------------------------ BC6H -- ns, transformed, pb, epb, rb, gb, bb local MODES6 = { { 2, 1, 5, 10, 5, 5, 5 }, { 2, 1, 5, 7, 6, 6, 6 }, { 2, 1, 5, 11, 5, 4, 4 }, { 2, 1, 5, 11, 4, 5, 4 }, { 2, 1, 5, 11, 4, 4, 5 }, { 2, 1, 5, 9, 5, 5, 5 }, { 2, 1, 5, 8, 6, 5, 5 }, { 2, 1, 5, 8, 5, 6, 5 }, { 2, 1, 5, 8, 5, 5, 6 }, { 2, 0, 5, 6, 6, 6, 6 }, { 1, 0, 0, 10, 10, 10, 10 }, { 1, 1, 0, 11, 9, 9, 9 }, { 1, 1, 0, 12, 8, 8, 8 }, { 1, 1, 0, 16, 4, 4, 4 }, } -- Table.F: for each endpoint bit in stream order, endpoint * 16 + bit (endpoints r0 g0 b0 r1 g1 b1 r2 ... b3) local PACK6 = { { 116, 132, 180, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 48, 49, 50, 51, 52, 164, 112, 113, 114, 115, 64, 65, 66, 67, 68, 176, 160, 161, 162, 163, 80, 81, 82, 83, 84, 177, 128, 129, 130, 131, 96, 97, 98, 99, 100, 178, 144, 145, 146, 147, 148, 179 }, { 117, 164, 165, 0, 1, 2, 3, 4, 5, 6, 176, 177, 132, 16, 17, 18, 19, 20, 21, 22, 133, 178, 116, 32, 33, 34, 35, 36, 37, 38, 179, 181, 180, 48, 49, 50, 51, 52, 53, 112, 113, 114, 115, 64, 65, 66, 67, 68, 69, 160, 161, 162, 163, 80, 81, 82, 83, 84, 85, 128, 129, 130, 131, 96, 97, 98, 99, 100, 101, 144, 145, 146, 147, 148, 149 }, { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 48, 49, 50, 51, 52, 10, 112, 113, 114, 115, 64, 65, 66, 67, 26, 176, 160, 161, 162, 163, 80, 81, 82, 83, 42, 177, 128, 129, 130, 131, 96, 97, 98, 99, 100, 178, 144, 145, 146, 147, 148, 179 }, { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 48, 49, 50, 51, 10, 164, 112, 113, 114, 115, 64, 65, 66, 67, 68, 26, 160, 161, 162, 163, 80, 81, 82, 83, 42, 177, 128, 129, 130, 131, 96, 97, 98, 99, 176, 178, 144, 145, 146, 147, 116, 179 }, { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 48, 49, 50, 51, 10, 132, 112, 113, 114, 115, 64, 65, 66, 67, 26, 176, 160, 161, 162, 163, 80, 81, 82, 83, 84, 42, 128, 129, 130, 131, 96, 97, 98, 99, 177, 178, 144, 145, 146, 147, 180, 179 }, { 0, 1, 2, 3, 4, 5, 6, 7, 8, 132, 16, 17, 18, 19, 20, 21, 22, 23, 24, 116, 32, 33, 34, 35, 36, 37, 38, 39, 40, 180, 48, 49, 50, 51, 52, 164, 112, 113, 114, 115, 64, 65, 66, 67, 68, 176, 160, 161, 162, 163, 80, 81, 82, 83, 84, 177, 128, 129, 130, 131, 96, 97, 98, 99, 100, 178, 144, 145, 146, 147, 148, 179 }, { 0, 1, 2, 3, 4, 5, 6, 7, 164, 132, 16, 17, 18, 19, 20, 21, 22, 23, 178, 116, 32, 33, 34, 35, 36, 37, 38, 39, 179, 180, 48, 49, 50, 51, 52, 53, 112, 113, 114, 115, 64, 65, 66, 67, 68, 176, 160, 161, 162, 163, 80, 81, 82, 83, 84, 177, 128, 129, 130, 131, 96, 97, 98, 99, 100, 101, 144, 145, 146, 147, 148, 149 }, { 0, 1, 2, 3, 4, 5, 6, 7, 176, 132, 16, 17, 18, 19, 20, 21, 22, 23, 117, 116, 32, 33, 34, 35, 36, 37, 38, 39, 165, 180, 48, 49, 50, 51, 52, 164, 112, 113, 114, 115, 64, 65, 66, 67, 68, 69, 160, 161, 162, 163, 80, 81, 82, 83, 84, 177, 128, 129, 130, 131, 96, 97, 98, 99, 100, 178, 144, 145, 146, 147, 148, 179 }, { 0, 1, 2, 3, 4, 5, 6, 7, 177, 132, 16, 17, 18, 19, 20, 21, 22, 23, 133, 116, 32, 33, 34, 35, 36, 37, 38, 39, 181, 180, 48, 49, 50, 51, 52, 164, 112, 113, 114, 115, 64, 65, 66, 67, 68, 176, 160, 161, 162, 163, 80, 81, 82, 83, 84, 85, 128, 129, 130, 131, 96, 97, 98, 99, 100, 178, 144, 145, 146, 147, 148, 179 }, { 0, 1, 2, 3, 4, 5, 164, 176, 177, 132, 16, 17, 18, 19, 20, 21, 117, 133, 178, 116, 32, 33, 34, 35, 36, 37, 165, 179, 181, 180, 48, 49, 50, 51, 52, 53, 112, 113, 114, 115, 64, 65, 66, 67, 68, 69, 160, 161, 162, 163, 80, 81, 82, 83, 84, 85, 128, 129, 130, 131, 96, 97, 98, 99, 100, 101, 144, 145, 146, 147, 148, 149 }, { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89 }, { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 48, 49, 50, 51, 52, 53, 54, 55, 56, 10, 64, 65, 66, 67, 68, 69, 70, 71, 72, 26, 80, 81, 82, 83, 84, 85, 86, 87, 88, 42 }, { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 48, 49, 50, 51, 52, 53, 54, 55, 11, 10, 64, 65, 66, 67, 68, 69, 70, 71, 27, 26, 80, 81, 82, 83, 84, 85, 86, 87, 43, 42 }, { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 48, 49, 50, 51, 15, 14, 13, 12, 11, 10, 64, 65, 66, 67, 31, 30, 29, 28, 27, 26, 80, 81, 82, 83, 47, 46, 45, 44, 43, 42 }, } local ep = table.create(12, 0) local uq = table.create(12, 0) local function signExtend(v: number, n: number): number v = band(v, lshift(1, n) - 1) return if v >= lshift(1, n - 1) then v - lshift(1, n) else v end local function unquantize(x: number, n: number, signed: boolean): number if not signed then if n >= 15 or x == 0 then return x elseif x == lshift(1, n) - 1 then return 0xFFFF end return rshift(x * 32768 + 0x4000, n - 1) end if n >= 16 then return x end local neg = x < 0 if neg then x = -x end if x ~= 0 then if x >= lshift(1, n - 1) - 1 then x = 0x7FFF else x = rshift(x * 32768 + 0x4000, n - 1) end end return if neg then -x else x end local function finish(v: number, signed: boolean): number if signed then if v < 0 then return -HALF[(-v * 31) // 32] end return HALF[(v * 31) // 32] end return HALF[(v * 31) // 64] end -- one BC6H block into blk as rgbaf32 (256 bytes, alpha 1) function Decode.bc6h(src: buffer, p: number, blk: buffer, signed: boolean) load(src, p) local m = band(w0, 31) local mode, bit, epbits, ib local low = band(m, 3) if low < 2 then mode, bit, epbits, ib = low, 2, 75, 3 elseif low == 2 then mode, bit, epbits, ib = 2 + rshift(m, 2), 5, 72, 3 else mode, bit, epbits, ib = 10 + rshift(m, 2), 5, 60, 4 end if mode >= 14 then for i = 0, 15 do local o = i * 16 writef32(blk, o, 0) writef32(blk, o + 4, 0) writef32(blk, o + 8, 0) writef32(blk, o + 12, 1) end return end local info = MODES6[mode + 1] local ns, tr, pb, epb, rbits, gbits, bbits = info[1], info[2], info[3], info[4], info[5], info[6], info[7] local nep = if ns == 2 then 12 else 6 for i = 1, 12 do ep[i] = 0 end local pack = PACK6[mode + 1] for i = 1, epbits do local d = pack[i] local e = rshift(d, 4) + 1 ep[e] = bor(ep[e], lshift(bits(bit + i - 1, 1), band(d, 15))) end bit += epbits local part = bits(bit, pb) bit += pb local mask = lshift(1, epb) - 1 if signed then ep[1], ep[2], ep[3] = signExtend(ep[1], epb), signExtend(ep[2], epb), signExtend(ep[3], epb) end if signed or tr == 1 then for i = 4, nep, 3 do ep[i], ep[i + 1], ep[i + 2] = signExtend(ep[i], rbits), signExtend(ep[i + 1], gbits), signExtend(ep[i + 2], bbits) end end if tr == 1 then for i = 4, nep, 3 do for c = 0, 2 do local v = band(ep[i + c] + ep[1 + c], mask) ep[i + c] = if signed then signExtend(v, epb) else v end end end for i = 1, nep do uq[i] = unquantize(ep[i], epb, signed) end local w = WEIGHTS[ib] local a1 = if ns == 2 then AI0[part + 1] else -1 for i = 0, 15 do local s = subset(ns, part, i) * 6 + 1 local n = if i == 0 or i == a1 then ib - 1 else ib local wt = w[bits(bit, n)] bit += n local t = 64 - wt local o = i * 16 -- floor((a * t + b * w + 32) / 64): the products may be negative (signed) - // floors like >> would writef32(blk, o, finish((uq[s] * t + uq[s + 3] * wt + 32) // 64, signed)) writef32(blk, o + 4, finish((uq[s + 1] * t + uq[s + 4] * wt + 32) // 64, signed)) writef32(blk, o + 8, finish((uq[s + 2] * t + uq[s + 5] * wt + 32) // 64, signed)) writef32(blk, o + 12, 1) end end return Decode