474 lines
14 KiB
Luau
474 lines
14 KiB
Luau
--!strict
|
|
--!native
|
|
--!optimize 2
|
|
-- image/tiff/Pixels: the hot loops of the TIFF codec (native; never throws): PackBits, 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.
|
|
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
|
|
local band, bor, rshift, lshift, byteswap = bit32.band, bit32.bor, bit32.rshift, bit32.lshift, bit32.byteswap
|
|
local floor = math.floor
|
|
|
|
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)
|
|
local function f32(bits: number): number
|
|
if band(bits, 0x7F800000) == 0x7F800000 then
|
|
if band(bits, 0x7FFFFF) ~= 0 then
|
|
return 0 / 0
|
|
end
|
|
return if bits >= 0x80000000 then -math.huge else math.huge
|
|
end
|
|
writeu32(scratch, 0, bits)
|
|
return readf32(scratch, 0)
|
|
end
|
|
local function f64(lo: number, hi: number): number
|
|
if band(hi, 0x7FF00000) == 0x7FF00000 then
|
|
if band(hi, 0xFFFFF) ~= 0 or lo ~= 0 then
|
|
return 0 / 0
|
|
end
|
|
return if hi >= 0x80000000 then -math.huge else math.huge
|
|
end
|
|
writeu32(scratch, 0, lo)
|
|
writeu32(scratch, 4, hi)
|
|
return readf64(scratch, 0)
|
|
end
|
|
Pixels.f32 = f32
|
|
Pixels.f64 = f64
|
|
|
|
-- ------------------------------------------------------------------------------------------------ PackBits
|
|
-- packbits(src, off, len, out, outOff, max) -> bytes written (stops when out or src is exhausted)
|
|
function Pixels.packbits(src: buffer, off: number, len: number, out: buffer, outOff: number, max: number): number
|
|
local p, e = off, off + len
|
|
local o, lim = outOff, outOff + max
|
|
while p < e and o < lim do
|
|
local n = readu8(src, p)
|
|
p += 1
|
|
if n < 128 then
|
|
local cnt = math.min(n + 1, e - p, lim - o)
|
|
if cnt <= 0 then
|
|
break
|
|
end
|
|
buffer.copy(out, o, src, p, cnt)
|
|
p += n + 1
|
|
o += cnt
|
|
elseif n > 128 then
|
|
if p >= e then
|
|
break
|
|
end
|
|
local cnt = math.min(257 - n, lim - o)
|
|
buffer.fill(out, o, readu8(src, p), cnt)
|
|
p += 1
|
|
o += cnt
|
|
end
|
|
end
|
|
return o - outOff
|
|
end
|
|
|
|
-- packbitsEncode(src, off, len) -> buffer (one row at a time is the caller's business)
|
|
function Pixels.packbitsEncode(src: buffer, off: number, len: number): buffer
|
|
local out = buffer.create(len + (len + 127) // 128 + 8)
|
|
local o = 0
|
|
local p, e = off, off + len
|
|
while p < e do
|
|
local run = 1
|
|
local v = readu8(src, p)
|
|
while p + run < e and run < 128 and readu8(src, p + run) == v do
|
|
run += 1
|
|
end
|
|
if run >= 3 then
|
|
writeu8(out, o, 257 - run)
|
|
writeu8(out, o + 1, v)
|
|
o += 2
|
|
p += run
|
|
else
|
|
local s = p
|
|
local n = 0
|
|
while p < e and n < 128 do
|
|
if p + 2 < e and readu8(src, p) == readu8(src, p + 1) and readu8(src, p) == readu8(src, p + 2) then
|
|
break
|
|
end
|
|
p += 1
|
|
n += 1
|
|
end
|
|
writeu8(out, o, n - 1)
|
|
buffer.copy(out, o + 1, src, s, n)
|
|
o += n + 1
|
|
end
|
|
end
|
|
local r = buffer.create(o)
|
|
buffer.copy(r, 0, out, 0, o)
|
|
return r
|
|
end
|
|
|
|
-- ------------------------------------------------------------------------------------------------ fill order
|
|
local REV = buffer.create(256)
|
|
for i = 0, 255 do
|
|
local r = 0
|
|
for k = 0, 7 do
|
|
if band(i, lshift(1, k)) ~= 0 then
|
|
r = bor(r, lshift(1, 7 - k))
|
|
end
|
|
end
|
|
writeu8(REV, i, r)
|
|
end
|
|
function Pixels.reverseBits(b: buffer, off: number, len: number)
|
|
for i = off, off + len - 1 do
|
|
writeu8(b, i, readu8(REV, readu8(b, i)))
|
|
end
|
|
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
|
|
if bps == 8 then
|
|
for i = base + stride, base + rb - 1 do
|
|
writeu8(b, i, band(readu8(b, i) + readu8(b, i - stride), 255))
|
|
end
|
|
elseif bps == 16 then
|
|
local n = rb // 2
|
|
for k = stride, n - 1 do
|
|
local i = base + k * 2
|
|
local j = i - stride * 2
|
|
if le then
|
|
writeu16(b, i, band(readu16(b, i) + readu16(b, j), 0xFFFF))
|
|
else
|
|
local v = readu8(b, i) * 256 + readu8(b, i + 1) + readu8(b, j) * 256 + readu8(b, j + 1)
|
|
writeu8(b, i, band(rshift(v, 8), 255))
|
|
writeu8(b, i + 1, band(v, 255))
|
|
end
|
|
end
|
|
elseif bps == 32 then
|
|
local n = rb // 4
|
|
for k = stride, n - 1 do
|
|
local i = base + k * 4
|
|
local j = i - stride * 4
|
|
if le then
|
|
writeu32(b, i, (readu32(b, i) + readu32(b, j)) % 4294967296)
|
|
else
|
|
writeu32(b, i, byteswap((byteswap(readu32(b, i)) + byteswap(readu32(b, j))) % 4294967296))
|
|
end
|
|
end
|
|
end
|
|
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
|
|
if bps == 8 then
|
|
for i = base + rb - 1, base + stride, -1 do
|
|
writeu8(b, i, band(readu8(b, i) - readu8(b, i - stride), 255))
|
|
end
|
|
elseif bps == 16 then
|
|
for k = rb // 2 - 1, stride, -1 do
|
|
local i = base + k * 2
|
|
writeu16(b, i, band(readu16(b, i) - readu16(b, i - stride * 2), 0xFFFF))
|
|
end
|
|
end
|
|
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.
|
|
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
|
|
local base = off + r * rb
|
|
for i = base + stride, base + rb - 1 do
|
|
writeu8(b, i, band(readu8(b, i) + readu8(b, i - stride), 255))
|
|
end
|
|
buffer.copy(tmp, 0, b, base, rb)
|
|
for c = 0, wc - 1 do
|
|
for k = 0, bytes - 1 do
|
|
writeu8(b, base + c * bytes + k, readu8(tmp, (bytes - k - 1) * wc + c))
|
|
end
|
|
end
|
|
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
|
|
local base = off + r * rb
|
|
for c = 0, wc - 1 do
|
|
for k = 0, bytes - 1 do
|
|
writeu8(tmp, (bytes - k - 1) * wc + c, readu8(b, base + c * bytes + k))
|
|
end
|
|
end
|
|
buffer.copy(b, base, tmp, 0, rb)
|
|
for i = base + rb - 1, base + stride, -1 do
|
|
writeu8(b, i, band(readu8(b, i) - readu8(b, i - stride), 255))
|
|
end
|
|
end
|
|
end
|
|
|
|
-- ------------------------------------------------------------------------------------------------ samples
|
|
local function half(h: number): number
|
|
local s = if h >= 0x8000 then -1 else 1
|
|
local e = band(rshift(h, 10), 31)
|
|
local m = band(h, 1023)
|
|
if e == 0 then
|
|
return s * m * 2 ^ -24
|
|
elseif e == 31 then
|
|
return if m == 0 then s * math.huge else 0 / 0
|
|
end
|
|
return s * (1 + m / 1024) * 2 ^ (e - 15)
|
|
end
|
|
|
|
export type Params = {
|
|
planes: { buffer },
|
|
rb: number, -- bytes per row in each plane
|
|
chunky: boolean,
|
|
w: number,
|
|
h: number,
|
|
spp: number,
|
|
bps: number,
|
|
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
|
|
assoc: boolean, -- premultiplied alpha (undone)
|
|
cmap: buffer?, -- palette: 3 * 2^bps u16 (red block, green block, blue block)
|
|
kind: string, -- the output pixel format
|
|
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
|
|
end
|
|
local b, rb, w, h, spp, alpha, kind, out = P.planes[1], P.rb, P.w, P.h, P.spp, P.alpha, P.kind, P.out
|
|
local o = 0
|
|
if P.photo == 2 and spp >= 3 and (kind == "rgba8" or kind == "rgb8") then
|
|
local four = kind == "rgba8"
|
|
for y = 0, h - 1 do
|
|
local p = y * rb
|
|
for _ = 0, w - 1 do
|
|
writeu8(out, o, readu8(b, p))
|
|
writeu8(out, o + 1, readu8(b, p + 1))
|
|
writeu8(out, o + 2, readu8(b, p + 2))
|
|
if four then
|
|
writeu8(out, o + 3, if alpha >= 0 then readu8(b, p + alpha) else 255)
|
|
o += 4
|
|
else
|
|
o += 3
|
|
end
|
|
p += spp
|
|
end
|
|
end
|
|
return true
|
|
elseif (P.photo == 1 or P.photo == 0) and (kind == "rgba8" or kind == "gray8" or kind == "graya8") then
|
|
local inv = if P.photo == 0 then 255 else 0
|
|
for y = 0, h - 1 do
|
|
local p = y * rb
|
|
for _ = 0, w - 1 do
|
|
local v = math.abs(inv - readu8(b, p))
|
|
if kind == "gray8" then
|
|
writeu8(out, o, v)
|
|
o += 1
|
|
elseif kind == "graya8" then
|
|
writeu8(out, o, v)
|
|
writeu8(out, o + 1, if alpha >= 0 then readu8(b, p + alpha) else 255)
|
|
o += 2
|
|
else
|
|
writeu32(out, o, v * 0x10101 + (if alpha >= 0 then readu8(b, p + alpha) else 255) * 0x1000000)
|
|
o += 4
|
|
end
|
|
p += spp
|
|
end
|
|
end
|
|
return true
|
|
end
|
|
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
|
|
end
|
|
local planes, rb, chunky, w, h, spp, bps, sfmt, le = P.planes, P.rb, P.chunky, P.w, P.h, P.spp, P.bps, P.sfmt, P.le
|
|
local photo, alpha, assoc, cmap, kind, out = P.photo, P.alpha, P.assoc, P.cmap, P.kind, P.out
|
|
local maxv = 2 ^ math.min(bps, 32) - 1
|
|
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
|
|
b, i = planes[1], x * spp + s
|
|
else
|
|
b, i = planes[s + 1], x
|
|
end
|
|
local ro = row * rb
|
|
if bps == 8 then
|
|
local v = readu8(b, ro + i)
|
|
return if signed then (v + 128) % 256 else v
|
|
elseif bps == 16 then
|
|
local p = ro + i * 2
|
|
local v = if le then readu16(b, p) else readu8(b, p) * 256 + readu8(b, p + 1)
|
|
if float then
|
|
return half(v)
|
|
end
|
|
return if signed then (v + 32768) % 65536 else v
|
|
elseif bps == 32 then
|
|
local p = ro + i * 4
|
|
if float then
|
|
return f32(if le then readu32(b, p) else byteswap(readu32(b, p)))
|
|
end
|
|
local v = if le then readu32(b, p) else byteswap(readu32(b, p))
|
|
return if signed then (v + 2147483648) % 4294967296 else v
|
|
elseif bps == 64 then
|
|
local p = ro + i * 8
|
|
if le then
|
|
return f64(readu32(b, p), readu32(b, p + 4))
|
|
end
|
|
return f64(byteswap(readu32(b, p + 4)), byteswap(readu32(b, p)))
|
|
else
|
|
-- any other depth (1..31): MSB-first bit fields
|
|
local bit = i * bps
|
|
local v = 0
|
|
local need = bps
|
|
local p = ro + rshift(bit, 3)
|
|
local sh = band(bit, 7)
|
|
local cur = band(readu8(b, p), rshift(255, sh))
|
|
local avail = 8 - sh
|
|
while need > 0 do
|
|
if need >= avail then
|
|
v = v * 2 ^ avail + cur
|
|
need -= avail
|
|
p += 1
|
|
cur = if need > 0 then readu8(b, p) else 0
|
|
avail = 8
|
|
else
|
|
v = v * 2 ^ need + rshift(cur, avail - need)
|
|
need = 0
|
|
end
|
|
end
|
|
return v
|
|
end
|
|
end
|
|
-- normalised 0..1 (floats as stored)
|
|
local function norm(s: number, x: number, row: number): number
|
|
local v = raw(s, x, row)
|
|
if float then
|
|
return v
|
|
end
|
|
return v / maxv
|
|
end
|
|
local o = 0
|
|
local is8 = kind == "rgba8" or kind == "rgb8" or kind == "gray8" or kind == "graya8"
|
|
local is16 = kind == "rgba16" or kind == "gray16"
|
|
local nch = if kind == "rgba8" or kind == "rgba16" or kind == "rgbaf32" then 4 elseif kind == "rgb8" then 3 elseif kind == "graya8" then 2 else 1
|
|
for y = 0, h - 1 do
|
|
for x = 0, w - 1 do
|
|
local r, g, bl, a
|
|
if photo == 3 then
|
|
local idx = raw(0, x, y)
|
|
if cmap and idx < cmapN then
|
|
r = readu16(cmap, idx * 2) / 65535
|
|
g = readu16(cmap, (cmapN + idx) * 2) / 65535
|
|
bl = readu16(cmap, (2 * cmapN + idx) * 2) / 65535
|
|
else
|
|
r, g, bl = 0, 0, 0
|
|
end
|
|
elseif photo == 2 then
|
|
r = norm(0, x, y)
|
|
g = norm(1, x, y)
|
|
bl = norm(2, x, y)
|
|
elseif photo == 5 then
|
|
local c, m, ye, k = norm(0, x, y), norm(1, x, y), norm(2, x, y), norm(3, x, y)
|
|
r, g, bl = (1 - c) * (1 - k), (1 - m) * (1 - k), (1 - ye) * (1 - k)
|
|
else
|
|
local v = norm(0, x, y)
|
|
if photo == 0 then
|
|
v = 1 - v
|
|
end
|
|
r, g, bl = v, v, v
|
|
end
|
|
if alpha >= 0 then
|
|
a = norm(alpha, x, y)
|
|
if assoc then
|
|
if a > 0 then
|
|
r = math.min(r / a, 1)
|
|
g = math.min(g / a, 1)
|
|
bl = math.min(bl / a, 1)
|
|
else
|
|
r, g, bl = 0, 0, 0
|
|
end
|
|
end
|
|
else
|
|
a = 1
|
|
end
|
|
if is8 then
|
|
if r ~= r then
|
|
r = 0
|
|
end
|
|
if g ~= g then
|
|
g = 0
|
|
end
|
|
if bl ~= bl then
|
|
bl = 0
|
|
end
|
|
if a ~= a then
|
|
a = 0
|
|
end
|
|
local R = floor(math.clamp(r, 0, 1) * 255 + 0.5)
|
|
if nch == 1 then
|
|
writeu8(out, o, R)
|
|
elseif nch == 2 then
|
|
writeu8(out, o, R)
|
|
writeu8(out, o + 1, floor(math.clamp(a, 0, 1) * 255 + 0.5))
|
|
else
|
|
writeu8(out, o, R)
|
|
writeu8(out, o + 1, floor(math.clamp(g, 0, 1) * 255 + 0.5))
|
|
writeu8(out, o + 2, floor(math.clamp(bl, 0, 1) * 255 + 0.5))
|
|
if nch == 4 then
|
|
writeu8(out, o + 3, floor(math.clamp(a, 0, 1) * 255 + 0.5))
|
|
end
|
|
end
|
|
o += nch
|
|
elseif is16 then
|
|
if r ~= r then
|
|
r = 0
|
|
end
|
|
if g ~= g then
|
|
g = 0
|
|
end
|
|
if bl ~= bl then
|
|
bl = 0
|
|
end
|
|
if a ~= a then
|
|
a = 0
|
|
end
|
|
writeu16(out, o, floor(math.clamp(r, 0, 1) * 65535 + 0.5))
|
|
if nch == 4 then
|
|
writeu16(out, o + 2, floor(math.clamp(g, 0, 1) * 65535 + 0.5))
|
|
writeu16(out, o + 4, floor(math.clamp(bl, 0, 1) * 65535 + 0.5))
|
|
writeu16(out, o + 6, floor(math.clamp(a, 0, 1) * 65535 + 0.5))
|
|
end
|
|
o += nch * 2
|
|
else
|
|
writef32(out, o, r)
|
|
writef32(out, o + 4, g)
|
|
writef32(out, o + 8, bl)
|
|
writef32(out, o + 12, a)
|
|
o += 16
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
return Pixels
|