image formats: comments are specs only
This commit is contained in:
1 parent
0bd7b87d2b
commit
b5546e208e
40 files changed
+246
-626
No files matched your search
@@ -1,9 +1,7 @@
|
||||
--!strict
|
||||
--!native
|
||||
--!optimize 2
|
||||
-- image/png/Encode: the PNG encoder's pixel stage. Picks the smallest lossless colour type for rgba8 pixels and
|
||||
-- writes the filtered scanlines (a filter byte + the row), choosing each row's filter by the minimum sum of absolute
|
||||
-- signed residuals (the libpng heuristic). Nothing here throws: the caller (init) validated the image.
|
||||
-- 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
|
||||
|
||||
@@ -11,7 +9,6 @@ local Encode = {}
|
||||
|
||||
local CHANNELS = { [0] = 1, [2] = 3, [4] = 2, [6] = 4 }
|
||||
|
||||
-- pickColorType(px, w, h) -> "rgba" | "rgb" | "graya" | "gray"
|
||||
function Encode.pickColorType(px: buffer, w: number, h: number): string
|
||||
local opaque, gray = true, true
|
||||
for i = 0, w * h - 1 do
|
||||
@@ -33,7 +30,6 @@ function Encode.pickColorType(px: buffer, w: number, h: number): string
|
||||
return "rgb"
|
||||
end
|
||||
|
||||
-- convert rgba8 to the colour type's packed rows (no filter bytes)
|
||||
local function pack(px: buffer, w: number, h: number, ct: number): buffer
|
||||
local ch = CHANNELS[ct]
|
||||
if ch == 4 then
|
||||
@@ -70,8 +66,6 @@ local function paeth(a: number, b: number, c: number): number
|
||||
return c
|
||||
end
|
||||
|
||||
-- the five filter costs (sums of |signed residual|) of the row at s (rb bytes; previous row at s - rb unless first);
|
||||
-- the arithmetic is inlined: this runs for every byte
|
||||
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
|
||||
@@ -101,7 +95,6 @@ local function costs(src: buffer, s: number, rb: number, bpp: number, first: boo
|
||||
return c0, c1, c2, c3, c4
|
||||
end
|
||||
|
||||
-- write the row at s filtered with f to out[o + 1 ..] (out[o] = f)
|
||||
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
|
||||
@@ -130,8 +123,6 @@ local function apply(src: buffer, s: number, rb: number, bpp: number, first: boo
|
||||
end
|
||||
end
|
||||
|
||||
-- filter(px, w, h, ct) -> the filtered scanlines (h * (1 + rowBytes)); each row's filter: the minimum sum of
|
||||
-- |signed residuals| (the libpng heuristic)
|
||||
function Encode.filter(px: buffer, w: number, h: number, ct: number): buffer
|
||||
local ch = CHANNELS[ct]
|
||||
local src = pack(px, w, h, ct)
|
||||
|
||||
Reference in new issue
Block a user