--!strict --!native --!optimize 2 -- image/bmp/Pixels: the pixel loops (native), decode and encode. Absorbed from bmp-luau b86aed6 (same author, no -- third-party code); the encoder loops are new. They never throw: the caller (init.luau) validates every size first, and -- hands in a source buffer that holds every byte an uncompressed loop can read (a truncated file is copied into a -- zero-padded buffer). The RLE loop checks each read against its end offset. Output: RGBA8, top-down rows, one -- u32 per pixel (R | G << 8 | B << 16 | A << 24, little-endian = bytes R, G, B, A). local readu8, readu16, readu32, writeu32, writeu8 = buffer.readu8, buffer.readu16, buffer.readu32, buffer.writeu32, buffer.writeu8 local band, rshift, bor, lshift = bit32.band, bit32.rshift, bit32.bor, bit32.lshift local Pixels = {} -- 1, 2, 4 and 8 bpp through a 256-entry u32 palette (pal) function Pixels.indexed(src: buffer, base: number, stride: number, bpp: number, dst: buffer, w: number, h: number, topDown: boolean, pal: buffer) local mask = lshift(1, bpp) - 1 for r = 0, h - 1 do local so = base + r * stride local d = (if topDown then r else h - 1 - r) * w * 4 if bpp == 8 then for x = 0, w - 1 do writeu32(dst, d + x * 4, readu32(pal, readu8(src, so + x) * 4)) end elseif bpp == 4 then for x = 0, w - 1 do local b = readu8(src, so + rshift(x, 1)) local i = if band(x, 1) == 0 then rshift(b, 4) else band(b, 15) writeu32(dst, d + x * 4, readu32(pal, i * 4)) end else for x = 0, w - 1 do local bit = x * bpp local b = readu8(src, so + rshift(bit, 3)) local i = band(rshift(b, 8 - bpp - band(bit, 7)), mask) writeu32(dst, d + x * 4, readu32(pal, i * 4)) end end end end -- 24 bpp BI_RGB: B, G, R function Pixels.bgr24(src: buffer, base: number, stride: number, dst: buffer, w: number, h: number, topDown: boolean) for r = 0, h - 1 do local so = base + r * stride local d = (if topDown then r else h - 1 - r) * w * 4 for x = 0, w - 1 do local s = so + x * 3 writeu32(dst, d + x * 4, bor(readu8(src, s + 2), lshift(readu8(src, s + 1), 8), lshift(readu8(src, s), 16), 0xFF000000)) end end end -- 32 bpp B, G, R, A (BI_RGB, or BITFIELDS with the standard masks). Returns the OR of every alpha byte. function Pixels.bgra32(src: buffer, base: number, stride: number, dst: buffer, w: number, h: number, topDown: boolean): number local aor = 0 for r = 0, h - 1 do local so = base + r * stride local d = (if topDown then r else h - 1 - r) * w * 4 for x = 0, w - 1 do local p = readu32(src, so + x * 4) -- swap R and B: B G R A -> R G B A writeu32(dst, d + x * 4, bor(band(p, 0xFF00FF00), band(rshift(p, 16), 0xFF), lshift(band(p, 0xFF), 16))) aor = bor(aor, p) end end return rshift(aor, 24) end -- 16 / 24 / 32 bpp through bit masks. sh* = shift of each mask, m* = mask >> shift, t* = scale tables (u8, one entry -- per masked value). A channel without a mask has m = 0 and a 1-entry table (0 for colour, 255 for alpha). -- Returns the OR of every masked alpha value (before scaling). function Pixels.masked(src: buffer, base: number, stride: number, bpp: number, dst: buffer, w: number, h: number, topDown: boolean, shr: number, mr: number, tr: buffer, shg: number, mg: number, tg: buffer, shb: number, mb: number, tb: buffer, sha: number, ma: number, ta: buffer): number local bytes = rshift(bpp, 3) local aor = 0 for r = 0, h - 1 do local so = base + r * stride local d = (if topDown then r else h - 1 - r) * w * 4 for x = 0, w - 1 do local s = so + x * bytes local p if bytes == 2 then p = readu16(src, s) elseif bytes == 4 then p = readu32(src, s) else p = bor(readu16(src, s), lshift(readu8(src, s + 2), 16)) end local a = band(rshift(p, sha), ma) aor = bor(aor, a) local o = d + x * 4 writeu8(dst, o, readu8(tr, band(rshift(p, shr), mr))) writeu8(dst, o + 1, readu8(tg, band(rshift(p, shg), mg))) writeu8(dst, o + 2, readu8(tb, band(rshift(p, shb), mb))) writeu8(dst, o + 3, readu8(ta, a)) end end return aor end -- Sets every alpha byte to 255 (32-bit images whose alpha channel is all zero are opaque). function Pixels.opaque(dst: buffer, n: number) for i = 0, n - 1 do writeu8(dst, i * 4 + 3, 255) end end -- RLE8 / RLE4 from src[pos, stop) into dst (pre-filled with transparent black; pixels the stream skips with a -- delta, an early end of line or an early end of bitmap stay transparent). Malformed or truncated data stops the -- decode and keeps what was drawn. Returns the number of pixels written. function Pixels.rle(src: buffer, pos: number, stop: number, rle4: boolean, dst: buffer, w: number, h: number, topDown: boolean, pal: buffer): number local x, y = 0, 0 local written = 0 local row = (if topDown then 0 else h - 1) * w * 4 local rowStep = if topDown then w * 4 else -w * 4 while y < h and pos + 1 < stop do local n = readu8(src, pos) local v = readu8(src, pos + 1) pos += 2 if n > 0 then -- encoded run: n pixels of index v (RLE4: alternating high / low nibble) local e = math.min(x + n, w) if rle4 then local hi, lo = readu32(pal, rshift(v, 4) * 4), readu32(pal, band(v, 15) * 4) for i = x, e - 1 do writeu32(dst, row + i * 4, if band(i - x, 1) == 0 then hi else lo) end else local c = readu32(pal, v * 4) for i = x, e - 1 do writeu32(dst, row + i * 4, c) end end if e > x then written += e - x end x += n elseif v == 0 then -- end of line x = 0 y += 1 row += rowStep elseif v == 1 then break -- end of bitmap elseif v == 2 then -- delta: right dx, up (in file rows) dy if pos + 1 >= stop then break end local dx, dy = readu8(src, pos), readu8(src, pos + 1) pos += 2 x += dx y += dy row += rowStep * dy else -- absolute run of v pixels, padded to a 16-bit boundary local nb = if rle4 then rshift(v + 1, 1) else v if pos + nb > stop then break end local e = math.min(x + v, w) for i = x, e - 1 do local k = i - x local idx if rle4 then local b = readu8(src, pos + rshift(k, 1)) idx = if band(k, 1) == 0 then rshift(b, 4) else band(b, 15) else idx = readu8(src, pos + k) end writeu32(dst, row + i * 4, readu32(pal, idx * 4)) end if e > x then written += e - x end x += v pos += nb + band(nb, 1) end end return written end -- Encoder: rgba8 (top-down) -> 24 bpp BGR rows, bottom-up, each padded to stride (dst is zero-filled) function Pixels.encode24(src: buffer, w: number, h: number, dst: buffer, base: number, stride: number) for y = 0, h - 1 do local s = y * w * 4 local d = base + (h - 1 - y) * stride for x = 0, w - 1 do local v = readu32(src, s + x * 4) writeu8(dst, d, band(rshift(v, 16), 255)) writeu8(dst, d + 1, band(rshift(v, 8), 255)) writeu8(dst, d + 2, band(v, 255)) d += 3 end end end -- rgba8 -> 32 bpp BGRA, bottom-up function Pixels.encode32(src: buffer, w: number, h: number, dst: buffer, base: number) for y = 0, h - 1 do local s = y * w * 4 local d = base + (h - 1 - y) * w * 4 for x = 0, w - 1 do local v = readu32(src, s + x * 4) -- R G B A (little-endian u32: R | G << 8 | B << 16 | A << 24) -> B G R A writeu32(dst, d + x * 4, bor(band(v, 0xFF00FF00), band(rshift(v, 16), 255), lshift(band(v, 255), 16))) end end end -- rgba8 -> 8 bpp indexes through idx (a table colour -> index, colours as R | G << 8 | B << 16), bottom-up function Pixels.encode8(src: buffer, w: number, h: number, dst: buffer, base: number, stride: number, idx: { [number]: number }) for y = 0, h - 1 do local s = y * w * 4 local d = base + (h - 1 - y) * stride for x = 0, w - 1 do writeu8(dst, d + x, idx[band(readu32(src, s + x * 4), 0xFFFFFF)] or 0) end end end -- colours(src, n, max) -> the distinct RGB colours in first-appearance order (R | G << 8 | B << 16), whether any -- alpha is below 255; stops counting at max + 1 colours function Pixels.colours(src: buffer, n: number, max: number): ({ number }, boolean) local seen: { [number]: boolean } = {} local list: { number } = {} local alpha = false for i = 0, n - 1 do local v = readu32(src, i * 4) if v < 0xFF000000 then alpha = true end local c = band(v, 0xFFFFFF) if #list <= max and not seen[c] then seen[c] = true list[#list + 1] = c end end return list, alpha end return Pixels