From cbb6c97ce462d798915bdf2ee47fb9aeff257cbf Mon Sep 17 00:00:00 2001 From: Chris Tsang <tyt2y7@gmail.com> Date: Sat, 31 Oct 2020 17:43:30 +0800 Subject: [PATCH] Publish to crates.io --- Readme.md | 41 +++++++- cmdapp/Cargo.toml | 29 +++--- cmdapp/LICENSE-APACHE | 201 ++++++++++++++++++++++++++++++++++++++++ cmdapp/LICENSE-MIT | 25 +++++ cmdapp/src/config.rs | 77 +++++++++++---- cmdapp/src/converter.rs | 20 ++-- cmdapp/src/lib.rs | 14 ++- cmdapp/src/util.rs | 24 ----- webapp/Cargo.toml | 2 +- 9 files changed, 358 insertions(+), 75 deletions(-) create mode 100644 cmdapp/LICENSE-APACHE create mode 100644 cmdapp/LICENSE-MIT delete mode 100644 cmdapp/src/util.rs diff --git a/Readme.md b/Readme.md index b24bae7..e25aceb 100644 --- a/Readme.md +++ b/Readme.md @@ -10,12 +10,43 @@ Comparing to Adobe Illustrator's Live Trace, VTracer's output is much more compa A technical description of the algorithm is on [visioncortex.org/vtracer-docs](//www.visioncortex.org/vtracer-docs). -## Tech Stack +## Web App -VTracer and its [core library](//github.com/visioncortex/visioncortex) is implemented in [Rust](//www.rust-lang.org/). It provides us a solid foundation to develop robust and efficient algorithms and easily bring it to interactive applications. The supported target for now is WASM. It is interactive and fast, and is a perfect showcase of the capability of the Rust + HTML5 platform. We do plan to develop a command line program for VTracer. - -## Screenshots +VTracer and its [core library](//github.com/visioncortex/visioncortex) is implemented in [Rust](//www.rust-lang.org/). It provides us a solid foundation to develop robust and efficient algorithms and easily bring it to interactive applications. The webapp is a perfect showcase of the capability of the Rust + HTML5 platform.  - \ No newline at end of file + + +## Command Line +``` +visioncortex VTracer +A cmd app to convert images into vector graphics. + +USAGE: + vtracer [OPTIONS] --input <input> --output <output> + +FLAGS: + -h, --help Prints help information + -V, --version Prints version information + +OPTIONS: + --colormode <color_mode> True color image `color` (default) or Binary image `bw` + -p, --color_precision <color_precision> Number of significant bits to use in an RGB channel + -c, --corner_threshold <corner_threshold> Minimum momentary angle (degree) to be considered a corner + -f, --filter_speckle <filter_speckle> Discard patches smaller than X px in size + -g, --gradient_step <gradient_step> Color difference between gradient layers + -i, --input <input> Path to input raster image + -m, --mode <mode> Curver fitting mode `pixel`, `polygon`, `spline` + -o, --output <output> Path to output vector graphics + --preset <preset> Use one of the preset configs `bw`, `poster`, `photo` + -l, --segment_length <segment_length> + Perform iterative subdivide smooth until all segments are shorter than this length + + -s, --splice_threshold <splice_threshold> Minimum angle displacement (degree) to splice a spline +``` + +### Usage +``` +./vtracer --input input.jpg --output output.svg +``` diff --git a/cmdapp/Cargo.toml b/cmdapp/Cargo.toml index e1ee0f1..d82c48e 100644 --- a/cmdapp/Cargo.toml +++ b/cmdapp/Cargo.toml @@ -1,15 +1,16 @@ - [package] - name = "vtracer" - version = "0.1.0" - authors = ["Chris Tsang <tyt2y7@gmail.com>"] - edition = "2018" - description = "A cmd app to convert images into vector graphics." - homepage = "http://www.visioncortex.org/vtracer" - repository = "https://github.com/visioncortex/vtracer/" - categories = ["graphics"] - keywords = ["svg", "computer-graphics"] +[package] +name = "vtracer" +version = "0.1.0" +authors = ["Chris Tsang <tyt2y7@gmail.com>"] +edition = "2018" +description = "A cmd app to convert images into vector graphics." +license = "MIT OR Apache-2.0" +homepage = "http://www.visioncortex.org/vtracer" +repository = "https://github.com/visioncortex/vtracer/" +categories = ["graphics"] +keywords = ["svg", "computer-graphics"] - [dependencies] - clap = "2.33.3" - image = "0.23.10" - visioncortex = { path = "../visioncortex" } \ No newline at end of file +[dependencies] +clap = "2.33.3" +image = "0.23.10" +visioncortex = "0.2.0" \ No newline at end of file diff --git a/cmdapp/LICENSE-APACHE b/cmdapp/LICENSE-APACHE new file mode 100644 index 0000000..f8e5e5e --- /dev/null +++ b/cmdapp/LICENSE-APACHE @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + +TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + +1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + +2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + +3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + +4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + +5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + +6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + +7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + +8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + +9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + +END OF TERMS AND CONDITIONS + +APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + +Copyright [yyyy] [name of copyright owner] + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. \ No newline at end of file diff --git a/cmdapp/LICENSE-MIT b/cmdapp/LICENSE-MIT new file mode 100644 index 0000000..14bf0d7 --- /dev/null +++ b/cmdapp/LICENSE-MIT @@ -0,0 +1,25 @@ +Copyright (c) 2020 Tsang Hao Fung + +Permission is hereby granted, free of charge, to any +person obtaining a copy of this software and associated +documentation files (the "Software"), to deal in the +Software without restriction, including without +limitation the rights to use, copy, modify, merge, +publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software +is furnished to do so, subject to the following +conditions: + +The above copyright notice and this permission notice +shall be included in all copies or substantial portions +of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF +ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED +TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A +PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT +SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR +IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +DEALINGS IN THE SOFTWARE. \ No newline at end of file diff --git a/cmdapp/src/config.rs b/cmdapp/src/config.rs index ba26490..b3dad86 100644 --- a/cmdapp/src/config.rs +++ b/cmdapp/src/config.rs @@ -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 +} \ No newline at end of file diff --git a/cmdapp/src/converter.rs b/cmdapp/src/converter.rs index da01b94..32fda5d 100644 --- a/cmdapp/src/converter.rs +++ b/cmdapp/src/converter.rs @@ -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) { diff --git a/cmdapp/src/lib.rs b/cmdapp/src/lib.rs index 74a8e01..ba5e0bf 100644 --- a/cmdapp/src/lib.rs +++ b/cmdapp/src/lib.rs @@ -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::*; \ No newline at end of file +pub use svg::*; \ No newline at end of file diff --git a/cmdapp/src/util.rs b/cmdapp/src/util.rs deleted file mode 100644 index 06034c1..0000000 --- a/cmdapp/src/util.rs +++ /dev/null @@ -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 -} \ No newline at end of file diff --git a/webapp/Cargo.toml b/webapp/Cargo.toml index b33238e..50e3e63 100644 --- a/webapp/Cargo.toml +++ b/webapp/Cargo.toml @@ -21,7 +21,7 @@ console_log = { version = "0.2", features = ["color"] } wasm-bindgen = { version = "0.2", features = ["serde-serialize"] } serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" -visioncortex = { path = "../visioncortex" } +visioncortex = "0.2.0" # The `console_error_panic_hook` crate provides better debugging of panics by # logging them with `console.error`. This is great for development, but requires