Skip to content

Commit

Permalink
make wasm feature flag granular to enable wasi compilation
Browse files Browse the repository at this point in the history
Signed-off-by: karthik2804 <karthik.ganeshram@fermyon.com>
  • Loading branch information
karthik2804 committed Mar 14, 2024
1 parent a2cefcb commit 8b17f68
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 10 deletions.
1 change: 1 addition & 0 deletions crate/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,7 @@ pub fn to_image_data(photon_image: PhotonImage) -> ImageData {
.unwrap()
}

#[cfg(not(target_os = "wasi"))]
fn set_panic_hook() {
// When the `console_error_panic_hook` feature is enabled, we can call the
// `set_panic_hook` function to get better error messages if we ever panic.
Expand Down
16 changes: 8 additions & 8 deletions crate/src/noise.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ use crate::PhotonImage;
#[cfg(feature = "enable_wasm")]
use wasm_bindgen::prelude::*;

#[cfg(target_family = "wasm")]
#[cfg(all(target_family = "wasm", not(target_os = "wasi")))]
use js_sys::Math::random;

#[cfg(not(target_family = "wasm"))]
#[cfg(not(all(target_arch = "wasm", not(target_os = "wasi"))))]
use rand::Rng;

/// Add randomized noise to an image.
Expand All @@ -39,14 +39,14 @@ use rand::Rng;
pub fn add_noise_rand(photon_image: &mut PhotonImage) {
let mut img = helpers::dyn_image_from_raw(photon_image);

#[cfg(not(target_family = "wasm"))]
#[cfg(not(all(target_arch = "wasm", not(target_os = "wasi"))))]
let mut rng = rand::thread_rng();

for (x, y) in ImageIterator::with_dimension(&img.dimensions()) {
#[cfg(not(target_family = "wasm"))]
#[cfg(not(all(target_arch = "wasm", not(target_os = "wasi"))))]
let offset = rng.gen_range(0, 150);

#[cfg(target_family = "wasm")]
#[cfg(all(target_arch = "wasm", not(target_os = "wasi")))]
let offset = (random() * 150.0) as u8;

let px =
Expand Down Expand Up @@ -84,13 +84,13 @@ pub fn add_noise_rand(photon_image: &mut PhotonImage) {
#[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"))]
#[cfg(not(all(target_arch = "wasm", not(target_os = "wasi"))))]
let mut rng = rand::thread_rng();

#[cfg(not(target_family = "wasm"))]
#[cfg(not(all(target_arch = "wasm", not(target_os = "wasi"))))]
let mut rng_gen = move || rng.gen();

#[cfg(target_family = "wasm")]
#[cfg(all(target_arch = "wasm", not(target_os = "wasi")))]
let rng_gen = || random();

for (x, y) in ImageIterator::with_dimension(&img.dimensions()) {
Expand Down
4 changes: 2 additions & 2 deletions crate/src/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ pub fn crop(
}
}

#[cfg(target_arch = "wasm32")]
#[cfg(all(target_arch = "wasm32", not(target_os = "wasi")))]
#[cfg_attr(feature = "enable_wasm", wasm_bindgen)]
pub fn crop_img_browser(
source_canvas: HtmlCanvasElement,
Expand Down Expand Up @@ -198,7 +198,7 @@ fn filter_type_from_sampling_filter(sampling_filter: SamplingFilter) -> FilterTy
/// * `width` - New width.
/// * `height` - New height.
/// * `sampling_filter` - Nearest = 1, Triangle = 2, CatmullRom = 3, Gaussian = 4, Lanczos3 = 5
#[cfg(target_arch = "wasm32")]
#[cfg(all(target_arch = "wasm32", not(target_os = "wasi")))]
#[cfg_attr(feature = "enable_wasm", wasm_bindgen)]
pub fn resize_img_browser(
photon_img: &PhotonImage,
Expand Down

0 comments on commit 8b17f68

Please sign in to comment.