Save .png output directly from rust.

This commit is contained in:
2018-09-11 09:46:50 -07:00
parent 0c9edd9e61
commit 672dc3cdd2
3 changed files with 323 additions and 9 deletions

View File

@@ -1,8 +1,10 @@
extern crate image;
extern crate rand;
extern crate rtiow;
use std::time::Instant;
use image::RgbImage;
use rand::Rng;
use rtiow::camera::Camera;
@@ -105,9 +107,11 @@ fn random_scene() -> Vec<Box<Hit>> {
fn main() -> Result<(), std::io::Error> {
let start = Instant::now();
let mut rng = rand::thread_rng();
let nx = 1200;
let ny = 800;
let ns = 100;
// Set to 1 to do full res.
let dev_factor = 4;
let nx = 1200 / dev_factor;
let ny = 800 / dev_factor;
let ns = 100 / dev_factor;
let (cam, world) = if BOOK_COVER {
let lookfrom = Vec3::new(13., 2., 3.);
let lookat = Vec3::new(0., 0., 0.);
@@ -167,8 +171,9 @@ fn main() -> Result<(), std::io::Error> {
]);
(cam, world)
};
println!("P3\n{} {}\n255", nx, ny);
for j in (0..ny).rev() {
let mut img = RgbImage::new(nx, ny);
for j in 0..ny {
for i in 0..nx {
let mut col: Vec3 = Default::default();
for _ in 0..ns {
@@ -181,10 +186,11 @@ fn main() -> Result<(), std::io::Error> {
// Gamma correct, use gamma 2 correction, which is 1/gamma where gamma=2 which is 1/2
// or sqrt.
col = Vec3::new(col[0].sqrt(), col[1].sqrt(), col[2].sqrt());
let ir = (255.99 * col[0]) as u32;
let ig = (255.99 * col[1]) as u32;
let ib = (255.99 * col[2]) as u32;
println!("{} {} {}", ir, ig, ib);
let ir = (255.99 * col[0]) as u8;
let ig = (255.99 * col[1]) as u8;
let ib = (255.99 * col[2]) as u8;
// ny-j is to flip y-axis.
img.put_pixel(i, ny - j - 1, image::Rgb([ir, ig, ib]));
}
}
let runtime = start.elapsed();
@@ -193,5 +199,10 @@ fn main() -> Result<(), std::io::Error> {
runtime.as_secs(),
runtime.subsec_millis()
);
let path = "/tmp/test.png";
// Write the contents of this image to the Writer in PNG format.
img.save(path).unwrap();
eprintln!("Saved {}", path);
Ok(())
}