Skip to content

feat(image): expose Image::intrinsic_size and scaled raster decoding via ImageCache::load_scaled - #1951

Merged
Hona merged 1 commit into
devfrom
feat/1361-svg-intrinsic-size-load-scaled
Nov 24, 2026
Merged

Hona merged 1 commit into
devfrom
feat/1361-svg-intrinsic-size-load-scaled

Conversation

@Hona

@Hona Hona commented Nov 24, 2026

Copy link
Copy Markdown
Member

Fixes #1361
Fixes #702

Summary

  • Image::intrinsic_size(&self) -> Option<[f32; 2]> (crates/rune-image/src/lib.rs, svg.rs, from_url.rs):
    • Captures usvg::Tree::size() inside svg_rgba_with_size during existing SVG parsing across every SVG decode path (load, svg, load_mime, file, url, and load_scaled, on native and wasm32).
    • Preserves non-integer CSS-pixel dimensions independently of the 2x default supersampling, the 4096 px raster cap, or custom target sizes; falls back to mip 0 dimensions as [f32; 2] for rasters and returns None while pending or on error. Image::size() is unchanged.
  • ImageCache::load_scaled(&self, bytes: impl Into<Arc<[u8]>>, size_pt: f32, scale: f32) -> Image (crates/rune-image/src/lib.rs, mips.rs, from_url.rs):
    • Content-addressed by (bytes, max_px) where max_px = (size_pt * scale).ceil().clamp(1.0, 8192.0) (non-finite inputs clamp to 1).
    • Decodes rasters using only frame 0 (skipping multi-frame GIF/WebP/APNG animation decode so thumbnails and notification icons never schedule idle frames), applies EXIF orientation, and aspect-preservingly downsamples in linear-premultiplied light (never upscaling, both sides >= 1) before generating mipmaps.
    • On wasm32, browser-decoded formats downsample in from_url::land_in_slices before sliced mip generation; SVG/PNG/ICNS decode through decode_scaled.
  • Example & docs: added the rune-image--decode story in examples/kit-gallery/src/stories/images.rs, ?image-scaled-proof in examples/web-hello/src/image_proof.rs, rustdoc examples, crates/rune/README.md, WEB.md, examples/README.md, and changelog/unreleased/2026-11-24-svg-intrinsic-size-and-load-scaled.md.

Architecture & Usage

flowchart LR
    A[ImageCache::load_scaled] --> B{is_svg?}
    B -- yes --> C[svg_rgba_with_size at max_px]
    C --> D[mip_chain + intrinsic: Some]
    B -- no --> E[Still frame-0 decode + EXIF orient]
    E --> F{longest > max_px?}
    F -- yes --> G[Linear-premultiplied area downsample]
    F -- no --> H[Keep original pixels]
    G --> I[mip_chain + intrinsic: None]
    H --> I
Loading
let cache = rune::ImageCache::new(waker);

// SVG: rasterized 2x (32x16), intrinsic CSS-pixel dimensions stay 16x8
let badge = cache.load(svg_bytes);
assert_eq!(badge.size(), Some([32, 16]));
assert_eq!(badge.intrinsic_size(), Some([16.0, 8.0]));

// Raster: 512x256 PNG decoded for a 32 pt slot at 2x -> 64x32, first frame only
let thumb = cache.load_scaled(png_bytes, 32.0, 2.0);
assert_eq!(thumb.size(), Some([64, 32]));

Outside Standards Followed

  • Intrinsic SVG sizing: HTML HTMLImageElement.naturalWidth / naturalHeight and SVG 2 / CSS Images intrinsic sizing (resolved by usvg::Tree::size()).
  • Scaled raster decode: ImageCache::svg(bytes, size_pt, scale) / ImageCache::app_icon_scaled API shape alongside browser createImageBitmap resize and Apple CGImageSourceCreateThumbnailAtIndex first-frame, orientation-applied, never-upscale thumbnail semantics.

@Hona
Hona force-pushed the feat/1361-svg-intrinsic-size-load-scaled branch from 1d1d8fa to 81b5ba7 Compare November 24, 2026 14:46
@Hona
Hona force-pushed the feat/1361-svg-intrinsic-size-load-scaled branch from 81b5ba7 to 3137fb1 Compare November 24, 2026 14:50
@Hona
Hona merged commit 4fcc338 into dev Nov 24, 2026
1 check passed
@Hona
Hona deleted the feat/1361-svg-intrinsic-size-load-scaled branch November 24, 2026 14:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ImageCache: expose intrinsic SVG dimensions independently of raster size Images: rasters decode at full resolution whatever size they draw at

1 participant