Skip to content

Pixels::set builds no mipmaps, so a streamed image drawn well below its size aliases #1385

Done

Description

@wmfeht

Context: Strata (lgse/strata), a file manager on Rune, streams thumbnails into one Pixels handle per file (the handle stays, the image arrives later) and draws them at whatever size the grid uses.

What's missing (67d73a5f). Pixels::set (crates/rune-image/src/pixels.rs:95-106) is the streaming path and clears frame.mips = None (:103; set_mips at :108-118 is pub(crate)), so a 256 px thumbnail drawn at 37 pt samples bilinearly and aliases. ImageCache images get mips, but ImageCache::rgba (crates/rune-image/src/lib.rs:310-318) makes a new handle for each call (keyed by the bytes) and fills its mips later, so a store that keeps one handle per file can't use it: each landed image would be a new handle, and the view would commit to swap it in.

Repro (headless; ran at 2a44156 with --snap out.png --scale 1 --at 0.5, source unchanged at 67d73a5f): a 256 × 256 one-pixel checkerboard drawn at 37 × 37 pt, streamed through Pixels::set (left) and through ImageCache::rgba of the same bytes (right).

use rune::{App, AppSpec, Cx, El, Image, ImageCache, Pixels, Waker, el, row};
struct S { streamed: Pixels, cached: Option<Image> }
fn board() -> Vec<u8> {
    (0..256u32 * 256).flat_map(|i| { let v = if (i % 256 + i / 256) % 2 == 0 { 0 } else { 255 }; [v, v, v, 255] }).collect()
}
fn init() -> S { let p = Pixels::new(); p.set(256, 256, board()); S { streamed: p, cached: None } }
fn setup(s: &mut S, waker: Waker) {
    let img = ImageCache::new(waker).rgba(256, 256, board());
    let _ = img.wait();
    s.cached = Some(img);
}
fn view(s: &S, _: &mut Cx<S>) -> El {
    let mut r = row().w_full().h_full().child(el().size(37.0, 37.0).pixels(&s.streamed)).child(el().size(13.0, 37.0));
    if let Some(i) = &s.cached { r = r.child(el().size(37.0, 37.0).pixels(i.pixels())); }
    r
}
fn main() {
    let mut spec = AppSpec::new("mips", (87.0, 37.0), init, view);
    spec.setup = Some(setup);
    App::new(spec).run();
}

The streamed square shows a beat pattern of near-black and white blocks (grey levels 7 to 248, standard deviation 37 of 255, inside its edge); the cached one is an even grey (188, no deviation). A 256 → 32 draw (an exact eighth) hides it.

Ask. A mipmapped set (levels built off the main thread), or ImageCache::rgba_into(&Pixels, …) that fills an existing handle.

Related: #163 (atlas pages keep a CPU copy), #148 (reading Pixels back), #702, #1042.

Activity

  1. Hona commented on Nov 24, 2026

    @Hona
    Member

    Added ImageCache::rgba_into on dev in #1907 (commit 1f6dd178b).

    use std::sync::Arc;
    use rune::{ImageCache, Pixels};
    
    let cache = ImageCache::new(Arc::new(|| {}));
    let thumb = Pixels::new();
    cache.rgba_into(&thumb, 256, 256, rgba_bytes);

    This follows Rune's ImageCache::rgba linear-light mip pipeline and latest-wins request discipline while keeping Pixels::set as the zero-allocation unmipped streaming path. rgba_into updates an existing Pixels handle in place without creating a new Image or content-hashing the bytes: images up to 64×64 finish inline on native, larger images build linear-premultiplied mipmaps on the shared decoder workers, and wasm32 builds them in main-thread slices. A monotonic per-handle ticket ensures a slower worker never overwrites a newer Pixels::set, Pixels::replace, or rgba_into call, and dropped handles skip pending work. Passing zero width or height with empty bytes clears the handle; oversized (> 8192 px) or mismatched-length inputs are ignored without modifying target.

    You can compare unmipped Pixels::replace vs mipmapped ImageCache::rgba_into on a 256×256 checkerboard downscaled to 37×37 pt in examples/matched (--scene mips).

    Verified on Windows (crates/rune-image 63 passed + 3 doctests, crates/rune/tests/pixels_streaming.rs, examples/matched tests), macOS arm64 (crates/rune-image 64 passed + 3 doctests), Linux aarch64 compile check, and wasm32 compile checks (cargo check-wasm, cargo check-webgl). Not run: a live desktop window or browser execution of the wasm32 sliced path.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions