--!strict -- image/sunras: Sun Raster (rasterfile(5), SunOS ; new code). Decode: 1-bit (0 = white without a colour -- map), 8-bit (grey or the RGB colour map), 24-bit and 32-bit pixels in BGR / XBGR order (RT_STANDARD, RT_OLD, -- RT_BYTE_ENCODED) or RGB / XRGB order (RT_FORMAT_RGB), the byte-encoded RLE (0x80 escapes), 16-bit row padding; in -- 32-bit files the X byte is alpha when it is not all zero. Encode: rgba8 as 24-bit (opaque) or 32-bit (alpha in the -- X byte) BGR, RT_BYTE_ENCODED (default) or RT_STANDARD. Not supported: RT_FORMAT_TIFF / RT_FORMAT_IFF (UNSUPPORTED), -- raw colour maps (RMT_RAW is ignored). -- ------------------------------------------------------------------------------------------------ requires local Err = require("@formats/core/Err") local Bytes = require("@formats/core/Bytes") local Sniff = require("@formats/core/Sniff") local Types = require("@formats/Types") local Pixels = require("@formats/codecs/image/Pixels") -- ------------------------------------------------------------------------------------------------ contract local Sunras = { name = "sunras", category = "image", extensions = { "ras", "sun", "rs", "im1", "im8", "im24", "im32" }, mime = { "image/x-sun-raster", "image/x-cmu-raster" }, deps = { "core/Err", "core/Bytes", "core/Sniff", "Types", "codecs/image/Pixels" }, caps = { decode = true, encode = true, alpha = true, palette = true, depths = { 1, 8, 24, 32 } }, } -- ------------------------------------------------------------------------------------------------ types export type DecodeOptions = { maxPixels: number?, -- refuse larger images (default 2^26) } export type EncodeOptions = { rle: boolean?, -- RT_BYTE_ENCODED (default true); false: RT_STANDARD } local MAGIC = "\89\166\106\149" local readu8, readu32 = buffer.readu8, buffer.readu32 local function be32(b: buffer, o: number): number return bit32.byteswap(readu32(b, o)) end -- ------------------------------------------------------------------------------------------------ sniff function Sunras.sniff(r: Bytes.Reader): number return Sniff.magic(r, MAGIC) end -- ------------------------------------------------------------------------------------------------ decode function Sunras.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Image?, Err.Err?) local b, o, limit = r.buf, r.base, r.limit if limit - o < 32 then return nil, Err.new(Err.TRUNCATED, "short header", o) end if buffer.readstring(b, o, 4) ~= MAGIC then return nil, Err.new(Err.CORRUPT, "not a Sun raster", o) end local w, h, depth, typ, maptype, maplen = be32(b, o + 4), be32(b, o + 8), be32(b, o + 12), be32(b, o + 20), be32(b, o + 24), be32(b, o + 28) if w == 0 or h == 0 or w > 65536 or h > 65536 then return nil, Err.new(Err.CORRUPT, "bad size", o + 4) end if depth ~= 1 and depth ~= 8 and depth ~= 24 and depth ~= 32 then return nil, Err.new(Err.UNSUPPORTED, depth .. "-bit pixels", o + 12) end if typ == 4 or typ == 5 or typ > 5 and typ ~= 0xFFFF then return nil, Err.new(Err.UNSUPPORTED, "raster type " .. typ, o + 20) 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 p = o + 32 if p + maplen > limit then return nil, Err.new(Err.TRUNCATED, "colour map runs past the end", p) end local pal = buffer.create(1024) local ncol = 0 if maptype == 1 and maplen > 0 then ncol = math.min(256, maplen // 3) for i = 0, ncol - 1 do buffer.writeu32(pal, i * 4, readu8(b, p + i) + readu8(b, p + ncol + i) * 256 + readu8(b, p + 2 * ncol + i) * 65536 + 0xFF000000) end elseif depth == 1 then buffer.writeu32(pal, 0, 0xFFFFFFFF) buffer.writeu32(pal, 4, 0xFF000000) else for i = 0, 255 do buffer.writeu32(pal, i * 4, i * 0x10101 + 0xFF000000) end end p += maplen local stride = ((w * depth + 15) // 16) * 2 local total = stride * h local raw = buffer.create(total + 4) local truncated = false if typ == 2 then local q = 0 while q < total and p < limit do local c = readu8(b, p) p += 1 if c ~= 0x80 then buffer.writeu8(raw, q, c) q += 1 else if p >= limit then break end local n = readu8(b, p) p += 1 if n == 0 then buffer.writeu8(raw, q, 0x80) q += 1 else if p >= limit then break end local cnt = math.min(n + 1, total - q) buffer.fill(raw, q, readu8(b, p), cnt) p += 1 q += cnt end end end truncated = q < total else local n = math.min(total, limit - p) buffer.copy(raw, 0, b, p, n) truncated = n < total end local img = Types.Image.new(w, h, "rgba8") local px = img.pixels if depth <= 8 then Pixels.indexed(raw, 0, stride, depth, px, w, h, true, pal) else local rgb = typ == 3 local bytes = depth // 8 local alpha = false if bytes == 4 then for y = 0, h - 1 do for x = 0, w - 1 do if readu8(raw, y * stride + x * 4) ~= 0 then alpha = true end end end end for y = 0, h - 1 do for x = 0, w - 1 do local s = y * stride + x * bytes local a = 255 if bytes == 4 then a = if alpha then readu8(raw, s) else 255 s += 1 end local c0, c1, c2 = readu8(raw, s), readu8(raw, s + 1), readu8(raw, s + 2) local rr, bb = if rgb then c0 else c2, if rgb then c2 else c0 buffer.writeu32(px, (y * w + x) * 4, rr + c1 * 256 + bb * 65536 + a * 16777216) end end end if ncol > 0 then local list: { { number } } = {} for i = 0, ncol - 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 if truncated then img.meta.truncated = true end img.meta.source = { format = "sunras", bitDepth = depth, type = typ, rle = typ == 2 } return img, nil end -- ------------------------------------------------------------------------------------------------ encode function Sunras.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 then return nil, Err.new(Err.ARG, "bad size") end local px = img.pixels local alpha = false for i = 0, w * h - 1 do if readu8(px, i * 4 + 3) < 255 then alpha = true break end end local depth = if alpha then 32 else 24 local bytes = depth // 8 local stride = ((w * depth + 15) // 16) * 2 local raw = buffer.create(stride * h) for y = 0, h - 1 do for x = 0, w - 1 do local s, d = (y * w + x) * 4, y * stride + x * bytes if alpha then buffer.writeu8(raw, d, readu8(px, s + 3)) d += 1 end buffer.writeu8(raw, d, readu8(px, s + 2)) buffer.writeu8(raw, d + 1, readu8(px, s + 1)) buffer.writeu8(raw, d + 2, readu8(px, s)) end end local rle = not (opts and opts.rle == false) local body = raw if rle then local wr = Bytes.writer(stride * h) local i, n = 0, stride * h while i < n do local v = readu8(raw, i) local run = 1 while i + run < n and run < 256 and readu8(raw, i + run) == v do run += 1 end if run >= 3 or v == 0x80 then if run == 1 then wr:u8(0x80) wr:u8(0) else wr:u8(0x80) wr:u8(run - 1) wr:u8(v) end else for _ = 1, run do wr:u8(v) end end i += run end body = wr:finish() end local out = Bytes.writer(32 + buffer.len(body)) out:string(MAGIC) out:u32be(w) out:u32be(h) out:u32be(depth) out:u32be(buffer.len(body)) out:u32be(if rle then 2 else 1) out:u32be(0) out:u32be(0) out:bytes(body) return out:finish(), nil end return Sunras