diff --git a/docs/FORMATS.md b/docs/FORMATS.md index 0a41a66..7b021d0 100644 --- a/docs/FORMATS.md +++ b/docs/FORMATS.md @@ -114,8 +114,11 @@ Codecs (layer 3, shared by formats; not loadable on their own), by domain: - codecs/font/Cff: Compact Font Format (Adobe TN #5176) and Type 2 charstrings (TN #5177): the outlines of OpenType fonts - codecs/font/TrueType: the sfnt font container (TrueType .ttf, OpenType .otf with CFF outlines, collections .ttc) parsed - **image** + - codecs/image/Bcn: the BCn block-compressed texture codecs, shared by the texture formats (DDS, KTX, VTF, BLP, ...): - codecs/image/Blend: layer compositing on rgba8 buffers (straight alpha): source-over with the separable and non-separable - codecs/image/Dct: the 8x8 DCTs JPEG, Motion-JPEG and MPEG-1 share (native; never throws). + - codecs/image/Etc: the Ericsson / Khronos block texture codecs, decode only: ETC1, ETC2 RGB (with the T, H and planar + - codecs/image/Half: IEEE 754 binary16 ("half") <-> number, for the float texel formats (DDS / KTX R16F..., BC6H, - codecs/image/Jpeg: the JPEG codec (ITU T.81) on byte ranges: image/jpeg is a thin format over it, and the Motion-JPEG - codecs/image/Pixels: row loops between packed pixel layouts and rgba8 (native), shared by the bitmap formats (BMP, - codecs/image/Quantize: palette quantization of rgba8 pixels. A codec (layer 1): image/gif (and later any palette diff --git a/src/Formats/codecs/image/Etc.luau b/src/Formats/codecs/image/Etc.luau new file mode 100644 index 0000000..4c8720f --- /dev/null +++ b/src/Formats/codecs/image/Etc.luau @@ -0,0 +1,315 @@ +--!strict +--!native +--!optimize 2 +-- codecs/image/Etc: the Ericsson / Khronos block texture codecs, decode only: ETC1, ETC2 RGB (with the T, H and planar +-- modes), ETC2 RGBA (an EAC alpha block + ETC2 colour), ETC2 RGB A1 (punch-through alpha) and EAC R11 / RG11 +-- (unsigned and signed). Written from the Khronos Data Format Specification 1.3, section 21 (ETC1 / ETC2 / EAC); new +-- in formats-luau. Shared by the texture formats (KTX, PVR, ...). Never throws: the caller gets a message instead. +-- +-- local px, err = Etc.decode("etc2_rgba", buf, offset, w, h) -- rgba8 pixels +-- local px16 = Etc.decode("eac_rg11", buf, offset, w, h, nil, 0, true) -- rgba16 (EAC keeps its 11 bits) +-- Etc.size("etc1", w, h) -- bytes of the block data +-- +-- Kinds: etc1, etc2_rgb, etc2_rgba, etc2_rgba1, eac_r11, eac_r11s, eac_rg11, eac_rg11s. R11 decodes to grey (r = g = b), +-- RG11 to red + green (blue 0); signed values are biased (-1 -> 1, 0 -> 128, +1 -> 255; rgba16: 0 / 32768 / 65535). +local Etc = {} + +Etc.BLOCK = table.freeze({ etc1 = 8, etc2_rgb = 8, etc2_rgba = 16, etc2_rgba1 = 8, eac_r11 = 8, eac_r11s = 8, eac_rg11 = 16, eac_rg11s = 16 }) :: { [string]: number } + +local readu8, readu32, writeu8, writeu16, writeu32 = buffer.readu8, buffer.readu32, buffer.writeu8, buffer.writeu16, buffer.writeu32 +local band, rshift, byteswap = bit32.band, bit32.rshift, bit32.byteswap + +-- ------------------------------------------------------------------------------------------------ tables +local MOD = { -- the ETC1 intensity modifiers per table: index (msb * 2 + lsb) -> a, b, -a, -b + { 2, 8, -2, -8 }, + { 5, 17, -5, -17 }, + { 9, 29, -9, -29 }, + { 13, 42, -13, -42 }, + { 18, 60, -18, -60 }, + { 24, 80, -24, -80 }, + { 33, 106, -33, -106 }, + { 47, 183, -47, -183 }, +} +local DIST = { 3, 6, 11, 16, 23, 32, 41, 64 } -- T / H mode distances +local EAC = { + { -3, -6, -9, -15, 2, 5, 8, 14 }, + { -3, -7, -10, -13, 2, 6, 9, 12 }, + { -2, -5, -8, -13, 1, 4, 7, 12 }, + { -2, -4, -6, -13, 1, 3, 5, 12 }, + { -3, -6, -8, -12, 2, 5, 7, 11 }, + { -3, -7, -9, -11, 2, 6, 8, 10 }, + { -4, -7, -8, -11, 3, 6, 7, 10 }, + { -3, -5, -8, -11, 2, 4, 7, 10 }, + { -2, -6, -8, -10, 1, 5, 7, 9 }, + { -2, -5, -8, -10, 1, 4, 7, 9 }, + { -2, -4, -8, -10, 1, 3, 7, 9 }, + { -2, -5, -7, -10, 1, 4, 6, 9 }, + { -3, -4, -7, -10, 2, 3, 6, 9 }, + { -1, -2, -3, -10, 0, 1, 2, 9 }, + { -4, -6, -8, -9, 3, 5, 7, 8 }, + { -3, -5, -7, -9, 2, 4, 6, 8 }, +} + +local function clamp8(v: number): number + return if v < 0 then 0 elseif v > 255 then 255 else v +end +local function rgb(r: number, g: number, b: number): number + return clamp8(r) + clamp8(g) * 256 + clamp8(b) * 65536 + 0xFF000000 +end + +-- ------------------------------------------------------------------------------------------------ colour blocks +-- blk: 16 u32 rgba8 pixels in raster order (y * 4 + x) +local P = table.create(4, 0) -- T / H paint colours (rgba8 u32) + +local function be32(src: buffer, p: number): number + return byteswap(readu32(src, p)) +end + +-- the 2-bit pixel index of pixel i (column-major: i = x * 4 + y) +local function pix(lo: number, i: number): number + return band(rshift(lo, i + 15), 2) + band(rshift(lo, i), 1) +end + +-- ETC1 / ETC2 colour block at p into blk. etc2: T / H / planar modes; punch: the RGB A1 variant (bit 33 = opaque). +local function colour(src: buffer, p: number, blk: buffer, etc2: boolean, punch: boolean) + local hi, lo = be32(src, p), be32(src, p + 4) + local diffBit = band(rshift(hi, 1), 1) + local flip = band(hi, 1) + local opaque = not punch or diffBit == 1 + local r1, g1, b1, r2, g2, b2 + if not punch and diffBit == 0 then + r1, r2 = band(rshift(hi, 28), 15) * 17, band(rshift(hi, 24), 15) * 17 + g1, g2 = band(rshift(hi, 20), 15) * 17, band(rshift(hi, 16), 15) * 17 + b1, b2 = band(rshift(hi, 12), 15) * 17, band(rshift(hi, 8), 15) * 17 + else + local R, G, B = band(rshift(hi, 27), 31), band(rshift(hi, 19), 31), band(rshift(hi, 11), 31) + local dR, dG, dB = band(rshift(hi, 24), 7), band(rshift(hi, 16), 7), band(rshift(hi, 8), 7) + if dR >= 4 then + dR -= 8 + end + if dG >= 4 then + dG -= 8 + end + if dB >= 4 then + dB -= 8 + end + local R2, G2, B2 = R + dR, G + dG, B + dB + if etc2 and (R2 < 0 or R2 > 31) then + -- T mode + local ra = band(rshift(hi, 27), 3) * 4 + band(rshift(hi, 24), 3) + local c1r, c1g, c1b = ra * 17, band(rshift(hi, 20), 15) * 17, band(rshift(hi, 16), 15) * 17 + local c2r, c2g, c2b = band(rshift(hi, 12), 15) * 17, band(rshift(hi, 8), 15) * 17, band(rshift(hi, 4), 15) * 17 + local d = DIST[band(rshift(hi, 2), 3) * 2 + band(hi, 1) + 1] + P[1] = rgb(c1r, c1g, c1b) + P[2] = rgb(c2r + d, c2g + d, c2b + d) + P[3] = rgb(c2r, c2g, c2b) + P[4] = rgb(c2r - d, c2g - d, c2b - d) + elseif etc2 and (G2 < 0 or G2 > 31) then + -- H mode + local q1r = band(rshift(hi, 27), 15) + local q1g = band(rshift(hi, 24), 7) * 2 + band(rshift(hi, 20), 1) + local q1b = band(rshift(hi, 19), 1) * 8 + band(rshift(hi, 15), 7) + local q2r, q2g, q2b = band(rshift(hi, 11), 15), band(rshift(hi, 7), 15), band(rshift(hi, 3), 15) + local order = if q1r * 256 + q1g * 16 + q1b >= q2r * 256 + q2g * 16 + q2b then 1 else 0 + local d = DIST[band(rshift(hi, 2), 1) * 4 + band(hi, 1) * 2 + order + 1] + local c1r, c1g, c1b, c2r, c2g, c2b = q1r * 17, q1g * 17, q1b * 17, q2r * 17, q2g * 17, q2b * 17 + P[1] = rgb(c1r + d, c1g + d, c1b + d) + P[2] = rgb(c1r - d, c1g - d, c1b - d) + P[3] = rgb(c2r + d, c2g + d, c2b + d) + P[4] = rgb(c2r - d, c2g - d, c2b - d) + elseif etc2 and (B2 < 0 or B2 > 31) then + -- planar (always opaque) + local ro = band(rshift(hi, 25), 63) + local go = band(rshift(hi, 24), 1) * 64 + band(rshift(hi, 17), 63) + local bo = band(rshift(hi, 16), 1) * 32 + band(rshift(hi, 11), 3) * 8 + band(rshift(hi, 7), 7) + local rh = band(rshift(hi, 2), 31) * 2 + band(hi, 1) + local gh, bh = band(rshift(lo, 25), 127), band(rshift(lo, 19), 63) + local rv, gv, bv = band(rshift(lo, 13), 63), band(rshift(lo, 6), 127), band(lo, 63) + ro, rh, rv = ro * 4 + rshift(ro, 4), rh * 4 + rshift(rh, 4), rv * 4 + rshift(rv, 4) + go, gh, gv = go * 2 + rshift(go, 6), gh * 2 + rshift(gh, 6), gv * 2 + rshift(gv, 6) + bo, bh, bv = bo * 4 + rshift(bo, 4), bh * 4 + rshift(bh, 4), bv * 4 + rshift(bv, 4) + for y = 0, 3 do + for x = 0, 3 do + local r = (x * (rh - ro) + y * (rv - ro) + 4 * ro + 2) // 4 + local g = (x * (gh - go) + y * (gv - go) + 4 * go + 2) // 4 + local b = (x * (bh - bo) + y * (bv - bo) + 4 * bo + 2) // 4 + writeu32(blk, (y * 4 + x) * 4, rgb(r, g, b)) + end + end + return + else + r1, g1, b1 = R * 8 + rshift(R, 2), G * 8 + rshift(G, 2), B * 8 + rshift(B, 2) + r2, g2, b2 = R2 * 8 + rshift(R2, 2), G2 * 8 + rshift(G2, 2), B2 * 8 + rshift(B2, 2) + end + if not r1 then + -- T / H: the paint colours by index + for i = 0, 15 do + local x, y = rshift(i, 2), band(i, 3) + local idx = pix(lo, i) + local c = if not opaque and idx == 2 then 0 else P[idx + 1] + writeu32(blk, (y * 4 + x) * 4, c) + end + return + end + end + local t1, t2 = MOD[band(rshift(hi, 5), 7) + 1], MOD[band(rshift(hi, 2), 7) + 1] + for i = 0, 15 do + local x, y = rshift(i, 2), band(i, 3) + local second = if flip == 0 then x >= 2 else y >= 2 + local idx = pix(lo, i) + local c + if not opaque and idx == 2 then + c = 0 + else + local m = if second then t2[idx + 1] else t1[idx + 1] + if not opaque and idx == 0 then + m = 0 + end + if second then + c = rgb((r2 :: number) + m, (g2 :: number) + m, (b2 :: number) + m) + else + c = rgb((r1 :: number) + m, (g1 :: number) + m, (b1 :: number) + m) + end + end + writeu32(blk, (y * 4 + x) * 4, c) + end +end + +-- ------------------------------------------------------------------------------------------------ EAC +local VALS = table.create(16, 0) + +-- the 16 values of the EAC block at p into VALS (raster order): mode "alpha" (8-bit), "u11" / "s11" (11-bit) +local function eac(src: buffer, p: number, mode: string) + local base = readu8(src, p) + local b1 = readu8(src, p + 1) + local mult, tab = rshift(b1, 4), EAC[band(b1, 15) + 1] + -- 48 index bits, pixel 0 (column-major) first: bytes 2..7 big-endian, as 24 + 24 bits + local hiIdx = readu8(src, p + 2) * 65536 + readu8(src, p + 3) * 256 + readu8(src, p + 4) + local loIdx = readu8(src, p + 5) * 65536 + readu8(src, p + 6) * 256 + readu8(src, p + 7) + if mode == "s11" then + if base >= 128 then + base -= 256 + end + if base == -128 then + base = -127 + end + end + for i = 0, 15 do + local idx = if i < 8 then band(rshift(hiIdx, 21 - i * 3), 7) else band(rshift(loIdx, 21 - (i - 8) * 3), 7) + local m = tab[idx + 1] + local v + if mode == "alpha" then + v = clamp8(base + m * mult) + elseif mode == "u11" then + v = if mult == 0 then base * 8 + 4 + m else base * 8 + 4 + m * mult * 8 + v = if v < 0 then 0 elseif v > 2047 then 2047 else v + else + v = if mult == 0 then base * 8 + m else base * 8 + m * mult * 8 + v = if v < -1023 then -1023 elseif v > 1023 then 1023 else v + end + VALS[band(i, 3) * 4 + rshift(i, 2) + 1] = v + end +end + +-- 11-bit values -> 8 / 16 bits +local function to8(v: number, signed: boolean): number + if signed then + return math.floor(v * 127 / 1023 + 0.5) + 128 + end + return math.floor(v * 255 / 2047 + 0.5) +end +local function to16(v: number, signed: boolean): number + if signed then + return math.floor((v + 1023) * 65535 / 2046 + 0.5) + end + return v * 32 + rshift(v, 6) +end + +-- ------------------------------------------------------------------------------------------------ decode +-- size(kind, w, h) -> bytes of the block data | nil for an unknown kind +function Etc.size(kind: string, w: number, h: number): number? + local b = Etc.BLOCK[kind] + return if b then ((w + 3) // 4) * ((h + 3) // 4) * b else nil +end + +-- decode(kind, src, offset, w, h, out?, outOffset?, wide?) -> pixels | nil, message. rgba8, or rgba16 for the EAC +-- kinds when wide is true. +function Etc.decode(kind: string, src: buffer, offset: number, w: number, h: number, out: buffer?, outOffset: number?, wide: boolean?): (buffer?, string?) + local bytes = Etc.BLOCK[kind] + if not bytes then + return nil, "unsupported: unknown block format " .. tostring(kind) + end + if w < 1 or h < 1 or w % 1 ~= 0 or h % 1 ~= 0 then + return nil, "bad image size" + end + local need = Etc.size(kind, w, h) :: number + if offset < 0 or offset + need > buffer.len(src) then + return nil, "truncated: block data runs past the end" + end + local isEac = string.sub(kind, 1, 3) == "eac" + local w16 = isEac and wide == true + local bpp = if w16 then 8 else 4 + local oo = outOffset or 0 + local dst = out or buffer.create(w * h * bpp) + if oo + w * h * bpp > buffer.len(dst) then + return nil, "output buffer too small" + end + local signed = kind == "eac_r11s" or kind == "eac_rg11s" + local two = kind == "eac_rg11" or kind == "eac_rg11s" + local mode11 = if signed then "s11" else "u11" + local etc2 = kind ~= "etc1" + local punch = kind == "etc2_rgba1" + local blk = buffer.create(16 * bpp) + local bw, bh = (w + 3) // 4, (h + 3) // 4 + local p = offset + local rowBytes = w * bpp + for by = 0, bh - 1 do + local rows = math.min(4, h - by * 4) + for bx = 0, bw - 1 do + if isEac then + eac(src, p, mode11) + for i = 0, 15 do + local v = VALS[i + 1] + if w16 then + local q = to16(v, signed) + writeu16(blk, i * 8, q) + writeu16(blk, i * 8 + 2, if two then 0 else q) + writeu16(blk, i * 8 + 4, if two then 0 else q) + writeu16(blk, i * 8 + 6, 65535) + else + local q = to8(v, signed) + writeu32(blk, i * 4, if two then q + 0xFF000000 else q + q * 256 + q * 65536 + 0xFF000000) + end + end + if two then + eac(src, p + 8, mode11) + for i = 0, 15 do + local v = VALS[i + 1] + if w16 then + writeu16(blk, i * 8 + 2, to16(v, signed)) + else + writeu8(blk, i * 4 + 1, to8(v, signed)) + end + end + end + elseif kind == "etc2_rgba" then + colour(src, p + 8, blk, true, false) + eac(src, p, "alpha") + for i = 0, 15 do + writeu8(blk, i * 4 + 3, VALS[i + 1]) + end + else + colour(src, p, blk, etc2, punch) + end + p += bytes + local cols = math.min(4, w - bx * 4) + local base = oo + (by * 4) * rowBytes + bx * 4 * bpp + for y = 0, rows - 1 do + buffer.copy(dst, base + y * rowBytes, blk, y * 4 * bpp, cols * bpp) + end + end + end + return dst, nil +end + +return Etc diff --git a/tests/codecs/etc.luau b/tests/codecs/etc.luau new file mode 100644 index 0000000..6d5da15 --- /dev/null +++ b/tests/codecs/etc.luau @@ -0,0 +1,129 @@ +-- codecs/image/Etc tests: random ETC1 / ETC2 (every mode: individual, differential, T, H, planar; RGB A1 opaque and +-- transparent; RGBA with EAC alpha) and EAC R11 / RG11 (unsigned, signed, multiplier 0) blocks against Godot 4's +-- decode (etcpak); odd sizes; the 16-bit EAC output; errors; the fuzz no-throw pass. Fixtures: tools/fixtures/etc.py. +local T = require("@tests/T") +local Etc = require("@formats/codecs/image/Etc") +local Fx = require("@tests/gen/etc") + +local t = T.suite("etc") + +local function maxDiff(a: buffer, b: buffer, n: number, channels: { number }): (number, number) + local worst, at = 0, -1 + for i = 0, n - 1 do + for _, c in channels do + local d = math.abs(buffer.readu8(a, i * 4 + c) - buffer.readu8(b, i * 4 + c)) + if d > worst then + worst, at = d, i + end + end + end + return worst, at +end + +local worstEac = 0 +for _, c in Fx do + local src = buffer.fromstring(c.data) + local px, err = Etc.decode(c.kind, src, 0, c.w, c.h) + t:check(px, c.name .. ": " .. tostring(err)) + local n = c.w * c.h + t:eq(buffer.len(px), n * 4, c.name .. ": size") + local eacKind = string.sub(c.kind, 1, 3) == "eac" + if c.spec then + -- signed EAC against the spec rule (values -1023..1023), biased into 8 bits + local two = c.kind == "eac_rg11s" + local bad = 0 + for i = 0, n - 1 do + for ch = 0, if two then 1 else 0 do + local want = math.floor(c.spec[i * 2 + ch + 1] * 127 / 1023 + 0.5) + 128 + if buffer.readu8(px, i * 4 + ch) ~= want then + bad += 1 + end + end + end + t:eq(bad, 0, c.name .. ": against the spec values") + elseif c.kind == "etc2_rgba1" then + -- Godot decoded the blocks as ETC2 RGB: opaque blocks match it; transparent ones have index 2 transparent + -- black and indexes 1 / 3 unchanged (index 0 drops its modifier in the differential mode); planar is opaque + local g = buffer.fromstring(c.godot) + local bw = (c.w + 3) // 4 + local bad = 0 + for y = 0, c.h - 1 do + for x = 0, c.w - 1 do + local bo = ((y // 4) * bw + x // 4) * 8 + local hi = bit32.byteswap(buffer.readu32(src, bo)) + local lo = bit32.byteswap(buffer.readu32(src, bo + 4)) + local i = (x % 4) * 4 + y % 4 + local idx = bit32.band(bit32.rshift(lo, i + 15), 2) + bit32.band(bit32.rshift(lo, i), 1) + local opaque = bit32.band(hi, 2) ~= 0 + local function ch(v: number, at: number): number + v = bit32.band(bit32.rshift(hi, at), 31) + (function(d: number): number + return if d >= 4 then d - 8 else d + end)(bit32.band(bit32.rshift(hi, at - 3), 7)) + return v + end + local r2, g2, b2 = ch(0, 27), ch(0, 19), ch(0, 11) + local planar = r2 >= 0 and r2 <= 31 and g2 >= 0 and g2 <= 31 and (b2 < 0 or b2 > 31) + local diff = r2 >= 0 and r2 <= 31 and g2 >= 0 and g2 <= 31 and b2 >= 0 and b2 <= 31 + local o = (y * c.w + x) * 4 + local mine, ref = buffer.readu32(px, o), buffer.readu32(g, o) + if opaque or planar then + bad += if mine == ref then 0 else 1 + elseif idx == 2 then + bad += if mine == 0 then 0 else 1 + elseif idx ~= 0 or not diff then + bad += if mine == ref then 0 else 1 + end + end + end + t:eq(bad, 0, c.name .. ": punch-through rules over Godot's ETC2 RGB decode") + else + local g = buffer.fromstring(c.godot) + local chans = if c.kind == "eac_r11" then { 0 } elseif eacKind then { 0, 1 } else { 0, 1, 2, 3 } + local d, at = maxDiff(px, g, n, chans) + if eacKind then + -- Godot keeps EAC as 16-bit / half floats and converts to 8 bits with its own rounding + worstEac = math.max(worstEac, d) + t:check(d <= 1, string.format("%s: %d off Godot at pixel %d", c.name, d, at)) + else + t:check(d == 0, string.format("%s: %d off Godot at pixel %d", c.name, d, at)) + end + end + -- 16-bit EAC agrees with the 8-bit output + if eacKind then + local p16 = Etc.decode(c.kind, src, 0, c.w, c.h, nil, 0, true) :: buffer + t:eq(buffer.len(p16), n * 8, c.name .. ": rgba16 size") + local ok = true + for i = 0, n - 1 do + local v16 = buffer.readu16(p16, i * 8) + ok = ok and math.abs(math.floor(v16 / 257 + 0.5) - buffer.readu8(px, i * 4)) <= 1 + end + t:check(ok, c.name .. ": rgba16 ~ rgba8") + end + -- from / into offsets + local big = buffer.create(buffer.len(src) + 3) + buffer.copy(big, 3, src) + local out = buffer.create(n * 4 + 4) + Etc.decode(c.kind, big, 3, c.w, c.h, out, 4) + t:check(buffer.readstring(out, 4, n * 4) == buffer.tostring(px), c.name .. ": offsets") +end +t:note("%d block images match Godot (EAC within %d)", #Fx, worstEac) + +do + local _, e1 = Etc.decode("etc3", buffer.create(8), 0, 4, 4) + t:check(e1 and string.find(e1, "unsupported") ~= nil, "unknown kind") + local _, e2 = Etc.decode("etc2_rgba", buffer.create(8), 0, 4, 4) + t:check(e2 and string.find(e2, "truncated") ~= nil, "short data") + t:eq(Etc.size("etc2_rgba", 5, 5), 64, "size") +end + +local samples = {} +for _, c in Fx do + table.insert(samples, buffer.fromstring(c.data)) +end +for kind in Etc.BLOCK do + t:fuzz("decode " .. kind, function(b: buffer) + local n = buffer.len(b) // Etc.BLOCK[kind] + return Etc.decode(kind, b, 0, math.max(1, 4 * n - 2), 4) + end, samples, { mutations = 2 }) +end +t:done() diff --git a/tools/fixtures/etc.py b/tools/fixtures/etc.py new file mode 100644 index 0000000..64657d1 --- /dev/null +++ b/tools/fixtures/etc.py @@ -0,0 +1,128 @@ +# ETC fixtures (tools/fixtures.py): random ETC1 / ETC2 / EAC blocks, sorted by mode so that every ETC2 mode +# (individual, differential, T, H, planar; punch-through opaque and transparent) and every EAC multiplier case is +# covered, decoded by Godot 4 (etcpak's decoder, through Image.decompress). +# tests/gen/etc.luau { { name, kind, w, h, data, godot (rgba8 bytes) } } +import importlib.util, os, random + +_spec = importlib.util.spec_from_file_location("_tex_reference", os.path.join(os.path.dirname(os.path.abspath(__file__)), "_tex_reference.py")) +R = importlib.util.module_from_spec(_spec) +_spec.loader.exec_module(R) + + +def mode(b, punch): + """the ETC2 mode of an 8-byte colour block""" + hi = int.from_bytes(b[:4], "big") + if not punch and not (hi >> 1) & 1: + return "individual" + def s3(v): + return v - 8 if v >= 4 else v + r, g, bb = (hi >> 27) & 31, (hi >> 19) & 31, (hi >> 11) & 31 + dr, dg, db = s3((hi >> 24) & 7), s3((hi >> 16) & 7), s3((hi >> 8) & 7) + if not 0 <= r + dr <= 31: + return "T" + if not 0 <= g + dg <= 31: + return "H" + if not 0 <= bb + db <= 31: + return "planar" + return "differential" + + +def colour_blocks(rng, n, want, punch=False, opaque=None): + out = bytearray() + while len(out) < n * 8: + b = bytearray(rng.getrandbits(8) for _ in range(8)) + if opaque is not None: + b[3] = (b[3] & 0xFD) | (2 if opaque else 0) + if mode(b, punch) == want: + out += b + return bytes(out) + + +def eac_blocks(rng, n, zero_mult=False): + out = bytearray() + for _ in range(n): + b = bytearray(rng.getrandbits(8) for _ in range(8)) + if zero_mult: + b[1] &= 0x0F + out += b + return bytes(out) + + +EAC = [[-3, -6, -9, -15, 2, 5, 8, 14], [-3, -7, -10, -13, 2, 6, 9, 12], [-2, -5, -8, -13, 1, 4, 7, 12], + [-2, -4, -6, -13, 1, 3, 5, 12], [-3, -6, -8, -12, 2, 5, 7, 11], [-3, -7, -9, -11, 2, 6, 8, 10], + [-4, -7, -8, -11, 3, 6, 7, 10], [-3, -5, -8, -11, 2, 4, 7, 10], [-2, -6, -8, -10, 1, 5, 7, 9], + [-2, -5, -8, -10, 1, 4, 7, 9], [-2, -4, -8, -10, 1, 3, 7, 9], [-2, -5, -7, -10, 1, 4, 6, 9], + [-3, -4, -7, -10, 2, 3, 6, 9], [-1, -2, -3, -10, 0, 1, 2, 9], [-4, -6, -8, -9, 3, 5, 7, 8], + [-3, -5, -7, -9, 2, 4, 6, 8]] + + +def s11(c): + """signed EAC R11 / RG11 -> the 11-bit values (-1023..1023) per pixel and channel, as a flat list""" + two = c["kind"] == "eac_rg11s" + w, h, data = c["w"], c["h"], c["data"] + bw = (w + 3) // 4 + out = [0] * (w * h * 2) + step = 16 if two else 8 + for bi in range(len(data) // step): + bx, by = bi % bw, bi // bw + for ch in range(2 if two else 1): + b = data[bi * step + ch * 8: bi * step + ch * 8 + 8] + base = b[0] - 256 if b[0] >= 128 else b[0] + base = max(base, -127) + mult, tab = b[1] >> 4, EAC[b[1] & 15] + bits = int.from_bytes(b[2:], "big") + for i in range(16): + m = tab[(bits >> (45 - 3 * i)) & 7] + v = base * 8 + (m * mult * 8 if mult else m) + v = max(-1023, min(1023, v)) + x, y = bx * 4 + i // 4, by * 4 + i % 4 + if x < w and y < h: + out[(y * w + x) * 2 + ch] = v + return out + + +def generate(ctx): + rng = random.Random(11) + cases = [] + + def add(name, kind, w, h, data): + cases.append({"name": name, "kind": kind, "w": w, "h": h, "data": data}) + + add("etc1 individual", "etc1", 16, 16, colour_blocks(rng, 16, "individual")) + add("etc1 differential", "etc1", 16, 16, colour_blocks(rng, 16, "differential")) + add("etc1 random 13x6", "etc1", 13, 6, bytes(rng.getrandbits(8) for _ in range(8 * 8))) + for m in ("individual", "differential", "T", "H", "planar"): + add("etc2_rgb " + m, "etc2_rgb", 16, 16, colour_blocks(rng, 16, m)) + for m in ("differential", "T", "H", "planar"): + for op in (True, False): + add("etc2_rgba1 %s %s" % (m, "opaque" if op else "transparent"), "etc2_rgba1", 16, 16, + colour_blocks(rng, 16, m, True, op)) + for m in ("individual", "differential", "T", "H", "planar"): + rgba = bytearray() + col = colour_blocks(rng, 16, m) + alpha = eac_blocks(rng, 16, m == "planar") + for i in range(16): + rgba += alpha[i * 8:i * 8 + 8] + col[i * 8:i * 8 + 8] + add("etc2_rgba " + m, "etc2_rgba", 16, 16, bytes(rgba)) + for kind in ("eac_r11", "eac_r11s"): + add(kind, kind, 16, 16, eac_blocks(rng, 16)) + add(kind + " multiplier 0", kind, 8, 8, eac_blocks(rng, 4, True)) + for kind in ("eac_rg11", "eac_rg11s"): + add(kind, kind, 16, 12, eac_blocks(rng, 24)) + add("etc2_rgb 10x5", "etc2_rgb", 10, 5, bytes(rng.getrandbits(8) for _ in range(6 * 8))) + # Godot cannot decode RGB A1 or the signed EAC kinds: A1 blocks go through it as ETC2 RGB (the test checks the + # punch-through rules on top; the opaque bit is set for Godot, else ETC2 RGB reads the individual mode), signed EAC is checked against s11() below (the KDFS 1.3 rule, written here) + jobs, idx = [], [] + for i, c in enumerate(cases): + k = "etc2_rgb" if c["kind"] == "etc2_rgba1" else c["kind"] + if k in ("eac_r11s", "eac_rg11s"): + c["spec"] = s11(c) + else: + data = c["data"] + if c["kind"] == "etc2_rgba1": # with the opaque bit set: ETC2 RGB's differential / T / H / planar + data = bytes(v | 2 if i % 8 == 3 else v for i, v in enumerate(data)) + jobs.append((data, c["w"], c["h"], R.GODOT[k], False)) + idx.append(i) + for i, out in zip(idx, R.godot(ctx, jobs)): + cases[i]["godot"] = out + ctx.emit("etc", cases)