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