--!strict -- image/pcx: ZSoft PC Paintbrush (the ZSoft "Technical Reference Manual" PCX header and RLE; new code). Decode: 1-bit -- monochrome, 1-bit x 2..4 planes (EGA, the header palette), 2 / 4-bit packed (the header palette), 8-bit with the -- 256-colour palette at the end (grey when it is missing), 8-bit x 3 / 4 planes (RGB / RGBA); RLE (runs may cross -- scan lines) and the rare uncompressed encoding; the window (xMin / yMin) and DPI (meta.dpi). Encode: rgba8 as -- 8-bit palette (up to 256 opaque colours), 24-bit (3 planes) or 32-bit (4 planes, when there is alpha), RLE. -- ------------------------------------------------------------------------------------------------ requires local Err = require("@rosetta/core/Err") local Bytes = require("@rosetta/core/Bytes") local Types = require("@rosetta/Types") local Pixels = require("@rosetta/codecs/image/Pixels") -- ------------------------------------------------------------------------------------------------ contract local Pcx = { name = "pcx", category = "image", extensions = { "pcx", "pcc", "dcx" }, mime = { "image/vnd.zbrush.pcx", "image/x-pcx" }, deps = { "core/Err", "core/Bytes", "Types", "codecs/image/Pixels" }, caps = { decode = true, encode = true, alpha = true, palette = true, depths = { 1, 2, 4, 8, 24, 32 } }, } -- ------------------------------------------------------------------------------------------------ types export type DecodeOptions = { maxPixels: number?, -- refuse larger images (default 2^26) } export type EncodeOptions = { palette: boolean?, -- 8-bit palette when possible (default true); false: always 24 / 32-bit planes } local readu8, readu16 = buffer.readu8, buffer.readu16 -- ------------------------------------------------------------------------------------------------ sniff function Pcx.sniff(r: Bytes.Reader): number if r:size() < 128 then return 0 end local m, v, e, bpp = r:peekU8(0), r:peekU8(1), r:peekU8(2), r:peekU8(3) if m ~= 10 or (v ~= 0 and v ~= 2 and v ~= 3 and v ~= 4 and v ~= 5) or e > 1 then return 0 end return if bpp == 1 or bpp == 2 or bpp == 4 or bpp == 8 then 0.8 else 0 end -- ------------------------------------------------------------------------------------------------ decode function Pcx.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Image?, Err.Err?) local b, o, limit = r.buf, r.base, r.limit if limit - o < 128 then return nil, Err.new(Err.TRUNCATED, "short header", o) end if readu8(b, o) ~= 10 then return nil, Err.new(Err.CORRUPT, "not a PCX file", o) end local version, enc, bpp = readu8(b, o + 1), readu8(b, o + 2), readu8(b, o + 3) local x0, y0, x1, y1 = readu16(b, o + 4), readu16(b, o + 6), readu16(b, o + 8), readu16(b, o + 10) local planes, bpl = readu8(b, o + 65), readu16(b, o + 66) local w, h = x1 - x0 + 1, y1 - y0 + 1 if x1 < x0 or y1 < y0 then return nil, Err.new(Err.CORRUPT, "bad window", o + 4) end local maxPixels = opts and opts.maxPixels or 2 ^ 26 if w * h > maxPixels then return nil, Err.new(Err.LIMIT, string.format("%d x %d is over the %d pixel limit", w, h, maxPixels)) end local kind if bpp == 8 and planes == 1 then kind = "pal8" elseif bpp == 8 and (planes == 3 or planes == 4) then kind = "rgb" elseif bpp == 1 and planes >= 1 and planes <= 4 then kind = "planar" elseif (bpp == 2 or bpp == 4) and planes == 1 then kind = "packed" else return nil, Err.new(Err.UNSUPPORTED, string.format("%d bits x %d planes", bpp, planes), o + 3) end if bpl * 8 < w * bpp then return nil, Err.new(Err.CORRUPT, "bytes per line too small", o + 66) end -- the scan lines (planes after each other per line), RLE across lines local total = bpl * planes * h local raw = buffer.create(total) local p, q = o + 128, 0 local truncated = false if enc == 1 then while q < total do if p >= limit then truncated = true break end local c = readu8(b, p) p += 1 if c >= 0xC0 then if p >= limit then truncated = true break end local n = math.min(c - 0xC0, total - q) buffer.fill(raw, q, readu8(b, p), n) p += 1 q += n else buffer.writeu8(raw, q, c) q += 1 end end else local n = math.min(total, limit - p) buffer.copy(raw, 0, b, p, n) p += n truncated = n < total end -- palettes local pal = buffer.create(1024) local function setPal(i: number, rr: number, gg: number, bb: number) buffer.writeu32(pal, i * 4, rr + gg * 256 + bb * 65536 + 0xFF000000) end if kind == "pal8" then local at = limit - 769 if at >= p and readu8(b, at) == 12 then for i = 0, 255 do setPal(i, readu8(b, at + 1 + i * 3), readu8(b, at + 2 + i * 3), readu8(b, at + 3 + i * 3)) end else for i = 0, 255 do setPal(i, i, i, i) end end elseif kind == "planar" and planes == 1 then setPal(0, 0, 0, 0) setPal(1, 255, 255, 255) else for i = 0, 15 do setPal(i, readu8(b, o + 16 + i * 3), readu8(b, o + 17 + i * 3), readu8(b, o + 18 + i * 3)) end end local img = Types.Image.new(w, h, "rgba8") local px = img.pixels if kind == "pal8" or kind == "packed" then Pixels.indexed(raw, 0, bpl, bpp, px, w, h, true, pal) elseif kind == "planar" then for y = 0, h - 1 do local row = y * bpl * planes for x = 0, w - 1 do local idx = 0 for pl = 0, planes - 1 do local byte = readu8(raw, row + pl * bpl + bit32.rshift(x, 3)) idx += bit32.band(bit32.rshift(byte, 7 - bit32.band(x, 7)), 1) * bit32.lshift(1, pl) end buffer.writeu32(px, (y * w + x) * 4, buffer.readu32(pal, idx * 4)) end end else for y = 0, h - 1 do local row = y * bpl * planes for x = 0, w - 1 do local d = (y * w + x) * 4 buffer.writeu8(px, d, readu8(raw, row + x)) buffer.writeu8(px, d + 1, readu8(raw, row + bpl + x)) buffer.writeu8(px, d + 2, readu8(raw, row + 2 * bpl + x)) buffer.writeu8(px, d + 3, if planes == 4 then readu8(raw, row + 3 * bpl + x) else 255) end end end if kind == "pal8" or kind == "packed" or (kind == "planar" and planes > 1) then local list: { { number } } = {} for i = 0, (if kind == "pal8" then 256 else bit32.lshift(1, bpp * planes)) - 1 do local v = buffer.readu32(pal, i * 4) list[i + 1] = { bit32.band(v, 255), bit32.band(bit32.rshift(v, 8), 255), bit32.band(bit32.rshift(v, 16), 255), 255 } end img.meta.palette = list end local hd, vd = readu16(b, o + 12), readu16(b, o + 14) if hd > 0 and vd > 0 then img.meta.dpi = { x = hd, y = vd } end if truncated then img.meta.truncated = true end img.meta.source = { format = "pcx", version = version, bitDepth = bpp * planes, planes = planes, x = x0, y = y0, rle = enc == 1 } return img, nil end -- ------------------------------------------------------------------------------------------------ encode -- RLE of one plane row into w local function rleRow(out: Bytes.Writer, src: buffer, p: number, n: number) local i = 0 while i < n do local v = readu8(src, p + i) local run = 1 while i + run < n and run < 63 and readu8(src, p + i + run) == v do run += 1 end if run > 1 or v >= 0xC0 then out:u8(0xC0 + run) end out:u8(v) i += run end end function Pcx.encode(img: Types.Image, opts: EncodeOptions?): (buffer?, Err.Err?) if type(img) ~= "table" or img.format ~= "rgba8" or type(img.pixels) ~= "buffer" then return nil, Err.new(Err.ARG, "needs an rgba8 Image") end local w, h = img.width, img.height if w < 1 or h < 1 or w > 65535 or h > 65535 then return nil, Err.new(Err.ARG, "size must be 1..65535") end local px = img.pixels local cols, alpha = Pixels.colours(px, w * h, 256) local usePal = not alpha and #cols <= 256 and not (opts and opts.palette == false) local planes = if usePal then 1 elseif alpha then 4 else 3 local bpl = w + w % 2 local out = Bytes.writer(128 + w * h * planes + 800) out:u8(10) out:u8(5) out:u8(1) out:u8(8) out:u16le(0) out:u16le(0) out:u16le(w - 1) out:u16le(h - 1) local dpi = img.meta and img.meta.dpi out:u16le(if dpi then math.floor(dpi.x + 0.5) else 72) out:u16le(if dpi then math.floor(dpi.y + 0.5) else 72) out:fill(0, 48) out:u8(0) out:u8(planes) out:u16le(bpl) out:u16le(1) out:fill(0, 58) local row = buffer.create(bpl) local index: { [number]: number } = {} for i, c in cols do index[c] = i - 1 end for y = 0, h - 1 do for pl = 0, planes - 1 do for x = 0, w - 1 do local s = (y * w + x) * 4 local v if usePal then v = index[bit32.band(buffer.readu32(px, s), 0xFFFFFF)] or 0 else v = readu8(px, s + pl) end buffer.writeu8(row, x, v) end rleRow(out, row, 0, bpl) end end if usePal then out:u8(12) for i = 0, 255 do local c = cols[i + 1] or 0 out:u8(bit32.band(c, 255)) out:u8(bit32.band(bit32.rshift(c, 8), 255)) out:u8(bit32.rshift(c, 16)) end end return out:finish(), nil end return Pcx