ws-tex: image/sgi + image/sunras (read + write)
SGI: verbatim / RLE, 1-2 bytes per channel, 1-4 channels (rgba16 native); write RGB(A) RLE / verbatim. Sun Raster: 1 / 8 / 24 / 32-bit, standard / byte-encoded / RGB types, colour maps; write 24 / 32-bit. References: ImageMagick, netpbm (sgitopnm-era writers, rasttopnm), Pillow.
This commit is contained in:
1 parent
d294c211f9
commit
403a4d6f31
10 files changed
+745
No files matched your search
@@ -158,6 +158,30 @@ local list: { Entry } = {
|
||||
return require("@formats/image/qoi")
|
||||
end,
|
||||
},
|
||||
{
|
||||
name = "sgi",
|
||||
category = "image",
|
||||
path = "image/sgi",
|
||||
extensions = { "sgi", "rgb", "rgba", "bw", "int", "inta" },
|
||||
mime = { "image/sgi", "image/x-rgb" },
|
||||
deps = { "core/Err", "core/Bytes", "core/Sniff", "Types" },
|
||||
caps = { decode = true, encode = true, alpha = true, depths = { 8, 16 } },
|
||||
load = function()
|
||||
return require("@formats/image/sgi")
|
||||
end,
|
||||
},
|
||||
{
|
||||
name = "sunras",
|
||||
category = "image",
|
||||
path = "image/sunras",
|
||||
extensions = { "ras", "sun", "rs", "im1", "im8", "im24", "im32" },
|
||||
mime = { "image/x-sun-raster", "image/x-cmu-raster" },
|
||||
deps = { "core/Err", "core/Bytes", "core/Sniff", "Types", "codecs/image/Pixels" },
|
||||
caps = { decode = true, encode = true, alpha = true, palette = true, depths = { 1, 8, 24, 32 } },
|
||||
load = function()
|
||||
return require("@formats/image/sunras")
|
||||
end,
|
||||
},
|
||||
{
|
||||
name = "tga",
|
||||
category = "image",
|
||||
|
||||
@@ -0,0 +1,286 @@
|
||||
--!strict
|
||||
-- image/sgi: Silicon Graphics image files (.rgb / .sgi / .bw / .rgba; Paul Haeberli's "The SGI Image File Format"
|
||||
-- 1.00; new code). Decode: verbatim and RLE storage, 1 and 2 bytes per channel, 1 (grey), 2 (grey + alpha), 3 (RGB)
|
||||
-- and 4 (RGBA) channels, dimension 1 / 2 / 3, rows bottom-up; rgba8 (2-byte channels keep their high byte), or rgba16
|
||||
-- with opts.native for 2-byte files; the image name (meta.text.name). Encode: rgba8 as RGB (or RGBA when there is
|
||||
-- alpha), RLE (default) or verbatim; rgba16 as 2-byte channels. Not supported: the obsolete dithered / screen /
|
||||
-- colour-map files (colormap field 1..3: UNSUPPORTED).
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ requires
|
||||
local Err = require("@formats/core/Err")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Sniff = require("@formats/core/Sniff")
|
||||
local Types = require("@formats/Types")
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ contract
|
||||
local Sgi = {
|
||||
name = "sgi",
|
||||
category = "image",
|
||||
extensions = { "sgi", "rgb", "rgba", "bw", "int", "inta" },
|
||||
mime = { "image/sgi", "image/x-rgb" },
|
||||
deps = { "core/Err", "core/Bytes", "core/Sniff", "Types" },
|
||||
caps = { decode = true, encode = true, alpha = true, depths = { 8, 16 } },
|
||||
}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ types
|
||||
export type DecodeOptions = {
|
||||
native: boolean?, -- rgba16 for 2-byte files (default false: rgba8)
|
||||
maxPixels: number?, -- refuse larger images (default 2^26)
|
||||
}
|
||||
export type EncodeOptions = {
|
||||
rle: boolean?, -- RLE storage (default true)
|
||||
name: string?, -- the image name (up to 79 bytes; default meta.text.name)
|
||||
}
|
||||
|
||||
local readu8, readu16, readu32 = buffer.readu8, buffer.readu16, buffer.readu32
|
||||
local function be16(b: buffer, o: number): number
|
||||
return bit32.rshift(bit32.byteswap(readu16(b, o)), 16)
|
||||
end
|
||||
local function be32(b: buffer, o: number): number
|
||||
return bit32.byteswap(readu32(b, o))
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ sniff
|
||||
function Sgi.sniff(r: Bytes.Reader): number
|
||||
if Sniff.magic(r, "\1\218") == 0 or r:size() < 512 then
|
||||
return 0
|
||||
end
|
||||
local st, bpc = r:peekU8(2), r:peekU8(3)
|
||||
return if st <= 1 and (bpc == 1 or bpc == 2) then 1 else 0
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ decode
|
||||
function Sgi.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Image?, Err.Err?)
|
||||
local b, o, limit = r.buf, r.base, r.limit
|
||||
if limit - o < 512 then
|
||||
return nil, Err.new(Err.TRUNCATED, "short header", o)
|
||||
end
|
||||
if be16(b, o) ~= 474 then
|
||||
return nil, Err.new(Err.CORRUPT, "not an SGI image", o)
|
||||
end
|
||||
local rle, bpc, dim = readu8(b, o + 2), readu8(b, o + 3), be16(b, o + 4)
|
||||
local w, h, z = be16(b, o + 6), be16(b, o + 8), be16(b, o + 10)
|
||||
if dim == 1 then
|
||||
h, z = 1, 1
|
||||
elseif dim == 2 then
|
||||
z = 1
|
||||
end
|
||||
if rle > 1 or (bpc ~= 1 and bpc ~= 2) or dim < 1 or dim > 3 or w == 0 or h == 0 or z == 0 then
|
||||
return nil, Err.new(Err.CORRUPT, "bad header", o + 2)
|
||||
end
|
||||
if z > 4 then
|
||||
return nil, Err.new(Err.UNSUPPORTED, z .. " channels", o + 10)
|
||||
end
|
||||
local cmap = be32(b, o + 104)
|
||||
if cmap ~= 0 then
|
||||
return nil, Err.new(Err.UNSUPPORTED, "colormap type " .. cmap, o + 104)
|
||||
end
|
||||
local maxPixels = opts and opts.maxPixels or 2 ^ 26
|
||||
if w * h > maxPixels then
|
||||
return nil, Err.new(Err.LIMIT, string.format("%d x %d is over the %d pixel limit", w, h, maxPixels))
|
||||
end
|
||||
-- channel planes as 16-bit samples (8-bit values scaled * 257)
|
||||
local plane = buffer.create(w * h * z * 2)
|
||||
local truncated = false
|
||||
local function put(c: number, row: number, x: number, v: number)
|
||||
buffer.writeu16(plane, ((c * h + row) * w + x) * 2, if bpc == 1 then v * 257 else v)
|
||||
end
|
||||
if rle == 0 then
|
||||
local need = w * h * z * bpc
|
||||
local p = o + 512
|
||||
local avail = math.min(need, limit - p)
|
||||
truncated = avail < need
|
||||
for i = 0, avail // bpc - 1 do
|
||||
local v = if bpc == 1 then readu8(b, p + i) else be16(b, p + i * 2)
|
||||
buffer.writeu16(plane, i * 2, if bpc == 1 then v * 257 else v)
|
||||
end
|
||||
else
|
||||
local n = h * z
|
||||
if o + 512 + n * 8 > limit then
|
||||
return nil, Err.new(Err.TRUNCATED, "RLE tables run past the end", o + 512)
|
||||
end
|
||||
for c = 0, z - 1 do
|
||||
for row = 0, h - 1 do
|
||||
local i = c * h + row
|
||||
local start, len = be32(b, o + 512 + i * 4), be32(b, o + 512 + n * 4 + i * 4)
|
||||
local p = o + start
|
||||
local stop = math.min(limit, p + len)
|
||||
local x = 0
|
||||
while x < w and p + bpc <= stop do
|
||||
local cnt = if bpc == 1 then readu8(b, p) else be16(b, p)
|
||||
p += bpc
|
||||
local count = bit32.band(cnt, 0x7F)
|
||||
if count == 0 then
|
||||
break
|
||||
end
|
||||
if bit32.band(cnt, 0x80) ~= 0 then
|
||||
for _ = 1, count do
|
||||
if x >= w or p + bpc > stop then
|
||||
break
|
||||
end
|
||||
put(c, row, x, if bpc == 1 then readu8(b, p) else be16(b, p))
|
||||
p += bpc
|
||||
x += 1
|
||||
end
|
||||
else
|
||||
if p + bpc > stop then
|
||||
break
|
||||
end
|
||||
local v = if bpc == 1 then readu8(b, p) else be16(b, p)
|
||||
p += bpc
|
||||
for _ = 1, count do
|
||||
if x >= w then
|
||||
break
|
||||
end
|
||||
put(c, row, x, v)
|
||||
x += 1
|
||||
end
|
||||
end
|
||||
end
|
||||
if x < w then
|
||||
truncated = true
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
local wide = opts and opts.native and bpc == 2
|
||||
local img = Types.Image.new(w, h, if wide then "rgba16" else "rgba8")
|
||||
local px = img.pixels
|
||||
for y = 0, h - 1 do
|
||||
local row = h - 1 - y
|
||||
for x = 0, w - 1 do
|
||||
local function s(c: number): number
|
||||
return buffer.readu16(plane, ((c * h + row) * w + x) * 2)
|
||||
end
|
||||
local rr: number = s(0)
|
||||
local gg: number = if z >= 3 then s(1) else rr
|
||||
local bb: number = if z >= 3 then s(2) else rr
|
||||
local aa: number = if z == 2 then s(1) elseif z == 4 then s(3) else 65535
|
||||
local d = (y * w + x)
|
||||
if wide then
|
||||
buffer.writeu16(px, d * 8, rr)
|
||||
buffer.writeu16(px, d * 8 + 2, gg)
|
||||
buffer.writeu16(px, d * 8 + 4, bb)
|
||||
buffer.writeu16(px, d * 8 + 6, aa)
|
||||
else
|
||||
buffer.writeu32(px, d * 4, bit32.rshift(rr, 8) + bit32.rshift(gg, 8) * 256 + bit32.rshift(bb, 8) * 65536 + bit32.rshift(aa, 8) * 16777216)
|
||||
end
|
||||
end
|
||||
end
|
||||
local name = buffer.readstring(b, o + 24, 80)
|
||||
local zpos = string.find(name, "\0", 1, true)
|
||||
name = if zpos then string.sub(name, 1, zpos - 1) else name
|
||||
if name ~= "" then
|
||||
img.meta.text = { name = name }
|
||||
end
|
||||
if truncated then
|
||||
img.meta.truncated = true
|
||||
end
|
||||
img.meta.source = { format = "sgi", bitDepth = bpc * 8, channels = z, rle = rle == 1, dimension = dim }
|
||||
return img, nil
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ encode
|
||||
function Sgi.encode(img: Types.Image, opts: EncodeOptions?): (buffer?, Err.Err?)
|
||||
if type(img) ~= "table" or (img.format ~= "rgba8" and img.format ~= "rgba16") or type(img.pixels) ~= "buffer" then
|
||||
return nil, Err.new(Err.ARG, "needs an rgba8 or rgba16 Image")
|
||||
end
|
||||
local w, h = img.width, img.height
|
||||
if w < 1 or h < 1 or w > 65535 or h > 65535 then
|
||||
return nil, Err.new(Err.ARG, "size must be 1..65535")
|
||||
end
|
||||
local wide = img.format == "rgba16"
|
||||
local bpc = if wide then 2 else 1
|
||||
local px = img.pixels
|
||||
local function sample(x: number, y: number, c: number): number
|
||||
local i = (y * w + x) * 4 + c
|
||||
return if wide then buffer.readu16(px, i * 2) else readu8(px, i)
|
||||
end
|
||||
local z = 3
|
||||
for i = 0, w * h - 1 do
|
||||
if (if wide then buffer.readu16(px, i * 8 + 6) < 65535 else readu8(px, i * 4 + 3) < 255) then
|
||||
z = 4
|
||||
break
|
||||
end
|
||||
end
|
||||
local rle = not (opts and opts.rle == false)
|
||||
local out = Bytes.writer(512 + w * h * z * bpc + h * z * 8)
|
||||
out:u16be(474)
|
||||
out:u8(if rle then 1 else 0)
|
||||
out:u8(bpc)
|
||||
out:u16be(3)
|
||||
out:u16be(w)
|
||||
out:u16be(h)
|
||||
out:u16be(z)
|
||||
out:u32be(0)
|
||||
out:u32be(if wide then 65535 else 255)
|
||||
out:u32be(0)
|
||||
local name = (opts and opts.name) or (img.meta and img.meta.text and img.meta.text.name) or ""
|
||||
name = string.sub(name, 1, 79)
|
||||
out:string(name)
|
||||
out:fill(0, 80 - #name)
|
||||
out:u32be(0)
|
||||
out:fill(0, 404)
|
||||
local function put(v: number)
|
||||
if wide then
|
||||
out:u16be(v)
|
||||
else
|
||||
out:u8(v)
|
||||
end
|
||||
end
|
||||
if not rle then
|
||||
for c = 0, z - 1 do
|
||||
for row = 0, h - 1 do
|
||||
local y = h - 1 - row
|
||||
for x = 0, w - 1 do
|
||||
put(sample(x, y, c))
|
||||
end
|
||||
end
|
||||
end
|
||||
return out:finish(), nil
|
||||
end
|
||||
local tables = out:tell()
|
||||
out:fill(0, h * z * 8)
|
||||
for c = 0, z - 1 do
|
||||
for row = 0, h - 1 do
|
||||
local y = h - 1 - row
|
||||
local start = out:tell()
|
||||
local x = 0
|
||||
while x < w do
|
||||
-- a run of equal samples (>= 3) or literals up to the next run
|
||||
local v = sample(x, y, c)
|
||||
local run = 1
|
||||
while x + run < w and run < 127 and sample(x + run, y, c) == v do
|
||||
run += 1
|
||||
end
|
||||
if run >= 3 then
|
||||
put(run)
|
||||
put(v)
|
||||
x += run
|
||||
else
|
||||
local lit = 0
|
||||
while x + lit < w and lit < 127 do
|
||||
local a = sample(x + lit, y, c)
|
||||
if x + lit + 2 < w and sample(x + lit + 1, y, c) == a and sample(x + lit + 2, y, c) == a then
|
||||
break
|
||||
end
|
||||
lit += 1
|
||||
end
|
||||
lit = math.max(lit, 1)
|
||||
put(0x80 + lit)
|
||||
for k = 0, lit - 1 do
|
||||
put(sample(x + k, y, c))
|
||||
end
|
||||
x += lit
|
||||
end
|
||||
end
|
||||
put(0)
|
||||
local i = c * h + row
|
||||
out:setU32be(tables + i * 4, start)
|
||||
out:setU32be(tables + h * z * 4 + i * 4, out:tell() - start)
|
||||
end
|
||||
end
|
||||
return out:finish(), nil
|
||||
end
|
||||
|
||||
return Sgi
|
||||
@@ -0,0 +1,247 @@
|
||||
--!strict
|
||||
-- image/sunras: Sun Raster (rasterfile(5), SunOS <rasterfile.h>; new code). Decode: 1-bit (0 = white without a colour
|
||||
-- map), 8-bit (grey or the RGB colour map), 24-bit and 32-bit pixels in BGR / XBGR order (RT_STANDARD, RT_OLD,
|
||||
-- RT_BYTE_ENCODED) or RGB / XRGB order (RT_FORMAT_RGB), the byte-encoded RLE (0x80 escapes), 16-bit row padding; in
|
||||
-- 32-bit files the X byte is alpha when it is not all zero. Encode: rgba8 as 24-bit (opaque) or 32-bit (alpha in the
|
||||
-- X byte) BGR, RT_BYTE_ENCODED (default) or RT_STANDARD. Not supported: RT_FORMAT_TIFF / RT_FORMAT_IFF (UNSUPPORTED),
|
||||
-- raw colour maps (RMT_RAW is ignored).
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ requires
|
||||
local Err = require("@formats/core/Err")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Sniff = require("@formats/core/Sniff")
|
||||
local Types = require("@formats/Types")
|
||||
local Pixels = require("@formats/codecs/image/Pixels")
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ contract
|
||||
local Sunras = {
|
||||
name = "sunras",
|
||||
category = "image",
|
||||
extensions = { "ras", "sun", "rs", "im1", "im8", "im24", "im32" },
|
||||
mime = { "image/x-sun-raster", "image/x-cmu-raster" },
|
||||
deps = { "core/Err", "core/Bytes", "core/Sniff", "Types", "codecs/image/Pixels" },
|
||||
caps = { decode = true, encode = true, alpha = true, palette = true, depths = { 1, 8, 24, 32 } },
|
||||
}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ types
|
||||
export type DecodeOptions = {
|
||||
maxPixels: number?, -- refuse larger images (default 2^26)
|
||||
}
|
||||
export type EncodeOptions = {
|
||||
rle: boolean?, -- RT_BYTE_ENCODED (default true); false: RT_STANDARD
|
||||
}
|
||||
|
||||
local MAGIC = "\89\166\106\149"
|
||||
local readu8, readu32 = buffer.readu8, buffer.readu32
|
||||
local function be32(b: buffer, o: number): number
|
||||
return bit32.byteswap(readu32(b, o))
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ sniff
|
||||
function Sunras.sniff(r: Bytes.Reader): number
|
||||
return Sniff.magic(r, MAGIC)
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ decode
|
||||
function Sunras.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Image?, Err.Err?)
|
||||
local b, o, limit = r.buf, r.base, r.limit
|
||||
if limit - o < 32 then
|
||||
return nil, Err.new(Err.TRUNCATED, "short header", o)
|
||||
end
|
||||
if buffer.readstring(b, o, 4) ~= MAGIC then
|
||||
return nil, Err.new(Err.CORRUPT, "not a Sun raster", o)
|
||||
end
|
||||
local w, h, depth, typ, maptype, maplen = be32(b, o + 4), be32(b, o + 8), be32(b, o + 12), be32(b, o + 20), be32(b, o + 24), be32(b, o + 28)
|
||||
if w == 0 or h == 0 or w > 65536 or h > 65536 then
|
||||
return nil, Err.new(Err.CORRUPT, "bad size", o + 4)
|
||||
end
|
||||
if depth ~= 1 and depth ~= 8 and depth ~= 24 and depth ~= 32 then
|
||||
return nil, Err.new(Err.UNSUPPORTED, depth .. "-bit pixels", o + 12)
|
||||
end
|
||||
if typ == 4 or typ == 5 or typ > 5 and typ ~= 0xFFFF then
|
||||
return nil, Err.new(Err.UNSUPPORTED, "raster type " .. typ, o + 20)
|
||||
end
|
||||
local maxPixels = opts and opts.maxPixels or 2 ^ 26
|
||||
if w * h > maxPixels then
|
||||
return nil, Err.new(Err.LIMIT, string.format("%d x %d is over the %d pixel limit", w, h, maxPixels))
|
||||
end
|
||||
local p = o + 32
|
||||
if p + maplen > limit then
|
||||
return nil, Err.new(Err.TRUNCATED, "colour map runs past the end", p)
|
||||
end
|
||||
local pal = buffer.create(1024)
|
||||
local ncol = 0
|
||||
if maptype == 1 and maplen > 0 then
|
||||
ncol = math.min(256, maplen // 3)
|
||||
for i = 0, ncol - 1 do
|
||||
buffer.writeu32(pal, i * 4, readu8(b, p + i) + readu8(b, p + ncol + i) * 256 + readu8(b, p + 2 * ncol + i) * 65536 + 0xFF000000)
|
||||
end
|
||||
elseif depth == 1 then
|
||||
buffer.writeu32(pal, 0, 0xFFFFFFFF)
|
||||
buffer.writeu32(pal, 4, 0xFF000000)
|
||||
else
|
||||
for i = 0, 255 do
|
||||
buffer.writeu32(pal, i * 4, i * 0x10101 + 0xFF000000)
|
||||
end
|
||||
end
|
||||
p += maplen
|
||||
local stride = ((w * depth + 15) // 16) * 2
|
||||
local total = stride * h
|
||||
local raw = buffer.create(total + 4)
|
||||
local truncated = false
|
||||
if typ == 2 then
|
||||
local q = 0
|
||||
while q < total and p < limit do
|
||||
local c = readu8(b, p)
|
||||
p += 1
|
||||
if c ~= 0x80 then
|
||||
buffer.writeu8(raw, q, c)
|
||||
q += 1
|
||||
else
|
||||
if p >= limit then
|
||||
break
|
||||
end
|
||||
local n = readu8(b, p)
|
||||
p += 1
|
||||
if n == 0 then
|
||||
buffer.writeu8(raw, q, 0x80)
|
||||
q += 1
|
||||
else
|
||||
if p >= limit then
|
||||
break
|
||||
end
|
||||
local cnt = math.min(n + 1, total - q)
|
||||
buffer.fill(raw, q, readu8(b, p), cnt)
|
||||
p += 1
|
||||
q += cnt
|
||||
end
|
||||
end
|
||||
end
|
||||
truncated = q < total
|
||||
else
|
||||
local n = math.min(total, limit - p)
|
||||
buffer.copy(raw, 0, b, p, n)
|
||||
truncated = n < total
|
||||
end
|
||||
local img = Types.Image.new(w, h, "rgba8")
|
||||
local px = img.pixels
|
||||
if depth <= 8 then
|
||||
Pixels.indexed(raw, 0, stride, depth, px, w, h, true, pal)
|
||||
else
|
||||
local rgb = typ == 3
|
||||
local bytes = depth // 8
|
||||
local alpha = false
|
||||
if bytes == 4 then
|
||||
for y = 0, h - 1 do
|
||||
for x = 0, w - 1 do
|
||||
if readu8(raw, y * stride + x * 4) ~= 0 then
|
||||
alpha = true
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
for y = 0, h - 1 do
|
||||
for x = 0, w - 1 do
|
||||
local s = y * stride + x * bytes
|
||||
local a = 255
|
||||
if bytes == 4 then
|
||||
a = if alpha then readu8(raw, s) else 255
|
||||
s += 1
|
||||
end
|
||||
local c0, c1, c2 = readu8(raw, s), readu8(raw, s + 1), readu8(raw, s + 2)
|
||||
local rr, bb = if rgb then c0 else c2, if rgb then c2 else c0
|
||||
buffer.writeu32(px, (y * w + x) * 4, rr + c1 * 256 + bb * 65536 + a * 16777216)
|
||||
end
|
||||
end
|
||||
end
|
||||
if ncol > 0 then
|
||||
local list: { { number } } = {}
|
||||
for i = 0, ncol - 1 do
|
||||
local v = buffer.readu32(pal, i * 4)
|
||||
list[i + 1] = { bit32.band(v, 255), bit32.band(bit32.rshift(v, 8), 255), bit32.band(bit32.rshift(v, 16), 255), 255 }
|
||||
end
|
||||
img.meta.palette = list
|
||||
end
|
||||
if truncated then
|
||||
img.meta.truncated = true
|
||||
end
|
||||
img.meta.source = { format = "sunras", bitDepth = depth, type = typ, rle = typ == 2 }
|
||||
return img, nil
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ encode
|
||||
function Sunras.encode(img: Types.Image, opts: EncodeOptions?): (buffer?, Err.Err?)
|
||||
if type(img) ~= "table" or img.format ~= "rgba8" or type(img.pixels) ~= "buffer" then
|
||||
return nil, Err.new(Err.ARG, "needs an rgba8 Image")
|
||||
end
|
||||
local w, h = img.width, img.height
|
||||
if w < 1 or h < 1 then
|
||||
return nil, Err.new(Err.ARG, "bad size")
|
||||
end
|
||||
local px = img.pixels
|
||||
local alpha = false
|
||||
for i = 0, w * h - 1 do
|
||||
if readu8(px, i * 4 + 3) < 255 then
|
||||
alpha = true
|
||||
break
|
||||
end
|
||||
end
|
||||
local depth = if alpha then 32 else 24
|
||||
local bytes = depth // 8
|
||||
local stride = ((w * depth + 15) // 16) * 2
|
||||
local raw = buffer.create(stride * h)
|
||||
for y = 0, h - 1 do
|
||||
for x = 0, w - 1 do
|
||||
local s, d = (y * w + x) * 4, y * stride + x * bytes
|
||||
if alpha then
|
||||
buffer.writeu8(raw, d, readu8(px, s + 3))
|
||||
d += 1
|
||||
end
|
||||
buffer.writeu8(raw, d, readu8(px, s + 2))
|
||||
buffer.writeu8(raw, d + 1, readu8(px, s + 1))
|
||||
buffer.writeu8(raw, d + 2, readu8(px, s))
|
||||
end
|
||||
end
|
||||
local rle = not (opts and opts.rle == false)
|
||||
local body = raw
|
||||
if rle then
|
||||
local wr = Bytes.writer(stride * h)
|
||||
local i, n = 0, stride * h
|
||||
while i < n do
|
||||
local v = readu8(raw, i)
|
||||
local run = 1
|
||||
while i + run < n and run < 256 and readu8(raw, i + run) == v do
|
||||
run += 1
|
||||
end
|
||||
if run >= 3 or v == 0x80 then
|
||||
if run == 1 then
|
||||
wr:u8(0x80)
|
||||
wr:u8(0)
|
||||
else
|
||||
wr:u8(0x80)
|
||||
wr:u8(run - 1)
|
||||
wr:u8(v)
|
||||
end
|
||||
else
|
||||
for _ = 1, run do
|
||||
wr:u8(v)
|
||||
end
|
||||
end
|
||||
i += run
|
||||
end
|
||||
body = wr:finish()
|
||||
end
|
||||
local out = Bytes.writer(32 + buffer.len(body))
|
||||
out:string(MAGIC)
|
||||
out:u32be(w)
|
||||
out:u32be(h)
|
||||
out:u32be(depth)
|
||||
out:u32be(buffer.len(body))
|
||||
out:u32be(if rle then 2 else 1)
|
||||
out:u32be(0)
|
||||
out:u32be(0)
|
||||
out:bytes(body)
|
||||
return out:finish(), nil
|
||||
end
|
||||
|
||||
return Sunras
|
||||
Reference in new issue
Block a user