Skip to content

Images: rasters decode at full resolution whatever size they draw at #702

Done

Description

@purian23

Rasters are decoded at the file's full resolution, whatever size they're drawn at. A 512 px PNG drawn at 64 px keeps 512×512 RGBA (1 MiB, plus mips) in the cache and the atlas, where 64×64 would do. Icons, avatars and notification images are almost always drawn far smaller than their files.

Proposal: ImageCache::load_scaled(bytes, size_pt, scale) decodes and downsamples to the draw size in device pixels, and builds the mip chain from there. An animated image decodes just its first frame unless the caller wants it animated. Otherwise an animated notification image keeps every frame at full size and wakes the app at the GIF's rate.

We use it for tray icons and notification images in our fork.

Activity

  1. Hona commented on Nov 24, 2026

    @Hona
    Member

    Added on dev in #1951 (merge commit 4fcc338ec).

    ImageCache::load_scaled(&self, bytes: impl Into<Arc<[u8]>>, size_pt: f32, scale: f32) -> Image decodes and downsamples raster images to fit (size_pt * scale).ceil().clamp(1.0, 8192.0) device pixels (preserving aspect ratio, never upscaling, applying EXIF orientation first, and decoding only frame 0 of animated GIF/WebP/APNG so thumbnails and notification icons never schedule idle frames) before building mipmaps in linear-premultiplied light. SVGs and SVGZs rasterize directly to that target size while preserving document dimensions on Image::intrinsic_size(). Handles are content-addressed by (bytes, max_px).

    let cache = rune::ImageCache::new(waker);
    let thumb = cache.load_scaled(png_bytes, 32.0, env.scale); // e.g. 512x256 -> 64x32 at 2x
    assert_eq!(thumb.wait(), Ok([64, 32]));

    This follows Rune's ImageCache::svg / ImageCache::app_icon_scaled API shape and browser createImageBitmap / Apple CGImageSourceCreateThumbnailAtIndex first-frame, orientation-applied, never-upscale thumbnail semantics. You can compare full vs scaled decodes interactively in examples/kit-gallery (--scene rune-image--decode).

    Verified with rune-image unit tests and doctests on Windows and macOS (including --no-default-features), a Linux aarch64 compile check, rune and rune-kit image tests on Windows, cargo check-wasm / cargo check-webgl, and a headless Chromium WebGL2 scenario covering both browser-decoded JPEG and Rust-decoded PNG downsampling. Not verified in a live interactive window, on a hardware browser GPU, on Safari/Firefox, or on iOS (browser check used software rendering in Chromium without KHR_parallel_shader_compile).

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