Skip to content

Commit

Permalink
support random noise by import js_sys::Math::random function
Browse files Browse the repository at this point in the history
  • Loading branch information
benliao committed Dec 18, 2023
1 parent 82593f3 commit e208754
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 12 deletions.
6 changes: 3 additions & 3 deletions crate/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ imageproc = { version = "0.22.0", default-features = false }
rusttype="0.9.2"
base64="0.13.0"
time="0.3.21"
wasm-bindgen = { version = "=0.2.85", optional = true }
wasm-bindgen = { version = "=0.2.89", optional = true }
serde = { version = "1.0", features = ["derive"] }
thiserror = "1.0"
js-sys = { version = "0.3.62", optional = true }
js-sys = { version = "0.3.66", optional = true }
node-sys = { version = "0.4.2", optional = true }
perlin2d = "0.2.6"

Expand All @@ -55,7 +55,7 @@ harness = false
wasm-opt = ["-O3", "--enable-mutable-globals"]

[dependencies.web-sys]
version = "0.3"
version = "0.3.66"
features = [
"Document",
"Element",
Expand Down
40 changes: 31 additions & 9 deletions crate/src/noise.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,23 @@
use image::Pixel;
use image::{GenericImage, GenericImageView};
use rand::Rng;

// use wasm_bindgen::prelude::*;
use crate::helpers;
use crate::iter::ImageIterator;
use crate::PhotonImage;

#[cfg(feature = "web-sys")]
use js_sys::Math::random;

#[cfg(not(feature = "web-sys"))]
use rand::Rng;

/// 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.
/// **[WASM SUPPORT IS AVAILABLE]**: Randomized thread pools cannot be created with WASM, but
/// a workaround using js_sys::Math::random works now.
/// # Arguments
/// * `img` - A PhotonImage.
///
Expand All @@ -29,10 +35,18 @@ use crate::PhotonImage;
/// ```
pub fn add_noise_rand(mut photon_image: PhotonImage) -> PhotonImage {
let mut img = helpers::dyn_image_from_raw(&photon_image);
let mut rng = rand::thread_rng();

#[cfg(not(feature = "web-sys"))]
let mut rng = rand::thread_rng();


for (x, y) in ImageIterator::with_dimension(&img.dimensions()) {
#[cfg(not(feature = "web-sys"))]
let offset = rng.gen_range(0, 150);

#[cfg(feature = "web-sys")]
let offset = (random() * 150.0) as u8;

let px =
img.get_pixel(x, y).map(
|ch| {
Expand All @@ -51,8 +65,8 @@ pub fn add_noise_rand(mut photon_image: PhotonImage) -> PhotonImage {

/// 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.
/// **[WASM SUPPORT IS AVAILABLE]**: Randomized thread pools cannot be created with WASM, but
/// a workaround using js_sys::Math::random works now.
/// # Arguments
/// * `name` - A PhotonImage that contains a view into the image.
///
Expand All @@ -68,12 +82,20 @@ pub fn add_noise_rand(mut photon_image: PhotonImage) -> PhotonImage {
/// ```
pub fn pink_noise(photon_image: &mut PhotonImage) {
let mut img = helpers::dyn_image_from_raw(photon_image);
#[cfg(not(feature = "web-sys"))]
let mut rng = rand::thread_rng();

#[cfg(not(feature = "web-sys"))]
let rng_gen() = move || {rng.gen()};

#[cfg(feature = "web-sys")]
let rng_gen = || {random()};

for (x, y) in ImageIterator::with_dimension(&img.dimensions()) {
let ran1: f64 = rng.gen(); // generates a float between 0 and 1
let ran2: f64 = rng.gen();
let ran3: f64 = rng.gen();

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;
Expand Down

0 comments on commit e208754

Please sign in to comment.