diff options
| author | Laurenz <laurmaedje@gmail.com> | 2022-06-14 17:20:31 +0200 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2022-06-14 17:20:31 +0200 |
| commit | 20b990d2973f68674e65391ae0f2a115ed1640a9 (patch) | |
| tree | 812ceb176fa857d80a02b1c55d6f5f7c380a769a | |
| parent | 7a6c2cce7747f7632f0be012f49b548db3e62a2d (diff) | |
Anti-aliased image rendering
| -rw-r--r-- | src/export/render.rs | 35 | ||||
| -rw-r--r-- | tests/ref/coma.png | bin | 85923 -> 88639 bytes | |||
| -rw-r--r-- | tests/ref/graphics/image.png | bin | 183386 -> 176635 bytes | |||
| -rw-r--r-- | tests/ref/graphics/transform.png | bin | 56071 -> 52123 bytes | |||
| -rw-r--r-- | tests/ref/layout/grid-3.png | bin | 64515 -> 64866 bytes | |||
| -rw-r--r-- | tests/ref/layout/pad.png | bin | 53637 -> 50981 bytes | |||
| -rw-r--r-- | tests/ref/layout/place-background.png | bin | 80459 -> 76964 bytes | |||
| -rw-r--r-- | tests/ref/layout/place.png | bin | 43337 -> 42912 bytes | |||
| -rw-r--r-- | tests/ref/style/set.png | bin | 20833 -> 20825 bytes | |||
| -rw-r--r-- | tests/ref/style/show-text.png | bin | 43671 -> 44844 bytes | |||
| -rw-r--r-- | tests/ref/text/bidi.png | bin | 25981 -> 26171 bytes | |||
| -rw-r--r-- | tests/ref/text/emoji.png | bin | 3340 -> 3821 bytes | |||
| -rw-r--r-- | tests/ref/text/fallback.png | bin | 10213 -> 10689 bytes | |||
| -rw-r--r-- | tests/ref/text/indent.png | bin | 46855 -> 46427 bytes | |||
| -rw-r--r-- | tests/ref/text/link.png | bin | 41124 -> 40822 bytes |
15 files changed, 19 insertions, 16 deletions
diff --git a/src/export/render.rs b/src/export/render.rs index b56a053b..4933edf0 100644 --- a/src/export/render.rs +++ b/src/export/render.rs @@ -2,6 +2,7 @@ use std::io::Read; +use image::imageops::FilterType; use image::{GenericImageView, Rgba}; use tiny_skia as sk; use ttf_parser::{GlyphId, OutlineBuilder}; @@ -351,33 +352,35 @@ fn render_image( let view_width = size.x.to_f32(); let view_height = size.y.to_f32(); - let pixmap = match img { + let aspect = (img.width() as f32) / (img.height() as f32); + let scale = ts.sx.max(ts.sy); + let w = (scale * view_width.max(aspect * view_height)).ceil() as u32; + let h = ((w as f32) / aspect).ceil() as u32; + + let mut pixmap = sk::Pixmap::new(w, h)?; + match img { Image::Raster(img) => { - let w = img.buf.width(); - let h = img.buf.height(); - let mut pixmap = sk::Pixmap::new(w, h)?; - for ((_, _, src), dest) in img.buf.pixels().zip(pixmap.pixels_mut()) { + let downscale = w < img.width(); + let filter = if downscale { + FilterType::Lanczos3 + } else { + FilterType::CatmullRom + }; + let buf = img.buf.resize(w, h, filter); + for ((_, _, src), dest) in buf.pixels().zip(pixmap.pixels_mut()) { let Rgba([r, g, b, a]) = src; *dest = sk::ColorU8::from_rgba(r, g, b, a).premultiply(); } - pixmap } Image::Svg(Svg(tree)) => { - let size = tree.svg_node().size; - let aspect = (size.width() / size.height()) as f32; - let scale = ts.sx.max(ts.sy); - let w = (scale * view_width.max(aspect * view_height)).ceil() as u32; - let h = ((w as f32) / aspect).ceil() as u32; - let mut pixmap = sk::Pixmap::new(w, h)?; resvg::render( &tree, FitTo::Size(w, h), sk::Transform::identity(), pixmap.as_mut(), - ); - pixmap + )?; } - }; + } let scale_x = view_width / pixmap.width() as f32; let scale_y = view_height / pixmap.height() as f32; @@ -386,7 +389,7 @@ fn render_image( paint.shader = sk::Pattern::new( pixmap.as_ref(), sk::SpreadMode::Pad, - sk::FilterQuality::Bilinear, + sk::FilterQuality::Nearest, 1.0, sk::Transform::from_scale(scale_x, scale_y), ); diff --git a/tests/ref/coma.png b/tests/ref/coma.png Binary files differindex 817c756a..62edb5e0 100644 --- a/tests/ref/coma.png +++ b/tests/ref/coma.png diff --git a/tests/ref/graphics/image.png b/tests/ref/graphics/image.png Binary files differindex 11a28980..a89f1963 100644 --- a/tests/ref/graphics/image.png +++ b/tests/ref/graphics/image.png diff --git a/tests/ref/graphics/transform.png b/tests/ref/graphics/transform.png Binary files differindex 1cb8efcd..cb4b0af2 100644 --- a/tests/ref/graphics/transform.png +++ b/tests/ref/graphics/transform.png diff --git a/tests/ref/layout/grid-3.png b/tests/ref/layout/grid-3.png Binary files differindex c44dae70..c17a5873 100644 --- a/tests/ref/layout/grid-3.png +++ b/tests/ref/layout/grid-3.png diff --git a/tests/ref/layout/pad.png b/tests/ref/layout/pad.png Binary files differindex 7c27bd26..55880ed1 100644 --- a/tests/ref/layout/pad.png +++ b/tests/ref/layout/pad.png diff --git a/tests/ref/layout/place-background.png b/tests/ref/layout/place-background.png Binary files differindex 99350bf8..eb2c4b71 100644 --- a/tests/ref/layout/place-background.png +++ b/tests/ref/layout/place-background.png diff --git a/tests/ref/layout/place.png b/tests/ref/layout/place.png Binary files differindex 6fc11706..f40b8c68 100644 --- a/tests/ref/layout/place.png +++ b/tests/ref/layout/place.png diff --git a/tests/ref/style/set.png b/tests/ref/style/set.png Binary files differindex 52512b85..502d74b5 100644 --- a/tests/ref/style/set.png +++ b/tests/ref/style/set.png diff --git a/tests/ref/style/show-text.png b/tests/ref/style/show-text.png Binary files differindex ae5230f1..b88e1f30 100644 --- a/tests/ref/style/show-text.png +++ b/tests/ref/style/show-text.png diff --git a/tests/ref/text/bidi.png b/tests/ref/text/bidi.png Binary files differindex 6f4c8acb..c9be82d4 100644 --- a/tests/ref/text/bidi.png +++ b/tests/ref/text/bidi.png diff --git a/tests/ref/text/emoji.png b/tests/ref/text/emoji.png Binary files differindex 3f3f698d..2036a6fc 100644 --- a/tests/ref/text/emoji.png +++ b/tests/ref/text/emoji.png diff --git a/tests/ref/text/fallback.png b/tests/ref/text/fallback.png Binary files differindex ae840734..bcd0942a 100644 --- a/tests/ref/text/fallback.png +++ b/tests/ref/text/fallback.png diff --git a/tests/ref/text/indent.png b/tests/ref/text/indent.png Binary files differindex 2196c33c..2d7a2e38 100644 --- a/tests/ref/text/indent.png +++ b/tests/ref/text/indent.png diff --git a/tests/ref/text/link.png b/tests/ref/text/link.png Binary files differindex 5a9b4ca3..fd1a3f21 100644 --- a/tests/ref/text/link.png +++ b/tests/ref/text/link.png |
