-- 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()