Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions crates/linkshot-app/src/editor/canvas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ pub fn canvas_ui(app: &mut App, ui: &mut egui::Ui, rect: Rect, ctx: &egui::Conte
let doc = &app.editor.docs[i].doc;
let opts = RenderOptions { scale: render_scale, checkerboard: true, skip_ids: skip.into_iter().collect(), clip: if whole { None } else { Some([tile.0 as f32, tile.1 as f32, tile.2 as f32, tile.3 as f32]) }, preview: true };
if let Some(pm) = app.editor.renderer.render_page(doc, doc.page(), &opts) {
let img = linkshot_render::pixmap_to_image(&pm);
let ci = egui::ColorImage::from_rgba_unmultiplied([img.width() as usize, img.height() as usize], img.as_raw());
// tiny-skia pixels are premultiplied RGBA, which is what egui stores: no conversion pass.
let ci = egui::ColorImage::from_rgba_premultiplied([pm.width() as usize, pm.height() as usize], pm.data());
let topts = egui::TextureOptions { magnification: egui::TextureFilter::Nearest, minification: egui::TextureFilter::Linear, wrap_mode: egui::TextureWrapMode::ClampToEdge, mipmap_mode: None };
match app.editor.canvas.texture.as_mut() {
Some(t) => t.set(ci, topts),
Expand Down
3 changes: 2 additions & 1 deletion crates/linkshot-app/src/editor/tools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,8 @@ fn commit(app: &mut App, name: &str, before: Document) {
}
}
}
fn bump(app: &mut App) { if let Some(d) = app.editor.current_doc_mut() { d.version += 1; } }
// `changed_at` holds back the tray thumbnail (a full re-render) until a drag or stroke pauses.
fn bump(app: &mut App) { if let Some(d) = app.editor.current_doc_mut() { d.version += 1; d.changed_at = std::time::Instant::now(); } }

pub fn handle_input(app: &mut App, ui: &mut egui::Ui, resp: &Response, canvas: Rect, ctx: &egui::Context) {
let Some(_) = app.editor.current else { return };
Expand Down
93 changes: 85 additions & 8 deletions crates/linkshot-render/src/blur.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,83 @@ fn transpose(src: &[u8], dst: &mut [u8], w: usize, h: usize) {
}
}

/// Approximates a gaussian blur with three box blurs. `sigma` in pixels.
pub fn gaussian_blur(pm: &mut Pixmap, sigma: f32) {
if sigma <= 0.3 { return; }
let w = pm.width() as usize;
let h = pm.height() as usize;
if w == 0 || h == 0 { return; }
// Box radii for 3 passes (Ivan Kutskir's method).
/// Box radii for three passes approximating a gaussian of `sigma` (Ivan Kutskir's method).
fn box_radii(sigma: f32) -> Vec<usize> {
let w_ideal = (12.0 * sigma * sigma / 3.0 + 1.0).sqrt();
let mut wl = w_ideal.floor() as i32;
if wl % 2 == 0 { wl -= 1; }
let wu = wl + 2;
let m_ideal = (12.0 * sigma * sigma - 3.0 * (wl * wl) as f32 - 12.0 * wl as f32 - 9.0) / (-4.0 * wl as f32 - 4.0);
let m = m_ideal.round() as i32;
let radii: Vec<usize> = (0..3).map(|i| (if i < m { wl } else { wu } - 1) as usize / 2).collect();
(0..3).map(|i| (if i < m { wl } else { wu } - 1) as usize / 2).collect()
}

/// [`box_blur_h`] for a single channel.
fn box_blur_h1(src: &[u8], dst: &mut [u8], w: usize, h: usize, r: usize) {
let div = (2 * r + 1) as u32;
// acc / div as a multiply: with inv = floor(2^32 / div) + 1 the error term is below div, so the
// quotient is exact while acc * div < 2^32 (acc <= 255 * div, i.e. div < 4104). A hardware
// divide per pixel per pass was most of the cost of a drop shadow.
let inv = (1u64 << 32) / div as u64 + 1;
let quot = |acc: u32| if div < 4096 { ((acc as u64 * inv) >> 32) as u32 } else { acc / div };
for y in 0..h {
let row = &src[y * w..(y + 1) * w];
let out = &mut dst[y * w..(y + 1) * w];
// Edge handling: clamp (replicate edge pixel), as in `box_blur_h`.
let mut acc: u32 = row[..=r.min(w - 1)].iter().map(|&v| v as u32).sum::<u32>() + r as u32 * row[0] as u32;
for x in 0..w {
out[x] = quot(acc) as u8;
let add = (x + r + 1).min(w - 1);
let sub = if x < r { 0 } else { x - r };
acc += row[add] as u32;
acc -= row[sub] as u32;
}
}
}

fn transpose1(src: &[u8], dst: &mut [u8], w: usize, h: usize) {
// In 32×32 blocks: a plain column-order walk misses the cache on every write.
const B: usize = 32;
for by in (0..h).step_by(B) {
for bx in (0..w).step_by(B) {
for y in by..(by + B).min(h) {
for x in bx..(bx + B).min(w) { dst[x * h + y] = src[y * w + x]; }
}
}
}
}

/// Drop shadow of `src`: its alpha × `opacity`, blurred by `sigma`, filled with `color`. A shadow is
/// one colour, so only the alpha channel is blurred, a quarter of the work of blurring RGBA.
pub fn shadow(src: &Pixmap, color: tiny_skia::Color, opacity: f32, sigma: f32) -> Option<Pixmap> {
let (w, h) = (src.width() as usize, src.height() as usize);
// 256 possible alphas: table lookups instead of float maths per pixel (same rounding).
let tint: [u8; 256] = std::array::from_fn(|v| (v as f32 * opacity).round().clamp(0.0, 255.0) as u8);
let mut a: Vec<u8> = src.pixels().iter().map(|p| tint[p.alpha() as usize]).collect();
if sigma > 0.3 {
let mut b = vec![0u8; a.len()];
for r in box_radii(sigma) {
if r == 0 { continue; }
box_blur_h1(&a, &mut b, w, h, r);
transpose1(&b, &mut a, w, h);
box_blur_h1(&a, &mut b, h, w, r);
transpose1(&b, &mut a, h, w);
}
}
let (r, g, b) = (color.red(), color.green(), color.blue());
let fill: [[u8; 4]; 256] = std::array::from_fn(|v| { let af = v as f32; [(r * af).round() as u8, (g * af).round() as u8, (b * af).round() as u8, v as u8] });
let mut data = vec![0u8; a.len() * 4];
for (px, &al) in data.chunks_exact_mut(4).zip(&a) { px.copy_from_slice(&fill[al as usize]); }
Pixmap::from_vec(data, tiny_skia::IntSize::from_wh(w as u32, h as u32)?)
}

/// Approximates a gaussian blur with three box blurs. `sigma` in pixels.
pub fn gaussian_blur(pm: &mut Pixmap, sigma: f32) {
if sigma <= 0.3 { return; }
let w = pm.width() as usize;
let h = pm.height() as usize;
if w == 0 || h == 0 { return; }
let radii = box_radii(sigma);

let mut a = pm.data().to_vec();
let mut b = vec![0u8; a.len()];
Expand Down Expand Up @@ -108,3 +171,17 @@ pub fn tint_alpha(pm: &mut Pixmap, color: tiny_skia::Color, opacity: f32) {
px[3] = a as u8;
}
}

#[cfg(test)]
mod tests {
#[test]
fn reciprocal_division_is_exact() {
for r in 0..2048u32 {
let div = 2 * r + 1;
let inv = (1u64 << 32) / div as u64 + 1;
for acc in (0..=255 * div).step_by((div as usize / 7).max(1)).chain([255 * div, 255 * div - 1]) {
assert_eq!(((acc as u64 * inv) >> 32) as u32, acc / div, "acc {acc} div {div}");
}
}
}
}
Loading