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
|
||||
@@ -6,6 +6,7 @@
|
||||
-- domain / MIT) as a reference for cmap 4 and the GPOS walk.
|
||||
--
|
||||
-- Never throws: every read is range-checked (out of range reads as 0).
|
||||
local Text = require("@formats/codecs/text/Text")
|
||||
local Tables = {}
|
||||
|
||||
local readu8 = buffer.readu8
|
||||
@@ -423,24 +424,7 @@ local MAC_ROMAN_HIGH = {
|
||||
}
|
||||
|
||||
local function utf16be(b: buffer, o: number, n: number): string
|
||||
local out = {}
|
||||
local k = 0
|
||||
while k + 1 < n do
|
||||
local c = u16(b, o + k)
|
||||
k += 2
|
||||
if c >= 0xD800 and c < 0xDC00 and k + 1 < n then
|
||||
local lo = u16(b, o + k)
|
||||
if lo >= 0xDC00 and lo < 0xE000 then
|
||||
c = 0x10000 + (c - 0xD800) * 1024 + (lo - 0xDC00)
|
||||
k += 2
|
||||
end
|
||||
end
|
||||
if c >= 0xD800 and c < 0xE000 then
|
||||
c = 0xFFFD
|
||||
end
|
||||
table.insert(out, utf8.char(c))
|
||||
end
|
||||
return table.concat(out)
|
||||
return Text.utf16(b, o, n, true)
|
||||
end
|
||||
|
||||
local function macRoman(b: buffer, o: number, n: number): string
|
||||
|
||||
@@ -31,6 +31,34 @@ function Text.trim(s: string): string
|
||||
return (string.match(s, "^%s*(.-)%s*$") :: string)
|
||||
end
|
||||
|
||||
-- utf16(buf, offset, length, bigEndian?) -> UTF-8 text of UTF-16 code units (surrogate pairs joined, lone surrogates
|
||||
-- become U+FFFD; an odd last byte is ignored). Font names, 7z / PSD / Windows strings, XML in UTF-16.
|
||||
function Text.utf16(b: buffer, o: number, n: number, bigEndian: boolean?): string
|
||||
local out = {}
|
||||
local k = 0
|
||||
local readu16 = buffer.readu16
|
||||
local function unit(at: number): number
|
||||
local v = readu16(b, at)
|
||||
return if bigEndian then (v % 256) * 256 + v // 256 else v
|
||||
end
|
||||
while k + 1 < n do
|
||||
local c = unit(o + k)
|
||||
k += 2
|
||||
if c >= 0xD800 and c < 0xDC00 and k + 1 < n then
|
||||
local lo = unit(o + k)
|
||||
if lo >= 0xDC00 and lo < 0xE000 then
|
||||
c = 0x10000 + (c - 0xD800) * 1024 + (lo - 0xDC00)
|
||||
k += 2
|
||||
end
|
||||
end
|
||||
if c >= 0xD800 and c < 0xE000 then
|
||||
c = 0xFFFD
|
||||
end
|
||||
table.insert(out, Text.utf8char(c))
|
||||
end
|
||||
return table.concat(out)
|
||||
end
|
||||
|
||||
-- toUtf8(text, warnings?) -> text as UTF-8: strips a BOM; text that is not valid UTF-8 is read as Windows-1252
|
||||
function Text.toUtf8(text: string, warnings: { string }?): string
|
||||
if string.sub(text, 1, 3) == "\239\187\191" then
|
||||
|
||||
Reference in new issue
Block a user