From b36c98b418bbf57d7e155e28dc6b33f805fe2f51 Mon Sep 17 00:00:00 2001 From: shinyobjectz Date: Wed, 18 Nov 2026 12:08:31 -0800 Subject: [PATCH] The window's memory: a picture decoded at the size its URL asks (raea-w, raea-h) A feed card drew a 2560x1440 still at 720 points at most, and raea's window held it decoded at full size: 14.7 MB of RGBA a still, and the window grew 25 MB for every still scrolled past (raea-feed e2e/memory.spec.ts: 1.1 GB open, 2.0 GB after 60 stills, 2.5 GB after opening a few). blitz-dom's image decode now fits a picture whose URL's query asks raea-w=N and/or raea-h=N to that box, its shape kept and never larger, area-averaged on premultiplied pixels when any is see-through. The servers ignore the query (the app's node server returned the same bytes; raea-page's file loads read url.path()), and so does a browser. The feed's cards ask theirs at the card's size. --- vendor/blitz-dom/src/net.rs | 65 +++++++++++++++++++++++++++++++++++-- vendor/vendor.toml | 2 +- 2 files changed, 63 insertions(+), 4 deletions(-) diff --git a/vendor/blitz-dom/src/net.rs b/vendor/blitz-dom/src/net.rs index 4936f96f..3507b8bc 100644 --- a/vendor/blitz-dom/src/net.rs +++ b/vendor/blitz-dom/src/net.rs @@ -580,9 +580,50 @@ impl NetHandler for ResourceHandler { } } +/// raea: a picture asked for at the size it's drawn: `raea-w=N` and/or `raea-h=N` in its URL's query decode it scaled +/// down to fit N pixels wide or high, its shape kept, never scaled up (the server doesn't read the query). The feed's +/// cards ask theirs at the card's size (raea-feed src/feed/media.ts): a 2560x1440 still held 14.7 MB decoded on a card +/// drawn 1440 pixels wide at most, 4.7 MB fitted (raea-feed e2e/memory.spec.ts). Area-averaged (`thumbnail`), on +/// premultiplied pixels when any is see-through, so no edge takes the colour of the transparent pixels beside it. +pub fn fit(url: &str, w: u32, h: u32, mut rgba: Vec) -> (u32, u32, Vec) { + let query = url.split('#').next().and_then(|u| u.split_once('?')).map_or("", |(_, q)| q); + let want = |k: &str| query.split('&').find_map(|kv| kv.strip_prefix(k)?.strip_prefix('=')?.parse::().ok()).filter(|n| *n > 0); + let scale = [want("raea-w").map(|n| n as f64 / w.max(1) as f64), want("raea-h").map(|n| n as f64 / h.max(1) as f64)] + .into_iter() + .flatten() + .fold(1.0f64, f64::min); + if scale >= 1.0 || w == 0 || h == 0 || rgba.len() != w as usize * h as usize * 4 { + return (w, h, rgba); + } + let (nw, nh) = (((w as f64 * scale).round() as u32).max(1), ((h as f64 * scale).round() as u32).max(1)); + let see_through = rgba.chunks_exact(4).any(|p| p[3] != 255); + if see_through { + for p in rgba.chunks_exact_mut(4) { + let a = p[3] as u32; + for c in &mut p[..3] { + *c = ((*c as u32 * a + 127) / 255) as u8; + } + } + } + let Some(img) = image::RgbaImage::from_raw(w, h, rgba) else { unreachable!("the length was checked") }; + let mut out = image::imageops::thumbnail(&img, nw, nh).into_raw(); + if see_through { + for p in out.chunks_exact_mut(4) { + let a = p[3] as u32; + if a > 0 { + for c in &mut p[..3] { + *c = ((*c as u32 * 255 + a / 2) / a).min(255) as u8; + } + } + } + } + (nw, nh, out) +} + impl ImageHandler { fn parse(&self, bytes: Bytes, url: &str) -> Result { if let Some((width, height, rgba)) = IMAGE_DECODER.get().and_then(|d| d(url, &bytes)) { + let (width, height, rgba) = fit(url, width, height, rgba); return Ok(Resource::Image(self.kind, width, height, Arc::new(rgba))); } let mut reader = image::ImageReader::new(Cursor::new(&bytes)) @@ -597,9 +638,7 @@ impl ImageHandler { } let image_err = match reader.decode() { Ok(image) => { - let width = image.width(); - let height = image.height(); - let raw_rgba8_data = image.into_rgba8().into_raw(); + let (width, height, raw_rgba8_data) = fit(url, image.width(), image.height(), image.into_rgba8().into_raw()); return Ok(Resource::Image( self.kind, width, @@ -631,6 +670,26 @@ impl ImageHandler { #[cfg(test)] mod tests { use super::*; + + #[test] + fn a_picture_fits_the_size_its_url_asks() { + let px = |w: u32, h: u32| vec![255u8; (w * h * 4) as usize]; + // the width asked, the shape kept + let (w, h, b) = fit("http://127.0.0.1:9/media/a.png?raea-w=100", 400, 200, px(400, 200)); + assert_eq!((w, h, b.len()), (100, 50, 100 * 50 * 4)); + // the tighter of a width and a height; a fragment after the query is no part of it + assert_eq!(fit("/media/a.png?x=1&raea-w=300&raea-h=50#t", 400, 200, px(400, 200)).0, 100); + // never larger, and nothing asked is the picture as it is + assert_eq!(fit("/media/a.png?raea-w=800", 400, 200, px(400, 200)).0, 400); + assert_eq!(fit("/media/a.png", 400, 200, px(400, 200)).0, 400); + assert_eq!(fit("/media/a.png?raea-wide=100", 400, 200, px(400, 200)).0, 400); + // a see-through pixel beside an opaque red one keeps no colour of its own in the average + let mut two = vec![255, 0, 0, 255, 0, 255, 0, 0]; + two.extend_from_slice(&[255, 0, 0, 255, 0, 255, 0, 0]); + let (_, _, one) = fit("/a.png?raea-w=1", 2, 2, two); + assert_eq!(one.len(), 4); + assert!(one[1] < 8 && one[0] > 240, "green bled into red: {one:?}"); + } use parley::fontique::FontStyle as Fq; use style::values::specified::Angle; diff --git a/vendor/vendor.toml b/vendor/vendor.toml index 1b7ea35d..e94a3c84 100644 --- a/vendor/vendor.toml +++ b/vendor/vendor.toml @@ -70,7 +70,7 @@ exit = "a Blitz release that draws these" path = "vendor/blitz-dom" upstream = "crates.io:0.3.0-beta.4" kind = "patch" -reason = "inline flex/grid baselines, auto table columns, table row rects, inline image load events, SVG text fonts, SVG non-scaling strokes, url() filters; Blitz main's #1244 and #1246; a node removed in a mutation batch dropped from the style, form and title nodes the batch flushes (raea #24); a node in a 3D rendering context (or with a projection) placed again on every resolve, so it follows a preserve-3d parent whose transform a script set (raea #19); a ParseErrorReporter (not_applied.rs) on every author parse, keeping what stylo drops for the render to say once (raea #34); a bevy_text box whose lines are no finite size (an Apple bitmap-only face, which parley scales by a units-per-em of 0) measures 0 by 0, as bevy_text does; a family list ending in ; getBoundingClientRect and the geometry walks stop at a freed containing-block link instead of panicking on its key, and an element in a display: none subtree answers a zero rect (raea #108)"raea-bevy fallback" (bevy_text's FontSource::Family) falls to the collection's fallback families, not to serif nor to the CoreText pick (parley: NO_HOOK_FAMILY); an out-of-flow box painted by the stacking context between it and its containing block, inside its opacity group (raea #39); an SVG's paint properties from CSS written into its serialized source for usvg (raea #45); transform-box: stroke-box as fill-box under non-scaling-stroke, a stylesheet's stroke in the stroke box, and the non-scaling mark on an element with its own id (raea #50); an SVG element's rewritten style attribute without a leading `;` when its own declarations were transforms alone; transform-box: view-box's reference box at the origin of user space, not at the viewBox's x and y; every inline box laid out in full while its line is only measured, since Taffy's measure cache keeps a box's size without its baseline (raea #109); an inline element has a border edge only where its border has a style, so a plain span (border-style none, its width still `medium`) doesn't split the shaping at its edges and lose the kerning across them (docs/layout-reads.feature LR10); an app-bundled svg face of a family the page already gave is left out, as one given before the page's is removed (raea/text loadFont fetches Google's faces at import, LR10); getBoundingClientRect through a perspective's projection (the node's Projection), and transformed_rect for a rect inside a node's box (LR10)" +reason = "inline flex/grid baselines, auto table columns, table row rects, inline image load events, SVG text fonts, SVG non-scaling strokes, url() filters; Blitz main's #1244 and #1246; a node removed in a mutation batch dropped from the style, form and title nodes the batch flushes (raea #24); a node in a 3D rendering context (or with a projection) placed again on every resolve, so it follows a preserve-3d parent whose transform a script set (raea #19); a ParseErrorReporter (not_applied.rs) on every author parse, keeping what stylo drops for the render to say once (raea #34); a bevy_text box whose lines are no finite size (an Apple bitmap-only face, which parley scales by a units-per-em of 0) measures 0 by 0, as bevy_text does; a family list ending in ; getBoundingClientRect and the geometry walks stop at a freed containing-block link instead of panicking on its key, and an element in a display: none subtree answers a zero rect (raea #108)"raea-bevy fallback" (bevy_text's FontSource::Family) falls to the collection's fallback families, not to serif nor to the CoreText pick (parley: NO_HOOK_FAMILY); an out-of-flow box painted by the stacking context between it and its containing block, inside its opacity group (raea #39); an SVG's paint properties from CSS written into its serialized source for usvg (raea #45); transform-box: stroke-box as fill-box under non-scaling-stroke, a stylesheet's stroke in the stroke box, and the non-scaling mark on an element with its own id (raea #50); an SVG element's rewritten style attribute without a leading `;` when its own declarations were transforms alone; transform-box: view-box's reference box at the origin of user space, not at the viewBox's x and y; every inline box laid out in full while its line is only measured, since Taffy's measure cache keeps a box's size without its baseline (raea #109); an inline element has a border edge only where its border has a style, so a plain span (border-style none, its width still `medium`) doesn't split the shaping at its edges and lose the kerning across them (docs/layout-reads.feature LR10); an app-bundled svg face of a family the page already gave is left out, as one given before the page's is removed (raea/text loadFont fetches Google's faces at import, LR10); getBoundingClientRect through a perspective's projection (the node's Projection), and transformed_rect for a rect inside a node's box (LR10); a picture fitted to the raea-w/raea-h its URL asks (net.rs fit)" exit = "a Blitz release with #1244/#1246 and these" [parley_engine]