ws-tex: image/ani (animated cursors -> frames through the decodeImage hook; write through encodeImage)

seq / rate steps, jiffy delays, CUR hotspots, INFO title; AF_ICON clear is UNSUPPORTED. Tested against
Pillow-written ICO / CUR frames assembled into ANI files.
This commit is contained in:
4DBug committed 2026-10-31 15:09:35 -05:00
1 parent 9e114fba8b
commit 00d47096ed
5 files changed
+334

No files matched your search

+12
View File
@@ -14,6 +14,18 @@ export type Entry = {
}
local list: { Entry } = {
{
name = "ani",
category = "image",
path = "image/ani",
extensions = { "ani" },
mime = { "application/x-navi-animation" },
deps = { "core/Err", "core/Bytes", "Types", "containers/Riff" },
caps = { decode = true, encode = true, alpha = true, animation = true, frames = true },
load = function()
return require("@formats/image/ani")
end,
},
{
name = "bmp",
category = "image",
+183
View File
@@ -0,0 +1,183 @@
--!strict
-- image/ani: Windows animated cursors (RIFF "ACON": the anih header, rate / seq chunks, LIST fram of icon chunks;
-- containers/Riff; new code). Decode: every step of the animation as img.frames (the seq order when there is one,
-- each step's rate or the default iDispRate in jiffies of 1/60 s as the delay), the icon chunks (ICO / CUR files)
-- through the api hook opts.decodeImage (F.load passes it; without it: UNSUPPORTED), the hotspots of CUR frames
-- (frame image meta.hotspot), INFO title / author (meta.text). The image is the first step; img.loops = 0 (cursors
-- loop forever). Encode: img.frames (or the image) through opts.encodeImage(frame, "ico") as icon chunks, with a
-- rate chunk when the delays differ. Not supported: raw (non-icon) frame bitmaps (AF_ICON clear: UNSUPPORTED).
-- ------------------------------------------------------------------------------------------------ requires
local Err = require("@formats/core/Err")
local Bytes = require("@formats/core/Bytes")
local Types = require("@formats/Types")
local Riff = require("@formats/containers/Riff")
-- ------------------------------------------------------------------------------------------------ contract
local Ani = {
name = "ani",
category = "image",
extensions = { "ani" },
mime = { "application/x-navi-animation" },
deps = { "core/Err", "core/Bytes", "Types", "containers/Riff" },
caps = { decode = true, encode = true, alpha = true, animation = true, frames = true },
}
-- ------------------------------------------------------------------------------------------------ types
export type DecodeOptions = {
decodeImage: ((buffer) -> (Types.Image?, Err.Err?))?, -- decodes the ICO / CUR frames (the api passes one)
maxFrames: number?, -- refuse more steps (default 10000)
}
export type EncodeOptions = {
encodeImage: ((Types.Image, string) -> (buffer?, Err.Err?))?, -- writes the ICO / CUR frames (the api passes one)
}
-- ------------------------------------------------------------------------------------------------ sniff
function Ani.sniff(r: Bytes.Reader): number
return if r:peek(4) == "RIFF" and r:peek(4, 8) == "ACON" then 1 else 0
end
-- ------------------------------------------------------------------------------------------------ decode
function Ani.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Image?, Err.Err?)
local f, ferr = Riff.read(r)
if not f then
return nil, ferr
end
if f.form ~= "ACON" then
return nil, Err.new(Err.CORRUPT, "not an ACON form", r.base + 8)
end
local anih = Riff.find(f, "anih")
if not anih or anih.avail < 36 then
return nil, Err.new(Err.CORRUPT, "missing anih header", r.base + 12)
end
local a = anih.offset
local nFrames, nSteps = Riff.u32(f, a + 4), Riff.u32(f, a + 8)
local dispRate, flags = Riff.u32(f, a + 28), Riff.u32(f, a + 32)
local maxFrames = opts and opts.maxFrames or 10000
if nSteps == 0 then
nSteps = nFrames
end
if nFrames > maxFrames or nSteps > maxFrames then
return nil, Err.new(Err.LIMIT, "too many frames", a + 4)
end
if bit32.band(flags, 1) == 0 then
return nil, Err.new(Err.UNSUPPORTED, "raw frame bitmaps (AF_ICON clear)", a + 32)
end
local decodeImage = opts and opts.decodeImage
if not decodeImage then
return nil, Err.new(Err.UNSUPPORTED, "icon frames need the decodeImage hook")
end
-- the icon chunks
local icons: { Types.Image } = {}
for _, c in f.chunks do
if c.id == "LIST" and Riff.listType(f, c) == "fram" then
for _, ic in Riff.list(f, c) do
if ic.id == "icon" then
local sub = buffer.create(ic.avail)
buffer.copy(sub, 0, f.buf, ic.offset, ic.avail)
local ok, im, ierr = pcall(decodeImage, sub)
if not ok or not im then
return nil, Err.new(Err.CORRUPT, "frame " .. (#icons + 1) .. ": " .. tostring(if ok then ierr else im), ic.offset)
end
table.insert(icons, im :: Types.Image)
end
end
end
end
if #icons == 0 then
return nil, Err.new(Err.TRUNCATED, "no icon frames")
end
local rate = Riff.find(f, "rate")
local seq = Riff.find(f, "seq ")
local frames: { Types.Frame } = {}
for s = 0, nSteps - 1 do
local idx = s
if seq and seq.avail >= (s + 1) * 4 then
idx = Riff.u32(f, seq.offset + s * 4)
end
local im = icons[idx + 1]
if not im then
if s >= #icons and not seq then
break -- fewer icons than steps: keep what is there
end
return nil, Err.new(Err.CORRUPT, "step " .. s .. " names frame " .. idx)
end
local jiffies = dispRate
if rate and rate.avail >= (s + 1) * 4 then
jiffies = Riff.u32(f, rate.offset + s * 4)
end
table.insert(frames, { image = im, delay = jiffies / 60, x = 0, y = 0, dispose = "background", blend = "source" } :: Types.Frame)
end
local first = frames[1].image
local img = Types.Image.new(0, 0, first.format)
img.width, img.height, img.pixels = first.width, first.height, first.pixels
img.frames = frames
img.loops = 0
img.meta.hotspot = first.meta.hotspot
local text: { [string]: string } = {}
for _, c in f.chunks do
if c.id == "LIST" and Riff.listType(f, c) == "INFO" then
for _, it in Riff.list(f, c) do
local key = if it.id == "INAM" then "title" elseif it.id == "IART" then "author" else nil
if key then
text[key] = (string.gsub(buffer.readstring(f.buf, it.offset, it.avail), "%z+$", ""))
end
end
end
end
if next(text) then
img.meta.text = text
end
img.meta.source = { format = "ani", frames = #icons, steps = #frames, sequence = seq ~= nil, rate = dispRate }
return img, nil
end
-- ------------------------------------------------------------------------------------------------ encode
function Ani.encode(img: Types.Image, opts: EncodeOptions?): (buffer?, Err.Err?)
if type(img) ~= "table" or type(img.pixels) ~= "buffer" then
return nil, Err.new(Err.ARG, "needs an Image")
end
local encodeImage = opts and opts.encodeImage
if not encodeImage then
return nil, Err.new(Err.UNSUPPORTED, "icon frames need the encodeImage hook")
end
local frames: { Types.Frame } = if type(img.frames) == "table" and #img.frames > 0 then img.frames else { { image = img, delay = 1 / 6, x = 0, y = 0, dispose = "background", blend = "source" } :: Types.Frame }
local w = Riff.writer("ACON")
local jiffies: { number } = {}
local same = true
for i, fr in frames do
jiffies[i] = math.max(1, math.floor((fr.delay or 0) * 60 + 0.5))
same = same and jiffies[i] == jiffies[1]
end
local head = Bytes.writer(36)
head:u32le(36)
head:u32le(#frames)
head:u32le(#frames)
head:u32le(0)
head:u32le(0)
head:u32le(0)
head:u32le(0)
head:u32le(jiffies[1])
head:u32le(1)
w:chunk("anih", head:finish())
if not same then
local rw = Bytes.writer(#frames * 4)
for _, j in jiffies do
rw:u32le(j)
end
w:chunk("rate", rw:finish())
end
local items = {}
for i, fr in frames do
local im = fr.image
local ok, data, err = pcall(encodeImage, im, "ico")
if not ok or not data then
return nil, Err.new(Err.ARG, "frame " .. i .. ": " .. tostring(if ok then err else data))
end
table.insert(items, { "icon", data })
end
w:list("fram", items)
return w:finish()
end
return Ani