Add PNG output
This commit is contained in:
@ -3,7 +3,7 @@ use std::time::Instant;
|
||||
|
||||
use crate::camera::Camera;
|
||||
use crate::hittable::{Hittable, Sphere};
|
||||
use crate::output::{Output, P3};
|
||||
use crate::output::{Output, P3, PNG};
|
||||
use crate::ray::Ray;
|
||||
use crate::vec3::{Color, Point3, Vec3};
|
||||
use rand::distributions::{Distribution, Uniform};
|
||||
@ -86,9 +86,8 @@ fn main() {
|
||||
const ASPECT_RATIO: f64 = 3.0 / 2.0;
|
||||
const IMAGE_WIDTH: usize = 1200;
|
||||
const IMAGE_HEIGHT: usize = (IMAGE_WIDTH as f64 / ASPECT_RATIO) as usize;
|
||||
const SAMPLES_PER_PIXEL: i32 = 500;
|
||||
const SAMPLES_PER_PIXEL: i32 = 100;
|
||||
const MAX_DEPTH: i32 = 50;
|
||||
let hh = IMAGE_HEIGHT;
|
||||
|
||||
let look_from = Point3::new(13.0, 2.0, 3.0);
|
||||
let look_at = Point3::new(0.0, 0.0, 0.0);
|
||||
@ -138,6 +137,6 @@ fn main() {
|
||||
}
|
||||
}
|
||||
});
|
||||
P3::write("imc.ppm", &pixels, IMAGE_WIDTH, IMAGE_HEIGHT).expect("Error writing image: {}");
|
||||
PNG::write("imc.png", &pixels, IMAGE_WIDTH, IMAGE_HEIGHT).expect("Error writing image: {}");
|
||||
eprintln!("\nDone. Time: {}ms", start.elapsed().as_millis());
|
||||
}
|
||||
|
@ -1,12 +1,14 @@
|
||||
use std::{fs::File, io::{Error, Write}};
|
||||
use std::{fs::File, io::{Error, Write, BufWriter, ErrorKind}};
|
||||
|
||||
use image::ImageOutputFormat;
|
||||
|
||||
pub trait Output {
|
||||
fn write(filename: &str, pixels: &Vec<u8>, width: usize, height: usize) -> Result<File, Error>;
|
||||
fn write(filename: &str, pixels: &Vec<u8>, width: usize, height: usize) -> Result<(), Error>;
|
||||
}
|
||||
|
||||
pub struct P3 {}
|
||||
impl Output for P3 {
|
||||
fn write(filename: &str, pixels: &Vec<u8>, width: usize, height: usize) -> Result<File, Error> {
|
||||
fn write(filename: &str, pixels: &Vec<u8>, width: usize, height: usize) -> Result<(), Error> {
|
||||
let mut file = File::create(filename)?;
|
||||
file.write(format!("P3\n{} {}\n255\n", width, height).as_bytes())?;
|
||||
let lines: Result<Vec<usize>, Error> = pixels
|
||||
@ -14,6 +16,22 @@ impl Output for P3 {
|
||||
.map(|chunk| format!("{} {} {}\n", chunk[0], chunk[1], chunk[2]))
|
||||
.map(|line| file.write(line.as_bytes()))
|
||||
.collect();
|
||||
lines.map(|_v| file)
|
||||
lines.map(|_l|())
|
||||
}
|
||||
}
|
||||
|
||||
pub struct PNG {}
|
||||
impl Output for PNG {
|
||||
fn write(filename: &str, pixels: &Vec<u8>, width: usize, height: usize) -> Result<(), Error> {
|
||||
let file = File::create(filename)?;
|
||||
let ref mut writer = BufWriter::new(file);
|
||||
image::write_buffer_with_format(
|
||||
writer,
|
||||
pixels,
|
||||
width as u32,
|
||||
height as u32,
|
||||
image::ColorType::Rgb8,
|
||||
ImageOutputFormat::Png)
|
||||
.map_err(|e| Error::new(ErrorKind::Other, e))
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user