Skip to content

Instantly share code, notes, and snippets.

@JessicaWachtel
Created January 19, 2026 17:36
Show Gist options
  • Select an option

  • Save JessicaWachtel/3e427f1207e1955370378e3edc08aa50 to your computer and use it in GitHub Desktop.

Select an option

Save JessicaWachtel/3e427f1207e1955370378e3edc08aa50 to your computer and use it in GitHub Desktop.
use wasm_bindgen::prelude::*;
// RACE 1: Light Task (Grayscale conversion)
#[wasm_bindgen]
pub fn apply_grayscale_wasm(pixels: &mut [u8]) {
for i in (0..pixels.len()).step_by(4) {
let r = pixels[i] as u32;
let g = pixels[i + 1] as u32;
let b = pixels[i + 2] as u32;
let avg = ((r + g + b) / 3) as u8;
pixels[i] = avg;
pixels[i + 1] = avg;
pixels[i + 2] = avg;
}
}
// RACE 2: Heavy Task (Sharpen)
#[wasm_bindgen]
pub fn apply_sharpen_wasm(pixels: &mut [u8], width: u32, height: u32) {
let copy = pixels.to_vec();
for y in 1..(height - 1) {
for x in 1..(width - 1) {
let i = ((y * width + x) * 4) as usize;
for c in 0..3 {
let center = i + c;
let top = center - (width * 4) as usize;
let bottom = center + (width * 4) as usize;
let val = 5 * copy[center] as i32
- copy[top] as i32 - copy[bottom] as i32
- copy[center - 4] as i32 - copy[center + 4] as i32;
pixels[center] = val.clamp(0, 255) as u8;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment