--!strict --!native --!optimize 2 -- image/exr/Piz: OpenEXR's PIZ decompressor (written from the OpenEXR 3.x sources' algorithm description: ImfPizCompressor -- / ImfHuf / ImfWav, BSD-3-Clause, licenses/openexr-LICENSE.md): the range bitmap and its reverse LUT, the Huffman -- stream (packed code lengths, canonical codes, the run-length symbol), the 2-D Haar wavelet (14- and 16-bit -- variants) per channel plane, then the planes rearranged into scanlines. Never throws: nil, message on bad data. local Piz = {} local readu8, readu16, writeu16, readu32 = buffer.readu8, buffer.readu16, buffer.writeu16, buffer.readu32 local band, rshift, lshift = bit32.band, bit32.rshift, bit32.lshift local USHORT_RANGE, BITMAP_SIZE = 65536, 8192 local HUF_ENCSIZE = 65537 local DECBITS = 14 local SHORT_ZEROCODE_RUN, LONG_ZEROCODE_RUN, SHORTEST_LONG_RUN = 59, 63, 6 -- ------------------------------------------------------------------------------------------------ Huffman -- the canonical code table: lengths[i] and codes[i] for symbols im..iM (1-based tables: symbol s at s + 1) local function unpackTable(src: buffer, p: number, limit: number, im: number, iM: number, len: { number }): number? local c, lc = 0, 0 local function bits(n: number): number? while lc < n do if p >= limit then return nil end c = c * 256 + readu8(src, p) p += 1 lc += 8 end lc -= n local v = c // 2 ^ lc c -= v * 2 ^ lc return v end local i = im while i <= iM do local l = bits(6) if not l then return nil end if l == LONG_ZEROCODE_RUN then local z = bits(8) if not z then return nil end z += SHORTEST_LONG_RUN if i + z > iM + 1 then return nil end for k = i, i + z - 1 do len[k + 1] = 0 end i += z elseif l >= SHORT_ZEROCODE_RUN then local z = l - SHORT_ZEROCODE_RUN + 2 if i + z > iM + 1 then return nil end for k = i, i + z - 1 do len[k + 1] = 0 end i += z else len[i + 1] = l i += 1 end end return p end -- hufUncompress: nRaw u16 values from [p, p + n) into out (a buffer of u16) | nil, message local function huffman(src: buffer, p: number, n: number, out: buffer, nRaw: number): string? if n == 0 then return if nRaw == 0 then nil else "empty Huffman data" end if n < 20 then return "truncated Huffman header" end local im, iM, nBits = readu32(src, p), readu32(src, p + 4), readu32(src, p + 12) if im >= HUF_ENCSIZE or iM >= HUF_ENCSIZE or im > iM then return "bad Huffman symbol range" end local limit = p + n local len = table.create(HUF_ENCSIZE, 0) local q = unpackTable(src, p + 20, limit, im, iM, len) if not q then return "bad Huffman table" end if q + (nBits + 7) // 8 > limit then return "truncated Huffman data" end -- canonical codes (OpenEXR's order: longest codes get the smallest values) local count = table.create(59, 0) for s = im, iM do local l = len[s + 1] if l > 58 then return "bad Huffman code length" end count[l + 1] += 1 end local start = table.create(59, 0) local c = 0 for l = 58, 1, -1 do local nc = (c + count[l + 1]) // 2 start[l + 1] = c c = nc end local fast = buffer.create(lshift(1, DECBITS) * 4) -- (symbol + 1) * 64 + length; 0 = long or invalid local long: { [number]: { [number]: number } } = {} local maxLong = 0 for s = im, iM do local l = len[s + 1] if l > 0 then local code = start[l + 1] start[l + 1] = code + 1 if l <= DECBITS then local base = code * 2 ^ (DECBITS - l) local v = (s + 1) * 64 + l for k = base, base + 2 ^ (DECBITS - l) - 1 do if k < lshift(1, DECBITS) then buffer.writeu32(fast, k * 4, v) end end else if l > 52 then return "unsupported: Huffman code longer than 52 bits" end local t = long[l] if not t then t = {} long[l] = t end t[code] = s maxLong = math.max(maxLong, l) end end end -- decode MSB-first from a zero-padded copy local nbytes = (nBits + 7) // 8 local data = buffer.create(nbytes + 8) buffer.copy(data, 0, src, q, nbytes) local bp = 0 local o = 0 local rlc = iM local mask = lshift(1, DECBITS) - 1 while bp < nBits do local byte = rshift(bp, 3) local w = bit32.byteswap(readu32(data, byte)) local peek = band(rshift(w, 32 - DECBITS - band(bp, 7)), mask) local e = readu32(fast, peek * 4) local sym: number, l: number = 0, 0 if e ~= 0 then sym = e // 64 - 1 l = band(e, 63) else -- a long code: bit by bit from length 15 sym = -1 local code = 0 for k = 0, maxLong - 1 do local bitAt = bp + k code = code * 2 + band(rshift(readu8(data, rshift(bitAt, 3)), 7 - band(bitAt, 7)), 1) if k + 1 > DECBITS then local t = long[k + 1] local found: number? = if t then t[code] else nil if found then sym = found l = k + 1 break end end end if sym < 0 then return "invalid Huffman code" end end bp += l if bp > nBits then return "Huffman code past the end of the data" end if sym == rlc then if bp + 8 > nBits then return "truncated run length" end local b2 = bit32.byteswap(readu32(data, rshift(bp, 3))) local cs = band(rshift(b2, 24 - band(bp, 7)), 255) bp += 8 if o + cs > nRaw then return "too much Huffman data" end if o == 0 then return "run length at the start" end local s = readu16(out, (o - 1) * 2) for _ = 1, cs do writeu16(out, o * 2, s) o += 1 end else if o >= nRaw then return "too much Huffman data" end writeu16(out, o * 2, sym) o += 1 end end if o ~= nRaw then return "not enough Huffman data" end return nil end -- ------------------------------------------------------------------------------------------------ wavelet -- the inverse 2-D Haar wavelet over n x ny u16 values at element offset base, strides ox / oy (in elements) local function wav2Decode(t: buffer, base: number, nx: number, ox: number, ny: number, oy: number, mx: number) local w14 = mx < 16384 local n = math.min(nx, ny) local p = 1 while p <= n do p *= 2 end p //= 2 local p2 = p p //= 2 local function dec(l: number, h: number): (number, number) if w14 then local ls = if l >= 32768 then l - 65536 else l local hs = if h >= 32768 then h - 65536 else h local ai = ls + hs % 2 + hs // 2 return ai % 65536, (ai - hs) % 65536 end local bb = (l - rshift(h, 1)) % 65536 return (h + bb - 32768) % 65536, bb end while p >= 1 do local oy1, oy2, ox1, ox2 = oy * p, oy * p2, ox * p, ox * p2 local py = base local ey = base + oy * (ny - p2) while py <= ey do local px = py local ex = py + ox * (nx - p2) while px <= ex do local p01 = px + ox1 local p10 = px + oy1 local p11 = p10 + ox1 local i00, i10 = dec(readu16(t, px * 2), readu16(t, p10 * 2)) local i01, i11 = dec(readu16(t, p01 * 2), readu16(t, p11 * 2)) local a, b = dec(i00, i01) writeu16(t, px * 2, a) writeu16(t, p01 * 2, b) local c, d = dec(i10, i11) writeu16(t, p10 * 2, c) writeu16(t, p11 * 2, d) px += ox2 end if band(nx, p) ~= 0 then local p10 = px + oy1 local a, b = dec(readu16(t, px * 2), readu16(t, p10 * 2)) writeu16(t, px * 2, a) writeu16(t, p10 * 2, b) end py += oy2 end if band(ny, p) ~= 0 then local px = py local ex = py + ox * (nx - p2) while px <= ex do local p01 = px + ox1 local a, b = dec(readu16(t, px * 2), readu16(t, p01 * 2)) writeu16(t, px * 2, a) writeu16(t, p01 * 2, b) px += ox2 end end p2 = p p //= 2 end end -- ------------------------------------------------------------------------------------------------ decode export type Chan = { nx: number, ny: number, size: number, ys: number } -- decode(src, p, n, chans, minY, maxY, outSize) -> the uncompressed block (scanline order) | nil, message. -- chans: per channel (sorted order) its sample counts nx / ny in the block, size (1 = half, 2 = float / uint) and -- ySampling. function Piz.decode(src: buffer, p: number, n: number, chans: { Chan }, minY: number, maxY: number, outSize: number): (buffer?, string?) local total = 0 for _, c in chans do total += c.nx * c.ny * c.size end if total * 2 ~= outSize then return nil, "PIZ block size mismatch" end local out = buffer.create(outSize) if total == 0 then return out, nil end local limit = p + n if p + 4 > limit then return nil, "truncated PIZ header" end local minNZ, maxNZ = readu16(src, p), readu16(src, p + 2) p += 4 if maxNZ >= BITMAP_SIZE then return nil, "bad PIZ bitmap range" end local bitmap = buffer.create(BITMAP_SIZE) if minNZ <= maxNZ then local cnt = maxNZ - minNZ + 1 if p + cnt > limit then return nil, "truncated PIZ bitmap" end buffer.copy(bitmap, minNZ, src, p, cnt) p += cnt end -- reverse LUT local lut = buffer.create(USHORT_RANGE * 2) local k = 0 for i = 0, USHORT_RANGE - 1 do if i == 0 or band(readu8(bitmap, rshift(i, 3)), lshift(1, band(i, 7))) ~= 0 then writeu16(lut, k * 2, i) k += 1 end end local maxValue = k - 1 if p + 4 > limit then return nil, "truncated PIZ length" end local length = readu32(src, p) p += 4 if length > limit - p then return nil, "truncated PIZ data" end local tmp = buffer.create(total * 2) local herr = huffman(src, p, length, tmp, total) if herr then return nil, herr end local starts = table.create(#chans, 0) local at = 0 for i, c in chans do starts[i] = at for j = 0, c.size - 1 do wav2Decode(tmp, at + j, c.nx, c.size, c.ny, c.nx * c.size, maxValue) end at += c.nx * c.ny * c.size end for i = 0, total - 1 do writeu16(tmp, i * 2, readu16(lut, readu16(tmp, i * 2) * 2)) end -- planes -> scanlines local o = 0 local cursor = table.clone(starts) for y = minY, maxY do for i, c in chans do if y % c.ys == 0 then local cnt = c.nx * c.size buffer.copy(out, o, tmp, cursor[i] * 2, cnt * 2) cursor[i] += cnt o += cnt * 2 end end end return out, nil end return Piz