Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions src/components/WasmImageProcessor.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const WasmImageProcessor = () => {
const inputId = useId();
const inputRef = useRef(null);

const { gaussianBlur, blackThreshold, kmeans, mergeSmallRegionsInPlace } = useWasmWorker();
const { bilateralFilter, blackThreshold, kmeans, mergeSmallRegionsInPlace } = useWasmWorker();

const [originalSrc, setOriginalSrc] = useState(null);
const [fileData, setFileData] = useState(null);
Expand Down Expand Up @@ -81,12 +81,19 @@ const WasmImageProcessor = () => {
const { width, height } = fileData;

step(20);
const blurred = await gaussianBlur(fileData);
// NOTE: Gaussian blur destroys the sharp outlines first, preventing the Bilateral filter from detecting and preserving them
// const blurred = await gaussianBlur(fileData);

const imgBilateralFiltered = await bilateralFilter({
pixels: fileData.pixels,
width,
height,
});

step(45);
const thresholded = await blackThreshold({
...fileData,
pixels: blurred,
pixels: imgBilateralFiltered,
num_colors: 8,
});

Expand Down Expand Up @@ -139,7 +146,7 @@ const WasmImageProcessor = () => {
step(0);
}, 800);
}
}, [fileData, gaussianBlur, blackThreshold, kmeans, mergeSmallRegionsInPlace, navigate, step]);
}, [fileData, bilateralFilter, blackThreshold, kmeans, mergeSmallRegionsInPlace, navigate, step]);

/* Memo'd UI fragments */
const EmptyState = useMemo(
Expand Down
5 changes: 4 additions & 1 deletion src/hooks/useWasmWorker.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ export function useWasmWorker() {
const gaussianBlur = async ({ pixels, width, height, sigma_pixels = width * 0.005 }) => {
return (await call('gaussian_blur_fft', { pixels, width, height, sigma_pixels }, ['pixels'])).output.pixels;
};
const bilateralFilter = async ({ pixels, width, height, sigma_spatial = 3.0, sigma_range = 50.0 }) => {
return (await call('bilateral_filter', { pixels, width, height, sigma_spatial, sigma_range }, ['pixels'])).output.pixels;
};
const blackThreshold = async ({ pixels, width, height, num_colors }) => {
return (await call('black_threshold_image', { pixels, width, height, num_colors }, ['pixels'])).output.pixels;
};
Expand All @@ -46,5 +49,5 @@ export function useWasmWorker() {
.output.pixels;
};

return { call, gaussianBlur, blackThreshold, kmeans, mergeSmallRegionsInPlace };
return { call, gaussianBlur, bilateralFilter, blackThreshold, kmeans, mergeSmallRegionsInPlace };
}
53 changes: 53 additions & 0 deletions src/hooks/useWasmWorker.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,59 @@ describe('useWasmWorker', () => {
});
});

describe('bilateralFilter', () => {
it('should call worker with bilateral_filter function', async () => {
const { result } = renderHook(() => useWasmWorker());

const pixels = new Uint8ClampedArray([255, 0, 0, 255]);
const width = 1;
const height = 1;

act(() => {
result.current.bilateralFilter({ pixels, width, height });
});

expect(mockWorkerInstance.postMessage).toHaveBeenCalledWith(
expect.objectContaining({
funcName: 'bilateral_filter',
args: expect.objectContaining({
pixels,
width,
height,
sigma_spatial: 3.0,
sigma_range: 50.0,
}),
bufferKeys: ['pixels'],
})
);
});

it('should use custom sigma_spatial and sigma_range when provided', async () => {
const { result } = renderHook(() => useWasmWorker());

const pixels = new Uint8ClampedArray([255, 0, 0, 255]);

act(() => {
result.current.bilateralFilter({
pixels,
width: 100,
height: 100,
sigma_spatial: 5.0,
sigma_range: 25.0,
});
});

expect(mockWorkerInstance.postMessage).toHaveBeenCalledWith(
expect.objectContaining({
args: expect.objectContaining({
sigma_spatial: 5.0,
sigma_range: 25.0,
}),
})
);
});
});

