ws-arch2: archive/zoo (types 1 / 2, stored / LZW / lzh, CRC-16; read) and codecs/compress/Lzw 'zoo' variant (13-bit codes)
codecs/compress/Lzw: decode accepts variant "zoo" (gif layout, 8-bit symbols, up to 13-bit codes; its tables grow to 8192 entries); gif / tiff behaviour unchanged.
This commit is contained in:
1 parent
d0b81f02c7
commit
f1c2c6ed06
6 files changed
+466
-9
No files matched your search
@@ -8,9 +8,11 @@
|
||||
-- tiff MSB-first codes, 8-bit symbols, clear = 256, EOI = 257, "early change": the width grows one code
|
||||
-- earlier (next free code = 2^width - 1). Old-style (pre-6.0, LSB-first, no early change) TIFF LZW is
|
||||
-- detected by its first bytes (0x00 then an odd byte) and decoded too.
|
||||
-- zoo the gif layout with 8-bit symbols and codes up to 13 bits (Zoo archives' method 1; decode only).
|
||||
--
|
||||
-- local n, err = Lzw.decode(src, off, len, out, outOff, maxOut, "gif", minCodeSize) -- bytes written
|
||||
-- local n, err = Lzw.decode(src, off, len, out, outOff, maxOut, "tiff")
|
||||
-- local n, err = Lzw.decode(src, off, len, out, outOff, maxOut, "zoo")
|
||||
-- local codes = Lzw.encode(data, off, len, "gif", minCodeSize) -- buffer
|
||||
-- local codes = Lzw.encode(data, off, len, "tiff")
|
||||
--
|
||||
@@ -24,13 +26,14 @@ local blen = buffer.len
|
||||
|
||||
local Lzw = {}
|
||||
|
||||
local MAX_CODES = 4096
|
||||
local MAX_CODES = 4096 -- 12-bit codes (GIF, TIFF)
|
||||
local TABLE = 8192 -- the tables hold 13-bit codes (Zoo)
|
||||
|
||||
-- per-call tables (allocated once: decode / encode are not re-entrant across coroutines, but never yield)
|
||||
local prefix = buffer.create(MAX_CODES * 2) -- u16: the code this entry extends
|
||||
local suffix = buffer.create(MAX_CODES) -- u8: its last byte
|
||||
local first = buffer.create(MAX_CODES) -- u8: its first byte
|
||||
local length = buffer.create(MAX_CODES * 2) -- u16: its length
|
||||
local prefix = buffer.create(TABLE * 2) -- u16: the code this entry extends
|
||||
local suffix = buffer.create(TABLE) -- u8: its last byte
|
||||
local first = buffer.create(TABLE) -- u8: its first byte
|
||||
local length = buffer.create(TABLE * 2) -- u16: its length
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ decode
|
||||
-- writes the string of code c (length l) ending at out[o + l - 1]; clipped to [o, lim)
|
||||
@@ -66,6 +69,10 @@ function Lzw.decode(src: buffer, off: number, len: number, out: buffer, outOff:
|
||||
local msb = variant == "tiff"
|
||||
local early = 0 -- 1 for TIFF's early change
|
||||
local min = minCodeSize or 8
|
||||
local maxCodes, maxBits = MAX_CODES, 12
|
||||
if variant == "zoo" then
|
||||
min, maxCodes, maxBits = 8, TABLE, 13
|
||||
end
|
||||
if msb then
|
||||
min = 8
|
||||
early = 1
|
||||
@@ -94,7 +101,7 @@ function Lzw.decode(src: buffer, off: number, len: number, out: buffer, outOff:
|
||||
local p = off
|
||||
local acc, nb = 0, 0
|
||||
while true do
|
||||
-- refill (at most 12 + 7 bits held: below 2^20)
|
||||
-- refill (at most 13 + 7 bits held: below 2^21)
|
||||
while nb < size do
|
||||
if p >= e then
|
||||
return o - outOff, "truncated"
|
||||
@@ -140,14 +147,14 @@ function Lzw.decode(src: buffer, off: number, len: number, out: buffer, outOff:
|
||||
if c < nextc then
|
||||
l = readu16(length, c * 2)
|
||||
emit(out, o, c, l, lim)
|
||||
if nextc < MAX_CODES then
|
||||
if nextc < maxCodes then
|
||||
writeu16(prefix, nextc * 2, prev)
|
||||
writeu8(suffix, nextc, readu8(first, c))
|
||||
writeu8(first, nextc, readu8(first, prev))
|
||||
writeu16(length, nextc * 2, readu16(length, prev * 2) + 1)
|
||||
nextc += 1
|
||||
end
|
||||
elseif c == nextc and nextc < MAX_CODES then
|
||||
elseif c == nextc and nextc < maxCodes then
|
||||
-- KwKwK: the new entry is prev + first(prev)
|
||||
local f = readu8(first, prev)
|
||||
writeu16(prefix, nextc * 2, prev)
|
||||
@@ -163,7 +170,7 @@ function Lzw.decode(src: buffer, off: number, len: number, out: buffer, outOff:
|
||||
o = math.min(o + l, lim)
|
||||
prev = c
|
||||
end
|
||||
if nextc + early >= lshift(1, size) and size < 12 then
|
||||
if nextc + early >= lshift(1, size) and size < maxBits then
|
||||
size += 1
|
||||
mask = lshift(1, size) - 1
|
||||
end
|
||||
|
||||
Reference in new issue
Block a user