Files
rosetta/src/Rosetta/image/tiff/Pixels.luau
T

389 lines
11 KiB
Luau

--!strict
--!native
--!optimize 2
-- 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
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: 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
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
-- ------------------------------------------------------------------------------------------------ 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
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
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
-- 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
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
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 half = Bytes.half
export type Params = {
planes: { buffer },
rb: number,
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,
assoc: boolean, -- premultiplied alpha (undone)
cmap: buffer?, -- palette: 3 * 2^bps u16 (red block, green block, blue block)
kind: string,
out: buffer,
}
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
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)
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