Advertisement
Guest User

Untitled

a guest
Aug 27th, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.73 KB | None | 0 0
  1. extern crate num;
  2. extern crate image;
  3.  
  4. use num::complex::*;
  5. use image::*;
  6. use std::time::SystemTime;
  7. use std::thread::*;
  8. use std::cmp::min;
  9.  
  10. static WIDTH : u32 = 3840;
  11. static HEIGHT : u32 = 2160;
  12. static CORES : u32 = 4;
  13.  
  14. fn cfunction(z: Complex64) -> Complex64 {
  15. return z*z - Complex::new(0.221, 0.713);
  16. }
  17.  
  18. fn calc_pixel<F>(x: u32, y: u32, f: F) -> Luma<u8>
  19. where F: Fn(Complex64) -> Complex64 {
  20. let pre_time = SystemTime::now();
  21. let x_f: f64 = -1. + 2. / WIDTH as f64 * f64::from(x);
  22. let y_f: f64 = -1. + 2. / HEIGHT as f64 * f64::from(y);
  23. let mut z = Complex::new(x_f, y_f);
  24. let mut i = 0u8;
  25. while i< 254 && z.norm() < 2. {
  26. z = f(z);
  27. i = i + 1;
  28. }
  29. return Luma([i; 1]);
  30. }
  31.  
  32. fn draw_buffer(x_off: u32, y_off: u32, width: u32, height: u32) -> GrayImage {
  33. let mut buf = GrayImage::new(width, height);
  34. for (x, y, pixel) in buf.enumerate_pixels_mut() {
  35. *pixel = calc_pixel(x + x_off, y + y_off, cfunction);
  36. }
  37. return buf;
  38. }
  39.  
  40. fn main() {
  41. let pre_time = SystemTime::now();
  42. let mut handles = Vec::new();
  43. let mut results = Vec::new();
  44. let lpt = HEIGHT/CORES;
  45. for i in 0..CORES {
  46. handles.push(spawn(move || {
  47. draw_buffer(0, i*lpt, WIDTH, min(HEIGHT - (CORES-1)*lpt, lpt))
  48. }));
  49. }
  50. for handle in handles {
  51. results.push(handle.join().unwrap());
  52. }
  53. for result in results {
  54. //TODO stitch image here
  55. result.save("output.png").expect("Error while saving.");
  56. }
  57. let dur = SystemTime::now().duration_since(pre_time).unwrap();
  58. println!("It took {}.{:08}s to draw a {}x{} julia set.", dur.as_secs(), dur.subsec_nanos(), WIDTH, HEIGHT);
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement