Selaa lähdekoodia

added the cli tool, a prgress estimation, and readme instructions

Hvrnbi 3 kuukautta sitten
vanhempi
sitoutus
aa67067c26
5 muutettua tiedostoa jossa 184 lisäystä ja 2 poistoa
  1. 142 0
      Cargo.lock
  2. 1 0
      Cargo.toml
  3. 16 1
      README.md
  4. 7 1
      src/lib.rs
  5. 18 0
      src/main.rs

+ 142 - 0
Cargo.lock

@@ -26,6 +26,56 @@ dependencies = [
  "equator",
 ]
 
+[[package]]
+name = "anstream"
+version = "1.0.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "824a212faf96e9acacdbd09febd34438f8f711fb84e09a8916013cd7815ca28d"
+dependencies = [
+ "anstyle",
+ "anstyle-parse",
+ "anstyle-query",
+ "anstyle-wincon",
+ "colorchoice",
+ "is_terminal_polyfill",
+ "utf8parse",
+]
+
+[[package]]
+name = "anstyle"
+version = "1.0.14"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "940b3a0ca603d1eade50a4846a2afffd5ef57a9feac2c0e2ec2e14f9ead76000"
+
+[[package]]
+name = "anstyle-parse"
+version = "1.0.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "52ce7f38b242319f7cabaa6813055467063ecdc9d355bbb4ce0c68908cd8130e"
+dependencies = [
+ "utf8parse",
+]
+
+[[package]]
+name = "anstyle-query"
+version = "1.1.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "40c48f72fd53cd289104fc64099abca73db4166ad86ea0b4341abe65af83dadc"
+dependencies = [
+ "windows-sys",
+]
+
+[[package]]
+name = "anstyle-wincon"
+version = "3.0.11"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "291e6a250ff86cd4a820112fb8898808a366d8f9f58ce16d1f538353ad55747d"
+dependencies = [
+ "anstyle",
+ "once_cell_polyfill",
+ "windows-sys",
+]
+
 [[package]]
 name = "anyhow"
 version = "1.0.103"
@@ -176,12 +226,58 @@ version = "1.0.4"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801"
 
+[[package]]
+name = "clap"
+version = "4.6.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "1ddb117e43bbf7dacf0a4190fef4d345b9bad68dfc649cb349e7d17d28428e51"
+dependencies = [
+ "clap_builder",
+ "clap_derive",
+]
+
+[[package]]
+name = "clap_builder"
+version = "4.6.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "714a53001bf66416adb0e2ef5ac857140e7dc3a0c48fb28b2f10762fc4b5069f"
+dependencies = [
+ "anstream",
+ "anstyle",
+ "clap_lex",
+ "strsim",
+]
+
+[[package]]
+name = "clap_derive"
+version = "4.6.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f2ce8604710f6733aa641a2b3731eaa1e8b3d9973d5e3565da11800813f997a9"
+dependencies = [
+ "heck",
+ "proc-macro2",
+ "quote",
+ "syn",
+]
+
+[[package]]
+name = "clap_lex"
+version = "1.1.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "c8d4a3bb8b1e0c1050499d1815f5ab16d04f0959b233085fb31653fbfc9d98f9"
+
 [[package]]
 name = "color_quant"
 version = "1.1.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "3d7b894f5411737b7867f4827955924d7c254fc9f4d91a6aad6b097804b1018b"
 
+[[package]]
+name = "colorchoice"
+version = "1.0.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "1d07550c9036bf2ae0c684c4297d503f838287c83c53686d05370d0e139ae570"
+
 [[package]]
 name = "crc32fast"
 version = "1.5.0"
@@ -343,6 +439,12 @@ dependencies = [
  "zerocopy",
 ]
 
+[[package]]
+name = "heck"
+version = "0.5.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
+
 [[package]]
 name = "image"
 version = "0.25.10"
@@ -394,6 +496,12 @@ dependencies = [
  "syn",
 ]
 
+[[package]]
+name = "is_terminal_polyfill"
+version = "1.70.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a6cb138bb79a146c1bd460005623e142ef0181e3d0219cb493e02f7d08a35695"
+
 [[package]]
 name = "itertools"
 version = "0.14.0"
@@ -572,10 +680,17 @@ version = "1.21.4"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50"
 
+[[package]]
+name = "once_cell_polyfill"
+version = "1.70.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe"
+
 [[package]]
 name = "p2sa"
 version = "0.1.0"
 dependencies = [
+ "clap",
  "edge-detection",
  "fastrand",
  "image",
@@ -823,6 +938,12 @@ version = "1.2.1"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "6ce2be8dc25455e1f91df71bfa12ad37d7af1092ae736f3a6cd0e37bc7810596"
 
+[[package]]
+name = "strsim"
+version = "0.11.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f"
+
 [[package]]
 name = "syn"
 version = "2.0.118"
@@ -874,6 +995,12 @@ version = "1.0.24"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75"
 
+[[package]]
+name = "utf8parse"
+version = "0.2.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821"
+
 [[package]]
 name = "v_frame"
 version = "0.3.9"
@@ -945,6 +1072,21 @@ version = "0.1.12"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "a28ac98ddc8b9274cb41bb4d9d4d5c425b6020c50c46f25559911905610b4a88"
 
+[[package]]
+name = "windows-link"
+version = "0.2.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5"
+
+[[package]]
+name = "windows-sys"
+version = "0.61.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc"
+dependencies = [
+ "windows-link",
+]
+
 [[package]]
 name = "wit-bindgen"
 version = "0.57.1"

+ 1 - 0
Cargo.toml

@@ -4,6 +4,7 @@ version = "0.1.0"
 edition = "2024"
 
 [dependencies]
+clap = { version = "4.6.1", features = ["derive"] }
 edge-detection = "0.3.0"
 fastrand = "2.4.1"
 image = "0.25.10"

+ 16 - 1
README.md

@@ -7,7 +7,15 @@ This is a Rust rewriting of a [project i did for school](https://github.com/Hvrn
 
 # CLI tool installation
 
-*Coming soon*
+The easiest way to install p2as is to use [cargo](https://doc.rust-lang.org/cargo/index.html) with this command :
+```shell
+cargo install p2sa
+```
+
+You can now use it in your terminal with this command :
+```shell
+p2sa your-src-image-path output-path output-width output-height nails-count
+```
 
 # Library usage
 
@@ -24,6 +32,7 @@ You can now call the p2sa function :
 use p2sa::p2sa;
 
 fn main() {
+    // p2sa(src_path: String, output_path: String, output_size: [u32; 2], nails_count: u32);
     p2sa("path/to/your/picture.png".to_string(), "path/to/your/result.png".to_string(), [1024, 1024], 400);
 }
 ```
@@ -43,6 +52,12 @@ It chooses the line that reduces the difference the most, and repeat this steps
 For more details, you can check [the wiki page of my school project](http://os-vps418.infomaniak.ch:1250/mediawiki/index.php/Filage_d'image_et_trac%C3%A9_de_droites_pour_un_rendu_artistique).
 It's in french but may be translated.
 
+# Usage advices
+
+If the output lacks details, you may want to increase the nails count.
+It may take some time (a few minutes) if you try to obtain an image of more than 2000x2000 px and more than 600 nails.
+You can obtain nice results with smaller resolutions.
+
 # Thanks
 
 This crate relies heavily on the rust crates [image](https://crates.io/crates/image) and [edge-detection](https://crates.io/crates/edge-detection).

+ 7 - 1
src/lib.rs

@@ -241,11 +241,17 @@ pub fn p2sa(src_path: String, output_path: String, new_size: [u32; 2], nails_cou
     let mut err_rising_cnt: u8 = 0;
     let mut lines_cnt: u16 = 0;
 
+    let mut first_err_red: f32 = 0.;
+
     while err_rising_cnt < 3 {
         let best_nail_and_err_red: ([u32; 2], f32) = find_best_line(&src_img, &new_img, start, nails.clone());
         let best_nail: [u32; 2] = best_nail_and_err_red.0;
+
+        if lines_cnt == 0 {
+            first_err_red = best_nail_and_err_red.1;
+        }
         
-        println!("Error reduction : {}", best_nail_and_err_red.1);
+        println!("Progress estimation : {} %", (best_nail_and_err_red.1 - first_err_red) * 100. / - first_err_red);
 
         if best_nail_and_err_red.1 >= 0. {
             err_rising_cnt += 1;

+ 18 - 0
src/main.rs

@@ -0,0 +1,18 @@
+mod lib;
+
+use clap::Parser;
+
+#[derive(Parser)]
+struct Args {
+    src_path: String,
+    output_path: String,
+    output_width: u32,
+    output_height: u32,
+    nails_count: u16
+}
+
+fn main() {
+    let args = Args::parse();
+
+    lib::p2sa(args.src_path, args.output_path, [args.output_width, args.output_height], args.nails_count);
+}