Files
rosetta/src/Rosetta/layered/psd/Channels.luau
T

370 lines
12 KiB
Luau

--!strict
--!native
-- layered/psd/Channels: PSD / PSB channel data. Decodes one channel (raw, PackBits RLE, ZIP, ZIP with prediction) to a
-- plane of big-endian samples, converts planes to rgba8 for every colour mode, and PackBits-encodes rows for the
-- writer. Adobe Photoshop File Formats Specification (2016), "Channel image data" / "Image data section".
-- Never throws: every read is range-checked first; failures return nil, "message".
local Deflate = require("@rosetta/codecs/compress/Deflate")
local PackBits = require("@rosetta/codecs/compress/PackBits")
local Channels = {}
local readu8, writeu8, readu16, writeu16, readu32, writeu32 = buffer.readu8, buffer.writeu8, buffer.readu16, buffer.writeu16, buffer.readu32, buffer.writeu32
local readf32, writef32, bcreate, bcopy, blen, bfill = buffer.readf32, buffer.writef32, buffer.create, buffer.copy, buffer.len, buffer.fill
local floor, min, max = math.floor, math.min, math.max
local byteswap = bit32.byteswap
-- rowBytes(w, depth) -> bytes per row of one channel
function Channels.rowBytes(w: number, depth: number): number
if depth == 1 then
return (w + 7) // 8
end
return w * (depth // 8)
end
-- rleCounts(src, p, limit, rows, psb) -> { counts }, offset after them | nil
function Channels.rleCounts(src: buffer, p: number, limit: number, rows: number, psb: boolean): ({ number }?, number)
local size = if psb then 4 else 2
if rows < 0 or p + rows * size > limit then
return nil, p
end
local counts: { number } = table.create(rows) :: { number }
for i = 1, rows do
if psb then
counts[i] = byteswap(readu32(src, p))
else
counts[i] = readu8(src, p) * 256 + readu8(src, p + 1)
end
p += size
end
return counts, p
end
-- rle(src, p, limit, counts, first, rows, rowBytes, dst, d) -> offset after the rows | nil, message
function Channels.rle(src: buffer, p: number, limit: number, counts: { number }, first: number, rows: number, rowBytes: number, dst: buffer, d: number): (number?, string?)
for i = 0, rows - 1 do
local c = counts[first + i] or 0
if p + c > limit then
return nil, "RLE row past the end"
end
local _, _, clean = PackBits.decode(src, p, p + c, dst, d + i * rowBytes, rowBytes)
if not clean then
-- a short or overlong row: keep what decoded, zero the rest (Photoshop is lenient too)
return nil, "bad RLE row"
end
p += c
end
return p, nil
end
-- unpredict(plane, w, h, depth): undo ZIP-with-prediction deltas in place
local function unpredict(plane: buffer, w: number, h: number, depth: number)
if depth == 8 then
for y = 0, h - 1 do
local o = y * w
local acc = readu8(plane, o)
for x = 1, w - 1 do
acc = (acc + readu8(plane, o + x)) % 256
writeu8(plane, o + x, acc)
end
end
elseif depth == 16 then
for y = 0, h - 1 do
local o = y * w * 2
local acc = readu8(plane, o) * 256 + readu8(plane, o + 1)
for x = 1, w - 1 do
local q = o + x * 2
acc = (acc + readu8(plane, q) * 256 + readu8(plane, q + 1)) % 65536
writeu8(plane, q, acc // 256)
writeu8(plane, q + 1, acc % 256)
end
end
elseif depth == 32 then
-- byte deltas over the row, then the row is 4 byte planes (all high bytes first)
local rb = w * 4
local tmp = bcreate(rb)
for y = 0, h - 1 do
local o = y * rb
local acc = readu8(plane, o)
for x = 1, rb - 1 do
acc = (acc + readu8(plane, o + x)) % 256
writeu8(plane, o + x, acc)
end
for x = 0, w - 1 do
writeu8(tmp, x * 4, readu8(plane, o + x))
writeu8(tmp, x * 4 + 1, readu8(plane, o + w + x))
writeu8(tmp, x * 4 + 2, readu8(plane, o + 2 * w + x))
writeu8(tmp, x * 4 + 3, readu8(plane, o + 3 * w + x))
end
bcopy(plane, o, tmp, 0, rb)
end
end
end
-- channel(src, p, limit, w, h, depth, psb) -> plane (h rows of rowBytes) | nil, message. src[p, limit) holds the
-- 2-byte compression code and the data of one layer channel.
function Channels.channel(src: buffer, p: number, limit: number, w: number, h: number, depth: number, psb: boolean): (buffer?, string?)
local rb = Channels.rowBytes(w, depth)
local plane = bcreate(rb * h)
if w == 0 or h == 0 then
return plane, nil
end
if p + 2 > limit then
return nil, "channel data past the end"
end
local comp = readu8(src, p) * 256 + readu8(src, p + 1)
p += 2
if comp == 0 then
if p + rb * h > limit then
return nil, "raw channel data past the end"
end
bcopy(plane, 0, src, p, rb * h)
elseif comp == 1 then
local counts, q = Channels.rleCounts(src, p, limit, h, psb)
if not counts then
return nil, "RLE row counts past the end"
end
local ok, err = Channels.rle(src, q, limit, counts, 1, h, rb, plane, 0)
if not ok then
return nil, err
end
elseif comp == 2 or comp == 3 then
local out, err = Deflate.zlib(src, p, limit - p, rb * h)
if not out then
return nil, "ZIP channel: " .. tostring(err)
end
plane = out
if comp == 3 then
unpredict(plane, w, h, depth)
end
else
return nil, "unknown channel compression " .. comp
end
return plane, nil
end
-- sample8(plane, i, depth) -> sample i as 0..255
local function sample8(plane: buffer, i: number, depth: number): number
if depth == 8 then
return readu8(plane, i)
elseif depth == 16 then
return floor((readu8(plane, i * 2) * 256 + readu8(plane, i * 2 + 1)) / 257 + 0.5)
elseif depth == 32 then
local f = readf32(plane, i * 4) -- the plane holds big-endian floats: swapped by the caller
if f ~= f then
return 0
end
return floor(max(0, min(1, f)) * 255 + 0.5)
end
-- 1-bit (rows are padded, so this is only right for whole-byte widths; toRGBA reads bitmaps itself): 1 = black
return if readu8(plane, i // 8) // 2 ^ (7 - i % 8) % 2 == 1 then 0 else 255
end
-- swap32(plane): big-endian f32 samples to native order, in place
function Channels.swap32(plane: buffer)
for i = 0, blen(plane) // 4 - 1 do
writeu32(plane, i * 4, byteswap(readu32(plane, i * 4)))
end
end
-- lab(L, a, b) 0..255 each (PSD Lab) -> sRGB r, g, b 0..255 (D50 -> D65 approximated by D65 white)
local function srgb(c: number): number
c = if c <= 0.0031308 then 12.92 * c else 1.055 * c ^ (1 / 2.4) - 0.055
return floor(max(0, min(1, c)) * 255 + 0.5)
end
local function lab(l8: number, a8: number, b8: number): (number, number, number)
local L = l8 * 100 / 255
local a, b = a8 - 128, b8 - 128
local fy = (L + 16) / 116
local fx = fy + a / 500
local fz = fy - b / 200
local function f(t: number): number
return if t > 6 / 29 then t * t * t else 3 * (6 / 29) ^ 2 * (t - 4 / 29)
end
local X, Y, Z = 0.95047 * f(fx), f(fy), 1.08883 * f(fz)
local r = 3.2404542 * X - 1.5371385 * Y - 0.4985314 * Z
local g = -0.969266 * X + 1.8760108 * Y + 0.041556 * Z
local bl = 0.0556434 * X - 0.2040259 * Y + 1.0572252 * Z
return srgb(r), srgb(g), srgb(bl)
end
-- toRGBA(planes, w, h, depth, mode, palette?, dst?) -> rgba8 buffer. planes: { [1..k] = colour planes, alpha = plane? }
-- mode: PSD colour mode (0 bitmap, 1 grey, 2 indexed, 3 RGB, 4 CMYK, 7 multichannel, 8 duotone, 9 Lab)
function Channels.toRGBA(planes: { [any]: buffer }, w: number, h: number, depth: number, mode: number, palette: buffer?): buffer
local n = w * h
local out = bcreate(n * 4)
local c1, c2, c3, c4 = planes[1], planes[2], planes[3], planes[4]
local alpha = planes.alpha
local bitmapW = (w + 7) // 8
if depth == 8 and (mode == 3 or mode == 1) and c1 and blen(c1) >= n then
-- the common case, without a call per sample
local g8 = if mode == 3 and c2 and blen(c2) >= n then c2 else c1
local b8 = if mode == 3 and c3 and blen(c3) >= n then c3 else c1
local a8 = if alpha and blen(alpha) >= n then alpha else nil
for i = 0, n - 1 do
local a = if a8 then readu8(a8, i) else 255
writeu32(out, i * 4, readu8(c1, i) + readu8(g8, i) * 256 + readu8(b8, i) * 65536 + a * 16777216)
end
return out
end
for i = 0, n - 1 do
local r, g, b = 0, 0, 0
if mode == 3 then
r = if c1 then sample8(c1, i, depth) else 0
g = if c2 then sample8(c2, i, depth) else r
b = if c3 then sample8(c3, i, depth) else r
elseif mode == 4 then
local cc = if c1 then sample8(c1, i, depth) else 255
local mm = if c2 then sample8(c2, i, depth) else 255
local yy = if c3 then sample8(c3, i, depth) else 255
local kk = if c4 then sample8(c4, i, depth) else 255
r = floor(cc * kk / 255 + 0.5)
g = floor(mm * kk / 255 + 0.5)
b = floor(yy * kk / 255 + 0.5)
elseif mode == 2 then
local k = if c1 then readu8(c1, i) else 0
if palette and blen(palette) >= 768 then
r = readu8(palette, k)
g = readu8(palette, 256 + k)
b = readu8(palette, 512 + k)
else
r, g, b = k, k, k
end
elseif mode == 0 then
local y, x = i // w, i % w
local bit = if c1 then readu8(c1, y * bitmapW + x // 8) // 2 ^ (7 - x % 8) % 2 else 0
r = if bit == 1 then 0 else 255
g, b = r, r
elseif mode == 9 then
local L = if c1 then sample8(c1, i, depth) else 0
local A = if c2 then sample8(c2, i, depth) else 128
local B = if c3 then sample8(c3, i, depth) else 128
r, g, b = lab(L, A, B)
else -- grey, duotone, multichannel: the first channel as grey
r = if c1 then sample8(c1, i, depth) else 0
g, b = r, r
end
local a = if alpha then sample8(alpha, i, depth) else 255
writeu32(out, i * 4, r + g * 256 + b * 65536 + a * 16777216)
end
return out
end
-- gray8(plane, w, h, depth) -> 1 byte per pixel
function Channels.gray8(plane: buffer, w: number, h: number, depth: number): buffer
local n = w * h
local out = bcreate(n)
for i = 0, n - 1 do
writeu8(out, i, sample8(plane, i, depth))
end
return out
end
-- unmatte(rgba, n): Photoshop stores a transparent merged image matted with white (C * a + 255 * (1 - a)): undo it
function Channels.unmatte(rgba: buffer, n: number)
for i = 0, n - 1 do
local o = i * 4
local a = readu8(rgba, o + 3)
if a == 0 then
writeu32(rgba, o, 0)
elseif a < 255 then
local f = a / 255
for k = 0, 2 do
local v = (readu8(rgba, o + k) - 255 * (1 - f)) / f
writeu8(rgba, o + k, floor(max(0, min(255, v)) + 1e-6))
end
end
end
end
-- matte(rgba, n) -> a copy matted with white (the writer's merged image)
function Channels.matte(rgba: buffer, n: number): buffer
local out = bcreate(n * 4)
bcopy(out, 0, rgba, 0, n * 4)
for i = 0, n - 1 do
local o = i * 4
local a = readu8(out, o + 3)
if a < 255 then
local f = a / 255
for k = 0, 2 do
writeu8(out, o + k, floor(readu8(out, o + k) * f + 255 * (1 - f) + 0.5))
end
end
end
return out
end
-- ------------------------------------------------------------------------------------------------ writer helpers
-- packRow(src, o, n, out, d) -> bytes written: PackBits of src[o, o + n) into out at d (out needs n + n // 128 + 1)
function Channels.packRow(src: buffer, o: number, n: number, out: buffer, d: number): number
local start = d
local i = 0
while i < n do
-- a run of at least 3 equal bytes?
local v = readu8(src, o + i)
local run = 1
while i + run < n and run < 128 and readu8(src, o + i + run) == v do
run += 1
end
if run >= 3 then
writeu8(out, d, 257 - run)
writeu8(out, d + 1, v)
d += 2
i += run
else
-- literals up to the next run of 3
local j = i
while j < n and j - i < 128 do
if j + 2 < n and readu8(src, o + j) == readu8(src, o + j + 1) and readu8(src, o + j) == readu8(src, o + j + 2) then
break
end
j += 1
end
local c = j - i
writeu8(out, d, c - 1)
bcopy(out, d + 1, src, o + i, c)
d += c + 1
i = j
end
end
return d - start
end
-- planes8(rgba, w, h) -> r, g, b, a planes (1 byte per pixel each)
function Channels.planes8(rgba: buffer, w: number, h: number): (buffer, buffer, buffer, buffer)
local n = w * h
local r, g, b, a = bcreate(n), bcreate(n), bcreate(n), bcreate(n)
if blen(rgba) < n * 4 then
return r, g, b, a
end
for i = 0, n - 1 do
local v = readu32(rgba, i * 4)
writeu8(r, i, v % 256)
writeu8(g, i, v // 256 % 256)
writeu8(b, i, v // 65536 % 256)
writeu8(a, i, v // 16777216)
end
return r, g, b, a
end
-- rleChannel(plane, w, h, psb) -> the RLE data of one channel (counts, then rows), without the compression code
function Channels.rleChannel(plane: buffer, w: number, h: number, psb: boolean): buffer
local cs = if psb then 4 else 2
local out = bcreate(h * cs + h * (w + w // 128 + 2))
local d = h * cs
for y = 0, h - 1 do
local n = Channels.packRow(plane, y * w, w, out, d)
if psb then
writeu32(out, y * 4, byteswap(n))
else
writeu16(out, y * 2, byteswap(n) // 65536)
end
d += n
end
local res = bcreate(d)
bcopy(res, 0, out, 0, d)
return res
end
return Channels