Publish to crates.io

This commit is contained in:
Chris Tsang
2020-10-31 17:43:30 +08:00
parent 0c4fa1d742
commit cbb6c97ce4
9 changed files with 358 additions and 75 deletions

View File

@@ -1,7 +1,7 @@
use std::str::FromStr;
use std::path::PathBuf;
use clap::{Arg, App};
use visioncortex::path::PathSimplifyMode;
use super::util::{path_simplify_mode, preset, deg2rad};
pub enum Preset {
Bw,
@@ -9,11 +9,18 @@ pub enum Preset {
Photo
}
#[derive(Debug)]
pub enum ColorMode {
Color,
Binary,
}
/// Converter config
#[derive(Debug)]
pub struct Config {
pub input_path: PathBuf,
pub output_path: PathBuf,
pub color_mode: String,
pub color_mode: ColorMode,
pub filter_speckle: usize,
pub color_precision: i32,
pub layer_difference: i32,
@@ -25,10 +32,10 @@ pub struct Config {
}
#[derive(Debug)]
pub struct ConverterConfig {
pub(crate) struct ConverterConfig {
pub input_path: PathBuf,
pub output_path: PathBuf,
pub color_mode: String,
pub color_mode: ColorMode,
pub filter_speckle_area: usize,
pub color_precision_loss: i32,
pub layer_difference: i32,
@@ -44,8 +51,8 @@ impl Default for Config {
Self {
input_path: PathBuf::default(),
output_path: PathBuf::default(),
color_mode: String::from("color"),
mode: path_simplify_mode("spline"),
color_mode: ColorMode::Color,
mode: PathSimplifyMode::Spline,
filter_speckle: 4,
color_precision: 6,
layer_difference: 16,
@@ -57,6 +64,40 @@ impl Default for Config {
}
}
impl FromStr for ColorMode {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"color" => Ok(Self::Color),
"binary" => Ok(Self::Binary),
_ => Err(format!("unknown ColorMode {}", s)),
}
}
}
impl FromStr for Preset {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"bw" => Ok(Self::Bw),
"poster" => Ok(Self::Poster),
"photo" => Ok(Self::Photo),
_ => Err(format!("unknown Preset {}", s)),
}
}
}
fn path_simplify_mode_from_str(s: &str) -> PathSimplifyMode {
match s {
"polygon" => PathSimplifyMode::Polygon,
"spline" => PathSimplifyMode::Spline,
"none" => PathSimplifyMode::None,
_ => panic!("unknown PathSimplifyMode {}", s),
}
}
impl Config {
pub fn from_args() -> Self {
let app = App::new("visioncortex VTracer").about("A cmd app to convert images into vector graphics.");
@@ -135,19 +176,19 @@ impl Config {
let output_path = matches.value_of("output").expect("Output path is required, please specify it by --output or -o.");
if let Some(value) = matches.value_of("preset") {
config = Self::from_preset(preset(value), input_path, output_path);
config = Self::from_preset(Preset::from_str(value).unwrap(), input_path, output_path);
}
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 = String::from(if value.trim() == "bw" || value.trim() == "BW" {"binary"} else {"color"})
config.color_mode = ColorMode::from_str(if value.trim() == "bw" || value.trim() == "BW" {"binary"} else {"color"}).unwrap()
}
if let Some(value) = matches.value_of("mode") {
let value = value.trim();
config.mode = path_simplify_mode(if value == "pixel" {
config.mode = path_simplify_mode_from_str(if value == "pixel" {
"none"
} else if value == "polygon" {
"polygon"
@@ -240,11 +281,11 @@ impl Config {
Preset::Bw => Self {
input_path,
output_path,
color_mode: String::from("binary"),
color_mode: ColorMode::Binary,
filter_speckle: 4,
color_precision: 6,
layer_difference: 16,
mode: path_simplify_mode("spline"),
mode: PathSimplifyMode::Spline,
corner_threshold: 60,
length_threshold: 4.0,
max_iterations: 10,
@@ -253,11 +294,11 @@ impl Config {
Preset::Poster => Self {
input_path,
output_path,
color_mode: String::from("color"),
color_mode: ColorMode::Color,
filter_speckle: 4,
color_precision: 8,
layer_difference: 16,
mode: path_simplify_mode("spline"),
mode: PathSimplifyMode::Spline,
corner_threshold: 60,
length_threshold: 4.0,
max_iterations: 10,
@@ -266,11 +307,11 @@ impl Config {
Preset::Photo => Self {
input_path,
output_path,
color_mode: String::from("color"),
color_mode: ColorMode::Color,
filter_speckle: 10,
color_precision: 8,
layer_difference: 48,
mode: path_simplify_mode("spline"),
mode: PathSimplifyMode::Spline,
corner_threshold: 180,
length_threshold: 4.0,
max_iterations: 10,
@@ -279,7 +320,7 @@ impl Config {
}
}
pub fn into_converter_config(self) -> ConverterConfig {
pub(crate) fn into_converter_config(self) -> ConverterConfig {
ConverterConfig {
input_path: self.input_path,
output_path: self.output_path,
@@ -295,3 +336,7 @@ impl Config {
}
}
}
fn deg2rad(deg: i32) -> f64 {
deg as f64 / 180.0 * std::f64::consts::PI
}

View File

@@ -1,24 +1,20 @@
use std::path::PathBuf;
use std::{fs::File, io::Write};
use visioncortex::Color;
use visioncortex::{ColorName, color_clusters::RunnerConfig};
use visioncortex::{ColorImage, color_clusters::Runner};
use super::config::{Config, ConverterConfig};
use visioncortex::{Color, ColorImage, ColorName, color_clusters::RunnerConfig, color_clusters::Runner};
use super::config::{Config, ColorMode, ConverterConfig};
use super::svg::SvgFile;
/// Convert an image file into svg file
pub fn convert_image_to_svg(config: Config) -> Result<(), String> {
let config = config.into_converter_config();
if config.color_mode == "color" {
color_image_to_svg(config)
} else if config.color_mode == "binary" {
binary_image_to_svg(config)
} else {
Err(format!("Unknown color mode: {}", config.color_mode))
match config.color_mode {
ColorMode::Color => color_image_to_svg(config),
ColorMode::Binary => binary_image_to_svg(config),
}
}
pub fn color_image_to_svg(config: ConverterConfig) -> Result<(), String> {
fn color_image_to_svg(config: ConverterConfig) -> Result<(), String> {
let (img, width, height);
match read_image(config.input_path) {
Ok(values) => {
@@ -69,7 +65,7 @@ pub fn color_image_to_svg(config: ConverterConfig) -> Result<(), String> {
Ok(())
}
pub fn binary_image_to_svg(config: ConverterConfig) -> Result<(), String> {
fn binary_image_to_svg(config: ConverterConfig) -> Result<(), String> {
let (img, width, height);
match read_image(config.input_path) {

View File

@@ -1,9 +1,17 @@
// Copyright 2020 Tsang Hao Fung. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
mod config;
mod converter;
mod svg;
mod util;
pub use config::*;
pub use converter::*;
pub use svg::*;
pub use util::*;
pub use svg::*;

View File

@@ -1,24 +0,0 @@
use visioncortex::PathSimplifyMode;
use super::Preset;
pub fn path_simplify_mode(s: &str) -> PathSimplifyMode {
match s {
"polygon" => PathSimplifyMode::Polygon,
"spline" => PathSimplifyMode::Spline,
"none" => PathSimplifyMode::None,
_ => panic!("unknown PathSimplifyMode {}", s),
}
}
pub fn preset(s: &str) -> Preset {
match s {
"bw" => Preset::Bw,
"poster" => Preset::Poster,
"photo" => Preset::Photo,
_ => panic!("unknown Preset {}", s),
}
}
pub fn deg2rad(deg: i32) -> f64 {
deg as f64 / 180.0 * std::f64::consts::PI
}