Skip to content

PdfCraft: An open-source PDF reader and editor written by scratch in pure Rust

Fill & Sign: remove white background from imported signature images (#637)

mrme2tiri-hash6e13a5fc5409
diff --git a/crates/engine/src/signature_image.rs b/crates/engine/src/signature_image.rs
index d1e3ad5b0..889195414 100644
--- a/crates/engine/src/signature_image.rs
+++ b/crates/engine/src/signature_image.rs
@@ -164,6 +164,23 @@ impl SignatureImage {
self.size
}
+ /// Make near-white paper transparent, keeping darker ink.
+ pub fn remove_white_background(&self, cutoff: u8, feather: u8) -> Result<Self, SignatureImageError> {
+ let [w, h] = self.size;
+ let mut rgba = image::RgbaImage::from_raw(w as u32, h as u32, self.rgba.as_ref().clone()).ok_or(SignatureImageError::Size)?;
+ let high = f32::from(cutoff);
+ let span = f32::from(feather.max(1));
+ for pixel in rgba.pixels_mut() {
+ let [r, g, b, a] = pixel.0;
+ let whiteness = f32::from(r.min(g).min(b));
+ let keep = ((high - whiteness) / span).clamp(0.0, 1.0);
+ pixel.0[3] = (f32::from(a) * keep).round() as u8;
+ }
+ let mut png = Cursor::new(Vec::new());
+ DynamicImage::ImageRgba8(rgba).write_to(&mut png, ImageFormat::Png)?;
+ Self::from_bytes(&png.into_inner())
+ }
+
/// The user-space box for the image with its left edge at `at` (a user-space point on `page`),
/// vertically centered, with the same 150 pt width cap as typed names. Left and centered are
/// as displayed: on a page with `/Rotate` the box runs along the turned page's axes.
@@ -324,4 +341,25 @@ mod tests {
assert!(rect.iter().zip(want).all(|(a, b)| (a - b).abs() < 0.01), "{rotation}: {rect:?}, want {want:?}");
}
}
+
+ #[test]
+ fn white_background_becomes_transparent_and_ink_is_kept() {
+ let mut source = image::RgbaImage::new(4, 1);
+ source.put_pixel(0, 0, image::Rgba([255, 255, 255, 255]));
+ source.put_pixel(1, 0, image::Rgba([20, 20, 20, 255]));
+ source.put_pixel(2, 0, image::Rgba([227, 227, 227, 255]));
+ source.put_pixel(3, 0, image::Rgba([10, 10, 10, 100]));
+ let mut encoded = Cursor::new(Vec::new());
+ source.write_to(&mut encoded, ImageFormat::Png).unwrap();
+
+ let image = SignatureImage::from_bytes(&encoded.into_inner()).unwrap();
+ let cleaned = image.remove_white_background(245, 35).unwrap();
+ let pixels = cleaned.rgba().as_chunks::<4>().0;
+
+ assert_eq!(cleaned.size(), [4, 1]);
+ assert_eq!(pixels[0][3], 0, "white paper must be transparent");
+ assert_eq!(pixels[1], [20, 20, 20, 255], "dark ink must be unchanged");
+ assert!(pixels[2][3] > 0 && pixels[2][3] < 255, "near-white must fade");
+ assert_eq!(pixels[3][3], 100, "existing transparency must be kept");
+ }
}
diff --git a/crates/ui-egui/src/fill_sign.rs b/crates/ui-egui/src/fill_sign.rs
index 152fde4a1..27c7ee261 100644
--- a/crates/ui-egui/src/fill_sign.rs
+++ b/crates/ui-egui/src/fill_sign.rs
@@ -706,7 +706,8 @@ impl crate::PdfCraftApp {
}
fn use_signature_image(&mut self, result: Result<SignatureImage, String>) {
- match result {
+ let cleaned = result.and_then(|image| image.remove_white_background(245, 35).map_err(|e| e.to_string()));
+ match cleaned {
Ok(image) => {
self.signature_draft.image = Some(image);
self.toast = None;