ErnstHot

Mandelbrot in Rust

Jun 26th, 2022 (edited)
329
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 2.37 KB | None
  1. /*
  2.     Needs these dependencies in Cargo.toml
  3.  
  4.     png = "0.17.5"
  5.     num = "0.4.0"
  6.  */
  7.  
  8. use std::path::Path;
  9. use std::fs::File;
  10. use std::io::BufWriter;
  11. use num::pow::Pow;
  12.  
  13.  
  14. pub fn main()
  15. {
  16.     let path = Path::new(r"/home/thorsten/Temp/image.png");
  17.  
  18.     let width = 512;
  19.     let height = 512;
  20.     let max_iterations: u16 = 64;
  21.     let start_x = -1.5;
  22.     let start_y = -1.0;
  23.     let scale = 2.0;
  24.  
  25.     let max_value = ((max_iterations - 1) * (256 / max_iterations)) as u8;
  26.  
  27.     let mut pixels: Vec<u8> = Vec::new();
  28.  
  29.     let delta_x: f64 = scale / width as f64;
  30.     let delta_y: f64 = scale / height as f64;
  31.  
  32.     let mut current_x: f64;
  33.     let mut current_y: f64 = start_y;
  34.  
  35.     for _ in 0..height
  36.     {
  37.         current_x = start_x;
  38.  
  39.         for _ in 0..width
  40.         {
  41.             let c = num::complex::Complex::new(current_x, current_y);
  42.             let mut z = c.clone();
  43.             let mut iterations: u16 = 0;
  44.  
  45.             for i in 0..max_iterations
  46.             {
  47.                 z = z.pow(2.0) + c;
  48.  
  49.                 if z.clone().to_polar().0 >= 2.0
  50.                 {
  51.                     break;
  52.                 }
  53.  
  54.                 iterations = i;
  55.             }
  56.  
  57.             let mut p_val = (iterations * (256 / max_iterations)) as u8;
  58.  
  59.             if p_val == max_value
  60.             {
  61.                 p_val = 0;
  62.             }
  63.  
  64.             pixels.push(p_val);
  65.             pixels.push(p_val);
  66.             pixels.push(p_val);
  67.             pixels.push(255);
  68.  
  69.             current_x += delta_x;
  70.         }
  71.         current_y += delta_y;
  72.     }
  73.  
  74.     save_png(width, height, &pixels, path);
  75. }
  76.  
  77.  
  78. fn save_png(width: u32, height: u32, pixels: &Vec<u8>, path: &Path)
  79. {
  80.     assert!(width > 0);
  81.     assert!(height > 0);
  82.     assert_eq!((width * height) as usize, pixels.len() / 4);
  83.  
  84.     let file = File::create(path).unwrap();
  85.     let ref mut w = BufWriter::new(file);
  86.  
  87.     let mut encoder = png::Encoder::new(w, width, height); // Width is 2 pixels and height is 1.
  88.     encoder.set_color(png::ColorType::Rgba);
  89.     encoder.set_depth(png::BitDepth::Eight);
  90.     encoder.set_trns(vec!(0xFFu8, 0xFFu8, 0xFFu8, 0xFFu8));
  91.     encoder.set_source_gamma(png::ScaledFloat::from_scaled(45455)); // 1.0 / 2.2, scaled by 100000
  92.     encoder.set_source_gamma(png::ScaledFloat::new(1.0 / 2.2));     // 1.0 / 2.2, unscaled, but rounded
  93.  
  94.     let source_chromaticities = png::SourceChromaticities::new(     // Using unscaled instantiation here
  95.         (0.31270, 0.32900),
  96.         (0.64000, 0.33000),
  97.         (0.30000, 0.60000),
  98.         (0.15000, 0.06000),
  99.     );
  100.  
  101.     encoder.set_source_chromaticities(source_chromaticities);
  102.     let mut writer = encoder.write_header().unwrap();
  103.  
  104.     writer.write_image_data(&pixels).unwrap(); // Save
  105. }
RAW Paste Data Copied