summaryrefslogtreecommitdiffstats
path: root/src/image/mod.rs
diff options
context:
space:
mode:
authorDenys Séguret <cano.petrole@gmail.com>2023-02-02 17:31:13 +0100
committerGitHub <noreply@github.com>2023-02-02 17:31:13 +0100
commit4b279733b65784dd73d991bf837bf0f35f32ccd6 (patch)
treebc1e6181cd14b271fb137481628bedc3b1d3abaa /src/image/mod.rs
parent1cb4d37f7aed946db98e278626df811c3a27ad52 (diff)
Render SVG files as images in preview (#660)
Diffstat (limited to 'src/image/mod.rs')
-rw-r--r--src/image/mod.rs26
1 files changed, 26 insertions, 0 deletions
diff --git a/src/image/mod.rs b/src/image/mod.rs
index caf5c13..7672ec1 100644
--- a/src/image/mod.rs
+++ b/src/image/mod.rs
@@ -1,7 +1,33 @@
mod double_line;
mod image_view;
+mod svg;
pub use {
image_view::ImageView,
};
+
+use {
+ crate::errors::ProgramError,
+ image::{
+ io::Reader,
+ DynamicImage,
+ },
+ std::path::Path,
+};
+
+// Max dimensions of the SVG image to render. A bigger size just makes it need
+// a little more memory and takes more time to render. There's no quality gain
+// in having this bigger than your screen
+pub const MAX_SVG_BITMAP_WIDTH: u32 = 1000;
+pub const MAX_SVG_BITMAP_HEIGHT: u32 = 1000;
+
+pub fn load(path: &Path) -> Result<DynamicImage, ProgramError> {
+ let is_svg = matches!(path.extension(), Some(ext) if ext == "svg" || ext == "SVG");
+ let img = if is_svg {
+ svg::render(path, MAX_SVG_BITMAP_WIDTH, MAX_SVG_BITMAP_HEIGHT)?
+ } else {
+ Reader::open(path)?.decode()?
+ };
+ Ok(img)
+}