pub fn render_image(
        &self,
        samples_per_pixel: i32,
        max_depth: i32,
        thread_number: usize,
    ) -> DynamicImage {
        let mut pool = ThreadPool::new(thread_number);

        let img = Arc::new(Mutex::new(DynamicImage::new_rgb8(
            self.image_width,
            self.image_height,
        )));
        let pb = Arc::new(Mutex::new(ProgressBar::new(self.image_height as u64)));

        for j in (0..self.image_height).rev() {
            let pb = Arc::clone(&pb);
            let world = Arc::clone(&self.world);
            let img = Arc::clone(&img);
            let camera = Arc::clone(&self.camera);

            let image_width = self.image_width;
            let image_height = self.image_height;

            // Each line rendering is sent to the thread pool
            pool.execute(move || {
                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 + canonical_random()) / (image_width - 1) as f64;
                        let v = (j as f64 + canonical_random()) / (image_height - 1) as f64;
                        let r = camera.get_ray(u, v);
                        pixel_color += Image::ray_color(Arc::clone(&world), r, max_depth);
                    }
                    pixel_color.write(
                        img.lock().unwrap().as_mut_rgb8().unwrap(),
                        i,
                        j,
                        samples_per_pixel,
                    )
                }

                pb.lock().unwrap().inc();
            });
        }

        pool.wait_all();

        pb.lock().unwrap().finish_print("Done!");

        let img = img.lock().unwrap().clone();
        img
    }
/// Renders the image to the PPM format to the specified stream
/// (may be a file or just standard output)
Renders the image to the PPM format to the specified stream
(may be a file or just standard output)
Renders the image to the PPM format to the specified stream ( may be a file or just standard output )
pub fn render_image(
        &self,
        samples_per_pixel: i32,
        max_depth: i32,
        thread_number: usize,
    ) -> DynamicImage {
        let mut pool = ThreadPool::new(thread_number);
        let img = Arc::new(Mutex::new(DynamicImage::new_rgb8(
            self.image_width,
            self.image_height,
        )));
        let pb = Arc::new(Mutex::new(ProgressBar::new(self.image_height as u64)));
        for j in (0..self.image_height).rev() {
            let pb = Arc::clone(&pb);
            let world = Arc::clone(&self.world);
            let img = Arc::clone(&img);
            let camera = Arc::clone(&self.camera);
            let image_width = self.image_width;
            let image_height = self.image_height;
            // Each line rendering is sent to the thread pool
            pool.execute(move || {
                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 + canonical_random()) / (image_width - 1) as f64;
                        let v = (j as f64 + canonical_random()) / (image_height - 1) as f64;
                        let r = camera.get_ray(u, v);
                        pixel_color += Image::ray_color(Arc::clone(&world), r, max_depth);
                    }
                    pixel_color.write(
                        img.lock().unwrap().as_mut_rgb8().unwrap(),
                        i,
                        j,
                        samples_per_pixel,
                    )
                }
                pb.lock().unwrap().inc();
            });
        }
        pool.wait_all();
        pb.lock().unwrap().finish_print("Done!");
        let img = img.lock().unwrap().clone();
        img
    }
pub fn render_image ( & self , samples_per_pixel : i32 , max_depth : i32 , thread_number : usize , ) -> DynamicImage { let mut pool = ThreadPool :: new ( thread_number ) ; let img = Arc :: new ( Mutex :: new ( DynamicImage :: new_rgb8 ( self . image_width , self . image_height , ) ) ) ; let pb = Arc :: new ( Mutex :: new ( ProgressBar :: new ( self . image_height as u64 ) ) ) ; for j in ( 0 .. self . image_height ) . rev ( ) { let pb = Arc :: clone ( & pb ) ; let world = Arc :: clone ( & self . world ) ; let img = Arc :: clone ( & img ) ; let camera = Arc :: clone ( & self . camera ) ; let image_width = self . image_width ; let image_height = self . image_height ; // Each line rendering is sent to the thread pool pool . execute ( move | | { 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 + canonical_random ( ) ) / ( image_width - 1 ) as f64 ; let v = ( j as f64 + canonical_random ( ) ) / ( image_height - 1 ) as f64 ; let r = camera . get_ray ( u , v ) ; pixel_color += Image :: ray_color ( Arc :: clone ( & world ) , r , max_depth ) ; } pixel_color . write ( img . lock ( ) . unwrap ( ) . as_mut_rgb8 ( ) . unwrap ( ) , i , j , samples_per_pixel , ) } pb . lock ( ) . unwrap ( ) . inc ( ) ; } ) ; } pool . wait_all ( ) ; pb . lock ( ) . unwrap ( ) . finish_print ( " " ) ; let img = img . lock ( ) . unwrap ( ) . clone ( ) ; img }
Renders the image to the PPM format to the specified stream
(may be a file or just standard output)
Renders the image to the PPM format to the specified stream ( may be a file or just standard output )
&self
samples_per_pixel
i32
max_depth
i32
thread_number
usize