image formats: comments are specs only

This commit is contained in:
4DBug committed 2026-11-01 01:54:53 -05:00
1 parent 0bd7b87d2b
commit b5546e208e
40 files changed
+246 -626

No files matched your search

+6 -17
View File
@@ -1,10 +1,7 @@
--!strict
--!native
--!optimize 2
-- image/tiff/Pixels: the hot loops of the TIFF codec (native; never throws): the horizontal (2) and
-- floating-point (3) predictors both ways (TIFF 6.0 section 14, Adobe TIFF technote 3; predictor 3 as libtiff's
-- fpAcc / fpDiff), fill-order bit reversal, and the sample converter from decoded planes to a Types.Image layout.
-- Callers (image/tiff/init) size every buffer before calling: planes hold h rows of rb bytes plus 8 bytes of slack.
-- image/tiff/Pixels: TIFF predictors 2 / 3 (TIFF 6.0 section 14, technote 3; 3 as libtiff fpAcc / fpDiff).
local Bytes = require("@rosetta/core/Bytes")
local readu8, writeu8, readu16, writeu16, readu32, writeu32 = buffer.readu8, buffer.writeu8, buffer.readu16, buffer.writeu16, buffer.readu32, buffer.writeu32
local readf32, writef32, readf64 = buffer.readf32, buffer.writef32, buffer.readf64
@@ -15,8 +12,7 @@ local Pixels = {}
local scratch = buffer.create(8)
-- floats read through their bits: NaN / infinity never load from memory (LuaJIT's FFI would hand back a NaN with an
-- arbitrary payload, which its NaN-tagged values misread as a table / userdata)
-- floats read through their bits: LuaJIT's FFI can hand back a NaN with a payload its NaN-tagging misreads
local function f32(bits: number): number
if band(bits, 0x7F800000) == 0x7F800000 then
if band(bits, 0x7FFFFF) ~= 0 then
@@ -59,7 +55,6 @@ function Pixels.reverseBits(b: buffer, off: number, len: number)
end
-- ------------------------------------------------------------------------------------------------ predictors
-- unpredict2(b, off, rows, rb, stride, bps, le): undo horizontal differencing (bps 8, 16 or 32; others untouched)
function Pixels.unpredict2(b: buffer, off: number, rows: number, rb: number, stride: number, bps: number, le: boolean)
for r = 0, rows - 1 do
local base = off + r * rb
@@ -95,7 +90,6 @@ function Pixels.unpredict2(b: buffer, off: number, rows: number, rb: number, str
end
end
-- predict2(b, off, rows, rb, stride, bps): apply horizontal differencing to little-endian samples (8 / 16 bit)
function Pixels.predict2(b: buffer, off: number, rows: number, rb: number, stride: number, bps: number)
for r = 0, rows - 1 do
local base = off + r * rb
@@ -112,8 +106,7 @@ function Pixels.predict2(b: buffer, off: number, rows: number, rb: number, strid
end
end
-- unpredict3(b, off, rows, rb, bytes, stride, tmp): undo the floating-point predictor; the samples come out little
-- endian whatever the file's byte order (libtiff fpAcc). tmp holds at least rb bytes.
-- the samples come out little endian whatever the file's byte order (libtiff fpAcc)
function Pixels.unpredict3(b: buffer, off: number, rows: number, rb: number, bytes: number, stride: number, tmp: buffer)
local wc = rb // bytes
for r = 0, rows - 1 do
@@ -130,7 +123,6 @@ function Pixels.unpredict3(b: buffer, off: number, rows: number, rb: number, byt
end
end
-- predict3(b, off, rows, rb, bytes, stride, tmp): the floating-point predictor on little-endian samples (fpDiff)
function Pixels.predict3(b: buffer, off: number, rows: number, rb: number, bytes: number, stride: number, tmp: buffer)
local wc = rb // bytes
for r = 0, rows - 1 do
@@ -152,7 +144,7 @@ local half = Bytes.half
export type Params = {
planes: { buffer },
rb: number, -- bytes per row in each plane
rb: number,
chunky: boolean,
w: number,
h: number,
@@ -161,14 +153,13 @@ export type Params = {
sfmt: number, -- 1 uint, 2 int, 3 float
le: boolean,
photo: number, -- 0 min-is-white, 1 min-is-black, 2 RGB, 3 palette, 5 CMYK
alpha: number, -- sample index of the alpha channel, -1 = none
alpha: number,
assoc: boolean, -- premultiplied alpha (undone)
cmap: buffer?, -- palette: 3 * 2^bps u16 (red block, green block, blue block)
kind: string, -- the output pixel format
kind: string,
out: buffer,
}
-- the common 8-bit chunky cases without per-sample dispatch; false when P is not one of them
local function fast(P: Params): boolean
if not P.chunky or P.bps ~= 8 or P.sfmt ~= 1 or P.assoc then
return false
@@ -218,7 +209,6 @@ local function fast(P: Params): boolean
return false
end
-- convert(P): every pixel into P.out in P.kind's layout
function Pixels.convert(P: Params)
if fast(P) then
return
@@ -229,7 +219,6 @@ function Pixels.convert(P: Params)
local float = sfmt == 3
local signed = sfmt == 2
local cmapN = 2 ^ math.min(bps, 16)
-- raw sample s of pixel x in the row starting at byte ro of its plane
local function raw(s: number, x: number, row: number): number
local b, i
if chunky then
+13 -30
View File
@@ -1,15 +1,7 @@
--!strict
-- image/tiff: TIFF 6.0 (+ Adobe technote 3 floating point, BigTIFF). New code from the TIFF 6.0 specification.
-- Decode: both byte orders, classic and BigTIFF, strips and tiles, planar configuration 1 and 2, compression none /
-- LZW (codecs/compress/Lzw, also old-style) / Deflate (8 and 32946, codecs/compress/Deflate) / PackBits, JPEG (7) through the api's
-- opts.decodeImage hook (JPEGTables stitched to each strip / tile), predictors 2 (8 / 16 / 32-bit) and 3 (float),
-- 1..32-bit unsigned / signed integer and 16 / 32 / 64-bit float samples, min-is-white / min-is-black, RGB, palette,
-- CMYK (naive conversion), extra samples (associated alpha is un-premultiplied), fill order 2, every page as
-- img.frames, resolution (meta.dpi), ICC (meta.icc). Decodes to rgba8, or with opts.native to the nearest standard
-- layout (gray8, gray16, graya8, rgb8, rgba8, rgba16, rgbaf32).
-- Encode: rgba8 / rgb8 / gray8 / graya8 / gray16 / rgba16 / rgbaf32 as the matching TIFF layout, compression none /
-- deflate / lzw / packbits with predictor 2 (integers) or 3 (floats), strips, multi-page from img.frames.
-- Not supported: old-style JPEG (6), CCITT fax (2, 3, 4), uncompressed YCbCr with subsampling, CIELab, JBIG.
-- image/tiff: TIFF 6.0 (+ Adobe technote 3, BigTIFF). Supports: strips, tiles, planar 1 / 2, none / LZW (old-style too)
-- / Deflate (8, 32946) / PackBits / JPEG (7), predictors 2 / 3, int / float samples, CMYK (naive), fill order 2. Not
-- supported: old-style JPEG (6), CCITT (2, 3, 4), YCbCr, CIELab, JBIG.
-- ------------------------------------------------------------------------------------------------ requires
local Err = require("@rosetta/core/Err")
@@ -32,16 +24,16 @@ local Tiff = {
-- ------------------------------------------------------------------------------------------------ types
export type DecodeOptions = {
native: boolean?, -- keep the source layout (gray8 / gray16 / graya8 / rgb8 / rgba8 / rgba16 / rgbaf32); default rgba8
maxPixels: number?, -- per page (default 2^26)
maxPages: number?, -- pages decoded into img.frames (default 1024)
page: number?, -- decode only this page (1-based)
decodeImage: ((buffer) -> (any, any))?, -- the api's hook, for JPEG-compressed pages
native: boolean?, -- default false: rgba8
maxPixels: number?, -- per page, default 2^26
maxPages: number?, -- default 1024
page: number?, -- 1-based
decodeImage: ((buffer) -> (any, any))?,
}
export type EncodeOptions = {
compression: ("none" | "deflate" | "lzw" | "packbits")?, -- default "deflate"
predictor: boolean?, -- horizontal / float predictor with deflate and lzw (default true)
level: number?, -- deflate level 0..9 (default 6)
predictor: boolean?, -- default true
level: number?, -- 0..9, default 6
rowsPerStrip: number?, -- default: about 64 KB per strip
}
@@ -90,7 +82,6 @@ local function off(c: Ctx, at: number): number
return if c.big then u64(c, at) else u32(c, at)
end
-- values(c, e, max) -> the entry's numbers (rationals divided) | nil when out of range / too many
local function values(c: Ctx, e: Entry?, max: number?): { number }?
if not e then
return nil
@@ -155,7 +146,6 @@ local function one(c: Ctx, e: Entry?, default: number): number
return if v and v[1] then v[1] else default
end
-- the IFD at absolute offset p: { [tag] = Entry }, next IFD offset (absolute, 0 = none) | nil, Err
local function readIfd(c: Ctx, p: number): ({ [number]: Entry }?, number, Err.Err?)
local esize = if c.big then 20 else 12
local hsize = if c.big then 8 else 2
@@ -203,7 +193,6 @@ local function bytesOf(c: Ctx, e: Entry?, max: number): buffer?
return x
end
-- decompress one chunk at [p, p + len) into out[o, o + size): bytes produced | nil, Err
local function inflateChunk(c: Ctx, comp: number, p: number, len: number, out: buffer, o: number, size: number, rev: boolean): (number?, Err.Err?)
if p < c.base or len < 0 or p + len > c.limit then
-- a chunk past the end: keep what exists (truncated files)
@@ -308,10 +297,10 @@ local function decodePage(c: Ctx, t: { [number]: Entry }, opts: DecodeOptions?):
if ch > h and not tiled then
ch = h
end
local pspp = if chunky then spp else 1 -- samples per pixel in one plane
local pspp = if chunky then spp else 1
local nplanes = if chunky then 1 else spp
local rb = math.ceil(w * pspp * bps / 8) -- bytes per image row in a plane
local crb = math.ceil(cw * pspp * bps / 8) -- bytes per chunk row
local rb = math.ceil(w * pspp * bps / 8)
local crb = math.ceil(cw * pspp * bps / 8)
if rb * h * nplanes > 2 ^ 30 or crb * ch > 2 ^ 30 then
return nil, Err.new(Err.LIMIT, "decoded data over 1 GB")
end
@@ -382,7 +371,6 @@ local function decodePage(c: Ctx, t: { [number]: Entry }, opts: DecodeOptions?):
elseif predictor == 3 and bytesPer >= 2 and crb % bytesPer == 0 then
Pixels.unpredict3(tmp, 0, rows, crb, bytesPer, pspp, ftmp)
end
-- into the plane
local plane = planes[pl + 1]
local x0b = math.floor(tx * cw * pspp * bps / 8)
local cols = math.min(cw, w - tx * cw)
@@ -402,7 +390,6 @@ local function decodePage(c: Ctx, t: { [number]: Entry }, opts: DecodeOptions?):
le = true -- fpAcc leaves the samples little endian
end
local colorType = PHOTOMETRIC[photo]
-- the output layout
local kind = "rgba8"
if opts and opts.native and comp ~= 7 then
if photo == 3 or photo == 5 then
@@ -506,7 +493,6 @@ local function decodePage(c: Ctx, t: { [number]: Entry }, opts: DecodeOptions?):
return img, nil
end
-- pages(r) -> the IFD tables of every page (and the context) | nil, Err
local function pages(r: Bytes.Reader, maxPages: number): ({ { [number]: Entry } }?, Ctx?, Err.Err?)
local b, base, limit = r.buf, r.base, r.limit
if limit - base < 8 then
@@ -551,7 +537,6 @@ local function pages(r: Bytes.Reader, maxPages: number): ({ { [number]: Entry }
return list, c, nil
end
-- info(r) -> { pages, width, height, bitDepth, samples, compression } of page 1 | nil, Err
function Tiff.info(r: Bytes.Reader): ({ [string]: any }?, Err.Err?)
local list, c, err = pages(r, 1024)
if not list or not c then
@@ -617,7 +602,6 @@ function Tiff.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Image?, Err.
end
-- ------------------------------------------------------------------------------------------------ encode
-- layout per pixel format: samples, bits, sample format, photometric, extra samples
local LAYOUT = {
gray8 = { 1, 8, 1, 1 },
graya8 = { 2, 8, 1, 1 },
@@ -764,7 +748,6 @@ function Tiff.encode(img: Types.Image, opts: EncodeOptions?): (buffer?, Err.Err?
if not tags or not strips then
return nil, err
end
-- strip data first
local soffs = {}
for i, s in strips do
if w:tell() % 2 == 1 then