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,16 +1,8 @@
|
||||
--!strict
|
||||
--!optimize 2
|
||||
-- image/bmp: Windows / OS/2 BMP. Absorbed from bmp-luau b86aed6 (same author, no third-party code; the reader), plus a
|
||||
-- new encoder. Decode: BITMAPCOREHEADER (OS/2 1.x, 12 bytes), OS/2 2.x (16..64), BITMAPINFOHEADER (40), the v2 / v3
|
||||
-- variants (52 / 56), BITMAPV4HEADER / BITMAPV5HEADER (108 / 124); 1 / 2 / 4 / 8 bpp paletted, 16 bpp (555 or
|
||||
-- BITFIELDS), 24 bpp, 32 bpp (BI_RGB, BITFIELDS, ALPHABITFIELDS), RLE8 / RLE4 (delta / end-of-line / end-of-bitmap),
|
||||
-- bottom-up and top-down rows. Lenient: bfSize / biSizeImage are ignored, a bad bfOffBits falls back to the end of the
|
||||
-- palette, a short or missing palette pads with opaque black, a truncated pixel array decodes as zero bytes, a 32-bit
|
||||
-- image whose alpha is all zero is opaque. Masks scale as Pillow does (v * 255 // max).
|
||||
-- Encode (rgba8): 8 bpp paletted when the image is opaque with at most 256 colours (palette in first-appearance
|
||||
-- order), else 24 bpp when opaque, else 32 bpp with alpha (BITMAPV4HEADER + BITFIELDS, or BI_RGB with
|
||||
-- opts.header = "info" as Pillow writes). Byte-identical to Pillow for 24 bpp, paletted and BI_RGB 32 bpp output.
|
||||
-- Not supported: embedded JPEG / PNG, OS/2 Huffman / RLE24, writing RLE / 16 bpp / 1-4 bpp.
|
||||
-- image/bmp: Windows / OS/2 BMP; absorbed from bmp-luau b86aed6 (same author), new encoder. Supports: headers 12 /
|
||||
-- 16..64 / 40 / 52 / 56 / 108 / 124, 1..32 bpp, BITFIELDS / ALPHABITFIELDS, RLE8 / RLE4, both row orders, Pillow's
|
||||
-- leniency and mask scaling; writes 8 / 24 / 32 bpp as Pillow. Not supported: embedded JPEG / PNG, OS/2 Huffman, RLE24.
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ requires
|
||||
local Err = require("@rosetta/core/Err")
|
||||
@@ -40,11 +32,11 @@ export type Info = {
|
||||
}
|
||||
|
||||
export type DecodeOptions = {
|
||||
maxPixels: number?, -- refuse images with more pixels than this (default 2^26 = 64 Mpx)
|
||||
maxPixels: number?, -- default 2^26
|
||||
}
|
||||
export type EncodeOptions = {
|
||||
bitDepth: (number | "auto")?, -- 8, 24, 32; default "auto": 8 when opaque with <= 256 colours, else 24 when opaque, else 32
|
||||
header: ("v4" | "info")?, -- 32 bpp: "v4" (default, BITFIELDS with an alpha mask) or "info" (BI_RGB, as Pillow)
|
||||
bitDepth: (number | "auto")?, -- default "auto": 8 if opaque with <= 256 colours, else 24 if opaque, else 32
|
||||
header: ("v4" | "info")?, -- 32 bpp: "v4" (default, BITFIELDS) or "info" (BI_RGB, as Pillow)
|
||||
}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ constants
|
||||
@@ -54,7 +46,6 @@ local COMPRESSION_NAMES = { [0] = "rgb", "rle8", "rle4", "bitfields", "jpeg", "p
|
||||
local readu8, readu16, readu32, readi32 = buffer.readu8, buffer.readu16, buffer.readu32, buffer.readi32
|
||||
local band, rshift, lshift, countrz = bit32.band, bit32.rshift, bit32.lshift, bit32.countrz
|
||||
|
||||
-- the reader's range as a buffer starting at offset 0 (copied when it is a part of a bigger buffer)
|
||||
local function whole(r: Bytes.Reader): buffer
|
||||
if r.base == 0 and r.limit == buffer.len(r.buf) then
|
||||
return r.buf
|
||||
@@ -70,7 +61,6 @@ function Bmp.sniff(r: Bytes.Reader): number
|
||||
if r:peek(2) ~= "BM" then
|
||||
return 0
|
||||
end
|
||||
-- a known header size: a strong match; "BM" alone is weak
|
||||
local n = r:peekU8(14) + math.max(r:peekU8(15), 0) * 256
|
||||
if r:peekU8(16) == 0 and r:peekU8(17) == 0 and (n == 12 or n == 16 or n == 40 or n == 52 or n == 56 or n == 64 or n == 108 or n == 124) then
|
||||
return 0.9
|
||||
@@ -79,7 +69,6 @@ function Bmp.sniff(r: Bytes.Reader): number
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ decode
|
||||
-- Header parse: info, palette start offset, palette entry size, header-provided masks (or nil) | nil, err
|
||||
local function parseHeader(buf: buffer): (Info?, any)
|
||||
local n = buffer.len(buf)
|
||||
if n < 18 then
|
||||
@@ -127,13 +116,11 @@ local function parseHeader(buf: buffer): (Info?, any)
|
||||
return info
|
||||
end
|
||||
|
||||
-- info(r) -> Info | nil, Err: the header without decoding the pixels
|
||||
function Bmp.info(r: Bytes.Reader): (Info?, Err.Err?)
|
||||
return parseHeader(whole(r))
|
||||
end
|
||||
|
||||
-- u8 table of size (m + 1) mapping a masked value 0..m to 0..255: v * 255 // m, as Pillow does (browsers replicate
|
||||
-- the top bits instead, which can differ by 1). m = 0 -> { empty }.
|
||||
-- v * 255 // m, as Pillow (browsers replicate the top bits, which can differ by 1)
|
||||
local function scaleTable(m: number, empty: number): buffer
|
||||
local t = buffer.create(m + 1)
|
||||
if m == 0 then
|
||||
@@ -146,7 +133,7 @@ local function scaleTable(m: number, empty: number): buffer
|
||||
return t
|
||||
end
|
||||
|
||||
-- shift, mask >> shift, table for one channel mask (wider than 16 bits: keep the top 16)
|
||||
-- a mask wider than 16 bits keeps its top 16
|
||||
local function channel(mask: number, empty: number): (number, number, buffer)
|
||||
if mask == 0 then
|
||||
return 0, 0, scaleTable(0, empty)
|
||||
@@ -166,7 +153,6 @@ local function channel(mask: number, empty: number): (number, number, buffer)
|
||||
return sh, m, scaleTable(m, empty)
|
||||
end
|
||||
|
||||
-- decode(r, opts?) -> rgba8 Image | nil, Err
|
||||
function Bmp.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Image?, Err.Err?)
|
||||
local buf = whole(r)
|
||||
local info, herr = parseHeader(buf)
|
||||
@@ -236,7 +222,6 @@ function Bmp.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Image?, Err.E
|
||||
end
|
||||
end
|
||||
|
||||
-- palette
|
||||
local entrySize = if hs == 12 then 3 else 4
|
||||
local declared = if hs >= 36 then readu32(buf, 46) else 0
|
||||
local palCount = 0
|
||||
@@ -334,7 +319,6 @@ function Bmp.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Image?, Err.E
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ encode
|
||||
-- encode(img, opts?) -> buffer | nil, Err
|
||||
function Bmp.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, "encode: needs an rgba8 Image")
|
||||
|
||||
Reference in new issue
Block a user