image_noise/
lib.rs

1//! # image-noise
2//! Creates A simple perlin noise texture in Rust.
3//!
4//! ```
5//! image-noise = "0.1.0"
6//! ```
7//1
8//! ```rust
9//! let image = image_noise::noise_image(1000, 10);
10//!
11//! let mut file = File::create("image.png").unwrap();
12//!
13//! let mut bytes: Vec<u8> = Vec::new();
14//! image
15//!     .write_to(&mut bytes, image::ImageOutputFormat::Png)
16//!     .expect("Can write to png");
17//! 
18//! file.write_all(&bytes).unwrap();
19//! ```
20
21use image::{DynamicImage, ImageBuffer, Rgb};
22use txture::PerlinNoise;
23
24/// Create a noise image with the size in width and height, and the resolution of the noise.
25pub fn noise_image(s: u32, gradient_point_number: u32) -> DynamicImage {
26    // Create perlin noise
27    let perlin_noise = PerlinNoise::new(s, gradient_point_number, true).unwrap();
28
29    // Create a new ImgBuf with width: imgx and height: imgy
30    let mut imgbuf: ImageBuffer<Rgb<u8>, Vec<u8>> = image::ImageBuffer::new(s, s);
31
32    // Iterate over the coordinates and pixels of the image
33    for (x, y, pixel) in imgbuf.enumerate_pixels_mut() {
34        let gray: u8 = perlin_noise.get_pixel_value(x, y);
35
36        *pixel = Rgb([gray, gray, gray]);
37    }
38
39    let image = DynamicImage::ImageRgb8(imgbuf);
40
41    return image;
42}