Skip to content

Commit

Permalink
rust n swift
Browse files Browse the repository at this point in the history
  • Loading branch information
LingDong- committed Apr 22, 2020
1 parent 2a6a087 commit 94ba08b
Show file tree
Hide file tree
Showing 10 changed files with 1,044 additions and 2 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

*A new algorithm for retrieving topological skeleton as a set of polylines from binary images.*

[Available in all your favorite languages](#impl): C, C++, Java, JavaScript, Python, Go, C#/Unity, Processing, OpenFrameworks.
[Available in all your favorite languages](#impl): C, C++, Java, JavaScript, Python, Go, C#/Unity, Swift, Rust, Processing, OpenFrameworks.

**[[Online Demo](https://skeleton-tracing.netlify.app)]**

Expand Down Expand Up @@ -53,6 +53,8 @@ Click on links below to see each implementation's documentation and code.
- [**OpenFrameworks addon**](of) (friendly wrapper on C++ version)
- [**C#**](cs) (demo script for Unity Engine)
- [**Go**](go) (parallelized with goroutines)
- [**Swift**](swift) (demo with NSImage and AppKit)
- [**Rust**](rs) (simple rust implementation)


**Developed at [Frank-Ratchye STUDIO for Creative Inquiry](https://studioforcreativeinquiry.org) at Carnegie Mellon University.**
7 changes: 6 additions & 1 deletion go/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@ All source code is in `src/traceskeleton/traceskeleton.go`
export GO_PATH=$PWD; go build example.go
```

**Note:** This is my "Hello World" in Go, feel free to PR if you feel something could be done more idiomatically!
Test the example:

```
./example path/to/image.png
```

Output will be saved as `out.svg`.

**Developed at [Frank-Ratchye STUDIO for Creative Inquiry](https://studioforcreativeinquiry.org) at Carnegie Mellon University.**
47 changes: 47 additions & 0 deletions rs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# traceskeleton.rs

Rust version.


## Usage

```rust
mod trace_skeleton; pub use trace_skeleton::*;

let mut im : Vec<u8> = vec![0,1,0,1,0,...]; // image
let w = 100; // width
let h = 100; // height

//raster thinning
trace_skeleton::thinnning_zs(&mut im, w, h);

//trace skeleton to polylines
let p : Vec<Vec<[usize;2]>> = trace_skeleton::trace_skeleton(&mut im,w,h,0,0,w,h,10,999);

//render as svg string
let svg : String = trace_skeleton::polylines_to_svg(&p,w,h);

```


## Compile

First compile

```
rustc -O example.rs
```

Then run

```
./example img.txt
```

Output will be written to `out.svg`

- `-O` should be passed to the compiler to gain a ~100x speedup.
- `img.txt` is an text file filled with "0" and "1" representing a bitmap image.


**Developed at [Frank-Ratchye STUDIO for Creative Inquiry](https://studioforcreativeinquiry.org) at Carnegie Mellon University.**
Binary file added rs/example
Binary file not shown.
40 changes: 40 additions & 0 deletions rs/example.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
mod trace_skeleton; pub use trace_skeleton::*;

fn read_txt_as_image(path:String) -> (Vec<u8>,usize,usize){
let txt = std::fs::read_to_string(path).expect("Unable to read file");
let bts = txt.as_bytes();
let mut im : Vec<u8> = vec![];
let mut w : usize = 0;
let mut h : usize = 1;
for i in 0..bts.len(){
if bts[i] == 48{
im.push(0);
w+=1;
}else if bts[i] == 49{
im.push(1);
w+=1;
}else if bts[i] == 10{
h+=1;
w=0;
}
}
return (im,w,h);
}

fn main(){
let argv: Vec<_> = std::env::args().collect();
let (mut im,w,h) = read_txt_as_image(argv[1].clone());

trace_skeleton::thinning_zs(&mut im,w,h);

// for i in 0..h{for j in 0..w{print!("{}",im[i*w+j]);}println!();}

let now = std::time::Instant::now();
let p : Vec<Vec<[usize;2]>> = trace_skeleton::trace_skeleton(&mut im,w,h,0,0,w,h,10,999);
let elapsed = now.elapsed();
println!("Elapsed: {:.2?}", elapsed);

let svg : String = trace_skeleton::polylines_to_svg(&p,w,h);

std::fs::write("out.svg", svg).expect("Unable to write file");
}
149 changes: 149 additions & 0 deletions rs/img.txt

Large diffs are not rendered by default.

Loading

0 comments on commit 94ba08b

Please sign in to comment.