working colour scanner

This commit is contained in:
Magnus Ulimoen 2020-07-25 00:45:14 +02:00
parent aae03e1f10
commit a393b69961
1 changed files with 49 additions and 22 deletions

View File

@ -295,7 +295,8 @@ impl Opt {
) )
})?; })?;
} }
assert_eq!(val.pop(), Some(0)); let first_zero = val.iter().position(|&x| x == 0).unwrap_or(val.len());
val.resize(first_zero, 0);
Ok(String::from_utf8(val).unwrap()) Ok(String::from_utf8(val).unwrap())
} }
fn set_string(&self, val: &str) -> Result<(), Error> { fn set_string(&self, val: &str) -> Result<(), Error> {
@ -344,23 +345,11 @@ struct Acquisition<'a> {
impl<'a> Acquisition<'a> { impl<'a> Acquisition<'a> {
fn cancel(self) {} fn cancel(self) {}
fn restart(&self) -> Result<(), Error> {
self.handle.start().map(|x| std::mem::forget(x))
}
fn get_image(self) -> Result<Image, Error> { fn read_image(&self, mut buffer: &mut [u8]) -> Result<(), Error> {
let parameters = self.handle.parameters()?;
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[..];
// unsafe { checked(|| sane_set_io_mode(self.handle.0, SANE_FALSE as _))? };
unsafe { unsafe {
'read_loop: loop { 'read_loop: loop {
let mut len = 0; let mut len = 0;
@ -384,6 +373,31 @@ impl<'a> Acquisition<'a> {
} }
} }
assert_eq!(buffer.len(), 0); assert_eq!(buffer.len(), 0);
Ok(())
}
fn get_image(self) -> Result<Image, Error> {
let parameters = self.handle.parameters()?;
let bytesize = parameters.pixels_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 _];
// unsafe { checked(|| sane_set_io_mode(self.handle.0, SANE_FALSE as _))? };
#[allow(non_upper_case_globals)]
match parameters.format() {
SANE_Frame_SANE_FRAME_GRAY => self.read_image(&mut image[..])?,
SANE_Frame_SANE_FRAME_RGB => {
self.read_image(&mut image[..])?;
}
format => todo!("format: {}", format),
};
#[allow(non_upper_case_globals)] #[allow(non_upper_case_globals)]
match (parameters.format(), parameters.depth()) { match (parameters.format(), parameters.depth()) {
@ -395,7 +409,15 @@ impl<'a> Acquisition<'a> {
) )
.unwrap(), .unwrap(),
)), )),
(_, _) => unimplemented!(), (SANE_Frame_SANE_FRAME_RGB, 8) => Ok(Image::Rgb8(
image::ImageBuffer::from_raw(
parameters.pixels_per_line() as _,
parameters.lines() as _,
image,
)
.unwrap(),
)),
(format, depth) => unimplemented!("format: {} depth: {}", format, depth),
} }
} }
} }
@ -407,7 +429,7 @@ impl Drop for Acquisition<'_> {
} }
enum Image { enum Image {
// Rgb8(image::ImageBuffer<image::Rgb<u8>, Vec<u8>>), Rgb8(image::ImageBuffer<image::Rgb<u8>, Vec<u8>>),
Gray8(image::ImageBuffer<image::Luma<u8>, Vec<u8>>), Gray8(image::ImageBuffer<image::Luma<u8>, Vec<u8>>),
} }
@ -415,6 +437,7 @@ impl Image {
fn save(&self, path: impl AsRef<std::path::Path>) -> image::ImageResult<()> { fn save(&self, path: impl AsRef<std::path::Path>) -> image::ImageResult<()> {
match self { match self {
Image::Gray8(im) => im.save(path), Image::Gray8(im) => im.save(path),
Image::Rgb8(im) => im.save(path),
} }
} }
} }
@ -459,13 +482,17 @@ fn main() {
} }
match optname { match optname {
"mode" => { "mode" => {
let active_mode = option.get_string().unwrap();
print!("\t\t"); print!("\t\t");
for opt in option.string_constraints().unwrap() { for opt in option.string_constraints().unwrap() {
if opt == active_mode {
print!("[{}]\t", opt);
} else {
print!("{}\t", opt); print!("{}\t", opt);
} }
}
println!(); println!();
println!("\t\tActive mode: {}", option.get_string().unwrap()); option.set_string("Color").unwrap();
// option.set_string("Gray").unwrap();
} }
"depth" => { "depth" => {
println!("\t\tCurrent depth: {}", option.get_int().unwrap()); println!("\t\tCurrent depth: {}", option.get_int().unwrap());