@@ -19,71 +19,66 @@ Create [`ThreadPool`] with thread count equal to number of cores with [`num_cpus
1919``` rust,edition2018,no_run
2020extern crate num;
2121extern crate num_cpus;
22- # use error_chain::error_chain;
22+ extern crate anyhow;
23+ extern crate threadpool;
24+ extern crate image;
25+ use anyhow::Result;
2326use std::sync::mpsc::{channel, RecvError};
2427use threadpool::ThreadPool;
2528use num::complex::Complex;
2629use image::{ImageBuffer, Pixel, Rgb};
27- #
28- # error_chain! {
29- # foreign_links {
30- # MpscRecv(RecvError);
31- # Io(std::io::Error);
32- # Image(image::ImageError);
33- # }
34- # }
35- #
36- # // Function converting intensity values to RGB
37- # // Based on http://www.efg2.com/Lab/ScienceAndEngineering/Spectra.htm
38- # fn wavelength_to_rgb(wavelength: u32) -> Rgb<u8> {
39- # let wave = wavelength as f32;
40- #
41- # let (r, g, b) = match wavelength {
42- # 380..=439 => ((440. - wave) / (440. - 380.), 0.0, 1.0),
43- # 440..=489 => (0.0, (wave - 440.) / (490. - 440.), 1.0),
44- # 490..=509 => (0.0, 1.0, (510. - wave) / (510. - 490.)),
45- # 510..=579 => ((wave - 510.) / (580. - 510.), 1.0, 0.0),
46- # 580..=644 => (1.0, (645. - wave) / (645. - 580.), 0.0),
47- # 645..=780 => (1.0, 0.0, 0.0),
48- # _ => (0.0, 0.0, 0.0),
49- # };
50- #
51- # let factor = match wavelength {
52- # 380..=419 => 0.3 + 0.7 * (wave - 380.) / (420. - 380.),
53- # 701..=780 => 0.3 + 0.7 * (780. - wave) / (780. - 700.),
54- # _ => 1.0,
55- # };
56- #
57- # let (r, g, b) = (normalize(r, factor), normalize(g, factor), normalize(b, factor));
58- # Rgb::from_channels(r, g, b, 0)
59- # }
60- #
61- # // Maps Julia set distance estimation to intensity values
62- # fn julia(c: Complex<f32>, x: u32, y: u32, width: u32, height: u32, max_iter: u32) -> u32 {
63- # let width = width as f32;
64- # let height = height as f32;
65- #
66- # let mut z = Complex {
67- # // scale and translate the point to image coordinates
68- # re: 3.0 * (x as f32 - 0.5 * width) / width,
69- # im: 2.0 * (y as f32 - 0.5 * height) / height,
70- # };
71- #
72- # let mut i = 0;
73- # for t in 0..max_iter {
74- # if z.norm() >= 2.0 {
75- # break;
76- # }
77- # z = z * z + c;
78- # i = t;
79- # }
80- # i
81- # }
82- #
83- # // Normalizes color intensity values within RGB range
84- # fn normalize(color: f32, factor: f32) -> u8 {
85- # ((color * factor).powf(0.8) * 255.) as u8
86- # }
30+
31+ // Function converting intensity values to RGB
32+ // Based on http://www.efg2.com/Lab/ScienceAndEngineering/Spectra.htm
33+ fn wavelength_to_rgb(wavelength: u32) -> Rgb<u8> {
34+ let wave = wavelength as f32;
35+
36+ let (r, g, b) = match wavelength {
37+ 380..=439 => ((440. - wave) / (440. - 380.), 0.0, 1.0),
38+ 440..=489 => (0.0, (wave - 440.) / (490. - 440.), 1.0),
39+ 490..=509 => (0.0, 1.0, (510. - wave) / (510. - 490.)),
40+ 510..=579 => ((wave - 510.) / (580. - 510.), 1.0, 0.0),
41+ 580..=644 => (1.0, (645. - wave) / (645. - 580.), 0.0),
42+ 645..=780 => (1.0, 0.0, 0.0),
43+ _ => (0.0, 0.0, 0.0),
44+ };
45+
46+ let factor = match wavelength {
47+ 380..=419 => 0.3 + 0.7 * (wave - 380.) / (420. - 380.),
48+ 701..=780 => 0.3 + 0.7 * (780. - wave) / (780. - 700.),
49+ _ => 1.0,
50+ };
51+
52+ let (r, g, b) = (normalize(r, factor), normalize(g, factor), normalize(b, factor));
53+ Rgb::from_channels(r, g, b, 0)
54+ }
55+
56+ // Maps Julia set distance estimation to intensity values
57+ fn julia(c: Complex<f32>, x: u32, y: u32, width: u32, height: u32, max_iter: u32) -> u32 {
58+ let width = width as f32;
59+ let height = height as f32;
60+
61+ let mut z = Complex {
62+ // scale and translate the point to image coordinates
63+ re: 3.0 * (x as f32 - 0.5 * width) / width,
64+ im: 2.0 * (y as f32 - 0.5 * height) / height,
65+ };
66+
67+ let mut i = 0;
68+ for t in 0..max_iter {
69+ if z.norm() >= 2.0 {
70+ break;
71+ }
72+ z = z * z + c;
73+ i = t;
74+ }
75+ i
76+ }
77+
78+ // Normalizes color intensity values within RGB range
79+ fn normalize(color: f32, factor: f32) -> u8 {
80+ ((color * factor).powf(0.8) * 255.) as u8
81+ }
8782
8883fn main() -> Result<()> {
8984 let (width, height) = (1920, 1080);
0 commit comments