describe('blackThreshold', () => {
it('should call worker with black_threshold_image function', async () => {
const { result } = renderHook(() => useWasmWorker());
Expand Down
3 changes: 3 additions & 0 deletions src/wasm/modules/image/include/image_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,7 @@ EXPORTED void threshold_image(uint8_t *ptr, const int width, const int height,
EXPORTED void black_threshold_image(uint8_t *ptr, const int width,
const int height, const int num_thresholds);

EXPORTED void bilateral_filter(uint8_t *image, size_t width, size_t height,
double sigma_spatial, double sigma_range);

#endif
103 changes: 103 additions & 0 deletions src/wasm/modules/image/src/bilateral_filter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
#include "image_utils.h"
#include <cmath>
#include <vector>
#include <algorithm>
#include <cstring>

static constexpr double SIGMA_RADIUS_FACTOR = 3.0;
static constexpr int MAX_PIXEL_VAL = 255;
// Max possible squared Euclidean distance in a 3-channel 8-bit image: 255^2 * 3 = 195075
// Means max delta between images (imageA - imageB) in RGB channels (255^2 * 3)
static constexpr int MAX_RGB_DIST_SQ = 255 * 255 * 3;

/*
The Bilateral Filter applies a composite weight based on both spatial distance and radiometric difference (intensity) to return an image that is smoothed while preserving edges.
It reduces noise in flat regions while preserving edges by assigning near-zero weight to pixels across high-contrast boundaries.

Parameters:
- image: Pointer to RGBA pixel buffer
- width, height: Image dimensions (px)
- sigma_spatial: Gaussian standard deviation for spatial proximity (spatial decay)
- sigma_range: Gaussian standard deviation for intensity difference (radiometric decay)
*/
void bilateral_filter(uint8_t *image, size_t width, size_t height,
double sigma_spatial, double sigma_range) {
if (sigma_spatial <= 0.0 || sigma_range <= 0.0) return;

const int radius = static_cast<int>(std::ceil(SIGMA_RADIUS_FACTOR * sigma_spatial));
const int kernel_width = 2 * radius + 1;
const size_t stride = width * 4;
std::vector<uint8_t> result(width * height * 4);

// NOTE: precompute Spatial Weights (Gaussian Kernel)
std::vector<double> spatial_weights(kernel_width * kernel_width);
double two_sigma_space_sq = 2 * sigma_spatial * sigma_spatial;

for (int ky = -radius; ky <= radius; ++ky) {
for (int kx = -radius; kx <= radius; ++kx) {
double dist2 = static_cast<double>(kx * kx + ky * ky);
spatial_weights[(ky + radius) * kernel_width + (kx + radius)] =
std::exp(-dist2 / two_sigma_space_sq);
}
}

// NOTE: precompute Range Weights
std::vector<double> range_lut(MAX_RGB_DIST_SQ + 1);
double two_sigma_range_sq = 2 * sigma_range * sigma_range;

for (int i = 0; i <= MAX_RGB_DIST_SQ; ++i) {
range_lut[i] = std::exp(-static_cast<double>(i) / two_sigma_range_sq);
}

int h = static_cast<int>(height);
int w = static_cast<int>(width);

for (int y = 0; y < h; ++y) {
for (int x = 0; x < w; ++x) {
size_t center_idx = (y * width + x) * 4;

uint8_t r0 = image[center_idx];
uint8_t g0 = image[center_idx + 1];
uint8_t b0 = image[center_idx + 2];
uint8_t a0 = image[center_idx + 3];

double r_acc = 0.0, g_acc = 0.0, b_acc = 0.0, weight_acc = 0.0;

for (int ky = -radius; ky <= radius; ++ky) {
int ny = std::clamp(y + ky, 0, h - 1);

for (int kx = -radius; kx <= radius; ++kx) {
int nx = std::clamp(x + kx, 0, w - 1);

size_t neighbor_idx = (ny * width + nx) * 4;

uint8_t r = image[neighbor_idx];
uint8_t g = image[neighbor_idx + 1];
uint8_t b = image[neighbor_idx + 2];

double w_space = spatial_weights[(ky + radius) * kernel_width + (kx + radius)];

int dr = static_cast<int>(r) - r0;
int dg = static_cast<int>(g) - g0;
int db = static_cast<int>(b) - b0;
int dist_sq = dr*dr + dg*dg + db*db;

double w_range = range_lut[dist_sq];
double w = w_space * w_range;

r_acc += r * w;
g_acc += g * w;
b_acc += b * w;
weight_acc += w;
}
}

result[center_idx] = static_cast<uint8_t>(std::clamp(r_acc / weight_acc, 0.0, 255.0));
result[center_idx + 1] = static_cast<uint8_t>(std::clamp(g_acc / weight_acc, 0.0, 255.0));
result[center_idx + 2] = static_cast<uint8_t>(std::clamp(b_acc / weight_acc, 0.0, 255.0));
result[center_idx + 3] = a0;
Comment thread
Ryan-Millard marked this conversation as resolved.
Resolved
}
}

std::memcpy(image, result.data(), result.size());
}