--!strict --!native --!optimize 2 -- image/png/Encode: PNG colour type choice and filtering (per-row minimum sum of |signed residuals|, as libpng). local Decode = require("@rosetta/image/png/Decode") local readu8, writeu8, readi16 = buffer.readu8, buffer.writeu8, buffer.readi16 local abs = math.abs local paethFor = Decode.paethFor 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 -- |residual as a signed byte| by residual + 256 (residual in -255 .. 255) local SABS = table.create(511, 0) for d = -255, 255 do local m = d % 256 SABS[d + 256] = if m < 128 then m else 256 - m end local function costs(src: buffer, s: number, rb: number, bpp: number, first: boolean, pt: buffer?): (number, number, number, number, number) local c0, c1, c2, c3, c4 = 0, 0, 0, 0, 0 local p = s - rb local sabs = SABS if first or rb <= bpp or not pt then 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 c0 += sabs[x + 256] c1 += sabs[x - a + 256] c2 += sabs[x - b + 256] c3 += sabs[x - (a + b) // 2 + 256] local pa, pb, pc = abs(b - c), abs(a - c), abs(a + b - c - c) c4 += sabs[x - (if pa <= pb and pa <= pc then a elseif pb <= pc then b else c) + 256] end return c0, c1, c2, c3, c4 end for i = 0, bpp - 1 do local x, b = readu8(src, s + i), readu8(src, p + i) c0 += sabs[x + 256] c1 += sabs[x + 256] c2 += sabs[x - b + 256] c3 += sabs[x - b // 2 + 256] c4 += sabs[x - b + 256] end for i = s + bpp, s + rb - 1 do local x, a = readu8(src, i), readu8(src, i - bpp) local b, c = readu8(src, i - rb), readu8(src, i - rb - bpp) c0 += sabs[x + 256] c1 += sabs[x - a + 256] c2 += sabs[x - b + 256] c3 += sabs[x - (a + b) // 2 + 256] c4 += sabs[x - c - readi16(pt, (a * 511 + b - c * 512) * 2 + 261120) + 256] 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, pt: buffer?) 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 if not first and rb > bpp and f ~= 3 and pt then for i = 0, bpp - 1 do writeu8(out, o + i, readu8(src, s + i) - (if f == 1 then 0 else readu8(src, p + i))) end local d = o - s if f == 1 then for i = s + bpp, s + rb - 1 do writeu8(out, i + d, readu8(src, i) - readu8(src, i - bpp)) end elseif f == 2 then for i = s + bpp, s + rb - 1 do writeu8(out, i + d, readu8(src, i) - readu8(src, i - rb)) end else for i = s + bpp, s + rb - 1 do local a, b, c = readu8(src, i - bpp), readu8(src, i - rb), readu8(src, i - rb - bpp) writeu8(out, i + d, readu8(src, i) - c - readi16(pt, (a * 511 + b - c * 512) * 2 + 261120)) end end return end 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)) local pt = paethFor(h * rb) 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, pt) 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), pt) end return out end return Encode