mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
Render zoomed-out viewport pages through the reduced composite
Pages reduced by k > 1 whose source starts on a multiple of k now use the band/tile reduced render (one-colour tile skipping, parallel tiles) on a factor-aligned cell grid instead of compositing every pixel at full size and averaging. Unaligned sources keep the averaging path, which tests use as the reference (U8/U16/F32, k = 2, 4, 16, max difference < 1e-4). The reduced render now checks the page's interrupt between bands.
This commit is contained in:
1 parent
66e9325379
commit
0f4e462570
2 files changed
+126
-11
No files matched your search
@@ -2,6 +2,7 @@ use super::content::RENDER_TILE;
|
||||
use super::ctx::Ctx;
|
||||
use super::render::{band_rows_for, render, render_bands, render_tiled_with};
|
||||
use super::{Buffer, Document, Rect, Rgba8Image, pattern, proxy, uniform};
|
||||
use photocraft_raster::Interrupt;
|
||||
|
||||
/// Documents above this many pixels get thumbnails from a proxy (see [`proxy`]) when that is
|
||||
/// faithful; smaller ones are reduced from the exact composite.
|
||||
@@ -101,23 +102,66 @@ pub(crate) fn render_reduced_with(
|
||||
uniform: bool,
|
||||
stats: &mut ReducedStats,
|
||||
) -> Buffer {
|
||||
match render_reduced_cells(doc, b, Cells::Fit(w, h), damage, band_rows, uniform, stats, Interrupt::NONE) {
|
||||
Ok(buf) => buf,
|
||||
// Only an interrupt fails a reduction, and this one has none.
|
||||
Err(_) => Buffer::transparent(Rect::EMPTY),
|
||||
}
|
||||
}
|
||||
|
||||
/// How a reduced render maps document pixels of its area to output pixels.
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
pub(crate) enum Cells {
|
||||
/// `w`×`h` output pixels spread evenly over the area ([`render_reduced`]).
|
||||
Fit(u32, u32),
|
||||
/// Output pixel `t` covers the area's pixels `[t·k, (t+1)·k)`, clipped to the area: a
|
||||
/// globally aligned reduction by `k` when the area starts on a multiple of `k`
|
||||
/// ([`crate::viewport::render_page`]).
|
||||
Factor(u32),
|
||||
}
|
||||
|
||||
/// [`render_reduced_with`] on a cell grid, checking `interrupt` between bands.
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
pub(crate) fn render_reduced_cells(
|
||||
doc: &Document,
|
||||
b: Rect,
|
||||
cells: Cells,
|
||||
damage: Option<Rect>,
|
||||
band_rows: i32,
|
||||
uniform: bool,
|
||||
stats: &mut ReducedStats,
|
||||
interrupt: Interrupt<'_>,
|
||||
) -> Result<Buffer, String> {
|
||||
let (fw, fh) = (b.width() as usize, b.height() as usize);
|
||||
let (w, h) = (w.clamp(1, b.width().max(1)) as usize, h.clamp(1, b.height().max(1)) as usize);
|
||||
let (w, h, k) = match cells {
|
||||
Cells::Fit(w, h) => (w.clamp(1, b.width().max(1)) as usize, h.clamp(1, b.height().max(1)) as usize, None),
|
||||
Cells::Factor(k) => {
|
||||
let k = k.max(1);
|
||||
(b.width().div_ceil(k).max(1) as usize, b.height().div_ceil(k).max(1) as usize, Some(k as usize))
|
||||
}
|
||||
};
|
||||
let full = Rect::from_xywh(0, 0, w as u32, h as u32);
|
||||
if fw == 0 || fh == 0 {
|
||||
return Buffer::transparent(if damage.is_some() { Rect::EMPTY } else { full });
|
||||
return Ok(Buffer::transparent(if damage.is_some() { Rect::EMPTY } else { full }));
|
||||
}
|
||||
// Output column / row of each document column / row: output pixel t covers [t·f/n, (t+1)·f/n).
|
||||
let span = |t: usize, f: usize, n: usize| (t * f / n, ((t + 1) * f / n).max(t * f / n + 1).min(f));
|
||||
// Output column / row of each document column / row: output pixel t covers [t·f/n, (t+1)·f/n)
|
||||
// (or [t·k, (t+1)·k) on a factor grid).
|
||||
let span = |t: usize, f: usize, n: usize| match k {
|
||||
Some(k) => ((t * k).min(f), ((t + 1) * k).min(f)),
|
||||
None => (t * f / n, ((t + 1) * f / n).max(t * f / n + 1).min(f)),
|
||||
};
|
||||
// The output pixel whose span holds document column / row `x` (spans are contiguous: n <= f).
|
||||
let cell = |x: usize, f: usize, n: usize| ((x + 1) * n).saturating_sub(1) / f;
|
||||
let cell = |x: usize, f: usize, n: usize| match k {
|
||||
Some(k) => x / k,
|
||||
None => ((x + 1) * n).saturating_sub(1) / f,
|
||||
};
|
||||
// Output pixels to produce and the document area they cover.
|
||||
let out = match damage {
|
||||
None => full,
|
||||
Some(d) => {
|
||||
let d = d.intersect(&b);
|
||||
if d.is_empty() {
|
||||
return Buffer::transparent(Rect::EMPTY);
|
||||
return Ok(Buffer::transparent(Rect::EMPTY));
|
||||
}
|
||||
let (x0, x1) = ((d.x0 - b.x0) as usize, (d.x1 - b.x0) as usize);
|
||||
let (y0, y1) = ((d.y0 - b.y0) as usize, (d.y1 - b.y0) as usize);
|
||||
@@ -131,7 +175,7 @@ pub(crate) fn render_reduced_with(
|
||||
if (w, h) == (fw, fh) {
|
||||
let mut px = render(doc, src);
|
||||
px.rect = out;
|
||||
return px;
|
||||
return Ok(px);
|
||||
}
|
||||
let map = |f: usize, n: usize| {
|
||||
let mut m = vec![0u32; f];
|
||||
@@ -200,6 +244,7 @@ pub(crate) fn render_reduced_with(
|
||||
let band_h = band_rows_for(src.width(), band_rows).max(RENDER_TILE) as usize;
|
||||
let mut y = grid(sy0, b.y0);
|
||||
while y < sy1 as i64 {
|
||||
interrupt.check().map_err(|e| e.to_string())?;
|
||||
let y1 = (y + band_h as i64).min(sy1 as i64);
|
||||
let tiles: Vec<(Rect, Rect)> = (y..y1)
|
||||
.step_by(t)
|
||||
@@ -255,7 +300,8 @@ pub(crate) fn render_reduced_with(
|
||||
y = y1;
|
||||
}
|
||||
} else {
|
||||
let ok: Result<(), ()> = render_bands(doc, src, band_rows, |band| {
|
||||
render_bands(doc, src, band_rows, |band| -> Result<(), String> {
|
||||
interrupt.check().map_err(|e| e.to_string())?;
|
||||
stats.composited += band.px.len() as u64;
|
||||
let (by0, by1) = ((band.rect.y0 - b.y0) as usize, (band.rect.y1 - b.y0) as usize);
|
||||
let sw = sx1 - sx0;
|
||||
@@ -265,8 +311,7 @@ pub(crate) fn render_reduced_with(
|
||||
}
|
||||
});
|
||||
Ok(())
|
||||
});
|
||||
debug_assert!(ok.is_ok());
|
||||
})?;
|
||||
}
|
||||
let col_w: Vec<usize> = (0..ow)
|
||||
.map(|x| {
|
||||
@@ -296,5 +341,6 @@ pub(crate) fn render_reduced_with(
|
||||
for (row, out) in acc.chunks_mut(ow).enumerate() {
|
||||
norm_row(row, out);
|
||||
}
|
||||
Buffer { rect: out, px: acc }
|
||||
interrupt.check().map_err(|e| e.to_string())?;
|
||||
Ok(Buffer { rect: out, px: acc })
|
||||
}
|
||||
@@ -112,6 +112,25 @@ pub fn render_page(doc: &Document, source: Rect, factor: u32, interrupt: Interru
|
||||
if k == 1 {
|
||||
return Ok(render(doc, source));
|
||||
}
|
||||
if source.x0.rem_euclid(k) != 0 || source.y0.rem_euclid(k) != 0 {
|
||||
return average_page(doc, source, k, out, interrupt);
|
||||
}
|
||||
// The reduced-render path the CPU canvas and navigator use: one-colour document tiles are
|
||||
// composited once instead of per pixel, and the rest in parallel tiles, then area-averaged
|
||||
// into cells aligned to multiples of `k` (the same cells as [`average_page`]).
|
||||
let stats = &mut crate::reduced::ReducedStats::default();
|
||||
let mut buf = crate::reduced::render_reduced_cells(doc, source, crate::reduced::Cells::Factor(factor), None, 128, true, stats, interrupt)?;
|
||||
if buf.rect.width() != out.width() || buf.rect.height() != out.height() {
|
||||
return Err("invalid viewport reduction".into());
|
||||
}
|
||||
buf.rect = out;
|
||||
Ok(buf)
|
||||
}
|
||||
|
||||
/// [`render_page`] by compositing every source pixel and averaging each `k`×`k` footprint (also
|
||||
/// the reference its reduced path is tested against).
|
||||
fn average_page(doc: &Document, source: Rect, k: i32, out: Rect, interrupt: Interrupt<'_>) -> Result<Buffer, String> {
|
||||
let factor = k.unsigned_abs();
|
||||
let mut result = Buffer::transparent(out);
|
||||
render_bands(doc, source, 128, |band| -> Result<(), String> {
|
||||
interrupt.check().map_err(|e| e.to_string())?;
|
||||
@@ -181,11 +200,61 @@ mod tests {
|
||||
}
|
||||
}
|
||||
}
|
||||
/// A white document with a noisy, partly transparent layer and a multiplied solid over one-colour
|
||||
/// tiles, so the reduced path takes both its one-colour and per-pixel branches.
|
||||
fn busy(depth: SampleType) -> Document {
|
||||
let mut d = Document::with_background("busy", Size::new(1100, 700), ColorMode::Rgb, depth, Color::WHITE);
|
||||
let mut s = photocraft_raster::Surface::new(d.pixel_format());
|
||||
let mut seed = 0x2545_f491_u32;
|
||||
let mut next = || {
|
||||
seed ^= seed << 13;
|
||||
seed ^= seed >> 17;
|
||||
seed ^= seed << 5;
|
||||
(seed % 1000) as f32 / 999.0
|
||||
};
|
||||
for y in (100..530).step_by(3) {
|
||||
for x in (190..905).step_by(3) {
|
||||
let px = [next(), next(), next(), next()];
|
||||
s.fill_rect(Rect::from_xywh(x, y, 3, 3), &px);
|
||||
}
|
||||
}
|
||||
d.layers.push(photocraft_doc::Layer::new("noise", photocraft_doc::LayerContent::Raster(s)));
|
||||
let mut s = photocraft_raster::Surface::new(d.pixel_format());
|
||||
s.fill_rect(Rect::new(400, 0, 1100, 650), &[0.2, 0.5, 0.9, 1.0]);
|
||||
let mut top = photocraft_doc::Layer::new("tint", photocraft_doc::LayerContent::Raster(s));
|
||||
top.opacity = 0.6;
|
||||
top.blend = photocraft_color::blend::BlendMode::Multiply;
|
||||
d.layers.push(top);
|
||||
d
|
||||
}
|
||||
#[test]
|
||||
fn reduced_pages_match_averaging_every_composited_pixel() {
|
||||
for depth in [SampleType::U8, SampleType::U16, SampleType::F32] {
|
||||
let d = busy(depth);
|
||||
for k in [2u32, 4, 16] {
|
||||
let ki = k as i32;
|
||||
for source in [d.bounds(), Rect::new(512, 256, 1024, 700), Rect::new(ki * 13, ki * 5, 1100, ki * 37)] {
|
||||
let source = source.intersect(&d.bounds());
|
||||
let out = Rect::new(source.x0 / ki, source.y0 / ki, (source.x1 + ki - 1) / ki, (source.y1 + ki - 1) / ki);
|
||||
let reference = average_page(&d, source, ki, out, Interrupt::NONE).unwrap();
|
||||
let got = render_page(&d, source, k, Interrupt::NONE).unwrap();
|
||||
assert_eq!(got.rect, reference.rect, "{depth:?} k={k} {source:?}");
|
||||
let worst = got.px.iter().zip(&reference.px).flat_map(|(a, b)| a.iter().zip(b).map(|(a, b)| (a - b).abs())).fold(0.0f32, f32::max);
|
||||
assert!(worst < 1e-4, "{depth:?} k={k} {source:?}: differs by {worst}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#[test]
|
||||
fn rejects_invalid_sizes_and_honors_cancellation() {
|
||||
let d = Document::new("d", Size::new(86400, 43200), ColorMode::Rgb, SampleType::U8);
|
||||
assert!(render_page(&d, d.bounds(), 1, Interrupt::NONE).is_err());
|
||||
assert!(render_page(&d, d.bounds(), 0, Interrupt::NONE).is_err());
|
||||
assert!(render_page(&d, Rect::new(0, 0, 512, 512), 1, Interrupt::cancel_only(&|| true)).is_err());
|
||||
// Cancelled after the first band of the reduced path.
|
||||
let checks = std::sync::atomic::AtomicUsize::new(0);
|
||||
let later = || checks.fetch_add(1, std::sync::atomic::Ordering::Relaxed) >= 2;
|
||||
assert!(render_page(&d, Rect::new(0, 0, 4096, 4096), 8, Interrupt::cancel_only(&later)).is_err());
|
||||
assert!(checks.load(std::sync::atomic::Ordering::Relaxed) >= 3);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user