From ead104f0ce6d0bf8263b1e678a3009ed04712979 Mon Sep 17 00:00:00 2001 From: Chris Tsang Date: Sun, 9 Oct 2022 17:20:42 +0800 Subject: [PATCH] Tweak should_key_image --- cmdapp/src/converter.rs | 26 +++++++------------------- 1 file changed, 7 insertions(+), 19 deletions(-) diff --git a/cmdapp/src/converter.rs b/cmdapp/src/converter.rs index 9d47f49..55d4cb9 100644 --- a/cmdapp/src/converter.rs +++ b/cmdapp/src/converter.rs @@ -63,28 +63,16 @@ fn should_key_image(img: &ColorImage) -> bool { return false; } - // Check for transparency in top and bottom rows of pixels + // Check for transparency at several scanlines let threshold = ((img.width * 2) as f32 * KEYING_THRESHOLD) as usize; let mut num_transparent_boundary_pixels = 0; - for x in 0..img.width { - if img.get_pixel(x, 0).a == 0 { - num_transparent_boundary_pixels += 1; - } - if img.get_pixel(x, img.height - 1).a == 0 { - num_transparent_boundary_pixels += 1; - } - - if num_transparent_boundary_pixels >= threshold { - return true; - } - } - - // Check for transparency in some inner pixels - let x_positions = [img.width / 4, img.width / 2, 3 * img.width / 4]; - let y_positions = [img.height / 4, img.height / 2, 3 * img.height / 4]; - for x in x_positions { - for y in y_positions { + let y_positions = [0, img.height / 4, img.height / 2, 3 * img.height / 4, img.height - 1]; + for y in y_positions { + for x in 0..img.width { if img.get_pixel(x, y).a == 0 { + num_transparent_boundary_pixels += 1; + } + if num_transparent_boundary_pixels >= threshold { return true; } }