ppm -> png transition

This commit is contained in:
2026-02-13 17:58:04 +01:00
parent a0dcd69d35
commit c763168823
3 changed files with 22 additions and 39 deletions

View File

@@ -7,4 +7,5 @@ edition = "2021"
[dependencies] [dependencies]
rand = "0.7.0" rand = "0.7.0"
rayon = "1.8" rayon = "1.8"
image = "0.25.9"

BIN
img.png

Binary file not shown.

Before

Width:  |  Height:  |  Size: 742 KiB

After

Width:  |  Height:  |  Size: 1.0 MiB

View File

@@ -8,13 +8,9 @@ pub mod rtweekend;
pub mod sphere; pub mod sphere;
pub mod vec3; pub mod vec3;
use image::{Pixel, RgbImage};
use rayon::prelude::*; use rayon::prelude::*;
use std::{
io::{stdout, Write},
sync::atomic::{AtomicUsize, Ordering},
};
use crate::{ use crate::{
camera::*, camera::*,
color::*, color::*,
@@ -145,42 +141,28 @@ fn main() {
.build_global() .build_global()
.unwrap(); .unwrap();
let mut buffer = vec![0u8; (image_width * image_height * 3) as usize]; let mut img_buffer = RgbImage::new(image_width as u32, image_height as u32);
let rows_completed = AtomicUsize::new(0); img_buffer
let total_rows = image_height as usize; .par_enumerate_pixels_mut()
.for_each(|(x, y, pixel)| {
// goofy workaround :D
let reversed_y = image_height as u32 - y;
buffer let mut pixel_color = Color::new(0.0, 0.0, 0.0);
.par_chunks_mut((image_width * 3) as usize) for _ in 0..samples_per_pixel {
.enumerate() let u = (x as f64 + random_f64()) / (image_width - 1) as f64;
.for_each(|(index, row_slice)| { let v = (reversed_y as f64 + random_f64()) / (image_height - 1) as f64;
let j = image_height - 1 - index as i32; let r = cam.get_ray(u, v);
pixel_color += ray_color(r, &world, max_depth);
for i in 0..image_width {
let mut pixel_color = Color::new(0.0, 0.0, 0.0);
for _ in 0..samples_per_pixel {
let u = (i as f64 + random_f64()) / (image_width - 1) as f64;
let v = (j as f64 + random_f64()) / (image_height - 1) as f64;
let r = cam.get_ray(u, v);
pixel_color += ray_color(r, &world, max_depth);
}
let (ir, ig, ib) = get_scaled_color_components(pixel_color, samples_per_pixel);
row_slice[(i as usize) * 3] = ir;
row_slice[(i as usize) * 3 + 1] = ig;
row_slice[(i as usize) * 3 + 2] = ib;
} }
// progress let (ir, ig, ib) = get_scaled_color_components(pixel_color, samples_per_pixel);
let completed = rows_completed.fetch_add(1, Ordering::Relaxed) + 1;
if completed % 10 == 0 || completed == total_rows { pixel.channels_mut()[0] = ir;
print!( pixel.channels_mut()[1] = ig;
"\r[ {:.2} % ] Rendering...", pixel.channels_mut()[2] = ib;
(completed as f64 / total_rows as f64) * 100.0
);
let _ = stdout().flush();
}
}); });
img_buffer.save("img.png").unwrap();
} }