Files
rosetta/src/Formats/image/tga/Codec.luau
T

158 lines
4.7 KiB
Luau

--!strict
--!native
--!optimize 2
-- image/tga/Codec: the Truevision TGA pixel loops (native; never throws). Written from the Truevision TGA 2.0
-- specification. decode reads raw or RLE pixel data (packets may cross scanlines) into rgba8 at the oriented
-- position; encode writes raw or RLE rows. Every read is bounds-checked; the output has exactly w * h * 4 bytes.
local readu8, readu16, readu32, writeu8, writeu32 = buffer.readu8, buffer.readu16, buffer.readu32, buffer.writeu8, buffer.writeu32
local Codec = {}
-- one source pixel at b[p] -> rgba as a u32; kind: 0 grey 8, 1 grey + alpha 16, 2 = 15/16-bit ARGB1555,
-- 3 = BGR 24, 4 = BGRA 32, 5 = index 8, 6 = index 16 (pal: u32 entries, palN of them)
local function pixel(b: buffer, p: number, kind: number, alpha1: boolean, pal: buffer, palN: number): number
if kind == 0 then
return readu8(b, p) * 0x10101 + 0xFF000000
elseif kind == 1 then
return readu8(b, p) * 0x10101 + readu8(b, p + 1) * 0x1000000
elseif kind == 2 then
local v = readu16(b, p)
local r, g, bl = (v // 1024) % 32, (v // 32) % 32, v % 32
local a = if alpha1 and v < 32768 then 0 else 255
return (r * 255 + 15) // 31 + ((g * 255 + 15) // 31) * 256 + ((bl * 255 + 15) // 31) * 65536 + a * 16777216 -- round(v * 255 / 31), as ImageMagick
elseif kind == 3 then
return readu8(b, p + 2) + readu8(b, p + 1) * 256 + readu8(b, p) * 65536 + 0xFF000000
elseif kind == 4 then
return readu8(b, p + 2) + readu8(b, p + 1) * 256 + readu8(b, p) * 65536 + readu8(b, p + 3) * 16777216
end
local i = if kind == 5 then readu8(b, p) else readu16(b, p)
if i >= palN then
return 0xFF000000
end
return readu32(pal, i * 4)
end
Codec.pixel = pixel
-- decode(b, p, limit, w, h, bpp, kind, alpha1, rle, bottomUp, rtl, pal, palN, out) -> pixels written, truncated?
function Codec.decode(b: buffer, p: number, limit: number, w: number, h: number, bpp: number, kind: number, alpha1: boolean, rle: boolean, bottomUp: boolean, rtl: boolean, pal: buffer, palN: number, out: buffer): (number, boolean)
local n = w * h
local i = 0
local function put(v: number)
local row, col = i // w, i % w
if bottomUp then
row = h - 1 - row
end
if rtl then
col = w - 1 - col
end
writeu32(out, (row * w + col) * 4, v)
i += 1
end
if not rle then
local avail = math.min(n, (limit - p) // bpp)
for _ = 1, avail do
put(pixel(b, p, kind, alpha1, pal, palN))
p += bpp
end
return i, avail < n
end
while i < n do
if p >= limit then
return i, true
end
local hd = readu8(b, p)
p += 1
local count = hd % 128 + 1
if hd >= 128 then
if p + bpp > limit then
return i, true
end
local v = pixel(b, p, kind, alpha1, pal, palN)
p += bpp
for _ = 1, math.min(count, n - i) do
put(v)
end
else
for _ = 1, math.min(count, n - i) do
if p + bpp > limit then
return i, true
end
put(pixel(b, p, kind, alpha1, pal, palN))
p += bpp
end
end
end
return i, false
end
-- encode(px, w, h, srcBpp, outBpp, rle, topDown, out, o) -> new offset. px: rgba8 / rgb8 / gray8 (srcBpp 4 / 3 / 1);
-- written as BGRA / BGR / grey (outBpp 4 / 3 / 1), rows top-down or bottom-up; RLE packets stay inside a row.
-- out needs room for w * h * (outBpp + 1) bytes.
function Codec.encode(px: buffer, w: number, h: number, srcBpp: number, outBpp: number, rle: boolean, topDown: boolean, out: buffer, o: number): number
local function get(q: number): number
if srcBpp == 1 then
return readu8(px, q)
elseif srcBpp == 3 then
return readu8(px, q) + readu8(px, q + 1) * 256 + readu8(px, q + 2) * 65536 + 0xFF000000
end
return readu32(px, q)
end
local function emit(v: number)
if outBpp == 1 then
writeu8(out, o, v % 256)
else
writeu8(out, o, (v // 65536) % 256)
writeu8(out, o + 1, (v // 256) % 256)
writeu8(out, o + 2, v % 256)
if outBpp == 4 then
writeu8(out, o + 3, v // 16777216)
end
end
o += outBpp
end
for y = 0, h - 1 do
local row = if topDown then y else h - 1 - y
local base = row * w * srcBpp
if not rle then
for x = 0, w - 1 do
emit(get(base + x * srcBpp))
end
else
local x = 0
while x < w do
local v = get(base + x * srcBpp)
local run = 1
while x + run < w and run < 128 and get(base + (x + run) * srcBpp) == v do
run += 1
end
if run >= 2 then
writeu8(out, o, 127 + run)
o += 1
emit(v)
x += run
else
-- a raw packet up to the next run of 2 or more
local s = x
local cnt = 0
while x < w and cnt < 128 do
local c = get(base + x * srcBpp)
if x + 1 < w and get(base + (x + 1) * srcBpp) == c then
break
end
cnt += 1
x += 1
end
writeu8(out, o, cnt - 1)
o += 1
for k = s, s + cnt - 1 do
emit(get(base + k * srcBpp))
end
end
end
end
end
return o
end
return Codec