Skip to main content

leaf_raster/
load.rs

1//! Decoding image files — raster formats through the `image` codecs, SVG
2//! through resvg — to the `DynamicImage` every consumer's pipeline speaks.
3
4use std::path::Path;
5
6/// Decode an image file to a `DynamicImage`, or `None` on any failure (missing,
7/// unreadable, or a format no decoder covers). SVG is rasterized with resvg
8/// ([`load_svg`]); every raster format goes through the `image` codec crate.
9pub fn load_image(path: &Path) -> Option<image::DynamicImage> {
10    if path
11        .extension()
12        .is_some_and(|e| e.eq_ignore_ascii_case("svg"))
13    {
14        return load_svg(&std::fs::read(path).ok()?);
15    }
16    image::ImageReader::open(path)
17        .ok()?
18        .with_guessed_format()
19        .ok()?
20        .decode()
21        .ok()
22}
23
24/// Rasterize an SVG's bytes to a `DynamicImage`, or `None` if it won't parse.
25///
26/// The `image` crate has no SVG support, so this is the vector path: usvg parses
27/// the document, resvg paints it onto a tiny-skia pixmap, and we hand the pixels
28/// back as an `RgbaImage` the rest of the pipeline treats like any decoded
29/// raster. We render at a fixed target resolution (scaling the SVG's own size so
30/// its longer side is ~[`SVG_TARGET_PX`]) rather than its intrinsic size: an SVG
31/// may declare a tiny viewport, and rasterizing that small would leave the
32/// consumer upscaling a blurry thumbnail. System fonts are loaded so an SVG that
33/// draws real `<text>` (not outlined paths) still renders its glyphs.
34pub fn load_svg(data: &[u8]) -> Option<image::DynamicImage> {
35    /// The longer side, in pixels, we rasterize an SVG to before the consumer
36    /// downscales it — big enough to stay crisp, capped so a huge viewport can't
37    /// blow up the allocation.
38    const SVG_TARGET_PX: f32 = 640.0;
39
40    let tree = svg_tree(data)?;
41    let size = tree.size();
42    let longest = size.width().max(size.height()).max(1.0);
43    // Scale so the longer side hits the target; clamp so a big SVG scales down
44    // and a small one up, but neither runs away. Never below 1px per side.
45    let scale = (SVG_TARGET_PX / longest).clamp(0.05, 16.0);
46    render_svg(&tree, scale)
47}
48
49/// Rasterize an SVG's bytes at `scale` times its own declared size — the
50/// vector path for a picture whose size is *meant*: a typeset formula
51/// (`leaf-math`) writes its root in pixels at the font size it was set at,
52/// and drawing it at anything else puts the glyphs at the wrong size. Where
53/// [`load_svg`] picks a resolution for a picture of unknown intent, this
54/// takes the picture's word for it. `None` if the document won't parse.
55pub fn rasterize_svg(data: &[u8], scale: f32) -> Option<image::DynamicImage> {
56    let tree = svg_tree(data)?;
57    render_svg(&tree, scale.clamp(0.05, 16.0))
58}
59
60/// Parse an SVG document with the system fonts on hand, so one that draws real
61/// `<text>` (not outlined paths) still renders its glyphs.
62fn svg_tree(data: &[u8]) -> Option<resvg::usvg::Tree> {
63    use resvg::usvg;
64
65    // The system font set, enumerated once per process rather than once per
66    // SVG — loading it is tens of milliseconds of directory walking, and it
67    // runs on the render path. usvg shares the database by `Arc`, so every
68    // decode after the first borrows the same one.
69    fn svg_fontdb() -> std::sync::Arc<resvg::usvg::fontdb::Database> {
70        use std::sync::{Arc, OnceLock};
71        static FONTS: OnceLock<Arc<resvg::usvg::fontdb::Database>> = OnceLock::new();
72        FONTS
73            .get_or_init(|| {
74                let mut db = resvg::usvg::fontdb::Database::new();
75                db.load_system_fonts();
76                Arc::new(db)
77            })
78            .clone()
79    }
80
81    let opt = usvg::Options {
82        fontdb: svg_fontdb(),
83        ..Default::default()
84    };
85    usvg::Tree::from_data(data, &opt).ok()
86}
87
88/// Paint a parsed SVG at `scale` onto a pixmap and hand the pixels back as
89/// straight-alpha RGBA.
90fn render_svg(tree: &resvg::usvg::Tree, scale: f32) -> Option<image::DynamicImage> {
91    use resvg::tiny_skia;
92
93    let size = tree.size();
94    let w = (size.width() * scale).ceil().max(1.0) as u32;
95    let h = (size.height() * scale).ceil().max(1.0) as u32;
96
97    let mut pixmap = tiny_skia::Pixmap::new(w, h)?;
98    resvg::render(
99        tree,
100        tiny_skia::Transform::from_scale(scale, scale),
101        &mut pixmap.as_mut(),
102    );
103
104    // tiny-skia stores premultiplied alpha; `image` expects straight alpha, so
105    // demultiply each pixel on the way into the RGBA buffer.
106    let mut rgba = Vec::with_capacity((w as usize) * (h as usize) * 4);
107    for px in pixmap.pixels() {
108        let c = px.demultiply();
109        rgba.extend_from_slice(&[c.red(), c.green(), c.blue(), c.alpha()]);
110    }
111    Some(image::DynamicImage::ImageRgba8(image::RgbaImage::from_raw(
112        w, h, rgba,
113    )?))
114}
115
116#[cfg(test)]
117mod tests {
118    use super::*;
119
120    #[test]
121    fn load_svg_rasterizes_to_straight_alpha_rgba() {
122        // A 20×10 solid-red rect. `image` can't decode SVG at all, so this only
123        // works via the resvg path.
124        let svg = br##"<svg xmlns="http://www.w3.org/2000/svg" width="20" height="10"><rect width="20" height="10" fill="#ff0000"/></svg>"##;
125        let img = load_svg(svg).expect("valid SVG should rasterize");
126        // Rendered at the target resolution, so upscaled from its 20×10 viewport
127        // while keeping the 2:1 aspect.
128        assert!(
129            img.width() >= 20 && img.height() >= 10,
130            "got {}×{}",
131            img.width(),
132            img.height()
133        );
134        assert_eq!(img.width(), img.height() * 2, "aspect ratio preserved");
135        // The fill lands as opaque, straight-alpha red — not premultiplied mush.
136        let rgba = img.to_rgba8();
137        let center = rgba.get_pixel(rgba.width() / 2, rgba.height() / 2).0;
138        assert_eq!(center, [255, 0, 0, 255], "center pixel is opaque red");
139    }
140
141    #[test]
142    fn load_svg_rejects_garbage() {
143        assert!(load_svg(b"not an svg at all").is_none());
144        assert!(rasterize_svg(b"not an svg at all", 1.0).is_none());
145    }
146
147    #[test]
148    fn rasterize_svg_takes_the_picture_s_own_size_times_the_scale() {
149        let svg = br##"<svg xmlns="http://www.w3.org/2000/svg" width="20" height="10"><rect width="20" height="10" fill="#ff0000"/></svg>"##;
150        let one = rasterize_svg(svg, 1.0).unwrap();
151        assert_eq!((one.width(), one.height()), (20, 10));
152        let two = rasterize_svg(svg, 2.0).unwrap();
153        assert_eq!((two.width(), two.height()), (40, 20));
154        // Outside the drawing the pixels are clear, not painted over.
155        let svg = br##"<svg xmlns="http://www.w3.org/2000/svg" width="20" height="10"><rect width="10" height="10" fill="#ff0000"/></svg>"##;
156        let img = rasterize_svg(svg, 1.0).unwrap().to_rgba8();
157        assert_eq!(
158            img.get_pixel(15, 5).0[3],
159            0,
160            "transparent where nothing is drawn"
161        );
162    }
163}