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. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
434a304315
commit
9bcf061338
13 files changed
+191
-178
No files matched your search
@@ -0,0 +1,95 @@
|
||||
--!strict
|
||||
--!native
|
||||
--!optimize 2
|
||||
-- codecs/compress/PackBits: the Apple PackBits run-length code (TIFF compression 32773, PSD / PSB RLE channels, IFF
|
||||
-- ByteRun1, ...): a header byte n < 128 copies n + 1 literal bytes, n > 128 repeats the next byte 257 - n times,
|
||||
-- 128 is a no-op. Decode and encode; moved here from image/tiff (TIFF and PSD both carried a decoder). Never throws.
|
||||
--
|
||||
-- local written, srcEnd, clean = PackBits.decode(src, p, limit, dst, d, n) -- up to n bytes into dst at d
|
||||
-- local packed = PackBits.encode(src, offset, length) -- a buffer (one row at a time)
|
||||
local PackBits = {}
|
||||
|
||||
local readu8, writeu8 = buffer.readu8, buffer.writeu8
|
||||
|
||||
-- decode(src, p, limit, dst, d, n) -> bytes written (<= n), the source offset after the codes used, clean (false when
|
||||
-- the source ran out before n bytes or a run overflowed n: lenient callers keep what was written, strict ones fail)
|
||||
function PackBits.decode(src: buffer, p: number, limit: number, dst: buffer, d: number, n: number): (number, number, boolean)
|
||||
local o, lim = d, d + n
|
||||
local clean = true
|
||||
while o < lim do
|
||||
if p >= limit then
|
||||
clean = false
|
||||
break
|
||||
end
|
||||
local h = readu8(src, p)
|
||||
p += 1
|
||||
if h < 128 then
|
||||
local want = h + 1
|
||||
local cnt = math.min(want, limit - p, lim - o)
|
||||
if cnt < want then
|
||||
clean = false
|
||||
end
|
||||
if cnt > 0 then
|
||||
buffer.copy(dst, o, src, p, cnt)
|
||||
o += cnt
|
||||
end
|
||||
p += want
|
||||
if p > limit then
|
||||
p = limit
|
||||
break
|
||||
end
|
||||
elseif h > 128 then
|
||||
if p >= limit then
|
||||
clean = false
|
||||
break
|
||||
end
|
||||
local want = 257 - h
|
||||
local cnt = math.min(want, lim - o)
|
||||
if cnt < want then
|
||||
clean = false
|
||||
end
|
||||
buffer.fill(dst, o, readu8(src, p), cnt)
|
||||
p += 1
|
||||
o += cnt
|
||||
end
|
||||
end
|
||||
return o - d, p, clean
|
||||
end
|
||||
|
||||
-- encode(src, offset, length) -> buffer: runs of 3+ equal bytes as repeats, the rest as literals (at most 128 each)
|
||||
function PackBits.encode(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
|
||||
|
||||
return PackBits
|
||||
Reference in new issue
Block a user