462 lines
22 KiB
Luau
462 lines
22 KiB
Luau
-- image/bmp tests: synthesized BMPs, the BMP Suite against Pillow (g/ must match, q/ and b/ must not throw).
|
|
-- Fixtures: tools/fixtures/bmp.py.
|
|
local T = require("@tests/T")
|
|
local BmpF = require("@rosetta/image/bmp")
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local Crc32 = require("@rosetta/core/Crc32")
|
|
local Err = require("@rosetta/core/Err")
|
|
local Types = require("@rosetta/Types")
|
|
local F = require("@rosetta")
|
|
local Suite = require("@tests/gen/bmp_suite")
|
|
local Enc = require("@tests/gen/bmp_enc")
|
|
|
|
local t = T.suite("bmp")
|
|
local function check(cond: any, label: string)
|
|
t:check(cond, label)
|
|
end
|
|
|
|
local Bmp = {}
|
|
function Bmp.decode(s: any, opts: any?): (any, any)
|
|
local b = if type(s) == "string" then buffer.fromstring(s) else s
|
|
if type(b) ~= "buffer" then
|
|
return nil, "expected a buffer or a string"
|
|
end
|
|
local img, err = BmpF.decode(Bytes.reader(b), opts)
|
|
if not img then
|
|
return nil, (err :: any).message
|
|
end
|
|
local src = img.meta.source :: any
|
|
local o = img :: any
|
|
o.bitsPerPixel, o.compression, o.topDown = src.bitDepth, src.compression, src.topDown
|
|
o.paletteSize, o.headerSize = src.paletteSize, src.headerSize
|
|
return img, nil
|
|
end
|
|
function Bmp.info(s: string): any
|
|
return BmpF.info(Bytes.reader(buffer.fromstring(s)))
|
|
end
|
|
function Bmp.isBmp(s: string): boolean
|
|
return BmpF.sniff(Bytes.reader(buffer.fromstring(s))) > 0
|
|
end
|
|
|
|
local function le(v: number, n: number): string
|
|
local b = buffer.create(4)
|
|
buffer.writeu32(b, 0, v % 4294967296)
|
|
return buffer.readstring(b, 0, n)
|
|
end
|
|
local function u16(v: number): string
|
|
return le(v, 2)
|
|
end
|
|
local function u32(v: number): string
|
|
return le(v, 4)
|
|
end
|
|
local function i32(v: number): string
|
|
return le(if v < 0 then v + 4294967296 else v, 4)
|
|
end
|
|
|
|
local function make(o): string
|
|
local hs = o.hs or 40
|
|
local hdr
|
|
if hs == 12 then
|
|
hdr = u32(12) .. u16(o.w) .. u16(o.h) .. u16(1) .. u16(o.bpp)
|
|
else
|
|
hdr = u32(hs) .. i32(o.w) .. i32(o.h) .. u16(1) .. u16(o.bpp) .. u32(o.comp or 0) .. u32(o.sizeImage or #o.pixels)
|
|
.. i32(2835) .. i32(2835) .. u32(o.clrUsed or 0) .. u32(0)
|
|
if hs >= 52 then
|
|
local m = o.masks or {}
|
|
hdr ..= u32(m[1] or 0) .. u32(m[2] or 0) .. u32(m[3] or 0)
|
|
if hs >= 56 then
|
|
hdr ..= u32(m[4] or 0)
|
|
end
|
|
end
|
|
hdr ..= string.rep("\0", hs - #hdr)
|
|
end
|
|
local extra = ""
|
|
if hs == 40 and o.masks then
|
|
for _, m in o.masks do
|
|
extra ..= u32(m)
|
|
end
|
|
end
|
|
local pal = ""
|
|
for _, c in o.palette or {} do
|
|
pal ..= string.char(c[3], c[2], c[1]) .. (if hs == 12 then "" else "\0")
|
|
end
|
|
local off = o.off or (14 + #hdr + #extra + #pal)
|
|
local body = hdr .. extra .. pal .. o.pixels
|
|
local size = o.bfSize or (14 + #body)
|
|
return "BM" .. u32(size) .. u32(0) .. u32(off) .. body
|
|
end
|
|
|
|
local function px(img, x, y): (number, number, number, number)
|
|
local o = (y * img.width + x) * 4
|
|
local b = img.pixels
|
|
return buffer.readu8(b, o), buffer.readu8(b, o + 1), buffer.readu8(b, o + 2), buffer.readu8(b, o + 3)
|
|
end
|
|
|
|
local function is(img, x, y, r, g, b, a, label)
|
|
local pr, pg, pb, pa = px(img, x, y)
|
|
check(pr == r and pg == g and pb == b and pa == a,
|
|
("%s: (%d,%d) = %d,%d,%d,%d, want %d,%d,%d,%d"):format(label, x, y, pr, pg, pb, pa, r, g, b, a))
|
|
end
|
|
|
|
local function decode(s: string, label: string): any
|
|
local img, err = Bmp.decode(s)
|
|
check(img ~= nil, label .. ": " .. tostring(err))
|
|
return img
|
|
end
|
|
|
|
-- 24 bpp, bottom-up, row padding (w = 3 -> 9 bytes + 3 padding)
|
|
do
|
|
-- file rows: bottom row first. bottom = red green blue, top = white black grey
|
|
local bottom = "\0\0\255" .. "\0\255\0" .. "\255\0\0" .. "\0\0\0"
|
|
local top = "\255\255\255" .. "\0\0\0" .. "\128\128\128" .. "\0\0\0"
|
|
local img = decode(make({ w = 3, h = 2, bpp = 24, pixels = bottom .. top }), "rgb24")
|
|
check(img.width == 3 and img.height == 2 and buffer.len(img.pixels) == 24, "rgb24 size")
|
|
check(img.compression == "rgb" and img.bitsPerPixel == 24 and not img.topDown, "rgb24 fields")
|
|
is(img, 0, 1, 255, 0, 0, 255, "rgb24 bottom-left red")
|
|
is(img, 1, 1, 0, 255, 0, 255, "rgb24 green")
|
|
is(img, 2, 1, 0, 0, 255, 255, "rgb24 blue")
|
|
is(img, 0, 0, 255, 255, 255, 255, "rgb24 top-left white")
|
|
is(img, 2, 0, 128, 128, 128, 255, "rgb24 grey")
|
|
-- top-down (negative height): the first file row is the top
|
|
local td = decode(make({ w = 3, h = -2, bpp = 24, pixels = bottom .. top }), "rgb24 top-down")
|
|
check(td.topDown and td.height == 2, "top-down fields")
|
|
is(td, 0, 0, 255, 0, 0, 255, "top-down first row on top")
|
|
is(td, 0, 1, 255, 255, 255, 255, "top-down second row")
|
|
local b = Bmp.decode(buffer.fromstring(make({ w = 3, h = 2, bpp = 24, pixels = bottom .. top })))
|
|
check(b and buffer.tostring(b.pixels) == buffer.tostring(img.pixels), "buffer input")
|
|
end
|
|
|
|
local PAL = { { 0, 0, 0 }, { 255, 255, 255 }, { 255, 0, 0 }, { 0, 255, 0 }, { 0, 0, 255 } }
|
|
|
|
-- 1 bpp: 10 pixels per row (2 bytes + 2 padding), MSB first
|
|
do
|
|
local row = string.char(0xA5, 0x80) .. "\0\0" -- 1010 0101 | 10
|
|
local img = decode(make({ w = 10, h = 1, bpp = 1, palette = { { 10, 20, 30 }, { 40, 50, 60 } }, pixels = row }), "pal1")
|
|
local want = { 1, 0, 1, 0, 0, 1, 0, 1, 1, 0 }
|
|
for x = 0, 9 do
|
|
local c = if want[x + 1] == 1 then 40 else 10
|
|
is(img, x, 0, c, c + 10, c + 20, 255, "pal1 x" .. x)
|
|
end
|
|
check(img.paletteSize == 2, "pal1 palette size")
|
|
end
|
|
|
|
-- 2 bpp (non-standard, as in the BMP Suite's q/pal2.bmp)
|
|
do
|
|
local img = decode(make({ w = 4, h = 1, bpp = 2, palette = PAL, clrUsed = 4, pixels = string.char(0x1B) .. "\0\0\0" }), "pal2")
|
|
is(img, 0, 0, 0, 0, 0, 255, "pal2 0")
|
|
is(img, 1, 0, 255, 255, 255, 255, "pal2 1")
|
|
is(img, 2, 0, 255, 0, 0, 255, "pal2 2")
|
|
is(img, 3, 0, 0, 255, 0, 255, "pal2 3")
|
|
end
|
|
|
|
-- 4 bpp, odd width; palette shorter than 16 (clrUsed = 5): index 7 is past it -> opaque black
|
|
do
|
|
local img = decode(make({ w = 3, h = 1, bpp = 4, palette = PAL, clrUsed = 5, pixels = string.char(0x24, 0x70, 0, 0) }), "pal4")
|
|
is(img, 0, 0, 255, 0, 0, 255, "pal4 x0 = 2")
|
|
is(img, 1, 0, 0, 0, 255, 255, "pal4 x1 = 4")
|
|
is(img, 2, 0, 0, 0, 0, 255, "pal4 x2 = 7 (past the palette)")
|
|
check(img.paletteSize == 5, "pal4 palette size")
|
|
end
|
|
|
|
-- 8 bpp with clrUsed = 0 but only 3 entries before bfOffBits (a palette shorter than 2^bpp)
|
|
do
|
|
local s = make({ w = 4, h = 1, bpp = 8, palette = { PAL[3], PAL[4], PAL[5] }, pixels = string.char(0, 1, 2, 200) })
|
|
local img = decode(s, "pal8 short")
|
|
check(img.paletteSize == 3, "pal8 short palette size " .. img.paletteSize)
|
|
is(img, 0, 0, 255, 0, 0, 255, "pal8 0")
|
|
is(img, 2, 0, 0, 0, 255, 255, "pal8 2")
|
|
is(img, 3, 0, 0, 0, 0, 255, "pal8 200 -> black")
|
|
end
|
|
|
|
-- OS/2 1.x core header: 16-bit dimensions, 3-byte palette entries
|
|
do
|
|
local img = decode(make({ hs = 12, w = 2, h = 2, bpp = 8, palette = PAL, pixels = string.char(1, 2, 0, 0, 3, 4, 0, 0) }), "core")
|
|
check(img.headerSize == 12 and img.paletteSize == 5, "core fields")
|
|
is(img, 0, 1, 255, 255, 255, 255, "core bottom-left")
|
|
is(img, 1, 1, 255, 0, 0, 255, "core bottom-right")
|
|
is(img, 0, 0, 0, 255, 0, 255, "core top-left")
|
|
is(img, 1, 0, 0, 0, 255, 255, "core top-right")
|
|
local img24 = decode(make({ hs = 12, w = 1, h = 1, bpp = 24, pixels = "\1\2\3\0" }), "core 24")
|
|
is(img24, 0, 0, 3, 2, 1, 255, "core 24")
|
|
end
|
|
|
|
-- 16 bpp: BI_RGB = 555; BITFIELDS 565; v3 header with a 4444 alpha mask. Scaling is v * 255 // max (as Pillow).
|
|
do
|
|
local p555 = 31 * 1024 + 16 * 32 + 1 -- r 31, g 16, b 1
|
|
local img = decode(make({ w = 1, h = 1, bpp = 16, pixels = u16(p555) .. "\0\0" }), "555")
|
|
is(img, 0, 0, 255, 131, 8, 255, "555")
|
|
local p565 = 1 * 2048 + 63 * 32 + 30
|
|
local img2 = decode(make({ w = 1, h = 1, bpp = 16, comp = 3, masks = { 0xF800, 0x7E0, 0x1F }, pixels = u16(p565) .. "\0\0" }), "565")
|
|
is(img2, 0, 0, 8, 255, 246, 255, "565")
|
|
check(img2.compression == "bitfields", "565 compression name")
|
|
local m4444 = { 0x0F00, 0x00F0, 0x000F, 0xF000 }
|
|
local img3 = decode(make({ hs = 56, w = 2, h = 1, bpp = 16, comp = 3, masks = m4444, pixels = u16(0x8F00) .. u16(0x00F0) }), "4444")
|
|
is(img3, 0, 0, 255, 0, 0, 136, "4444 alpha")
|
|
is(img3, 1, 0, 0, 255, 0, 0, "4444 transparent")
|
|
-- an alpha mask whose values are all zero: opaque
|
|
local img4 = decode(make({ hs = 56, w = 1, h = 1, bpp = 16, comp = 3, masks = m4444, pixels = u16(0x0F00) .. "\0\0" }), "4444 zero alpha")
|
|
is(img4, 0, 0, 255, 0, 0, 255, "4444 all-zero alpha -> opaque")
|
|
-- v4 header with BI_RGB: masks in the header are ignored -> 555
|
|
local img5 = decode(make({ hs = 108, w = 1, h = 1, bpp = 16, masks = { 0xF800, 0x7E0, 0x1F }, pixels = u16(p555) .. "\0\0" }), "v4 555")
|
|
is(img5, 0, 0, 255, 131, 8, 255, "v4 BI_RGB 16 -> 555")
|
|
end
|
|
|
|
do
|
|
-- BI_RGB, alpha bytes all zero -> opaque
|
|
local img = decode(make({ w = 2, h = 1, bpp = 32, pixels = "\1\2\3\0\4\5\6\0" }), "rgb32 zero alpha")
|
|
is(img, 0, 0, 3, 2, 1, 255, "rgb32 opaque 0")
|
|
is(img, 1, 0, 6, 5, 4, 255, "rgb32 opaque 1")
|
|
-- BI_RGB with alpha bytes used (as browsers do)
|
|
local img2 = decode(make({ w = 2, h = 1, bpp = 32, pixels = "\1\2\3\128\4\5\6\0" }), "rgb32 alpha")
|
|
is(img2, 0, 0, 3, 2, 1, 128, "rgb32 alpha 128")
|
|
is(img2, 1, 0, 6, 5, 4, 0, "rgb32 alpha 0")
|
|
-- BITFIELDS in an unusual order (R in the top byte), no alpha mask -> opaque even with bytes there
|
|
local img3 = decode(make({ w = 1, h = 1, bpp = 32, comp = 3, masks = { 0xFF000000, 0xFF0000, 0xFF00 }, pixels = "\9\3\2\1" }), "rgb32 bf")
|
|
is(img3, 0, 0, 1, 2, 3, 255, "rgb32 bitfields order")
|
|
-- standard BITFIELDS without an alpha mask: the 4th byte is ignored
|
|
local img4 = decode(make({ w = 1, h = 1, bpp = 32, comp = 3, masks = { 0xFF0000, 0xFF00, 0xFF }, pixels = "\1\2\3\77" }), "rgb32 bf std")
|
|
is(img4, 0, 0, 3, 2, 1, 255, "rgb32 std bitfields opaque")
|
|
-- ALPHABITFIELDS (compression 6) after a 40-byte header
|
|
local img5 = decode(make({ w = 1, h = 1, bpp = 32, comp = 6, masks = { 0xFF, 0xFF00, 0xFF0000, 0xFF000000 }, pixels = "\1\2\3\4" }), "alphabf")
|
|
is(img5, 0, 0, 1, 2, 3, 4, "alphabitfields RGBA")
|
|
check(img5.compression == "alphabitfields", "alphabitfields name")
|
|
-- v5 header with BITFIELDS + alpha mask; 10-bit channels (1010102)
|
|
local v = 1023 + 512 * 1024 + 0 * 1048576 + 3 * 1073741824
|
|
local img6 = decode(make({ hs = 124, w = 1, h = 1, bpp = 32, comp = 3, masks = { 0x3FF, 0xFFC00, 0x3FF00000, 0xC0000000 }, pixels = u32(v) }), "1010102")
|
|
is(img6, 0, 0, 255, 127, 0, 255, "1010102")
|
|
-- v4 header with BI_RGB 32: alpha byte used
|
|
local img7 = decode(make({ hs = 108, w = 1, h = -1, bpp = 32, pixels = "\1\2\3\200" }), "v4 rgb32")
|
|
is(img7, 0, 0, 3, 2, 1, 200, "v4 rgb32 alpha")
|
|
-- 24 bpp with BITFIELDS (Pillow accepts it)
|
|
local img8 = decode(make({ w = 1, h = 1, bpp = 24, comp = 3, masks = { 0xFF, 0xFF00, 0xFF0000 }, pixels = "\1\2\3\0" }), "rgb24 bf")
|
|
is(img8, 0, 0, 1, 2, 3, 255, "rgb24 bitfields")
|
|
end
|
|
|
|
-- RLE8: run, absolute (odd length, padded), end of line, delta, end of bitmap; skipped pixels are transparent
|
|
do
|
|
local pal = PAL
|
|
local stream = string.char(
|
|
3, 2, -- row 0 (bottom): 3 x red
|
|
0, 3, 1, 3, 4, 0, -- absolute 3: white green blue + pad
|
|
0, 0, -- EOL (row 0 is 6 wide: x = 6, w = 8: x 6..7 skipped)
|
|
0, 2, 2, 1, -- delta right 2, up 1: row 2, x = 2
|
|
2, 4, -- 2 x blue at row 2 x 2..3
|
|
0, 1 -- EOB: row 3 never written
|
|
)
|
|
local img = decode(make({ w = 8, h = 4, bpp = 8, comp = 1, palette = pal, pixels = stream }), "rle8")
|
|
check(img.compression == "rle8", "rle8 name")
|
|
for x = 0, 2 do
|
|
is(img, x, 3, 255, 0, 0, 255, "rle8 run " .. x)
|
|
end
|
|
is(img, 3, 3, 255, 255, 255, 255, "rle8 abs 0")
|
|
is(img, 4, 3, 0, 255, 0, 255, "rle8 abs 1")
|
|
is(img, 5, 3, 0, 0, 255, 255, "rle8 abs 2")
|
|
is(img, 6, 3, 0, 0, 0, 0, "rle8 EOL skipped -> transparent")
|
|
is(img, 0, 2, 0, 0, 0, 0, "rle8 row 1 skipped by the delta")
|
|
is(img, 1, 1, 0, 0, 0, 0, "rle8 delta skip")
|
|
is(img, 2, 1, 0, 0, 255, 255, "rle8 after delta 0")
|
|
is(img, 3, 1, 0, 0, 255, 255, "rle8 after delta 1")
|
|
is(img, 4, 1, 0, 0, 0, 0, "rle8 after the run")
|
|
is(img, 0, 0, 0, 0, 0, 0, "rle8 EOB: top row empty")
|
|
-- a run past the row end is clipped; truncated data keeps what was drawn
|
|
local img2 = decode(make({ w = 2, h = 1, bpp = 8, comp = 1, palette = pal, pixels = string.char(5, 2, 0) }), "rle8 clip")
|
|
is(img2, 1, 0, 255, 0, 0, 255, "rle8 clipped run")
|
|
-- top-down RLE (out of spec, accepted)
|
|
local img3 = decode(make({ w = 1, h = -2, bpp = 8, comp = 1, palette = pal, pixels = string.char(1, 2, 0, 0, 1, 3, 0, 1) }), "rle8 top-down")
|
|
is(img3, 0, 0, 255, 0, 0, 255, "rle8 top-down row 0")
|
|
is(img3, 0, 1, 0, 255, 0, 255, "rle8 top-down row 1")
|
|
end
|
|
|
|
-- RLE4: alternating nibbles in a run, an odd absolute run (3 pixels in 2 bytes, padded to 4)
|
|
do
|
|
local stream = string.char(5, 0x23, 0, 3, 0x14, 0x30, 0, 0, 0, 1)
|
|
local img = decode(make({ w = 8, h = 1, bpp = 4, comp = 2, palette = PAL, pixels = stream }), "rle4")
|
|
check(img.compression == "rle4", "rle4 name")
|
|
local want = { 2, 3, 2, 3, 2, 1, 4, 3 }
|
|
for x = 0, 7 do
|
|
local c = PAL[want[x + 1] + 1]
|
|
is(img, x, 0, c[1], c[2], c[3], 255, "rle4 x" .. x)
|
|
end
|
|
end
|
|
|
|
-- leniency: bad bfSize, bad biSizeImage, bad bfOffBits, truncated pixels
|
|
do
|
|
local pixels = "\0\0\255" .. "\0\0\0" .. "\255\0\0" .. "\0\0\0"
|
|
local base = decode(make({ w = 1, h = 3, bpp = 24, pixels = pixels }), "lenient base")
|
|
local function same(img, label)
|
|
check(img and buffer.tostring(img.pixels) == buffer.tostring(base.pixels), label)
|
|
end
|
|
same(Bmp.decode(make({ w = 1, h = 3, bpp = 24, pixels = pixels, bfSize = 0 })), "bfSize 0")
|
|
same(Bmp.decode(make({ w = 1, h = 3, bpp = 24, pixels = pixels, bfSize = 999999 })), "bfSize too big")
|
|
same(Bmp.decode(make({ w = 1, h = 3, bpp = 24, pixels = pixels, sizeImage = 0 })), "biSizeImage 0")
|
|
same(Bmp.decode(make({ w = 1, h = 3, bpp = 24, pixels = pixels, sizeImage = 123456 })), "biSizeImage wrong")
|
|
same(Bmp.decode(make({ w = 1, h = 3, bpp = 24, pixels = pixels, off = 0 })), "bfOffBits 0")
|
|
same(Bmp.decode(make({ w = 1, h = 3, bpp = 24, pixels = pixels, off = 99999 })), "bfOffBits past the end")
|
|
-- truncated: the missing (top) rows read as zeros = opaque black
|
|
local t = decode(make({ w = 1, h = 3, bpp = 24, pixels = string.sub(pixels, 1, 5) }), "truncated")
|
|
is(t, 0, 2, 255, 0, 0, 255, "truncated: bottom row intact")
|
|
is(t, 0, 1, 0, 0, 0, 255, "truncated: partial row zero-filled")
|
|
is(t, 0, 0, 0, 0, 0, 255, "truncated: missing row black")
|
|
local t2 = decode(make({ w = 4, h = 4, bpp = 8, palette = PAL, pixels = "" }), "no pixel data")
|
|
is(t2, 3, 3, 0, 0, 0, 255, "no pixel data: index 0")
|
|
end
|
|
|
|
do
|
|
local s = make({ hs = 124, w = 7, h = -5, bpp = 32, comp = 3, masks = { 0xFF0000, 0xFF00, 0xFF, 0xFF000000 }, pixels = "" })
|
|
local info = Bmp.info(s)
|
|
check(info and info.width == 7 and info.height == 5 and info.topDown and info.bitsPerPixel == 32, "info fields")
|
|
check(info and info.compression == 3 and info.headerSize == 124 and not info.os2, "info compression / header")
|
|
check(Bmp.isBmp(s) and not Bmp.isBmp("\137PNG") and not Bmp.isBmp(""), "isBmp")
|
|
end
|
|
|
|
-- errors: nil + message, never a throw
|
|
do
|
|
local function fails(s: any, pattern: string, label: string, opts: any?)
|
|
local ok, img, err = pcall(Bmp.decode, s, opts)
|
|
check(ok, label .. ": threw " .. tostring(img))
|
|
check(img == nil and type(err) == "string" and string.find(err, pattern) ~= nil, label .. ": " .. tostring(err))
|
|
end
|
|
fails("", "too short", "empty")
|
|
fails(123, "expected a buffer", "not a buffer")
|
|
fails("\137PNG\r\n\26\n" .. string.rep("\0", 40), "not a BMP", "png")
|
|
fails("BM" .. string.rep("\0", 12) .. u32(200) .. string.rep("\0", 20), "header size", "header past the end")
|
|
fails("BM" .. string.rep("\0", 12) .. u32(8) .. string.rep("\0", 40), "header size", "header too small")
|
|
fails(make({ w = 0, h = 1, bpp = 24, pixels = "" }), "bad size", "zero width")
|
|
fails(make({ w = -4, h = 1, bpp = 24, pixels = "" }), "bad size", "negative width")
|
|
fails(make({ w = 1, h = 0, bpp = 24, pixels = "" }), "bad size", "zero height")
|
|
fails(make({ w = 1, h = 1, bpp = 3, pixels = "\0\0\0\0" }), "bit depth", "bpp 3")
|
|
fails(make({ w = 1, h = 1, bpp = 64, pixels = string.rep("\0", 8) }), "bit depth", "bpp 64")
|
|
fails(make({ w = 1, h = 1, bpp = 24, comp = 1, pixels = "" }), "RLE", "RLE8 with 24 bpp")
|
|
fails(make({ w = 1, h = 1, bpp = 8, comp = 3, pixels = "" }), "BITFIELDS", "BITFIELDS with 8 bpp")
|
|
fails(make({ w = 1, h = 1, bpp = 24, comp = 4, pixels = "" }), "JPEG", "jpeg")
|
|
fails(make({ w = 1, h = 1, bpp = 24, comp = 5, pixels = "" }), "PNG", "png in bmp")
|
|
fails(make({ w = 1, h = 1, bpp = 24, comp = 9, pixels = "" }), "compression", "unknown compression")
|
|
fails(make({ w = 1, h = 1, bpp = 32, comp = 3, pixels = "" }), "masks", "truncated bit masks")
|
|
fails(make({ w = 100000, h = 100000, bpp = 24, pixels = "" }), "too large", "too large")
|
|
fails(make({ w = 100, h = 100, bpp = 24, pixels = "" }), "too large", "maxPixels option", { maxPixels = 9999 })
|
|
end
|
|
|
|
do
|
|
local seeds = {
|
|
make({ w = 3, h = 2, bpp = 24, pixels = string.rep("\1\2\3", 8) }),
|
|
make({ w = 10, h = 3, bpp = 1, palette = PAL, clrUsed = 2, pixels = string.rep("\170\85\0\0", 3) }),
|
|
make({ w = 5, h = 2, bpp = 4, palette = PAL, pixels = string.rep("\18\52\5\0", 2) }),
|
|
make({ hs = 12, w = 2, h = 2, bpp = 8, palette = PAL, pixels = string.char(1, 2, 0, 0, 3, 4, 0, 0) }),
|
|
make({ hs = 56, w = 2, h = 1, bpp = 16, comp = 3, masks = { 0x0F00, 0xF0, 0xF, 0xF000 }, pixels = "\1\2\3\4" }),
|
|
make({ hs = 124, w = 2, h = -2, bpp = 32, comp = 3, masks = { 0xFF0000, 0xFF00, 0xFF, 0xFF000000 }, pixels = string.rep("\1\2\3\4", 4) }),
|
|
make({ w = 8, h = 4, bpp = 8, comp = 1, palette = PAL, pixels = string.char(3, 2, 0, 3, 1, 3, 4, 0, 0, 0, 0, 2, 2, 1, 2, 4, 0, 1) }),
|
|
make({ w = 8, h = 1, bpp = 4, comp = 2, palette = PAL, pixels = string.char(5, 0x23, 0, 3, 0x14, 0x30, 0, 0, 0, 1) }),
|
|
}
|
|
local state = 12345
|
|
local function rand(n: number): number
|
|
state = (state * 1103515245 + 12345) % 2147483648
|
|
return state % n
|
|
end
|
|
local runs = 0
|
|
for _, s in seeds do
|
|
for cut = 0, #s do
|
|
local ok, img = pcall(Bmp.decode, string.sub(s, 1, cut))
|
|
check(ok, "truncated at " .. cut .. " threw: " .. tostring(img))
|
|
runs += 1
|
|
end
|
|
for _ = 1, 400 do
|
|
local b = buffer.fromstring(s)
|
|
for _ = 1, 1 + rand(4) do
|
|
buffer.writeu8(b, rand(#s), rand(256))
|
|
end
|
|
local ok, img = pcall(Bmp.decode, b, { maxPixels = 2 ^ 20 })
|
|
check(ok, "corrupted file threw: " .. tostring(img))
|
|
if ok and img then
|
|
check(buffer.len(img.pixels) == img.width * img.height * 4, "corrupted file: pixel buffer size")
|
|
end
|
|
runs += 1
|
|
end
|
|
end
|
|
t:note("fuzz: %d decodes, no throw", runs)
|
|
end
|
|
|
|
do
|
|
local nMatch, nOther = 0, 0
|
|
local samples = {}
|
|
for _, e in Suite do
|
|
local ok, img, err = pcall(BmpF.decode, Bytes.reader(buffer.fromstring(e.data)))
|
|
t:check(ok, e.name .. ": threw " .. tostring(img))
|
|
local same = img and e.w and img.width == e.w and img.height == e.h and Crc32.of(img.pixels) == e.crc
|
|
if e.mode == "match" then
|
|
t:check(same, e.name .. ": matches Pillow (" .. tostring(err) .. ")")
|
|
nMatch += 1
|
|
else
|
|
t:check(img == nil or buffer.len(img.pixels) == img.width * img.height * 4, e.name .. ": pixel buffer size")
|
|
nOther += 1
|
|
end
|
|
if #samples < 30 then
|
|
table.insert(samples, e.data)
|
|
end
|
|
end
|
|
t:note("BMP Suite: %d g/ files match Pillow, %d q/ + b/ files decode or fail cleanly", nMatch, nOther)
|
|
t:fuzz("decode (suite)", function(b: buffer)
|
|
return BmpF.decode(Bytes.reader(b), { maxPixels = 2 ^ 20 })
|
|
end, samples, { mutations = 6 })
|
|
end
|
|
|
|
do
|
|
for _, c in Enc do
|
|
local img = Types.Image.new(c.w, c.h)
|
|
buffer.writestring(img.pixels, 0, c.rgba)
|
|
local opts: any = if c.mode == "RGBA" then { header = "info" } elseif c.mode == "RGB" then { bitDepth = 24 } else nil
|
|
local out, err = BmpF.encode(img, opts)
|
|
t:check(out, c.name .. ": encode: " .. tostring(err))
|
|
local same = buffer.tostring(out :: buffer) == c.bmp
|
|
if not same then
|
|
t:note("%s ours %s", c.name, T.hex(out :: buffer, 70))
|
|
t:note("%s pillow %s", c.name, T.hex(buffer.fromstring(c.bmp), 70))
|
|
end
|
|
t:check(same, c.name .. ": identical to Pillow")
|
|
local back = BmpF.decode(Bytes.reader(out :: buffer)) :: any
|
|
t:check(back and buffer.tostring(back.pixels) == c.rgba, c.name .. ": round trip")
|
|
-- the default 32 bpp header (V4 BITFIELDS) round-trips too
|
|
if c.mode == "RGBA" then
|
|
local v4 = BmpF.encode(img) :: buffer
|
|
t:eq(buffer.readu32(v4, 14), 108, c.name .. ": V4 header")
|
|
local b2 = BmpF.decode(Bytes.reader(v4)) :: any
|
|
t:check(b2 and buffer.tostring(b2.pixels) == c.rgba and b2.meta.source.compression == "bitfields", c.name .. ": V4 round trip")
|
|
end
|
|
end
|
|
t:note("encoder: %d files identical to Pillow", #Enc)
|
|
local img = Types.Image.new(5, 4)
|
|
for i = 0, 19 do
|
|
buffer.writeu32(img.pixels, i * 4, 0xFF000000 + i * 0x010203)
|
|
end
|
|
for _, d in { 8, 24, 32 } do
|
|
local out = BmpF.encode(img, { bitDepth = d }) :: buffer
|
|
local back = BmpF.decode(Bytes.reader(out)) :: any
|
|
t:check(back.meta.source.bitDepth == d and buffer.tostring(back.pixels) == buffer.tostring(img.pixels), "bitDepth " .. d)
|
|
end
|
|
img.meta.dpi = { x = 300, y = 150 }
|
|
local withDpi = BmpF.decode(Bytes.reader(BmpF.encode(img) :: buffer)) :: any
|
|
t:check(math.abs(withDpi.meta.dpi.x - 300) < 0.1 and math.abs(withDpi.meta.dpi.y - 150) < 0.1, "dpi round trip")
|
|
local many = Types.Image.new(300, 1)
|
|
for i = 0, 299 do
|
|
buffer.writeu32(many.pixels, i * 4, 0xFF000000 + i)
|
|
end
|
|
local _, e1 = BmpF.encode(many, { bitDepth = 8 })
|
|
t:check(e1 and e1.code == Err.ARG, "8 bpp with 300 colours")
|
|
t:eq(buffer.readu16(BmpF.encode(many) :: buffer, 28), 24, "auto: 300 opaque colours -> 24 bpp")
|
|
local _, e2 = BmpF.encode({} :: any)
|
|
t:check(e2 and e2.code == Err.ARG, "not an image")
|
|
local _, e3 = BmpF.encode(img, { bitDepth = 16 } :: any)
|
|
t:check(e3 and e3.code == Err.ARG, "bitDepth 16")
|
|
local c = Enc[1]
|
|
local loaded = F.image.load(c.bmp)
|
|
t:check(loaded and loaded.width == c.w and buffer.tostring(loaded.pixels) == c.rgba, "F.image.load")
|
|
t:eq(F.detect(c.bmp), "bmp", "F.detect")
|
|
local saved = F.save(loaded, nil, { format = "bmp" })
|
|
t:check(saved and buffer.tostring(saved) == c.bmp, "F.save bmp")
|
|
local big = buffer.create(#c.bmp + 5)
|
|
buffer.writestring(big, 3, c.bmp)
|
|
local sub = BmpF.decode(Bytes.reader(big, 3, #c.bmp)) :: any
|
|
t:check(sub and buffer.tostring(sub.pixels) == c.rgba, "decode from a sub-range")
|
|
end
|
|
|
|
t:done()
|