--!strict --!native --!optimize 2 -- image/png/Encode: PNG colour type choice and filtering (per-row minimum sum of |signed residuals|, as libpng). local readu8, writeu8 = buffer.readu8, buffer.writeu8 local bcreate, bcopy = buffer.create, buffer.copy local Encode = {} local CHANNELS = { [0] = 1, [2] = 3, [4] = 2, [6] = 4 } function Encode.pickColorType(px: buffer, w: number, h: number): string local opaque, gray = true, true for i = 0, w * h - 1 do local o = i * 4 local r, g, b, a = readu8(px, o), readu8(px, o + 1), readu8(px, o + 2), readu8(px, o + 3) if a ~= 255 then opaque = false end if r ~= g or g ~= b then gray = false end if not opaque and not gray then return "rgba" end end if gray then return if opaque then "gray" else "graya" end return "rgb" end local function pack(px: buffer, w: number, h: number, ct: number): buffer local ch = CHANNELS[ct] if ch == 4 then return px end local out = bcreate(w * h * ch) local n = w * h if ct == 2 then for i = 0, n - 1 do bcopy(out, i * 3, px, i * 4, 3) end elseif ct == 0 then for i = 0, n - 1 do writeu8(out, i, readu8(px, i * 4)) end else -- graya for i = 0, n - 1 do writeu8(out, i * 2, readu8(px, i * 4)) writeu8(out, i * 2 + 1, readu8(px, i * 4 + 3)) end end return out end local function paeth(a: number, b: number, c: number): number local pa = math.abs(b - c) local pb = math.abs(a - c) local pc = math.abs(a + b - c - c) if pa <= pb and pa <= pc then return a elseif pb <= pc then return b end return c end local function costs(src: buffer, s: number, rb: number, bpp: number, first: boolean): (number, number, number, number, number) local c0, c1, c2, c3, c4 = 0, 0, 0, 0, 0 local p = s - rb for i = 0, rb - 1 do local x = readu8(src, s + i) local a = if i >= bpp then readu8(src, s + i - bpp) else 0 local b, c = 0, 0 if not first then b = readu8(src, p + i) if i >= bpp then c = readu8(src, p + i - bpp) end end local d = if x < 128 then x else 256 - x c0 += d d = (x - a) % 256 c1 += if d < 128 then d else 256 - d d = (x - b) % 256 c2 += if d < 128 then d else 256 - d d = (x - (a + b) // 2) % 256 c3 += if d < 128 then d else 256 - d local pa, pb, pc = math.abs(b - c), math.abs(a - c), math.abs(a + b - c - c) local pr = if pa <= pb and pa <= pc then a elseif pb <= pc then b else c d = (x - pr) % 256 c4 += if d < 128 then d else 256 - d end return c0, c1, c2, c3, c4 end local function apply(src: buffer, s: number, rb: number, bpp: number, first: boolean, f: number, out: buffer, o: number) writeu8(out, o, f) o += 1 if f == 0 or (f == 2 and first) then bcopy(out, o, src, s, rb) return end local p = s - rb for i = 0, rb - 1 do local x = readu8(src, s + i) local a = if i >= bpp then readu8(src, s + i - bpp) else 0 local pred if f == 1 then pred = a elseif f == 2 then pred = readu8(src, p + i) elseif f == 3 then pred = (a + (if first then 0 else readu8(src, p + i))) // 2 elseif first then pred = a else local c = if i >= bpp then readu8(src, p + i - bpp) else 0 pred = paeth(a, readu8(src, p + i), c) end writeu8(out, o + i, x - pred) end end function Encode.filter(px: buffer, w: number, h: number, ct: number): buffer local ch = CHANNELS[ct] local src = pack(px, w, h, ct) local rb = w * ch local out = bcreate(h * (rb + 1)) for y = 0, h - 1 do local s = y * rb local first = y == 0 local c0, c1, c2, c3, c4 = costs(src, s, rb, ch, first) local best, bc = 0, c0 if c1 < bc then best, bc = 1, c1 end if c2 < bc then best, bc = 2, c2 end if c3 < bc then best, bc = 3, c3 end if c4 < bc then best = 4 end apply(src, s, rb, ch, first, best, out, y * (rb + 1)) end return out end return Encode