Extract common logic in examples
This commit is contained in:
1 parent
c8c71cc07e
commit
5de9976d1c
3 files changed
+45
-64
No files matched your search
+7
-34
@@ -1,38 +1,11 @@
|
|||||||
|
mod common;
|
||||||
|
|
||||||
use ccvl::filter;
|
use ccvl::filter;
|
||||||
use ccvl::image::*;
|
use ccvl::image;
|
||||||
|
|
||||||
fn main() -> std::process::ExitCode {
|
fn main() -> std::process::ExitCode {
|
||||||
let args = std::env::args().collect::<Vec<String>>();
|
common::process_image(|img| {
|
||||||
if args.len() != 3 {
|
let greyscale = img.map(|p| image::Luminance::from(p));
|
||||||
println!("Usage: {} IN OUT", args[0].as_str());
|
filter::canny(greyscale).map(|p| p.into())
|
||||||
return std::process::ExitCode::FAILURE;
|
})
|
||||||
}
|
|
||||||
|
|
||||||
let input_path = args[1].as_str();
|
|
||||||
let output_path = args[2].as_str();
|
|
||||||
|
|
||||||
match Image::load::<Farbfeld>(input_path) {
|
|
||||||
Err(Error::Invalid) => {
|
|
||||||
println!("{} is not a valid image", input_path);
|
|
||||||
std::process::ExitCode::FAILURE
|
|
||||||
}
|
|
||||||
Err(Error::Io(err)) => {
|
|
||||||
println!("I/O error loading image {}: {}", input_path, err);
|
|
||||||
std::process::ExitCode::FAILURE
|
|
||||||
}
|
|
||||||
Ok(img) => {
|
|
||||||
let result = filter::canny(img.map(|p| Luminance::from(p)))
|
|
||||||
.map(|p| p.into());
|
|
||||||
match result.save::<Farbfeld>(output_path) {
|
|
||||||
Err(err) => {
|
|
||||||
println!(
|
|
||||||
"I/O error saving image {}: {}",
|
|
||||||
output_path, err
|
|
||||||
);
|
|
||||||
std::process::ExitCode::FAILURE
|
|
||||||
}
|
|
||||||
Ok(()) => std::process::ExitCode::SUCCESS,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,33 @@
|
|||||||
|
use ccvl::image::*;
|
||||||
|
|
||||||
|
pub fn process_image<F>(f: F) -> std::process::ExitCode
|
||||||
|
where
|
||||||
|
F: FnOnce(Image<SRGBA>) -> Image<SRGBA>,
|
||||||
|
{
|
||||||
|
let args = std::env::args().collect::<Vec<String>>();
|
||||||
|
if args.len() != 3 {
|
||||||
|
println!("Usage: {} IN OUT", args[0].as_str());
|
||||||
|
return std::process::ExitCode::FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
|
let input_path = args[1].as_str();
|
||||||
|
let output_path = args[2].as_str();
|
||||||
|
|
||||||
|
match Image::load::<Farbfeld>(input_path) {
|
||||||
|
Err(Error::Invalid) => {
|
||||||
|
println!("{} is not a valid image", input_path);
|
||||||
|
std::process::ExitCode::FAILURE
|
||||||
|
}
|
||||||
|
Err(Error::Io(err)) => {
|
||||||
|
println!("I/O error loading image {}: {}", input_path, err);
|
||||||
|
std::process::ExitCode::FAILURE
|
||||||
|
}
|
||||||
|
Ok(img) => match f(img).save::<Farbfeld>(output_path) {
|
||||||
|
Err(err) => {
|
||||||
|
println!("I/O error saving image {}: {}", output_path, err);
|
||||||
|
std::process::ExitCode::FAILURE
|
||||||
|
}
|
||||||
|
Ok(()) => std::process::ExitCode::SUCCESS,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
+5
-30
@@ -1,34 +1,9 @@
|
|||||||
use ccvl::image::*;
|
mod common;
|
||||||
|
|
||||||
fn filter(img: Image<SRGBA>) -> Image<SRGBA> {
|
use ccvl::image;
|
||||||
img.map(|p| Luminance::from(p).into())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn main() -> std::process::ExitCode {
|
fn main() -> std::process::ExitCode {
|
||||||
let args = std::env::args().collect::<Vec<String>>();
|
common::process_image(|img| {
|
||||||
if args.len() != 3 {
|
img.map(|p| image::Luminance::from(p).into())
|
||||||
println!("Usage: {} IN OUT", args[0].as_str());
|
})
|
||||||
return std::process::ExitCode::FAILURE;
|
|
||||||
}
|
|
||||||
|
|
||||||
let input_path = args[1].as_str();
|
|
||||||
let output_path = args[2].as_str();
|
|
||||||
|
|
||||||
match Image::load::<Farbfeld>(input_path) {
|
|
||||||
Err(Error::Invalid) => {
|
|
||||||
println!("{} is not a valid image", input_path);
|
|
||||||
std::process::ExitCode::FAILURE
|
|
||||||
}
|
|
||||||
Err(Error::Io(err)) => {
|
|
||||||
println!("I/O error loading image {}: {}", input_path, err);
|
|
||||||
std::process::ExitCode::FAILURE
|
|
||||||
}
|
|
||||||
Ok(img) => match filter(img).save::<Farbfeld>(output_path) {
|
|
||||||
Err(err) => {
|
|
||||||
println!("I/O error saving image {}: {}", output_path, err);
|
|
||||||
std::process::ExitCode::FAILURE
|
|
||||||
}
|
|
||||||
Ok(()) => std::process::ExitCode::SUCCESS,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user