Each pill now shows the icon the app's .desktop file names (Icon=), looked up in the freedesktop icon themes, instead of the letter tile. The tile stays as the fallback for windows with no app id, no desktop file or no icon the host can read. - WIT: a new `icons` capability (`app-icon(app-id) -> u32`, a handle, never a path or pixels) and an `image(image-op)` draw op (appended, so existing modules keep their ABI). taskbar-widget and plasmoid-host import `icons`. Host raster gains `load-image(path)` and a `draw-image` op; the plasmoids host gets an icons table (6: app id, handle). - Compositor (shell/icons.mbt): window app id -> desktop file (resolve_app_id, so StartupWMClass and last-dotted-part matches work) -> Icon= -> theme lookup (XDG data dirs, ~/.icons, flatpak exports; hicolor, Adwaita, breeze; 48/64/32 px PNG first, then scalable SVG when the host can draw it, then other sizes, then /usr/share/pixmaps). Absolute Icon= paths are used as they are. Results are cached per app id; the table is published only when a module imports `icons`. - wasm host (host-wasm): PNG via the png crate, SVG via resvg (rendered sharp at each size), PNGs area-averaged to the drawn size; loads cached by path. - Native build: a small PNG decoder (compositor/renderer/png.mbt: inflate + filters, 1-16 bit, palettes with tRNS) and the Image op in the software renderer. SVG-only icons fall back to the tile there. The wasmer stub serves `icons.app-icon` from the same table. - Tests: PNG decoder fixtures (all filters, fixed/dynamic/stored blocks, palette), image scaling and drawing, icon lookup order, Icon= parsing, task module icon-or-tile (native and wasmtime), host raster PNG+SVG loading and drawing. Checked end to end with the wasm host: foot's hicolor icon shows on its pill. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DPL5hhmtDt52EjaW8jevGU
This commit is contained in:
53 files changed
+2284
-473
No files matched your search
@@ -0,0 +1,88 @@
|
||||
///| Images the compositor loaded (app icons) and the `Image` draw op that shows them.
|
||||
|
||||
///| Loaded images; handle `i + 1` is `images[i]` (0 means "no image").
|
||||
let images : Array[Pixmap] = []
|
||||
|
||||
///| Each image scaled to the sizes it has been drawn at, by (handle, w, h).
|
||||
let scaled : Map[(UInt, Int, Int), Pixmap] = {}
|
||||
|
||||
///| Keep `p` for `Image` ops and return its handle (never 0).
|
||||
pub fn add_image(p : Pixmap) -> UInt {
|
||||
images.push(p)
|
||||
images.length().reinterpret_as_uint()
|
||||
}
|
||||
|
||||
///| Scale `src` to `w`x`h`: each target pixel averages the source pixels it covers (alpha
|
||||
/// weighted, so transparent edges don't darken), or takes the nearest one when enlarging.
|
||||
pub fn scale_pixmap(src : Pixmap, w : Int, h : Int) -> Pixmap {
|
||||
let out = Pixmap::new(w, h)
|
||||
for y = 0; y < h; y = y + 1 {
|
||||
let y0 = y * src.height / h
|
||||
let y1 = { let e = (y + 1) * src.height / h; if e > y0 { e } else { y0 + 1 } }
|
||||
for x = 0; x < w; x = x + 1 {
|
||||
let x0 = x * src.width / w
|
||||
let x1 = { let e = (x + 1) * src.width / w; if e > x0 { e } else { x0 + 1 } }
|
||||
let mut r = 0
|
||||
let mut g = 0
|
||||
let mut b = 0
|
||||
let mut a = 0
|
||||
let mut n = 0
|
||||
for sy = y0; sy < y1; sy = sy + 1 {
|
||||
for sx = x0; sx < x1; sx = sx + 1 {
|
||||
let o = (sy * src.width + sx) * 4
|
||||
let pa = src.data[o + 3].to_int()
|
||||
r = r + src.data[o].to_int() * pa
|
||||
g = g + src.data[o + 1].to_int() * pa
|
||||
b = b + src.data[o + 2].to_int() * pa
|
||||
a = a + pa
|
||||
n = n + 1
|
||||
}
|
||||
}
|
||||
let o = (y * w + x) * 4
|
||||
if a > 0 {
|
||||
out.data[o] = (r / a).to_byte()
|
||||
out.data[o + 1] = (g / a).to_byte()
|
||||
out.data[o + 2] = (b / a).to_byte()
|
||||
out.data[o + 3] = (a / n).to_byte()
|
||||
}
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
///| Draw image `handle` scaled into `r` (unknown handles draw nothing).
|
||||
fn draw_image(dst : Pixmap, handle : UInt, r : @scene.Rect, clip : @scene.Rect?) -> Unit {
|
||||
let i = handle.reinterpret_as_int() - 1
|
||||
if i < 0 || i >= images.length() || r.w <= 0 || r.h <= 0 || r.w > 1024 || r.h > 1024 {
|
||||
return
|
||||
}
|
||||
let key = (handle, r.w, r.h)
|
||||
let img = match scaled.get(key) {
|
||||
Some(p) => p
|
||||
None => {
|
||||
let src = images[i]
|
||||
let p = if src.width == r.w && src.height == r.h {
|
||||
src
|
||||
} else {
|
||||
scale_pixmap(src, r.w, r.h)
|
||||
}
|
||||
scaled.set(key, p)
|
||||
p
|
||||
}
|
||||
}
|
||||
for y = 0; y < r.h; y = y + 1 {
|
||||
for x = 0; x < r.w; x = x + 1 {
|
||||
let o = (y * r.w + x) * 4
|
||||
blend_pixel(
|
||||
dst,
|
||||
r.x + x,
|
||||
r.y + y,
|
||||
img.data[o].to_int(),
|
||||
img.data[o + 1].to_int(),
|
||||
img.data[o + 2].to_int(),
|
||||
img.data[o + 3].to_int(),
|
||||
clip,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -6,8 +6,12 @@ import {
|
||||
}
|
||||
|
||||
// Values
|
||||
pub fn add_image(Pixmap) -> UInt32
|
||||
|
||||
pub fn blit_shm_surface(Pixmap, @scene.ShmBufferSnapshot, @scene.Rect) -> Unit
|
||||
|
||||
pub fn decode_png(Bytes) -> Pixmap?
|
||||
|
||||
pub fn draw_ops(Pixmap, Array[@scene.DrawOp], Int32, Int32) -> Unit
|
||||
|
||||
pub fn encode_png(Pixmap) -> Bytes
|
||||
@@ -16,6 +20,8 @@ pub fn encode_ppm(Pixmap) -> Bytes
|
||||
|
||||
pub fn render_scene(@scene.SceneGraph) -> Pixmap
|
||||
|
||||
pub fn scale_pixmap(Pixmap, Int32, Int32) -> Pixmap
|
||||
|
||||
pub fn set_glyph_source((Int32, Int32, Bool, Bool) -> Glyph) -> Unit
|
||||
|
||||
pub fn text_width(String, Int32, Bool, Bool) -> Int32
|
||||
|
||||
@@ -0,0 +1,434 @@
|
||||
///| A small PNG decoder for app icons (the native build has no image library). Handles what icon
|
||||
/// themes ship: 8-bit grey, grey+alpha, RGB, RGBA (16-bit read as 8), palettes of 1-8 bits with
|
||||
/// tRNS alpha. Interlaced images and anything over 1024 px a side are refused.
|
||||
|
||||
///|
|
||||
priv suberror PngError
|
||||
|
||||
///| Inflate (RFC 1951) state over a zlib stream's deflate data.
|
||||
priv struct Inflate {
|
||||
src : Bytes
|
||||
mut pos : Int
|
||||
mut bitbuf : Int
|
||||
mut bitcnt : Int
|
||||
out : Array[Byte]
|
||||
}
|
||||
|
||||
///|
|
||||
fn Inflate::bits(self : Inflate, need : Int) -> Int raise PngError {
|
||||
let mut val = self.bitbuf
|
||||
while self.bitcnt < need {
|
||||
if self.pos >= self.src.length() {
|
||||
raise PngError
|
||||
}
|
||||
val = val | (self.src[self.pos].to_int() << self.bitcnt)
|
||||
self.pos = self.pos + 1
|
||||
self.bitcnt = self.bitcnt + 8
|
||||
}
|
||||
self.bitbuf = val >> need
|
||||
self.bitcnt = self.bitcnt - need
|
||||
val & ((1 << need) - 1)
|
||||
}
|
||||
|
||||
///| A canonical Huffman code: how many codes of each length, and the symbols in code order.
|
||||
priv struct Huffman {
|
||||
count : FixedArray[Int]
|
||||
symbol : FixedArray[Int]
|
||||
}
|
||||
|
||||
///|
|
||||
fn Huffman::build(lengths : FixedArray[Int], start : Int, n : Int) -> Huffman raise PngError {
|
||||
let count = FixedArray::make(16, 0)
|
||||
let symbol = FixedArray::make(n, 0)
|
||||
for i = 0; i < n; i = i + 1 {
|
||||
count[lengths[start + i]] = count[lengths[start + i]] + 1
|
||||
}
|
||||
let mut left = 1
|
||||
for len = 1; len < 16; len = len + 1 {
|
||||
left = (left << 1) - count[len]
|
||||
if left < 0 {
|
||||
raise PngError
|
||||
}
|
||||
}
|
||||
let offs = FixedArray::make(16, 0)
|
||||
for len = 1; len < 15; len = len + 1 {
|
||||
offs[len + 1] = offs[len] + count[len]
|
||||
}
|
||||
for i = 0; i < n; i = i + 1 {
|
||||
let l = lengths[start + i]
|
||||
if l != 0 {
|
||||
symbol[offs[l]] = i
|
||||
offs[l] = offs[l] + 1
|
||||
}
|
||||
}
|
||||
{ count, symbol }
|
||||
}
|
||||
|
||||
///|
|
||||
fn Inflate::decode(self : Inflate, h : Huffman) -> Int raise PngError {
|
||||
let mut code = 0
|
||||
let mut first = 0
|
||||
let mut index = 0
|
||||
for len = 1; len < 16; len = len + 1 {
|
||||
code = code | self.bits(1)
|
||||
let c = h.count[len]
|
||||
if code - c < first {
|
||||
return h.symbol[index + (code - first)]
|
||||
}
|
||||
index = index + c
|
||||
first = (first + c) << 1
|
||||
code = code << 1
|
||||
}
|
||||
raise PngError
|
||||
}
|
||||
|
||||
///|
|
||||
let len_base : FixedArray[Int] = [
|
||||
3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 35, 43, 51, 59, 67, 83,
|
||||
99, 115, 131, 163, 195, 227, 258,
|
||||
]
|
||||
|
||||
///|
|
||||
let len_extra : FixedArray[Int] = [
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5,
|
||||
5, 0,
|
||||
]
|
||||
|
||||
///|
|
||||
let dist_base : FixedArray[Int] = [
|
||||
1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 257, 385, 513, 769, 1025,
|
||||
1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577,
|
||||
]
|
||||
|
||||
///|
|
||||
let dist_extra : FixedArray[Int] = [
|
||||
0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11,
|
||||
12, 12, 13, 13,
|
||||
]
|
||||
|
||||
///| Decode one compressed block's symbols until end-of-block; `limit` caps the output size.
|
||||
fn Inflate::codes(
|
||||
self : Inflate,
|
||||
lencode : Huffman,
|
||||
distcode : Huffman,
|
||||
limit : Int,
|
||||
) -> Unit raise PngError {
|
||||
while true {
|
||||
let sym = self.decode(lencode)
|
||||
if sym < 256 {
|
||||
self.out.push(sym.to_byte())
|
||||
} else if sym == 256 {
|
||||
return
|
||||
} else {
|
||||
let s = sym - 257
|
||||
if s >= 29 {
|
||||
raise PngError
|
||||
}
|
||||
let len = len_base[s] + self.bits(len_extra[s])
|
||||
let ds = self.decode(distcode)
|
||||
if ds >= 30 {
|
||||
raise PngError
|
||||
}
|
||||
let dist = dist_base[ds] + self.bits(dist_extra[ds])
|
||||
let n = self.out.length()
|
||||
if dist > n || n + len > limit {
|
||||
raise PngError
|
||||
}
|
||||
for k = 0; k < len; k = k + 1 {
|
||||
self.out.push(self.out[n - dist + k])
|
||||
}
|
||||
}
|
||||
if self.out.length() > limit {
|
||||
raise PngError
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
fn Inflate::stored(self : Inflate) -> Unit raise PngError {
|
||||
self.bitbuf = 0
|
||||
self.bitcnt = 0
|
||||
if self.pos + 4 > self.src.length() {
|
||||
raise PngError
|
||||
}
|
||||
let len = self.src[self.pos].to_int() | (self.src[self.pos + 1].to_int() << 8)
|
||||
let nlen = self.src[self.pos + 2].to_int() | (self.src[self.pos + 3].to_int() << 8)
|
||||
self.pos = self.pos + 4
|
||||
if len != (nlen ^ 0xFFFF) || self.pos + len > self.src.length() {
|
||||
raise PngError
|
||||
}
|
||||
for k = 0; k < len; k = k + 1 {
|
||||
self.out.push(self.src[self.pos + k])
|
||||
}
|
||||
self.pos = self.pos + len
|
||||
}
|
||||
|
||||
///|
|
||||
fn Inflate::fixed(self : Inflate, limit : Int) -> Unit raise PngError {
|
||||
let l = FixedArray::make(288 + 30, 5)
|
||||
for i = 0; i < 288; i = i + 1 {
|
||||
l[i] = if i < 144 { 8 } else if i < 256 { 9 } else if i < 280 { 7 } else { 8 }
|
||||
}
|
||||
self.codes(Huffman::build(l, 0, 288), Huffman::build(l, 288, 30), limit)
|
||||
}
|
||||
|
||||
///|
|
||||
let code_order : FixedArray[Int] = [
|
||||
16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15,
|
||||
]
|
||||
|
||||
///|
|
||||
fn Inflate::dynamic(self : Inflate, limit : Int) -> Unit raise PngError {
|
||||
let nlen = self.bits(5) + 257
|
||||
let ndist = self.bits(5) + 1
|
||||
let ncode = self.bits(4) + 4
|
||||
if nlen > 286 || ndist > 30 {
|
||||
raise PngError
|
||||
}
|
||||
let lengths = FixedArray::make(320, 0)
|
||||
for i = 0; i < ncode; i = i + 1 {
|
||||
lengths[code_order[i]] = self.bits(3)
|
||||
}
|
||||
let lencode = Huffman::build(lengths, 0, 19)
|
||||
let mut idx = 0
|
||||
while idx < nlen + ndist {
|
||||
let sym = self.decode(lencode)
|
||||
if sym < 16 {
|
||||
lengths[idx] = sym
|
||||
idx = idx + 1
|
||||
} else {
|
||||
let mut len = 0
|
||||
let rep = if sym == 16 {
|
||||
if idx == 0 {
|
||||
raise PngError
|
||||
}
|
||||
len = lengths[idx - 1]
|
||||
3 + self.bits(2)
|
||||
} else if sym == 17 {
|
||||
3 + self.bits(3)
|
||||
} else {
|
||||
11 + self.bits(7)
|
||||
}
|
||||
if idx + rep > nlen + ndist {
|
||||
raise PngError
|
||||
}
|
||||
for k = 0; k < rep; k = k + 1 {
|
||||
lengths[idx] = len
|
||||
idx = idx + 1
|
||||
}
|
||||
}
|
||||
}
|
||||
if lengths[256] == 0 {
|
||||
raise PngError
|
||||
}
|
||||
let lc = Huffman::build(lengths, 0, nlen)
|
||||
let dc = Huffman::build(lengths, nlen, ndist)
|
||||
self.codes(lc, dc, limit)
|
||||
}
|
||||
|
||||
///| Inflate a zlib stream (header checked, checksum ignored), refusing output beyond `limit`.
|
||||
fn zlib_inflate(src : Bytes, limit : Int) -> Array[Byte] raise PngError {
|
||||
if src.length() < 2 || (src[0].to_int() & 0x0F) != 8 ||
|
||||
((src[0].to_int() << 8) | src[1].to_int()) % 31 != 0 {
|
||||
raise PngError
|
||||
}
|
||||
let s : Inflate = { src, pos: 2, bitbuf: 0, bitcnt: 0, out: [] }
|
||||
while true {
|
||||
let last = s.bits(1)
|
||||
match s.bits(2) {
|
||||
0 => s.stored()
|
||||
1 => s.fixed(limit)
|
||||
2 => s.dynamic(limit)
|
||||
_ => raise PngError
|
||||
}
|
||||
if s.out.length() > limit {
|
||||
raise PngError
|
||||
}
|
||||
if last == 1 {
|
||||
break
|
||||
}
|
||||
}
|
||||
s.out
|
||||
}
|
||||
|
||||
///|
|
||||
fn be32(b : Bytes, o : Int) -> Int {
|
||||
(b[o].to_int() << 24) | (b[o + 1].to_int() << 16) | (b[o + 2].to_int() << 8) |
|
||||
b[o + 3].to_int()
|
||||
}
|
||||
|
||||
///|
|
||||
fn paeth(a : Int, b : Int, c : Int) -> Int {
|
||||
let p = a + b - c
|
||||
let pa = (p - a).abs()
|
||||
let pb = (p - b).abs()
|
||||
let pc = (p - c).abs()
|
||||
if pa <= pb && pa <= pc {
|
||||
a
|
||||
} else if pb <= pc {
|
||||
b
|
||||
} else {
|
||||
c
|
||||
}
|
||||
}
|
||||
|
||||
///| Decode a PNG file into an RGBA pixmap; None when it is not a PNG this decoder handles.
|
||||
pub fn decode_png(data : Bytes) -> Pixmap? {
|
||||
decode_png_inner(data) catch {
|
||||
_ => None
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
fn decode_png_inner(data : Bytes) -> Pixmap? raise PngError {
|
||||
let sig : FixedArray[Int] = [137, 80, 78, 71, 13, 10, 26, 10]
|
||||
if data.length() < 33 {
|
||||
return None
|
||||
}
|
||||
for i = 0; i < 8; i = i + 1 {
|
||||
if data[i].to_int() != sig[i] {
|
||||
return None
|
||||
}
|
||||
}
|
||||
let mut w = 0
|
||||
let mut h = 0
|
||||
let mut depth = 0
|
||||
let mut ctype = 0
|
||||
let mut interlace = 0
|
||||
let mut palette : Array[Byte] = []
|
||||
let mut trns : Array[Byte] = []
|
||||
let idat : Array[Byte] = []
|
||||
let mut pos = 8
|
||||
while pos + 8 <= data.length() {
|
||||
let len = be32(data, pos)
|
||||
if len < 0 || pos + 12 + len > data.length() {
|
||||
return None
|
||||
}
|
||||
let kind = data[pos + 4:pos + 8].to_bytes()
|
||||
let body = pos + 8
|
||||
if kind == b"IHDR" && len >= 13 {
|
||||
w = be32(data, body)
|
||||
h = be32(data, body + 4)
|
||||
depth = data[body + 8].to_int()
|
||||
ctype = data[body + 9].to_int()
|
||||
interlace = data[body + 12].to_int()
|
||||
} else if kind == b"PLTE" {
|
||||
palette = data[body:body + len].to_bytes().to_array()
|
||||
} else if kind == b"tRNS" {
|
||||
trns = data[body:body + len].to_bytes().to_array()
|
||||
} else if kind == b"IDAT" {
|
||||
for k = 0; k < len; k = k + 1 {
|
||||
idat.push(data[body + k])
|
||||
}
|
||||
} else if kind == b"IEND" {
|
||||
break
|
||||
}
|
||||
pos = body + len + 4
|
||||
}
|
||||
if w <= 0 || h <= 0 || w > 1024 || h > 1024 || interlace != 0 {
|
||||
return None
|
||||
}
|
||||
let channels = match ctype {
|
||||
0 => 1
|
||||
2 => 3
|
||||
3 => 1
|
||||
4 => 2
|
||||
6 => 4
|
||||
_ => return None
|
||||
}
|
||||
let ok_depth = match ctype {
|
||||
0 | 3 => depth == 1 || depth == 2 || depth == 4 || depth == 8 || (ctype == 0 && depth == 16)
|
||||
_ => depth == 8 || depth == 16
|
||||
}
|
||||
if !ok_depth {
|
||||
return None
|
||||
}
|
||||
let bits_pp = channels * depth
|
||||
let bpp = if bits_pp < 8 { 1 } else { bits_pp / 8 }
|
||||
let stride = (w * bits_pp + 7) / 8
|
||||
let raw = zlib_inflate(Bytes::from_array(idat), h * (stride + 1))
|
||||
if raw.length() < h * (stride + 1) {
|
||||
return None
|
||||
}
|
||||
// Undo the per-row filters in place, into `cur` rows of `stride` bytes.
|
||||
let px = FixedArray::make(h * stride, b'\x00')
|
||||
for y = 0; y < h; y = y + 1 {
|
||||
let f = raw[y * (stride + 1)].to_int()
|
||||
let src = y * (stride + 1) + 1
|
||||
let row = y * stride
|
||||
for x = 0; x < stride; x = x + 1 {
|
||||
let a = if x >= bpp { px[row + x - bpp].to_int() } else { 0 }
|
||||
let b = if y > 0 { px[row - stride + x].to_int() } else { 0 }
|
||||
let c = if x >= bpp && y > 0 { px[row - stride + x - bpp].to_int() } else { 0 }
|
||||
let v = raw[src + x].to_int()
|
||||
let out = match f {
|
||||
0 => v
|
||||
1 => v + a
|
||||
2 => v + b
|
||||
3 => v + (a + b) / 2
|
||||
4 => v + paeth(a, b, c)
|
||||
_ => raise PngError
|
||||
}
|
||||
px[row + x] = (out & 0xFF).to_byte()
|
||||
}
|
||||
}
|
||||
let img = Pixmap::new(w, h)
|
||||
// Sample `i` (0-based within the row) of `n` bits, for 1/2/4/8-bit rows.
|
||||
let sample = fn(row : Int, i : Int) -> Int {
|
||||
if depth == 8 {
|
||||
px[row + i].to_int()
|
||||
} else if depth == 16 {
|
||||
px[row + i * 2].to_int()
|
||||
} else {
|
||||
let bit = i * depth
|
||||
let byte = px[row + bit / 8].to_int()
|
||||
(byte >> (8 - depth - bit % 8)) & ((1 << depth) - 1)
|
||||
}
|
||||
}
|
||||
let scale = fn(v : Int) -> Int {
|
||||
match depth {
|
||||
1 => v * 255
|
||||
2 => v * 85
|
||||
4 => v * 17
|
||||
_ => v
|
||||
}
|
||||
}
|
||||
for y = 0; y < h; y = y + 1 {
|
||||
let row = y * stride
|
||||
for x = 0; x < w; x = x + 1 {
|
||||
let (r, g, b, a) = match ctype {
|
||||
0 => {
|
||||
let v = scale(sample(row, x))
|
||||
(v, v, v, 255)
|
||||
}
|
||||
2 => (sample(row, x * 3), sample(row, x * 3 + 1), sample(row, x * 3 + 2), 255)
|
||||
3 => {
|
||||
let i = sample(row, x)
|
||||
if i * 3 + 2 >= palette.length() {
|
||||
(0, 0, 0, 0)
|
||||
} else {
|
||||
let a = if i < trns.length() { trns[i].to_int() } else { 255 }
|
||||
(palette[i * 3].to_int(), palette[i * 3 + 1].to_int(), palette[i * 3 + 2].to_int(), a)
|
||||
}
|
||||
}
|
||||
4 => {
|
||||
let v = sample(row, x * 2)
|
||||
(v, v, v, sample(row, x * 2 + 1))
|
||||
}
|
||||
_ =>
|
||||
(
|
||||
sample(row, x * 4),
|
||||
sample(row, x * 4 + 1),
|
||||
sample(row, x * 4 + 2),
|
||||
sample(row, x * 4 + 3),
|
||||
)
|
||||
}
|
||||
let o = (y * w + x) * 4
|
||||
img.data[o] = r.to_byte()
|
||||
img.data[o + 1] = g.to_byte()
|
||||
img.data[o + 2] = b.to_byte()
|
||||
img.data[o + 3] = a.to_byte()
|
||||
}
|
||||
}
|
||||
Some(img)
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
///|
|
||||
fn px(p : Pixmap, x : Int, y : Int) -> (Int, Int, Int, Int) {
|
||||
let o = (y * p.width + x) * 4
|
||||
(p.data[o].to_int(), p.data[o + 1].to_int(), p.data[o + 2].to_int(), p.data[o + 3].to_int())
|
||||
}
|
||||
|
||||
///|
|
||||
test "decode_png: RGBA rows using all five filters (fixed Huffman)" {
|
||||
let p = decode_png(b"\x89\x50\x4e\x47\x0d\x0a\x1a\x0a\x00\x00\x00\x0d\x49\x48\x44\x52\x00\x00\x00\x03\x00\x00\x00\x05\x08\x06\x00\x00\x00\x80\x71\x56\xa2\x00\x00\x00\x3e\x49\x44\x41\x54\x78\xda\x63\x60\x60\x60\xf8\xaf\xc1\xc0\x70\x38\x80\x81\xa1\x9d\x91\xdd\x08\xc4\x91\x3b\x02\xc2\x4c\x40\x0e\x03\xbb\x91\x1c\x10\xdb\x30\x30\xf3\xa5\x30\x34\x48\x48\xda\x3c\x92\x90\xf4\x7e\xc4\x02\x96\x61\x00\xca\x30\xd8\x30\x00\x00\x9a\x17\x0b\x5d\x72\xb0\x16\x16\x00\x00\x00\x00\x49\x45\x4e\x44\xae\x42\x60\x82").unwrap()
|
||||
assert_eq((p.width, p.height), (3, 5))
|
||||
for y = 0; y < 5; y = y + 1 {
|
||||
for x = 0; x < 3; x = x + 1 {
|
||||
assert_eq(px(p, x, y), ((x * 40 + y * 7) & 255, (y * 50) & 255, (x * y * 30) & 255, 255 - x * 60))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
test "decode_png: RGB with dynamic Huffman blocks" {
|
||||
let p = decode_png(b"\x89\x50\x4e\x47\x0d\x0a\x1a\x0a\x00\x00\x00\x0d\x49\x48\x44\x52\x00\x00\x00\x28\x00\x00\x00\x28\x08\x02\x00\x00\x00\x03\x9c\x2f\x3a\x00\x00\x00\x33\x49\x44\x41\x54\x78\xda\xed\xcd\xc1\x09\x00\x20\x0c\x00\xb1\x03\xa5\xfb\x8f\xec\x16\x0a\x12\xc8\x3f\xbb\x9a\xd6\x7d\xbb\x59\xf5\x80\x58\x2c\x16\x8b\xc5\x62\xb1\x58\x2c\x16\x8b\xc5\x62\xb1\xf8\xc7\xf8\x00\xd3\x7b\x15\x32\x6b\xd7\xe3\x22\x00\x00\x00\x00\x49\x45\x4e\x44\xae\x42\x60\x82").unwrap()
|
||||
assert_eq((p.width, p.height), (40, 40))
|
||||
for y = 0; y < 40; y = y + 1 {
|
||||
for x = 0; x < 40; x = x + 1 {
|
||||
assert_eq(px(p, x, y), (x * 6, y * 6, (x + y) * 3, 255))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
test "decode_png: 2-bit palette with tRNS, and stored grey+alpha" {
|
||||
let p = decode_png(b"\x89\x50\x4e\x47\x0d\x0a\x1a\x0a\x00\x00\x00\x0d\x49\x48\x44\x52\x00\x00\x00\x03\x00\x00\x00\x02\x02\x03\x00\x00\x00\xe0\x1a\x8e\x89\x00\x00\x00\x0c\x50\x4c\x54\x45\xff\x00\x00\x00\xff\x00\x00\x00\xff\x09\x09\x09\x5c\x71\x7e\x86\x00\x00\x00\x03\x74\x52\x4e\x53\xff\x80\x00\x7f\x6d\x68\x78\x00\x00\x00\x0c\x49\x44\x41\x54\x78\xda\x63\x90\x60\x7c\x02\x00\x01\x32\x00\xfe\x06\xf2\x6e\x18\x00\x00\x00\x00\x49\x45\x4e\x44\xae\x42\x60\x82").unwrap()
|
||||
assert_eq(px(p, 0, 0), (255, 0, 0, 255))
|
||||
assert_eq(px(p, 1, 0), (0, 255, 0, 128))
|
||||
assert_eq(px(p, 2, 0), (0, 0, 255, 0))
|
||||
assert_eq(px(p, 0, 1), (9, 9, 9, 255))
|
||||
assert_eq(px(p, 2, 1), (0, 255, 0, 128))
|
||||
let g = decode_png(b"\x89\x50\x4e\x47\x0d\x0a\x1a\x0a\x00\x00\x00\x0d\x49\x48\x44\x52\x00\x00\x00\x02\x00\x00\x00\x01\x08\x04\x00\x00\x00\x5e\x2b\xb7\x01\x00\x00\x00\x10\x49\x44\x41\x54\x78\x01\x01\x05\x00\xfa\xff\x00\x0a\x14\x1e\x28\x00\xcd\x00\x65\x7b\x27\x54\xba\x00\x00\x00\x00\x49\x45\x4e\x44\xae\x42\x60\x82").unwrap()
|
||||
assert_eq(px(g, 0, 0), (10, 10, 10, 20))
|
||||
assert_eq(px(g, 1, 0), (30, 30, 30, 40))
|
||||
}
|
||||
|
||||
///|
|
||||
test "decode_png refuses what isn't a PNG it can read" {
|
||||
assert_true(decode_png(b"not a png at all, not a png at all, nope").is_empty())
|
||||
assert_true(decode_png(b"").is_empty())
|
||||
// a truncated file
|
||||
let cut = b"\x89\x50\x4e\x47\x0d\x0a\x1a\x0a\x00\x00\x00\x0d\x49\x48\x44\x52\x00\x00\x00\x28\x00\x00\x00\x28\x08\x02\x00\x00\x00\x03\x9c\x2f\x3a\x00\x00\x00\x33\x49\x44\x41\x54\x78\xda\xed\xcd\xc1\x09\x00\x20\x0c\x00\xb1\x03\xa5\xfb\x8f\xec\x16\x0a\x12\xc8\x3f\xbb\x9a\xd6\x7d\xbb\x59\xf5\x80\x58\x2c\x16\x8b\xc5\x62\xb1\x58\x2c\x16"
|
||||
assert_true(decode_png(cut).is_empty())
|
||||
}
|
||||
|
||||
///|
|
||||
test "scale_pixmap averages down and draw_image blends the icon into the frame" {
|
||||
let src = Pixmap::new(4, 4)
|
||||
for i = 0; i < 16; i = i + 1 {
|
||||
src.data[i * 4] = b'\xff'
|
||||
src.data[i * 4 + 3] = if i % 2 == 0 { b'\xff' } else { b'\x00' }
|
||||
}
|
||||
let s = scale_pixmap(src, 2, 2)
|
||||
// half the source pixels are opaque red: the result is red at half alpha
|
||||
assert_eq(px(s, 0, 0), (255, 0, 0, 127))
|
||||
let h = add_image(src)
|
||||
assert_true(h != 0U)
|
||||
let dst = Pixmap::new(10, 10)
|
||||
draw_ops(dst, [Image({ x: 2, y: 2, w: 4, h: 4 }, h)], 0, 0)
|
||||
assert_eq(px(dst, 2, 2), (255, 0, 0, 255))
|
||||
assert_eq(px(dst, 3, 2).3, 0)
|
||||
assert_eq(px(dst, 0, 0), (0, 0, 0, 0))
|
||||
// unknown handles draw nothing
|
||||
draw_ops(dst, [Image({ x: 0, y: 0, w: 2, h: 2 }, 9999U)], 0, 0)
|
||||
assert_eq(px(dst, 0, 0), (0, 0, 0, 0))
|
||||
}
|
||||
@@ -668,6 +668,8 @@ pub fn draw_ops(
|
||||
PushClip(r) =>
|
||||
clip_stack.push(@scene.Rect::new(r.x + ox, r.y + oy, r.w, r.h))
|
||||
PopClip => if clip_stack.length() > 0 { ignore(clip_stack.pop()) }
|
||||
Image(r, handle) =>
|
||||
draw_image(dst, handle, @scene.Rect::new(r.x + ox, r.y + oy, r.w, r.h), cur_clip)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user