1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
//! Add noise to images.

extern crate image;
extern crate rand;
use image::{GenericImage, GenericImageView};
use rand::Rng;
use image::{Pixel};
// use wasm_bindgen::prelude::*;
use crate::{PhotonImage};
use crate::{helpers};

/// Add randomized noise to an image. 
/// This function adds a Gaussian Noise Sample to each pixel through incrementing each channel by a randomized offset.
/// This randomized offset is generated by creating a randomized thread pool.
/// **[WASM SUPPORT NOT AVAILABLE]**: Randomized thread pools cannot be created with WASM using the code used currently, but 
/// a workaround is oncoming. 
/// # Arguments
/// * `img` - A PhotonImage.
/// 
/// # Example
///
/// ```
/// // For example:
/// use photon::noise;
/// photon::noise::add_noise_rand(img);
/// ```
pub fn add_noise_rand(mut photon_image: PhotonImage) -> PhotonImage {
    let mut img = helpers::dyn_image_from_raw(&photon_image);
    let (width, height) = img.dimensions();
    let mut rng = rand::thread_rng();

    for x in 0..width {
        for y in 0..height {
            let offset = rng.gen_range(0, 150);
            let px = img.get_pixel(x, y).map(|ch| if ch <= 255 - offset { ch + offset } else { 255});
            img.put_pixel(x, y, px);
        }
    }
    photon_image.raw_pixels = img.raw_pixels();
    return photon_image;
}

/// Add pink-tinted noise to an image. 
/// 
/// **[WASM SUPPORT NOT AVAILABLE]**: Randomized thread pools cannot be created using the code used currently, but 
/// support is oncoming. 
/// # Arguments
/// * `name` - A PhotonImage that contains a view into the image.
/// 
/// # Example
///
/// ```
/// // For example, to add pink-tinted noise to an image:
/// use photon::noise;
/// photon::noise::pink_noise(img);
/// ```
pub fn pink_noise(mut photon_image: &mut PhotonImage) {
    let mut img = helpers::dyn_image_from_raw(&photon_image);
    let (width, height) = img.dimensions();
    let mut rng = rand::thread_rng();

    for x in 0..width {
        for y in 0..height {
            let ran1: f64 = rng.gen(); // generates a float between 0 and 1
            let ran2: f64 = rng.gen();
            let ran3: f64 = rng.gen();

            let ran_color1: f64 = 0.6 + ran1 * 0.6;
            let ran_color2: f64 = 0.6 + ran2 * 0.1;
            let ran_color3: f64 = 0.6 + ran3 * 0.4;

            let mut px = img.get_pixel(x, y);
            px.data[0] = (px.data[0] as f64 * 0.99 * ran_color1) as u8;
            px.data[1] = (px.data[1] as f64 * 0.99 * ran_color2) as u8;
            px.data[2] = (px.data[2] as f64 * 0.99 * ran_color3) as u8;
            img.put_pixel(x, y, px);
        }
    }
    photon_image.raw_pixels = img.raw_pixels();
}