From aae03e1f103398d6be7da43e017d82e5adeeabef Mon Sep 17 00:00:00 2001 From: Magnus Ulimoen Date: Fri, 24 Jul 2020 21:41:25 +0200 Subject: [PATCH] first successful scan! --- Cargo.toml | 1 + src/main.rs | 41 +++++++++++++++++++++++++++++++++++++---- 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 67a3e4f..7f4196d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,6 +6,7 @@ edition = "2018" [dependencies] sane-sys = { path = "sane-sys" } +image = "0.23.7" [workspace] members = [ diff --git a/src/main.rs b/src/main.rs index e127181..15d3898 100644 --- a/src/main.rs +++ b/src/main.rs @@ -345,10 +345,17 @@ struct Acquisition<'a> { impl<'a> Acquisition<'a> { fn cancel(self) {} - fn get_image(self) -> Result<(), Error> { + fn get_image(self) -> Result { let parameters = self.handle.parameters()?; - let bytesize = parameters.bytes_per_line() * parameters.depth(); + let bytesize = parameters.bytes_per_line() + * (parameters.depth() / 8) + * parameters.lines() + * if parameters.format() == SANE_Frame_SANE_FRAME_GRAY { + 1 + } else { + 3 + }; let mut image = vec![0_u8; bytesize as _]; let mut buffer = &mut image[..]; @@ -378,7 +385,18 @@ impl<'a> Acquisition<'a> { } assert_eq!(buffer.len(), 0); - Ok(()) + #[allow(non_upper_case_globals)] + match (parameters.format(), parameters.depth()) { + (SANE_Frame_SANE_FRAME_GRAY, 8) => Ok(Image::Gray8( + image::ImageBuffer::from_raw( + parameters.pixels_per_line() as _, + parameters.lines() as _, + image, + ) + .unwrap(), + )), + (_, _) => unimplemented!(), + } } } @@ -388,7 +406,20 @@ impl Drop for Acquisition<'_> { } } -const TESTDEVICE: bool = true; +enum Image { + // Rgb8(image::ImageBuffer, Vec>), + Gray8(image::ImageBuffer, Vec>), +} + +impl Image { + fn save(&self, path: impl AsRef) -> image::ImageResult<()> { + match self { + Image::Gray8(im) => im.save(path), + } + } +} + +const TESTDEVICE: bool = false; fn main() { let (context, version) = Context::init().unwrap(); @@ -451,4 +482,6 @@ fn main() { let acq = handle.start().unwrap(); let image = acq.get_image().unwrap(); + + image.save("test.png").unwrap(); }