Skip to content

Commit

Permalink
Remove path support from Config
Browse files Browse the repository at this point in the history
Instead the input_path and output_path have to be passed to
convert_image_to_svg() manually.
  • Loading branch information
linkmauve committed Oct 29, 2023
1 parent 6cdef49 commit 2b5b574
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 29 deletions.
15 changes: 1 addition & 14 deletions cmdapp/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::str::FromStr;
use std::path::PathBuf;
use visioncortex::PathSimplifyMode;

pub enum Preset {
Expand All @@ -20,8 +19,6 @@ pub enum Hierarchical {

/// Converter config
pub struct Config {
pub input_path: PathBuf,
pub output_path: PathBuf,
pub color_mode: ColorMode,
pub hierarchical: Hierarchical,
pub filter_speckle: usize,
Expand Down Expand Up @@ -52,8 +49,6 @@ pub(crate) struct ConverterConfig {
impl Default for Config {
fn default() -> Self {
Self {
input_path: PathBuf::default(),
output_path: PathBuf::default(),
color_mode: ColorMode::Color,
hierarchical: Hierarchical::Stacked,
mode: PathSimplifyMode::Spline,
Expand Down Expand Up @@ -107,13 +102,9 @@ impl FromStr for Preset {
}

impl Config {
pub fn from_preset(preset: Preset, input_path: &str, output_path: &str) -> Self {
let input_path = PathBuf::from(input_path);
let output_path = PathBuf::from(output_path);
pub fn from_preset(preset: Preset) -> Self {
match preset {
Preset::Bw => Self {
input_path,
output_path,
color_mode: ColorMode::Binary,
hierarchical: Hierarchical::Stacked,
filter_speckle: 4,
Expand All @@ -127,8 +118,6 @@ impl Config {
path_precision: Some(8),
},
Preset::Poster => Self {
input_path,
output_path,
color_mode: ColorMode::Color,
hierarchical: Hierarchical::Stacked,
filter_speckle: 4,
Expand All @@ -142,8 +131,6 @@ impl Config {
path_precision: Some(8),
},
Preset::Photo => Self {
input_path,
output_path,
color_mode: ColorMode::Color,
hierarchical: Hierarchical::Stacked,
filter_speckle: 10,
Expand Down
7 changes: 3 additions & 4 deletions cmdapp/src/converter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,14 @@ const NUM_UNUSED_COLOR_ITERATIONS: usize = 6;
const KEYING_THRESHOLD: f32 = 0.2;

/// Convert an image file into svg file
pub fn convert_image_to_svg(config: Config) -> Result<(), String> {
let img = read_image(&config.input_path)?;
let output_path = config.output_path.clone();
pub fn convert_image_to_svg(input_path: &Path, output_path: &Path, config: Config) -> Result<(), String> {
let img = read_image(input_path)?;
let config = config.into_converter_config();
let svg = match config.color_mode {
ColorMode::Color => color_image_to_svg(img, config)?,
ColorMode::Binary => binary_image_to_svg(img, config)?,
};
write_svg(svg, &output_path)
write_svg(svg, output_path)
}

fn color_exists_in_image(img: &ColorImage, color: Color) -> bool {
Expand Down
16 changes: 8 additions & 8 deletions cmdapp/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ fn path_simplify_mode_from_str(s: &str) -> PathSimplifyMode {
}
}

pub fn config_from_args() -> Config {
pub fn config_from_args() -> (PathBuf, PathBuf, Config) {
let app = App::new("visioncortex VTracer ".to_owned() + env!("CARGO_PKG_VERSION"))
.about("A cmd app to convert images into vector graphics.");

Expand Down Expand Up @@ -107,13 +107,13 @@ pub fn config_from_args() -> Config {
let input_path = matches.value_of("input").expect("Input path is required, please specify it by --input or -i.");
let output_path = matches.value_of("output").expect("Output path is required, please specify it by --output or -o.");

let input_path = PathBuf::from(input_path);
let output_path = PathBuf::from(output_path);

if let Some(value) = matches.value_of("preset") {
config = Config::from_preset(Preset::from_str(value).unwrap(), input_path, output_path);
config = Config::from_preset(Preset::from_str(value).unwrap());
}

config.input_path = PathBuf::from(input_path);
config.output_path = PathBuf::from(output_path);

if let Some(value) = matches.value_of("color_mode") {
config.color_mode = ColorMode::from_str(if value.trim() == "bw" || value.trim() == "BW" {"binary"} else {"color"}).unwrap()
}
Expand Down Expand Up @@ -216,12 +216,12 @@ pub fn config_from_args() -> Config {
}
}

config
(input_path, output_path, config)
}

fn main() {
let config = config_from_args();
let result = converter::convert_image_to_svg(config);
let (input_path, output_path, config) = config_from_args();
let result = converter::convert_image_to_svg(&input_path, &output_path, config);
match result {
Ok(()) => {
println!("Conversion successful.");
Expand Down
4 changes: 1 addition & 3 deletions cmdapp/src/python.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ fn convert_image_to_svg_py(
let max_iterations = max_iterations.unwrap_or(10);

let config = Config {
input_path,
output_path,
color_mode,
hierarchical,
filter_speckle,
Expand All @@ -69,7 +67,7 @@ fn convert_image_to_svg_py(
..Default::default()
};

convert_image_to_svg(config).unwrap();
convert_image_to_svg(&input_path, &output_path, config).unwrap();
Ok(())
}

Expand Down

0 comments on commit 2b5b574

Please sign in to comment.