Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extern crate num;
- extern crate image;
- use num::complex::*;
- use image::*;
- use std::time::SystemTime;
- use std::thread::*;
- use std::cmp::min;
- static WIDTH : u32 = 3840;
- static HEIGHT : u32 = 2160;
- static CORES : u32 = 4;
- fn cfunction(z: Complex64) -> Complex64 {
- return z*z - Complex::new(0.221, 0.713);
- }
- fn calc_pixel<F>(x: u32, y: u32, f: F) -> Luma<u8>
- where F: Fn(Complex64) -> Complex64 {
- let pre_time = SystemTime::now();
- let x_f: f64 = -1. + 2. / WIDTH as f64 * f64::from(x);
- let y_f: f64 = -1. + 2. / HEIGHT as f64 * f64::from(y);
- let mut z = Complex::new(x_f, y_f);
- let mut i = 0u8;
- while i< 254 && z.norm() < 2. {
- z = f(z);
- i = i + 1;
- }
- return Luma([i; 1]);
- }
- fn draw_buffer(x_off: u32, y_off: u32, width: u32, height: u32) -> GrayImage {
- let mut buf = GrayImage::new(width, height);
- for (x, y, pixel) in buf.enumerate_pixels_mut() {
- *pixel = calc_pixel(x + x_off, y + y_off, cfunction);
- }
- return buf;
- }
- fn main() {
- let pre_time = SystemTime::now();
- let mut handles = Vec::new();
- let mut results = Vec::new();
- let lpt = HEIGHT/CORES;
- for i in 0..CORES {
- handles.push(spawn(move || {
- draw_buffer(0, i*lpt, WIDTH, min(HEIGHT - (CORES-1)*lpt, lpt))
- }));
- }
- for handle in handles {
- results.push(handle.join().unwrap());
- }
- for result in results {
- //TODO stitch image here
- result.save("output.png").expect("Error while saving.");
- }
- let dur = SystemTime::now().duration_since(pre_time).unwrap();
- println!("It took {}.{:08}s to draw a {}x{} julia set.", dur.as_secs(), dur.subsec_nanos(), WIDTH, HEIGHT);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement