From 214dfbcfd26f14b40275db6c15114644f1addd84 Mon Sep 17 00:00:00 2001 From: "Dr.Guo" Date: Mon, 3 Nov 2025 23:38:33 +0800 Subject: [PATCH] Optimize threshold_mut function with parallel iteration Replace serial iterators with par_iter_mut() for improved performance. Leverage Rayon for parallel processing of image pixels. Maintains same functionality while potentially reducing processing time. --- src/contrast.rs | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/contrast.rs b/src/contrast.rs index c9024f1b..7740063f 100644 --- a/src/contrast.rs +++ b/src/contrast.rs @@ -254,29 +254,29 @@ pub fn threshold(image: &GrayImage, threshold: u8, threshold_type: ThresholdType pub fn threshold_mut(image: &mut GrayImage, threshold: u8, threshold_type: ThresholdType) { match threshold_type { ThresholdType::Binary => { - for p in image.iter_mut() { + image.par_iter_mut().for_each(|p| { *p = if *p > threshold { 255 } else { 0 }; - } + }); } ThresholdType::BinaryInverted => { - for p in image.iter_mut() { + image.par_iter_mut().for_each(|p| { *p = if *p > threshold { 0 } else { 255 }; - } + }); } ThresholdType::Truncate => { - for p in image.iter_mut() { + image.par_iter_mut().for_each(|p| { *p = if *p > threshold { threshold } else { *p }; - } + }); } ThresholdType::ToZero => { - for p in image.iter_mut() { + image.par_iter_mut().for_each(|p| { *p = if *p > threshold { 0 } else { *p }; - } + }); } ThresholdType::ToZeroInverted => { - for p in image.iter_mut() { + image.par_iter_mut().for_each(|p| { *p = if *p > threshold { *p } else { 0 }; - } + }); } } }