ws-cleanup-types: image/pnm, model/*: type lookup tables, table.create results and literals; casts for known values
This commit is contained in:
1 parent
7c15b02354
commit
139441a37b
23 files changed
+139
-137
No files matched your search
@@ -123,7 +123,7 @@ function Codec.pack(s: buffer, n: number, ch: number, maxval: number, invert: bo
|
||||
r, g, b, a = get(q), get(q + 1), get(q + 2), get(q + 3)
|
||||
end
|
||||
if fmt == 0 then
|
||||
writeu32(dst, i * 4, r + g * 256 + b * 65536 + a * 16777216)
|
||||
writeu32(dst, i * 4, r + g * 256 + b * 65536 + (a :: number) * 16777216)
|
||||
elseif fmt == 1 then
|
||||
writeu8(dst, i * 3, r)
|
||||
writeu8(dst, i * 3 + 1, g)
|
||||
@@ -132,12 +132,12 @@ function Codec.pack(s: buffer, n: number, ch: number, maxval: number, invert: bo
|
||||
writeu8(dst, i, r)
|
||||
elseif fmt == 3 then
|
||||
writeu8(dst, i * 2, r)
|
||||
writeu8(dst, i * 2 + 1, a)
|
||||
writeu8(dst, i * 2 + 1, a :: number)
|
||||
elseif fmt == 4 then
|
||||
writeu16(dst, i * 8, r)
|
||||
writeu16(dst, i * 8 + 2, g)
|
||||
writeu16(dst, i * 8 + 4, b)
|
||||
writeu16(dst, i * 8 + 6, a)
|
||||
writeu16(dst, i * 8 + 6, a :: number)
|
||||
elseif fmt == 5 then
|
||||
writeu16(dst, i * 2, r)
|
||||
end
|
||||
@@ -217,9 +217,9 @@ function Codec.extract(px: buffer, fmt: number, n: number, ch: number, wide: boo
|
||||
for c = 0, vals - 1 do
|
||||
local v
|
||||
if ch <= 2 then
|
||||
v = if c == 0 then gray else a
|
||||
v = if c == 0 then gray else a :: number
|
||||
else
|
||||
v = if c == 0 then r elseif c == 1 then g elseif c == 2 then b else a
|
||||
v = if c == 0 then r elseif c == 1 then g elseif c == 2 then b else a :: number
|
||||
end
|
||||
if wide and not w16 then
|
||||
v *= 257
|
||||
|
||||
@@ -38,8 +38,8 @@ export type EncodeOptions = {
|
||||
}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ constants
|
||||
local FMT_CODE = { rgba8 = 0, rgb8 = 1, gray8 = 2, graya8 = 3, rgba16 = 4, gray16 = 5, rgbaf32 = 6 }
|
||||
local BPP = { rgba8 = 4, rgb8 = 3, gray8 = 1, graya8 = 2, rgba16 = 8, gray16 = 2, rgbaf32 = 16 }
|
||||
local FMT_CODE: { [string]: number } = { rgba8 = 0, rgb8 = 1, gray8 = 2, graya8 = 3, rgba16 = 4, gray16 = 5, rgbaf32 = 6 }
|
||||
local BPP: { [string]: number } = { rgba8 = 4, rgb8 = 3, gray8 = 1, graya8 = 2, rgba16 = 8, gray16 = 2, rgbaf32 = 16 }
|
||||
local readu8 = buffer.readu8
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ sniff
|
||||
@@ -106,7 +106,7 @@ function Pnm.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Image?, Err.E
|
||||
end
|
||||
local kind = string.char(readu8(b, r.base + 1))
|
||||
local p = r.base + 2
|
||||
local w, h, maxval, ch, mode
|
||||
local w: number?, h: number?, maxval: number?, ch: number, mode: number
|
||||
local bits = false -- PBM: 1 = black
|
||||
local bw = false -- PAM BLACKANDWHITE: 1 = white
|
||||
local tupl: string? = nil
|
||||
@@ -137,9 +137,9 @@ function Pnm.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Image?, Err.E
|
||||
end
|
||||
ch = if kind == "3" or kind == "6" or kind == "F" then 3 else 1
|
||||
bits = kind == "1" or kind == "4"
|
||||
mode = if kind == "1" then 4 elseif kind == "2" or kind == "3" then 3 elseif kind == "4" then 0 elseif maxval > 255 then 2 else 1
|
||||
mode = if kind == "1" then 4 elseif kind == "2" or kind == "3" then 3 elseif kind == "4" then 0 elseif (maxval :: number) > 255 then 2 else 1
|
||||
elseif kind == "7" then
|
||||
local depth
|
||||
local depth: number?
|
||||
while true do
|
||||
local key
|
||||
key, p = token(b, p, limit)
|
||||
@@ -175,15 +175,16 @@ function Pnm.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Image?, Err.E
|
||||
if w == nil or h == nil or depth == nil or maxval == nil then
|
||||
return nil, Err.new(Err.CORRUPT, "PAM header misses WIDTH / HEIGHT / DEPTH / MAXVAL", p)
|
||||
end
|
||||
if depth < 1 or depth > 4 then
|
||||
return nil, Err.new(Err.UNSUPPORTED, "PAM depth " .. depth)
|
||||
if (depth :: number) < 1 or (depth :: number) > 4 then
|
||||
return nil, Err.new(Err.UNSUPPORTED, "PAM depth " .. (depth :: number))
|
||||
end
|
||||
ch = depth
|
||||
bw = tupl ~= nil and string.sub(tupl, 1, 13) == "BLACKANDWHITE"
|
||||
mode = if maxval > 255 then 2 else 1
|
||||
ch = depth :: number
|
||||
bw = tupl ~= nil and string.sub(tupl :: string, 1, 13) == "BLACKANDWHITE"
|
||||
mode = if (maxval :: number) > 255 then 2 else 1
|
||||
else
|
||||
return nil, Err.new(Err.CORRUPT, "not a Netpbm file", r.base)
|
||||
end
|
||||
local w, h, maxval = w :: number, h :: number, maxval :: number -- each branch above returns when one is nil
|
||||
if w < 1 or h < 1 or maxval < 1 or maxval > 65535 then
|
||||
return nil, Err.new(Err.CORRUPT, string.format("bad size %d x %d or maxval %d", w, h, maxval))
|
||||
end
|
||||
@@ -221,7 +222,7 @@ function Pnm.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Image?, Err.E
|
||||
if not ok then
|
||||
return nil, Err.new(if e == "truncated" then Err.TRUNCATED else Err.CORRUPT, tostring(e))
|
||||
end
|
||||
local fmt = "rgba8"
|
||||
local fmt: Types.PixelFormat = "rgba8"
|
||||
if native then
|
||||
local wide = maxval > 255
|
||||
if bits or bw or ch == 1 then
|
||||
@@ -260,7 +261,7 @@ function Pnm.encode(img: Types.Image, opts: EncodeOptions?): (buffer?, Err.Err?)
|
||||
if fc == nil then
|
||||
return nil, Err.new(Err.ARG, "unsupported pixel format " .. tostring(img.format))
|
||||
end
|
||||
local w, h = img.width, img.height
|
||||
local w: number, h: number = img.width, img.height
|
||||
if type(w) ~= "number" or type(h) ~= "number" or w < 1 or h < 1 or w % 1 ~= 0 or h % 1 ~= 0 or buffer.len(img.pixels) < w * h * BPP[img.format] then
|
||||
return nil, Err.new(Err.ARG, "bad image size or pixel buffer")
|
||||
end
|
||||
@@ -301,7 +302,7 @@ function Pnm.encode(img: Types.Image, opts: EncodeOptions?): (buffer?, Err.Err?)
|
||||
end
|
||||
local ascii = o.ascii == true
|
||||
local wideSrc = img.format == "gray16" or img.format == "rgba16" or img.format == "rgbaf32"
|
||||
local maxval = o.maxval or (if wideSrc then 65535 else 255)
|
||||
local maxval: number = o.maxval or (if wideSrc then 65535 else 255)
|
||||
if type(maxval) ~= "number" or maxval < 1 or maxval > 65535 or maxval % 1 ~= 0 then
|
||||
return nil, Err.new(Err.ARG, "maxval must be 1..65535")
|
||||
end
|
||||
|
||||
Reference in new issue
Block a user