summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRosyid Haryadi <rosyid_haryadi@protonmail.com>2025-03-01 20:50:10 +0700
committerRosyid Haryadi <rosyid_haryadi@protonmail.com>2025-03-01 20:50:10 +0700
commit8589ed72f4b43f214332aab26959962a7dac4b69 (patch)
treea2b66fadc80bd6b790c442219d70fa6053b2668b
parent006c0c1f7d622bb2eabbf8b2445cd48d2c493d3b (diff)
upd png exporter
-rw-r--r--Cargo.toml1
-rw-r--r--src/main.rs3
-rw-r--r--src/view.rs12
3 files changed, 15 insertions, 1 deletions
diff --git a/Cargo.toml b/Cargo.toml
index b71bd93..c25a669 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -4,3 +4,4 @@ version = "0.1.0"
edition = "2021"
[dependencies]
+image = "0.25.5"
diff --git a/src/main.rs b/src/main.rs
index 3f3b866..3c67e9e 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -18,7 +18,8 @@ fn main() {
let view = View {
display_buffer: &display_buffer,
- viewer: render_viewer::ppm_exporter,
+ // viewer: render_viewer::ppm_exporter,
+ viewer: render_viewer::png_exporter,
};
view.display();
}
diff --git a/src/view.rs b/src/view.rs
index 28ce849..bf3a356 100644
--- a/src/view.rs
+++ b/src/view.rs
@@ -22,6 +22,7 @@ impl<'a> View<'a> {
}
pub mod render_viewer {
+ use crate::global::Pixel;
use super::*;
pub fn ppm_exporter(data: &DisplayBuffer) -> Result<String, std::io::Error> {
@@ -39,4 +40,15 @@ pub mod render_viewer {
file.write_all(txt_data.as_bytes())?;
Ok(format!("Output rendered to file {}", file_name))
}
+
+ pub fn png_exporter(data: &DisplayBuffer) -> Result<String, std::io::Error> {
+ let file_name = "output.png";
+ 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();
+ Ok(format!("Output rendered to file {}", file_name))
+ }
}