imageproc/filter/
sharpen.rs

1use super::{filter3x3, gaussian_blur_f32};
2use crate::{
3    definitions::{Clamp, Image},
4    map::{map_colors2, map_subpixels},
5};
6use image::{GrayImage, Luma};
7
8/// Sharpens a grayscale image by applying a 3x3 approximation to the Laplacian.
9#[must_use = "the function does not modify the original image"]
10pub fn sharpen3x3(image: &GrayImage) -> GrayImage {
11    let identity_minus_laplacian = [0, -1, 0, -1, 5, -1, 0, -1, 0];
12    filter3x3(image, &identity_minus_laplacian)
13}
14
15/// Sharpens a grayscale image using a Gaussian as a low-pass filter.
16///
17/// * `sigma` is the standard deviation of the Gaussian filter used.
18/// * `amount` controls the level of sharpening. `output = input + amount * edges`.
19// TODO: remove unnecessary allocations, support colour images
20#[must_use = "the function does not modify the original image"]
21pub fn sharpen_gaussian(image: &GrayImage, sigma: f32, amount: f32) -> GrayImage {
22    let image = map_subpixels(image, |x| x as f32);
23    let smooth: Image<Luma<f32>> = gaussian_blur_f32(&image, sigma);
24    map_colors2(&image, &smooth, |p, q| {
25        let v = (1.0 + amount) * p[0] - amount * q[0];
26        Luma([<u8 as Clamp<f32>>::clamp(v)])
27    })
28}