From e20875485947b91de6ea25d594786d5708798013 Mon Sep 17 00:00:00 2001 From: benliao Date: Mon, 18 Dec 2023 11:44:25 +0800 Subject: [PATCH 1/7] support random noise by import js_sys::Math::random function --- crate/Cargo.toml | 6 +++--- crate/src/noise.rs | 40 +++++++++++++++++++++++++++++++--------- 2 files changed, 34 insertions(+), 12 deletions(-) diff --git a/crate/Cargo.toml b/crate/Cargo.toml index 84414da..13f02f4 100644 --- a/crate/Cargo.toml +++ b/crate/Cargo.toml @@ -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" @@ -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", diff --git a/crate/src/noise.rs b/crate/src/noise.rs index f595c12..ee0f70c 100644 --- a/crate/src/noise.rs +++ b/crate/src/noise.rs @@ -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. /// @@ -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| { @@ -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. /// @@ -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; From ff2bc52b8af4af005654821b2aeddc1907374acd Mon Sep 17 00:00:00 2001 From: benliao Date: Tue, 19 Dec 2023 00:18:41 +0800 Subject: [PATCH 2/7] support random noise by import js_sys::Math::random function --- README.md | 1 + crate/src/noise.rs | 10 ++++------ 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 1e44418..76f3eaa 100644 --- a/README.md +++ b/README.md @@ -230,6 +230,7 @@ Contributors include (be sure to add yourself to the list if you submitted a PR) * **Arnav Jindal** - [@Daggy1234](https://github.com/Daggy1234) * **NorbertGarfield** - [@NorbertGarfield](https://github.com/NorbertGarfield) * **bboshoven** - [@bboshoven](https://github.com/bboshoven) +* **benliao** - [@benliao](https://github.com/benliao) * **Future You(?)** - (See Contributing above) ## License diff --git a/crate/src/noise.rs b/crate/src/noise.rs index ee0f70c..f53c467 100644 --- a/crate/src/noise.rs +++ b/crate/src/noise.rs @@ -35,10 +35,9 @@ use rand::Rng; /// ``` pub fn add_noise_rand(mut photon_image: PhotonImage) -> 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 mut rng = rand::thread_rng(); for (x, y) in ImageIterator::with_dimension(&img.dimensions()) { #[cfg(not(feature = "web-sys"))] @@ -86,13 +85,12 @@ pub fn pink_noise(photon_image: &mut PhotonImage) { let mut rng = rand::thread_rng(); #[cfg(not(feature = "web-sys"))] - let rng_gen() = move || {rng.gen()}; + let rng_gen() = move || rng.gen(); #[cfg(feature = "web-sys")] - let rng_gen = || {random()}; + 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(); From bf05fe8107fb88f729d8c7cbd57d52e6f803404e Mon Sep 17 00:00:00 2001 From: benliao Date: Tue, 19 Dec 2023 00:26:27 +0800 Subject: [PATCH 3/7] support random noise by import js_sys::Math::random function --- crate/Cargo.toml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/crate/Cargo.toml b/crate/Cargo.toml index 13f02f4..84414da 100644 --- a/crate/Cargo.toml +++ b/crate/Cargo.toml @@ -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.89", optional = true } +wasm-bindgen = { version = "=0.2.85", optional = true } serde = { version = "1.0", features = ["derive"] } thiserror = "1.0" -js-sys = { version = "0.3.66", optional = true } +js-sys = { version = "0.3.62", optional = true } node-sys = { version = "0.4.2", optional = true } perlin2d = "0.2.6" @@ -55,7 +55,7 @@ harness = false wasm-opt = ["-O3", "--enable-mutable-globals"] [dependencies.web-sys] -version = "0.3.66" +version = "0.3" features = [ "Document", "Element", From 08cc61d09d401b7f20fa7d0ad4944b1963c88236 Mon Sep 17 00:00:00 2001 From: benliao Date: Tue, 19 Dec 2023 01:17:14 +0800 Subject: [PATCH 4/7] support random noise by import js_sys::Math::random function --- crate/src/noise.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crate/src/noise.rs b/crate/src/noise.rs index f53c467..087bc51 100644 --- a/crate/src/noise.rs +++ b/crate/src/noise.rs @@ -85,7 +85,7 @@ pub fn pink_noise(photon_image: &mut PhotonImage) { let mut rng = rand::thread_rng(); #[cfg(not(feature = "web-sys"))] - let rng_gen() = move || rng.gen(); + let rng_gen = move || rng.gen(); #[cfg(feature = "web-sys")] let rng_gen = || random(); From 8c0c57e23450bc83656f29c83bb1a3a1d442978c Mon Sep 17 00:00:00 2001 From: benliao Date: Tue, 19 Dec 2023 01:39:03 +0800 Subject: [PATCH 5/7] support random noise by import js_sys::Math::random function --- crate/examples/example.rs | 2 ++ crate/src/noise.rs | 19 +++++++++---------- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/crate/examples/example.rs b/crate/examples/example.rs index af432b4..7e50f76 100644 --- a/crate/examples/example.rs +++ b/crate/examples/example.rs @@ -20,6 +20,8 @@ fn main() -> Result<(), Box> { // Apply the effect in the HSV colour space photon::colour_spaces::hsl(&mut img, effect, 0.2_f32); + photon::noise::pink_noise(&mut img); + // Write the contents of this image in JPG format. photon::native::save_image(img, &format!("output_{}.jpg", effect)[..])?; diff --git a/crate/src/noise.rs b/crate/src/noise.rs index 087bc51..3f4128d 100644 --- a/crate/src/noise.rs +++ b/crate/src/noise.rs @@ -3,15 +3,14 @@ use image::Pixel; use image::{GenericImage, GenericImageView}; -// use wasm_bindgen::prelude::*; use crate::helpers; use crate::iter::ImageIterator; use crate::PhotonImage; -#[cfg(feature = "web-sys")] +#[cfg(target_family = "wasm")] use js_sys::Math::random; -#[cfg(not(feature = "web-sys"))] +#[cfg(not(target_family = "wasm"))] use rand::Rng; /// Add randomized noise to an image. @@ -36,14 +35,14 @@ use rand::Rng; pub fn add_noise_rand(mut photon_image: PhotonImage) -> PhotonImage { let mut img = helpers::dyn_image_from_raw(&photon_image); - #[cfg(not(feature = "web-sys"))] + #[cfg(not(target_family = "wasm"))] let mut rng = rand::thread_rng(); for (x, y) in ImageIterator::with_dimension(&img.dimensions()) { - #[cfg(not(feature = "web-sys"))] + #[cfg(not(target_family = "wasm"))] let offset = rng.gen_range(0, 150); - #[cfg(feature = "web-sys")] + #[cfg(target_family = "wasm")] let offset = (random() * 150.0) as u8; let px = @@ -81,13 +80,13 @@ 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"))] + #[cfg(not(target_family = "wasm"))] let mut rng = rand::thread_rng(); - #[cfg(not(feature = "web-sys"))] - let rng_gen = move || rng.gen(); + #[cfg(not(target_family = "wasm"))] + let mut rng_gen = move || rng.gen(); - #[cfg(feature = "web-sys")] + #[cfg(target_family = "wasm")] let rng_gen = || random(); for (x, y) in ImageIterator::with_dimension(&img.dimensions()) { From 09413b909bd03cde96b4a84eeef1a3ca38d2b5d5 Mon Sep 17 00:00:00 2001 From: benliao Date: Tue, 19 Dec 2023 09:23:41 +0800 Subject: [PATCH 6/7] support random noise by import js_sys::Math::random function, add to webpack demo --- crate/examples/example.rs | 2 -- crate/src/noise.rs | 12 ++++++++---- webpack_demo/index.html | 4 ++++ webpack_demo/js/index.js | 8 ++++++++ 4 files changed, 20 insertions(+), 6 deletions(-) diff --git a/crate/examples/example.rs b/crate/examples/example.rs index 7e50f76..af432b4 100644 --- a/crate/examples/example.rs +++ b/crate/examples/example.rs @@ -20,8 +20,6 @@ fn main() -> Result<(), Box> { // Apply the effect in the HSV colour space photon::colour_spaces::hsl(&mut img, effect, 0.2_f32); - photon::noise::pink_noise(&mut img); - // Write the contents of this image in JPG format. photon::native::save_image(img, &format!("output_{}.jpg", effect)[..])?; diff --git a/crate/src/noise.rs b/crate/src/noise.rs index 3f4128d..c7e27f4 100644 --- a/crate/src/noise.rs +++ b/crate/src/noise.rs @@ -7,6 +7,9 @@ use crate::helpers; use crate::iter::ImageIterator; use crate::PhotonImage; +#[cfg(feature = "enable_wasm")] +use wasm_bindgen::prelude::*; + #[cfg(target_family = "wasm")] use js_sys::Math::random; @@ -29,10 +32,11 @@ use rand::Rng; /// use photon_rs::noise::add_noise_rand; /// use photon_rs::PhotonImage; /// -/// let img = open_image("img.jpg").expect("File should open"); -/// let result: PhotonImage = add_noise_rand(img); +/// let mut img = open_image("img.jpg").expect("File should open"); +/// add_noise_rand(&mut img); /// ``` -pub fn add_noise_rand(mut photon_image: PhotonImage) -> PhotonImage { +#[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(target_family = "wasm"))] @@ -58,7 +62,6 @@ pub fn add_noise_rand(mut photon_image: PhotonImage) -> PhotonImage { img.put_pixel(x, y, px); } photon_image.raw_pixels = img.to_bytes(); - photon_image } /// Add pink-tinted noise to an image. @@ -78,6 +81,7 @@ pub fn add_noise_rand(mut photon_image: PhotonImage) -> PhotonImage { /// let mut img = open_image("img.jpg").expect("File should open"); /// pink_noise(&mut img); /// ``` +#[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(target_family = "wasm"))] diff --git a/webpack_demo/index.html b/webpack_demo/index.html index ba6c1b5..b80c2cb 100644 --- a/webpack_demo/index.html +++ b/webpack_demo/index.html @@ -262,6 +262,10 @@

