Skip to content

Commit 9a6beef

Browse files
committed
Refactor median filter to remove gamma handling and simplify algorithm
1 parent a6b538a commit 9a6beef

File tree

1 file changed

+4
-19
lines changed

1 file changed

+4
-19
lines changed

node-graph/nodes/raster/src/filter.rs

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,6 @@ async fn median_filter(
5252
#[range((0., 50.))]
5353
#[hard_min(0.)]
5454
radius: PixelLength,
55-
/// Opt to incorrectly apply the filter with color calculations in gamma space for compatibility with the results from other software.
56-
gamma: bool,
5755
) -> Table<Raster<CPU>> {
5856
image_frame
5957
.into_iter()
@@ -65,7 +63,7 @@ async fn median_filter(
6563
// Minimum filter radius
6664
image.clone()
6765
} else {
68-
Raster::new_cpu(median_filter_algorithm(image.into_data(), radius as u32, gamma))
66+
Raster::new_cpu(median_filter_algorithm(image.into_data(), radius as u32))
6967
};
7068

7169
row.element = filtered_image;
@@ -212,13 +210,7 @@ fn box_blur_algorithm(mut original_buffer: Image<Color>, radius: f64, gamma: boo
212210
y_axis
213211
}
214212

215-
fn median_filter_algorithm(mut original_buffer: Image<Color>, radius: u32, gamma: bool) -> Image<Color> {
216-
if gamma {
217-
original_buffer.map_pixels(|px| px.to_gamma_srgb().to_associated_alpha(px.a()));
218-
} else {
219-
original_buffer.map_pixels(|px| px.to_associated_alpha(px.a()));
220-
}
221-
213+
fn median_filter_algorithm(original_buffer: Image<Color>, radius: u32) -> Image<Color> {
222214
let (width, height) = original_buffer.dimensions();
223215
let mut output = Image::new(width, height, Color::TRANSPARENT);
224216

@@ -260,17 +252,10 @@ fn median_filter_algorithm(mut original_buffer: Image<Color>, radius: u32, gamma
260252
}
261253
}
262254

263-
if gamma {
264-
output.map_pixels(|px| px.to_linear_srgb().to_unassociated_alpha());
265-
} else {
266-
output.map_pixels(|px| px.to_unassociated_alpha());
267-
}
268-
269255
output
270256
}
271-
272-
/// Finds the median of a slice using quickselect for efficiency.
273-
/// This avoids the cost of full sorting (O(n log n)).
257+
/// Finds the median of a slice using quickselect for O(n) average case performance.
258+
/// This is more efficient than sorting the entire slice which would be O(n log n).
274259
fn median_quickselect(values: &mut [f32]) -> f32 {
275260
let mid: usize = values.len() / 2;
276261
// nth_unstable is like quickselect: average O(n)

0 commit comments

Comments
 (0)