summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRosyid Haryadi <rosyid_haryadi@protonmail.com>2025-03-01 21:06:18 +0700
committerRosyid Haryadi <rosyid_haryadi@protonmail.com>2025-03-01 21:06:18 +0700
commitfb30b41fb1f4ebbe5274d2cb93e98a1f2bec445f (patch)
treee565fa34d70ee17aa26d31661d4dd1a2097454ce
parent8589ed72f4b43f214332aab26959962a7dac4b69 (diff)
upd image exporter
-rw-r--r--src/view.rs27
1 files changed, 24 insertions, 3 deletions
diff --git a/src/view.rs b/src/view.rs
index bf3a356..0ed5d43 100644
--- a/src/view.rs
+++ b/src/view.rs
@@ -22,6 +22,7 @@ impl<'a> View<'a> {
}
pub mod render_viewer {
+ use image::ImageFormat;
use crate::global::Pixel;
use super::*;
@@ -41,14 +42,34 @@ pub mod render_viewer {
Ok(format!("Output rendered to file {}", file_name))
}
- pub fn png_exporter(data: &DisplayBuffer) -> Result<String, std::io::Error> {
- let file_name = "output.png";
+ fn common_img_format_exporter(data: &DisplayBuffer, img_format: ImageFormat) -> Result<String, std::io::Error> {
+ let ext = match img_format {
+ ImageFormat::Png => { "png" }
+ ImageFormat::Jpeg => { "jpeg" }
+ ImageFormat::Bmp => { "bmp" }
+ _ => {
+ return Err(std::io::Error::new(std::io::ErrorKind::Other, "Not supported"));
+ }
+ };
+ let file_name = format!("output.{}", ext);
let mut imgbuf = image::ImageBuffer::new(IMG_WIDTH as u32, IMG_HEIGHT as u32);
for (x, y, pixel) in imgbuf.enumerate_pixels_mut() {
let Pixel{r, g, b} = data[y as usize][x as usize];
*pixel = image::Rgb([r, g, b]);
}
- imgbuf.save(file_name).unwrap();
+ imgbuf.save(file_name.clone()).unwrap();
Ok(format!("Output rendered to file {}", file_name))
}
+
+ pub fn png_exporter(data: &DisplayBuffer) -> Result<String, std::io::Error> {
+ common_img_format_exporter(data, ImageFormat::Png)
+ }
+
+ pub fn jpeg_exporter(data: &DisplayBuffer) -> Result<String, std::io::Error> {
+ common_img_format_exporter(data, ImageFormat::Jpeg)
+ }
+
+ pub fn bmp_exporter(data: &DisplayBuffer) -> Result<String, std::io::Error> {
+ common_img_format_exporter(data, ImageFormat::Bmp)
+ }
}