codecs/image: comments to specs only
This commit is contained in:
1 parent
0bd7b87d2b
commit
95a6b03f70
30 files changed
+224
-770
No files matched your search
@@ -1,21 +1,13 @@
|
||||
--!strict
|
||||
--!native
|
||||
--!optimize 2
|
||||
-- codecs/image/Pixels: row loops between packed pixel layouts and rgba8 (native), shared by the bitmap formats (BMP,
|
||||
-- the DIBs inside ICO / CUR, ...): 1 / 2 / 4 / 8-bit indexes through a palette, BGR / BGRA, 5-5-5, bit-masked 16 /
|
||||
-- 24 / 32-bit pixels, RLE4 / RLE8, rows bottom-up or top-down; and the encoder direction (rgba8 -> BGR / BGRA /
|
||||
-- 8-bit indexes, bottom-up). Absorbed from bmp-luau b86aed6 (same author, no third-party code); the encoder loops
|
||||
-- are new. They never throw: the caller 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 (bytes R, G, B, A).
|
||||
-- codecs/image/Pixels: row loops between packed pixel layouts and rgba8 for BMP / DIB.
|
||||
-- Indexed, BGR(A), 5-5-5, bit masks, RLE4 / RLE8. From bmp-luau b86aed6 (same author); the encoder loops are new.
|
||||
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 = {}
|
||||
|
||||
-- palette(src, base, n, bgr?) -> a 256-entry u32 palette for indexed(): n RGB triples (BGR when bgr) at src[base..],
|
||||
-- opaque; entries past n (or past the end of src) are opaque black. The game formats' 768-byte palettes (PLAYPAL,
|
||||
-- Quake palette.lmp, Half-Life MDL / WAD3 / BSP 30 textures).
|
||||
function Pixels.palette(src: buffer, base: number, n: number, bgr: boolean?): buffer
|
||||
local pal = buffer.create(1024)
|
||||
local m = if base >= 0 then math.clamp(math.min(n, (buffer.len(src) - base) // 3), 0, 256) else 0
|
||||
@@ -34,7 +26,6 @@ function Pixels.palette(src: buffer, base: number, n: number, bgr: boolean?): bu
|
||||
return pal
|
||||
end
|
||||
|
||||
-- 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
|
||||
@@ -88,7 +79,7 @@ function Pixels.bgr24(src: buffer, base: number, stride: number, dst: buffer, w:
|
||||
end
|
||||
end
|
||||
|
||||
-- 32 bpp B, G, R, A (BI_RGB, or BITFIELDS with the standard masks). Returns the OR of every alpha byte.
|
||||
-- 32 bpp B, G, R, A (BI_RGB, or BITFIELDS with the standard masks)
|
||||
function Pixels.bgra32(src: buffer, base: number, stride: number, dst: buffer, w: number, h: number,
|
||||
topDown: boolean): number
|
||||
local aor = 0
|
||||
@@ -97,7 +88,6 @@ function Pixels.bgra32(src: buffer, base: number, stride: number, dst: buffer, w
|
||||
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
|
||||
@@ -105,9 +95,6 @@ function Pixels.bgra32(src: buffer, base: number, stride: number, dst: buffer, w
|
||||
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
|
||||
@@ -138,16 +125,14 @@ function Pixels.masked(src: buffer, base: number, stride: number, bpp: number, d
|
||||
return aor
|
||||
end
|
||||
|
||||
-- Sets every alpha byte to 255 (32-bit images whose alpha channel is all zero are opaque).
|
||||
-- 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.
|
||||
-- RLE8 / RLE4: pixels skipped by a delta or an early end of line / bitmap stay transparent
|
||||
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
|
||||
@@ -221,7 +206,6 @@ function Pixels.rle(src: buffer, pos: number, stop: number, rle4: boolean, dst:
|
||||
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
|
||||
@@ -236,20 +220,17 @@ function Pixels.encode24(src: buffer, w: number, h: number, dst: buffer, base: n
|
||||
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
|
||||
@@ -260,8 +241,6 @@ function Pixels.encode8(src: buffer, w: number, h: number, dst: buffer, base: nu
|
||||
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 } = {}
|
||||
|
||||
Reference in new issue
Block a user