ws-cleanup: codecs/compress/PackBits; UTF-16 and half floats from one place
- codecs/compress/PackBits (new, from image/tiff/Pixels): decode (lenient count + a clean flag for strict callers) and encode; image/tiff and layered/psd (which carried its own decoder) use it. tests/codecs/packbits. - codecs/text/Text.utf16(buf, offset, length, bigEndian?): the UTF-16 -> UTF-8 conversion codecs/font/TrueType, archive/7z and layered/psd each wrote (lone surrogates -> U+FFFD everywhere now). - image/tiff/Pixels and data/cbor use core/Bytes.half instead of their own float16 decoders.
This commit is contained in:
1 parent
51b08575c4
commit
44a599f00c
13 files changed
+191
-178
No files matched your search
@@ -1,10 +1,11 @@
|
||||
--!strict
|
||||
--!native
|
||||
--!optimize 2
|
||||
-- image/tiff/Pixels: the hot loops of the TIFF codec (native; never throws): PackBits, the horizontal (2) and
|
||||
-- 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.
|
||||
local Bytes = require("@formats/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
|
||||
@@ -40,71 +41,6 @@ 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
|
||||
@@ -212,17 +148,7 @@ function Pixels.predict3(b: buffer, off: number, rows: number, rb: number, bytes
|
||||
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
|
||||
local half = Bytes.half
|
||||
|
||||
export type Params = {
|
||||
planes: { buffer },
|
||||
|
||||
@@ -18,6 +18,7 @@ local Types = require("@formats/Types")
|
||||
local Deflate = require("@formats/codecs/compress/Deflate")
|
||||
local Lzw = require("@formats/codecs/compress/Lzw")
|
||||
local Pixels = require("@formats/image/tiff/Pixels")
|
||||
local PackBits = require("@formats/codecs/compress/PackBits")
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ contract
|
||||
local Tiff = {
|
||||
@@ -25,7 +26,7 @@ local Tiff = {
|
||||
category = "image",
|
||||
extensions = { "tif", "tiff" },
|
||||
mime = { "image/tiff" },
|
||||
deps = { "core/Err", "core/Bytes", "Types", "codecs/compress/Deflate", "codecs/compress/Lzw" },
|
||||
deps = { "core/Err", "core/Bytes", "Types", "codecs/compress/Deflate", "codecs/compress/Lzw", "codecs/compress/PackBits" },
|
||||
caps = { decode = true, encode = true, alpha = true, pages = true, depths = { 1, 2, 4, 8, 16, 32 }, float = true },
|
||||
}
|
||||
|
||||
@@ -237,7 +238,7 @@ local function inflateChunk(c: Ctx, comp: number, p: number, len: number, out: b
|
||||
buffer.copy(out, o, raw :: buffer, 0, n)
|
||||
return n, nil
|
||||
elseif comp == 32773 then
|
||||
return Pixels.packbits(src, sp, len, out, o, size), nil
|
||||
return (PackBits.decode(src, sp, sp + len, out, o, size)), nil
|
||||
end
|
||||
return nil, Err.new(Err.UNSUPPORTED, "compression " .. comp)
|
||||
end
|
||||
@@ -671,7 +672,7 @@ local function encodePage(img: Types.Image, o: EncodeOptions, comp: number): ({
|
||||
else
|
||||
local parts, total = {}, 0
|
||||
for r = 0, rows - 1 do
|
||||
local p = Pixels.packbitsEncode(raw, r * rb, rb)
|
||||
local p = PackBits.encode(raw, r * rb, rb)
|
||||
table.insert(parts, p)
|
||||
total += buffer.len(p)
|
||||
end
|
||||
|
||||
Reference in new issue
Block a user