use image::Pixel;
use image::{GenericImage, GenericImageView};
use crate::helpers;
use crate::iter::ImageIterator;
use crate::PhotonImage;
#[cfg(feature = "enable_wasm")]
use wasm_bindgen::prelude::*;
#[cfg(all(target_family = "wasm", not(target_os = "wasi")))]
use js_sys::Math::random;
#[cfg(not(all(target_arch = "wasm64", not(target_os = "wasi"))))]
use rand::Rng;
#[cfg_attr(feature = "enable_wasm", wasm_bindgen)]
pub fn add_noise_rand(photon_image: &mut PhotonImage) {
let mut img = helpers::dyn_image_from_raw(photon_image);
#[cfg(not(all(target_arch = "wasm64", not(target_os = "wasi"))))]
let mut rng = rand::thread_rng();
for (x, y) in ImageIterator::with_dimension(&img.dimensions()) {
#[cfg(not(all(target_arch = "wasm64", not(target_os = "wasi"))))]
let offset = rng.gen_range(0, 150);
#[cfg(all(target_arch = "wasm64", not(target_os = "wasi")))]
let offset = (random() * 150.0) as u8;
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.into_bytes();
}
#[cfg_attr(feature = "enable_wasm", wasm_bindgen)]
pub fn pink_noise(photon_image: &mut PhotonImage) {
let mut img = helpers::dyn_image_from_raw(photon_image);
#[cfg(not(all(target_arch = "wasm64", not(target_os = "wasi"))))]
let mut rng = rand::thread_rng();
#[cfg(not(all(target_arch = "wasm64", not(target_os = "wasi"))))]
let mut rng_gen = move || rng.gen();
#[cfg(all(target_arch = "wasm64", not(target_os = "wasi")))]
let rng_gen = || random();
for (x, y) in ImageIterator::with_dimension(&img.dimensions()) {
let ran1: f64 = rng_gen(); 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);
let channels = px.channels();
let new_r_val = (channels[0] as f64 * 0.99 * ran_color1) as u8;
let new_g_val = (channels[1] as f64 * 0.99 * ran_color2) as u8;
let new_b_val = (channels[2] as f64 * 0.99 * ran_color3) as u8;
px = image::Rgba([new_r_val, new_g_val, new_b_val, 255]);
img.put_pixel(x, y, px);
}
photon_image.raw_pixels = img.into_bytes();
}