Conv

  • Sharpen
  • JavaScript
  • +

    Noise

    +
  • Add Noise Rand
  • +
  • Pink Noise
  • +

    Multiple

    Blend
  • Blend
  • diff --git a/webpack_demo/js/index.js b/webpack_demo/js/index.js index f820594..7b18a72 100644 --- a/webpack_demo/js/index.js +++ b/webpack_demo/js/index.js @@ -51,6 +51,12 @@ import("../../crate/pkg").then(module => { button.addEventListener("click", function(){applyEffect(event)}, false); } + let noise_buttons = document.getElementsByClassName("noise"); + for (let i = 0; i < noise_buttons.length; i++) { + let button = noise_buttons[i]; + button.addEventListener("click", function(){applyEffect(event)}, false); + } + let blend_buttons = document.getElementsByClassName("blend"); for (let i = 0; i < blend_buttons.length; i++) { let button = blend_buttons[i]; @@ -174,6 +180,8 @@ import("../../crate/pkg").then(module => { "text": function() {return module.draw_text(rust_image, "welcome to WebAssembly", 10, 20)}, "text_border": function() {return module.draw_text_with_border(rust_image, "welcome to the edge", 10, 20)}, "test": function() {return module.filter(rust_image, "rosetint")}, + "pink_noise": function() {return module.pink_noise(rust_image)}, + "add_noise_rand": function() {return module.add_noise_rand(rust_image)}, }; // Filter the image, the PhotonImage's raw pixels are modified and From f1372c60b597f8da1c96522b96d14529f2ab5aab Mon Sep 17 00:00:00 2001 From: benliao Date: Tue, 19 Dec 2023 20:38:10 +0800 Subject: [PATCH 7/7] fix a clippy check fail --- crate/src/noise.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crate/src/noise.rs b/crate/src/noise.rs index c7e27f4..dafd009 100644 --- a/crate/src/noise.rs +++ b/crate/src/noise.rs @@ -37,7 +37,7 @@ 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); + let mut img = helpers::dyn_image_from_raw(photon_image); #[cfg(not(target_family = "wasm"))] let mut rng = rand::thread_rng();